winemaker: Warn on include path starting with drive letter.
[wine/multimedia.git] / dlls / jscript / error.c
blobe3cfce634ee1e75ac6929a428560ecace8ee072a
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 typedef struct {
30 DispatchEx dispex;
32 VARIANT number;
33 VARIANT description;
34 VARIANT message;
35 } ErrorInstance;
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 nameW[] = {'n','a','m','e',0};
40 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
41 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
43 static inline ErrorInstance *error_from_vdisp(vdisp_t *vdisp)
45 return (ErrorInstance*)vdisp->u.jsdisp;
48 static inline ErrorInstance *error_this(vdisp_t *jsthis)
50 return is_vclass(jsthis, JSCLASS_ERROR) ? error_from_vdisp(jsthis) : NULL;
53 static HRESULT Error_number(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
54 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
56 ErrorInstance *This = error_from_vdisp(jsthis);
58 TRACE("\n");
60 switch(flags) {
61 case DISPATCH_PROPERTYGET:
62 return VariantCopy(retv, &This->number);
63 case DISPATCH_PROPERTYPUT:
64 return VariantCopy(&This->number, get_arg(dp, 0));
65 default:
66 FIXME("unimplemented flags %x\n", flags);
67 return E_NOTIMPL;
71 static HRESULT Error_description(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
72 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
74 ErrorInstance *This = error_from_vdisp(jsthis);
76 TRACE("\n");
78 switch(flags) {
79 case DISPATCH_PROPERTYGET:
80 return VariantCopy(retv, &This->description);
81 case DISPATCH_PROPERTYPUT:
82 return VariantCopy(&This->description, get_arg(dp, 0));
83 default:
84 FIXME("unimplemented flags %x\n", flags);
85 return E_NOTIMPL;
89 /* ECMA-262 3rd Edition 15.11.4.3 */
90 static HRESULT Error_message(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
91 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
93 ErrorInstance *This = error_from_vdisp(jsthis);
95 TRACE("\n");
97 switch(flags) {
98 case DISPATCH_PROPERTYGET:
99 return VariantCopy(retv, &This->message);
100 case DISPATCH_PROPERTYPUT:
101 return VariantCopy(&This->message, get_arg(dp, 0));
102 default:
103 FIXME("unimplemented flags %x\n", flags);
104 return E_NOTIMPL;
108 /* ECMA-262 3rd Edition 15.11.4.4 */
109 static HRESULT Error_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
110 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
112 ErrorInstance *error;
113 BSTR name, msg = NULL, ret = NULL;
114 VARIANT v;
115 HRESULT hres;
117 static const WCHAR str[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
119 TRACE("\n");
121 error = error_this(jsthis);
122 if(ctx->version < 2 || !error) {
123 if(retv) {
124 V_VT(retv) = VT_BSTR;
125 V_BSTR(retv) = SysAllocString(str);
126 if(!V_BSTR(retv))
127 return E_OUTOFMEMORY;
129 return S_OK;
132 hres = jsdisp_propget_name(&error->dispex, nameW, &v, ei, caller);
133 if(FAILED(hres))
134 return hres;
136 hres = to_string(ctx, &v, ei, &name);
137 VariantClear(&v);
138 if(FAILED(hres))
139 return hres;
141 if(V_VT(&error->message) != VT_EMPTY) {
142 hres = to_string(ctx, &error->message, ei, &msg);
143 if(SUCCEEDED(hres) && !*msg) {
144 SysFreeString(msg);
145 msg = NULL;
149 if(SUCCEEDED(hres)) {
150 if(msg) {
151 DWORD name_len, msg_len;
153 name_len = SysStringLen(name);
154 msg_len = SysStringLen(msg);
156 ret = SysAllocStringLen(NULL, name_len + msg_len + 2);
157 if(ret) {
158 memcpy(ret, name, name_len*sizeof(WCHAR));
159 ret[name_len] = ':';
160 ret[name_len+1] = ' ';
161 memcpy(ret+name_len+2, msg, msg_len*sizeof(WCHAR));
163 }else {
164 ret = name;
165 name = NULL;
169 SysFreeString(msg);
170 SysFreeString(name);
171 if(FAILED(hres))
172 return hres;
173 if(!ret)
174 return E_OUTOFMEMORY;
176 if(retv) {
177 V_VT(retv) = VT_BSTR;
178 V_BSTR(retv) = ret;
179 }else {
180 SysFreeString(ret);
183 return S_OK;
186 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
187 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
189 TRACE("\n");
191 switch(flags) {
192 case INVOKE_FUNC:
193 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
194 default:
195 FIXME("unimplemented flags %x\n", flags);
196 return E_NOTIMPL;
199 return S_OK;
202 static void Error_destructor(DispatchEx *dispex)
204 ErrorInstance *This = (ErrorInstance*)dispex;
206 VariantClear(&This->number);
207 VariantClear(&This->description);
208 VariantClear(&This->message);
209 heap_free(This);
212 static const builtin_prop_t Error_props[] = {
213 {descriptionW, Error_description, 0},
214 {messageW, Error_message, 0},
215 {numberW, Error_number, 0},
216 {toStringW, Error_toString, PROPF_METHOD}
219 static const builtin_info_t Error_info = {
220 JSCLASS_ERROR,
221 {NULL, Error_value, 0},
222 sizeof(Error_props)/sizeof(*Error_props),
223 Error_props,
224 Error_destructor,
225 NULL
228 static const builtin_prop_t ErrorInst_props[] = {
229 {descriptionW, Error_description, 0},
230 {messageW, Error_message, 0},
231 {numberW, Error_number, 0},
234 static const builtin_info_t ErrorInst_info = {
235 JSCLASS_ERROR,
236 {NULL, Error_value, 0},
237 sizeof(ErrorInst_props)/sizeof(*ErrorInst_props),
238 ErrorInst_props,
239 Error_destructor,
240 NULL
243 static HRESULT alloc_error(script_ctx_t *ctx, DispatchEx *prototype,
244 DispatchEx *constr, ErrorInstance **ret)
246 ErrorInstance *err;
247 HRESULT hres;
249 err = heap_alloc_zero(sizeof(ErrorInstance));
250 if(!err)
251 return E_OUTOFMEMORY;
253 if(prototype)
254 hres = init_dispex(&err->dispex, ctx, &Error_info, prototype);
255 else
256 hres = init_dispex_from_constr(&err->dispex, ctx, &ErrorInst_info,
257 constr ? constr : ctx->error_constr);
258 if(FAILED(hres)) {
259 heap_free(err);
260 return hres;
263 *ret = err;
264 return S_OK;
267 static HRESULT create_error(script_ctx_t *ctx, DispatchEx *constr,
268 const UINT *number, const WCHAR *msg, DispatchEx **ret)
270 ErrorInstance *err;
271 HRESULT hres;
273 hres = alloc_error(ctx, NULL, constr, &err);
274 if(FAILED(hres))
275 return hres;
277 if(number) {
278 V_VT(&err->number) = VT_I4;
279 V_I4(&err->number) = *number;
282 V_VT(&err->message) = VT_BSTR;
283 if(msg) V_BSTR(&err->message) = SysAllocString(msg);
284 else V_BSTR(&err->message) = SysAllocStringLen(NULL, 0);
286 VariantCopy(&err->description, &err->message);
288 if(!V_BSTR(&err->message)) {
289 heap_free(err);
290 return E_OUTOFMEMORY;
293 *ret = &err->dispex;
294 return S_OK;
297 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
298 VARIANT *retv, jsexcept_t *ei, DispatchEx *constr) {
299 DispatchEx *err;
300 VARIANT numv;
301 UINT num;
302 BSTR msg = NULL;
303 HRESULT hres;
305 V_VT(&numv) = VT_NULL;
307 if(arg_cnt(dp)) {
308 hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
309 if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
310 hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
311 else if(V_VT(&numv) == VT_I4)
312 num = V_I4(&numv);
313 else
314 num = V_R8(&numv);
316 if(FAILED(hres))
317 return hres;
320 if(arg_cnt(dp)>1 && !msg) {
321 hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
322 if(FAILED(hres))
323 return hres;
326 switch(flags) {
327 case INVOKE_FUNC:
328 case DISPATCH_CONSTRUCT:
329 if(V_VT(&numv) == VT_NULL)
330 hres = create_error(ctx, constr, NULL, msg, &err);
331 else
332 hres = create_error(ctx, constr, &num, msg, &err);
333 SysFreeString(msg);
335 if(FAILED(hres))
336 return hres;
338 if(retv) {
339 V_VT(retv) = VT_DISPATCH;
340 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(err);
342 else
343 jsdisp_release(err);
345 return S_OK;
347 default:
348 FIXME("unimplemented flags %x\n", flags);
349 return E_NOTIMPL;
353 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
354 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
356 TRACE("\n");
357 return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
360 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
361 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
363 TRACE("\n");
364 return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
367 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
368 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
370 TRACE("\n");
371 return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
374 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
375 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
377 TRACE("\n");
378 return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
381 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
382 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
384 TRACE("\n");
385 return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
388 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
389 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
391 TRACE("\n");
392 return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
395 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
396 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
398 TRACE("\n");
399 return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
402 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
403 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
405 TRACE("\n");
406 return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
409 HRESULT init_error_constr(script_ctx_t *ctx, DispatchEx *object_prototype)
411 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
412 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
413 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
414 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
415 static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
416 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
417 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
418 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
419 static const WCHAR *names[] = {ErrorW, EvalErrorW, RangeErrorW,
420 ReferenceErrorW, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
421 DispatchEx **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
422 &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_error_constr,
423 &ctx->syntax_error_constr, &ctx->type_error_constr,
424 &ctx->uri_error_constr};
425 static builtin_invoke_t constr_val[] = {ErrorConstr_value, EvalErrorConstr_value,
426 RangeErrorConstr_value, ReferenceErrorConstr_value, RegExpErrorConstr_value,
427 SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
429 ErrorInstance *err;
430 INT i;
431 VARIANT v;
432 HRESULT hres;
434 for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
435 hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
436 if(FAILED(hres))
437 return hres;
439 V_VT(&v) = VT_BSTR;
440 V_BSTR(&v) = SysAllocString(names[i]);
441 if(!V_BSTR(&v)) {
442 jsdisp_release(&err->dispex);
443 return E_OUTOFMEMORY;
446 hres = jsdisp_propput_name(&err->dispex, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
448 if(SUCCEEDED(hres))
449 hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
450 PROPF_CONSTR|1, &err->dispex, constr_addr[i]);
452 jsdisp_release(&err->dispex);
453 VariantClear(&v);
454 if(FAILED(hres))
455 return hres;
458 return S_OK;
461 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, DispatchEx *constr)
463 WCHAR buf[1024], *pos = NULL;
464 DispatchEx *err;
465 HRESULT hres;
467 buf[0] = '\0';
468 LoadStringW(jscript_hinstance, id&0xFFFF, buf, sizeof(buf)/sizeof(WCHAR));
470 if(str) pos = strchrW(buf, '|');
471 if(pos) {
472 int len = strlenW(str);
473 memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
474 memcpy(pos, str, len*sizeof(WCHAR));
477 WARN("%s\n", debugstr_w(buf));
479 id |= JSCRIPT_ERROR;
480 hres = create_error(ctx, constr, &id, buf, &err);
481 if(FAILED(hres))
482 return hres;
484 if(!ei)
485 return id;
487 V_VT(&ei->var) = VT_DISPATCH;
488 V_DISPATCH(&ei->var) = (IDispatch*)_IDispatchEx_(err);
490 return id;
493 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
495 return throw_error(ctx, ei, id, str, ctx->error_constr);
498 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
500 return throw_error(ctx, ei, id, str, ctx->range_error_constr);
503 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
505 return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
508 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
510 return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
513 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
515 return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
518 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
520 return throw_error(ctx, ei, id, str, ctx->type_error_constr);
523 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
525 return throw_error(ctx, ei, id, str, ctx->uri_error_constr);