jscript: Add Error_toString 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 message;
29 } ErrorInstance;
30
31 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
32 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
33 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
34 static const WCHAR propertyIsEnumerableW[] =
35     {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
36 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
37
38 /* ECMA-262 3rd Edition    15.11.4.3 */
39 static HRESULT Error_message(DispatchEx *dispex, LCID lcid, WORD flags,
40         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
41 {
42     ErrorInstance *This = (ErrorInstance*)dispex;
43
44     TRACE("\n");
45
46     switch(flags) {
47     case DISPATCH_PROPERTYGET:
48         return VariantCopy(retv, &This->message);
49     case DISPATCH_PROPERTYPUT:
50         return VariantCopy(&This->message, get_arg(dp, 0));
51     default:
52         FIXME("unimplemented flags %x\n", flags);
53         return E_NOTIMPL;
54     }
55 }
56
57 /* ECMA-262 3rd Edition    15.11.4.4 */
58 static HRESULT Error_toString(DispatchEx *dispex, LCID lcid, WORD flags,
59         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
60 {
61     static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
62
63     TRACE("\n");
64
65     if(retv) {
66         V_VT(retv) = VT_BSTR;
67         V_BSTR(retv) = SysAllocString(str);
68         if(!V_BSTR(retv))
69             return E_OUTOFMEMORY;
70     }
71
72     return S_OK;
73 }
74
75 static HRESULT Error_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags,
76         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
77 {
78     FIXME("\n");
79     return E_NOTIMPL;
80 }
81
82 static HRESULT Error_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags,
83         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
84 {
85     FIXME("\n");
86     return E_NOTIMPL;
87 }
88
89
90 static HRESULT Error_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags,
91         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
92 {
93     FIXME("\n");
94     return E_NOTIMPL;
95 }
96
97 static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags,
98         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
99 {
100     FIXME("\n");
101     return E_NOTIMPL;
102 }
103
104 static void Error_destructor(DispatchEx *dispex)
105 {
106     ErrorInstance *This = (ErrorInstance*)dispex;
107
108     VariantClear(&This->message);
109     heap_free(This);
110 }
111
112 static const builtin_prop_t Error_props[] = {
113     {hasOwnPropertyW,           Error_hasOwnProperty,               PROPF_METHOD},
114     {isPrototypeOfW,            Error_isPrototypeOf,                PROPF_METHOD},
115     {messageW,                  Error_message,                      0},
116     {propertyIsEnumerableW,     Error_propertyIsEnumerable,         PROPF_METHOD},
117     {toStringW,                 Error_toString,                     PROPF_METHOD}
118 };
119
120 static const builtin_info_t Error_info = {
121     JSCLASS_ERROR,
122     {NULL, Error_value, 0},
123     sizeof(Error_props)/sizeof(*Error_props),
124     Error_props,
125     Error_destructor,
126     NULL
127 };
128
129 static const builtin_prop_t ErrorInst_props[] = {
130     {hasOwnPropertyW,           Error_hasOwnProperty,               PROPF_METHOD},
131     {isPrototypeOfW,            Error_isPrototypeOf,                PROPF_METHOD},
132     {messageW,                  Error_message,                      0},
133     {propertyIsEnumerableW,     Error_propertyIsEnumerable,         PROPF_METHOD}
134 };
135
136 static const builtin_info_t ErrorInst_info = {
137     JSCLASS_ERROR,
138     {NULL, Error_value, 0},
139     sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
140     ErrorInst_props,
141     Error_destructor,
142     NULL
143 };
144
145 static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype,
146         DispatchEx *constr, ErrorInstance **ret)
147 {
148     ErrorInstance *err;
149     DispatchEx *inherit;
150     HRESULT hres;
151
152     err = heap_alloc_zero(sizeof(ErrorInstance));
153     if(!err)
154         return E_OUTOFMEMORY;
155
156     inherit = error_prototype ? ctx->object_constr : ctx->error_constr;
157     hres = init_dispex_from_constr(&err->dispex, ctx,
158             error_prototype ? &Error_info : &ErrorInst_info,
159             constr ? constr : inherit);
160     if(FAILED(hres)) {
161         heap_free(err);
162         return hres;
163     }
164
165     *ret = err;
166     return S_OK;
167 }
168
169 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
170         const WCHAR *msg, DispatchEx **ret)
171 {
172     ErrorInstance *err;
173     HRESULT hres;
174
175     hres = alloc_error(ctx, FALSE, constr, &err);
176     if(FAILED(hres))
177         return hres;
178
179     V_VT(&err->message) = VT_BSTR;
180     if(msg) V_BSTR(&err->message) = SysAllocString(msg);
181     else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
182
183     if(!V_BSTR(&err->message)) {
184         heap_free(err);
185         return E_OUTOFMEMORY;
186     }
187
188     *ret = &err->dispex;
189     return S_OK;
190 }
191
192 static HRESULT error_constr(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
193         VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
194     DispatchEx *err;
195     BSTR msg = NULL;
196     HRESULT hres;
197
198     if(arg_cnt(dp)) {
199         hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &msg);
200         if(FAILED(hres))
201             return hres;
202     }
203
204     switch(flags) {
205     case INVOKE_FUNC:
206     case DISPATCH_CONSTRUCT:
207         hres = create_error(dispex->ctx, constr, msg, &err);
208         if(FAILED(hres))
209             return hres;
210
211         if(retv) {
212             V_VT(retv) = VT_DISPATCH;
213             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
214         }
215         else
216             IDispatchEx_Release(_IDispatchEx_(err));
217
218         return S_OK;
219
220     default:
221         FIXME("unimplemented flags %x\n", flags);
222         return E_NOTIMPL;
223     }
224 }
225
226 static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
227         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
228 {
229     TRACE("\n");
230     return error_constr(dispex, flags, dp, retv, ei,
231             dispex->ctx->error_constr);
232 }
233
234 static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
235         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
236 {
237     TRACE("\n");
238     return error_constr(dispex, flags, dp, retv, ei,
239             dispex->ctx->eval_error_constr);
240 }
241
242 static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
243         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
244 {
245     TRACE("\n");
246     return error_constr(dispex, flags, dp, retv, ei,
247             dispex->ctx->range_error_constr);
248 }
249
250 static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
251         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
252 {
253     TRACE("\n");
254     return error_constr(dispex, flags, dp, retv, ei,
255             dispex->ctx->reference_error_constr);
256 }
257
258 static HRESULT SyntaxErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
259         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
260 {
261     TRACE("\n");
262     return error_constr(dispex, flags, dp, retv, ei,
263             dispex->ctx->syntax_error_constr);
264 }
265
266 static HRESULT TypeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
267         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
268 {
269     TRACE("\n");
270     return error_constr(dispex, flags, dp, retv, ei,
271             dispex->ctx->type_error_constr);
272 }
273
274 static HRESULT URIErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
275         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
276 {
277     TRACE("\n");
278     return error_constr(dispex, flags, dp, retv, ei,
279             dispex->ctx->uri_error_constr);
280 }
281
282 HRESULT init_error_constr(script_ctx_t *ctx)
283 {
284     static const WCHAR nameW[] = {'n','a','m','e',0};
285     static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
286     static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
287     static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
288     static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
289     static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
290     static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
291     static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
292     static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
293         ReferenceErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
294     DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
295         &ctx->range_error_constr, &ctx->reference_error_constr,
296         &ctx->syntax_error_constr, &ctx->type_error_constr,
297         &ctx->uri_error_constr};
298     static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
299         RangeErrorConstr_value, ReferenceErrorConstr_value, SyntaxErrorConstr_value,
300         TypeErrorConstr_value, URIErrorConstr_value};
301
302     ErrorInstance *err;
303     INT i;
304     VARIANT v;
305     HRESULT hres;
306
307     for(i=0; i<7; i++) {
308         hres = alloc_error(ctx, i==0, NULL, &err);
309         if(FAILED(hres))
310             return hres;
311
312         V_VT(&v) = VT_BSTR;
313         V_BSTR(&v) = SysAllocString(names[i]);
314         if(!V_BSTR(&v)) {
315             IDispatchEx_Release(_IDispatchEx_(&err->dispex));
316             return E_OUTOFMEMORY;
317         }
318
319         hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/);
320
321         if(SUCCEEDED(hres))
322             hres = create_builtin_function(ctx, constr_val[i], NULL,
323                     PROPF_CONSTR, &err->dispex, constr_addr[i]);
324
325         IDispatchEx_Release(_IDispatchEx_(&err->dispex));
326         VariantClear(&v);
327         if(FAILED(hres))
328             return hres;
329     }
330
331     return S_OK;
332 }