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};
42 static HRESULT
Error_number(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
43 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
45 ErrorInstance
*This
= (ErrorInstance
*)dispex
;
50 case DISPATCH_PROPERTYGET
:
51 return VariantCopy(retv
, &This
->number
);
52 case DISPATCH_PROPERTYPUT
:
53 return VariantCopy(&This
->number
, get_arg(dp
, 0));
55 FIXME("unimplemented flags %x\n", flags
);
60 static HRESULT
Error_description(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
61 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
63 ErrorInstance
*This
= (ErrorInstance
*)dispex
;
68 case DISPATCH_PROPERTYGET
:
69 return VariantCopy(retv
, &This
->description
);
70 case DISPATCH_PROPERTYPUT
:
71 return VariantCopy(&This
->description
, get_arg(dp
, 0));
73 FIXME("unimplemented flags %x\n", flags
);
78 /* ECMA-262 3rd Edition 15.11.4.3 */
79 static HRESULT
Error_message(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
80 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
82 ErrorInstance
*This
= (ErrorInstance
*)dispex
;
87 case DISPATCH_PROPERTYGET
:
88 return VariantCopy(retv
, &This
->message
);
89 case DISPATCH_PROPERTYPUT
:
90 return VariantCopy(&This
->message
, get_arg(dp
, 0));
92 FIXME("unimplemented flags %x\n", flags
);
97 /* ECMA-262 3rd Edition 15.11.4.4 */
98 static HRESULT
Error_toString(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
99 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
101 static const WCHAR str
[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
106 V_VT(retv
) = VT_BSTR
;
107 V_BSTR(retv
) = SysAllocString(str
);
109 return E_OUTOFMEMORY
;
115 static HRESULT
Error_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
116 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
122 return throw_type_error(dispex
->ctx
, ei
, IDS_NOT_FUNC
, NULL
);
124 FIXME("unimplemented flags %x\n", flags
);
131 static void Error_destructor(DispatchEx
*dispex
)
133 ErrorInstance
*This
= (ErrorInstance
*)dispex
;
135 VariantClear(&This
->number
);
136 VariantClear(&This
->description
);
137 VariantClear(&This
->message
);
141 static const builtin_prop_t Error_props
[] = {
142 {descriptionW
, Error_description
, 0},
143 {messageW
, Error_message
, 0},
144 {numberW
, Error_number
, 0},
145 {toStringW
, Error_toString
, PROPF_METHOD
}
148 static const builtin_info_t Error_info
= {
150 {NULL
, Error_value
, 0},
151 sizeof(Error_props
)/sizeof(*Error_props
),
157 static const builtin_prop_t ErrorInst_props
[] = {
158 {descriptionW
, Error_description
, 0},
159 {messageW
, Error_message
, 0},
160 {numberW
, Error_number
, 0},
163 static const builtin_info_t ErrorInst_info
= {
165 {NULL
, Error_value
, 0},
166 sizeof(ErrorInst_props
)/sizeof(*ErrorInst_props
),
172 static HRESULT
alloc_error(script_ctx_t
*ctx
, DispatchEx
*prototype
,
173 DispatchEx
*constr
, ErrorInstance
**ret
)
178 err
= heap_alloc_zero(sizeof(ErrorInstance
));
180 return E_OUTOFMEMORY
;
183 hres
= init_dispex(&err
->dispex
, ctx
, &Error_info
, prototype
);
185 hres
= init_dispex_from_constr(&err
->dispex
, ctx
, &ErrorInst_info
,
186 constr
? constr
: ctx
->error_constr
);
196 static HRESULT
create_error(script_ctx_t
*ctx
, DispatchEx
*constr
,
197 const UINT
*number
, const WCHAR
*msg
, DispatchEx
**ret
)
202 hres
= alloc_error(ctx
, NULL
, constr
, &err
);
207 V_VT(&err
->number
) = VT_I4
;
208 V_I4(&err
->number
) = *number
;
211 V_VT(&err
->message
) = VT_BSTR
;
212 if(msg
) V_BSTR(&err
->message
) = SysAllocString(msg
);
213 else V_BSTR(&err
->message
) = SysAllocStringLen(NULL
, 0);
215 VariantCopy(&err
->description
, &err
->message
);
217 if(!V_BSTR(&err
->message
)) {
219 return E_OUTOFMEMORY
;
226 static HRESULT
error_constr(DispatchEx
*dispex
, WORD flags
, DISPPARAMS
*dp
,
227 VARIANT
*retv
, jsexcept_t
*ei
, DispatchEx
*constr
) {
234 V_VT(&numv
) = VT_NULL
;
237 hres
= to_number(dispex
->ctx
, get_arg(dp
, 0), ei
, &numv
);
238 if(FAILED(hres
) || (V_VT(&numv
)==VT_R8
&& isnan(V_R8(&numv
))))
239 hres
= to_string(dispex
->ctx
, get_arg(dp
, 0), ei
, &msg
);
240 else if(V_VT(&numv
) == VT_I4
)
249 if(arg_cnt(dp
)>1 && !msg
) {
250 hres
= to_string(dispex
->ctx
, get_arg(dp
, 1), ei
, &msg
);
257 case DISPATCH_CONSTRUCT
:
258 if(V_VT(&numv
) == VT_NULL
)
259 hres
= create_error(dispex
->ctx
, constr
, NULL
, msg
, &err
);
261 hres
= create_error(dispex
->ctx
, constr
, &num
, msg
, &err
);
267 V_VT(retv
) = VT_DISPATCH
;
268 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(err
);
271 IDispatchEx_Release(_IDispatchEx_(err
));
276 FIXME("unimplemented flags %x\n", flags
);
281 static HRESULT
ErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
282 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
285 return error_constr(dispex
, flags
, dp
, retv
, ei
,
286 dispex
->ctx
->error_constr
);
289 static HRESULT
EvalErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
290 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
293 return error_constr(dispex
, flags
, dp
, retv
, ei
,
294 dispex
->ctx
->eval_error_constr
);
297 static HRESULT
RangeErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
298 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
301 return error_constr(dispex
, flags
, dp
, retv
, ei
,
302 dispex
->ctx
->range_error_constr
);
305 static HRESULT
ReferenceErrorConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
,
306 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
309 return error_constr(dispex
, flags
, dp
, retv
, ei
,
310 dispex
->ctx
->reference_error_constr
);
313 static HRESULT
SyntaxErrorConstr_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
->syntax_error_constr
);
321 static HRESULT
TypeErrorConstr_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
->type_error_constr
);
329 static HRESULT
URIErrorConstr_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
->uri_error_constr
);
337 HRESULT
init_error_constr(script_ctx_t
*ctx
, DispatchEx
*object_prototype
)
339 static const WCHAR nameW
[] = {'n','a','m','e',0};
340 static const WCHAR ErrorW
[] = {'E','r','r','o','r',0};
341 static const WCHAR EvalErrorW
[] = {'E','v','a','l','E','r','r','o','r',0};
342 static const WCHAR RangeErrorW
[] = {'R','a','n','g','e','E','r','r','o','r',0};
343 static const WCHAR ReferenceErrorW
[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
344 static const WCHAR SyntaxErrorW
[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
345 static const WCHAR TypeErrorW
[] = {'T','y','p','e','E','r','r','o','r',0};
346 static const WCHAR URIErrorW
[] = {'U','R','I','E','r','r','o','r',0};
347 static const WCHAR
*names
[] = {ErrorW
, EvalErrorW
, RangeErrorW
,
348 ReferenceErrorW
, SyntaxErrorW
, TypeErrorW
, URIErrorW
};
349 DispatchEx
**constr_addr
[] = {&ctx
->error_constr
, &ctx
->eval_error_constr
,
350 &ctx
->range_error_constr
, &ctx
->reference_error_constr
,
351 &ctx
->syntax_error_constr
, &ctx
->type_error_constr
,
352 &ctx
->uri_error_constr
};
353 static builtin_invoke_t constr_val
[] = {ErrorConstr_value
, EvalErrorConstr_value
,
354 RangeErrorConstr_value
, ReferenceErrorConstr_value
, SyntaxErrorConstr_value
,
355 TypeErrorConstr_value
, URIErrorConstr_value
};
363 hres
= alloc_error(ctx
, i
==0 ? object_prototype
: NULL
, NULL
, &err
);
368 V_BSTR(&v
) = SysAllocString(names
[i
]);
370 IDispatchEx_Release(_IDispatchEx_(&err
->dispex
));
371 return E_OUTOFMEMORY
;
374 hres
= jsdisp_propput_name(&err
->dispex
, nameW
, ctx
->lcid
, &v
, NULL
/*FIXME*/, NULL
/*FIXME*/);
377 hres
= create_builtin_function(ctx
, constr_val
[i
], NULL
,
378 PROPF_CONSTR
, &err
->dispex
, constr_addr
[i
]);
380 IDispatchEx_Release(_IDispatchEx_(&err
->dispex
));
389 static HRESULT
throw_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
, DispatchEx
*constr
)
391 WCHAR buf
[1024], *pos
= NULL
;
396 LoadStringW(jscript_hinstance
, id
&0xFFFF, buf
, sizeof(buf
)/sizeof(WCHAR
));
398 if(str
) pos
= strchrW(buf
, '|');
400 int len
= strlenW(str
);
401 memmove(pos
+len
, pos
+1, (strlenW(pos
+1)+1)*sizeof(WCHAR
));
402 memcpy(pos
, str
, len
*sizeof(WCHAR
));
405 WARN("%s\n", debugstr_w(buf
));
408 hres
= create_error(ctx
, constr
, &id
, buf
, &err
);
415 V_VT(&ei
->var
) = VT_DISPATCH
;
416 V_DISPATCH(&ei
->var
) = (IDispatch
*)_IDispatchEx_(err
);
421 HRESULT
throw_eval_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
423 return throw_error(ctx
, ei
, id
, str
, ctx
->eval_error_constr
);
426 HRESULT
throw_range_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
428 return throw_error(ctx
, ei
, id
, str
, ctx
->range_error_constr
);
431 HRESULT
throw_reference_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
433 return throw_error(ctx
, ei
, id
, str
, ctx
->reference_error_constr
);
436 HRESULT
throw_syntax_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
438 return throw_error(ctx
, ei
, id
, str
, ctx
->syntax_error_constr
);
441 HRESULT
throw_type_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
443 return throw_error(ctx
, ei
, id
, str
, ctx
->type_error_constr
);
446 HRESULT
throw_uri_error(script_ctx_t
*ctx
, jsexcept_t
*ei
, UINT id
, const WCHAR
*str
)
448 return throw_error(ctx
, ei
, id
, str
, ctx
->uri_error_constr
);