jscript: Use bytecode for execution main code block in exec_source.
[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.4 */
716 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
717 {
718     expression_statement_t *stat = (expression_statement_t*)_stat;
719     VARIANT val;
720     HRESULT hres;
721
722     TRACE("\n");
723
724     hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &val);
725     if(FAILED(hres))
726         return hres;
727
728     *ret = val;
729     TRACE("= %s\n", debugstr_variant(ret));
730     return S_OK;
731 }
732
733 /* ECMA-262 3rd Edition    12.5 */
734 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
735 {
736     if_statement_t *stat = (if_statement_t*)_stat;
737     VARIANT_BOOL b;
738     VARIANT v;
739     HRESULT hres;
740
741     TRACE("\n");
742
743     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &v);
744     if(FAILED(hres))
745         return hres;
746
747     hres = to_boolean(&v, &b);
748     VariantClear(&v);
749     if(FAILED(hres))
750         return hres;
751
752     if(b)
753         hres = stat_eval(ctx, stat->if_stat, rt, ret);
754     else if(stat->else_stat)
755         hres = stat_eval(ctx, stat->else_stat, rt, ret);
756     else
757         V_VT(ret) = VT_EMPTY;
758
759     return hres;
760 }
761
762 /* ECMA-262 3rd Edition    12.6.2 */
763 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
764 {
765     while_statement_t *stat = (while_statement_t*)_stat;
766     VARIANT val, tmp;
767     VARIANT_BOOL b;
768     BOOL test_expr;
769     HRESULT hres;
770
771     TRACE("\n");
772
773     V_VT(&val) = VT_EMPTY;
774     test_expr = !stat->do_while;
775
776     while(1) {
777         if(test_expr) {
778             hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &tmp);
779             if(FAILED(hres))
780                 break;
781
782             hres = to_boolean(&tmp, &b);
783             VariantClear(&tmp);
784             if(FAILED(hres) || !b)
785                 break;
786         }else {
787             test_expr = TRUE;
788         }
789
790         hres = stat_eval(ctx, stat->statement, rt, &tmp);
791         if(FAILED(hres))
792             break;
793
794         VariantClear(&val);
795         val = tmp;
796
797         if(rt->type == RT_CONTINUE)
798             rt->type = RT_NORMAL;
799         if(rt->type != RT_NORMAL)
800             break;
801     }
802
803     if(FAILED(hres)) {
804         VariantClear(&val);
805         return hres;
806     }
807
808     if(rt->type == RT_BREAK)
809         rt->type = RT_NORMAL;
810
811     *ret = val;
812     return S_OK;
813 }
814
815 /* ECMA-262 3rd Edition    12.6.3 */
816 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
817 {
818     for_statement_t *stat = (for_statement_t*)_stat;
819     VARIANT val, tmp, retv;
820     VARIANT_BOOL b;
821     HRESULT hres;
822
823     TRACE("\n");
824
825     if(stat->variable_list) {
826         hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
827         if(FAILED(hres))
828             return hres;
829     }else if(stat->begin_expr) {
830         hres = expr_eval(ctx, stat->begin_expr, 0, &rt->ei, &val);
831         if(FAILED(hres))
832             return hres;
833
834         VariantClear(&val);
835     }
836
837     V_VT(&retv) = VT_EMPTY;
838
839     while(1) {
840         if(stat->expr) {
841             hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &tmp);
842             if(FAILED(hres))
843                 break;
844
845             hres = to_boolean(&tmp, &b);
846             VariantClear(&tmp);
847             if(FAILED(hres) || !b)
848                 break;
849         }
850
851         hres = stat_eval(ctx, stat->statement, rt, &tmp);
852         if(FAILED(hres))
853             break;
854
855         VariantClear(&retv);
856         retv = tmp;
857
858         if(rt->type == RT_CONTINUE)
859             rt->type = RT_NORMAL;
860         else if(rt->type != RT_NORMAL)
861             break;
862
863         if(stat->end_expr) {
864             hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &val);
865             if(FAILED(hres))
866                 break;
867
868             VariantClear(&val);
869         }
870     }
871
872     if(FAILED(hres)) {
873         VariantClear(&retv);
874         return hres;
875     }
876
877     if(rt->type == RT_BREAK)
878         rt->type = RT_NORMAL;
879
880     *ret = retv;
881     return S_OK;
882 }
883
884 static HRESULT array_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
885 static HRESULT member_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
886 static HRESULT identifier_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
887
888 /* ECMA-262 3rd Edition    12.6.4 */
889 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
890 {
891     forin_statement_t *stat = (forin_statement_t*)_stat;
892     VARIANT val, name, retv, tmp;
893     DISPID id = DISPID_STARTENUM;
894     BSTR str, identifier = NULL;
895     IDispatchEx *in_obj;
896     exprval_t exprval;
897     HRESULT hres;
898
899     TRACE("\n");
900
901     if(stat->variable) {
902         hres = variable_list_eval(ctx, stat->variable, &rt->ei);
903         if(FAILED(hres))
904             return hres;
905     }
906
907     hres = expr_eval(ctx, stat->in_expr, 0, &rt->ei, &val);
908     if(FAILED(hres))
909         return hres;
910
911     if(V_VT(&val) != VT_DISPATCH) {
912         TRACE("in vt %d\n", V_VT(&val));
913         VariantClear(&val);
914         V_VT(ret) = VT_EMPTY;
915         return S_OK;
916     }
917
918     hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
919     IDispatch_Release(V_DISPATCH(&val));
920     if(FAILED(hres)) {
921         TRACE("Object doesn't support IDispatchEx\n");
922         V_VT(ret) = VT_EMPTY;
923         return S_OK;
924     }
925
926     V_VT(&retv) = VT_EMPTY;
927
928     if(stat->variable)
929         identifier = SysAllocString(stat->variable->identifier);
930
931     while(1) {
932         hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
933         if(FAILED(hres) || hres == S_FALSE)
934             break;
935
936         hres = IDispatchEx_GetMemberName(in_obj, id, &str);
937         if(FAILED(hres))
938             break;
939
940         TRACE("iter %s\n", debugstr_w(str));
941
942         if(stat->variable) {
943             hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
944         }else {
945             switch(stat->expr->type) {
946             case EXPR_ARRAY:
947                 hres = array_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
948                 break;
949             case EXPR_IDENT:
950                 hres = identifier_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
951                 break;
952             case EXPR_MEMBER:
953                 hres = member_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
954                 break;
955             default:
956                 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &tmp);
957                 if(FAILED(hres))
958                     break;
959
960                 VariantClear(&tmp);
961                 hres = throw_reference_error(ctx, &rt->ei, JS_E_ILLEGAL_ASSIGN, NULL);
962                 break;
963             }
964         }
965         if(SUCCEEDED(hres)) {
966             V_VT(&name) = VT_BSTR;
967             V_BSTR(&name) = str;
968             hres = put_value(ctx, &exprval, &name, &rt->ei);
969             exprval_release(&exprval);
970         }
971         SysFreeString(str);
972         if(FAILED(hres))
973             break;
974
975         hres = stat_eval(ctx, stat->statement, rt, &tmp);
976         if(FAILED(hres))
977             break;
978
979         VariantClear(&retv);
980         retv = tmp;
981
982         if(rt->type == RT_CONTINUE)
983             rt->type = RT_NORMAL;
984         else if(rt->type != RT_NORMAL)
985             break;
986     }
987
988     SysFreeString(identifier);
989     IDispatchEx_Release(in_obj);
990     if(FAILED(hres)) {
991         VariantClear(&retv);
992         return hres;
993     }
994
995     if(rt->type == RT_BREAK)
996         rt->type = RT_NORMAL;
997
998     *ret = retv;
999     return S_OK;
1000 }
1001
1002 /* ECMA-262 3rd Edition    12.7 */
1003 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1004 {
1005     branch_statement_t *stat = (branch_statement_t*)_stat;
1006
1007     TRACE("\n");
1008
1009     if(stat->identifier) {
1010         FIXME("indentifier not implemented\n");
1011         return E_NOTIMPL;
1012     }
1013
1014     rt->type = RT_CONTINUE;
1015     V_VT(ret) = VT_EMPTY;
1016     return S_OK;
1017 }
1018
1019 /* ECMA-262 3rd Edition    12.8 */
1020 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1021 {
1022     branch_statement_t *stat = (branch_statement_t*)_stat;
1023
1024     TRACE("\n");
1025
1026     if(stat->identifier) {
1027         FIXME("indentifier not implemented\n");
1028         return E_NOTIMPL;
1029     }
1030
1031     rt->type = RT_BREAK;
1032     V_VT(ret) = VT_EMPTY;
1033     return S_OK;
1034 }
1035
1036 /* ECMA-262 3rd Edition    12.9 */
1037 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1038 {
1039     expression_statement_t *stat = (expression_statement_t*)_stat;
1040     HRESULT hres;
1041
1042     TRACE("\n");
1043
1044     if(stat->expr) {
1045         hres = expr_eval(ctx, stat->expr, 0, &rt->ei, ret);
1046         if(FAILED(hres))
1047             return hres;
1048     }else {
1049         V_VT(ret) = VT_EMPTY;
1050     }
1051
1052     TRACE("= %s\n", debugstr_variant(ret));
1053     rt->type = RT_RETURN;
1054     return S_OK;
1055 }
1056
1057 /* ECMA-262 3rd Edition    12.10 */
1058 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1059 {
1060     with_statement_t *stat = (with_statement_t*)_stat;
1061     IDispatch *disp;
1062     jsdisp_t *obj;
1063     VARIANT val;
1064     HRESULT hres;
1065
1066     TRACE("\n");
1067
1068     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &val);
1069     if(FAILED(hres))
1070         return hres;
1071
1072     hres = to_object(ctx, &val, &disp);
1073     VariantClear(&val);
1074     if(FAILED(hres))
1075         return hres;
1076
1077     obj = iface_to_jsdisp((IUnknown*)disp);
1078     IDispatch_Release(disp);
1079     if(!obj) {
1080         FIXME("disp id not jsdisp\n");
1081         return E_NOTIMPL;
1082     }
1083
1084     hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1085     jsdisp_release(obj);
1086     if(FAILED(hres))
1087         return hres;
1088
1089     hres = stat_eval(ctx, stat->statement, rt, ret);
1090
1091     scope_pop(&ctx->exec_ctx->scope_chain);
1092     return hres;
1093 }
1094
1095 /* ECMA-262 3rd Edition    12.12 */
1096 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1097 {
1098     FIXME("\n");
1099     return E_NOTIMPL;
1100 }
1101
1102 /* ECMA-262 3rd Edition    12.13 */
1103 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1104 {
1105     switch_statement_t *stat = (switch_statement_t*)_stat;
1106     case_clausule_t *iter, *default_clausule = NULL;
1107     statement_t *stat_iter;
1108     VARIANT val, cval;
1109     BOOL b;
1110     HRESULT hres;
1111
1112     TRACE("\n");
1113
1114     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &val);
1115     if(FAILED(hres))
1116         return hres;
1117
1118     for(iter = stat->case_list; iter; iter = iter->next) {
1119         if(!iter->expr) {
1120             default_clausule = iter;
1121             continue;
1122         }
1123
1124         hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &cval);
1125         if(FAILED(hres))
1126             break;
1127
1128         hres = equal2_values(&val, &cval, &b);
1129         VariantClear(&cval);
1130         if(FAILED(hres) || b)
1131             break;
1132     }
1133
1134     VariantClear(&val);
1135     if(FAILED(hres))
1136         return hres;
1137
1138     if(!iter)
1139         iter = default_clausule;
1140
1141     V_VT(&val) = VT_EMPTY;
1142     if(iter) {
1143         VARIANT tmp;
1144
1145         for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1146             hres = stat_eval(ctx, stat_iter, rt, &tmp);
1147             if(FAILED(hres))
1148                 break;
1149
1150             VariantClear(&val);
1151             val = tmp;
1152
1153             if(rt->type != RT_NORMAL)
1154                 break;
1155         }
1156     }
1157
1158     if(FAILED(hres)) {
1159         VariantClear(&val);
1160         return hres;
1161     }
1162
1163     if(rt->type == RT_BREAK)
1164         rt->type = RT_NORMAL;
1165
1166     *ret = val;
1167     return S_OK;
1168 }
1169
1170 /* ECMA-262 3rd Edition    12.13 */
1171 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1172 {
1173     expression_statement_t *stat = (expression_statement_t*)_stat;
1174     VARIANT val;
1175     HRESULT hres;
1176
1177     TRACE("\n");
1178
1179     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &val);
1180     if(FAILED(hres))
1181         return hres;
1182
1183     rt->ei.var = val;
1184     return DISP_E_EXCEPTION;
1185 }
1186
1187 static HRESULT interp_throw(exec_ctx_t *ctx)
1188 {
1189     const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1190
1191     TRACE("%08x\n", arg);
1192
1193     return throw_reference_error(ctx->parser->script, &ctx->ei, arg, NULL);
1194 }
1195
1196 static HRESULT interp_throw_type(exec_ctx_t *ctx)
1197 {
1198     const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1199     const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
1200
1201     TRACE("%08x %s\n", hres, debugstr_w(str));
1202
1203     return throw_type_error(ctx->parser->script, &ctx->ei, hres, str);
1204 }
1205
1206 /* ECMA-262 3rd Edition    12.14 */
1207 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1208 {
1209     jsdisp_t *var_disp;
1210     VARIANT ex, val;
1211     HRESULT hres;
1212
1213     ex = rt->ei.var;
1214     memset(&rt->ei, 0, sizeof(jsexcept_t));
1215
1216     hres = create_dispex(ctx, NULL, NULL, &var_disp);
1217     if(SUCCEEDED(hres)) {
1218         hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1219         if(SUCCEEDED(hres)) {
1220             hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1221             if(SUCCEEDED(hres)) {
1222                 hres = stat_eval(ctx, block->statement, rt, &val);
1223                 scope_pop(&ctx->exec_ctx->scope_chain);
1224             }
1225         }
1226
1227         jsdisp_release(var_disp);
1228     }
1229
1230     VariantClear(&ex);
1231     if(FAILED(hres))
1232         return hres;
1233
1234     *ret = val;
1235     return S_OK;
1236 }
1237
1238 /* ECMA-262 3rd Edition    12.14 */
1239 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1240 {
1241     try_statement_t *stat = (try_statement_t*)_stat;
1242     VARIANT val;
1243     HRESULT hres;
1244
1245     TRACE("\n");
1246
1247     hres = stat_eval(ctx, stat->try_statement, rt, &val);
1248     if(FAILED(hres)) {
1249         TRACE("EXCEPTION\n");
1250         if(!stat->catch_block)
1251             return hres;
1252
1253         hres = catch_eval(ctx, stat->catch_block, rt, &val);
1254         if(FAILED(hres))
1255             return hres;
1256     }
1257
1258     if(stat->finally_statement) {
1259         VariantClear(&val);
1260         hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1261         if(FAILED(hres))
1262             return hres;
1263     }
1264
1265     *ret = val;
1266     return S_OK;
1267 }
1268
1269 /* ECMA-262 3rd Edition    13 */
1270 static HRESULT interp_func(exec_ctx_t *ctx)
1271 {
1272     function_expression_t *expr = ctx->parser->code->instrs[ctx->ip].arg1.func;
1273     jsdisp_t *dispex;
1274     VARIANT v;
1275     HRESULT hres;
1276
1277     TRACE("\n");
1278
1279     hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1280             expr->src_str, expr->src_len, &dispex);
1281     if(FAILED(hres))
1282         return hres;
1283
1284     var_set_jsdisp(&v, dispex);
1285     return stack_push(ctx, &v);
1286 }
1287
1288 /* ECMA-262 3rd Edition    11.2.1 */
1289 static HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1290 {
1291     binary_expression_t *expr = (binary_expression_t*)_expr;
1292     VARIANT member, val;
1293     DISPID id;
1294     BSTR str;
1295     IDispatch *obj = NULL;
1296     HRESULT hres;
1297
1298     TRACE("\n");
1299
1300     hres = expr_eval(ctx, expr->expression1, 0, ei, &member);
1301     if(FAILED(hres))
1302         return hres;
1303
1304     hres = expr_eval(ctx, expr->expression2, 0, ei, &val);
1305     if(SUCCEEDED(hres)) {
1306         hres = to_object(ctx, &member, &obj);
1307         if(FAILED(hres))
1308             VariantClear(&val);
1309     }
1310     VariantClear(&member);
1311     if(SUCCEEDED(hres)) {
1312         hres = to_string(ctx, &val, ei, &str);
1313         VariantClear(&val);
1314         if(SUCCEEDED(hres)) {
1315             hres = disp_get_id(ctx, obj, str, fdexNameEnsure, &id);
1316             SysFreeString(str);
1317         }
1318
1319         if(SUCCEEDED(hres))
1320             exprval_set_idref(ret, obj, id);
1321
1322         IDispatch_Release(obj);
1323     }
1324
1325     return hres;
1326 }
1327
1328 /* ECMA-262 3rd Edition    11.2.1 */
1329 static HRESULT interp_array(exec_ctx_t *ctx)
1330 {
1331     VARIANT v, *namev;
1332     IDispatch *obj;
1333     DISPID id;
1334     BSTR name;
1335     HRESULT hres;
1336
1337     TRACE("\n");
1338
1339     namev = stack_pop(ctx);
1340
1341     hres = stack_pop_object(ctx, &obj);
1342     if(FAILED(hres)) {
1343         VariantClear(namev);
1344         return hres;
1345     }
1346
1347     hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1348     VariantClear(namev);
1349     if(FAILED(hres)) {
1350         IDispatch_Release(obj);
1351         return hres;
1352     }
1353
1354     hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
1355     SysFreeString(name);
1356     if(SUCCEEDED(hres)) {
1357         hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1358     }else if(hres == DISP_E_UNKNOWNNAME) {
1359         V_VT(&v) = VT_EMPTY;
1360         hres = S_OK;
1361     }
1362     IDispatch_Release(obj);
1363     if(FAILED(hres))
1364         return hres;
1365
1366     return stack_push(ctx, &v);
1367 }
1368
1369 /* ECMA-262 3rd Edition    11.2.1 */
1370 static HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1371 {
1372     member_expression_t *expr = (member_expression_t*)_expr;
1373     IDispatch *obj = NULL;
1374     VARIANT member;
1375     DISPID id;
1376     BSTR str;
1377     HRESULT hres;
1378
1379     TRACE("\n");
1380
1381     hres = expr_eval(ctx, expr->expression, 0, ei, &member);
1382     if(FAILED(hres))
1383         return hres;
1384
1385     hres = to_object(ctx, &member, &obj);
1386     VariantClear(&member);
1387     if(FAILED(hres))
1388         return hres;
1389
1390     str = SysAllocString(expr->identifier);
1391     if(!str) {
1392         IDispatch_Release(obj);
1393         return E_OUTOFMEMORY;
1394     }
1395
1396     hres = disp_get_id(ctx, obj, str, fdexNameEnsure, &id);
1397     SysFreeString(str);
1398     if(SUCCEEDED(hres))
1399         exprval_set_idref(ret, obj, id);
1400
1401     IDispatch_Release(obj);
1402     return hres;
1403 }
1404
1405 /* ECMA-262 3rd Edition    11.2.1 */
1406 static HRESULT interp_member(exec_ctx_t *ctx)
1407 {
1408     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1409     IDispatch *obj;
1410     VARIANT v;
1411     DISPID id;
1412     HRESULT hres;
1413
1414     TRACE("\n");
1415
1416     hres = stack_pop_object(ctx, &obj);
1417     if(FAILED(hres))
1418         return hres;
1419
1420     hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
1421     if(SUCCEEDED(hres)) {
1422         V_VT(&v) = VT_EMPTY;
1423         hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1424     }else if(hres == DISP_E_UNKNOWNNAME) {
1425         V_VT(&v) = VT_EMPTY;
1426         hres = S_OK;
1427     }
1428     IDispatch_Release(obj);
1429     if(FAILED(hres))
1430         return hres;
1431
1432     return stack_push(ctx, &v);
1433 }
1434
1435 /* ECMA-262 3rd Edition    11.2.1 */
1436 static HRESULT interp_memberid(exec_ctx_t *ctx)
1437 {
1438     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1439     VARIANT *objv, *namev;
1440     IDispatch *obj;
1441     BSTR name;
1442     DISPID id;
1443     HRESULT hres;
1444
1445     TRACE("%x\n", arg);
1446
1447     namev = stack_pop(ctx);
1448     objv = stack_pop(ctx);
1449
1450     hres = to_object(ctx->parser->script, objv, &obj);
1451     VariantClear(objv);
1452     if(SUCCEEDED(hres)) {
1453         hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1454         if(FAILED(hres))
1455             IDispatch_Release(obj);
1456     }
1457     VariantClear(namev);
1458     if(FAILED(hres))
1459         return hres;
1460
1461     hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
1462     SysFreeString(name);
1463     if(FAILED(hres)) {
1464         IDispatch_Release(obj);
1465         if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1466             obj = NULL;
1467             id = JS_E_INVALID_PROPERTY;
1468         }else {
1469             return hres;
1470         }
1471     }
1472
1473     return stack_push_objid(ctx, obj, id);
1474 }
1475
1476 /* ECMA-262 3rd Edition    11.2.1 */
1477 static HRESULT interp_refval(exec_ctx_t *ctx)
1478 {
1479     IDispatch *disp;
1480     VARIANT v;
1481     DISPID id;
1482     HRESULT hres;
1483
1484     TRACE("\n");
1485
1486     disp = stack_topn_objid(ctx, 0, &id);
1487     if(!disp)
1488         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1489
1490     hres = disp_propget(ctx->parser->script, disp, id, &v, &ctx->ei, NULL/*FIXME*/);
1491     if(FAILED(hres))
1492         return hres;
1493
1494     return stack_push(ctx, &v);
1495 }
1496
1497 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1498 {
1499     VARIANT tmp;
1500     unsigned i;
1501
1502     dp->cArgs = arg_cnt;
1503     dp->rgdispidNamedArgs = NULL;
1504     dp->cNamedArgs = 0;
1505
1506     assert(ctx->top >= arg_cnt);
1507
1508     for(i=1; i*2 <= arg_cnt; i++) {
1509         tmp = ctx->stack[ctx->top-i];
1510         ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1511         ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1512     }
1513
1514     dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1515 }
1516
1517 /* ECMA-262 3rd Edition    11.2.2 */
1518 static HRESULT interp_new(exec_ctx_t *ctx)
1519 {
1520     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1521     VARIANT *constr, v;
1522     DISPPARAMS dp;
1523     HRESULT hres;
1524
1525     TRACE("%d\n", arg);
1526
1527     constr = stack_topn(ctx, arg);
1528
1529     /* NOTE: Should use to_object here */
1530
1531     if(V_VT(constr) == VT_NULL)
1532         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1533     else if(V_VT(constr) != VT_DISPATCH)
1534         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1535     else if(!V_DISPATCH(constr))
1536         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1537
1538     jsstack_to_dp(ctx, arg, &dp);
1539     hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1540             DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1541     if(FAILED(hres))
1542         return hres;
1543
1544     stack_popn(ctx, arg+1);
1545     return stack_push(ctx, &v);
1546 }
1547
1548 /* ECMA-262 3rd Edition    11.2.3 */
1549 static HRESULT interp_call(exec_ctx_t *ctx)
1550 {
1551     const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1552     const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1553     VARIANT v, *objv;
1554     DISPPARAMS dp;
1555     HRESULT hres;
1556
1557     TRACE("%d %d\n", argn, do_ret);
1558
1559     objv = stack_topn(ctx, argn);
1560     if(V_VT(objv) != VT_DISPATCH)
1561         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1562
1563     jsstack_to_dp(ctx, argn, &dp);
1564     hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1565             do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1566     if(FAILED(hres))
1567         return hres;
1568
1569     stack_popn(ctx, argn+1);
1570     return do_ret ? stack_push(ctx, &v) : S_OK;
1571
1572 }
1573
1574 /* ECMA-262 3rd Edition    11.2.3 */
1575 static HRESULT interp_call_member(exec_ctx_t *ctx)
1576 {
1577     const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1578     const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1579     IDispatch *obj;
1580     DISPPARAMS dp;
1581     VARIANT v;
1582     DISPID id;
1583     HRESULT hres;
1584
1585     TRACE("%d %d\n", argn, do_ret);
1586
1587     obj = stack_topn_objid(ctx, argn, &id);
1588     if(!obj)
1589         return throw_type_error(ctx->parser->script, &ctx->ei, id, NULL);
1590
1591     jsstack_to_dp(ctx, argn, &dp);
1592     hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1593     if(FAILED(hres))
1594         return hres;
1595
1596     stack_popn(ctx, argn+2);
1597     return do_ret ? stack_push(ctx, &v) : S_OK;
1598
1599 }
1600
1601 /* ECMA-262 3rd Edition    11.1.1 */
1602 static HRESULT interp_this(exec_ctx_t *ctx)
1603 {
1604     VARIANT v;
1605
1606     TRACE("\n");
1607
1608     V_VT(&v) = VT_DISPATCH;
1609     V_DISPATCH(&v) = ctx->this_obj;
1610     IDispatch_AddRef(ctx->this_obj);
1611     return stack_push(ctx, &v);
1612 }
1613
1614 /* ECMA-262 3rd Edition    10.1.4 */
1615 static HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1616 {
1617     identifier_expression_t *expr = (identifier_expression_t*)_expr;
1618     BSTR identifier;
1619     HRESULT hres;
1620
1621     TRACE("\n");
1622
1623     identifier = SysAllocString(expr->identifier);
1624     if(!identifier)
1625         return E_OUTOFMEMORY;
1626
1627     hres = identifier_eval(ctx, identifier, fdexNameEnsure, ei, ret);
1628
1629     SysFreeString(identifier);
1630     return hres;
1631 }
1632
1633 /* ECMA-262 3rd Edition    10.1.4 */
1634 static HRESULT interp_ident(exec_ctx_t *ctx)
1635 {
1636     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1637     exprval_t exprval;
1638     VARIANT v;
1639     HRESULT hres;
1640
1641     TRACE("%s\n", debugstr_w(arg));
1642
1643     hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1644     if(FAILED(hres))
1645         return hres;
1646
1647     hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1648     exprval_release(&exprval);
1649     if(FAILED(hres))
1650         return hres;
1651
1652     return stack_push(ctx, &v);
1653 }
1654
1655 /* ECMA-262 3rd Edition    10.1.4 */
1656 static HRESULT interp_identid(exec_ctx_t *ctx)
1657 {
1658     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1659     const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1660     exprval_t exprval;
1661     HRESULT hres;
1662
1663     TRACE("%s %x\n", debugstr_w(arg), flags);
1664
1665     hres = identifier_eval(ctx->parser->script, arg, flags, &ctx->ei, &exprval);
1666     if(FAILED(hres))
1667         return hres;
1668
1669     if(exprval.type != EXPRVAL_IDREF) {
1670         WARN("invalid ref\n");
1671         exprval_release(&exprval);
1672         return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1673     }
1674
1675     return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1676 }
1677
1678 /* ECMA-262 3rd Edition    7.8.1 */
1679 static HRESULT interp_null(exec_ctx_t *ctx)
1680 {
1681     VARIANT v;
1682
1683     TRACE("\n");
1684
1685     V_VT(&v) = VT_NULL;
1686     return stack_push(ctx, &v);
1687 }
1688
1689 /* ECMA-262 3rd Edition    7.8.2 */
1690 static HRESULT interp_bool(exec_ctx_t *ctx)
1691 {
1692     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1693
1694     TRACE("%s\n", arg ? "true" : "false");
1695
1696     return stack_push_bool(ctx, arg);
1697 }
1698
1699 /* ECMA-262 3rd Edition    7.8.3 */
1700 static HRESULT interp_int(exec_ctx_t *ctx)
1701 {
1702     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1703     VARIANT v;
1704
1705     TRACE("%d\n", arg);
1706
1707     V_VT(&v) = VT_I4;
1708     V_I4(&v) = arg;
1709     return stack_push(ctx, &v);
1710 }
1711
1712 /* ECMA-262 3rd Edition    7.8.3 */
1713 static HRESULT interp_double(exec_ctx_t *ctx)
1714 {
1715     const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1716     VARIANT v;
1717
1718     TRACE("%lf\n", arg);
1719
1720     V_VT(&v) = VT_R8;
1721     V_R8(&v) = arg;
1722     return stack_push(ctx, &v);
1723 }
1724
1725 /* ECMA-262 3rd Edition    7.8.4 */
1726 static HRESULT interp_str(exec_ctx_t *ctx)
1727 {
1728     const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1729     VARIANT v;
1730
1731     TRACE("%s\n", debugstr_w(str));
1732
1733     V_VT(&v) = VT_BSTR;
1734     V_BSTR(&v) = SysAllocString(str);
1735     if(!V_BSTR(&v))
1736         return E_OUTOFMEMORY;
1737
1738     return stack_push(ctx, &v);
1739 }
1740
1741 /* ECMA-262 3rd Edition    7.8 */
1742 static HRESULT interp_regexp(exec_ctx_t *ctx)
1743 {
1744     const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1745     const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1746     jsdisp_t *regexp;
1747     VARIANT v;
1748     HRESULT hres;
1749
1750     TRACE("%s %x\n", debugstr_w(source), flags);
1751
1752     hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, &regexp);
1753     if(FAILED(hres))
1754         return hres;
1755
1756     var_set_jsdisp(&v, regexp);
1757     return stack_push(ctx, &v);
1758 }
1759
1760 /* ECMA-262 3rd Edition    11.1.4 */
1761 static HRESULT interp_carray(exec_ctx_t *ctx)
1762 {
1763     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1764     jsdisp_t *array;
1765     VARIANT *v, r;
1766     unsigned i;
1767     HRESULT hres;
1768
1769     TRACE("%u\n", arg);
1770
1771     hres = create_array(ctx->parser->script, arg, &array);
1772     if(FAILED(hres))
1773         return hres;
1774
1775     i = arg;
1776     while(i--) {
1777         v = stack_pop(ctx);
1778         hres = jsdisp_propput_idx(array, i, v, &ctx->ei, NULL/*FIXME*/);
1779         VariantClear(v);
1780         if(FAILED(hres)) {
1781             jsdisp_release(array);
1782             return hres;
1783         }
1784     }
1785
1786     var_set_jsdisp(&r, array);
1787     return stack_push(ctx, &r);
1788 }
1789
1790 /* ECMA-262 3rd Edition    11.1.5 */
1791 HRESULT interp_new_obj(exec_ctx_t *ctx)
1792 {
1793     jsdisp_t *obj;
1794     VARIANT v;
1795     HRESULT hres;
1796
1797     TRACE("\n");
1798
1799     hres = create_object(ctx->parser->script, NULL, &obj);
1800     if(FAILED(hres))
1801         return hres;
1802
1803     var_set_jsdisp(&v, obj);
1804     return stack_push(ctx, &v);
1805 }
1806
1807 /* ECMA-262 3rd Edition    11.1.5 */
1808 HRESULT interp_obj_prop(exec_ctx_t *ctx)
1809 {
1810     const BSTR name = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1811     jsdisp_t *obj;
1812     VARIANT *v;
1813     HRESULT hres;
1814
1815     TRACE("%s\n", debugstr_w(name));
1816
1817     v = stack_pop(ctx);
1818
1819     assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1820     obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1821
1822     hres = jsdisp_propput_name(obj, name, v, &ctx->ei, NULL/*FIXME*/);
1823     VariantClear(v);
1824     return hres;
1825 }
1826
1827 /* ECMA-262 3rd Edition    11.11 */
1828 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
1829 {
1830     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1831     VARIANT_BOOL b;
1832     HRESULT hres;
1833
1834     TRACE("\n");
1835
1836     hres = to_boolean(stack_top(ctx), &b);
1837     if(FAILED(hres))
1838         return hres;
1839
1840     if(b) {
1841         ctx->ip = arg;
1842     }else {
1843         stack_popn(ctx, 1);
1844         ctx->ip++;
1845     }
1846     return S_OK;
1847 }
1848
1849 /* ECMA-262 3rd Edition    11.11 */
1850 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
1851 {
1852     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1853     VARIANT_BOOL b;
1854     HRESULT hres;
1855
1856     TRACE("\n");
1857
1858     hres = to_boolean(stack_top(ctx), &b);
1859     if(FAILED(hres))
1860         return hres;
1861
1862     if(b) {
1863         stack_popn(ctx, 1);
1864         ctx->ip++;
1865     }else {
1866         ctx->ip = arg;
1867     }
1868     return S_OK;
1869 }
1870
1871 /* ECMA-262 3rd Edition    11.10 */
1872 static HRESULT interp_or(exec_ctx_t *ctx)
1873 {
1874     INT l, r;
1875     HRESULT hres;
1876
1877     TRACE("\n");
1878
1879     hres = stack_pop_int(ctx, &r);
1880     if(FAILED(hres))
1881         return hres;
1882
1883     hres = stack_pop_int(ctx, &l);
1884     if(FAILED(hres))
1885         return hres;
1886
1887     return stack_push_int(ctx, l|r);
1888 }
1889
1890 /* ECMA-262 3rd Edition    11.10 */
1891 static HRESULT interp_xor(exec_ctx_t *ctx)
1892 {
1893     INT l, r;
1894     HRESULT hres;
1895
1896     TRACE("\n");
1897
1898     hres = stack_pop_int(ctx, &r);
1899     if(FAILED(hres))
1900         return hres;
1901
1902     hres = stack_pop_int(ctx, &l);
1903     if(FAILED(hres))
1904         return hres;
1905
1906     return stack_push_int(ctx, l^r);
1907 }
1908
1909 /* ECMA-262 3rd Edition    11.10 */
1910 static HRESULT interp_and(exec_ctx_t *ctx)
1911 {
1912     INT l, r;
1913     HRESULT hres;
1914
1915     TRACE("\n");
1916
1917     hres = stack_pop_int(ctx, &r);
1918     if(FAILED(hres))
1919         return hres;
1920
1921     hres = stack_pop_int(ctx, &l);
1922     if(FAILED(hres))
1923         return hres;
1924
1925     return stack_push_int(ctx, l&r);
1926 }
1927
1928 /* ECMA-262 3rd Edition    11.8.6 */
1929 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1930 {
1931     jsdisp_t *obj, *iter, *tmp = NULL;
1932     VARIANT prot, *v;
1933     BOOL ret = FALSE;
1934     HRESULT hres;
1935
1936     static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1937
1938     v = stack_pop(ctx);
1939     if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1940         VariantClear(v);
1941         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1942     }
1943
1944     obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1945     IDispatch_Release(V_DISPATCH(v));
1946     if(!obj) {
1947         FIXME("non-jsdisp objects not supported\n");
1948         return E_FAIL;
1949     }
1950
1951     if(is_class(obj, JSCLASS_FUNCTION)) {
1952         hres = jsdisp_propget_name(obj, prototypeW, &prot, &ctx->ei, NULL/*FIXME*/);
1953     }else {
1954         hres = throw_type_error(ctx->parser->script, &ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1955     }
1956     jsdisp_release(obj);
1957     if(FAILED(hres))
1958         return hres;
1959
1960     v = stack_pop(ctx);
1961
1962     if(V_VT(&prot) == VT_DISPATCH) {
1963         if(V_VT(v) == VT_DISPATCH)
1964             tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1965         for(iter = tmp; !ret && iter; iter = iter->prototype) {
1966             hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1967             if(FAILED(hres))
1968                 break;
1969         }
1970
1971         if(tmp)
1972             jsdisp_release(tmp);
1973     }else {
1974         FIXME("prototype is not an object\n");
1975         hres = E_FAIL;
1976     }
1977
1978     VariantClear(&prot);
1979     VariantClear(v);
1980     if(FAILED(hres))
1981         return hres;
1982
1983     return stack_push_bool(ctx, ret);
1984 }
1985
1986 /* ECMA-262 3rd Edition    11.8.7 */
1987 static HRESULT interp_in(exec_ctx_t *ctx)
1988 {
1989     VARIANT *obj, *v;
1990     DISPID id = 0;
1991     BOOL ret;
1992     BSTR str;
1993     HRESULT hres;
1994
1995     TRACE("\n");
1996
1997     obj = stack_pop(ctx);
1998     v = stack_pop(ctx);
1999
2000     if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2001         VariantClear(obj);
2002         VariantClear(v);
2003         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2004     }
2005
2006     hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2007     VariantClear(v);
2008     if(FAILED(hres)) {
2009         IDispatch_Release(V_DISPATCH(obj));
2010         return hres;
2011     }
2012
2013     hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2014     IDispatch_Release(V_DISPATCH(obj));
2015     SysFreeString(str);
2016     if(SUCCEEDED(hres))
2017         ret = TRUE;
2018     else if(hres == DISP_E_UNKNOWNNAME)
2019         ret = FALSE;
2020     else
2021         return hres;
2022
2023     return stack_push_bool(ctx, ret);
2024 }
2025
2026 /* ECMA-262 3rd Edition    11.6.1 */
2027 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2028 {
2029     VARIANT r, l;
2030     HRESULT hres;
2031
2032     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2033     if(FAILED(hres))
2034         return hres;
2035
2036     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2037     if(FAILED(hres)) {
2038         VariantClear(&l);
2039         return hres;
2040     }
2041
2042     if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2043         BSTR lstr = NULL, rstr = NULL;
2044
2045         if(V_VT(&l) == VT_BSTR)
2046             lstr = V_BSTR(&l);
2047         else
2048             hres = to_string(ctx, &l, ei, &lstr);
2049
2050         if(SUCCEEDED(hres)) {
2051             if(V_VT(&r) == VT_BSTR)
2052                 rstr = V_BSTR(&r);
2053             else
2054                 hres = to_string(ctx, &r, ei, &rstr);
2055         }
2056
2057         if(SUCCEEDED(hres)) {
2058             int len1, len2;
2059
2060             len1 = SysStringLen(lstr);
2061             len2 = SysStringLen(rstr);
2062
2063             V_VT(retv) = VT_BSTR;
2064             V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2065             memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2066             memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2067         }
2068
2069         if(V_VT(&l) != VT_BSTR)
2070             SysFreeString(lstr);
2071         if(V_VT(&r) != VT_BSTR)
2072             SysFreeString(rstr);
2073     }else {
2074         VARIANT nl, nr;
2075
2076         hres = to_number(ctx, &l, ei, &nl);
2077         if(SUCCEEDED(hres)) {
2078             hres = to_number(ctx, &r, ei, &nr);
2079             if(SUCCEEDED(hres))
2080                 num_set_val(retv, num_val(&nl) + num_val(&nr));
2081         }
2082     }
2083
2084     VariantClear(&r);
2085     VariantClear(&l);
2086     return hres;
2087 }
2088
2089 /* ECMA-262 3rd Edition    11.6.1 */
2090 static HRESULT interp_add(exec_ctx_t *ctx)
2091 {
2092     VARIANT *l, *r, ret;
2093     HRESULT hres;
2094
2095     r = stack_pop(ctx);
2096     l = stack_pop(ctx);
2097
2098     TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2099
2100     hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2101     VariantClear(l);
2102     VariantClear(r);
2103     if(FAILED(hres))
2104         return hres;
2105
2106     return stack_push(ctx, &ret);
2107 }
2108
2109 /* ECMA-262 3rd Edition    11.6.2 */
2110 static HRESULT interp_sub(exec_ctx_t *ctx)
2111 {
2112     VARIANT l, r;
2113     HRESULT hres;
2114
2115     TRACE("\n");
2116
2117     hres = stack_pop_number(ctx, &r);
2118     if(FAILED(hres))
2119         return hres;
2120
2121     hres = stack_pop_number(ctx, &l);
2122     if(FAILED(hres))
2123         return hres;
2124
2125     return stack_push_number(ctx, num_val(&l)-num_val(&r));
2126 }
2127
2128 /* ECMA-262 3rd Edition    11.5.1 */
2129 static HRESULT interp_mul(exec_ctx_t *ctx)
2130 {
2131     VARIANT l, r;
2132     HRESULT hres;
2133
2134     TRACE("\n");
2135
2136     hres = stack_pop_number(ctx, &r);
2137     if(FAILED(hres))
2138         return hres;
2139
2140     hres = stack_pop_number(ctx, &l);
2141     if(FAILED(hres))
2142         return hres;
2143
2144     return stack_push_number(ctx, num_val(&l)*num_val(&r));
2145 }
2146
2147 /* ECMA-262 3rd Edition    11.5.2 */
2148 static HRESULT interp_div(exec_ctx_t *ctx)
2149 {
2150     VARIANT l, r;
2151     HRESULT hres;
2152
2153     TRACE("\n");
2154
2155     hres = stack_pop_number(ctx, &r);
2156     if(FAILED(hres))
2157         return hres;
2158
2159     hres = stack_pop_number(ctx, &l);
2160     if(FAILED(hres))
2161         return hres;
2162
2163     return stack_push_number(ctx, num_val(&l)/num_val(&r));
2164 }
2165
2166 /* ECMA-262 3rd Edition    11.5.3 */
2167 static HRESULT interp_mod(exec_ctx_t *ctx)
2168 {
2169     VARIANT l, r;
2170     HRESULT hres;
2171
2172     TRACE("\n");
2173
2174     hres = stack_pop_number(ctx, &r);
2175     if(FAILED(hres))
2176         return hres;
2177
2178     hres = stack_pop_number(ctx, &l);
2179     if(FAILED(hres))
2180         return hres;
2181
2182     return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2183 }
2184
2185 /* ECMA-262 3rd Edition    11.4.2 */
2186 static HRESULT interp_delete(exec_ctx_t *ctx)
2187 {
2188     VARIANT *obj_var, *name_var;
2189     IDispatchEx *dispex;
2190     IDispatch *obj;
2191     BSTR name;
2192     BOOL ret;
2193     HRESULT hres;
2194
2195     TRACE("\n");
2196
2197     name_var = stack_pop(ctx);
2198     obj_var = stack_pop(ctx);
2199
2200     hres = to_object(ctx->parser->script, obj_var, &obj);
2201     VariantClear(obj_var);
2202     if(FAILED(hres)) {
2203         VariantClear(name_var);
2204         return hres;
2205     }
2206
2207     hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2208     VariantClear(name_var);
2209     if(FAILED(hres)) {
2210         IDispatch_Release(obj);
2211         return hres;
2212     }
2213
2214     hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2215     if(SUCCEEDED(hres)) {
2216         hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2217         ret = TRUE;
2218         IDispatchEx_Release(dispex);
2219     }else {
2220         hres = S_OK;
2221         ret = FALSE;
2222     }
2223
2224     IDispatch_Release(obj);
2225     SysFreeString(name);
2226     if(FAILED(hres))
2227         return hres;
2228
2229     return stack_push_bool(ctx, ret);
2230 }
2231
2232 /* ECMA-262 3rd Edition    11.4.2 */
2233 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
2234 {
2235     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2236     IDispatchEx *dispex;
2237     exprval_t exprval;
2238     BOOL ret = FALSE;
2239     HRESULT hres;
2240
2241     TRACE("%s\n", debugstr_w(arg));
2242
2243     hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2244     if(FAILED(hres))
2245         return hres;
2246
2247     if(exprval.type != EXPRVAL_IDREF) {
2248         FIXME("Unsupported exprval\n");
2249         exprval_release(&exprval);
2250         return E_NOTIMPL;
2251     }
2252
2253     hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2254     IDispatch_Release(exprval.u.idref.disp);
2255     if(SUCCEEDED(hres)) {
2256         hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2257         IDispatchEx_Release(dispex);
2258         if(FAILED(hres))
2259             return hres;
2260
2261         ret = TRUE;
2262     }
2263
2264     return stack_push_bool(ctx, ret);
2265 }
2266
2267 /* ECMA-262 3rd Edition    11.4.2 */
2268 static HRESULT interp_void(exec_ctx_t *ctx)
2269 {
2270     VARIANT v;
2271
2272     TRACE("\n");
2273
2274     stack_popn(ctx, 1);
2275
2276     V_VT(&v) = VT_EMPTY;
2277     return stack_push(ctx, &v);
2278 }
2279
2280 /* ECMA-262 3rd Edition    11.4.3 */
2281 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
2282 {
2283     switch(V_VT(v)) {
2284     case VT_EMPTY:
2285         *ret = undefinedW;
2286         break;
2287     case VT_NULL:
2288         *ret = objectW;
2289         break;
2290     case VT_BOOL:
2291         *ret = booleanW;
2292         break;
2293     case VT_I4:
2294     case VT_R8:
2295         *ret = numberW;
2296         break;
2297     case VT_BSTR:
2298         *ret = stringW;
2299         break;
2300     case VT_DISPATCH: {
2301         jsdisp_t *dispex;
2302
2303         if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
2304             *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2305             jsdisp_release(dispex);
2306         }else {
2307             *ret = objectW;
2308         }
2309         break;
2310     }
2311     default:
2312         FIXME("unhandled vt %d\n", V_VT(v));
2313         return E_NOTIMPL;
2314     }
2315
2316     return S_OK;
2317 }
2318
2319 /* ECMA-262 3rd Edition    11.4.3 */
2320 static HRESULT interp_typeofid(exec_ctx_t *ctx)
2321 {
2322     const WCHAR *ret;
2323     IDispatch *obj;
2324     VARIANT v;
2325     DISPID id;
2326     HRESULT hres;
2327
2328     static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2329
2330     TRACE("\n");
2331
2332     obj = stack_pop_objid(ctx, &id);
2333     if(!obj)
2334         return stack_push_string(ctx, undefinedW);
2335
2336     V_VT(&v) = VT_EMPTY;
2337     hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2338     IDispatch_Release(obj);
2339     if(FAILED(hres))
2340         return stack_push_string(ctx, unknownW);
2341
2342     hres = typeof_string(&v, &ret);
2343     VariantClear(&v);
2344     if(FAILED(hres))
2345         return hres;
2346
2347     return stack_push_string(ctx, ret);
2348 }
2349
2350 /* ECMA-262 3rd Edition    11.4.3 */
2351 static HRESULT interp_typeofident(exec_ctx_t *ctx)
2352 {
2353     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2354     exprval_t exprval;
2355     const WCHAR *ret;
2356     VARIANT v;
2357     HRESULT hres;
2358
2359     TRACE("%s\n", debugstr_w(arg));
2360
2361     hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
2362     if(FAILED(hres))
2363         return hres;
2364
2365     if(exprval.type == EXPRVAL_INVALID) {
2366         hres = stack_push_string(ctx, undefinedW);
2367         exprval_release(&exprval);
2368         return hres;
2369     }
2370
2371     hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
2372     exprval_release(&exprval);
2373     if(FAILED(hres))
2374         return hres;
2375
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.3 */
2385 static HRESULT interp_typeof(exec_ctx_t *ctx)
2386 {
2387     const WCHAR *ret;
2388     VARIANT *v;
2389     HRESULT hres;
2390
2391     TRACE("\n");
2392
2393     v = stack_pop(ctx);
2394     hres = typeof_string(v, &ret);
2395     VariantClear(v);
2396     if(FAILED(hres))
2397         return hres;
2398
2399     return stack_push_string(ctx, ret);
2400 }
2401
2402 /* ECMA-262 3rd Edition    11.4.7 */
2403 static HRESULT interp_minus(exec_ctx_t *ctx)
2404 {
2405     VARIANT n;
2406     HRESULT hres;
2407
2408     TRACE("\n");
2409
2410     hres = stack_pop_number(ctx, &n);
2411     if(FAILED(hres))
2412         return hres;
2413
2414     return stack_push_number(ctx, -num_val(&n));
2415 }
2416
2417 /* ECMA-262 3rd Edition    11.4.6 */
2418 static HRESULT interp_tonum(exec_ctx_t *ctx)
2419 {
2420     VARIANT *v, num;
2421     HRESULT hres;
2422
2423     TRACE("\n");
2424
2425     v = stack_pop(ctx);
2426     hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2427     VariantClear(v);
2428     if(FAILED(hres))
2429         return hres;
2430
2431     return stack_push(ctx, &num);
2432 }
2433
2434 /* ECMA-262 3rd Edition    11.3.1 */
2435 static HRESULT interp_postinc(exec_ctx_t *ctx)
2436 {
2437     const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2438     IDispatch *obj;
2439     DISPID id;
2440     VARIANT v;
2441     HRESULT hres;
2442
2443     TRACE("%d\n", arg);
2444
2445     obj = stack_pop_objid(ctx, &id);
2446     if(!obj)
2447         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2448
2449     hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2450     if(SUCCEEDED(hres)) {
2451         VARIANT n, inc;
2452
2453         hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2454         if(SUCCEEDED(hres)) {
2455             num_set_val(&inc, num_val(&n)+(double)arg);
2456             hres = disp_propput(ctx->parser->script, obj, id, &inc, &ctx->ei, NULL/*FIXME*/);
2457         }
2458         if(FAILED(hres))
2459             VariantClear(&v);
2460     }
2461     IDispatch_Release(obj);
2462     if(FAILED(hres))
2463         return hres;
2464
2465     return stack_push(ctx, &v);
2466 }
2467
2468 /* ECMA-262 3rd Edition    11.4.4, 11.4.5 */
2469 static HRESULT interp_preinc(exec_ctx_t *ctx)
2470 {
2471     const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2472     IDispatch *obj;
2473     DISPID id;
2474     VARIANT v;
2475     HRESULT hres;
2476
2477     TRACE("%d\n", arg);
2478
2479     obj = stack_pop_objid(ctx, &id);
2480     if(!obj)
2481         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2482
2483     hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2484     if(SUCCEEDED(hres)) {
2485         VARIANT n;
2486
2487         hres = to_number(ctx->parser->script, &v, &ctx->ei, &n);
2488         VariantClear(&v);
2489         if(SUCCEEDED(hres)) {
2490             num_set_val(&v, num_val(&n)+(double)arg);
2491             hres = disp_propput(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
2492         }
2493     }
2494     IDispatch_Release(obj);
2495     if(FAILED(hres))
2496         return hres;
2497
2498     return stack_push(ctx, &v);
2499 }
2500
2501 /* ECMA-262 3rd Edition    11.9.3 */
2502 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2503 {
2504     if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2505        return equal2_values(lval, rval, ret);
2506
2507     /* FIXME: NULL disps should be handled in more general way */
2508     if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2509         VARIANT v;
2510         V_VT(&v) = VT_NULL;
2511         return equal_values(ctx, &v, rval, ei, ret);
2512     }
2513
2514     if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2515         VARIANT v;
2516         V_VT(&v) = VT_NULL;
2517         return equal_values(ctx, lval, &v, ei, ret);
2518     }
2519
2520     if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2521        (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2522         *ret = TRUE;
2523         return S_OK;
2524     }
2525
2526     if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2527         VARIANT v;
2528         HRESULT hres;
2529
2530         hres = to_number(ctx, lval, ei, &v);
2531         if(FAILED(hres))
2532             return hres;
2533
2534         return equal_values(ctx, &v, rval, ei, ret);
2535     }
2536
2537     if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2538         VARIANT v;
2539         HRESULT hres;
2540
2541         hres = to_number(ctx, rval, ei, &v);
2542         if(FAILED(hres))
2543             return hres;
2544
2545         return equal_values(ctx, lval, &v, ei, ret);
2546     }
2547
2548     if(V_VT(rval) == VT_BOOL) {
2549         VARIANT v;
2550
2551         V_VT(&v) = VT_I4;
2552         V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2553         return equal_values(ctx, lval, &v, ei, ret);
2554     }
2555
2556     if(V_VT(lval) == VT_BOOL) {
2557         VARIANT v;
2558
2559         V_VT(&v) = VT_I4;
2560         V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2561         return equal_values(ctx, &v, rval, ei, ret);
2562     }
2563
2564
2565     if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2566         VARIANT v;
2567         HRESULT hres;
2568
2569         hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2570         if(FAILED(hres))
2571             return hres;
2572
2573         hres = equal_values(ctx, lval, &v, ei, ret);
2574
2575         VariantClear(&v);
2576         return hres;
2577     }
2578
2579
2580     if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2581         VARIANT v;
2582         HRESULT hres;
2583
2584         hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2585         if(FAILED(hres))
2586             return hres;
2587
2588         hres = equal_values(ctx, &v, rval, ei, ret);
2589
2590         VariantClear(&v);
2591         return hres;
2592     }
2593
2594
2595     *ret = FALSE;
2596     return S_OK;
2597 }
2598
2599 /* ECMA-262 3rd Edition    11.9.1 */
2600 static HRESULT interp_eq(exec_ctx_t *ctx)
2601 {
2602     VARIANT *l, *r;
2603     BOOL b;
2604     HRESULT hres;
2605
2606     r = stack_pop(ctx);
2607     l = stack_pop(ctx);
2608
2609     TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2610
2611     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2612     VariantClear(l);
2613     VariantClear(r);
2614     if(FAILED(hres))
2615         return hres;
2616
2617     return stack_push_bool(ctx, b);
2618 }
2619
2620 /* ECMA-262 3rd Edition    11.9.2 */
2621 static HRESULT interp_neq(exec_ctx_t *ctx)
2622 {
2623     VARIANT *l, *r;
2624     BOOL b;
2625     HRESULT hres;
2626
2627     r = stack_pop(ctx);
2628     l = stack_pop(ctx);
2629
2630     TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2631
2632     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2633     VariantClear(l);
2634     VariantClear(r);
2635     if(FAILED(hres))
2636         return hres;
2637
2638     return stack_push_bool(ctx, !b);
2639 }
2640
2641 /* ECMA-262 3rd Edition    11.9.4 */
2642 static HRESULT interp_eq2(exec_ctx_t *ctx)
2643 {
2644     VARIANT *l, *r;
2645     BOOL b;
2646     HRESULT hres;
2647
2648     TRACE("\n");
2649
2650     r = stack_pop(ctx);
2651     l = stack_pop(ctx);
2652
2653     hres = equal2_values(r, l, &b);
2654     VariantClear(l);
2655     VariantClear(r);
2656     if(FAILED(hres))
2657         return hres;
2658
2659     return stack_push_bool(ctx, b);
2660 }
2661
2662 /* ECMA-262 3rd Edition    11.9.5 */
2663 static HRESULT interp_neq2(exec_ctx_t *ctx)
2664 {
2665     VARIANT *l, *r;
2666     BOOL b;
2667     HRESULT hres;
2668
2669     TRACE("\n");
2670
2671     r = stack_pop(ctx);
2672     l = stack_pop(ctx);
2673
2674     hres = equal2_values(r, l, &b);
2675     VariantClear(l);
2676     VariantClear(r);
2677     if(FAILED(hres))
2678         return hres;
2679
2680     return stack_push_bool(ctx, !b);
2681 }
2682
2683 /* ECMA-262 3rd Edition    11.8.5 */
2684 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2685 {
2686     VARIANT l, r, ln, rn;
2687     HRESULT hres;
2688
2689     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2690     if(FAILED(hres))
2691         return hres;
2692
2693     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2694     if(FAILED(hres)) {
2695         VariantClear(&l);
2696         return hres;
2697     }
2698
2699     if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2700         *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2701         SysFreeString(V_BSTR(&l));
2702         SysFreeString(V_BSTR(&r));
2703         return S_OK;
2704     }
2705
2706     hres = to_number(ctx, &l, ei, &ln);
2707     VariantClear(&l);
2708     if(SUCCEEDED(hres))
2709         hres = to_number(ctx, &r, ei, &rn);
2710     VariantClear(&r);
2711     if(FAILED(hres))
2712         return hres;
2713
2714     if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2715         *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2716     }else  {
2717         DOUBLE ld = num_val(&ln);
2718         DOUBLE rd = num_val(&rn);
2719
2720         *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2721     }
2722
2723     return S_OK;
2724 }
2725
2726 /* ECMA-262 3rd Edition    11.8.1 */
2727 static HRESULT interp_lt(exec_ctx_t *ctx)
2728 {
2729     VARIANT *l, *r;
2730     BOOL b;
2731     HRESULT hres;
2732
2733     r = stack_pop(ctx);
2734     l = stack_pop(ctx);
2735
2736     TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2737
2738     hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
2739     VariantClear(l);
2740     VariantClear(r);
2741     if(FAILED(hres))
2742         return hres;
2743
2744     return stack_push_bool(ctx, b);
2745 }
2746
2747 /* ECMA-262 3rd Edition    11.8.1 */
2748 static HRESULT interp_lteq(exec_ctx_t *ctx)
2749 {
2750     VARIANT *l, *r;
2751     BOOL b;
2752     HRESULT hres;
2753
2754     r = stack_pop(ctx);
2755     l = stack_pop(ctx);
2756
2757     TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2758
2759     hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
2760     VariantClear(l);
2761     VariantClear(r);
2762     if(FAILED(hres))
2763         return hres;
2764
2765     return stack_push_bool(ctx, b);
2766 }
2767
2768 /* ECMA-262 3rd Edition    11.8.2 */
2769 static HRESULT interp_gt(exec_ctx_t *ctx)
2770 {
2771     VARIANT *l, *r;
2772     BOOL b;
2773     HRESULT hres;
2774
2775     r = stack_pop(ctx);
2776     l = stack_pop(ctx);
2777
2778     TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2779
2780     hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
2781     VariantClear(l);
2782     VariantClear(r);
2783     if(FAILED(hres))
2784         return hres;
2785
2786     return stack_push_bool(ctx, b);
2787 }
2788
2789 /* ECMA-262 3rd Edition    11.8.4 */
2790 static HRESULT interp_gteq(exec_ctx_t *ctx)
2791 {
2792     VARIANT *l, *r;
2793     BOOL b;
2794     HRESULT hres;
2795
2796     r = stack_pop(ctx);
2797     l = stack_pop(ctx);
2798
2799     TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2800
2801     hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
2802     VariantClear(l);
2803     VariantClear(r);
2804     if(FAILED(hres))
2805         return hres;
2806
2807     return stack_push_bool(ctx, b);
2808 }
2809
2810 /* ECMA-262 3rd Edition    11.4.8 */
2811 static HRESULT interp_bneg(exec_ctx_t *ctx)
2812 {
2813     VARIANT *v, r;
2814     INT i;
2815     HRESULT hres;
2816
2817     TRACE("\n");
2818
2819     v = stack_pop(ctx);
2820     hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
2821     VariantClear(v);
2822     if(FAILED(hres))
2823         return hres;
2824
2825     V_VT(&r) = VT_I4;
2826     V_I4(&r) = ~i;
2827     return stack_push(ctx, &r);
2828 }
2829
2830 /* ECMA-262 3rd Edition    11.4.9 */
2831 static HRESULT interp_neg(exec_ctx_t *ctx)
2832 {
2833     VARIANT *v;
2834     VARIANT_BOOL b;
2835     HRESULT hres;
2836
2837     TRACE("\n");
2838
2839     v = stack_pop(ctx);
2840     hres = to_boolean(v, &b);
2841     VariantClear(v);
2842     if(FAILED(hres))
2843         return hres;
2844
2845     return stack_push_bool(ctx, !b);
2846 }
2847
2848 /* ECMA-262 3rd Edition    11.7.1 */
2849 static HRESULT interp_lshift(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.2 */
2867 static HRESULT interp_rshift(exec_ctx_t *ctx)
2868 {
2869     DWORD r;
2870     INT l;
2871     HRESULT hres;
2872
2873     hres = stack_pop_uint(ctx, &r);
2874     if(FAILED(hres))
2875         return hres;
2876
2877     hres = stack_pop_int(ctx, &l);
2878     if(FAILED(hres))
2879         return hres;
2880
2881     return stack_push_int(ctx, l >> (r&0x1f));
2882 }
2883
2884 /* ECMA-262 3rd Edition    11.7.3 */
2885 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2886 {
2887     DWORD r, l;
2888     HRESULT hres;
2889
2890     hres = stack_pop_uint(ctx, &r);
2891     if(FAILED(hres))
2892         return hres;
2893
2894     hres = stack_pop_uint(ctx, &l);
2895     if(FAILED(hres))
2896         return hres;
2897
2898     return stack_push_int(ctx, l >> (r&0x1f));
2899 }
2900
2901 /* ECMA-262 3rd Edition    11.13.1 */
2902 static HRESULT interp_assign(exec_ctx_t *ctx)
2903 {
2904     IDispatch *disp;
2905     DISPID id;
2906     VARIANT *v;
2907     HRESULT hres;
2908
2909     TRACE("\n");
2910
2911     v = stack_pop(ctx);
2912     disp = stack_pop_objid(ctx, &id);
2913
2914     if(!disp)
2915         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2916
2917     hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
2918     IDispatch_Release(disp);
2919     if(FAILED(hres)) {
2920         VariantClear(v);
2921         return hres;
2922     }
2923
2924     return stack_push(ctx, v);
2925 }
2926
2927 static HRESULT interp_undefined(exec_ctx_t *ctx)
2928 {
2929     VARIANT v;
2930
2931     TRACE("\n");
2932
2933     V_VT(&v) = VT_EMPTY;
2934     return stack_push(ctx, &v);
2935 }
2936
2937 static HRESULT interp_jmp(exec_ctx_t *ctx)
2938 {
2939     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2940
2941     TRACE("\n");
2942
2943     ctx->ip = arg;
2944     return S_OK;
2945 }
2946
2947 static HRESULT interp_pop(exec_ctx_t *ctx)
2948 {
2949     TRACE("\n");
2950
2951     stack_popn(ctx, 1);
2952     return S_OK;
2953 }
2954
2955 static HRESULT interp_ret(exec_ctx_t *ctx)
2956 {
2957     TRACE("\n");
2958
2959     ctx->ip = -1;
2960     return S_OK;
2961 }
2962
2963 static HRESULT interp_tree(exec_ctx_t *ctx)
2964 {
2965     instr_t *instr = ctx->parser->code->instrs+ctx->ip;
2966     VARIANT v;
2967     HRESULT hres;
2968
2969     TRACE("\n");
2970
2971     hres = stat_eval(ctx->parser->script, instr->arg1.stat, ctx->rt, &v);
2972     if(FAILED(hres))
2973         return hres;
2974
2975     return stack_push(ctx, &v);
2976 }
2977
2978 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2979
2980 static const op_func_t op_funcs[] = {
2981 #define X(x,a,b,c) interp_##x,
2982 OP_LIST
2983 #undef X
2984 };
2985
2986 static const unsigned op_move[] = {
2987 #define X(a,x,b,c) x,
2988 OP_LIST
2989 #undef X
2990 };
2991
2992 HRESULT compiled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
2993 {
2994     exec_ctx_t *exec_ctx = ctx->exec_ctx;
2995     unsigned prev_ip, prev_top;
2996     return_type_t *prev_rt;
2997     jsop_t op;
2998     HRESULT hres = S_OK;
2999
3000     TRACE("\n");
3001
3002     if(stat->instr_off == -1) {
3003         hres = compile_subscript_stat(exec_ctx->parser, stat, FALSE, &stat->instr_off);
3004         if(FAILED(hres))
3005             return hres;
3006     }
3007
3008     prev_rt = exec_ctx->rt;
3009     prev_top = exec_ctx->top;
3010     prev_ip = exec_ctx->ip;
3011     exec_ctx->ip = stat->instr_off;
3012     exec_ctx->rt = rt;
3013
3014     while(exec_ctx->ip != -1 && exec_ctx->rt->type == RT_NORMAL) {
3015         op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3016         hres = op_funcs[op](exec_ctx);
3017         if(FAILED(hres))
3018             break;
3019         exec_ctx->ip += op_move[op];
3020     }
3021
3022     exec_ctx->rt = prev_rt;
3023     exec_ctx->ip = prev_ip;
3024
3025     if(FAILED(hres)) {
3026         stack_popn(exec_ctx, exec_ctx->top-prev_top);
3027         return hres;
3028     }
3029
3030     assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
3031
3032     if(exec_ctx->top == prev_top)
3033         V_VT(ret) = VT_EMPTY;
3034     else
3035         *ret = *stack_pop(exec_ctx);
3036     return S_OK;
3037 }
3038
3039 static HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, VARIANT *ret)
3040 {
3041     exec_ctx_t *exec_ctx = ctx->exec_ctx;
3042     unsigned prev_ip, prev_top;
3043     jsop_t op;
3044     HRESULT hres = S_OK;
3045
3046     TRACE("\n");
3047
3048     if(expr->instr_off == -1) {
3049         hres = compile_subscript(ctx->exec_ctx->parser, expr, !(flags & EXPR_NOVAL), &expr->instr_off);
3050         if(FAILED(hres))
3051             return hres;
3052     }
3053
3054     prev_top = exec_ctx->top;
3055     prev_ip = exec_ctx->ip;
3056     exec_ctx->ip = expr->instr_off;
3057
3058     while(exec_ctx->ip != -1) {
3059         op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3060         hres = op_funcs[op](exec_ctx);
3061         if(FAILED(hres))
3062             break;
3063         exec_ctx->ip += op_move[op];
3064     }
3065
3066     exec_ctx->ip = prev_ip;
3067
3068     if(FAILED(hres)) {
3069         stack_popn(exec_ctx, exec_ctx->top-prev_top);
3070         *ei = exec_ctx->ei;
3071         memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3072         return hres;
3073     }
3074
3075     assert(exec_ctx->top == prev_top+1 || ((flags&EXPR_NOVAL) && exec_ctx->top == prev_top));
3076
3077     if(exec_ctx->top == prev_top)
3078         V_VT(ret) = VT_EMPTY;
3079     else
3080         *ret = *stack_pop(exec_ctx);
3081     return S_OK;
3082 }