jscript: Use bytecode for '|=' expression.
[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 xor_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2174 {
2175     INT li, ri;
2176     HRESULT hres;
2177
2178     hres = to_int32(ctx, lval, ei, &li);
2179     if(FAILED(hres))
2180         return hres;
2181
2182     hres = to_int32(ctx, rval, ei, &ri);
2183     if(FAILED(hres))
2184         return hres;
2185
2186     V_VT(retv) = VT_I4;
2187     V_I4(retv) = li^ri;
2188     return S_OK;
2189 }
2190
2191 /* ECMA-262 3rd Edition    11.10 */
2192 static HRESULT interp_xor(exec_ctx_t *ctx)
2193 {
2194     INT l, r;
2195     HRESULT hres;
2196
2197     TRACE("\n");
2198
2199     hres = stack_pop_int(ctx, &r);
2200     if(FAILED(hres))
2201         return hres;
2202
2203     hres = stack_pop_int(ctx, &l);
2204     if(FAILED(hres))
2205         return hres;
2206
2207     return stack_push_int(ctx, l^r);
2208 }
2209
2210 /* ECMA-262 3rd Edition    11.10 */
2211 static HRESULT bitand_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2212 {
2213     INT li, ri;
2214     HRESULT hres;
2215
2216     hres = to_int32(ctx, lval, ei, &li);
2217     if(FAILED(hres))
2218         return hres;
2219
2220     hres = to_int32(ctx, rval, ei, &ri);
2221     if(FAILED(hres))
2222         return hres;
2223
2224     V_VT(retv) = VT_I4;
2225     V_I4(retv) = li&ri;
2226     return S_OK;
2227 }
2228
2229 /* ECMA-262 3rd Edition    11.10 */
2230 HRESULT binary_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2231 {
2232     binary_expression_t *expr = (binary_expression_t*)_expr;
2233
2234     TRACE("\n");
2235
2236     return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
2237 }
2238
2239 /* ECMA-262 3rd Edition    11.8.6 */
2240 static HRESULT instanceof_eval(script_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
2241 {
2242     jsdisp_t *obj, *iter, *tmp = NULL;
2243     VARIANT_BOOL ret = VARIANT_FALSE;
2244     BOOL b;
2245     VARIANT var;
2246     HRESULT hres;
2247
2248     static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2249
2250     if(V_VT(objv) != VT_DISPATCH || !V_DISPATCH(objv))
2251         return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2252
2253     obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2254     if(!obj) {
2255         FIXME("non-jsdisp objects not supported\n");
2256         return E_FAIL;
2257     }
2258
2259     if(is_class(obj, JSCLASS_FUNCTION)) {
2260         hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2261     }else {
2262         hres = throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2263     }
2264     jsdisp_release(obj);
2265     if(FAILED(hres))
2266         return hres;
2267
2268     if(V_VT(&var) == VT_DISPATCH) {
2269         if(V_VT(inst) == VT_DISPATCH)
2270             tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2271         for(iter = tmp; iter; iter = iter->prototype) {
2272             hres = disp_cmp(V_DISPATCH(&var), to_disp(iter), &b);
2273             if(FAILED(hres))
2274                 break;
2275             if(b) {
2276                 ret = VARIANT_TRUE;
2277                 break;
2278             }
2279         }
2280
2281         if(tmp)
2282             jsdisp_release(tmp);
2283     }else {
2284         FIXME("prototype is not an object\n");
2285         hres = E_FAIL;
2286     }
2287
2288     VariantClear(&var);
2289     if(FAILED(hres))
2290         return hres;
2291
2292     V_VT(retv) = VT_BOOL;
2293     V_BOOL(retv) = ret;
2294     return S_OK;
2295 }
2296
2297 /* ECMA-262 3rd Edition    11.8.6 */
2298 HRESULT instanceof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2299 {
2300     binary_expression_t *expr = (binary_expression_t*)_expr;
2301
2302     TRACE("\n");
2303
2304     return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2305 }
2306
2307 /* ECMA-262 3rd Edition    11.8.7 */
2308 static HRESULT interp_in(exec_ctx_t *ctx)
2309 {
2310     VARIANT *obj, *v;
2311     DISPID id = 0;
2312     BOOL ret;
2313     BSTR str;
2314     HRESULT hres;
2315
2316     TRACE("\n");
2317
2318     obj = stack_pop(ctx);
2319     v = stack_pop(ctx);
2320
2321     if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2322         VariantClear(obj);
2323         VariantClear(v);
2324         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2325     }
2326
2327     hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2328     VariantClear(v);
2329     if(FAILED(hres)) {
2330         IDispatch_Release(V_DISPATCH(obj));
2331         return hres;
2332     }
2333
2334     hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2335     IDispatch_Release(V_DISPATCH(obj));
2336     SysFreeString(str);
2337     if(SUCCEEDED(hres))
2338         ret = TRUE;
2339     else if(hres == DISP_E_UNKNOWNNAME)
2340         ret = FALSE;
2341     else
2342         return hres;
2343
2344     return stack_push_bool(ctx, ret);
2345 }
2346
2347 /* ECMA-262 3rd Edition    11.6.1 */
2348 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2349 {
2350     VARIANT r, l;
2351     HRESULT hres;
2352
2353     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2354     if(FAILED(hres))
2355         return hres;
2356
2357     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2358     if(FAILED(hres)) {
2359         VariantClear(&l);
2360         return hres;
2361     }
2362
2363     if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2364         BSTR lstr = NULL, rstr = NULL;
2365
2366         if(V_VT(&l) == VT_BSTR)
2367             lstr = V_BSTR(&l);
2368         else
2369             hres = to_string(ctx, &l, ei, &lstr);
2370
2371         if(SUCCEEDED(hres)) {
2372             if(V_VT(&r) == VT_BSTR)
2373                 rstr = V_BSTR(&r);
2374             else
2375                 hres = to_string(ctx, &r, ei, &rstr);
2376         }
2377
2378         if(SUCCEEDED(hres)) {
2379             int len1, len2;
2380
2381             len1 = SysStringLen(lstr);
2382             len2 = SysStringLen(rstr);
2383
2384             V_VT(retv) = VT_BSTR;
2385             V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2386             memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2387             memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2388         }
2389
2390         if(V_VT(&l) != VT_BSTR)
2391             SysFreeString(lstr);
2392         if(V_VT(&r) != VT_BSTR)
2393             SysFreeString(rstr);
2394     }else {
2395         VARIANT nl, nr;
2396
2397         hres = to_number(ctx, &l, ei, &nl);
2398         if(SUCCEEDED(hres)) {
2399             hres = to_number(ctx, &r, ei, &nr);
2400             if(SUCCEEDED(hres))
2401                 num_set_val(retv, num_val(&nl) + num_val(&nr));
2402         }
2403     }
2404
2405     VariantClear(&r);
2406     VariantClear(&l);
2407     return hres;
2408 }
2409
2410 /* ECMA-262 3rd Edition    11.6.1 */
2411 static HRESULT interp_add(exec_ctx_t *ctx)
2412 {
2413     VARIANT *l, *r, ret;
2414     HRESULT hres;
2415
2416     r = stack_pop(ctx);
2417     l = stack_pop(ctx);
2418
2419     TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2420
2421     hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2422     VariantClear(l);
2423     VariantClear(r);
2424     if(FAILED(hres))
2425         return hres;
2426
2427     return stack_push(ctx, &ret);
2428 }
2429
2430 /* ECMA-262 3rd Edition    11.6.2 */
2431 static HRESULT interp_sub(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.1 */
2450 static HRESULT interp_mul(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.2 */
2469 static HRESULT interp_div(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, num_val(&l)/num_val(&r));
2485 }
2486
2487 /* ECMA-262 3rd Edition    11.5.3 */
2488 static HRESULT interp_mod(exec_ctx_t *ctx)
2489 {
2490     VARIANT l, r;
2491     HRESULT hres;
2492
2493     TRACE("\n");
2494
2495     hres = stack_pop_number(ctx, &r);
2496     if(FAILED(hres))
2497         return hres;
2498
2499     hres = stack_pop_number(ctx, &l);
2500     if(FAILED(hres))
2501         return hres;
2502
2503     return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2504 }
2505
2506 /* ECMA-262 3rd Edition    11.4.2 */
2507 HRESULT delete_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2508 {
2509     unary_expression_t *expr = (unary_expression_t*)_expr;
2510     VARIANT_BOOL b = VARIANT_FALSE;
2511     exprval_t exprval;
2512     HRESULT hres;
2513
2514     TRACE("\n");
2515
2516     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2517     if(FAILED(hres))
2518         return hres;
2519
2520     switch(exprval.type) {
2521     case EXPRVAL_IDREF: {
2522         IDispatchEx *dispex;
2523
2524         hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2525         if(SUCCEEDED(hres)) {
2526             hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2527             b = VARIANT_TRUE;
2528             IDispatchEx_Release(dispex);
2529         }
2530         break;
2531     }
2532     default:
2533         FIXME("unsupported type %d\n", exprval.type);
2534         hres = E_NOTIMPL;
2535     }
2536
2537     exprval_release(&exprval);
2538     if(FAILED(hres))
2539         return hres;
2540
2541     return return_bool(ret, b);
2542 }
2543
2544 /* ECMA-262 3rd Edition    11.4.2 */
2545 static HRESULT interp_delete(exec_ctx_t *ctx)
2546 {
2547     VARIANT *obj_var, *name_var;
2548     IDispatchEx *dispex;
2549     IDispatch *obj;
2550     BSTR name;
2551     BOOL ret;
2552     HRESULT hres;
2553
2554     TRACE("\n");
2555
2556     name_var = stack_pop(ctx);
2557     obj_var = stack_pop(ctx);
2558
2559     hres = to_object(ctx->parser->script, obj_var, &obj);
2560     VariantClear(obj_var);
2561     if(FAILED(hres)) {
2562         VariantClear(name_var);
2563         return hres;
2564     }
2565
2566     hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2567     VariantClear(name_var);
2568     if(FAILED(hres)) {
2569         IDispatch_Release(obj);
2570         return hres;
2571     }
2572
2573     hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2574     if(SUCCEEDED(hres)) {
2575         hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2576         ret = TRUE;
2577         IDispatchEx_Release(dispex);
2578     }else {
2579         hres = S_OK;
2580         ret = FALSE;
2581     }
2582
2583     IDispatch_Release(obj);
2584     SysFreeString(name);
2585     if(FAILED(hres))
2586         return hres;
2587
2588     return stack_push_bool(ctx, ret);
2589 }
2590
2591 /* ECMA-262 3rd Edition    11.4.2 */
2592 static HRESULT interp_void(exec_ctx_t *ctx)
2593 {
2594     VARIANT v;
2595
2596     TRACE("\n");
2597
2598     stack_popn(ctx, 1);
2599
2600     V_VT(&v) = VT_EMPTY;
2601     return stack_push(ctx, &v);
2602 }
2603
2604 /* ECMA-262 3rd Edition    11.4.3 */
2605 static HRESULT typeof_exprval(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2606 {
2607     VARIANT val;
2608     HRESULT hres;
2609
2610     static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2611     static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2612     static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2613     static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2614     static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2615     static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2616     static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
2617
2618     if(exprval->type == EXPRVAL_INVALID) {
2619         *ret = undefinedW;
2620         return S_OK;
2621     }
2622
2623     hres = exprval_to_value(ctx, exprval, ei, &val);
2624     if(FAILED(hres)) {
2625         if(exprval->type == EXPRVAL_IDREF) {
2626             *ret = unknownW;
2627             return S_OK;
2628         }
2629         return hres;
2630     }
2631
2632     switch(V_VT(&val)) {
2633     case VT_EMPTY:
2634         *ret = undefinedW;
2635         break;
2636     case VT_NULL:
2637         *ret = objectW;
2638         break;
2639     case VT_BOOL:
2640         *ret = booleanW;
2641         break;
2642     case VT_I4:
2643     case VT_R8:
2644         *ret = numberW;
2645         break;
2646     case VT_BSTR:
2647         *ret = stringW;
2648         break;
2649     case VT_DISPATCH: {
2650         jsdisp_t *dispex;
2651
2652         if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2653             *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2654             jsdisp_release(dispex);
2655         }else {
2656             *ret = objectW;
2657         }
2658         break;
2659     }
2660     default:
2661         FIXME("unhandled vt %d\n", V_VT(&val));
2662         hres = E_NOTIMPL;
2663     }
2664
2665     VariantClear(&val);
2666     return hres;
2667 }
2668
2669 HRESULT typeof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2670 {
2671     unary_expression_t *expr = (unary_expression_t*)_expr;
2672     const WCHAR *str = NULL;
2673     exprval_t exprval;
2674     HRESULT hres;
2675
2676     TRACE("\n");
2677
2678     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2679     if(FAILED(hres))
2680         return hres;
2681
2682     hres = typeof_exprval(ctx, &exprval, ei, &str);
2683     exprval_release(&exprval);
2684     if(FAILED(hres))
2685         return hres;
2686
2687     ret->type = EXPRVAL_VARIANT;
2688     V_VT(&ret->u.var) = VT_BSTR;
2689     V_BSTR(&ret->u.var) = SysAllocString(str);
2690     if(!V_BSTR(&ret->u.var))
2691         return E_OUTOFMEMORY;
2692
2693     return S_OK;
2694 }
2695
2696 /* ECMA-262 3rd Edition    11.4.7 */
2697 static HRESULT interp_minus(exec_ctx_t *ctx)
2698 {
2699     VARIANT n;
2700     HRESULT hres;
2701
2702     TRACE("\n");
2703
2704     hres = stack_pop_number(ctx, &n);
2705     if(FAILED(hres))
2706         return hres;
2707
2708     return stack_push_number(ctx, -num_val(&n));
2709 }
2710
2711 /* ECMA-262 3rd Edition    11.4.6 */
2712 static HRESULT interp_tonum(exec_ctx_t *ctx)
2713 {
2714     VARIANT *v, num;
2715     HRESULT hres;
2716
2717     TRACE("\n");
2718
2719     v = stack_pop(ctx);
2720     hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2721     VariantClear(v);
2722     if(FAILED(hres))
2723         return hres;
2724
2725     return stack_push(ctx, &num);
2726 }
2727
2728 /* ECMA-262 3rd Edition    11.3.1 */
2729 HRESULT post_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2730 {
2731     unary_expression_t *expr = (unary_expression_t*)_expr;
2732     VARIANT val, num;
2733     exprval_t exprval;
2734     HRESULT hres;
2735
2736     TRACE("\n");
2737
2738     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2739     if(FAILED(hres))
2740         return hres;
2741
2742     hres = exprval_value(ctx, &exprval, ei, &val);
2743     if(SUCCEEDED(hres)) {
2744         hres = to_number(ctx, &val, ei, &num);
2745         VariantClear(&val);
2746     }
2747
2748     if(SUCCEEDED(hres)) {
2749         VARIANT inc;
2750         num_set_val(&inc, num_val(&num)+1.0);
2751         hres = put_value(ctx, &exprval, &inc, ei);
2752     }
2753
2754     exprval_release(&exprval);
2755     if(FAILED(hres))
2756         return hres;
2757
2758     ret->type = EXPRVAL_VARIANT;
2759     ret->u.var = num;
2760     return S_OK;
2761 }
2762
2763 /* ECMA-262 3rd Edition    11.3.2 */
2764 HRESULT post_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2765 {
2766     unary_expression_t *expr = (unary_expression_t*)_expr;
2767     VARIANT val, num;
2768     exprval_t exprval;
2769     HRESULT hres;
2770
2771     TRACE("\n");
2772
2773     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2774     if(FAILED(hres))
2775         return hres;
2776
2777     hres = exprval_value(ctx, &exprval, ei, &val);
2778     if(SUCCEEDED(hres)) {
2779         hres = to_number(ctx, &val, ei, &num);
2780         VariantClear(&val);
2781     }
2782
2783     if(SUCCEEDED(hres)) {
2784         VARIANT dec;
2785         num_set_val(&dec, num_val(&num)-1.0);
2786         hres = put_value(ctx, &exprval, &dec, ei);
2787     }
2788
2789     exprval_release(&exprval);
2790     if(FAILED(hres))
2791         return hres;
2792
2793     ret->type = EXPRVAL_VARIANT;
2794     ret->u.var = num;
2795     return S_OK;
2796 }
2797
2798 /* ECMA-262 3rd Edition    11.4.4 */
2799 HRESULT pre_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2800 {
2801     unary_expression_t *expr = (unary_expression_t*)_expr;
2802     VARIANT val, num;
2803     exprval_t exprval;
2804     HRESULT hres;
2805
2806     TRACE("\n");
2807
2808     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2809     if(FAILED(hres))
2810         return hres;
2811
2812     hres = exprval_value(ctx, &exprval, ei, &val);
2813     if(SUCCEEDED(hres)) {
2814         hres = to_number(ctx, &val, ei, &num);
2815         VariantClear(&val);
2816     }
2817
2818     if(SUCCEEDED(hres)) {
2819         num_set_val(&val, num_val(&num)+1.0);
2820         hres = put_value(ctx, &exprval, &val, ei);
2821     }
2822
2823     exprval_release(&exprval);
2824     if(FAILED(hres))
2825         return hres;
2826
2827     ret->type = EXPRVAL_VARIANT;
2828     ret->u.var = val;
2829     return S_OK;
2830 }
2831
2832 /* ECMA-262 3rd Edition    11.4.5 */
2833 HRESULT pre_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2834 {
2835     unary_expression_t *expr = (unary_expression_t*)_expr;
2836     VARIANT val, num;
2837     exprval_t exprval;
2838     HRESULT hres;
2839
2840     TRACE("\n");
2841
2842     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2843     if(FAILED(hres))
2844         return hres;
2845
2846     hres = exprval_value(ctx, &exprval, ei, &val);
2847     if(SUCCEEDED(hres)) {
2848         hres = to_number(ctx, &val, ei, &num);
2849         VariantClear(&val);
2850     }
2851
2852     if(SUCCEEDED(hres)) {
2853         num_set_val(&val, num_val(&num)-1.0);
2854         hres = put_value(ctx, &exprval, &val, ei);
2855     }
2856
2857     exprval_release(&exprval);
2858     if(FAILED(hres))
2859         return hres;
2860
2861     ret->type = EXPRVAL_VARIANT;
2862     ret->u.var = val;
2863     return S_OK;
2864 }
2865
2866 /* ECMA-262 3rd Edition    11.9.3 */
2867 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2868 {
2869     if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2870        return equal2_values(lval, rval, ret);
2871
2872     /* FIXME: NULL disps should be handled in more general way */
2873     if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2874         VARIANT v;
2875         V_VT(&v) = VT_NULL;
2876         return equal_values(ctx, &v, rval, ei, ret);
2877     }
2878
2879     if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2880         VARIANT v;
2881         V_VT(&v) = VT_NULL;
2882         return equal_values(ctx, lval, &v, ei, ret);
2883     }
2884
2885     if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2886        (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2887         *ret = TRUE;
2888         return S_OK;
2889     }
2890
2891     if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2892         VARIANT v;
2893         HRESULT hres;
2894
2895         hres = to_number(ctx, lval, ei, &v);
2896         if(FAILED(hres))
2897             return hres;
2898
2899         return equal_values(ctx, &v, rval, ei, ret);
2900     }
2901
2902     if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2903         VARIANT v;
2904         HRESULT hres;
2905
2906         hres = to_number(ctx, rval, ei, &v);
2907         if(FAILED(hres))
2908             return hres;
2909
2910         return equal_values(ctx, lval, &v, ei, ret);
2911     }
2912
2913     if(V_VT(rval) == VT_BOOL) {
2914         VARIANT v;
2915
2916         V_VT(&v) = VT_I4;
2917         V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2918         return equal_values(ctx, lval, &v, ei, ret);
2919     }
2920
2921     if(V_VT(lval) == VT_BOOL) {
2922         VARIANT v;
2923
2924         V_VT(&v) = VT_I4;
2925         V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2926         return equal_values(ctx, &v, rval, ei, ret);
2927     }
2928
2929
2930     if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2931         VARIANT v;
2932         HRESULT hres;
2933
2934         hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2935         if(FAILED(hres))
2936             return hres;
2937
2938         hres = equal_values(ctx, lval, &v, ei, ret);
2939
2940         VariantClear(&v);
2941         return hres;
2942     }
2943
2944
2945     if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2946         VARIANT v;
2947         HRESULT hres;
2948
2949         hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2950         if(FAILED(hres))
2951             return hres;
2952
2953         hres = equal_values(ctx, &v, rval, ei, ret);
2954
2955         VariantClear(&v);
2956         return hres;
2957     }
2958
2959
2960     *ret = FALSE;
2961     return S_OK;
2962 }
2963
2964 /* ECMA-262 3rd Edition    11.9.1 */
2965 static HRESULT interp_eq(exec_ctx_t *ctx)
2966 {
2967     VARIANT *l, *r;
2968     BOOL b;
2969     HRESULT hres;
2970
2971     r = stack_pop(ctx);
2972     l = stack_pop(ctx);
2973
2974     TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2975
2976     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2977     VariantClear(l);
2978     VariantClear(r);
2979     if(FAILED(hres))
2980         return hres;
2981
2982     return stack_push_bool(ctx, b);
2983 }
2984
2985 /* ECMA-262 3rd Edition    11.9.2 */
2986 static HRESULT interp_neq(exec_ctx_t *ctx)
2987 {
2988     VARIANT *l, *r;
2989     BOOL b;
2990     HRESULT hres;
2991
2992     r = stack_pop(ctx);
2993     l = stack_pop(ctx);
2994
2995     TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2996
2997     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2998     VariantClear(l);
2999     VariantClear(r);
3000     if(FAILED(hres))
3001         return hres;
3002
3003     return stack_push_bool(ctx, !b);
3004 }
3005
3006 /* ECMA-262 3rd Edition    11.9.4 */
3007 static HRESULT interp_eq2(exec_ctx_t *ctx)
3008 {
3009     VARIANT *l, *r;
3010     BOOL b;
3011     HRESULT hres;
3012
3013     TRACE("\n");
3014
3015     r = stack_pop(ctx);
3016     l = stack_pop(ctx);
3017
3018     hres = equal2_values(r, l, &b);
3019     VariantClear(l);
3020     VariantClear(r);
3021     if(FAILED(hres))
3022         return hres;
3023
3024     return stack_push_bool(ctx, b);
3025 }
3026
3027 /* ECMA-262 3rd Edition    11.9.5 */
3028 static HRESULT interp_neq2(exec_ctx_t *ctx)
3029 {
3030     VARIANT *l, *r;
3031     BOOL b;
3032     HRESULT hres;
3033
3034     TRACE("\n");
3035
3036     r = stack_pop(ctx);
3037     l = stack_pop(ctx);
3038
3039     hres = equal2_values(r, l, &b);
3040     VariantClear(l);
3041     VariantClear(r);
3042     if(FAILED(hres))
3043         return hres;
3044
3045     return stack_push_bool(ctx, !b);
3046 }
3047
3048 /* ECMA-262 3rd Edition    11.8.5 */
3049 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
3050 {
3051     VARIANT l, r, ln, rn;
3052     HRESULT hres;
3053
3054     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
3055     if(FAILED(hres))
3056         return hres;
3057
3058     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
3059     if(FAILED(hres)) {
3060         VariantClear(&l);
3061         return hres;
3062     }
3063
3064     if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
3065         *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
3066         SysFreeString(V_BSTR(&l));
3067         SysFreeString(V_BSTR(&r));
3068         return S_OK;
3069     }
3070
3071     hres = to_number(ctx, &l, ei, &ln);
3072     VariantClear(&l);
3073     if(SUCCEEDED(hres))
3074         hres = to_number(ctx, &r, ei, &rn);
3075     VariantClear(&r);
3076     if(FAILED(hres))
3077         return hres;
3078
3079     if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
3080         *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
3081     }else  {
3082         DOUBLE ld = num_val(&ln);
3083         DOUBLE rd = num_val(&rn);
3084
3085         *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
3086     }
3087
3088     return S_OK;
3089 }
3090
3091 /* ECMA-262 3rd Edition    11.8.1 */
3092 static HRESULT interp_lt(exec_ctx_t *ctx)
3093 {
3094     VARIANT *l, *r;
3095     BOOL b;
3096     HRESULT hres;
3097
3098     r = stack_pop(ctx);
3099     l = stack_pop(ctx);
3100
3101     TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
3102
3103     hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
3104     VariantClear(l);
3105     VariantClear(r);
3106     if(FAILED(hres))
3107         return hres;
3108
3109     return stack_push_bool(ctx, b);
3110 }
3111
3112 /* ECMA-262 3rd Edition    11.8.1 */
3113 static HRESULT interp_lteq(exec_ctx_t *ctx)
3114 {
3115     VARIANT *l, *r;
3116     BOOL b;
3117     HRESULT hres;
3118
3119     r = stack_pop(ctx);
3120     l = stack_pop(ctx);
3121
3122     TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
3123
3124     hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
3125     VariantClear(l);
3126     VariantClear(r);
3127     if(FAILED(hres))
3128         return hres;
3129
3130     return stack_push_bool(ctx, b);
3131 }
3132
3133 /* ECMA-262 3rd Edition    11.8.2 */
3134 static HRESULT interp_gt(exec_ctx_t *ctx)
3135 {
3136     VARIANT *l, *r;
3137     BOOL b;
3138     HRESULT hres;
3139
3140     r = stack_pop(ctx);
3141     l = stack_pop(ctx);
3142
3143     TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
3144
3145     hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
3146     VariantClear(l);
3147     VariantClear(r);
3148     if(FAILED(hres))
3149         return hres;
3150
3151     return stack_push_bool(ctx, b);
3152 }
3153
3154 /* ECMA-262 3rd Edition    11.8.4 */
3155 static HRESULT interp_gteq(exec_ctx_t *ctx)
3156 {
3157     VARIANT *l, *r;
3158     BOOL b;
3159     HRESULT hres;
3160
3161     r = stack_pop(ctx);
3162     l = stack_pop(ctx);
3163
3164     TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
3165
3166     hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
3167     VariantClear(l);
3168     VariantClear(r);
3169     if(FAILED(hres))
3170         return hres;
3171
3172     return stack_push_bool(ctx, b);
3173 }
3174
3175 /* ECMA-262 3rd Edition    11.4.8 */
3176 static HRESULT interp_bneg(exec_ctx_t *ctx)
3177 {
3178     VARIANT *v, r;
3179     INT i;
3180     HRESULT hres;
3181
3182     TRACE("\n");
3183
3184     v = stack_pop(ctx);
3185     hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3186     VariantClear(v);
3187     if(FAILED(hres))
3188         return hres;
3189
3190     V_VT(&r) = VT_I4;
3191     V_I4(&r) = ~i;
3192     return stack_push(ctx, &r);
3193 }
3194
3195 /* ECMA-262 3rd Edition    11.4.9 */
3196 static HRESULT interp_neg(exec_ctx_t *ctx)
3197 {
3198     VARIANT *v;
3199     VARIANT_BOOL b;
3200     HRESULT hres;
3201
3202     TRACE("\n");
3203
3204     v = stack_pop(ctx);
3205     hres = to_boolean(v, &b);
3206     VariantClear(v);
3207     if(FAILED(hres))
3208         return hres;
3209
3210     return stack_push_bool(ctx, !b);
3211 }
3212
3213 /* ECMA-262 3rd Edition    11.7.1 */
3214 static HRESULT lshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3215 {
3216     DWORD ri;
3217     INT li;
3218     HRESULT hres;
3219
3220     hres = to_int32(ctx, lval, ei, &li);
3221     if(FAILED(hres))
3222         return hres;
3223
3224     hres = to_uint32(ctx, rval, ei, &ri);
3225     if(FAILED(hres))
3226         return hres;
3227
3228     V_VT(retv) = VT_I4;
3229     V_I4(retv) = li << (ri&0x1f);
3230     return S_OK;
3231 }
3232
3233 /* ECMA-262 3rd Edition    11.7.1 */
3234 HRESULT left_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3235 {
3236     binary_expression_t *expr = (binary_expression_t*)_expr;
3237
3238     TRACE("\n");
3239
3240     return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3241 }
3242
3243 /* ECMA-262 3rd Edition    11.7.2 */
3244 static HRESULT rshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3245 {
3246     DWORD ri;
3247     INT li;
3248     HRESULT hres;
3249
3250     hres = to_int32(ctx, lval, ei, &li);
3251     if(FAILED(hres))
3252         return hres;
3253
3254     hres = to_uint32(ctx, rval, ei, &ri);
3255     if(FAILED(hres))
3256         return hres;
3257
3258     V_VT(retv) = VT_I4;
3259     V_I4(retv) = li >> (ri&0x1f);
3260     return S_OK;
3261 }
3262
3263 /* ECMA-262 3rd Edition    11.7.2 */
3264 HRESULT right_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3265 {
3266     binary_expression_t *expr = (binary_expression_t*)_expr;
3267
3268     TRACE("\n");
3269
3270     return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3271 }
3272
3273 /* ECMA-262 3rd Edition    11.7.3 */
3274 static HRESULT rshift2_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3275 {
3276     DWORD li, ri;
3277     HRESULT hres;
3278
3279     hres = to_uint32(ctx, lval, ei, &li);
3280     if(FAILED(hres))
3281         return hres;
3282
3283     hres = to_uint32(ctx, rval, ei, &ri);
3284     if(FAILED(hres))
3285         return hres;
3286
3287     V_VT(retv) = VT_I4;
3288     V_I4(retv) = li >> (ri&0x1f);
3289     return S_OK;
3290 }
3291
3292 /* ECMA-262 3rd Edition    11.7.3 */
3293 HRESULT right2_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3294 {
3295     binary_expression_t *expr = (binary_expression_t*)_expr;
3296
3297     TRACE("\n");
3298
3299     return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3300 }
3301
3302 /* ECMA-262 3rd Edition    11.13.1 */
3303 static HRESULT interp_assign(exec_ctx_t *ctx)
3304 {
3305     IDispatch *disp;
3306     DISPID id;
3307     VARIANT *v;
3308     HRESULT hres;
3309
3310     TRACE("\n");
3311
3312     v = stack_pop(ctx);
3313     disp = stack_pop_objid(ctx, &id);
3314
3315     if(!disp)
3316         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
3317
3318     hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
3319     IDispatch_Release(disp);
3320     if(FAILED(hres)) {
3321         VariantClear(v);
3322         return hres;
3323     }
3324
3325     return stack_push(ctx, v);
3326 }
3327
3328 /* ECMA-262 3rd Edition    11.13.2 */
3329 HRESULT assign_lshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3330 {
3331     binary_expression_t *expr = (binary_expression_t*)_expr;
3332
3333     TRACE("\n");
3334
3335     return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3336 }
3337
3338 /* ECMA-262 3rd Edition    11.13.2 */
3339 HRESULT assign_rshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3340 {
3341     binary_expression_t *expr = (binary_expression_t*)_expr;
3342
3343     TRACE("\n");
3344
3345     return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3346 }
3347
3348 /* ECMA-262 3rd Edition    11.13.2 */
3349 HRESULT assign_rrshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3350 {
3351     binary_expression_t *expr = (binary_expression_t*)_expr;
3352
3353     TRACE("\n");
3354
3355     return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3356 }
3357
3358 /* ECMA-262 3rd Edition    11.13.2 */
3359 HRESULT assign_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3360 {
3361     binary_expression_t *expr = (binary_expression_t*)_expr;
3362
3363     TRACE("\n");
3364
3365     return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3366 }
3367
3368 /* ECMA-262 3rd Edition    11.13.2 */
3369 HRESULT assign_xor_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3370 {
3371     binary_expression_t *expr = (binary_expression_t*)_expr;
3372
3373     TRACE("\n");
3374
3375     return assign_oper_eval(ctx, expr->expression1, expr->expression2, xor_eval, ei, ret);
3376 }
3377
3378 static HRESULT interp_jmp(exec_ctx_t *ctx)
3379 {
3380     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3381
3382     TRACE("\n");
3383
3384     ctx->ip = arg;
3385     return S_OK;
3386 }
3387
3388 static HRESULT interp_pop(exec_ctx_t *ctx)
3389 {
3390     TRACE("\n");
3391
3392     stack_popn(ctx, 1);
3393     return S_OK;
3394 }
3395
3396 static HRESULT interp_ret(exec_ctx_t *ctx)
3397 {
3398     TRACE("\n");
3399
3400     ctx->ip = -1;
3401     return S_OK;
3402 }
3403
3404 static HRESULT interp_tree(exec_ctx_t *ctx)
3405 {
3406     instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3407     exprval_t val;
3408     VARIANT v;
3409     HRESULT hres;
3410
3411     TRACE("\n");
3412
3413     hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3414     if(FAILED(hres))
3415         return hres;
3416
3417     hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3418     exprval_release(&val);
3419     if(FAILED(hres))
3420         return hres;
3421
3422     return stack_push(ctx, &v);
3423 }
3424
3425 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3426
3427 static const op_func_t op_funcs[] = {
3428 #define X(x,a,b,c) interp_##x,
3429 OP_LIST
3430 #undef X
3431 };
3432
3433 static const unsigned op_move[] = {
3434 #define X(a,x,b,c) x,
3435 OP_LIST
3436 #undef X
3437 };
3438
3439 HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3440 {
3441     exec_ctx_t *exec_ctx = ctx->exec_ctx;
3442     unsigned prev_ip, prev_top;
3443     jsop_t op;
3444     HRESULT hres = S_OK;
3445
3446     TRACE("\n");
3447
3448     prev_top = exec_ctx->top;
3449     prev_ip = exec_ctx->ip;
3450     exec_ctx->ip = expr->instr_off;
3451
3452     while(exec_ctx->ip != -1) {
3453         op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3454         hres = op_funcs[op](exec_ctx);
3455         if(FAILED(hres))
3456             break;
3457         exec_ctx->ip += op_move[op];
3458     }
3459
3460     exec_ctx->ip = prev_ip;
3461
3462     if(FAILED(hres)) {
3463         stack_popn(exec_ctx, exec_ctx->top-prev_top);
3464         *ei = exec_ctx->ei;
3465         memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3466         return hres;
3467     }
3468
3469     assert(exec_ctx->top == prev_top+1);
3470
3471     ret->type = EXPRVAL_VARIANT;
3472     ret->u.var = *stack_pop(exec_ctx);
3473     return S_OK;
3474 }
3475
3476 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3477 {
3478     HRESULT hres;
3479
3480     TRACE("\n");
3481
3482     hres = compile_subscript(ctx->exec_ctx->parser, expr, &expr->instr_off);
3483     if(FAILED(hres))
3484         return hres;
3485
3486     if(expr->eval == compiled_expression_eval)
3487         expr->eval = interp_expression_eval;
3488
3489     return expr->eval(ctx, expr, flags, ei, ret);
3490 }