jscript: Added new variable representation and use it for internal function return...
[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         unsigned argc, VARIANT *argv, jsval_t *r, 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(r) {
52             BSTR ret = SysAllocString(object_errorW);
53             if(!ret)
54                 return E_OUTOFMEMORY;
55             *r = jsval_string(ret);
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(r)
120         *r = jsval_string(ret);
121     else
122         SysFreeString(ret);
123     return S_OK;
124 }
125
126 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
127         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
128 {
129     TRACE("\n");
130
131     switch(flags) {
132     case INVOKE_FUNC:
133         return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
134     default:
135         FIXME("unimplemented flags %x\n", flags);
136         return E_NOTIMPL;
137     }
138
139     return S_OK;
140 }
141
142 static const builtin_prop_t Error_props[] = {
143     {toStringW,                 Error_toString,                     PROPF_METHOD}
144 };
145
146 static const builtin_info_t Error_info = {
147     JSCLASS_ERROR,
148     {NULL, Error_value, 0},
149     sizeof(Error_props)/sizeof(*Error_props),
150     Error_props,
151     NULL,
152     NULL
153 };
154
155 static const builtin_info_t ErrorInst_info = {
156     JSCLASS_ERROR,
157     {NULL, Error_value, 0},
158     0,
159     NULL,
160     NULL,
161     NULL
162 };
163
164 static HRESULT alloc_error(script_ctx_t *ctx, jsdisp_t *prototype,
165         jsdisp_t *constr, jsdisp_t **ret)
166 {
167     jsdisp_t *err;
168     HRESULT hres;
169
170     err = heap_alloc_zero(sizeof(*err));
171     if(!err)
172         return E_OUTOFMEMORY;
173
174     if(prototype)
175         hres = init_dispex(err, ctx, &Error_info, prototype);
176     else
177         hres = init_dispex_from_constr(err, ctx, &ErrorInst_info,
178             constr ? constr : ctx->error_constr);
179     if(FAILED(hres)) {
180         heap_free(err);
181         return hres;
182     }
183
184     *ret = err;
185     return S_OK;
186 }
187
188 static HRESULT create_error(script_ctx_t *ctx, jsdisp_t *constr,
189         UINT number, const WCHAR *msg, jsdisp_t **ret)
190 {
191     jsdisp_t *err;
192     VARIANT v;
193     HRESULT hres;
194
195     hres = alloc_error(ctx, NULL, constr, &err);
196     if(FAILED(hres))
197         return hres;
198
199     num_set_int(&v, number);
200     hres = jsdisp_propput_name(err, numberW, &v, NULL/*FIXME*/);
201     if(FAILED(hres)) {
202         jsdisp_release(err);
203         return hres;
204     }
205
206     V_VT(&v) = VT_BSTR;
207     if(msg) V_BSTR(&v) = SysAllocString(msg);
208     else V_BSTR(&v) = SysAllocStringLen(NULL, 0);
209     if(V_BSTR(&v)) {
210         hres = jsdisp_propput_name(err, messageW, &v, NULL/*FIXME*/);
211         if(SUCCEEDED(hres))
212             hres = jsdisp_propput_name(err, descriptionW, &v, NULL/*FIXME*/);
213         SysFreeString(V_BSTR(&v));
214     }else {
215         hres = E_OUTOFMEMORY;
216     }
217     if(FAILED(hres)) {
218         jsdisp_release(err);
219         return hres;
220     }
221
222     *ret = err;
223     return S_OK;
224 }
225
226 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, unsigned argc, VARIANT *argv,
227         jsval_t *r, jsexcept_t *ei, jsdisp_t *constr) {
228     jsdisp_t *err;
229     UINT num = 0;
230     BSTR msg = NULL;
231     HRESULT hres;
232
233     if(argc) {
234         double n;
235
236         hres = to_number(ctx, argv, ei, &n);
237         if(FAILED(hres)) /* FIXME: really? */
238             n = NAN;
239         if(isnan(n))
240             hres = to_string(ctx, argv, ei, &msg);
241         if(FAILED(hres))
242             return hres;
243         num = n;
244     }
245
246     if(argc>1 && !msg) {
247         hres = to_string(ctx, argv+1, ei, &msg);
248         if(FAILED(hres))
249             return hres;
250     }
251
252     switch(flags) {
253     case INVOKE_FUNC:
254     case DISPATCH_CONSTRUCT:
255         hres = create_error(ctx, constr, num, msg, &err);
256         SysFreeString(msg);
257
258         if(FAILED(hres))
259             return hres;
260
261         if(r)
262             *r = jsval_obj(err);
263         else
264             jsdisp_release(err);
265         return S_OK;
266
267     default:
268         FIXME("unimplemented flags %x\n", flags);
269         return E_NOTIMPL;
270     }
271 }
272
273 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
274         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
275 {
276     TRACE("\n");
277     return error_constr(ctx, flags, argc, argv, r, ei, ctx->error_constr);
278 }
279
280 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
281         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
282 {
283     TRACE("\n");
284     return error_constr(ctx, flags, argc, argv, r, ei, ctx->eval_error_constr);
285 }
286
287 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
288         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
289 {
290     TRACE("\n");
291     return error_constr(ctx, flags, argc, argv, r, ei, ctx->range_error_constr);
292 }
293
294 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
295         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
296 {
297     TRACE("\n");
298     return error_constr(ctx, flags, argc, argv, r, ei, ctx->reference_error_constr);
299 }
300
301 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
302         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
303 {
304     TRACE("\n");
305     return error_constr(ctx, flags, argc, argv, r, ei, ctx->regexp_error_constr);
306 }
307
308 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
309         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
310 {
311     TRACE("\n");
312     return error_constr(ctx, flags, argc, argv, r, ei, ctx->syntax_error_constr);
313 }
314
315 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
316         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
317 {
318     TRACE("\n");
319     return error_constr(ctx, flags, argc, argv, r, ei, ctx->type_error_constr);
320 }
321
322 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
323         unsigned argc, VARIANT *argv, jsval_t *r, jsexcept_t *ei)
324 {
325     TRACE("\n");
326     return error_constr(ctx, flags, argc, argv, r, ei, ctx->uri_error_constr);
327 }
328
329 HRESULT init_error_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
330 {
331     static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
332     static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
333     static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
334     static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
335     static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
336     static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
337     static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
338     static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
339     static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
340         ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
341     jsdisp_t **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
342         &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
343         &ctx->syntax_error_constr, &ctx->type_error_constr,
344         &ctx->uri_error_constr};
345     static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
346         RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
347         SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
348
349     jsdisp_t *err;
350     INT i;
351     VARIANT v;
352     HRESULT hres;
353
354     for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
355         hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
356         if(FAILED(hres))
357             return hres;
358
359         V_VT(&v) = VT_BSTR;
360         V_BSTR(&v) = SysAllocString(names[i]);
361         if(!V_BSTR(&v)) {
362             jsdisp_release(err);
363             return E_OUTOFMEMORY;
364         }
365
366         hres = jsdisp_propput_name(err, nameW, &v, NULL/*FIXME*/);
367
368         if(SUCCEEDED(hres))
369             hres = create_builtin_constructor(ctx, constr_val[i], names[i], NULL,
370                     PROPF_CONSTR|1, err, constr_addr[i]);
371
372         jsdisp_release(err);
373         VariantClear(&v);
374         if(FAILED(hres))
375             return hres;
376     }
377
378     return S_OK;
379 }
380
381 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str, jsdisp_t *constr)
382 {
383     WCHAR buf[1024], *pos = NULL;
384     jsdisp_t *err;
385     HRESULT hres;
386
387     if(!is_jscript_error(error))
388         return error;
389
390     buf[0] = '\0';
391     LoadStringW(jscript_hinstance, HRESULT_CODE(error),  buf, sizeof(buf)/sizeof(WCHAR));
392
393     if(str) pos = strchrW(buf, '|');
394     if(pos) {
395         int len = strlenW(str);
396         memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
397         memcpy(pos, str, len*sizeof(WCHAR));
398     }
399
400     WARN("%s\n", debugstr_w(buf));
401
402     hres = create_error(ctx, constr, error, buf, &err);
403     if(FAILED(hres))
404         return hres;
405
406     if(ei)
407         var_set_jsdisp(&ei->var, err);
408     return error;
409 }
410
411 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
412 {
413     return throw_error(ctx, ei, error, str, ctx->error_constr);
414 }
415
416 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
417 {
418     return throw_error(ctx, ei, error, str, ctx->range_error_constr);
419 }
420
421 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
422 {
423     return throw_error(ctx, ei, error, str, ctx->reference_error_constr);
424 }
425
426 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
427 {
428     return throw_error(ctx, ei, error, str, ctx->regexp_error_constr);
429 }
430
431 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
432 {
433     return throw_error(ctx, ei, error, str, ctx->syntax_error_constr);
434 }
435
436 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
437 {
438     return throw_error(ctx, ei, error, str, ctx->type_error_constr);
439 }
440
441 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
442 {
443     return throw_error(ctx, ei, error, str, ctx->uri_error_constr);
444 }