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