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