diff --git a/dlls/jscript/Makefile.in b/dlls/jscript/Makefile.in index 3a32a299036..b7405462463 100644 --- a/dlls/jscript/Makefile.in +++ b/dlls/jscript/Makefile.in @@ -13,6 +13,7 @@ C_SRCS = \ date.c \ dispex.c \ engine.c \ + error.c \ function.c \ global.c \ jscript.c \ diff --git a/dlls/jscript/error.c b/dlls/jscript/error.c new file mode 100644 index 00000000000..40228d4a82e --- /dev/null +++ b/dlls/jscript/error.c @@ -0,0 +1,310 @@ +/* + * Copyright 2009 Piotr Caban + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "jscript.h" + +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(jscript); + +typedef struct { + DispatchEx dispex; + + VARIANT message; +} ErrorInstance; + +static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0}; +static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0}; +static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0}; +static const WCHAR propertyIsEnumerableW[] = + {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0}; +static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0}; + +/* ECMA-262 3rd Edition 15.11.4.3 */ +static HRESULT Error_message(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + FIXME("\n"); + return E_NOTIMPL; +} + +static HRESULT Error_toString(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + FIXME("\n"); + return E_NOTIMPL; +} + +static HRESULT Error_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + FIXME("\n"); + return E_NOTIMPL; +} + +static HRESULT Error_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + FIXME("\n"); + return E_NOTIMPL; +} + + +static HRESULT Error_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + FIXME("\n"); + return E_NOTIMPL; +} + +static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + FIXME("\n"); + return E_NOTIMPL; +} + +static void Error_destructor(DispatchEx *dispex) +{ + ErrorInstance *This = (ErrorInstance*)dispex; + + VariantClear(&This->message); + heap_free(This); +} + +static const builtin_prop_t Error_props[] = { + {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD}, + {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD}, + {messageW, Error_message, 0}, + {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD}, + {toStringW, Error_toString, PROPF_METHOD} +}; + +static const builtin_info_t Error_info = { + JSCLASS_ERROR, + {NULL, Error_value, 0}, + sizeof(Error_props)/sizeof(*Error_props), + Error_props, + Error_destructor, + NULL +}; + +static const builtin_prop_t ErrorInst_props[] = { + {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD}, + {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD}, + {messageW, Error_message, 0}, + {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD} +}; + +static const builtin_info_t ErrorInst_info = { + JSCLASS_ERROR, + {NULL, Error_value, 0}, + sizeof(ErrorInst_props)/sizeof(*ErrorInst_props), + ErrorInst_props, + Error_destructor, + NULL +}; + +static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype, + DispatchEx *constr, ErrorInstance **ret) +{ + ErrorInstance *err; + DispatchEx *inherit; + HRESULT hres; + + err = heap_alloc_zero(sizeof(ErrorInstance)); + if(!err) + return E_OUTOFMEMORY; + + inherit = error_prototype ? ctx->object_constr : ctx->error_constr; + hres = init_dispex_from_constr(&err->dispex, ctx, + error_prototype ? &Error_info : &ErrorInst_info, + constr ? constr : inherit); + if(FAILED(hres)) { + heap_free(err); + return hres; + } + + *ret = err; + return S_OK; +} + +static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr, + const WCHAR *msg, DispatchEx **ret) +{ + ErrorInstance *err; + HRESULT hres; + + hres = alloc_error(ctx, FALSE, constr, &err); + if(FAILED(hres)) + return hres; + + V_VT(&err->message) = VT_BSTR; + if(msg) V_BSTR(&err->message) = SysAllocString(msg); + else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0); + + if(!V_BSTR(&err->message)) { + heap_free(err); + return E_OUTOFMEMORY; + } + + *ret = &err->dispex; + return S_OK; +} + +static HRESULT error_constr(DispatchEx *dispex, WORD flags, DISPPARAMS *dp, + VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) { + DispatchEx *err; + BSTR msg = NULL; + HRESULT hres; + + if(arg_cnt(dp)) { + hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &msg); + if(FAILED(hres)) + return hres; + } + + switch(flags) { + case INVOKE_FUNC: + case DISPATCH_CONSTRUCT: + hres = create_error(dispex->ctx, constr, msg, &err); + if(FAILED(hres)) + return hres; + + if(retv) { + V_VT(retv) = VT_DISPATCH; + V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err); + } + else + IDispatchEx_Release(_IDispatchEx_(err)); + + return S_OK; + + default: + FIXME("unimplemented flags %x\n", flags); + return E_NOTIMPL; + } +} + +static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + return error_constr(dispex, flags, dp, retv, ei, + dispex->ctx->error_constr); +} + +static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + return error_constr(dispex, flags, dp, retv, ei, + dispex->ctx->eval_error_constr); +} + +static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + return error_constr(dispex, flags, dp, retv, ei, + dispex->ctx->range_error_constr); +} + +static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + return error_constr(dispex, flags, dp, retv, ei, + dispex->ctx->reference_error_constr); +} + +static HRESULT SyntaxErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + return error_constr(dispex, flags, dp, retv, ei, + dispex->ctx->syntax_error_constr); +} + +static HRESULT TypeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + return error_constr(dispex, flags, dp, retv, ei, + dispex->ctx->type_error_constr); +} + +static HRESULT URIErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, + DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + return error_constr(dispex, flags, dp, retv, ei, + dispex->ctx->uri_error_constr); +} + +HRESULT init_error_constr(script_ctx_t *ctx) +{ + static const WCHAR nameW[] = {'n','a','m','e',0}; + static const WCHAR ErrorW[] = {'E','r','r','o','r',0}; + static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0}; + static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0}; + static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0}; + static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0}; + static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0}; + static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0}; + static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW, + ReferenceErrorW, SyntaxErrorW, TypeErrorW, URIErrorW}; + DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr, + &ctx->range_error_constr, &ctx->reference_error_constr, + &ctx->syntax_error_constr, &ctx->type_error_constr, + &ctx->uri_error_constr}; + static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value, + RangeErrorConstr_value, ReferenceErrorConstr_value, SyntaxErrorConstr_value, + TypeErrorConstr_value, URIErrorConstr_value}; + + ErrorInstance *err; + INT i; + VARIANT v; + HRESULT hres; + + for(i=0; i<7; i++) { + hres = alloc_error(ctx, i==0, NULL, &err); + if(FAILED(hres)) + return hres; + + V_VT(&v) = VT_BSTR; + V_BSTR(&v) = SysAllocString(names[i]); + if(!V_BSTR(&v)) { + IDispatchEx_Release(_IDispatchEx_(&err->dispex)); + return E_OUTOFMEMORY; + } + + hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/); + + if(SUCCEEDED(hres)) + hres = create_builtin_function(ctx, constr_val[i], NULL, + PROPF_CONSTR, &err->dispex, constr_addr[i]); + + IDispatchEx_Release(_IDispatchEx_(&err->dispex)); + VariantClear(&v); + if(FAILED(hres)) + return hres; + } + + return S_OK; +} diff --git a/dlls/jscript/global.c b/dlls/jscript/global.c index 9428e6578e2..1ec113d9757 100644 --- a/dlls/jscript/global.c +++ b/dlls/jscript/global.c @@ -33,6 +33,13 @@ static const WCHAR InfinityW[] = {'I','n','f','i','n','i','t','y',0}; static const WCHAR ArrayW[] = {'A','r','r','a','y',0}; static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0}; static const WCHAR DateW[] = {'D','a','t','e',0}; +static const WCHAR ErrorW[] = {'E','r','r','o','r',0}; +static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0}; +static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0}; +static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0}; +static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0}; +static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0}; +static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0}; static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0}; static const WCHAR NumberW[] = {'N','u','m','b','e','r',0}; static const WCHAR ObjectW[] = {'O','b','j','e','c','t',0}; @@ -165,6 +172,62 @@ static HRESULT JSGlobal_Date(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARA return constructor_call(dispex->ctx->date_constr, lcid, flags, dp, retv, ei, sp); } +static HRESULT JSGlobal_Error(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, + VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + + return constructor_call(dispex->ctx->error_constr, lcid, flags, dp, retv, ei, sp); +} + +static HRESULT JSGlobal_EvalError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, + VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + + return constructor_call(dispex->ctx->eval_error_constr, lcid, flags, dp, retv, ei, sp); +} + +static HRESULT JSGlobal_RangeError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, + VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + + return constructor_call(dispex->ctx->range_error_constr, lcid, flags, dp, retv, ei, sp); +} + +static HRESULT JSGlobal_ReferenceError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, + VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + + return constructor_call(dispex->ctx->reference_error_constr, lcid, flags, dp, retv, ei, sp); +} + +static HRESULT JSGlobal_SyntaxError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, + VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + + return constructor_call(dispex->ctx->syntax_error_constr, lcid, flags, dp, retv, ei, sp); +} + +static HRESULT JSGlobal_TypeError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, + VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + + return constructor_call(dispex->ctx->type_error_constr, lcid, flags, dp, retv, ei, sp); +} + +static HRESULT JSGlobal_URIError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, + VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) +{ + TRACE("\n"); + + return constructor_call(dispex->ctx->uri_error_constr, lcid, flags, dp, retv, ei, sp); +} + static HRESULT JSGlobal_Function(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) { @@ -541,6 +604,8 @@ static const builtin_prop_t JSGlobal_props[] = { {CollectGarbageW, JSGlobal_CollectGarbage, PROPF_METHOD}, {DateW, JSGlobal_Date, PROPF_CONSTR}, {EnumeratorW, JSGlobal_Enumerator, PROPF_METHOD}, + {ErrorW, JSGlobal_Error, PROPF_CONSTR}, + {EvalErrorW, JSGlobal_EvalError, PROPF_CONSTR}, {FunctionW, JSGlobal_Function, PROPF_CONSTR}, {_GetObjectW, JSGlobal_GetObject, PROPF_METHOD}, {InfinityW, JSGlobal_Infinity, 0}, @@ -548,12 +613,17 @@ static const builtin_prop_t JSGlobal_props[] = { {NaNW, JSGlobal_NaN, 0}, {NumberW, JSGlobal_Number, PROPF_CONSTR}, {ObjectW, JSGlobal_Object, PROPF_CONSTR}, + {RangeErrorW, JSGlobal_RangeError, PROPF_CONSTR}, + {ReferenceErrorW, JSGlobal_ReferenceError, PROPF_CONSTR}, {RegExpW, JSGlobal_RegExp, PROPF_CONSTR}, {ScriptEngineW, JSGlobal_ScriptEngine, PROPF_METHOD}, {ScriptEngineBuildVersionW, JSGlobal_ScriptEngineBuildVersion, PROPF_METHOD}, {ScriptEngineMajorVersionW, JSGlobal_ScriptEngineMajorVersion, PROPF_METHOD}, {ScriptEngineMinorVersionW, JSGlobal_ScriptEngineMinorVersion, PROPF_METHOD}, {StringW, JSGlobal_String, PROPF_CONSTR}, + {SyntaxErrorW, JSGlobal_SyntaxError, PROPF_CONSTR}, + {TypeErrorW, JSGlobal_TypeError, PROPF_CONSTR}, + {URIErrorW, JSGlobal_URIError, PROPF_CONSTR}, {VBArrayW, JSGlobal_VBArray, PROPF_METHOD}, {encodeURIW, JSGlobal_encodeURI, PROPF_METHOD}, {escapeW, JSGlobal_escape, PROPF_METHOD}, @@ -598,6 +668,10 @@ static HRESULT init_constructors(script_ctx_t *ctx, DispatchEx *object_prototype if(FAILED(hres)) return hres; + hres = init_error_constr(ctx); + if(FAILED(hres)) + return hres; + hres = create_number_constr(ctx, &ctx->number_constr); if(FAILED(hres)) return hres; diff --git a/dlls/jscript/jscript.h b/dlls/jscript/jscript.h index 370b3bdccdd..14903e9a179 100644 --- a/dlls/jscript/jscript.h +++ b/dlls/jscript/jscript.h @@ -188,6 +188,13 @@ struct _script_ctx_t { DispatchEx *array_constr; DispatchEx *bool_constr; DispatchEx *date_constr; + DispatchEx *error_constr; + DispatchEx *eval_error_constr; + DispatchEx *range_error_constr; + DispatchEx *reference_error_constr; + DispatchEx *syntax_error_constr; + DispatchEx *type_error_constr; + DispatchEx *uri_error_constr; DispatchEx *number_constr; DispatchEx *object_constr; DispatchEx *regexp_constr; @@ -208,6 +215,7 @@ HRESULT create_object_prototype(script_ctx_t*,DispatchEx**); HRESULT create_array_constr(script_ctx_t*,DispatchEx**); HRESULT create_bool_constr(script_ctx_t*,DispatchEx**); HRESULT create_date_constr(script_ctx_t*,DispatchEx**); +HRESULT init_error_constr(script_ctx_t*); HRESULT create_number_constr(script_ctx_t*,DispatchEx**); HRESULT create_object_constr(script_ctx_t*,DispatchEx*,DispatchEx**); HRESULT create_regexp_constr(script_ctx_t*,DispatchEx**); diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js index a80bd9f6d50..a82931e0870 100644 --- a/dlls/jscript/tests/api.js +++ b/dlls/jscript/tests/api.js @@ -1244,4 +1244,31 @@ ok(bool.toString() === "true", "bool.toString() = " + bool.toString()); ok(bool.valueOf() === Boolean(1), "bool.valueOf() = " + bool.valueOf()); ok(bool.toLocaleString() === bool.toString(), "bool.toLocaleString() = " + bool.toLocaleString()); +ok(Error.prototype !== TypeError.prototype, "Error.prototype === TypeError.prototype"); +ok(RangeError.prototype !== TypeError.prototype, "RangeError.prototype === TypeError.prototype"); +ok(Error.prototype.toLocaleString === Object.prototype.toLocaleString, + "Error.prototype.toLocaleString !== Object.prototype.toLocaleString"); +err = new Error(); +ok(err.valueOf === Object.prototype.valueOf, "err.valueOf !== Object.prototype.valueOf"); +ok(Error.prototype.name === "Error", "Error.prototype.name = " + Error.prototype.name); +ok(err.name === "Error", "err.name = " + err.name); +err = new EvalError(); +ok(EvalError.prototype.name === "EvalError", "EvalError.prototype.name = " + EvalError.prototype.name); +ok(err.name === "EvalError", "err.name = " + err.name); +err = new RangeError(); +ok(RangeError.prototype.name === "RangeError", "RangeError.prototype.name = " + RangeError.prototype.name); +ok(err.name === "RangeError", "err.name = " + err.name); +err = new ReferenceError(); +ok(ReferenceError.prototype.name === "ReferenceError", "ReferenceError.prototype.name = " + ReferenceError.prototype.name); +ok(err.name === "ReferenceError", "err.name = " + err.name); +err = new SyntaxError(); +ok(SyntaxError.prototype.name === "SyntaxError", "SyntaxError.prototype.name = " + SyntaxError.prototype.name); +ok(err.name === "SyntaxError", "err.name = " + err.name); +err = new TypeError(); +ok(TypeError.prototype.name === "TypeError", "TypeError.prototype.name = " + TypeError.prototype.name); +ok(err.name === "TypeError", "err.name = " + err.name); +err = new URIError(); +ok(URIError.prototype.name === "URIError", "URIError.prototype.name = " + URIError.prototype.name); +ok(err.name === "URIError", "err.name = " + err.name); + reportSuccess();