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);
29 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
30 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
31 static const WCHAR nameW[] = {'n','a','m','e',0};
32 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
33 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
35 /* ECMA-262 3rd Edition 15.11.4.4 */
36 static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags,
37 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
40 BSTR name = NULL, msg = NULL, ret = NULL;
44 static const WCHAR object_errorW[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
48 jsthis = get_jsdisp(vthis);
49 if(!jsthis || ctx->version < 2) {
52 V_BSTR(retv) = SysAllocString(object_errorW);
59 hres = jsdisp_propget_name(jsthis, nameW, &v, ei, caller);
63 if(V_VT(&v) != VT_EMPTY) {
64 hres = to_string(ctx, &v, ei, &name);
74 hres = jsdisp_propget_name(jsthis, messageW, &v, ei, caller);
76 if(V_VT(&v) != VT_EMPTY) {
77 hres = to_string(ctx, &v, ei, &msg);
79 if(SUCCEEDED(hres) && !*msg) {
88 DWORD name_len, msg_len;
90 name_len = SysStringLen(name);
91 msg_len = SysStringLen(msg);
93 ret = SysAllocStringLen(NULL, name_len + msg_len + 2);
95 memcpy(ret, name, name_len*sizeof(WCHAR));
97 ret[name_len+1] = ' ';
98 memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR));
107 ret = SysAllocString(object_errorW);
109 hres = E_OUTOFMEMORY;
118 return E_OUTOFMEMORY;
121 V_VT(retv) = VT_BSTR;
130 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
131 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
137 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
139 FIXME("unimplemented flags %x\n", flags);
146 static const builtin_prop_t Error_props[] = {
147 {toStringW, Error_toString, PROPF_METHOD}
150 static const builtin_info_t Error_info = {
152 {NULL, Error_value, 0},
153 sizeof(Error_props)/sizeof(*Error_props),
159 static const builtin_info_t ErrorInst_info = {
161 {NULL, Error_value, 0},
168 static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
169 DispatchEx *constr, DispatchEx **ret)
174 err = heap_alloc_zero(sizeof(*err));
176 return E_OUTOFMEMORY;
179 hres = init_dispex(err, ctx, &Error_info, prototype);
181 hres = init_dispex_from_constr(err, ctx, &ErrorInst_info,
182 constr ? constr : ctx->error_constr);
192 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
193 UINT number, const WCHAR *msg, DispatchEx **ret)
199 hres = alloc_error(ctx, NULL, constr, &err);
205 hres = jsdisp_propput_name(err, numberW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
212 if(msg) V_BSTR(&v) = SysAllocString(msg);
213 else V_BSTR(&v) = SysAllocStringLen(NULL, 0);
215 hres = jsdisp_propput_name(err, messageW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
217 hres = jsdisp_propput_name(err, descriptionW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
218 SysFreeString(V_BSTR(&v));
220 hres = E_OUTOFMEMORY;
231 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
232 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
241 hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
242 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
243 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
244 else if(V_VT(&numv) == VT_I4)
253 if(arg_cnt(dp)>1 && !msg) {
254 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
261 case DISPATCH_CONSTRUCT:
262 hres = create_error(ctx, constr, num, msg, &err);
269 V_VT(retv) = VT_DISPATCH;
270 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
278 FIXME("unimplemented flags %x\n", flags);
283 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
284 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
287 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
290 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
291 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
294 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
297 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
298 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
301 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
304 static HRESULT ReferenceErrorConstr_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->reference_error_constr);
311 static HRESULT RegExpErrorConstr_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->regexp_error_constr);
318 static HRESULT SyntaxErrorConstr_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->syntax_error_constr);
325 static HRESULT TypeErrorConstr_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->type_error_constr);
332 static HRESULT URIErrorConstr_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->uri_error_constr);
339 HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
341 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
342 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
343 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
344 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
345 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
346 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
347 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
348 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
349 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
350 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
351 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
352 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
353 &ctx->syntax_error_constr, &ctx->type_error_constr,
354 &ctx->uri_error_constr};
355 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
356 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
357 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
364 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
365 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
370 V_BSTR(&v) = SysAllocString(names[i]);
373 return E_OUTOFMEMORY;
376 hres = jsdisp_propput_name(err, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
379 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
380 PROPF_CONSTR|1, err, constr_addr[i]);
391 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
393 WCHAR buf[1024], *pos = NULL;
398 LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR));
400 if(str) pos = strchrW(buf, '|');
402 int len = strlenW(str);
403 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
404 memcpy(pos, str, len*sizeof(WCHAR));
407 WARN("%s\n", debugstr_w(buf));
410 hres = create_error(ctx, constr, id, buf, &err);
417 V_VT(&ei->var) = VT_DISPATCH;
418 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
423 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
425 return throw_error(ctx, ei, id, str, ctx->error_constr);
428 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
430 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
433 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
435 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
438 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
440 return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
443 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
445 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
448 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
450 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
453 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
455 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);