jscript: Avoid using dispex->ctx.
[wine] / dlls / jscript / error.c
1 /*
2  * Copyright 2009 Piotr Caban
3  *
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.
8  *
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.
13  *
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
17  */
18 #include "config.h"
19 #include "wine/port.h"
20
21 #include <math.h>
22
23 #include "jscript.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28
29 typedef struct {
30     DispatchEx dispex;
31
32     VARIANT number;
33     VARIANT description;
34     VARIANT message;
35 } ErrorInstance;
36
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};
41
42 static HRESULT Error_number(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
43         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
44 {
45     ErrorInstance *This = (ErrorInstance*)dispex;
46
47     TRACE("\n");
48
49     switch(flags) {
50     case DISPATCH_PROPERTYGET:
51         return VariantCopy(retv, &This->number);
52     case DISPATCH_PROPERTYPUT:
53         return VariantCopy(&This->number, get_arg(dp, 0));
54     default:
55         FIXME("unimplemented flags %x\n", flags);
56         return E_NOTIMPL;
57     }
58 }
59
60 static HRESULT Error_description(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
61         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
62 {
63     ErrorInstance *This = (ErrorInstance*)dispex;
64
65     TRACE("\n");
66
67     switch(flags) {
68     case DISPATCH_PROPERTYGET:
69         return VariantCopy(retv, &This->description);
70     case DISPATCH_PROPERTYPUT:
71         return VariantCopy(&This->description, get_arg(dp, 0));
72     default:
73         FIXME("unimplemented flags %x\n", flags);
74         return E_NOTIMPL;
75     }
76 }
77
78 /* ECMA-262 3rd Edition    15.11.4.3 */
79 static HRESULT Error_message(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
80         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
81 {
82     ErrorInstance *This = (ErrorInstance*)dispex;
83
84     TRACE("\n");
85
86     switch(flags) {
87     case DISPATCH_PROPERTYGET:
88         return VariantCopy(retv, &This->message);
89     case DISPATCH_PROPERTYPUT:
90         return VariantCopy(&This->message, get_arg(dp, 0));
91     default:
92         FIXME("unimplemented flags %x\n", flags);
93         return E_NOTIMPL;
94     }
95 }
96
97 /* ECMA-262 3rd Edition    15.11.4.4 */
98 static HRESULT Error_toString(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
99         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
100 {
101     static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
102
103     TRACE("\n");
104
105     if(retv) {
106         V_VT(retv) = VT_BSTR;
107         V_BSTR(retv) = SysAllocString(str);
108         if(!V_BSTR(retv))
109             return E_OUTOFMEMORY;
110     }
111
112     return S_OK;
113 }
114
115 static HRESULT Error_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
116         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
117 {
118     TRACE("\n");
119
120     switch(flags) {
121     case INVOKE_FUNC:
122         return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
123     default:
124         FIXME("unimplemented flags %x\n", flags);
125         return E_NOTIMPL;
126     }
127
128     return S_OK;
129 }
130
131 static void Error_destructor(DispatchEx *dispex)
132 {
133     ErrorInstance *This = (ErrorInstance*)dispex;
134
135     VariantClear(&This->number);
136     VariantClear(&This->description);
137     VariantClear(&This->message);
138     heap_free(This);
139 }
140
141 static const builtin_prop_t Error_props[] = {
142     {descriptionW,              Error_description,                  0},
143     {messageW,                  Error_message,                      0},
144     {numberW,                   Error_number,                       0},
145     {toStringW,                 Error_toString,                     PROPF_METHOD}
146 };
147
148 static const builtin_info_t Error_info = {
149     JSCLASS_ERROR,
150     {NULL, Error_value, 0},
151     sizeof(Error_props)/sizeof(*Error_props),
152     Error_props,
153     Error_destructor,
154     NULL
155 };
156
157 static const builtin_prop_t ErrorInst_props[] = {
158     {descriptionW,              Error_description,                  0},
159     {messageW,                  Error_message,                      0},
160     {numberW,                   Error_number,                       0},
161 };
162
163 static const builtin_info_t ErrorInst_info = {
164     JSCLASS_ERROR,
165     {NULL, Error_value, 0},
166     sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
167     ErrorInst_props,
168     Error_destructor,
169     NULL
170 };
171
172 static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
173         DispatchEx *constr, ErrorInstance **ret)
174 {
175     ErrorInstance *err;
176     HRESULT hres;
177
178     err = heap_alloc_zero(sizeof(ErrorInstance));
179     if(!err)
180         return E_OUTOFMEMORY;
181
182     if(prototype)
183         hres = init_dispex(&err->dispex, ctx, &Error_info, prototype);
184     else
185         hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info,
186             constr ? constr : ctx->error_constr);
187     if(FAILED(hres)) {
188         heap_free(err);
189         return hres;
190     }
191
192     *ret = err;
193     return S_OK;
194 }
195
196 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
197         const UINT *number, const WCHAR *msg, DispatchEx **ret)
198 {
199     ErrorInstance *err;
200     HRESULT hres;
201
202     hres = alloc_error(ctx, NULL, constr, &err);
203     if(FAILED(hres))
204         return hres;
205
206     if(number) {
207         V_VT(&err->number) = VT_I4;
208         V_I4(&err->number) = *number;
209     }
210
211     V_VT(&err->message) = VT_BSTR;
212     if(msg) V_BSTR(&err->message) = SysAllocString(msg);
213     else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
214
215     VariantCopy(&err->description, &err->message);
216
217     if(!V_BSTR(&err->message)) {
218         heap_free(err);
219         return E_OUTOFMEMORY;
220     }
221
222     *ret = &err->dispex;
223     return S_OK;
224 }
225
226 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
227         VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
228     DispatchEx *err;
229     VARIANT numv;
230     UINT num;
231     BSTR msg = NULL;
232     HRESULT hres;
233
234     V_VT(&numv) = VT_NULL;
235
236     if(arg_cnt(dp)) {
237         hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
238         if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
239             hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
240         else if(V_VT(&numv) == VT_I4)
241             num = V_I4(&numv);
242         else
243             num = V_R8(&numv);
244
245         if(FAILED(hres))
246             return hres;
247     }
248
249     if(arg_cnt(dp)>1 && !msg) {
250         hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
251         if(FAILED(hres))
252             return hres;
253     }
254
255     switch(flags) {
256     case INVOKE_FUNC:
257     case DISPATCH_CONSTRUCT:
258         if(V_VT(&numv) == VT_NULL)
259             hres = create_error(ctx, constr, NULL, msg, &err);
260         else
261             hres = create_error(ctx, constr, &num, msg, &err);
262
263         if(FAILED(hres))
264             return hres;
265
266         if(retv) {
267             V_VT(retv) = VT_DISPATCH;
268             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
269         }
270         else
271             jsdisp_release(err);
272
273         return S_OK;
274
275     default:
276         FIXME("unimplemented flags %x\n", flags);
277         return E_NOTIMPL;
278     }
279 }
280
281 static HRESULT ErrorConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
282         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
283 {
284     TRACE("\n");
285     return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
286 }
287
288 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
289         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
290 {
291     TRACE("\n");
292     return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
293 }
294
295 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
296         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
297 {
298     TRACE("\n");
299     return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
300 }
301
302 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
303         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
304 {
305     TRACE("\n");
306     return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
307 }
308
309 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
310         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
311 {
312     TRACE("\n");
313     return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
314 }
315
316 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
317         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
318 {
319     TRACE("\n");
320     return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
321 }
322
323 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
324         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
325 {
326     TRACE("\n");
327     return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
328 }
329
330 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, DispatchEx *dispex, WORD flags,
331         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
332 {
333     TRACE("\n");
334     return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
335 }
336
337 HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
338 {
339     static const WCHAR nameW[] = {'n','a','m','e',0};
340     static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
341     static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
342     static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
343     static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
344     static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
345     static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
346     static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
347     static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
348     static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
349         ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
350     DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
351         &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
352         &ctx->syntax_error_constr, &ctx->type_error_constr,
353         &ctx->uri_error_constr};
354     static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
355         RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
356         SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
357
358     ErrorInstance *err;
359     INT i;
360     VARIANT v;
361     HRESULT hres;
362
363     for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
364         hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
365         if(FAILED(hres))
366             return hres;
367
368         V_VT(&v) = VT_BSTR;
369         V_BSTR(&v) = SysAllocString(names[i]);
370         if(!V_BSTR(&v)) {
371             jsdisp_release(&err->dispex);
372             return E_OUTOFMEMORY;
373         }
374
375         hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
376
377         if(SUCCEEDED(hres))
378             hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
379                     PROPF_CONSTR, &err->dispex, constr_addr[i]);
380
381         jsdisp_release(&err->dispex);
382         VariantClear(&v);
383         if(FAILED(hres))
384             return hres;
385     }
386
387     return S_OK;
388 }
389
390 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
391 {
392     WCHAR buf[1024], *pos = NULL;
393     DispatchEx *err;
394     HRESULT hres;
395
396     buf[0] = '\0';
397     LoadStringW(jscript_hinstance, id&0xFFFF,  buf, sizeof(buf)/sizeof(WCHAR));
398
399     if(str) pos = strchrW(buf, '|');
400     if(pos) {
401         int len = strlenW(str);
402         memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
403         memcpy(pos, str, len*sizeof(WCHAR));
404     }
405
406     WARN("%s\n", debugstr_w(buf));
407
408     id |= JSCRIPT_ERROR;
409     hres = create_error(ctx, constr, &id, buf, &err);
410     if(FAILED(hres))
411         return hres;
412
413     if(!ei)
414         return id;
415
416     V_VT(&ei->var) = VT_DISPATCH;
417     V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
418
419     return id;
420 }
421
422 HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
423 {
424     return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
425 }
426
427 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
428 {
429     return throw_error(ctx, ei, id, str, ctx->range_error_constr);
430 }
431
432 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
433 {
434     return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
435 }
436
437 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
438 {
439     return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
440 }
441
442 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
443 {
444     return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
445 }
446
447 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
448 {
449     return throw_error(ctx, ei, id, str, ctx->type_error_constr);
450 }
451
452 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
453 {
454     return throw_error(ctx, ei, id, str, ctx->uri_error_constr);
455 }