compile.c 55.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2011 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
 */

#include <math.h>
#include <assert.h>

#include "jscript.h"
#include "engine.h"
24
#include "parser.h"
25 26 27 28

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29
WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
30

31 32 33 34 35 36
typedef struct _statement_ctx_t {
    unsigned stack_use;
    BOOL using_scope;
    BOOL using_except;

    unsigned break_label;
37
    unsigned continue_label;
38

39 40
    const labelled_statement_t *labelled_stat;

41 42 43
    struct _statement_ctx_t *next;
} statement_ctx_t;

44
typedef struct {
45 46 47
    parser_ctx_t *parser;
    bytecode_t *code;

48 49
    BOOL from_eval;

50 51
    unsigned code_off;
    unsigned code_size;
52

53 54 55 56 57
    unsigned *labels;
    unsigned labels_size;
    unsigned labels_cnt;

    statement_ctx_t *stat_ctx;
58
    function_code_t *func;
59 60 61

    variable_declaration_t *var_head;
    variable_declaration_t *var_tail;
62 63 64

    function_expression_t *func_head;
    function_expression_t *func_tail;
65
} compiler_ctx_t;
66

67 68 69 70 71 72 73 74 75 76
static const struct {
    const char *op_str;
    instr_arg_type_t arg1_type;
    instr_arg_type_t arg2_type;
} instr_info[] = {
#define X(n,a,b,c) {#n,b,c},
OP_LIST
#undef X
};

77 78 79 80
static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
{
    switch(type) {
    case ARG_STR:
81
        TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
82 83 84 85 86 87 88 89 90 91 92 93 94 95
        break;
    case ARG_BSTR:
        TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
        break;
    case ARG_INT:
        TRACE_(jscript_disas)("\t%d", arg->uint);
        break;
    case ARG_UINT:
    case ARG_ADDR:
        TRACE_(jscript_disas)("\t%u", arg->uint);
        break;
    case ARG_FUNC:
    case ARG_NONE:
        break;
96
    DEFAULT_UNREACHABLE;
97 98 99 100 101 102 103 104 105
    }
}

static void dump_code(compiler_ctx_t *ctx, unsigned off)
{
    instr_t *instr;

    for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
        TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
106 107 108 109 110 111
        if(instr_info[instr->op].arg1_type == ARG_DBL) {
            TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
        }else {
            dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
            dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
        }
112 113 114 115
        TRACE_(jscript_disas)("\n");
    }
}

116
static HRESULT compile_expression(compiler_ctx_t*,expression_t*,BOOL);
117
static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
118

119 120
static inline void *compiler_alloc(bytecode_t *code, size_t size)
{
121
    return heap_pool_alloc(&code->heap, size);
122 123
}

124
static jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
125
{
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    jsstr_t *new_str;

    if(!ctx->code->str_pool_size) {
        ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
        if(!ctx->code->str_pool)
            return NULL;
        ctx->code->str_pool_size = 8;
    }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
        jsstr_t **new_pool;

        new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
        if(!new_pool)
            return NULL;

        ctx->code->str_pool = new_pool;
        ctx->code->str_pool_size *= 2;
    }

    new_str = jsstr_alloc_len(str, len);
    if(!new_str)
        return NULL;

    ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
    return new_str;
}

static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
{
    return compiler_alloc_string_len(ctx, str, strlenW(str));
155 156
}

157
static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
158 159 160 161
{
    if(!ctx->code->bstr_pool_size) {
        ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
        if(!ctx->code->bstr_pool)
162
            return FALSE;
163 164 165 166 167 168
        ctx->code->bstr_pool_size = 8;
    }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
        BSTR *new_pool;

        new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
        if(!new_pool)
169
            return FALSE;
170 171 172 173 174

        ctx->code->bstr_pool = new_pool;
        ctx->code->bstr_pool_size *= 2;
    }

175 176 177 178 179 180 181 182
    return TRUE;
}

static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
{
    if(!ensure_bstr_slot(ctx))
        return NULL;

183 184 185 186 187 188 189
    ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
    if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
        return NULL;

    return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
}

190 191 192 193 194 195 196 197 198 199 200 201
static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
{
    if(!ensure_bstr_slot(ctx))
        return NULL;

    ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
    if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
        return NULL;

    return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
}

202 203 204 205
static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
{
    assert(ctx->code_size >= ctx->code_off);

206
    if(ctx->code_size == ctx->code_off) {
207 208 209 210
        instr_t *new_instrs;

        new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
        if(!new_instrs)
211
            return 0;
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

        ctx->code->instrs = new_instrs;
        ctx->code_size *= 2;
    }

    ctx->code->instrs[ctx->code_off].op = op;
    return ctx->code_off++;
}

static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
{
    assert(off < ctx->code_off);
    return ctx->code->instrs + off;
}

227 228 229 230 231
static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
{
    unsigned instr;

    instr = push_instr(ctx, op);
232
    if(!instr)
233 234
        return E_OUTOFMEMORY;

235
    instr_ptr(ctx, instr)->u.arg->lng = arg;
236 237 238
    return S_OK;
}

239 240 241
static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
{
    unsigned instr;
242
    jsstr_t *str;
243

244
    str = compiler_alloc_string(ctx, arg);
245 246 247 248
    if(!str)
        return E_OUTOFMEMORY;

    instr = push_instr(ctx, op);
249
    if(!instr)
250 251
        return E_OUTOFMEMORY;

252
    instr_ptr(ctx, instr)->u.arg->str = str;
253 254 255
    return S_OK;
}

256 257 258 259 260 261 262 263 264 265
static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
{
    unsigned instr;
    WCHAR *str;

    str = compiler_alloc_bstr(ctx, arg);
    if(!str)
        return E_OUTOFMEMORY;

    instr = push_instr(ctx, op);
266
    if(!instr)
267 268
        return E_OUTOFMEMORY;

269
    instr_ptr(ctx, instr)->u.arg->bstr = str;
270 271 272
    return S_OK;
}

273 274 275 276 277 278 279 280 281 282
static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
{
    unsigned instr;
    WCHAR *str;

    str = compiler_alloc_bstr(ctx, arg1);
    if(!str)
        return E_OUTOFMEMORY;

    instr = push_instr(ctx, op);
283
    if(!instr)
284 285
        return E_OUTOFMEMORY;

286 287
    instr_ptr(ctx, instr)->u.arg[0].bstr = str;
    instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
288 289 290
    return S_OK;
}

291 292 293
static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
{
    unsigned instr;
294
    jsstr_t *str;
295

296
    str = compiler_alloc_string(ctx, arg2);
297 298 299 300
    if(!str)
        return E_OUTOFMEMORY;

    instr = push_instr(ctx, op);
301
    if(!instr)
302 303
        return E_OUTOFMEMORY;

304 305
    instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
    instr_ptr(ctx, instr)->u.arg[1].str = str;
306 307 308
    return S_OK;
}

