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