jscript: Throw more SyntaxErrors in parser.
[wine] / dlls / jscript / global.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
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
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <math.h>
23
24 #include "jscript.h"
25 #include "engine.h"
26
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30
31 static const WCHAR NaNW[] = {'N','a','N',0};
32 static const WCHAR InfinityW[] = {'I','n','f','i','n','i','t','y',0};
33 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
34 static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
35 static const WCHAR DateW[] = {'D','a','t','e',0};
36 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
37 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
38 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
39 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
40 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
41 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
42 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
43 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
44 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
45 static const WCHAR ObjectW[] = {'O','b','j','e','c','t',0};
46 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
47 static const WCHAR RegExpW[] = {'R','e','g','E','x','p',0};
48 static const WCHAR ActiveXObjectW[] = {'A','c','t','i','v','e','X','O','b','j','e','c','t',0};
49 static const WCHAR VBArrayW[] = {'V','B','A','r','r','a','y',0};
50 static const WCHAR EnumeratorW[] = {'E','n','u','m','e','r','a','t','o','r',0};
51 static const WCHAR escapeW[] = {'e','s','c','a','p','e',0};
52 static const WCHAR evalW[] = {'e','v','a','l',0};
53 static const WCHAR isNaNW[] = {'i','s','N','a','N',0};
54 static const WCHAR isFiniteW[] = {'i','s','F','i','n','i','t','e',0};
55 static const WCHAR parseIntW[] = {'p','a','r','s','e','I','n','t',0};
56 static const WCHAR parseFloatW[] = {'p','a','r','s','e','F','l','o','a','t',0};
57 static const WCHAR unescapeW[] = {'u','n','e','s','c','a','p','e',0};
58 static const WCHAR _GetObjectW[] = {'G','e','t','O','b','j','e','c','t',0};
59 static const WCHAR ScriptEngineW[] = {'S','c','r','i','p','t','E','n','g','i','n','e',0};
60 static const WCHAR ScriptEngineMajorVersionW[] =
61     {'S','c','r','i','p','t','E','n','g','i','n','e','M','a','j','o','r','V','e','r','s','i','o','n',0};
62 static const WCHAR ScriptEngineMinorVersionW[] =
63     {'S','c','r','i','p','t','E','n','g','i','n','e','M','i','n','o','r','V','e','r','s','i','o','n',0};
64 static const WCHAR ScriptEngineBuildVersionW[] =
65     {'S','c','r','i','p','t','E','n','g','i','n','e','B','u','i','l','d','V','e','r','s','i','o','n',0};
66 static const WCHAR CollectGarbageW[] = {'C','o','l','l','e','c','t','G','a','r','b','a','g','e',0};
67 static const WCHAR MathW[] = {'M','a','t','h',0};
68 static const WCHAR encodeURIW[] = {'e','n','c','o','d','e','U','R','I',0};
69
70 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
71
72 static int uri_char_table[] = {
73     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 00-0f */
74     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 10-1f */
75     0,2,0,0,1,0,1,2,2,2,2,1,1,2,2,1, /* 20-2f */
76     2,2,2,2,2,2,2,2,2,2,1,1,0,1,0,1, /* 30-3f */
77     1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 40-4f */
78     2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,2, /* 50-5f */
79     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 60-6f */
80     2,2,2,2,2,2,2,2,2,2,2,0,0,0,2,0, /* 70-7f */
81 };
82
83 /* 1 - reserved */
84 /* 2 - unescaped */
85
86 static inline BOOL is_uri_reserved(WCHAR c)
87 {
88     return c < 128 && uri_char_table[c] == 1;
89 }
90
91 static inline BOOL is_uri_unescaped(WCHAR c)
92 {
93     return c < 128 && uri_char_table[c] == 2;
94 }
95
96 static WCHAR int_to_char(int i)
97 {
98     if(i < 10)
99         return '0'+i;
100     return 'A'+i-10;
101 }
102
103 static HRESULT constructor_call(DispatchEx *constr, LCID lcid, WORD flags, DISPPARAMS *dp,
104         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
105 {
106     if(flags != DISPATCH_PROPERTYGET)
107         return jsdisp_call_value(constr, lcid, flags, dp, retv, ei, sp);
108
109     V_VT(retv) = VT_DISPATCH;
110     V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(constr);
111     IDispatchEx_AddRef(_IDispatchEx_(constr));
112     return S_OK;
113 }
114
115 static HRESULT JSGlobal_NaN(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
116         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
117 {
118     TRACE("\n");
119
120     switch(flags) {
121     case DISPATCH_PROPERTYGET:
122         num_set_nan(retv);
123         break;
124
125     default:
126         FIXME("unimplemented flags %x\n", flags);
127         return E_NOTIMPL;
128     }
129
130     return S_OK;
131 }
132
133 static HRESULT JSGlobal_Infinity(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
134         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
135 {
136     TRACE("\n");
137
138     switch(flags) {
139     case DISPATCH_PROPERTYGET:
140         num_set_inf(retv, TRUE);
141         break;
142
143     default:
144         FIXME("unimplemented flags %x\n", flags);
145         return E_NOTIMPL;
146     }
147
148     return S_OK;
149 }
150
151 static HRESULT JSGlobal_Array(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
152         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
153 {
154     TRACE("\n");
155
156     return constructor_call(dispex->ctx->array_constr, lcid, flags, dp, retv, ei, sp);
157 }
158
159 static HRESULT JSGlobal_Boolean(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
160         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
161 {
162     TRACE("\n");
163
164     return constructor_call(dispex->ctx->bool_constr, lcid, flags, dp, retv, ei, sp);
165 }
166
167 static HRESULT JSGlobal_Date(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
168         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
169 {
170     TRACE("\n");
171
172     return constructor_call(dispex->ctx->date_constr, lcid, flags, dp, retv, ei, sp);
173 }
174
175 static HRESULT JSGlobal_Error(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
176         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
177 {
178     TRACE("\n");
179
180     return constructor_call(dispex->ctx->error_constr, lcid, flags, dp, retv, ei, sp);
181 }
182
183 static HRESULT JSGlobal_EvalError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
184         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
185 {
186     TRACE("\n");
187
188     return constructor_call(dispex->ctx->eval_error_constr, lcid, flags, dp, retv, ei, sp);
189 }
190
191 static HRESULT JSGlobal_RangeError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
192         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
193 {
194     TRACE("\n");
195
196     return constructor_call(dispex->ctx->range_error_constr, lcid, flags, dp, retv, ei, sp);
197 }
198
199 static HRESULT JSGlobal_ReferenceError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
200         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
201 {
202     TRACE("\n");
203
204     return constructor_call(dispex->ctx->reference_error_constr, lcid, flags, dp, retv, ei, sp);
205 }
206
207 static HRESULT JSGlobal_SyntaxError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
208         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
209 {
210     TRACE("\n");
211
212     return constructor_call(dispex->ctx->syntax_error_constr, lcid, flags, dp, retv, ei, sp);
213 }
214
215 static HRESULT JSGlobal_TypeError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
216         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
217 {
218     TRACE("\n");
219
220     return constructor_call(dispex->ctx->type_error_constr, lcid, flags, dp, retv, ei, sp);
221 }
222
223 static HRESULT JSGlobal_URIError(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
224         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
225 {
226     TRACE("\n");
227
228     return constructor_call(dispex->ctx->uri_error_constr, lcid, flags, dp, retv, ei, sp);
229 }
230
231 static HRESULT JSGlobal_Function(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
232         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
233 {
234     TRACE("\n");
235
236     return constructor_call(dispex->ctx->function_constr, lcid, flags, dp, retv, ei, sp);
237 }
238
239 static HRESULT JSGlobal_Number(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
240         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
241 {
242     TRACE("\n");
243
244     return constructor_call(dispex->ctx->number_constr, lcid, flags, dp, retv, ei, sp);
245 }
246
247 static HRESULT JSGlobal_Object(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
248         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
249 {
250     TRACE("\n");
251
252     return constructor_call(dispex->ctx->object_constr, lcid, flags, dp, retv, ei, sp);
253 }
254
255 static HRESULT JSGlobal_String(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
256         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
257 {
258     TRACE("\n");
259
260     return constructor_call(dispex->ctx->string_constr, lcid, flags, dp, retv, ei, sp);
261 }
262
263 static HRESULT JSGlobal_RegExp(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
264         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
265 {
266     TRACE("\n");
267
268     return constructor_call(dispex->ctx->regexp_constr, lcid, flags, dp, retv, ei, sp);
269 }
270
271 static HRESULT JSGlobal_ActiveXObject(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
272         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
273 {
274     FIXME("\n");
275     return E_NOTIMPL;
276 }
277
278 static HRESULT JSGlobal_VBArray(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
279         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
280 {
281     FIXME("\n");
282     return E_NOTIMPL;
283 }
284
285 static HRESULT JSGlobal_Enumerator(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
286         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
287 {
288     FIXME("\n");
289     return E_NOTIMPL;
290 }
291
292 static HRESULT JSGlobal_escape(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
293         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
294 {
295     FIXME("\n");
296     return E_NOTIMPL;
297 }
298
299 /* ECMA-262 3rd Edition    15.1.2.1 */
300 static HRESULT JSGlobal_eval(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
301         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
302 {
303     parser_ctx_t *parser_ctx;
304     VARIANT *arg;
305     HRESULT hres;
306
307     TRACE("\n");
308
309     if(!arg_cnt(dp)) {
310         if(retv)
311             V_VT(retv) = VT_EMPTY;
312         return S_OK;
313     }
314
315     arg = get_arg(dp, 0);
316     if(V_VT(arg) != VT_BSTR) {
317         if(retv) {
318             V_VT(retv) = VT_EMPTY;
319             return VariantCopy(retv, arg);
320         }
321         return S_OK;
322     }
323
324     if(!dispex->ctx->exec_ctx) {
325         FIXME("No active exec_ctx\n");
326         return E_UNEXPECTED;
327     }
328
329     TRACE("parsing %s\n", debugstr_w(V_BSTR(arg)));
330     hres = script_parse(dispex->ctx, V_BSTR(arg), NULL, &parser_ctx);
331     if(FAILED(hres)) {
332         WARN("parse (%s) failed: %08x\n", debugstr_w(V_BSTR(arg)), hres);
333         return throw_syntax_error(dispex->ctx, ei, hres, NULL);
334     }
335
336     hres = exec_source(dispex->ctx->exec_ctx, parser_ctx, parser_ctx->source, ei, retv);
337     parser_release(parser_ctx);
338
339     return hres;
340 }
341
342 static HRESULT JSGlobal_isNaN(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
343         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
344 {
345     VARIANT_BOOL ret = VARIANT_FALSE;
346     VARIANT num;
347     HRESULT hres;
348
349     TRACE("\n");
350
351     if(arg_cnt(dp)) {
352         hres = to_number(dispex->ctx, get_arg(dp,0), ei, &num);
353         if(FAILED(hres))
354             return hres;
355
356         if(V_VT(&num) == VT_R8 && isnan(V_R8(&num)))
357             ret = VARIANT_TRUE;
358     }else {
359         ret = VARIANT_TRUE;
360     }
361
362     if(retv) {
363         V_VT(retv) = VT_BOOL;
364         V_BOOL(retv) = ret;
365     }
366     return S_OK;
367 }
368
369 static HRESULT JSGlobal_isFinite(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
370         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
371 {
372     VARIANT_BOOL ret = VARIANT_FALSE;
373     HRESULT hres;
374
375     TRACE("\n");
376
377     if(arg_cnt(dp)) {
378         VARIANT num;
379
380         hres = to_number(dispex->ctx, get_arg(dp,0), ei, &num);
381         if(FAILED(hres))
382             return hres;
383
384         if(V_VT(&num) != VT_R8 || (!isinf(V_R8(&num)) && !isnan(V_R8(&num))))
385             ret = VARIANT_TRUE;
386     }
387
388     if(retv) {
389         V_VT(retv) = VT_BOOL;
390         V_BOOL(retv) = ret;
391     }
392     return S_OK;
393 }
394
395 static INT char_to_int(WCHAR c)
396 {
397     if('0' <= c && c <= '9')
398         return c - '0';
399     if('a' <= c && c <= 'z')
400         return c - 'a' + 10;
401     if('A' <= c && c <= 'Z')
402         return c - 'A' + 10;
403     return 100;
404 }
405
406 static HRESULT JSGlobal_parseInt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
407         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
408 {
409     DOUBLE ret = 0.0;
410     INT radix=10, i;
411     WCHAR *ptr;
412     BOOL neg = FALSE;
413     BSTR str;
414     HRESULT hres;
415
416     if(!arg_cnt(dp)) {
417         if(retv) num_set_nan(retv);
418         return S_OK;
419     }
420
421     if(arg_cnt(dp) >= 2) {
422         hres = to_int32(dispex->ctx, get_arg(dp, 1), ei, &radix);
423         if(FAILED(hres))
424             return hres;
425
426         if(!radix) {
427             radix = 10;
428         }else if(radix < 2 || radix > 36) {
429             WARN("radix %d out of range\n", radix);
430             return E_FAIL;
431         }
432     }
433
434     hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
435     if(FAILED(hres))
436         return hres;
437
438     for(ptr = str; isspaceW(*ptr); ptr++);
439
440     switch(*ptr) {
441     case '+':
442         ptr++;
443         break;
444     case '-':
445         neg = TRUE;
446         ptr++;
447         break;
448     case '0':
449         ptr++;
450         if(*ptr == 'x' || *ptr == 'X') {
451             radix = 16;
452             ptr++;
453         }
454     }
455
456     while(*ptr) {
457         i = char_to_int(*ptr++);
458         if(i > radix)
459             break;
460
461         ret = ret*radix + i;
462     }
463
464     SysFreeString(str);
465
466     if(neg)
467         ret = -ret;
468
469     if(retv)
470         num_set_val(retv, ret);
471     return S_OK;
472 }
473
474 static HRESULT JSGlobal_parseFloat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
475         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
476 {
477     FIXME("\n");
478     return E_NOTIMPL;
479 }
480
481 static HRESULT JSGlobal_unescape(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
482         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
483 {
484     FIXME("\n");
485     return E_NOTIMPL;
486 }
487
488 static HRESULT JSGlobal_GetObject(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
489         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
490 {
491     FIXME("\n");
492     return E_NOTIMPL;
493 }
494
495 static HRESULT JSGlobal_ScriptEngine(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
496         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
497 {
498     FIXME("\n");
499     return E_NOTIMPL;
500 }
501
502 static HRESULT JSGlobal_ScriptEngineMajorVersion(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
503         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
504 {
505     FIXME("\n");
506     return E_NOTIMPL;
507 }
508
509 static HRESULT JSGlobal_ScriptEngineMinorVersion(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
510         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
511 {
512     FIXME("\n");
513     return E_NOTIMPL;
514 }
515
516 static HRESULT JSGlobal_ScriptEngineBuildVersion(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
517         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
518 {
519     FIXME("\n");
520     return E_NOTIMPL;
521 }
522
523 static HRESULT JSGlobal_CollectGarbage(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
524         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
525 {
526     FIXME("\n");
527     return E_NOTIMPL;
528 }
529
530 static HRESULT JSGlobal_encodeURI(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
531         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
532 {
533     const WCHAR *ptr;
534     DWORD len = 0, i;
535     char buf[4];
536     BSTR str, ret;
537     WCHAR *rptr;
538     HRESULT hres;
539
540     TRACE("\n");
541
542     if(!arg_cnt(dp)) {
543         if(retv) {
544             ret = SysAllocString(undefinedW);
545             if(!ret)
546                 return E_OUTOFMEMORY;
547
548             V_VT(retv) = VT_BSTR;
549             V_BSTR(retv) = ret;
550         }
551
552         return S_OK;
553     }
554
555     hres = to_string(dispex->ctx, get_arg(dp,0), ei, &str);
556     if(FAILED(hres))
557         return hres;
558
559     for(ptr = str; *ptr; ptr++) {
560         if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
561             len++;
562         }else {
563             i = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, NULL, 0, NULL, NULL)*3;
564             if(!i) {
565                 FIXME("throw URIError\n");
566                 return E_FAIL;
567             }
568
569             len += i;
570         }
571     }
572
573     rptr = ret = SysAllocStringLen(NULL, len);
574     if(!ret)
575         return E_OUTOFMEMORY;
576
577     for(ptr = str; *ptr; ptr++) {
578         if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
579             *rptr++ = *ptr;
580         }else {
581             len = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, buf, sizeof(buf), NULL, NULL);
582             for(i=0; i<len; i++) {
583                 *rptr++ = '%';
584                 *rptr++ = int_to_char((BYTE)buf[i] >> 4);
585                 *rptr++ = int_to_char(buf[i] & 0x0f);
586             }
587         }
588     }
589
590     TRACE("%s -> %s\n", debugstr_w(str), debugstr_w(ret));
591     if(retv) {
592         V_VT(retv) = VT_BSTR;
593         V_BSTR(retv) = ret;
594     }else {
595         SysFreeString(ret);
596     }
597     return S_OK;
598 }
599
600 static const builtin_prop_t JSGlobal_props[] = {
601     {ActiveXObjectW,             JSGlobal_ActiveXObject,             PROPF_METHOD},
602     {ArrayW,                     JSGlobal_Array,                     PROPF_CONSTR},
603     {BooleanW,                   JSGlobal_Boolean,                   PROPF_CONSTR},
604     {CollectGarbageW,            JSGlobal_CollectGarbage,            PROPF_METHOD},
605     {DateW,                      JSGlobal_Date,                      PROPF_CONSTR},
606     {EnumeratorW,                JSGlobal_Enumerator,                PROPF_METHOD},
607     {ErrorW,                     JSGlobal_Error,                     PROPF_CONSTR},
608     {EvalErrorW,                 JSGlobal_EvalError,                 PROPF_CONSTR},
609     {FunctionW,                  JSGlobal_Function,                  PROPF_CONSTR},
610     {_GetObjectW,                JSGlobal_GetObject,                 PROPF_METHOD},
611     {InfinityW,                  JSGlobal_Infinity,                  0},
612 /*  {MathW,                      JSGlobal_Math,                      0},  */
613     {NaNW,                       JSGlobal_NaN,                       0},
614     {NumberW,                    JSGlobal_Number,                    PROPF_CONSTR},
615     {ObjectW,                    JSGlobal_Object,                    PROPF_CONSTR},
616     {RangeErrorW,                JSGlobal_RangeError,                PROPF_CONSTR},
617     {ReferenceErrorW,            JSGlobal_ReferenceError,            PROPF_CONSTR},
618     {RegExpW,                    JSGlobal_RegExp,                    PROPF_CONSTR},
619     {ScriptEngineW,              JSGlobal_ScriptEngine,              PROPF_METHOD},
620     {ScriptEngineBuildVersionW,  JSGlobal_ScriptEngineBuildVersion,  PROPF_METHOD},
621     {ScriptEngineMajorVersionW,  JSGlobal_ScriptEngineMajorVersion,  PROPF_METHOD},
622     {ScriptEngineMinorVersionW,  JSGlobal_ScriptEngineMinorVersion,  PROPF_METHOD},
623     {StringW,                    JSGlobal_String,                    PROPF_CONSTR},
624     {SyntaxErrorW,               JSGlobal_SyntaxError,               PROPF_CONSTR},
625     {TypeErrorW,                 JSGlobal_TypeError,                 PROPF_CONSTR},
626     {URIErrorW,                  JSGlobal_URIError,                  PROPF_CONSTR},
627     {VBArrayW,                   JSGlobal_VBArray,                   PROPF_METHOD},
628     {encodeURIW,                 JSGlobal_encodeURI,                 PROPF_METHOD},
629     {escapeW,                    JSGlobal_escape,                    PROPF_METHOD},
630     {evalW,                      JSGlobal_eval,                      PROPF_METHOD|1},
631     {isFiniteW,                  JSGlobal_isFinite,                  PROPF_METHOD},
632     {isNaNW,                     JSGlobal_isNaN,                     PROPF_METHOD},
633     {parseFloatW,                JSGlobal_parseFloat,                PROPF_METHOD},
634     {parseIntW,                  JSGlobal_parseInt,                  PROPF_METHOD|2},
635     {unescapeW,                  JSGlobal_unescape,                  PROPF_METHOD}
636 };
637
638 static const builtin_info_t JSGlobal_info = {
639     JSCLASS_GLOBAL,
640     {NULL, NULL, 0},
641     sizeof(JSGlobal_props)/sizeof(*JSGlobal_props),
642     JSGlobal_props,
643     NULL,
644     NULL
645 };
646
647 static HRESULT init_constructors(script_ctx_t *ctx, DispatchEx *object_prototype)
648 {
649     HRESULT hres;
650
651     hres = init_function_constr(ctx, object_prototype);
652     if(FAILED(hres))
653         return hres;
654
655     hres = create_object_constr(ctx, object_prototype, &ctx->object_constr);
656     if(FAILED(hres))
657         return hres;
658
659     hres = create_array_constr(ctx, &ctx->array_constr);
660     if(FAILED(hres))
661         return hres;
662
663     hres = create_bool_constr(ctx, &ctx->bool_constr);
664     if(FAILED(hres))
665         return hres;
666
667     hres = create_date_constr(ctx, &ctx->date_constr);
668     if(FAILED(hres))
669         return hres;
670
671     hres = init_error_constr(ctx);
672     if(FAILED(hres))
673         return hres;
674
675     hres = create_number_constr(ctx, &ctx->number_constr);
676     if(FAILED(hres))
677         return hres;
678
679     hres = create_regexp_constr(ctx, &ctx->regexp_constr);
680     if(FAILED(hres))
681         return hres;
682
683     hres = create_string_constr(ctx, &ctx->string_constr);
684     if(FAILED(hres))
685         return hres;
686
687     return S_OK;
688 }
689
690 HRESULT init_global(script_ctx_t *ctx)
691 {
692     DispatchEx *math, *object_prototype;
693     VARIANT var;
694     HRESULT hres;
695
696     if(ctx->global)
697         return S_OK;
698
699     hres = create_object_prototype(ctx, &object_prototype);
700     if(FAILED(hres))
701         return hres;
702
703     hres = init_constructors(ctx, object_prototype);
704     jsdisp_release(object_prototype);
705     if(FAILED(hres))
706         return hres;
707
708     hres = create_dispex(ctx, &JSGlobal_info, NULL, &ctx->global);
709     if(FAILED(hres))
710         return hres;
711
712     hres = create_math(ctx, &math);
713     if(FAILED(hres))
714         return hres;
715
716     V_VT(&var) = VT_DISPATCH;
717     V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(math);
718     hres = jsdisp_propput_name(ctx->global, MathW, ctx->lcid, &var, NULL/*FIXME*/, NULL/*FIXME*/);
719     jsdisp_release(math);
720
721     return hres;
722 }