winex11: Separate fetching the window icon bits and setting the WM hints.
[wine.git] / dlls / jscript / error.c
blobcc602f642de5be0603d4503e7fa59300f24a0587
1 /*
2 * Copyright 2009 Piotr Caban
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.
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.
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
24 #include "jscript.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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};
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)
40 jsdisp_t *jsthis;
41 BSTR name = NULL, msg = NULL, ret = NULL;
42 jsval_t v;
43 HRESULT hres;
45 static const WCHAR object_errorW[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
47 TRACE("\n");
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);
57 return S_OK;
60 hres = jsdisp_propget_name(jsthis, nameW, &v);
61 if(FAILED(hres))
62 return hres;
64 if(!is_undefined(v)) {
65 hres = to_string(ctx, v, &name);
66 jsval_release(v);
67 if(FAILED(hres))
68 return hres;
69 if(!*name) {
70 SysFreeString(name);
71 name = NULL;
75 hres = jsdisp_propget_name(jsthis, messageW, &v);
76 if(SUCCEEDED(hres)) {
77 if(!is_undefined(v)) {
78 hres = to_string(ctx, v, &msg);
79 jsval_release(v);
80 if(SUCCEEDED(hres) && !*msg) {
81 SysFreeString(msg);
82 msg = NULL;
87 if(SUCCEEDED(hres)) {
88 if(name && msg) {
89 DWORD name_len, msg_len;
91 name_len = SysStringLen(name);
92 msg_len = SysStringLen(msg);
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));
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);
112 SysFreeString(msg);
113 SysFreeString(name);
114 if(FAILED(hres))
115 return hres;
116 if(!ret)
117 return E_OUTOFMEMORY;
119 if(r)
120 *r = jsval_string(ret);
121 else
122 SysFreeString(ret);
123 return S_OK;
126 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
127 unsigned argc, jsval_t *argv, jsval_t *r)
129 TRACE("\n");
131 switch(flags) {
132 case INVOKE_FUNC:
133 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
134 default:
135 FIXME("unimplemented flags %x\n", flags);
136 return E_NOTIMPL;
139 return S_OK;
142 static const builtin_prop_t Error_props[] = {
143 {toStringW, Error_toString, PROPF_METHOD}
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
155 static const builtin_info_t ErrorInst_info = {
156 JSCLASS_ERROR,
157 {NULL, Error_value, 0},
159 NULL,
160 NULL,
161 NULL
164 static HRESULT alloc_error(script_ctx_t *ctx, jsdisp_t *prototype,
165 jsdisp_t *constr, jsdisp_t **ret)
167 jsdisp_t *err;
168 HRESULT hres;
170 err = heap_alloc_zero(sizeof(*err));
171 if(!err)
172 return E_OUTOFMEMORY;
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;
184 *ret = err;
185 return S_OK;
188 static HRESULT create_error(script_ctx_t *ctx, jsdisp_t *constr,
189 UINT number, const WCHAR *msg, jsdisp_t **ret)
191 jsdisp_t *err;
192 BSTR str;
193 HRESULT hres;
195 hres = alloc_error(ctx, NULL, constr, &err);
196 if(FAILED(hres))
197 return hres;
199 hres = jsdisp_propput_name(err, numberW, jsval_number((INT)number));
200 if(FAILED(hres)) {
201 jsdisp_release(err);
202 return hres;
205 if(msg) str = SysAllocString(msg);
206 else str = SysAllocStringLen(NULL, 0);
207 if(str) {
208 hres = jsdisp_propput_name(err, messageW, jsval_string(str));
209 if(SUCCEEDED(hres))
210 hres = jsdisp_propput_name(err, descriptionW, jsval_string(str));
211 SysFreeString(str);
212 }else {
213 hres = E_OUTOFMEMORY;
215 if(FAILED(hres)) {
216 jsdisp_release(err);
217 return hres;
220 *ret = err;
221 return S_OK;
224 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, unsigned argc, jsval_t *argv,
225 jsval_t *r, jsdisp_t *constr) {
226 jsdisp_t *err;
227 UINT num = 0;
228 BSTR msg = NULL;
229 HRESULT hres;
231 if(argc) {
232 double n;
234 hres = to_number(ctx, argv[0], &n);
235 if(FAILED(hres)) /* FIXME: really? */
236 n = NAN;
237 if(isnan(n))
238 hres = to_string(ctx, argv[0], &msg);
239 if(FAILED(hres))
240 return hres;
241 num = n;
244 if(argc>1 && !msg) {
245 hres = to_string(ctx, argv[1], &msg);
246 if(FAILED(hres))
247 return hres;
250 switch(flags) {
251 case INVOKE_FUNC:
252 case DISPATCH_CONSTRUCT:
253 hres = create_error(ctx, constr, num, msg, &err);
254 SysFreeString(msg);
256 if(FAILED(hres))
257 return hres;
259 if(r)
260 *r = jsval_obj(err);
261 else
262 jsdisp_release(err);
263 return S_OK;
265 default:
266 FIXME("unimplemented flags %x\n", flags);
267 return E_NOTIMPL;
271 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
272 unsigned argc, jsval_t *argv, jsval_t *r)
274 TRACE("\n");
275 return error_constr(ctx, flags, argc, argv, r, ctx->error_constr);
278 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
279 unsigned argc, jsval_t *argv, jsval_t *r)
281 TRACE("\n");
282 return error_constr(ctx, flags, argc, argv, r, ctx->eval_error_constr);
285 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
286 unsigned argc, jsval_t *argv, jsval_t *r)
288 TRACE("\n");
289 return error_constr(ctx, flags, argc, argv, r, ctx->range_error_constr);
292 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
293 unsigned argc, jsval_t *argv, jsval_t *r)
295 TRACE("\n");
296 return error_constr(ctx, flags, argc, argv, r, ctx->reference_error_constr);
299 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
300 unsigned argc, jsval_t *argv, jsval_t *r)
302 TRACE("\n");
303 return error_constr(ctx, flags, argc, argv, r, ctx->regexp_error_constr);
306 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
307 unsigned argc, jsval_t *argv, jsval_t *r)
309 TRACE("\n");
310 return error_constr(ctx, flags, argc, argv, r, ctx->syntax_error_constr);
313 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
314 unsigned argc, jsval_t *argv, jsval_t *r)
316 TRACE("\n");
317 return error_constr(ctx, flags, argc, argv, r, ctx->type_error_constr);
320 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
321 unsigned argc, jsval_t *argv, jsval_t *r)
323 TRACE("\n");
324 return error_constr(ctx, flags, argc, argv, r, ctx->uri_error_constr);
327 HRESULT init_error_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
329 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
330 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
331 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
332 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
333 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
334 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
335 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
336 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
337 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
338 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
339 jsdisp_t **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
340 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
341 &ctx->syntax_error_constr, &ctx->type_error_constr,
342 &ctx->uri_error_constr};
343 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
344 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
345 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
347 jsdisp_t *err;
348 INT i;
349 BSTR str;
350 HRESULT hres;
352 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
353 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
354 if(FAILED(hres))
355 return hres;
357 str = SysAllocString(names[i]);
358 if(!str) {
359 jsdisp_release(err);
360 return E_OUTOFMEMORY;
363 hres = jsdisp_propput_name(err, nameW, jsval_string(str));
364 SysFreeString(str);
365 if(SUCCEEDED(hres))
366 hres = create_builtin_constructor(ctx, constr_val[i], names[i], NULL,
367 PROPF_CONSTR|1, err, constr_addr[i]);
369 jsdisp_release(err);
370 if(FAILED(hres))
371 return hres;
374 return S_OK;
377 static HRESULT throw_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str, jsdisp_t *constr)
379 WCHAR buf[1024], *pos = NULL;
380 jsdisp_t *err;
381 HRESULT hres;
383 if(!is_jscript_error(error))
384 return error;
386 buf[0] = '\0';
387 LoadStringW(jscript_hinstance, HRESULT_CODE(error), buf, sizeof(buf)/sizeof(WCHAR));
389 if(str) pos = strchrW(buf, '|');
390 if(pos) {
391 int len = strlenW(str);
392 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
393 memcpy(pos, str, len*sizeof(WCHAR));
396 WARN("%s\n", debugstr_w(buf));
398 hres = create_error(ctx, constr, error, buf, &err);
399 if(FAILED(hres))
400 return hres;
402 jsval_release(ctx->ei.val);
403 ctx->ei.val = jsval_obj(err);
404 return error;
407 HRESULT throw_generic_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
409 return throw_error(ctx, error, str, ctx->error_constr);
412 HRESULT throw_range_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
414 return throw_error(ctx, error, str, ctx->range_error_constr);
417 HRESULT throw_reference_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
419 return throw_error(ctx, error, str, ctx->reference_error_constr);
422 HRESULT throw_regexp_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
424 return throw_error(ctx, error, str, ctx->regexp_error_constr);
427 HRESULT throw_syntax_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
429 return throw_error(ctx, error, str, ctx->syntax_error_constr);
432 HRESULT throw_type_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
434 return throw_error(ctx, error, str, ctx->type_error_constr);
437 HRESULT throw_uri_error(script_ctx_t *ctx, HRESULT error, const WCHAR *str)
439 return throw_error(ctx, error, str, ctx->uri_error_constr);