309 310 311 312 313
static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
{
    unsigned instr;

    instr = push_instr(ctx, op);
314
    if(!instr)
315 316
        return E_OUTOFMEMORY;

317
    instr_ptr(ctx, instr)->u.dbl = arg;
318 319 320
    return S_OK;
}

321 322 323 324 325
static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
{
    instr_ptr(ctx, instr)->u.arg->uint = arg;
}

326 327 328 329 330
static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
{
    unsigned instr;

    instr = push_instr(ctx, op);
331
    if(!instr)
332 333
        return E_OUTOFMEMORY;

334
    set_arg_uint(ctx, instr, arg);
335 336 337
    return S_OK;
}

338 339 340 341
static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
{
    HRESULT hres;

342
    hres = compile_expression(ctx, expr->expression1, TRUE);
343 344 345
    if(FAILED(hres))
        return hres;

346
    hres = compile_expression(ctx, expr->expression2, TRUE);
347 348 349
    if(FAILED(hres))
        return hres;

350
    return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
351 352
}

353 354 355 356
static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
{
    HRESULT hres;

357
    hres = compile_expression(ctx, expr->expression, TRUE);
358 359 360
    if(FAILED(hres))
        return hres;

361
    return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
362 363
}

364 365 366 367 368
/* ECMA-262 3rd Edition    11.2.1 */
static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
{
    HRESULT hres;

369
    hres = compile_expression(ctx, expr->expression, TRUE);
370 371 372 373 374 375
    if(FAILED(hres))
        return hres;

    return push_instr_bstr(ctx, OP_member, expr->identifier);
}

376 377 378 379 380 381 382
#define LABEL_FLAG 0x80000000

static unsigned alloc_label(compiler_ctx_t *ctx)
{
    if(!ctx->labels_size) {
        ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
        if(!ctx->labels)
383
            return 0;
384 385 386 387 388 389
        ctx->labels_size = 8;
    }else if(ctx->labels_size == ctx->labels_cnt) {
        unsigned *new_labels;

        new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
        if(!new_labels)
390
            return 0;
391 392 393 394 395 396 397 398 399 400 401 402 403 404

        ctx->labels = new_labels;
        ctx->labels_size *= 2;
    }

    return ctx->labels_cnt++ | LABEL_FLAG;
}

static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
{
    assert(label & LABEL_FLAG);
    ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
}

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static inline BOOL is_memberid_expr(expression_type_t type)
{
    return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
}

static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
{
    HRESULT hres = S_OK;

    switch(expr->type) {
    case EXPR_IDENT: {
        identifier_expression_t *ident_expr = (identifier_expression_t*)expr;

        hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
        break;
    }
    case EXPR_ARRAY: {
422
        binary_expression_t *array_expr = (binary_expression_t*)expr;
423

424
        hres = compile_expression(ctx, array_expr->expression1, TRUE);
425 426 427
        if(FAILED(hres))
            return hres;

428
        hres = compile_expression(ctx, array_expr->expression2, TRUE);
429 430 431 432 433 434 435 436 437
        if(FAILED(hres))
            return hres;

        hres = push_instr_uint(ctx, OP_memberid, flags);
        break;
    }
    case EXPR_MEMBER: {
        member_expression_t *member_expr = (member_expression_t*)expr;

438
        hres = compile_expression(ctx, member_expr->expression, TRUE);
439 440 441 442 443 444 445 446 447 448 449
        if(FAILED(hres))
            return hres;

        /* FIXME: Potential optimization */
        hres = push_instr_str(ctx, OP_str, member_expr->identifier);
        if(FAILED(hres))
            return hres;

        hres = push_instr_uint(ctx, OP_memberid, flags);
        break;
    }
450
    DEFAULT_UNREACHABLE;
451 452 453 454 455
    }

    return hres;
}

456
static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
457 458 459 460
{
    HRESULT hres;

    if(!is_memberid_expr(expr->expression->type)) {
461
        hres = compile_expression(ctx, expr->expression, TRUE);
462 463 464
        if(FAILED(hres))
            return hres;

465
        return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
466 467 468 469 470 471
    }

    hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
    if(FAILED(hres))
        return hres;

472
    return push_instr_int(ctx, op, n);
473 474
}

475
/* ECMA-262 3rd Edition    11.14 */
476
static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr, BOOL emit_ret)
477 478 479
{
    HRESULT hres;

480
    hres = compile_expression(ctx, expr->expression1, FALSE);
481 482 483
    if(FAILED(hres))
        return hres;

484
    return compile_expression(ctx, expr->expression2, emit_ret);
485 486
}

487 488 489 490 491 492
/* ECMA-262 3rd Edition    11.11 */
static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
{
    unsigned instr;
    HRESULT hres;

493
    hres = compile_expression(ctx, expr->expression1, TRUE);
494 495 496 497
    if(FAILED(hres))
        return hres;

    instr = push_instr(ctx, op);
498
    if(!instr)
499 500
        return E_OUTOFMEMORY;

501
    hres = compile_expression(ctx, expr->expression2, TRUE);
502 503 504
    if(FAILED(hres))
        return hres;

505
    set_arg_uint(ctx, instr, ctx->code_off);
506 507 508
    return S_OK;
}

509 510 511 512 513 514
/* ECMA-262 3rd Edition    11.12 */
static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
{
    unsigned jmp_false, jmp_end;
    HRESULT hres;

515
    hres = compile_expression(ctx, expr->expression, TRUE);
516 517 518
    if(FAILED(hres))
        return hres;

519
    jmp_false = push_instr(ctx, OP_cnd_z);
520
    if(!jmp_false)
521 522
        return E_OUTOFMEMORY;

523
    hres = compile_expression(ctx, expr->true_expression, TRUE);
524 525 526 527
    if(FAILED(hres))
        return hres;

    jmp_end = push_instr(ctx, OP_jmp);
528
    if(!jmp_end)
529 530
        return E_OUTOFMEMORY;

531
    set_arg_uint(ctx, jmp_false, ctx->code_off);
532 533 534
    hres = push_instr_uint(ctx, OP_pop, 1);
    if(FAILED(hres))
        return hres;
535

536
    hres = compile_expression(ctx, expr->false_expression, TRUE);
537 538 539
    if(FAILED(hres))
        return hres;

540
    set_arg_uint(ctx, jmp_end, ctx->code_off);
541 542 543
    return S_OK;
}

544 545 546 547 548 549
static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
{
    unsigned arg_cnt = 0;
    argument_t *arg;
    HRESULT hres;

550
    hres = compile_expression(ctx, expr->expression, TRUE);
551 552 553 554
    if(FAILED(hres))
        return hres;

    for(arg = expr->argument_list; arg; arg = arg->next) {
555
        hres = compile_expression(ctx, arg->expr, TRUE);
556 557 558 559 560
        if(FAILED(hres))
            return hres;
        arg_cnt++;
    }

561
    return push_instr_uint(ctx, OP_new, arg_cnt);
562 563
}

