2 * Copyright 2009 Piotr Caban
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
32 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
33 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
34 static const WCHAR propertyIsEnumerableW[] =
35 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
36 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
38 /* ECMA-262 3rd Edition 15.11.4.3 */
39 static HRESULT Error_message(DispatchEx *dispex, LCID lcid, WORD flags,
40 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
46 static HRESULT Error_toString(DispatchEx *dispex, LCID lcid, WORD flags,
47 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
53 static HRESULT Error_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags,
54 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
60 static HRESULT Error_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags,
61 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
68 static HRESULT Error_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags,
69 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
75 static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags,
76 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
82 static void Error_destructor(DispatchEx *dispex)
84 ErrorInstance *This = (ErrorInstance*)dispex;
86 VariantClear(&This->message);
90 static const builtin_prop_t Error_props[] = {
91 {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD},
92 {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD},
93 {messageW, Error_message, 0},
94 {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD},
95 {toStringW, Error_toString, PROPF_METHOD}
98 static const builtin_info_t Error_info = {
100 {NULL, Error_value, 0},
101 sizeof(Error_props)/sizeof(*Error_props),
107 static const builtin_prop_t ErrorInst_props[] = {
108 {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD},
109 {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD},
110 {messageW, Error_message, 0},
111 {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD}
114 static const builtin_info_t ErrorInst_info = {
116 {NULL, Error_value, 0},
117 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
123 static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype,
124 DispatchEx *constr, ErrorInstance **ret)
130 err = heap_alloc_zero(sizeof(ErrorInstance));
132 return E_OUTOFMEMORY;
134 inherit = error_prototype ? ctx->object_constr : ctx->error_constr;
135 hres = init_dispex_from_constr(&err->dispex, ctx,
136 error_prototype ? &Error_info : &ErrorInst_info,
137 constr ? constr : inherit);
147 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
148 const WCHAR *msg, DispatchEx **ret)
153 hres = alloc_error(ctx, FALSE, constr, &err);
157 V_VT(&err->message) = VT_BSTR;
158 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
159 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
161 if(!V_BSTR(&err->message)) {
163 return E_OUTOFMEMORY;
170 static HRESULT error_constr(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
171 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
177 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &msg);
184 case DISPATCH_CONSTRUCT:
185 hres = create_error(dispex->ctx, constr, msg, &err);
190 V_VT(retv) = VT_DISPATCH;
191 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
194 IDispatchEx_Release(_IDispatchEx_(err));
199 FIXME("unimplemented flags %x\n", flags);
204 static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
205 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
208 return error_constr(dispex, flags, dp, retv, ei,
209 dispex->ctx->error_constr);
212 static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
213 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
216 return error_constr(dispex, flags, dp, retv, ei,
217 dispex->ctx->eval_error_constr);
220 static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
221 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
224 return error_constr(dispex, flags, dp, retv, ei,
225 dispex->ctx->range_error_constr);
228 static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
229 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
232 return error_constr(dispex, flags, dp, retv, ei,
233 dispex->ctx->reference_error_constr);
236 static HRESULT SyntaxErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
237 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
240 return error_constr(dispex, flags, dp, retv, ei,
241 dispex->ctx->syntax_error_constr);
244 static HRESULT TypeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
245 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
248 return error_constr(dispex, flags, dp, retv, ei,
249 dispex->ctx->type_error_constr);
252 static HRESULT URIErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
253 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
256 return error_constr(dispex, flags, dp, retv, ei,
257 dispex->ctx->uri_error_constr);
260 HRESULT init_error_constr(script_ctx_t *ctx)
262 static const WCHAR nameW[] = {'n','a','m','e',0};
263 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
264 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
265 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
266 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
267 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
268 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
269 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
270 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
271 ReferenceErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
272 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
273 &ctx->range_error_constr, &ctx->reference_error_constr,
274 &ctx->syntax_error_constr, &ctx->type_error_constr,
275 &ctx->uri_error_constr};
276 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
277 RangeErrorConstr_value, ReferenceErrorConstr_value, SyntaxErrorConstr_value,
278 TypeErrorConstr_value, URIErrorConstr_value};
286 hres = alloc_error(ctx, i==0, NULL, &err);
291 V_BSTR(&v) = SysAllocString(names[i]);
293 IDispatchEx_Release(_IDispatchEx_(&err->dispex));
294 return E_OUTOFMEMORY;
297 hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/);
300 hres = create_builtin_function(ctx, constr_val[i], NULL,
301 PROPF_CONSTR, &err->dispex, constr_addr[i]);
303 IDispatchEx_Release(_IDispatchEx_(&err->dispex));