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 unsigned argc, jsval_t *argv, jsval_t *r)
41 jsstr_t *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) {
54 str = jsstr_alloc(object_errorW);
57 *r = jsval_string(str);
62 hres = jsdisp_propget_name(jsthis, nameW, &v);
66 if(!is_undefined(v)) {
67 hres = to_string(ctx, v, &name);
73 hres = jsdisp_propget_name(jsthis, messageW, &v);
75 if(!is_undefined(v)) {
76 hres = to_string(ctx, v, &msg);
82 unsigned name_len = name ? jsstr_length(name) : 0;
83 unsigned msg_len = msg ? jsstr_length(msg) : 0;
85 if(name_len && msg_len) {
86 ret = jsstr_alloc_buf(name_len + msg_len + 2);
88 jsstr_flush(name, ret->str);
89 ret->str[name_len] = ':';
90 ret->str[name_len+1] = ' ';
91 jsstr_flush(msg, ret->str+name_len+2);
100 ret = jsstr_alloc(object_errorW);
111 return E_OUTOFMEMORY;
114 *r = jsval_string(ret);
120 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
121 unsigned argc, jsval_t *argv, jsval_t *r)
127 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
129 FIXME("unimplemented flags %x\n", flags);
136 static const builtin_prop_t Error_props[] = {
137 {toStringW, Error_toString, PROPF_METHOD}
140 static const builtin_info_t Error_info = {
142 {NULL, Error_value, 0},
143 sizeof(Error_props)/sizeof(*Error_props),
149 static const builtin_info_t ErrorInst_info = {
151 {NULL, Error_value, 0},
158 static HRESULT alloc_error(script_ctx_t *ctx, jsdisp_t *prototype,
159 jsdisp_t *constr, jsdisp_t **ret)
164 err = heap_alloc_zero(sizeof(*err));
166 return E_OUTOFMEMORY;
169 hres = init_dispex(err, ctx, &Error_info, prototype);
171 hres = init_dispex_from_constr(err, ctx, &ErrorInst_info,
172 constr ? constr : ctx->error_constr);
182 static HRESULT create_error(script_ctx_t *ctx, jsdisp_t *constr,
183 UINT number, jsstr_t *msg, jsdisp_t **ret)
188 hres = alloc_error(ctx, NULL, constr, &err);
192 hres = jsdisp_propput_dontenum(err, numberW, jsval_number((INT)number));
198 hres = jsdisp_propput_name(err, messageW, jsval_string(msg));
200 hres = jsdisp_propput_dontenum(err, descriptionW, jsval_string(msg));
210 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, unsigned argc, jsval_t *argv,
211 jsval_t *r, jsdisp_t *constr) {
220 hres = to_number(ctx, argv[0], &n);
221 if(FAILED(hres)) /* FIXME: really? */
224 hres = to_string(ctx, argv[0], &msg);
232 hres = to_string(ctx, argv[1], &msg);
242 case DISPATCH_CONSTRUCT:
243 hres = create_error(ctx, constr, num, msg, &err);
257 FIXME("unimplemented flags %x\n", flags);
262 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
263 unsigned argc, jsval_t *argv, jsval_t *r)
266 return error_constr(ctx, flags, argc, argv, r, ctx->error_constr);
269 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
270 unsigned argc, jsval_t *argv, jsval_t *r)
273 return error_constr(ctx, flags, argc, argv, r, ctx->eval_error_constr);
276 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
277 unsigned argc, jsval_t *argv, jsval_t *r)
280 return error_constr(ctx, flags, argc, argv, r, ctx->range_error_constr);
283 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
284 unsigned argc, jsval_t *argv, jsval_t *r)
287 return error_constr(ctx, flags, argc, argv, r, ctx->reference_error_constr);
290 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
291 unsigned argc, jsval_t *argv, jsval_t *r)
294 return error_constr(ctx, flags, argc, argv, r, ctx->regexp_error_constr);
297 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
298 unsigned argc, jsval_t *argv, jsval_t *r)
301 return error_constr(ctx, flags, argc, argv, r, ctx->syntax_error_constr);
304 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
305 unsigned argc, jsval_t *argv, jsval_t *r)
308 return error_constr(ctx, flags, argc, argv, r, ctx->type_error_constr);
311 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
312 unsigned argc, jsval_t *argv, jsval_t *r)
315 return error_constr(ctx, flags, argc, argv, r, ctx->uri_error_constr);
318 HRESULT init_error_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
320 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
321 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
322 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
323 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
324 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
325 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
326 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
327 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
328 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
329 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
330 jsdisp_t **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
331 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
332 &ctx->syntax_error_constr, &ctx->type_error_constr,
333 &ctx->uri_error_constr};
334 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
335 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
336 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
343 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
344 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
348 str = jsstr_alloc(names[i]);
351 return E_OUTOFMEMORY;
354 hres = jsdisp_propput_dontenum(err, nameW, jsval_string(str));
357 hres = create_builtin_constructor(ctx, constr_val[i], names[i], NULL,
358 PROPF_CONSTR|1, err, constr_addr[i]);
368 static HRESULT throw_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str, jsdisp_t *constr)
370 WCHAR buf[1024], *pos = NULL;
375 if(!is_jscript_error(error))
379 LoadStringW(jscript_hinstance, HRESULT_CODE(error), buf, sizeof(buf)/sizeof(WCHAR));
381 if(str) pos = strchrW(buf, '|');
383 int len = strlenW(str);
384 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
385 memcpy(pos, str, len*sizeof(WCHAR));
388 WARN("%s\n", debugstr_w(buf));
390 msg = jsstr_alloc(buf);
392 return E_OUTOFMEMORY;
394 hres = create_error(ctx, constr, error, msg, &err);
399 jsval_release(ctx->ei.val);
400 ctx->ei.val = jsval_obj(err);
404 HRESULT throw_generic_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
406 return throw_error(ctx, error, str, ctx->error_constr);
409 HRESULT throw_range_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
411 return throw_error(ctx, error, str, ctx->range_error_constr);
414 HRESULT throw_reference_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
416 return throw_error(ctx, error, str, ctx->reference_error_constr);
419 HRESULT throw_regexp_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
421 return throw_error(ctx, error, str, ctx->regexp_error_constr);
424 HRESULT throw_syntax_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
426 return throw_error(ctx, error, str, ctx->syntax_error_constr);
429 HRESULT throw_type_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
431 return throw_error(ctx, error, str, ctx->type_error_constr);
434 HRESULT throw_uri_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
436 return throw_error(ctx, error, str, ctx->uri_error_constr);