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