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
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
31 /* ECMA-262 3rd Edition 15.11.4.4 */
32 static HRESULT
Error_toString(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
33 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
35 jsstr_t
*name
= NULL
, *msg
= NULL
, *ret
= NULL
;
36 jsdisp_t
*jsthis
= NULL
;
42 if(is_object_instance(vthis
))
43 jsthis
= to_jsdisp(get_object(vthis
));
44 else if(ctx
->version
>= SCRIPTLANGUAGEVERSION_ES5
)
45 return JS_E_OBJECT_EXPECTED
;
47 if(!jsthis
|| ctx
->version
< 2) {
51 str
= jsstr_alloc(L
"[object Error]");
54 *r
= jsval_string(str
);
59 hres
= jsdisp_propget_name(jsthis
, L
"name", &v
);
63 if(!is_undefined(v
)) {
64 hres
= to_string(ctx
, v
, &name
);
70 hres
= jsdisp_propget_name(jsthis
, L
"message", &v
);
72 if(!is_undefined(v
)) {
73 hres
= to_string(ctx
, v
, &msg
);
79 unsigned name_len
= name
? jsstr_length(name
) : 0;
80 unsigned msg_len
= msg
? jsstr_length(msg
) : 0;
82 if(name_len
&& msg_len
) {
85 ret
= jsstr_alloc_buf(name_len
+ msg_len
+ 2, &ptr
);
87 jsstr_flush(name
, ptr
);
89 ptr
[name_len
+1] = ' ';
90 jsstr_flush(msg
, ptr
+name_len
+2);
101 ret
= jsstr_alloc(L
"[object Error]");
112 return E_OUTOFMEMORY
;
115 *r
= jsval_string(ret
);
121 static HRESULT
Error_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
122 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
128 return JS_E_FUNCTION_EXPECTED
;
130 FIXME("unimplemented flags %x\n", flags
);
137 static const builtin_prop_t Error_props
[] = {
138 {L
"toString", Error_toString
, PROPF_METHOD
}
141 static const builtin_info_t Error_info
= {
144 ARRAY_SIZE(Error_props
),
150 static const builtin_info_t ErrorInst_info
= {
159 static HRESULT
alloc_error(script_ctx_t
*ctx
, jsdisp_t
*prototype
,
160 jsdisp_t
*constr
, jsdisp_t
**ret
)
165 err
= heap_alloc_zero(sizeof(*err
));
167 return E_OUTOFMEMORY
;
170 hres
= init_dispex(err
, ctx
, &Error_info
, prototype
);
172 hres
= init_dispex_from_constr(err
, ctx
, &ErrorInst_info
,
173 constr
? constr
: ctx
->error_constr
);
183 static HRESULT
create_error(script_ctx_t
*ctx
, jsdisp_t
*constr
,
184 UINT number
, jsstr_t
*msg
, jsdisp_t
**ret
)
189 hres
= alloc_error(ctx
, NULL
, constr
, &err
);
193 hres
= jsdisp_define_data_property(err
, L
"number", PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
194 jsval_number((INT
)number
));
200 hres
= jsdisp_define_data_property(err
, L
"message",
201 PROPF_WRITABLE
| PROPF_ENUMERABLE
| PROPF_CONFIGURABLE
,
204 hres
= jsdisp_define_data_property(err
, L
"description", PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
215 static HRESULT
error_constr(script_ctx_t
*ctx
, WORD flags
, unsigned argc
, jsval_t
*argv
,
216 jsval_t
*r
, jsdisp_t
*constr
) {
225 hres
= to_number(ctx
, argv
[0], &n
);
226 if(FAILED(hres
)) /* FIXME: really? */
229 hres
= to_string(ctx
, argv
[0], &msg
);
237 hres
= to_string(ctx
, argv
[1], &msg
);
247 case DISPATCH_CONSTRUCT
:
248 hres
= create_error(ctx
, constr
, num
, msg
, &err
);
262 FIXME("unimplemented flags %x\n", flags
);
267 static HRESULT
ErrorConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
268 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
271 return error_constr(ctx
, flags
, argc
, argv
, r
, ctx
->error_constr
);
274 static HRESULT
EvalErrorConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
275 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
278 return error_constr(ctx
, flags
, argc
, argv
, r
, ctx
->eval_error_constr
);
281 static HRESULT
RangeErrorConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
282 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
285 return error_constr(ctx
, flags
, argc
, argv
, r
, ctx
->range_error_constr
);
288 static HRESULT
ReferenceErrorConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
289 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
292 return error_constr(ctx
, flags
, argc
, argv
, r
, ctx
->reference_error_constr
);
295 static HRESULT
RegExpErrorConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
296 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
299 return error_constr(ctx
, flags
, argc
, argv
, r
, ctx
->regexp_error_constr
);
302 static HRESULT
SyntaxErrorConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
303 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
306 return error_constr(ctx
, flags
, argc
, argv
, r
, ctx
->syntax_error_constr
);
309 static HRESULT
TypeErrorConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
310 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
313 return error_constr(ctx
, flags
, argc
, argv
, r
, ctx
->type_error_constr
);
316 static HRESULT
URIErrorConstr_value(script_ctx_t
*ctx
, jsval_t vthis
, WORD flags
,
317 unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
320 return error_constr(ctx
, flags
, argc
, argv
, r
, ctx
->uri_error_constr
);
323 HRESULT
init_error_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
)
325 static const WCHAR
*names
[] = {L
"Error", L
"EvalError", L
"RangeError",
326 L
"ReferenceError", L
"RegExpError", L
"SyntaxError", L
"TypeError", L
"URIError"};
327 jsdisp_t
**constr_addr
[] = {&ctx
->error_constr
, &ctx
->eval_error_constr
,
328 &ctx
->range_error_constr
, &ctx
->reference_error_constr
, &ctx
->regexp_error_constr
,
329 &ctx
->syntax_error_constr
, &ctx
->type_error_constr
,
330 &ctx
->uri_error_constr
};
331 static builtin_invoke_t constr_val
[] = {ErrorConstr_value
, EvalErrorConstr_value
,
332 RangeErrorConstr_value
, ReferenceErrorConstr_value
, RegExpErrorConstr_value
,
333 SyntaxErrorConstr_value
, TypeErrorConstr_value
, URIErrorConstr_value
};
340 for(i
=0; i
< ARRAY_SIZE(names
); i
++) {
341 hres
= alloc_error(ctx
, i
==0 ? object_prototype
: NULL
, NULL
, &err
);
345 str
= jsstr_alloc(names
[i
]);
348 return E_OUTOFMEMORY
;
351 hres
= jsdisp_define_data_property(err
, L
"name", PROPF_WRITABLE
| PROPF_CONFIGURABLE
,
355 hres
= create_builtin_constructor(ctx
, constr_val
[i
], names
[i
], NULL
,
356 PROPF_CONSTR
|1, err
, constr_addr
[i
]);
366 static jsstr_t
*format_error_message(HRESULT error
, const WCHAR
*arg
)
368 size_t len
, arg_len
= 0;
369 const WCHAR
*res
, *pos
;
373 if(!is_jscript_error(error
))
374 return jsstr_empty();
376 len
= LoadStringW(jscript_hinstance
, HRESULT_CODE(error
), (WCHAR
*)&res
, 0);
378 pos
= wmemchr(res
, '|', len
);
380 arg_len
= lstrlenW(arg
);
381 r
= jsstr_alloc_buf(len
+ arg_len
- (pos
? 1 : 0), &buf
);
383 return jsstr_empty();
387 memcpy(p
, res
, (pos
- res
) * sizeof(WCHAR
));
390 pos
= pos
? pos
+ 1 : res
;
392 memcpy(p
, arg
, arg_len
* sizeof(WCHAR
));
396 memcpy(p
, pos
, (res
+ len
- pos
) * sizeof(WCHAR
));
400 HRESULT
throw_error(script_ctx_t
*ctx
, HRESULT error
, const WCHAR
*str
)
402 jsexcept_t
*ei
= ctx
->ei
;
403 TRACE("%08lx\n", error
);
407 ei
->message
= format_error_message(error
, str
);
408 return DISP_E_EXCEPTION
;
411 void set_error_location(jsexcept_t
*ei
, bytecode_t
*code
, unsigned loc
, unsigned source_id
, jsstr_t
*line
)
413 if(is_jscript_error(ei
->error
)) {
418 len
= LoadStringW(jscript_hinstance
, source_id
, (WCHAR
*)&res
, 0);
419 ei
->source
= jsstr_alloc_len(res
, len
);
422 ei
->message
= format_error_message(ei
->error
, NULL
);
425 TRACE("source %s in %s\n", debugstr_w(code
->source
+ loc
), debugstr_w(code
->source
));
427 ei
->code
= bytecode_addref(code
);
430 ei
->line
= jsstr_addref(line
);
433 jsdisp_t
*create_builtin_error(script_ctx_t
*ctx
)
435 jsdisp_t
*constr
= ctx
->error_constr
, *r
;
436 jsexcept_t
*ei
= ctx
->ei
;
439 assert(FAILED(ei
->error
) && ei
->error
!= DISP_E_EXCEPTION
);
441 if(is_jscript_error(ei
->error
)) {
444 case JS_E_MISSING_SEMICOLON
:
445 case JS_E_MISSING_LBRACKET
:
446 case JS_E_MISSING_RBRACKET
:
447 case JS_E_EXPECTED_IDENTIFIER
:
448 case JS_E_EXPECTED_ASSIGN
:
449 case JS_E_INVALID_CHAR
:
450 case JS_E_UNTERMINATED_STRING
:
451 case JS_E_MISPLACED_RETURN
:
452 case JS_E_INVALID_BREAK
:
453 case JS_E_INVALID_CONTINUE
:
454 case JS_E_LABEL_REDEFINED
:
455 case JS_E_LABEL_NOT_FOUND
:
456 case JS_E_EXPECTED_CCEND
:
457 case JS_E_DISABLED_CC
:
458 case JS_E_EXPECTED_AT
:
459 constr
= ctx
->syntax_error_constr
;
462 case JS_E_TO_PRIMITIVE
:
463 case JS_E_INVALIDARG
:
464 case JS_E_OBJECT_REQUIRED
:
465 case JS_E_INVALID_PROPERTY
:
466 case JS_E_INVALID_ACTION
:
467 case JS_E_MISSING_ARG
:
468 case JS_E_FUNCTION_EXPECTED
:
469 case JS_E_DATE_EXPECTED
:
470 case JS_E_NUMBER_EXPECTED
:
471 case JS_E_OBJECT_EXPECTED
:
472 case JS_E_UNDEFINED_VARIABLE
:
473 case JS_E_BOOLEAN_EXPECTED
:
474 case JS_E_VBARRAY_EXPECTED
:
475 case JS_E_INVALID_DELETE
:
476 case JS_E_JSCRIPT_EXPECTED
:
477 case JS_E_ENUMERATOR_EXPECTED
:
478 case JS_E_REGEXP_EXPECTED
:
479 case JS_E_ARRAY_EXPECTED
:
480 case JS_E_CYCLIC_PROTO_VALUE
:
481 case JS_E_CANNOT_CREATE_FOR_NONEXTENSIBLE
:
482 case JS_E_OBJECT_NONEXTENSIBLE
:
483 case JS_E_NONCONFIGURABLE_REDEFINED
:
484 case JS_E_NONWRITABLE_MODIFIED
:
485 case JS_E_PROP_DESC_MISMATCH
:
486 case JS_E_INVALID_WRITABLE_PROP_DESC
:
487 constr
= ctx
->type_error_constr
;
490 case JS_E_SUBSCRIPT_OUT_OF_RANGE
:
491 case JS_E_FRACTION_DIGITS_OUT_OF_RANGE
:
492 case JS_E_PRECISION_OUT_OF_RANGE
:
493 case JS_E_INVALID_LENGTH
:
494 constr
= ctx
->range_error_constr
;
497 case JS_E_ILLEGAL_ASSIGN
:
498 constr
= ctx
->reference_error_constr
;
501 case JS_E_REGEXP_SYNTAX
:
502 constr
= ctx
->regexp_error_constr
;
505 case JS_E_INVALID_URI_CODING
:
506 case JS_E_INVALID_URI_CHAR
:
507 constr
= ctx
->uri_error_constr
;
512 hres
= create_error(ctx
, constr
, ei
->error
, ei
->message
? ei
->message
: jsstr_empty(), &r
);
513 return SUCCEEDED(hres
) ? r
: NULL
;