push(f) 02fc09c39e59d4e65f728da07b0bc5b717866e58
[wine/hacks.git] / dlls / jscript / error.c
blob25a3836cc8f5e00dd41a080c150bff52f8a86cd7
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
18 #include "config.h"
19 #include "wine/port.h"
21 #include <math.h>
23 #include "jscript.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 typedef struct {
30 DispatchEx dispex;
32 VARIANT number;
33 VARIANT description;
34 VARIANT message;
35 } ErrorInstance;
37 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
38 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
39 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
40 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
41 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
42 static const WCHAR propertyIsEnumerableW[] =
43 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
44 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
46 static HRESULT Error_number(DispatchEx *dispex, LCID lcid, WORD flags,
47 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
49 ErrorInstance *This = (ErrorInstance*)dispex;
51 TRACE("\n");
53 switch(flags) {
54 case DISPATCH_PROPERTYGET:
55 return VariantCopy(retv, &This->number);
56 case DISPATCH_PROPERTYPUT:
57 return VariantCopy(&This->number, get_arg(dp, 0));
58 default:
59 FIXME("unimplemented flags %x\n", flags);
60 return E_NOTIMPL;
64 static HRESULT Error_description(DispatchEx *dispex, LCID lcid, WORD flags,
65 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
67 ErrorInstance *This = (ErrorInstance*)dispex;
69 TRACE("\n");
71 switch(flags) {
72 case DISPATCH_PROPERTYGET:
73 return VariantCopy(retv, &This->description);
74 case DISPATCH_PROPERTYPUT:
75 return VariantCopy(&This->description, get_arg(dp, 0));
76 default:
77 FIXME("unimplemented flags %x\n", flags);
78 return E_NOTIMPL;
82 /* ECMA-262 3rd Edition 15.11.4.3 */
83 static HRESULT Error_message(DispatchEx *dispex, LCID lcid, WORD flags,
84 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
86 ErrorInstance *This = (ErrorInstance*)dispex;
88 TRACE("\n");
90 switch(flags) {
91 case DISPATCH_PROPERTYGET:
92 return VariantCopy(retv, &This->message);
93 case DISPATCH_PROPERTYPUT:
94 return VariantCopy(&This->message, get_arg(dp, 0));
95 default:
96 FIXME("unimplemented flags %x\n", flags);
97 return E_NOTIMPL;
101 /* ECMA-262 3rd Edition 15.11.4.4 */
102 static HRESULT Error_toString(DispatchEx *dispex, LCID lcid, WORD flags,
103 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
105 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
107 TRACE("\n");
109 if(retv) {
110 V_VT(retv) = VT_BSTR;
111 V_BSTR(retv) = SysAllocString(str);
112 if(!V_BSTR(retv))
113 return E_OUTOFMEMORY;
116 return S_OK;
119 static HRESULT Error_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags,
120 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
122 FIXME("\n");
123 return E_NOTIMPL;
126 static HRESULT Error_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags,
127 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
129 FIXME("\n");
130 return E_NOTIMPL;
134 static HRESULT Error_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags,
135 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
137 FIXME("\n");
138 return E_NOTIMPL;
141 static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags,
142 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
144 TRACE("\n");
146 switch(flags) {
147 case INVOKE_FUNC:
148 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
149 default:
150 FIXME("unimplemented flags %x\n", flags);
151 return E_NOTIMPL;
154 return S_OK;
157 static void Error_destructor(DispatchEx *dispex)
159 ErrorInstance *This = (ErrorInstance*)dispex;
161 VariantClear(&This->number);
162 VariantClear(&This->description);
163 VariantClear(&This->message);
164 heap_free(This);
167 static const builtin_prop_t Error_props[] = {
168 {descriptionW, Error_description, 0},
169 {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD},
170 {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD},
171 {messageW, Error_message, 0},
172 {numberW, Error_number, 0},
173 {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD},
174 {toStringW, Error_toString, PROPF_METHOD}
177 static const builtin_info_t Error_info = {
178 JSCLASS_ERROR,
179 {NULL, Error_value, 0},
180 sizeof(Error_props)/sizeof(*Error_props),
181 Error_props,
182 Error_destructor,
183 NULL
186 static const builtin_prop_t ErrorInst_props[] = {
187 {descriptionW, Error_description, 0},
188 {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD},
189 {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD},
190 {messageW, Error_message, 0},
191 {numberW, Error_number, 0},
192 {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD}
195 static const builtin_info_t ErrorInst_info = {
196 JSCLASS_ERROR,
197 {NULL, Error_value, 0},
198 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
199 ErrorInst_props,
200 Error_destructor,
201 NULL
204 static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype,
205 DispatchEx *constr, ErrorInstance **ret)
207 ErrorInstance *err;
208 DispatchEx *inherit;
209 HRESULT hres;
211 err = heap_alloc_zero(sizeof(ErrorInstance));
212 if(!err)
213 return E_OUTOFMEMORY;
215 inherit = error_prototype ? ctx->object_constr : ctx->error_constr;
216 hres = init_dispex_from_constr(&err->dispex, ctx,
217 error_prototype ? &Error_info : &ErrorInst_info,
218 constr ? constr : inherit);
219 if(FAILED(hres)) {
220 heap_free(err);
221 return hres;
224 *ret = err;
225 return S_OK;
228 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
229 const UINT *number, const WCHAR *msg, DispatchEx **ret)
231 ErrorInstance *err;
232 HRESULT hres;
234 hres = alloc_error(ctx, FALSE, constr, &err);
235 if(FAILED(hres))
236 return hres;
238 if(number) {
239 V_VT(&err->number) = VT_I4;
240 V_I4(&err->number) = *number;
243 V_VT(&err->message) = VT_BSTR;
244 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
245 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
247 VariantCopy(&err->description, &err->message);
249 if(!V_BSTR(&err->message)) {
250 heap_free(err);
251 return E_OUTOFMEMORY;
254 *ret = &err->dispex;
255 return S_OK;
258 static HRESULT error_constr(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
259 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
260 DispatchEx *err;
261 VARIANT numv;
262 UINT num;
263 BSTR msg = NULL;
264 HRESULT hres;
266 V_VT(&numv) = VT_NULL;
268 if(arg_cnt(dp)) {
269 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &numv);
270 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
271 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &msg);
272 else if(V_VT(&numv) == VT_I4)
273 num = V_I4(&numv);
274 else
275 num = V_R8(&numv);
277 if(FAILED(hres))
278 return hres;
281 if(arg_cnt(dp)>1 && !msg) {
282 hres = to_string(dispex->ctx, get_arg(dp, 1), ei, &msg);
283 if(FAILED(hres))
284 return hres;
287 switch(flags) {
288 case INVOKE_FUNC:
289 case DISPATCH_CONSTRUCT:
290 if(V_VT(&numv) == VT_NULL)
291 hres = create_error(dispex->ctx, constr, NULL, msg, &err);
292 else
293 hres = create_error(dispex->ctx, constr, &num, msg, &err);
295 if(FAILED(hres))
296 return hres;
298 if(retv) {
299 V_VT(retv) = VT_DISPATCH;
300 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
302 else
303 IDispatchEx_Release(_IDispatchEx_(err));
305 return S_OK;
307 default:
308 FIXME("unimplemented flags %x\n", flags);
309 return E_NOTIMPL;
313 static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
314 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
316 TRACE("\n");
317 return error_constr(dispex, flags, dp, retv, ei,
318 dispex->ctx->error_constr);
321 static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
322 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
324 TRACE("\n");
325 return error_constr(dispex, flags, dp, retv, ei,
326 dispex->ctx->eval_error_constr);
329 static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
330 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
332 TRACE("\n");
333 return error_constr(dispex, flags, dp, retv, ei,
334 dispex->ctx->range_error_constr);
337 static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
338 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
340 TRACE("\n");
341 return error_constr(dispex, flags, dp, retv, ei,
342 dispex->ctx->reference_error_constr);
345 static HRESULT SyntaxErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
346 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
348 TRACE("\n");
349 return error_constr(dispex, flags, dp, retv, ei,
350 dispex->ctx->syntax_error_constr);
353 static HRESULT TypeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
354 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
356 TRACE("\n");
357 return error_constr(dispex, flags, dp, retv, ei,
358 dispex->ctx->type_error_constr);
361 static HRESULT URIErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
362 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
364 TRACE("\n");
365 return error_constr(dispex, flags, dp, retv, ei,
366 dispex->ctx->uri_error_constr);
369 HRESULT init_error_constr(script_ctx_t *ctx)
371 static const WCHAR nameW[] = {'n','a','m','e',0};
372 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
373 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
374 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
375 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
376 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
377 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
378 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
379 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
380 ReferenceErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
381 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
382 &ctx->range_error_constr, &ctx->reference_error_constr,
383 &ctx->syntax_error_constr, &ctx->type_error_constr,
384 &ctx->uri_error_constr};
385 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
386 RangeErrorConstr_value, ReferenceErrorConstr_value, SyntaxErrorConstr_value,
387 TypeErrorConstr_value, URIErrorConstr_value};
389 ErrorInstance *err;
390 INT i;
391 VARIANT v;
392 HRESULT hres;
394 for(i=0; i<7; i++) {
395 hres = alloc_error(ctx, i==0, NULL, &err);
396 if(FAILED(hres))
397 return hres;
399 V_VT(&v) = VT_BSTR;
400 V_BSTR(&v) = SysAllocString(names[i]);
401 if(!V_BSTR(&v)) {
402 IDispatchEx_Release(_IDispatchEx_(&err->dispex));
403 return E_OUTOFMEMORY;
406 hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/);
408 if(SUCCEEDED(hres))
409 hres = create_builtin_function(ctx, constr_val[i], NULL,
410 PROPF_CONSTR, &err->dispex, constr_addr[i]);
412 IDispatchEx_Release(_IDispatchEx_(&err->dispex));
413 VariantClear(&v);
414 if(FAILED(hres))
415 return hres;
418 return S_OK;
421 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
423 WCHAR buf[1024], *pos = NULL;
424 DispatchEx *err;
425 HRESULT hres;
427 buf[0] = '\0';
428 LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR));
430 if(str) pos = strchrW(buf, '|');
431 if(pos) {
432 int len = strlenW(str);
433 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
434 memcpy(pos, str, len*sizeof(WCHAR));
437 WARN("%s\n", debugstr_w(buf));
439 id |= JSCRIPT_ERROR;
440 hres = create_error(ctx, constr, &id, buf, &err);
441 if(FAILED(hres))
442 return hres;
444 if(!ei)
445 return id;
447 V_VT(&ei->var) = VT_DISPATCH;
448 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
450 return id;
453 HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
455 return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
458 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
460 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
463 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
465 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
468 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
470 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
473 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
475 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
478 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
480 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);