blob: d160dd66f4b1e95d1b9b9a34508b1a15b682dc0a [file] [log] [blame]
Piotr Cabanf33f5c92009-07-20 18:17:51 +02001/*
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 */
Piotr Cabana77e3692009-07-20 18:18:22 +020018#include "config.h"
19#include "wine/port.h"
20
21#include <math.h>
Piotr Cabanf33f5c92009-07-20 18:17:51 +020022
23#include "jscript.h"
24
25#include "wine/debug.h"
26
27WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28
29typedef struct {
30 DispatchEx dispex;
31
Piotr Caban7c0a7022009-07-20 18:18:17 +020032 VARIANT number;
33 VARIANT description;
Piotr Cabanf33f5c92009-07-20 18:17:51 +020034 VARIANT message;
35} ErrorInstance;
36
Piotr Caban7c0a7022009-07-20 18:18:17 +020037static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
Piotr Cabanf33f5c92009-07-20 18:17:51 +020038static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
Piotr Caban7c0a7022009-07-20 18:18:17 +020039static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
Piotr Cabanf33f5c92009-07-20 18:17:51 +020040static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
41static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
42static 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};
44static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
45
Piotr Caban7c0a7022009-07-20 18:18:17 +020046static HRESULT Error_number(DispatchEx *dispex, LCID lcid, WORD flags,
47 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
48{
Piotr Cabanf3eef0d2009-07-20 18:18:19 +020049 ErrorInstance *This = (ErrorInstance*)dispex;
50
51 TRACE("\n");
52
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;
61 }
Piotr Caban7c0a7022009-07-20 18:18:17 +020062}
63
64static HRESULT Error_description(DispatchEx *dispex, LCID lcid, WORD flags,
65 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
66{
Piotr Cabanf3eef0d2009-07-20 18:18:19 +020067 ErrorInstance *This = (ErrorInstance*)dispex;
68
69 TRACE("\n");
70
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;
79 }
Piotr Caban7c0a7022009-07-20 18:18:17 +020080}
81
Piotr Cabanf33f5c92009-07-20 18:17:51 +020082/* ECMA-262 3rd Edition 15.11.4.3 */
83static HRESULT Error_message(DispatchEx *dispex, LCID lcid, WORD flags,
84 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
85{
Piotr Cabanf17b1f62009-07-20 18:17:54 +020086 ErrorInstance *This = (ErrorInstance*)dispex;
87
88 TRACE("\n");
89
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;
98 }
Piotr Cabanf33f5c92009-07-20 18:17:51 +020099}
100
Piotr Caban2d71dac2009-07-20 18:17:57 +0200101/* ECMA-262 3rd Edition 15.11.4.4 */
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200102static HRESULT Error_toString(DispatchEx *dispex, LCID lcid, WORD flags,
103 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
104{
Piotr Caban2d71dac2009-07-20 18:17:57 +0200105 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
106
107 TRACE("\n");
108
109 if(retv) {
110 V_VT(retv) = VT_BSTR;
111 V_BSTR(retv) = SysAllocString(str);
112 if(!V_BSTR(retv))
113 return E_OUTOFMEMORY;
114 }
115
116 return S_OK;
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200117}
118
119static HRESULT Error_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags,
120 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
121{
122 FIXME("\n");
123 return E_NOTIMPL;
124}
125
126static HRESULT Error_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags,
127 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
128{
129 FIXME("\n");
130 return E_NOTIMPL;
131}
132
133
134static HRESULT Error_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags,
135 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
136{
137 FIXME("\n");
138 return E_NOTIMPL;
139}
140
141static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags,
142 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
143{
144 FIXME("\n");
145 return E_NOTIMPL;
146}
147
148static void Error_destructor(DispatchEx *dispex)
149{
150 ErrorInstance *This = (ErrorInstance*)dispex;
151
Piotr Cabanf3eef0d2009-07-20 18:18:19 +0200152 VariantClear(&This->number);
153 VariantClear(&This->description);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200154 VariantClear(&This->message);
155 heap_free(This);
156}
157
158static const builtin_prop_t Error_props[] = {
Piotr Caban7c0a7022009-07-20 18:18:17 +0200159 {descriptionW, Error_description, 0},
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200160 {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD},
161 {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD},
162 {messageW, Error_message, 0},
Piotr Caban7c0a7022009-07-20 18:18:17 +0200163 {numberW, Error_number, 0},
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200164 {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD},
165 {toStringW, Error_toString, PROPF_METHOD}
166};
167
168static const builtin_info_t Error_info = {
169 JSCLASS_ERROR,
170 {NULL, Error_value, 0},
171 sizeof(Error_props)/sizeof(*Error_props),
172 Error_props,
173 Error_destructor,
174 NULL
175};
176
177static const builtin_prop_t ErrorInst_props[] = {
Piotr Caban7c0a7022009-07-20 18:18:17 +0200178 {descriptionW, Error_description, 0},
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200179 {hasOwnPropertyW, Error_hasOwnProperty, PROPF_METHOD},
180 {isPrototypeOfW, Error_isPrototypeOf, PROPF_METHOD},
181 {messageW, Error_message, 0},
Piotr Caban7c0a7022009-07-20 18:18:17 +0200182 {numberW, Error_number, 0},
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200183 {propertyIsEnumerableW, Error_propertyIsEnumerable, PROPF_METHOD}
184};
185
186static const builtin_info_t ErrorInst_info = {
187 JSCLASS_ERROR,
188 {NULL, Error_value, 0},
189 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
190 ErrorInst_props,
191 Error_destructor,
192 NULL
193};
194
195static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype,
196 DispatchEx *constr, ErrorInstance **ret)
197{
198 ErrorInstance *err;
199 DispatchEx *inherit;
200 HRESULT hres;
201
202 err = heap_alloc_zero(sizeof(ErrorInstance));
203 if(!err)
204 return E_OUTOFMEMORY;
205
206 inherit = error_prototype ? ctx->object_constr : ctx->error_constr;
207 hres = init_dispex_from_constr(&err->dispex, ctx,
208 error_prototype ? &Error_info : &ErrorInst_info,
209 constr ? constr : inherit);
210 if(FAILED(hres)) {
211 heap_free(err);
212 return hres;
213 }
214
215 *ret = err;
216 return S_OK;
217}
218
219static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
Piotr Cabana77e3692009-07-20 18:18:22 +0200220 const UINT *number, const WCHAR *msg, DispatchEx **ret)
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200221{
222 ErrorInstance *err;
223 HRESULT hres;
224
225 hres = alloc_error(ctx, FALSE, constr, &err);
226 if(FAILED(hres))
227 return hres;
228
Piotr Cabana77e3692009-07-20 18:18:22 +0200229 if(number) {
230 V_VT(&err->number) = VT_I4;
231 V_I4(&err->number) = *number;
232 }
233
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200234 V_VT(&err->message) = VT_BSTR;
235 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
236 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
237
Piotr Cabana77e3692009-07-20 18:18:22 +0200238 VariantCopy(&err->description, &err->message);
239
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200240 if(!V_BSTR(&err->message)) {
241 heap_free(err);
242 return E_OUTOFMEMORY;
243 }
244
245 *ret = &err->dispex;
246 return S_OK;
247}
248
249static HRESULT error_constr(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
250 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
251 DispatchEx *err;
Piotr Cabana77e3692009-07-20 18:18:22 +0200252 VARIANT numv;
253 UINT num;
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200254 BSTR msg = NULL;
255 HRESULT hres;
256
Piotr Cabana77e3692009-07-20 18:18:22 +0200257 V_VT(&numv) = VT_NULL;
258
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200259 if(arg_cnt(dp)) {
Piotr Cabana77e3692009-07-20 18:18:22 +0200260 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &numv);
261 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
262 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &msg);
263 else if(V_VT(&numv) == VT_I4)
264 num = V_I4(&numv);
265 else
266 num = V_R8(&numv);
267
268 if(FAILED(hres))
269 return hres;
270 }
271
272 if(arg_cnt(dp)>1 && !msg) {
273 hres = to_string(dispex->ctx, get_arg(dp, 1), ei, &msg);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200274 if(FAILED(hres))
275 return hres;
276 }
277
278 switch(flags) {
279 case INVOKE_FUNC:
280 case DISPATCH_CONSTRUCT:
Piotr Cabana77e3692009-07-20 18:18:22 +0200281 if(V_VT(&numv) == VT_NULL)
282 hres = create_error(dispex->ctx, constr, NULL, msg, &err);
283 else
284 hres = create_error(dispex->ctx, constr, &num, msg, &err);
285
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200286 if(FAILED(hres))
287 return hres;
288
289 if(retv) {
290 V_VT(retv) = VT_DISPATCH;
291 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
292 }
293 else
294 IDispatchEx_Release(_IDispatchEx_(err));
295
296 return S_OK;
297
298 default:
299 FIXME("unimplemented flags %x\n", flags);
300 return E_NOTIMPL;
301 }
302}
303
304static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
305 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
306{
307 TRACE("\n");
308 return error_constr(dispex, flags, dp, retv, ei,
309 dispex->ctx->error_constr);
310}
311
312static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
313 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
314{
315 TRACE("\n");
316 return error_constr(dispex, flags, dp, retv, ei,
317 dispex->ctx->eval_error_constr);
318}
319
320static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
321 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
322{
323 TRACE("\n");
324 return error_constr(dispex, flags, dp, retv, ei,
325 dispex->ctx->range_error_constr);
326}
327
328static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
329 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
330{
331 TRACE("\n");
332 return error_constr(dispex, flags, dp, retv, ei,
333 dispex->ctx->reference_error_constr);
334}
335
336static HRESULT SyntaxErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
337 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
338{
339 TRACE("\n");
340 return error_constr(dispex, flags, dp, retv, ei,
341 dispex->ctx->syntax_error_constr);
342}
343
344static HRESULT TypeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
345 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
346{
347 TRACE("\n");
348 return error_constr(dispex, flags, dp, retv, ei,
349 dispex->ctx->type_error_constr);
350}
351
352static HRESULT URIErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
353 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
354{
355 TRACE("\n");
356 return error_constr(dispex, flags, dp, retv, ei,
357 dispex->ctx->uri_error_constr);
358}
359
360HRESULT init_error_constr(script_ctx_t *ctx)
361{
362 static const WCHAR nameW[] = {'n','a','m','e',0};
363 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
364 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
365 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
366 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
367 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
368 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
369 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
370 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
371 ReferenceErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
372 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
373 &ctx->range_error_constr, &ctx->reference_error_constr,
374 &ctx->syntax_error_constr, &ctx->type_error_constr,
375 &ctx->uri_error_constr};
376 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
377 RangeErrorConstr_value, ReferenceErrorConstr_value, SyntaxErrorConstr_value,
378 TypeErrorConstr_value, URIErrorConstr_value};
379
380 ErrorInstance *err;
381 INT i;
382 VARIANT v;
383 HRESULT hres;
384
385 for(i=0; i<7; i++) {
386 hres = alloc_error(ctx, i==0, NULL, &err);
387 if(FAILED(hres))
388 return hres;
389
390 V_VT(&v) = VT_BSTR;
391 V_BSTR(&v) = SysAllocString(names[i]);
392 if(!V_BSTR(&v)) {
393 IDispatchEx_Release(_IDispatchEx_(&err->dispex));
394 return E_OUTOFMEMORY;
395 }
396
397 hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/);
398
399 if(SUCCEEDED(hres))
400 hres = create_builtin_function(ctx, constr_val[i], NULL,
401 PROPF_CONSTR, &err->dispex, constr_addr[i]);
402
403 IDispatchEx_Release(_IDispatchEx_(&err->dispex));
404 VariantClear(&v);
405 if(FAILED(hres))
406 return hres;
407 }
408
409 return S_OK;
410}
Piotr Caban469b5972009-07-20 18:18:00 +0200411
412static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
413{
414 WCHAR buf[1024], *pos = NULL;
415 DispatchEx *err;
416 HRESULT hres;
417
418 TRACE("\n");
419
420 LoadStringW(jscript_hinstance, id, buf, sizeof(buf)/sizeof(WCHAR));
421
422 if(str) pos = strchrW(buf, '|');
423 if(pos) {
424 int len = strlenW(str);
425 memmove(pos+len, pos+1, strlenW(pos+1)*sizeof(WCHAR));
426 memcpy(pos, str, len*sizeof(WCHAR));
427 }
428
Piotr Cabana77e3692009-07-20 18:18:22 +0200429 id |= 0x800A0000;
430 hres = create_error(ctx, constr, &id, buf, &err);
Piotr Caban469b5972009-07-20 18:18:00 +0200431 if(FAILED(hres))
432 return hres;
433
434 if(!ei)
435 return id;
436
437 V_VT(&ei->var) = VT_DISPATCH;
438 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
439
Piotr Cabana77e3692009-07-20 18:18:22 +0200440 return id;
Piotr Caban469b5972009-07-20 18:18:00 +0200441}
442
443HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
444{
445 return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
446}
447
448HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
449{
450 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
451}
452
453HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
454{
455 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
456}
457
458HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
459{
460 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
461}
462
463HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
464{
465 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
466}
467
468HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
469{
470 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);
471}