global.c 32.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2008 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

19 20

#include <math.h>
21
#include <limits.h>
22

23
#include "jscript.h"
24
#include "engine.h"
25 26 27 28 29 30 31 32 33 34

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

static const WCHAR NaNW[] = {'N','a','N',0};
static const WCHAR InfinityW[] = {'I','n','f','i','n','i','t','y',0};
static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
static const WCHAR BooleanW[] = {'B','o','o','l','e','a','n',0};
static const WCHAR DateW[] = {'D','a','t','e',0};
35 36 37 38 39 40 41
static const WCHAR ErrorW[] = {'E','r','r','o','r',0};
static const WCHAR EvalErrorW[] = {'E','v','a','l','E','r','r','o','r',0};
static const WCHAR RangeErrorW[] = {'R','a','n','g','e','E','r','r','o','r',0};
static const WCHAR ReferenceErrorW[] = {'R','e','f','e','r','e','n','c','e','E','r','r','o','r',0};
static const WCHAR SyntaxErrorW[] = {'S','y','n','t','a','x','E','r','r','o','r',0};
static const WCHAR TypeErrorW[] = {'T','y','p','e','E','r','r','o','r',0};
static const WCHAR URIErrorW[] = {'U','R','I','E','r','r','o','r',0};
42 43 44 45 46
static const WCHAR FunctionW[] = {'F','u','n','c','t','i','o','n',0};
static const WCHAR NumberW[] = {'N','u','m','b','e','r',0};
static const WCHAR ObjectW[] = {'O','b','j','e','c','t',0};
static const WCHAR StringW[] = {'S','t','r','i','n','g',0};
static const WCHAR RegExpW[] = {'R','e','g','E','x','p',0};
47
static const WCHAR RegExpErrorW[] = {'R','e','g','E','x','p','E','r','r','o','r',0};
48 49 50 51 52 53 54 55 56 57
static const WCHAR ActiveXObjectW[] = {'A','c','t','i','v','e','X','O','b','j','e','c','t',0};
static const WCHAR VBArrayW[] = {'V','B','A','r','r','a','y',0};
static const WCHAR EnumeratorW[] = {'E','n','u','m','e','r','a','t','o','r',0};
static const WCHAR escapeW[] = {'e','s','c','a','p','e',0};
static const WCHAR evalW[] = {'e','v','a','l',0};
static const WCHAR isNaNW[] = {'i','s','N','a','N',0};
static const WCHAR isFiniteW[] = {'i','s','F','i','n','i','t','e',0};
static const WCHAR parseIntW[] = {'p','a','r','s','e','I','n','t',0};
static const WCHAR parseFloatW[] = {'p','a','r','s','e','F','l','o','a','t',0};
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 60 61 62 63 64 65 66 67
static const WCHAR ScriptEngineW[] = {'S','c','r','i','p','t','E','n','g','i','n','e',0};
static const WCHAR ScriptEngineMajorVersionW[] =
    {'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};
static const WCHAR ScriptEngineMinorVersionW[] =
    {'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};
static const WCHAR ScriptEngineBuildVersionW[] =
    {'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};
static const WCHAR CollectGarbageW[] = {'C','o','l','l','e','c','t','G','a','r','b','a','g','e',0};
static const WCHAR MathW[] = {'M','a','t','h',0};
68
static const WCHAR JSONW[] = {'J','S','O','N',0};
69
static const WCHAR encodeURIW[] = {'e','n','c','o','d','e','U','R','I',0};
70 71 72
static const WCHAR decodeURIW[] = {'d','e','c','o','d','e','U','R','I',0};
static const WCHAR encodeURIComponentW[] = {'e','n','c','o','d','e','U','R','I','C','o','m','p','o','n','e','n','t',0};
static const WCHAR decodeURIComponentW[] = {'d','e','c','o','d','e','U','R','I','C','o','m','p','o','n','e','n','t',0};
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};

static int uri_char_table[] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 00-0f */
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 10-1f */
    0,2,0,0,1,0,1,2,2,2,2,1,1,2,2,1, /* 20-2f */
    2,2,2,2,2,2,2,2,2,2,1,1,0,1,0,1, /* 30-3f */
    1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 40-4f */
    2,2,2,2,2,2,2,2,2,2,2,0,0,0,0,2, /* 50-5f */
    0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 60-6f */
    2,2,2,2,2,2,2,2,2,2,2,0,0,0,2,0, /* 70-7f */
};

/* 1 - reserved */
/* 2 - unescaped */

static inline BOOL is_uri_reserved(WCHAR c)
{
    return c < 128 && uri_char_table[c] == 1;
}

static inline BOOL is_uri_unescaped(WCHAR c)
{
    return c < 128 && uri_char_table[c] == 2;
}

100
/* Check that the character is one of the 69 non-blank characters as defined by ECMA-262 B.2.1 */
101 102 103 104 105 106
static inline BOOL is_ecma_nonblank(const WCHAR c)
{
    return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
        c == '@' || c == '*' || c == '_' || c == '+' || c == '-' || c == '.' || c == '/');
}

107 108 109 110 111 112
static WCHAR int_to_char(int i)
{
    if(i < 10)
        return '0'+i;
    return 'A'+i-10;
}
113

