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