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