114
static HRESULT JSGlobal_escape(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
115
        jsval_t *r)
116
{
117
    jsstr_t *ret_str, *str;
118
    const WCHAR *ptr, *buf;
119
    DWORD len = 0;
120
    WCHAR *ret;
121 122 123 124
    HRESULT hres;

    TRACE("\n");

125
    if(!argc) {
126 127
        if(r)
            *r = jsval_string(jsstr_undefined());
128 129 130
        return S_OK;
    }

131
    hres = to_flat_string(ctx, argv[0], &str, &buf);
132 133 134
    if(FAILED(hres))
        return hres;

135
    for(ptr = buf; *ptr; ptr++) {
136 137
        if(*ptr > 0xff)
            len += 6;
138
        else if(is_ecma_nonblank(*ptr))
139
            len++;
140 141
        else
            len += 3;
142 143
    }

144 145
    ret_str = jsstr_alloc_buf(len, &ret);
    if(!ret_str) {
146
        jsstr_release(str);
147
        return E_OUTOFMEMORY;
148
    }
149 150

    len = 0;
151
    for(ptr = buf; *ptr; ptr++) {
152 153 154 155 156 157 158 159
        if(*ptr > 0xff) {
            ret[len++] = '%';
            ret[len++] = 'u';
            ret[len++] = int_to_char(*ptr >> 12);
            ret[len++] = int_to_char((*ptr >> 8) & 0xf);
            ret[len++] = int_to_char((*ptr >> 4) & 0xf);
            ret[len++] = int_to_char(*ptr & 0xf);
        }
160
        else if(is_ecma_nonblank(*ptr))
161 162
            ret[len++] = *ptr;
        else {
163 164 165
            ret[len++] = '%';
            ret[len++] = int_to_char(*ptr >> 4);
            ret[len++] = int_to_char(*ptr & 0xf);
166 167 168
        }
    }

169
    jsstr_release(str);
170

171
    if(r)
172
        *r = jsval_string(ret_str);
173
    else
174
        jsstr_release(ret_str);
175
    return S_OK;
176 177
}

178
/* ECMA-262 3rd Edition    15.1.2.1 */
179
HRESULT JSGlobal_eval(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
180
        jsval_t *r)
181
{
182
    call_frame_t *frame = ctx->call_ctx;
183
    DWORD exec_flags = EXEC_EVAL;
184
    bytecode_t *code;
185
    const WCHAR *src;
186 187 188 189
    HRESULT hres;

    TRACE("\n");

190
    if(!argc) {
191 192
        if(r)
            *r = jsval_undefined();
193 194 195
        return S_OK;
    }

196
    if(!is_string(argv[0])) {
197
        if(r)
198
            return jsval_copy(argv[0], r);
199 200 201
        return S_OK;
    }

202 203 204 205
    src = jsstr_flatten(get_string(argv[0]));
    if(!src)
        return E_OUTOFMEMORY;

206
    TRACE("parsing %s\n", debugstr_jsval(argv[0]));
207
    hres = compile_script(ctx, src, 0, 0, NULL, NULL, TRUE, FALSE, &code);
208
    if(FAILED(hres)) {
209
        WARN("parse (%s) failed: %08x\n", debugstr_jsval(argv[0]), hres);
210
        return hres;
211 212
    }

213
    if(!frame || (frame->flags & EXEC_GLOBAL))
214
        exec_flags |= EXEC_GLOBAL;
215 216
    if(flags & DISPATCH_JSCRIPT_CALLEREXECSSOURCE)
        exec_flags |= EXEC_RETURN_TO_INTERP;
217 218
    hres = exec_source(ctx, exec_flags, code, &code->global_code, frame ? frame->scope : NULL,
            frame ? frame->this_obj : NULL, NULL, frame ? frame->variable_obj : ctx->global, 0, NULL, r);
219
    release_bytecode(code);
220
    return hres;
221 222
}

223
static HRESULT JSGlobal_isNaN(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
224
        jsval_t *r)
225
{
226
    BOOL ret = TRUE;
227
    double n;
228 229 230 231
    HRESULT hres;

    TRACE("\n");

232
    if(argc) {
233
        hres = to_number(ctx, argv[0], &n);
234 235 236
        if(FAILED(hres))
            return hres;

237
        if(!isnan(n))
238
            ret = FALSE;
239 240
    }

241 242
    if(r)
        *r = jsval_bool(ret);
243
    return S_OK;
244 245
}

246
static HRESULT JSGlobal_isFinite(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
247
        jsval_t *r)
248
{
249
    BOOL ret = FALSE;
250 251 252 253
    HRESULT hres;

    TRACE("\n");

254
    if(argc) {
255
        double n;
256

257
        hres = to_number(ctx, argv[0], &n);
258 259 260
        if(FAILED(hres))
            return hres;

261
        ret = is_finite(n);
262 263
    }

264 265
    if(r)
        *r = jsval_bool(ret);
266
    return S_OK;
267 268
}

