static HRESULT Number_toFixed(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { NumberInstance *number; DOUBLE val; INT prec = 0; jsstr_t *str; HRESULT hres; TRACE("\n"); if(!(number = number_this(jsthis))) return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL); if(argc) { hres = to_int32(ctx, argv[0], &prec); if(FAILED(hres)) return hres; if(prec<0 || prec>20) return throw_range_error(ctx, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL); } val = number->value; if(!is_finite(val)) { hres = to_string(ctx, jsval_number(val), &str); if(FAILED(hres)) return hres; }else { str = number_to_fixed(val, prec); if(!str) return E_OUTOFMEMORY; } if(r) *r = jsval_string(str); else jsstr_release(str); return S_OK; }
static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { FunctionInstance *function; jsstr_t *str; HRESULT hres; TRACE("\n"); if(!(function = function_this(jsthis))) return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL); hres = function_to_string(function, &str); if(FAILED(hres)) return hres; if(r) *r = jsval_string(str); else jsstr_release(str); return S_OK; }
static HRESULT Enumerator_moveNext(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { EnumeratorInstance *This; HRESULT hres = S_OK; TRACE("\n"); if (!(This = enumerator_this(jsthis))) return throw_type_error(ctx, JS_E_ENUMERATOR_EXPECTED, NULL); if (This->enumvar) { hres = enumvar_get_next_item(This); if (FAILED(hres)) return hres; } if (r) *r = jsval_undefined(); return S_OK; }
static HRESULT Function_call(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller) { FunctionInstance *function; DISPPARAMS args = {NULL,NULL,0,0}; IDispatch *this_obj = NULL; DWORD argc; HRESULT hres; TRACE("\n"); if(!(function = function_this(jsthis))) return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL); argc = arg_cnt(dp); if(argc) { VARIANT *v = get_arg(dp,0); if(V_VT(v) != VT_EMPTY && V_VT(v) != VT_NULL) { hres = to_object(ctx, v, &this_obj); if(FAILED(hres)) return hres; } args.cArgs = argc-1; } if(args.cArgs) args.rgvarg = dp->rgvarg + dp->cArgs - args.cArgs-1; hres = call_function(ctx, function, this_obj, &args, retv, ei, caller); if(this_obj) IDispatch_Release(this_obj); return hres; }
static HRESULT Function_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) { FunctionInstance *function; BSTR str; HRESULT hres; TRACE("\n"); if(!(function = function_this(jsthis))) return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL); hres = function_to_string(function, &str); if(FAILED(hres)) return hres; if(retv) { V_VT(retv) = VT_BSTR; V_BSTR(retv) = str; }else { SysFreeString(str); } return S_OK; }
/* ECMA-262 3rd Edition 15.7.4.2 */ static HRESULT Number_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { NumberInstance *number; INT radix = 10; DOUBLE val; jsstr_t *str; HRESULT hres; TRACE("\n"); if(!(number = number_this(jsthis))) return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL); if(argc) { hres = to_int32(ctx, argv[0], &radix); if(FAILED(hres)) return hres; if(radix<2 || radix>36) return throw_type_error(ctx, JS_E_INVALIDARG, NULL); } val = number->value; if(radix==10 || isnan(val) || isinf(val)) { hres = to_string(ctx, jsval_number(val), &str); if(FAILED(hres)) return hres; } else { INT idx = 0; DOUBLE integ, frac, log_radix = 0; WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16]; BOOL exp = FALSE; if(val<0) { val = -val; buf[idx++] = '-'; } while(1) { integ = floor(val); frac = val-integ; if(integ == 0) buf[idx++] = '0'; while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) { buf[idx] = fmod(integ, radix); if(buf[idx]<10) buf[idx] += '0'; else buf[idx] += 'a'-10; integ /= radix; idx++; } if(idx<NUMBER_TOSTRING_BUF_SIZE) { INT beg = buf[0]=='-'?1:0; INT end = idx-1; WCHAR wch; while(end > beg) { wch = buf[beg]; buf[beg++] = buf[end]; buf[end--] = wch; } } if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.'; while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) { frac *= radix; buf[idx] = fmod(frac, radix); frac -= buf[idx]; if(buf[idx]<10) buf[idx] += '0'; else buf[idx] += 'a'-10; idx++; } if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) { exp = TRUE; idx = (buf[0]=='-') ? 1 : 0; log_radix = floor(log(val)/log(radix)); val *= pow(radix, -log_radix); continue; } break; } while(buf[idx-1] == '0') idx--; if(buf[idx-1] == '.') idx--; if(exp) { if(log_radix==0) buf[idx] = 0; else { static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0}; WCHAR ch; if(log_radix<0) { log_radix = -log_radix; ch = '-'; } else ch = '+'; sprintfW(&buf[idx], formatW, ch, (int)log_radix); } } else buf[idx] = '\0'; str = jsstr_alloc(buf); if(!str) return E_OUTOFMEMORY; } if(r) *r = jsval_string(str); else jsstr_release(str); return S_OK; }
/* ECMA-262 3rd Edition 15.7.4.2 */ static HRESULT Number_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) { NumberInstance *number; INT radix = 10; DOUBLE val; BSTR str; HRESULT hres; TRACE("\n"); if(!(number = number_this(jsthis))) return throw_type_error(ctx, ei, IDS_NOT_NUM, NULL); if(arg_cnt(dp)) { hres = to_int32(ctx, get_arg(dp, 0), ei, &radix); if(FAILED(hres)) return hres; if(radix<2 || radix>36) return throw_type_error(ctx, ei, IDS_INVALID_CALL_ARG, NULL); } if(V_VT(&number->num) == VT_I4) val = V_I4(&number->num); else val = V_R8(&number->num); if(radix==10 || isnan(val) || isinf(val)) { hres = to_string(ctx, &number->num, ei, &str); if(FAILED(hres)) return hres; } else { INT idx = 0; DOUBLE integ, frac, log_radix = 0; WCHAR buf[NUMBER_TOSTRING_BUF_SIZE+16]; BOOL exp = FALSE; if(val<0) { val = -val; buf[idx++] = '-'; } while(1) { integ = floor(val); frac = val-integ; if(integ == 0) buf[idx++] = '0'; while(integ>=1 && idx<NUMBER_TOSTRING_BUF_SIZE) { buf[idx] = fmod(integ, radix); if(buf[idx]<10) buf[idx] += '0'; else buf[idx] += 'a'-10; integ /= radix; idx++; } if(idx<NUMBER_TOSTRING_BUF_SIZE) { INT beg = buf[0]=='-'?1:0; INT end = idx-1; WCHAR wch; while(end > beg) { wch = buf[beg]; buf[beg++] = buf[end]; buf[end--] = wch; } } if(idx != NUMBER_TOSTRING_BUF_SIZE) buf[idx++] = '.'; while(frac>0 && idx<NUMBER_TOSTRING_BUF_SIZE) { frac *= radix; buf[idx] = fmod(frac, radix); frac -= buf[idx]; if(buf[idx]<10) buf[idx] += '0'; else buf[idx] += 'a'-10; idx++; } if(idx==NUMBER_TOSTRING_BUF_SIZE && !exp) { exp = TRUE; idx = (buf[0]=='-') ? 1 : 0; log_radix = floor(log(val)/log(radix)); val *= pow(radix, -log_radix); continue; } break; } while(buf[idx-1] == '0') idx--; if(buf[idx-1] == '.') idx--; if(exp) { if(log_radix==0) buf[idx++] = '\0'; else { static const WCHAR formatW[] = {'(','e','%','c','%','d',')',0}; WCHAR ch; if(log_radix<0) { log_radix = -log_radix; ch = '-'; } else ch = '+'; sprintfW(&buf[idx], formatW, ch, (int)log_radix); } } else buf[idx] = '\0'; str = SysAllocString(buf); if(!str) return E_OUTOFMEMORY; } if(retv) { V_VT(retv) = VT_BSTR; V_BSTR(retv) = str; }else { SysFreeString(str); } return S_OK; }
static HRESULT Function_apply(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { FunctionInstance *function; jsval_t *args = NULL; unsigned i, cnt = 0; IDispatch *this_obj = NULL; HRESULT hres = S_OK; TRACE("\n"); if(!(function = function_this(jsthis)) && (jsthis->flags & VDISP_JSDISP)) return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL); if(argc) { if(!is_undefined(argv[0]) && !is_null(argv[0])) { hres = to_object(ctx, argv[0], &this_obj); if(FAILED(hres)) return hres; } } if(argc >= 2) { jsdisp_t *arg_array = NULL; if(is_object_instance(argv[1])) { arg_array = iface_to_jsdisp(get_object(argv[1])); if(arg_array && (!is_class(arg_array, JSCLASS_ARRAY) && !is_class(arg_array, JSCLASS_ARGUMENTS) )) { jsdisp_release(arg_array); arg_array = NULL; } } if(arg_array) { hres = array_to_args(ctx, arg_array, &cnt, &args); jsdisp_release(arg_array); }else { FIXME("throw TypeError\n"); hres = E_FAIL; } } if(SUCCEEDED(hres)) { if(function) { hres = call_function(ctx, function, this_obj, cnt, args, (flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE) != 0, r); }else { jsval_t res; hres = disp_call_value(ctx, jsthis->u.disp, this_obj, DISPATCH_METHOD, cnt, args, &res); if(SUCCEEDED(hres)) { if(r) *r = res; else jsval_release(res); } } } if(this_obj) IDispatch_Release(this_obj); for(i=0; i < cnt; i++) jsval_release(args[i]); heap_free(args); return hres; }
/* ECMA-262 3rd Edition 9.9 */ HRESULT to_object(script_ctx_t *ctx, jsval_t val, IDispatch **disp) { jsdisp_t *dispex; HRESULT hres; switch(jsval_type(val)) { case JSV_STRING: hres = create_string(ctx, get_string(val), &dispex); if(FAILED(hres)) return hres; *disp = to_disp(dispex); break; case JSV_NUMBER: hres = create_number(ctx, get_number(val), &dispex); if(FAILED(hres)) return hres; *disp = to_disp(dispex); break; case JSV_OBJECT: if(get_object(val)) { *disp = get_object(val); IDispatch_AddRef(*disp); }else { jsdisp_t *obj; hres = create_object(ctx, NULL, &obj); if(FAILED(hres)) return hres; *disp = to_disp(obj); } break; case JSV_BOOL: hres = create_bool(ctx, get_bool(val), &dispex); if(FAILED(hres)) return hres; *disp = to_disp(dispex); break; case JSV_UNDEFINED: case JSV_NULL: WARN("object expected\n"); return throw_type_error(ctx, JS_E_OBJECT_EXPECTED, NULL); case JSV_VARIANT: switch(V_VT(get_variant(val))) { case VT_ARRAY|VT_VARIANT: hres = create_vbarray(ctx, V_ARRAY(get_variant(val)), &dispex); if(FAILED(hres)) return hres; *disp = to_disp(dispex); break; default: FIXME("Unsupported %s\n", debugstr_variant(get_variant(val))); return E_NOTIMPL; } break; } return S_OK; }
/* ECMA-262 3rd Edition 9.1 */ HRESULT to_primitive(script_ctx_t *ctx, jsval_t val, jsval_t *ret, hint_t hint) { if(is_object_instance(val)) { jsdisp_t *jsdisp; jsval_t prim; DISPID id; HRESULT hres; static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0}; static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0}; if(!get_object(val)) { *ret = jsval_null(); return S_OK; } jsdisp = iface_to_jsdisp((IUnknown*)get_object(val)); if(!jsdisp) return disp_propget(ctx, get_object(val), DISPID_VALUE, ret); if(hint == NO_HINT) hint = is_class(jsdisp, JSCLASS_DATE) ? HINT_STRING : HINT_NUMBER; /* Native implementation doesn't throw TypeErrors, returns strange values */ hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? toStringW : valueOfW, 0, &id); if(SUCCEEDED(hres)) { hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim); if(FAILED(hres)) { WARN("call error - forwarding exception\n"); jsdisp_release(jsdisp); return hres; }else if(!is_object_instance(prim)) { jsdisp_release(jsdisp); *ret = prim; return S_OK; }else { IDispatch_Release(get_object(prim)); } } hres = jsdisp_get_id(jsdisp, hint == HINT_STRING ? valueOfW : toStringW, 0, &id); if(SUCCEEDED(hres)) { hres = jsdisp_call(jsdisp, id, DISPATCH_METHOD, 0, NULL, &prim); if(FAILED(hres)) { WARN("call error - forwarding exception\n"); jsdisp_release(jsdisp); return hres; }else if(!is_object_instance(prim)) { jsdisp_release(jsdisp); *ret = prim; return S_OK; }else { IDispatch_Release(get_object(prim)); } } jsdisp_release(jsdisp); WARN("failed\n"); return throw_type_error(ctx, JS_E_TO_PRIMITIVE, NULL); } return jsval_copy(val, ret); }