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