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