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