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