uxtheme: Remove unused variable.
[wine] / dlls / jscript / compile.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <math.h>
20 #include <assert.h>
21
22 #include "jscript.h"
23 #include "engine.h"
24
25 #include "wine/debug.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
28 WINE_DECLARE_DEBUG_CHANNEL(jscript_disas);
29
30 typedef struct _statement_ctx_t {
31     unsigned stack_use;
32     BOOL using_scope;
33     BOOL using_except;
34
35     unsigned break_label;
36     unsigned continue_label;
37
38     const labelled_statement_t *labelled_stat;
39
40     struct _statement_ctx_t *next;
41 } statement_ctx_t;
42
43 typedef struct {
44     parser_ctx_t *parser;
45     bytecode_t *code;
46
47     unsigned code_off;
48     unsigned code_size;
49
50     unsigned *labels;
51     unsigned labels_size;
52     unsigned labels_cnt;
53
54     statement_ctx_t *stat_ctx;
55     function_code_t *func;
56
57     variable_declaration_t *var_head;
58     variable_declaration_t *var_tail;
59
60     function_expression_t *func_head;
61     function_expression_t *func_tail;
62 } compiler_ctx_t;
63
64 static const struct {
65     const char *op_str;
66     instr_arg_type_t arg1_type;
67     instr_arg_type_t arg2_type;
68 } instr_info[] = {
69 #define X(n,a,b,c) {#n,b,c},
70 OP_LIST
71 #undef X
72 };
73
74 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
75 {
76     switch(type) {
77     case ARG_STR:
78         TRACE_(jscript_disas)("\t%s", debugstr_jsstr(arg->str));
79         break;
80     case ARG_BSTR:
81         TRACE_(jscript_disas)("\t%s", debugstr_wn(arg->bstr, SysStringLen(arg->bstr)));
82         break;
83     case ARG_INT:
84         TRACE_(jscript_disas)("\t%d", arg->uint);
85         break;
86     case ARG_UINT:
87     case ARG_ADDR:
88         TRACE_(jscript_disas)("\t%u", arg->uint);
89         break;
90     case ARG_FUNC:
91     case ARG_NONE:
92         break;
93     default:
94         assert(0);
95     }
96 }
97
98 static void dump_code(compiler_ctx_t *ctx, unsigned off)
99 {
100     instr_t *instr;
101
102     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
103         TRACE_(jscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
104         if(instr_info[instr->op].arg1_type == ARG_DBL) {
105             TRACE_(jscript_disas)("\t%lf", instr->u.dbl);
106         }else {
107             dump_instr_arg(instr_info[instr->op].arg1_type, instr->u.arg);
108             dump_instr_arg(instr_info[instr->op].arg2_type, instr->u.arg+1);
109         }
110         TRACE_(jscript_disas)("\n");
111     }
112 }
113
114 static HRESULT compile_expression(compiler_ctx_t*,expression_t*);
115 static HRESULT compile_statement(compiler_ctx_t*,statement_ctx_t*,statement_t*);
116
117 static inline void *compiler_alloc(bytecode_t *code, size_t size)
118 {
119     return jsheap_alloc(&code->heap, size);
120 }
121
122 static jsstr_t *compiler_alloc_string_len(compiler_ctx_t *ctx, const WCHAR *str, unsigned len)
123 {
124     jsstr_t *new_str;
125
126     if(!ctx->code->str_pool_size) {
127         ctx->code->str_pool = heap_alloc(8 * sizeof(jsstr_t*));
128         if(!ctx->code->str_pool)
129             return NULL;
130         ctx->code->str_pool_size = 8;
131     }else if(ctx->code->str_pool_size == ctx->code->str_cnt) {
132         jsstr_t **new_pool;
133
134         new_pool = heap_realloc(ctx->code->str_pool, ctx->code->str_pool_size*2*sizeof(jsstr_t*));
135         if(!new_pool)
136             return NULL;
137
138         ctx->code->str_pool = new_pool;
139         ctx->code->str_pool_size *= 2;
140     }
141
142     new_str = jsstr_alloc_len(str, len);
143     if(!new_str)
144         return NULL;
145
146     ctx->code->str_pool[ctx->code->str_cnt++] = new_str;
147     return new_str;
148 }
149
150 static jsstr_t *compiler_alloc_string(compiler_ctx_t *ctx, const WCHAR *str)
151 {
152     return compiler_alloc_string_len(ctx, str, strlenW(str));
153 }
154
155 static BOOL ensure_bstr_slot(compiler_ctx_t *ctx)
156 {
157     if(!ctx->code->bstr_pool_size) {
158         ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
159         if(!ctx->code->bstr_pool)
160             return FALSE;
161         ctx->code->bstr_pool_size = 8;
162     }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
163         BSTR *new_pool;
164
165         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
166         if(!new_pool)
167             return FALSE;
168
169         ctx->code->bstr_pool = new_pool;
170         ctx->code->bstr_pool_size *= 2;
171     }
172
173     return TRUE;
174 }
175
176 static BSTR compiler_alloc_bstr(compiler_ctx_t *ctx, const WCHAR *str)
177 {
178     if(!ensure_bstr_slot(ctx))
179         return NULL;
180
181     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
182     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
183         return NULL;
184
185     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
186 }
187
188 static BSTR compiler_alloc_bstr_len(compiler_ctx_t *ctx, const WCHAR *str, size_t len)
189 {
190     if(!ensure_bstr_slot(ctx))
191         return NULL;
192
193     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocStringLen(str, len);
194     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
195         return NULL;
196
197     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
198 }
199
200 static unsigned push_instr(compiler_ctx_t *ctx, jsop_t op)
201 {
202     assert(ctx->code_size >= ctx->code_off);
203
204     if(ctx->code_size == ctx->code_off) {
205         instr_t *new_instrs;
206
207         new_instrs = heap_realloc(ctx->code->instrs, ctx->code_size*2*sizeof(instr_t));
208         if(!new_instrs)
209             return 0;
210
211         ctx->code->instrs = new_instrs;
212         ctx->code_size *= 2;
213     }
214
215     ctx->code->instrs[ctx->code_off].op = op;
216     return ctx->code_off++;
217 }
218
219 static inline instr_t *instr_ptr(compiler_ctx_t *ctx, unsigned off)
220 {
221     assert(off < ctx->code_off);
222     return ctx->code->instrs + off;
223 }
224
225 static HRESULT push_instr_int(compiler_ctx_t *ctx, jsop_t op, LONG arg)
226 {
227     unsigned instr;
228
229     instr = push_instr(ctx, op);
230     if(!instr)
231         return E_OUTOFMEMORY;
232
233     instr_ptr(ctx, instr)->u.arg->lng = arg;
234     return S_OK;
235 }
236
237 static HRESULT push_instr_str(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
238 {
239     unsigned instr;
240     jsstr_t *str;
241
242     str = compiler_alloc_string(ctx, arg);
243     if(!str)
244         return E_OUTOFMEMORY;
245
246     instr = push_instr(ctx, op);
247     if(!instr)
248         return E_OUTOFMEMORY;
249
250     instr_ptr(ctx, instr)->u.arg->str = str;
251     return S_OK;
252 }
253
254 static HRESULT push_instr_bstr(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg)
255 {
256     unsigned instr;
257     WCHAR *str;
258
259     str = compiler_alloc_bstr(ctx, arg);
260     if(!str)
261         return E_OUTOFMEMORY;
262
263     instr = push_instr(ctx, op);
264     if(!instr)
265         return E_OUTOFMEMORY;
266
267     instr_ptr(ctx, instr)->u.arg->bstr = str;
268     return S_OK;
269 }
270
271 static HRESULT push_instr_bstr_uint(compiler_ctx_t *ctx, jsop_t op, const WCHAR *arg1, unsigned arg2)
272 {
273     unsigned instr;
274     WCHAR *str;
275
276     str = compiler_alloc_bstr(ctx, arg1);
277     if(!str)
278         return E_OUTOFMEMORY;
279
280     instr = push_instr(ctx, op);
281     if(!instr)
282         return E_OUTOFMEMORY;
283
284     instr_ptr(ctx, instr)->u.arg[0].bstr = str;
285     instr_ptr(ctx, instr)->u.arg[1].uint = arg2;
286     return S_OK;
287 }
288
289 static HRESULT push_instr_uint_str(compiler_ctx_t *ctx, jsop_t op, unsigned arg1, const WCHAR *arg2)
290 {
291     unsigned instr;
292     jsstr_t *str;
293
294     str = compiler_alloc_string(ctx, arg2);
295     if(!str)
296         return E_OUTOFMEMORY;
297
298     instr = push_instr(ctx, op);
299     if(!instr)
300         return E_OUTOFMEMORY;
301
302     instr_ptr(ctx, instr)->u.arg[0].uint = arg1;
303     instr_ptr(ctx, instr)->u.arg[1].str = str;
304     return S_OK;
305 }
306
307 static HRESULT push_instr_double(compiler_ctx_t *ctx, jsop_t op, double arg)
308 {
309     unsigned instr;
310
311     instr = push_instr(ctx, op);
312     if(!instr)
313         return E_OUTOFMEMORY;
314
315     instr_ptr(ctx, instr)->u.dbl = arg;
316     return S_OK;
317 }
318
319 static inline void set_arg_uint(compiler_ctx_t *ctx, unsigned instr, unsigned arg)
320 {
321     instr_ptr(ctx, instr)->u.arg->uint = arg;
322 }
323
324 static HRESULT push_instr_uint(compiler_ctx_t *ctx, jsop_t op, unsigned arg)
325 {
326     unsigned instr;
327
328     instr = push_instr(ctx, op);
329     if(!instr)
330         return E_OUTOFMEMORY;
331
332     set_arg_uint(ctx, instr, arg);
333     return S_OK;
334 }
335
336 static HRESULT compile_binary_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
337 {
338     HRESULT hres;
339
340     hres = compile_expression(ctx, expr->expression1);
341     if(FAILED(hres))
342         return hres;
343
344     hres = compile_expression(ctx, expr->expression2);
345     if(FAILED(hres))
346         return hres;
347
348     return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
349 }
350
351 static HRESULT compile_unary_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op)
352 {
353     HRESULT hres;
354
355     hres = compile_expression(ctx, expr->expression);
356     if(FAILED(hres))
357         return hres;
358
359     return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
360 }
361
362 /* ECMA-262 3rd Edition    11.2.1 */
363 static HRESULT compile_member_expression(compiler_ctx_t *ctx, member_expression_t *expr)
364 {
365     HRESULT hres;
366
367     hres = compile_expression(ctx, expr->expression);
368     if(FAILED(hres))
369         return hres;
370
371     return push_instr_bstr(ctx, OP_member, expr->identifier);
372 }
373
374 #define LABEL_FLAG 0x80000000
375
376 static unsigned alloc_label(compiler_ctx_t *ctx)
377 {
378     if(!ctx->labels_size) {
379         ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
380         if(!ctx->labels)
381             return 0;
382         ctx->labels_size = 8;
383     }else if(ctx->labels_size == ctx->labels_cnt) {
384         unsigned *new_labels;
385
386         new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
387         if(!new_labels)
388             return 0;
389
390         ctx->labels = new_labels;
391         ctx->labels_size *= 2;
392     }
393
394     return ctx->labels_cnt++ | LABEL_FLAG;
395 }
396
397 static void label_set_addr(compiler_ctx_t *ctx, unsigned label)
398 {
399     assert(label & LABEL_FLAG);
400     ctx->labels[label & ~LABEL_FLAG] = ctx->code_off;
401 }
402
403 static inline BOOL is_memberid_expr(expression_type_t type)
404 {
405     return type == EXPR_IDENT || type == EXPR_MEMBER || type == EXPR_ARRAY;
406 }
407
408 static HRESULT compile_memberid_expression(compiler_ctx_t *ctx, expression_t *expr, unsigned flags)
409 {
410     HRESULT hres = S_OK;
411
412     switch(expr->type) {
413     case EXPR_IDENT: {
414         identifier_expression_t *ident_expr = (identifier_expression_t*)expr;
415
416         hres = push_instr_bstr_uint(ctx, OP_identid, ident_expr->identifier, flags);
417         break;
418     }
419     case EXPR_ARRAY: {
420         binary_expression_t *array_expr = (binary_expression_t*)expr;
421
422         hres = compile_expression(ctx, array_expr->expression1);
423         if(FAILED(hres))
424             return hres;
425
426         hres = compile_expression(ctx, array_expr->expression2);
427         if(FAILED(hres))
428             return hres;
429
430         hres = push_instr_uint(ctx, OP_memberid, flags);
431         break;
432     }
433     case EXPR_MEMBER: {
434         member_expression_t *member_expr = (member_expression_t*)expr;
435
436         hres = compile_expression(ctx, member_expr->expression);
437         if(FAILED(hres))
438             return hres;
439
440         /* FIXME: Potential optimization */
441         hres = push_instr_str(ctx, OP_str, member_expr->identifier);
442         if(FAILED(hres))
443             return hres;
444
445         hres = push_instr_uint(ctx, OP_memberid, flags);
446         break;
447     }
448     default:
449         assert(0);
450     }
451
452     return hres;
453 }
454
455 static HRESULT compile_increment_expression(compiler_ctx_t *ctx, unary_expression_t *expr, jsop_t op, int n)
456 {
457     HRESULT hres;
458
459     if(!is_memberid_expr(expr->expression->type)) {
460         hres = compile_expression(ctx, expr->expression);
461         if(FAILED(hres))
462             return hres;
463
464         return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
465     }
466
467     hres = compile_memberid_expression(ctx, expr->expression, fdexNameEnsure);
468     if(FAILED(hres))
469         return hres;
470
471     return push_instr_int(ctx, op, n);
472 }
473
474 /* ECMA-262 3rd Edition    11.14 */
475 static HRESULT compile_comma_expression(compiler_ctx_t *ctx, binary_expression_t *expr)
476 {
477     HRESULT hres;
478
479     hres = compile_expression(ctx, expr->expression1);
480     if(FAILED(hres))
481         return hres;
482
483     if(!push_instr(ctx, OP_pop))
484         return E_OUTOFMEMORY;
485
486     return compile_expression(ctx, expr->expression2);
487 }
488
489 /* ECMA-262 3rd Edition    11.11 */
490 static HRESULT compile_logical_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
491 {
492     unsigned instr;
493     HRESULT hres;
494
495     hres = compile_expression(ctx, expr->expression1);
496     if(FAILED(hres))
497         return hres;
498
499     instr = push_instr(ctx, op);
500     if(!instr)
501         return E_OUTOFMEMORY;
502
503     hres = compile_expression(ctx, expr->expression2);
504     if(FAILED(hres))
505         return hres;
506
507     set_arg_uint(ctx, instr, ctx->code_off);
508     return S_OK;
509 }
510
511 /* ECMA-262 3rd Edition    11.12 */
512 static HRESULT compile_conditional_expression(compiler_ctx_t *ctx, conditional_expression_t *expr)
513 {
514     unsigned jmp_false, jmp_end;
515     HRESULT hres;
516
517     hres = compile_expression(ctx, expr->expression);
518     if(FAILED(hres))
519         return hres;
520
521     jmp_false = push_instr(ctx, OP_cnd_z);
522     if(!jmp_false)
523         return E_OUTOFMEMORY;
524
525     hres = compile_expression(ctx, expr->true_expression);
526     if(FAILED(hres))
527         return hres;
528
529     jmp_end = push_instr(ctx, OP_jmp);
530     if(!jmp_end)
531         return E_OUTOFMEMORY;
532
533     set_arg_uint(ctx, jmp_false, ctx->code_off);
534     if(!push_instr(ctx, OP_pop))
535         return E_OUTOFMEMORY;
536
537     hres = compile_expression(ctx, expr->false_expression);
538     if(FAILED(hres))
539         return hres;
540
541     set_arg_uint(ctx, jmp_end, ctx->code_off);
542     return S_OK;
543 }
544
545 static HRESULT compile_new_expression(compiler_ctx_t *ctx, call_expression_t *expr)
546 {
547     unsigned arg_cnt = 0;
548     argument_t *arg;
549     HRESULT hres;
550
551     hres = compile_expression(ctx, expr->expression);
552     if(FAILED(hres))
553         return hres;
554
555     for(arg = expr->argument_list; arg; arg = arg->next) {
556         hres = compile_expression(ctx, arg->expr);
557         if(FAILED(hres))
558             return hres;
559         arg_cnt++;
560     }
561
562     return push_instr_uint(ctx, OP_new, arg_cnt);
563 }
564
565 static HRESULT compile_call_expression(compiler_ctx_t *ctx, call_expression_t *expr, BOOL *no_ret)
566 {
567     unsigned arg_cnt = 0;
568     argument_t *arg;
569     unsigned instr;
570     jsop_t op;
571     HRESULT hres;
572
573     if(is_memberid_expr(expr->expression->type)) {
574         op = OP_call_member;
575         hres = compile_memberid_expression(ctx, expr->expression, 0);
576     }else {
577         op = OP_call;
578         hres = compile_expression(ctx, expr->expression);
579     }
580
581     if(FAILED(hres))
582         return hres;
583
584     for(arg = expr->argument_list; arg; arg = arg->next) {
585         hres = compile_expression(ctx, arg->expr);
586         if(FAILED(hres))
587             return hres;
588         arg_cnt++;
589     }
590
591     instr = push_instr(ctx, op);
592     if(!instr)
593         return E_OUTOFMEMORY;
594
595     instr_ptr(ctx, instr)->u.arg[0].uint = arg_cnt;
596     instr_ptr(ctx, instr)->u.arg[1].lng = no_ret == NULL;
597     if(no_ret)
598         *no_ret = TRUE;
599     return S_OK;
600 }
601
602 static HRESULT compile_delete_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
603 {
604     HRESULT hres;
605
606     switch(expr->expression->type) {
607     case EXPR_ARRAY: {
608         binary_expression_t *array_expr = (binary_expression_t*)expr->expression;
609
610         hres = compile_expression(ctx, array_expr->expression1);
611         if(FAILED(hres))
612             return hres;
613
614         hres = compile_expression(ctx, array_expr->expression2);
615         if(FAILED(hres))
616             return hres;
617
618         if(!push_instr(ctx, OP_delete))
619             return E_OUTOFMEMORY;
620         break;
621     }
622     case EXPR_MEMBER: {
623         member_expression_t *member_expr = (member_expression_t*)expr->expression;
624
625         hres = compile_expression(ctx, member_expr->expression);
626         if(FAILED(hres))
627             return hres;
628
629         /* FIXME: Potential optimization */
630         hres = push_instr_str(ctx, OP_str, member_expr->identifier);
631         if(FAILED(hres))
632             return hres;
633
634         if(!push_instr(ctx, OP_delete))
635             return E_OUTOFMEMORY;
636         break;
637     }
638     case EXPR_IDENT:
639         return push_instr_bstr(ctx, OP_delete_ident, ((identifier_expression_t*)expr->expression)->identifier);
640     default: {
641         const WCHAR fixmeW[] = {'F','I','X','M','E',0};
642
643         WARN("invalid delete, unimplemented exception message\n");
644
645         hres = compile_expression(ctx, expr->expression);
646         if(FAILED(hres))
647             return hres;
648
649         return push_instr_uint_str(ctx, OP_throw_type, JS_E_INVALID_DELETE, fixmeW);
650     }
651     }
652
653     return S_OK;
654 }
655
656 static HRESULT compile_assign_expression(compiler_ctx_t *ctx, binary_expression_t *expr, jsop_t op)
657 {
658     BOOL use_throw_path = FALSE;
659     unsigned arg_cnt = 0;
660     HRESULT hres;
661
662     if(expr->expression1->type == EXPR_CALL) {
663         call_expression_t *call_expr = (call_expression_t*)expr->expression1;
664         argument_t *arg;
665
666         if(op != OP_LAST) {
667             FIXME("op %d not supported on parametrized assign expressions\n", op);
668             return E_NOTIMPL;
669         }
670
671         if(is_memberid_expr(call_expr->expression->type) && call_expr->argument_list) {
672             hres = compile_memberid_expression(ctx, call_expr->expression, fdexNameEnsure);
673             if(FAILED(hres))
674                 return hres;
675
676             for(arg = call_expr->argument_list; arg; arg = arg->next) {
677                 hres = compile_expression(ctx, arg->expr);
678                 if(FAILED(hres))
679                     return hres;
680                 arg_cnt++;
681             }
682         }else {
683             use_throw_path = TRUE;
684         }
685     }else if(is_memberid_expr(expr->expression1->type)) {
686         hres = compile_memberid_expression(ctx, expr->expression1, fdexNameEnsure);
687         if(FAILED(hres))
688             return hres;
689     }else {
690         use_throw_path = TRUE;
691     }
692
693     if(use_throw_path) {
694         /* Illegal assignment: evaluate and throw */
695         hres = compile_expression(ctx, expr->expression1);
696         if(FAILED(hres))
697             return hres;
698
699         hres = compile_expression(ctx, expr->expression2);
700         if(FAILED(hres))
701             return hres;
702
703         if(op != OP_LAST && !push_instr(ctx, op))
704             return E_OUTOFMEMORY;
705
706         return push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
707     }
708
709     if(op != OP_LAST && !push_instr(ctx, OP_refval))
710         return E_OUTOFMEMORY;
711
712     hres = compile_expression(ctx, expr->expression2);
713     if(FAILED(hres))
714         return hres;
715
716     if(op != OP_LAST && !push_instr(ctx, op))
717         return E_OUTOFMEMORY;
718
719     if(arg_cnt)
720         return push_instr_uint(ctx, OP_assign_call, arg_cnt);
721
722     if(!push_instr(ctx, OP_assign))
723         return E_OUTOFMEMORY;
724
725     return S_OK;
726 }
727
728 static HRESULT compile_typeof_expression(compiler_ctx_t *ctx, unary_expression_t *expr)
729 {
730     jsop_t op;
731     HRESULT hres;
732
733     if(is_memberid_expr(expr->expression->type)) {
734         if(expr->expression->type == EXPR_IDENT)
735             return push_instr_bstr(ctx, OP_typeofident, ((identifier_expression_t*)expr->expression)->identifier);
736
737         op = OP_typeofid;
738         hres = compile_memberid_expression(ctx, expr->expression, 0);
739     }else {
740         op = OP_typeof;
741         hres = compile_expression(ctx, expr->expression);
742     }
743     if(FAILED(hres))
744         return hres;
745
746     return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
747 }
748
749 static HRESULT compile_literal(compiler_ctx_t *ctx, literal_t *literal)
750 {
751     switch(literal->type) {
752     case LT_BOOL:
753         return push_instr_int(ctx, OP_bool, literal->u.bval);
754     case LT_DOUBLE:
755         return push_instr_double(ctx, OP_double, literal->u.dval);
756     case LT_NULL:
757         return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
758     case LT_STRING:
759         return push_instr_str(ctx, OP_str, literal->u.wstr);
760     case LT_REGEXP: {
761         unsigned instr;
762         jsstr_t *str;
763
764         str = compiler_alloc_string_len(ctx, literal->u.regexp.str, literal->u.regexp.str_len);
765         if(!str)
766             return E_OUTOFMEMORY;
767
768         instr = push_instr(ctx, OP_regexp);
769         if(!instr)
770             return E_OUTOFMEMORY;
771
772         instr_ptr(ctx, instr)->u.arg[0].str = str;
773         instr_ptr(ctx, instr)->u.arg[1].uint = literal->u.regexp.flags;
774         return S_OK;
775     }
776     default:
777         assert(0);
778         return E_FAIL;
779     }
780 }
781
782 static HRESULT literal_as_bstr(compiler_ctx_t *ctx, literal_t *literal, BSTR *str)
783 {
784     switch(literal->type) {
785     case LT_STRING:
786         *str = compiler_alloc_bstr(ctx, literal->u.wstr);
787         break;
788     case LT_DOUBLE: {
789         jsstr_t *jsstr;
790         HRESULT hres;
791
792         hres = double_to_string(literal->u.dval, &jsstr);
793         if(FAILED(hres))
794             return hres;
795
796         *str = SysAllocStringLen(jsstr->str, jsstr_length(jsstr));
797         jsstr_release(jsstr);
798         break;
799     }
800     default:
801         assert(0);
802     }
803
804     return *str ? S_OK : E_OUTOFMEMORY;
805 }
806
807 static HRESULT compile_array_literal(compiler_ctx_t *ctx, array_literal_expression_t *expr)
808 {
809     unsigned i, elem_cnt = expr->length;
810     array_element_t *iter;
811     HRESULT hres;
812
813     for(iter = expr->element_list; iter; iter = iter->next) {
814         elem_cnt += iter->elision+1;
815
816         for(i=0; i < iter->elision; i++) {
817             if(!push_instr(ctx, OP_undefined))
818                 return E_OUTOFMEMORY;
819         }
820
821         hres = compile_expression(ctx, iter->expr);
822         if(FAILED(hres))
823             return hres;
824     }
825
826     for(i=0; i < expr->length; i++) {
827         if(!push_instr(ctx, OP_undefined))
828             return E_OUTOFMEMORY;
829     }
830
831     return push_instr_uint(ctx, OP_carray, elem_cnt);
832 }
833
834 static HRESULT compile_object_literal(compiler_ctx_t *ctx, property_value_expression_t *expr)
835 {
836     prop_val_t *iter;
837     unsigned instr;
838     BSTR name;
839     HRESULT hres;
840
841     if(!push_instr(ctx, OP_new_obj))
842         return E_OUTOFMEMORY;
843
844     for(iter = expr->property_list; iter; iter = iter->next) {
845         hres = literal_as_bstr(ctx, iter->name, &name);
846         if(FAILED(hres))
847             return hres;
848
849         hres = compile_expression(ctx, iter->value);
850         if(FAILED(hres))
851             return hres;
852
853         instr = push_instr(ctx, OP_obj_prop);
854         if(!instr)
855             return E_OUTOFMEMORY;
856
857         instr_ptr(ctx, instr)->u.arg->bstr = name;
858     }
859
860     return S_OK;
861 }
862
863 static HRESULT compile_function_expression(compiler_ctx_t *ctx, function_expression_t *expr)
864 {
865     ctx->func_tail = ctx->func_tail ? (ctx->func_tail->next = expr) : (ctx->func_head = expr);
866
867     /* FIXME: not exactly right */
868     if(expr->identifier) {
869         ctx->func->func_cnt++;
870         return push_instr_bstr(ctx, OP_ident, expr->identifier);
871     }
872
873     return push_instr_uint(ctx, OP_func, ctx->func->func_cnt++);
874 }
875
876 static HRESULT compile_expression_noret(compiler_ctx_t *ctx, expression_t *expr, BOOL *no_ret)
877 {
878     switch(expr->type) {
879     case EXPR_ADD:
880         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
881     case EXPR_AND:
882         return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_z);
883     case EXPR_ARRAY:
884         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_array);
885     case EXPR_ARRAYLIT:
886         return compile_array_literal(ctx, (array_literal_expression_t*)expr);
887     case EXPR_ASSIGN:
888         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_LAST);
889     case EXPR_ASSIGNADD:
890         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_add);
891     case EXPR_ASSIGNAND:
892         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_and);
893     case EXPR_ASSIGNSUB:
894         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_sub);
895     case EXPR_ASSIGNMUL:
896         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mul);
897     case EXPR_ASSIGNDIV:
898         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_div);
899     case EXPR_ASSIGNMOD:
900         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_mod);
901     case EXPR_ASSIGNOR:
902         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_or);
903     case EXPR_ASSIGNLSHIFT:
904         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_lshift);
905     case EXPR_ASSIGNRSHIFT:
906         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift);
907     case EXPR_ASSIGNRRSHIFT:
908         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
909     case EXPR_ASSIGNXOR:
910         return compile_assign_expression(ctx, (binary_expression_t*)expr, OP_xor);
911     case EXPR_BAND:
912         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
913     case EXPR_BITNEG:
914         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_bneg);
915     case EXPR_BOR:
916         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
917     case EXPR_CALL:
918         return compile_call_expression(ctx, (call_expression_t*)expr, no_ret);
919     case EXPR_COMMA:
920         return compile_comma_expression(ctx, (binary_expression_t*)expr);
921     case EXPR_COND:
922         return compile_conditional_expression(ctx, (conditional_expression_t*)expr);
923     case EXPR_DELETE:
924         return compile_delete_expression(ctx, (unary_expression_t*)expr);
925     case EXPR_DIV:
926         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
927     case EXPR_EQ:
928         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq);
929     case EXPR_EQEQ:
930         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eq2);
931     case EXPR_FUNC:
932         return compile_function_expression(ctx, (function_expression_t*)expr);
933     case EXPR_GREATER:
934         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
935     case EXPR_GREATEREQ:
936         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
937     case EXPR_IDENT:
938         return push_instr_bstr(ctx, OP_ident, ((identifier_expression_t*)expr)->identifier);
939     case EXPR_IN:
940         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_in);
941     case EXPR_INSTANCEOF:
942         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_instanceof);
943     case EXPR_LESS:
944         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
945     case EXPR_LESSEQ:
946         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
947     case EXPR_LITERAL:
948         return compile_literal(ctx, ((literal_expression_t*)expr)->literal);
949     case EXPR_LOGNEG:
950         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
951     case EXPR_LSHIFT:
952         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lshift);
953     case EXPR_MEMBER:
954         return compile_member_expression(ctx, (member_expression_t*)expr);
955     case EXPR_MINUS:
956         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_minus);
957     case EXPR_MOD:
958         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
959     case EXPR_MUL:
960         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
961     case EXPR_NEW:
962         return compile_new_expression(ctx, (call_expression_t*)expr);
963     case EXPR_NOTEQ:
964         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq);
965     case EXPR_NOTEQEQ:
966         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_neq2);
967     case EXPR_OR:
968         return compile_logical_expression(ctx, (binary_expression_t*)expr, OP_cnd_nz);
969     case EXPR_PLUS:
970         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_tonum);
971     case EXPR_POSTDEC:
972         return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, -1);
973     case EXPR_POSTINC:
974         return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_postinc, 1);
975     case EXPR_PREDEC:
976         return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, -1);
977     case EXPR_PREINC:
978         return compile_increment_expression(ctx, (unary_expression_t*)expr, OP_preinc, 1);
979     case EXPR_PROPVAL:
980         return compile_object_literal(ctx, (property_value_expression_t*)expr);
981     case EXPR_RSHIFT:
982         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift);
983     case EXPR_RRSHIFT:
984         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_rshift2);
985     case EXPR_SUB:
986         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
987     case EXPR_THIS:
988         return push_instr(ctx, OP_this) ? S_OK : E_OUTOFMEMORY;
989     case EXPR_TYPEOF:
990         return compile_typeof_expression(ctx, (unary_expression_t*)expr);
991     case EXPR_VOID:
992         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_void);
993     case EXPR_BXOR:
994         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
995     default:
996         assert(0);
997     }
998
999     return S_OK;
1000 }
1001
1002 static HRESULT compile_expression(compiler_ctx_t *ctx, expression_t *expr)
1003 {
1004     return compile_expression_noret(ctx, expr, NULL);
1005 }
1006
1007 static inline BOOL is_loop_statement(statement_type_t type)
1008 {
1009     return type == STAT_FOR || type == STAT_FORIN || type == STAT_WHILE;
1010 }
1011
1012 /* ECMA-262 3rd Edition    12.1 */
1013 static HRESULT compile_block_statement(compiler_ctx_t *ctx, statement_t *iter)
1014 {
1015     HRESULT hres;
1016
1017     /* FIXME: do it only if needed */
1018     if(!iter)
1019         return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
1020
1021     while(1) {
1022         hres = compile_statement(ctx, NULL, iter);
1023         if(FAILED(hres))
1024             return hres;
1025
1026         iter = iter->next;
1027         if(!iter)
1028             break;
1029
1030         if(!push_instr(ctx, OP_pop))
1031             return E_OUTOFMEMORY;
1032     }
1033
1034     return S_OK;
1035 }
1036
1037 /* ECMA-262 3rd Edition    12.2 */
1038 static HRESULT compile_variable_list(compiler_ctx_t *ctx, variable_declaration_t *list)
1039 {
1040     variable_declaration_t *iter;
1041     HRESULT hres;
1042
1043     assert(list != NULL);
1044
1045     if(ctx->var_tail)
1046         ctx->var_tail->global_next = list;
1047     else
1048         ctx->var_head = list;
1049
1050     for(iter = list; iter; iter = iter->next) {
1051         ctx->func->var_cnt++;
1052         iter->global_next = iter->next;
1053         if(!iter->next)
1054             ctx->var_tail = iter;
1055
1056         if(!iter->expr)
1057             continue;
1058
1059         hres = compile_expression(ctx, iter->expr);
1060         if(FAILED(hres))
1061             return hres;
1062
1063         hres = push_instr_bstr(ctx, OP_var_set, iter->identifier);
1064         if(FAILED(hres))
1065             return hres;
1066     }
1067
1068     return S_OK;
1069 }
1070
1071 /* ECMA-262 3rd Edition    12.2 */
1072 static HRESULT compile_var_statement(compiler_ctx_t *ctx, var_statement_t *stat)
1073 {
1074     HRESULT hres;
1075
1076     hres = compile_variable_list(ctx, stat->variable_list);
1077     if(FAILED(hres))
1078         return hres;
1079
1080     return push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY;
1081 }
1082
1083 /* ECMA-262 3rd Edition    12.4 */
1084 static HRESULT compile_expression_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1085 {
1086     BOOL no_ret = FALSE;
1087     HRESULT hres;
1088
1089     hres = compile_expression_noret(ctx, stat->expr, &no_ret);
1090     if(FAILED(hres))
1091         return hres;
1092
1093     /* FIXME: that's a big potential optimization */
1094     if(no_ret && !push_instr(ctx, OP_undefined))
1095         return E_OUTOFMEMORY;
1096
1097     return S_OK;
1098 }
1099
1100 /* ECMA-262 3rd Edition    12.5 */
1101 static HRESULT compile_if_statement(compiler_ctx_t *ctx, if_statement_t *stat)
1102 {
1103     unsigned jmp_else, jmp_end;
1104     HRESULT hres;
1105
1106     hres = compile_expression(ctx, stat->expr);
1107     if(FAILED(hres))
1108         return hres;
1109
1110     jmp_else = push_instr(ctx, OP_jmp_z);
1111     if(!jmp_else)
1112         return E_OUTOFMEMORY;
1113
1114     hres = compile_statement(ctx, NULL, stat->if_stat);
1115     if(FAILED(hres))
1116         return hres;
1117
1118     jmp_end = push_instr(ctx, OP_jmp);
1119     if(!jmp_end)
1120         return E_OUTOFMEMORY;
1121
1122     set_arg_uint(ctx, jmp_else, ctx->code_off);
1123
1124     if(stat->else_stat) {
1125         hres = compile_statement(ctx, NULL, stat->else_stat);
1126         if(FAILED(hres))
1127             return hres;
1128     }else {
1129         /* FIXME: We could sometimes avoid it */
1130         if(!push_instr(ctx, OP_undefined))
1131             return E_OUTOFMEMORY;
1132     }
1133
1134     set_arg_uint(ctx, jmp_end, ctx->code_off);
1135     return S_OK;
1136 }
1137
1138 /* ECMA-262 3rd Edition    12.6.2 */
1139 static HRESULT compile_while_statement(compiler_ctx_t *ctx, while_statement_t *stat)
1140 {
1141     statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1142     unsigned jmp_off;
1143     HRESULT hres;
1144
1145     stat_ctx.break_label = alloc_label(ctx);
1146     if(!stat_ctx.break_label)
1147         return E_OUTOFMEMORY;
1148
1149     stat_ctx.continue_label = alloc_label(ctx);
1150     if(!stat_ctx.continue_label)
1151         return E_OUTOFMEMORY;
1152
1153     if(!stat->do_while) {
1154         /* FIXME: avoid */
1155         if(!push_instr(ctx, OP_undefined))
1156             return E_OUTOFMEMORY;
1157
1158         jmp_off = ctx->code_off;
1159         label_set_addr(ctx, stat_ctx.continue_label);
1160         hres = compile_expression(ctx, stat->expr);
1161         if(FAILED(hres))
1162             return hres;
1163
1164         hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1165         if(FAILED(hres))
1166             return hres;
1167
1168         if(!push_instr(ctx, OP_pop))
1169             return E_OUTOFMEMORY;
1170     }else {
1171         jmp_off = ctx->code_off;
1172     }
1173
1174     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1175     if(FAILED(hres))
1176         return hres;
1177
1178     if(stat->do_while) {
1179         label_set_addr(ctx, stat_ctx.continue_label);
1180         hres = compile_expression(ctx, stat->expr);
1181         if(FAILED(hres))
1182             return hres;
1183
1184         hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1185         if(FAILED(hres))
1186             return hres;
1187
1188         if(!push_instr(ctx, OP_pop))
1189             return E_OUTOFMEMORY;
1190     }
1191
1192     hres = push_instr_uint(ctx, OP_jmp, jmp_off);
1193     if(FAILED(hres))
1194         return hres;
1195
1196     label_set_addr(ctx, stat_ctx.break_label);
1197     return S_OK;
1198 }
1199
1200 /* ECMA-262 3rd Edition    12.6.3 */
1201 static HRESULT compile_for_statement(compiler_ctx_t *ctx, for_statement_t *stat)
1202 {
1203     statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1204     unsigned expr_off;
1205     HRESULT hres;
1206
1207     if(stat->variable_list) {
1208         hres = compile_variable_list(ctx, stat->variable_list);
1209         if(FAILED(hres))
1210             return hres;
1211     }else if(stat->begin_expr) {
1212         BOOL no_ret = FALSE;
1213
1214         hres = compile_expression_noret(ctx, stat->begin_expr, &no_ret);
1215         if(FAILED(hres))
1216             return hres;
1217         if(!no_ret && !push_instr(ctx, OP_pop))
1218             return E_OUTOFMEMORY;
1219     }
1220
1221     stat_ctx.break_label = alloc_label(ctx);
1222     if(!stat_ctx.break_label)
1223         return E_OUTOFMEMORY;
1224
1225     stat_ctx.continue_label = alloc_label(ctx);
1226     if(!stat_ctx.continue_label)
1227         return E_OUTOFMEMORY;
1228
1229     /* FIXME: avoid */
1230     if(!push_instr(ctx, OP_undefined))
1231         return E_OUTOFMEMORY;
1232
1233     expr_off = ctx->code_off;
1234
1235     if(stat->expr) {
1236         hres = compile_expression(ctx, stat->expr);
1237         if(FAILED(hres))
1238             return hres;
1239
1240         hres = push_instr_uint(ctx, OP_jmp_z, stat_ctx.break_label);
1241         if(FAILED(hres))
1242             return hres;
1243     }
1244
1245     if(!push_instr(ctx, OP_pop))
1246         return E_OUTOFMEMORY;
1247
1248     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1249     if(FAILED(hres))
1250         return hres;
1251
1252     label_set_addr(ctx, stat_ctx.continue_label);
1253
1254     if(stat->end_expr) {
1255         BOOL no_ret = FALSE;
1256
1257         hres = compile_expression_noret(ctx, stat->end_expr, &no_ret);
1258         if(FAILED(hres))
1259             return hres;
1260
1261         if(!no_ret && !push_instr(ctx, OP_pop))
1262             return E_OUTOFMEMORY;
1263     }
1264
1265     hres = push_instr_uint(ctx, OP_jmp, expr_off);
1266     if(FAILED(hres))
1267         return hres;
1268
1269     label_set_addr(ctx, stat_ctx.break_label);
1270     return S_OK;
1271 }
1272
1273 /* ECMA-262 3rd Edition    12.6.4 */
1274 static HRESULT compile_forin_statement(compiler_ctx_t *ctx, forin_statement_t *stat)
1275 {
1276     statement_ctx_t stat_ctx = {4, FALSE, FALSE};
1277     HRESULT hres;
1278
1279     if(stat->variable) {
1280         hres = compile_variable_list(ctx, stat->variable);
1281         if(FAILED(hres))
1282             return hres;
1283     }
1284
1285     stat_ctx.break_label = alloc_label(ctx);
1286     if(!stat_ctx.break_label)
1287         return E_OUTOFMEMORY;
1288
1289     stat_ctx.continue_label = alloc_label(ctx);
1290     if(!stat_ctx.continue_label)
1291         return E_OUTOFMEMORY;
1292
1293     hres = compile_expression(ctx, stat->in_expr);
1294     if(FAILED(hres))
1295         return hres;
1296
1297     if(stat->variable) {
1298         hres = push_instr_bstr_uint(ctx, OP_identid, stat->variable->identifier, fdexNameEnsure);
1299         if(FAILED(hres))
1300             return hres;
1301     }else if(is_memberid_expr(stat->expr->type)) {
1302         hres = compile_memberid_expression(ctx, stat->expr, fdexNameEnsure);
1303         if(FAILED(hres))
1304             return hres;
1305     }else {
1306         hres = push_instr_uint(ctx, OP_throw_ref, JS_E_ILLEGAL_ASSIGN);
1307         if(FAILED(hres))
1308             return hres;
1309
1310         /* FIXME: compile statement anyways when we depend on compiler to check errors */
1311         return S_OK;
1312     }
1313
1314     hres = push_instr_int(ctx, OP_int, DISPID_STARTENUM);
1315     if(FAILED(hres))
1316         return hres;
1317
1318     /* FIXME: avoid */
1319     if(!push_instr(ctx, OP_undefined))
1320         return E_OUTOFMEMORY;
1321
1322     label_set_addr(ctx, stat_ctx.continue_label);
1323     hres = push_instr_uint(ctx, OP_forin, stat_ctx.break_label);
1324     if(FAILED(hres))
1325         return E_OUTOFMEMORY;
1326
1327     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1328     if(FAILED(hres))
1329         return hres;
1330
1331     hres = push_instr_uint(ctx, OP_jmp, stat_ctx.continue_label);
1332     if(FAILED(hres))
1333         return hres;
1334
1335     label_set_addr(ctx, stat_ctx.break_label);
1336     return S_OK;
1337 }
1338
1339 static HRESULT pop_to_stat(compiler_ctx_t *ctx, BOOL var_stack, BOOL scope_stack, statement_ctx_t *stat_ctx)
1340 {
1341     unsigned stack_pop = 0;
1342     statement_ctx_t *iter;
1343
1344     for(iter = ctx->stat_ctx; iter != stat_ctx; iter = iter->next) {
1345         if(scope_stack) {
1346             if(iter->using_scope && !push_instr(ctx, OP_pop_scope))
1347                 return E_OUTOFMEMORY;
1348             if(iter->using_except && !push_instr(ctx, OP_pop_except))
1349                 return E_OUTOFMEMORY;
1350         }
1351         stack_pop += iter->stack_use;
1352     }
1353
1354     if(var_stack) {
1355         /* FIXME: optimize */
1356         while(stack_pop--) {
1357             if(!push_instr(ctx, OP_pop))
1358                 return E_OUTOFMEMORY;
1359         }
1360     }
1361
1362
1363     return S_OK;
1364 }
1365
1366 /* ECMA-262 3rd Edition    12.7 */
1367 static HRESULT compile_continue_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1368 {
1369     statement_ctx_t *pop_ctx;
1370     HRESULT hres;
1371
1372     if(stat->identifier) {
1373         statement_t *label_stat;
1374         statement_ctx_t *iter;
1375
1376         pop_ctx = NULL;
1377
1378         for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1379             if(iter->continue_label)
1380                 pop_ctx = iter;
1381             if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier))
1382                 break;
1383         }
1384
1385         if(!iter) {
1386             WARN("Label not found\n");
1387             return JS_E_LABEL_NOT_FOUND;
1388         }
1389
1390         /* Labelled continue are allowed only on loops */
1391         for(label_stat = iter->labelled_stat->statement;
1392             label_stat->type == STAT_LABEL;
1393             label_stat = ((labelled_statement_t*)label_stat)->statement);
1394         if(!is_loop_statement(label_stat->type)) {
1395             WARN("Label is not a loop\n");
1396             return JS_E_INVALID_CONTINUE;
1397         }
1398
1399         assert(pop_ctx != NULL);
1400     }else {
1401         for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1402             if(pop_ctx->continue_label)
1403                 break;
1404         }
1405
1406         if(!pop_ctx) {
1407             WARN("continue outside loop\n");
1408             return JS_E_INVALID_CONTINUE;
1409         }
1410     }
1411
1412     hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx);
1413     if(FAILED(hres))
1414         return hres;
1415
1416     if(!push_instr(ctx, OP_undefined))
1417         return E_OUTOFMEMORY;
1418
1419     return push_instr_uint(ctx, OP_jmp, pop_ctx->continue_label);
1420 }
1421
1422 /* ECMA-262 3rd Edition    12.8 */
1423 static HRESULT compile_break_statement(compiler_ctx_t *ctx, branch_statement_t *stat)
1424 {
1425     statement_ctx_t *pop_ctx;
1426     HRESULT hres;
1427
1428     if(stat->identifier) {
1429         for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1430             if(pop_ctx->labelled_stat && !strcmpW(pop_ctx->labelled_stat->identifier, stat->identifier)) {
1431                 assert(pop_ctx->break_label);
1432                 break;
1433             }
1434         }
1435
1436         if(!pop_ctx) {
1437             WARN("Label not found\n");
1438             return JS_E_LABEL_NOT_FOUND;
1439         }
1440     }else {
1441         for(pop_ctx = ctx->stat_ctx; pop_ctx; pop_ctx = pop_ctx->next) {
1442             if(pop_ctx->break_label && !pop_ctx->labelled_stat)
1443                 break;
1444         }
1445
1446         if(!pop_ctx) {
1447             WARN("Break outside loop\n");
1448             return JS_E_INVALID_BREAK;
1449         }
1450     }
1451
1452     hres = pop_to_stat(ctx, TRUE, TRUE, pop_ctx->next);
1453     if(FAILED(hres))
1454         return hres;
1455
1456     if(!push_instr(ctx, OP_undefined))
1457         return E_OUTOFMEMORY;
1458
1459     return push_instr_uint(ctx, OP_jmp, pop_ctx->break_label);
1460 }
1461
1462 /* ECMA-262 3rd Edition    12.9 */
1463 static HRESULT compile_return_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1464 {
1465     HRESULT hres;
1466
1467     hres = pop_to_stat(ctx, TRUE, FALSE, NULL);
1468     if(FAILED(hres))
1469         return hres;
1470
1471     if(stat->expr) {
1472         hres = compile_expression(ctx, stat->expr);
1473         if(FAILED(hres))
1474             return hres;
1475     }
1476
1477     hres = pop_to_stat(ctx, FALSE, TRUE, NULL);
1478     if(FAILED(hres))
1479         return hres;
1480
1481     return push_instr(ctx, OP_ret) ? S_OK : E_OUTOFMEMORY;
1482 }
1483
1484 /* ECMA-262 3rd Edition    12.10 */
1485 static HRESULT compile_with_statement(compiler_ctx_t *ctx, with_statement_t *stat)
1486 {
1487     statement_ctx_t stat_ctx = {0, TRUE, FALSE};
1488     HRESULT hres;
1489
1490     hres = compile_expression(ctx, stat->expr);
1491     if(FAILED(hres))
1492         return hres;
1493
1494     if(!push_instr(ctx, OP_push_scope))
1495         return E_OUTOFMEMORY;
1496
1497     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1498     if(FAILED(hres))
1499         return hres;
1500
1501     if(!push_instr(ctx, OP_pop_scope))
1502         return E_OUTOFMEMORY;
1503
1504     return S_OK;
1505 }
1506
1507 /* ECMA-262 3rd Edition    12.10 */
1508 static HRESULT compile_labelled_statement(compiler_ctx_t *ctx, labelled_statement_t *stat)
1509 {
1510     statement_ctx_t stat_ctx = {0, FALSE, FALSE, 0, 0, stat}, *iter;
1511     HRESULT hres;
1512
1513     for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1514         if(iter->labelled_stat && !strcmpW(iter->labelled_stat->identifier, stat->identifier)) {
1515             WARN("Label %s redefined\n", debugstr_w(stat->identifier));
1516             return JS_E_LABEL_REDEFINED;
1517         }
1518     }
1519
1520     /* Labelled breaks are allowed for any labelled statements, not only loops (violating spec) */
1521     stat_ctx.break_label = alloc_label(ctx);
1522     if(!stat_ctx.break_label)
1523         return E_OUTOFMEMORY;
1524
1525     hres = compile_statement(ctx, &stat_ctx, stat->statement);
1526     if(FAILED(hres))
1527         return hres;
1528
1529     label_set_addr(ctx, stat_ctx.break_label);
1530     return S_OK;
1531 }
1532
1533 /* ECMA-262 3rd Edition    12.13 */
1534 static HRESULT compile_switch_statement(compiler_ctx_t *ctx, switch_statement_t *stat)
1535 {
1536     statement_ctx_t stat_ctx = {0, FALSE, FALSE};
1537     unsigned case_cnt = 0, *case_jmps, i, default_jmp;
1538     BOOL have_default = FALSE;
1539     statement_t *stat_iter;
1540     case_clausule_t *iter;
1541     HRESULT hres;
1542
1543     hres = compile_expression(ctx, stat->expr);
1544     if(FAILED(hres))
1545         return hres;
1546
1547     stat_ctx.break_label = alloc_label(ctx);
1548     if(!stat_ctx.break_label)
1549         return E_OUTOFMEMORY;
1550
1551     for(iter = stat->case_list; iter; iter = iter->next) {
1552         if(iter->expr)
1553             case_cnt++;
1554     }
1555
1556     case_jmps = heap_alloc(case_cnt * sizeof(*case_jmps));
1557     if(!case_jmps)
1558         return E_OUTOFMEMORY;
1559
1560     i = 0;
1561     for(iter = stat->case_list; iter; iter = iter->next) {
1562         if(!iter->expr) {
1563             have_default = TRUE;
1564             continue;
1565         }
1566
1567         hres = compile_expression(ctx, iter->expr);
1568         if(FAILED(hres))
1569             break;
1570
1571         case_jmps[i] = push_instr(ctx, OP_case);
1572         if(!case_jmps[i]) {
1573             hres = E_OUTOFMEMORY;
1574             break;
1575         }
1576         i++;
1577     }
1578
1579     if(SUCCEEDED(hres)) {
1580         if(push_instr(ctx, OP_pop)) {
1581             default_jmp = push_instr(ctx, OP_jmp);
1582             if(!default_jmp)
1583                 hres = E_OUTOFMEMORY;
1584         }else {
1585             hres = E_OUTOFMEMORY;
1586         }
1587     }
1588
1589     if(FAILED(hres)) {
1590         heap_free(case_jmps);
1591         return hres;
1592     }
1593
1594     i = 0;
1595     for(iter = stat->case_list; iter; iter = iter->next) {
1596         while(iter->next && iter->next->stat == iter->stat) {
1597             set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1598             iter = iter->next;
1599         }
1600
1601         set_arg_uint(ctx, iter->expr ? case_jmps[i++] : default_jmp, ctx->code_off);
1602
1603         if(iter->stat) {
1604             for(stat_iter = iter->stat; stat_iter && (!iter->next || iter->next->stat != stat_iter);
1605                 stat_iter = stat_iter->next) {
1606                 hres = compile_statement(ctx, &stat_ctx, stat_iter);
1607                 if(FAILED(hres))
1608                     break;
1609
1610                 if(stat_iter->next && !push_instr(ctx, OP_pop)) {
1611                     hres = E_OUTOFMEMORY;
1612                     break;
1613                 }
1614             }
1615             if(FAILED(hres))
1616                 break;
1617         }else {
1618             if(!push_instr(ctx, OP_undefined)) {
1619                 hres = E_OUTOFMEMORY;
1620                 break;
1621             }
1622         }
1623     }
1624
1625     heap_free(case_jmps);
1626     if(FAILED(hres))
1627         return hres;
1628     assert(i == case_cnt);
1629
1630     if(!have_default) {
1631         hres = push_instr_uint(ctx, OP_jmp, stat_ctx.break_label);
1632         if(FAILED(hres))
1633             return hres;
1634         set_arg_uint(ctx, default_jmp, ctx->code_off);
1635         if(!push_instr(ctx, OP_undefined))
1636             return E_OUTOFMEMORY;
1637     }
1638
1639     label_set_addr(ctx, stat_ctx.break_label);
1640     return S_OK;
1641 }
1642
1643 /* ECMA-262 3rd Edition    12.13 */
1644 static HRESULT compile_throw_statement(compiler_ctx_t *ctx, expression_statement_t *stat)
1645 {
1646     HRESULT hres;
1647
1648     hres = compile_expression(ctx, stat->expr);
1649     if(FAILED(hres))
1650         return hres;
1651
1652     return push_instr(ctx, OP_throw) ? S_OK : E_OUTOFMEMORY;
1653 }
1654
1655 /* ECMA-262 3rd Edition    12.14 */
1656 static HRESULT compile_try_statement(compiler_ctx_t *ctx, try_statement_t *stat)
1657 {
1658     statement_ctx_t try_ctx = {0, FALSE, TRUE}, catch_ctx = {0, TRUE, FALSE};
1659     statement_ctx_t finally_ctx = {2, FALSE, FALSE};
1660     unsigned push_except;
1661     BSTR ident;
1662     HRESULT hres;
1663
1664     push_except = push_instr(ctx, OP_push_except);
1665     if(!push_except)
1666         return E_OUTOFMEMORY;
1667
1668     if(stat->catch_block) {
1669         ident = compiler_alloc_bstr(ctx, stat->catch_block->identifier);
1670         if(!ident)
1671             return E_OUTOFMEMORY;
1672     }else {
1673         ident = NULL;
1674     }
1675
1676     instr_ptr(ctx, push_except)->u.arg[1].bstr = ident;
1677
1678     if(!stat->catch_block)
1679         try_ctx.stack_use = 2;
1680
1681     hres = compile_statement(ctx, &try_ctx, stat->try_statement);
1682     if(FAILED(hres))
1683         return hres;
1684
1685     if(!push_instr(ctx, OP_pop_except))
1686         return E_OUTOFMEMORY;
1687
1688     if(stat->catch_block) {
1689         unsigned jmp_finally;
1690
1691         jmp_finally = push_instr(ctx, OP_jmp);
1692         if(!jmp_finally)
1693             return E_OUTOFMEMORY;
1694
1695         instr_ptr(ctx, push_except)->u.arg[0].uint = ctx->code_off;
1696
1697         hres = compile_statement(ctx, &catch_ctx, stat->catch_block->statement);
1698         if(FAILED(hres))
1699             return hres;
1700
1701         if(!push_instr(ctx, OP_pop_scope))
1702             return E_OUTOFMEMORY;
1703
1704         set_arg_uint(ctx, jmp_finally, ctx->code_off);
1705     }else {
1706         set_arg_uint(ctx, push_except, ctx->code_off);
1707     }
1708
1709     if(stat->finally_statement) {
1710         /* FIXME: avoid */
1711         if(!push_instr(ctx, OP_pop))
1712             return E_OUTOFMEMORY;
1713
1714         hres = compile_statement(ctx, stat->catch_block ? NULL : &finally_ctx, stat->finally_statement);
1715         if(FAILED(hres))
1716             return hres;
1717
1718         if(!stat->catch_block && !push_instr(ctx, OP_end_finally))
1719             return E_OUTOFMEMORY;
1720     }
1721
1722     return S_OK;
1723 }
1724
1725 static HRESULT compile_statement(compiler_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1726 {
1727     HRESULT hres;
1728
1729     if(stat_ctx) {
1730         stat_ctx->next = ctx->stat_ctx;
1731         ctx->stat_ctx = stat_ctx;
1732     }
1733
1734     switch(stat->type) {
1735     case STAT_BLOCK:
1736         hres = compile_block_statement(ctx, ((block_statement_t*)stat)->stat_list);
1737         break;
1738     case STAT_BREAK:
1739         hres = compile_break_statement(ctx, (branch_statement_t*)stat);
1740         break;
1741     case STAT_CONTINUE:
1742         hres = compile_continue_statement(ctx, (branch_statement_t*)stat);
1743         break;
1744     case STAT_EMPTY:
1745         hres = push_instr(ctx, OP_undefined) ? S_OK : E_OUTOFMEMORY; /* FIXME */
1746         break;
1747     case STAT_EXPR:
1748         hres = compile_expression_statement(ctx, (expression_statement_t*)stat);
1749         break;
1750     case STAT_FOR:
1751         hres = compile_for_statement(ctx, (for_statement_t*)stat);
1752         break;
1753     case STAT_FORIN:
1754         hres = compile_forin_statement(ctx, (forin_statement_t*)stat);
1755         break;
1756     case STAT_IF:
1757         hres = compile_if_statement(ctx, (if_statement_t*)stat);
1758         break;
1759     case STAT_LABEL:
1760         hres = compile_labelled_statement(ctx, (labelled_statement_t*)stat);
1761         break;
1762     case STAT_RETURN:
1763         hres = compile_return_statement(ctx, (expression_statement_t*)stat);
1764         break;
1765     case STAT_SWITCH:
1766         hres = compile_switch_statement(ctx, (switch_statement_t*)stat);
1767         break;
1768     case STAT_THROW:
1769         hres = compile_throw_statement(ctx, (expression_statement_t*)stat);
1770         break;
1771     case STAT_TRY:
1772         hres = compile_try_statement(ctx, (try_statement_t*)stat);
1773         break;
1774     case STAT_VAR:
1775         hres = compile_var_statement(ctx, (var_statement_t*)stat);
1776         break;
1777     case STAT_WHILE:
1778         hres = compile_while_statement(ctx, (while_statement_t*)stat);
1779         break;
1780     case STAT_WITH:
1781         hres = compile_with_statement(ctx, (with_statement_t*)stat);
1782         break;
1783     default:
1784         assert(0);
1785         hres = E_FAIL;
1786     }
1787
1788     if(stat_ctx) {
1789         assert(ctx->stat_ctx == stat_ctx);
1790         ctx->stat_ctx = stat_ctx->next;
1791     }
1792
1793     return hres;
1794 }
1795
1796 static void resolve_labels(compiler_ctx_t *ctx, unsigned off)
1797 {
1798     instr_t *instr;
1799
1800     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->code_off; instr++) {
1801         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->u.arg->uint & LABEL_FLAG)) {
1802             assert((instr->u.arg->uint & ~LABEL_FLAG) < ctx->labels_cnt);
1803             instr->u.arg->uint = ctx->labels[instr->u.arg->uint & ~LABEL_FLAG];
1804         }
1805         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1806     }
1807
1808     ctx->labels_cnt = 0;
1809 }
1810
1811 void release_bytecode(bytecode_t *code)
1812 {
1813     unsigned i;
1814
1815     if(--code->ref)
1816         return;
1817
1818     for(i=0; i < code->bstr_cnt; i++)
1819         SysFreeString(code->bstr_pool[i]);
1820     for(i=0; i < code->str_cnt; i++)
1821         jsstr_release(code->str_pool[i]);
1822
1823     heap_free(code->source);
1824     jsheap_free(&code->heap);
1825     heap_free(code->bstr_pool);
1826     heap_free(code->str_pool);
1827     heap_free(code->instrs);
1828     heap_free(code);
1829 }
1830
1831 static HRESULT init_code(compiler_ctx_t *compiler, const WCHAR *source)
1832 {
1833     compiler->code = heap_alloc_zero(sizeof(bytecode_t));
1834     if(!compiler->code)
1835         return E_OUTOFMEMORY;
1836
1837     compiler->code->ref = 1;
1838     jsheap_init(&compiler->code->heap);
1839
1840     compiler->code->source = heap_strdupW(source);
1841     if(!compiler->code->source) {
1842         release_bytecode(compiler->code);
1843         return E_OUTOFMEMORY;
1844     }
1845
1846     compiler->code->instrs = heap_alloc(64 * sizeof(instr_t));
1847     if(!compiler->code->instrs) {
1848         release_bytecode(compiler->code);
1849         return E_OUTOFMEMORY;
1850     }
1851
1852     compiler->code_size = 64;
1853     compiler->code_off = 1;
1854     return S_OK;
1855 }
1856
1857 static HRESULT compile_function(compiler_ctx_t *ctx, source_elements_t *source, function_expression_t *func_expr,
1858         BOOL from_eval, function_code_t *func)
1859 {
1860     variable_declaration_t *var_iter;
1861     function_expression_t *iter;
1862     unsigned off, i;
1863     HRESULT hres;
1864
1865     TRACE("\n");
1866
1867     ctx->var_head = ctx->var_tail = NULL;
1868     ctx->func_head = ctx->func_tail = NULL;
1869
1870     off = ctx->code_off;
1871     ctx->func = func;
1872     hres = compile_block_statement(ctx, source->statement);
1873     if(FAILED(hres))
1874         return hres;
1875
1876     resolve_labels(ctx, off);
1877
1878     if(!from_eval && !push_instr(ctx, OP_pop))
1879         return E_OUTOFMEMORY;
1880     if(!push_instr(ctx, OP_ret))
1881         return E_OUTOFMEMORY;
1882
1883     if(TRACE_ON(jscript_disas))
1884         dump_code(ctx, off);
1885
1886     func->instr_off = off;
1887
1888     if(func_expr && func_expr->identifier) {
1889         func->name = compiler_alloc_bstr(ctx, func_expr->identifier);
1890         if(!func->name)
1891             return E_OUTOFMEMORY;
1892     }
1893
1894     if(func_expr) {
1895         parameter_t *param_iter;
1896
1897         func->source = func_expr->src_str;
1898         func->source_len = func_expr->src_len;
1899
1900         for(param_iter = func_expr->parameter_list; param_iter; param_iter = param_iter->next)
1901             func->param_cnt++;
1902
1903         func->params = compiler_alloc(ctx->code, func->param_cnt * sizeof(*func->params));
1904         if(!func->params)
1905             return E_OUTOFMEMORY;
1906
1907         for(param_iter = func_expr->parameter_list, i=0; param_iter; param_iter = param_iter->next, i++) {
1908             func->params[i] = compiler_alloc_bstr(ctx, param_iter->identifier);
1909             if(!func->params[i])
1910                 return E_OUTOFMEMORY;
1911         }
1912     }
1913
1914     func->variables = compiler_alloc(ctx->code, func->var_cnt * sizeof(*func->variables));
1915     if(!func->variables)
1916         return E_OUTOFMEMORY;
1917
1918     for(var_iter = ctx->var_head, i=0; var_iter; var_iter = var_iter->global_next, i++) {
1919         func->variables[i] = compiler_alloc_bstr(ctx, var_iter->identifier);
1920         if(!func->variables[i])
1921             return E_OUTOFMEMORY;
1922     }
1923
1924     assert(i == func->var_cnt);
1925
1926     func->funcs = compiler_alloc(ctx->code, func->func_cnt * sizeof(*func->funcs));
1927     if(!func->funcs)
1928         return E_OUTOFMEMORY;
1929     memset(func->funcs, 0, func->func_cnt * sizeof(*func->funcs));
1930
1931     for(iter = ctx->func_head, i=0; iter; iter = iter->next, i++) {
1932         hres = compile_function(ctx, iter->source_elements, iter, FALSE, func->funcs+i);
1933         if(FAILED(hres))
1934             return hres;
1935     }
1936
1937     assert(i == func->func_cnt);
1938
1939     return S_OK;
1940 }
1941
1942 static HRESULT parse_arguments(compiler_ctx_t *ctx, const WCHAR *args, BSTR *arg_array, unsigned *args_size)
1943 {
1944     const WCHAR *ptr = args, *ptr2;
1945     unsigned arg_cnt = 0;
1946
1947     while(isspaceW(*ptr))
1948         ptr++;
1949     if(!*ptr) {
1950         if(args_size)
1951             *args_size = 0;
1952         return S_OK;
1953     }
1954
1955     while(1) {
1956         if(!isalphaW(*ptr) && *ptr != '_') {
1957             FIXME("expected alpha or '_': %s\n", debugstr_w(ptr));
1958             return E_FAIL;
1959         }
1960
1961         ptr2 = ptr;
1962         while(isalnumW(*ptr) || *ptr == '_')
1963             ptr++;
1964
1965         if(*ptr && *ptr != ',' && !isspaceW(*ptr)) {
1966             FIXME("unexpected har %s\n", debugstr_w(ptr));
1967             return E_FAIL;
1968         }
1969
1970         if(arg_array) {
1971             arg_array[arg_cnt] = compiler_alloc_bstr_len(ctx, ptr2, ptr-ptr2);
1972             if(!arg_array[arg_cnt])
1973                 return E_OUTOFMEMORY;
1974         }
1975         arg_cnt++;
1976
1977         while(isspaceW(*ptr))
1978             ptr++;
1979         if(!*ptr)
1980             break;
1981         if(*ptr != ',') {
1982             FIXME("expected ',': %s\n", debugstr_w(ptr));
1983             return E_FAIL;
1984         }
1985
1986         ptr++;
1987         while(isspaceW(*ptr))
1988             ptr++;
1989     }
1990
1991     if(args_size)
1992         *args_size = arg_cnt;
1993     return S_OK;
1994 }
1995
1996 static HRESULT compile_arguments(compiler_ctx_t *ctx, const WCHAR *args)
1997 {
1998     HRESULT hres;
1999
2000     hres = parse_arguments(ctx, args, NULL, &ctx->code->global_code.param_cnt);
2001     if(FAILED(hres))
2002         return hres;
2003
2004     ctx->code->global_code.params = compiler_alloc(ctx->code,
2005             ctx->code->global_code.param_cnt * sizeof(*ctx->code->global_code.params));
2006     if(!ctx->code->global_code.params)
2007         return E_OUTOFMEMORY;
2008
2009     return parse_arguments(ctx, args, ctx->code->global_code.params, NULL);
2010 }
2011
2012 HRESULT compile_script(script_ctx_t *ctx, const WCHAR *code, const WCHAR *args, const WCHAR *delimiter,
2013         BOOL from_eval, BOOL use_decode, bytecode_t **ret)
2014 {
2015     compiler_ctx_t compiler = {0};
2016     HRESULT hres;
2017
2018     hres = init_code(&compiler, code);
2019     if(FAILED(hres))
2020         return hres;
2021
2022     if(args) {
2023         hres = compile_arguments(&compiler, args);
2024         if(FAILED(hres))
2025             return hres;
2026     }
2027
2028     if(use_decode) {
2029         hres = decode_source(compiler.code->source);
2030         if(FAILED(hres)) {
2031             WARN("Decoding failed\n");
2032             return hres;
2033         }
2034     }
2035
2036     hres = script_parse(ctx, compiler.code->source, delimiter, from_eval, &compiler.parser);
2037     if(FAILED(hres)) {
2038         release_bytecode(compiler.code);
2039         return hres;
2040     }
2041
2042     hres = compile_function(&compiler, compiler.parser->source, NULL, from_eval, &compiler.code->global_code);
2043     parser_release(compiler.parser);
2044     if(FAILED(hres)) {
2045         release_bytecode(compiler.code);
2046         return hres;
2047     }
2048
2049     *ret = compiler.code;
2050     return S_OK;
2051 }