2 * Copyright 2009 Piotr Caban
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.
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.
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
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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};
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)
42 ErrorInstance *This = (ErrorInstance*)dispex;
47 case DISPATCH_PROPERTYGET:
48 return VariantCopy(retv, &This->message);
49 case DISPATCH_PROPERTYPUT:
50 return VariantCopy(&This->message, get_arg(dp, 0));
52 FIXME("unimplemented flags %x\n", flags);
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)
61 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
67 V_BSTR(retv) = SysAllocString(str);
75 static HRESULT Error_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags,
76 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
82 static HRESULT Error_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags,
83 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
90 static HRESULT Error_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags,
91 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
97 static HRESULT Error_value(DispatchEx *dispex, LCID lcid, WORD flags,
98 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
104 static void Error_destructor(DispatchEx *dispex)
106 ErrorInstance *This = (ErrorInstance*)dispex;
108 VariantClear(&This->message);
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}
120 static const builtin_info_t Error_info = {
122 {NULL, Error_value, 0},
123 sizeof(Error_props)/sizeof(*Error_props),
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}
136 static const builtin_info_t ErrorInst_info = {
138 {NULL, Error_value, 0},
139 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
145 static HRESULT alloc_error(script_ctx_t *ctx, BOOL error_prototype,
146 DispatchEx *constr, ErrorInstance **ret)
152 err = heap_alloc_zero(sizeof(ErrorInstance));
154 return E_OUTOFMEMORY;
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);
169 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
170 const WCHAR *msg, DispatchEx **ret)
175 hres = alloc_error(ctx, FALSE, constr, &err);
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);
183 if(!V_BSTR(&err->message)) {
185 return E_OUTOFMEMORY;
192 static HRESULT error_constr(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
193 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
199 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &msg);
206 case DISPATCH_CONSTRUCT:
207 hres = create_error(dispex->ctx, constr, msg, &err);
212 V_VT(retv) = VT_DISPATCH;
213 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
216 IDispatchEx_Release(_IDispatchEx_(err));
221 FIXME("unimplemented flags %x\n", flags);
226 static HRESULT ErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
227 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
230 return error_constr(dispex, flags, dp, retv, ei,
231 dispex->ctx->error_constr);
234 static HRESULT EvalErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
235 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
238 return error_constr(dispex, flags, dp, retv, ei,
239 dispex->ctx->eval_error_constr);
242 static HRESULT RangeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
243 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
246 return error_constr(dispex, flags, dp, retv, ei,
247 dispex->ctx->range_error_constr);
250 static HRESULT ReferenceErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
251 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
254 return error_constr(dispex, flags, dp, retv, ei,
255 dispex->ctx->reference_error_constr);
258 static HRESULT SyntaxErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
259 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
262 return error_constr(dispex, flags, dp, retv, ei,
263 dispex->ctx->syntax_error_constr);
266 static HRESULT TypeErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
267 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
270 return error_constr(dispex, flags, dp, retv, ei,
271 dispex->ctx->type_error_constr);
274 static HRESULT URIErrorConstr_value(DispatchEx *dispex, LCID lcid, WORD flags,
275 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
278 return error_constr(dispex, flags, dp, retv, ei,
279 dispex->ctx->uri_error_constr);
282 HRESULT init_error_constr(script_ctx_t *ctx)
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};
308 hres = alloc_error(ctx, i==0, NULL, &err);
313 V_BSTR(&v) = SysAllocString(names[i]);
315 IDispatchEx_Release(_IDispatchEx_(&err->dispex));
316 return E_OUTOFMEMORY;
319 hres = jsdisp_propput_name(&err->dispex, nameW, ctx->lcid, &v, NULL/*FIXME*/, NULL/*FIXME*/);
322 hres = create_builtin_function(ctx, constr_val[i], NULL,
323 PROPF_CONSTR, &err->dispex, constr_addr[i]);
325 IDispatchEx_Release(_IDispatchEx_(&err->dispex));