269 270 271 272 273 274 275 276 277 278 279
static INT char_to_int(WCHAR c)
{
    if('0' <= c && c <= '9')
        return c - '0';
    if('a' <= c && c <= 'z')
        return c - 'a' + 10;
    if('A' <= c && c <= 'Z')
        return c - 'A' + 10;
    return 100;
}

280
static HRESULT JSGlobal_parseInt(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
281
        jsval_t *r)
282
{
283
    BOOL neg = FALSE, empty = TRUE;
284
    const WCHAR *ptr;
285
    DOUBLE ret = 0.0;
286
    INT radix=0, i;
287
    jsstr_t *str;
288 289
    HRESULT hres;

290
    if(!argc) {
291 292
        if(r)
            *r = jsval_number(NAN);
293
        return S_OK;
294 295
    }

296
    if(argc >= 2) {
297
        hres = to_int32(ctx, argv[1], &radix);
298 299 300
        if(FAILED(hres))
            return hres;

301
        if(radix && (radix < 2 || radix > 36)) {
302
            WARN("radix %d out of range\n", radix);
303 304
            if(r)
                *r = jsval_number(NAN);
305
            return S_OK;
306 307 308
        }
    }

309
    hres = to_flat_string(ctx, argv[0], &str, &ptr);
310 311 312
    if(FAILED(hres))
        return hres;

313
    while(iswspace(*ptr))
314
        ptr++;
315 316 317 318 319 320 321 322 323 324 325

    switch(*ptr) {
    case '+':
        ptr++;
        break;
    case '-':
        neg = TRUE;
        ptr++;
        break;
    }

326 327 328 329 330 331 332 333 334 335 336 337 338
    if(!radix) {
        if(*ptr == '0') {
            if(ptr[1] == 'x' || ptr[1] == 'X') {
                radix = 16;
                ptr += 2;
            }else {
                radix = 8;
                ptr++;
                empty = FALSE;
            }
        }else {
            radix = 10;
        }
339 340
    }else if(radix == 16 && *ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X')) {
        ptr += 2;
341
    }
342

343 344 345 346 347 348 349
    i = char_to_int(*ptr++);
    if(i < radix) {
        do {
            ret = ret*radix + i;
            i = char_to_int(*ptr++);
        }while(i < radix);
    }else if(empty) {
350
        ret = NAN;
351 352
    }

353
    jsstr_release(str);
354 355 356 357

    if(neg)
        ret = -ret;

358 359
    if(r)
        *r = jsval_number(ret);
360
    return S_OK;
361 362
}

363
static HRESULT JSGlobal_parseFloat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
364
        jsval_t *r)
365
{
366
    LONGLONG d = 0, hlp;
367
    jsstr_t *val_str;
368
    int exp = 0;
369
    const WCHAR *str;
370 371 372
    BOOL ret_nan = TRUE, positive = TRUE;
    HRESULT hres;

373
    if(!argc) {
374 375
        if(r)
            *r = jsval_number(NAN);
376 377 378
        return S_OK;
    }

379 380
    TRACE("%s\n", debugstr_jsval(argv[0]));

381
    hres = to_flat_string(ctx, argv[0], &val_str, &str);
382 383 384
    if(FAILED(hres))
        return hres;

385
    while(iswspace(*str)) str++;
386 387 388 389 390 391 392 393

    if(*str == '+')
        str++;
    else if(*str == '-') {
        positive = FALSE;
        str++;
    }

394
    if(is_digit(*str))
395 396
        ret_nan = FALSE;

397
    while(is_digit(*str)) {
398
        hlp = d*10 + *(str++) - '0';
399
        if(d>MAXLONGLONG/10 || hlp<0) {
400 401 402 403 404 405
            exp++;
            break;
        }
        else
            d = hlp;
    }
406
    while(is_digit(*str)) {
407 408 409 410 411 412
        exp++;
        str++;
    }

    if(*str == '.') str++;

413
    if(is_digit(*str))
414 415
        ret_nan = FALSE;

416
    while(is_digit(*str)) {
417
        hlp = d*10 + *(str++) - '0';
418
        if(d>MAXLONGLONG/10 || hlp<0)
419 420 421 422 423
            break;

        d = hlp;
        exp--;
    }
424
    while(is_digit(*str))
425 426 427 428 429 430 431 432 433 434 435 436 437
        str++;

    if(*str && !ret_nan && (*str=='e' || *str=='E')) {
        int sign = 1, e = 0;

        str++;
        if(*str == '+')
            str++;
        else if(*str == '-') {
            sign = -1;
            str++;
        }

438
        while(is_digit(*str)) {
439 440 441 442 443 444 445 446 447 448
            if(e>INT_MAX/10 || (e = e*10 + *str++ - '0')<0)
                e = INT_MAX;
        }
        e *= sign;

        if(exp<0 && e<0 && exp+e>0) exp = INT_MIN;
        else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
        else exp += e;
    }

449
    jsstr_release(val_str);
450 451

    if(ret_nan) {
452 453
        if(r)
            *r = jsval_number(NAN);
454 455 456
        return S_OK;
    }

457 458
    if(!positive)
        d = -d;
459 460
    if(r)
        *r = jsval_number(exp>0 ? d*pow(10, exp) : d/pow(10, -exp));
461
    return S_OK;
462 463
}

