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