564
static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL emit_ret)
565 566 567 568
{
    unsigned arg_cnt = 0;
    argument_t *arg;
    unsigned instr;
569
    jsop_t op;
570 571
    HRESULT hres;

572 573 574 575 576
    if(is_memberid_expr(expr->expression->type)) {
        op = OP_call_member;
        hres = compile_memberid_expression(ctx, expr->expression, 0);
    }else {
        op = OP_call;
577
        hres = compile_expression(ctx, expr->expression, TRUE);
578 579 580 581 582 583
    }

    if(FAILED(hres))
        return hres;

    for(arg = expr->argument_list; arg; arg = arg->next) {
584
        hres = compile_expression(ctx, arg->expr, TRUE);
585 586 587 588 589
        if(FAILED(hres))
            return hres;
        arg_cnt++;
    }

590
    instr = push_instr(ctx, op);
591
    if(!instr)
592 593
        return E_OUTOFMEMORY;

594
    instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
595
    instr_ptr(ctx, instr)->u.arg[1].lng = emit_ret;
596 597 598
    return S_OK;
}

599 600 601 602 603 604
static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
{
    HRESULT hres;

    switch(expr->expression->type) {
    case EXPR_ARRAY: {
605
        binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
606

607
        hres = compile_expression(ctx, array_expr->expression1, TRUE);
608 609 610
        if(FAILED(hres))
            return hres;

611
        hres = compile_expression(ctx, array_expr->expression2, TRUE);
612 613 614
        if(FAILED(hres))
            return hres;

615
        if(!push_instr(ctx, OP_delete))
616 617 618
            return E_OUTOFMEMORY;
        break;
    }
619 620 621
    case EXPR_MEMBER: {
        member_expression_t *member_expr = (member_expression_t*)expr->expression;

622
        hres = compile_expression(ctx, member_expr->expression, TRUE);
623 624 625 626 627 628 629 630
        if(FAILED(hres))
            return hres;

        /* FIXME: Potential optimization */
        hres = push_instr_str(ctx, OP_str, member_expr->identifier);
        if(FAILED(hres))
            return hres;

631
        if(!push_instr(ctx, OP_delete))
632 633 634
            return E_OUTOFMEMORY;
        break;
    }
635 636
    case EXPR_IDENT:
        return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
637 638 639 640 641
    default: {
        const WCHAR fixmeW[] = {'F','I','X','M','E',0};

        WARN("invalid delete, unimplemented exception message\n");

642
        hres = compile_expression(ctx, expr->expression, TRUE);
643 644 645 646 647
        if(FAILED(hres))
            return hres;

        return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
    }
648 649 650 651 652
    }

    return S_OK;
}

653
static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
654
{
655 656
    BOOL use_throw_path = FALSE;
    unsigned arg_cnt = 0;
657 658
    HRESULT hres;

659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    if(expr->expression1->type == EXPR_CALL) {
        call_expression_t *call_expr = (call_expression_t*)expr->expression1;
        argument_t *arg;

        if(op != OP_LAST) {
            FIXME("op %d not supported on parametrized assign expressions\n", op);
            return E_NOTIMPL;
        }

        if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
            hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
            if(FAILED(hres))
                return hres;

            for(arg = call_expr->argument_list; arg; arg = arg->next) {
674
                hres = compile_expression(ctx, arg->expr, TRUE);
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
                if(FAILED(hres))
                    return hres;
                arg_cnt++;
            }
        }else {
            use_throw_path = TRUE;
        }
    }else if(is_memberid_expr(expr->expression1->type)) {
        hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
        if(FAILED(hres))
            return hres;
    }else {
        use_throw_path = TRUE;
    }

    if(use_throw_path) {
        /* Illegal assignment: evaluate and throw */
692
        hres = compile_expression(ctx, expr->expression1, TRUE);
693 694 695
        if(FAILED(hres))
            return hres;

696
        hres = compile_expression(ctx, expr->expression2, TRUE);
697 698 699
        if(FAILED(hres))
            return hres;

700
        if(op != OP_LAST && !push_instr(ctx, op))
701 702
            return E_OUTOFMEMORY;

703
        return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
704 705
    }

706
    if(op != OP_LAST && !push_instr(ctx, OP_refval))
707
        return E_OUTOFMEMORY;
708

709
    hres = compile_expression(ctx, expr->expression2, TRUE);
710 711 712
    if(FAILED(hres))
        return hres;

713
    if(op != OP_LAST && !push_instr(ctx, op))
714 715
        return E_OUTOFMEMORY;

716 717 718
    if(arg_cnt)
        return push_instr_uint(ctx, OP_assign_call, arg_cnt);

719
    if(!push_instr(ctx, OP_assign))
720 721 722 723 724
        return E_OUTOFMEMORY;

    return S_OK;
}

725 726 727 728 729 730 731
static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
{
    jsop_t op;
    HRESULT hres;

    if(is_memberid_expr(expr->expression->type)) {
        if(expr->expression->type == EXPR_IDENT)
732
            return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
733 734 735 736 737

        op = OP_typeofid;
        hres = compile_memberid_expression(ctx, expr->expression, 0);
    }else {
        op = OP_typeof;
738
        hres = compile_expression(ctx, expr->expression, TRUE);
739 740 741 742
    }
    if(FAILED(hres))
        return hres;

743
    return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
744 745
}

746
static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
747 748
{
    switch(literal->type) {
749 750
    case LT_BOOL:
        return push_instr_int(ctx, OP_bool, literal->u.bval);
751 752
    case LT_DOUBLE:
        return push_instr_double(ctx, OP_double, literal->u.dval);
753
    case LT_NULL:
754
        return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
755 756
    case LT_STRING:
        return push_instr_str(ctx, OP_str, literal->u.wstr);
757 758
    case LT_REGEXP: {
        unsigned instr;
759
        jsstr_t *str;
760

761
        str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
762 763 764 765
        if(!str)
            return E_OUTOFMEMORY;

        instr = push_instr(ctx, OP_regexp);
766
        if(!instr)
767 768
            return E_OUTOFMEMORY;

769 770
        instr_ptr(ctx, instr)->u.arg[0].str = str;
        instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
771 772
        return S_OK;
    }
773
    DEFAULT_UNREACHABLE;
774
    }
775
    return E_FAIL;
776 777
}

778 779 780 781 782 783
static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
{
    switch(literal->type) {
    case LT_STRING:
        *str = compiler_alloc_bstr(ctx, literal->u.wstr);
        break;
784 785 786 787 788 789 790 791
    case LT_DOUBLE: {
        jsstr_t *jsstr;
        HRESULT hres;

        hres = double_to_string(literal->u.dval, &jsstr);
        if(FAILED(hres))
            return hres;

792 793 794
        *str = compiler_alloc_bstr_len(ctx, NULL, jsstr_length(jsstr));
        if(*str)
            jsstr_flush(jsstr, *str);
795 796 797
        jsstr_release(jsstr);
        break;
    }
798
    DEFAULT_UNREACHABLE;
799 800 801 802 803
    }

    return *str ? S_OK : E_OUTOFMEMORY;
}