464
static inline int hex_to_int(const WCHAR wch) {
465
    if(towupper(wch)>='A' && towupper(wch)<='F') return towupper(wch)-'A'+10;
466
    if(is_digit(wch)) return wch-'0';
467 468 469
    return -1;
}

470
static HRESULT JSGlobal_unescape(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
471
        jsval_t *r)
472
{
473
    jsstr_t *ret_str, *str;
474
    const WCHAR *ptr, *buf;
475
    DWORD len = 0;
476
    WCHAR *ret;
477 478 479 480
    HRESULT hres;

    TRACE("\n");

481
    if(!argc) {
482 483
        if(r)
            *r = jsval_string(jsstr_undefined());
484 485 486
        return S_OK;
    }

487
    hres = to_flat_string(ctx, argv[0], &str, &buf);
488 489 490
    if(FAILED(hres))
        return hres;

491
    for(ptr = buf; *ptr; ptr++) {
492 493 494 495 496 497 498 499 500 501 502
        if(*ptr == '%') {
            if(hex_to_int(*(ptr+1))!=-1 && hex_to_int(*(ptr+2))!=-1)
                ptr += 2;
            else if(*(ptr+1)=='u' && hex_to_int(*(ptr+2))!=-1 && hex_to_int(*(ptr+3))!=-1
                    && hex_to_int(*(ptr+4))!=-1 && hex_to_int(*(ptr+5))!=-1)
                ptr += 5;
        }

        len++;
    }

503 504
    ret_str = jsstr_alloc_buf(len, &ret);
    if(!ret_str) {
505
        jsstr_release(str);
506
        return E_OUTOFMEMORY;
507
    }
508 509

    len = 0;
510
    for(ptr = buf; *ptr; ptr++) {
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
        if(*ptr == '%') {
            if(hex_to_int(*(ptr+1))!=-1 && hex_to_int(*(ptr+2))!=-1) {
                ret[len] = (hex_to_int(*(ptr+1))<<4) + hex_to_int(*(ptr+2));
                ptr += 2;
            }
            else if(*(ptr+1)=='u' && hex_to_int(*(ptr+2))!=-1 && hex_to_int(*(ptr+3))!=-1
                    && hex_to_int(*(ptr+4))!=-1 && hex_to_int(*(ptr+5))!=-1) {
                ret[len] = (hex_to_int(*(ptr+2))<<12) + (hex_to_int(*(ptr+3))<<8)
                    + (hex_to_int(*(ptr+4))<<4) + hex_to_int(*(ptr+5));
                ptr += 5;
            }
            else
                ret[len] = *ptr;
        }
        else
            ret[len] = *ptr;

        len++;
    }

531
    jsstr_release(str);
532

533
    if(r)
534
        *r = jsval_string(ret_str);
535
    else
536
        jsstr_release(ret_str);
537
    return S_OK;
538 539
}

540
static HRESULT JSGlobal_GetObject(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
541
        jsval_t *r)
542 543 544 545 546
{
    FIXME("\n");
    return E_NOTIMPL;
}

547
static HRESULT JSGlobal_ScriptEngine(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
548
        jsval_t *r)
549
{
550 551 552 553
    static const WCHAR JScriptW[] = {'J','S','c','r','i','p','t',0};

    TRACE("\n");

554
    if(r) {
555
        jsstr_t *ret;
556

557
        ret = jsstr_alloc(JScriptW);
558 559 560
        if(!ret)
            return E_OUTOFMEMORY;

561
        *r = jsval_string(ret);
562 563 564
    }

    return S_OK;
565 566
}

567
static HRESULT JSGlobal_ScriptEngineMajorVersion(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
568
        jsval_t *r)
569
{
570 571
    TRACE("\n");

572 573
    if(r)
        *r = jsval_number(JSCRIPT_MAJOR_VERSION);
574
    return S_OK;
575 576
}

577
static HRESULT JSGlobal_ScriptEngineMinorVersion(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
578
        jsval_t *r)
579
{
580 581
    TRACE("\n");

582 583
    if(r)
        *r = jsval_number(JSCRIPT_MINOR_VERSION);
584
    return S_OK;
585 586
}

587
static HRESULT JSGlobal_ScriptEngineBuildVersion(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
588
        jsval_t *r)
589
{
590 591
    TRACE("\n");

592 593
    if(r)
        *r = jsval_number(JSCRIPT_BUILD_VERSION);
594
    return S_OK;
595 596
}

597
static HRESULT JSGlobal_CollectGarbage(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
598
        jsval_t *r)
599
{
600 601 602 603
    static int once = 0;
    if (!once++)
        FIXME(": stub\n");
    return S_OK;
604 605
}

606
static HRESULT JSGlobal_encodeURI(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
607
        jsval_t *r)
