jscript: Get rid of lcid argument in builtin function implementations.
[wine/multimedia.git] / dlls / jscript / global.c
blob3c4438d4e1a2519ab9fcbe47834c0792caa2090b
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
23 #include <limits.h>
25 #include "jscript.h"
26 #include "engine.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 #define LONGLONG_MAX (((LONGLONG)0x7fffffff<<32)|0xffffffff)
34 static const WCHAR NaNW[] = {'N','a','N',0};
35 static const WCHAR InfinityW[] = {'I','n','f','i','n','i','t','y',0};
36 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
37 static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
38 static const WCHAR DateW[] = {'D','a','t','e',0};
39 static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
40 static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
41 static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
42 static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
43 static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
44 static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
45 static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
46 static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
47 static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
48 static const WCHAR ObjectW[] = {'O','b','j','e','c','t',0};
49 static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
50 static const WCHAR RegExpW[] = {'R','e','g','E','x','p',0};
51 static const WCHAR ActiveXObjectW[] = {'A','c','t','i','v','e','X','O','b','j','e','c','t',0};
52 static const WCHAR VBArrayW[] = {'V','B','A','r','r','a','y',0};
53 static const WCHAR EnumeratorW[] = {'E','n','u','m','e','r','a','t','o','r',0};
54 static const WCHAR escapeW[] = {'e','s','c','a','p','e',0};
55 static const WCHAR evalW[] = {'e','v','a','l',0};
56 static const WCHAR isNaNW[] = {'i','s','N','a','N',0};
57 static const WCHAR isFiniteW[] = {'i','s','F','i','n','i','t','e',0};
58 static const WCHAR parseIntW[] = {'p','a','r','s','e','I','n','t',0};
59 static const WCHAR parseFloatW[] = {'p','a','r','s','e','F','l','o','a','t',0};
60 static const WCHAR unescapeW[] = {'u','n','e','s','c','a','p','e',0};
61 static const WCHAR _GetObjectW[] = {'G','e','t','O','b','j','e','c','t',0};
62 static const WCHAR ScriptEngineW[] = {'S','c','r','i','p','t','E','n','g','i','n','e',0};
63 static const WCHAR ScriptEngineMajorVersionW[] =
64 {'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};
65 static const WCHAR ScriptEngineMinorVersionW[] =
66 {'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};
67 static const WCHAR ScriptEngineBuildVersionW[] =
68 {'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};
69 static const WCHAR CollectGarbageW[] = {'C','o','l','l','e','c','t','G','a','r','b','a','g','e',0};
70 static const WCHAR MathW[] = {'M','a','t','h',0};
71 static const WCHAR encodeURIW[] = {'e','n','c','o','d','e','U','R','I',0};
73 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
75 static int uri_char_table[] = {
76 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 00-0f */
77 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 10-1f */
78 0,2,0,0,1,0,1,2,2,2,2,1,1,2,2,1, /* 20-2f */
79 2,2,2,2,2,2,2,2,2,2,1,1,0,1,0,1, /* 30-3f */
80 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 40-4f */
81 2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,2, /* 50-5f */
82 0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 60-6f */
83 2,2,2,2,2,2,2,2,2,2,2,0,0,0,2,0, /* 70-7f */
86 /* 1 - reserved */
87 /* 2 - unescaped */
89 static inline BOOL is_uri_reserved(WCHAR c)
91 return c < 128 && uri_char_table[c] == 1;
94 static inline BOOL is_uri_unescaped(WCHAR c)
96 return c < 128 && uri_char_table[c] == 2;
99 static WCHAR int_to_char(int i)
101 if(i < 10)
102 return '0'+i;
103 return 'A'+i-10;
106 static HRESULT constructor_call(DispatchEx *constr, WORD flags, DISPPARAMS *dp,
107 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
109 if(flags != DISPATCH_PROPERTYGET)
110 return jsdisp_call_value(constr, flags, dp, retv, ei, sp);
112 V_VT(retv) = VT_DISPATCH;
113 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(constr);
114 IDispatchEx_AddRef(_IDispatchEx_(constr));
115 return S_OK;
118 static HRESULT JSGlobal_NaN(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
119 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
121 TRACE("\n");
123 switch(flags) {
124 case DISPATCH_PROPERTYGET:
125 num_set_nan(retv);
126 break;
128 default:
129 FIXME("unimplemented flags %x\n", flags);
130 return E_NOTIMPL;
133 return S_OK;
136 static HRESULT JSGlobal_Infinity(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
137 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
139 TRACE("\n");
141 switch(flags) {
142 case DISPATCH_PROPERTYGET:
143 num_set_inf(retv, TRUE);
144 break;
146 default:
147 FIXME("unimplemented flags %x\n", flags);
148 return E_NOTIMPL;
151 return S_OK;
154 static HRESULT JSGlobal_Array(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
155 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
157 TRACE("\n");
159 return constructor_call(dispex->ctx->array_constr, flags, dp, retv, ei, sp);
162 static HRESULT JSGlobal_Boolean(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
163 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
165 TRACE("\n");
167 return constructor_call(dispex->ctx->bool_constr, flags, dp, retv, ei, sp);
170 static HRESULT JSGlobal_Date(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
171 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
173 TRACE("\n");
175 return constructor_call(dispex->ctx->date_constr, flags, dp, retv, ei, sp);
178 static HRESULT JSGlobal_Error(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
179 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
181 TRACE("\n");
183 return constructor_call(dispex->ctx->error_constr, flags, dp, retv, ei, sp);
186 static HRESULT JSGlobal_EvalError(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
187 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
189 TRACE("\n");
191 return constructor_call(dispex->ctx->eval_error_constr, flags, dp, retv, ei, sp);
194 static HRESULT JSGlobal_RangeError(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
195 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
197 TRACE("\n");
199 return constructor_call(dispex->ctx->range_error_constr, flags, dp, retv, ei, sp);
202 static HRESULT JSGlobal_ReferenceError(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
203 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
205 TRACE("\n");
207 return constructor_call(dispex->ctx->reference_error_constr, flags, dp, retv, ei, sp);
210 static HRESULT JSGlobal_SyntaxError(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
211 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
213 TRACE("\n");
215 return constructor_call(dispex->ctx->syntax_error_constr, flags, dp, retv, ei, sp);
218 static HRESULT JSGlobal_TypeError(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
219 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
221 TRACE("\n");
223 return constructor_call(dispex->ctx->type_error_constr, flags, dp, retv, ei, sp);
226 static HRESULT JSGlobal_URIError(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
227 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
229 TRACE("\n");
231 return constructor_call(dispex->ctx->uri_error_constr, flags, dp, retv, ei, sp);
234 static HRESULT JSGlobal_Function(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
235 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
237 TRACE("\n");
239 return constructor_call(dispex->ctx->function_constr, flags, dp, retv, ei, sp);
242 static HRESULT JSGlobal_Number(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
243 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
245 TRACE("\n");
247 return constructor_call(dispex->ctx->number_constr, flags, dp, retv, ei, sp);
250 static HRESULT JSGlobal_Object(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
251 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
253 TRACE("\n");
255 return constructor_call(dispex->ctx->object_constr, flags, dp, retv, ei, sp);
258 static HRESULT JSGlobal_String(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
259 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
261 TRACE("\n");
263 return constructor_call(dispex->ctx->string_constr, flags, dp, retv, ei, sp);
266 static HRESULT JSGlobal_RegExp(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
267 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
269 TRACE("\n");
271 return constructor_call(dispex->ctx->regexp_constr, flags, dp, retv, ei, sp);
274 static HRESULT JSGlobal_ActiveXObject(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
275 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
277 FIXME("\n");
278 return E_NOTIMPL;
281 static HRESULT JSGlobal_VBArray(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
282 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
284 FIXME("\n");
285 return E_NOTIMPL;
288 static HRESULT JSGlobal_Enumerator(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
289 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
291 FIXME("\n");
292 return E_NOTIMPL;
295 static HRESULT JSGlobal_escape(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
296 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
298 FIXME("\n");
299 return E_NOTIMPL;
302 /* ECMA-262 3rd Edition 15.1.2.1 */
303 static HRESULT JSGlobal_eval(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
304 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
306 parser_ctx_t *parser_ctx;
307 VARIANT *arg;
308 HRESULT hres;
310 TRACE("\n");
312 if(!arg_cnt(dp)) {
313 if(retv)
314 V_VT(retv) = VT_EMPTY;
315 return S_OK;
318 arg = get_arg(dp, 0);
319 if(V_VT(arg) != VT_BSTR) {
320 if(retv) {
321 V_VT(retv) = VT_EMPTY;
322 return VariantCopy(retv, arg);
324 return S_OK;
327 if(!dispex->ctx->exec_ctx) {
328 FIXME("No active exec_ctx\n");
329 return E_UNEXPECTED;
332 TRACE("parsing %s\n", debugstr_w(V_BSTR(arg)));
333 hres = script_parse(dispex->ctx, V_BSTR(arg), NULL, &parser_ctx);
334 if(FAILED(hres)) {
335 WARN("parse (%s) failed: %08x\n", debugstr_w(V_BSTR(arg)), hres);
336 return throw_syntax_error(dispex->ctx, ei, hres, NULL);
339 hres = exec_source(dispex->ctx->exec_ctx, parser_ctx, parser_ctx->source, ei, retv);
340 parser_release(parser_ctx);
342 return hres;
345 static HRESULT JSGlobal_isNaN(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
346 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
348 VARIANT_BOOL ret = VARIANT_FALSE;
349 VARIANT num;
350 HRESULT hres;
352 TRACE("\n");
354 if(arg_cnt(dp)) {
355 hres = to_number(dispex->ctx, get_arg(dp,0), ei, &num);
356 if(FAILED(hres))
357 return hres;
359 if(V_VT(&num) == VT_R8 && isnan(V_R8(&num)))
360 ret = VARIANT_TRUE;
361 }else {
362 ret = VARIANT_TRUE;
365 if(retv) {
366 V_VT(retv) = VT_BOOL;
367 V_BOOL(retv) = ret;
369 return S_OK;
372 static HRESULT JSGlobal_isFinite(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
373 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
375 VARIANT_BOOL ret = VARIANT_FALSE;
376 HRESULT hres;
378 TRACE("\n");
380 if(arg_cnt(dp)) {
381 VARIANT num;
383 hres = to_number(dispex->ctx, get_arg(dp,0), ei, &num);
384 if(FAILED(hres))
385 return hres;
387 if(V_VT(&num) != VT_R8 || (!isinf(V_R8(&num)) && !isnan(V_R8(&num))))
388 ret = VARIANT_TRUE;
391 if(retv) {
392 V_VT(retv) = VT_BOOL;
393 V_BOOL(retv) = ret;
395 return S_OK;
398 static INT char_to_int(WCHAR c)
400 if('0' <= c && c <= '9')
401 return c - '0';
402 if('a' <= c && c <= 'z')
403 return c - 'a' + 10;
404 if('A' <= c && c <= 'Z')
405 return c - 'A' + 10;
406 return 100;
409 static HRESULT JSGlobal_parseInt(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
410 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
412 DOUBLE ret = 0.0;
413 INT radix=10, i;
414 WCHAR *ptr;
415 BOOL neg = FALSE;
416 BSTR str;
417 HRESULT hres;
419 if(!arg_cnt(dp)) {
420 if(retv) num_set_nan(retv);
421 return S_OK;
424 if(arg_cnt(dp) >= 2) {
425 hres = to_int32(dispex->ctx, get_arg(dp, 1), ei, &radix);
426 if(FAILED(hres))
427 return hres;
429 if(!radix) {
430 radix = 10;
431 }else if(radix < 2 || radix > 36) {
432 WARN("radix %d out of range\n", radix);
433 return E_FAIL;
437 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
438 if(FAILED(hres))
439 return hres;
441 for(ptr = str; isspaceW(*ptr); ptr++);
443 switch(*ptr) {
444 case '+':
445 ptr++;
446 break;
447 case '-':
448 neg = TRUE;
449 ptr++;
450 break;
451 case '0':
452 ptr++;
453 if(*ptr == 'x' || *ptr == 'X') {
454 radix = 16;
455 ptr++;
459 while(*ptr) {
460 i = char_to_int(*ptr++);
461 if(i > radix)
462 break;
464 ret = ret*radix + i;
467 SysFreeString(str);
469 if(neg)
470 ret = -ret;
472 if(retv)
473 num_set_val(retv, ret);
474 return S_OK;
477 static HRESULT JSGlobal_parseFloat(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
478 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
480 LONGLONG d = 0, hlp;
481 int exp = 0, length;
482 VARIANT *arg;
483 WCHAR *str;
484 BSTR val_str = NULL;
485 BOOL ret_nan = TRUE, positive = TRUE;
486 HRESULT hres;
488 if(!arg_cnt(dp)) {
489 if(retv)
490 num_set_nan(retv);
491 return S_OK;
494 arg = get_arg(dp, 0);
495 hres = to_string(dispex->ctx, arg, ei, &val_str);
496 if(FAILED(hres))
497 return hres;
499 str = val_str;
500 length = SysStringLen(val_str);
502 while(isspaceW(*str)) str++;
504 if(*str == '+')
505 str++;
506 else if(*str == '-') {
507 positive = FALSE;
508 str++;
511 if(isdigitW(*str))
512 ret_nan = FALSE;
514 while(isdigitW(*str)) {
515 hlp = d*10 + *(str++) - '0';
516 if(d>LONGLONG_MAX/10 || hlp<0) {
517 exp++;
518 break;
520 else
521 d = hlp;
523 while(isdigitW(*str)) {
524 exp++;
525 str++;
528 if(*str == '.') str++;
530 if(isdigitW(*str))
531 ret_nan = FALSE;
533 while(isdigitW(*str)) {
534 hlp = d*10 + *(str++) - '0';
535 if(d>LONGLONG_MAX/10 || hlp<0)
536 break;
538 d = hlp;
539 exp--;
541 while(isdigitW(*str))
542 str++;
544 if(*str && !ret_nan && (*str=='e' || *str=='E')) {
545 int sign = 1, e = 0;
547 str++;
548 if(*str == '+')
549 str++;
550 else if(*str == '-') {
551 sign = -1;
552 str++;
555 while(isdigitW(*str)) {
556 if(e>INT_MAX/10 || (e = e*10 + *str++ - '0')<0)
557 e = INT_MAX;
559 e *= sign;
561 if(exp<0 && e<0 && exp+e>0) exp = INT_MIN;
562 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
563 else exp += e;
566 SysFreeString(val_str);
568 if(ret_nan) {
569 if(retv)
570 num_set_nan(retv);
571 return S_OK;
574 V_VT(retv) = VT_R8;
575 V_R8(retv) = (double)(positive?d:-d)*pow(10, exp);
576 return S_OK;
579 static inline int hex_to_int(const WCHAR wch) {
580 if(toupperW(wch)>='A' && toupperW(wch)<='F') return toupperW(wch)-'A'+10;
581 if(isdigitW(wch)) return wch-'0';
582 return -1;
585 static HRESULT JSGlobal_unescape(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
586 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
588 BSTR ret, str;
589 const WCHAR *ptr;
590 DWORD len = 0;
591 HRESULT hres;
593 TRACE("\n");
595 if(!arg_cnt(dp)) {
596 if(retv) {
597 ret = SysAllocString(undefinedW);
598 if(!ret)
599 return E_OUTOFMEMORY;
601 V_VT(retv) = VT_BSTR;
602 V_BSTR(retv) = ret;
605 return S_OK;
608 hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str);
609 if(FAILED(hres))
610 return hres;
612 for(ptr=str; *ptr; ptr++) {
613 if(*ptr == '%') {
614 if(hex_to_int(*(ptr+1))!=-1 && hex_to_int(*(ptr+2))!=-1)
615 ptr += 2;
616 else if(*(ptr+1)=='u' && hex_to_int(*(ptr+2))!=-1 && hex_to_int(*(ptr+3))!=-1
617 && hex_to_int(*(ptr+4))!=-1 && hex_to_int(*(ptr+5))!=-1)
618 ptr += 5;
621 len++;
624 ret = SysAllocStringLen(NULL, len);
625 if(!ret)
626 return E_OUTOFMEMORY;
628 len = 0;
629 for(ptr=str; *ptr; ptr++) {
630 if(*ptr == '%') {
631 if(hex_to_int(*(ptr+1))!=-1 && hex_to_int(*(ptr+2))!=-1) {
632 ret[len] = (hex_to_int(*(ptr+1))<<4) + hex_to_int(*(ptr+2));
633 ptr += 2;
635 else if(*(ptr+1)=='u' && hex_to_int(*(ptr+2))!=-1 && hex_to_int(*(ptr+3))!=-1
636 && hex_to_int(*(ptr+4))!=-1 && hex_to_int(*(ptr+5))!=-1) {
637 ret[len] = (hex_to_int(*(ptr+2))<<12) + (hex_to_int(*(ptr+3))<<8)
638 + (hex_to_int(*(ptr+4))<<4) + hex_to_int(*(ptr+5));
639 ptr += 5;
641 else
642 ret[len] = *ptr;
644 else
645 ret[len] = *ptr;
647 len++;
650 if(retv) {
651 V_VT(retv) = VT_BSTR;
652 V_BSTR(retv) = ret;
654 else
655 SysFreeString(ret);
657 return S_OK;
660 static HRESULT JSGlobal_GetObject(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
661 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
663 FIXME("\n");
664 return E_NOTIMPL;
667 static HRESULT JSGlobal_ScriptEngine(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
668 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
670 FIXME("\n");
671 return E_NOTIMPL;
674 static HRESULT JSGlobal_ScriptEngineMajorVersion(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
675 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
677 FIXME("\n");
678 return E_NOTIMPL;
681 static HRESULT JSGlobal_ScriptEngineMinorVersion(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
682 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
684 FIXME("\n");
685 return E_NOTIMPL;
688 static HRESULT JSGlobal_ScriptEngineBuildVersion(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
689 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
691 FIXME("\n");
692 return E_NOTIMPL;
695 static HRESULT JSGlobal_CollectGarbage(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
696 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
698 FIXME("\n");
699 return E_NOTIMPL;
702 static HRESULT JSGlobal_encodeURI(DispatchEx *dispex, WORD flags, DISPPARAMS *dp,
703 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
705 const WCHAR *ptr;
706 DWORD len = 0, i;
707 char buf[4];
708 BSTR str, ret;
709 WCHAR *rptr;
710 HRESULT hres;
712 TRACE("\n");
714 if(!arg_cnt(dp)) {
715 if(retv) {
716 ret = SysAllocString(undefinedW);
717 if(!ret)
718 return E_OUTOFMEMORY;
720 V_VT(retv) = VT_BSTR;
721 V_BSTR(retv) = ret;
724 return S_OK;
727 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &str);
728 if(FAILED(hres))
729 return hres;
731 for(ptr = str; *ptr; ptr++) {
732 if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
733 len++;
734 }else {
735 i = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, NULL, 0, NULL, NULL)*3;
736 if(!i) {
737 FIXME("throw URIError\n");
738 return E_FAIL;
741 len += i;
745 rptr = ret = SysAllocStringLen(NULL, len);
746 if(!ret)
747 return E_OUTOFMEMORY;
749 for(ptr = str; *ptr; ptr++) {
750 if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
751 *rptr++ = *ptr;
752 }else {
753 len = WideCharToMultiByte(CP_UTF8, 0, ptr, 1, buf, sizeof(buf), NULL, NULL);
754 for(i=0; i<len; i++) {
755 *rptr++ = '%';
756 *rptr++ = int_to_char((BYTE)buf[i] >> 4);
757 *rptr++ = int_to_char(buf[i] & 0x0f);
762 TRACE("%s -> %s\n", debugstr_w(str), debugstr_w(ret));
763 if(retv) {
764 V_VT(retv) = VT_BSTR;
765 V_BSTR(retv) = ret;
766 }else {
767 SysFreeString(ret);
769 return S_OK;
772 static const builtin_prop_t JSGlobal_props[] = {
773 {ActiveXObjectW, JSGlobal_ActiveXObject, PROPF_METHOD},
774 {ArrayW, JSGlobal_Array, PROPF_CONSTR},
775 {BooleanW, JSGlobal_Boolean, PROPF_CONSTR},
776 {CollectGarbageW, JSGlobal_CollectGarbage, PROPF_METHOD},
777 {DateW, JSGlobal_Date, PROPF_CONSTR},
778 {EnumeratorW, JSGlobal_Enumerator, PROPF_METHOD},
779 {ErrorW, JSGlobal_Error, PROPF_CONSTR},
780 {EvalErrorW, JSGlobal_EvalError, PROPF_CONSTR},
781 {FunctionW, JSGlobal_Function, PROPF_CONSTR},
782 {_GetObjectW, JSGlobal_GetObject, PROPF_METHOD},
783 {InfinityW, JSGlobal_Infinity, 0},
784 /* {MathW, JSGlobal_Math, 0}, */
785 {NaNW, JSGlobal_NaN, 0},
786 {NumberW, JSGlobal_Number, PROPF_CONSTR},
787 {ObjectW, JSGlobal_Object, PROPF_CONSTR},
788 {RangeErrorW, JSGlobal_RangeError, PROPF_CONSTR},
789 {ReferenceErrorW, JSGlobal_ReferenceError, PROPF_CONSTR},
790 {RegExpW, JSGlobal_RegExp, PROPF_CONSTR},
791 {ScriptEngineW, JSGlobal_ScriptEngine, PROPF_METHOD},
792 {ScriptEngineBuildVersionW, JSGlobal_ScriptEngineBuildVersion, PROPF_METHOD},
793 {ScriptEngineMajorVersionW, JSGlobal_ScriptEngineMajorVersion, PROPF_METHOD},
794 {ScriptEngineMinorVersionW, JSGlobal_ScriptEngineMinorVersion, PROPF_METHOD},
795 {StringW, JSGlobal_String, PROPF_CONSTR},
796 {SyntaxErrorW, JSGlobal_SyntaxError, PROPF_CONSTR},
797 {TypeErrorW, JSGlobal_TypeError, PROPF_CONSTR},
798 {URIErrorW, JSGlobal_URIError, PROPF_CONSTR},
799 {VBArrayW, JSGlobal_VBArray, PROPF_METHOD},
800 {encodeURIW, JSGlobal_encodeURI, PROPF_METHOD},
801 {escapeW, JSGlobal_escape, PROPF_METHOD},
802 {evalW, JSGlobal_eval, PROPF_METHOD|1},
803 {isFiniteW, JSGlobal_isFinite, PROPF_METHOD},
804 {isNaNW, JSGlobal_isNaN, PROPF_METHOD},
805 {parseFloatW, JSGlobal_parseFloat, PROPF_METHOD},
806 {parseIntW, JSGlobal_parseInt, PROPF_METHOD|2},
807 {unescapeW, JSGlobal_unescape, PROPF_METHOD}
810 static const builtin_info_t JSGlobal_info = {
811 JSCLASS_GLOBAL,
812 {NULL, NULL, 0},
813 sizeof(JSGlobal_props)/sizeof(*JSGlobal_props),
814 JSGlobal_props,
815 NULL,
816 NULL
819 static HRESULT init_constructors(script_ctx_t *ctx, DispatchEx *object_prototype)
821 HRESULT hres;
823 hres = init_function_constr(ctx, object_prototype);
824 if(FAILED(hres))
825 return hres;
827 hres = create_object_constr(ctx, object_prototype, &ctx->object_constr);
828 if(FAILED(hres))
829 return hres;
831 hres = create_array_constr(ctx, object_prototype, &ctx->array_constr);
832 if(FAILED(hres))
833 return hres;
835 hres = create_bool_constr(ctx, object_prototype, &ctx->bool_constr);
836 if(FAILED(hres))
837 return hres;
839 hres = create_date_constr(ctx, object_prototype, &ctx->date_constr);
840 if(FAILED(hres))
841 return hres;
843 hres = init_error_constr(ctx, object_prototype);
844 if(FAILED(hres))
845 return hres;
847 hres = create_number_constr(ctx, object_prototype, &ctx->number_constr);
848 if(FAILED(hres))
849 return hres;
851 hres = create_regexp_constr(ctx, object_prototype, &ctx->regexp_constr);
852 if(FAILED(hres))
853 return hres;
855 hres = create_string_constr(ctx, object_prototype, &ctx->string_constr);
856 if(FAILED(hres))
857 return hres;
859 return S_OK;
862 HRESULT init_global(script_ctx_t *ctx)
864 DispatchEx *math, *object_prototype;
865 VARIANT var;
866 HRESULT hres;
868 if(ctx->global)
869 return S_OK;
871 hres = create_object_prototype(ctx, &object_prototype);
872 if(FAILED(hres))
873 return hres;
875 hres = init_constructors(ctx, object_prototype);
876 jsdisp_release(object_prototype);
877 if(FAILED(hres))
878 return hres;
880 hres = create_dispex(ctx, &JSGlobal_info, NULL, &ctx->global);
881 if(FAILED(hres))
882 return hres;
884 hres = create_math(ctx, &math);
885 if(FAILED(hres))
886 return hres;
888 V_VT(&var) = VT_DISPATCH;
889 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(math);
890 hres = jsdisp_propput_name(ctx->global, MathW, &var, NULL/*FIXME*/, NULL/*FIXME*/);
891 jsdisp_release(math);
893 return hres;