po: Fix Finnish translation.
[wine/multimedia.git] / dlls / jscript / error.c
blob21a8160b8d183f38f78acc2f08fd85216c168163
1 /*
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
18 #include "config.h"
19 #include "wine/port.h"
21 #include <math.h>
23 #include "jscript.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 static const WCHAR descriptionW[] = {'d','e','s','c','r','i','p','t','i','o','n',0};
30 static const WCHAR messageW[] = {'m','e','s','s','a','g','e',0};
31 static const WCHAR nameW[] = {'n','a','m','e',0};
32 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
33 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
35 /* ECMA-262 3rd Edition 15.11.4.4 */
36 static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags,
37 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
39 jsdisp_t *jsthis;
40 BSTR name = NULL, msg = NULL, ret = NULL;
41 VARIANT v;
42 HRESULT hres;
44 static const WCHAR object_errorW[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
46 TRACE("\n");
48 jsthis = get_jsdisp(vthis);
49 if(!jsthis || ctx->version < 2) {
50 if(retv) {
51 V_VT(retv) = VT_BSTR;
52 V_BSTR(retv) = SysAllocString(object_errorW);
53 if(!V_BSTR(retv))
54 return E_OUTOFMEMORY;
56 return S_OK;
59 hres = jsdisp_propget_name(jsthis, nameW, &v, ei, caller);
60 if(FAILED(hres))
61 return hres;
63 if(V_VT(&v) != VT_EMPTY) {
64 hres = to_string(ctx, &v, ei, &name);
65 VariantClear(&v);
66 if(FAILED(hres))
67 return hres;
68 if(!*name) {
69 SysFreeString(name);
70 name = NULL;
74 hres = jsdisp_propget_name(jsthis, messageW, &v, ei, caller);
75 if(SUCCEEDED(hres)) {
76 if(V_VT(&v) != VT_EMPTY) {
77 hres = to_string(ctx, &v, ei, &msg);
78 VariantClear(&v);
79 if(SUCCEEDED(hres) && !*msg) {
80 SysFreeString(msg);
81 msg = NULL;
86 if(SUCCEEDED(hres)) {
87 if(name && msg) {
88 DWORD name_len, msg_len;
90 name_len = SysStringLen(name);
91 msg_len = SysStringLen(msg);
93 ret = SysAllocStringLen(NULL, name_len + msg_len + 2);
94 if(ret) {
95 memcpy(ret, name, name_len*sizeof(WCHAR));
96 ret[name_len] = ':';
97 ret[name_len+1] = ' ';
98 memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR));
100 }else if(name) {
101 ret = name;
102 name = NULL;
103 }else if(msg) {
104 ret = msg;
105 msg = NULL;
106 }else {
107 ret = SysAllocString(object_errorW);
111 SysFreeString(msg);
112 SysFreeString(name);
113 if(FAILED(hres))
114 return hres;
115 if(!ret)
116 return E_OUTOFMEMORY;
118 if(retv) {
119 V_VT(retv) = VT_BSTR;
120 V_BSTR(retv) = ret;
121 }else {
122 SysFreeString(ret);
125 return S_OK;
128 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
129 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
131 TRACE("\n");
133 switch(flags) {
134 case INVOKE_FUNC:
135 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
136 default:
137 FIXME("unimplemented flags %x\n", flags);
138 return E_NOTIMPL;
141 return S_OK;
144 static const builtin_prop_t Error_props[] = {
145 {toStringW, Error_toString, PROPF_METHOD}
148 static const builtin_info_t Error_info = {
149 JSCLASS_ERROR,
150 {NULL, Error_value, 0},
151 sizeof(Error_props)/sizeof(*Error_props),
152 Error_props,
153 NULL,
154 NULL
157 static const builtin_info_t ErrorInst_info = {
158 JSCLASS_ERROR,
159 {NULL, Error_value, 0},
161 NULL,
162 NULL,
163 NULL
166 static HRESULT alloc_error(script_ctx_t *ctx, jsdisp_t *prototype,
167 jsdisp_t *constr, jsdisp_t **ret)
169 jsdisp_t *err;
170 HRESULT hres;
172 err = heap_alloc_zero(sizeof(*err));
173 if(!err)
174 return E_OUTOFMEMORY;
176 if(prototype)
177 hres = init_dispex(err, ctx, &Error_info, prototype);
178 else
179 hres = init_dispex_from_constr(err, ctx, &ErrorInst_info,
180 constr ? constr : ctx->error_constr);
181 if(FAILED(hres)) {
182 heap_free(err);
183 return hres;
186 *ret = err;
187 return S_OK;
190 static HRESULT create_error(script_ctx_t *ctx, jsdisp_t *constr,
191 UINT number, const WCHAR *msg, jsdisp_t **ret)
193 jsdisp_t *err;
194 VARIANT v;
195 HRESULT hres;
197 hres = alloc_error(ctx, NULL, constr, &err);
198 if(FAILED(hres))
199 return hres;
201 V_VT(&v) = VT_I4;
202 V_I4(&v) = number;
203 hres = jsdisp_propput_name(err, numberW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
204 if(FAILED(hres)) {
205 jsdisp_release(err);
206 return hres;
209 V_VT(&v) = VT_BSTR;
210 if(msg) V_BSTR(&v) = SysAllocString(msg);
211 else V_BSTR(&v) = SysAllocStringLen(NULL, 0);
212 if(V_BSTR(&v)) {
213 hres = jsdisp_propput_name(err, messageW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
214 if(SUCCEEDED(hres))
215 hres = jsdisp_propput_name(err, descriptionW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
216 SysFreeString(V_BSTR(&v));
217 }else {
218 hres = E_OUTOFMEMORY;
220 if(FAILED(hres)) {
221 jsdisp_release(err);
222 return hres;
225 *ret = err;
226 return S_OK;
229 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
230 VARIANT *retv, jsexcept_t *ei, jsdisp_t *constr) {
231 jsdisp_t *err;
232 UINT num = 0;
233 BSTR msg = NULL;
234 HRESULT hres;
236 if(arg_cnt(dp)) {
237 VARIANT numv;
239 hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
240 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
241 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
242 else if(V_VT(&numv) == VT_I4)
243 num = V_I4(&numv);
244 else
245 num = V_R8(&numv);
247 if(FAILED(hres))
248 return hres;
251 if(arg_cnt(dp)>1 && !msg) {
252 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
253 if(FAILED(hres))
254 return hres;
257 switch(flags) {
258 case INVOKE_FUNC:
259 case DISPATCH_CONSTRUCT:
260 hres = create_error(ctx, constr, num, msg, &err);
261 SysFreeString(msg);
263 if(FAILED(hres))
264 return hres;
266 if(retv)
267 var_set_jsdisp(retv, err);
268 else
269 jsdisp_release(err);
271 return S_OK;
273 default:
274 FIXME("unimplemented flags %x\n", flags);
275 return E_NOTIMPL;
279 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
280 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
282 TRACE("\n");
283 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
286 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
287 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
289 TRACE("\n");
290 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
293 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
294 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
296 TRACE("\n");
297 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
300 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
301 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
303 TRACE("\n");
304 return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
307 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
308 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
310 TRACE("\n");
311 return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
314 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
315 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
317 TRACE("\n");
318 return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
321 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
322 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
324 TRACE("\n");
325 return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
328 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
329 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
331 TRACE("\n");
332 return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
335 HRESULT init_error_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
337 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
338 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
339 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
340 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
341 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
342 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
343 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
344 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
345 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
346 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
347 jsdisp_t **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
348 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
349 &ctx->syntax_error_constr, &ctx->type_error_constr,
350 &ctx->uri_error_constr};
351 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
352 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
353 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
355 jsdisp_t *err;
356 INT i;
357 VARIANT v;
358 HRESULT hres;
360 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
361 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
362 if(FAILED(hres))
363 return hres;
365 V_VT(&v) = VT_BSTR;
366 V_BSTR(&v) = SysAllocString(names[i]);
367 if(!V_BSTR(&v)) {
368 jsdisp_release(err);
369 return E_OUTOFMEMORY;
372 hres = jsdisp_propput_name(err, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
374 if(SUCCEEDED(hres))
375 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
376 PROPF_CONSTR|1, err, constr_addr[i]);
378 jsdisp_release(err);
379 VariantClear(&v);
380 if(FAILED(hres))
381 return hres;
384 return S_OK;
387 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str, jsdisp_t *constr)
389 WCHAR buf[1024], *pos = NULL;
390 jsdisp_t *err;
391 HRESULT hres;
393 if(!is_jscript_error(error))
394 return error;
396 buf[0] = '\0';
397 LoadStringW(jscript_hinstance, HRESULT_CODE(error), buf, sizeof(buf)/sizeof(WCHAR));
399 if(str) pos = strchrW(buf, '|');
400 if(pos) {
401 int len = strlenW(str);
402 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
403 memcpy(pos, str, len*sizeof(WCHAR));
406 WARN("%s\n", debugstr_w(buf));
408 hres = create_error(ctx, constr, error, buf, &err);
409 if(FAILED(hres))
410 return hres;
412 if(ei)
413 var_set_jsdisp(&ei->var, err);
414 return error;
417 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
419 return throw_error(ctx, ei, error, str, ctx->error_constr);
422 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
424 return throw_error(ctx, ei, error, str, ctx->range_error_constr);
427 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
429 return throw_error(ctx, ei, error, str, ctx->reference_error_constr);
432 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
434 return throw_error(ctx, ei, error, str, ctx->regexp_error_constr);
437 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
439 return throw_error(ctx, ei, error, str, ctx->syntax_error_constr);
442 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
444 return throw_error(ctx, ei, error, str, ctx->type_error_constr);
447 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, HRESULT error, const WCHAR *str)
449 return throw_error(ctx, ei, error, str, ctx->uri_error_constr);