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);
35 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
36 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
37 static const WCHAR nameW[] = {'n','a','m','e',0};
38 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
39 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
41 static inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp)
43 return (ErrorInstance*)vdisp->u.jsdisp;
46 static inline ErrorInstance *error_this(vdisp_t *jsthis)
48 return is_vclass(jsthis, JSCLASS_ERROR) ? error_from_vdisp(jsthis) : NULL;
51 /* ECMA-262 3rd Edition 15.11.4.3 */
52 static HRESULT Error_message(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
53 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
55 ErrorInstance *This = error_from_vdisp(jsthis);
60 case DISPATCH_PROPERTYGET:
61 return VariantCopy(retv, &This->message);
62 case DISPATCH_PROPERTYPUT:
63 return VariantCopy(&This->message, get_arg(dp, 0));
65 FIXME("unimplemented flags %x\n", flags);
70 /* ECMA-262 3rd Edition 15.11.4.4 */
71 static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
72 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
75 BSTR name, msg = NULL, ret = NULL;
79 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
83 error = error_this(jsthis);
84 if(ctx->version < 2 || !error) {
87 V_BSTR(retv) = SysAllocString(str);
94 hres = jsdisp_propget_name(&error->dispex, nameW, &v, ei, caller);
98 hres = to_string(ctx, &v, ei, &name);
103 if(V_VT(&error->message) != VT_EMPTY) {
104 hres = to_string(ctx, &error->message, ei, &msg);
105 if(SUCCEEDED(hres) && !*msg) {
111 if(SUCCEEDED(hres)) {
113 DWORD name_len, msg_len;
115 name_len = SysStringLen(name);
116 msg_len = SysStringLen(msg);
118 ret = SysAllocStringLen(NULL, name_len + msg_len + 2);
120 memcpy(ret, name, name_len*sizeof(WCHAR));
122 ret[name_len+1] = ' ';
123 memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR));
136 return E_OUTOFMEMORY;
139 V_VT(retv) = VT_BSTR;
148 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
149 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
155 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
157 FIXME("unimplemented flags %x\n", flags);
164 static void Error_destructor(DispatchEx *dispex)
166 ErrorInstance *This = (ErrorInstance*)dispex;
168 VariantClear(&This->message);
172 static const builtin_prop_t Error_props[] = {
173 {messageW, Error_message, 0},
174 {toStringW, Error_toString, PROPF_METHOD}
177 static const builtin_info_t Error_info = {
179 {NULL, Error_value, 0},
180 sizeof(Error_props)/sizeof(*Error_props),
186 static const builtin_prop_t ErrorInst_props[] = {
187 {messageW, Error_message, 0},
190 static const builtin_info_t ErrorInst_info = {
192 {NULL, Error_value, 0},
193 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
199 static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
200 DispatchEx *constr, ErrorInstance **ret)
205 err = heap_alloc_zero(sizeof(ErrorInstance));
207 return E_OUTOFMEMORY;
210 hres = init_dispex(&err->dispex, ctx, &Error_info, prototype);
212 hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info,
213 constr ? constr : ctx->error_constr);
223 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
224 UINT number, const WCHAR *msg, DispatchEx **ret)
230 hres = alloc_error(ctx, NULL, constr, &err);
236 hres = jsdisp_propput_name(&err->dispex, numberW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
238 jsdisp_release(&err->dispex);
242 V_VT(&err->message) = VT_BSTR;
243 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
244 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
245 if(!V_BSTR(&err->message)) {
247 return E_OUTOFMEMORY;
250 hres = jsdisp_propput_name(&err->dispex, descriptionW, &err->message, NULL/*FIXME*/, NULL/*FIXME*/);
252 jsdisp_release(&err->dispex);
260 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
261 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
270 hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
271 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
272 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
273 else if(V_VT(&numv) == VT_I4)
282 if(arg_cnt(dp)>1 && !msg) {
283 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
290 case DISPATCH_CONSTRUCT:
291 hres = create_error(ctx, constr, num, msg, &err);
298 V_VT(retv) = VT_DISPATCH;
299 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
307 FIXME("unimplemented flags %x\n", flags);
312 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
313 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
316 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
319 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
320 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
323 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
326 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
327 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
330 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
333 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
334 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
337 return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
340 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
341 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
344 return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
347 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
348 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
351 return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
354 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
355 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
358 return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
361 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
362 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
365 return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
368 HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
370 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
371 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
372 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
373 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
374 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
375 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
376 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
377 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
378 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
379 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
380 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
381 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
382 &ctx->syntax_error_constr, &ctx->type_error_constr,
383 &ctx->uri_error_constr};
384 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
385 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
386 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
393 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
394 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
399 V_BSTR(&v) = SysAllocString(names[i]);
401 jsdisp_release(&err->dispex);
402 return E_OUTOFMEMORY;
405 hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
408 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
409 PROPF_CONSTR|1, &err->dispex, constr_addr[i]);
411 jsdisp_release(&err->dispex);
420 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
422 WCHAR buf[1024], *pos = NULL;
427 LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR));
429 if(str) pos = strchrW(buf, '|');
431 int len = strlenW(str);
432 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
433 memcpy(pos, str, len*sizeof(WCHAR));
436 WARN("%s\n", debugstr_w(buf));
439 hres = create_error(ctx, constr, id, buf, &err);
446 V_VT(&ei->var) = VT_DISPATCH;
447 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
452 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
454 return throw_error(ctx, ei, id, str, ctx->error_constr);
457 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
459 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
462 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
464 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
467 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
469 return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
472 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
474 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
477 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
479 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
482 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
484 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);