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
19 #include "wine/port.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
37 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
38 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
39 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
40 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
41 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
42 static const WCHAR propertyIsEnumerableW[] =
43 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
44 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
46 static HRESULT Error_number(DispatchEx *dispex, LCID lcid, WORD flags,
47 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
49 ErrorInstance *This = (ErrorInstance*)dispex;
54 case DISPATCH_PROPERTYGET:
55 return VariantCopy(retv, &This->number);
56 case DISPATCH_PROPERTYPUT:
57 return VariantCopy(&This->number, get_arg(dp, 0));
59 FIXME("unimplemented flags %x\n", flags);
64 static HRESULT Error_description(DispatchEx *dispex, LCID lcid, WORD flags,
65 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
67 ErrorInstance *This = (ErrorInstance*)dispex;
72 case DISPATCH_PROPERTYGET:
73 return VariantCopy(retv, &This->description);
74 case DISPATCH_PROPERTYPUT:
75 return VariantCopy(&This->description, get_arg(dp, 0));
77 FIXME("unimplemented flags %x\n", flags);
82 /* ECMA-262 3rd Edition 15.11.4.3 */
83 static HRESULT Error_message(DispatchEx *dispex, LCID lcid, WORD flags,
84 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
86 ErrorInstance *This = (ErrorInstance*)dispex;
91 case DISPATCH_PROPERTYGET:
92 return VariantCopy(retv, &This->message);
93 case DISPATCH_PROPERTYPUT:
94 return VariantCopy(&This->message, get_arg(dp, 0));
96 FIXME("unimplemented flags %x\n", flags);
101 /* ECMA-262 3rd Edition 15.11.4.4 */
102 static HRESULT Error_toString(DispatchEx *dispex, LCID lcid, WORD flags,
103 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
105 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
110 V_VT(retv) = VT_BSTR;
111 V_BSTR(retv) = SysAllocString(str);
113 return E_OUTOFMEMORY;
119 static HRESULT Error_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags,
120 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
126 static HRESULT Error_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags,
127 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
134 static HRESULT Error_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags,
135 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
141 static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags,
142 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
148 static void Error_destructor(DispatchEx *dispex)
150 ErrorInstance *This = (ErrorInstance*)dispex;
152 VariantClear(&This->number);
153 VariantClear(&This->description);
154 VariantClear(&This->message);
158 static const builtin_prop_t Error_props[] = {
159 {descriptionW, Error_description, 0},
160 {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD},
161 {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD},
162 {messageW, Error_message, 0},
163 {numberW, Error_number, 0},
164 {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD},
165 {toStringW, Error_toString, PROPF_METHOD}
168 static const builtin_info_t Error_info = {
170 {NULL, Error_value, 0},
171 sizeof(Error_props)/sizeof(*Error_props),
177 static const builtin_prop_t ErrorInst_props[] = {
178 {descriptionW, Error_description, 0},
179 {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD},
180 {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD},
181 {messageW, Error_message, 0},
182 {numberW, Error_number, 0},
183 {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD}
186 static const builtin_info_t ErrorInst_info = {
188 {NULL, Error_value, 0},
189 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
195 static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype,
196 DispatchEx *constr, ErrorInstance **ret)
202 err = heap_alloc_zero(sizeof(ErrorInstance));
204 return E_OUTOFMEMORY;
206 inherit = error_prototype ? ctx->object_constr : ctx->error_constr;
207 hres = init_dispex_from_constr(&err->dispex, ctx,
208 error_prototype ? &Error_info : &ErrorInst_info,
209 constr ? constr : inherit);
219 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
220 const UINT *number, const WCHAR *msg, DispatchEx **ret)
225 hres = alloc_error(ctx, FALSE, constr, &err);
230 V_VT(&err->number) = VT_I4;
231 V_I4(&err->number) = *number;
234 V_VT(&err->message) = VT_BSTR;
235 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
236 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
238 VariantCopy(&err->description, &err->message);
240 if(!V_BSTR(&err->message)) {
242 return E_OUTOFMEMORY;
249 static HRESULT error_constr(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
250 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
257 V_VT(&numv) = VT_NULL;
260 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &numv);
261 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
262 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &msg);
263 else if(V_VT(&numv) == VT_I4)
272 if(arg_cnt(dp)>1 && !msg) {
273 hres = to_string(dispex->ctx, get_arg(dp, 1), ei, &msg);
280 case DISPATCH_CONSTRUCT:
281 if(V_VT(&numv) == VT_NULL)
282 hres = create_error(dispex->ctx, constr, NULL, msg, &err);
284 hres = create_error(dispex->ctx, constr, &num, msg, &err);
290 V_VT(retv) = VT_DISPATCH;
291 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
294 IDispatchEx_Release(_IDispatchEx_(err));
299 FIXME("unimplemented flags %x\n", flags);
304 static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
305 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
308 return error_constr(dispex, flags, dp, retv, ei,
309 dispex->ctx->error_constr);
312 static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
313 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
316 return error_constr(dispex, flags, dp, retv, ei,
317 dispex->ctx->eval_error_constr);
320 static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
321 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
324 return error_constr(dispex, flags, dp, retv, ei,
325 dispex->ctx->range_error_constr);
328 static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
329 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
332 return error_constr(dispex, flags, dp, retv, ei,
333 dispex->ctx->reference_error_constr);
336 static HRESULT SyntaxErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
337 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
340 return error_constr(dispex, flags, dp, retv, ei,
341 dispex->ctx->syntax_error_constr);
344 static HRESULT TypeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
345 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
348 return error_constr(dispex, flags, dp, retv, ei,
349 dispex->ctx->type_error_constr);
352 static HRESULT URIErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
353 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
356 return error_constr(dispex, flags, dp, retv, ei,
357 dispex->ctx->uri_error_constr);
360 HRESULT init_error_constr(script_ctx_t *ctx)
362 static const WCHAR nameW[] = {'n','a','m','e',0};
363 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
364 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
365 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
366 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
367 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
368 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
369 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
370 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
371 ReferenceErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
372 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
373 &ctx->range_error_constr, &ctx->reference_error_constr,
374 &ctx->syntax_error_constr, &ctx->type_error_constr,
375 &ctx->uri_error_constr};
376 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
377 RangeErrorConstr_value, ReferenceErrorConstr_value, SyntaxErrorConstr_value,
378 TypeErrorConstr_value, URIErrorConstr_value};
386 hres = alloc_error(ctx, i==0, NULL, &err);
391 V_BSTR(&v) = SysAllocString(names[i]);
393 IDispatchEx_Release(_IDispatchEx_(&err->dispex));
394 return E_OUTOFMEMORY;
397 hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/);
400 hres = create_builtin_function(ctx, constr_val[i], NULL,
401 PROPF_CONSTR, &err->dispex, constr_addr[i]);
403 IDispatchEx_Release(_IDispatchEx_(&err->dispex));
412 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
414 WCHAR buf[1024], *pos = NULL;
420 LoadStringW(jscript_hinstance, id, buf, sizeof(buf)/sizeof(WCHAR));
422 if(str) pos = strchrW(buf, '|');
424 int len = strlenW(str);
425 memmove(pos+len, pos+1, strlenW(pos+1)*sizeof(WCHAR));
426 memcpy(pos, str, len*sizeof(WCHAR));
430 hres = create_error(ctx, constr, &id, buf, &err);
437 V_VT(&ei->var) = VT_DISPATCH;
438 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
443 HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
445 return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
448 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
450 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
453 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
455 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
458 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
460 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
463 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
465 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
468 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
470 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);