setupapi/tests: Fix some test failures on Win98.
[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 inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp)
43 {
44     return (ErrorInstance*)vdisp->u.jsdisp;
45 }
46
47 static HRESULT Error_number(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
48         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
49 {
50     ErrorInstance *This = error_from_vdisp(jsthis);
51
52     TRACE("\n");
53
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;
62     }
63 }
64
65 static HRESULT Error_description(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
66         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
67 {
68     ErrorInstance *This = error_from_vdisp(jsthis);
69
70     TRACE("\n");
71
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;
80     }
81 }
82
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)
86 {
87     ErrorInstance *This = error_from_vdisp(jsthis);
88
89     TRACE("\n");
90
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;
99     }
100 }
101
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)
105 {
106     static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
107
108     TRACE("\n");
109
110     if(retv) {
111         V_VT(retv) = VT_BSTR;
112         V_BSTR(retv) = SysAllocString(str);
113         if(!V_BSTR(retv))
114             return E_OUTOFMEMORY;
115     }
116
117     return S_OK;
118 }
119
120 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
121         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
122 {
123     TRACE("\n");
124
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;
131     }
132
133     return S_OK;
134 }
135
136 static void Error_destructor(DispatchEx *dispex)
137 {
138     ErrorInstance *This = (ErrorInstance*)dispex;
139
140     VariantClear(&This->number);
141     VariantClear(&This->description);
142     VariantClear(&This->message);
143     heap_free(This);
144 }
145
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}
151 };
152
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
160 };
161
162 static const builtin_prop_t ErrorInst_props[] = {
163     {descriptionW,              Error_description,                  0},
164     {messageW,                  Error_message,                      0},
165     {numberW,                   Error_number,                       0},
166 };
167
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
175 };
176
177 static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
178         DispatchEx *constr, ErrorInstance **ret)
179 {
180     ErrorInstance *err;
181     HRESULT hres;
182
183     err = heap_alloc_zero(sizeof(ErrorInstance));
184     if(!err)
185         return E_OUTOFMEMORY;
186
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;
195     }
196
197     *ret = err;
198     return S_OK;
199 }
200
201 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
202         const UINT *number, const WCHAR *msg, DispatchEx **ret)
203 {
204     ErrorInstance *err;
205     HRESULT hres;
206
207     hres = alloc_error(ctx, NULL, constr, &err);
208     if(FAILED(hres))
209         return hres;
210
211     if(number) {
212         V_VT(&err->number) = VT_I4;
213         V_I4(&err->number) = *number;
214     }
215
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);
219
220     VariantCopy(&err->description, &err->message);
221
222     if(!V_BSTR(&err->message)) {
223         heap_free(err);
224         return E_OUTOFMEMORY;
225     }
226
227     *ret = &err->dispex;
228     return S_OK;
229 }
230
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;
238
239     V_VT(&numv) = VT_NULL;
240
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);
249
250         if(FAILED(hres))
251             return hres;
252     }
253
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;
258     }
259
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);
267
268         if(FAILED(hres))
269             return hres;
270
271         if(retv) {
272             V_VT(retv) = VT_DISPATCH;
273             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
274         }
275         else
276             jsdisp_release(err);
277
278         return S_OK;
279
280     default:
281         FIXME("unimplemented flags %x\n", flags);
282         return E_NOTIMPL;
283     }
284 }
285
286 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
287         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
288 {
289     TRACE("\n");
290     return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
291 }
292
293 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
294         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
295 {
296     TRACE("\n");
297     return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
298 }
299
300 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
301         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
302 {
303     TRACE("\n");
304     return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
305 }
306
307 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
308         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
309 {
310     TRACE("\n");
311     return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
312 }
313
314 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
315         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
316 {
317     TRACE("\n");
318     return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
319 }
320
321 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
322         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
323 {
324     TRACE("\n");
325     return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
326 }
327
328 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
329         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
330 {
331     TRACE("\n");
332     return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
333 }
334
335 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
336         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
337 {
338     TRACE("\n");
339     return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
340 }
341
342 HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
343 {
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};
362
363     ErrorInstance *err;
364     INT i;
365     VARIANT v;
366     HRESULT hres;
367
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;
372
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;
378         }
379
380         hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
381
382         if(SUCCEEDED(hres))
383             hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
384                     PROPF_CONSTR, &err->dispex, constr_addr[i]);
385
386         jsdisp_release(&err->dispex);
387         VariantClear(&v);
388         if(FAILED(hres))
389             return hres;
390     }
391
392     return S_OK;
393 }
394
395 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
396 {
397     WCHAR buf[1024], *pos = NULL;
398     DispatchEx *err;
399     HRESULT hres;
400
401     buf[0] = '\0';
402     LoadStringW(jscript_hinstance, id&0xFFFF,  buf, sizeof(buf)/sizeof(WCHAR));
403
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));
409     }
410
411     WARN("%s\n", debugstr_w(buf));
412
413     id |= JSCRIPT_ERROR;
414     hres = create_error(ctx, constr, &id, buf, &err);
415     if(FAILED(hres))
416         return hres;
417
418     if(!ei)
419         return id;
420
421     V_VT(&ei->var) = VT_DISPATCH;
422     V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
423
424     return id;
425 }
426
427 HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
428 {
429     return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
430 }
431
432 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
433 {
434     return throw_error(ctx, ei, id, str, ctx->error_constr);
435 }
436
437 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
438 {
439     return throw_error(ctx, ei, id, str, ctx->range_error_constr);
440 }
441
442 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
443 {
444     return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
445 }
446
447 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
448 {
449     return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
450 }
451
452 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
453 {
454     return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
455 }
456
457 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
458 {
459     return throw_error(ctx, ei, id, str, ctx->type_error_constr);
460 }
461
462 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
463 {
464     return throw_error(ctx, ei, id, str, ctx->uri_error_constr);
465 }