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