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);
33 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
34 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
35 static const WCHAR nameW[] = {'n','a','m','e',0};
36 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
37 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
39 static inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp)
41 return (ErrorInstance*)vdisp->u.jsdisp;
44 static inline ErrorInstance *error_this(vdisp_t *jsthis)
46 return is_vclass(jsthis, JSCLASS_ERROR) ? error_from_vdisp(jsthis) : NULL;
49 /* ECMA-262 3rd Edition 15.11.4.4 */
50 static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags,
51 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
54 BSTR name = NULL, msg = NULL, ret = NULL;
58 static const WCHAR object_errorW[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
62 jsthis = get_jsdisp(vthis);
63 if(!jsthis || ctx->version < 2) {
66 V_BSTR(retv) = SysAllocString(object_errorW);
73 hres = jsdisp_propget_name(jsthis, nameW, &v, ei, caller);
77 if(V_VT(&v) != VT_EMPTY) {
78 hres = to_string(ctx, &v, ei, &name);
88 hres = jsdisp_propget_name(jsthis, messageW, &v, ei, caller);
90 if(V_VT(&v) != VT_EMPTY) {
91 hres = to_string(ctx, &v, ei, &msg);
93 if(SUCCEEDED(hres) && !*msg) {
100 if(SUCCEEDED(hres)) {
102 DWORD name_len, msg_len;
104 name_len = SysStringLen(name);
105 msg_len = SysStringLen(msg);
107 ret = SysAllocStringLen(NULL, name_len + msg_len + 2);
109 memcpy(ret, name, name_len*sizeof(WCHAR));
111 ret[name_len+1] = ' ';
112 memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR));
121 ret = SysAllocString(object_errorW);
123 hres = E_OUTOFMEMORY;
132 return E_OUTOFMEMORY;
135 V_VT(retv) = VT_BSTR;
144 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
145 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
151 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
153 FIXME("unimplemented flags %x\n", flags);
160 static void Error_destructor(DispatchEx *dispex)
162 ErrorInstance *This = (ErrorInstance*)dispex;
167 static const builtin_prop_t Error_props[] = {
168 {toStringW, Error_toString, PROPF_METHOD}
171 static const builtin_info_t Error_info = {
173 {NULL, Error_value, 0},
174 sizeof(Error_props)/sizeof(*Error_props),
180 static const builtin_info_t ErrorInst_info = {
182 {NULL, Error_value, 0},
189 static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
190 DispatchEx *constr, ErrorInstance **ret)
195 err = heap_alloc_zero(sizeof(ErrorInstance));
197 return E_OUTOFMEMORY;
200 hres = init_dispex(&err->dispex, ctx, &Error_info, prototype);
202 hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info,
203 constr ? constr : ctx->error_constr);
213 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
214 UINT number, const WCHAR *msg, DispatchEx **ret)
220 hres = alloc_error(ctx, NULL, constr, &err);
226 hres = jsdisp_propput_name(&err->dispex, numberW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
228 jsdisp_release(&err->dispex);
233 if(msg) V_BSTR(&v) = SysAllocString(msg);
234 else V_BSTR(&v) = SysAllocStringLen(NULL, 0);
236 hres = jsdisp_propput_name(&err->dispex, messageW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
238 hres = jsdisp_propput_name(&err->dispex, descriptionW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
239 SysFreeString(V_BSTR(&v));
241 hres = E_OUTOFMEMORY;
244 jsdisp_release(&err->dispex);
252 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
253 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
262 hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
263 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
264 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
265 else if(V_VT(&numv) == VT_I4)
274 if(arg_cnt(dp)>1 && !msg) {
275 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
282 case DISPATCH_CONSTRUCT:
283 hres = create_error(ctx, constr, num, msg, &err);
290 V_VT(retv) = VT_DISPATCH;
291 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
299 FIXME("unimplemented flags %x\n", flags);
304 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
305 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
308 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
311 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
312 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
315 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
318 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
319 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
322 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
325 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
326 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
329 return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
332 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
333 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
336 return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
339 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
340 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
343 return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
346 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
347 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
350 return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
353 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
354 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
357 return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
360 HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
362 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
363 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
364 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
365 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
366 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','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, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
372 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
373 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_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, RegExpErrorConstr_value,
378 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
385 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
386 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
391 V_BSTR(&v) = SysAllocString(names[i]);
393 jsdisp_release(&err->dispex);
394 return E_OUTOFMEMORY;
397 hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
400 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
401 PROPF_CONSTR|1, &err->dispex, constr_addr[i]);
403 jsdisp_release(&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;
419 LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR));
421 if(str) pos = strchrW(buf, '|');
423 int len = strlenW(str);
424 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
425 memcpy(pos, str, len*sizeof(WCHAR));
428 WARN("%s\n", debugstr_w(buf));
431 hres = create_error(ctx, constr, id, buf, &err);
438 V_VT(&ei->var) = VT_DISPATCH;
439 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
444 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
446 return throw_error(ctx, ei, id, str, ctx->error_constr);
449 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
451 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
454 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
456 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
459 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
461 return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
464 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
466 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
469 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
471 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
474 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
476 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);