608
{
609
    const WCHAR *ptr, *uri;
610
    jsstr_t *str, *ret;
611 612 613 614 615 616 617
    DWORD len = 0, i;
    char buf[4];
    WCHAR *rptr;
    HRESULT hres;

    TRACE("\n");

618
    if(!argc) {
619 620
        if(r)
            *r = jsval_string(jsstr_undefined());
621 622 623
        return S_OK;
    }

624
    hres = to_flat_string(ctx, argv[0], &str, &uri);
625 626 627
    if(FAILED(hres))
        return hres;

628
    for(ptr = uri; *ptr; ptr++) {
629 630 631
        if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
            len++;
        }else {
632
            i = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, ptr, 1, NULL, 0, NULL, NULL)*3;
633
            if(!i) {
634
                jsstr_release(str);
635
                return JS_E_INVALID_URI_CHAR;
636 637 638 639 640 641
            }

            len += i;
        }
    }

642 643
    ret = jsstr_alloc_buf(len, &rptr);
    if(!ret) {
644
        jsstr_release(str);
645
        return E_OUTOFMEMORY;
646
    }
647

648
    for(ptr = uri; *ptr; ptr++) {
649 650 651
        if(is_uri_unescaped(*ptr) || is_uri_reserved(*ptr) || *ptr == '#') {
            *rptr++ = *ptr;
        }else {
652
            len = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, ptr, 1, buf, sizeof(buf), NULL, NULL);
653 654 655 656 657 658 659 660
            for(i=0; i<len; i++) {
                *rptr++ = '%';
                *rptr++ = int_to_char((BYTE)buf[i] >> 4);
                *rptr++ = int_to_char(buf[i] & 0x0f);
            }
        }
    }

661 662
    TRACE("%s -> %s\n", debugstr_jsstr(str), debugstr_jsstr(ret));
    jsstr_release(str);
663

664 665 666
    if(r)
        *r = jsval_string(ret);
    else
667
        jsstr_release(ret);
668 669 670
    return S_OK;
}

671
static HRESULT JSGlobal_decodeURI(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
672
        jsval_t *r)
673
{
674
    const WCHAR *ptr, *uri;
675 676 677
    jsstr_t *str, *ret_str;
    unsigned len = 0;
    int i, val, res;
678
    WCHAR *ret;
679 680 681 682 683 684
    char buf[4];
    WCHAR out;
    HRESULT hres;

    TRACE("\n");

685
    if(!argc) {
686 687
        if(r)
            *r = jsval_string(jsstr_undefined());
688 689 690
        return S_OK;
    }

691
    hres = to_flat_string(ctx, argv[0], &str, &uri);
692 693 694
    if(FAILED(hres))
        return hres;

695
    for(ptr = uri; *ptr; ptr++) {
696 697 698 699 700 701 702 703 704 705
        if(*ptr != '%') {
            len++;
        }else {
            res = 0;
            for(i=0; i<4; i++) {
                if(ptr[i*3]!='%' || hex_to_int(ptr[i*3+1])==-1 || (val=hex_to_int(ptr[i*3+2]))==-1)
                    break;
                val += hex_to_int(ptr[i*3+1])<<4;
                buf[i] = val;

706
                res = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, buf, i+1, &out, 1);
707 708 709 710 711
                if(res)
                    break;
            }

            if(!res) {
712
                jsstr_release(str);
713
                return JS_E_INVALID_URI_CODING;
714 715 716 717 718 719 720
            }

            ptr += i*3+2;
            len++;
        }
    }

721 722
    ret_str = jsstr_alloc_buf(len, &ret);
    if(!ret_str) {
723
        jsstr_release(str);
724 725 726
        return E_OUTOFMEMORY;
    }

727
    for(ptr = uri; *ptr; ptr++) {
728
        if(*ptr != '%') {
729
            *ret++ = *ptr;
730 731 732 733 734 735 736
        }else {
            for(i=0; i<4; i++) {
                if(ptr[i*3]!='%' || hex_to_int(ptr[i*3+1])==-1 || (val=hex_to_int(ptr[i*3+2]))==-1)
                    break;
                val += hex_to_int(ptr[i*3+1])<<4;
                buf[i] = val;

737
                res = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, buf, i+1, ret, 1);
738 739 740 741 742
                if(res)
                    break;
            }

            ptr += i*3+2;
743
            ret++;
744 745 746
        }
    }

747
    TRACE("%s -> %s\n", debugstr_jsstr(str), debugstr_jsstr(ret_str));
748
    jsstr_release(str);
749

750
    if(r)
751
        *r = jsval_string(ret_str);
752
    else
753
        jsstr_release(ret_str);
754
    return S_OK;
755 756
}

757
static HRESULT JSGlobal_encodeURIComponent(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
758
        jsval_t *r)
759
{
760
    jsstr_t *str, *ret_str;
761
    char buf[4];
762
    const WCHAR *ptr, *uri;
763
    DWORD len = 0, size, i;
764
    WCHAR *ret;
765 766 767 768
    HRESULT hres;

    TRACE("\n");

769
    if(!argc) {
770 771
        if(r)
            *r = jsval_string(jsstr_undefined());
772 773 774
        return S_OK;
    }

775
    hres = to_flat_string(ctx, argv[0], &str, &uri);
776 777 778
    if(FAILED(hres))
        return hres;

779
    for(ptr = uri; *ptr; ptr++) {
780 781 782
        if(is_uri_unescaped(*ptr))
            len++;
        else {
783
            size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, ptr, 1, NULL, 0, NULL, NULL);
784
            if(!size) {
785
                jsstr_release(str);
786
                return JS_E_INVALID_URI_CHAR;
787 788 789 790 791
            }
            len += size*3;
        }
    }

