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};
42 static inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp)
44 return (ErrorInstance*)vdisp->u.jsdisp;
47 static HRESULT Error_number(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
48 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
50 ErrorInstance *This = error_from_vdisp(jsthis);
55 case DISPATCH_PROPERTYGET:
56 return VariantCopy(retv, &This->number);
57 case DISPATCH_PROPERTYPUT:
58 return VariantCopy(&This->number, get_arg(dp, 0));
60 FIXME("unimplemented flags %x\n", flags);
65 static HRESULT Error_description(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
66 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
68 ErrorInstance *This = error_from_vdisp(jsthis);
73 case DISPATCH_PROPERTYGET:
74 return VariantCopy(retv, &This->description);
75 case DISPATCH_PROPERTYPUT:
76 return VariantCopy(&This->description, get_arg(dp, 0));
78 FIXME("unimplemented flags %x\n", flags);
83 /* ECMA-262 3rd Edition 15.11.4.3 */
84 static HRESULT Error_message(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
85 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
87 ErrorInstance *This = error_from_vdisp(jsthis);
92 case DISPATCH_PROPERTYGET:
93 return VariantCopy(retv, &This->message);
94 case DISPATCH_PROPERTYPUT:
95 return VariantCopy(&This->message, get_arg(dp, 0));
97 FIXME("unimplemented flags %x\n", flags);
102 /* ECMA-262 3rd Edition 15.11.4.4 */
103 static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
104 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
106 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
111 V_VT(retv) = VT_BSTR;
112 V_BSTR(retv) = SysAllocString(str);
114 return E_OUTOFMEMORY;
120 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
121 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
127 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
129 FIXME("unimplemented flags %x\n", flags);
136 static void Error_destructor(DispatchEx *dispex)
138 ErrorInstance *This = (ErrorInstance*)dispex;
140 VariantClear(&This->number);
141 VariantClear(&This->description);
142 VariantClear(&This->message);
146 static const builtin_prop_t Error_props[] = {
147 {descriptionW, Error_description, 0},
148 {messageW, Error_message, 0},
149 {numberW, Error_number, 0},
150 {toStringW, Error_toString, PROPF_METHOD}
153 static const builtin_info_t Error_info = {
155 {NULL, Error_value, 0},
156 sizeof(Error_props)/sizeof(*Error_props),
162 static const builtin_prop_t ErrorInst_props[] = {
163 {descriptionW, Error_description, 0},
164 {messageW, Error_message, 0},
165 {numberW, Error_number, 0},
168 static const builtin_info_t ErrorInst_info = {
170 {NULL, Error_value, 0},
171 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
177 static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
178 DispatchEx *constr, ErrorInstance **ret)
183 err = heap_alloc_zero(sizeof(ErrorInstance));
185 return E_OUTOFMEMORY;
188 hres = init_dispex(&err->dispex, ctx, &Error_info, prototype);
190 hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info,
191 constr ? constr : ctx->error_constr);
201 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
202 const UINT *number, const WCHAR *msg, DispatchEx **ret)
207 hres = alloc_error(ctx, NULL, constr, &err);
212 V_VT(&err->number) = VT_I4;
213 V_I4(&err->number) = *number;
216 V_VT(&err->message) = VT_BSTR;
217 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
218 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
220 VariantCopy(&err->description, &err->message);
222 if(!V_BSTR(&err->message)) {
224 return E_OUTOFMEMORY;
231 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
232 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
239 V_VT(&numv) = VT_NULL;
242 hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
243 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
244 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
245 else if(V_VT(&numv) == VT_I4)
254 if(arg_cnt(dp)>1 && !msg) {
255 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
262 case DISPATCH_CONSTRUCT:
263 if(V_VT(&numv) == VT_NULL)
264 hres = create_error(ctx, constr, NULL, msg, &err);
266 hres = create_error(ctx, constr, &num, msg, &err);
272 V_VT(retv) = VT_DISPATCH;
273 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
281 FIXME("unimplemented flags %x\n", flags);
286 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
287 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
290 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
293 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
294 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
297 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
300 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
301 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
304 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
307 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
308 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
311 return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
314 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
315 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
318 return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
321 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
322 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
325 return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
328 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
329 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
332 return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
335 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
336 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
339 return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
342 HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
344 static const WCHAR nameW[] = {'n','a','m','e',0};
345 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
346 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
347 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
348 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
349 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
350 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
351 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
352 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
353 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
354 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
355 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
356 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
357 &ctx->syntax_error_constr, &ctx->type_error_constr,
358 &ctx->uri_error_constr};
359 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
360 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
361 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
368 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
369 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
374 V_BSTR(&v) = SysAllocString(names[i]);
376 jsdisp_release(&err->dispex);
377 return E_OUTOFMEMORY;
380 hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
383 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
384 PROPF_CONSTR, &err->dispex, constr_addr[i]);
386 jsdisp_release(&err->dispex);
395 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
397 WCHAR buf[1024], *pos = NULL;
402 LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR));
404 if(str) pos = strchrW(buf, '|');
406 int len = strlenW(str);
407 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
408 memcpy(pos, str, len*sizeof(WCHAR));
411 WARN("%s\n", debugstr_w(buf));
414 hres = create_error(ctx, constr, &id, buf, &err);
421 V_VT(&ei->var) = VT_DISPATCH;
422 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
427 HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
429 return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
432 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
434 return throw_error(ctx, ei, id, str, ctx->error_constr);
437 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
439 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
442 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
444 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
447 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
449 return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
452 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
454 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
457 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
459 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
462 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
464 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);