804 805 806 807 808 809 810 811 812 813
static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
{
    unsigned i, elem_cnt = expr->length;
    array_element_t *iter;
    HRESULT hres;

    for(iter = expr->element_list; iter; iter = iter->next) {
        elem_cnt += iter->elision+1;

        for(i=0; i < iter->elision; i++) {
814
            if(!push_instr(ctx, OP_undefined))
815 816 817
                return E_OUTOFMEMORY;
        }

818
        hres = compile_expression(ctx, iter->expr, TRUE);
819 820 821 822 823
        if(FAILED(hres))
            return hres;
    }

    for(i=0; i < expr->length; i++) {
824
        if(!push_instr(ctx, OP_undefined))
825 826 827 828 829 830
            return E_OUTOFMEMORY;
    }

    return push_instr_uint(ctx, OP_carray, elem_cnt);
}

831 832 833 834 835 836 837
static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
{
    prop_val_t *iter;
    unsigned instr;
    BSTR name;
    HRESULT hres;

838
    if(!push_instr(ctx, OP_new_obj))
839 840 841 842 843 844 845
        return E_OUTOFMEMORY;

    for(iter = expr->property_list; iter; iter = iter->next) {
        hres = literal_as_bstr(ctx, iter->name, &name);
        if(FAILED(hres))
            return hres;

846
        hres = compile_expression(ctx, iter->value, TRUE);
847 848 849 850
        if(FAILED(hres))
            return hres;

        instr = push_instr(ctx, OP_obj_prop);
851
        if(!instr)
852 853
            return E_OUTOFMEMORY;

854
        instr_ptr(ctx, instr)->u.arg->bstr = name;
855 856 857 858 859
    }

    return S_OK;
}

860 861
static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
{
862 863
    ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);

864
    /* FIXME: not exactly right */
865 866
    if(expr->identifier) {
        ctx->func->func_cnt++;
867
        return push_instr_bstr(ctx, OP_ident, expr->identifier);
868
    }
869

870
    return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
871
}
872

873
static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr, BOOL emit_ret)
874
{
875 876
    HRESULT hres;

877
    switch(expr->type) {
878
    case EXPR_ADD:
879 880
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
        break;
881
    case EXPR_AND:
882 883
        hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
        break;
884
    case EXPR_ARRAY:
885 886
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
        break;
887
    case EXPR_ARRAYLIT:
888 889
        hres = compile_array_literal(ctx, (array_literal_expression_t*)expr);
        break;
890
    case EXPR_ASSIGN:
891 892
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
        break;
893
    case EXPR_ASSIGNADD:
894 895
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
        break;
896
    case EXPR_ASSIGNAND:
897 898
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
        break;
899
    case EXPR_ASSIGNSUB:
900 901
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
        break;
902
    case EXPR_ASSIGNMUL:
903 904
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
        break;
905
    case EXPR_ASSIGNDIV:
906 907
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
        break;
908
    case EXPR_ASSIGNMOD:
909 910
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
        break;
911
    case EXPR_ASSIGNOR:
912 913
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
        break;
914
    case EXPR_ASSIGNLSHIFT:
915 916
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
        break;
917
    case EXPR_ASSIGNRSHIFT:
918 919
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
        break;
920
    case EXPR_ASSIGNRRSHIFT:
921 922
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
        break;
923
    case EXPR_ASSIGNXOR:
924 925
        hres = compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
        break;
926
    case EXPR_BAND:
927 928
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
        break;
929
    case EXPR_BITNEG:
930 931
        hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
        break;
932
    case EXPR_BOR:
933 934
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
        break;
935
    case EXPR_CALL:
936
        return compile_call_expression(ctx, (call_expression_t*)expr, emit_ret);
937
    case EXPR_COMMA:
938
        return compile_comma_expression(ctx, (binary_expression_t*)expr, emit_ret);
939
    case EXPR_COND:
940 941
        hres = compile_conditional_expression(ctx, (conditional_expression_t*)expr);
        break;
942
    case EXPR_DELETE:
943 944
        hres = compile_delete_expression(ctx, (unary_expression_t*)expr);
        break;
945
    case EXPR_DIV:
946 947
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
        break;
948
    case EXPR_EQ:
949 950
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
        break;
951
    case EXPR_EQEQ:
952 953
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
        break;
954
    case EXPR_FUNC:
955 956
        hres = compile_function_expression(ctx, (function_expression_t*)expr);
        break;
957
    case EXPR_GREATER:
958 959
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
        break;
960
    case EXPR_GREATEREQ:
961 962
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
        break;
963
    case EXPR_IDENT:
964 965
        hres = push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
        break;
966
    case EXPR_IN:
967 968
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
        break;
969
    case EXPR_INSTANCEOF:
970 971
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
        break;
972
    case EXPR_LESS:
973 974
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
        break;
975
    case EXPR_LESSEQ:
976 977
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
        break;
978
    case EXPR_LITERAL:
979 980
        hres = compile_literal(ctx, ((literal_expression_t*)expr)->literal);
        break;
981
    case EXPR_LOGNEG:
982 983
        hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
        break;
984
    case EXPR_LSHIFT:
985 986
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
        break;
987
    case EXPR_MEMBER:
988 989
        hres = compile_member_expression(ctx, (member_expression_t*)expr);
        break;
990
    case EXPR_MINUS:
991 992
        hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
        break;
993
    case EXPR_MOD:
994 995
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
        break;
996
    case EXPR_MUL:
997 998
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
        break;
999
    case EXPR_NEW:
1000 1001
        hres = compile_new_expression(ctx, (call_expression_t*)expr);
        break;
1002
    case EXPR_NOTEQ:
1003 1004
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
        break;
1005
    case EXPR_NOTEQEQ:
1006 1007
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
        break;
1008
    case EXPR_OR:
1009 1010
        hres = compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
        break;
1011
    case EXPR_PLUS:
1012 1013
        hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
        break;
1014
    case EXPR_POSTDEC:
1015 1016
        hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
        break;
1017
    case EXPR_POSTINC:
1018 1019
        hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
        break;
1020
    case EXPR_PREDEC:
1021 1022
        hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
        break;
1023
    case EXPR_PREINC:
1024 1025
        hres = compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
        break;
1026
    case EXPR_PROPVAL:
1027 1028
        hres = compile_object_literal(ctx, (property_value_expression_t*)expr);
        break;
1029
    case EXPR_RSHIFT:
1030 1031
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
        break;
1032
    case EXPR_RRSHIFT:
1033 1034
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
        break;
1035
    case EXPR_SUB:
1036 1037
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
        break;
1038
    case EXPR_THIS:
1039
        return !emit_ret || push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
1040
    case EXPR_TYPEOF:
1041 1042
        hres = compile_typeof_expression(ctx, (unary_expression_t*)expr);
        break;
1043
    case EXPR_VOID:
1044 1045
        hres = compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
        break;
1046
    case EXPR_BXOR:
1047 1048
        hres = compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
        break;
1049
    DEFAULT_UNREACHABLE;
1050 1051
    }

1052 1053
    if(FAILED(hres))
        return hres;
1054

1055
    return emit_ret ? S_OK : push_instr_uint(ctx, OP_pop, 1);
1056 1057
}

