2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
20 #include "wine/port.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 #define EXPR_NOVAL 0x0001
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};
42 struct _return_type_t {
52 static inline HRESULT stat_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
54 return stat->eval(ctx, stat, rt, ret);
57 static HRESULT expr_eval(script_ctx_t*,expression_t*,DWORD,jsexcept_t*,VARIANT*);
59 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
61 if(!ctx->stack_size) {
62 ctx->stack = heap_alloc(16*sizeof(VARIANT));
66 }else if(ctx->stack_size == ctx->top) {
69 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
75 ctx->stack = new_stack;
79 ctx->stack[ctx->top++] = *v;
83 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
88 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
89 return stack_push(ctx, &v);
92 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
96 num_set_val(&v, number);
97 return stack_push(ctx, &v);
100 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
106 return stack_push(ctx, &v);
109 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
114 V_BSTR(&v) = SysAllocString(str);
115 return V_BSTR(&v) ? stack_push(ctx, &v) : E_OUTOFMEMORY;
118 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
123 V_VT(&v) = VT_DISPATCH;
124 V_DISPATCH(&v) = disp;
125 hres = stack_push(ctx, &v);
131 return stack_push(ctx, &v);
134 static inline VARIANT *stack_top(exec_ctx_t *ctx)
137 return ctx->stack + ctx->top-1;
140 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
142 assert(ctx->top > n);
143 return ctx->stack + ctx->top-1-n;
146 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
149 return ctx->stack + --ctx->top;
152 static void stack_popn(exec_ctx_t *ctx, unsigned n)
155 VariantClear(stack_pop(ctx));
158 static HRESULT stack_pop_number(exec_ctx_t *ctx, VARIANT *r)
164 hres = to_number(ctx->parser->script, v, ctx->ei, r);
169 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
175 if(V_VT(v) == VT_DISPATCH) {
177 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
182 hres = to_object(ctx->parser->script, v, r);
187 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
189 return to_int32(ctx->parser->script, stack_pop(ctx), ctx->ei, r);
192 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
194 return to_uint32(ctx->parser->script, stack_pop(ctx), ctx->ei, r);
197 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
199 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
201 *id = V_INT(stack_pop(ctx));
202 return V_DISPATCH(stack_pop(ctx));
205 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
207 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
209 *id = V_INT(stack_topn(ctx, n));
210 return V_DISPATCH(stack_topn(ctx, n+1));
213 static void exprval_release(exprval_t *val)
216 case EXPRVAL_VARIANT:
217 if(V_VT(&val->u.var) != VT_EMPTY)
218 VariantClear(&val->u.var);
221 if(val->u.idref.disp)
222 IDispatch_Release(val->u.idref.disp);
224 case EXPRVAL_INVALID:
225 SysFreeString(val->u.identifier);
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)
232 V_VT(ret) = VT_EMPTY;
235 case EXPRVAL_VARIANT:
236 return VariantCopy(ret, &val->u.var);
238 if(!val->u.idref.disp) {
239 FIXME("throw ReferenceError\n");
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);
248 ERR("type %d\n", val->type);
252 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
254 if(val->type == EXPRVAL_VARIANT) {
256 V_VT(&val->u.var) = VT_EMPTY;
260 return exprval_value(ctx, val, ei, ret);
263 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
265 val->type = EXPRVAL_IDREF;
266 val->u.idref.disp = disp;
267 val->u.idref.id = id;
270 IDispatch_AddRef(disp);
273 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
275 scope_chain_t *new_scope;
277 new_scope = heap_alloc(sizeof(scope_chain_t));
279 return E_OUTOFMEMORY;
284 new_scope->obj = obj;
288 new_scope->next = scope;
290 new_scope->next = NULL;
297 static void scope_pop(scope_chain_t **scope)
306 void scope_release(scope_chain_t *scope)
312 scope_release(scope->next);
314 jsdisp_release(scope->obj);
318 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
319 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
323 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
325 return E_OUTOFMEMORY;
328 ctx->is_global = is_global;
331 ctx->this_obj = this_obj;
332 else if(script_ctx->host_global)
333 ctx->this_obj = script_ctx->host_global;
335 ctx->this_obj = to_disp(script_ctx->global);
336 IDispatch_AddRef(ctx->this_obj);
338 jsdisp_addref(var_disp);
339 ctx->var_disp = var_disp;
343 ctx->scope_chain = scope;
350 void exec_release(exec_ctx_t *ctx)
356 scope_release(ctx->scope_chain);
358 jsdisp_release(ctx->var_disp);
360 IDispatch_Release(ctx->this_obj);
361 heap_free(ctx->stack);
365 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
370 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
372 TRACE("unsing IDispatch\n");
375 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
379 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
380 IDispatchEx_Release(dispex);
384 /* ECMA-262 3rd Edition 8.7.2 */
385 static HRESULT put_value(script_ctx_t *ctx, exprval_t *ref, VARIANT *v, jsexcept_t *ei)
387 if(ref->type != EXPRVAL_IDREF)
388 return throw_reference_error(ctx, ei, JS_E_ILLEGAL_ASSIGN, NULL);
390 return disp_propput(ctx, ref->u.idref.disp, ref->u.idref.id, v, ei, NULL/*FIXME*/);
393 static inline BOOL is_null(const VARIANT *v)
395 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
398 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
400 IObjectIdentity *identity;
401 IUnknown *unk1, *unk2;
409 if(!disp1 || !disp2) {
414 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
418 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
420 IUnknown_Release(unk1);
427 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
428 if(SUCCEEDED(hres)) {
429 hres = IObjectIdentity_IsEqualObject(identity, unk2);
430 IObjectIdentity_Release(identity);
437 IUnknown_Release(unk1);
438 IUnknown_Release(unk2);
442 /* ECMA-262 3rd Edition 11.9.6 */
443 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
447 if(V_VT(lval) != V_VT(rval)) {
448 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
449 *ret = num_val(lval) == num_val(rval);
450 else if(is_null(lval))
451 *ret = is_null(rval);
463 *ret = V_I4(lval) == V_I4(rval);
466 *ret = V_R8(lval) == V_R8(rval);
470 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
471 else if(!V_BSTR(rval))
472 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
474 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
477 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
479 *ret = !V_BOOL(lval) == !V_BOOL(rval);
482 FIXME("unimplemented vt %d\n", V_VT(lval));
489 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
495 for(item = ctx->named_items; item; item = item->next) {
496 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
497 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
498 if(SUCCEEDED(hres)) {
500 exprval_set_idref(ret, item->disp, id);
509 HRESULT exec_source(exec_ctx_t *ctx, parser_ctx_t *parser, source_elements_t *source, BOOL from_eval,
510 jsexcept_t *ei, VARIANT *retv)
512 script_ctx_t *script = parser->script;
513 function_declaration_t *func;
514 parser_ctx_t *prev_parser;
517 exec_ctx_t *prev_ctx;
521 for(func = source->functions; func; func = func->next) {
525 hres = create_source_function(parser, func->expr->parameter_list, func->expr->source_elements,
526 ctx->scope_chain, func->expr->src_str, func->expr->src_len, &func_obj);
530 var_set_jsdisp(&var, func_obj);
531 hres = jsdisp_propput_name(ctx->var_disp, func->expr->identifier, &var, ei, NULL);
532 jsdisp_release(func_obj);
537 for(var = source->variables; var; var = var->next) {
541 name = SysAllocString(var->identifier);
543 return E_OUTOFMEMORY;
545 if(!ctx->is_global || !lookup_global_members(parser->script, name, NULL))
546 hres = jsdisp_get_id(ctx->var_disp, var->identifier, fdexNameEnsure, &id);
552 prev_ctx = script->exec_ctx;
553 script->exec_ctx = ctx;
555 prev_parser = ctx->parser;
556 ctx->parser = parser;
558 V_VT(&val) = VT_EMPTY;
559 memset(&rt, 0, sizeof(rt));
562 if(source->statement) {
563 if(source->statement->instr_off == -1)
564 hres = compile_subscript_stat(ctx->parser, source->statement, TRUE, &source->statement->instr_off);
566 hres = compiled_statement_eval(script, source->statement, &rt, &val);
569 script->exec_ctx = prev_ctx;
570 ctx->parser = prev_parser;
572 if(rt.type != RT_NORMAL && rt.type != RT_RETURN) {
573 FIXME("wrong rt %d\n", rt.type);
583 if(!retv || (!from_eval && rt.type != RT_RETURN))
590 /* ECMA-262 3rd Edition 10.1.4 */
591 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, DWORD flags, jsexcept_t *ei, exprval_t *ret)
593 scope_chain_t *scope;
598 TRACE("%s\n", debugstr_w(identifier));
600 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
601 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
602 if(SUCCEEDED(hres)) {
603 exprval_set_idref(ret, to_disp(scope->obj), id);
608 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
609 if(SUCCEEDED(hres)) {
610 exprval_set_idref(ret, to_disp(ctx->global), id);
614 for(item = ctx->named_items; item; item = item->next) {
615 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
622 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
623 SCRIPTINFO_IUNKNOWN, &unk, NULL);
625 WARN("GetItemInfo failed: %08x\n", hres);
629 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
630 IUnknown_Release(unk);
632 WARN("object does not implement IDispatch\n");
637 ret->type = EXPRVAL_VARIANT;
638 V_VT(&ret->u.var) = VT_DISPATCH;
639 V_DISPATCH(&ret->u.var) = item->disp;
640 IDispatch_AddRef(item->disp);
645 if(lookup_global_members(ctx, identifier, ret))
648 if(flags & fdexNameEnsure) {
649 hres = jsdisp_get_id(ctx->global, identifier, fdexNameEnsure, &id);
653 exprval_set_idref(ret, to_disp(ctx->global), id);
657 ret->type = EXPRVAL_INVALID;
658 ret->u.identifier = SysAllocString(identifier);
659 if(!ret->u.identifier)
660 return E_OUTOFMEMORY;
665 /* ECMA-262 3rd Edition 12.2 */
666 static HRESULT variable_list_eval(script_ctx_t *ctx, variable_declaration_t *var_list, jsexcept_t *ei)
668 variable_declaration_t *iter;
671 for(iter = var_list; iter; iter = iter->next) {
677 hres = expr_eval(ctx, iter->expr, 0, ei, &val);
681 hres = jsdisp_propput_name(ctx->exec_ctx->var_disp, iter->identifier, &val, ei, NULL/*FIXME*/);
690 /* ECMA-262 3rd Edition 12.2 */
691 HRESULT var_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
693 var_statement_t *stat = (var_statement_t*)_stat;
698 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
702 V_VT(ret) = VT_EMPTY;
706 /* ECMA-262 3rd Edition 12.6.2 */
707 HRESULT while_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
709 while_statement_t *stat = (while_statement_t*)_stat;
717 V_VT(&val) = VT_EMPTY;
718 test_expr = !stat->do_while;
722 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &tmp);
726 hres = to_boolean(&tmp, &b);
728 if(FAILED(hres) || !b)
734 hres = stat_eval(ctx, stat->statement, rt, &tmp);
741 if(rt->type == RT_CONTINUE)
742 rt->type = RT_NORMAL;
743 if(rt->type != RT_NORMAL)
752 if(rt->type == RT_BREAK)
753 rt->type = RT_NORMAL;
759 /* ECMA-262 3rd Edition 12.6.3 */
760 HRESULT for_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
762 for_statement_t *stat = (for_statement_t*)_stat;
763 VARIANT val, tmp, retv;
769 if(stat->variable_list) {
770 hres = variable_list_eval(ctx, stat->variable_list, &rt->ei);
773 }else if(stat->begin_expr) {
774 hres = expr_eval(ctx, stat->begin_expr, 0, &rt->ei, &val);
781 V_VT(&retv) = VT_EMPTY;
785 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &tmp);
789 hres = to_boolean(&tmp, &b);
791 if(FAILED(hres) || !b)
795 hres = stat_eval(ctx, stat->statement, rt, &tmp);
802 if(rt->type == RT_CONTINUE)
803 rt->type = RT_NORMAL;
804 else if(rt->type != RT_NORMAL)
808 hres = expr_eval(ctx, stat->end_expr, 0, &rt->ei, &val);
821 if(rt->type == RT_BREAK)
822 rt->type = RT_NORMAL;
828 static HRESULT array_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
829 static HRESULT member_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
830 static HRESULT identifier_expression_eval(script_ctx_t*,expression_t*,jsexcept_t*,exprval_t*);
832 /* ECMA-262 3rd Edition 12.6.4 */
833 HRESULT forin_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
835 forin_statement_t *stat = (forin_statement_t*)_stat;
836 VARIANT val, name, retv, tmp;
837 DISPID id = DISPID_STARTENUM;
838 BSTR str, identifier = NULL;
846 hres = variable_list_eval(ctx, stat->variable, &rt->ei);
851 hres = expr_eval(ctx, stat->in_expr, 0, &rt->ei, &val);
855 if(V_VT(&val) != VT_DISPATCH) {
856 TRACE("in vt %d\n", V_VT(&val));
858 V_VT(ret) = VT_EMPTY;
862 hres = IDispatch_QueryInterface(V_DISPATCH(&val), &IID_IDispatchEx, (void**)&in_obj);
863 IDispatch_Release(V_DISPATCH(&val));
865 TRACE("Object doesn't support IDispatchEx\n");
866 V_VT(ret) = VT_EMPTY;
870 V_VT(&retv) = VT_EMPTY;
873 identifier = SysAllocString(stat->variable->identifier);
876 hres = IDispatchEx_GetNextDispID(in_obj, fdexEnumDefault, id, &id);
877 if(FAILED(hres) || hres == S_FALSE)
880 hres = IDispatchEx_GetMemberName(in_obj, id, &str);
884 TRACE("iter %s\n", debugstr_w(str));
887 hres = identifier_eval(ctx, identifier, 0, NULL, &exprval);
889 switch(stat->expr->type) {
891 hres = array_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
894 hres = identifier_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
897 hres = member_expression_eval(ctx, stat->expr, &rt->ei, &exprval);
900 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &tmp);
905 hres = throw_reference_error(ctx, &rt->ei, JS_E_ILLEGAL_ASSIGN, NULL);
909 if(SUCCEEDED(hres)) {
910 V_VT(&name) = VT_BSTR;
912 hres = put_value(ctx, &exprval, &name, &rt->ei);
913 exprval_release(&exprval);
919 hres = stat_eval(ctx, stat->statement, rt, &tmp);
926 if(rt->type == RT_CONTINUE)
927 rt->type = RT_NORMAL;
928 else if(rt->type != RT_NORMAL)
932 SysFreeString(identifier);
933 IDispatchEx_Release(in_obj);
939 if(rt->type == RT_BREAK)
940 rt->type = RT_NORMAL;
946 /* ECMA-262 3rd Edition 12.7 */
947 HRESULT continue_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
949 branch_statement_t *stat = (branch_statement_t*)_stat;
953 if(stat->identifier) {
954 FIXME("indentifier not implemented\n");
958 rt->type = RT_CONTINUE;
959 V_VT(ret) = VT_EMPTY;
963 /* ECMA-262 3rd Edition 12.8 */
964 HRESULT break_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
966 branch_statement_t *stat = (branch_statement_t*)_stat;
970 if(stat->identifier) {
971 FIXME("indentifier not implemented\n");
976 V_VT(ret) = VT_EMPTY;
980 /* ECMA-262 3rd Edition 12.9 */
981 HRESULT return_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
983 expression_statement_t *stat = (expression_statement_t*)_stat;
989 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, ret);
993 V_VT(ret) = VT_EMPTY;
996 TRACE("= %s\n", debugstr_variant(ret));
997 rt->type = RT_RETURN;
1001 /* ECMA-262 3rd Edition 12.10 */
1002 HRESULT with_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1004 with_statement_t *stat = (with_statement_t*)_stat;
1012 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &val);
1016 hres = to_object(ctx, &val, &disp);
1021 obj = iface_to_jsdisp((IUnknown*)disp);
1022 IDispatch_Release(disp);
1024 FIXME("disp id not jsdisp\n");
1028 hres = scope_push(ctx->exec_ctx->scope_chain, obj, &ctx->exec_ctx->scope_chain);
1029 jsdisp_release(obj);
1033 hres = stat_eval(ctx, stat->statement, rt, ret);
1035 scope_pop(&ctx->exec_ctx->scope_chain);
1039 /* ECMA-262 3rd Edition 12.12 */
1040 HRESULT labelled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
1046 /* ECMA-262 3rd Edition 12.13 */
1047 HRESULT switch_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1049 switch_statement_t *stat = (switch_statement_t*)_stat;
1050 case_clausule_t *iter, *default_clausule = NULL;
1051 statement_t *stat_iter;
1058 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &val);
1062 for(iter = stat->case_list; iter; iter = iter->next) {
1064 default_clausule = iter;
1068 hres = expr_eval(ctx, iter->expr, 0, &rt->ei, &cval);
1072 hres = equal2_values(&val, &cval, &b);
1073 VariantClear(&cval);
1074 if(FAILED(hres) || b)
1083 iter = default_clausule;
1085 V_VT(&val) = VT_EMPTY;
1089 for(stat_iter = iter->stat; stat_iter; stat_iter = stat_iter->next) {
1090 hres = stat_eval(ctx, stat_iter, rt, &tmp);
1097 if(rt->type != RT_NORMAL)
1107 if(rt->type == RT_BREAK)
1108 rt->type = RT_NORMAL;
1114 /* ECMA-262 3rd Edition 12.13 */
1115 HRESULT throw_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1117 expression_statement_t *stat = (expression_statement_t*)_stat;
1123 hres = expr_eval(ctx, stat->expr, 0, &rt->ei, &val);
1128 return DISP_E_EXCEPTION;
1131 static HRESULT interp_throw(exec_ctx_t *ctx)
1133 const HRESULT arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1135 TRACE("%08x\n", arg);
1137 return throw_reference_error(ctx->parser->script, ctx->ei, arg, NULL);
1140 static HRESULT interp_throw_type(exec_ctx_t *ctx)
1142 const HRESULT hres = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1143 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg2.str;
1145 TRACE("%08x %s\n", hres, debugstr_w(str));
1147 return throw_type_error(ctx->parser->script, ctx->ei, hres, str);
1150 /* ECMA-262 3rd Edition 12.14 */
1151 static HRESULT catch_eval(script_ctx_t *ctx, catch_block_t *block, return_type_t *rt, VARIANT *ret)
1158 memset(&rt->ei, 0, sizeof(jsexcept_t));
1160 hres = create_dispex(ctx, NULL, NULL, &var_disp);
1161 if(SUCCEEDED(hres)) {
1162 hres = jsdisp_propput_name(var_disp, block->identifier, &ex, &rt->ei, NULL/*FIXME*/);
1163 if(SUCCEEDED(hres)) {
1164 hres = scope_push(ctx->exec_ctx->scope_chain, var_disp, &ctx->exec_ctx->scope_chain);
1165 if(SUCCEEDED(hres)) {
1166 hres = stat_eval(ctx, block->statement, rt, &val);
1167 scope_pop(&ctx->exec_ctx->scope_chain);
1171 jsdisp_release(var_disp);
1182 /* ECMA-262 3rd Edition 12.14 */
1183 HRESULT try_statement_eval(script_ctx_t *ctx, statement_t *_stat, return_type_t *rt, VARIANT *ret)
1185 try_statement_t *stat = (try_statement_t*)_stat;
1191 hres = stat_eval(ctx, stat->try_statement, rt, &val);
1193 TRACE("EXCEPTION\n");
1194 if(!stat->catch_block)
1197 hres = catch_eval(ctx, stat->catch_block, rt, &val);
1202 if(stat->finally_statement) {
1204 hres = stat_eval(ctx, stat->finally_statement, rt, &val);
1213 /* ECMA-262 3rd Edition 13 */
1214 static HRESULT interp_func(exec_ctx_t *ctx)
1216 function_expression_t *expr = ctx->parser->code->instrs[ctx->ip].arg1.func;
1223 hres = create_source_function(ctx->parser, expr->parameter_list, expr->source_elements, ctx->scope_chain,
1224 expr->src_str, expr->src_len, &dispex);
1228 var_set_jsdisp(&v, dispex);
1229 return stack_push(ctx, &v);
1232 /* ECMA-262 3rd Edition 11.2.1 */
1233 static HRESULT array_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1235 binary_expression_t *expr = (binary_expression_t*)_expr;
1236 VARIANT member, val;
1239 IDispatch *obj = NULL;
1244 hres = expr_eval(ctx, expr->expression1, 0, ei, &member);
1248 hres = expr_eval(ctx, expr->expression2, 0, ei, &val);
1249 if(SUCCEEDED(hres)) {
1250 hres = to_object(ctx, &member, &obj);
1254 VariantClear(&member);
1255 if(SUCCEEDED(hres)) {
1256 hres = to_string(ctx, &val, ei, &str);
1258 if(SUCCEEDED(hres)) {
1259 hres = disp_get_id(ctx, obj, str, fdexNameEnsure, &id);
1264 exprval_set_idref(ret, obj, id);
1266 IDispatch_Release(obj);
1272 /* ECMA-262 3rd Edition 11.2.1 */
1273 static HRESULT interp_array(exec_ctx_t *ctx)
1283 namev = stack_pop(ctx);
1285 hres = stack_pop_object(ctx, &obj);
1287 VariantClear(namev);
1291 hres = to_string(ctx->parser->script, namev, ctx->ei, &name);
1292 VariantClear(namev);
1294 IDispatch_Release(obj);
1298 hres = disp_get_id(ctx->parser->script, obj, name, 0, &id);
1299 SysFreeString(name);
1300 if(SUCCEEDED(hres)) {
1301 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1302 }else if(hres == DISP_E_UNKNOWNNAME) {
1303 V_VT(&v) = VT_EMPTY;
1306 IDispatch_Release(obj);
1310 return stack_push(ctx, &v);
1313 /* ECMA-262 3rd Edition 11.2.1 */
1314 static HRESULT member_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1316 member_expression_t *expr = (member_expression_t*)_expr;
1317 IDispatch *obj = NULL;
1325 hres = expr_eval(ctx, expr->expression, 0, ei, &member);
1329 hres = to_object(ctx, &member, &obj);
1330 VariantClear(&member);
1334 str = SysAllocString(expr->identifier);
1336 IDispatch_Release(obj);
1337 return E_OUTOFMEMORY;
1340 hres = disp_get_id(ctx, obj, str, fdexNameEnsure, &id);
1343 exprval_set_idref(ret, obj, id);
1345 IDispatch_Release(obj);
1349 /* ECMA-262 3rd Edition 11.2.1 */
1350 static HRESULT interp_member(exec_ctx_t *ctx)
1352 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1360 hres = stack_pop_object(ctx, &obj);
1364 hres = disp_get_id(ctx->parser->script, obj, arg, 0, &id);
1365 if(SUCCEEDED(hres)) {
1366 V_VT(&v) = VT_EMPTY;
1367 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
1368 }else if(hres == DISP_E_UNKNOWNNAME) {
1369 V_VT(&v) = VT_EMPTY;
1372 IDispatch_Release(obj);
1376 return stack_push(ctx, &v);
1379 /* ECMA-262 3rd Edition 11.2.1 */
1380 static HRESULT interp_memberid(exec_ctx_t *ctx)
1382 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1383 VARIANT *objv, *namev;
1391 namev = stack_pop(ctx);
1392 objv = stack_pop(ctx);
1394 hres = to_object(ctx->parser->script, objv, &obj);
1396 if(SUCCEEDED(hres)) {
1397 hres = to_string(ctx->parser->script, namev, ctx->ei, &name);
1399 IDispatch_Release(obj);
1401 VariantClear(namev);
1405 hres = disp_get_id(ctx->parser->script, obj, name, arg, &id);
1406 SysFreeString(name);
1408 IDispatch_Release(obj);
1409 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
1411 id = JS_E_INVALID_PROPERTY;
1417 return stack_push_objid(ctx, obj, id);
1420 /* ECMA-262 3rd Edition 11.2.1 */
1421 static HRESULT interp_refval(exec_ctx_t *ctx)
1430 disp = stack_topn_objid(ctx, 0, &id);
1432 return throw_reference_error(ctx->parser->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
1434 hres = disp_propget(ctx->parser->script, disp, id, &v, ctx->ei, NULL/*FIXME*/);
1438 return stack_push(ctx, &v);
1441 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
1446 dp->cArgs = arg_cnt;
1447 dp->rgdispidNamedArgs = NULL;
1450 assert(ctx->top >= arg_cnt);
1452 for(i=1; i*2 <= arg_cnt; i++) {
1453 tmp = ctx->stack[ctx->top-i];
1454 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
1455 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
1458 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
1461 /* ECMA-262 3rd Edition 11.2.2 */
1462 static HRESULT interp_new(exec_ctx_t *ctx)
1464 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1471 constr = stack_topn(ctx, arg);
1473 /* NOTE: Should use to_object here */
1475 if(V_VT(constr) == VT_NULL)
1476 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1477 else if(V_VT(constr) != VT_DISPATCH)
1478 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
1479 else if(!V_DISPATCH(constr))
1480 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1482 jsstack_to_dp(ctx, arg, &dp);
1483 hres = disp_call(ctx->parser->script, V_DISPATCH(constr), DISPID_VALUE,
1484 DISPATCH_CONSTRUCT, &dp, &v, ctx->ei, NULL/*FIXME*/);
1488 stack_popn(ctx, arg+1);
1489 return stack_push(ctx, &v);
1492 /* ECMA-262 3rd Edition 11.2.3 */
1493 static HRESULT interp_call(exec_ctx_t *ctx)
1495 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1496 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1501 TRACE("%d %d\n", argn, do_ret);
1503 objv = stack_topn(ctx, argn);
1504 if(V_VT(objv) != VT_DISPATCH)
1505 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1507 jsstack_to_dp(ctx, argn, &dp);
1508 hres = disp_call(ctx->parser->script, V_DISPATCH(objv), DISPID_VALUE, DISPATCH_METHOD, &dp,
1509 do_ret ? &v : NULL, ctx->ei, NULL/*FIXME*/);
1513 stack_popn(ctx, argn+1);
1514 return do_ret ? stack_push(ctx, &v) : S_OK;
1518 /* ECMA-262 3rd Edition 11.2.3 */
1519 static HRESULT interp_call_member(exec_ctx_t *ctx)
1521 const unsigned argn = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1522 const int do_ret = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1529 TRACE("%d %d\n", argn, do_ret);
1531 obj = stack_topn_objid(ctx, argn, &id);
1533 return throw_type_error(ctx->parser->script, ctx->ei, id, NULL);
1535 jsstack_to_dp(ctx, argn, &dp);
1536 hres = disp_call(ctx->parser->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, ctx->ei, NULL/*FIXME*/);
1540 stack_popn(ctx, argn+2);
1541 return do_ret ? stack_push(ctx, &v) : S_OK;
1545 /* ECMA-262 3rd Edition 11.1.1 */
1546 static HRESULT interp_this(exec_ctx_t *ctx)
1552 V_VT(&v) = VT_DISPATCH;
1553 V_DISPATCH(&v) = ctx->this_obj;
1554 IDispatch_AddRef(ctx->this_obj);
1555 return stack_push(ctx, &v);
1558 /* ECMA-262 3rd Edition 10.1.4 */
1559 static HRESULT identifier_expression_eval(script_ctx_t *ctx, expression_t *_expr, jsexcept_t *ei, exprval_t *ret)
1561 identifier_expression_t *expr = (identifier_expression_t*)_expr;
1567 identifier = SysAllocString(expr->identifier);
1569 return E_OUTOFMEMORY;
1571 hres = identifier_eval(ctx, identifier, fdexNameEnsure, ei, ret);
1573 SysFreeString(identifier);
1577 /* ECMA-262 3rd Edition 10.1.4 */
1578 static HRESULT interp_ident(exec_ctx_t *ctx)
1580 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1585 TRACE("%s\n", debugstr_w(arg));
1587 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
1591 hres = exprval_to_value(ctx->parser->script, &exprval, ctx->ei, &v);
1592 exprval_release(&exprval);
1596 return stack_push(ctx, &v);
1599 /* ECMA-262 3rd Edition 10.1.4 */
1600 static HRESULT interp_identid(exec_ctx_t *ctx)
1602 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1603 const unsigned flags = ctx->parser->code->instrs[ctx->ip].arg2.uint;
1607 TRACE("%s %x\n", debugstr_w(arg), flags);
1609 hres = identifier_eval(ctx->parser->script, arg, flags, ctx->ei, &exprval);
1613 if(exprval.type != EXPRVAL_IDREF) {
1614 WARN("invalid ref\n");
1615 exprval_release(&exprval);
1616 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1619 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1622 /* ECMA-262 3rd Edition 7.8.1 */
1623 static HRESULT interp_null(exec_ctx_t *ctx)
1630 return stack_push(ctx, &v);
1633 /* ECMA-262 3rd Edition 7.8.2 */
1634 static HRESULT interp_bool(exec_ctx_t *ctx)
1636 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1638 TRACE("%s\n", arg ? "true" : "false");
1640 return stack_push_bool(ctx, arg);
1643 /* ECMA-262 3rd Edition 7.8.3 */
1644 static HRESULT interp_int(exec_ctx_t *ctx)
1646 const LONG arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
1653 return stack_push(ctx, &v);
1656 /* ECMA-262 3rd Edition 7.8.3 */
1657 static HRESULT interp_double(exec_ctx_t *ctx)
1659 const double arg = *ctx->parser->code->instrs[ctx->ip].arg1.dbl;
1662 TRACE("%lf\n", arg);
1666 return stack_push(ctx, &v);
1669 /* ECMA-262 3rd Edition 7.8.4 */
1670 static HRESULT interp_str(exec_ctx_t *ctx)
1672 const WCHAR *str = ctx->parser->code->instrs[ctx->ip].arg1.str;
1675 TRACE("%s\n", debugstr_w(str));
1678 V_BSTR(&v) = SysAllocString(str);
1680 return E_OUTOFMEMORY;
1682 return stack_push(ctx, &v);
1685 /* ECMA-262 3rd Edition 7.8 */
1686 static HRESULT interp_regexp(exec_ctx_t *ctx)
1688 const WCHAR *source = ctx->parser->code->instrs[ctx->ip].arg1.str;
1689 const LONG flags = ctx->parser->code->instrs[ctx->ip].arg2.lng;
1694 TRACE("%s %x\n", debugstr_w(source), flags);
1696 hres = create_regexp(ctx->parser->script, source, strlenW(source), flags, ®exp);
1700 var_set_jsdisp(&v, regexp);
1701 return stack_push(ctx, &v);
1704 /* ECMA-262 3rd Edition 11.1.4 */
1705 static HRESULT interp_carray(exec_ctx_t *ctx)
1707 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1715 hres = create_array(ctx->parser->script, arg, &array);
1722 hres = jsdisp_propput_idx(array, i, v, ctx->ei, NULL/*FIXME*/);
1725 jsdisp_release(array);
1730 var_set_jsdisp(&r, array);
1731 return stack_push(ctx, &r);
1734 /* ECMA-262 3rd Edition 11.1.5 */
1735 HRESULT interp_new_obj(exec_ctx_t *ctx)
1743 hres = create_object(ctx->parser->script, NULL, &obj);
1747 var_set_jsdisp(&v, obj);
1748 return stack_push(ctx, &v);
1751 /* ECMA-262 3rd Edition 11.1.5 */
1752 HRESULT interp_obj_prop(exec_ctx_t *ctx)
1754 const BSTR name = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
1759 TRACE("%s\n", debugstr_w(name));
1763 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1764 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1766 hres = jsdisp_propput_name(obj, name, v, ctx->ei, NULL/*FIXME*/);
1771 /* ECMA-262 3rd Edition 11.11 */
1772 static HRESULT interp_jmp_nz(exec_ctx_t *ctx)
1774 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1780 hres = to_boolean(stack_top(ctx), &b);
1793 /* ECMA-262 3rd Edition 11.11 */
1794 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
1796 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
1802 hres = to_boolean(stack_top(ctx), &b);
1815 /* ECMA-262 3rd Edition 11.10 */
1816 static HRESULT interp_or(exec_ctx_t *ctx)
1823 hres = stack_pop_int(ctx, &r);
1827 hres = stack_pop_int(ctx, &l);
1831 return stack_push_int(ctx, l|r);
1834 /* ECMA-262 3rd Edition 11.10 */
1835 static HRESULT interp_xor(exec_ctx_t *ctx)
1842 hres = stack_pop_int(ctx, &r);
1846 hres = stack_pop_int(ctx, &l);
1850 return stack_push_int(ctx, l^r);
1853 /* ECMA-262 3rd Edition 11.10 */
1854 static HRESULT interp_and(exec_ctx_t *ctx)
1861 hres = stack_pop_int(ctx, &r);
1865 hres = stack_pop_int(ctx, &l);
1869 return stack_push_int(ctx, l&r);
1872 /* ECMA-262 3rd Edition 11.8.6 */
1873 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1875 jsdisp_t *obj, *iter, *tmp = NULL;
1880 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1883 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1885 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1888 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1889 IDispatch_Release(V_DISPATCH(v));
1891 FIXME("non-jsdisp objects not supported\n");
1895 if(is_class(obj, JSCLASS_FUNCTION)) {
1896 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei, NULL/*FIXME*/);
1898 hres = throw_type_error(ctx->parser->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1900 jsdisp_release(obj);
1906 if(V_VT(&prot) == VT_DISPATCH) {
1907 if(V_VT(v) == VT_DISPATCH)
1908 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1909 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1910 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1916 jsdisp_release(tmp);
1918 FIXME("prototype is not an object\n");
1922 VariantClear(&prot);
1927 return stack_push_bool(ctx, ret);
1930 /* ECMA-262 3rd Edition 11.8.7 */
1931 static HRESULT interp_in(exec_ctx_t *ctx)
1941 obj = stack_pop(ctx);
1944 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1947 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1950 hres = to_string(ctx->parser->script, v, ctx->ei, &str);
1953 IDispatch_Release(V_DISPATCH(obj));
1957 hres = disp_get_id(ctx->parser->script, V_DISPATCH(obj), str, 0, &id);
1958 IDispatch_Release(V_DISPATCH(obj));
1962 else if(hres == DISP_E_UNKNOWNNAME)
1967 return stack_push_bool(ctx, ret);
1970 /* ECMA-262 3rd Edition 11.6.1 */
1971 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1976 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1980 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1986 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1987 BSTR lstr = NULL, rstr = NULL;
1989 if(V_VT(&l) == VT_BSTR)
1992 hres = to_string(ctx, &l, ei, &lstr);
1994 if(SUCCEEDED(hres)) {
1995 if(V_VT(&r) == VT_BSTR)
1998 hres = to_string(ctx, &r, ei, &rstr);
2001 if(SUCCEEDED(hres)) {
2004 len1 = SysStringLen(lstr);
2005 len2 = SysStringLen(rstr);
2007 V_VT(retv) = VT_BSTR;
2008 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
2009 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
2010 memcpy(V_BSTR(retv)+len1, rstr, (len2+1)*sizeof(WCHAR));
2013 if(V_VT(&l) != VT_BSTR)
2014 SysFreeString(lstr);
2015 if(V_VT(&r) != VT_BSTR)
2016 SysFreeString(rstr);
2020 hres = to_number(ctx, &l, ei, &nl);
2021 if(SUCCEEDED(hres)) {
2022 hres = to_number(ctx, &r, ei, &nr);
2024 num_set_val(retv, num_val(&nl) + num_val(&nr));
2033 /* ECMA-262 3rd Edition 11.6.1 */
2034 static HRESULT interp_add(exec_ctx_t *ctx)
2036 VARIANT *l, *r, ret;
2042 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
2044 hres = add_eval(ctx->parser->script, l, r, ctx->ei, &ret);
2050 return stack_push(ctx, &ret);
2053 /* ECMA-262 3rd Edition 11.6.2 */
2054 static HRESULT interp_sub(exec_ctx_t *ctx)
2061 hres = stack_pop_number(ctx, &r);
2065 hres = stack_pop_number(ctx, &l);
2069 return stack_push_number(ctx, num_val(&l)-num_val(&r));
2072 /* ECMA-262 3rd Edition 11.5.1 */
2073 static HRESULT interp_mul(exec_ctx_t *ctx)
2080 hres = stack_pop_number(ctx, &r);
2084 hres = stack_pop_number(ctx, &l);
2088 return stack_push_number(ctx, num_val(&l)*num_val(&r));
2091 /* ECMA-262 3rd Edition 11.5.2 */
2092 static HRESULT interp_div(exec_ctx_t *ctx)
2099 hres = stack_pop_number(ctx, &r);
2103 hres = stack_pop_number(ctx, &l);
2107 return stack_push_number(ctx, num_val(&l)/num_val(&r));
2110 /* ECMA-262 3rd Edition 11.5.3 */
2111 static HRESULT interp_mod(exec_ctx_t *ctx)
2118 hres = stack_pop_number(ctx, &r);
2122 hres = stack_pop_number(ctx, &l);
2126 return stack_push_number(ctx, fmod(num_val(&l), num_val(&r)));
2129 /* ECMA-262 3rd Edition 11.4.2 */
2130 static HRESULT interp_delete(exec_ctx_t *ctx)
2132 VARIANT *obj_var, *name_var;
2133 IDispatchEx *dispex;
2141 name_var = stack_pop(ctx);
2142 obj_var = stack_pop(ctx);
2144 hres = to_object(ctx->parser->script, obj_var, &obj);
2145 VariantClear(obj_var);
2147 VariantClear(name_var);
2151 hres = to_string(ctx->parser->script, name_var, ctx->ei, &name);
2152 VariantClear(name_var);
2154 IDispatch_Release(obj);
2158 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
2159 if(SUCCEEDED(hres)) {
2160 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->parser->script, fdexNameCaseSensitive));
2162 IDispatchEx_Release(dispex);
2168 IDispatch_Release(obj);
2169 SysFreeString(name);
2173 return stack_push_bool(ctx, ret);
2176 /* ECMA-262 3rd Edition 11.4.2 */
2177 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
2179 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2180 IDispatchEx *dispex;
2185 TRACE("%s\n", debugstr_w(arg));
2187 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
2191 if(exprval.type != EXPRVAL_IDREF) {
2192 FIXME("Unsupported exprval\n");
2193 exprval_release(&exprval);
2197 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
2198 IDispatch_Release(exprval.u.idref.disp);
2199 if(SUCCEEDED(hres)) {
2200 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
2201 IDispatchEx_Release(dispex);
2208 return stack_push_bool(ctx, ret);
2211 /* ECMA-262 3rd Edition 11.4.2 */
2212 static HRESULT interp_void(exec_ctx_t *ctx)
2220 V_VT(&v) = VT_EMPTY;
2221 return stack_push(ctx, &v);
2224 /* ECMA-262 3rd Edition 11.4.3 */
2225 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
2247 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
2248 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
2249 jsdisp_release(dispex);
2256 FIXME("unhandled vt %d\n", V_VT(v));
2263 /* ECMA-262 3rd Edition 11.4.3 */
2264 static HRESULT interp_typeofid(exec_ctx_t *ctx)
2272 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
2276 obj = stack_pop_objid(ctx, &id);
2278 return stack_push_string(ctx, undefinedW);
2280 V_VT(&v) = VT_EMPTY;
2281 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
2282 IDispatch_Release(obj);
2284 return stack_push_string(ctx, unknownW);
2286 hres = typeof_string(&v, &ret);
2291 return stack_push_string(ctx, ret);
2294 /* ECMA-262 3rd Edition 11.4.3 */
2295 static HRESULT interp_typeofident(exec_ctx_t *ctx)
2297 const BSTR arg = ctx->parser->code->instrs[ctx->ip].arg1.bstr;
2303 TRACE("%s\n", debugstr_w(arg));
2305 hres = identifier_eval(ctx->parser->script, arg, 0, ctx->ei, &exprval);
2309 if(exprval.type == EXPRVAL_INVALID) {
2310 hres = stack_push_string(ctx, undefinedW);
2311 exprval_release(&exprval);
2315 hres = exprval_to_value(ctx->parser->script, &exprval, ctx->ei, &v);
2316 exprval_release(&exprval);
2320 hres = typeof_string(&v, &ret);
2325 return stack_push_string(ctx, ret);
2328 /* ECMA-262 3rd Edition 11.4.3 */
2329 static HRESULT interp_typeof(exec_ctx_t *ctx)
2338 hres = typeof_string(v, &ret);
2343 return stack_push_string(ctx, ret);
2346 /* ECMA-262 3rd Edition 11.4.7 */
2347 static HRESULT interp_minus(exec_ctx_t *ctx)
2354 hres = stack_pop_number(ctx, &n);
2358 return stack_push_number(ctx, -num_val(&n));
2361 /* ECMA-262 3rd Edition 11.4.6 */
2362 static HRESULT interp_tonum(exec_ctx_t *ctx)
2370 hres = to_number(ctx->parser->script, v, ctx->ei, &num);
2375 return stack_push(ctx, &num);
2378 /* ECMA-262 3rd Edition 11.3.1 */
2379 static HRESULT interp_postinc(exec_ctx_t *ctx)
2381 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2389 obj = stack_pop_objid(ctx, &id);
2391 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2393 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
2394 if(SUCCEEDED(hres)) {
2397 hres = to_number(ctx->parser->script, &v, ctx->ei, &n);
2398 if(SUCCEEDED(hres)) {
2399 num_set_val(&inc, num_val(&n)+(double)arg);
2400 hres = disp_propput(ctx->parser->script, obj, id, &inc, ctx->ei, NULL/*FIXME*/);
2405 IDispatch_Release(obj);
2409 return stack_push(ctx, &v);
2412 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
2413 static HRESULT interp_preinc(exec_ctx_t *ctx)
2415 const int arg = ctx->parser->code->instrs[ctx->ip].arg1.lng;
2423 obj = stack_pop_objid(ctx, &id);
2425 return throw_type_error(ctx->parser->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
2427 hres = disp_propget(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
2428 if(SUCCEEDED(hres)) {
2431 hres = to_number(ctx->parser->script, &v, ctx->ei, &n);
2433 if(SUCCEEDED(hres)) {
2434 num_set_val(&v, num_val(&n)+(double)arg);
2435 hres = disp_propput(ctx->parser->script, obj, id, &v, ctx->ei, NULL/*FIXME*/);
2438 IDispatch_Release(obj);
2442 return stack_push(ctx, &v);
2445 /* ECMA-262 3rd Edition 11.9.3 */
2446 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
2448 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
2449 return equal2_values(lval, rval, ret);
2451 /* FIXME: NULL disps should be handled in more general way */
2452 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
2455 return equal_values(ctx, &v, rval, ei, ret);
2458 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
2461 return equal_values(ctx, lval, &v, ei, ret);
2464 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
2465 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
2470 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2474 hres = to_number(ctx, lval, ei, &v);
2478 return equal_values(ctx, &v, rval, ei, ret);
2481 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2485 hres = to_number(ctx, rval, ei, &v);
2489 return equal_values(ctx, lval, &v, ei, ret);
2492 if(V_VT(rval) == VT_BOOL) {
2496 V_I4(&v) = V_BOOL(rval) ? 1 : 0;
2497 return equal_values(ctx, lval, &v, ei, ret);
2500 if(V_VT(lval) == VT_BOOL) {
2504 V_I4(&v) = V_BOOL(lval) ? 1 : 0;
2505 return equal_values(ctx, &v, rval, ei, ret);
2509 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2513 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2517 hres = equal_values(ctx, lval, &v, ei, ret);
2524 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2528 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2532 hres = equal_values(ctx, &v, rval, ei, ret);
2543 /* ECMA-262 3rd Edition 11.9.1 */
2544 static HRESULT interp_eq(exec_ctx_t *ctx)
2553 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2555 hres = equal_values(ctx->parser->script, l, r, ctx->ei, &b);
2561 return stack_push_bool(ctx, b);
2564 /* ECMA-262 3rd Edition 11.9.2 */
2565 static HRESULT interp_neq(exec_ctx_t *ctx)
2574 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2576 hres = equal_values(ctx->parser->script, l, r, ctx->ei, &b);
2582 return stack_push_bool(ctx, !b);
2585 /* ECMA-262 3rd Edition 11.9.4 */
2586 static HRESULT interp_eq2(exec_ctx_t *ctx)
2597 hres = equal2_values(r, l, &b);
2603 return stack_push_bool(ctx, b);
2606 /* ECMA-262 3rd Edition 11.9.5 */
2607 static HRESULT interp_neq2(exec_ctx_t *ctx)
2618 hres = equal2_values(r, l, &b);
2624 return stack_push_bool(ctx, !b);
2627 /* ECMA-262 3rd Edition 11.8.5 */
2628 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2630 VARIANT l, r, ln, rn;
2633 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2637 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2643 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2644 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2645 SysFreeString(V_BSTR(&l));
2646 SysFreeString(V_BSTR(&r));
2650 hres = to_number(ctx, &l, ei, &ln);
2653 hres = to_number(ctx, &r, ei, &rn);
2658 if(V_VT(&ln) == VT_I4 && V_VT(&rn) == VT_I4) {
2659 *ret = (V_I4(&ln) < V_I4(&rn)) ^ greater;
2661 DOUBLE ld = num_val(&ln);
2662 DOUBLE rd = num_val(&rn);
2664 *ret = !isnan(ld) && !isnan(rd) && ((ld < rd) ^ greater);
2670 /* ECMA-262 3rd Edition 11.8.1 */
2671 static HRESULT interp_lt(exec_ctx_t *ctx)
2680 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2682 hres = less_eval(ctx->parser->script, l, r, FALSE, ctx->ei, &b);
2688 return stack_push_bool(ctx, b);
2691 /* ECMA-262 3rd Edition 11.8.1 */
2692 static HRESULT interp_lteq(exec_ctx_t *ctx)
2701 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2703 hres = less_eval(ctx->parser->script, r, l, TRUE, ctx->ei, &b);
2709 return stack_push_bool(ctx, b);
2712 /* ECMA-262 3rd Edition 11.8.2 */
2713 static HRESULT interp_gt(exec_ctx_t *ctx)
2722 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2724 hres = less_eval(ctx->parser->script, r, l, FALSE, ctx->ei, &b);
2730 return stack_push_bool(ctx, b);
2733 /* ECMA-262 3rd Edition 11.8.4 */
2734 static HRESULT interp_gteq(exec_ctx_t *ctx)
2743 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2745 hres = less_eval(ctx->parser->script, l, r, TRUE, ctx->ei, &b);
2751 return stack_push_bool(ctx, b);
2754 /* ECMA-262 3rd Edition 11.4.8 */
2755 static HRESULT interp_bneg(exec_ctx_t *ctx)
2764 hres = to_int32(ctx->parser->script, v, ctx->ei, &i);
2771 return stack_push(ctx, &r);
2774 /* ECMA-262 3rd Edition 11.4.9 */
2775 static HRESULT interp_neg(exec_ctx_t *ctx)
2784 hres = to_boolean(v, &b);
2789 return stack_push_bool(ctx, !b);
2792 /* ECMA-262 3rd Edition 11.7.1 */
2793 static HRESULT interp_lshift(exec_ctx_t *ctx)
2799 hres = stack_pop_uint(ctx, &r);
2803 hres = stack_pop_int(ctx, &l);
2807 return stack_push_int(ctx, l << (r&0x1f));
2810 /* ECMA-262 3rd Edition 11.7.2 */
2811 static HRESULT interp_rshift(exec_ctx_t *ctx)
2817 hres = stack_pop_uint(ctx, &r);
2821 hres = stack_pop_int(ctx, &l);
2825 return stack_push_int(ctx, l >> (r&0x1f));
2828 /* ECMA-262 3rd Edition 11.7.3 */
2829 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2834 hres = stack_pop_uint(ctx, &r);
2838 hres = stack_pop_uint(ctx, &l);
2842 return stack_push_int(ctx, l >> (r&0x1f));
2845 /* ECMA-262 3rd Edition 11.13.1 */
2846 static HRESULT interp_assign(exec_ctx_t *ctx)
2856 disp = stack_pop_objid(ctx, &id);
2859 return throw_reference_error(ctx->parser->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2861 hres = disp_propput(ctx->parser->script, disp, id, v, ctx->ei, NULL/*FIXME*/);
2862 IDispatch_Release(disp);
2868 return stack_push(ctx, v);
2871 static HRESULT interp_undefined(exec_ctx_t *ctx)
2877 V_VT(&v) = VT_EMPTY;
2878 return stack_push(ctx, &v);
2881 static HRESULT interp_jmp(exec_ctx_t *ctx)
2883 const unsigned arg = ctx->parser->code->instrs[ctx->ip].arg1.uint;
2891 static HRESULT interp_pop(exec_ctx_t *ctx)
2899 static HRESULT interp_ret(exec_ctx_t *ctx)
2907 static HRESULT interp_tree(exec_ctx_t *ctx)
2909 instr_t *instr = ctx->parser->code->instrs+ctx->ip;
2915 hres = stat_eval(ctx->parser->script, instr->arg1.stat, ctx->rt, &v);
2919 return stack_push(ctx, &v);
2922 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2924 static const op_func_t op_funcs[] = {
2925 #define X(x,a,b,c) interp_##x,
2930 static const unsigned op_move[] = {
2931 #define X(a,x,b,c) x,
2936 HRESULT compiled_statement_eval(script_ctx_t *ctx, statement_t *stat, return_type_t *rt, VARIANT *ret)
2938 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2939 unsigned prev_ip, prev_top;
2940 return_type_t *prev_rt;
2941 jsexcept_t *prev_ei;
2943 HRESULT hres = S_OK;
2947 if(stat->instr_off == -1) {
2948 hres = compile_subscript_stat(exec_ctx->parser, stat, FALSE, &stat->instr_off);
2953 prev_rt = exec_ctx->rt;
2954 prev_top = exec_ctx->top;
2955 prev_ip = exec_ctx->ip;
2956 prev_ei = exec_ctx->ei;
2957 exec_ctx->ip = stat->instr_off;
2959 exec_ctx->ei = &rt->ei;
2961 while(exec_ctx->ip != -1 && exec_ctx->rt->type == RT_NORMAL) {
2962 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
2963 hres = op_funcs[op](exec_ctx);
2966 exec_ctx->ip += op_move[op];
2969 exec_ctx->rt = prev_rt;
2970 exec_ctx->ip = prev_ip;
2971 exec_ctx->ei = prev_ei;
2974 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2978 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2980 if(exec_ctx->top == prev_top)
2981 V_VT(ret) = VT_EMPTY;
2983 *ret = *stack_pop(exec_ctx);
2987 static HRESULT expr_eval(script_ctx_t *ctx, expression_t *expr, DWORD flags, jsexcept_t *ei, VARIANT *ret)
2989 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2990 unsigned prev_ip, prev_top;
2991 jsexcept_t *prev_ei;
2993 HRESULT hres = S_OK;
2997 if(expr->instr_off == -1) {
2998 hres = compile_subscript(ctx->exec_ctx->parser, expr, !(flags & EXPR_NOVAL), &expr->instr_off);
3003 prev_top = exec_ctx->top;
3004 prev_ip = exec_ctx->ip;
3005 prev_ei = exec_ctx->ei;
3006 exec_ctx->ip = expr->instr_off;
3009 while(exec_ctx->ip != -1) {
3010 op = exec_ctx->parser->code->instrs[exec_ctx->ip].op;
3011 hres = op_funcs[op](exec_ctx);
3014 exec_ctx->ip += op_move[op];
3017 exec_ctx->ip = prev_ip;
3018 exec_ctx->ei = prev_ei;
3021 stack_popn(exec_ctx, exec_ctx->top-prev_top);
3025 assert(exec_ctx->top == prev_top+1 || ((flags&EXPR_NOVAL) && exec_ctx->top == prev_top));
3027 if(exec_ctx->top == prev_top)
3028 V_VT(ret) = VT_EMPTY;
3030 *ret = *stack_pop(exec_ctx);