792 793
    ret_str = jsstr_alloc_buf(len, &ret);
    if(!ret_str) {
794
        jsstr_release(str);
795 796 797
        return E_OUTOFMEMORY;
    }

798
    for(ptr = uri; *ptr; ptr++) {
799
        if(is_uri_unescaped(*ptr)) {
800
            *ret++ = *ptr;
801
        }else {
802
            size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, ptr, 1, buf, sizeof(buf), NULL, NULL);
803
            for(i=0; i<size; i++) {
804 805 806
                *ret++ = '%';
                *ret++ = int_to_char((BYTE)buf[i] >> 4);
                *ret++ = int_to_char(buf[i] & 0x0f);
807 808 809 810
            }
        }
    }

811
    jsstr_release(str);
812

813
    if(r)
814
        *r = jsval_string(ret_str);
815
    else
816
        jsstr_release(ret_str);
817
    return S_OK;
818 819
}

820
/* ECMA-262 3rd Edition    15.1.3.2 */
821
static HRESULT JSGlobal_decodeURIComponent(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
822
        jsval_t *r)
823
{
824
    const WCHAR *ptr, *uri;
825
    jsstr_t *str, *ret;
826 827 828 829 830 831
    WCHAR *out_ptr;
    DWORD len = 0;
    HRESULT hres;

    TRACE("\n");

832
    if(!argc) {
833 834
        if(r)
            *r = jsval_string(jsstr_undefined());
835 836 837
        return S_OK;
    }

838
    hres = to_flat_string(ctx, argv[0], &str, &uri);
839 840 841
    if(FAILED(hres))
        return hres;

842
    ptr = uri;
843 844 845 846 847 848 849
    while(*ptr) {
        if(*ptr == '%') {
            char octets[4];
            unsigned char mask = 0x80;
            int i, size, num_bytes = 0;
            if(hex_to_int(*(ptr+1)) < 0 || hex_to_int(*(ptr+2)) < 0) {
                FIXME("Throw URIError: Invalid hex sequence\n");
850
                jsstr_release(str);
851 852 853 854 855 856 857 858 859 860
                return E_FAIL;
            }
            octets[0] = (hex_to_int(*(ptr+1)) << 4) + hex_to_int(*(ptr+2));
            ptr += 3;
            while(octets[0] & mask) {
                mask = mask >> 1;
                ++num_bytes;
            }
            if(num_bytes == 1 || num_bytes > 4) {
                FIXME("Throw URIError: Invalid initial UTF character\n");
861
                jsstr_release(str);
862 863 864 865 866
                return E_FAIL;
            }
            for(i = 1; i < num_bytes; ++i) {
                if(*ptr != '%'){
                    FIXME("Throw URIError: Incomplete UTF sequence\n");
867
                    jsstr_release(str);
868 869 870 871
                    return E_FAIL;
                }
                if(hex_to_int(*(ptr+1)) < 0 || hex_to_int(*(ptr+2)) < 0) {
                    FIXME("Throw URIError: Invalid hex sequence\n");
872
                    jsstr_release(str);
873 874 875 876 877 878 879 880 881
                    return E_FAIL;
                }
                octets[i] = (hex_to_int(*(ptr+1)) << 4) + hex_to_int(*(ptr+2));
                ptr += 3;
            }
            size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, octets,
                    num_bytes ? num_bytes : 1, NULL, 0);
            if(size == 0) {
                FIXME("Throw URIError: Invalid UTF sequence\n");
882
                jsstr_release(str);
883 884 885 886 887 888 889 890 891
                return E_FAIL;
            }
            len += size;
        }else {
            ++ptr;
            ++len;
        }
    }

892
    ret = jsstr_alloc_buf(len, &out_ptr);
893
    if(!ret) {
894
        jsstr_release(str);
895 896 897
        return E_OUTOFMEMORY;
    }

898
    ptr = uri;
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
    while(*ptr) {
        if(*ptr == '%') {
            char octets[4];
            unsigned char mask = 0x80;
            int i, size, num_bytes = 0;
            octets[0] = (hex_to_int(*(ptr+1)) << 4) + hex_to_int(*(ptr+2));
            ptr += 3;
            while(octets[0] & mask) {
                mask = mask >> 1;
                ++num_bytes;
            }
            for(i = 1; i < num_bytes; ++i) {
                octets[i] = (hex_to_int(*(ptr+1)) << 4) + hex_to_int(*(ptr+2));
                ptr += 3;
            }
            size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, octets,
                    num_bytes ? num_bytes : 1, out_ptr, len);
            len -= size;
            out_ptr += size;
        }else {
            *out_ptr++ = *ptr++;
            --len;
        }
    }

924
    jsstr_release(str);
925

926 927 928
    if(r)
        *r = jsval_string(ret);
    else
929
        jsstr_release(ret);
930
    return S_OK;
931 932
}

