jscript: Added Error_number and Error_description implementation.
[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
19 #include "jscript.h"
20
21 #include "wine/debug.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
24
25 typedef struct {
26     DispatchEx dispex;
27
28     VARIANT number;
29     VARIANT description;
30     VARIANT message;
31 } ErrorInstance;
32
33 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
34 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
35 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
36 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
37 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
38 static const WCHAR propertyIsEnumerableW[] =
39     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
40 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
41
42 static HRESULT Error_number(DispatchEx *dispex, LCID lcid, WORD flags,
43         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
44 {
45     ErrorInstance *This = (ErrorInstance*)dispex;
46
47     TRACE("\n");
48
49     switch(flags) {
50     case DISPATCH_PROPERTYGET:
51         return VariantCopy(retv, &This->number);
52     case DISPATCH_PROPERTYPUT:
53         return VariantCopy(&This->number, get_arg(dp, 0));
54     default:
55         FIXME("unimplemented flags %x\n", flags);
56         return E_NOTIMPL;
57     }
58 }
59
60 static HRESULT Error_description(DispatchEx *dispex, LCID lcid, WORD flags,
61         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
62 {
63     ErrorInstance *This = (ErrorInstance*)dispex;
64
65     TRACE("\n");
66
67     switch(flags) {
68     case DISPATCH_PROPERTYGET:
69         return VariantCopy(retv, &This->description);
70     case DISPATCH_PROPERTYPUT:
71         return VariantCopy(&This->description, get_arg(dp, 0));
72     default:
73         FIXME("unimplemented flags %x\n", flags);
74         return E_NOTIMPL;
75     }
76 }
77
78 /* ECMA-262 3rd Edition    15.11.4.3 */
79 static HRESULT Error_message(DispatchEx *dispex, LCID lcid, WORD flags,
80         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
81 {
82     ErrorInstance *This = (ErrorInstance*)dispex;
83
84     TRACE("\n");
85
86     switch(flags) {
87     case DISPATCH_PROPERTYGET:
88         return VariantCopy(retv, &This->message);
89     case DISPATCH_PROPERTYPUT:
90         return VariantCopy(&This->message, get_arg(dp, 0));
91     default:
92         FIXME("unimplemented flags %x\n", flags);
93         return E_NOTIMPL;
94     }
95 }
96
97 /* ECMA-262 3rd Edition    15.11.4.4 */
98 static HRESULT Error_toString(DispatchEx *dispex, LCID lcid, WORD flags,
99         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
100 {
101     static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
102
103     TRACE("\n");
104
105     if(retv) {
106         V_VT(retv) = VT_BSTR;
107         V_BSTR(retv) = SysAllocString(str);
108         if(!V_BSTR(retv))
109             return E_OUTOFMEMORY;
110     }
111
112     return S_OK;
113 }
114
115 static HRESULT Error_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags,
116         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
117 {
118     FIXME("\n");
119     return E_NOTIMPL;
120 }
121
122 static HRESULT Error_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags,
123         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
124 {
125     FIXME("\n");
126     return E_NOTIMPL;
127 }
128
129
130 static HRESULT Error_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags,
131         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
132 {
133     FIXME("\n");
134     return E_NOTIMPL;
135 }
136
137 static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags,
138         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
139 {
140     FIXME("\n");
141     return E_NOTIMPL;
142 }
143
144 static void Error_destructor(DispatchEx *dispex)
145 {
146     ErrorInstance *This = (ErrorInstance*)dispex;
147
148     VariantClear(&This->number);
149     VariantClear(&This->description);
150     VariantClear(&This->message);
151     heap_free(This);
152 }
153
154 static const builtin_prop_t Error_props[] = {
155     {descriptionW,              Error_description,                  0},
156     {hasOwnPropertyW,           Error_hasOwnProperty,               PROPF_METHOD},
157     {isPrototypeOfW,            Error_isPrototypeOf,                PROPF_METHOD},
158     {messageW,                  Error_message,                      0},
159     {numberW,                   Error_number,                       0},
160     {propertyIsEnumerableW,     Error_propertyIsEnumerable,         PROPF_METHOD},
161     {toStringW,                 Error_toString,                     PROPF_METHOD}
162 };
163
164 static const builtin_info_t Error_info = {
165     JSCLASS_ERROR,
166     {NULL, Error_value, 0},
167     sizeof(Error_props)/sizeof(*Error_props),
168     Error_props,
169     Error_destructor,
170     NULL
171 };
172
173 static const builtin_prop_t ErrorInst_props[] = {
174     {descriptionW,              Error_description,                  0},
175     {hasOwnPropertyW,           Error_hasOwnProperty,               PROPF_METHOD},
176     {isPrototypeOfW,            Error_isPrototypeOf,                PROPF_METHOD},
177     {messageW,                  Error_message,                      0},
178     {numberW,                   Error_number,                       0},
179     {propertyIsEnumerableW,     Error_propertyIsEnumerable,         PROPF_METHOD}
180 };
181
182 static const builtin_info_t ErrorInst_info = {
183     JSCLASS_ERROR,
184     {NULL, Error_value, 0},
185     sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
186     ErrorInst_props,
187     Error_destructor,
188     NULL
189 };
190
191 static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype,
192         DispatchEx *constr, ErrorInstance **ret)
193 {
194     ErrorInstance *err;
195     DispatchEx *inherit;
196     HRESULT hres;
197
198     err = heap_alloc_zero(sizeof(ErrorInstance));
199     if(!err)
200         return E_OUTOFMEMORY;
201
202     inherit = error_prototype ? ctx->object_constr : ctx->error_constr;
203     hres = init_dispex_from_constr(&err->dispex, ctx,
204             error_prototype ? &Error_info : &ErrorInst_info,
205             constr ? constr : inherit);
206     if(FAILED(hres)) {
207         heap_free(err);
208         return hres;
209     }
210
211     *ret = err;
212     return S_OK;
213 }
214
215 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
216         const WCHAR *msg, DispatchEx **ret)
217 {
218     ErrorInstance *err;
219     HRESULT hres;
220
221     hres = alloc_error(ctx, FALSE, constr, &err);
222     if(FAILED(hres))
223         return hres;
224
225     V_VT(&err->message) = VT_BSTR;
226     if(msg) V_BSTR(&err->message) = SysAllocString(msg);
227     else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
228
229     if(!V_BSTR(&err->message)) {
230         heap_free(err);
231         return E_OUTOFMEMORY;
232     }
233
234     *ret = &err->dispex;
235     return S_OK;
236 }
237
238 static HRESULT error_constr(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
239         VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
240     DispatchEx *err;
241     BSTR msg = NULL;
242     HRESULT hres;
243
244     if(arg_cnt(dp)) {
245         hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &msg);
246         if(FAILED(hres))
247             return hres;
248     }
249
250     switch(flags) {
251     case INVOKE_FUNC:
252     case DISPATCH_CONSTRUCT:
253         hres = create_error(dispex->ctx, constr, msg, &err);
254         if(FAILED(hres))
255             return hres;
256
257         if(retv) {
258             V_VT(retv) = VT_DISPATCH;
259             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
260         }
261         else
262             IDispatchEx_Release(_IDispatchEx_(err));
263
264         return S_OK;
265
266     default:
267         FIXME("unimplemented flags %x\n", flags);
268         return E_NOTIMPL;
269     }
270 }
271
272 static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
273         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
274 {
275     TRACE("\n");
276     return error_constr(dispex, flags, dp, retv, ei,
277             dispex->ctx->error_constr);
278 }
279
280 static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
281         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
282 {
283     TRACE("\n");
284     return error_constr(dispex, flags, dp, retv, ei,
285             dispex->ctx->eval_error_constr);
286 }
287
288 static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
289         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
290 {
291     TRACE("\n");
292     return error_constr(dispex, flags, dp, retv, ei,
293             dispex->ctx->range_error_constr);
294 }
295
296 static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
297         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
298 {
299     TRACE("\n");
300     return error_constr(dispex, flags, dp, retv, ei,
301             dispex->ctx->reference_error_constr);
302 }
303
304 static HRESULT SyntaxErrorConstr_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->syntax_error_constr);
310 }
311
312 static HRESULT TypeErrorConstr_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->type_error_constr);
318 }
319
320 static HRESULT URIErrorConstr_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->uri_error_constr);
326 }
327
328 HRESULT init_error_constr(script_ctx_t *ctx)
329 {
330     static const WCHAR nameW[] = {'n','a','m','e',0};
331     static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
332     static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
333     static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
334     static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
335     static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
336     static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
337     static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
338     static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
339         ReferenceErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
340     DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
341         &ctx->range_error_constr, &ctx->reference_error_constr,
342         &ctx->syntax_error_constr, &ctx->type_error_constr,
343         &ctx->uri_error_constr};
344     static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
345         RangeErrorConstr_value, ReferenceErrorConstr_value, SyntaxErrorConstr_value,
346         TypeErrorConstr_value, URIErrorConstr_value};
347
348     ErrorInstance *err;
349     INT i;
350     VARIANT v;
351     HRESULT hres;
352
353     for(i=0; i<7; i++) {
354         hres = alloc_error(ctx, i==0, NULL, &err);
355         if(FAILED(hres))
356             return hres;
357
358         V_VT(&v) = VT_BSTR;
359         V_BSTR(&v) = SysAllocString(names[i]);
360         if(!V_BSTR(&v)) {
361             IDispatchEx_Release(_IDispatchEx_(&err->dispex));
362             return E_OUTOFMEMORY;
363         }
364
365         hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/);
366
367         if(SUCCEEDED(hres))
368             hres = create_builtin_function(ctx, constr_val[i], NULL,
369                     PROPF_CONSTR, &err->dispex, constr_addr[i]);
370
371         IDispatchEx_Release(_IDispatchEx_(&err->dispex));
372         VariantClear(&v);
373         if(FAILED(hres))
374             return hres;
375     }
376
377     return S_OK;
378 }
379
380 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
381 {
382     WCHAR buf[1024], *pos = NULL;
383     DispatchEx *err;
384     HRESULT hres;
385
386     TRACE("\n");
387
388     LoadStringW(jscript_hinstance, id,  buf, sizeof(buf)/sizeof(WCHAR));
389
390     if(str) pos = strchrW(buf, '|');
391     if(pos) {
392         int len = strlenW(str);
393         memmove(pos+len, pos+1, strlenW(pos+1)*sizeof(WCHAR));
394         memcpy(pos, str, len*sizeof(WCHAR));
395     }
396
397     hres = create_error(ctx, constr, buf, &err);
398     if(FAILED(hres))
399         return hres;
400
401     if(!ei)
402         return id;
403
404     V_VT(&ei->var) = VT_DISPATCH;
405     V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
406
407     return 0x800A0000+id;
408 }
409
410 HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
411 {
412     return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
413 }
414
415 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
416 {
417     return throw_error(ctx, ei, id, str, ctx->range_error_constr);
418 }
419
420 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
421 {
422     return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
423 }
424
425 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
426 {
427     return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
428 }
429
430 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
431 {
432     return throw_error(ctx, ei, id, str, ctx->type_error_constr);
433 }
434
435 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
436 {
437     return throw_error(ctx, ei, id, str, ctx->uri_error_constr);
438 }