jscript: Store source code range in function_code_t.
[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     function_expression_t *expr;
808     jsdisp_t *dispex;
809     VARIANT v;
810     HRESULT hres;
811
812     TRACE("%d\n", func_idx);
813
814     expr = ctx->func_code->funcs[func_idx].expr;
815
816     hres = create_source_function(ctx->script, ctx->code, expr->parameter_list, ctx->func_code->funcs+func_idx,
817             ctx->scope_chain, &dispex);
818     if(FAILED(hres))
819         return hres;
820
821     var_set_jsdisp(&v, dispex);
822     return stack_push(ctx, &v);
823 }
824
825 /* ECMA-262 3rd Edition    11.2.1 */
826 static HRESULT interp_array(exec_ctx_t *ctx)
827 {
828     VARIANT v, *namev;
829     IDispatch *obj;
830     DISPID id;
831     BSTR name;
832     HRESULT hres;
833
834     TRACE("\n");
835
836     namev = stack_pop(ctx);
837
838     hres = stack_pop_object(ctx, &obj);
839     if(FAILED(hres)) {
840         VariantClear(namev);
841         return hres;
842     }
843
844     hres = to_string(ctx->script, namev, ctx->ei, &name);
845     VariantClear(namev);
846     if(FAILED(hres)) {
847         IDispatch_Release(obj);
848         return hres;
849     }
850
851     hres = disp_get_id(ctx->script, obj, name, 0, &id);
852     SysFreeString(name);
853     if(SUCCEEDED(hres)) {
854         hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
855     }else if(hres == DISP_E_UNKNOWNNAME) {
856         V_VT(&v) = VT_EMPTY;
857         hres = S_OK;
858     }
859     IDispatch_Release(obj);
860     if(FAILED(hres))
861         return hres;
862
863     return stack_push(ctx, &v);
864 }
865
866 /* ECMA-262 3rd Edition    11.2.1 */
867 static HRESULT interp_member(exec_ctx_t *ctx)
868 {
869     const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
870     IDispatch *obj;
871     VARIANT v;
872     DISPID id;
873     HRESULT hres;
874
875     TRACE("\n");
876
877     hres = stack_pop_object(ctx, &obj);
878     if(FAILED(hres))
879         return hres;
880
881     hres = disp_get_id(ctx->script, obj, arg, 0, &id);
882     if(SUCCEEDED(hres)) {
883         V_VT(&v) = VT_EMPTY;
884         hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
885     }else if(hres == DISP_E_UNKNOWNNAME) {
886         V_VT(&v) = VT_EMPTY;
887         hres = S_OK;
888     }
889     IDispatch_Release(obj);
890     if(FAILED(hres))
891         return hres;
892
893     return stack_push(ctx, &v);
894 }
895
896 /* ECMA-262 3rd Edition    11.2.1 */
897 static HRESULT interp_memberid(exec_ctx_t *ctx)
898 {
899     const unsigned arg = ctx->code->instrs[ctx->ip].arg1.lng;
900     VARIANT *objv, *namev;
901     IDispatch *obj;
902     BSTR name;
903     DISPID id;
904     HRESULT hres;
905
906     TRACE("%x\n", arg);
907
908     namev = stack_pop(ctx);
909     objv = stack_pop(ctx);
910
911     hres = to_object(ctx->script, objv, &obj);
912     VariantClear(objv);
913     if(SUCCEEDED(hres)) {
914         hres = to_string(ctx->script, namev, ctx->ei, &name);
915         if(FAILED(hres))
916             IDispatch_Release(obj);
917     }
918     VariantClear(namev);
919     if(FAILED(hres))
920         return hres;
921
922     hres = disp_get_id(ctx->script, obj, name, arg, &id);
923     SysFreeString(name);
924     if(FAILED(hres)) {
925         IDispatch_Release(obj);
926         if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
927             obj = NULL;
928             id = JS_E_INVALID_PROPERTY;
929         }else {
930             return hres;
931         }
932     }
933
934     return stack_push_objid(ctx, obj, id);
935 }
936
937 /* ECMA-262 3rd Edition    11.2.1 */
938 static HRESULT interp_refval(exec_ctx_t *ctx)
939 {
940     IDispatch *disp;
941     VARIANT v;
942     DISPID id;
943     HRESULT hres;
944
945     TRACE("\n");
946
947     disp = stack_topn_objid(ctx, 0, &id);
948     if(!disp)
949         return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
950
951     hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
952     if(FAILED(hres))
953         return hres;
954
955     return stack_push(ctx, &v);
956 }
957
958 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
959 {
960     VARIANT tmp;
961     unsigned i;
962
963     dp->cArgs = arg_cnt;
964     dp->rgdispidNamedArgs = NULL;
965     dp->cNamedArgs = 0;
966
967     assert(ctx->top >= arg_cnt);
968
969     for(i=1; i*2 <= arg_cnt; i++) {
970         tmp = ctx->stack[ctx->top-i];
971         ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
972         ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
973     }
974
975     dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
976 }
977
978 /* ECMA-262 3rd Edition    11.2.2 */
979 static HRESULT interp_new(exec_ctx_t *ctx)
980 {
981     const LONG arg = ctx->code->instrs[ctx->ip].arg1.lng;
982     VARIANT *constr, v;
983     DISPPARAMS dp;
984     HRESULT hres;
985
986     TRACE("%d\n", arg);
987
988     constr = stack_topn(ctx, arg);
989
990     /* NOTE: Should use to_object here */
991
992     if(V_VT(constr) == VT_NULL)
993         return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
994     else if(V_VT(constr) != VT_DISPATCH)
995         return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
996     else if(!V_DISPATCH(constr))
997         return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
998
999     jsstack_to_dp(ctx, arg, &dp);
1000     hres = disp_call(ctx->script, V_DISPATCH(constr), DISPID_VALUE,
1001             DISPATCH_CONSTRUCT, &dp, &v, ctx->ei);
1002     if(FAILED(hres))
1003         return hres;
1004
1005     stack_popn(ctx, arg+1);
1006     return stack_push(ctx, &v);
1007 }
1008
1009 /* ECMA-262 3rd Edition    11.2.3 */
1010 static HRESULT interp_call(exec_ctx_t *ctx)
1011 {
1012     const unsigned argn = ctx->code->instrs[ctx->ip].arg1.uint;
1013     const int do_ret = ctx->code->instrs[ctx->ip].arg2.lng;
1014     VARIANT v, *objv;
1015     DISPPARAMS dp;
1016     HRESULT hres;
1017
1018     TRACE("%d %d\n", argn, do_ret);
1019
1020     objv = stack_topn(ctx, argn);
1021     if(V_VT(objv) != VT_DISPATCH)
1022         return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1023
1024     jsstack_to_dp(ctx, argn, &dp);
1025     hres = disp_call(ctx->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1026             do_ret ? &v : NULL, ctx->ei);
1027     if(FAILED(hres))
1028         return hres;
1029
1030     stack_popn(ctx, argn+1);
1031     return do_ret ? stack_push(ctx, &v) : S_OK;
1032
1033 }
1034
1035 /* ECMA-262 3rd Edition    11.2.3 */
1036 static HRESULT interp_call_member(exec_ctx_t *ctx)
1037 {
1038     const unsigned argn = ctx->code->instrs[ctx->ip].arg1.uint;
1039     const int do_ret = ctx->code->instrs[ctx->ip].arg2.lng;
1040     IDispatch *obj;
1041     DISPPARAMS dp;
1042     VARIANT v;
1043     DISPID id;
1044     HRESULT hres;
1045
1046     TRACE("%d %d\n", argn, do_ret);
1047
1048     obj = stack_topn_objid(ctx, argn, &id);
1049     if(!obj)
1050         return throw_type_error(ctx->script, ctx->ei, id, NULL);
1051
1052     jsstack_to_dp(ctx, argn, &dp);
1053     hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, ctx->ei);
1054     if(FAILED(hres))
1055         return hres;
1056
1057     stack_popn(ctx, argn+2);
1058     return do_ret ? stack_push(ctx, &v) : S_OK;
1059
1060 }
1061
1062 /* ECMA-262 3rd Edition    11.1.1 */
1063 static HRESULT interp_this(exec_ctx_t *ctx)
1064 {
1065     VARIANT v;
1066
1067     TRACE("\n");
1068
1069     V_VT(&v) = VT_DISPATCH;
1070     V_DISPATCH(&v) = ctx->this_obj;
1071     IDispatch_AddRef(ctx->this_obj);
1072     return stack_push(ctx, &v);
1073 }
1074
1075 /* ECMA-262 3rd Edition    10.1.4 */
1076 static HRESULT interp_ident(exec_ctx_t *ctx)
1077 {
1078     const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
1079     exprval_t exprval;
1080     VARIANT v;
1081     HRESULT hres;
1082
1083     TRACE("%s\n", debugstr_w(arg));
1084
1085     hres = identifier_eval(ctx->script, arg, &exprval);
1086     if(FAILED(hres))
1087         return hres;
1088
1089     if(exprval.type == EXPRVAL_INVALID)
1090         return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1091
1092     hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1093     exprval_release(&exprval);
1094     if(FAILED(hres))
1095         return hres;
1096
1097     return stack_push(ctx, &v);
1098 }
1099
1100 /* ECMA-262 3rd Edition    10.1.4 */
1101 static HRESULT interp_identid(exec_ctx_t *ctx)
1102 {
1103     const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
1104     const unsigned flags = ctx->code->instrs[ctx->ip].arg2.uint;
1105     exprval_t exprval;
1106     HRESULT hres;
1107
1108     TRACE("%s %x\n", debugstr_w(arg), flags);
1109
1110     hres = identifier_eval(ctx->script, arg, &exprval);
1111     if(FAILED(hres))
1112         return hres;
1113
1114     if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1115         DISPID id;
1116
1117         hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1118         if(FAILED(hres))
1119             return hres;
1120
1121         exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1122     }
1123
1124     if(exprval.type != EXPRVAL_IDREF) {
1125         WARN("invalid ref\n");
1126         exprval_release(&exprval);
1127         return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1128     }
1129
1130     return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1131 }
1132
1133 /* ECMA-262 3rd Edition    7.8.1 */
1134 static HRESULT interp_null(exec_ctx_t *ctx)
1135 {
1136     VARIANT v;
1137
1138     TRACE("\n");
1139
1140     V_VT(&v) = VT_NULL;
1141     return stack_push(ctx, &v);
1142 }
1143
1144 /* ECMA-262 3rd Edition    7.8.2 */
1145 static HRESULT interp_bool(exec_ctx_t *ctx)
1146 {
1147     const LONG arg = ctx->code->instrs[ctx->ip].arg1.lng;
1148
1149     TRACE("%s\n", arg ? "true" : "false");
1150
1151     return stack_push_bool(ctx, arg);
1152 }
1153
1154 /* ECMA-262 3rd Edition    7.8.3 */
1155 static HRESULT interp_int(exec_ctx_t *ctx)
1156 {
1157     const LONG arg = ctx->code->instrs[ctx->ip].arg1.lng;
1158     VARIANT v;
1159
1160     TRACE("%d\n", arg);
1161
1162     V_VT(&v) = VT_I4;
1163     V_I4(&v) = arg;
1164     return stack_push(ctx, &v);
1165 }
1166
1167 /* ECMA-262 3rd Edition    7.8.3 */
1168 static HRESULT interp_double(exec_ctx_t *ctx)
1169 {
1170     const double arg = *ctx->code->instrs[ctx->ip].arg1.dbl;
1171     VARIANT v;
1172
1173     TRACE("%lf\n", arg);
1174
1175     V_VT(&v) = VT_R8;
1176     V_R8(&v) = arg;
1177     return stack_push(ctx, &v);
1178 }
1179
1180 /* ECMA-262 3rd Edition    7.8.4 */
1181 static HRESULT interp_str(exec_ctx_t *ctx)
1182 {
1183     const WCHAR *str = ctx->code->instrs[ctx->ip].arg1.str;
1184     VARIANT v;
1185
1186     TRACE("%s\n", debugstr_w(str));
1187
1188     V_VT(&v) = VT_BSTR;
1189     V_BSTR(&v) = SysAllocString(str);
1190     if(!V_BSTR(&v))
1191         return E_OUTOFMEMORY;
1192
1193     return stack_push(ctx, &v);
1194 }
1195
1196 /* ECMA-262 3rd Edition    7.8 */
1197 static HRESULT interp_regexp(exec_ctx_t *ctx)
1198 {
1199     const WCHAR *source = ctx->code->instrs[ctx->ip].arg1.str;
1200     const LONG flags = ctx->code->instrs[ctx->ip].arg2.lng;
1201     jsdisp_t *regexp;
1202     VARIANT v;
1203     HRESULT hres;
1204
1205     TRACE("%s %x\n", debugstr_w(source), flags);
1206
1207     hres = create_regexp(ctx->script, source, strlenW(source), flags, &regexp);
1208     if(FAILED(hres))
1209         return hres;
1210
1211     var_set_jsdisp(&v, regexp);
1212     return stack_push(ctx, &v);
1213 }
1214
1215 /* ECMA-262 3rd Edition    11.1.4 */
1216 static HRESULT interp_carray(exec_ctx_t *ctx)
1217 {
1218     const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
1219     jsdisp_t *array;
1220     VARIANT *v, r;
1221     unsigned i;
1222     HRESULT hres;
1223
1224     TRACE("%u\n", arg);
1225
1226     hres = create_array(ctx->script, arg, &array);
1227     if(FAILED(hres))
1228         return hres;
1229
1230     i = arg;
1231     while(i--) {
1232         v = stack_pop(ctx);
1233         hres = jsdisp_propput_idx(array, i, v, ctx->ei);
1234         VariantClear(v);
1235         if(FAILED(hres)) {
1236             jsdisp_release(array);
1237             return hres;
1238         }
1239     }
1240
1241     var_set_jsdisp(&r, array);
1242     return stack_push(ctx, &r);
1243 }
1244
1245 /* ECMA-262 3rd Edition    11.1.5 */
1246 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1247 {
1248     jsdisp_t *obj;
1249     VARIANT v;
1250     HRESULT hres;
1251
1252     TRACE("\n");
1253
1254     hres = create_object(ctx->script, NULL, &obj);
1255     if(FAILED(hres))
1256         return hres;
1257
1258     var_set_jsdisp(&v, obj);
1259     return stack_push(ctx, &v);
1260 }
1261
1262 /* ECMA-262 3rd Edition    11.1.5 */
1263 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1264 {
1265     const BSTR name = ctx->code->instrs[ctx->ip].arg1.bstr;
1266     jsdisp_t *obj;
1267     VARIANT *v;
1268     HRESULT hres;
1269
1270     TRACE("%s\n", debugstr_w(name));
1271
1272     v = stack_pop(ctx);
1273
1274     assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1275     obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1276
1277     hres = jsdisp_propput_name(obj, name, v, ctx->ei);
1278     VariantClear(v);
1279     return hres;
1280 }
1281
1282 /* ECMA-262 3rd Edition    11.11 */
1283 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1284 {
1285     const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
1286     VARIANT_BOOL b;
1287     HRESULT hres;
1288
1289     TRACE("\n");
1290
1291     hres = to_boolean(stack_top(ctx), &b);
1292     if(FAILED(hres))
1293         return hres;
1294
1295     if(b) {
1296         ctx->ip = arg;
1297     }else {
1298         stack_popn(ctx, 1);
1299         ctx->ip++;
1300     }
1301     return S_OK;
1302 }
1303
1304 /* ECMA-262 3rd Edition    11.11 */
1305 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1306 {
1307     const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
1308     VARIANT_BOOL b;
1309     HRESULT hres;
1310
1311     TRACE("\n");
1312
1313     hres = to_boolean(stack_top(ctx), &b);
1314     if(FAILED(hres))
1315         return hres;
1316
1317     if(b) {
1318         stack_popn(ctx, 1);
1319         ctx->ip++;
1320     }else {
1321         ctx->ip = arg;
1322     }
1323     return S_OK;
1324 }
1325
1326 /* ECMA-262 3rd Edition    11.10 */
1327 static HRESULT interp_or(exec_ctx_t *ctx)
1328 {
1329     INT l, r;
1330     HRESULT hres;
1331
1332     TRACE("\n");
1333
1334     hres = stack_pop_int(ctx, &r);
1335     if(FAILED(hres))
1336         return hres;
1337
1338     hres = stack_pop_int(ctx, &l);
1339     if(FAILED(hres))
1340         return hres;
1341
1342     return stack_push_int(ctx, l|r);
1343 }
1344
1345 /* ECMA-262 3rd Edition    11.10 */
1346 static HRESULT interp_xor(exec_ctx_t *ctx)
1347 {
1348     INT l, r;
1349     HRESULT hres;
1350
1351     TRACE("\n");
1352
1353     hres = stack_pop_int(ctx, &r);
1354     if(FAILED(hres))
1355         return hres;
1356
1357     hres = stack_pop_int(ctx, &l);
1358     if(FAILED(hres))
1359         return hres;
1360
1361     return stack_push_int(ctx, l^r);
1362 }
1363
1364 /* ECMA-262 3rd Edition    11.10 */
1365 static HRESULT interp_and(exec_ctx_t *ctx)
1366 {
1367     INT l, r;
1368     HRESULT hres;
1369
1370     TRACE("\n");
1371
1372     hres = stack_pop_int(ctx, &r);
1373     if(FAILED(hres))
1374         return hres;
1375
1376     hres = stack_pop_int(ctx, &l);
1377     if(FAILED(hres))
1378         return hres;
1379
1380     return stack_push_int(ctx, l&r);
1381 }
1382
1383 /* ECMA-262 3rd Edition    11.8.6 */
1384 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1385 {
1386     jsdisp_t *obj, *iter, *tmp = NULL;
1387     VARIANT prot, *v;
1388     BOOL ret = FALSE;
1389     HRESULT hres;
1390
1391     static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1392
1393     v = stack_pop(ctx);
1394     if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1395         VariantClear(v);
1396         return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1397     }
1398
1399     obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1400     IDispatch_Release(V_DISPATCH(v));
1401     if(!obj) {
1402         FIXME("non-jsdisp objects not supported\n");
1403         return E_FAIL;
1404     }
1405
1406     if(is_class(obj, JSCLASS_FUNCTION)) {
1407         hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1408     }else {
1409         hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1410     }
1411     jsdisp_release(obj);
1412     if(FAILED(hres))
1413         return hres;
1414
1415     v = stack_pop(ctx);
1416
1417     if(V_VT(&prot) == VT_DISPATCH) {
1418         if(V_VT(v) == VT_DISPATCH)
1419             tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1420         for(iter = tmp; !ret && iter; iter = iter->prototype) {
1421             hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1422             if(FAILED(hres))
1423                 break;
1424         }
1425
1426         if(tmp)
1427             jsdisp_release(tmp);
1428     }else {
1429         FIXME("prototype is not an object\n");
1430         hres = E_FAIL;
1431     }
1432
1433     VariantClear(&prot);
1434     VariantClear(v);
1435     if(FAILED(hres))
1436         return hres;
1437
1438     return stack_push_bool(ctx, ret);
1439 }
1440
1441 /* ECMA-262 3rd Edition    11.8.7 */
1442 static HRESULT interp_in(exec_ctx_t *ctx)
1443 {
1444     VARIANT *obj, *v;
1445     DISPID id = 0;
1446     BOOL ret;
1447     BSTR str;
1448     HRESULT hres;
1449
1450     TRACE("\n");
1451
1452     obj = stack_pop(ctx);
1453     v = stack_pop(ctx);
1454
1455     if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1456         VariantClear(obj);
1457         VariantClear(v);
1458         return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1459     }
1460
1461     hres = to_string(ctx->script, v, ctx->ei, &str);
1462     VariantClear(v);
1463     if(FAILED(hres)) {
1464         IDispatch_Release(V_DISPATCH(obj));
1465         return hres;
1466     }
1467
1468     hres = disp_get_id(ctx->script, V_DISPATCH(obj), str, 0, &id);
1469     IDispatch_Release(V_DISPATCH(obj));
1470     SysFreeString(str);
1471     if(SUCCEEDED(hres))
1472         ret = TRUE;
1473     else if(hres == DISP_E_UNKNOWNNAME)
1474         ret = FALSE;
1475     else
1476         return hres;
1477
1478     return stack_push_bool(ctx, ret);
1479 }
1480
1481 /* ECMA-262 3rd Edition    11.6.1 */
1482 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1483 {
1484     VARIANT r, l;
1485     HRESULT hres;
1486
1487     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1488     if(FAILED(hres))
1489         return hres;
1490
1491     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1492     if(FAILED(hres)) {
1493         VariantClear(&l);
1494         return hres;
1495     }
1496
1497     if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1498         BSTR lstr = NULL, rstr = NULL;
1499
1500         if(V_VT(&l) == VT_BSTR)
1501             lstr = V_BSTR(&l);
1502         else
1503             hres = to_string(ctx, &l, ei, &lstr);
1504
1505         if(SUCCEEDED(hres)) {
1506             if(V_VT(&r) == VT_BSTR)
1507                 rstr = V_BSTR(&r);
1508             else
1509                 hres = to_string(ctx, &r, ei, &rstr);
1510         }
1511
1512         if(SUCCEEDED(hres)) {
1513             int len1, len2;
1514
1515             len1 = SysStringLen(lstr);
1516             len2 = SysStringLen(rstr);
1517
1518             V_VT(retv) = VT_BSTR;
1519             V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1520             if(len1)
1521                 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1522             if(len2)
1523                 memcpy(V_BSTR(retv)+len1, rstr, len2*sizeof(WCHAR));
1524             V_BSTR(retv)[len1+len2] = 0;
1525         }
1526
1527         if(V_VT(&l) != VT_BSTR)
1528             SysFreeString(lstr);
1529         if(V_VT(&r) != VT_BSTR)
1530             SysFreeString(rstr);
1531     }else {
1532         double nl, nr;
1533
1534         hres = to_number(ctx, &l, ei, &nl);
1535         if(SUCCEEDED(hres)) {
1536             hres = to_number(ctx, &r, ei, &nr);
1537             if(SUCCEEDED(hres))
1538                 num_set_val(retv, nl + nr);
1539         }
1540     }
1541
1542     VariantClear(&r);
1543     VariantClear(&l);
1544     return hres;
1545 }
1546
1547 /* ECMA-262 3rd Edition    11.6.1 */
1548 static HRESULT interp_add(exec_ctx_t *ctx)
1549 {
1550     VARIANT *l, *r, ret;
1551     HRESULT hres;
1552
1553     r = stack_pop(ctx);
1554     l = stack_pop(ctx);
1555
1556     TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1557
1558     hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1559     VariantClear(l);
1560     VariantClear(r);
1561     if(FAILED(hres))
1562         return hres;
1563
1564     return stack_push(ctx, &ret);
1565 }
1566
1567 /* ECMA-262 3rd Edition    11.6.2 */
1568 static HRESULT interp_sub(exec_ctx_t *ctx)
1569 {
1570     double l, r;
1571     HRESULT hres;
1572
1573     TRACE("\n");
1574
1575     hres = stack_pop_number(ctx, &r);
1576     if(FAILED(hres))
1577         return hres;
1578
1579     hres = stack_pop_number(ctx, &l);
1580     if(FAILED(hres))
1581         return hres;
1582
1583     return stack_push_number(ctx, l-r);
1584 }
1585
1586 /* ECMA-262 3rd Edition    11.5.1 */
1587 static HRESULT interp_mul(exec_ctx_t *ctx)
1588 {
1589     double l, r;
1590     HRESULT hres;
1591
1592     TRACE("\n");
1593
1594     hres = stack_pop_number(ctx, &r);
1595     if(FAILED(hres))
1596         return hres;
1597
1598     hres = stack_pop_number(ctx, &l);
1599     if(FAILED(hres))
1600         return hres;
1601
1602     return stack_push_number(ctx, l*r);
1603 }
1604
1605 /* ECMA-262 3rd Edition    11.5.2 */
1606 static HRESULT interp_div(exec_ctx_t *ctx)
1607 {
1608     double l, r;
1609     HRESULT hres;
1610
1611     TRACE("\n");
1612
1613     hres = stack_pop_number(ctx, &r);
1614     if(FAILED(hres))
1615         return hres;
1616
1617     hres = stack_pop_number(ctx, &l);
1618     if(FAILED(hres))
1619         return hres;
1620
1621     return stack_push_number(ctx, l/r);
1622 }
1623
1624 /* ECMA-262 3rd Edition    11.5.3 */
1625 static HRESULT interp_mod(exec_ctx_t *ctx)
1626 {
1627     double l, r;
1628     HRESULT hres;
1629
1630     TRACE("\n");
1631
1632     hres = stack_pop_number(ctx, &r);
1633     if(FAILED(hres))
1634         return hres;
1635
1636     hres = stack_pop_number(ctx, &l);
1637     if(FAILED(hres))
1638         return hres;
1639
1640     return stack_push_number(ctx, fmod(l, r));
1641 }
1642
1643 /* ECMA-262 3rd Edition    11.4.2 */
1644 static HRESULT interp_delete(exec_ctx_t *ctx)
1645 {
1646     VARIANT *obj_var, *name_var;
1647     IDispatchEx *dispex;
1648     IDispatch *obj;
1649     BSTR name;
1650     BOOL ret;
1651     HRESULT hres;
1652
1653     TRACE("\n");
1654
1655     name_var = stack_pop(ctx);
1656     obj_var = stack_pop(ctx);
1657
1658     hres = to_object(ctx->script, obj_var, &obj);
1659     VariantClear(obj_var);
1660     if(FAILED(hres)) {
1661         VariantClear(name_var);
1662         return hres;
1663     }
1664
1665     hres = to_string(ctx->script, name_var, ctx->ei, &name);
1666     VariantClear(name_var);
1667     if(FAILED(hres)) {
1668         IDispatch_Release(obj);
1669         return hres;
1670     }
1671
1672     hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1673     if(SUCCEEDED(hres)) {
1674         hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1675         ret = TRUE;
1676         IDispatchEx_Release(dispex);
1677     }else {
1678         hres = S_OK;
1679         ret = FALSE;
1680     }
1681
1682     IDispatch_Release(obj);
1683     SysFreeString(name);
1684     if(FAILED(hres))
1685         return hres;
1686
1687     return stack_push_bool(ctx, ret);
1688 }
1689
1690 /* ECMA-262 3rd Edition    11.4.2 */
1691 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1692 {
1693     const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
1694     IDispatchEx *dispex;
1695     exprval_t exprval;
1696     BOOL ret = FALSE;
1697     HRESULT hres;
1698
1699     TRACE("%s\n", debugstr_w(arg));
1700
1701     hres = identifier_eval(ctx->script, arg, &exprval);
1702     if(FAILED(hres))
1703         return hres;
1704
1705     if(exprval.type != EXPRVAL_IDREF) {
1706         FIXME("Unsupported exprval\n");
1707         exprval_release(&exprval);
1708         return E_NOTIMPL;
1709     }
1710
1711     hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1712     IDispatch_Release(exprval.u.idref.disp);
1713     if(SUCCEEDED(hres)) {
1714         hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1715         IDispatchEx_Release(dispex);
1716         if(FAILED(hres))
1717             return hres;
1718
1719         ret = TRUE;
1720     }
1721
1722     return stack_push_bool(ctx, ret);
1723 }
1724
1725 /* ECMA-262 3rd Edition    11.4.2 */
1726 static HRESULT interp_void(exec_ctx_t *ctx)
1727 {
1728     VARIANT v;
1729
1730     TRACE("\n");
1731
1732     stack_popn(ctx, 1);
1733
1734     V_VT(&v) = VT_EMPTY;
1735     return stack_push(ctx, &v);
1736 }
1737
1738 /* ECMA-262 3rd Edition    11.4.3 */
1739 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1740 {
1741     switch(V_VT(v)) {
1742     case VT_EMPTY:
1743         *ret = undefinedW;
1744         break;
1745     case VT_NULL:
1746         *ret = objectW;
1747         break;
1748     case VT_BOOL:
1749         *ret = booleanW;
1750         break;
1751     case VT_I4:
1752     case VT_R8:
1753         *ret = numberW;
1754         break;
1755     case VT_BSTR:
1756         *ret = stringW;
1757         break;
1758     case VT_DISPATCH: {
1759         jsdisp_t *dispex;
1760
1761         if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1762             *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1763             jsdisp_release(dispex);
1764         }else {
1765             *ret = objectW;
1766         }
1767         break;
1768     }
1769     default:
1770         FIXME("unhandled vt %d\n", V_VT(v));
1771         return E_NOTIMPL;
1772     }
1773
1774     return S_OK;
1775 }
1776
1777 /* ECMA-262 3rd Edition    11.4.3 */
1778 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1779 {
1780     const WCHAR *ret;
1781     IDispatch *obj;
1782     VARIANT v;
1783     DISPID id;
1784     HRESULT hres;
1785
1786     static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1787
1788     TRACE("\n");
1789
1790     obj = stack_pop_objid(ctx, &id);
1791     if(!obj)
1792         return stack_push_string(ctx, undefinedW);
1793
1794     V_VT(&v) = VT_EMPTY;
1795     hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1796     IDispatch_Release(obj);
1797     if(FAILED(hres))
1798         return stack_push_string(ctx, unknownW);
1799
1800     hres = typeof_string(&v, &ret);
1801     VariantClear(&v);
1802     if(FAILED(hres))
1803         return hres;
1804
1805     return stack_push_string(ctx, ret);
1806 }
1807
1808 /* ECMA-262 3rd Edition    11.4.3 */
1809 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1810 {
1811     const BSTR arg = ctx->code->instrs[ctx->ip].arg1.bstr;
1812     exprval_t exprval;
1813     const WCHAR *ret;
1814     VARIANT v;
1815     HRESULT hres;
1816
1817     TRACE("%s\n", debugstr_w(arg));
1818
1819     hres = identifier_eval(ctx->script, arg, &exprval);
1820     if(FAILED(hres))
1821         return hres;
1822
1823     if(exprval.type == EXPRVAL_INVALID) {
1824         hres = stack_push_string(ctx, undefinedW);
1825         exprval_release(&exprval);
1826         return hres;
1827     }
1828
1829     hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1830     exprval_release(&exprval);
1831     if(FAILED(hres))
1832         return hres;
1833
1834     hres = typeof_string(&v, &ret);
1835     VariantClear(&v);
1836     if(FAILED(hres))
1837         return hres;
1838
1839     return stack_push_string(ctx, ret);
1840 }
1841
1842 /* ECMA-262 3rd Edition    11.4.3 */
1843 static HRESULT interp_typeof(exec_ctx_t *ctx)
1844 {
1845     const WCHAR *ret;
1846     VARIANT *v;
1847     HRESULT hres;
1848
1849     TRACE("\n");
1850
1851     v = stack_pop(ctx);
1852     hres = typeof_string(v, &ret);
1853     VariantClear(v);
1854     if(FAILED(hres))
1855         return hres;
1856
1857     return stack_push_string(ctx, ret);
1858 }
1859
1860 /* ECMA-262 3rd Edition    11.4.7 */
1861 static HRESULT interp_minus(exec_ctx_t *ctx)
1862 {
1863     double n;
1864     HRESULT hres;
1865
1866     TRACE("\n");
1867
1868     hres = stack_pop_number(ctx, &n);
1869     if(FAILED(hres))
1870         return hres;
1871
1872     return stack_push_number(ctx, -n);
1873 }
1874
1875 /* ECMA-262 3rd Edition    11.4.6 */
1876 static HRESULT interp_tonum(exec_ctx_t *ctx)
1877 {
1878     VARIANT *v;
1879     double n;
1880     HRESULT hres;
1881
1882     TRACE("\n");
1883
1884     v = stack_pop(ctx);
1885     hres = to_number(ctx->script, v, ctx->ei, &n);
1886     VariantClear(v);
1887     if(FAILED(hres))
1888         return hres;
1889
1890     return stack_push_number(ctx, n);
1891 }
1892
1893 /* ECMA-262 3rd Edition    11.3.1 */
1894 static HRESULT interp_postinc(exec_ctx_t *ctx)
1895 {
1896     const int arg = ctx->code->instrs[ctx->ip].arg1.lng;
1897     IDispatch *obj;
1898     DISPID id;
1899     VARIANT v;
1900     HRESULT hres;
1901
1902     TRACE("%d\n", arg);
1903
1904     obj = stack_pop_objid(ctx, &id);
1905     if(!obj)
1906         return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1907
1908     hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1909     if(SUCCEEDED(hres)) {
1910         VARIANT inc;
1911         double n;
1912
1913         hres = to_number(ctx->script, &v, ctx->ei, &n);
1914         if(SUCCEEDED(hres)) {
1915             num_set_val(&inc, n+(double)arg);
1916             hres = disp_propput(ctx->script, obj, id, &inc, ctx->ei);
1917         }
1918         if(FAILED(hres))
1919             VariantClear(&v);
1920     }
1921     IDispatch_Release(obj);
1922     if(FAILED(hres))
1923         return hres;
1924
1925     return stack_push(ctx, &v);
1926 }
1927
1928 /* ECMA-262 3rd Edition    11.4.4, 11.4.5 */
1929 static HRESULT interp_preinc(exec_ctx_t *ctx)
1930 {
1931     const int arg = ctx->code->instrs[ctx->ip].arg1.lng;
1932     IDispatch *obj;
1933     DISPID id;
1934     VARIANT v;
1935     HRESULT hres;
1936
1937     TRACE("%d\n", arg);
1938
1939     obj = stack_pop_objid(ctx, &id);
1940     if(!obj)
1941         return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1942
1943     hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1944     if(SUCCEEDED(hres)) {
1945         double n;
1946
1947         hres = to_number(ctx->script, &v, ctx->ei, &n);
1948         VariantClear(&v);
1949         if(SUCCEEDED(hres)) {
1950             num_set_val(&v, n+(double)arg);
1951             hres = disp_propput(ctx->script, obj, id, &v, ctx->ei);
1952         }
1953     }
1954     IDispatch_Release(obj);
1955     if(FAILED(hres))
1956         return hres;
1957
1958     return stack_push(ctx, &v);
1959 }
1960
1961 /* ECMA-262 3rd Edition    11.9.3 */
1962 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1963 {
1964     if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1965        return equal2_values(lval, rval, ret);
1966
1967     /* FIXME: NULL disps should be handled in more general way */
1968     if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1969         VARIANT v;
1970         V_VT(&v) = VT_NULL;
1971         return equal_values(ctx, &v, rval, ei, ret);
1972     }
1973
1974     if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1975         VARIANT v;
1976         V_VT(&v) = VT_NULL;
1977         return equal_values(ctx, lval, &v, ei, ret);
1978     }
1979
1980     if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1981        (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1982         *ret = TRUE;
1983         return S_OK;
1984     }
1985
1986     if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1987         VARIANT v;
1988         double n;
1989         HRESULT hres;
1990
1991         hres = to_number(ctx, lval, ei, &n);
1992         if(FAILED(hres))
1993             return hres;
1994
1995         /* FIXME: optimize */
1996         num_set_val(&v, n);
1997
1998         return equal_values(ctx, &v, rval, ei, ret);
1999     }
2000
2001     if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2002         VARIANT v;
2003         double n;
2004         HRESULT hres;
2005
2006         hres = to_number(ctx, rval, ei, &n);
2007         if(FAILED(hres))
2008             return hres;
2009
2010         /* FIXME: optimize */
2011         num_set_val(&v, n);
2012
2013         return equal_values(ctx, lval, &v, ei, ret);
2014     }
2015
2016     if(V_VT(rval) == VT_BOOL) {
2017         VARIANT v;
2018
2019         V_VT(&v) = VT_I4;
2020         V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2021         return equal_values(ctx, lval, &v, ei, ret);
2022     }
2023
2024     if(V_VT(lval) == VT_BOOL) {
2025         VARIANT v;
2026
2027         V_VT(&v) = VT_I4;
2028         V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2029         return equal_values(ctx, &v, rval, ei, ret);
2030     }
2031
2032
2033     if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2034         VARIANT v;
2035         HRESULT hres;
2036
2037         hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2038         if(FAILED(hres))
2039             return hres;
2040
2041         hres = equal_values(ctx, lval, &v, ei, ret);
2042
2043         VariantClear(&v);
2044         return hres;
2045     }
2046
2047
2048     if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2049         VARIANT v;
2050         HRESULT hres;
2051
2052         hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2053         if(FAILED(hres))
2054             return hres;
2055
2056         hres = equal_values(ctx, &v, rval, ei, ret);
2057
2058         VariantClear(&v);
2059         return hres;
2060     }
2061
2062
2063     *ret = FALSE;
2064     return S_OK;
2065 }
2066
2067 /* ECMA-262 3rd Edition    11.9.1 */
2068 static HRESULT interp_eq(exec_ctx_t *ctx)
2069 {
2070     VARIANT *l, *r;
2071     BOOL b;
2072     HRESULT hres;
2073
2074     r = stack_pop(ctx);
2075     l = stack_pop(ctx);
2076
2077     TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2078
2079     hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2080     VariantClear(l);
2081     VariantClear(r);
2082     if(FAILED(hres))
2083         return hres;
2084
2085     return stack_push_bool(ctx, b);
2086 }
2087
2088 /* ECMA-262 3rd Edition    11.9.2 */
2089 static HRESULT interp_neq(exec_ctx_t *ctx)
2090 {
2091     VARIANT *l, *r;
2092     BOOL b;
2093     HRESULT hres;
2094
2095     r = stack_pop(ctx);
2096     l = stack_pop(ctx);
2097
2098     TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2099
2100     hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2101     VariantClear(l);
2102     VariantClear(r);
2103     if(FAILED(hres))
2104         return hres;
2105
2106     return stack_push_bool(ctx, !b);
2107 }
2108
2109 /* ECMA-262 3rd Edition    11.9.4 */
2110 static HRESULT interp_eq2(exec_ctx_t *ctx)
2111 {
2112     VARIANT *l, *r;
2113     BOOL b;
2114     HRESULT hres;
2115
2116     TRACE("\n");
2117
2118     r = stack_pop(ctx);
2119     l = stack_pop(ctx);
2120
2121     hres = equal2_values(r, l, &b);
2122     VariantClear(l);
2123     VariantClear(r);
2124     if(FAILED(hres))
2125         return hres;
2126
2127     return stack_push_bool(ctx, b);
2128 }
2129
2130 /* ECMA-262 3rd Edition    11.9.5 */
2131 static HRESULT interp_neq2(exec_ctx_t *ctx)
2132 {
2133     VARIANT *l, *r;
2134     BOOL b;
2135     HRESULT hres;
2136
2137     TRACE("\n");
2138
2139     r = stack_pop(ctx);
2140     l = stack_pop(ctx);
2141
2142     hres = equal2_values(r, l, &b);
2143     VariantClear(l);
2144     VariantClear(r);
2145     if(FAILED(hres))
2146         return hres;
2147
2148     return stack_push_bool(ctx, !b);
2149 }
2150
2151 /* ECMA-262 3rd Edition    11.8.5 */
2152 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2153 {
2154     double ln, rn;
2155     VARIANT l, r;
2156     HRESULT hres;
2157
2158     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2159     if(FAILED(hres))
2160         return hres;
2161
2162     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2163     if(FAILED(hres)) {
2164         VariantClear(&l);
2165         return hres;
2166     }
2167
2168     if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2169         *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2170         SysFreeString(V_BSTR(&l));
2171         SysFreeString(V_BSTR(&r));
2172         return S_OK;
2173     }
2174
2175     hres = to_number(ctx, &l, ei, &ln);
2176     VariantClear(&l);
2177     if(SUCCEEDED(hres))
2178         hres = to_number(ctx, &r, ei, &rn);
2179     VariantClear(&r);
2180     if(FAILED(hres))
2181         return hres;
2182
2183     *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2184     return S_OK;
2185 }
2186
2187 /* ECMA-262 3rd Edition    11.8.1 */
2188 static HRESULT interp_lt(exec_ctx_t *ctx)
2189 {
2190     VARIANT *l, *r;
2191     BOOL b;
2192     HRESULT hres;
2193
2194     r = stack_pop(ctx);
2195     l = stack_pop(ctx);
2196
2197     TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2198
2199     hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2200     VariantClear(l);
2201     VariantClear(r);
2202     if(FAILED(hres))
2203         return hres;
2204
2205     return stack_push_bool(ctx, b);
2206 }
2207
2208 /* ECMA-262 3rd Edition    11.8.1 */
2209 static HRESULT interp_lteq(exec_ctx_t *ctx)
2210 {
2211     VARIANT *l, *r;
2212     BOOL b;
2213     HRESULT hres;
2214
2215     r = stack_pop(ctx);
2216     l = stack_pop(ctx);
2217
2218     TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2219
2220     hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2221     VariantClear(l);
2222     VariantClear(r);
2223     if(FAILED(hres))
2224         return hres;
2225
2226     return stack_push_bool(ctx, b);
2227 }
2228
2229 /* ECMA-262 3rd Edition    11.8.2 */
2230 static HRESULT interp_gt(exec_ctx_t *ctx)
2231 {
2232     VARIANT *l, *r;
2233     BOOL b;
2234     HRESULT hres;
2235
2236     r = stack_pop(ctx);
2237     l = stack_pop(ctx);
2238
2239     TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2240
2241     hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2242     VariantClear(l);
2243     VariantClear(r);
2244     if(FAILED(hres))
2245         return hres;
2246
2247     return stack_push_bool(ctx, b);
2248 }
2249
2250 /* ECMA-262 3rd Edition    11.8.4 */
2251 static HRESULT interp_gteq(exec_ctx_t *ctx)
2252 {
2253     VARIANT *l, *r;
2254     BOOL b;
2255     HRESULT hres;
2256
2257     r = stack_pop(ctx);
2258     l = stack_pop(ctx);
2259
2260     TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2261
2262     hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2263     VariantClear(l);
2264     VariantClear(r);
2265     if(FAILED(hres))
2266         return hres;
2267
2268     return stack_push_bool(ctx, b);
2269 }
2270
2271 /* ECMA-262 3rd Edition    11.4.8 */
2272 static HRESULT interp_bneg(exec_ctx_t *ctx)
2273 {
2274     VARIANT *v, r;
2275     INT i;
2276     HRESULT hres;
2277
2278     TRACE("\n");
2279
2280     v = stack_pop(ctx);
2281     hres = to_int32(ctx->script, v, ctx->ei, &i);
2282     VariantClear(v);
2283     if(FAILED(hres))
2284         return hres;
2285
2286     V_VT(&r) = VT_I4;
2287     V_I4(&r) = ~i;
2288     return stack_push(ctx, &r);
2289 }
2290
2291 /* ECMA-262 3rd Edition    11.4.9 */
2292 static HRESULT interp_neg(exec_ctx_t *ctx)
2293 {
2294     VARIANT *v;
2295     VARIANT_BOOL b;
2296     HRESULT hres;
2297
2298     TRACE("\n");
2299
2300     v = stack_pop(ctx);
2301     hres = to_boolean(v, &b);
2302     VariantClear(v);
2303     if(FAILED(hres))
2304         return hres;
2305
2306     return stack_push_bool(ctx, !b);
2307 }
2308
2309 /* ECMA-262 3rd Edition    11.7.1 */
2310 static HRESULT interp_lshift(exec_ctx_t *ctx)
2311 {
2312     DWORD r;
2313     INT l;
2314     HRESULT hres;
2315
2316     hres = stack_pop_uint(ctx, &r);
2317     if(FAILED(hres))
2318         return hres;
2319
2320     hres = stack_pop_int(ctx, &l);
2321     if(FAILED(hres))
2322         return hres;
2323
2324     return stack_push_int(ctx, l << (r&0x1f));
2325 }
2326
2327 /* ECMA-262 3rd Edition    11.7.2 */
2328 static HRESULT interp_rshift(exec_ctx_t *ctx)
2329 {
2330     DWORD r;
2331     INT l;
2332     HRESULT hres;
2333
2334     hres = stack_pop_uint(ctx, &r);
2335     if(FAILED(hres))
2336         return hres;
2337
2338     hres = stack_pop_int(ctx, &l);
2339     if(FAILED(hres))
2340         return hres;
2341
2342     return stack_push_int(ctx, l >> (r&0x1f));
2343 }
2344
2345 /* ECMA-262 3rd Edition    11.7.3 */
2346 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2347 {
2348     DWORD r, l;
2349     HRESULT hres;
2350
2351     hres = stack_pop_uint(ctx, &r);
2352     if(FAILED(hres))
2353         return hres;
2354
2355     hres = stack_pop_uint(ctx, &l);
2356     if(FAILED(hres))
2357         return hres;
2358
2359     return stack_push_int(ctx, l >> (r&0x1f));
2360 }
2361
2362 /* ECMA-262 3rd Edition    11.13.1 */
2363 static HRESULT interp_assign(exec_ctx_t *ctx)
2364 {
2365     IDispatch *disp;
2366     DISPID id;
2367     VARIANT *v;
2368     HRESULT hres;
2369
2370     TRACE("\n");
2371
2372     v = stack_pop(ctx);
2373     disp = stack_pop_objid(ctx, &id);
2374
2375     if(!disp)
2376         return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2377
2378     hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2379     IDispatch_Release(disp);
2380     if(FAILED(hres)) {
2381         VariantClear(v);
2382         return hres;
2383     }
2384
2385     return stack_push(ctx, v);
2386 }
2387
2388 /* JScript extension */
2389 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2390 {
2391     const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
2392     DISPID propput_dispid = DISPID_PROPERTYPUT;
2393     IDispatch *disp;
2394     DISPPARAMS dp;
2395     VARIANT *v;
2396     DISPID id;
2397     HRESULT hres;
2398
2399     TRACE("%u\n", arg);
2400
2401     disp = stack_topn_objid(ctx, arg+1, &id);
2402     if(!disp)
2403         return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2404
2405     jsstack_to_dp(ctx, arg+1, &dp);
2406     dp.cNamedArgs = 1;
2407     dp.rgdispidNamedArgs = &propput_dispid;
2408     hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, &dp, NULL, ctx->ei);
2409     if(FAILED(hres))
2410         return hres;
2411
2412     v = stack_pop(ctx);
2413     stack_popn(ctx, arg+2);
2414     return stack_push(ctx, v);
2415 }
2416
2417 static HRESULT interp_undefined(exec_ctx_t *ctx)
2418 {
2419     VARIANT v;
2420
2421     TRACE("\n");
2422
2423     V_VT(&v) = VT_EMPTY;
2424     return stack_push(ctx, &v);
2425 }
2426
2427 static HRESULT interp_jmp(exec_ctx_t *ctx)
2428 {
2429     const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
2430
2431     TRACE("\n");
2432
2433     ctx->ip = arg;
2434     return S_OK;
2435 }
2436
2437 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2438 {
2439     const unsigned arg = ctx->code->instrs[ctx->ip].arg1.uint;
2440     VARIANT_BOOL b;
2441     VARIANT *v;
2442     HRESULT hres;
2443
2444     TRACE("\n");
2445
2446     v = stack_pop(ctx);
2447     hres = to_boolean(v, &b);
2448     VariantClear(v);
2449     if(FAILED(hres))
2450         return hres;
2451
2452     if(b)
2453         ctx->ip++;
2454     else
2455         ctx->ip = arg;
2456     return S_OK;
2457 }
2458
2459 static HRESULT interp_pop(exec_ctx_t *ctx)
2460 {
2461     TRACE("\n");
2462
2463     stack_popn(ctx, 1);
2464     return S_OK;
2465 }
2466
2467 static HRESULT interp_ret(exec_ctx_t *ctx)
2468 {
2469     TRACE("\n");
2470
2471     ctx->ip = -1;
2472     return S_OK;
2473 }
2474
2475 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2476
2477 static const op_func_t op_funcs[] = {
2478 #define X(x,a,b,c) interp_##x,
2479 OP_LIST
2480 #undef X
2481 };
2482
2483 static const unsigned op_move[] = {
2484 #define X(a,x,b,c) x,
2485 OP_LIST
2486 #undef X
2487 };
2488
2489 static HRESULT unwind_exception(exec_ctx_t *ctx)
2490 {
2491     except_frame_t *except_frame;
2492     VARIANT except_val;
2493     BSTR ident;
2494     HRESULT hres;
2495
2496     except_frame = ctx->except_frame;
2497     ctx->except_frame = except_frame->next;
2498
2499     assert(except_frame->stack_top <= ctx->top);
2500     stack_popn(ctx, ctx->top - except_frame->stack_top);
2501
2502     while(except_frame->scope != ctx->scope_chain)
2503         scope_pop(&ctx->scope_chain);
2504
2505     ctx->ip = except_frame->catch_off;
2506
2507     except_val = ctx->ei->var;
2508     memset(ctx->ei, 0, sizeof(*ctx->ei));
2509
2510     ident = except_frame->ident;
2511     heap_free(except_frame);
2512
2513     if(ident) {
2514         jsdisp_t *scope_obj;
2515
2516         hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2517         if(SUCCEEDED(hres)) {
2518             hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei);
2519             if(FAILED(hres))
2520                 jsdisp_release(scope_obj);
2521         }
2522         VariantClear(&except_val);
2523         if(FAILED(hres))
2524             return hres;
2525
2526         hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2527         jsdisp_release(scope_obj);
2528     }else {
2529         VARIANT v;
2530
2531         hres = stack_push(ctx, &except_val);
2532         if(FAILED(hres))
2533             return hres;
2534
2535         hres = stack_push_bool(ctx, FALSE);
2536         if(FAILED(hres))
2537             return hres;
2538
2539         V_VT(&v) = VT_EMPTY;
2540         hres = stack_push(ctx, &v);
2541     }
2542
2543     return hres;
2544 }
2545
2546 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsexcept_t *ei, VARIANT *ret)
2547 {
2548     exec_ctx_t *exec_ctx = ctx->exec_ctx;
2549     except_frame_t *prev_except_frame;
2550     function_code_t *prev_func;
2551     unsigned prev_ip, prev_top;
2552     scope_chain_t *prev_scope;
2553     bytecode_t *prev_code;
2554     jsexcept_t *prev_ei;
2555     jsop_t op;
2556     HRESULT hres = S_OK;
2557
2558     TRACE("\n");
2559
2560     prev_top = exec_ctx->top;
2561     prev_scope = exec_ctx->scope_chain;
2562     prev_except_frame = exec_ctx->except_frame;
2563     prev_ip = exec_ctx->ip;
2564     prev_ei = exec_ctx->ei;
2565     prev_code = exec_ctx->code;
2566     prev_func = exec_ctx->func_code;
2567     exec_ctx->ip = func->instr_off;
2568     exec_ctx->ei = ei;
2569     exec_ctx->except_frame = NULL;
2570     exec_ctx->code = code;
2571     exec_ctx->func_code = func;
2572
2573     while(exec_ctx->ip != -1) {
2574         op = code->instrs[exec_ctx->ip].op;
2575         hres = op_funcs[op](exec_ctx);
2576         if(FAILED(hres)) {
2577             TRACE("EXCEPTION\n");
2578
2579             if(!exec_ctx->except_frame)
2580                 break;
2581
2582             hres = unwind_exception(exec_ctx);
2583             if(FAILED(hres))
2584                 break;
2585         }else {
2586             exec_ctx->ip += op_move[op];
2587         }
2588     }
2589
2590     exec_ctx->ip = prev_ip;
2591     exec_ctx->ei = prev_ei;
2592     exec_ctx->except_frame = prev_except_frame;
2593     exec_ctx->code = prev_code;
2594     exec_ctx->func_code = prev_func;
2595
2596     if(FAILED(hres)) {
2597         while(exec_ctx->scope_chain != prev_scope)
2598             scope_pop(&exec_ctx->scope_chain);
2599         stack_popn(exec_ctx, exec_ctx->top-prev_top);
2600         return hres;
2601     }
2602
2603     assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2604     assert(exec_ctx->scope_chain == prev_scope);
2605
2606     if(exec_ctx->top == prev_top)
2607         V_VT(ret) = VT_EMPTY;
2608     else
2609         *ret = *stack_pop(exec_ctx);
2610     return S_OK;
2611 }
2612
2613 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval,
2614         jsexcept_t *ei, VARIANT *retv)
2615 {
2616     exec_ctx_t *prev_ctx;
2617     var_list_t *var;
2618     VARIANT val;
2619     unsigned i;
2620     HRESULT hres = S_OK;
2621
2622     for(i = 0; i < func->func_cnt; i++) {
2623         function_expression_t *expr;
2624         jsdisp_t *func_obj;
2625         VARIANT var;
2626
2627         if(!func->funcs[i].name)
2628             continue;
2629
2630         expr = func->funcs[i].expr;
2631         hres = create_source_function(ctx->script, code, expr->parameter_list, func->funcs+i,
2632                 ctx->scope_chain, &func_obj);
2633         if(FAILED(hres))
2634             return hres;
2635
2636         var_set_jsdisp(&var, func_obj);
2637         hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, &var, ei);
2638         jsdisp_release(func_obj);
2639         if(FAILED(hres))
2640             return hres;
2641     }
2642
2643     for(var = func->source_elements->variables; var; var = var->next) {
2644         DISPID id = 0;
2645         BSTR name;
2646
2647         name = SysAllocString(var->identifier);
2648         if(!name)
2649             return E_OUTOFMEMORY;
2650
2651         if(!ctx->is_global || !lookup_global_members(ctx->script, name, NULL))
2652             hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
2653         SysFreeString(name);
2654         if(FAILED(hres))
2655             return hres;
2656     }
2657
2658     prev_ctx = ctx->script->exec_ctx;
2659     ctx->script->exec_ctx = ctx;
2660
2661     hres = enter_bytecode(ctx->script, code, func, ei, &val);
2662     assert(ctx->script->exec_ctx == ctx);
2663     ctx->script->exec_ctx = prev_ctx;
2664     if(FAILED(hres))
2665         return hres;
2666
2667     if(retv)
2668         *retv = val;
2669     else
2670         VariantClear(&val);
2671     return S_OK;
2672 }