jscript: Use bytecode for calls on identifier and member expressions.
[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_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
186 {
187     assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
188
189     *id = V_INT(stack_topn(ctx, n));
190     return V_DISPATCH(stack_topn(ctx, n+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     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1642     VARIANT *objv, *namev;
1643     IDispatch *obj;
1644     BSTR name;
1645     DISPID id;
1646     HRESULT hres;
1647
1648     TRACE("%x\n", arg);
1649
1650     namev = stack_pop(ctx);
1651     objv = stack_pop(ctx);
1652
1653     hres = to_object(ctx->parser->script, objv, &obj);
1654     VariantClear(objv);
1655     if(SUCCEEDED(hres)) {
1656         hres = to_string(ctx->parser->script, namev, &ctx->ei, &name);
1657         if(FAILED(hres))
1658             IDispatch_Release(obj);
1659     }
1660     VariantClear(namev);
1661     if(FAILED(hres))
1662         return hres;
1663
1664     hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
1665     SysFreeString(name);
1666     if(FAILED(hres)) {
1667         IDispatch_Release(obj);
1668         if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1669             obj = NULL;
1670             id = JS_E_INVALID_PROPERTY;
1671         }else {
1672             return hres;
1673         }
1674     }
1675
1676     return stack_push_objid(ctx, obj, id);
1677 }
1678
1679 /* ECMA-262 3rd Edition    11.2.1 */
1680 static HRESULT interp_refval(exec_ctx_t *ctx)
1681 {
1682     IDispatch *disp;
1683     VARIANT v;
1684     DISPID id;
1685     HRESULT hres;
1686
1687     TRACE("\n");
1688
1689     disp = stack_topn_objid(ctx, 0, &id);
1690     if(!disp)
1691         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1692
1693     hres = disp_propget(ctx->parser->script, disp, id, &v, &ctx->ei, NULL/*FIXME*/);
1694     if(FAILED(hres))
1695         return hres;
1696
1697     return stack_push(ctx, &v);
1698 }
1699
1700 static void free_dp(DISPPARAMS *dp)
1701 {
1702     DWORD i;
1703
1704     for(i=0; i < dp->cArgs; i++)
1705         VariantClear(dp->rgvarg+i);
1706     heap_free(dp->rgvarg);
1707 }
1708
1709 static HRESULT args_to_param(script_ctx_t *ctx, argument_t *args, jsexcept_t *ei, DISPPARAMS *dp)
1710 {
1711     VARIANTARG *vargs;
1712     exprval_t exprval;
1713     argument_t *iter;
1714     DWORD cnt = 0, i;
1715     HRESULT hres = S_OK;
1716
1717     memset(dp, 0, sizeof(*dp));
1718     if(!args)
1719         return S_OK;
1720
1721     for(iter = args; iter; iter = iter->next)
1722         cnt++;
1723
1724     vargs = heap_alloc_zero(cnt * sizeof(*vargs));
1725     if(!vargs)
1726         return E_OUTOFMEMORY;
1727
1728     for(i = cnt, iter = args; iter; iter = iter->next) {
1729         hres = expr_eval(ctx, iter->expr, 0, ei, &exprval);
1730         if(FAILED(hres))
1731             break;
1732
1733         hres = exprval_to_value(ctx, &exprval, ei, vargs + (--i));
1734         exprval_release(&exprval);
1735         if(FAILED(hres))
1736             break;
1737     }
1738
1739     if(FAILED(hres)) {
1740         free_dp(dp);
1741         return hres;
1742     }
1743
1744     dp->rgvarg = vargs;
1745     dp->cArgs = cnt;
1746     return S_OK;
1747 }
1748
1749 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1750 {
1751     VARIANT tmp;
1752     unsigned i;
1753
1754     dp->cArgs = arg_cnt;
1755     dp->rgdispidNamedArgs = NULL;
1756     dp->cNamedArgs = 0;
1757
1758     assert(ctx->top >= arg_cnt);
1759
1760     for(i=1; i*2 <= arg_cnt; i++) {
1761         tmp = ctx->stack[ctx->top-i];
1762         ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1763         ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1764     }
1765
1766     dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1767 }
1768
1769 /* ECMA-262 3rd Edition    11.2.2 */
1770 static HRESULT interp_new(exec_ctx_t *ctx)
1771 {
1772     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1773     VARIANT *constr, v;
1774     DISPPARAMS dp;
1775     HRESULT hres;
1776
1777     TRACE("%d\n", arg);
1778
1779     constr = stack_topn(ctx, arg);
1780
1781     /* NOTE: Should use to_object here */
1782
1783     if(V_VT(constr) == VT_NULL)
1784         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1785     else if(V_VT(constr) != VT_DISPATCH)
1786         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_ACTION, NULL);
1787     else if(!V_DISPATCH(constr))
1788         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1789
1790     jsstack_to_dp(ctx, arg, &dp);
1791     hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1792             DISPATCH_CONSTRUCT, &dp, &v, &ctx->ei, NULL/*FIXME*/);
1793     if(FAILED(hres))
1794         return hres;
1795
1796     stack_popn(ctx, arg+1);
1797     return stack_push(ctx, &v);
1798 }
1799
1800 /* ECMA-262 3rd Edition    11.2.3 */
1801 HRESULT call_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1802 {
1803     call_expression_t *expr = (call_expression_t*)_expr;
1804     VARIANT var;
1805     exprval_t exprval;
1806     DISPPARAMS dp;
1807     HRESULT hres;
1808
1809     TRACE("\n");
1810
1811     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
1812     if(FAILED(hres))
1813         return hres;
1814
1815     hres = args_to_param(ctx, expr->argument_list, ei, &dp);
1816     if(SUCCEEDED(hres)) {
1817         switch(exprval.type) {
1818         case EXPRVAL_VARIANT:
1819             if(V_VT(&exprval.u.var) == VT_DISPATCH)
1820                 hres = disp_call(ctx, V_DISPATCH(&exprval.u.var), DISPID_VALUE,
1821                         DISPATCH_METHOD, &dp, flags & EXPR_NOVAL ? NULL : &var, ei, NULL/*FIXME*/);
1822             else
1823                 hres = throw_type_error(ctx, ei, JS_E_INVALID_PROPERTY, NULL);
1824             break;
1825         case EXPRVAL_IDREF:
1826             assert(0);
1827         case EXPRVAL_INVALID:
1828             hres = throw_type_error(ctx, ei, JS_E_OBJECT_EXPECTED, NULL);
1829             break;
1830         default:
1831             FIXME("unimplemented type %d\n", exprval.type);
1832             hres = E_NOTIMPL;
1833         }
1834
1835         free_dp(&dp);
1836     }
1837
1838     exprval_release(&exprval);
1839     if(FAILED(hres))
1840         return hres;
1841
1842     ret->type = EXPRVAL_VARIANT;
1843     if(flags & EXPR_NOVAL) {
1844         V_VT(&ret->u.var) = VT_EMPTY;
1845     }else {
1846         TRACE("= %s\n", debugstr_variant(&var));
1847         ret->u.var = var;
1848     }
1849     return S_OK;
1850 }
1851
1852 /* ECMA-262 3rd Edition    11.2.3 */
1853 static HRESULT interp_call_member(exec_ctx_t *ctx)
1854 {
1855     const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1856     const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1857     IDispatch *obj;
1858     DISPPARAMS dp;
1859     VARIANT v;
1860     DISPID id;
1861     HRESULT hres;
1862
1863     TRACE("%d %d\n", argn, do_ret);
1864
1865     obj = stack_topn_objid(ctx, argn, &id);
1866     if(!obj)
1867         return throw_type_error(ctx->parser->script, &ctx->ei, id, NULL);
1868
1869     jsstack_to_dp(ctx, argn, &dp);
1870     hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, &ctx->ei, NULL/*FIXME*/);
1871     if(FAILED(hres))
1872         return hres;
1873
1874     stack_popn(ctx, argn+2);
1875     return do_ret ? stack_push(ctx, &v) : S_OK;
1876
1877 }
1878
1879 /* ECMA-262 3rd Edition    11.1.1 */
1880 static HRESULT interp_this(exec_ctx_t *ctx)
1881 {
1882     VARIANT v;
1883
1884     TRACE("\n");
1885
1886     V_VT(&v) = VT_DISPATCH;
1887     V_DISPATCH(&v) = ctx->this_obj;
1888     IDispatch_AddRef(ctx->this_obj);
1889     return stack_push(ctx, &v);
1890 }
1891
1892 /* ECMA-262 3rd Edition    10.1.4 */
1893 HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
1894 {
1895     identifier_expression_t *expr = (identifier_expression_t*)_expr;
1896     BSTR identifier;
1897     HRESULT hres;
1898
1899     TRACE("\n");
1900
1901     identifier = SysAllocString(expr->identifier);
1902     if(!identifier)
1903         return E_OUTOFMEMORY;
1904
1905     hres = identifier_eval(ctx, identifier, flags, ei, ret);
1906
1907     SysFreeString(identifier);
1908     return hres;
1909 }
1910
1911 /* ECMA-262 3rd Edition    10.1.4 */
1912 static HRESULT interp_ident(exec_ctx_t *ctx)
1913 {
1914     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1915     exprval_t exprval;
1916     VARIANT v;
1917     HRESULT hres;
1918
1919     TRACE("%s\n", debugstr_w(arg));
1920
1921     hres = identifier_eval(ctx->parser->script, arg, 0, &ctx->ei, &exprval);
1922     if(FAILED(hres))
1923         return hres;
1924
1925     hres = exprval_to_value(ctx->parser->script, &exprval, &ctx->ei, &v);
1926     exprval_release(&exprval);
1927     if(FAILED(hres))
1928         return hres;
1929
1930     return stack_push(ctx, &v);
1931 }
1932
1933 /* ECMA-262 3rd Edition    10.1.4 */
1934 static HRESULT interp_identid(exec_ctx_t *ctx)
1935 {
1936     const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1937     const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1938     exprval_t exprval;
1939     HRESULT hres;
1940
1941     TRACE("%s %x\n", debugstr_w(arg), flags);
1942
1943     hres = identifier_eval(ctx->parser->script, arg, (flags&fdexNameEnsure) ? EXPR_NEWREF : 0, &ctx->ei, &exprval);
1944     if(FAILED(hres))
1945         return hres;
1946
1947     if(exprval.type != EXPRVAL_IDREF) {
1948         WARN("invalid ref\n");
1949         exprval_release(&exprval);
1950         return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1951     }
1952
1953     return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1954 }
1955
1956 /* ECMA-262 3rd Edition    7.8.1 */
1957 static HRESULT interp_null(exec_ctx_t *ctx)
1958 {
1959     VARIANT v;
1960
1961     TRACE("\n");
1962
1963     V_VT(&v) = VT_NULL;
1964     return stack_push(ctx, &v);
1965 }
1966
1967 /* ECMA-262 3rd Edition    7.8.2 */
1968 static HRESULT interp_bool(exec_ctx_t *ctx)
1969 {
1970     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1971
1972     TRACE("%s\n", arg ? "true" : "false");
1973
1974     return stack_push_bool(ctx, arg);
1975 }
1976
1977 /* ECMA-262 3rd Edition    7.8.3 */
1978 static HRESULT interp_int(exec_ctx_t *ctx)
1979 {
1980     const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1981     VARIANT v;
1982
1983     TRACE("%d\n", arg);
1984
1985     V_VT(&v) = VT_I4;
1986     V_I4(&v) = arg;
1987     return stack_push(ctx, &v);
1988 }
1989
1990 /* ECMA-262 3rd Edition    7.8.3 */
1991 static HRESULT interp_double(exec_ctx_t *ctx)
1992 {
1993     const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1994     VARIANT v;
1995
1996     TRACE("%lf\n", arg);
1997
1998     V_VT(&v) = VT_R8;
1999     V_R8(&v) = arg;
2000     return stack_push(ctx, &v);
2001 }
2002
2003 /* ECMA-262 3rd Edition    7.8.4 */
2004 static HRESULT interp_str(exec_ctx_t *ctx)
2005 {
2006     const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
2007     VARIANT v;
2008
2009     TRACE("%s\n", debugstr_w(str));
2010
2011     V_VT(&v) = VT_BSTR;
2012     V_BSTR(&v) = SysAllocString(str);
2013     if(!V_BSTR(&v))
2014         return E_OUTOFMEMORY;
2015
2016     return stack_push(ctx, &v);
2017 }
2018
2019 /* ECMA-262 3rd Edition    7.8 */
2020 static HRESULT interp_regexp(exec_ctx_t *ctx)
2021 {
2022     const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
2023     const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
2024     jsdisp_t *regexp;
2025     VARIANT v;
2026     HRESULT hres;
2027
2028     TRACE("%s %x\n", debugstr_w(source), flags);
2029
2030     hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, &regexp);
2031     if(FAILED(hres))
2032         return hres;
2033
2034     var_set_jsdisp(&v, regexp);
2035     return stack_push(ctx, &v);
2036 }
2037
2038 /* ECMA-262 3rd Edition    11.1.4 */
2039 HRESULT array_literal_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2040 {
2041     array_literal_expression_t *expr = (array_literal_expression_t*)_expr;
2042     DWORD length = 0, i = 0;
2043     array_element_t *elem;
2044     jsdisp_t *array;
2045     exprval_t exprval;
2046     VARIANT val;
2047     HRESULT hres;
2048
2049     TRACE("\n");
2050
2051     for(elem = expr->element_list; elem; elem = elem->next)
2052         length += elem->elision+1;
2053     length += expr->length;
2054
2055     hres = create_array(ctx, length, &array);
2056     if(FAILED(hres))
2057         return hres;
2058
2059     for(elem = expr->element_list; elem; elem = elem->next) {
2060         i += elem->elision;
2061
2062         hres = expr_eval(ctx, elem->expr, 0, ei, &exprval);
2063         if(FAILED(hres))
2064             break;
2065
2066         hres = exprval_to_value(ctx, &exprval, ei, &val);
2067         exprval_release(&exprval);
2068         if(FAILED(hres))
2069             break;
2070
2071         hres = jsdisp_propput_idx(array, i, &val, ei, NULL/*FIXME*/);
2072         VariantClear(&val);
2073         if(FAILED(hres))
2074             break;
2075
2076         i++;
2077     }
2078
2079     if(FAILED(hres)) {
2080         jsdisp_release(array);
2081         return hres;
2082     }
2083
2084     ret->type = EXPRVAL_VARIANT;
2085     var_set_jsdisp(&ret->u.var, array);
2086     return S_OK;
2087 }
2088
2089 /* ECMA-262 3rd Edition    11.1.5 */
2090 HRESULT property_value_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2091 {
2092     property_value_expression_t *expr = (property_value_expression_t*)_expr;
2093     VARIANT val, tmp;
2094     jsdisp_t *obj;
2095     prop_val_t *iter;
2096     exprval_t exprval;
2097     BSTR name;
2098     HRESULT hres;
2099
2100     TRACE("\n");
2101
2102     hres = create_object(ctx, NULL, &obj);
2103     if(FAILED(hres))
2104         return hres;
2105
2106     for(iter = expr->property_list; iter; iter = iter->next) {
2107         hres = literal_to_var(ctx, iter->name, &tmp);
2108         if(FAILED(hres))
2109             break;
2110
2111         hres = to_string(ctx, &tmp, ei, &name);
2112         VariantClear(&tmp);
2113         if(FAILED(hres))
2114             break;
2115
2116         hres = expr_eval(ctx, iter->value, 0, ei, &exprval);
2117         if(SUCCEEDED(hres)) {
2118             hres = exprval_to_value(ctx, &exprval, ei, &val);
2119             exprval_release(&exprval);
2120             if(SUCCEEDED(hres)) {
2121                 hres = jsdisp_propput_name(obj, name, &val, ei, NULL/*FIXME*/);
2122                 VariantClear(&val);
2123             }
2124         }
2125
2126         SysFreeString(name);
2127         if(FAILED(hres))
2128             break;
2129     }
2130
2131     if(FAILED(hres)) {
2132         jsdisp_release(obj);
2133         return hres;
2134     }
2135
2136     ret->type = EXPRVAL_VARIANT;
2137     var_set_jsdisp(&ret->u.var, obj);
2138     return S_OK;
2139 }
2140
2141 /* ECMA-262 3rd Edition    11.11 */
2142 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
2143 {
2144     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2145     VARIANT_BOOL b;
2146     HRESULT hres;
2147
2148     TRACE("\n");
2149
2150     hres = to_boolean(stack_top(ctx), &b);
2151     if(FAILED(hres))
2152         return hres;
2153
2154     if(b) {
2155         ctx->ip = arg;
2156     }else {
2157         stack_popn(ctx, 1);
2158         ctx->ip++;
2159     }
2160     return S_OK;
2161 }
2162
2163 /* ECMA-262 3rd Edition    11.11 */
2164 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2165 {
2166     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2167     VARIANT_BOOL b;
2168     HRESULT hres;
2169
2170     TRACE("\n");
2171
2172     hres = to_boolean(stack_top(ctx), &b);
2173     if(FAILED(hres))
2174         return hres;
2175
2176     if(b) {
2177         stack_popn(ctx, 1);
2178         ctx->ip++;
2179     }else {
2180         ctx->ip = arg;
2181     }
2182     return S_OK;
2183 }
2184
2185 /* ECMA-262 3rd Edition    11.10 */
2186 static HRESULT interp_or(exec_ctx_t *ctx)
2187 {
2188     INT l, r;
2189     HRESULT hres;
2190
2191     TRACE("\n");
2192
2193     hres = stack_pop_int(ctx, &r);
2194     if(FAILED(hres))
2195         return hres;
2196
2197     hres = stack_pop_int(ctx, &l);
2198     if(FAILED(hres))
2199         return hres;
2200
2201     return stack_push_int(ctx, l|r);
2202 }
2203
2204 /* ECMA-262 3rd Edition    11.10 */
2205 static HRESULT interp_xor(exec_ctx_t *ctx)
2206 {
2207     INT l, r;
2208     HRESULT hres;
2209
2210     TRACE("\n");
2211
2212     hres = stack_pop_int(ctx, &r);
2213     if(FAILED(hres))
2214         return hres;
2215
2216     hres = stack_pop_int(ctx, &l);
2217     if(FAILED(hres))
2218         return hres;
2219
2220     return stack_push_int(ctx, l^r);
2221 }
2222
2223 /* ECMA-262 3rd Edition    11.10 */
2224 static HRESULT bitand_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2225 {
2226     INT li, ri;
2227     HRESULT hres;
2228
2229     hres = to_int32(ctx, lval, ei, &li);
2230     if(FAILED(hres))
2231         return hres;
2232
2233     hres = to_int32(ctx, rval, ei, &ri);
2234     if(FAILED(hres))
2235         return hres;
2236
2237     V_VT(retv) = VT_I4;
2238     V_I4(retv) = li&ri;
2239     return S_OK;
2240 }
2241
2242 /* ECMA-262 3rd Edition    11.10 */
2243 HRESULT binary_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2244 {
2245     binary_expression_t *expr = (binary_expression_t*)_expr;
2246
2247     TRACE("\n");
2248
2249     return binary_expr_eval(ctx, expr, bitand_eval, ei, ret);
2250 }
2251
2252 /* ECMA-262 3rd Edition    11.8.6 */
2253 static HRESULT instanceof_eval(script_ctx_t *ctx, VARIANT *inst, VARIANT *objv, jsexcept_t *ei, VARIANT *retv)
2254 {
2255     jsdisp_t *obj, *iter, *tmp = NULL;
2256     VARIANT_BOOL ret = VARIANT_FALSE;
2257     BOOL b;
2258     VARIANT var;
2259     HRESULT hres;
2260
2261     static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
2262
2263     if(V_VT(objv) != VT_DISPATCH || !V_DISPATCH(objv))
2264         return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2265
2266     obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(objv));
2267     if(!obj) {
2268         FIXME("non-jsdisp objects not supported\n");
2269         return E_FAIL;
2270     }
2271
2272     if(is_class(obj, JSCLASS_FUNCTION)) {
2273         hres = jsdisp_propget_name(obj, prototypeW, &var, ei, NULL/*FIXME*/);
2274     }else {
2275         hres = throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
2276     }
2277     jsdisp_release(obj);
2278     if(FAILED(hres))
2279         return hres;
2280
2281     if(V_VT(&var) == VT_DISPATCH) {
2282         if(V_VT(inst) == VT_DISPATCH)
2283             tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(inst));
2284         for(iter = tmp; iter; iter = iter->prototype) {
2285             hres = disp_cmp(V_DISPATCH(&var), to_disp(iter), &b);
2286             if(FAILED(hres))
2287                 break;
2288             if(b) {
2289                 ret = VARIANT_TRUE;
2290                 break;
2291             }
2292         }
2293
2294         if(tmp)
2295             jsdisp_release(tmp);
2296     }else {
2297         FIXME("prototype is not an object\n");
2298         hres = E_FAIL;
2299     }
2300
2301     VariantClear(&var);
2302     if(FAILED(hres))
2303         return hres;
2304
2305     V_VT(retv) = VT_BOOL;
2306     V_BOOL(retv) = ret;
2307     return S_OK;
2308 }
2309
2310 /* ECMA-262 3rd Edition    11.8.6 */
2311 HRESULT instanceof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2312 {
2313     binary_expression_t *expr = (binary_expression_t*)_expr;
2314
2315     TRACE("\n");
2316
2317     return binary_expr_eval(ctx, expr, instanceof_eval, ei, ret);
2318 }
2319
2320 /* ECMA-262 3rd Edition    11.8.7 */
2321 static HRESULT interp_in(exec_ctx_t *ctx)
2322 {
2323     VARIANT *obj, *v;
2324     DISPID id = 0;
2325     BOOL ret;
2326     BSTR str;
2327     HRESULT hres;
2328
2329     TRACE("\n");
2330
2331     obj = stack_pop(ctx);
2332     v = stack_pop(ctx);
2333
2334     if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
2335         VariantClear(obj);
2336         VariantClear(v);
2337         return throw_type_error(ctx->parser->script, &ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2338     }
2339
2340     hres = to_string(ctx->parser->script, v, &ctx->ei, &str);
2341     VariantClear(v);
2342     if(FAILED(hres)) {
2343         IDispatch_Release(V_DISPATCH(obj));
2344         return hres;
2345     }
2346
2347     hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
2348     IDispatch_Release(V_DISPATCH(obj));
2349     SysFreeString(str);
2350     if(SUCCEEDED(hres))
2351         ret = TRUE;
2352     else if(hres == DISP_E_UNKNOWNNAME)
2353         ret = FALSE;
2354     else
2355         return hres;
2356
2357     return stack_push_bool(ctx, ret);
2358 }
2359
2360 /* ECMA-262 3rd Edition    11.6.1 */
2361 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
2362 {
2363     VARIANT r, l;
2364     HRESULT hres;
2365
2366     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2367     if(FAILED(hres))
2368         return hres;
2369
2370     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2371     if(FAILED(hres)) {
2372         VariantClear(&l);
2373         return hres;
2374     }
2375
2376     if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
2377         BSTR lstr = NULL, rstr = NULL;
2378
2379         if(V_VT(&l) == VT_BSTR)
2380             lstr = V_BSTR(&l);
2381         else
2382             hres = to_string(ctx, &l, ei, &lstr);
2383
2384         if(SUCCEEDED(hres)) {
2385             if(V_VT(&r) == VT_BSTR)
2386                 rstr = V_BSTR(&r);
2387             else
2388                 hres = to_string(ctx, &r, ei, &rstr);
2389         }
2390
2391         if(SUCCEEDED(hres)) {
2392             int len1, len2;
2393
2394             len1 = SysStringLen(lstr);
2395             len2 = SysStringLen(rstr);
2396
2397             V_VT(retv) = VT_BSTR;
2398             V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2399             memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2400             memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2401         }
2402
2403         if(V_VT(&l) != VT_BSTR)
2404             SysFreeString(lstr);
2405         if(V_VT(&r) != VT_BSTR)
2406             SysFreeString(rstr);
2407     }else {
2408         VARIANT nl, nr;
2409
2410         hres = to_number(ctx, &l, ei, &nl);
2411         if(SUCCEEDED(hres)) {
2412             hres = to_number(ctx, &r, ei, &nr);
2413             if(SUCCEEDED(hres))
2414                 num_set_val(retv, num_val(&nl) + num_val(&nr));
2415         }
2416     }
2417
2418     VariantClear(&r);
2419     VariantClear(&l);
2420     return hres;
2421 }
2422
2423 /* ECMA-262 3rd Edition    11.6.1 */
2424 static HRESULT interp_add(exec_ctx_t *ctx)
2425 {
2426     VARIANT *l, *r, ret;
2427     HRESULT hres;
2428
2429     r = stack_pop(ctx);
2430     l = stack_pop(ctx);
2431
2432     TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2433
2434     hres = add_eval(ctx->parser->script, l, r, &ctx->ei, &ret);
2435     VariantClear(l);
2436     VariantClear(r);
2437     if(FAILED(hres))
2438         return hres;
2439
2440     return stack_push(ctx, &ret);
2441 }
2442
2443 /* ECMA-262 3rd Edition    11.6.2 */
2444 static HRESULT interp_sub(exec_ctx_t *ctx)
2445 {
2446     VARIANT l, r;
2447     HRESULT hres;
2448
2449     TRACE("\n");
2450
2451     hres = stack_pop_number(ctx, &r);
2452     if(FAILED(hres))
2453         return hres;
2454
2455     hres = stack_pop_number(ctx, &l);
2456     if(FAILED(hres))
2457         return hres;
2458
2459     return stack_push_number(ctx, num_val(&l)-num_val(&r));
2460 }
2461
2462 /* ECMA-262 3rd Edition    11.5.1 */
2463 static HRESULT interp_mul(exec_ctx_t *ctx)
2464 {
2465     VARIANT l, r;
2466     HRESULT hres;
2467
2468     TRACE("\n");
2469
2470     hres = stack_pop_number(ctx, &r);
2471     if(FAILED(hres))
2472         return hres;
2473
2474     hres = stack_pop_number(ctx, &l);
2475     if(FAILED(hres))
2476         return hres;
2477
2478     return stack_push_number(ctx, num_val(&l)*num_val(&r));
2479 }
2480
2481 /* ECMA-262 3rd Edition    11.5.2 */
2482 static HRESULT interp_div(exec_ctx_t *ctx)
2483 {
2484     VARIANT l, r;
2485     HRESULT hres;
2486
2487     TRACE("\n");
2488
2489     hres = stack_pop_number(ctx, &r);
2490     if(FAILED(hres))
2491         return hres;
2492
2493     hres = stack_pop_number(ctx, &l);
2494     if(FAILED(hres))
2495         return hres;
2496
2497     return stack_push_number(ctx, num_val(&l)/num_val(&r));
2498 }
2499
2500 /* ECMA-262 3rd Edition    11.5.3 */
2501 static HRESULT interp_mod(exec_ctx_t *ctx)
2502 {
2503     VARIANT l, r;
2504     HRESULT hres;
2505
2506     TRACE("\n");
2507
2508     hres = stack_pop_number(ctx, &r);
2509     if(FAILED(hres))
2510         return hres;
2511
2512     hres = stack_pop_number(ctx, &l);
2513     if(FAILED(hres))
2514         return hres;
2515
2516     return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2517 }
2518
2519 /* ECMA-262 3rd Edition    11.4.2 */
2520 HRESULT delete_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2521 {
2522     unary_expression_t *expr = (unary_expression_t*)_expr;
2523     VARIANT_BOOL b = VARIANT_FALSE;
2524     exprval_t exprval;
2525     HRESULT hres;
2526
2527     TRACE("\n");
2528
2529     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2530     if(FAILED(hres))
2531         return hres;
2532
2533     switch(exprval.type) {
2534     case EXPRVAL_IDREF: {
2535         IDispatchEx *dispex;
2536
2537         hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2538         if(SUCCEEDED(hres)) {
2539             hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2540             b = VARIANT_TRUE;
2541             IDispatchEx_Release(dispex);
2542         }
2543         break;
2544     }
2545     default:
2546         FIXME("unsupported type %d\n", exprval.type);
2547         hres = E_NOTIMPL;
2548     }
2549
2550     exprval_release(&exprval);
2551     if(FAILED(hres))
2552         return hres;
2553
2554     return return_bool(ret, b);
2555 }
2556
2557 /* ECMA-262 3rd Edition    11.4.2 */
2558 static HRESULT interp_delete(exec_ctx_t *ctx)
2559 {
2560     VARIANT *obj_var, *name_var;
2561     IDispatchEx *dispex;
2562     IDispatch *obj;
2563     BSTR name;
2564     BOOL ret;
2565     HRESULT hres;
2566
2567     TRACE("\n");
2568
2569     name_var = stack_pop(ctx);
2570     obj_var = stack_pop(ctx);
2571
2572     hres = to_object(ctx->parser->script, obj_var, &obj);
2573     VariantClear(obj_var);
2574     if(FAILED(hres)) {
2575         VariantClear(name_var);
2576         return hres;
2577     }
2578
2579     hres = to_string(ctx->parser->script, name_var, &ctx->ei, &name);
2580     VariantClear(name_var);
2581     if(FAILED(hres)) {
2582         IDispatch_Release(obj);
2583         return hres;
2584     }
2585
2586     hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2587     if(SUCCEEDED(hres)) {
2588         hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2589         ret = TRUE;
2590         IDispatchEx_Release(dispex);
2591     }else {
2592         hres = S_OK;
2593         ret = FALSE;
2594     }
2595
2596     IDispatch_Release(obj);
2597     SysFreeString(name);
2598     if(FAILED(hres))
2599         return hres;
2600
2601     return stack_push_bool(ctx, ret);
2602 }
2603
2604 /* ECMA-262 3rd Edition    11.4.2 */
2605 static HRESULT interp_void(exec_ctx_t *ctx)
2606 {
2607     VARIANT v;
2608
2609     TRACE("\n");
2610
2611     stack_popn(ctx, 1);
2612
2613     V_VT(&v) = VT_EMPTY;
2614     return stack_push(ctx, &v);
2615 }
2616
2617 /* ECMA-262 3rd Edition    11.4.3 */
2618 static HRESULT typeof_exprval(script_ctx_t *ctx, exprval_t *exprval, jsexcept_t *ei, const WCHAR **ret)
2619 {
2620     VARIANT val;
2621     HRESULT hres;
2622
2623     static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
2624     static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
2625     static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
2626     static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
2627     static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
2628     static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2629     static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
2630
2631     if(exprval->type == EXPRVAL_INVALID) {
2632         *ret = undefinedW;
2633         return S_OK;
2634     }
2635
2636     hres = exprval_to_value(ctx, exprval, ei, &val);
2637     if(FAILED(hres)) {
2638         if(exprval->type == EXPRVAL_IDREF) {
2639             *ret = unknownW;
2640             return S_OK;
2641         }
2642         return hres;
2643     }
2644
2645     switch(V_VT(&val)) {
2646     case VT_EMPTY:
2647         *ret = undefinedW;
2648         break;
2649     case VT_NULL:
2650         *ret = objectW;
2651         break;
2652     case VT_BOOL:
2653         *ret = booleanW;
2654         break;
2655     case VT_I4:
2656     case VT_R8:
2657         *ret = numberW;
2658         break;
2659     case VT_BSTR:
2660         *ret = stringW;
2661         break;
2662     case VT_DISPATCH: {
2663         jsdisp_t *dispex;
2664
2665         if(V_DISPATCH(&val) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(&val)))) {
2666             *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2667             jsdisp_release(dispex);
2668         }else {
2669             *ret = objectW;
2670         }
2671         break;
2672     }
2673     default:
2674         FIXME("unhandled vt %d\n", V_VT(&val));
2675         hres = E_NOTIMPL;
2676     }
2677
2678     VariantClear(&val);
2679     return hres;
2680 }
2681
2682 HRESULT typeof_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2683 {
2684     unary_expression_t *expr = (unary_expression_t*)_expr;
2685     const WCHAR *str = NULL;
2686     exprval_t exprval;
2687     HRESULT hres;
2688
2689     TRACE("\n");
2690
2691     hres = expr_eval(ctx, expr->expression, 0, ei, &exprval);
2692     if(FAILED(hres))
2693         return hres;
2694
2695     hres = typeof_exprval(ctx, &exprval, ei, &str);
2696     exprval_release(&exprval);
2697     if(FAILED(hres))
2698         return hres;
2699
2700     ret->type = EXPRVAL_VARIANT;
2701     V_VT(&ret->u.var) = VT_BSTR;
2702     V_BSTR(&ret->u.var) = SysAllocString(str);
2703     if(!V_BSTR(&ret->u.var))
2704         return E_OUTOFMEMORY;
2705
2706     return S_OK;
2707 }
2708
2709 /* ECMA-262 3rd Edition    11.4.7 */
2710 static HRESULT interp_minus(exec_ctx_t *ctx)
2711 {
2712     VARIANT n;
2713     HRESULT hres;
2714
2715     TRACE("\n");
2716
2717     hres = stack_pop_number(ctx, &n);
2718     if(FAILED(hres))
2719         return hres;
2720
2721     return stack_push_number(ctx, -num_val(&n));
2722 }
2723
2724 /* ECMA-262 3rd Edition    11.4.6 */
2725 static HRESULT interp_tonum(exec_ctx_t *ctx)
2726 {
2727     VARIANT *v, num;
2728     HRESULT hres;
2729
2730     TRACE("\n");
2731
2732     v = stack_pop(ctx);
2733     hres = to_number(ctx->parser->script, v, &ctx->ei, &num);
2734     VariantClear(v);
2735     if(FAILED(hres))
2736         return hres;
2737
2738     return stack_push(ctx, &num);
2739 }
2740
2741 /* ECMA-262 3rd Edition    11.3.1 */
2742 HRESULT post_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2743 {
2744     unary_expression_t *expr = (unary_expression_t*)_expr;
2745     VARIANT val, num;
2746     exprval_t exprval;
2747     HRESULT hres;
2748
2749     TRACE("\n");
2750
2751     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2752     if(FAILED(hres))
2753         return hres;
2754
2755     hres = exprval_value(ctx, &exprval, ei, &val);
2756     if(SUCCEEDED(hres)) {
2757         hres = to_number(ctx, &val, ei, &num);
2758         VariantClear(&val);
2759     }
2760
2761     if(SUCCEEDED(hres)) {
2762         VARIANT inc;
2763         num_set_val(&inc, num_val(&num)+1.0);
2764         hres = put_value(ctx, &exprval, &inc, ei);
2765     }
2766
2767     exprval_release(&exprval);
2768     if(FAILED(hres))
2769         return hres;
2770
2771     ret->type = EXPRVAL_VARIANT;
2772     ret->u.var = num;
2773     return S_OK;
2774 }
2775
2776 /* ECMA-262 3rd Edition    11.3.2 */
2777 HRESULT post_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2778 {
2779     unary_expression_t *expr = (unary_expression_t*)_expr;
2780     VARIANT val, num;
2781     exprval_t exprval;
2782     HRESULT hres;
2783
2784     TRACE("\n");
2785
2786     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2787     if(FAILED(hres))
2788         return hres;
2789
2790     hres = exprval_value(ctx, &exprval, ei, &val);
2791     if(SUCCEEDED(hres)) {
2792         hres = to_number(ctx, &val, ei, &num);
2793         VariantClear(&val);
2794     }
2795
2796     if(SUCCEEDED(hres)) {
2797         VARIANT dec;
2798         num_set_val(&dec, num_val(&num)-1.0);
2799         hres = put_value(ctx, &exprval, &dec, ei);
2800     }
2801
2802     exprval_release(&exprval);
2803     if(FAILED(hres))
2804         return hres;
2805
2806     ret->type = EXPRVAL_VARIANT;
2807     ret->u.var = num;
2808     return S_OK;
2809 }
2810
2811 /* ECMA-262 3rd Edition    11.4.4 */
2812 HRESULT pre_increment_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2813 {
2814     unary_expression_t *expr = (unary_expression_t*)_expr;
2815     VARIANT val, num;
2816     exprval_t exprval;
2817     HRESULT hres;
2818
2819     TRACE("\n");
2820
2821     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2822     if(FAILED(hres))
2823         return hres;
2824
2825     hres = exprval_value(ctx, &exprval, ei, &val);
2826     if(SUCCEEDED(hres)) {
2827         hres = to_number(ctx, &val, ei, &num);
2828         VariantClear(&val);
2829     }
2830
2831     if(SUCCEEDED(hres)) {
2832         num_set_val(&val, num_val(&num)+1.0);
2833         hres = put_value(ctx, &exprval, &val, ei);
2834     }
2835
2836     exprval_release(&exprval);
2837     if(FAILED(hres))
2838         return hres;
2839
2840     ret->type = EXPRVAL_VARIANT;
2841     ret->u.var = val;
2842     return S_OK;
2843 }
2844
2845 /* ECMA-262 3rd Edition    11.4.5 */
2846 HRESULT pre_decrement_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
2847 {
2848     unary_expression_t *expr = (unary_expression_t*)_expr;
2849     VARIANT val, num;
2850     exprval_t exprval;
2851     HRESULT hres;
2852
2853     TRACE("\n");
2854
2855     hres = expr_eval(ctx, expr->expression, EXPR_NEWREF, ei, &exprval);
2856     if(FAILED(hres))
2857         return hres;
2858
2859     hres = exprval_value(ctx, &exprval, ei, &val);
2860     if(SUCCEEDED(hres)) {
2861         hres = to_number(ctx, &val, ei, &num);
2862         VariantClear(&val);
2863     }
2864
2865     if(SUCCEEDED(hres)) {
2866         num_set_val(&val, num_val(&num)-1.0);
2867         hres = put_value(ctx, &exprval, &val, ei);
2868     }
2869
2870     exprval_release(&exprval);
2871     if(FAILED(hres))
2872         return hres;
2873
2874     ret->type = EXPRVAL_VARIANT;
2875     ret->u.var = val;
2876     return S_OK;
2877 }
2878
2879 /* ECMA-262 3rd Edition    11.9.3 */
2880 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2881 {
2882     if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2883        return equal2_values(lval, rval, ret);
2884
2885     /* FIXME: NULL disps should be handled in more general way */
2886     if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2887         VARIANT v;
2888         V_VT(&v) = VT_NULL;
2889         return equal_values(ctx, &v, rval, ei, ret);
2890     }
2891
2892     if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2893         VARIANT v;
2894         V_VT(&v) = VT_NULL;
2895         return equal_values(ctx, lval, &v, ei, ret);
2896     }
2897
2898     if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2899        (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2900         *ret = TRUE;
2901         return S_OK;
2902     }
2903
2904     if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2905         VARIANT v;
2906         HRESULT hres;
2907
2908         hres = to_number(ctx, lval, ei, &v);
2909         if(FAILED(hres))
2910             return hres;
2911
2912         return equal_values(ctx, &v, rval, ei, ret);
2913     }
2914
2915     if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2916         VARIANT v;
2917         HRESULT hres;
2918
2919         hres = to_number(ctx, rval, ei, &v);
2920         if(FAILED(hres))
2921             return hres;
2922
2923         return equal_values(ctx, lval, &v, ei, ret);
2924     }
2925
2926     if(V_VT(rval) == VT_BOOL) {
2927         VARIANT v;
2928
2929         V_VT(&v) = VT_I4;
2930         V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2931         return equal_values(ctx, lval, &v, ei, ret);
2932     }
2933
2934     if(V_VT(lval) == VT_BOOL) {
2935         VARIANT v;
2936
2937         V_VT(&v) = VT_I4;
2938         V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2939         return equal_values(ctx, &v, rval, ei, ret);
2940     }
2941
2942
2943     if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2944         VARIANT v;
2945         HRESULT hres;
2946
2947         hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2948         if(FAILED(hres))
2949             return hres;
2950
2951         hres = equal_values(ctx, lval, &v, ei, ret);
2952
2953         VariantClear(&v);
2954         return hres;
2955     }
2956
2957
2958     if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2959         VARIANT v;
2960         HRESULT hres;
2961
2962         hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2963         if(FAILED(hres))
2964             return hres;
2965
2966         hres = equal_values(ctx, &v, rval, ei, ret);
2967
2968         VariantClear(&v);
2969         return hres;
2970     }
2971
2972
2973     *ret = FALSE;
2974     return S_OK;
2975 }
2976
2977 /* ECMA-262 3rd Edition    11.9.1 */
2978 static HRESULT interp_eq(exec_ctx_t *ctx)
2979 {
2980     VARIANT *l, *r;
2981     BOOL b;
2982     HRESULT hres;
2983
2984     r = stack_pop(ctx);
2985     l = stack_pop(ctx);
2986
2987     TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2988
2989     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
2990     VariantClear(l);
2991     VariantClear(r);
2992     if(FAILED(hres))
2993         return hres;
2994
2995     return stack_push_bool(ctx, b);
2996 }
2997
2998 /* ECMA-262 3rd Edition    11.9.2 */
2999 static HRESULT interp_neq(exec_ctx_t *ctx)
3000 {
3001     VARIANT *l, *r;
3002     BOOL b;
3003     HRESULT hres;
3004
3005     r = stack_pop(ctx);
3006     l = stack_pop(ctx);
3007
3008     TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
3009
3010     hres = equal_values(ctx->parser->script, l, r, &ctx->ei, &b);
3011     VariantClear(l);
3012     VariantClear(r);
3013     if(FAILED(hres))
3014         return hres;
3015
3016     return stack_push_bool(ctx, !b);
3017 }
3018
3019 /* ECMA-262 3rd Edition    11.9.4 */
3020 static HRESULT interp_eq2(exec_ctx_t *ctx)
3021 {
3022     VARIANT *l, *r;
3023     BOOL b;
3024     HRESULT hres;
3025
3026     TRACE("\n");
3027
3028     r = stack_pop(ctx);
3029     l = stack_pop(ctx);
3030
3031     hres = equal2_values(r, l, &b);
3032     VariantClear(l);
3033     VariantClear(r);
3034     if(FAILED(hres))
3035         return hres;
3036
3037     return stack_push_bool(ctx, b);
3038 }
3039
3040 /* ECMA-262 3rd Edition    11.9.5 */
3041 static HRESULT interp_neq2(exec_ctx_t *ctx)
3042 {
3043     VARIANT *l, *r;
3044     BOOL b;
3045     HRESULT hres;
3046
3047     TRACE("\n");
3048
3049     r = stack_pop(ctx);
3050     l = stack_pop(ctx);
3051
3052     hres = equal2_values(r, l, &b);
3053     VariantClear(l);
3054     VariantClear(r);
3055     if(FAILED(hres))
3056         return hres;
3057
3058     return stack_push_bool(ctx, !b);
3059 }
3060
3061 /* ECMA-262 3rd Edition    11.8.5 */
3062 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
3063 {
3064     VARIANT l, r, ln, rn;
3065     HRESULT hres;
3066
3067     hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
3068     if(FAILED(hres))
3069         return hres;
3070
3071     hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
3072     if(FAILED(hres)) {
3073         VariantClear(&l);
3074         return hres;
3075     }
3076
3077     if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
3078         *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
3079         SysFreeString(V_BSTR(&l));
3080         SysFreeString(V_BSTR(&r));
3081         return S_OK;
3082     }
3083
3084     hres = to_number(ctx, &l, ei, &ln);
3085     VariantClear(&l);
3086     if(SUCCEEDED(hres))
3087         hres = to_number(ctx, &r, ei, &rn);
3088     VariantClear(&r);
3089     if(FAILED(hres))
3090         return hres;
3091
3092     if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
3093         *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
3094     }else  {
3095         DOUBLE ld = num_val(&ln);
3096         DOUBLE rd = num_val(&rn);
3097
3098         *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
3099     }
3100
3101     return S_OK;
3102 }
3103
3104 /* ECMA-262 3rd Edition    11.8.1 */
3105 static HRESULT interp_lt(exec_ctx_t *ctx)
3106 {
3107     VARIANT *l, *r;
3108     BOOL b;
3109     HRESULT hres;
3110
3111     r = stack_pop(ctx);
3112     l = stack_pop(ctx);
3113
3114     TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
3115
3116     hres = less_eval(ctx->parser->script, l, r, FALSE, &ctx->ei, &b);
3117     VariantClear(l);
3118     VariantClear(r);
3119     if(FAILED(hres))
3120         return hres;
3121
3122     return stack_push_bool(ctx, b);
3123 }
3124
3125 /* ECMA-262 3rd Edition    11.8.1 */
3126 static HRESULT interp_lteq(exec_ctx_t *ctx)
3127 {
3128     VARIANT *l, *r;
3129     BOOL b;
3130     HRESULT hres;
3131
3132     r = stack_pop(ctx);
3133     l = stack_pop(ctx);
3134
3135     TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
3136
3137     hres = less_eval(ctx->parser->script, r, l, TRUE, &ctx->ei, &b);
3138     VariantClear(l);
3139     VariantClear(r);
3140     if(FAILED(hres))
3141         return hres;
3142
3143     return stack_push_bool(ctx, b);
3144 }
3145
3146 /* ECMA-262 3rd Edition    11.8.2 */
3147 static HRESULT interp_gt(exec_ctx_t *ctx)
3148 {
3149     VARIANT *l, *r;
3150     BOOL b;
3151     HRESULT hres;
3152
3153     r = stack_pop(ctx);
3154     l = stack_pop(ctx);
3155
3156     TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
3157
3158     hres = less_eval(ctx->parser->script, r, l, FALSE, &ctx->ei, &b);
3159     VariantClear(l);
3160     VariantClear(r);
3161     if(FAILED(hres))
3162         return hres;
3163
3164     return stack_push_bool(ctx, b);
3165 }
3166
3167 /* ECMA-262 3rd Edition    11.8.4 */
3168 static HRESULT interp_gteq(exec_ctx_t *ctx)
3169 {
3170     VARIANT *l, *r;
3171     BOOL b;
3172     HRESULT hres;
3173
3174     r = stack_pop(ctx);
3175     l = stack_pop(ctx);
3176
3177     TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
3178
3179     hres = less_eval(ctx->parser->script, l, r, TRUE, &ctx->ei, &b);
3180     VariantClear(l);
3181     VariantClear(r);
3182     if(FAILED(hres))
3183         return hres;
3184
3185     return stack_push_bool(ctx, b);
3186 }
3187
3188 /* ECMA-262 3rd Edition    11.4.8 */
3189 static HRESULT interp_bneg(exec_ctx_t *ctx)
3190 {
3191     VARIANT *v, r;
3192     INT i;
3193     HRESULT hres;
3194
3195     TRACE("\n");
3196
3197     v = stack_pop(ctx);
3198     hres = to_int32(ctx->parser->script, v, &ctx->ei, &i);
3199     VariantClear(v);
3200     if(FAILED(hres))
3201         return hres;
3202
3203     V_VT(&r) = VT_I4;
3204     V_I4(&r) = ~i;
3205     return stack_push(ctx, &r);
3206 }
3207
3208 /* ECMA-262 3rd Edition    11.4.9 */
3209 static HRESULT interp_neg(exec_ctx_t *ctx)
3210 {
3211     VARIANT *v;
3212     VARIANT_BOOL b;
3213     HRESULT hres;
3214
3215     TRACE("\n");
3216
3217     v = stack_pop(ctx);
3218     hres = to_boolean(v, &b);
3219     VariantClear(v);
3220     if(FAILED(hres))
3221         return hres;
3222
3223     return stack_push_bool(ctx, !b);
3224 }
3225
3226 /* ECMA-262 3rd Edition    11.7.1 */
3227 static HRESULT lshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3228 {
3229     DWORD ri;
3230     INT li;
3231     HRESULT hres;
3232
3233     hres = to_int32(ctx, lval, ei, &li);
3234     if(FAILED(hres))
3235         return hres;
3236
3237     hres = to_uint32(ctx, rval, ei, &ri);
3238     if(FAILED(hres))
3239         return hres;
3240
3241     V_VT(retv) = VT_I4;
3242     V_I4(retv) = li << (ri&0x1f);
3243     return S_OK;
3244 }
3245
3246 /* ECMA-262 3rd Edition    11.7.1 */
3247 HRESULT left_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3248 {
3249     binary_expression_t *expr = (binary_expression_t*)_expr;
3250
3251     TRACE("\n");
3252
3253     return binary_expr_eval(ctx, expr, lshift_eval, ei, ret);
3254 }
3255
3256 /* ECMA-262 3rd Edition    11.7.2 */
3257 static HRESULT rshift_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3258 {
3259     DWORD ri;
3260     INT li;
3261     HRESULT hres;
3262
3263     hres = to_int32(ctx, lval, ei, &li);
3264     if(FAILED(hres))
3265         return hres;
3266
3267     hres = to_uint32(ctx, rval, ei, &ri);
3268     if(FAILED(hres))
3269         return hres;
3270
3271     V_VT(retv) = VT_I4;
3272     V_I4(retv) = li >> (ri&0x1f);
3273     return S_OK;
3274 }
3275
3276 /* ECMA-262 3rd Edition    11.7.2 */
3277 HRESULT right_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3278 {
3279     binary_expression_t *expr = (binary_expression_t*)_expr;
3280
3281     TRACE("\n");
3282
3283     return binary_expr_eval(ctx, expr, rshift_eval, ei, ret);
3284 }
3285
3286 /* ECMA-262 3rd Edition    11.7.3 */
3287 static HRESULT rshift2_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
3288 {
3289     DWORD li, ri;
3290     HRESULT hres;
3291
3292     hres = to_uint32(ctx, lval, ei, &li);
3293     if(FAILED(hres))
3294         return hres;
3295
3296     hres = to_uint32(ctx, rval, ei, &ri);
3297     if(FAILED(hres))
3298         return hres;
3299
3300     V_VT(retv) = VT_I4;
3301     V_I4(retv) = li >> (ri&0x1f);
3302     return S_OK;
3303 }
3304
3305 /* ECMA-262 3rd Edition    11.7.3 */
3306 HRESULT right2_shift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3307 {
3308     binary_expression_t *expr = (binary_expression_t*)_expr;
3309
3310     TRACE("\n");
3311
3312     return binary_expr_eval(ctx, expr, rshift2_eval, ei, ret);
3313 }
3314
3315 /* ECMA-262 3rd Edition    11.13.1 */
3316 static HRESULT interp_assign(exec_ctx_t *ctx)
3317 {
3318     IDispatch *disp;
3319     DISPID id;
3320     VARIANT *v;
3321     HRESULT hres;
3322
3323     TRACE("\n");
3324
3325     v = stack_pop(ctx);
3326     disp = stack_pop_objid(ctx, &id);
3327
3328     if(!disp)
3329         return throw_reference_error(ctx->parser->script, &ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
3330
3331     hres = disp_propput(ctx->parser->script, disp, id, v, &ctx->ei, NULL/*FIXME*/);
3332     IDispatch_Release(disp);
3333     if(FAILED(hres)) {
3334         VariantClear(v);
3335         return hres;
3336     }
3337
3338     return stack_push(ctx, v);
3339 }
3340
3341 /* ECMA-262 3rd Edition    11.13.2 */
3342 HRESULT assign_lshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3343 {
3344     binary_expression_t *expr = (binary_expression_t*)_expr;
3345
3346     TRACE("\n");
3347
3348     return assign_oper_eval(ctx, expr->expression1, expr->expression2, lshift_eval, ei, ret);
3349 }
3350
3351 /* ECMA-262 3rd Edition    11.13.2 */
3352 HRESULT assign_rshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3353 {
3354     binary_expression_t *expr = (binary_expression_t*)_expr;
3355
3356     TRACE("\n");
3357
3358     return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift_eval, ei, ret);
3359 }
3360
3361 /* ECMA-262 3rd Edition    11.13.2 */
3362 HRESULT assign_rrshift_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3363 {
3364     binary_expression_t *expr = (binary_expression_t*)_expr;
3365
3366     TRACE("\n");
3367
3368     return assign_oper_eval(ctx, expr->expression1, expr->expression2, rshift2_eval, ei, ret);
3369 }
3370
3371 /* ECMA-262 3rd Edition    11.13.2 */
3372 HRESULT assign_and_expression_eval(script_ctx_t *ctx, expression_t *_expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3373 {
3374     binary_expression_t *expr = (binary_expression_t*)_expr;
3375
3376     TRACE("\n");
3377
3378     return assign_oper_eval(ctx, expr->expression1, expr->expression2, bitand_eval, ei, ret);
3379 }
3380
3381 static HRESULT interp_jmp(exec_ctx_t *ctx)
3382 {
3383     const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
3384
3385     TRACE("\n");
3386
3387     ctx->ip = arg;
3388     return S_OK;
3389 }
3390
3391 static HRESULT interp_pop(exec_ctx_t *ctx)
3392 {
3393     TRACE("\n");
3394
3395     stack_popn(ctx, 1);
3396     return S_OK;
3397 }
3398
3399 static HRESULT interp_ret(exec_ctx_t *ctx)
3400 {
3401     TRACE("\n");
3402
3403     ctx->ip = -1;
3404     return S_OK;
3405 }
3406
3407 static HRESULT interp_tree(exec_ctx_t *ctx)
3408 {
3409     instr_t *instr = ctx->parser->code->instrs+ctx->ip;
3410     exprval_t val;
3411     VARIANT v;
3412     HRESULT hres;
3413
3414     TRACE("\n");
3415
3416     hres = expr_eval(ctx->parser->script, instr->arg1.expr, 0, &ctx->ei, &val);
3417     if(FAILED(hres))
3418         return hres;
3419
3420     hres = exprval_to_value(ctx->parser->script, &val, &ctx->ei, &v);
3421     exprval_release(&val);
3422     if(FAILED(hres))
3423         return hres;
3424
3425     return stack_push(ctx, &v);
3426 }
3427
3428 typedef HRESULT (*op_func_t)(exec_ctx_t*);
3429
3430 static const op_func_t op_funcs[] = {
3431 #define X(x,a,b,c) interp_##x,
3432 OP_LIST
3433 #undef X
3434 };
3435
3436 static const unsigned op_move[] = {
3437 #define X(a,x,b,c) x,
3438 OP_LIST
3439 #undef X
3440 };
3441
3442 static HRESULT interp_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3443 {
3444     exec_ctx_t *exec_ctx = ctx->exec_ctx;
3445     unsigned prev_ip, prev_top;
3446     jsop_t op;
3447     HRESULT hres = S_OK;
3448
3449     TRACE("\n");
3450
3451     prev_top = exec_ctx->top;
3452     prev_ip = exec_ctx->ip;
3453     exec_ctx->ip = expr->instr_off;
3454
3455     while(exec_ctx->ip != -1) {
3456         op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3457         hres = op_funcs[op](exec_ctx);
3458         if(FAILED(hres))
3459             break;
3460         exec_ctx->ip += op_move[op];
3461     }
3462
3463     exec_ctx->ip = prev_ip;
3464
3465     if(FAILED(hres)) {
3466         stack_popn(exec_ctx, exec_ctx->top-prev_top);
3467         *ei = exec_ctx->ei;
3468         memset(&exec_ctx->ei, 0, sizeof(exec_ctx->ei));
3469         return hres;
3470     }
3471
3472     assert(exec_ctx->top == prev_top+1 || ((flags&EXPR_NOVAL) && exec_ctx->top == prev_top));
3473
3474     ret->type = EXPRVAL_VARIANT;
3475     if(exec_ctx->top == prev_top)
3476         V_VT(&ret->u.var) = VT_EMPTY;
3477     else
3478         ret->u.var = *stack_pop(exec_ctx);
3479     return S_OK;
3480 }
3481
3482 HRESULT compiled_expression_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, exprval_t *ret)
3483 {
3484     HRESULT hres;
3485
3486     TRACE("\n");
3487
3488     hres = compile_subscript(ctx->exec_ctx->parser, expr, !(flags & EXPR_NOVAL), &expr->instr_off);
3489     if(FAILED(hres))
3490         return hres;
3491
3492     if(expr->eval == compiled_expression_eval)
3493         expr->eval = interp_expression_eval;
3494
3495     return expr->eval(ctx, expr, flags, ei, ret);
3496 }