1058 1059 1060 1061 1062
static inline BOOL is_loop_statement(statement_type_t type)
{
    return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
}

1063
/* ECMA-262 3rd Edition    12.1 */
1064 1065 1066 1067
static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
{
    HRESULT hres;

1068
    while(iter) {
1069
        hres = compile_statement(ctx, NULL, iter);
1070 1071 1072 1073 1074 1075 1076 1077 1078
        if(FAILED(hres))
            return hres;

        iter = iter->next;
    }

    return S_OK;
}

1079
/* ECMA-262 3rd Edition    12.2 */
1080
static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1081 1082 1083 1084
{
    variable_declaration_t *iter;
    HRESULT hres;

1085 1086 1087 1088 1089 1090 1091
    assert(list != NULL);

    if(ctx->var_tail)
        ctx->var_tail->global_next = list;
    else
        ctx->var_head = list;

1092
    for(iter = list; iter; iter = iter->next) {
1093 1094 1095 1096 1097
        ctx->func->var_cnt++;
        iter->global_next = iter->next;
        if(!iter->next)
            ctx->var_tail = iter;

1098 1099 1100
        if(!iter->expr)
            continue;

1101
        hres = compile_expression(ctx, iter->expr, TRUE);
1102 1103 1104 1105 1106 1107 1108 1109
        if(FAILED(hres))
            return hres;

        hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
        if(FAILED(hres))
            return hres;
    }

1110 1111 1112 1113 1114 1115
    return S_OK;
}

/* ECMA-262 3rd Edition    12.2 */
static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
{
1116
    return compile_variable_list(ctx, stat->variable_list);
1117 1118
}

1119 1120 1121 1122 1123
/* ECMA-262 3rd Edition    12.4 */
static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
{
    HRESULT hres;

1124 1125 1126
    hres = compile_expression(ctx, stat->expr, ctx->from_eval);
    if(FAILED(hres))
        return hres;
1127

1128
    return !ctx->from_eval || push_instr(ctx, OP_setret) ? S_OK : E_OUTOFMEMORY;
1129 1130
}

1131 1132 1133
/* ECMA-262 3rd Edition    12.5 */
static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
{
1134
    unsigned jmp_else;
1135 1136
    HRESULT hres;

1137
    hres = compile_expression(ctx, stat->expr, TRUE);
1138 1139 1140
    if(FAILED(hres))
        return hres;

1141
    jmp_else = push_instr(ctx, OP_jmp_z);
1142
    if(!jmp_else)
1143 1144
        return E_OUTOFMEMORY;

1145
    hres = compile_statement(ctx, NULL, stat->if_stat);
1146 1147 1148
    if(FAILED(hres))
        return hres;

1149 1150 1151 1152 1153 1154
    if(stat->else_stat) {
        unsigned jmp_end;

        jmp_end = push_instr(ctx, OP_jmp);
        if(!jmp_end)
            return E_OUTOFMEMORY;
1155

1156
        set_arg_uint(ctx, jmp_else, ctx->code_off);
1157

1158
        hres = compile_statement(ctx, NULL, stat->else_stat);
1159 1160
        if(FAILED(hres))
            return hres;
1161 1162

        set_arg_uint(ctx, jmp_end, ctx->code_off);
1163
    }else {
1164
        set_arg_uint(ctx, jmp_else, ctx->code_off);
1165 1166 1167 1168 1169
    }

    return S_OK;
}

1170 1171 1172
/* ECMA-262 3rd Edition    12.6.2 */
static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
{
1173
    statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1174
    unsigned jmp_off;
1175 1176
    HRESULT hres;

1177
    stat_ctx.break_label = alloc_label(ctx);
1178
    if(!stat_ctx.break_label)
1179 1180
        return E_OUTOFMEMORY;

1181
    stat_ctx.continue_label = alloc_label(ctx);
1182
    if(!stat_ctx.continue_label)
1183 1184
        return E_OUTOFMEMORY;

1185
    jmp_off = ctx->code_off;
1186

1187
    if(!stat->do_while) {
1188
        label_set_addr(ctx, stat_ctx.continue_label);
1189
        hres = compile_expression(ctx, stat->expr, TRUE);
1190 1191 1192
        if(FAILED(hres))
            return hres;

1193 1194 1195
        hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
        if(FAILED(hres))
            return hres;
1196 1197
    }

1198
    hres = compile_statement(ctx, &stat_ctx, stat->statement);
1199 1200 1201 1202
    if(FAILED(hres))
        return hres;

    if(stat->do_while) {
1203
        label_set_addr(ctx, stat_ctx.continue_label);
1204
        hres = compile_expression(ctx, stat->expr, TRUE);
1205 1206 1207
        if(FAILED(hres))
            return hres;

1208 1209 1210
        hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
        if(FAILED(hres))
            return hres;
1211
    }
1212

1213 1214 1215 1216
    hres = push_instr_uint(ctx, OP_jmp, jmp_off);
    if(FAILED(hres))
        return hres;

1217
    label_set_addr(ctx, stat_ctx.break_label);
1218 1219 1220
    return S_OK;
}

1221 1222 1223
/* ECMA-262 3rd Edition    12.6.3 */
static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
{
1224
    statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1225
    unsigned expr_off;
1226 1227 1228 1229 1230 1231 1232
    HRESULT hres;

    if(stat->variable_list) {
        hres = compile_variable_list(ctx, stat->variable_list);
        if(FAILED(hres))
            return hres;
    }else if(stat->begin_expr) {
1233
        hres = compile_expression(ctx, stat->begin_expr, FALSE);
1234 1235 1236 1237
        if(FAILED(hres))
            return hres;
    }

1238
    stat_ctx.break_label = alloc_label(ctx);
1239
    if(!stat_ctx.break_label)
1240 1241
        return E_OUTOFMEMORY;

1242
    stat_ctx.continue_label = alloc_label(ctx);
1243
    if(!stat_ctx.continue_label)
1244 1245
        return E_OUTOFMEMORY;

1246 1247 1248
    expr_off = ctx->code_off;

    if(stat->expr) {
1249
        hres = compile_expression(ctx, stat->expr, TRUE);
1250 1251 1252
        if(FAILED(hres))
            return hres;

1253 1254 1255
        hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
        if(FAILED(hres))
            return hres;
1256 1257
    }

1258
    hres = compile_statement(ctx, &stat_ctx, stat->statement);
1259 1260 1261
    if(FAILED(hres))
        return hres;

1262 1263
    label_set_addr(ctx, stat_ctx.continue_label);

1264
    if(stat->end_expr) {
1265
        hres = compile_expression(ctx, stat->end_expr, FALSE);
1266 1267 1268 1269 1270 1271 1272 1273
        if(FAILED(hres))
            return hres;
    }

    hres = push_instr_uint(ctx, OP_jmp, expr_off);
    if(FAILED(hres))
        return hres;

1274
    label_set_addr(ctx, stat_ctx.break_label);
1275 1276 1277
    return S_OK;
}

