jscript: Lookup global members after script_disp.
[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 #include "config.h"
19 #include "wine/port.h"
20
21 #include <math.h>
22
23 #include "jscript.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28
29 typedef struct {
30     DispatchEx dispex;
31
32     VARIANT number;
33     VARIANT description;
34     VARIANT message;
35 } ErrorInstance;
36
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};
45
46 static HRESULT Error_number(DispatchEx *dispex, LCID lcid, WORD flags,
47         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
48 {
49     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     }
62 }
63
64 static HRESULT Error_description(DispatchEx *dispex, LCID lcid, WORD flags,
65         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
66 {
67     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     }
80 }
81
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)
85 {
86     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     }
99 }
100
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)
104 {
105     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;
117 }
118
119 static 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
126 static 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
134 static 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
141 static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags,
142         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
143 {
144     TRACE("\n");
145
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;
152     }
153
154     return S_OK;
155 }
156
157 static void Error_destructor(DispatchEx *dispex)
158 {
159     ErrorInstance *This = (ErrorInstance*)dispex;
160
161     VariantClear(&This->number);
162     VariantClear(&This->description);
163     VariantClear(&This->message);
164     heap_free(This);
165 }
166
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}
175 };
176
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
184 };
185
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}
193 };
194
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
202 };
203
204 static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype,
205         DispatchEx *constr, ErrorInstance **ret)
206 {
207     ErrorInstance *err;
208     DispatchEx *inherit;
209     HRESULT hres;
210
211     err = heap_alloc_zero(sizeof(ErrorInstance));
212     if(!err)
213         return E_OUTOFMEMORY;
214
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;
222     }
223
224     *ret = err;
225     return S_OK;
226 }
227
228 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
229         const UINT *number, const WCHAR *msg, DispatchEx **ret)
230 {
231     ErrorInstance *err;
232     HRESULT hres;
233
234     hres = alloc_error(ctx, FALSE, constr, &err);
235     if(FAILED(hres))
236         return hres;
237
238     if(number) {
239         V_VT(&err->number) = VT_I4;
240         V_I4(&err->number) = *number;
241     }
242
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);
246
247     VariantCopy(&err->description, &err->message);
248
249     if(!V_BSTR(&err->message)) {
250         heap_free(err);
251         return E_OUTOFMEMORY;
252     }
253
254     *ret = &err->dispex;
255     return S_OK;
256 }
257
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;
265
266     V_VT(&numv) = VT_NULL;
267
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);
276
277         if(FAILED(hres))
278             return hres;
279     }
280
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;
285     }
286
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);
294
295         if(FAILED(hres))
296             return hres;
297
298         if(retv) {
299             V_VT(retv) = VT_DISPATCH;
300             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
301         }
302         else
303             IDispatchEx_Release(_IDispatchEx_(err));
304
305         return S_OK;
306
307     default:
308         FIXME("unimplemented flags %x\n", flags);
309         return E_NOTIMPL;
310     }
311 }
312
313 static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
314         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
315 {
316     TRACE("\n");
317     return error_constr(dispex, flags, dp, retv, ei,
318             dispex->ctx->error_constr);
319 }
320
321 static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
322         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
323 {
324     TRACE("\n");
325     return error_constr(dispex, flags, dp, retv, ei,
326             dispex->ctx->eval_error_constr);
327 }
328
329 static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
330         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
331 {
332     TRACE("\n");
333     return error_constr(dispex, flags, dp, retv, ei,
334             dispex->ctx->range_error_constr);
335 }
336
337 static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
338         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
339 {
340     TRACE("\n");
341     return error_constr(dispex, flags, dp, retv, ei,
342             dispex->ctx->reference_error_constr);
343 }
344
345 static HRESULT SyntaxErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
346         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
347 {
348     TRACE("\n");
349     return error_constr(dispex, flags, dp, retv, ei,
350             dispex->ctx->syntax_error_constr);
351 }
352
353 static HRESULT TypeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
354         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
355 {
356     TRACE("\n");
357     return error_constr(dispex, flags, dp, retv, ei,
358             dispex->ctx->type_error_constr);
359 }
360
361 static HRESULT URIErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
362         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
363 {
364     TRACE("\n");
365     return error_constr(dispex, flags, dp, retv, ei,
366             dispex->ctx->uri_error_constr);
367 }
368
369 HRESULT init_error_constr(script_ctx_t *ctx)
370 {
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};
388
389     ErrorInstance *err;
390     INT i;
391     VARIANT v;
392     HRESULT hres;
393
394     for(i=0; i<7; i++) {
395         hres = alloc_error(ctx, i==0, NULL, &err);
396         if(FAILED(hres))
397             return hres;
398
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;
404         }
405
406         hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/);
407
408         if(SUCCEEDED(hres))
409             hres = create_builtin_function(ctx, constr_val[i], NULL,
410                     PROPF_CONSTR, &err->dispex, constr_addr[i]);
411
412         IDispatchEx_Release(_IDispatchEx_(&err->dispex));
413         VariantClear(&v);
414         if(FAILED(hres))
415             return hres;
416     }
417
418     return S_OK;
419 }
420
421 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
422 {
423     WCHAR buf[1024], *pos = NULL;
424     DispatchEx *err;
425     HRESULT hres;
426
427     buf[0] = '\0';
428     LoadStringW(jscript_hinstance, id&0xFFFF,  buf, sizeof(buf)/sizeof(WCHAR));
429
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));
435     }
436
437     WARN("%s\n", debugstr_w(buf));
438
439     id |= JSCRIPT_ERROR;
440     hres = create_error(ctx, constr, &id, buf, &err);
441     if(FAILED(hres))
442         return hres;
443
444     if(!ei)
445         return id;
446
447     V_VT(&ei->var) = VT_DISPATCH;
448     V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
449
450     return id;
451 }
452
453 HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
454 {
455     return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
456 }
457
458 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
459 {
460     return throw_error(ctx, ei, id, str, ctx->range_error_constr);
461 }
462
463 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
464 {
465     return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
466 }
467
468 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
469 {
470     return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
471 }
472
473 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
474 {
475     return throw_error(ctx, ei, id, str, ctx->type_error_constr);
476 }
477
478 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
479 {
480     return throw_error(ctx, ei, id, str, ctx->uri_error_constr);
481 }