iphlpapi: Remove stray code (clang).
[wine] / dlls / jscript / error.c
1 /*
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  */
18 #include "config.h"
19 #include "wine/port.h"
20
21 #include <math.h>
22
23 #include "jscript.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28
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};
34
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)
38 {
39     jsdisp_t *jsthis;
40     BSTR name = NULL, msg = NULL, ret = NULL;
41     VARIANT v;
42     HRESULT hres;
43
44     static const WCHAR object_errorW[] = {'[','o','b','j','e','c','t',' ','E','r','r','o','r',']',0};
45
46     TRACE("\n");
47
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;
55         }
56         return S_OK;
57     }
58
59     hres = jsdisp_propget_name(jsthis, nameW, &v, ei, caller);
60     if(FAILED(hres))
61         return hres;
62
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;
71         }
72     }
73
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;
82             }
83         }
84     }
85
86     if(SUCCEEDED(hres)) {
87         if(name && msg) {
88             DWORD name_len, msg_len;
89
90             name_len = SysStringLen(name);
91             msg_len = SysStringLen(msg);
92
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));
99             }
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);
108             if(!V_BSTR(retv))
109                 hres = E_OUTOFMEMORY;
110         }
111     }
112
113     SysFreeString(msg);
114     SysFreeString(name);
115     if(FAILED(hres))
116         return hres;
117     if(!ret)
118         return E_OUTOFMEMORY;
119
120     if(retv) {
121         V_VT(retv) = VT_BSTR;
122         V_BSTR(retv) = ret;
123     }else {
124         SysFreeString(ret);
125     }
126
127     return S_OK;
128 }
129
130 static HRESULT Error_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
131         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
132 {
133     TRACE("\n");
134
135     switch(flags) {
136     case INVOKE_FUNC:
137         return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
138     default:
139         FIXME("unimplemented flags %x\n", flags);
140         return E_NOTIMPL;
141     }
142
143     return S_OK;
144 }
145
146 static const builtin_prop_t Error_props[] = {
147     {toStringW,                 Error_toString,                     PROPF_METHOD}
148 };
149
150 static const builtin_info_t Error_info = {
151     JSCLASS_ERROR,
152     {NULL, Error_value, 0},
153     sizeof(Error_props)/sizeof(*Error_props),
154     Error_props,
155     NULL,
156     NULL
157 };
158
159 static const builtin_info_t ErrorInst_info = {
160     JSCLASS_ERROR,
161     {NULL, Error_value, 0},
162     0,
163     NULL,
164     NULL,
165     NULL
166 };
167
168 static HRESULT alloc_error(script_ctx_t *ctx, jsdisp_t *prototype,
169         jsdisp_t *constr, jsdisp_t **ret)
170 {
171     jsdisp_t *err;
172     HRESULT hres;
173
174     err = heap_alloc_zero(sizeof(*err));
175     if(!err)
176         return E_OUTOFMEMORY;
177
178     if(prototype)
179         hres = init_dispex(err, ctx, &Error_info, prototype);
180     else
181         hres = init_dispex_from_constr(err, ctx, &ErrorInst_info,
182             constr ? constr : ctx->error_constr);
183     if(FAILED(hres)) {
184         heap_free(err);
185         return hres;
186     }
187
188     *ret = err;
189     return S_OK;
190 }
191
192 static HRESULT create_error(script_ctx_t *ctx, jsdisp_t *constr,
193         UINT number, const WCHAR *msg, jsdisp_t **ret)
194 {
195     jsdisp_t *err;
196     VARIANT v;
197     HRESULT hres;
198
199     hres = alloc_error(ctx, NULL, constr, &err);
200     if(FAILED(hres))
201         return hres;
202
203     V_VT(&v) = VT_I4;
204     V_I4(&v) = number;
205     hres = jsdisp_propput_name(err, numberW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
206     if(FAILED(hres)) {
207         jsdisp_release(err);
208         return hres;
209     }
210
211     V_VT(&v) = VT_BSTR;
212     if(msg) V_BSTR(&v) = SysAllocString(msg);
213     else V_BSTR(&v) = SysAllocStringLen(NULL, 0);
214     if(V_BSTR(&v)) {
215         hres = jsdisp_propput_name(err, messageW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
216         if(SUCCEEDED(hres))
217             hres = jsdisp_propput_name(err, descriptionW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
218         SysFreeString(V_BSTR(&v));
219     }else {
220         hres = E_OUTOFMEMORY;
221     }
222     if(FAILED(hres)) {
223         jsdisp_release(err);
224         return hres;
225     }
226
227     *ret = err;
228     return S_OK;
229 }
230
231 static HRESULT error_constr(script_ctx_t *ctx, WORD flags, DISPPARAMS *dp,
232         VARIANT *retv, jsexcept_t *ei, jsdisp_t *constr) {
233     jsdisp_t *err;
234     UINT num = 0;
235     BSTR msg = NULL;
236     HRESULT hres;
237
238     if(arg_cnt(dp)) {
239         VARIANT numv;
240
241         hres = to_number(ctx, get_arg(dp, 0), ei, &numv);
242         if(FAILED(hres) || (V_VT(&numv)==VT_R8 && isnan(V_R8(&numv))))
243             hres = to_string(ctx, get_arg(dp, 0), ei, &msg);
244         else if(V_VT(&numv) == VT_I4)
245             num = V_I4(&numv);
246         else
247             num = V_R8(&numv);
248
249         if(FAILED(hres))
250             return hres;
251     }
252
253     if(arg_cnt(dp)>1 && !msg) {
254         hres = to_string(ctx, get_arg(dp, 1), ei, &msg);
255         if(FAILED(hres))
256             return hres;
257     }
258
259     switch(flags) {
260     case INVOKE_FUNC:
261     case DISPATCH_CONSTRUCT:
262         hres = create_error(ctx, constr, num, msg, &err);
263         SysFreeString(msg);
264
265         if(FAILED(hres))
266             return hres;
267
268         if(retv)
269             var_set_jsdisp(retv, err);
270         else
271             jsdisp_release(err);
272
273         return S_OK;
274
275     default:
276         FIXME("unimplemented flags %x\n", flags);
277         return E_NOTIMPL;
278     }
279 }
280
281 static HRESULT ErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
282         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
283 {
284     TRACE("\n");
285     return error_constr(ctx, flags, dp, retv, ei, ctx->error_constr);
286 }
287
288 static HRESULT EvalErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
289         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
290 {
291     TRACE("\n");
292     return error_constr(ctx, flags, dp, retv, ei, ctx->eval_error_constr);
293 }
294
295 static HRESULT RangeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
296         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
297 {
298     TRACE("\n");
299     return error_constr(ctx, flags, dp, retv, ei, ctx->range_error_constr);
300 }
301
302 static HRESULT ReferenceErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
303         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
304 {
305     TRACE("\n");
306     return error_constr(ctx, flags, dp, retv, ei, ctx->reference_error_constr);
307 }
308
309 static HRESULT RegExpErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
310         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
311 {
312     TRACE("\n");
313     return error_constr(ctx, flags, dp, retv, ei, ctx->regexp_error_constr);
314 }
315
316 static HRESULT SyntaxErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
317         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
318 {
319     TRACE("\n");
320     return error_constr(ctx, flags, dp, retv, ei, ctx->syntax_error_constr);
321 }
322
323 static HRESULT TypeErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
324         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
325 {
326     TRACE("\n");
327     return error_constr(ctx, flags, dp, retv, ei, ctx->type_error_constr);
328 }
329
330 static HRESULT URIErrorConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags,
331         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
332 {
333     TRACE("\n");
334     return error_constr(ctx, flags, dp, retv, ei, ctx->uri_error_constr);
335 }
336
337 HRESULT init_error_constr(script_ctx_t *ctx, jsdisp_t *object_prototype)
338 {
339     static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
340     static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
341     static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
342     static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
343     static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','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, RegExpErrorW, SyntaxErrorW, TypeErrorW, URIErrorW};
349     jsdisp_t **constr_addr[] = {&ctx->error_constr, &ctx->eval_error_constr,
350         &ctx->range_error_constr, &ctx->reference_error_constr, &ctx->regexp_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, RegExpErrorConstr_value,
355         SyntaxErrorConstr_value, TypeErrorConstr_value, URIErrorConstr_value};
356
357     jsdisp_t *err;
358     INT i;
359     VARIANT v;
360     HRESULT hres;
361
362     for(i=0; i < sizeof(names)/sizeof(names[0]); i++) {
363         hres = alloc_error(ctx, i==0 ? object_prototype : NULL, NULL, &err);
364         if(FAILED(hres))
365             return hres;
366
367         V_VT(&v) = VT_BSTR;
368         V_BSTR(&v) = SysAllocString(names[i]);
369         if(!V_BSTR(&v)) {
370             jsdisp_release(err);
371             return E_OUTOFMEMORY;
372         }
373
374         hres = jsdisp_propput_name(err, nameW, &v, NULL/*FIXME*/, NULL/*FIXME*/);
375
376         if(SUCCEEDED(hres))
377             hres = create_builtin_function(ctx, constr_val[i], names[i], NULL,
378                     PROPF_CONSTR|1, err, constr_addr[i]);
379
380         jsdisp_release(err);
381         VariantClear(&v);
382         if(FAILED(hres))
383             return hres;
384     }
385
386     return S_OK;
387 }
388
389 static HRESULT throw_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str, jsdisp_t *constr)
390 {
391     WCHAR buf[1024], *pos = NULL;
392     jsdisp_t *err;
393     HRESULT hres;
394
395     buf[0] = '\0';
396     LoadStringW(jscript_hinstance, id&0xFFFF,  buf, sizeof(buf)/sizeof(WCHAR));
397
398     if(str) pos = strchrW(buf, '|');
399     if(pos) {
400         int len = strlenW(str);
401         memmove(pos+len, pos+1, (strlenW(pos+1)+1)*sizeof(WCHAR));
402         memcpy(pos, str, len*sizeof(WCHAR));
403     }
404
405     WARN("%s\n", debugstr_w(buf));
406
407     id |= JSCRIPT_ERROR;
408     hres = create_error(ctx, constr, id, buf, &err);
409     if(FAILED(hres))
410         return hres;
411
412     if(ei)
413         var_set_jsdisp(&ei->var, err);
414     return id;
415 }
416
417 HRESULT throw_generic_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
418 {
419     return throw_error(ctx, ei, id, str, ctx->error_constr);
420 }
421
422 HRESULT throw_range_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
423 {
424     return throw_error(ctx, ei, id, str, ctx->range_error_constr);
425 }
426
427 HRESULT throw_reference_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
428 {
429     return throw_error(ctx, ei, id, str, ctx->reference_error_constr);
430 }
431
432 HRESULT throw_regexp_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
433 {
434     return throw_error(ctx, ei, id, str, ctx->regexp_error_constr);
435 }
436
437 HRESULT throw_syntax_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
438 {
439     return throw_error(ctx, ei, id, str, ctx->syntax_error_constr);
440 }
441
442 HRESULT throw_type_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
443 {
444     return throw_error(ctx, ei, id, str, ctx->type_error_constr);
445 }
446
447 HRESULT throw_uri_error(script_ctx_t *ctx, jsexcept_t *ei, UINT id, const WCHAR *str)
448 {
449     return throw_error(ctx, ei, id, str, ctx->uri_error_constr);
450 }