1278 1279 1280
/* ECMA-262 3rd Edition    12.6.4 */
static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
{
1281
    statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1282 1283 1284 1285 1286 1287 1288 1289
    HRESULT hres;

    if(stat->variable) {
        hres = compile_variable_list(ctx, stat->variable);
        if(FAILED(hres))
            return hres;
    }

1290
    stat_ctx.break_label = alloc_label(ctx);
1291
    if(!stat_ctx.break_label)
1292 1293
        return E_OUTOFMEMORY;

1294
    stat_ctx.continue_label = alloc_label(ctx);
1295
    if(!stat_ctx.continue_label)
1296 1297
        return E_OUTOFMEMORY;

1298
    hres = compile_expression(ctx, stat->in_expr, TRUE);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    if(FAILED(hres))
        return hres;

    if(stat->variable) {
        hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
        if(FAILED(hres))
            return hres;
    }else if(is_memberid_expr(stat->expr->type)) {
        hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
        if(FAILED(hres))
            return hres;
    }else {
1311
        hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
        if(FAILED(hres))
            return hres;

        /* FIXME: compile statement anyways when we depend on compiler to check errors */
        return S_OK;
    }

    hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
    if(FAILED(hres))
        return hres;

1323
    label_set_addr(ctx, stat_ctx.continue_label);
1324 1325
    hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
    if(FAILED(hres))
1326 1327
        return E_OUTOFMEMORY;

1328
    hres = compile_statement(ctx, &stat_ctx, stat->statement);
1329 1330 1331
    if(FAILED(hres))
        return hres;

1332
    hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1333 1334 1335
    if(FAILED(hres))
        return hres;

1336
    label_set_addr(ctx, stat_ctx.break_label);
1337 1338 1339
    return S_OK;
}

1340
static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1341 1342
{
    unsigned stack_pop = 0;
1343
    statement_ctx_t *iter;
1344

1345
    for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1346 1347 1348 1349 1350 1351
        if(scope_stack) {
            if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
                return E_OUTOFMEMORY;
            if(iter->using_except && !push_instr(ctx, OP_pop_except))
                return E_OUTOFMEMORY;
        }
1352 1353 1354
        stack_pop += iter->stack_use;
    }

1355 1356 1357 1358 1359 1360
    if(var_stack && stack_pop) {
        HRESULT hres;

        hres = push_instr_uint(ctx, OP_pop, stack_pop);
        if(FAILED(hres))
            return hres;
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
    }

    return S_OK;
}

/* ECMA-262 3rd Edition    12.7 */
static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
{
    statement_ctx_t *pop_ctx;
    HRESULT hres;

1372 1373 1374
    if(stat->identifier) {
        statement_t *label_stat;
        statement_ctx_t *iter;
1375

1376 1377 1378 1379 1380 1381 1382 1383
        pop_ctx = NULL;

        for(iter = ctx->stat_ctx; iter; iter = iter->next) {
            if(iter->continue_label)
                pop_ctx = iter;
            if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
                break;
        }
1384

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
        if(!iter) {
            WARN("Label not found\n");
            return JS_E_LABEL_NOT_FOUND;
        }

        /* Labelled continue are allowed only on loops */
        for(label_stat = iter->labelled_stat->statement;
            label_stat->type == STAT_LABEL;
            label_stat = ((labelled_statement_t*)label_stat)->statement);
        if(!is_loop_statement(label_stat->type)) {
            WARN("Label is not a loop\n");
            return JS_E_INVALID_CONTINUE;
        }
1398 1399

        assert(pop_ctx != NULL);
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
    }else {
        for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
            if(pop_ctx->continue_label)
                break;
        }

        if(!pop_ctx) {
            WARN("continue outside loop\n");
            return JS_E_INVALID_CONTINUE;
        }
    }
1411

1412
    hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
    if(FAILED(hres))
        return hres;

    return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
}

/* ECMA-262 3rd Edition    12.8 */
static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
{
    statement_ctx_t *pop_ctx;
    HRESULT hres;

1425 1426 1427 1428 1429 1430 1431
    if(stat->identifier) {
        for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
            if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
                assert(pop_ctx->break_label);
                break;
            }
        }
1432

1433 1434 1435 1436 1437 1438 1439 1440 1441
        if(!pop_ctx) {
            WARN("Label not found\n");
            return JS_E_LABEL_NOT_FOUND;
        }
    }else {
        for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
            if(pop_ctx->break_label && !pop_ctx->labelled_stat)
                break;
        }
1442

1443 1444 1445 1446 1447
        if(!pop_ctx) {
            WARN("Break outside loop\n");
            return JS_E_INVALID_BREAK;
        }
    }
1448

1449
    hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1450 1451 1452 1453 1454 1455
    if(FAILED(hres))
        return hres;

    return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
}

1456 1457 1458
/* ECMA-262 3rd Edition    12.9 */
static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
{
1459 1460
    HRESULT hres;

1461 1462 1463 1464 1465
    if(ctx->from_eval) {
        WARN("misplaced return statement\n");
        return JS_E_MISPLACED_RETURN;
    }

1466
    hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1467 1468 1469
    if(FAILED(hres))
        return hres;

1470 1471 1472 1473 1474 1475 1476
    if(stat->expr) {
        hres = compile_expression(ctx, stat->expr, TRUE);
        if(FAILED(hres))
            return hres;
        if(!push_instr(ctx, OP_setret))
            return E_OUTOFMEMORY;
    }
1477

1478 1479 1480 1481
    hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
    if(FAILED(hres))
        return hres;

1482
    return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1483 1484
}

1485 1486 1487
/* ECMA-262 3rd Edition    12.10 */
static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
{
1488
    statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1489 1490
    HRESULT hres;

1491
    hres = compile_expression(ctx, stat->expr, TRUE);
1492 1493 1494
    if(FAILED(hres))
        return hres;

1495
    if(!push_instr(ctx, OP_push_scope))
1496 1497
        return E_OUTOFMEMORY;

1498
    hres = compile_statement(ctx, &stat_ctx, stat->statement);
1499 1500 1501
    if(FAILED(hres))
        return hres;

1502
    if(!push_instr(ctx, OP_pop_scope))
1503 1504 1505 1506 1507
        return E_OUTOFMEMORY;

    return S_OK;
}

