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
19 #include "wine/port.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
37 static const WCHAR descriptionW
[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
38 static const WCHAR messageW
[] = {'m','e','s','s','a','g','e',0};
39 static const WCHAR numberW
[] = {'n','u','m','b','e','r',0};
40 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
41 static const WCHAR hasOwnPropertyW
[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
42 static const WCHAR propertyIsEnumerableW
[] =
43 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
44 static const WCHAR isPrototypeOfW
[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
46 static HRESULT
Error_number(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
47 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
49 ErrorInstance
*This
= (ErrorInstance
*)dispex
;
54 case DISPATCH_PROPERTYGET
:
55 return VariantCopy(retv
, &This
->number
);
56 case DISPATCH_PROPERTYPUT
:
57 return VariantCopy(&This
->number
, get_arg(dp
, 0));
59 FIXME("unimplemented flags %x\n", flags
);
64 static HRESULT
Error_description(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
65 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
67 ErrorInstance
*This
= (ErrorInstance
*)dispex
;
72 case DISPATCH_PROPERTYGET
:
73 return VariantCopy(retv
, &This
->description
);
74 case DISPATCH_PROPERTYPUT
:
75 return VariantCopy(&This
->description
, get_arg(dp
, 0));
77 FIXME("unimplemented flags %x\n", flags
);
82 /* ECMA-262 3rd Edition 15.11.4.3 */
83 static HRESULT
Error_message(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
84 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
86 ErrorInstance
*This
= (ErrorInstance
*)dispex
;
91 case DISPATCH_PROPERTYGET
:
92 return VariantCopy(retv
, &This
->message
);
93 case DISPATCH_PROPERTYPUT
:
94 return VariantCopy(&This
->message
, get_arg(dp
, 0));
96 FIXME("unimplemented flags %x\n", flags
);
101 /* ECMA-262 3rd Edition 15.11.4.4 */
102 static HRESULT
Error_toString(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
103 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
105 static const WCHAR str
[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
110 V_VT(retv
) = VT_BSTR
;
111 V_BSTR(retv
) = SysAllocString(str
);
113 return E_OUTOFMEMORY
;
119 static HRESULT
Error_hasOwnProperty(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
120 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
126 static HRESULT
Error_propertyIsEnumerable(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
127 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
134 static HRESULT
Error_isPrototypeOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
135 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
141 static HRESULT
Error_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
142 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
148 return throw_type_error(dispex
->ctx
, ei
, IDS_NOT_FUNC
, NULL
);
150 FIXME("unimplemented flags %x\n", flags
);
157 static void Error_destructor(DispatchEx
*dispex
)
159 ErrorInstance
*This
= (ErrorInstance
*)dispex
;
161 VariantClear(&This
->number
);
162 VariantClear(&This
->description
);
163 VariantClear(&This
->message
);
167 static const builtin_prop_t Error_props
[] = {
168 {descriptionW
, Error_description
, 0},
169 {hasOwnPropertyW
, Error_hasOwnProperty
, PROPF_METHOD
},
170 {isPrototypeOfW
, Error_isPrototypeOf
, PROPF_METHOD
},
171 {messageW
, Error_message
, 0},
172 {numberW
, Error_number
, 0},
173 {propertyIsEnumerableW
, Error_propertyIsEnumerable
, PROPF_METHOD
},
174 {toStringW
, Error_toString
, PROPF_METHOD
}
177 static const builtin_info_t Error_info
= {
179 {NULL
, Error_value
, 0},
180 sizeof(Error_props
)/sizeof(*Error_props
),
186 static const builtin_prop_t ErrorInst_props
[] = {
187 {descriptionW
, Error_description
, 0},
188 {hasOwnPropertyW
, Error_hasOwnProperty
, PROPF_METHOD
},
189 {isPrototypeOfW
, Error_isPrototypeOf
, PROPF_METHOD
},
190 {messageW
, Error_message
, 0},
191 {numberW
, Error_number
, 0},
192 {propertyIsEnumerableW
, Error_propertyIsEnumerable
, PROPF_METHOD
}
195 static const builtin_info_t ErrorInst_info
= {
197 {NULL
, Error_value
, 0},
198 sizeof(ErrorInst_props
)/sizeof(*ErrorInst_props
),
204 static HRESULT
alloc_error(script_ctx_t
*ctx
, BOOL error_prototype
,
205 DispatchEx
*constr
, ErrorInstance
**ret
)
211 err
= heap_alloc_zero(sizeof(ErrorInstance
));
213 return E_OUTOFMEMORY
;
215 inherit
= error_prototype
? ctx
->object_constr
: ctx
->error_constr
;
216 hres
= init_dispex_from_constr(&err
->dispex
, ctx
,
217 error_prototype
? &Error_info
: &ErrorInst_info
,
218 constr
? constr
: inherit
);
228 static HRESULT
create_error(script_ctx_t
*ctx
, DispatchEx
*constr
,
229 const UINT
*number
, const WCHAR
*msg
, DispatchEx
**ret
)
234 hres
= alloc_error(ctx
, FALSE
, constr
, &err
);
239 V_VT(&err
->number
) = VT_I4
;
240 V_I4(&err
->number
) = *number
;
243 V_VT(&err
->message
) = VT_BSTR
;
244 if(msg
) V_BSTR(&err
->message
) = SysAllocString(msg
);
245 else V_BSTR(&err
->message
) = SysAllocStringLen(NULL
, 0);
247 VariantCopy(&err
->description
, &err
->message
);
249 if(!V_BSTR(&err
->message
)) {
251 return E_OUTOFMEMORY
;
258 static HRESULT
error_constr(DispatchEx
*dispex
, WORD flags
, DISPPARAMS
*dp
,
259 VARIANT
*retv
, jsexcept_t
*ei
, DispatchEx
*constr
) {
266 V_VT(&numv
) = VT_NULL
;
269 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &numv
);
270 if(FAILED(hres
) || (V_VT(&numv
)==VT_R8
&& isnan(V_R8(&numv
))))
271 hres
= to_string(dispex
->ctx
, get_arg(dp
, 0), ei
, &msg
);
272 else if(V_VT(&numv
) == VT_I4
)
281 if(arg_cnt(dp
)>1 && !msg
) {
282 hres
= to_string(dispex
->ctx
, get_arg(dp
, 1), ei
, &msg
);
289 case DISPATCH_CONSTRUCT
:
290 if(V_VT(&numv
) == VT_NULL
)
291 hres
= create_error(dispex
->ctx
, constr
, NULL
, msg
, &err
);
293 hres
= create_error(dispex
->ctx
, constr
, &num
, msg
, &err
);
299 V_VT(retv
) = VT_DISPATCH
;
300 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(err
);
303 IDispatchEx_Release(_IDispatchEx_(err
));
308 FIXME("unimplemented flags %x\n", flags
);
313 static HRESULT
ErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
314 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
317 return error_constr(dispex
, flags
, dp
, retv
, ei
,
318 dispex
->ctx
->error_constr
);
321 static HRESULT
EvalErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
322 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
325 return error_constr(dispex
, flags
, dp
, retv
, ei
,
326 dispex
->ctx
->eval_error_constr
);
329 static HRESULT
RangeErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
330 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
333 return error_constr(dispex
, flags
, dp
, retv
, ei
,
334 dispex
->ctx
->range_error_constr
);
337 static HRESULT
ReferenceErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
338 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
341 return error_constr(dispex
, flags
, dp
, retv
, ei
,
342 dispex
->ctx
->reference_error_constr
);
345 static HRESULT
SyntaxErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
346 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
349 return error_constr(dispex
, flags
, dp
, retv
, ei
,
350 dispex
->ctx
->syntax_error_constr
);
353 static HRESULT
TypeErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
354 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
357 return error_constr(dispex
, flags
, dp
, retv
, ei
,
358 dispex
->ctx
->type_error_constr
);
361 static HRESULT
URIErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
362 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
365 return error_constr(dispex
, flags
, dp
, retv
, ei
,
366 dispex
->ctx
->uri_error_constr
);
369 HRESULT
init_error_constr(script_ctx_t
*ctx
)
371 static const WCHAR nameW
[] = {'n','a','m','e',0};
372 static const WCHAR ErrorW
[] = {'E','r','r','o','r',0};
373 static const WCHAR EvalErrorW
[] = {'E','v','a','l','E','r','r','o','r',0};
374 static const WCHAR RangeErrorW
[] = {'R','a','n','g','e','E','r','r','o','r',0};
375 static const WCHAR ReferenceErrorW
[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
376 static const WCHAR SyntaxErrorW
[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
377 static const WCHAR TypeErrorW
[] = {'T','y','p','e','E','r','r','o','r',0};
378 static const WCHAR URIErrorW
[] = {'U','R','I','E','r','r','o','r',0};
379 static const WCHAR
*names
[] = {ErrorW
, EvalErrorW
, RangeErrorW
,
380 ReferenceErrorW
, SyntaxErrorW
, TypeErrorW
, URIErrorW
};
381 DispatchEx
**constr_addr
[] = {&ctx
->error_constr
, &ctx
->eval_error_constr
,
382 &ctx
->range_error_constr
, &ctx
->reference_error_constr
,
383 &ctx
->syntax_error_constr
, &ctx
->type_error_constr
,
384 &ctx
->uri_error_constr
};
385 static builtin_invoke_t constr_val
[] = {ErrorConstr_value
, EvalErrorConstr_value
,
386 RangeErrorConstr_value
, ReferenceErrorConstr_value
, SyntaxErrorConstr_value
,
387 TypeErrorConstr_value
, URIErrorConstr_value
};
395 hres
= alloc_error(ctx
, i
==0, NULL
, &err
);
400 V_BSTR(&v
) = SysAllocString(names
[i
]);
402 IDispatchEx_Release(_IDispatchEx_(&err
->dispex
));
403 return E_OUTOFMEMORY
;
406 hres
= jsdisp_propput_name(&err
->dispex
, nameW
, ctx
->lcid
, &v
, NULL
/*FIXME*/, NULL
/*FIXME*/);
409 hres
= create_builtin_function(ctx
, constr_val
[i
], NULL
,
410 PROPF_CONSTR
, &err
->dispex
, constr_addr
[i
]);
412 IDispatchEx_Release(_IDispatchEx_(&err
->dispex
));
421 static HRESULT
throw_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
, DispatchEx
*constr
)
423 WCHAR buf
[1024], *pos
= NULL
;
428 LoadStringW(jscript_hinstance
, id
&0xFFFF, buf
, sizeof(buf
)/sizeof(WCHAR
));
430 if(str
) pos
= strchrW(buf
, '|');
432 int len
= strlenW(str
);
433 memmove(pos
+len
, pos
+1, (strlenW(pos
+1)+1)*sizeof(WCHAR
));
434 memcpy(pos
, str
, len
*sizeof(WCHAR
));
437 WARN("%s\n", debugstr_w(buf
));
440 hres
= create_error(ctx
, constr
, &id
, buf
, &err
);
447 V_VT(&ei
->var
) = VT_DISPATCH
;
448 V_DISPATCH(&ei
->var
) = (IDispatch
*)_IDispatchEx_(err
);
453 HRESULT
throw_eval_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
455 return throw_error(ctx
, ei
, id
, str
, ctx
->eval_error_constr
);
458 HRESULT
throw_range_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
460 return throw_error(ctx
, ei
, id
, str
, ctx
->range_error_constr
);
463 HRESULT
throw_reference_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
465 return throw_error(ctx
, ei
, id
, str
, ctx
->reference_error_constr
);
468 HRESULT
throw_syntax_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
470 return throw_error(ctx
, ei
, id
, str
, ctx
->syntax_error_constr
);
473 HRESULT
throw_type_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
475 return throw_error(ctx
, ei
, id
, str
, ctx
->type_error_constr
);
478 HRESULT
throw_uri_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
480 return throw_error(ctx
, ei
, id
, str
, ctx
->uri_error_constr
);