933 934
static const builtin_prop_t JSGlobal_props[] = {
    {CollectGarbageW,            JSGlobal_CollectGarbage,            PROPF_METHOD},
935
    {_GetObjectW,                JSGlobal_GetObject,                 PROPF_METHOD|2},
936
    {ScriptEngineW,              JSGlobal_ScriptEngine,              PROPF_METHOD},
937 938 939
    {ScriptEngineBuildVersionW,  JSGlobal_ScriptEngineBuildVersion,  PROPF_METHOD},
    {ScriptEngineMajorVersionW,  JSGlobal_ScriptEngineMajorVersion,  PROPF_METHOD},
    {ScriptEngineMinorVersionW,  JSGlobal_ScriptEngineMinorVersion,  PROPF_METHOD},
940 941 942 943 944
    {decodeURIW,                 JSGlobal_decodeURI,                 PROPF_METHOD|1},
    {decodeURIComponentW,        JSGlobal_decodeURIComponent,        PROPF_METHOD|1},
    {encodeURIW,                 JSGlobal_encodeURI,                 PROPF_METHOD|1},
    {encodeURIComponentW,        JSGlobal_encodeURIComponent,        PROPF_METHOD|1},
    {escapeW,                    JSGlobal_escape,                    PROPF_METHOD|1},
945
    {evalW,                      JSGlobal_eval,                      PROPF_METHOD|1},
946 947 948
    {isFiniteW,                  JSGlobal_isFinite,                  PROPF_METHOD|1},
    {isNaNW,                     JSGlobal_isNaN,                     PROPF_METHOD|1},
    {parseFloatW,                JSGlobal_parseFloat,                PROPF_METHOD|1},
949
    {parseIntW,                  JSGlobal_parseInt,                  PROPF_METHOD|2},
950
    {unescapeW,                  JSGlobal_unescape,                  PROPF_METHOD|1}
951 952 953 954 955
};

static const builtin_info_t JSGlobal_info = {
    JSCLASS_GLOBAL,
    {NULL, NULL, 0},
956
    ARRAY_SIZE(JSGlobal_props),
957 958 959 960 961
    JSGlobal_props,
    NULL,
    NULL
};

962
static HRESULT init_constructors(script_ctx_t *ctx, jsdisp_t *object_prototype)
963 964 965
{
    HRESULT hres;

966
    hres = init_function_constr(ctx, object_prototype);
967 968 969
    if(FAILED(hres))
        return hres;

970 971
    hres = jsdisp_define_data_property(ctx->global, FunctionW, PROPF_WRITABLE,
                                       jsval_obj(ctx->function_constr));
972 973 974
    if(FAILED(hres))
        return hres;

975
    hres = create_object_constr(ctx, object_prototype, &ctx->object_constr);
976 977 978
    if(FAILED(hres))
        return hres;

979 980
    hres = jsdisp_define_data_property(ctx->global, ObjectW, PROPF_WRITABLE,
                                       jsval_obj(ctx->object_constr));
981 982 983
    if(FAILED(hres))
        return hres;

984
    hres = create_array_constr(ctx, object_prototype, &ctx->array_constr);
985 986 987
    if(FAILED(hres))
        return hres;

988 989
    hres = jsdisp_define_data_property(ctx->global, ArrayW, PROPF_WRITABLE,
                                       jsval_obj(ctx->array_constr));
990 991 992
    if(FAILED(hres))
        return hres;

993
    hres = create_bool_constr(ctx, object_prototype, &ctx->bool_constr);
994 995 996
    if(FAILED(hres))
        return hres;

997 998
    hres = jsdisp_define_data_property(ctx->global, BooleanW, PROPF_WRITABLE,
                                       jsval_obj(ctx->bool_constr));
999 1000 1001
    if(FAILED(hres))
        return hres;

1002
    hres = create_date_constr(ctx, object_prototype, &ctx->date_constr);
1003 1004 1005
    if(FAILED(hres))
        return hres;

1006 1007
    hres = jsdisp_define_data_property(ctx->global, DateW, PROPF_WRITABLE,
                                       jsval_obj(ctx->date_constr));
1008 1009 1010
    if(FAILED(hres))
        return hres;

1011 1012 1013 1014 1015 1016 1017 1018 1019
    hres = create_enumerator_constr(ctx, object_prototype, &ctx->enumerator_constr);
    if(FAILED(hres))
        return hres;

    hres = jsdisp_define_data_property(ctx->global, EnumeratorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->enumerator_constr));
    if(FAILED(hres))
        return hres;

1020
    hres = init_error_constr(ctx, object_prototype);
1021 1022 1023
    if(FAILED(hres))
        return hres;

1024 1025
    hres = jsdisp_define_data_property(ctx->global, ErrorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->error_constr));
1026 1027 1028
    if(FAILED(hres))
        return hres;

1029 1030
    hres = jsdisp_define_data_property(ctx->global, EvalErrorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->eval_error_constr));
1031 1032 1033
    if(FAILED(hres))
        return hres;

1034 1035
    hres = jsdisp_define_data_property(ctx->global, RangeErrorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->range_error_constr));
1036 1037 1038
    if(FAILED(hres))
        return hres;

1039 1040
    hres = jsdisp_define_data_property(ctx->global, ReferenceErrorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->reference_error_constr));
