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