push 8b07bf1f08b23b9893a622b47d2be359556765b1
[wine/hacks.git] / dlls / jscript / error.c
blob2b193ef6ac1cf7ceaa3bddd85d287f2b6aca0ef4
1 /*
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
18 #include "config.h"
19 #include "wine/port.h"
21 #include <math.h>
23 #include "jscript.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 typedef struct {
30 DispatchEx dispex;
32 VARIANT number;
33 VARIANT description;
34 VARIANT message;
35 } ErrorInstance;
37 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
38 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
39 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
40 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
42 static inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp)
44 return (ErrorInstance*)vdisp->u.jsdisp;
47 static HRESULT Error_number(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
48 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
50 ErrorInstance *This = error_from_vdisp(jsthis);
52 TRACE("\n");
54 switch(flags) {
55 case DISPATCH_PROPERTYGET:
56 return VariantCopy(retv, &This->number);
57 case DISPATCH_PROPERTYPUT:
58 return VariantCopy(&This->number, get_arg(dp, 0));
59 default:
60 FIXME("unimplemented flags %x\n", flags);
61 return E_NOTIMPL;
65 static HRESULT Error_description(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
66 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
68 ErrorInstance *This = error_from_vdisp(jsthis);
70 TRACE("\n");
72 switch(flags) {
73 case DISPATCH_PROPERTYGET:
74 return VariantCopy(retv, &This->description);
75 case DISPATCH_PROPERTYPUT:
76 return VariantCopy(&This->description, get_arg(dp, 0));
77 default:
78 FIXME("unimplemented flags %x\n", flags);
79 return E_NOTIMPL;
83 /* ECMA-262 3rd Edition 15.11.4.3 */
84 static HRESULT Error_message(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
85 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
87 ErrorInstance *This = error_from_vdisp(jsthis);
89 TRACE("\n");
91 switch(flags) {
92 case DISPATCH_PROPERTYGET:
93 return VariantCopy(retv, &This->message);
94 case DISPATCH_PROPERTYPUT:
95 return VariantCopy(&This->message, get_arg(dp, 0));
96 default:
97 FIXME("unimplemented flags %x\n", flags);
98 return E_NOTIMPL;
102 /* ECMA-262 3rd Edition 15.11.4.4 */
103 static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
104 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
106 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
108 TRACE("\n");
110 if(retv) {
111 V_VT(retv) = VT_BSTR;
112 V_BSTR(retv) = SysAllocString(str);
113 if(!V_BSTR(retv))
114 return E_OUTOFMEMORY;
117 return S_OK;
120 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
121 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
123 TRACE("\n");
125 switch(flags) {
126 case INVOKE_FUNC:
127 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
128 default:
129 FIXME("unimplemented flags %x\n", flags);
130 return E_NOTIMPL;
133 return S_OK;
136 static void Error_destructor(DispatchEx *dispex)
138 ErrorInstance *This = (ErrorInstance*)dispex;
140 VariantClear(&This->number);
141 VariantClear(&This->description);
142 VariantClear(&This->message);
143 heap_free(This);
146 static const builtin_prop_t Error_props[] = {
147 {descriptionW, Error_description, 0},
148 {messageW, Error_message, 0},
149 {numberW, Error_number, 0},
150 {toStringW, Error_toString, PROPF_METHOD}
153 static const builtin_info_t Error_info = {
154 JSCLASS_ERROR,
155 {NULL, Error_value, 0},
156 sizeof(Error_props)/sizeof(*Error_props),
157 Error_props,
158 Error_destructor,
159 NULL
162 static const builtin_prop_t ErrorInst_props[] = {
163 {descriptionW, Error_description, 0},
164 {messageW, Error_message, 0},
165 {numberW, Error_number, 0},
168 static const builtin_info_t ErrorInst_info = {
169 JSCLASS_ERROR,
170 {NULL, Error_value, 0},
171 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
172 ErrorInst_props,
173 Error_destructor,
174 NULL
177 static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
178 DispatchEx *constr, ErrorInstance **ret)
180 ErrorInstance *err;
181 HRESULT hres;
183 err = heap_alloc_zero(sizeof(ErrorInstance));
184 if(!err)
185 return E_OUTOFMEMORY;
187 if(prototype)
188 hres = init_dispex(&err->dispex, ctx, &Error_info, prototype);
189 else
190 hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info,
191 constr ? constr : ctx->error_constr);
192 if(FAILED(hres)) {
193 heap_free(err);
194 return hres;
197 *ret = err;
198 return S_OK;
201 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
202 const UINT *number, const WCHAR *msg, DispatchEx **ret)
204 ErrorInstance *err;
205 HRESULT hres;
207 hres = alloc_error(ctx, NULL, constr, &err);
208 if(FAILED(hres))
209 return hres;
211 if(number) {
212 V_VT(&err->number) = VT_I4;
213 V_I4(&err->number) = *number;
216 V_VT(&err->message) = VT_BSTR;
217 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
218 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
220 VariantCopy(&err->description, &err->message);
222 if(!V_BSTR(&err->message)) {
223 heap_free(err);
224 return E_OUTOFMEMORY;
227 *ret = &err->dispex;
228 return S_OK;
231 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
232 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
233 DispatchEx *err;
234 VARIANT numv;
235 UINT num;
236 BSTR msg = NULL;
237 HRESULT hres;
239 V_VT(&numv) = VT_NULL;
241 if(arg_cnt(dp)) {
242 hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
243 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
244 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
245 else if(V_VT(&numv) == VT_I4)
246 num = V_I4(&numv);
247 else
248 num = V_R8(&numv);
250 if(FAILED(hres))
251 return hres;
254 if(arg_cnt(dp)>1 && !msg) {
255 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
256 if(FAILED(hres))
257 return hres;
260 switch(flags) {
261 case INVOKE_FUNC:
262 case DISPATCH_CONSTRUCT:
263 if(V_VT(&numv) == VT_NULL)
264 hres = create_error(ctx, constr, NULL, msg, &err);
265 else
266 hres = create_error(ctx, constr, &num, msg, &err);
268 if(FAILED(hres))
269 return hres;
271 if(retv) {
272 V_VT(retv) = VT_DISPATCH;
273 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
275 else
276 jsdisp_release(err);
278 return S_OK;
280 default:
281 FIXME("unimplemented flags %x\n", flags);
282 return E_NOTIMPL;
286 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
287 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
289 TRACE("\n");
290 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
293 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
294 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
296 TRACE("\n");
297 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
300 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
301 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
303 TRACE("\n");
304 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
307 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
308 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
310 TRACE("\n");
311 return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
314 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
315 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
317 TRACE("\n");
318 return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
321 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
322 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
324 TRACE("\n");
325 return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
328 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
329 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
331 TRACE("\n");
332 return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
335 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
336 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
338 TRACE("\n");
339 return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
342 HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
344 static const WCHAR nameW[] = {'n','a','m','e',0};
345 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
346 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
347 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
348 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
349 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
350 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
351 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
352 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
353 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
354 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
355 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
356 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
357 &ctx->syntax_error_constr, &ctx->type_error_constr,
358 &ctx->uri_error_constr};
359 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
360 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
361 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
363 ErrorInstance *err;
364 INT i;
365 VARIANT v;
366 HRESULT hres;
368 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
369 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
370 if(FAILED(hres))
371 return hres;
373 V_VT(&v) = VT_BSTR;
374 V_BSTR(&v) = SysAllocString(names[i]);
375 if(!V_BSTR(&v)) {
376 jsdisp_release(&err->dispex);
377 return E_OUTOFMEMORY;
380 hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
382 if(SUCCEEDED(hres))
383 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
384 PROPF_CONSTR|1, &err->dispex, constr_addr[i]);
386 jsdisp_release(&err->dispex);
387 VariantClear(&v);
388 if(FAILED(hres))
389 return hres;
392 return S_OK;
395 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
397 WCHAR buf[1024], *pos = NULL;
398 DispatchEx *err;
399 HRESULT hres;
401 buf[0] = '\0';
402 LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR));
404 if(str) pos = strchrW(buf, '|');
405 if(pos) {
406 int len = strlenW(str);
407 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
408 memcpy(pos, str, len*sizeof(WCHAR));
411 WARN("%s\n", debugstr_w(buf));
413 id |= JSCRIPT_ERROR;
414 hres = create_error(ctx, constr, &id, buf, &err);
415 if(FAILED(hres))
416 return hres;
418 if(!ei)
419 return id;
421 V_VT(&ei->var) = VT_DISPATCH;
422 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
424 return id;
427 HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
429 return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
432 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
434 return throw_error(ctx, ei, id, str, ctx->error_constr);
437 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
439 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
442 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
444 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
447 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
449 return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
452 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
454 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
457 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
459 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
462 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
464 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);