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
20 #include "wine/port.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
31 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
32 static const WCHAR nameW[] = {'n','a','m','e',0};
33 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
34 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
36 /* ECMA-262 3rd Edition 15.11.4.4 */
37 static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags,
38 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
41 BSTR name = NULL, msg = NULL, ret = NULL;
45 static const WCHAR object_errorW[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
49 jsthis = get_jsdisp(vthis);
50 if(!jsthis || ctx->version < 2) {
53 V_BSTR(retv) = SysAllocString(object_errorW);
60 hres = jsdisp_propget_name(jsthis, nameW, &v, ei);
64 if(V_VT(&v) != VT_EMPTY) {
65 hres = to_string(ctx, &v, ei, &name);
75 hres = jsdisp_propget_name(jsthis, messageW, &v, ei);
77 if(V_VT(&v) != VT_EMPTY) {
78 hres = to_string(ctx, &v, ei, &msg);
80 if(SUCCEEDED(hres) && !*msg) {
89 DWORD name_len, msg_len;
91 name_len = SysStringLen(name);
92 msg_len = SysStringLen(msg);
94 ret = SysAllocStringLen(NULL, name_len + msg_len + 2);
96 memcpy(ret, name, name_len*sizeof(WCHAR));
98 ret[name_len+1] = ' ';
99 memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR));
108 ret = SysAllocString(object_errorW);
117 return E_OUTOFMEMORY;
120 V_VT(retv) = VT_BSTR;
129 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
130 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
136 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
138 FIXME("unimplemented flags %x\n", flags);
145 static const builtin_prop_t Error_props[] = {
146 {toStringW, Error_toString, PROPF_METHOD}
149 static const builtin_info_t Error_info = {
151 {NULL, Error_value, 0},
152 sizeof(Error_props)/sizeof(*Error_props),
158 static const builtin_info_t ErrorInst_info = {
160 {NULL, Error_value, 0},
167 static HRESULT alloc_error(script_ctx_t *ctx, jsdisp_t *prototype,
168 jsdisp_t *constr, jsdisp_t **ret)
173 err = heap_alloc_zero(sizeof(*err));
175 return E_OUTOFMEMORY;
178 hres = init_dispex(err, ctx, &Error_info, prototype);
180 hres = init_dispex_from_constr(err, ctx, &ErrorInst_info,
181 constr ? constr : ctx->error_constr);
191 static HRESULT create_error(script_ctx_t *ctx, jsdisp_t *constr,
192 UINT number, const WCHAR *msg, jsdisp_t **ret)
198 hres = alloc_error(ctx, NULL, constr, &err);
204 hres = jsdisp_propput_name(err, numberW, &v, NULL/*FIXME*/);
211 if(msg) V_BSTR(&v) = SysAllocString(msg);
212 else V_BSTR(&v) = SysAllocStringLen(NULL, 0);
214 hres = jsdisp_propput_name(err, messageW, &v, NULL/*FIXME*/);
216 hres = jsdisp_propput_name(err, descriptionW, &v, NULL/*FIXME*/);
217 SysFreeString(V_BSTR(&v));
219 hres = E_OUTOFMEMORY;
230 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
231 VARIANT *retv, jsexcept_t *ei, jsdisp_t *constr) {
240 hres = to_number(ctx, get_arg(dp, 0), ei, &n);
241 if(FAILED(hres)) /* FIXME: really? */
244 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
250 if(arg_cnt(dp)>1 && !msg) {
251 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
258 case DISPATCH_CONSTRUCT:
259 hres = create_error(ctx, constr, num, msg, &err);
266 var_set_jsdisp(retv, err);
273 FIXME("unimplemented flags %x\n", flags);
278 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
279 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
282 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
285 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
286 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
289 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
292 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
293 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
296 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
299 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
300 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
303 return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
306 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
307 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
310 return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
313 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
314 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
317 return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
320 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
321 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
324 return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
327 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
328 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
331 return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
334 HRESULT init_error_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
336 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
337 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
338 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
339 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
340 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
341 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
342 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
343 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
344 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
345 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
346 jsdisp_t **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
347 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
348 &ctx->syntax_error_constr, &ctx->type_error_constr,
349 &ctx->uri_error_constr};
350 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
351 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
352 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
359 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
360 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
365 V_BSTR(&v) = SysAllocString(names[i]);
368 return E_OUTOFMEMORY;
371 hres = jsdisp_propput_name(err, nameW, &v, NULL/*FIXME*/);
374 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
375 PROPF_CONSTR|1, err, constr_addr[i]);
386 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str, jsdisp_t *constr)
388 WCHAR buf[1024], *pos = NULL;
392 if(!is_jscript_error(error))
396 LoadStringW(jscript_hinstance, HRESULT_CODE(error), buf, sizeof(buf)/sizeof(WCHAR));
398 if(str) pos = strchrW(buf, '|');
400 int len = strlenW(str);
401 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
402 memcpy(pos, str, len*sizeof(WCHAR));
405 WARN("%s\n", debugstr_w(buf));
407 hres = create_error(ctx, constr, error, buf, &err);
412 var_set_jsdisp(&ei->var, err);
416 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
418 return throw_error(ctx, ei, error, str, ctx->error_constr);
421 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
423 return throw_error(ctx, ei, error, str, ctx->range_error_constr);
426 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
428 return throw_error(ctx, ei, error, str, ctx->reference_error_constr);
431 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
433 return throw_error(ctx, ei, error, str, ctx->regexp_error_constr);
436 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
438 return throw_error(ctx, ei, error, str, ctx->syntax_error_constr);
441 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
443 return throw_error(ctx, ei, error, str, ctx->type_error_constr);
446 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
448 return throw_error(ctx, ei, error, str, ctx->uri_error_constr);