1508 1509 1510 1511
/* ECMA-262 3rd Edition    12.10 */
static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
{
    statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1512
    HRESULT hres;
1513 1514 1515 1516 1517 1518 1519 1520

    for(iter = ctx->stat_ctx; iter; iter = iter->next) {
        if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
            WARN("Label %s redefined\n", debugstr_w(stat->identifier));
            return JS_E_LABEL_REDEFINED;
        }
    }

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
    /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
    stat_ctx.break_label = alloc_label(ctx);
    if(!stat_ctx.break_label)
        return E_OUTOFMEMORY;

    hres = compile_statement(ctx, &stat_ctx, stat->statement);
    if(FAILED(hres))
        return hres;

    label_set_addr(ctx, stat_ctx.break_label);
    return S_OK;
1532 1533
}

1534 1535 1536
/* ECMA-262 3rd Edition    12.13 */
static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
{
1537
    statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1538 1539 1540 1541 1542 1543
    unsigned case_cnt = 0, *case_jmps, i, default_jmp;
    BOOL have_default = FALSE;
    statement_t *stat_iter;
    case_clausule_t *iter;
    HRESULT hres;

1544
    hres = compile_expression(ctx, stat->expr, TRUE);
1545 1546 1547
    if(FAILED(hres))
        return hres;

1548
    stat_ctx.break_label = alloc_label(ctx);
1549
    if(!stat_ctx.break_label)
1550 1551
        return E_OUTOFMEMORY;

1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
    for(iter = stat->case_list; iter; iter = iter->next) {
        if(iter->expr)
            case_cnt++;
    }

    case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
    if(!case_jmps)
        return E_OUTOFMEMORY;

    i = 0;
    for(iter = stat->case_list; iter; iter = iter->next) {
        if(!iter->expr) {
            have_default = TRUE;
            continue;
        }

1568
        hres = compile_expression(ctx, iter->expr, TRUE);
1569 1570 1571 1572
        if(FAILED(hres))
            break;

        case_jmps[i] = push_instr(ctx, OP_case);
1573
        if(!case_jmps[i]) {
1574 1575 1576 1577 1578 1579 1580
            hres = E_OUTOFMEMORY;
            break;
        }
        i++;
    }

    if(SUCCEEDED(hres)) {
1581 1582
        hres = push_instr_uint(ctx, OP_pop, 1);
        if(SUCCEEDED(hres)) {
1583
            default_jmp = push_instr(ctx, OP_jmp);
1584
            if(!default_jmp)
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
                hres = E_OUTOFMEMORY;
        }
    }

    if(FAILED(hres)) {
        heap_free(case_jmps);
        return hres;
    }

    i = 0;
    for(iter = stat->case_list; iter; iter = iter->next) {
        while(iter->next && iter->next->stat == iter->stat) {
1597
            set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1598 1599 1600
            iter = iter->next;
        }

1601
        set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1602

1603 1604 1605
        for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
            stat_iter = stat_iter->next) {
            hres = compile_statement(ctx, &stat_ctx, stat_iter);
1606 1607 1608
            if(FAILED(hres))
                break;
        }
1609 1610
        if(FAILED(hres))
            break;
1611 1612 1613 1614 1615 1616 1617
    }

    heap_free(case_jmps);
    if(FAILED(hres))
        return hres;
    assert(i == case_cnt);

1618
    if(!have_default) {
1619 1620 1621
        hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
        if(FAILED(hres))
            return hres;
1622
        set_arg_uint(ctx, default_jmp, ctx->code_off);
1623
    }
1624

1625
    label_set_addr(ctx, stat_ctx.break_label);
1626 1627 1628
    return S_OK;
}

1629 1630 1631 1632 1633
/* ECMA-262 3rd Edition    12.13 */
static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
{
    HRESULT hres;

1634
    hres = compile_expression(ctx, stat->expr, TRUE);
1635 1636 1637
    if(FAILED(hres))
        return hres;

1638
    return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1639 1640
}

1641 1642 1643
/* ECMA-262 3rd Edition    12.14 */
static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
{
1644 1645
    statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
    statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1646
    unsigned push_except;
1647 1648 1649 1650
    BSTR ident;
    HRESULT hres;

    push_except = push_instr(ctx, OP_push_except);
1651
    if(!push_except)
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
        return E_OUTOFMEMORY;

    if(stat->catch_block) {
        ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
        if(!ident)
            return E_OUTOFMEMORY;
    }else {
        ident = NULL;
    }

1662
    instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1663

1664 1665 1666 1667
    if(!stat->catch_block)
        try_ctx.stack_use = 2;

    hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1668 1669 1670
    if(FAILED(hres))
        return hres;

1671
    if(!push_instr(ctx, OP_pop_except))
1672 1673 1674 1675 1676 1677
        return E_OUTOFMEMORY;

    if(stat->catch_block) {
        unsigned jmp_finally;

        jmp_finally = push_instr(ctx, OP_jmp);
1678
        if(!jmp_finally)
1679 1680
            return E_OUTOFMEMORY;

1681
        instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1682

1683
        hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1684 1685 1686
        if(FAILED(hres))
            return hres;

1687
        if(!push_instr(ctx, OP_pop_scope))
1688 1689
            return E_OUTOFMEMORY;

1690
        set_arg_uint(ctx, jmp_finally, ctx->code_off);
1691
    }else {
1692
        set_arg_uint(ctx, push_except, ctx->code_off);
1693 1694 1695
    }

    if(stat->finally_statement) {
1696
        hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1697 1698 1699
        if(FAILED(hres))
            return hres;

1700
        if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1701 1702 1703 1704 1705 1706
            return E_OUTOFMEMORY;
    }

    return S_OK;
}

1707
static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1708
{
1709 1710 1711 1712 1713 1714 1715
    HRESULT hres;

    if(stat_ctx) {
        stat_ctx->next = ctx->stat_ctx;
        ctx->stat_ctx = stat_ctx;
    }

1716 1717
    switch(stat->type) {
    case STAT_BLOCK:
1718 1719
        hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
        break;
1720 1721 1722 1723 1724 1725
    case STAT_BREAK:
        hres = compile_break_statement(ctx, (branch_statement_t*)stat);
        break;
    case STAT_CONTINUE:
        hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
        break;
1726
    case STAT_EMPTY:
1727 1728
        /* nothing to do */
        hres = S_OK;
1729
        break;
1730
    case STAT_EXPR:
1731 1732
        hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
        break;
1733
    case STAT_FOR:
1734 1735
        hres = compile_for_statement(ctx, (for_statement_t*)stat);
        break;
1736
    case STAT_FORIN:
1737 1738
        hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
        break;
1739
    case STAT_IF:
1740 1741
        hres = compile_if_statement(ctx, (if_statement_t*)stat);
        break;
1742
    case STAT_LABEL:
1743
        hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1744
        break;
1745 1746 1747
    case STAT_RETURN:
        hres = compile_return_statement(ctx, (expression_statement_t*)stat);
        break;
1748
    case STAT_SWITCH:
1749 1750
        hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
        break;
1751
    case STAT_THROW:
1752 1753
        hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
        break;
1754
    case STAT_TRY:
1755 1756
        hres = compile_try_statement(ctx, (try_statement_t*)stat);
        break;
1757
    case STAT_VAR:
1758 1759
        hres = compile_var_statement(ctx, (var_statement_t*)stat);
        break;
1760
    case STAT_WHILE:
1761 1762
        hres = compile_while_statement(ctx, (while_statement_t*)stat);
        break;
1763
    case STAT_WITH:
1764 1765
        hres = compile_with_statement(ctx, (with_statement_t*)stat);
        break;
1766
    DEFAULT_UNREACHABLE;
1767
    }
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781

    if(stat_ctx) {
        assert(ctx->stat_ctx == stat_ctx);
        ctx->stat_ctx = stat_ctx->next;
    }

    return hres;
}