1041 1042 1043
    if(FAILED(hres))
        return hres;

1044 1045
    hres = jsdisp_define_data_property(ctx->global, RegExpErrorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->regexp_error_constr));
1046 1047 1048
    if(FAILED(hres))
        return hres;

1049 1050
    hres = jsdisp_define_data_property(ctx->global, SyntaxErrorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->syntax_error_constr));
1051 1052 1053
    if(FAILED(hres))
        return hres;

1054 1055
    hres = jsdisp_define_data_property(ctx->global, TypeErrorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->type_error_constr));
1056 1057 1058
    if(FAILED(hres))
        return hres;

1059 1060
    hres = jsdisp_define_data_property(ctx->global, URIErrorW, PROPF_WRITABLE,
                                       jsval_obj(ctx->uri_error_constr));
1061 1062 1063
    if(FAILED(hres))
        return hres;

1064
    hres = create_number_constr(ctx, object_prototype, &ctx->number_constr);
1065 1066 1067
    if(FAILED(hres))
        return hres;

1068 1069
    hres = jsdisp_define_data_property(ctx->global, NumberW, PROPF_WRITABLE,
                                       jsval_obj(ctx->number_constr));
1070 1071 1072
    if(FAILED(hres))
        return hres;

1073
    hres = create_regexp_constr(ctx, object_prototype, &ctx->regexp_constr);
1074 1075 1076
    if(FAILED(hres))
        return hres;

1077 1078
    hres = jsdisp_define_data_property(ctx->global, RegExpW, PROPF_WRITABLE,
                                       jsval_obj(ctx->regexp_constr));
1079 1080 1081
    if(FAILED(hres))
        return hres;

1082
    hres = create_string_constr(ctx, object_prototype, &ctx->string_constr);
1083 1084 1085
    if(FAILED(hres))
        return hres;

1086 1087
    hres = jsdisp_define_data_property(ctx->global, StringW, PROPF_WRITABLE,
                                       jsval_obj(ctx->string_constr));
1088 1089 1090
    if(FAILED(hres))
        return hres;

1091 1092 1093 1094
    hres = create_vbarray_constr(ctx, object_prototype, &ctx->vbarray_constr);
    if(FAILED(hres))
        return hres;

1095 1096
    hres = jsdisp_define_data_property(ctx->global, VBArrayW, PROPF_WRITABLE,
                                       jsval_obj(ctx->vbarray_constr));
1097 1098 1099
    if(FAILED(hres))
        return hres;

1100 1101 1102
    return S_OK;
}

1103 1104
HRESULT init_global(script_ctx_t *ctx)
{
1105
    unsigned const_flags = ctx->version >= SCRIPTLANGUAGEVERSION_ES5 ? 0 : PROPF_WRITABLE;
1106
    jsdisp_t *math, *object_prototype, *constr;
1107 1108
    HRESULT hres;

1109 1110 1111
    if(ctx->global)
        return S_OK;

1112
    hres = create_dispex(ctx, &JSGlobal_info, NULL, &ctx->global);
1113 1114 1115
    if(FAILED(hres))
        return hres;

1116
    hres = create_object_prototype(ctx, &object_prototype);
1117 1118 1119
    if(FAILED(hres))
        return hres;

1120 1121
    hres = init_constructors(ctx, object_prototype);
    jsdisp_release(object_prototype);
1122 1123 1124 1125 1126 1127 1128
    if(FAILED(hres))
        return hres;

    hres = create_math(ctx, &math);
    if(FAILED(hres))
        return hres;

1129
    hres = jsdisp_define_data_property(ctx->global, MathW, PROPF_WRITABLE, jsval_obj(math));
1130 1131 1132 1133
    jsdisp_release(math);
    if(FAILED(hres))
        return hres;

1134 1135 1136 1137 1138 1139 1140
    if(ctx->version >= 2) {
        jsdisp_t *json;

        hres = create_json(ctx, &json);
        if(FAILED(hres))
            return hres;

1141
        hres = jsdisp_define_data_property(ctx->global, JSONW, PROPF_WRITABLE, jsval_obj(json));
1142 1143 1144 1145 1146
        jsdisp_release(json);
        if(FAILED(hres))
            return hres;
    }

1147 1148 1149 1150
    hres = create_activex_constr(ctx, &constr);
    if(FAILED(hres))
        return hres;

1151 1152
    hres = jsdisp_define_data_property(ctx->global, ActiveXObjectW, PROPF_WRITABLE,
                                       jsval_obj(constr));
1153 1154 1155 1156
    jsdisp_release(constr);
    if(FAILED(hres))
        return hres;

1157
    hres = jsdisp_define_data_property(ctx->global, undefinedW, const_flags, jsval_undefined());
1158 1159 1160
    if(FAILED(hres))
        return hres;

1161
    hres = jsdisp_define_data_property(ctx->global, NaNW, const_flags, jsval_number(NAN));
1162 1163
    if(FAILED(hres))
        return hres;
1164

1165
    hres = jsdisp_define_data_property(ctx->global, InfinityW, const_flags, jsval_number(INFINITY));
1166
    return hres;
1167
}