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