static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
{
    instr_t *instr;

    for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1782 1783 1784
        if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
            assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
            instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1785 1786 1787 1788 1789
        }
        assert(instr_info[instr->op].arg2_type != ARG_ADDR);
    }

    ctx->labels_cnt = 0;
1790 1791
}

1792 1793
void release_bytecode(bytecode_t *code)
{
1794 1795
    unsigned i;

1796 1797 1798
    if(--code->ref)
        return;

1799 1800
    for(i=0; i < code->bstr_cnt; i++)
        SysFreeString(code->bstr_pool[i]);
1801 1802
    for(i=0; i < code->str_cnt; i++)
        jsstr_release(code->str_pool[i]);
1803

1804
    heap_free(code->source);
1805
    heap_pool_free(&code->heap);
1806
    heap_free(code->bstr_pool);
1807
    heap_free(code->str_pool);
1808 1809 1810 1811
    heap_free(code->instrs);
    heap_free(code);
}

1812
static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1813
{
1814
    compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1815
    if(!compiler->code)
1816
        return E_OUTOFMEMORY;
1817

1818
    compiler->code->ref = 1;
1819
    heap_pool_init(&compiler->code->heap);
1820

1821 1822 1823 1824 1825 1826
    compiler->code->source = heap_strdupW(source);
    if(!compiler->code->source) {
        release_bytecode(compiler->code);
        return E_OUTOFMEMORY;
    }

1827 1828 1829 1830
    compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
    if(!compiler->code->instrs) {
        release_bytecode(compiler->code);
        return E_OUTOFMEMORY;
1831 1832
    }

1833
    compiler->code_size = 64;
1834
    compiler->code_off = 1;
1835 1836 1837
    return S_OK;
}

1838 1839
static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
        BOOL from_eval, function_code_t *func)
1840
{
1841
    variable_declaration_t *var_iter;
1842
    function_expression_t *iter;
1843
    unsigned off, i;
1844 1845 1846 1847
    HRESULT hres;

    TRACE("\n");

1848
    ctx->var_head = ctx->var_tail = NULL;
1849
    ctx->func_head = ctx->func_tail = NULL;
1850
    ctx->from_eval = from_eval;
1851

1852
    off = ctx->code_off;
1853
    ctx->func = func;
1854
    hres = compile_block_statement(ctx, source->statement);
1855 1856 1857
    if(FAILED(hres))
        return hres;

1858
    resolve_labels(ctx, off);
1859

1860
    if(!push_instr(ctx, OP_ret))
1861 1862 1863
        return E_OUTOFMEMORY;

    if(TRACE_ON(jscript_disas))
1864 1865
        dump_code(ctx, off);

1866
    func->instr_off = off;
1867 1868 1869 1870 1871 1872 1873

    if(func_expr && func_expr->identifier) {
        func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
        if(!func->name)
            return E_OUTOFMEMORY;
    }

1874
    if(func_expr) {
1875 1876
        parameter_t *param_iter;

1877 1878 1879
        func->source = func_expr->src_str;
        func->source_len = func_expr->src_len;

1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
        for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
            func->param_cnt++;

        func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
        if(!func->params)
            return E_OUTOFMEMORY;

        for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
            func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
            if(!func->params[i])
                return E_OUTOFMEMORY;
        }
    }
1893

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905
    func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
    if(!func->variables)
        return E_OUTOFMEMORY;

    for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
        func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
        if(!func->variables[i])
            return E_OUTOFMEMORY;
    }

    assert(i == func->var_cnt);

1906
    func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1907 1908
    if(!func->funcs)
        return E_OUTOFMEMORY;
1909
    memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1910

1911 1912
    for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
        hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1913 1914 1915
        if(FAILED(hres))
            return hres;
    }
1916

1917
    assert(i == func->func_cnt);
1918

1919
    return S_OK;
1920
}
1921

1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
{
    const WCHAR *ptr = args, *ptr2;
    unsigned arg_cnt = 0;

    while(isspaceW(*ptr))
        ptr++;
    if(!*ptr) {
        if(args_size)
            *args_size = 0;
        return S_OK;
    }

    while(1) {
        if(!isalphaW(*ptr) && *ptr != '_') {
            FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
            return E_FAIL;
        }

        ptr2 = ptr;
        while(isalnumW(*ptr) || *ptr == '_')
            ptr++;

        if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
            FIXME("unexpected har %s\n", debugstr_w(ptr));
            return E_FAIL;
        }

        if(arg_array) {
            arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
            if(!arg_array[arg_cnt])
                return E_OUTOFMEMORY;
        }
        arg_cnt++;

        while(isspaceW(*ptr))
            ptr++;
        if(!*ptr)
            break;
        if(*ptr != ',') {
            FIXME("expected ',': %s\n", debugstr_w(ptr));
            return E_FAIL;
        }

        ptr++;
        while(isspaceW(*ptr))
            ptr++;
    }

    if(args_size)
        *args_size = arg_cnt;
    return S_OK;
}

static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
{
    HRESULT hres;

    hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
    if(FAILED(hres))
        return hres;

    ctx->code->global_code.params = compiler_alloc(ctx->code,
            ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
    if(!ctx->code->global_code.params)
        return E_OUTOFMEMORY;

    return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
}

HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
        BOOL from_eval, BOOL use_decode, bytecode_t **ret)
1994
{
1995
    compiler_ctx_t compiler = {0};
1996 1997
    HRESULT hres;

1998
    hres = init_code(&compiler, code);
1999 2000 2001
    if(FAILED(hres))
        return hres;

2002 2003 2004 2005 2006 2007
    if(args) {
        hres = compile_arguments(&compiler, args);
        if(FAILED(hres))
            return hres;
    }

2008 2009 2010 2011 2012 2013 2014 2015
    if(use_decode) {
        hres = decode_source(compiler.code->source);
        if(FAILED(hres)) {
            WARN("Decoding failed\n");
            return hres;
        }
    }

2016
    hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2017
    if(FAILED(hres)) {
2018
        release_bytecode(compiler.code);
2019
        return hres;
2020 2021
    }

2022
    hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2023
    parser_release(compiler.parser);
2024
    if(FAILED(hres)) {
2025
        release_bytecode(compiler.code);
2026 2027 2028
        return hres;
    }

2029
    *ret = compiler.code;
2030
    return S_OK;
2031
}