jscript: Use bytecode for '^=' expression implementation.
[wine] / dlls / jscript / engine.c
1 /*
2  * Copyright 2008 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 #define EXPR_NOVAL   0x0001
33 #define EXPR_NEWREF  0x0002
34
35 struct _return_type_t {
36     enum{
37         RT_NORMAL,
38         RT_RETURN,
39         RT_BREAK,
40         RT_CONTINUE
41     } type;
42     jsexcept_t ei;
43 };
44
45 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
46 {
47     return stat->eval(ctx, stat, rt, ret);
48 }
49
50 static inline HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
51 {
52     return expr->eval(ctx, expr, flags, ei, ret);
53 }
54
55 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
56 {
57     if(!ctx->stack_size) {
58         ctx->stack = heap_alloc(16*sizeof(VARIANT));
59         if(!ctx->stack)
60             return E_OUTOFMEMORY;
61         ctx->stack_size = 16;
62     }else if(ctx->stack_size == ctx->top) {
63         VARIANT *new_stack;
64
65         new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
66         if(!new_stack) {
67             VariantClear(v);
68             return E_OUTOFMEMORY;
69         }
70
71         ctx->stack = new_stack;
72         ctx->stack_size *= 2;
73     }
74
75     ctx->stack[ctx->top++] = *v;
76     return S_OK;
77 }
78
79 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
80 {
81     VARIANT v;
82
83     V_VT(&v) = VT_BOOL;
84     V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
85     return stack_push(ctx, &v);
86 }
87
88 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
89 {
90     VARIANT v;
91
92     num_set_val(&v, number);
93     return stack_push(ctx, &v);
94 }
95
96 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
97 {
98     VARIANT v;
99
100     V_VT(&v) = VT_I4;
101     V_I4(&v) = n;
102     return stack_push(ctx, &v);
103 }
104
105 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
106 {
107     VARIANT v;
108     HRESULT hres;
109
110     V_VT(&v) = VT_DISPATCH;
111     V_DISPATCH(&v) = disp;
112     hres = stack_push(ctx, &v);
113     if(FAILED(hres))
114         return hres;
115
116     V_VT(&v) = VT_INT;
117     V_INT(&v) = id;
118     return stack_push(ctx, &v);
119 }
120
121 static inline VARIANT *stack_top(exec_ctx_t *ctx)
122 {
123     assert(ctx->top);
124     return ctx->stack + ctx->top-1;
125 }
126
127 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
128 {
129     assert(ctx->top > n);
130     return ctx->stack + ctx->top-1-n;
131 }
132
133 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
134 {
135     assert(ctx->top);
136     return ctx->stack + --ctx->top;
137 }
138
139 static void stack_popn(exec_ctx_t *ctx, unsigned n)
140 {
141     while(n--)
142         VariantClear(stack_pop(ctx));
143 }
144
145 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
146 {
147     VARIANT *v;
148     HRESULT hres;
149
150     v = stack_pop(ctx);
151     hres = to_number(ctx->parser->script, v, &ctx->ei, r);
152     VariantClear(v);
153     return hres;
154 }
155
156 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
157 {
158     VARIANT *v;
159     HRESULT hres;
160
161     v = stack_pop(ctx);
162     if(V_VT(v) == VT_DISPATCH) {
163         *r = V_DISPATCH(v);
164         return S_OK;
165     }
166
167     hres = to_object(ctx->parser->script, v, r);
168     VariantClear(v);
169     return hres;
170 }
171
172 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
173 {
174     return to_int32(ctx->parser->script, stack_pop(ctx), &ctx->ei, r);
175 }
176
177 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
178 {
179     assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
180
181     *id = V_INT(stack_pop(ctx));
182     return V_DISPATCH(stack_pop(ctx));
183 }
184
185 static inline IDispatch *stack_top_objid(exec_ctx_t *ctx, DISPID *id)
186 {
187     assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
188
189     *id = V_INT(stack_top(ctx));
190     return V_DISPATCH(stack_topn(ctx, 1));
191 }
192
193 static void exprval_release(exprval_t *val)
194 {
195     switch(val->type) {
196     case EXPRVAL_VARIANT:
197         if(V_VT(&val->u.var) != VT_EMPTY)
198             VariantClear(&val->u.var);
199         return;
200     case EXPRVAL_IDREF:
201         if(val->u.idref.disp)
202             IDispatch_Release(val->u.idref.disp);
203         return;
204     case EXPRVAL_INVALID:
205         SysFreeString(val->u.identifier);
206     }
207 }
208
209 /* ECMA-262 3rd Edition    8.7.1 */
210 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
211 {
212     V_VT(ret) = VT_EMPTY;
213
214     switch(val->type) {
215     case EXPRVAL_VARIANT:
216         return VariantCopy(ret, &val->u.var);
217     case EXPRVAL_IDREF:
218         if(!val->u.idref.disp) {
219             FIXME("throw ReferenceError\n");
220             return E_FAIL;
221         }
222
223         return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei, NULL/*FIXME*/);
224     case EXPRVAL_INVALID:
225         return throw_type_error(ctx, ei, JS_E_UNDEFINED_VARIABLE, val->u.identifier);
226     }
227
228     ERR("type %d\n", val->type);
229     return E_FAIL;
230 }
231
232 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
233 {
234     if(val->type == EXPRVAL_VARIANT) {
235         *ret = val->u.var;
236         V_VT(&val->u.var) = VT_EMPTY;
237         return S_OK;
238     }
239
240     return exprval_value(ctx, val, ei, ret);
241 }
242
243 static HRESULT exprval_to_boolean(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, VARIANT_BOOL *b)
244 {
245     if(exprval->type != EXPRVAL_VARIANT) {
246         VARIANT val;
247         HRESULT hres;
248
249         hres = exprval_to_value(ctx, exprval, ei, &val);
250         if(FAILED(hres))
251             return hres;
252
253         hres = to_boolean(&val, b);
254         VariantClear(&val);
255         return hres;
256     }
257
258     return to_boolean(&exprval->u.var, b);
259 }
260
261 static void exprval_init(exprval_t *val)
262 {
263     val->type = EXPRVAL_VARIANT;
264     V_VT(&val->u.var) = VT_EMPTY;
265 }
266
267 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
268 {
269     val->type = EXPRVAL_IDREF;
270     val->u.idref.disp = disp;
271     val->u.idref.id = id;
272
273     if(disp)
274         IDispatch_AddRef(disp);
275 }
276
277 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
278 {
279     scope_chain_t *new_scope;
280
281     new_scope = heap_alloc(sizeof(scope_chain_t));
282     if(!new_scope)
283         return E_OUTOFMEMORY;
284
285     new_scope->ref = 1;
286
287     jsdisp_addref(obj);
288     new_scope->obj = obj;
289
290     if(scope) {
291         scope_addref(scope);
292         new_scope->next = scope;
293     }else {
294         new_scope->next = NULL;
295     }
296
297     *ret = new_scope;
298     return S_OK;
299 }
300
301 static void scope_pop(scope_chain_t **scope)
302 {
303     scope_chain_t *tmp;
304
305     tmp = *scope;
306     *scope = tmp->next;
307     scope_release(tmp);
308 }
309
310 void scope_release(scope_chain_t *scope)
311 {
312     if(--scope->ref)
313         return;
314
315     if(scope->next)
316         scope_release(scope->next);
317
318     jsdisp_release(scope->obj);
319     heap_free(scope);
320 }
321
322 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
323         scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
324 {
325     exec_ctx_t *ctx;
326
327     ctx = heap_alloc_zero(sizeof(exec_ctx_t));
328     if(!ctx)
329         return E_OUTOFMEMORY;
330
331     ctx->ref = 1;
332     ctx->is_global = is_global;
333
334     if(this_obj)
335         ctx->this_obj = this_obj;
336     else if(script_ctx->host_global)
337         ctx->this_obj = script_ctx->host_global;
338     else
339         ctx->this_obj = to_disp(script_ctx->global);
340     IDispatch_AddRef(ctx->this_obj);
341
342     jsdisp_addref(var_disp);
343     ctx->var_disp = var_disp;
344
345     if(scope) {
346         scope_addref(scope);
347         ctx->scope_chain = scope;
348     }
349
350     *ret = ctx;
351     return S_OK;
352 }
353
354 void exec_release(exec_ctx_t *ctx)
355 {
356     if(--ctx->ref)
357         return;
358
359     if(ctx->scope_chain)
360         scope_release(ctx->scope_chain);
361     if(ctx->var_disp)
362         jsdisp_release(ctx->var_disp);
363     if(ctx->this_obj)
364         IDispatch_Release(ctx->this_obj);
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("unsing 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 /* ECMA-262 3rd Edition    8.7.2 */
389 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
390 {
391     if(ref->type != EXPRVAL_IDREF)
392         return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
393
394     return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
395 }
396
397 static inline BOOL is_null(const VARIANT *v)
398 {
399     return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
400 }
401
402 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
403 {
404     IObjectIdentity *identity;
405     IUnknown *unk1, *unk2;
406     HRESULT hres;
407
408     if(disp1 == disp2) {
409         *ret = TRUE;
410         return S_OK;
411     }
412
413     if(!disp1 || !disp2) {
414         *ret = FALSE;
415         return S_OK;
416     }
417
418     hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
419     if(FAILED(hres))
420         return hres;
421
422     hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
423     if(FAILED(hres)) {
424         IUnknown_Release(unk1);
425         return hres;
426     }
427
428     if(unk1 == unk2) {
429         *ret = TRUE;
430     }else {
431         hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
432         if(SUCCEEDED(hres)) {
433             hres = IObjectIdentity_IsEqualObject(identity, unk2);
434             IObjectIdentity_Release(identity);
435             *ret = hres == S_OK;
436         }else {
437             *ret = FALSE;
438         }
439     }
440
441     IUnknown_Release(unk1);
442     IUnknown_Release(unk2);
443     return S_OK;
444 }
445
446 /* ECMA-262 3rd Edition    11.9.6 */
447 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
448 {
449     TRACE("\n");
450
451     if(V_VT(lval) != V_VT(rval)) {
452         if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
453             *ret = num_val(lval) == num_val(rval);
454         else if(is_null(lval))
455             *ret = is_null(rval);
456         else
457             *ret = FALSE;
458         return S_OK;
459     }
460
461     switch(V_VT(lval)) {
462     case VT_EMPTY:
463     case VT_NULL:
464         *ret = VARIANT_TRUE;
465         break;
466     case VT_I4:
467         *ret = V_I4(lval) == V_I4(rval);
468         break;
469     case VT_R8:
470         *ret = V_R8(lval) == V_R8(rval);
471         break;
472     case VT_BSTR:
473         if(!V_BSTR(lval))
474             *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
475         else if(!V_BSTR(rval))
476             *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
477         else
478             *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
479         break;
480     case VT_DISPATCH:
481         return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
482     case VT_BOOL:
483         *ret = !V_BOOL(lval) == !V_BOOL(rval);
484         break;
485     default:
486         FIXME("unimplemented vt %d\n", V_VT(lval));
487         return E_NOTIMPL;
488     }
489
490     return S_OK;
491 }
492
493 static HRESULT literal_to_var(script_ctx_t *ctx, literal_t *literal, VARIANT *v)
494 {
495     switch(literal->type) {
496     case LT_NULL:
497         V_VT(v) = VT_NULL;
498         break;
499     case LT_INT:
500         V_VT(v) = VT_I4;
501         V_I4(v) = literal->u.lval;
502         break;
503     case LT_DOUBLE:
504         V_VT(v) = VT_R8;
505         V_R8(v) = literal->u.dval;
506         break;
507     case LT_STRING: {
508         BSTR str = SysAllocString(literal->u.wstr);
509         if(!str)
510             return E_OUTOFMEMORY;
511
512         V_VT(v) = VT_BSTR;
513         V_BSTR(v) = str;
514         break;
515     }
516     case LT_BOOL:
517         V_VT(v) = VT_BOOL;
518         V_BOOL(v) = literal->u.bval;
519         break;
520     case LT_REGEXP: {
521         jsdisp_t *regexp;
522         HRESULT hres;
523
524         hres = create_regexp(ctx, literal->u.regexp.str, literal->u.regexp.str_len,
525                              literal->u.regexp.flags, &regexp);
526         if(FAILED(hres))
527             return hres;
528
529         var_set_jsdisp(v, regexp);
530     }
531     }
532
533     return S_OK;
534 }
535
536 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
537 {
538     named_item_t *item;
539     DISPID id;
540     HRESULT hres;
541
542     for(item = ctx->named_items; item; item = item->next) {
543         if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
544             hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
545             if(SUCCEEDED(hres)) {
546                 if(ret)
547                     exprval_set_idref(ret, item->disp, id);
548                 return TRUE;
549             }
550         }
551     }
552
553     return FALSE;
554 }
555
556 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
557         jsexcept_t *ei, VARIANT *retv)
558 {
559     script_ctx_t *script = parser->script;
560     function_declaration_t *func;
561     parser_ctx_t *prev_parser;
562     var_list_t *var;
563     VARIANT val, tmp;
564     statement_t *stat;
565     exec_ctx_t *prev_ctx;
566     return_type_t rt;
567     HRESULT hres = S_OK;
568
569     for(func = source->functions; func; func = func->next) {
570         jsdisp_t *func_obj;
571         VARIANT var;
572
573         hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
574                 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
575         if(FAILED(hres))
576             return hres;
577
578         var_set_jsdisp(&var, func_obj);
579         hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
580         jsdisp_release(func_obj);
581         if(FAILED(hres))
582             return hres;
583     }
584
585     for(var = source->variables; var; var = var->next) {
586         DISPID id = 0;
587         BSTR name;
588
589         name = SysAllocString(var->identifier);
590         if(!name)
591             return E_OUTOFMEMORY;
592
593         if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
594             hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
595         SysFreeString(name);
596         if(FAILED(hres))
597             return hres;
598     }
599
600     prev_ctx = script->exec_ctx;
601     script->exec_ctx = ctx;
602
603     prev_parser = ctx->parser;
604     ctx->parser = parser;
605
606     V_VT(&val) = VT_EMPTY;
607     memset(&rt, 0, sizeof(rt));
608     rt.type = RT_NORMAL;
609
610     for(stat = source->statement; stat; stat = stat->next) {
611         hres = stat_eval(script, stat, &rt, &tmp);
612         if(FAILED(hres))
613             break;
614
615         VariantClear(&val);
616         val = tmp;
617         if(rt.type != RT_NORMAL)
618             break;
619     }
620
621     script->exec_ctx = prev_ctx;
622     ctx->parser = prev_parser;
623
624     if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
625         FIXME("wrong rt %d\n", rt.type);
626         hres = E_FAIL;
627     }
628
629     *ei = rt.ei;
630     if(FAILED(hres)) {
631         VariantClear(&val);
632         return hres;
633     }
634
635     if(!retv || (!from_eval && rt.type != RT_RETURN))
636         VariantClear(&val);
637     if(retv)
638         *retv = val;
639     return S_OK;
640 }
641
642 /* ECMA-262 3rd Edition    10.1.4 */
643 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
644 {
645     scope_chain_t *scope;
646     named_item_t *item;
647     DISPID id = 0;
648     HRESULT hres;
649
650     TRACE("%s\n", debugstr_w(identifier));
651
652     for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
653         hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
654         if(SUCCEEDED(hres)) {
655             exprval_set_idref(ret, to_disp(scope->obj), id);
656             return S_OK;
657         }
658     }
659
660     hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
661     if(SUCCEEDED(hres)) {
662         exprval_set_idref(ret, to_disp(ctx->global), id);
663         return S_OK;
664     }
665
666     for(item = ctx->named_items; item; item = item->next) {
667         if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
668             if(!item->disp) {
669                 IUnknown *unk;
670
671                 if(!ctx->site)
672                     break;
673
674                 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
675                                                      SCRIPTINFO_IUNKNOWN, &unk, NULL);
676                 if(FAILED(hres)) {
677                     WARN("GetItemInfo failed: %08x\n", hres);
678                     break;
679                 }
680
681                 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
682                 IUnknown_Release(unk);
683                 if(FAILED(hres)) {
684                     WARN("object does not implement IDispatch\n");
685                     break;
686                 }
687             }
688
689             ret->type = EXPRVAL_VARIANT;
690             V_VT(&ret->u.var) = VT_DISPATCH;
691             V_DISPATCH(&ret->u.var) = item->disp;
692             IDispatch_AddRef(item->disp);
693             return S_OK;
694         }
695     }
696
697     if(lookup_global_members(ctx, identifier, ret))
698         return S_OK;
699
700     if(flags & EXPR_NEWREF) {
701         hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
702         if(FAILED(hres))
703             return hres;
704
705         exprval_set_idref(ret, to_disp(ctx->global), id);
706         return S_OK;
707     }
708
709     ret->type = EXPRVAL_INVALID;
710     ret->u.identifier = SysAllocString(identifier);
711     if(!ret->u.identifier)
712         return E_OUTOFMEMORY;
713
714     return S_OK;
715 }
716
717 /* ECMA-262 3rd Edition    12.1 */
718 HRESULT block_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
719 {
720     block_statement_t *stat = (block_statement_t*)_stat;
721     VARIANT val, tmp;
722     statement_t *iter;
723     HRESULT hres = S_OK;
724
725     TRACE("\n");
726
727     V_VT(&val) = VT_EMPTY;
728     for(iter = stat->stat_list; iter; iter = iter->next) {
729         hres = stat_eval(ctx, iter, rt, &tmp);
730         if(FAILED(hres))
731             break;
732
733         VariantClear(&val);
734         val = tmp;
735         if(rt->type != RT_NORMAL)
736             break;
737     }
738
739     if(FAILED(hres)) {
740         VariantClear(&val);
741         return hres;
742     }
743
744     *ret = val;
745     return S_OK;
746 }
747
748 /* ECMA-262 3rd Edition    12.2 */
749 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
750 {
751     variable_declaration_t *iter;
752     HRESULT hres = S_OK;
753
754     for(iter = var_list; iter; iter = iter->next) {
755         exprval_t exprval;
756         VARIANT val;
757
758         if(!iter->expr)
759             continue;
760
761         hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
762         if(FAILED(hres))
763             break;
764
765         hres = exprval_to_value(ctx, &exprval, ei, &val);
766         exprval_release(&exprval);
767         if(FAILED(hres))
768             break;
769
770         hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
771         VariantClear(&val);
772         if(FAILED(hres))
773             break;
774     }
775
776     return hres;
777 }
778
779 /* ECMA-262 3rd Edition    12.2 */
780 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
781 {
782     var_statement_t *stat = (var_statement_t*)_stat;
783     HRESULT hres;
784
785     TRACE("\n");
786
787     hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
788     if(FAILED(hres))
789         return hres;
790
791     V_VT(ret) = VT_EMPTY;
792     return S_OK;
793 }
794
795 /* ECMA-262 3rd Edition    12.3 */
796 HRESULT empty_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
797 {
798     TRACE("\n");
799
800     V_VT(ret) = VT_EMPTY;
801     return S_OK;
802 }
803
804 /* ECMA-262 3rd Edition    12.4 */
805 HRESULT expression_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
806 {
807     expression_statement_t *stat = (expression_statement_t*)_stat;
808     exprval_t exprval;
809     VARIANT val;
810     HRESULT hres;
811
812     TRACE("\n");
813
814     hres = expr_eval(ctx, stat->expr, EXPR_NOVAL, &rt->ei, &exprval);
815     if(FAILED(hres))
816         return hres;
817
818     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
819     exprval_release(&exprval);
820     if(FAILED(hres))
821         return hres;
822
823     *ret = val;
824     TRACE("= %s\n", debugstr_variant(ret));
825     return S_OK;
826 }
827
828 /* ECMA-262 3rd Edition    12.5 */
829 HRESULT if_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
830 {
831     if_statement_t *stat = (if_statement_t*)_stat;
832     exprval_t exprval;
833     VARIANT_BOOL b;
834     HRESULT hres;
835
836     TRACE("\n");
837
838     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
839     if(FAILED(hres))
840         return hres;
841
842     hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
843     exprval_release(&exprval);
844     if(FAILED(hres))
845         return hres;
846
847     if(b)
848         hres = stat_eval(ctx, stat->if_stat, rt, ret);
849     else if(stat->else_stat)
850         hres = stat_eval(ctx, stat->else_stat, rt, ret);
851     else
852         V_VT(ret) = VT_EMPTY;
853
854     return hres;
855 }
856
857 /* ECMA-262 3rd Edition    12.6.2 */
858 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
859 {
860     while_statement_t *stat = (while_statement_t*)_stat;
861     exprval_t exprval;
862     VARIANT val, tmp;
863     VARIANT_BOOL b;
864     BOOL test_expr;
865     HRESULT hres;
866
867     TRACE("\n");
868
869     V_VT(&val) = VT_EMPTY;
870     test_expr = !stat->do_while;
871
872     while(1) {
873         if(test_expr) {
874             hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
875             if(FAILED(hres))
876                 break;
877
878             hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
879             exprval_release(&exprval);
880             if(FAILED(hres) || !b)
881                 break;
882         }else {
883             test_expr = TRUE;
884         }
885
886         hres = stat_eval(ctx, stat->statement, rt, &tmp);
887         if(FAILED(hres))
888             break;
889
890         VariantClear(&val);
891         val = tmp;
892
893         if(rt->type == RT_CONTINUE)
894             rt->type = RT_NORMAL;
895         if(rt->type != RT_NORMAL)
896             break;
897     }
898
899     if(FAILED(hres)) {
900         VariantClear(&val);
901         return hres;
902     }
903
904     if(rt->type == RT_BREAK)
905         rt->type = RT_NORMAL;
906
907     *ret = val;
908     return S_OK;
909 }
910
911 /* ECMA-262 3rd Edition    12.6.3 */
912 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
913 {
914     for_statement_t *stat = (for_statement_t*)_stat;
915     VARIANT val, tmp, retv;
916     exprval_t exprval;
917     VARIANT_BOOL b;
918     HRESULT hres;
919
920     TRACE("\n");
921
922     if(stat->variable_list) {
923         hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
924         if(FAILED(hres))
925             return hres;
926     }else if(stat->begin_expr) {
927         hres = expr_eval(ctx, stat->begin_expr, EXPR_NEWREF, &rt->ei, &exprval);
928         if(FAILED(hres))
929             return hres;
930
931         hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
932         exprval_release(&exprval);
933         if(FAILED(hres))
934             return hres;
935
936         VariantClear(&val);
937     }
938
939     V_VT(&retv) = VT_EMPTY;
940
941     while(1) {
942         if(stat->expr) {
943             hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
944             if(FAILED(hres))
945                 break;
946
947             hres = exprval_to_boolean(ctx, &exprval, &rt->ei, &b);
948             exprval_release(&exprval);
949             if(FAILED(hres) || !b)
950                 break;
951         }
952
953         hres = stat_eval(ctx, stat->statement, rt, &tmp);
954         if(FAILED(hres))
955             break;
956
957         VariantClear(&retv);
958         retv = tmp;
959
960         if(rt->type == RT_CONTINUE)
961             rt->type = RT_NORMAL;
962         else if(rt->type != RT_NORMAL)
963             break;
964
965         if(stat->end_expr) {
966             hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &exprval);
967             if(FAILED(hres))
968                 break;
969
970             hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
971             exprval_release(&exprval);
972             if(FAILED(hres))
973                 break;
974
975             VariantClear(&val);
976         }
977     }
978
979     if(FAILED(hres)) {
980         VariantClear(&retv);
981         return hres;
982     }
983
984     if(rt->type == RT_BREAK)
985         rt->type = RT_NORMAL;
986
987     *ret = retv;
988     return S_OK;
989 }
990
991 /* ECMA-262 3rd Edition    12.6.4 */
992 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
993 {
994     forin_statement_t *stat = (forin_statement_t*)_stat;
995     VARIANT val, name, retv, tmp;
996     DISPID id = DISPID_STARTENUM;
997     BSTR str, identifier = NULL;
998     IDispatchEx *in_obj;
999     exprval_t exprval;
1000     HRESULT hres;
1001
1002     TRACE("\n");
1003
1004     if(stat->variable) {
1005         hres = variable_list_eval(ctx, stat->variable, &rt->ei);
1006         if(FAILED(hres))
1007             return hres;
1008     }
1009
1010     hres = expr_eval(ctx, stat->in_expr, EXPR_NEWREF, &rt->ei, &exprval);
1011     if(FAILED(hres))
1012         return hres;
1013
1014     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1015     exprval_release(&exprval);
1016     if(FAILED(hres))
1017         return hres;
1018
1019     if(V_VT(&val) != VT_DISPATCH) {
1020         TRACE("in vt %d\n", V_VT(&val));
1021         VariantClear(&val);
1022         V_VT(ret) = VT_EMPTY;
1023         return S_OK;
1024     }
1025
1026     hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
1027     IDispatch_Release(V_DISPATCH(&val));
1028     if(FAILED(hres)) {
1029         TRACE("Object doesn't support IDispatchEx\n");
1030         V_VT(ret) = VT_EMPTY;
1031         return S_OK;
1032     }
1033
1034     V_VT(&retv) = VT_EMPTY;
1035
1036     if(stat->variable)
1037         identifier = SysAllocString(stat->variable->identifier);
1038
1039     while(1) {
1040         hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
1041         if(FAILED(hres) || hres == S_FALSE)
1042             break;
1043
1044         hres = IDispatchEx_GetMemberName(in_obj, id, &str);
1045         if(FAILED(hres))
1046             break;
1047
1048         TRACE("iter %s\n", debugstr_w(str));
1049
1050         if(stat->variable)
1051             hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
1052         else
1053             hres = expr_eval(ctx, stat->expr, EXPR_NEWREF, &rt->ei, &exprval);
1054         if(SUCCEEDED(hres)) {
1055             V_VT(&name) = VT_BSTR;
1056             V_BSTR(&name) = str;
1057             hres = put_value(ctx, &exprval, &name, &rt->ei);
1058             exprval_release(&exprval);
1059         }
1060         SysFreeString(str);
1061         if(FAILED(hres))
1062             break;
1063
1064         hres = stat_eval(ctx, stat->statement, rt, &tmp);
1065         if(FAILED(hres))
1066             break;
1067
1068         VariantClear(&retv);
1069         retv = tmp;
1070
1071         if(rt->type == RT_CONTINUE)
1072             rt->type = RT_NORMAL;
1073         else if(rt->type != RT_NORMAL)
1074             break;
1075     }
1076
1077     SysFreeString(identifier);
1078     IDispatchEx_Release(in_obj);
1079     if(FAILED(hres)) {
1080         VariantClear(&retv);
1081         return hres;
1082     }
1083
1084     if(rt->type == RT_BREAK)
1085         rt->type = RT_NORMAL;
1086
1087     *ret = retv;
1088     return S_OK;
1089 }
1090
1091 /* ECMA-262 3rd Edition    12.7 */
1092 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1093 {
1094     branch_statement_t *stat = (branch_statement_t*)_stat;
1095
1096     TRACE("\n");
1097
1098     if(stat->identifier) {
1099         FIXME("indentifier not implemented\n");
1100         return E_NOTIMPL;
1101     }
1102
1103     rt->type = RT_CONTINUE;
1104     V_VT(ret) = VT_EMPTY;
1105     return S_OK;
1106 }
1107
1108 /* ECMA-262 3rd Edition    12.8 */
1109 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1110 {
1111     branch_statement_t *stat = (branch_statement_t*)_stat;
1112
1113     TRACE("\n");
1114
1115     if(stat->identifier) {
1116         FIXME("indentifier not implemented\n");
1117         return E_NOTIMPL;
1118     }
1119
1120     rt->type = RT_BREAK;
1121     V_VT(ret) = VT_EMPTY;
1122     return S_OK;
1123 }
1124
1125 /* ECMA-262 3rd Edition    12.9 */
1126 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1127 {
1128     expression_statement_t *stat = (expression_statement_t*)_stat;
1129     HRESULT hres;
1130
1131     TRACE("\n");
1132
1133     if(stat->expr) {
1134         exprval_t exprval;
1135
1136         hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1137         if(FAILED(hres))
1138             return hres;
1139
1140         hres = exprval_to_value(ctx, &exprval, &rt->ei, ret);
1141         exprval_release(&exprval);
1142         if(FAILED(hres))
1143             return hres;
1144     }else {
1145         V_VT(ret) = VT_EMPTY;
1146     }
1147
1148     TRACE("= %s\n", debugstr_variant(ret));
1149     rt->type = RT_RETURN;
1150     return S_OK;
1151 }
1152
1153 /* ECMA-262 3rd Edition    12.10 */
1154 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1155 {
1156     with_statement_t *stat = (with_statement_t*)_stat;
1157     exprval_t exprval;
1158     IDispatch *disp;
1159     jsdisp_t *obj;
1160     VARIANT val;
1161     HRESULT hres;
1162
1163     TRACE("\n");
1164
1165     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1166     if(FAILED(hres))
1167         return hres;
1168
1169     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1170     exprval_release(&exprval);
1171     if(FAILED(hres))
1172         return hres;
1173
1174     hres = to_object(ctx, &val, &disp);
1175     VariantClear(&val);
1176     if(FAILED(hres))
1177         return hres;
1178
1179     obj = iface_to_jsdisp((IUnknown*)disp);
1180     IDispatch_Release(disp);
1181     if(!obj) {
1182         FIXME("disp id not jsdisp\n");
1183         return E_NOTIMPL;
1184     }
1185
1186     hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1187     jsdisp_release(obj);
1188     if(FAILED(hres))
1189         return hres;
1190
1191     hres = stat_eval(ctx, stat->statement, rt, ret);
1192
1193     scope_pop(&ctx->exec_ctx->scope_chain);
1194     return hres;
1195 }
1196
1197 /* ECMA-262 3rd Edition    12.12 */
1198 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1199 {
1200     FIXME("\n");
1201     return E_NOTIMPL;
1202 }
1203
1204 /* ECMA-262 3rd Edition    12.13 */
1205 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1206 {
1207     switch_statement_t *stat = (switch_statement_t*)_stat;
1208     case_clausule_t *iter, *default_clausule = NULL;
1209     statement_t *stat_iter;
1210     VARIANT val, cval;
1211     exprval_t exprval;
1212     BOOL b;
1213     HRESULT hres;
1214
1215     TRACE("\n");
1216
1217     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1218     if(FAILED(hres))
1219         return hres;
1220
1221     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1222     exprval_release(&exprval);
1223     if(FAILED(hres))
1224         return hres;
1225
1226     for(iter = stat->case_list; iter; iter = iter->next) {
1227         if(!iter->expr) {
1228             default_clausule = iter;
1229             continue;
1230         }
1231
1232         hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &exprval);
1233         if(FAILED(hres))
1234             break;
1235
1236         hres = exprval_to_value(ctx, &exprval, &rt->ei, &cval);
1237         exprval_release(&exprval);
1238         if(FAILED(hres))
1239             break;
1240
1241         hres = equal2_values(&val, &cval, &b);
1242         VariantClear(&cval);
1243         if(FAILED(hres) || b)
1244             break;
1245     }
1246
1247     VariantClear(&val);
1248     if(FAILED(hres))
1249         return hres;
1250
1251     if(!iter)
1252         iter = default_clausule;
1253
1254     V_VT(&val) = VT_EMPTY;
1255     if(iter) {
1256         VARIANT tmp;
1257
1258         for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1259             hres = stat_eval(ctx, stat_iter, rt, &tmp);
1260             if(FAILED(hres))
1261                 break;
1262
1263             VariantClear(&val);
1264             val = tmp;
1265
1266             if(rt->type != RT_NORMAL)
1267                 break;
1268         }
1269     }
1270
1271     if(FAILED(hres)) {
1272         VariantClear(&val);
1273         return hres;
1274     }
1275
1276     if(rt->type == RT_BREAK)
1277         rt->type = RT_NORMAL;
1278
1279     *ret = val;
1280     return S_OK;
1281 }
1282
1283 /* ECMA-262 3rd Edition    12.13 */
1284 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1285 {
1286     expression_statement_t *stat = (expression_statement_t*)_stat;
1287     exprval_t exprval;
1288     VARIANT val;
1289     HRESULT hres;
1290
1291     TRACE("\n");
1292
1293     hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &exprval);
1294     if(FAILED(hres))
1295         return hres;
1296
1297     hres = exprval_to_value(ctx, &exprval, &rt->ei, &val);
1298     exprval_release(&exprval);
1299     if(FAILED(hres))
1300         return hres;
1301
1302     rt->ei.var = val;
1303     return DISP_E_EXCEPTION;
1304 }
1305
1306 static HRESULT interp_throw(exec_ctx_t *ctx)
1307 {
1308     const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1309
1310     TRACE("%08x\n", arg);
1311
1312     return throw_reference_error(ctx->parser->script, &ctx->ei, arg, NULL);
1313 }
1314
1315 /* ECMA-262 3rd Edition    12.14 */
1316 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1317 {
1318     jsdisp_t *var_disp;
1319     VARIANT ex, val;
1320     HRESULT hres;
1321
1322     ex = rt->ei.var;
1323     memset(&rt->ei, 0, sizeof(jsexcept_t));
1324
1325     hres = create_dispex(ctx, NULL, NULL, &var_disp);
1326     if(SUCCEEDED(hres)) {
1327         hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1328         if(SUCCEEDED(hres)) {
1329             hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1330             if(SUCCEEDED(hres)) {
1331                 hres = stat_eval(ctx, block->statement, rt, &val);
1332                 scope_pop(&ctx->exec_ctx->scope_chain);
1333             }
1334         }
1335
1336         jsdisp_release(var_disp);
1337     }
1338
1339     VariantClear(&ex);
1340     if(FAILED(hres))
1341         return hres;
1342
1343     *ret = val;
1344     return S_OK;
1345 }
1346
1347 /* ECMA-262 3rd Edition    12.14 */
1348 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1349 {
1350     try_statement_t *stat = (try_statement_t*)_stat;
1351     VARIANT val;
1352     HRESULT hres;
1353
1354     TRACE("\n");
1355
1356     hres = stat_eval(ctx, stat->try_statement, rt, &val);
1357     if(FAILED(hres)) {
1358         TRACE("EXCEPTION\n");
1359         if(!stat->catch_block)
1360             return hres;
1361
1362         hres = catch_eval(ctx, stat->catch_block, rt, &val);
1363         if(FAILED(hres))
1364             return hres;
1365     }
1366
1367     if(stat->finally_statement) {
1368         VariantClear(&val);
1369         hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1370         if(FAILED(hres))
1371             return hres;
1372     }
1373
1374     *ret = val;
1375     return S_OK;
1376 }
1377
1378 static HRESULT return_bool(exprval_t *ret, DWORD b)
1379 {
1380     ret->type = EXPRVAL_VARIANT;
1381     V_VT(&ret->u.var) = VT_BOOL;
1382     V_BOOL(&ret->u.var) = b ? VARIANT_TRUE : VARIANT_FALSE;
1383
1384     return S_OK;
1385 }
1386
1387 static HRESULT get_binary_expr_values(script_ctx_t *ctx, binary_expression_t *expr, jsexcept_t *ei, VARIANT *lval, VARIANT *rval)
1388 {
1389     exprval_t exprval;
1390     HRESULT hres;
1391
1392     hres = expr_eval(ctx, expr->expression1, 0, ei, &exprval);
1393     if(FAILED(hres))
1394         return hres;
1395
1396     hres = exprval_to_value(ctx, &exprval, ei, lval);
1397     exprval_release(&exprval);
1398     if(FAILED(hres))
1399         return hres;
1400
1401     hres = expr_eval(ctx, expr->expression2, 0, ei, &exprval);
1402     if(SUCCEEDED(hres)) {
1403         hres = exprval_to_value(ctx, &exprval, ei, rval);
1404         exprval_release(&exprval);
1405     }
1406
1407     if(FAILED(hres)) {
1408         VariantClear(lval);
1409         return hres;
1410     }
1411
1412     return S_OK;
1413 }
1414
1415 typedef HRESULT (*oper_t)(script_ctx_t*,VARIANT*,VARIANT*,jsexcept_t*,VARIANT*);
1416
1417 static HRESULT binary_expr_eval(script_ctx_t *ctx, binary_expression_t *expr, oper_t oper, jsexcept_t *ei,
1418         exprval_t *ret)
1419 {
1420     VARIANT lval, rval, retv;
1421     HRESULT hres;
1422
1423     hres = get_binary_expr_values(ctx, expr, ei, &lval, &rval);
1424     if(FAILED(hres))
1425         return hres;
1426
1427     hres = oper(ctx, &lval, &rval, ei, &retv);
1428     VariantClear(&lval);
1429     VariantClear(&rval);
1430     if(FAILED(hres))
1431         return hres;
1432
1433     ret->type = EXPRVAL_VARIANT;
1434     ret->u.var = retv;
1435     return S_OK;
1436 }
1437
1438 /* ECMA-262 3rd Edition    11.13.2 */
1439 static HRESULT assign_oper_eval(script_ctx_t *ctx, expression_t *lexpr, expression_t *rexpr, oper_t oper,
1440                                 jsexcept_t *ei, exprval_t *ret)
1441 {
1442     VARIANT retv, lval, rval;
1443     exprval_t exprval, exprvalr;
1444     HRESULT hres;
1445
1446     hres = expr_eval(ctx, lexpr, EXPR_NEWREF, ei, &exprval);
1447     if(FAILED(hres))
1448         return hres;
1449
1450     hres = exprval_value(ctx, &exprval, ei, &lval);
1451     if(SUCCEEDED(hres)) {
1452         hres = expr_eval(ctx, rexpr, 0, ei, &exprvalr);
1453         if(SUCCEEDED(hres)) {
1454             hres = exprval_value(ctx, &exprvalr, ei, &rval);
1455             exprval_release(&exprvalr);
1456         }
1457         if(SUCCEEDED(hres)) {
1458             hres = oper(ctx, &lval, &rval, ei, &retv);
1459             VariantClear(&rval);
1460         }
1461         VariantClear(&lval);
1462     }
1463
1464     if(SUCCEEDED(hres)) {
1465         hres = put_value(ctx, &exprval, &retv, ei);
1466         if(FAILED(hres))
1467             VariantClear(&retv);
1468     }
1469     exprval_release(&exprval);
1470
1471     if(FAILED(hres))
1472         return hres;
1473
1474     ret->type = EXPRVAL_VARIANT;
1475     ret->u.var = retv;
1476     return S_OK;
1477 }
1478
1479 /* ECMA-262 3rd Edition    13 */
1480 HRESULT function_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1481 {
1482     function_expression_t *expr = (function_expression_t*)_expr;
1483     VARIANT var;
1484     HRESULT hres;
1485
1486     TRACE("\n");
1487
1488     if(expr->identifier) {
1489         hres = jsdisp_propget_name(ctx->exec_ctx->var_disp, expr->identifier, &var, ei, NULL/*FIXME*/);
1490         if(FAILED(hres))
1491             return hres;
1492     }else {
1493         jsdisp_t *dispex;
1494
1495         hres = create_source_function(ctx->exec_ctx->parser, expr->parameter_list, expr->source_elements, ctx->exec_ctx->scope_chain,
1496                 expr->src_str, expr->src_len, &dispex);
1497         if(FAILED(hres))
1498             return hres;
1499
1500         var_set_jsdisp(&var, dispex);
1501     }
1502
1503     ret->type = EXPRVAL_VARIANT;
1504     ret->u.var = var;
1505     return S_OK;
1506 }
1507
1508 /* ECMA-262 3rd Edition    11.2.1 */
1509 HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1510 {
1511     array_expression_t *expr = (array_expression_t*)_expr;
1512     exprval_t exprval;
1513     VARIANT member, val;
1514     DISPID id;
1515     BSTR str;
1516     IDispatch *obj = NULL;
1517     HRESULT hres;
1518
1519     TRACE("\n");
1520
1521     hres = expr_eval(ctx, expr->member_expr, 0, ei, &exprval);
1522     if(FAILED(hres))
1523         return hres;
1524
1525     hres = exprval_to_value(ctx, &exprval, ei, &member);
1526     exprval_release(&exprval);
1527     if(FAILED(hres))
1528         return hres;
1529
1530     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
1531     if(SUCCEEDED(hres)) {
1532         hres = exprval_to_value(ctx, &exprval, ei, &val);
1533         exprval_release(&exprval);
1534     }
1535
1536     if(SUCCEEDED(hres)) {
1537         hres = to_object(ctx, &member, &obj);
1538         if(FAILED(hres))
1539             VariantClear(&val);
1540     }
1541     VariantClear(&member);
1542     if(SUCCEEDED(hres)) {
1543         hres = to_string(ctx, &val, ei, &str);
1544         VariantClear(&val);
1545         if(SUCCEEDED(hres)) {
1546             hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1547             SysFreeString(str);
1548         }
1549
1550         if(SUCCEEDED(hres)) {
1551             exprval_set_idref(ret, obj, id);
1552         }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1553             exprval_init(ret);
1554             hres = S_OK;
1555         }
1556
1557         IDispatch_Release(obj);
1558     }
1559
1560     return hres;
1561 }
1562
1563 /* ECMA-262 3rd Edition    11.2.1 */
1564 HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1565 {
1566     member_expression_t *expr = (member_expression_t*)_expr;
1567     IDispatch *obj = NULL;
1568     exprval_t exprval;
1569     VARIANT member;
1570     DISPID id;
1571     BSTR str;
1572     HRESULT hres;
1573
1574     TRACE("\n");
1575
1576     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1577     if(FAILED(hres))
1578         return hres;
1579
1580     hres = exprval_to_value(ctx, &exprval, ei, &member);
1581     exprval_release(&exprval);
1582     if(FAILED(hres))
1583         return hres;
1584
1585     hres = to_object(ctx, &member, &obj);
1586     VariantClear(&member);
1587     if(FAILED(hres))
1588         return hres;
1589
1590     str = SysAllocString(expr->identifier);
1591     if(!str) {
1592         IDispatch_Release(obj);
1593         return E_OUTOFMEMORY;
1594     }
1595
1596     hres = disp_get_id(ctx, obj, str, flags & EXPR_NEWREF ? fdexNameEnsure : 0, &id);
1597     SysFreeString(str);
1598     if(SUCCEEDED(hres)) {
1599         exprval_set_idref(ret, obj, id);
1600     }else if(!(flags & EXPR_NEWREF) && hres == DISP_E_UNKNOWNNAME) {
1601         exprval_init(ret);
1602         hres = S_OK;
1603     }
1604
1605     IDispatch_Release(obj);
1606     return hres;
1607 }
1608
1609 /* ECMA-262 3rd Edition    11.2.1 */
1610 static HRESULT interp_member(exec_ctx_t *ctx)
1611 {
1612     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1613     IDispatch *obj;
1614     VARIANT v;
1615     DISPID id;
1616     HRESULT hres;
1617
1618     TRACE("\n");
1619
1620     hres = stack_pop_object(ctx, &obj);
1621     if(FAILED(hres))
1622         return hres;
1623
1624     hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
1625     if(SUCCEEDED(hres)) {
1626         hres = disp_propget(ctx->parser->script, obj, id, &v, &ctx->ei, NULL/*FIXME*/);
1627     }else if(hres == DISP_E_UNKNOWNNAME) {
1628         V_VT(&v) = VT_EMPTY;
1629         hres = S_OK;
1630     }
1631     IDispatch_Release(obj);
1632     if(FAILED(hres))
1633         return hres;
1634
1635     return stack_push(ctx, &v);
1636 }
1637
1638 /* ECMA-262 3rd Edition    11.2.1 */
1639 static HRESULT interp_memberid(exec_ctx_t *ctx)
1640 {
1641     VARIANT *objv, *namev;
1642     IDispatch *obj;
1643     BSTR name;
1644     DISPID id;
1645     HRESULT hres;
1646
1647     TRACE("\n");
1648
1649     namev = stack_pop(ctx);
1650     objv = stack_pop(ctx);
1651
1652     hres = to_object(ctx->parser->script, objv, &obj);
1653     VariantClear(objv);
1654     if(SUCCEEDED(hres)) {
1655         hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1656         if(FAILED(hres))
1657             IDispatch_Release(obj);
1658     }
1659     VariantClear(namev);
1660     if(FAILED(hres))
1661         return hres;
1662
1663     hres = disp_get_id(ctx->parser->script, obj, name, fdexNameEnsure, &id);
1664     SysFreeString(name);
1665     if(FAILED(hres)) {
1666         IDispatch_Release(obj);
1667         return hres;
1668     }
1669
1670     return stack_push_objid(ctx, obj, id);
1671 }
1672
1673 /* ECMA-262 3rd Edition    11.2.1 */
1674 static HRESULT interp_refval(exec_ctx_t *ctx)
1675 {
1676     IDispatch *disp;
1677     VARIANT v;
1678     DISPID id;
1679     HRESULT hres;
1680
1681     TRACE("\n");
1682
1683     disp = stack_top_objid(ctx, &id);
1684     if(!disp)
1685         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1686
1687     hres = disp_propget(ctx->parser->script, disp, id, &v, &ctx->ei, NULL/*FIXME*/);
1688     if(FAILED(hres))
1689         return hres;
1690
1691     return stack_push(ctx, &v);
1692 }
1693
1694 static void free_dp(DISPPARAMS *dp)
1695 {
1696     DWORD i;
1697
1698     for(i=0; i < dp->cArgs; i++)
1699         VariantClear(dp->rgvarg+i);
1700     heap_free(dp->rgvarg);
1701 }
1702
1703 static HRESULT args_to_param(script_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1704 {
1705     VARIANTARG *vargs;
1706     exprval_t exprval;
1707     argument_t *iter;
1708     DWORD cnt = 0, i;
1709     HRESULT hres = S_OK;
1710
1711     memset(dp, 0, sizeof(*dp));
1712     if(!args)
1713         return S_OK;
1714
1715     for(iter = args; iter; iter = iter->next)
1716         cnt++;
1717
1718     vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1719     if(!vargs)
1720         return E_OUTOFMEMORY;
1721
1722     for(i = cnt, iter = args; iter; iter = iter->next) {
1723         hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1724         if(FAILED(hres))
1725             break;
1726
1727         hres = exprval_to_value(ctx, &exprval, ei, vargs + (--i));
1728         exprval_release(&exprval);
1729         if(FAILED(hres))
1730             break;
1731     }
1732
1733     if(FAILED(hres)) {
1734         free_dp(dp);
1735         return hres;
1736     }
1737
1738     dp->rgvarg = vargs;
1739     dp->cArgs = cnt;
1740     return S_OK;
1741 }
1742
1743 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1744 {
1745     VARIANT tmp;
1746     unsigned i;
1747
1748     dp->cArgs = arg_cnt;
1749     dp->rgdispidNamedArgs = NULL;
1750     dp->cNamedArgs = 0;
1751
1752     assert(ctx->top >= arg_cnt);
1753
1754     for(i=1; i*2 <= arg_cnt; i++) {
1755         tmp = ctx->stack[ctx->top-i];
1756         ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1757         ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1758     }
1759
1760     dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1761 }
1762
1763 /* ECMA-262 3rd Edition    11.2.2 */
1764 static HRESULT interp_new(exec_ctx_t *ctx)
1765 {
1766     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1767     VARIANT *constr, v;
1768     DISPPARAMS dp;
1769     HRESULT hres;
1770
1771     TRACE("%d\n", arg);
1772
1773     constr = stack_topn(ctx, arg);
1774
1775     /* NOTE: Should use to_object here */
1776
1777     if(V_VT(constr) == VT_NULL)
1778         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1779     else if(V_VT(constr) != VT_DISPATCH)
1780         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1781     else if(!V_DISPATCH(constr))
1782         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1783
1784     jsstack_to_dp(ctx, arg, &dp);
1785     hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1786             DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1787     if(FAILED(hres))
1788         return hres;
1789
1790     stack_popn(ctx, arg+1);
1791     return stack_push(ctx, &v);
1792 }
1793
1794 /* ECMA-262 3rd Edition    11.2.3 */
1795 HRESULT call_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1796 {
1797     call_expression_t *expr = (call_expression_t*)_expr;
1798     VARIANT var;
1799     exprval_t exprval;
1800     DISPPARAMS dp;
1801     HRESULT hres;
1802
1803     TRACE("\n");
1804
1805     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1806     if(FAILED(hres))
1807         return hres;
1808
1809     hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1810     if(SUCCEEDED(hres)) {
1811         switch(exprval.type) {
1812         case EXPRVAL_VARIANT:
1813             if(V_VT(&exprval.u.var) == VT_DISPATCH)
1814                 hres = disp_call(ctx, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1815                         DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1816             else
1817                 hres = throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1818             break;
1819         case EXPRVAL_IDREF:
1820             hres = disp_call(ctx, exprval.u.idref.disp, exprval.u.idref.id,
1821                     DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1822             break;
1823         case EXPRVAL_INVALID:
1824             hres = throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1825             break;
1826         default:
1827             FIXME("unimplemented type %d\n", exprval.type);
1828             hres = E_NOTIMPL;
1829         }
1830
1831         free_dp(&dp);
1832     }
1833
1834     exprval_release(&exprval);
1835     if(FAILED(hres))
1836         return hres;
1837
1838     ret->type = EXPRVAL_VARIANT;
1839     if(flags & EXPR_NOVAL) {
1840         V_VT(&ret->u.var) = VT_EMPTY;
1841     }else {
1842         TRACE("= %s\n", debugstr_variant(&var));
1843         ret->u.var = var;
1844     }
1845     return S_OK;
1846 }
1847
1848 /* ECMA-262 3rd Edition    11.1.1 */
1849 static HRESULT interp_this(exec_ctx_t *ctx)
1850 {
1851     VARIANT v;
1852
1853     TRACE("\n");
1854
1855     V_VT(&v) = VT_DISPATCH;
1856     V_DISPATCH(&v) = ctx->this_obj;
1857     IDispatch_AddRef(ctx->this_obj);
1858     return stack_push(ctx, &v);
1859 }
1860
1861 /* ECMA-262 3rd Edition    10.1.4 */
1862 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1863 {
1864     identifier_expression_t *expr = (identifier_expression_t*)_expr;
1865     BSTR identifier;
1866     HRESULT hres;
1867
1868     TRACE("\n");
1869
1870     identifier = SysAllocString(expr->identifier);
1871     if(!identifier)
1872         return E_OUTOFMEMORY;
1873
1874     hres = identifier_eval(ctx, identifier, flags, ei, ret);
1875
1876     SysFreeString(identifier);
1877     return hres;
1878 }
1879
1880 /* ECMA-262 3rd Edition    10.1.4 */
1881 static HRESULT interp_ident(exec_ctx_t *ctx)
1882 {
1883     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1884     exprval_t exprval;
1885     VARIANT v;
1886     HRESULT hres;
1887
1888     TRACE("%s\n", debugstr_w(arg));
1889
1890     hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1891     if(FAILED(hres))
1892         return hres;
1893
1894     hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1895     exprval_release(&exprval);
1896     if(FAILED(hres))
1897         return hres;
1898
1899     return stack_push(ctx, &v);
1900 }
1901
1902 /* ECMA-262 3rd Edition    10.1.4 */
1903 static HRESULT interp_identid(exec_ctx_t *ctx)
1904 {
1905     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1906     exprval_t exprval;
1907     HRESULT hres;
1908
1909     TRACE("%s\n", debugstr_w(arg));
1910
1911     hres = identifier_eval(ctx->parser->script, arg, EXPR_NEWREF, &ctx->ei, &exprval);
1912     if(FAILED(hres))
1913         return hres;
1914
1915     if(exprval.type != EXPRVAL_IDREF) {
1916         WARN("invalid ref\n");
1917         exprval_release(&exprval);
1918         return stack_push_objid(ctx, NULL, -1);
1919     }
1920
1921     return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1922 }
1923
1924 /* ECMA-262 3rd Edition    7.8.1 */
1925 static HRESULT interp_null(exec_ctx_t *ctx)
1926 {
1927     VARIANT v;
1928
1929     TRACE("\n");
1930
1931     V_VT(&v) = VT_NULL;
1932     return stack_push(ctx, &v);
1933 }
1934
1935 /* ECMA-262 3rd Edition    7.8.2 */
1936 static HRESULT interp_bool(exec_ctx_t *ctx)
1937 {
1938     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1939
1940     TRACE("%s\n", arg ? "true" : "false");
1941
1942     return stack_push_bool(ctx, arg);
1943 }
1944
1945 /* ECMA-262 3rd Edition    7.8.3 */
1946 static HRESULT interp_int(exec_ctx_t *ctx)
1947 {
1948     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1949     VARIANT v;
1950
1951     TRACE("%d\n", arg);
1952
1953     V_VT(&v) = VT_I4;
1954     V_I4(&v) = arg;
1955     return stack_push(ctx, &v);
1956 }
1957
1958 /* ECMA-262 3rd Edition    7.8.3 */
1959 static HRESULT interp_double(exec_ctx_t *ctx)
1960 {
1961     const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1962     VARIANT v;
1963
1964     TRACE("%lf\n", arg);
1965
1966     V_VT(&v) = VT_R8;
1967     V_R8(&v) = arg;
1968     return stack_push(ctx, &v);
1969 }
1970
1971 /* ECMA-262 3rd Edition    7.8.4 */
1972 static HRESULT interp_str(exec_ctx_t *ctx)
1973 {
1974     const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1975     VARIANT v;
1976
1977     TRACE("%s\n", debugstr_w(str));
1978
1979     V_VT(&v) = VT_BSTR;
1980     V_BSTR(&v) = SysAllocString(str);
1981     if(!V_BSTR(&v))
1982         return E_OUTOFMEMORY;
1983
1984     return stack_push(ctx, &v);
1985 }
1986
1987 /* ECMA-262 3rd Edition    7.8 */
1988 static HRESULT interp_regexp(exec_ctx_t *ctx)
1989 {
1990     const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1991     const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1992     jsdisp_t *regexp;
1993     VARIANT v;
1994     HRESULT hres;
1995
1996     TRACE("%s %x\n", debugstr_w(source), flags);
1997
1998     hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, &regexp);
1999     if(FAILED(hres))
2000         return hres;
2001
2002     var_set_jsdisp(&v, regexp);
2003     return stack_push(ctx, &v);
2004 }
2005
2006 /* ECMA-262 3rd Edition    11.1.4 */
2007 HRESULT array_literal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2008 {
2009     array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
2010     DWORD length = 0, i = 0;
2011     array_element_t *elem;
2012     jsdisp_t *array;
2013     exprval_t exprval;
2014     VARIANT val;
2015     HRESULT hres;
2016
2017     TRACE("\n");
2018
2019     for(elem = expr->element_list; elem; elem = elem->next)
2020         length += elem->elision+1;
2021     length += expr->length;
2022
2023     hres = create_array(ctx, length, &array);
2024     if(FAILED(hres))
2025         return hres;
2026
2027     for(elem = expr->element_list; elem; elem = elem->next) {
2028         i += elem->elision;
2029
2030         hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
2031         if(FAILED(hres))
2032             break;
2033
2034         hres = exprval_to_value(ctx, &exprval, ei, &val);
2035         exprval_release(&exprval);
2036         if(FAILED(hres))
2037             break;
2038
2039         hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
2040         VariantClear(&val);
2041         if(FAILED(hres))
2042             break;
2043
2044         i++;
2045     }
2046
2047     if(FAILED(hres)) {
2048         jsdisp_release(array);
2049         return hres;
2050     }
2051
2052     ret->type = EXPRVAL_VARIANT;
2053     var_set_jsdisp(&ret->u.var, array);
2054     return S_OK;
2055 }
2056
2057 /* ECMA-262 3rd Edition    11.1.5 */
2058 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2059 {
2060     property_value_expression_t *expr = (property_value_expression_t*)_expr;
2061     VARIANT val, tmp;
2062     jsdisp_t *obj;
2063     prop_val_t *iter;
2064     exprval_t exprval;
2065     BSTR name;
2066     HRESULT hres;
2067
2068     TRACE("\n");
2069
2070     hres = create_object(ctx, NULL, &obj);
2071     if(FAILED(hres))
2072         return hres;
2073
2074     for(iter = expr->property_list; iter; iter = iter->next) {
2075         hres = literal_to_var(ctx, iter->name, &tmp);
2076         if(FAILED(hres))
2077             break;
2078
2079         hres = to_string(ctx, &tmp, ei, &name);
2080         VariantClear(&tmp);
2081         if(FAILED(hres))
2082             break;
2083
2084         hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
2085         if(SUCCEEDED(hres)) {
2086             hres = exprval_to_value(ctx, &exprval, ei, &val);
2087             exprval_release(&exprval);
2088             if(SUCCEEDED(hres)) {
2089                 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
2090                 VariantClear(&val);
2091             }
2092         }
2093
2094         SysFreeString(name);
2095         if(FAILED(hres))
2096             break;
2097     }
2098
2099     if(FAILED(hres)) {
2100         jsdisp_release(obj);
2101         return hres;
2102     }
2103
2104     ret->type = EXPRVAL_VARIANT;
2105     var_set_jsdisp(&ret->u.var, obj);
2106     return S_OK;
2107 }
2108
2109 /* ECMA-262 3rd Edition    11.11 */
2110 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
2111 {
2112     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2113     VARIANT_BOOL b;
2114     HRESULT hres;
2115
2116     TRACE("\n");
2117
2118     hres = to_boolean(stack_top(ctx), &b);
2119     if(FAILED(hres))
2120         return hres;
2121
2122     if(b) {
2123         ctx->ip = arg;
2124     }else {
2125         stack_popn(ctx, 1);
2126         ctx->ip++;
2127     }
2128     return S_OK;
2129 }
2130
2131 /* ECMA-262 3rd Edition    11.11 */
2132 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2133 {
2134     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2135     VARIANT_BOOL b;
2136     HRESULT hres;
2137
2138     TRACE("\n");
2139
2140     hres = to_boolean(stack_top(ctx), &b);
2141     if(FAILED(hres))
2142         return hres;
2143
2144     if(b) {
2145         stack_popn(ctx, 1);
2146         ctx->ip++;
2147     }else {
2148         ctx->ip = arg;
2149     }
2150     return S_OK;
2151 }
2152
2153 /* ECMA-262 3rd Edition    11.10 */
2154 static HRESULT interp_or(exec_ctx_t *ctx)
2155 {
2156     INT l, r;
2157     HRESULT hres;
2158
2159     TRACE("\n");
2160
2161     hres = stack_pop_int(ctx, &r);
2162     if(FAILED(hres))
2163         return hres;
2164
2165     hres = stack_pop_int(ctx, &l);
2166     if(FAILED(hres))
2167         return hres;
2168
2169     return stack_push_int(ctx, l|r);
2170 }
2171
2172 /* ECMA-262 3rd Edition    11.10 */
2173 static HRESULT interp_xor(exec_ctx_t *ctx)
2174 {
2175     INT l, r;
2176     HRESULT hres;
2177
2178     TRACE("\n");
2179
2180     hres = stack_pop_int(ctx, &r);
2181     if(FAILED(hres))
2182         return hres;
2183
2184     hres = stack_pop_int(ctx, &l);
2185     if(FAILED(hres))
2186         return hres;
2187
2188     return stack_push_int(ctx, l^r);
2189 }
2190
2191 /* ECMA-262 3rd Edition    11.10 */
2192 static HRESULT bitand_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2193 {
2194     INT li, ri;
2195     HRESULT hres;
2196
2197     hres = to_int32(ctx, lval, ei, &li);
2198     if(FAILED(hres))
2199         return hres;
2200
2201     hres = to_int32(ctx, rval, ei, &ri);
2202     if(FAILED(hres))
2203         return hres;
2204
2205     V_VT(retv) = VT_I4;
2206     V_I4(retv) = li&ri;
2207     return S_OK;
2208 }
2209
2210 /* ECMA-262 3rd Edition    11.10 */
2211 HRESULT binary_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2212 {
2213     binary_expression_t *expr = (binary_expression_t*)_expr;
2214
2215     TRACE("\n");
2216
2217     return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
2218 }
2219
2220 /* ECMA-262 3rd Edition    11.8.6 */
2221 static HRESULT instanceof_eval(script_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
2222 {
2223     jsdisp_t *obj, *iter, *tmp = NULL;
2224     VARIANT_BOOL ret = VARIANT_FALSE;
2225     BOOL b;
2226     VARIANT var;
2227     HRESULT hres;
2228
2229     static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2230
2231     if(V_VT(objv) != VT_DISPATCH || !V_DISPATCH(objv))
2232         return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2233
2234     obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2235     if(!obj) {
2236         FIXME("non-jsdisp objects not supported\n");
2237         return E_FAIL;
2238     }
2239
2240     if(is_class(obj, JSCLASS_FUNCTION)) {
2241         hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2242     }else {
2243         hres = throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2244     }
2245     jsdisp_release(obj);
2246     if(FAILED(hres))
2247         return hres;
2248
2249     if(V_VT(&var) == VT_DISPATCH) {
2250         if(V_VT(inst) == VT_DISPATCH)
2251             tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2252         for(iter = tmp; iter; iter = iter->prototype) {
2253             hres = disp_cmp(V_DISPATCH(&var), to_disp(iter), &b);
2254             if(FAILED(hres))
2255                 break;
2256             if(b) {
2257                 ret = VARIANT_TRUE;
2258                 break;
2259             }
2260         }
2261
2262         if(tmp)
2263             jsdisp_release(tmp);
2264     }else {
2265         FIXME("prototype is not an object\n");
2266         hres = E_FAIL;
2267     }
2268
2269     VariantClear(&var);
2270     if(FAILED(hres))
2271         return hres;
2272
2273     V_VT(retv) = VT_BOOL;
2274     V_BOOL(retv) = ret;
2275     return S_OK;
2276 }
2277
2278 /* ECMA-262 3rd Edition    11.8.6 */
2279 HRESULT instanceof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2280 {
2281     binary_expression_t *expr = (binary_expression_t*)_expr;
2282
2283     TRACE("\n");
2284
2285     return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2286 }
2287
2288 /* ECMA-262 3rd Edition    11.8.7 */
2289 static HRESULT interp_in(exec_ctx_t *ctx)
2290 {
2291     VARIANT *obj, *v;
2292     DISPID id = 0;
2293     BOOL ret;
2294     BSTR str;
2295     HRESULT hres;
2296
2297     TRACE("\n");
2298
2299     obj = stack_pop(ctx);
2300     v = stack_pop(ctx);
2301
2302     if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2303         VariantClear(obj);
2304         VariantClear(v);
2305         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2306     }
2307
2308     hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2309     VariantClear(v);
2310     if(FAILED(hres)) {
2311         IDispatch_Release(V_DISPATCH(obj));
2312         return hres;
2313     }
2314
2315     hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2316     IDispatch_Release(V_DISPATCH(obj));
2317     SysFreeString(str);
2318     if(SUCCEEDED(hres))
2319         ret = TRUE;
2320     else if(hres == DISP_E_UNKNOWNNAME)
2321         ret = FALSE;
2322     else
2323         return hres;
2324
2325     return stack_push_bool(ctx, ret);
2326 }
2327
2328 /* ECMA-262 3rd Edition    11.6.1 */
2329 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2330 {
2331     VARIANT r, l;
2332     HRESULT hres;
2333
2334     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2335     if(FAILED(hres))
2336         return hres;
2337
2338     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2339     if(FAILED(hres)) {
2340         VariantClear(&l);
2341         return hres;
2342     }
2343
2344     if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2345         BSTR lstr = NULL, rstr = NULL;
2346
2347         if(V_VT(&l) == VT_BSTR)
2348             lstr = V_BSTR(&l);
2349         else
2350             hres = to_string(ctx, &l, ei, &lstr);
2351
2352         if(SUCCEEDED(hres)) {
2353             if(V_VT(&r) == VT_BSTR)
2354                 rstr = V_BSTR(&r);
2355             else
2356                 hres = to_string(ctx, &r, ei, &rstr);
2357         }
2358
2359         if(SUCCEEDED(hres)) {
2360             int len1, len2;
2361
2362             len1 = SysStringLen(lstr);
2363             len2 = SysStringLen(rstr);
2364
2365             V_VT(retv) = VT_BSTR;
2366             V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2367             memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2368             memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2369         }
2370
2371         if(V_VT(&l) != VT_BSTR)
2372             SysFreeString(lstr);
2373         if(V_VT(&r) != VT_BSTR)
2374             SysFreeString(rstr);
2375     }else {
2376         VARIANT nl, nr;
2377
2378         hres = to_number(ctx, &l, ei, &nl);
2379         if(SUCCEEDED(hres)) {
2380             hres = to_number(ctx, &r, ei, &nr);
2381             if(SUCCEEDED(hres))
2382                 num_set_val(retv, num_val(&nl) + num_val(&nr));
2383         }
2384     }
2385
2386     VariantClear(&r);
2387     VariantClear(&l);
2388     return hres;
2389 }
2390
2391 /* ECMA-262 3rd Edition    11.6.1 */
2392 static HRESULT interp_add(exec_ctx_t *ctx)
2393 {
2394     VARIANT *l, *r, ret;
2395     HRESULT hres;
2396
2397     r = stack_pop(ctx);
2398     l = stack_pop(ctx);
2399
2400     TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2401
2402     hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2403     VariantClear(l);
2404     VariantClear(r);
2405     if(FAILED(hres))
2406         return hres;
2407
2408     return stack_push(ctx, &ret);
2409 }
2410
2411 /* ECMA-262 3rd Edition    11.6.2 */
2412 static HRESULT interp_sub(exec_ctx_t *ctx)
2413 {
2414     VARIANT l, r;
2415     HRESULT hres;
2416
2417     TRACE("\n");
2418
2419     hres = stack_pop_number(ctx, &r);
2420     if(FAILED(hres))
2421         return hres;
2422
2423     hres = stack_pop_number(ctx, &l);
2424     if(FAILED(hres))
2425         return hres;
2426
2427     return stack_push_number(ctx, num_val(&l)-num_val(&r));
2428 }
2429
2430 /* ECMA-262 3rd Edition    11.5.1 */
2431 static HRESULT interp_mul(exec_ctx_t *ctx)
2432 {
2433     VARIANT l, r;
2434     HRESULT hres;
2435
2436     TRACE("\n");
2437
2438     hres = stack_pop_number(ctx, &r);
2439     if(FAILED(hres))
2440         return hres;
2441
2442     hres = stack_pop_number(ctx, &l);
2443     if(FAILED(hres))
2444         return hres;
2445
2446     return stack_push_number(ctx, num_val(&l)*num_val(&r));
2447 }
2448
2449 /* ECMA-262 3rd Edition    11.5.2 */
2450 static HRESULT interp_div(exec_ctx_t *ctx)
2451 {
2452     VARIANT l, r;
2453     HRESULT hres;
2454
2455     TRACE("\n");
2456
2457     hres = stack_pop_number(ctx, &r);
2458     if(FAILED(hres))
2459         return hres;
2460
2461     hres = stack_pop_number(ctx, &l);
2462     if(FAILED(hres))
2463         return hres;
2464
2465     return stack_push_number(ctx, num_val(&l)/num_val(&r));
2466 }
2467
2468 /* ECMA-262 3rd Edition    11.5.3 */
2469 static HRESULT interp_mod(exec_ctx_t *ctx)
2470 {
2471     VARIANT l, r;
2472     HRESULT hres;
2473
2474     TRACE("\n");
2475
2476     hres = stack_pop_number(ctx, &r);
2477     if(FAILED(hres))
2478         return hres;
2479
2480     hres = stack_pop_number(ctx, &l);
2481     if(FAILED(hres))
2482         return hres;
2483
2484     return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2485 }
2486
2487 /* ECMA-262 3rd Edition    11.4.2 */
2488 HRESULT delete_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2489 {
2490     unary_expression_t *expr = (unary_expression_t*)_expr;
2491     VARIANT_BOOL b = VARIANT_FALSE;
2492     exprval_t exprval;
2493     HRESULT hres;
2494
2495     TRACE("\n");
2496
2497     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2498     if(FAILED(hres))
2499         return hres;
2500
2501     switch(exprval.type) {
2502     case EXPRVAL_IDREF: {
2503         IDispatchEx *dispex;
2504
2505         hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2506         if(SUCCEEDED(hres)) {
2507             hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2508             b = VARIANT_TRUE;
2509             IDispatchEx_Release(dispex);
2510         }
2511         break;
2512     }
2513     default:
2514         FIXME("unsupported type %d\n", exprval.type);
2515         hres = E_NOTIMPL;
2516     }
2517
2518     exprval_release(&exprval);
2519     if(FAILED(hres))
2520         return hres;
2521
2522     return return_bool(ret, b);
2523 }
2524
2525 /* ECMA-262 3rd Edition    11.4.2 */
2526 static HRESULT interp_delete(exec_ctx_t *ctx)
2527 {
2528     VARIANT *obj_var, *name_var;
2529     IDispatchEx *dispex;
2530     IDispatch *obj;
2531     BSTR name;
2532     BOOL ret;
2533     HRESULT hres;
2534
2535     TRACE("\n");
2536
2537     name_var = stack_pop(ctx);
2538     obj_var = stack_pop(ctx);
2539
2540     hres = to_object(ctx->parser->script, obj_var, &obj);
2541     VariantClear(obj_var);
2542     if(FAILED(hres)) {
2543         VariantClear(name_var);
2544         return hres;
2545     }
2546
2547     hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2548     VariantClear(name_var);
2549     if(FAILED(hres)) {
2550         IDispatch_Release(obj);
2551         return hres;
2552     }
2553
2554     hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2555     if(SUCCEEDED(hres)) {
2556         hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2557         ret = TRUE;
2558         IDispatchEx_Release(dispex);
2559     }else {
2560         hres = S_OK;
2561         ret = FALSE;
2562     }
2563
2564     IDispatch_Release(obj);
2565     SysFreeString(name);
2566     if(FAILED(hres))
2567         return hres;
2568
2569     return stack_push_bool(ctx, ret);
2570 }
2571
2572 /* ECMA-262 3rd Edition    11.4.2 */
2573 static HRESULT interp_void(exec_ctx_t *ctx)
2574 {
2575     VARIANT v;
2576
2577     TRACE("\n");
2578
2579     stack_popn(ctx, 1);
2580
2581     V_VT(&v) = VT_EMPTY;
2582     return stack_push(ctx, &v);
2583 }
2584
2585 /* ECMA-262 3rd Edition    11.4.3 */
2586 static HRESULT typeof_exprval(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2587 {
2588     VARIANT val;
2589     HRESULT hres;
2590
2591     static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2592     static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2593     static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2594     static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2595     static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2596     static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2597     static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
2598
2599     if(exprval->type == EXPRVAL_INVALID) {
2600         *ret = undefinedW;
2601         return S_OK;
2602     }
2603
2604     hres = exprval_to_value(ctx, exprval, ei, &val);
2605     if(FAILED(hres)) {
2606         if(exprval->type == EXPRVAL_IDREF) {
2607             *ret = unknownW;
2608             return S_OK;
2609         }
2610         return hres;
2611     }
2612
2613     switch(V_VT(&val)) {
2614     case VT_EMPTY:
2615         *ret = undefinedW;
2616         break;
2617     case VT_NULL:
2618         *ret = objectW;
2619         break;
2620     case VT_BOOL:
2621         *ret = booleanW;
2622         break;
2623     case VT_I4:
2624     case VT_R8:
2625         *ret = numberW;
2626         break;
2627     case VT_BSTR:
2628         *ret = stringW;
2629         break;
2630     case VT_DISPATCH: {
2631         jsdisp_t *dispex;
2632
2633         if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2634             *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2635             jsdisp_release(dispex);
2636         }else {
2637             *ret = objectW;
2638         }
2639         break;
2640     }
2641     default:
2642         FIXME("unhandled vt %d\n", V_VT(&val));
2643         hres = E_NOTIMPL;
2644     }
2645
2646     VariantClear(&val);
2647     return hres;
2648 }
2649
2650 HRESULT typeof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2651 {
2652     unary_expression_t *expr = (unary_expression_t*)_expr;
2653     const WCHAR *str = NULL;
2654     exprval_t exprval;
2655     HRESULT hres;
2656
2657     TRACE("\n");
2658
2659     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2660     if(FAILED(hres))
2661         return hres;
2662
2663     hres = typeof_exprval(ctx, &exprval, ei, &str);
2664     exprval_release(&exprval);
2665     if(FAILED(hres))
2666         return hres;
2667
2668     ret->type = EXPRVAL_VARIANT;
2669     V_VT(&ret->u.var) = VT_BSTR;
2670     V_BSTR(&ret->u.var) = SysAllocString(str);
2671     if(!V_BSTR(&ret->u.var))
2672         return E_OUTOFMEMORY;
2673
2674     return S_OK;
2675 }
2676
2677 /* ECMA-262 3rd Edition    11.4.7 */
2678 static HRESULT interp_minus(exec_ctx_t *ctx)
2679 {
2680     VARIANT n;
2681     HRESULT hres;
2682
2683     TRACE("\n");
2684
2685     hres = stack_pop_number(ctx, &n);
2686     if(FAILED(hres))
2687         return hres;
2688
2689     return stack_push_number(ctx, -num_val(&n));
2690 }
2691
2692 /* ECMA-262 3rd Edition    11.4.6 */
2693 static HRESULT interp_tonum(exec_ctx_t *ctx)
2694 {
2695     VARIANT *v, num;
2696     HRESULT hres;
2697
2698     TRACE("\n");
2699
2700     v = stack_pop(ctx);
2701     hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2702     VariantClear(v);
2703     if(FAILED(hres))
2704         return hres;
2705
2706     return stack_push(ctx, &num);
2707 }
2708
2709 /* ECMA-262 3rd Edition    11.3.1 */
2710 HRESULT post_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2711 {
2712     unary_expression_t *expr = (unary_expression_t*)_expr;
2713     VARIANT val, num;
2714     exprval_t exprval;
2715     HRESULT hres;
2716
2717     TRACE("\n");
2718
2719     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2720     if(FAILED(hres))
2721         return hres;
2722
2723     hres = exprval_value(ctx, &exprval, ei, &val);
2724     if(SUCCEEDED(hres)) {
2725         hres = to_number(ctx, &val, ei, &num);
2726         VariantClear(&val);
2727     }
2728
2729     if(SUCCEEDED(hres)) {
2730         VARIANT inc;
2731         num_set_val(&inc, num_val(&num)+1.0);
2732         hres = put_value(ctx, &exprval, &inc, ei);
2733     }
2734
2735     exprval_release(&exprval);
2736     if(FAILED(hres))
2737         return hres;
2738
2739     ret->type = EXPRVAL_VARIANT;
2740     ret->u.var = num;
2741     return S_OK;
2742 }
2743
2744 /* ECMA-262 3rd Edition    11.3.2 */
2745 HRESULT post_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2746 {
2747     unary_expression_t *expr = (unary_expression_t*)_expr;
2748     VARIANT val, num;
2749     exprval_t exprval;
2750     HRESULT hres;
2751
2752     TRACE("\n");
2753
2754     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2755     if(FAILED(hres))
2756         return hres;
2757
2758     hres = exprval_value(ctx, &exprval, ei, &val);
2759     if(SUCCEEDED(hres)) {
2760         hres = to_number(ctx, &val, ei, &num);
2761         VariantClear(&val);
2762     }
2763
2764     if(SUCCEEDED(hres)) {
2765         VARIANT dec;
2766         num_set_val(&dec, num_val(&num)-1.0);
2767         hres = put_value(ctx, &exprval, &dec, ei);
2768     }
2769
2770     exprval_release(&exprval);
2771     if(FAILED(hres))
2772         return hres;
2773
2774     ret->type = EXPRVAL_VARIANT;
2775     ret->u.var = num;
2776     return S_OK;
2777 }
2778
2779 /* ECMA-262 3rd Edition    11.4.4 */
2780 HRESULT pre_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2781 {
2782     unary_expression_t *expr = (unary_expression_t*)_expr;
2783     VARIANT val, num;
2784     exprval_t exprval;
2785     HRESULT hres;
2786
2787     TRACE("\n");
2788
2789     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2790     if(FAILED(hres))
2791         return hres;
2792
2793     hres = exprval_value(ctx, &exprval, ei, &val);
2794     if(SUCCEEDED(hres)) {
2795         hres = to_number(ctx, &val, ei, &num);
2796         VariantClear(&val);
2797     }
2798
2799     if(SUCCEEDED(hres)) {
2800         num_set_val(&val, num_val(&num)+1.0);
2801         hres = put_value(ctx, &exprval, &val, ei);
2802     }
2803
2804     exprval_release(&exprval);
2805     if(FAILED(hres))
2806         return hres;
2807
2808     ret->type = EXPRVAL_VARIANT;
2809     ret->u.var = val;
2810     return S_OK;
2811 }
2812
2813 /* ECMA-262 3rd Edition    11.4.5 */
2814 HRESULT pre_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2815 {
2816     unary_expression_t *expr = (unary_expression_t*)_expr;
2817     VARIANT val, num;
2818     exprval_t exprval;
2819     HRESULT hres;
2820
2821     TRACE("\n");
2822
2823     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2824     if(FAILED(hres))
2825         return hres;
2826
2827     hres = exprval_value(ctx, &exprval, ei, &val);
2828     if(SUCCEEDED(hres)) {
2829         hres = to_number(ctx, &val, ei, &num);
2830         VariantClear(&val);
2831     }
2832
2833     if(SUCCEEDED(hres)) {
2834         num_set_val(&val, num_val(&num)-1.0);
2835         hres = put_value(ctx, &exprval, &val, ei);
2836     }
2837
2838     exprval_release(&exprval);
2839     if(FAILED(hres))
2840         return hres;
2841
2842     ret->type = EXPRVAL_VARIANT;
2843     ret->u.var = val;
2844     return S_OK;
2845 }
2846
2847 /* ECMA-262 3rd Edition    11.9.3 */
2848 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2849 {
2850     if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2851        return equal2_values(lval, rval, ret);
2852
2853     /* FIXME: NULL disps should be handled in more general way */
2854     if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2855         VARIANT v;
2856         V_VT(&v) = VT_NULL;
2857         return equal_values(ctx, &v, rval, ei, ret);
2858     }
2859
2860     if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2861         VARIANT v;
2862         V_VT(&v) = VT_NULL;
2863         return equal_values(ctx, lval, &v, ei, ret);
2864     }
2865
2866     if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2867        (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2868         *ret = TRUE;
2869         return S_OK;
2870     }
2871
2872     if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2873         VARIANT v;
2874         HRESULT hres;
2875
2876         hres = to_number(ctx, lval, ei, &v);
2877         if(FAILED(hres))
2878             return hres;
2879
2880         return equal_values(ctx, &v, rval, ei, ret);
2881     }
2882
2883     if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2884         VARIANT v;
2885         HRESULT hres;
2886
2887         hres = to_number(ctx, rval, ei, &v);
2888         if(FAILED(hres))
2889             return hres;
2890
2891         return equal_values(ctx, lval, &v, ei, ret);
2892     }
2893
2894     if(V_VT(rval) == VT_BOOL) {
2895         VARIANT v;
2896
2897         V_VT(&v) = VT_I4;
2898         V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2899         return equal_values(ctx, lval, &v, ei, ret);
2900     }
2901
2902     if(V_VT(lval) == VT_BOOL) {
2903         VARIANT v;
2904
2905         V_VT(&v) = VT_I4;
2906         V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2907         return equal_values(ctx, &v, rval, ei, ret);
2908     }
2909
2910
2911     if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2912         VARIANT v;
2913         HRESULT hres;
2914
2915         hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2916         if(FAILED(hres))
2917             return hres;
2918
2919         hres = equal_values(ctx, lval, &v, ei, ret);
2920
2921         VariantClear(&v);
2922         return hres;
2923     }
2924
2925
2926     if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2927         VARIANT v;
2928         HRESULT hres;
2929
2930         hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2931         if(FAILED(hres))
2932             return hres;
2933
2934         hres = equal_values(ctx, &v, rval, ei, ret);
2935
2936         VariantClear(&v);
2937         return hres;
2938     }
2939
2940
2941     *ret = FALSE;
2942     return S_OK;
2943 }
2944
2945 /* ECMA-262 3rd Edition    11.9.1 */
2946 static HRESULT interp_eq(exec_ctx_t *ctx)
2947 {
2948     VARIANT *l, *r;
2949     BOOL b;
2950     HRESULT hres;
2951
2952     r = stack_pop(ctx);
2953     l = stack_pop(ctx);
2954
2955     TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2956
2957     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2958     VariantClear(l);
2959     VariantClear(r);
2960     if(FAILED(hres))
2961         return hres;
2962
2963     return stack_push_bool(ctx, b);
2964 }
2965
2966 /* ECMA-262 3rd Edition    11.9.2 */
2967 static HRESULT interp_neq(exec_ctx_t *ctx)
2968 {
2969     VARIANT *l, *r;
2970     BOOL b;
2971     HRESULT hres;
2972
2973     r = stack_pop(ctx);
2974     l = stack_pop(ctx);
2975
2976     TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2977
2978     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2979     VariantClear(l);
2980     VariantClear(r);
2981     if(FAILED(hres))
2982         return hres;
2983
2984     return stack_push_bool(ctx, !b);
2985 }
2986
2987 /* ECMA-262 3rd Edition    11.9.4 */
2988 static HRESULT interp_eq2(exec_ctx_t *ctx)
2989 {
2990     VARIANT *l, *r;
2991     BOOL b;
2992     HRESULT hres;
2993
2994     TRACE("\n");
2995
2996     r = stack_pop(ctx);
2997     l = stack_pop(ctx);
2998
2999     hres = equal2_values(r, l, &b);
3000     VariantClear(l);
3001     VariantClear(r);
3002     if(FAILED(hres))
3003         return hres;
3004
3005     return stack_push_bool(ctx, b);
3006 }
3007
3008 /* ECMA-262 3rd Edition    11.9.5 */
3009 static HRESULT interp_neq2(exec_ctx_t *ctx)
3010 {
3011     VARIANT *l, *r;
3012     BOOL b;
3013     HRESULT hres;
3014
3015     TRACE("\n");
3016
3017     r = stack_pop(ctx);
3018     l = stack_pop(ctx);
3019
3020     hres = equal2_values(r, l, &b);
3021     VariantClear(l);
3022     VariantClear(r);
3023     if(FAILED(hres))
3024         return hres;
3025
3026     return stack_push_bool(ctx, !b);
3027 }
3028
3029 /* ECMA-262 3rd Edition    11.8.5 */
3030 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
3031 {
3032     VARIANT l, r, ln, rn;
3033     HRESULT hres;
3034
3035     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
3036     if(FAILED(hres))
3037         return hres;
3038
3039     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
3040     if(FAILED(hres)) {
3041         VariantClear(&l);
3042         return hres;
3043     }
3044
3045     if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
3046         *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
3047         SysFreeString(V_BSTR(&l));
3048         SysFreeString(V_BSTR(&r));
3049         return S_OK;
3050     }
3051
3052     hres = to_number(ctx, &l, ei, &ln);
3053     VariantClear(&l);
3054     if(SUCCEEDED(hres))
3055         hres = to_number(ctx, &r, ei, &rn);
3056     VariantClear(&r);
3057     if(FAILED(hres))
3058         return hres;
3059
3060     if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
3061         *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
3062     }else  {
3063         DOUBLE ld = num_val(&ln);
3064         DOUBLE rd = num_val(&rn);
3065
3066         *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
3067     }
3068
3069     return S_OK;
3070 }
3071
3072 /* ECMA-262 3rd Edition    11.8.1 */
3073 static HRESULT interp_lt(exec_ctx_t *ctx)
3074 {
3075     VARIANT *l, *r;
3076     BOOL b;
3077     HRESULT hres;
3078
3079     r = stack_pop(ctx);
3080     l = stack_pop(ctx);
3081
3082     TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
3083
3084     hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
3085     VariantClear(l);
3086     VariantClear(r);
3087     if(FAILED(hres))
3088         return hres;
3089
3090     return stack_push_bool(ctx, b);
3091 }
3092
3093 /* ECMA-262 3rd Edition    11.8.1 */
3094 static HRESULT interp_lteq(exec_ctx_t *ctx)
3095 {
3096     VARIANT *l, *r;
3097     BOOL b;
3098     HRESULT hres;
3099
3100     r = stack_pop(ctx);
3101     l = stack_pop(ctx);
3102
3103     TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
3104
3105     hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
3106     VariantClear(l);
3107     VariantClear(r);
3108     if(FAILED(hres))
3109         return hres;
3110
3111     return stack_push_bool(ctx, b);
3112 }
3113
3114 /* ECMA-262 3rd Edition    11.8.2 */
3115 static HRESULT interp_gt(exec_ctx_t *ctx)
3116 {
3117     VARIANT *l, *r;
3118     BOOL b;
3119     HRESULT hres;
3120
3121     r = stack_pop(ctx);
3122     l = stack_pop(ctx);
3123
3124     TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
3125
3126     hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
3127     VariantClear(l);
3128     VariantClear(r);
3129     if(FAILED(hres))
3130         return hres;
3131
3132     return stack_push_bool(ctx, b);
3133 }
3134
3135 /* ECMA-262 3rd Edition    11.8.4 */
3136 static HRESULT interp_gteq(exec_ctx_t *ctx)
3137 {
3138     VARIANT *l, *r;
3139     BOOL b;
3140     HRESULT hres;
3141
3142     r = stack_pop(ctx);
3143     l = stack_pop(ctx);
3144
3145     TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
3146
3147     hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
3148     VariantClear(l);
3149     VariantClear(r);
3150     if(FAILED(hres))
3151         return hres;
3152
3153     return stack_push_bool(ctx, b);
3154 }
3155
3156 /* ECMA-262 3rd Edition    11.4.8 */
3157 static HRESULT interp_bneg(exec_ctx_t *ctx)
3158 {
3159     VARIANT *v, r;
3160     INT i;
3161     HRESULT hres;
3162
3163     TRACE("\n");
3164
3165     v = stack_pop(ctx);
3166     hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3167     VariantClear(v);
3168     if(FAILED(hres))
3169         return hres;
3170
3171     V_VT(&r) = VT_I4;
3172     V_I4(&r) = ~i;
3173     return stack_push(ctx, &r);
3174 }
3175
3176 /* ECMA-262 3rd Edition    11.4.9 */
3177 static HRESULT interp_neg(exec_ctx_t *ctx)
3178 {
3179     VARIANT *v;
3180     VARIANT_BOOL b;
3181     HRESULT hres;
3182
3183     TRACE("\n");
3184
3185     v = stack_pop(ctx);
3186     hres = to_boolean(v, &b);
3187     VariantClear(v);
3188     if(FAILED(hres))
3189         return hres;
3190
3191     return stack_push_bool(ctx, !b);
3192 }
3193
3194 /* ECMA-262 3rd Edition    11.7.1 */
3195 static HRESULT lshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3196 {
3197     DWORD ri;
3198     INT li;
3199     HRESULT hres;
3200
3201     hres = to_int32(ctx, lval, ei, &li);
3202     if(FAILED(hres))
3203         return hres;
3204
3205     hres = to_uint32(ctx, rval, ei, &ri);
3206     if(FAILED(hres))
3207         return hres;
3208
3209     V_VT(retv) = VT_I4;
3210     V_I4(retv) = li << (ri&0x1f);
3211     return S_OK;
3212 }
3213
3214 /* ECMA-262 3rd Edition    11.7.1 */
3215 HRESULT left_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3216 {
3217     binary_expression_t *expr = (binary_expression_t*)_expr;
3218
3219     TRACE("\n");
3220
3221     return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3222 }
3223
3224 /* ECMA-262 3rd Edition    11.7.2 */
3225 static HRESULT rshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3226 {
3227     DWORD ri;
3228     INT li;
3229     HRESULT hres;
3230
3231     hres = to_int32(ctx, lval, ei, &li);
3232     if(FAILED(hres))
3233         return hres;
3234
3235     hres = to_uint32(ctx, rval, ei, &ri);
3236     if(FAILED(hres))
3237         return hres;
3238
3239     V_VT(retv) = VT_I4;
3240     V_I4(retv) = li >> (ri&0x1f);
3241     return S_OK;
3242 }
3243
3244 /* ECMA-262 3rd Edition    11.7.2 */
3245 HRESULT right_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3246 {
3247     binary_expression_t *expr = (binary_expression_t*)_expr;
3248
3249     TRACE("\n");
3250
3251     return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3252 }
3253
3254 /* ECMA-262 3rd Edition    11.7.3 */
3255 static HRESULT rshift2_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3256 {
3257     DWORD li, ri;
3258     HRESULT hres;
3259
3260     hres = to_uint32(ctx, lval, ei, &li);
3261     if(FAILED(hres))
3262         return hres;
3263
3264     hres = to_uint32(ctx, rval, ei, &ri);
3265     if(FAILED(hres))
3266         return hres;
3267
3268     V_VT(retv) = VT_I4;
3269     V_I4(retv) = li >> (ri&0x1f);
3270     return S_OK;
3271 }
3272
3273 /* ECMA-262 3rd Edition    11.7.3 */
3274 HRESULT right2_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3275 {
3276     binary_expression_t *expr = (binary_expression_t*)_expr;
3277
3278     TRACE("\n");
3279
3280     return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3281 }
3282
3283 /* ECMA-262 3rd Edition    11.13.1 */
3284 static HRESULT interp_assign(exec_ctx_t *ctx)
3285 {
3286     IDispatch *disp;
3287     DISPID id;
3288     VARIANT *v;
3289     HRESULT hres;
3290
3291     TRACE("\n");
3292
3293     v = stack_pop(ctx);
3294     disp = stack_pop_objid(ctx, &id);
3295
3296     if(!disp)
3297         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
3298
3299     hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
3300     IDispatch_Release(disp);
3301     if(FAILED(hres)) {
3302         VariantClear(v);
3303         return hres;
3304     }
3305
3306     return stack_push(ctx, v);
3307 }
3308
3309 /* ECMA-262 3rd Edition    11.13.2 */
3310 HRESULT assign_lshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3311 {
3312     binary_expression_t *expr = (binary_expression_t*)_expr;
3313
3314     TRACE("\n");
3315
3316     return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3317 }
3318
3319 /* ECMA-262 3rd Edition    11.13.2 */
3320 HRESULT assign_rshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3321 {
3322     binary_expression_t *expr = (binary_expression_t*)_expr;
3323
3324     TRACE("\n");
3325
3326     return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3327 }
3328
3329 /* ECMA-262 3rd Edition    11.13.2 */
3330 HRESULT assign_rrshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3331 {
3332     binary_expression_t *expr = (binary_expression_t*)_expr;
3333
3334     TRACE("\n");
3335
3336     return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3337 }
3338
3339 /* ECMA-262 3rd Edition    11.13.2 */
3340 HRESULT assign_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3341 {
3342     binary_expression_t *expr = (binary_expression_t*)_expr;
3343
3344     TRACE("\n");
3345
3346     return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3347 }
3348
3349 static HRESULT interp_jmp(exec_ctx_t *ctx)
3350 {
3351     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3352
3353     TRACE("\n");
3354
3355     ctx->ip = arg;
3356     return S_OK;
3357 }
3358
3359 static HRESULT interp_pop(exec_ctx_t *ctx)
3360 {
3361     TRACE("\n");
3362
3363     stack_popn(ctx, 1);
3364     return S_OK;
3365 }
3366
3367 static HRESULT interp_ret(exec_ctx_t *ctx)
3368 {
3369     TRACE("\n");
3370
3371     ctx->ip = -1;
3372     return S_OK;
3373 }
3374
3375 static HRESULT interp_tree(exec_ctx_t *ctx)
3376 {
3377     instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3378     exprval_t val;
3379     VARIANT v;
3380     HRESULT hres;
3381
3382     TRACE("\n");
3383
3384     hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3385     if(FAILED(hres))
3386         return hres;
3387
3388     hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3389     exprval_release(&val);
3390     if(FAILED(hres))
3391         return hres;
3392
3393     return stack_push(ctx, &v);
3394 }
3395
3396 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3397
3398 static const op_func_t op_funcs[] = {
3399 #define X(x,a,b,c) interp_##x,
3400 OP_LIST
3401 #undef X
3402 };
3403
3404 static const unsigned op_move[] = {
3405 #define X(a,x,b,c) x,
3406 OP_LIST
3407 #undef X
3408 };
3409
3410 HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3411 {
3412     exec_ctx_t *exec_ctx = ctx->exec_ctx;
3413     unsigned prev_ip, prev_top;
3414     jsop_t op;
3415     HRESULT hres = S_OK;
3416
3417     TRACE("\n");
3418
3419     prev_top = exec_ctx->top;
3420     prev_ip = exec_ctx->ip;
3421     exec_ctx->ip = expr->instr_off;
3422
3423     while(exec_ctx->ip != -1) {
3424         op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3425         hres = op_funcs[op](exec_ctx);
3426         if(FAILED(hres))
3427             break;
3428         exec_ctx->ip += op_move[op];
3429     }
3430
3431     exec_ctx->ip = prev_ip;
3432
3433     if(FAILED(hres)) {
3434         stack_popn(exec_ctx, exec_ctx->top-prev_top);
3435         *ei = exec_ctx->ei;
3436         memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3437         return hres;
3438     }
3439
3440     assert(exec_ctx->top == prev_top+1);
3441
3442     ret->type = EXPRVAL_VARIANT;
3443     ret->u.var = *stack_pop(exec_ctx);
3444     return S_OK;
3445 }
3446
3447 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3448 {
3449     HRESULT hres;
3450
3451     TRACE("\n");
3452
3453     hres = compile_subscript(ctx->exec_ctx->parser, expr, &expr->instr_off);
3454     if(FAILED(hres))
3455         return hres;
3456
3457     if(expr->eval == compiled_expression_eval)
3458         expr->eval = interp_expression_eval;
3459
3460     return expr->eval(ctx, expr, flags, ei, ret);
3461 }