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