jscript: Added bytecode version of member expression.
[wine] / dlls / jscript / engine.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <math.h>
23 #include <assert.h>
24
25 #include "jscript.h"
26 #include "engine.h"
27
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31
32 #define EXPR_NOVAL   0x0001
33 #define EXPR_NEWREF  0x0002
34
35 struct _return_type_t {
36     enum{
37         RT_NORMAL,
38         RT_RETURN,
39         RT_BREAK,
40         RT_CONTINUE
41     } type;
42     jsexcept_t ei;
43 };
44
45 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
46 {
47     return stat->eval(ctx, stat, rt, ret);
48 }
49
50 static inline HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
51 {
52     return expr->eval(ctx, expr, flags, ei, ret);
53 }
54
55 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
56 {
57     if(!ctx->stack_size) {
58         ctx->stack = heap_alloc(16*sizeof(VARIANT));
59         if(!ctx->stack)
60             return E_OUTOFMEMORY;
61         ctx->stack_size = 16;
62     }else if(ctx->stack_size == ctx->top) {
63         VARIANT *new_stack;
64
65         new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
66         if(!new_stack) {
67             VariantClear(v);
68             return E_OUTOFMEMORY;
69         }
70
71         ctx->stack = new_stack;
72         ctx->stack_size *= 2;
73     }
74
75     ctx->stack[ctx->top++] = *v;
76     return S_OK;
77 }
78
79 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
80 {
81     VARIANT v;
82
83     V_VT(&v) = VT_BOOL;
84     V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
85     return stack_push(ctx, &v);
86 }
87
88 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
89 {
90     VARIANT v;
91
92     num_set_val(&v, number);
93     return stack_push(ctx, &v);
94 }
95
96 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
97 {
98     VARIANT v;
99
100     V_VT(&v) = VT_I4;
101     V_I4(&v) = n;
102     return stack_push(ctx, &v);
103 }
104
105 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
106 {
107     VARIANT v;
108     HRESULT hres;
109
110     V_VT(&v) = VT_DISPATCH;
111     V_DISPATCH(&v) = disp;
112     hres = stack_push(ctx, &v);
113     if(FAILED(hres))
114         return hres;
115
116     V_VT(&v) = VT_INT;
117     V_INT(&v) = id;
118     return stack_push(ctx, &v);
119 }
120
121 static inline VARIANT *stack_top(exec_ctx_t *ctx)
122 {
123     assert(ctx->top);
124     return ctx->stack + ctx->top-1;
125 }
126
127 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
128 {
129     assert(ctx->top > n);
130     return ctx->stack + ctx->top-1-n;
131 }
132
133 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
134 {
135     assert(ctx->top);
136     return ctx->stack + --ctx->top;
137 }
138
139 static void stack_popn(exec_ctx_t *ctx, unsigned n)
140 {
141     while(n--)
142         VariantClear(stack_pop(ctx));
143 }
144
145 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
146 {
147     VARIANT *v;
148     HRESULT hres;
149
150     v = stack_pop(ctx);
151     hres = to_number(ctx->parser->script, v, &ctx->ei, r);
152     VariantClear(v);
153     return hres;
154 }
155
156 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
157 {
158     VARIANT *v;
159     HRESULT hres;
160
161     v = stack_pop(ctx);
162     if(V_VT(v) == VT_DISPATCH) {
163         *r = V_DISPATCH(v);
164         return S_OK;
165     }
166
167     hres = to_object(ctx->parser->script, v, r);
168     VariantClear(v);
169     return hres;
170 }
171
172 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
173 {
174     return to_int32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
175 }
176
177 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
178 {
179     assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
180
181     *id = V_INT(stack_pop(ctx));
182     return V_DISPATCH(stack_pop(ctx));
183 }
184
185 static inline IDispatch *stack_top_objid(exec_ctx_t *ctx, DISPID *id)
186 {
187     assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
188
189     *id = V_INT(stack_top(ctx));
190     return V_DISPATCH(stack_topn(ctx, 1));
191 }
192
193 static void exprval_release(exprval_t *val)
194 {
195     switch(val->type) {
196     case EXPRVAL_VARIANT:
197         if(V_VT(&val->u.var) != VT_EMPTY)
198             VariantClear(&val->u.var);
199         return;
200     case EXPRVAL_IDREF:
201         if(val->u.idref.disp)
202             IDispatch_Release(val->u.idref.disp);
203         return;
204     case EXPRVAL_INVALID:
205         SysFreeString(val->u.identifier);
206     }
207 }
208
209 /* ECMA-262 3rd Edition    8.7.1 */
210 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
211 {
212     V_VT(ret) = VT_EMPTY;
213
214     switch(val->type) {
215     case EXPRVAL_VARIANT:
216         return VariantCopy(ret, &val->u.var);
217     case EXPRVAL_IDREF:
218         if(!val->u.idref.disp) {
219             FIXME("throw ReferenceError\n");
220             return E_FAIL;
221         }
222
223         return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
224     case EXPRVAL_INVALID:
225         return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
226     }
227
228     ERR("type %d\n", val->type);
229     return E_FAIL;
230 }
231
232 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
233 {
234     if(val->type == EXPRVAL_VARIANT) {
235         *ret = val->u.var;
236         V_VT(&val->u.var) = VT_EMPTY;
237         return S_OK;
238     }
239
240     return exprval_value(ctx, val, ei, ret);
241 }
242
243 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
244 {
245     if(exprval->type != EXPRVAL_VARIANT) {
246         VARIANT val;
247         HRESULT hres;
248
249         hres = exprval_to_value(ctx, exprval, ei, &val);
250         if(FAILED(hres))
251             return hres;
252
253         hres = to_boolean(&val, b);
254         VariantClear(&val);
255         return hres;
256     }
257
258     return to_boolean(&exprval->u.var, b);
259 }
260
261 static void exprval_init(exprval_t *val)
262 {
263     val->type = EXPRVAL_VARIANT;
264     V_VT(&val->u.var) = VT_EMPTY;
265 }
266
267 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
268 {
269     val->type = EXPRVAL_IDREF;
270     val->u.idref.disp = disp;
271     val->u.idref.id = id;
272
273     if(disp)
274         IDispatch_AddRef(disp);
275 }
276
277 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
278 {
279     scope_chain_t *new_scope;
280
281     new_scope = heap_alloc(sizeof(scope_chain_t));
282     if(!new_scope)
283         return E_OUTOFMEMORY;
284
285     new_scope->ref = 1;
286
287     jsdisp_addref(obj);
288     new_scope->obj = obj;
289
290     if(scope) {
291         scope_addref(scope);
292         new_scope->next = scope;
293     }else {
294         new_scope->next = NULL;
295     }
296
297     *ret = new_scope;
298     return S_OK;
299 }
300
301 static void scope_pop(scope_chain_t **scope)
302 {
303     scope_chain_t *tmp;
304
305     tmp = *scope;
306     *scope = tmp->next;
307     scope_release(tmp);
308 }
309
310 void scope_release(scope_chain_t *scope)
311 {
312     if(--scope->ref)
313         return;
314
315     if(scope->next)
316         scope_release(scope->next);
317
318     jsdisp_release(scope->obj);
319     heap_free(scope);
320 }
321
322 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
323         scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
324 {
325     exec_ctx_t *ctx;
326
327     ctx = heap_alloc_zero(sizeof(exec_ctx_t));
328     if(!ctx)
329         return E_OUTOFMEMORY;
330
331     ctx->ref = 1;
332     ctx->is_global = is_global;
333
334     if(this_obj)
335         ctx->this_obj = this_obj;
336     else if(script_ctx->host_global)
337         ctx->this_obj = script_ctx->host_global;
338     else
339         ctx->this_obj = to_disp(script_ctx->global);
340     IDispatch_AddRef(ctx->this_obj);
341
342     jsdisp_addref(var_disp);
343     ctx->var_disp = var_disp;
344
345     if(scope) {
346         scope_addref(scope);
347         ctx->scope_chain = scope;
348     }
349
350     *ret = ctx;
351     return S_OK;
352 }
353
354 void exec_release(exec_ctx_t *ctx)
355 {
356     if(--ctx->ref)
357         return;
358
359     if(ctx->scope_chain)
360         scope_release(ctx->scope_chain);
361     if(ctx->var_disp)
362         jsdisp_release(ctx->var_disp);
363     if(ctx->this_obj)
364         IDispatch_Release(ctx->this_obj);
365     heap_free(ctx->stack);
366     heap_free(ctx);
367 }
368
369 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
370 {
371     IDispatchEx *dispex;
372     HRESULT hres;
373
374     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
375     if(FAILED(hres)) {
376         TRACE("unsing IDispatch\n");
377
378         *id = 0;
379         return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
380     }
381
382     *id = 0;
383     hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
384     IDispatchEx_Release(dispex);
385     return hres;
386 }
387
388 /* ECMA-262 3rd Edition    8.7.2 */
389 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
390 {
391     if(ref->type != EXPRVAL_IDREF)
392         return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
393
394     return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
395 }
396
397 static inline BOOL is_null(const VARIANT *v)
398 {
399     return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
400 }
401
402 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
403 {
404     IObjectIdentity *identity;
405     IUnknown *unk1, *unk2;
406     HRESULT hres;
407
408     if(disp1 == disp2) {
409         *ret = TRUE;
410         return S_OK;
411     }
412
413     if(!disp1 || !disp2) {
414         *ret = FALSE;
415         return S_OK;
416     }
417
418     hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
419     if(FAILED(hres))
420         return hres;
421
422     hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
423     if(FAILED(hres)) {
424         IUnknown_Release(unk1);
425         return hres;
426     }
427
428     if(unk1 == unk2) {
429         *ret = TRUE;
430     }else {
431         hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
432         if(SUCCEEDED(hres)) {
433             hres = IObjectIdentity_IsEqualObject(identity, unk2);
434             IObjectIdentity_Release(identity);
435             *ret = hres == S_OK;
436         }else {
437             *ret = FALSE;
438         }
439     }
440
441     IUnknown_Release(unk1);
442     IUnknown_Release(unk2);
443     return S_OK;
444 }
445
446 /* ECMA-262 3rd Edition    11.9.6 */
447 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
448 {
449     TRACE("\n");
450
451     if(V_VT(lval) != V_VT(rval)) {
452         if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
453             *ret = num_val(lval) == num_val(rval);
454         else if(is_null(lval))
455             *ret = is_null(rval);
456         else
457             *ret = FALSE;
458         return S_OK;
459     }
460
461     switch(V_VT(lval)) {
462     case VT_EMPTY:
463     case VT_NULL:
464         *ret = VARIANT_TRUE;
465         break;
466     case VT_I4:
467         *ret = V_I4(lval) == V_I4(rval);
468         break;
469     case VT_R8:
470         *ret = V_R8(lval) == V_R8(rval);
471         break;
472     case VT_BSTR:
473         if(!V_BSTR(lval))
474             *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
475         else if(!V_BSTR(rval))
476             *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
477         else
478             *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
479         break;
480     case VT_DISPATCH:
481         return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
482     case VT_BOOL:
483         *ret = !V_BOOL(lval) == !V_BOOL(rval);
484         break;
485     default:
486         FIXME("unimplemented vt %d\n", V_VT(lval));
487         return E_NOTIMPL;
488     }
489
490     return S_OK;
491 }
492
493 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
494 {
495     switch(literal->type) {
496     case LT_NULL:
497         V_VT(v) = VT_NULL;
498         break;
499     case LT_INT:
500         V_VT(v) = VT_I4;
501         V_I4(v) = literal->u.lval;
502         break;
503     case LT_DOUBLE:
504         V_VT(v) = VT_R8;
505         V_R8(v) = literal->u.dval;
506         break;
507     case LT_STRING: {
508         BSTR str = SysAllocString(literal->u.wstr);
509         if(!str)
510             return E_OUTOFMEMORY;
511
512         V_VT(v) = VT_BSTR;
513         V_BSTR(v) = str;
514         break;
515     }
516     case LT_BOOL:
517         V_VT(v) = VT_BOOL;
518         V_BOOL(v) = literal->u.bval;
519         break;
520     case LT_REGEXP: {
521         jsdisp_t *regexp;
522         HRESULT hres;
523
524         hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
525                              literal->u.regexp.flags, &regexp);
526         if(FAILED(hres))
527             return hres;
528
529         var_set_jsdisp(v, regexp);
530     }
531     }
532
533     return S_OK;
534 }
535
536 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
537 {
538     named_item_t *item;
539     DISPID id;
540     HRESULT hres;
541
542     for(item = ctx->named_items; item; item = item->next) {
543         if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
544             hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
545             if(SUCCEEDED(hres)) {
546                 if(ret)
547                     exprval_set_idref(ret, item->disp, id);
548                 return TRUE;
549             }
550         }
551     }
552
553     return FALSE;
554 }
555
556 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
557         jsexcept_t *ei, VARIANT *retv)
558 {
559     script_ctx_t *script = parser->script;
560     function_declaration_t *func;
561     parser_ctx_t *prev_parser;
562     var_list_t *var;
563     VARIANT val, tmp;
564     statement_t *stat;
565     exec_ctx_t *prev_ctx;
566     return_type_t rt;
567     HRESULT hres = S_OK;
568
569     for(func = source->functions; func; func = func->next) {
570         jsdisp_t *func_obj;
571         VARIANT var;
572
573         hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
574                 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
575         if(FAILED(hres))
576             return hres;
577
578         var_set_jsdisp(&var, func_obj);
579         hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
580         jsdisp_release(func_obj);
581         if(FAILED(hres))
582             return hres;
583     }
584
585     for(var = source->variables; var; var = var->next) {
586         DISPID id = 0;
587         BSTR name;
588
589         name = SysAllocString(var->identifier);
590         if(!name)
591             return E_OUTOFMEMORY;
592
593         if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
594             hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
595         SysFreeString(name);
596         if(FAILED(hres))
597             return hres;
598     }
599
600     prev_ctx = script->exec_ctx;
601     script->exec_ctx = ctx;
602
603     prev_parser = ctx->parser;
604     ctx->parser = parser;
605
606     V_VT(&val) = VT_EMPTY;
607     memset(&rt, 0, sizeof(rt));
608     rt.type = RT_NORMAL;
609
610     for(stat = source->statement; stat; stat = stat->next) {
611         hres = stat_eval(script, stat, &rt, &tmp);
612         if(FAILED(hres))
613             break;
614
615         VariantClear(&val);
616         val = tmp;
617         if(rt.type != RT_NORMAL)
618             break;
619     }
620
621     script->exec_ctx = prev_ctx;
622     ctx->parser = prev_parser;
623
624     if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
625         FIXME("wrong rt %d\n", rt.type);
626         hres = E_FAIL;
627     }
628
629     *ei = rt.ei;
630     if(FAILED(hres)) {
631         VariantClear(&val);
632         return hres;
633     }
634
635     if(!retv || (!from_eval && rt.type != RT_RETURN))
636         VariantClear(&val);
637     if(retv)
638         *retv = val;
639     return S_OK;
640 }
641
642 /* ECMA-262 3rd Edition    10.1.4 */
643 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
644 {
645     scope_chain_t *scope;
646     named_item_t *item;
647     DISPID id = 0;
648     HRESULT hres;
649
650     TRACE("%s\n", debugstr_w(identifier));
651
652     for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
653         hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
654         if(SUCCEEDED(hres)) {
655             exprval_set_idref(ret, to_disp(scope->obj), id);
656             return S_OK;
657         }
658     }
659
660     hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
661     if(SUCCEEDED(hres)) {
662         exprval_set_idref(ret, to_disp(ctx->global), id);
663         return S_OK;
664     }
665
666     for(item = ctx->named_items; item; item = item->next) {
667         if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
668             if(!item->disp) {
669                 IUnknown *unk;
670
671                 if(!ctx->site)
672                     break;
673
674                 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
675                                                      SCRIPTINFO_IUNKNOWN, &unk, NULL);
676                 if(FAILED(hres)) {
677                     WARN("GetItemInfo failed: %08x\n", hres);
678                     break;
679                 }
680
681                 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
682                 IUnknown_Release(unk);
683                 if(FAILED(hres)) {
684                     WARN("object does not implement IDispatch\n");
685                     break;
686                 }
687             }
688
689             ret->type = EXPRVAL_VARIANT;
690             V_VT(&ret->u.var) = VT_DISPATCH;
691             V_DISPATCH(&ret->u.var) = item->disp;
692             IDispatch_AddRef(item->disp);
693             return S_OK;
694         }
695     }
696
697     if(lookup_global_members(ctx, identifier, ret))
698         return S_OK;
699
700     if(flags & EXPR_NEWREF) {
701         hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
702         if(FAILED(hres))
703             return hres;
704
705         exprval_set_idref(ret, to_disp(ctx->global), id);
706         return S_OK;
707     }
708
709     ret->type = EXPRVAL_INVALID;
710     ret->u.identifier = SysAllocString(identifier);
711     if(!ret->u.identifier)
712         return E_OUTOFMEMORY;
713
714     return S_OK;
715 }
716
717 /* ECMA-262 3rd Edition    12.1 */
718 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
719 {
720     block_statement_t *stat = (block_statement_t*)_stat;
721     VARIANT val, tmp;
722     statement_t *iter;
723     HRESULT hres = S_OK;
724
725     TRACE("\n");
726
727     V_VT(&val) = VT_EMPTY;
728     for(iter = stat->stat_list; iter; iter = iter->next) {
729         hres = stat_eval(ctx, iter, rt, &tmp);
730         if(FAILED(hres))
731             break;
732
733         VariantClear(&val);
734         val = tmp;
735         if(rt->type != RT_NORMAL)
736             break;
737     }
738
739     if(FAILED(hres)) {
740         VariantClear(&val);
741         return hres;
742     }
743
744     *ret = val;
745     return S_OK;
746 }
747
748 /* ECMA-262 3rd Edition    12.2 */
749 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
750 {
751     variable_declaration_t *iter;
752     HRESULT hres = S_OK;
753
754     for(iter = var_list; iter; iter = iter->next) {
755         exprval_t exprval;
756         VARIANT val;
757
758         if(!iter->expr)
759             continue;
760
761         hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
762         if(FAILED(hres))
763             break;
764
765         hres = exprval_to_value(ctx, &exprval, ei, &val);
766         exprval_release(&exprval);
767         if(FAILED(hres))
768             break;
769
770         hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
771         VariantClear(&val);
772         if(FAILED(hres))
773             break;
774     }
775
776     return hres;
777 }
778
779 /* ECMA-262 3rd Edition    12.2 */
780 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
781 {
782     var_statement_t *stat = (var_statement_t*)_stat;
783     HRESULT hres;
784
785     TRACE("\n");
786
787     hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
788     if(FAILED(hres))
789         return hres;
790
791     V_VT(ret) = VT_EMPTY;
792     return S_OK;
793 }
794
795 /* ECMA-262 3rd Edition    12.3 */
796 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
797 {
798     TRACE("\n");
799
800     V_VT(ret) = VT_EMPTY;
801     return S_OK;
802 }
803
804 /* ECMA-262 3rd Edition    12.4 */
805 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
806 {
807     expression_statement_t *stat = (expression_statement_t*)_stat;
808     exprval_t exprval;
809     VARIANT val;
810     HRESULT hres;
811
812     TRACE("\n");
813
814     hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
815     if(FAILED(hres))
816         return hres;
817
818     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
819     exprval_release(&exprval);
820     if(FAILED(hres))
821         return hres;
822
823     *ret = val;
824     TRACE("= %s\n", debugstr_variant(ret));
825     return S_OK;
826 }
827
828 /* ECMA-262 3rd Edition    12.5 */
829 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
830 {
831     if_statement_t *stat = (if_statement_t*)_stat;
832     exprval_t exprval;
833     VARIANT_BOOL b;
834     HRESULT hres;
835
836     TRACE("\n");
837
838     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
839     if(FAILED(hres))
840         return hres;
841
842     hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
843     exprval_release(&exprval);
844     if(FAILED(hres))
845         return hres;
846
847     if(b)
848         hres = stat_eval(ctx, stat->if_stat, rt, ret);
849     else if(stat->else_stat)
850         hres = stat_eval(ctx, stat->else_stat, rt, ret);
851     else
852         V_VT(ret) = VT_EMPTY;
853
854     return hres;
855 }
856
857 /* ECMA-262 3rd Edition    12.6.2 */
858 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
859 {
860     while_statement_t *stat = (while_statement_t*)_stat;
861     exprval_t exprval;
862     VARIANT val, tmp;
863     VARIANT_BOOL b;
864     BOOL test_expr;
865     HRESULT hres;
866
867     TRACE("\n");
868
869     V_VT(&val) = VT_EMPTY;
870     test_expr = !stat->do_while;
871
872     while(1) {
873         if(test_expr) {
874             hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
875             if(FAILED(hres))
876                 break;
877
878             hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
879             exprval_release(&exprval);
880             if(FAILED(hres) || !b)
881                 break;
882         }else {
883             test_expr = TRUE;
884         }
885
886         hres = stat_eval(ctx, stat->statement, rt, &tmp);
887         if(FAILED(hres))
888             break;
889
890         VariantClear(&val);
891         val = tmp;
892
893         if(rt->type == RT_CONTINUE)
894             rt->type = RT_NORMAL;
895         if(rt->type != RT_NORMAL)
896             break;
897     }
898
899     if(FAILED(hres)) {
900         VariantClear(&val);
901         return hres;
902     }
903
904     if(rt->type == RT_BREAK)
905         rt->type = RT_NORMAL;
906
907     *ret = val;
908     return S_OK;
909 }
910
911 /* ECMA-262 3rd Edition    12.6.3 */
912 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
913 {
914     for_statement_t *stat = (for_statement_t*)_stat;
915     VARIANT val, tmp, retv;
916     exprval_t exprval;
917     VARIANT_BOOL b;
918     HRESULT hres;
919
920     TRACE("\n");
921
922     if(stat->variable_list) {
923         hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
924         if(FAILED(hres))
925             return hres;
926     }else if(stat->begin_expr) {
927         hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
928         if(FAILED(hres))
929             return hres;
930
931         hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
932         exprval_release(&exprval);
933         if(FAILED(hres))
934             return hres;
935
936         VariantClear(&val);
937     }
938
939     V_VT(&retv) = VT_EMPTY;
940
941     while(1) {
942         if(stat->expr) {
943             hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
944             if(FAILED(hres))
945                 break;
946
947             hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
948             exprval_release(&exprval);
949             if(FAILED(hres) || !b)
950                 break;
951         }
952
953         hres = stat_eval(ctx, stat->statement, rt, &tmp);
954         if(FAILED(hres))
955             break;
956
957         VariantClear(&retv);
958         retv = tmp;
959
960         if(rt->type == RT_CONTINUE)
961             rt->type = RT_NORMAL;
962         else if(rt->type != RT_NORMAL)
963             break;
964
965         if(stat->end_expr) {
966             hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
967             if(FAILED(hres))
968                 break;
969
970             hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
971             exprval_release(&exprval);
972             if(FAILED(hres))
973                 break;
974
975             VariantClear(&val);
976         }
977     }
978
979     if(FAILED(hres)) {
980         VariantClear(&retv);
981         return hres;
982     }
983
984     if(rt->type == RT_BREAK)
985         rt->type = RT_NORMAL;
986
987     *ret = retv;
988     return S_OK;
989 }
990
991 /* ECMA-262 3rd Edition    12.6.4 */
992 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
993 {
994     forin_statement_t *stat = (forin_statement_t*)_stat;
995     VARIANT val, name, retv, tmp;
996     DISPID id = DISPID_STARTENUM;
997     BSTR str, identifier = NULL;
998     IDispatchEx *in_obj;
999     exprval_t exprval;
1000     HRESULT hres;
1001
1002     TRACE("\n");
1003
1004     if(stat->variable) {
1005         hres = variable_list_eval(ctx, stat->variable, &rt->ei);
1006         if(FAILED(hres))
1007             return hres;
1008     }
1009
1010     hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
1011     if(FAILED(hres))
1012         return hres;
1013
1014     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1015     exprval_release(&exprval);
1016     if(FAILED(hres))
1017         return hres;
1018
1019     if(V_VT(&val) != VT_DISPATCH) {
1020         TRACE("in vt %d\n", V_VT(&val));
1021         VariantClear(&val);
1022         V_VT(ret) = VT_EMPTY;
1023         return S_OK;
1024     }
1025
1026     hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
1027     IDispatch_Release(V_DISPATCH(&val));
1028     if(FAILED(hres)) {
1029         TRACE("Object doesn't support IDispatchEx\n");
1030         V_VT(ret) = VT_EMPTY;
1031         return S_OK;
1032     }
1033
1034     V_VT(&retv) = VT_EMPTY;
1035
1036     if(stat->variable)
1037         identifier = SysAllocString(stat->variable->identifier);
1038
1039     while(1) {
1040         hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
1041         if(FAILED(hres) || hres == S_FALSE)
1042             break;
1043
1044         hres = IDispatchEx_GetMemberName(in_obj, id, &str);
1045         if(FAILED(hres))
1046             break;
1047
1048         TRACE("iter %s\n", debugstr_w(str));
1049
1050         if(stat->variable)
1051             hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
1052         else
1053             hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
1054         if(SUCCEEDED(hres)) {
1055             V_VT(&name) = VT_BSTR;
1056             V_BSTR(&name) = str;
1057             hres = put_value(ctx, &exprval, &name, &rt->ei);
1058             exprval_release(&exprval);
1059         }
1060         SysFreeString(str);
1061         if(FAILED(hres))
1062             break;
1063
1064         hres = stat_eval(ctx, stat->statement, rt, &tmp);
1065         if(FAILED(hres))
1066             break;
1067
1068         VariantClear(&retv);
1069         retv = tmp;
1070
1071         if(rt->type == RT_CONTINUE)
1072             rt->type = RT_NORMAL;
1073         else if(rt->type != RT_NORMAL)
1074             break;
1075     }
1076
1077     SysFreeString(identifier);
1078     IDispatchEx_Release(in_obj);
1079     if(FAILED(hres)) {
1080         VariantClear(&retv);
1081         return hres;
1082     }
1083
1084     if(rt->type == RT_BREAK)
1085         rt->type = RT_NORMAL;
1086
1087     *ret = retv;
1088     return S_OK;
1089 }
1090
1091 /* ECMA-262 3rd Edition    12.7 */
1092 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1093 {
1094     branch_statement_t *stat = (branch_statement_t*)_stat;
1095
1096     TRACE("\n");
1097
1098     if(stat->identifier) {
1099         FIXME("indentifier not implemented\n");
1100         return E_NOTIMPL;
1101     }
1102
1103     rt->type = RT_CONTINUE;
1104     V_VT(ret) = VT_EMPTY;
1105     return S_OK;
1106 }
1107
1108 /* ECMA-262 3rd Edition    12.8 */
1109 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1110 {
1111     branch_statement_t *stat = (branch_statement_t*)_stat;
1112
1113     TRACE("\n");
1114
1115     if(stat->identifier) {
1116         FIXME("indentifier not implemented\n");
1117         return E_NOTIMPL;
1118     }
1119
1120     rt->type = RT_BREAK;
1121     V_VT(ret) = VT_EMPTY;
1122     return S_OK;
1123 }
1124
1125 /* ECMA-262 3rd Edition    12.9 */
1126 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1127 {
1128     expression_statement_t *stat = (expression_statement_t*)_stat;
1129     HRESULT hres;
1130
1131     TRACE("\n");
1132
1133     if(stat->expr) {
1134         exprval_t exprval;
1135
1136         hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1137         if(FAILED(hres))
1138             return hres;
1139
1140         hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1141         exprval_release(&exprval);
1142         if(FAILED(hres))
1143             return hres;
1144     }else {
1145         V_VT(ret) = VT_EMPTY;
1146     }
1147
1148     TRACE("= %s\n", debugstr_variant(ret));
1149     rt->type = RT_RETURN;
1150     return S_OK;
1151 }
1152
1153 /* ECMA-262 3rd Edition    12.10 */
1154 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1155 {
1156     with_statement_t *stat = (with_statement_t*)_stat;
1157     exprval_t exprval;
1158     IDispatch *disp;
1159     jsdisp_t *obj;
1160     VARIANT val;
1161     HRESULT hres;
1162
1163     TRACE("\n");
1164
1165     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1166     if(FAILED(hres))
1167         return hres;
1168
1169     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1170     exprval_release(&exprval);
1171     if(FAILED(hres))
1172         return hres;
1173
1174     hres = to_object(ctx, &val, &disp);
1175     VariantClear(&val);
1176     if(FAILED(hres))
1177         return hres;
1178
1179     obj = iface_to_jsdisp((IUnknown*)disp);
1180     IDispatch_Release(disp);
1181     if(!obj) {
1182         FIXME("disp id not jsdisp\n");
1183         return E_NOTIMPL;
1184     }
1185
1186     hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1187     jsdisp_release(obj);
1188     if(FAILED(hres))
1189         return hres;
1190
1191     hres = stat_eval(ctx, stat->statement, rt, ret);
1192
1193     scope_pop(&ctx->exec_ctx->scope_chain);
1194     return hres;
1195 }
1196
1197 /* ECMA-262 3rd Edition    12.12 */
1198 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1199 {
1200     FIXME("\n");
1201     return E_NOTIMPL;
1202 }
1203
1204 /* ECMA-262 3rd Edition    12.13 */
1205 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1206 {
1207     switch_statement_t *stat = (switch_statement_t*)_stat;
1208     case_clausule_t *iter, *default_clausule = NULL;
1209     statement_t *stat_iter;
1210     VARIANT val, cval;
1211     exprval_t exprval;
1212     BOOL b;
1213     HRESULT hres;
1214
1215     TRACE("\n");
1216
1217     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1218     if(FAILED(hres))
1219         return hres;
1220
1221     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1222     exprval_release(&exprval);
1223     if(FAILED(hres))
1224         return hres;
1225
1226     for(iter = stat->case_list; iter; iter = iter->next) {
1227         if(!iter->expr) {
1228             default_clausule = iter;
1229             continue;
1230         }
1231
1232         hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1233         if(FAILED(hres))
1234             break;
1235
1236         hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1237         exprval_release(&exprval);
1238         if(FAILED(hres))
1239             break;
1240
1241         hres = equal2_values(&val, &cval, &b);
1242         VariantClear(&cval);
1243         if(FAILED(hres) || b)
1244             break;
1245     }
1246
1247     VariantClear(&val);
1248     if(FAILED(hres))
1249         return hres;
1250
1251     if(!iter)
1252         iter = default_clausule;
1253
1254     V_VT(&val) = VT_EMPTY;
1255     if(iter) {
1256         VARIANT tmp;
1257
1258         for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1259             hres = stat_eval(ctx, stat_iter, rt, &tmp);
1260             if(FAILED(hres))
1261                 break;
1262
1263             VariantClear(&val);
1264             val = tmp;
1265
1266             if(rt->type != RT_NORMAL)
1267                 break;
1268         }
1269     }
1270
1271     if(FAILED(hres)) {
1272         VariantClear(&val);
1273         return hres;
1274     }
1275
1276     if(rt->type == RT_BREAK)
1277         rt->type = RT_NORMAL;
1278
1279     *ret = val;
1280     return S_OK;
1281 }
1282
1283 /* ECMA-262 3rd Edition    12.13 */
1284 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1285 {
1286     expression_statement_t *stat = (expression_statement_t*)_stat;
1287     exprval_t exprval;
1288     VARIANT val;
1289     HRESULT hres;
1290
1291     TRACE("\n");
1292
1293     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1294     if(FAILED(hres))
1295         return hres;
1296
1297     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1298     exprval_release(&exprval);
1299     if(FAILED(hres))
1300         return hres;
1301
1302     rt->ei.var = val;
1303     return DISP_E_EXCEPTION;
1304 }
1305
1306 static HRESULT interp_throw(exec_ctx_t *ctx)
1307 {
1308     const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1309
1310     TRACE("%08x\n", arg);
1311
1312     return throw_reference_error(ctx->parser->script, &ctx->ei, arg, NULL);
1313 }
1314
1315 /* ECMA-262 3rd Edition    12.14 */
1316 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1317 {
1318     jsdisp_t *var_disp;
1319     VARIANT ex, val;
1320     HRESULT hres;
1321
1322     ex = rt->ei.var;
1323     memset(&rt->ei, 0, sizeof(jsexcept_t));
1324
1325     hres = create_dispex(ctx, NULL, NULL, &var_disp);
1326     if(SUCCEEDED(hres)) {
1327         hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1328         if(SUCCEEDED(hres)) {
1329             hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1330             if(SUCCEEDED(hres)) {
1331                 hres = stat_eval(ctx, block->statement, rt, &val);
1332                 scope_pop(&ctx->exec_ctx->scope_chain);
1333             }
1334         }
1335
1336         jsdisp_release(var_disp);
1337     }
1338
1339     VariantClear(&ex);
1340     if(FAILED(hres))
1341         return hres;
1342
1343     *ret = val;
1344     return S_OK;
1345 }
1346
1347 /* ECMA-262 3rd Edition    12.14 */
1348 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1349 {
1350     try_statement_t *stat = (try_statement_t*)_stat;
1351     VARIANT val;
1352     HRESULT hres;
1353
1354     TRACE("\n");
1355
1356     hres = stat_eval(ctx, stat->try_statement, rt, &val);
1357     if(FAILED(hres)) {
1358         TRACE("EXCEPTION\n");
1359         if(!stat->catch_block)
1360             return hres;
1361
1362         hres = catch_eval(ctx, stat->catch_block, rt, &val);
1363         if(FAILED(hres))
1364             return hres;
1365     }
1366
1367     if(stat->finally_statement) {
1368         VariantClear(&val);
1369         hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1370         if(FAILED(hres))
1371             return hres;
1372     }
1373
1374     *ret = val;
1375     return S_OK;
1376 }
1377
1378 static HRESULT return_bool(exprval_t *ret, DWORD b)
1379 {
1380     ret->type = EXPRVAL_VARIANT;
1381     V_VT(&ret->u.var) = VT_BOOL;
1382     V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1383
1384     return S_OK;
1385 }
1386
1387 static HRESULT get_binary_expr_values(script_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1388 {
1389     exprval_t exprval;
1390     HRESULT hres;
1391
1392     hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1393     if(FAILED(hres))
1394         return hres;
1395
1396     hres = exprval_to_value(ctx, &exprval, ei, lval);
1397     exprval_release(&exprval);
1398     if(FAILED(hres))
1399         return hres;
1400
1401     hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1402     if(SUCCEEDED(hres)) {
1403         hres = exprval_to_value(ctx, &exprval, ei, rval);
1404         exprval_release(&exprval);
1405     }
1406
1407     if(FAILED(hres)) {
1408         VariantClear(lval);
1409         return hres;
1410     }
1411
1412     return S_OK;
1413 }
1414
1415 typedef HRESULT (*oper_t)(script_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1416
1417 static HRESULT binary_expr_eval(script_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1418         exprval_t *ret)
1419 {
1420     VARIANT lval, rval, retv;
1421     HRESULT hres;
1422
1423     hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1424     if(FAILED(hres))
1425         return hres;
1426
1427     hres = oper(ctx, &lval, &rval, ei, &retv);
1428     VariantClear(&lval);
1429     VariantClear(&rval);
1430     if(FAILED(hres))
1431         return hres;
1432
1433     ret->type = EXPRVAL_VARIANT;
1434     ret->u.var = retv;
1435     return S_OK;
1436 }
1437
1438 /* ECMA-262 3rd Edition    11.13.2 */
1439 static HRESULT assign_oper_eval(script_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1440                                 jsexcept_t *ei, exprval_t *ret)
1441 {
1442     VARIANT retv, lval, rval;
1443     exprval_t exprval, exprvalr;
1444     HRESULT hres;
1445
1446     hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1447     if(FAILED(hres))
1448         return hres;
1449
1450     hres = exprval_value(ctx, &exprval, ei, &lval);
1451     if(SUCCEEDED(hres)) {
1452         hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1453         if(SUCCEEDED(hres)) {
1454             hres = exprval_value(ctx, &exprvalr, ei, &rval);
1455             exprval_release(&exprvalr);
1456         }
1457         if(SUCCEEDED(hres)) {
1458             hres = oper(ctx, &lval, &rval, ei, &retv);
1459             VariantClear(&rval);
1460         }
1461         VariantClear(&lval);
1462     }
1463
1464     if(SUCCEEDED(hres)) {
1465         hres = put_value(ctx, &exprval, &retv, ei);
1466         if(FAILED(hres))
1467             VariantClear(&retv);
1468     }
1469     exprval_release(&exprval);
1470
1471     if(FAILED(hres))
1472         return hres;
1473
1474     ret->type = EXPRVAL_VARIANT;
1475     ret->u.var = retv;
1476     return S_OK;
1477 }
1478
1479 /* ECMA-262 3rd Edition    13 */
1480 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1481 {
1482     function_expression_t *expr = (function_expression_t*)_expr;
1483     VARIANT var;
1484     HRESULT hres;
1485
1486     TRACE("\n");
1487
1488     if(expr->identifier) {
1489         hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1490         if(FAILED(hres))
1491             return hres;
1492     }else {
1493         jsdisp_t *dispex;
1494
1495         hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1496                 expr->src_str, expr->src_len, &dispex);
1497         if(FAILED(hres))
1498             return hres;
1499
1500         var_set_jsdisp(&var, dispex);
1501     }
1502
1503     ret->type = EXPRVAL_VARIANT;
1504     ret->u.var = var;
1505     return S_OK;
1506 }
1507
1508 /* ECMA-262 3rd Edition    11.2.1 */
1509 HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1510 {
1511     array_expression_t *expr = (array_expression_t*)_expr;
1512     exprval_t exprval;
1513     VARIANT member, val;
1514     DISPID id;
1515     BSTR str;
1516     IDispatch *obj = NULL;
1517     HRESULT hres;
1518
1519     TRACE("\n");
1520
1521     hres = expr_eval(ctx, expr->member_expr, 0, ei, &exprval);
1522     if(FAILED(hres))
1523         return hres;
1524
1525     hres = exprval_to_value(ctx, &exprval, ei, &member);
1526     exprval_release(&exprval);
1527     if(FAILED(hres))
1528         return hres;
1529
1530     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1531     if(SUCCEEDED(hres)) {
1532         hres = exprval_to_value(ctx, &exprval, ei, &val);
1533         exprval_release(&exprval);
1534     }
1535
1536     if(SUCCEEDED(hres)) {
1537         hres = to_object(ctx, &member, &obj);
1538         if(FAILED(hres))
1539             VariantClear(&val);
1540     }
1541     VariantClear(&member);
1542     if(SUCCEEDED(hres)) {
1543         hres = to_string(ctx, &val, ei, &str);
1544         VariantClear(&val);
1545         if(SUCCEEDED(hres)) {
1546             hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1547             SysFreeString(str);
1548         }
1549
1550         if(SUCCEEDED(hres)) {
1551             exprval_set_idref(ret, obj, id);
1552         }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1553             exprval_init(ret);
1554             hres = S_OK;
1555         }
1556
1557         IDispatch_Release(obj);
1558     }
1559
1560     return hres;
1561 }
1562
1563 /* ECMA-262 3rd Edition    11.2.1 */
1564 HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1565 {
1566     member_expression_t *expr = (member_expression_t*)_expr;
1567     IDispatch *obj = NULL;
1568     exprval_t exprval;
1569     VARIANT member;
1570     DISPID id;
1571     BSTR str;
1572     HRESULT hres;
1573
1574     TRACE("\n");
1575
1576     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1577     if(FAILED(hres))
1578         return hres;
1579
1580     hres = exprval_to_value(ctx, &exprval, ei, &member);
1581     exprval_release(&exprval);
1582     if(FAILED(hres))
1583         return hres;
1584
1585     hres = to_object(ctx, &member, &obj);
1586     VariantClear(&member);
1587     if(FAILED(hres))
1588         return hres;
1589
1590     str = SysAllocString(expr->identifier);
1591     if(!str) {
1592         IDispatch_Release(obj);
1593         return E_OUTOFMEMORY;
1594     }
1595
1596     hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1597     SysFreeString(str);
1598     if(SUCCEEDED(hres)) {
1599         exprval_set_idref(ret, obj, id);
1600     }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1601         exprval_init(ret);
1602         hres = S_OK;
1603     }
1604
1605     IDispatch_Release(obj);
1606     return hres;
1607 }
1608
1609 /* ECMA-262 3rd Edition    11.2.1 */
1610 static HRESULT interp_member(exec_ctx_t *ctx)
1611 {
1612     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1613     IDispatch *obj;
1614     VARIANT v;
1615     DISPID id;
1616     HRESULT hres;
1617
1618     TRACE("\n");
1619
1620     hres = stack_pop_object(ctx, &obj);
1621     if(FAILED(hres))
1622         return hres;
1623
1624     hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
1625     if(SUCCEEDED(hres)) {
1626         hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1627     }else if(hres == DISP_E_UNKNOWNNAME) {
1628         V_VT(&v) = VT_EMPTY;
1629         hres = S_OK;
1630     }
1631     IDispatch_Release(obj);
1632     if(FAILED(hres))
1633         return hres;
1634
1635     return stack_push(ctx, &v);
1636 }
1637
1638 /* ECMA-262 3rd Edition    11.2.1 */
1639 static HRESULT interp_memberid(exec_ctx_t *ctx)
1640 {
1641     VARIANT *objv, *namev;
1642     IDispatch *obj;
1643     BSTR name;
1644     DISPID id;
1645     HRESULT hres;
1646
1647     TRACE("\n");
1648
1649     namev = stack_pop(ctx);
1650     objv = stack_pop(ctx);
1651
1652     hres = to_object(ctx->parser->script, objv, &obj);
1653     VariantClear(objv);
1654     if(SUCCEEDED(hres)) {
1655         hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1656         if(FAILED(hres))
1657             IDispatch_Release(obj);
1658     }
1659     VariantClear(namev);
1660     if(FAILED(hres))
1661         return hres;
1662
1663     hres = disp_get_id(ctx->parser->script, obj, name, fdexNameEnsure, &id);
1664     SysFreeString(name);
1665     if(FAILED(hres)) {
1666         IDispatch_Release(obj);
1667         return hres;
1668     }
1669
1670     return stack_push_objid(ctx, obj, id);
1671 }
1672
1673 /* ECMA-262 3rd Edition    11.2.1 */
1674 static HRESULT interp_refval(exec_ctx_t *ctx)
1675 {
1676     IDispatch *disp;
1677     VARIANT v;
1678     DISPID id;
1679     HRESULT hres;
1680
1681     TRACE("\n");
1682
1683     disp = stack_top_objid(ctx, &id);
1684     if(!disp)
1685         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1686
1687     hres = disp_propget(ctx->parser->script, disp, id, &v, &ctx->ei, NULL/*FIXME*/);
1688     if(FAILED(hres))
1689         return hres;
1690
1691     return stack_push(ctx, &v);
1692 }
1693
1694 static void free_dp(DISPPARAMS *dp)
1695 {
1696     DWORD i;
1697
1698     for(i=0; i < dp->cArgs; i++)
1699         VariantClear(dp->rgvarg+i);
1700     heap_free(dp->rgvarg);
1701 }
1702
1703 static HRESULT args_to_param(script_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1704 {
1705     VARIANTARG *vargs;
1706     exprval_t exprval;
1707     argument_t *iter;
1708     DWORD cnt = 0, i;
1709     HRESULT hres = S_OK;
1710
1711     memset(dp, 0, sizeof(*dp));
1712     if(!args)
1713         return S_OK;
1714
1715     for(iter = args; iter; iter = iter->next)
1716         cnt++;
1717
1718     vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1719     if(!vargs)
1720         return E_OUTOFMEMORY;
1721
1722     for(i = cnt, iter = args; iter; iter = iter->next) {
1723         hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1724         if(FAILED(hres))
1725             break;
1726
1727         hres = exprval_to_value(ctx, &exprval, ei, vargs + (--i));
1728         exprval_release(&exprval);
1729         if(FAILED(hres))
1730             break;
1731     }
1732
1733     if(FAILED(hres)) {
1734         free_dp(dp);
1735         return hres;
1736     }
1737
1738     dp->rgvarg = vargs;
1739     dp->cArgs = cnt;
1740     return S_OK;
1741 }
1742
1743 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1744 {
1745     VARIANT tmp;
1746     unsigned i;
1747
1748     dp->cArgs = arg_cnt;
1749     dp->rgdispidNamedArgs = NULL;
1750     dp->cNamedArgs = 0;
1751
1752     assert(ctx->top >= arg_cnt);
1753
1754     for(i=1; i*2 <= arg_cnt; i++) {
1755         tmp = ctx->stack[ctx->top-i];
1756         ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1757         ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1758     }
1759
1760     dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1761 }
1762
1763 /* ECMA-262 3rd Edition    11.2.2 */
1764 static HRESULT interp_new(exec_ctx_t *ctx)
1765 {
1766     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1767     VARIANT *constr, v;
1768     DISPPARAMS dp;
1769     HRESULT hres;
1770
1771     TRACE("%d\n", arg);
1772
1773     constr = stack_topn(ctx, arg);
1774
1775     /* NOTE: Should use to_object here */
1776
1777     if(V_VT(constr) == VT_NULL)
1778         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1779     else if(V_VT(constr) != VT_DISPATCH)
1780         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1781     else if(!V_DISPATCH(constr))
1782         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1783
1784     jsstack_to_dp(ctx, arg, &dp);
1785     hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1786             DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1787     if(FAILED(hres))
1788         return hres;
1789
1790     stack_popn(ctx, arg+1);
1791     return stack_push(ctx, &v);
1792 }
1793
1794 /* ECMA-262 3rd Edition    11.2.3 */
1795 HRESULT call_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1796 {
1797     call_expression_t *expr = (call_expression_t*)_expr;
1798     VARIANT var;
1799     exprval_t exprval;
1800     DISPPARAMS dp;
1801     HRESULT hres;
1802
1803     TRACE("\n");
1804
1805     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1806     if(FAILED(hres))
1807         return hres;
1808
1809     hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1810     if(SUCCEEDED(hres)) {
1811         switch(exprval.type) {
1812         case EXPRVAL_VARIANT:
1813             if(V_VT(&exprval.u.var) == VT_DISPATCH)
1814                 hres = disp_call(ctx, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1815                         DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1816             else
1817                 hres = throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1818             break;
1819         case EXPRVAL_IDREF:
1820             hres = disp_call(ctx, exprval.u.idref.disp, exprval.u.idref.id,
1821                     DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1822             break;
1823         case EXPRVAL_INVALID:
1824             hres = throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1825             break;
1826         default:
1827             FIXME("unimplemented type %d\n", exprval.type);
1828             hres = E_NOTIMPL;
1829         }
1830
1831         free_dp(&dp);
1832     }
1833
1834     exprval_release(&exprval);
1835     if(FAILED(hres))
1836         return hres;
1837
1838     ret->type = EXPRVAL_VARIANT;
1839     if(flags & EXPR_NOVAL) {
1840         V_VT(&ret->u.var) = VT_EMPTY;
1841     }else {
1842         TRACE("= %s\n", debugstr_variant(&var));
1843         ret->u.var = var;
1844     }
1845     return S_OK;
1846 }
1847
1848 /* ECMA-262 3rd Edition    11.1.1 */
1849 static HRESULT interp_this(exec_ctx_t *ctx)
1850 {
1851     VARIANT v;
1852
1853     TRACE("\n");
1854
1855     V_VT(&v) = VT_DISPATCH;
1856     V_DISPATCH(&v) = ctx->this_obj;
1857     IDispatch_AddRef(ctx->this_obj);
1858     return stack_push(ctx, &v);
1859 }
1860
1861 /* ECMA-262 3rd Edition    10.1.4 */
1862 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1863 {
1864     identifier_expression_t *expr = (identifier_expression_t*)_expr;
1865     BSTR identifier;
1866     HRESULT hres;
1867
1868     TRACE("\n");
1869
1870     identifier = SysAllocString(expr->identifier);
1871     if(!identifier)
1872         return E_OUTOFMEMORY;
1873
1874     hres = identifier_eval(ctx, identifier, flags, ei, ret);
1875
1876     SysFreeString(identifier);
1877     return hres;
1878 }
1879
1880 /* ECMA-262 3rd Edition    10.1.4 */
1881 static HRESULT interp_ident(exec_ctx_t *ctx)
1882 {
1883     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1884     exprval_t exprval;
1885     VARIANT v;
1886     HRESULT hres;
1887
1888     TRACE("%s\n", debugstr_w(arg));
1889
1890     hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1891     if(FAILED(hres))
1892         return hres;
1893
1894     hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1895     exprval_release(&exprval);
1896     if(FAILED(hres))
1897         return hres;
1898
1899     return stack_push(ctx, &v);
1900 }
1901
1902 /* ECMA-262 3rd Edition    10.1.4 */
1903 static HRESULT interp_identid(exec_ctx_t *ctx)
1904 {
1905     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1906     exprval_t exprval;
1907     HRESULT hres;
1908
1909     TRACE("%s\n", debugstr_w(arg));
1910
1911     hres = identifier_eval(ctx->parser->script, arg, EXPR_NEWREF, &ctx->ei, &exprval);
1912     if(FAILED(hres))
1913         return hres;
1914
1915     if(exprval.type != EXPRVAL_IDREF) {
1916         WARN("invalid ref\n");
1917         exprval_release(&exprval);
1918         return stack_push_objid(ctx, NULL, -1);
1919     }
1920
1921     return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1922 }
1923
1924 /* ECMA-262 3rd Edition    7.8.1 */
1925 static HRESULT interp_null(exec_ctx_t *ctx)
1926 {
1927     VARIANT v;
1928
1929     TRACE("\n");
1930
1931     V_VT(&v) = VT_NULL;
1932     return stack_push(ctx, &v);
1933 }
1934
1935 /* ECMA-262 3rd Edition    7.8.2 */
1936 static HRESULT interp_bool(exec_ctx_t *ctx)
1937 {
1938     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1939
1940     TRACE("%s\n", arg ? "true" : "false");
1941
1942     return stack_push_bool(ctx, arg);
1943 }
1944
1945 /* ECMA-262 3rd Edition    7.8.3 */
1946 static HRESULT interp_int(exec_ctx_t *ctx)
1947 {
1948     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1949     VARIANT v;
1950
1951     TRACE("%d\n", arg);
1952
1953     V_VT(&v) = VT_I4;
1954     V_I4(&v) = arg;
1955     return stack_push(ctx, &v);
1956 }
1957
1958 /* ECMA-262 3rd Edition    7.8.3 */
1959 static HRESULT interp_double(exec_ctx_t *ctx)
1960 {
1961     const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1962     VARIANT v;
1963
1964     TRACE("%lf\n", arg);
1965
1966     V_VT(&v) = VT_R8;
1967     V_R8(&v) = arg;
1968     return stack_push(ctx, &v);
1969 }
1970
1971 /* ECMA-262 3rd Edition    7.8.4 */
1972 static HRESULT interp_str(exec_ctx_t *ctx)
1973 {
1974     const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1975     VARIANT v;
1976
1977     TRACE("%s\n", debugstr_w(str));
1978
1979     V_VT(&v) = VT_BSTR;
1980     V_BSTR(&v) = SysAllocString(str);
1981     if(!V_BSTR(&v))
1982         return E_OUTOFMEMORY;
1983
1984     return stack_push(ctx, &v);
1985 }
1986
1987 /* ECMA-262 3rd Edition    7.8 */
1988 static HRESULT interp_regexp(exec_ctx_t *ctx)
1989 {
1990     const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1991     const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1992     jsdisp_t *regexp;
1993     VARIANT v;
1994     HRESULT hres;
1995
1996     TRACE("%s %x\n", debugstr_w(source), flags);
1997
1998     hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, &regexp);
1999     if(FAILED(hres))
2000         return hres;
2001
2002     var_set_jsdisp(&v, regexp);
2003     return stack_push(ctx, &v);
2004 }
2005
2006 /* ECMA-262 3rd Edition    11.1.4 */
2007 HRESULT array_literal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2008 {
2009     array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
2010     DWORD length = 0, i = 0;
2011     array_element_t *elem;
2012     jsdisp_t *array;
2013     exprval_t exprval;
2014     VARIANT val;
2015     HRESULT hres;
2016
2017     TRACE("\n");
2018
2019     for(elem = expr->element_list; elem; elem = elem->next)
2020         length += elem->elision+1;
2021     length += expr->length;
2022
2023     hres = create_array(ctx, length, &array);
2024     if(FAILED(hres))
2025         return hres;
2026
2027     for(elem = expr->element_list; elem; elem = elem->next) {
2028         i += elem->elision;
2029
2030         hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
2031         if(FAILED(hres))
2032             break;
2033
2034         hres = exprval_to_value(ctx, &exprval, ei, &val);
2035         exprval_release(&exprval);
2036         if(FAILED(hres))
2037             break;
2038
2039         hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
2040         VariantClear(&val);
2041         if(FAILED(hres))
2042             break;
2043
2044         i++;
2045     }
2046
2047     if(FAILED(hres)) {
2048         jsdisp_release(array);
2049         return hres;
2050     }
2051
2052     ret->type = EXPRVAL_VARIANT;
2053     var_set_jsdisp(&ret->u.var, array);
2054     return S_OK;
2055 }
2056
2057 /* ECMA-262 3rd Edition    11.1.5 */
2058 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2059 {
2060     property_value_expression_t *expr = (property_value_expression_t*)_expr;
2061     VARIANT val, tmp;
2062     jsdisp_t *obj;
2063     prop_val_t *iter;
2064     exprval_t exprval;
2065     BSTR name;
2066     HRESULT hres;
2067
2068     TRACE("\n");
2069
2070     hres = create_object(ctx, NULL, &obj);
2071     if(FAILED(hres))
2072         return hres;
2073
2074     for(iter = expr->property_list; iter; iter = iter->next) {
2075         hres = literal_to_var(ctx, iter->name, &tmp);
2076         if(FAILED(hres))
2077             break;
2078
2079         hres = to_string(ctx, &tmp, ei, &name);
2080         VariantClear(&tmp);
2081         if(FAILED(hres))
2082             break;
2083
2084         hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
2085         if(SUCCEEDED(hres)) {
2086             hres = exprval_to_value(ctx, &exprval, ei, &val);
2087             exprval_release(&exprval);
2088             if(SUCCEEDED(hres)) {
2089                 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
2090                 VariantClear(&val);
2091             }
2092         }
2093
2094         SysFreeString(name);
2095         if(FAILED(hres))
2096             break;
2097     }
2098
2099     if(FAILED(hres)) {
2100         jsdisp_release(obj);
2101         return hres;
2102     }
2103
2104     ret->type = EXPRVAL_VARIANT;
2105     var_set_jsdisp(&ret->u.var, obj);
2106     return S_OK;
2107 }
2108
2109 /* ECMA-262 3rd Edition    11.11 */
2110 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
2111 {
2112     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2113     VARIANT_BOOL b;
2114     HRESULT hres;
2115
2116     TRACE("\n");
2117
2118     hres = to_boolean(stack_top(ctx), &b);
2119     if(FAILED(hres))
2120         return hres;
2121
2122     if(b) {
2123         ctx->ip = arg;
2124     }else {
2125         stack_popn(ctx, 1);
2126         ctx->ip++;
2127     }
2128     return S_OK;
2129 }
2130
2131 /* ECMA-262 3rd Edition    11.11 */
2132 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2133 {
2134     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2135     VARIANT_BOOL b;
2136     HRESULT hres;
2137
2138     TRACE("\n");
2139
2140     hres = to_boolean(stack_top(ctx), &b);
2141     if(FAILED(hres))
2142         return hres;
2143
2144     if(b) {
2145         stack_popn(ctx, 1);
2146         ctx->ip++;
2147     }else {
2148         ctx->ip = arg;
2149     }
2150     return S_OK;
2151 }
2152
2153 /* ECMA-262 3rd Edition    11.10 */
2154 static HRESULT bitor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2155 {
2156     INT li, ri;
2157     HRESULT hres;
2158
2159     hres = to_int32(ctx, lval, ei, &li);
2160     if(FAILED(hres))
2161         return hres;
2162
2163     hres = to_int32(ctx, rval, ei, &ri);
2164     if(FAILED(hres))
2165         return hres;
2166
2167     V_VT(retv) = VT_I4;
2168     V_I4(retv) = li|ri;
2169     return S_OK;
2170 }
2171
2172 /* ECMA-262 3rd Edition    11.10 */
2173 static HRESULT interp_or(exec_ctx_t *ctx)
2174 {
2175     INT l, r;
2176     HRESULT hres;
2177
2178     TRACE("\n");
2179
2180     hres = stack_pop_int(ctx, &r);
2181     if(FAILED(hres))
2182         return hres;
2183
2184     hres = stack_pop_int(ctx, &l);
2185     if(FAILED(hres))
2186         return hres;
2187
2188     return stack_push_int(ctx, l|r);
2189 }
2190
2191 /* ECMA-262 3rd Edition    11.10 */
2192 static HRESULT xor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2193 {
2194     INT li, ri;
2195     HRESULT hres;
2196
2197     hres = to_int32(ctx, lval, ei, &li);
2198     if(FAILED(hres))
2199         return hres;
2200
2201     hres = to_int32(ctx, rval, ei, &ri);
2202     if(FAILED(hres))
2203         return hres;
2204
2205     V_VT(retv) = VT_I4;
2206     V_I4(retv) = li^ri;
2207     return S_OK;
2208 }
2209
2210 /* ECMA-262 3rd Edition    11.10 */
2211 static HRESULT interp_xor(exec_ctx_t *ctx)
2212 {
2213     INT l, r;
2214     HRESULT hres;
2215
2216     TRACE("\n");
2217
2218     hres = stack_pop_int(ctx, &r);
2219     if(FAILED(hres))
2220         return hres;
2221
2222     hres = stack_pop_int(ctx, &l);
2223     if(FAILED(hres))
2224         return hres;
2225
2226     return stack_push_int(ctx, l^r);
2227 }
2228
2229 /* ECMA-262 3rd Edition    11.10 */
2230 static HRESULT bitand_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2231 {
2232     INT li, ri;
2233     HRESULT hres;
2234
2235     hres = to_int32(ctx, lval, ei, &li);
2236     if(FAILED(hres))
2237         return hres;
2238
2239     hres = to_int32(ctx, rval, ei, &ri);
2240     if(FAILED(hres))
2241         return hres;
2242
2243     V_VT(retv) = VT_I4;
2244     V_I4(retv) = li&ri;
2245     return S_OK;
2246 }
2247
2248 /* ECMA-262 3rd Edition    11.10 */
2249 HRESULT binary_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2250 {
2251     binary_expression_t *expr = (binary_expression_t*)_expr;
2252
2253     TRACE("\n");
2254
2255     return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
2256 }
2257
2258 /* ECMA-262 3rd Edition    11.8.6 */
2259 static HRESULT instanceof_eval(script_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
2260 {
2261     jsdisp_t *obj, *iter, *tmp = NULL;
2262     VARIANT_BOOL ret = VARIANT_FALSE;
2263     BOOL b;
2264     VARIANT var;
2265     HRESULT hres;
2266
2267     static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2268
2269     if(V_VT(objv) != VT_DISPATCH || !V_DISPATCH(objv))
2270         return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2271
2272     obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2273     if(!obj) {
2274         FIXME("non-jsdisp objects not supported\n");
2275         return E_FAIL;
2276     }
2277
2278     if(is_class(obj, JSCLASS_FUNCTION)) {
2279         hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2280     }else {
2281         hres = throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2282     }
2283     jsdisp_release(obj);
2284     if(FAILED(hres))
2285         return hres;
2286
2287     if(V_VT(&var) == VT_DISPATCH) {
2288         if(V_VT(inst) == VT_DISPATCH)
2289             tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2290         for(iter = tmp; iter; iter = iter->prototype) {
2291             hres = disp_cmp(V_DISPATCH(&var), to_disp(iter), &b);
2292             if(FAILED(hres))
2293                 break;
2294             if(b) {
2295                 ret = VARIANT_TRUE;
2296                 break;
2297             }
2298         }
2299
2300         if(tmp)
2301             jsdisp_release(tmp);
2302     }else {
2303         FIXME("prototype is not an object\n");
2304         hres = E_FAIL;
2305     }
2306
2307     VariantClear(&var);
2308     if(FAILED(hres))
2309         return hres;
2310
2311     V_VT(retv) = VT_BOOL;
2312     V_BOOL(retv) = ret;
2313     return S_OK;
2314 }
2315
2316 /* ECMA-262 3rd Edition    11.8.6 */
2317 HRESULT instanceof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2318 {
2319     binary_expression_t *expr = (binary_expression_t*)_expr;
2320
2321     TRACE("\n");
2322
2323     return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2324 }
2325
2326 /* ECMA-262 3rd Edition    11.8.7 */
2327 static HRESULT interp_in(exec_ctx_t *ctx)
2328 {
2329     VARIANT *obj, *v;
2330     DISPID id = 0;
2331     BOOL ret;
2332     BSTR str;
2333     HRESULT hres;
2334
2335     TRACE("\n");
2336
2337     obj = stack_pop(ctx);
2338     v = stack_pop(ctx);
2339
2340     if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2341         VariantClear(obj);
2342         VariantClear(v);
2343         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2344     }
2345
2346     hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2347     VariantClear(v);
2348     if(FAILED(hres)) {
2349         IDispatch_Release(V_DISPATCH(obj));
2350         return hres;
2351     }
2352
2353     hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2354     IDispatch_Release(V_DISPATCH(obj));
2355     SysFreeString(str);
2356     if(SUCCEEDED(hres))
2357         ret = TRUE;
2358     else if(hres == DISP_E_UNKNOWNNAME)
2359         ret = FALSE;
2360     else
2361         return hres;
2362
2363     return stack_push_bool(ctx, ret);
2364 }
2365
2366 /* ECMA-262 3rd Edition    11.6.1 */
2367 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2368 {
2369     VARIANT r, l;
2370     HRESULT hres;
2371
2372     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2373     if(FAILED(hres))
2374         return hres;
2375
2376     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2377     if(FAILED(hres)) {
2378         VariantClear(&l);
2379         return hres;
2380     }
2381
2382     if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2383         BSTR lstr = NULL, rstr = NULL;
2384
2385         if(V_VT(&l) == VT_BSTR)
2386             lstr = V_BSTR(&l);
2387         else
2388             hres = to_string(ctx, &l, ei, &lstr);
2389
2390         if(SUCCEEDED(hres)) {
2391             if(V_VT(&r) == VT_BSTR)
2392                 rstr = V_BSTR(&r);
2393             else
2394                 hres = to_string(ctx, &r, ei, &rstr);
2395         }
2396
2397         if(SUCCEEDED(hres)) {
2398             int len1, len2;
2399
2400             len1 = SysStringLen(lstr);
2401             len2 = SysStringLen(rstr);
2402
2403             V_VT(retv) = VT_BSTR;
2404             V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2405             memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2406             memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2407         }
2408
2409         if(V_VT(&l) != VT_BSTR)
2410             SysFreeString(lstr);
2411         if(V_VT(&r) != VT_BSTR)
2412             SysFreeString(rstr);
2413     }else {
2414         VARIANT nl, nr;
2415
2416         hres = to_number(ctx, &l, ei, &nl);
2417         if(SUCCEEDED(hres)) {
2418             hres = to_number(ctx, &r, ei, &nr);
2419             if(SUCCEEDED(hres))
2420                 num_set_val(retv, num_val(&nl) + num_val(&nr));
2421         }
2422     }
2423
2424     VariantClear(&r);
2425     VariantClear(&l);
2426     return hres;
2427 }
2428
2429 /* ECMA-262 3rd Edition    11.6.1 */
2430 static HRESULT interp_add(exec_ctx_t *ctx)
2431 {
2432     VARIANT *l, *r, ret;
2433     HRESULT hres;
2434
2435     r = stack_pop(ctx);
2436     l = stack_pop(ctx);
2437
2438     TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2439
2440     hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2441     VariantClear(l);
2442     VariantClear(r);
2443     if(FAILED(hres))
2444         return hres;
2445
2446     return stack_push(ctx, &ret);
2447 }
2448
2449 /* ECMA-262 3rd Edition    11.6.2 */
2450 static HRESULT interp_sub(exec_ctx_t *ctx)
2451 {
2452     VARIANT l, r;
2453     HRESULT hres;
2454
2455     TRACE("\n");
2456
2457     hres = stack_pop_number(ctx, &r);
2458     if(FAILED(hres))
2459         return hres;
2460
2461     hres = stack_pop_number(ctx, &l);
2462     if(FAILED(hres))
2463         return hres;
2464
2465     return stack_push_number(ctx, num_val(&l)-num_val(&r));
2466 }
2467
2468 /* ECMA-262 3rd Edition    11.5.1 */
2469 static HRESULT interp_mul(exec_ctx_t *ctx)
2470 {
2471     VARIANT l, r;
2472     HRESULT hres;
2473
2474     TRACE("\n");
2475
2476     hres = stack_pop_number(ctx, &r);
2477     if(FAILED(hres))
2478         return hres;
2479
2480     hres = stack_pop_number(ctx, &l);
2481     if(FAILED(hres))
2482         return hres;
2483
2484     return stack_push_number(ctx, num_val(&l)*num_val(&r));
2485 }
2486
2487 /* ECMA-262 3rd Edition    11.5.2 */
2488 static HRESULT interp_div(exec_ctx_t *ctx)
2489 {
2490     VARIANT l, r;
2491     HRESULT hres;
2492
2493     TRACE("\n");
2494
2495     hres = stack_pop_number(ctx, &r);
2496     if(FAILED(hres))
2497         return hres;
2498
2499     hres = stack_pop_number(ctx, &l);
2500     if(FAILED(hres))
2501         return hres;
2502
2503     return stack_push_number(ctx, num_val(&l)/num_val(&r));
2504 }
2505
2506 /* ECMA-262 3rd Edition    11.5.3 */
2507 static HRESULT interp_mod(exec_ctx_t *ctx)
2508 {
2509     VARIANT l, r;
2510     HRESULT hres;
2511
2512     TRACE("\n");
2513
2514     hres = stack_pop_number(ctx, &r);
2515     if(FAILED(hres))
2516         return hres;
2517
2518     hres = stack_pop_number(ctx, &l);
2519     if(FAILED(hres))
2520         return hres;
2521
2522     return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2523 }
2524
2525 /* ECMA-262 3rd Edition    11.4.2 */
2526 HRESULT delete_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2527 {
2528     unary_expression_t *expr = (unary_expression_t*)_expr;
2529     VARIANT_BOOL b = VARIANT_FALSE;
2530     exprval_t exprval;
2531     HRESULT hres;
2532
2533     TRACE("\n");
2534
2535     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2536     if(FAILED(hres))
2537         return hres;
2538
2539     switch(exprval.type) {
2540     case EXPRVAL_IDREF: {
2541         IDispatchEx *dispex;
2542
2543         hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2544         if(SUCCEEDED(hres)) {
2545             hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2546             b = VARIANT_TRUE;
2547             IDispatchEx_Release(dispex);
2548         }
2549         break;
2550     }
2551     default:
2552         FIXME("unsupported type %d\n", exprval.type);
2553         hres = E_NOTIMPL;
2554     }
2555
2556     exprval_release(&exprval);
2557     if(FAILED(hres))
2558         return hres;
2559
2560     return return_bool(ret, b);
2561 }
2562
2563 /* ECMA-262 3rd Edition    11.4.2 */
2564 static HRESULT interp_delete(exec_ctx_t *ctx)
2565 {
2566     VARIANT *obj_var, *name_var;
2567     IDispatchEx *dispex;
2568     IDispatch *obj;
2569     BSTR name;
2570     BOOL ret;
2571     HRESULT hres;
2572
2573     TRACE("\n");
2574
2575     name_var = stack_pop(ctx);
2576     obj_var = stack_pop(ctx);
2577
2578     hres = to_object(ctx->parser->script, obj_var, &obj);
2579     VariantClear(obj_var);
2580     if(FAILED(hres)) {
2581         VariantClear(name_var);
2582         return hres;
2583     }
2584
2585     hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2586     VariantClear(name_var);
2587     if(FAILED(hres)) {
2588         IDispatch_Release(obj);
2589         return hres;
2590     }
2591
2592     hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2593     if(SUCCEEDED(hres)) {
2594         hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2595         ret = TRUE;
2596         IDispatchEx_Release(dispex);
2597     }else {
2598         hres = S_OK;
2599         ret = FALSE;
2600     }
2601
2602     IDispatch_Release(obj);
2603     SysFreeString(name);
2604     if(FAILED(hres))
2605         return hres;
2606
2607     return stack_push_bool(ctx, ret);
2608 }
2609
2610 /* ECMA-262 3rd Edition    11.4.2 */
2611 static HRESULT interp_void(exec_ctx_t *ctx)
2612 {
2613     VARIANT v;
2614
2615     TRACE("\n");
2616
2617     stack_popn(ctx, 1);
2618
2619     V_VT(&v) = VT_EMPTY;
2620     return stack_push(ctx, &v);
2621 }
2622
2623 /* ECMA-262 3rd Edition    11.4.3 */
2624 static HRESULT typeof_exprval(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2625 {
2626     VARIANT val;
2627     HRESULT hres;
2628
2629     static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2630     static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2631     static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2632     static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2633     static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2634     static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2635     static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
2636
2637     if(exprval->type == EXPRVAL_INVALID) {
2638         *ret = undefinedW;
2639         return S_OK;
2640     }
2641
2642     hres = exprval_to_value(ctx, exprval, ei, &val);
2643     if(FAILED(hres)) {
2644         if(exprval->type == EXPRVAL_IDREF) {
2645             *ret = unknownW;
2646             return S_OK;
2647         }
2648         return hres;
2649     }
2650
2651     switch(V_VT(&val)) {
2652     case VT_EMPTY:
2653         *ret = undefinedW;
2654         break;
2655     case VT_NULL:
2656         *ret = objectW;
2657         break;
2658     case VT_BOOL:
2659         *ret = booleanW;
2660         break;
2661     case VT_I4:
2662     case VT_R8:
2663         *ret = numberW;
2664         break;
2665     case VT_BSTR:
2666         *ret = stringW;
2667         break;
2668     case VT_DISPATCH: {
2669         jsdisp_t *dispex;
2670
2671         if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2672             *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2673             jsdisp_release(dispex);
2674         }else {
2675             *ret = objectW;
2676         }
2677         break;
2678     }
2679     default:
2680         FIXME("unhandled vt %d\n", V_VT(&val));
2681         hres = E_NOTIMPL;
2682     }
2683
2684     VariantClear(&val);
2685     return hres;
2686 }
2687
2688 HRESULT typeof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2689 {
2690     unary_expression_t *expr = (unary_expression_t*)_expr;
2691     const WCHAR *str = NULL;
2692     exprval_t exprval;
2693     HRESULT hres;
2694
2695     TRACE("\n");
2696
2697     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2698     if(FAILED(hres))
2699         return hres;
2700
2701     hres = typeof_exprval(ctx, &exprval, ei, &str);
2702     exprval_release(&exprval);
2703     if(FAILED(hres))
2704         return hres;
2705
2706     ret->type = EXPRVAL_VARIANT;
2707     V_VT(&ret->u.var) = VT_BSTR;
2708     V_BSTR(&ret->u.var) = SysAllocString(str);
2709     if(!V_BSTR(&ret->u.var))
2710         return E_OUTOFMEMORY;
2711
2712     return S_OK;
2713 }
2714
2715 /* ECMA-262 3rd Edition    11.4.7 */
2716 static HRESULT interp_minus(exec_ctx_t *ctx)
2717 {
2718     VARIANT n;
2719     HRESULT hres;
2720
2721     TRACE("\n");
2722
2723     hres = stack_pop_number(ctx, &n);
2724     if(FAILED(hres))
2725         return hres;
2726
2727     return stack_push_number(ctx, -num_val(&n));
2728 }
2729
2730 /* ECMA-262 3rd Edition    11.4.6 */
2731 static HRESULT interp_tonum(exec_ctx_t *ctx)
2732 {
2733     VARIANT *v, num;
2734     HRESULT hres;
2735
2736     TRACE("\n");
2737
2738     v = stack_pop(ctx);
2739     hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2740     VariantClear(v);
2741     if(FAILED(hres))
2742         return hres;
2743
2744     return stack_push(ctx, &num);
2745 }
2746
2747 /* ECMA-262 3rd Edition    11.3.1 */
2748 HRESULT post_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2749 {
2750     unary_expression_t *expr = (unary_expression_t*)_expr;
2751     VARIANT val, num;
2752     exprval_t exprval;
2753     HRESULT hres;
2754
2755     TRACE("\n");
2756
2757     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2758     if(FAILED(hres))
2759         return hres;
2760
2761     hres = exprval_value(ctx, &exprval, ei, &val);
2762     if(SUCCEEDED(hres)) {
2763         hres = to_number(ctx, &val, ei, &num);
2764         VariantClear(&val);
2765     }
2766
2767     if(SUCCEEDED(hres)) {
2768         VARIANT inc;
2769         num_set_val(&inc, num_val(&num)+1.0);
2770         hres = put_value(ctx, &exprval, &inc, ei);
2771     }
2772
2773     exprval_release(&exprval);
2774     if(FAILED(hres))
2775         return hres;
2776
2777     ret->type = EXPRVAL_VARIANT;
2778     ret->u.var = num;
2779     return S_OK;
2780 }
2781
2782 /* ECMA-262 3rd Edition    11.3.2 */
2783 HRESULT post_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2784 {
2785     unary_expression_t *expr = (unary_expression_t*)_expr;
2786     VARIANT val, num;
2787     exprval_t exprval;
2788     HRESULT hres;
2789
2790     TRACE("\n");
2791
2792     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2793     if(FAILED(hres))
2794         return hres;
2795
2796     hres = exprval_value(ctx, &exprval, ei, &val);
2797     if(SUCCEEDED(hres)) {
2798         hres = to_number(ctx, &val, ei, &num);
2799         VariantClear(&val);
2800     }
2801
2802     if(SUCCEEDED(hres)) {
2803         VARIANT dec;
2804         num_set_val(&dec, num_val(&num)-1.0);
2805         hres = put_value(ctx, &exprval, &dec, ei);
2806     }
2807
2808     exprval_release(&exprval);
2809     if(FAILED(hres))
2810         return hres;
2811
2812     ret->type = EXPRVAL_VARIANT;
2813     ret->u.var = num;
2814     return S_OK;
2815 }
2816
2817 /* ECMA-262 3rd Edition    11.4.4 */
2818 HRESULT pre_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2819 {
2820     unary_expression_t *expr = (unary_expression_t*)_expr;
2821     VARIANT val, num;
2822     exprval_t exprval;
2823     HRESULT hres;
2824
2825     TRACE("\n");
2826
2827     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2828     if(FAILED(hres))
2829         return hres;
2830
2831     hres = exprval_value(ctx, &exprval, ei, &val);
2832     if(SUCCEEDED(hres)) {
2833         hres = to_number(ctx, &val, ei, &num);
2834         VariantClear(&val);
2835     }
2836
2837     if(SUCCEEDED(hres)) {
2838         num_set_val(&val, num_val(&num)+1.0);
2839         hres = put_value(ctx, &exprval, &val, ei);
2840     }
2841
2842     exprval_release(&exprval);
2843     if(FAILED(hres))
2844         return hres;
2845
2846     ret->type = EXPRVAL_VARIANT;
2847     ret->u.var = val;
2848     return S_OK;
2849 }
2850
2851 /* ECMA-262 3rd Edition    11.4.5 */
2852 HRESULT pre_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2853 {
2854     unary_expression_t *expr = (unary_expression_t*)_expr;
2855     VARIANT val, num;
2856     exprval_t exprval;
2857     HRESULT hres;
2858
2859     TRACE("\n");
2860
2861     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2862     if(FAILED(hres))
2863         return hres;
2864
2865     hres = exprval_value(ctx, &exprval, ei, &val);
2866     if(SUCCEEDED(hres)) {
2867         hres = to_number(ctx, &val, ei, &num);
2868         VariantClear(&val);
2869     }
2870
2871     if(SUCCEEDED(hres)) {
2872         num_set_val(&val, num_val(&num)-1.0);
2873         hres = put_value(ctx, &exprval, &val, ei);
2874     }
2875
2876     exprval_release(&exprval);
2877     if(FAILED(hres))
2878         return hres;
2879
2880     ret->type = EXPRVAL_VARIANT;
2881     ret->u.var = val;
2882     return S_OK;
2883 }
2884
2885 /* ECMA-262 3rd Edition    11.9.3 */
2886 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2887 {
2888     if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2889        return equal2_values(lval, rval, ret);
2890
2891     /* FIXME: NULL disps should be handled in more general way */
2892     if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2893         VARIANT v;
2894         V_VT(&v) = VT_NULL;
2895         return equal_values(ctx, &v, rval, ei, ret);
2896     }
2897
2898     if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2899         VARIANT v;
2900         V_VT(&v) = VT_NULL;
2901         return equal_values(ctx, lval, &v, ei, ret);
2902     }
2903
2904     if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2905        (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2906         *ret = TRUE;
2907         return S_OK;
2908     }
2909
2910     if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2911         VARIANT v;
2912         HRESULT hres;
2913
2914         hres = to_number(ctx, lval, ei, &v);
2915         if(FAILED(hres))
2916             return hres;
2917
2918         return equal_values(ctx, &v, rval, ei, ret);
2919     }
2920
2921     if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2922         VARIANT v;
2923         HRESULT hres;
2924
2925         hres = to_number(ctx, rval, ei, &v);
2926         if(FAILED(hres))
2927             return hres;
2928
2929         return equal_values(ctx, lval, &v, ei, ret);
2930     }
2931
2932     if(V_VT(rval) == VT_BOOL) {
2933         VARIANT v;
2934
2935         V_VT(&v) = VT_I4;
2936         V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2937         return equal_values(ctx, lval, &v, ei, ret);
2938     }
2939
2940     if(V_VT(lval) == VT_BOOL) {
2941         VARIANT v;
2942
2943         V_VT(&v) = VT_I4;
2944         V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2945         return equal_values(ctx, &v, rval, ei, ret);
2946     }
2947
2948
2949     if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2950         VARIANT v;
2951         HRESULT hres;
2952
2953         hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2954         if(FAILED(hres))
2955             return hres;
2956
2957         hres = equal_values(ctx, lval, &v, ei, ret);
2958
2959         VariantClear(&v);
2960         return hres;
2961     }
2962
2963
2964     if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2965         VARIANT v;
2966         HRESULT hres;
2967
2968         hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2969         if(FAILED(hres))
2970             return hres;
2971
2972         hres = equal_values(ctx, &v, rval, ei, ret);
2973
2974         VariantClear(&v);
2975         return hres;
2976     }
2977
2978
2979     *ret = FALSE;
2980     return S_OK;
2981 }
2982
2983 /* ECMA-262 3rd Edition    11.9.1 */
2984 static HRESULT interp_eq(exec_ctx_t *ctx)
2985 {
2986     VARIANT *l, *r;
2987     BOOL b;
2988     HRESULT hres;
2989
2990     r = stack_pop(ctx);
2991     l = stack_pop(ctx);
2992
2993     TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2994
2995     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2996     VariantClear(l);
2997     VariantClear(r);
2998     if(FAILED(hres))
2999         return hres;
3000
3001     return stack_push_bool(ctx, b);
3002 }
3003
3004 /* ECMA-262 3rd Edition    11.9.2 */
3005 static HRESULT interp_neq(exec_ctx_t *ctx)
3006 {
3007     VARIANT *l, *r;
3008     BOOL b;
3009     HRESULT hres;
3010
3011     r = stack_pop(ctx);
3012     l = stack_pop(ctx);
3013
3014     TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
3015
3016     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
3017     VariantClear(l);
3018     VariantClear(r);
3019     if(FAILED(hres))
3020         return hres;
3021
3022     return stack_push_bool(ctx, !b);
3023 }
3024
3025 /* ECMA-262 3rd Edition    11.9.4 */
3026 static HRESULT interp_eq2(exec_ctx_t *ctx)
3027 {
3028     VARIANT *l, *r;
3029     BOOL b;
3030     HRESULT hres;
3031
3032     TRACE("\n");
3033
3034     r = stack_pop(ctx);
3035     l = stack_pop(ctx);
3036
3037     hres = equal2_values(r, l, &b);
3038     VariantClear(l);
3039     VariantClear(r);
3040     if(FAILED(hres))
3041         return hres;
3042
3043     return stack_push_bool(ctx, b);
3044 }
3045
3046 /* ECMA-262 3rd Edition    11.9.5 */
3047 static HRESULT interp_neq2(exec_ctx_t *ctx)
3048 {
3049     VARIANT *l, *r;
3050     BOOL b;
3051     HRESULT hres;
3052
3053     TRACE("\n");
3054
3055     r = stack_pop(ctx);
3056     l = stack_pop(ctx);
3057
3058     hres = equal2_values(r, l, &b);
3059     VariantClear(l);
3060     VariantClear(r);
3061     if(FAILED(hres))
3062         return hres;
3063
3064     return stack_push_bool(ctx, !b);
3065 }
3066
3067 /* ECMA-262 3rd Edition    11.8.5 */
3068 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
3069 {
3070     VARIANT l, r, ln, rn;
3071     HRESULT hres;
3072
3073     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
3074     if(FAILED(hres))
3075         return hres;
3076
3077     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
3078     if(FAILED(hres)) {
3079         VariantClear(&l);
3080         return hres;
3081     }
3082
3083     if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
3084         *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
3085         SysFreeString(V_BSTR(&l));
3086         SysFreeString(V_BSTR(&r));
3087         return S_OK;
3088     }
3089
3090     hres = to_number(ctx, &l, ei, &ln);
3091     VariantClear(&l);
3092     if(SUCCEEDED(hres))
3093         hres = to_number(ctx, &r, ei, &rn);
3094     VariantClear(&r);
3095     if(FAILED(hres))
3096         return hres;
3097
3098     if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
3099         *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
3100     }else  {
3101         DOUBLE ld = num_val(&ln);
3102         DOUBLE rd = num_val(&rn);
3103
3104         *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
3105     }
3106
3107     return S_OK;
3108 }
3109
3110 /* ECMA-262 3rd Edition    11.8.1 */
3111 static HRESULT interp_lt(exec_ctx_t *ctx)
3112 {
3113     VARIANT *l, *r;
3114     BOOL b;
3115     HRESULT hres;
3116
3117     r = stack_pop(ctx);
3118     l = stack_pop(ctx);
3119
3120     TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
3121
3122     hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
3123     VariantClear(l);
3124     VariantClear(r);
3125     if(FAILED(hres))
3126         return hres;
3127
3128     return stack_push_bool(ctx, b);
3129 }
3130
3131 /* ECMA-262 3rd Edition    11.8.1 */
3132 static HRESULT interp_lteq(exec_ctx_t *ctx)
3133 {
3134     VARIANT *l, *r;
3135     BOOL b;
3136     HRESULT hres;
3137
3138     r = stack_pop(ctx);
3139     l = stack_pop(ctx);
3140
3141     TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
3142
3143     hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
3144     VariantClear(l);
3145     VariantClear(r);
3146     if(FAILED(hres))
3147         return hres;
3148
3149     return stack_push_bool(ctx, b);
3150 }
3151
3152 /* ECMA-262 3rd Edition    11.8.2 */
3153 static HRESULT interp_gt(exec_ctx_t *ctx)
3154 {
3155     VARIANT *l, *r;
3156     BOOL b;
3157     HRESULT hres;
3158
3159     r = stack_pop(ctx);
3160     l = stack_pop(ctx);
3161
3162     TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
3163
3164     hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
3165     VariantClear(l);
3166     VariantClear(r);
3167     if(FAILED(hres))
3168         return hres;
3169
3170     return stack_push_bool(ctx, b);
3171 }
3172
3173 /* ECMA-262 3rd Edition    11.8.4 */
3174 static HRESULT interp_gteq(exec_ctx_t *ctx)
3175 {
3176     VARIANT *l, *r;
3177     BOOL b;
3178     HRESULT hres;
3179
3180     r = stack_pop(ctx);
3181     l = stack_pop(ctx);
3182
3183     TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
3184
3185     hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
3186     VariantClear(l);
3187     VariantClear(r);
3188     if(FAILED(hres))
3189         return hres;
3190
3191     return stack_push_bool(ctx, b);
3192 }
3193
3194 /* ECMA-262 3rd Edition    11.4.8 */
3195 static HRESULT interp_bneg(exec_ctx_t *ctx)
3196 {
3197     VARIANT *v, r;
3198     INT i;
3199     HRESULT hres;
3200
3201     TRACE("\n");
3202
3203     v = stack_pop(ctx);
3204     hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3205     VariantClear(v);
3206     if(FAILED(hres))
3207         return hres;
3208
3209     V_VT(&r) = VT_I4;
3210     V_I4(&r) = ~i;
3211     return stack_push(ctx, &r);
3212 }
3213
3214 /* ECMA-262 3rd Edition    11.4.9 */
3215 static HRESULT interp_neg(exec_ctx_t *ctx)
3216 {
3217     VARIANT *v;
3218     VARIANT_BOOL b;
3219     HRESULT hres;
3220
3221     TRACE("\n");
3222
3223     v = stack_pop(ctx);
3224     hres = to_boolean(v, &b);
3225     VariantClear(v);
3226     if(FAILED(hres))
3227         return hres;
3228
3229     return stack_push_bool(ctx, !b);
3230 }
3231
3232 /* ECMA-262 3rd Edition    11.7.1 */
3233 static HRESULT lshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3234 {
3235     DWORD ri;
3236     INT li;
3237     HRESULT hres;
3238
3239     hres = to_int32(ctx, lval, ei, &li);
3240     if(FAILED(hres))
3241         return hres;
3242
3243     hres = to_uint32(ctx, rval, ei, &ri);
3244     if(FAILED(hres))
3245         return hres;
3246
3247     V_VT(retv) = VT_I4;
3248     V_I4(retv) = li << (ri&0x1f);
3249     return S_OK;
3250 }
3251
3252 /* ECMA-262 3rd Edition    11.7.1 */
3253 HRESULT left_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3254 {
3255     binary_expression_t *expr = (binary_expression_t*)_expr;
3256
3257     TRACE("\n");
3258
3259     return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3260 }
3261
3262 /* ECMA-262 3rd Edition    11.7.2 */
3263 static HRESULT rshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3264 {
3265     DWORD ri;
3266     INT li;
3267     HRESULT hres;
3268
3269     hres = to_int32(ctx, lval, ei, &li);
3270     if(FAILED(hres))
3271         return hres;
3272
3273     hres = to_uint32(ctx, rval, ei, &ri);
3274     if(FAILED(hres))
3275         return hres;
3276
3277     V_VT(retv) = VT_I4;
3278     V_I4(retv) = li >> (ri&0x1f);
3279     return S_OK;
3280 }
3281
3282 /* ECMA-262 3rd Edition    11.7.2 */
3283 HRESULT right_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3284 {
3285     binary_expression_t *expr = (binary_expression_t*)_expr;
3286
3287     TRACE("\n");
3288
3289     return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3290 }
3291
3292 /* ECMA-262 3rd Edition    11.7.3 */
3293 static HRESULT rshift2_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3294 {
3295     DWORD li, ri;
3296     HRESULT hres;
3297
3298     hres = to_uint32(ctx, lval, ei, &li);
3299     if(FAILED(hres))
3300         return hres;
3301
3302     hres = to_uint32(ctx, rval, ei, &ri);
3303     if(FAILED(hres))
3304         return hres;
3305
3306     V_VT(retv) = VT_I4;
3307     V_I4(retv) = li >> (ri&0x1f);
3308     return S_OK;
3309 }
3310
3311 /* ECMA-262 3rd Edition    11.7.3 */
3312 HRESULT right2_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3313 {
3314     binary_expression_t *expr = (binary_expression_t*)_expr;
3315
3316     TRACE("\n");
3317
3318     return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3319 }
3320
3321 /* ECMA-262 3rd Edition    11.13.1 */
3322 static HRESULT interp_assign(exec_ctx_t *ctx)
3323 {
3324     IDispatch *disp;
3325     DISPID id;
3326     VARIANT *v;
3327     HRESULT hres;
3328
3329     TRACE("\n");
3330
3331     v = stack_pop(ctx);
3332     disp = stack_pop_objid(ctx, &id);
3333
3334     if(!disp)
3335         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
3336
3337     hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
3338     IDispatch_Release(disp);
3339     if(FAILED(hres)) {
3340         VariantClear(v);
3341         return hres;
3342     }
3343
3344     return stack_push(ctx, v);
3345 }
3346
3347 /* ECMA-262 3rd Edition    11.13.2 */
3348 HRESULT assign_lshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3349 {
3350     binary_expression_t *expr = (binary_expression_t*)_expr;
3351
3352     TRACE("\n");
3353
3354     return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3355 }
3356
3357 /* ECMA-262 3rd Edition    11.13.2 */
3358 HRESULT assign_rshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3359 {
3360     binary_expression_t *expr = (binary_expression_t*)_expr;
3361
3362     TRACE("\n");
3363
3364     return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3365 }
3366
3367 /* ECMA-262 3rd Edition    11.13.2 */
3368 HRESULT assign_rrshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3369 {
3370     binary_expression_t *expr = (binary_expression_t*)_expr;
3371
3372     TRACE("\n");
3373
3374     return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3375 }
3376
3377 /* ECMA-262 3rd Edition    11.13.2 */
3378 HRESULT assign_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3379 {
3380     binary_expression_t *expr = (binary_expression_t*)_expr;
3381
3382     TRACE("\n");
3383
3384     return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3385 }
3386
3387 /* ECMA-262 3rd Edition    11.13.2 */
3388 HRESULT assign_or_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3389 {
3390     binary_expression_t *expr = (binary_expression_t*)_expr;
3391
3392     TRACE("\n");
3393
3394     return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitor_eval, ei, ret);
3395 }
3396
3397 /* ECMA-262 3rd Edition    11.13.2 */
3398 HRESULT assign_xor_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3399 {
3400     binary_expression_t *expr = (binary_expression_t*)_expr;
3401
3402     TRACE("\n");
3403
3404     return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);
3405 }
3406
3407 static HRESULT interp_jmp(exec_ctx_t *ctx)
3408 {
3409     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3410
3411     TRACE("\n");
3412
3413     ctx->ip = arg;
3414     return S_OK;
3415 }
3416
3417 static HRESULT interp_pop(exec_ctx_t *ctx)
3418 {
3419     TRACE("\n");
3420
3421     stack_popn(ctx, 1);
3422     return S_OK;
3423 }
3424
3425 static HRESULT interp_ret(exec_ctx_t *ctx)
3426 {
3427     TRACE("\n");
3428
3429     ctx->ip = -1;
3430     return S_OK;
3431 }
3432
3433 static HRESULT interp_tree(exec_ctx_t *ctx)
3434 {
3435     instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3436     exprval_t val;
3437     VARIANT v;
3438     HRESULT hres;
3439
3440     TRACE("\n");
3441
3442     hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3443     if(FAILED(hres))
3444         return hres;
3445
3446     hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3447     exprval_release(&val);
3448     if(FAILED(hres))
3449         return hres;
3450
3451     return stack_push(ctx, &v);
3452 }
3453
3454 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3455
3456 static const op_func_t op_funcs[] = {
3457 #define X(x,a,b,c) interp_##x,
3458 OP_LIST
3459 #undef X
3460 };
3461
3462 static const unsigned op_move[] = {
3463 #define X(a,x,b,c) x,
3464 OP_LIST
3465 #undef X
3466 };
3467
3468 HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3469 {
3470     exec_ctx_t *exec_ctx = ctx->exec_ctx;
3471     unsigned prev_ip, prev_top;
3472     jsop_t op;
3473     HRESULT hres = S_OK;
3474
3475     TRACE("\n");
3476
3477     prev_top = exec_ctx->top;
3478     prev_ip = exec_ctx->ip;
3479     exec_ctx->ip = expr->instr_off;
3480
3481     while(exec_ctx->ip != -1) {
3482         op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3483         hres = op_funcs[op](exec_ctx);
3484         if(FAILED(hres))
3485             break;
3486         exec_ctx->ip += op_move[op];
3487     }
3488
3489     exec_ctx->ip = prev_ip;
3490
3491     if(FAILED(hres)) {
3492         stack_popn(exec_ctx, exec_ctx->top-prev_top);
3493         *ei = exec_ctx->ei;
3494         memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3495         return hres;
3496     }
3497
3498     assert(exec_ctx->top == prev_top+1);
3499
3500     ret->type = EXPRVAL_VARIANT;
3501     ret->u.var = *stack_pop(exec_ctx);
3502     return S_OK;
3503 }
3504
3505 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3506 {
3507     HRESULT hres;
3508
3509     TRACE("\n");
3510
3511     hres = compile_subscript(ctx->exec_ctx->parser, expr, &expr->instr_off);
3512     if(FAILED(hres))
3513         return hres;
3514
3515     if(expr->eval == compiled_expression_eval)
3516         expr->eval = interp_expression_eval;
3517
3518     return expr->eval(ctx, expr, flags, ei, ret);
3519 }