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