msvcp90: Added num_put<char> class stub.
[wine/multimedia.git] / dlls / jscript / engine.c
blob3b20e6aede4403adcdc519075f415386f0efc74d
1 /*
2 * Copyright 2008,2011 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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
23 #include <assert.h>
25 #include "jscript.h"
26 #include "engine.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
32 static const WCHAR booleanW[] = {'b','o','o','l','e','a','n',0};
33 static const WCHAR functionW[] = {'f','u','n','c','t','i','o','n',0};
34 static const WCHAR numberW[] = {'n','u','m','b','e','r',0};
35 static const WCHAR objectW[] = {'o','b','j','e','c','t',0};
36 static const WCHAR stringW[] = {'s','t','r','i','n','g',0};
37 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
38 static const WCHAR unknownW[] = {'u','n','k','n','o','w','n',0};
40 struct _except_frame_t {
41 unsigned stack_top;
42 scope_chain_t *scope;
43 unsigned catch_off;
44 BSTR ident;
46 except_frame_t *next;
49 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
51 if(!ctx->stack_size) {
52 ctx->stack = heap_alloc(16*sizeof(VARIANT));
53 if(!ctx->stack)
54 return E_OUTOFMEMORY;
55 ctx->stack_size = 16;
56 }else if(ctx->stack_size == ctx->top) {
57 VARIANT *new_stack;
59 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(VARIANT));
60 if(!new_stack) {
61 VariantClear(v);
62 return E_OUTOFMEMORY;
65 ctx->stack = new_stack;
66 ctx->stack_size *= 2;
69 ctx->stack[ctx->top++] = *v;
70 return S_OK;
73 static HRESULT stack_push_bool(exec_ctx_t *ctx, BOOL b)
75 VARIANT v;
77 V_VT(&v) = VT_BOOL;
78 V_BOOL(&v) = b ? VARIANT_TRUE : VARIANT_FALSE;
79 return stack_push(ctx, &v);
82 static inline HRESULT stack_push_number(exec_ctx_t *ctx, double number)
84 VARIANT v;
86 num_set_val(&v, number);
87 return stack_push(ctx, &v);
90 static inline HRESULT stack_push_int(exec_ctx_t *ctx, INT n)
92 VARIANT v;
94 V_VT(&v) = VT_I4;
95 V_I4(&v) = n;
96 return stack_push(ctx, &v);
99 static inline HRESULT stack_push_string(exec_ctx_t *ctx, const WCHAR *str)
101 VARIANT v;
103 V_VT(&v) = VT_BSTR;
104 V_BSTR(&v) = SysAllocString(str);
105 return V_BSTR(&v) ? stack_push(ctx, &v) : E_OUTOFMEMORY;
108 static HRESULT stack_push_objid(exec_ctx_t *ctx, IDispatch *disp, DISPID id)
110 VARIANT v;
111 HRESULT hres;
113 V_VT(&v) = VT_DISPATCH;
114 V_DISPATCH(&v) = disp;
115 hres = stack_push(ctx, &v);
116 if(FAILED(hres))
117 return hres;
119 V_VT(&v) = VT_INT;
120 V_INT(&v) = id;
121 return stack_push(ctx, &v);
124 static inline VARIANT *stack_top(exec_ctx_t *ctx)
126 assert(ctx->top);
127 return ctx->stack + ctx->top-1;
130 static inline VARIANT *stack_topn(exec_ctx_t *ctx, unsigned n)
132 assert(ctx->top > n);
133 return ctx->stack + ctx->top-1-n;
136 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
138 assert(ctx->top);
139 return ctx->stack + --ctx->top;
142 static void stack_popn(exec_ctx_t *ctx, unsigned n)
144 while(n--)
145 VariantClear(stack_pop(ctx));
148 static HRESULT stack_pop_number(exec_ctx_t *ctx, double *r)
150 VARIANT *v;
151 HRESULT hres;
153 v = stack_pop(ctx);
154 hres = to_number(ctx->script, v, ctx->ei, r);
155 VariantClear(v);
156 return hres;
159 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
161 VARIANT *v;
162 HRESULT hres;
164 v = stack_pop(ctx);
165 if(V_VT(v) == VT_DISPATCH) {
166 if(!V_DISPATCH(v))
167 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
168 *r = V_DISPATCH(v);
169 return S_OK;
172 hres = to_object(ctx->script, v, r);
173 VariantClear(v);
174 return hres;
177 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
179 return to_int32(ctx->script, stack_pop(ctx), ctx->ei, r);
182 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
184 return to_uint32(ctx->script, stack_pop(ctx), ctx->ei, r);
187 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
189 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
191 *id = V_INT(stack_pop(ctx));
192 return V_DISPATCH(stack_pop(ctx));
195 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
197 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
199 *id = V_INT(stack_topn(ctx, n));
200 return V_DISPATCH(stack_topn(ctx, n+1));
203 static void exprval_release(exprval_t *val)
205 switch(val->type) {
206 case EXPRVAL_VARIANT:
207 if(V_VT(&val->u.var) != VT_EMPTY)
208 VariantClear(&val->u.var);
209 return;
210 case EXPRVAL_IDREF:
211 if(val->u.idref.disp)
212 IDispatch_Release(val->u.idref.disp);
213 return;
214 case EXPRVAL_INVALID:
215 return;
219 /* ECMA-262 3rd Edition 8.7.1 */
220 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
222 V_VT(ret) = VT_EMPTY;
224 switch(val->type) {
225 case EXPRVAL_VARIANT:
226 return VariantCopy(ret, &val->u.var);
227 case EXPRVAL_IDREF:
228 if(!val->u.idref.disp) {
229 FIXME("throw ReferenceError\n");
230 return E_FAIL;
233 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei);
234 case EXPRVAL_INVALID:
235 assert(0);
238 ERR("type %d\n", val->type);
239 return E_FAIL;
242 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
244 if(val->type == EXPRVAL_VARIANT) {
245 *ret = val->u.var;
246 V_VT(&val->u.var) = VT_EMPTY;
247 return S_OK;
250 return exprval_value(ctx, val, ei, ret);
253 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
255 val->type = EXPRVAL_IDREF;
256 val->u.idref.disp = disp;
257 val->u.idref.id = id;
259 if(disp)
260 IDispatch_AddRef(disp);
263 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
265 scope_chain_t *new_scope;
267 new_scope = heap_alloc(sizeof(scope_chain_t));
268 if(!new_scope)
269 return E_OUTOFMEMORY;
271 new_scope->ref = 1;
273 jsdisp_addref(obj);
274 new_scope->obj = obj;
276 if(scope) {
277 scope_addref(scope);
278 new_scope->next = scope;
279 }else {
280 new_scope->next = NULL;
283 *ret = new_scope;
284 return S_OK;
287 static void scope_pop(scope_chain_t **scope)
289 scope_chain_t *tmp;
291 tmp = *scope;
292 *scope = tmp->next;
293 scope_release(tmp);
296 void scope_release(scope_chain_t *scope)
298 if(--scope->ref)
299 return;
301 if(scope->next)
302 scope_release(scope->next);
304 jsdisp_release(scope->obj);
305 heap_free(scope);
308 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
309 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
311 exec_ctx_t *ctx;
313 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
314 if(!ctx)
315 return E_OUTOFMEMORY;
317 ctx->ref = 1;
318 ctx->is_global = is_global;
320 if(this_obj)
321 ctx->this_obj = this_obj;
322 else if(script_ctx->host_global)
323 ctx->this_obj = script_ctx->host_global;
324 else
325 ctx->this_obj = to_disp(script_ctx->global);
326 IDispatch_AddRef(ctx->this_obj);
328 jsdisp_addref(var_disp);
329 ctx->var_disp = var_disp;
331 script_addref(script_ctx);
332 ctx->script = script_ctx;
334 if(scope) {
335 scope_addref(scope);
336 ctx->scope_chain = scope;
339 *ret = ctx;
340 return S_OK;
343 void exec_release(exec_ctx_t *ctx)
345 if(--ctx->ref)
346 return;
348 if(ctx->scope_chain)
349 scope_release(ctx->scope_chain);
350 if(ctx->var_disp)
351 jsdisp_release(ctx->var_disp);
352 if(ctx->this_obj)
353 IDispatch_Release(ctx->this_obj);
354 if(ctx->script)
355 script_release(ctx->script);
356 heap_free(ctx->stack);
357 heap_free(ctx);
360 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
362 IDispatchEx *dispex;
363 HRESULT hres;
365 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
366 if(FAILED(hres)) {
367 TRACE("using IDispatch\n");
369 *id = 0;
370 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
373 *id = 0;
374 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
375 IDispatchEx_Release(dispex);
376 return hres;
379 static inline BOOL is_null(const VARIANT *v)
381 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
384 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
386 IObjectIdentity *identity;
387 IUnknown *unk1, *unk2;
388 HRESULT hres;
390 if(disp1 == disp2) {
391 *ret = TRUE;
392 return S_OK;
395 if(!disp1 || !disp2) {
396 *ret = FALSE;
397 return S_OK;
400 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
401 if(FAILED(hres))
402 return hres;
404 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
405 if(FAILED(hres)) {
406 IUnknown_Release(unk1);
407 return hres;
410 if(unk1 == unk2) {
411 *ret = TRUE;
412 }else {
413 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
414 if(SUCCEEDED(hres)) {
415 hres = IObjectIdentity_IsEqualObject(identity, unk2);
416 IObjectIdentity_Release(identity);
417 *ret = hres == S_OK;
418 }else {
419 *ret = FALSE;
423 IUnknown_Release(unk1);
424 IUnknown_Release(unk2);
425 return S_OK;
428 /* ECMA-262 3rd Edition 11.9.6 */
429 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
431 TRACE("\n");
433 if(V_VT(lval) != V_VT(rval)) {
434 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
435 *ret = num_val(lval) == num_val(rval);
436 else if(is_null(lval))
437 *ret = is_null(rval);
438 else
439 *ret = FALSE;
440 return S_OK;
443 switch(V_VT(lval)) {
444 case VT_EMPTY:
445 case VT_NULL:
446 *ret = VARIANT_TRUE;
447 break;
448 case VT_I4:
449 *ret = V_I4(lval) == V_I4(rval);
450 break;
451 case VT_R8:
452 *ret = V_R8(lval) == V_R8(rval);
453 break;
454 case VT_BSTR:
455 if(!V_BSTR(lval))
456 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
457 else if(!V_BSTR(rval))
458 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
459 else
460 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
461 break;
462 case VT_DISPATCH:
463 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
464 case VT_BOOL:
465 *ret = !V_BOOL(lval) == !V_BOOL(rval);
466 break;
467 default:
468 FIXME("unimplemented vt %d\n", V_VT(lval));
469 return E_NOTIMPL;
472 return S_OK;
475 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
477 named_item_t *item;
478 DISPID id;
479 HRESULT hres;
481 for(item = ctx->named_items; item; item = item->next) {
482 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
483 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
484 if(SUCCEEDED(hres)) {
485 if(ret)
486 exprval_set_idref(ret, item->disp, id);
487 return TRUE;
492 return FALSE;
495 /* ECMA-262 3rd Edition 10.1.4 */
496 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
498 scope_chain_t *scope;
499 named_item_t *item;
500 DISPID id = 0;
501 HRESULT hres;
503 TRACE("%s\n", debugstr_w(identifier));
505 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
506 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
507 if(SUCCEEDED(hres)) {
508 exprval_set_idref(ret, to_disp(scope->obj), id);
509 return S_OK;
513 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
514 if(SUCCEEDED(hres)) {
515 exprval_set_idref(ret, to_disp(ctx->global), id);
516 return S_OK;
519 for(item = ctx->named_items; item; item = item->next) {
520 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
521 if(!item->disp) {
522 IUnknown *unk;
524 if(!ctx->site)
525 break;
527 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
528 SCRIPTINFO_IUNKNOWN, &unk, NULL);
529 if(FAILED(hres)) {
530 WARN("GetItemInfo failed: %08x\n", hres);
531 break;
534 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
535 IUnknown_Release(unk);
536 if(FAILED(hres)) {
537 WARN("object does not implement IDispatch\n");
538 break;
542 ret->type = EXPRVAL_VARIANT;
543 V_VT(&ret->u.var) = VT_DISPATCH;
544 V_DISPATCH(&ret->u.var) = item->disp;
545 IDispatch_AddRef(item->disp);
546 return S_OK;
550 if(lookup_global_members(ctx, identifier, ret))
551 return S_OK;
553 ret->type = EXPRVAL_INVALID;
554 return S_OK;
557 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
558 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
561 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
562 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
565 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
566 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
569 static inline const WCHAR *get_op_str(exec_ctx_t *ctx, int i){
570 return ctx->code->instrs[ctx->ip].u.arg[i].str;
573 static inline double get_op_double(exec_ctx_t *ctx){
574 return ctx->code->instrs[ctx->ip].u.dbl;
577 /* ECMA-262 3rd Edition 12.2 */
578 static HRESULT interp_var_set(exec_ctx_t *ctx)
580 const BSTR name = get_op_bstr(ctx, 0);
581 VARIANT *v;
582 HRESULT hres;
584 TRACE("%s\n", debugstr_w(name));
586 v = stack_pop(ctx);
587 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei);
588 VariantClear(v);
589 return hres;
592 /* ECMA-262 3rd Edition 12.6.4 */
593 static HRESULT interp_forin(exec_ctx_t *ctx)
595 const HRESULT arg = get_op_uint(ctx, 0);
596 IDispatch *var_obj, *obj = NULL;
597 IDispatchEx *dispex;
598 DISPID id, var_id;
599 BSTR name = NULL;
600 VARIANT *val;
601 HRESULT hres;
603 TRACE("\n");
605 val = stack_pop(ctx);
607 assert(V_VT(stack_top(ctx)) == VT_I4);
608 id = V_I4(stack_top(ctx));
610 var_obj = stack_topn_objid(ctx, 1, &var_id);
611 if(!var_obj) {
612 FIXME("invalid ref\n");
613 VariantClear(val);
614 return E_FAIL;
617 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
618 obj = V_DISPATCH(stack_topn(ctx, 3));
620 if(obj) {
621 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
622 if(SUCCEEDED(hres)) {
623 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
624 if(hres == S_OK)
625 hres = IDispatchEx_GetMemberName(dispex, id, &name);
626 IDispatchEx_Release(dispex);
627 if(FAILED(hres)) {
628 VariantClear(val);
629 return hres;
631 }else {
632 TRACE("No IDispatchEx\n");
636 if(name) {
637 VARIANT v;
639 VariantClear(val);
641 V_I4(stack_top(ctx)) = id;
643 V_VT(&v) = VT_BSTR;
644 V_BSTR(&v) = name;
645 hres = disp_propput(ctx->script, var_obj, var_id, &v, ctx->ei);
646 SysFreeString(name);
647 if(FAILED(hres))
648 return hres;
650 ctx->ip++;
651 }else {
652 stack_popn(ctx, 4);
653 ctx->ip = arg;
654 return stack_push(ctx, val);
656 return S_OK;
659 /* ECMA-262 3rd Edition 12.10 */
660 static HRESULT interp_push_scope(exec_ctx_t *ctx)
662 IDispatch *disp;
663 jsdisp_t *obj;
664 VARIANT *v;
665 HRESULT hres;
667 TRACE("\n");
669 v = stack_pop(ctx);
670 hres = to_object(ctx->script, v, &disp);
671 VariantClear(v);
672 if(FAILED(hres))
673 return hres;
675 obj = to_jsdisp(disp);
676 if(!obj) {
677 IDispatch_Release(disp);
678 FIXME("disp is not jsdisp\n");
679 return E_NOTIMPL;
682 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
683 jsdisp_release(obj);
684 return hres;
687 /* ECMA-262 3rd Edition 12.10 */
688 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
690 TRACE("\n");
692 scope_pop(&ctx->scope_chain);
693 return S_OK;
696 /* ECMA-262 3rd Edition 12.13 */
697 static HRESULT interp_case(exec_ctx_t *ctx)
699 const unsigned arg = get_op_uint(ctx, 0);
700 VARIANT *v;
701 BOOL b;
702 HRESULT hres;
704 TRACE("\n");
706 v = stack_pop(ctx);
707 hres = equal2_values(stack_top(ctx), v, &b);
708 VariantClear(v);
709 if(FAILED(hres))
710 return hres;
712 if(b) {
713 stack_popn(ctx, 1);
714 ctx->ip = arg;
715 }else {
716 ctx->ip++;
718 return S_OK;
721 /* ECMA-262 3rd Edition 12.13 */
722 static HRESULT interp_throw(exec_ctx_t *ctx)
724 TRACE("\n");
726 ctx->ei->var = *stack_pop(ctx);
727 return DISP_E_EXCEPTION;
730 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
732 const HRESULT arg = get_op_uint(ctx, 0);
734 TRACE("%08x\n", arg);
736 return throw_reference_error(ctx->script, ctx->ei, arg, NULL);
739 static HRESULT interp_throw_type(exec_ctx_t *ctx)
741 const HRESULT hres = get_op_uint(ctx, 0);
742 const WCHAR *str = get_op_str(ctx, 1);
744 TRACE("%08x %s\n", hres, debugstr_w(str));
746 return throw_type_error(ctx->script, ctx->ei, hres, str);
749 /* ECMA-262 3rd Edition 12.14 */
750 static HRESULT interp_push_except(exec_ctx_t *ctx)
752 const unsigned arg1 = get_op_uint(ctx, 0);
753 const BSTR arg2 = get_op_bstr(ctx, 1);
754 except_frame_t *except;
755 unsigned stack_top;
757 TRACE("\n");
759 stack_top = ctx->top;
761 if(!arg2) {
762 HRESULT hres;
764 hres = stack_push_bool(ctx, TRUE);
765 if(FAILED(hres))
766 return hres;
767 hres = stack_push_bool(ctx, TRUE);
768 if(FAILED(hres))
769 return hres;
772 except = heap_alloc(sizeof(*except));
773 if(!except)
774 return E_OUTOFMEMORY;
776 except->stack_top = stack_top;
777 except->scope = ctx->scope_chain;
778 except->catch_off = arg1;
779 except->ident = arg2;
780 except->next = ctx->except_frame;
781 ctx->except_frame = except;
782 return S_OK;
785 /* ECMA-262 3rd Edition 12.14 */
786 static HRESULT interp_pop_except(exec_ctx_t *ctx)
788 except_frame_t *except;
790 TRACE("\n");
792 except = ctx->except_frame;
793 assert(except != NULL);
795 ctx->except_frame = except->next;
796 heap_free(except);
797 return S_OK;
800 /* ECMA-262 3rd Edition 12.14 */
801 static HRESULT interp_end_finally(exec_ctx_t *ctx)
803 VARIANT *v;
805 TRACE("\n");
807 v = stack_pop(ctx);
809 assert(V_VT(stack_top(ctx)) == VT_BOOL);
810 if(!V_BOOL(stack_top(ctx))) {
811 TRACE("passing exception\n");
813 VariantClear(v);
814 stack_popn(ctx, 1);
815 ctx->ei->var = *stack_pop(ctx);
816 return DISP_E_EXCEPTION;
819 stack_popn(ctx, 2);
820 return stack_push(ctx, v);
823 /* ECMA-262 3rd Edition 13 */
824 static HRESULT interp_func(exec_ctx_t *ctx)
826 unsigned func_idx = get_op_uint(ctx, 0);
827 jsdisp_t *dispex;
828 VARIANT v;
829 HRESULT hres;
831 TRACE("%d\n", func_idx);
833 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
834 ctx->scope_chain, &dispex);
835 if(FAILED(hres))
836 return hres;
838 var_set_jsdisp(&v, dispex);
839 return stack_push(ctx, &v);
842 /* ECMA-262 3rd Edition 11.2.1 */
843 static HRESULT interp_array(exec_ctx_t *ctx)
845 VARIANT v, *namev;
846 IDispatch *obj;
847 DISPID id;
848 BSTR name;
849 HRESULT hres;
851 TRACE("\n");
853 namev = stack_pop(ctx);
855 hres = stack_pop_object(ctx, &obj);
856 if(FAILED(hres)) {
857 VariantClear(namev);
858 return hres;
861 hres = to_string(ctx->script, namev, ctx->ei, &name);
862 VariantClear(namev);
863 if(FAILED(hres)) {
864 IDispatch_Release(obj);
865 return hres;
868 hres = disp_get_id(ctx->script, obj, name, 0, &id);
869 SysFreeString(name);
870 if(SUCCEEDED(hres)) {
871 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
872 }else if(hres == DISP_E_UNKNOWNNAME) {
873 V_VT(&v) = VT_EMPTY;
874 hres = S_OK;
876 IDispatch_Release(obj);
877 if(FAILED(hres))
878 return hres;
880 return stack_push(ctx, &v);
883 /* ECMA-262 3rd Edition 11.2.1 */
884 static HRESULT interp_member(exec_ctx_t *ctx)
886 const BSTR arg = get_op_bstr(ctx, 0);
887 IDispatch *obj;
888 VARIANT v;
889 DISPID id;
890 HRESULT hres;
892 TRACE("\n");
894 hres = stack_pop_object(ctx, &obj);
895 if(FAILED(hres))
896 return hres;
898 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
899 if(SUCCEEDED(hres)) {
900 V_VT(&v) = VT_EMPTY;
901 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
902 }else if(hres == DISP_E_UNKNOWNNAME) {
903 V_VT(&v) = VT_EMPTY;
904 hres = S_OK;
906 IDispatch_Release(obj);
907 if(FAILED(hres))
908 return hres;
910 return stack_push(ctx, &v);
913 /* ECMA-262 3rd Edition 11.2.1 */
914 static HRESULT interp_memberid(exec_ctx_t *ctx)
916 const unsigned arg = get_op_uint(ctx, 0);
917 VARIANT *objv, *namev;
918 IDispatch *obj;
919 BSTR name;
920 DISPID id;
921 HRESULT hres;
923 TRACE("%x\n", arg);
925 namev = stack_pop(ctx);
926 objv = stack_pop(ctx);
928 hres = to_object(ctx->script, objv, &obj);
929 VariantClear(objv);
930 if(SUCCEEDED(hres)) {
931 hres = to_string(ctx->script, namev, ctx->ei, &name);
932 if(FAILED(hres))
933 IDispatch_Release(obj);
935 VariantClear(namev);
936 if(FAILED(hres))
937 return hres;
939 hres = disp_get_id(ctx->script, obj, name, arg, &id);
940 SysFreeString(name);
941 if(FAILED(hres)) {
942 IDispatch_Release(obj);
943 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
944 obj = NULL;
945 id = JS_E_INVALID_PROPERTY;
946 }else {
947 return hres;
951 return stack_push_objid(ctx, obj, id);
954 /* ECMA-262 3rd Edition 11.2.1 */
955 static HRESULT interp_refval(exec_ctx_t *ctx)
957 IDispatch *disp;
958 VARIANT v;
959 DISPID id;
960 HRESULT hres;
962 TRACE("\n");
964 disp = stack_topn_objid(ctx, 0, &id);
965 if(!disp)
966 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
968 hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
969 if(FAILED(hres))
970 return hres;
972 return stack_push(ctx, &v);
975 static void jsstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
977 VARIANT tmp;
978 unsigned i;
980 dp->cArgs = arg_cnt;
981 dp->rgdispidNamedArgs = NULL;
982 dp->cNamedArgs = 0;
984 assert(ctx->top >= arg_cnt);
986 for(i=1; i*2 <= arg_cnt; i++) {
987 tmp = ctx->stack[ctx->top-i];
988 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
989 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
992 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
995 /* ECMA-262 3rd Edition 11.2.2 */
996 static HRESULT interp_new(exec_ctx_t *ctx)
998 const unsigned arg = get_op_uint(ctx, 0);
999 VARIANT *constr, v;
1000 DISPPARAMS dp;
1001 HRESULT hres;
1003 TRACE("%d\n", arg);
1005 constr = stack_topn(ctx, arg);
1007 /* NOTE: Should use to_object here */
1009 if(V_VT(constr) == VT_NULL)
1010 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1011 else if(V_VT(constr) != VT_DISPATCH)
1012 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
1013 else if(!V_DISPATCH(constr))
1014 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1016 jsstack_to_dp(ctx, arg, &dp);
1017 hres = disp_call_value(ctx->script, V_DISPATCH(constr), NULL, DISPATCH_CONSTRUCT, &dp, &v, ctx->ei);
1018 if(FAILED(hres))
1019 return hres;
1021 stack_popn(ctx, arg+1);
1022 return stack_push(ctx, &v);
1025 /* ECMA-262 3rd Edition 11.2.3 */
1026 static HRESULT interp_call(exec_ctx_t *ctx)
1028 const unsigned argn = get_op_uint(ctx, 0);
1029 const int do_ret = get_op_int(ctx, 1);
1030 VARIANT v, *objv;
1031 DISPPARAMS dp;
1032 HRESULT hres;
1034 TRACE("%d %d\n", argn, do_ret);
1036 objv = stack_topn(ctx, argn);
1037 if(V_VT(objv) != VT_DISPATCH)
1038 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1040 jsstack_to_dp(ctx, argn, &dp);
1041 hres = disp_call_value(ctx->script, V_DISPATCH(objv), NULL, DISPATCH_METHOD, &dp,
1042 do_ret ? &v : NULL, ctx->ei);
1043 if(FAILED(hres))
1044 return hres;
1046 stack_popn(ctx, argn+1);
1047 return do_ret ? stack_push(ctx, &v) : S_OK;
1051 /* ECMA-262 3rd Edition 11.2.3 */
1052 static HRESULT interp_call_member(exec_ctx_t *ctx)
1054 const unsigned argn = get_op_uint(ctx, 0);
1055 const int do_ret = get_op_int(ctx, 1);
1056 IDispatch *obj;
1057 DISPPARAMS dp;
1058 VARIANT v;
1059 DISPID id;
1060 HRESULT hres;
1062 TRACE("%d %d\n", argn, do_ret);
1064 obj = stack_topn_objid(ctx, argn, &id);
1065 if(!obj)
1066 return throw_type_error(ctx->script, ctx->ei, id, NULL);
1068 jsstack_to_dp(ctx, argn, &dp);
1069 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, &dp, do_ret ? &v : NULL, ctx->ei);
1070 if(FAILED(hres))
1071 return hres;
1073 stack_popn(ctx, argn+2);
1074 return do_ret ? stack_push(ctx, &v) : S_OK;
1078 /* ECMA-262 3rd Edition 11.1.1 */
1079 static HRESULT interp_this(exec_ctx_t *ctx)
1081 VARIANT v;
1083 TRACE("\n");
1085 V_VT(&v) = VT_DISPATCH;
1086 V_DISPATCH(&v) = ctx->this_obj;
1087 IDispatch_AddRef(ctx->this_obj);
1088 return stack_push(ctx, &v);
1091 /* ECMA-262 3rd Edition 10.1.4 */
1092 static HRESULT interp_ident(exec_ctx_t *ctx)
1094 const BSTR arg = get_op_bstr(ctx, 0);
1095 exprval_t exprval;
1096 VARIANT v;
1097 HRESULT hres;
1099 TRACE("%s\n", debugstr_w(arg));
1101 hres = identifier_eval(ctx->script, arg, &exprval);
1102 if(FAILED(hres))
1103 return hres;
1105 if(exprval.type == EXPRVAL_INVALID)
1106 return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1108 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1109 exprval_release(&exprval);
1110 if(FAILED(hres))
1111 return hres;
1113 return stack_push(ctx, &v);
1116 /* ECMA-262 3rd Edition 10.1.4 */
1117 static HRESULT interp_identid(exec_ctx_t *ctx)
1119 const BSTR arg = get_op_bstr(ctx, 0);
1120 const unsigned flags = get_op_uint(ctx, 1);
1121 exprval_t exprval;
1122 HRESULT hres;
1124 TRACE("%s %x\n", debugstr_w(arg), flags);
1126 hres = identifier_eval(ctx->script, arg, &exprval);
1127 if(FAILED(hres))
1128 return hres;
1130 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1131 DISPID id;
1133 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1134 if(FAILED(hres))
1135 return hres;
1137 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1140 if(exprval.type != EXPRVAL_IDREF) {
1141 WARN("invalid ref\n");
1142 exprval_release(&exprval);
1143 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1146 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1149 /* ECMA-262 3rd Edition 7.8.1 */
1150 static HRESULT interp_null(exec_ctx_t *ctx)
1152 VARIANT v;
1154 TRACE("\n");
1156 V_VT(&v) = VT_NULL;
1157 return stack_push(ctx, &v);
1160 /* ECMA-262 3rd Edition 7.8.2 */
1161 static HRESULT interp_bool(exec_ctx_t *ctx)
1163 const int arg = get_op_int(ctx, 0);
1165 TRACE("%s\n", arg ? "true" : "false");
1167 return stack_push_bool(ctx, arg);
1170 /* ECMA-262 3rd Edition 7.8.3 */
1171 static HRESULT interp_int(exec_ctx_t *ctx)
1173 const int arg = get_op_int(ctx, 0);
1174 VARIANT v;
1176 TRACE("%d\n", arg);
1178 V_VT(&v) = VT_I4;
1179 V_I4(&v) = arg;
1180 return stack_push(ctx, &v);
1183 /* ECMA-262 3rd Edition 7.8.3 */
1184 static HRESULT interp_double(exec_ctx_t *ctx)
1186 const double arg = get_op_double(ctx);
1188 TRACE("%lf\n", arg);
1190 return stack_push_number(ctx, arg);
1193 /* ECMA-262 3rd Edition 7.8.4 */
1194 static HRESULT interp_str(exec_ctx_t *ctx)
1196 const WCHAR *str = get_op_str(ctx, 0);
1197 VARIANT v;
1199 TRACE("%s\n", debugstr_w(str));
1201 V_VT(&v) = VT_BSTR;
1202 V_BSTR(&v) = SysAllocString(str);
1203 if(!V_BSTR(&v))
1204 return E_OUTOFMEMORY;
1206 return stack_push(ctx, &v);
1209 /* ECMA-262 3rd Edition 7.8 */
1210 static HRESULT interp_regexp(exec_ctx_t *ctx)
1212 const WCHAR *source = get_op_str(ctx, 0);
1213 const unsigned flags = get_op_uint(ctx, 1);
1214 jsdisp_t *regexp;
1215 VARIANT v;
1216 HRESULT hres;
1218 TRACE("%s %x\n", debugstr_w(source), flags);
1220 hres = create_regexp(ctx->script, source, strlenW(source), flags, &regexp);
1221 if(FAILED(hres))
1222 return hres;
1224 var_set_jsdisp(&v, regexp);
1225 return stack_push(ctx, &v);
1228 /* ECMA-262 3rd Edition 11.1.4 */
1229 static HRESULT interp_carray(exec_ctx_t *ctx)
1231 const unsigned arg = get_op_uint(ctx, 0);
1232 jsdisp_t *array;
1233 VARIANT *v, r;
1234 unsigned i;
1235 HRESULT hres;
1237 TRACE("%u\n", arg);
1239 hres = create_array(ctx->script, arg, &array);
1240 if(FAILED(hres))
1241 return hres;
1243 i = arg;
1244 while(i--) {
1245 v = stack_pop(ctx);
1246 hres = jsdisp_propput_idx(array, i, v, ctx->ei);
1247 VariantClear(v);
1248 if(FAILED(hres)) {
1249 jsdisp_release(array);
1250 return hres;
1254 var_set_jsdisp(&r, array);
1255 return stack_push(ctx, &r);
1258 /* ECMA-262 3rd Edition 11.1.5 */
1259 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1261 jsdisp_t *obj;
1262 VARIANT v;
1263 HRESULT hres;
1265 TRACE("\n");
1267 hres = create_object(ctx->script, NULL, &obj);
1268 if(FAILED(hres))
1269 return hres;
1271 var_set_jsdisp(&v, obj);
1272 return stack_push(ctx, &v);
1275 /* ECMA-262 3rd Edition 11.1.5 */
1276 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1278 const BSTR name = get_op_bstr(ctx, 0);
1279 jsdisp_t *obj;
1280 VARIANT *v;
1281 HRESULT hres;
1283 TRACE("%s\n", debugstr_w(name));
1285 v = stack_pop(ctx);
1287 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1288 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1290 hres = jsdisp_propput_name(obj, name, v, ctx->ei);
1291 VariantClear(v);
1292 return hres;
1295 /* ECMA-262 3rd Edition 11.11 */
1296 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1298 const unsigned arg = get_op_uint(ctx, 0);
1299 VARIANT_BOOL b;
1300 HRESULT hres;
1302 TRACE("\n");
1304 hres = to_boolean(stack_top(ctx), &b);
1305 if(FAILED(hres))
1306 return hres;
1308 if(b) {
1309 ctx->ip = arg;
1310 }else {
1311 stack_popn(ctx, 1);
1312 ctx->ip++;
1314 return S_OK;
1317 /* ECMA-262 3rd Edition 11.11 */
1318 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1320 const unsigned arg = get_op_uint(ctx, 0);
1321 VARIANT_BOOL b;
1322 HRESULT hres;
1324 TRACE("\n");
1326 hres = to_boolean(stack_top(ctx), &b);
1327 if(FAILED(hres))
1328 return hres;
1330 if(b) {
1331 stack_popn(ctx, 1);
1332 ctx->ip++;
1333 }else {
1334 ctx->ip = arg;
1336 return S_OK;
1339 /* ECMA-262 3rd Edition 11.10 */
1340 static HRESULT interp_or(exec_ctx_t *ctx)
1342 INT l, r;
1343 HRESULT hres;
1345 TRACE("\n");
1347 hres = stack_pop_int(ctx, &r);
1348 if(FAILED(hres))
1349 return hres;
1351 hres = stack_pop_int(ctx, &l);
1352 if(FAILED(hres))
1353 return hres;
1355 return stack_push_int(ctx, l|r);
1358 /* ECMA-262 3rd Edition 11.10 */
1359 static HRESULT interp_xor(exec_ctx_t *ctx)
1361 INT l, r;
1362 HRESULT hres;
1364 TRACE("\n");
1366 hres = stack_pop_int(ctx, &r);
1367 if(FAILED(hres))
1368 return hres;
1370 hres = stack_pop_int(ctx, &l);
1371 if(FAILED(hres))
1372 return hres;
1374 return stack_push_int(ctx, l^r);
1377 /* ECMA-262 3rd Edition 11.10 */
1378 static HRESULT interp_and(exec_ctx_t *ctx)
1380 INT l, r;
1381 HRESULT hres;
1383 TRACE("\n");
1385 hres = stack_pop_int(ctx, &r);
1386 if(FAILED(hres))
1387 return hres;
1389 hres = stack_pop_int(ctx, &l);
1390 if(FAILED(hres))
1391 return hres;
1393 return stack_push_int(ctx, l&r);
1396 /* ECMA-262 3rd Edition 11.8.6 */
1397 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1399 jsdisp_t *obj, *iter, *tmp = NULL;
1400 VARIANT prot, *v;
1401 BOOL ret = FALSE;
1402 HRESULT hres;
1404 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1406 v = stack_pop(ctx);
1407 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1408 VariantClear(v);
1409 return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1412 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1413 IDispatch_Release(V_DISPATCH(v));
1414 if(!obj) {
1415 FIXME("non-jsdisp objects not supported\n");
1416 return E_FAIL;
1419 if(is_class(obj, JSCLASS_FUNCTION)) {
1420 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1421 }else {
1422 hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1424 jsdisp_release(obj);
1425 if(FAILED(hres))
1426 return hres;
1428 v = stack_pop(ctx);
1430 if(V_VT(&prot) == VT_DISPATCH) {
1431 if(V_VT(v) == VT_DISPATCH)
1432 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1433 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1434 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1435 if(FAILED(hres))
1436 break;
1439 if(tmp)
1440 jsdisp_release(tmp);
1441 }else {
1442 FIXME("prototype is not an object\n");
1443 hres = E_FAIL;
1446 VariantClear(&prot);
1447 VariantClear(v);
1448 if(FAILED(hres))
1449 return hres;
1451 return stack_push_bool(ctx, ret);
1454 /* ECMA-262 3rd Edition 11.8.7 */
1455 static HRESULT interp_in(exec_ctx_t *ctx)
1457 VARIANT *obj, *v;
1458 DISPID id = 0;
1459 BOOL ret;
1460 BSTR str;
1461 HRESULT hres;
1463 TRACE("\n");
1465 obj = stack_pop(ctx);
1466 v = stack_pop(ctx);
1468 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1469 VariantClear(obj);
1470 VariantClear(v);
1471 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1474 hres = to_string(ctx->script, v, ctx->ei, &str);
1475 VariantClear(v);
1476 if(FAILED(hres)) {
1477 IDispatch_Release(V_DISPATCH(obj));
1478 return hres;
1481 hres = disp_get_id(ctx->script, V_DISPATCH(obj), str, 0, &id);
1482 IDispatch_Release(V_DISPATCH(obj));
1483 SysFreeString(str);
1484 if(SUCCEEDED(hres))
1485 ret = TRUE;
1486 else if(hres == DISP_E_UNKNOWNNAME)
1487 ret = FALSE;
1488 else
1489 return hres;
1491 return stack_push_bool(ctx, ret);
1494 /* ECMA-262 3rd Edition 11.6.1 */
1495 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1497 VARIANT r, l;
1498 HRESULT hres;
1500 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1501 if(FAILED(hres))
1502 return hres;
1504 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1505 if(FAILED(hres)) {
1506 VariantClear(&l);
1507 return hres;
1510 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1511 BSTR lstr = NULL, rstr = NULL;
1513 if(V_VT(&l) == VT_BSTR)
1514 lstr = V_BSTR(&l);
1515 else
1516 hres = to_string(ctx, &l, ei, &lstr);
1518 if(SUCCEEDED(hres)) {
1519 if(V_VT(&r) == VT_BSTR)
1520 rstr = V_BSTR(&r);
1521 else
1522 hres = to_string(ctx, &r, ei, &rstr);
1525 if(SUCCEEDED(hres)) {
1526 int len1, len2;
1528 len1 = SysStringLen(lstr);
1529 len2 = SysStringLen(rstr);
1531 V_VT(retv) = VT_BSTR;
1532 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1533 if(len1)
1534 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1535 if(len2)
1536 memcpy(V_BSTR(retv)+len1, rstr, len2*sizeof(WCHAR));
1537 V_BSTR(retv)[len1+len2] = 0;
1540 if(V_VT(&l) != VT_BSTR)
1541 SysFreeString(lstr);
1542 if(V_VT(&r) != VT_BSTR)
1543 SysFreeString(rstr);
1544 }else {
1545 double nl, nr;
1547 hres = to_number(ctx, &l, ei, &nl);
1548 if(SUCCEEDED(hres)) {
1549 hres = to_number(ctx, &r, ei, &nr);
1550 if(SUCCEEDED(hres))
1551 num_set_val(retv, nl + nr);
1555 VariantClear(&r);
1556 VariantClear(&l);
1557 return hres;
1560 /* ECMA-262 3rd Edition 11.6.1 */
1561 static HRESULT interp_add(exec_ctx_t *ctx)
1563 VARIANT *l, *r, ret;
1564 HRESULT hres;
1566 r = stack_pop(ctx);
1567 l = stack_pop(ctx);
1569 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1571 hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1572 VariantClear(l);
1573 VariantClear(r);
1574 if(FAILED(hres))
1575 return hres;
1577 return stack_push(ctx, &ret);
1580 /* ECMA-262 3rd Edition 11.6.2 */
1581 static HRESULT interp_sub(exec_ctx_t *ctx)
1583 double l, r;
1584 HRESULT hres;
1586 TRACE("\n");
1588 hres = stack_pop_number(ctx, &r);
1589 if(FAILED(hres))
1590 return hres;
1592 hres = stack_pop_number(ctx, &l);
1593 if(FAILED(hres))
1594 return hres;
1596 return stack_push_number(ctx, l-r);
1599 /* ECMA-262 3rd Edition 11.5.1 */
1600 static HRESULT interp_mul(exec_ctx_t *ctx)
1602 double l, r;
1603 HRESULT hres;
1605 TRACE("\n");
1607 hres = stack_pop_number(ctx, &r);
1608 if(FAILED(hres))
1609 return hres;
1611 hres = stack_pop_number(ctx, &l);
1612 if(FAILED(hres))
1613 return hres;
1615 return stack_push_number(ctx, l*r);
1618 /* ECMA-262 3rd Edition 11.5.2 */
1619 static HRESULT interp_div(exec_ctx_t *ctx)
1621 double l, r;
1622 HRESULT hres;
1624 TRACE("\n");
1626 hres = stack_pop_number(ctx, &r);
1627 if(FAILED(hres))
1628 return hres;
1630 hres = stack_pop_number(ctx, &l);
1631 if(FAILED(hres))
1632 return hres;
1634 return stack_push_number(ctx, l/r);
1637 /* ECMA-262 3rd Edition 11.5.3 */
1638 static HRESULT interp_mod(exec_ctx_t *ctx)
1640 double l, r;
1641 HRESULT hres;
1643 TRACE("\n");
1645 hres = stack_pop_number(ctx, &r);
1646 if(FAILED(hres))
1647 return hres;
1649 hres = stack_pop_number(ctx, &l);
1650 if(FAILED(hres))
1651 return hres;
1653 return stack_push_number(ctx, fmod(l, r));
1656 /* ECMA-262 3rd Edition 11.4.2 */
1657 static HRESULT interp_delete(exec_ctx_t *ctx)
1659 VARIANT *obj_var, *name_var;
1660 IDispatchEx *dispex;
1661 IDispatch *obj;
1662 BSTR name;
1663 BOOL ret;
1664 HRESULT hres;
1666 TRACE("\n");
1668 name_var = stack_pop(ctx);
1669 obj_var = stack_pop(ctx);
1671 hres = to_object(ctx->script, obj_var, &obj);
1672 VariantClear(obj_var);
1673 if(FAILED(hres)) {
1674 VariantClear(name_var);
1675 return hres;
1678 hres = to_string(ctx->script, name_var, ctx->ei, &name);
1679 VariantClear(name_var);
1680 if(FAILED(hres)) {
1681 IDispatch_Release(obj);
1682 return hres;
1685 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1686 if(SUCCEEDED(hres)) {
1687 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1688 ret = TRUE;
1689 IDispatchEx_Release(dispex);
1690 }else {
1691 hres = S_OK;
1692 ret = FALSE;
1695 IDispatch_Release(obj);
1696 SysFreeString(name);
1697 if(FAILED(hres))
1698 return hres;
1700 return stack_push_bool(ctx, ret);
1703 /* ECMA-262 3rd Edition 11.4.2 */
1704 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1706 const BSTR arg = get_op_bstr(ctx, 0);
1707 IDispatchEx *dispex;
1708 exprval_t exprval;
1709 BOOL ret = FALSE;
1710 HRESULT hres;
1712 TRACE("%s\n", debugstr_w(arg));
1714 hres = identifier_eval(ctx->script, arg, &exprval);
1715 if(FAILED(hres))
1716 return hres;
1718 if(exprval.type != EXPRVAL_IDREF) {
1719 FIXME("Unsupported exprval\n");
1720 exprval_release(&exprval);
1721 return E_NOTIMPL;
1724 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1725 IDispatch_Release(exprval.u.idref.disp);
1726 if(SUCCEEDED(hres)) {
1727 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1728 IDispatchEx_Release(dispex);
1729 if(FAILED(hres))
1730 return hres;
1732 ret = TRUE;
1735 return stack_push_bool(ctx, ret);
1738 /* ECMA-262 3rd Edition 11.4.2 */
1739 static HRESULT interp_void(exec_ctx_t *ctx)
1741 VARIANT v;
1743 TRACE("\n");
1745 stack_popn(ctx, 1);
1747 V_VT(&v) = VT_EMPTY;
1748 return stack_push(ctx, &v);
1751 /* ECMA-262 3rd Edition 11.4.3 */
1752 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1754 switch(V_VT(v)) {
1755 case VT_EMPTY:
1756 *ret = undefinedW;
1757 break;
1758 case VT_NULL:
1759 *ret = objectW;
1760 break;
1761 case VT_BOOL:
1762 *ret = booleanW;
1763 break;
1764 case VT_I4:
1765 case VT_R8:
1766 *ret = numberW;
1767 break;
1768 case VT_BSTR:
1769 *ret = stringW;
1770 break;
1771 case VT_DISPATCH: {
1772 jsdisp_t *dispex;
1774 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1775 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1776 jsdisp_release(dispex);
1777 }else {
1778 *ret = objectW;
1780 break;
1782 default:
1783 FIXME("unhandled vt %d\n", V_VT(v));
1784 return E_NOTIMPL;
1787 return S_OK;
1790 /* ECMA-262 3rd Edition 11.4.3 */
1791 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1793 const WCHAR *ret;
1794 IDispatch *obj;
1795 VARIANT v;
1796 DISPID id;
1797 HRESULT hres;
1799 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1801 TRACE("\n");
1803 obj = stack_pop_objid(ctx, &id);
1804 if(!obj)
1805 return stack_push_string(ctx, undefinedW);
1807 V_VT(&v) = VT_EMPTY;
1808 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1809 IDispatch_Release(obj);
1810 if(FAILED(hres))
1811 return stack_push_string(ctx, unknownW);
1813 hres = typeof_string(&v, &ret);
1814 VariantClear(&v);
1815 if(FAILED(hres))
1816 return hres;
1818 return stack_push_string(ctx, ret);
1821 /* ECMA-262 3rd Edition 11.4.3 */
1822 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1824 const BSTR arg = get_op_bstr(ctx, 0);
1825 exprval_t exprval;
1826 const WCHAR *ret;
1827 VARIANT v;
1828 HRESULT hres;
1830 TRACE("%s\n", debugstr_w(arg));
1832 hres = identifier_eval(ctx->script, arg, &exprval);
1833 if(FAILED(hres))
1834 return hres;
1836 if(exprval.type == EXPRVAL_INVALID) {
1837 hres = stack_push_string(ctx, undefinedW);
1838 exprval_release(&exprval);
1839 return hres;
1842 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1843 exprval_release(&exprval);
1844 if(FAILED(hres))
1845 return hres;
1847 hres = typeof_string(&v, &ret);
1848 VariantClear(&v);
1849 if(FAILED(hres))
1850 return hres;
1852 return stack_push_string(ctx, ret);
1855 /* ECMA-262 3rd Edition 11.4.3 */
1856 static HRESULT interp_typeof(exec_ctx_t *ctx)
1858 const WCHAR *ret;
1859 VARIANT *v;
1860 HRESULT hres;
1862 TRACE("\n");
1864 v = stack_pop(ctx);
1865 hres = typeof_string(v, &ret);
1866 VariantClear(v);
1867 if(FAILED(hres))
1868 return hres;
1870 return stack_push_string(ctx, ret);
1873 /* ECMA-262 3rd Edition 11.4.7 */
1874 static HRESULT interp_minus(exec_ctx_t *ctx)
1876 double n;
1877 HRESULT hres;
1879 TRACE("\n");
1881 hres = stack_pop_number(ctx, &n);
1882 if(FAILED(hres))
1883 return hres;
1885 return stack_push_number(ctx, -n);
1888 /* ECMA-262 3rd Edition 11.4.6 */
1889 static HRESULT interp_tonum(exec_ctx_t *ctx)
1891 VARIANT *v;
1892 double n;
1893 HRESULT hres;
1895 TRACE("\n");
1897 v = stack_pop(ctx);
1898 hres = to_number(ctx->script, v, ctx->ei, &n);
1899 VariantClear(v);
1900 if(FAILED(hres))
1901 return hres;
1903 return stack_push_number(ctx, n);
1906 /* ECMA-262 3rd Edition 11.3.1 */
1907 static HRESULT interp_postinc(exec_ctx_t *ctx)
1909 const int arg = get_op_int(ctx, 0);
1910 IDispatch *obj;
1911 DISPID id;
1912 VARIANT v;
1913 HRESULT hres;
1915 TRACE("%d\n", arg);
1917 obj = stack_pop_objid(ctx, &id);
1918 if(!obj)
1919 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1921 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1922 if(SUCCEEDED(hres)) {
1923 VARIANT inc;
1924 double n;
1926 hres = to_number(ctx->script, &v, ctx->ei, &n);
1927 if(SUCCEEDED(hres)) {
1928 num_set_val(&inc, n+(double)arg);
1929 hres = disp_propput(ctx->script, obj, id, &inc, ctx->ei);
1931 if(FAILED(hres))
1932 VariantClear(&v);
1934 IDispatch_Release(obj);
1935 if(FAILED(hres))
1936 return hres;
1938 return stack_push(ctx, &v);
1941 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1942 static HRESULT interp_preinc(exec_ctx_t *ctx)
1944 const int arg = get_op_int(ctx, 0);
1945 IDispatch *obj;
1946 DISPID id;
1947 VARIANT v;
1948 HRESULT hres;
1950 TRACE("%d\n", arg);
1952 obj = stack_pop_objid(ctx, &id);
1953 if(!obj)
1954 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1956 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1957 if(SUCCEEDED(hres)) {
1958 double n;
1960 hres = to_number(ctx->script, &v, ctx->ei, &n);
1961 VariantClear(&v);
1962 if(SUCCEEDED(hres)) {
1963 num_set_val(&v, n+(double)arg);
1964 hres = disp_propput(ctx->script, obj, id, &v, ctx->ei);
1967 IDispatch_Release(obj);
1968 if(FAILED(hres))
1969 return hres;
1971 return stack_push(ctx, &v);
1974 /* ECMA-262 3rd Edition 11.9.3 */
1975 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1977 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1978 return equal2_values(lval, rval, ret);
1980 /* FIXME: NULL disps should be handled in more general way */
1981 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1982 VARIANT v;
1983 V_VT(&v) = VT_NULL;
1984 return equal_values(ctx, &v, rval, ei, ret);
1987 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1988 VARIANT v;
1989 V_VT(&v) = VT_NULL;
1990 return equal_values(ctx, lval, &v, ei, ret);
1993 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1994 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1995 *ret = TRUE;
1996 return S_OK;
1999 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
2000 VARIANT v;
2001 double n;
2002 HRESULT hres;
2004 hres = to_number(ctx, lval, ei, &n);
2005 if(FAILED(hres))
2006 return hres;
2008 /* FIXME: optimize */
2009 num_set_val(&v, n);
2011 return equal_values(ctx, &v, rval, ei, ret);
2014 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
2015 VARIANT v;
2016 double n;
2017 HRESULT hres;
2019 hres = to_number(ctx, rval, ei, &n);
2020 if(FAILED(hres))
2021 return hres;
2023 /* FIXME: optimize */
2024 num_set_val(&v, n);
2026 return equal_values(ctx, lval, &v, ei, ret);
2029 if(V_VT(rval) == VT_BOOL) {
2030 VARIANT v;
2032 num_set_int(&v, V_BOOL(rval) ? 1 : 0);
2033 return equal_values(ctx, lval, &v, ei, ret);
2036 if(V_VT(lval) == VT_BOOL) {
2037 VARIANT v;
2039 num_set_int(&v, V_BOOL(lval) ? 1 : 0);
2040 return equal_values(ctx, &v, rval, ei, ret);
2044 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2045 VARIANT v;
2046 HRESULT hres;
2048 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2049 if(FAILED(hres))
2050 return hres;
2052 hres = equal_values(ctx, lval, &v, ei, ret);
2054 VariantClear(&v);
2055 return hres;
2059 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2060 VARIANT v;
2061 HRESULT hres;
2063 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2064 if(FAILED(hres))
2065 return hres;
2067 hres = equal_values(ctx, &v, rval, ei, ret);
2069 VariantClear(&v);
2070 return hres;
2074 *ret = FALSE;
2075 return S_OK;
2078 /* ECMA-262 3rd Edition 11.9.1 */
2079 static HRESULT interp_eq(exec_ctx_t *ctx)
2081 VARIANT *l, *r;
2082 BOOL b;
2083 HRESULT hres;
2085 r = stack_pop(ctx);
2086 l = stack_pop(ctx);
2088 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2090 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2091 VariantClear(l);
2092 VariantClear(r);
2093 if(FAILED(hres))
2094 return hres;
2096 return stack_push_bool(ctx, b);
2099 /* ECMA-262 3rd Edition 11.9.2 */
2100 static HRESULT interp_neq(exec_ctx_t *ctx)
2102 VARIANT *l, *r;
2103 BOOL b;
2104 HRESULT hres;
2106 r = stack_pop(ctx);
2107 l = stack_pop(ctx);
2109 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2111 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2112 VariantClear(l);
2113 VariantClear(r);
2114 if(FAILED(hres))
2115 return hres;
2117 return stack_push_bool(ctx, !b);
2120 /* ECMA-262 3rd Edition 11.9.4 */
2121 static HRESULT interp_eq2(exec_ctx_t *ctx)
2123 VARIANT *l, *r;
2124 BOOL b;
2125 HRESULT hres;
2127 TRACE("\n");
2129 r = stack_pop(ctx);
2130 l = stack_pop(ctx);
2132 hres = equal2_values(r, l, &b);
2133 VariantClear(l);
2134 VariantClear(r);
2135 if(FAILED(hres))
2136 return hres;
2138 return stack_push_bool(ctx, b);
2141 /* ECMA-262 3rd Edition 11.9.5 */
2142 static HRESULT interp_neq2(exec_ctx_t *ctx)
2144 VARIANT *l, *r;
2145 BOOL b;
2146 HRESULT hres;
2148 TRACE("\n");
2150 r = stack_pop(ctx);
2151 l = stack_pop(ctx);
2153 hres = equal2_values(r, l, &b);
2154 VariantClear(l);
2155 VariantClear(r);
2156 if(FAILED(hres))
2157 return hres;
2159 return stack_push_bool(ctx, !b);
2162 /* ECMA-262 3rd Edition 11.8.5 */
2163 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2165 double ln, rn;
2166 VARIANT l, r;
2167 HRESULT hres;
2169 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2170 if(FAILED(hres))
2171 return hres;
2173 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2174 if(FAILED(hres)) {
2175 VariantClear(&l);
2176 return hres;
2179 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2180 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2181 SysFreeString(V_BSTR(&l));
2182 SysFreeString(V_BSTR(&r));
2183 return S_OK;
2186 hres = to_number(ctx, &l, ei, &ln);
2187 VariantClear(&l);
2188 if(SUCCEEDED(hres))
2189 hres = to_number(ctx, &r, ei, &rn);
2190 VariantClear(&r);
2191 if(FAILED(hres))
2192 return hres;
2194 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2195 return S_OK;
2198 /* ECMA-262 3rd Edition 11.8.1 */
2199 static HRESULT interp_lt(exec_ctx_t *ctx)
2201 VARIANT *l, *r;
2202 BOOL b;
2203 HRESULT hres;
2205 r = stack_pop(ctx);
2206 l = stack_pop(ctx);
2208 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2210 hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2211 VariantClear(l);
2212 VariantClear(r);
2213 if(FAILED(hres))
2214 return hres;
2216 return stack_push_bool(ctx, b);
2219 /* ECMA-262 3rd Edition 11.8.1 */
2220 static HRESULT interp_lteq(exec_ctx_t *ctx)
2222 VARIANT *l, *r;
2223 BOOL b;
2224 HRESULT hres;
2226 r = stack_pop(ctx);
2227 l = stack_pop(ctx);
2229 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2231 hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2232 VariantClear(l);
2233 VariantClear(r);
2234 if(FAILED(hres))
2235 return hres;
2237 return stack_push_bool(ctx, b);
2240 /* ECMA-262 3rd Edition 11.8.2 */
2241 static HRESULT interp_gt(exec_ctx_t *ctx)
2243 VARIANT *l, *r;
2244 BOOL b;
2245 HRESULT hres;
2247 r = stack_pop(ctx);
2248 l = stack_pop(ctx);
2250 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2252 hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2253 VariantClear(l);
2254 VariantClear(r);
2255 if(FAILED(hres))
2256 return hres;
2258 return stack_push_bool(ctx, b);
2261 /* ECMA-262 3rd Edition 11.8.4 */
2262 static HRESULT interp_gteq(exec_ctx_t *ctx)
2264 VARIANT *l, *r;
2265 BOOL b;
2266 HRESULT hres;
2268 r = stack_pop(ctx);
2269 l = stack_pop(ctx);
2271 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2273 hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2274 VariantClear(l);
2275 VariantClear(r);
2276 if(FAILED(hres))
2277 return hres;
2279 return stack_push_bool(ctx, b);
2282 /* ECMA-262 3rd Edition 11.4.8 */
2283 static HRESULT interp_bneg(exec_ctx_t *ctx)
2285 VARIANT *v;
2286 INT i;
2287 HRESULT hres;
2289 TRACE("\n");
2291 v = stack_pop(ctx);
2292 hres = to_int32(ctx->script, v, ctx->ei, &i);
2293 VariantClear(v);
2294 if(FAILED(hres))
2295 return hres;
2297 return stack_push_int(ctx, ~i);
2300 /* ECMA-262 3rd Edition 11.4.9 */
2301 static HRESULT interp_neg(exec_ctx_t *ctx)
2303 VARIANT *v;
2304 VARIANT_BOOL b;
2305 HRESULT hres;
2307 TRACE("\n");
2309 v = stack_pop(ctx);
2310 hres = to_boolean(v, &b);
2311 VariantClear(v);
2312 if(FAILED(hres))
2313 return hres;
2315 return stack_push_bool(ctx, !b);
2318 /* ECMA-262 3rd Edition 11.7.1 */
2319 static HRESULT interp_lshift(exec_ctx_t *ctx)
2321 DWORD r;
2322 INT l;
2323 HRESULT hres;
2325 hres = stack_pop_uint(ctx, &r);
2326 if(FAILED(hres))
2327 return hres;
2329 hres = stack_pop_int(ctx, &l);
2330 if(FAILED(hres))
2331 return hres;
2333 return stack_push_int(ctx, l << (r&0x1f));
2336 /* ECMA-262 3rd Edition 11.7.2 */
2337 static HRESULT interp_rshift(exec_ctx_t *ctx)
2339 DWORD r;
2340 INT l;
2341 HRESULT hres;
2343 hres = stack_pop_uint(ctx, &r);
2344 if(FAILED(hres))
2345 return hres;
2347 hres = stack_pop_int(ctx, &l);
2348 if(FAILED(hres))
2349 return hres;
2351 return stack_push_int(ctx, l >> (r&0x1f));
2354 /* ECMA-262 3rd Edition 11.7.3 */
2355 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2357 DWORD r, l;
2358 HRESULT hres;
2360 hres = stack_pop_uint(ctx, &r);
2361 if(FAILED(hres))
2362 return hres;
2364 hres = stack_pop_uint(ctx, &l);
2365 if(FAILED(hres))
2366 return hres;
2368 return stack_push_int(ctx, l >> (r&0x1f));
2371 /* ECMA-262 3rd Edition 11.13.1 */
2372 static HRESULT interp_assign(exec_ctx_t *ctx)
2374 IDispatch *disp;
2375 DISPID id;
2376 VARIANT *v;
2377 HRESULT hres;
2379 TRACE("\n");
2381 v = stack_pop(ctx);
2382 disp = stack_pop_objid(ctx, &id);
2384 if(!disp)
2385 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2387 hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2388 IDispatch_Release(disp);
2389 if(FAILED(hres)) {
2390 VariantClear(v);
2391 return hres;
2394 return stack_push(ctx, v);
2397 /* JScript extension */
2398 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2400 const unsigned arg = get_op_uint(ctx, 0);
2401 DISPID propput_dispid = DISPID_PROPERTYPUT;
2402 IDispatch *disp;
2403 DISPPARAMS dp;
2404 VARIANT *v;
2405 DISPID id;
2406 HRESULT hres;
2408 TRACE("%u\n", arg);
2410 disp = stack_topn_objid(ctx, arg+1, &id);
2411 if(!disp)
2412 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2414 jsstack_to_dp(ctx, arg+1, &dp);
2415 dp.cNamedArgs = 1;
2416 dp.rgdispidNamedArgs = &propput_dispid;
2417 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, &dp, NULL, ctx->ei);
2418 if(FAILED(hres))
2419 return hres;
2421 v = stack_pop(ctx);
2422 stack_popn(ctx, arg+2);
2423 return stack_push(ctx, v);
2426 static HRESULT interp_undefined(exec_ctx_t *ctx)
2428 VARIANT v;
2430 TRACE("\n");
2432 V_VT(&v) = VT_EMPTY;
2433 return stack_push(ctx, &v);
2436 static HRESULT interp_jmp(exec_ctx_t *ctx)
2438 const unsigned arg = get_op_uint(ctx, 0);
2440 TRACE("%u\n", arg);
2442 ctx->ip = arg;
2443 return S_OK;
2446 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2448 const unsigned arg = get_op_uint(ctx, 0);
2449 VARIANT_BOOL b;
2450 VARIANT *v;
2451 HRESULT hres;
2453 TRACE("\n");
2455 v = stack_pop(ctx);
2456 hres = to_boolean(v, &b);
2457 VariantClear(v);
2458 if(FAILED(hres))
2459 return hres;
2461 if(b)
2462 ctx->ip++;
2463 else
2464 ctx->ip = arg;
2465 return S_OK;
2468 static HRESULT interp_pop(exec_ctx_t *ctx)
2470 TRACE("\n");
2472 stack_popn(ctx, 1);
2473 return S_OK;
2476 static HRESULT interp_ret(exec_ctx_t *ctx)
2478 TRACE("\n");
2480 ctx->ip = -1;
2481 return S_OK;
2484 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2486 static const op_func_t op_funcs[] = {
2487 #define X(x,a,b,c) interp_##x,
2488 OP_LIST
2489 #undef X
2492 static const unsigned op_move[] = {
2493 #define X(a,x,b,c) x,
2494 OP_LIST
2495 #undef X
2498 static HRESULT unwind_exception(exec_ctx_t *ctx)
2500 except_frame_t *except_frame;
2501 VARIANT except_val;
2502 BSTR ident;
2503 HRESULT hres;
2505 except_frame = ctx->except_frame;
2506 ctx->except_frame = except_frame->next;
2508 assert(except_frame->stack_top <= ctx->top);
2509 stack_popn(ctx, ctx->top - except_frame->stack_top);
2511 while(except_frame->scope != ctx->scope_chain)
2512 scope_pop(&ctx->scope_chain);
2514 ctx->ip = except_frame->catch_off;
2516 except_val = ctx->ei->var;
2517 memset(ctx->ei, 0, sizeof(*ctx->ei));
2519 ident = except_frame->ident;
2520 heap_free(except_frame);
2522 if(ident) {
2523 jsdisp_t *scope_obj;
2525 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2526 if(SUCCEEDED(hres)) {
2527 hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei);
2528 if(FAILED(hres))
2529 jsdisp_release(scope_obj);
2531 VariantClear(&except_val);
2532 if(FAILED(hres))
2533 return hres;
2535 hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2536 jsdisp_release(scope_obj);
2537 }else {
2538 VARIANT v;
2540 hres = stack_push(ctx, &except_val);
2541 if(FAILED(hres))
2542 return hres;
2544 hres = stack_push_bool(ctx, FALSE);
2545 if(FAILED(hres))
2546 return hres;
2548 V_VT(&v) = VT_EMPTY;
2549 hres = stack_push(ctx, &v);
2552 return hres;
2555 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsexcept_t *ei, VARIANT *ret)
2557 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2558 except_frame_t *prev_except_frame;
2559 function_code_t *prev_func;
2560 unsigned prev_ip, prev_top;
2561 scope_chain_t *prev_scope;
2562 bytecode_t *prev_code;
2563 jsexcept_t *prev_ei;
2564 jsop_t op;
2565 HRESULT hres = S_OK;
2567 TRACE("\n");
2569 prev_top = exec_ctx->top;
2570 prev_scope = exec_ctx->scope_chain;
2571 prev_except_frame = exec_ctx->except_frame;
2572 prev_ip = exec_ctx->ip;
2573 prev_ei = exec_ctx->ei;
2574 prev_code = exec_ctx->code;
2575 prev_func = exec_ctx->func_code;
2576 exec_ctx->ip = func->instr_off;
2577 exec_ctx->ei = ei;
2578 exec_ctx->except_frame = NULL;
2579 exec_ctx->code = code;
2580 exec_ctx->func_code = func;
2582 while(exec_ctx->ip != -1) {
2583 op = code->instrs[exec_ctx->ip].op;
2584 hres = op_funcs[op](exec_ctx);
2585 if(FAILED(hres)) {
2586 TRACE("EXCEPTION\n");
2588 if(!exec_ctx->except_frame)
2589 break;
2591 hres = unwind_exception(exec_ctx);
2592 if(FAILED(hres))
2593 break;
2594 }else {
2595 exec_ctx->ip += op_move[op];
2599 exec_ctx->ip = prev_ip;
2600 exec_ctx->ei = prev_ei;
2601 exec_ctx->except_frame = prev_except_frame;
2602 exec_ctx->code = prev_code;
2603 exec_ctx->func_code = prev_func;
2605 if(FAILED(hres)) {
2606 while(exec_ctx->scope_chain != prev_scope)
2607 scope_pop(&exec_ctx->scope_chain);
2608 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2609 return hres;
2612 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2613 assert(exec_ctx->scope_chain == prev_scope);
2615 if(exec_ctx->top == prev_top)
2616 V_VT(ret) = VT_EMPTY;
2617 else
2618 *ret = *stack_pop(exec_ctx);
2619 return S_OK;
2622 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval,
2623 jsexcept_t *ei, VARIANT *retv)
2625 exec_ctx_t *prev_ctx;
2626 VARIANT val;
2627 unsigned i;
2628 HRESULT hres = S_OK;
2630 for(i = 0; i < func->func_cnt; i++) {
2631 jsdisp_t *func_obj;
2632 VARIANT var;
2634 if(!func->funcs[i].name)
2635 continue;
2637 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2638 if(FAILED(hres))
2639 return hres;
2641 var_set_jsdisp(&var, func_obj);
2642 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, &var, ei);
2643 jsdisp_release(func_obj);
2644 if(FAILED(hres))
2645 return hres;
2648 for(i=0; i < func->var_cnt; i++) {
2649 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2650 DISPID id = 0;
2652 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2653 if(FAILED(hres))
2654 return hres;
2658 prev_ctx = ctx->script->exec_ctx;
2659 ctx->script->exec_ctx = ctx;
2661 hres = enter_bytecode(ctx->script, code, func, ei, &val);
2662 assert(ctx->script->exec_ctx == ctx);
2663 ctx->script->exec_ctx = prev_ctx;
2664 if(FAILED(hres))
2665 return hres;
2667 if(retv)
2668 *retv = val;
2669 else
2670 VariantClear(&val);
2671 return S_OK;