blob: 9530a4ce52adb21b30a1659c36ca5e86e73ca199 [file] [log] [blame]
Piotr Cabanf33f5c92009-07-20 18:17:51 +02001/*
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 */
Piotr Cabana77e3692009-07-20 18:18:22 +020018#include "config.h"
19#include "wine/port.h"
20
21#include <math.h>
Piotr Cabanf33f5c92009-07-20 18:17:51 +020022
23#include "jscript.h"
24
25#include "wine/debug.h"
26
27WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28
29typedef struct {
30 DispatchEx dispex;
31
Piotr Caban7c0a7022009-07-20 18:18:17 +020032 VARIANT number;
33 VARIANT description;
Piotr Cabanf33f5c92009-07-20 18:17:51 +020034 VARIANT message;
35} ErrorInstance;
36
Piotr Caban7c0a7022009-07-20 18:18:17 +020037static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
Piotr Cabanf33f5c92009-07-20 18:17:51 +020038static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
Piotr Caban7c0a7022009-07-20 18:18:17 +020039static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
Piotr Cabanf33f5c92009-07-20 18:17:51 +020040static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
Piotr Cabanf33f5c92009-07-20 18:17:51 +020041
Jacek Cabanf8c2b422009-09-23 16:18:39 +020042static inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp)
43{
44 return (ErrorInstance*)vdisp->u.jsdisp;
45}
46
47static HRESULT Error_number(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Caban7c0a7022009-07-20 18:18:17 +020048 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
49{
Jacek Cabanf8c2b422009-09-23 16:18:39 +020050 ErrorInstance *This = error_from_vdisp(jsthis);
Piotr Cabanf3eef0d2009-07-20 18:18:19 +020051
52 TRACE("\n");
53
54 switch(flags) {
55 case DISPATCH_PROPERTYGET:
56 return VariantCopy(retv, &This->number);
57 case DISPATCH_PROPERTYPUT:
58 return VariantCopy(&This->number, get_arg(dp, 0));
59 default:
60 FIXME("unimplemented flags %x\n", flags);
61 return E_NOTIMPL;
62 }
Piotr Caban7c0a7022009-07-20 18:18:17 +020063}
64
Jacek Cabanf8c2b422009-09-23 16:18:39 +020065static HRESULT Error_description(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Caban7c0a7022009-07-20 18:18:17 +020066 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
67{
Jacek Cabanf8c2b422009-09-23 16:18:39 +020068 ErrorInstance *This = error_from_vdisp(jsthis);
Piotr Cabanf3eef0d2009-07-20 18:18:19 +020069
70 TRACE("\n");
71
72 switch(flags) {
73 case DISPATCH_PROPERTYGET:
74 return VariantCopy(retv, &This->description);
75 case DISPATCH_PROPERTYPUT:
76 return VariantCopy(&This->description, get_arg(dp, 0));
77 default:
78 FIXME("unimplemented flags %x\n", flags);
79 return E_NOTIMPL;
80 }
Piotr Caban7c0a7022009-07-20 18:18:17 +020081}
82
Piotr Cabanf33f5c92009-07-20 18:17:51 +020083/* ECMA-262 3rd Edition 15.11.4.3 */
Jacek Cabanf8c2b422009-09-23 16:18:39 +020084static HRESULT Error_message(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +020085 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
86{
Jacek Cabanf8c2b422009-09-23 16:18:39 +020087 ErrorInstance *This = error_from_vdisp(jsthis);
Piotr Cabanf17b1f62009-07-20 18:17:54 +020088
89 TRACE("\n");
90
91 switch(flags) {
92 case DISPATCH_PROPERTYGET:
93 return VariantCopy(retv, &This->message);
94 case DISPATCH_PROPERTYPUT:
95 return VariantCopy(&This->message, get_arg(dp, 0));
96 default:
97 FIXME("unimplemented flags %x\n", flags);
98 return E_NOTIMPL;
99 }
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200100}
101
Piotr Caban2d71dac2009-07-20 18:17:57 +0200102/* ECMA-262 3rd Edition 15.11.4.4 */
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200103static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200104 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
105{
Piotr Caban2d71dac2009-07-20 18:17:57 +0200106 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
107
108 TRACE("\n");
109
110 if(retv) {
111 V_VT(retv) = VT_BSTR;
112 V_BSTR(retv) = SysAllocString(str);
113 if(!V_BSTR(retv))
114 return E_OUTOFMEMORY;
115 }
116
117 return S_OK;
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200118}
119
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200120static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200121 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
122{
Piotr Caban8dd1d9b2009-07-22 13:02:30 +0200123 TRACE("\n");
124
125 switch(flags) {
126 case INVOKE_FUNC:
Jacek Caban5dcd1822009-09-23 16:17:17 +0200127 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
Piotr Caban8dd1d9b2009-07-22 13:02:30 +0200128 default:
129 FIXME("unimplemented flags %x\n", flags);
130 return E_NOTIMPL;
131 }
132
133 return S_OK;
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200134}
135
136static void Error_destructor(DispatchEx *dispex)
137{
138 ErrorInstance *This = (ErrorInstance*)dispex;
139
Piotr Cabanf3eef0d2009-07-20 18:18:19 +0200140 VariantClear(&This->number);
141 VariantClear(&This->description);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200142 VariantClear(&This->message);
143 heap_free(This);
144}
145
146static const builtin_prop_t Error_props[] = {
Piotr Caban7c0a7022009-07-20 18:18:17 +0200147 {descriptionW, Error_description, 0},
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200148 {messageW, Error_message, 0},
Piotr Caban7c0a7022009-07-20 18:18:17 +0200149 {numberW, Error_number, 0},
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200150 {toStringW, Error_toString, PROPF_METHOD}
151};
152
153static const builtin_info_t Error_info = {
154 JSCLASS_ERROR,
155 {NULL, Error_value, 0},
156 sizeof(Error_props)/sizeof(*Error_props),
157 Error_props,
158 Error_destructor,
159 NULL
160};
161
162static const builtin_prop_t ErrorInst_props[] = {
Piotr Caban7c0a7022009-07-20 18:18:17 +0200163 {descriptionW, Error_description, 0},
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200164 {messageW, Error_message, 0},
Piotr Caban7c0a7022009-07-20 18:18:17 +0200165 {numberW, Error_number, 0},
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200166};
167
168static const builtin_info_t ErrorInst_info = {
169 JSCLASS_ERROR,
170 {NULL, Error_value, 0},
171 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
172 ErrorInst_props,
173 Error_destructor,
174 NULL
175};
176
Piotr Caban28734e32009-08-14 11:58:10 +0200177static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200178 DispatchEx *constr, ErrorInstance **ret)
179{
180 ErrorInstance *err;
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200181 HRESULT hres;
182
183 err = heap_alloc_zero(sizeof(ErrorInstance));
184 if(!err)
185 return E_OUTOFMEMORY;
186
Piotr Caban28734e32009-08-14 11:58:10 +0200187 if(prototype)
188 hres = init_dispex(&err->dispex, ctx, &Error_info, prototype);
189 else
190 hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info,
191 constr ? constr : ctx->error_constr);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200192 if(FAILED(hres)) {
193 heap_free(err);
194 return hres;
195 }
196
197 *ret = err;
198 return S_OK;
199}
200
201static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
Piotr Cabana77e3692009-07-20 18:18:22 +0200202 const UINT *number, const WCHAR *msg, DispatchEx **ret)
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200203{
204 ErrorInstance *err;
205 HRESULT hres;
206
Piotr Caban28734e32009-08-14 11:58:10 +0200207 hres = alloc_error(ctx, NULL, constr, &err);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200208 if(FAILED(hres))
209 return hres;
210
Piotr Cabana77e3692009-07-20 18:18:22 +0200211 if(number) {
212 V_VT(&err->number) = VT_I4;
213 V_I4(&err->number) = *number;
214 }
215
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200216 V_VT(&err->message) = VT_BSTR;
217 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
218 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
219
Piotr Cabana77e3692009-07-20 18:18:22 +0200220 VariantCopy(&err->description, &err->message);
221
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200222 if(!V_BSTR(&err->message)) {
223 heap_free(err);
224 return E_OUTOFMEMORY;
225 }
226
227 *ret = &err->dispex;
228 return S_OK;
229}
230
Jacek Caban5dcd1822009-09-23 16:17:17 +0200231static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200232 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
233 DispatchEx *err;
Piotr Cabana77e3692009-07-20 18:18:22 +0200234 VARIANT numv;
235 UINT num;
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200236 BSTR msg = NULL;
237 HRESULT hres;
238
Piotr Cabana77e3692009-07-20 18:18:22 +0200239 V_VT(&numv) = VT_NULL;
240
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200241 if(arg_cnt(dp)) {
Jacek Caban5dcd1822009-09-23 16:17:17 +0200242 hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
Piotr Cabana77e3692009-07-20 18:18:22 +0200243 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
Jacek Caban5dcd1822009-09-23 16:17:17 +0200244 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
Piotr Cabana77e3692009-07-20 18:18:22 +0200245 else if(V_VT(&numv) == VT_I4)
246 num = V_I4(&numv);
247 else
248 num = V_R8(&numv);
249
250 if(FAILED(hres))
251 return hres;
252 }
253
254 if(arg_cnt(dp)>1 && !msg) {
Jacek Caban5dcd1822009-09-23 16:17:17 +0200255 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200256 if(FAILED(hres))
257 return hres;
258 }
259
260 switch(flags) {
261 case INVOKE_FUNC:
262 case DISPATCH_CONSTRUCT:
Piotr Cabana77e3692009-07-20 18:18:22 +0200263 if(V_VT(&numv) == VT_NULL)
Jacek Caban5dcd1822009-09-23 16:17:17 +0200264 hres = create_error(ctx, constr, NULL, msg, &err);
Piotr Cabana77e3692009-07-20 18:18:22 +0200265 else
Jacek Caban5dcd1822009-09-23 16:17:17 +0200266 hres = create_error(ctx, constr, &num, msg, &err);
Piotr Cabana77e3692009-07-20 18:18:22 +0200267
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200268 if(FAILED(hres))
269 return hres;
270
271 if(retv) {
272 V_VT(retv) = VT_DISPATCH;
273 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
274 }
275 else
Jacek Cabanc444a492009-09-01 13:26:08 +0200276 jsdisp_release(err);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200277
278 return S_OK;
279
280 default:
281 FIXME("unimplemented flags %x\n", flags);
282 return E_NOTIMPL;
283 }
284}
285
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200286static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200287 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
288{
289 TRACE("\n");
Jacek Caban5dcd1822009-09-23 16:17:17 +0200290 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200291}
292
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200293static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200294 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
295{
296 TRACE("\n");
Jacek Caban5dcd1822009-09-23 16:17:17 +0200297 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200298}
299
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200300static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200301 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
302{
303 TRACE("\n");
Jacek Caban5dcd1822009-09-23 16:17:17 +0200304 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200305}
306
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200307static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200308 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
309{
310 TRACE("\n");
Jacek Caban5dcd1822009-09-23 16:17:17 +0200311 return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200312}
313
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200314static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Jacek Caban9e523c62009-09-23 16:09:22 +0200315 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
316{
317 TRACE("\n");
Jacek Caban5dcd1822009-09-23 16:17:17 +0200318 return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
Jacek Caban9e523c62009-09-23 16:09:22 +0200319}
320
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200321static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200322 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
323{
324 TRACE("\n");
Jacek Caban5dcd1822009-09-23 16:17:17 +0200325 return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200326}
327
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200328static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200329 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
330{
331 TRACE("\n");
Jacek Caban5dcd1822009-09-23 16:17:17 +0200332 return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200333}
334
Jacek Cabanf8c2b422009-09-23 16:18:39 +0200335static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200336 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
337{
338 TRACE("\n");
Jacek Caban5dcd1822009-09-23 16:17:17 +0200339 return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200340}
341
Piotr Caban28734e32009-08-14 11:58:10 +0200342HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200343{
344 static const WCHAR nameW[] = {'n','a','m','e',0};
345 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
346 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
347 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
348 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
Jacek Caban9e523c62009-09-23 16:09:22 +0200349 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200350 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
351 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
352 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
353 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
Jacek Caban9e523c62009-09-23 16:09:22 +0200354 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200355 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
Jacek Caban9e523c62009-09-23 16:09:22 +0200356 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200357 &ctx->syntax_error_constr, &ctx->type_error_constr,
358 &ctx->uri_error_constr};
359 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
Jacek Caban9e523c62009-09-23 16:09:22 +0200360 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
361 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200362
363 ErrorInstance *err;
364 INT i;
365 VARIANT v;
366 HRESULT hres;
367
Jacek Caban9e523c62009-09-23 16:09:22 +0200368 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
Piotr Caban28734e32009-08-14 11:58:10 +0200369 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200370 if(FAILED(hres))
371 return hres;
372
373 V_VT(&v) = VT_BSTR;
374 V_BSTR(&v) = SysAllocString(names[i]);
375 if(!V_BSTR(&v)) {
Jacek Cabanc444a492009-09-01 13:26:08 +0200376 jsdisp_release(&err->dispex);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200377 return E_OUTOFMEMORY;
378 }
379
Jacek Cabanfadfab52009-09-23 16:11:11 +0200380 hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200381
382 if(SUCCEEDED(hres))
Jacek Caband918a182009-09-17 01:04:44 +0200383 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200384 PROPF_CONSTR, &err->dispex, constr_addr[i]);
385
Jacek Cabanc444a492009-09-01 13:26:08 +0200386 jsdisp_release(&err->dispex);
Piotr Cabanf33f5c92009-07-20 18:17:51 +0200387 VariantClear(&v);
388 if(FAILED(hres))
389 return hres;
390 }
391
392 return S_OK;
393}
Piotr Caban469b5972009-07-20 18:18:00 +0200394
395static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
396{
397 WCHAR buf[1024], *pos = NULL;
398 DispatchEx *err;
399 HRESULT hres;
400
Piotr Caban98223b92009-07-24 09:35:56 +0200401 buf[0] = '\0';
402 LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR));
Piotr Caban469b5972009-07-20 18:18:00 +0200403
404 if(str) pos = strchrW(buf, '|');
405 if(pos) {
406 int len = strlenW(str);
Piotr Caband8e841c2009-07-22 13:01:59 +0200407 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
Piotr Caban469b5972009-07-20 18:18:00 +0200408 memcpy(pos, str, len*sizeof(WCHAR));
409 }
410
Piotr Caband8e841c2009-07-22 13:01:59 +0200411 WARN("%s\n", debugstr_w(buf));
412
Piotr Caban98223b92009-07-24 09:35:56 +0200413 id |= JSCRIPT_ERROR;
Piotr Cabana77e3692009-07-20 18:18:22 +0200414 hres = create_error(ctx, constr, &id, buf, &err);
Piotr Caban469b5972009-07-20 18:18:00 +0200415 if(FAILED(hres))
416 return hres;
417
418 if(!ei)
419 return id;
420
421 V_VT(&ei->var) = VT_DISPATCH;
422 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
423
Piotr Cabana77e3692009-07-20 18:18:22 +0200424 return id;
Piotr Caban469b5972009-07-20 18:18:00 +0200425}
426
427HRESULT throw_eval_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
428{
429 return throw_error(ctx, ei, id, str, ctx->eval_error_constr);
430}
431
Jacek Caban6d4533a2009-09-30 14:34:47 +0200432HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
433{
434 return throw_error(ctx, ei, id, str, ctx->error_constr);
435}
436
Piotr Caban469b5972009-07-20 18:18:00 +0200437HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
438{
439 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
440}
441
442HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
443{
444 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
445}
446
Jacek Caban9e523c62009-09-23 16:09:22 +0200447HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
448{
449 return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
450}
451
Piotr Caban469b5972009-07-20 18:18:00 +0200452HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
453{
454 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
455}
456
457HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
458{
459 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
460}
461
462HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
463{
464 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);
465}