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