d3dcompiler: Don't allow semantics on void functions.
[wine/multimedia.git] / dlls / jscript / engine.c
blobe6c26462b45e003e323ddbd222408a2240b9da94
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_args(exec_ctx_t *ctx, unsigned n)
138 if(!n)
139 return NULL;
140 assert(ctx->top > n-1);
141 return ctx->stack + ctx->top-n;
144 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
146 assert(ctx->top);
147 return ctx->stack + --ctx->top;
150 static void stack_popn(exec_ctx_t *ctx, unsigned n)
152 while(n--)
153 VariantClear(stack_pop(ctx));
156 static HRESULT stack_pop_number(exec_ctx_t *ctx, double *r)
158 VARIANT *v;
159 HRESULT hres;
161 v = stack_pop(ctx);
162 hres = to_number(ctx->script, v, ctx->ei, r);
163 VariantClear(v);
164 return hres;
167 static HRESULT stack_pop_object(exec_ctx_t *ctx, IDispatch **r)
169 VARIANT *v;
170 HRESULT hres;
172 v = stack_pop(ctx);
173 if(V_VT(v) == VT_DISPATCH) {
174 if(!V_DISPATCH(v))
175 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_REQUIRED, NULL);
176 *r = V_DISPATCH(v);
177 return S_OK;
180 hres = to_object(ctx->script, v, r);
181 VariantClear(v);
182 return hres;
185 static inline HRESULT stack_pop_int(exec_ctx_t *ctx, INT *r)
187 return to_int32(ctx->script, stack_pop(ctx), ctx->ei, r);
190 static inline HRESULT stack_pop_uint(exec_ctx_t *ctx, DWORD *r)
192 return to_uint32(ctx->script, stack_pop(ctx), ctx->ei, r);
195 static inline IDispatch *stack_pop_objid(exec_ctx_t *ctx, DISPID *id)
197 assert(V_VT(stack_top(ctx)) == VT_INT && V_VT(stack_topn(ctx, 1)) == VT_DISPATCH);
199 *id = V_INT(stack_pop(ctx));
200 return V_DISPATCH(stack_pop(ctx));
203 static inline IDispatch *stack_topn_objid(exec_ctx_t *ctx, unsigned n, DISPID *id)
205 assert(V_VT(stack_topn(ctx, n)) == VT_INT && V_VT(stack_topn(ctx, n+1)) == VT_DISPATCH);
207 *id = V_INT(stack_topn(ctx, n));
208 return V_DISPATCH(stack_topn(ctx, n+1));
211 static void exprval_release(exprval_t *val)
213 switch(val->type) {
214 case EXPRVAL_VARIANT:
215 if(V_VT(&val->u.var) != VT_EMPTY)
216 VariantClear(&val->u.var);
217 return;
218 case EXPRVAL_IDREF:
219 if(val->u.idref.disp)
220 IDispatch_Release(val->u.idref.disp);
221 return;
222 case EXPRVAL_INVALID:
223 return;
227 /* ECMA-262 3rd Edition 8.7.1 */
228 static HRESULT exprval_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
230 V_VT(ret) = VT_EMPTY;
232 switch(val->type) {
233 case EXPRVAL_VARIANT:
234 return VariantCopy(ret, &val->u.var);
235 case EXPRVAL_IDREF:
236 if(!val->u.idref.disp) {
237 FIXME("throw ReferenceError\n");
238 return E_FAIL;
241 return disp_propget(ctx, val->u.idref.disp, val->u.idref.id, ret, ei);
242 case EXPRVAL_INVALID:
243 assert(0);
246 ERR("type %d\n", val->type);
247 return E_FAIL;
250 static HRESULT exprval_to_value(script_ctx_t *ctx, exprval_t *val, jsexcept_t *ei, VARIANT *ret)
252 if(val->type == EXPRVAL_VARIANT) {
253 *ret = val->u.var;
254 V_VT(&val->u.var) = VT_EMPTY;
255 return S_OK;
258 return exprval_value(ctx, val, ei, ret);
261 static void exprval_set_idref(exprval_t *val, IDispatch *disp, DISPID id)
263 val->type = EXPRVAL_IDREF;
264 val->u.idref.disp = disp;
265 val->u.idref.id = id;
267 if(disp)
268 IDispatch_AddRef(disp);
271 HRESULT scope_push(scope_chain_t *scope, jsdisp_t *obj, scope_chain_t **ret)
273 scope_chain_t *new_scope;
275 new_scope = heap_alloc(sizeof(scope_chain_t));
276 if(!new_scope)
277 return E_OUTOFMEMORY;
279 new_scope->ref = 1;
281 jsdisp_addref(obj);
282 new_scope->obj = obj;
284 if(scope) {
285 scope_addref(scope);
286 new_scope->next = scope;
287 }else {
288 new_scope->next = NULL;
291 *ret = new_scope;
292 return S_OK;
295 static void scope_pop(scope_chain_t **scope)
297 scope_chain_t *tmp;
299 tmp = *scope;
300 *scope = tmp->next;
301 scope_release(tmp);
304 void scope_release(scope_chain_t *scope)
306 if(--scope->ref)
307 return;
309 if(scope->next)
310 scope_release(scope->next);
312 jsdisp_release(scope->obj);
313 heap_free(scope);
316 HRESULT create_exec_ctx(script_ctx_t *script_ctx, IDispatch *this_obj, jsdisp_t *var_disp,
317 scope_chain_t *scope, BOOL is_global, exec_ctx_t **ret)
319 exec_ctx_t *ctx;
321 ctx = heap_alloc_zero(sizeof(exec_ctx_t));
322 if(!ctx)
323 return E_OUTOFMEMORY;
325 ctx->ref = 1;
326 ctx->is_global = is_global;
328 if(this_obj)
329 ctx->this_obj = this_obj;
330 else if(script_ctx->host_global)
331 ctx->this_obj = script_ctx->host_global;
332 else
333 ctx->this_obj = to_disp(script_ctx->global);
334 IDispatch_AddRef(ctx->this_obj);
336 jsdisp_addref(var_disp);
337 ctx->var_disp = var_disp;
339 script_addref(script_ctx);
340 ctx->script = script_ctx;
342 if(scope) {
343 scope_addref(scope);
344 ctx->scope_chain = scope;
347 *ret = ctx;
348 return S_OK;
351 void exec_release(exec_ctx_t *ctx)
353 if(--ctx->ref)
354 return;
356 if(ctx->scope_chain)
357 scope_release(ctx->scope_chain);
358 if(ctx->var_disp)
359 jsdisp_release(ctx->var_disp);
360 if(ctx->this_obj)
361 IDispatch_Release(ctx->this_obj);
362 if(ctx->script)
363 script_release(ctx->script);
364 heap_free(ctx->stack);
365 heap_free(ctx);
368 static HRESULT disp_get_id(script_ctx_t *ctx, IDispatch *disp, BSTR name, DWORD flags, DISPID *id)
370 IDispatchEx *dispex;
371 HRESULT hres;
373 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
374 if(FAILED(hres)) {
375 TRACE("using IDispatch\n");
377 *id = 0;
378 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
381 *id = 0;
382 hres = IDispatchEx_GetDispID(dispex, name, make_grfdex(ctx, flags|fdexNameCaseSensitive), id);
383 IDispatchEx_Release(dispex);
384 return hres;
387 static inline BOOL is_null(const VARIANT *v)
389 return V_VT(v) == VT_NULL || (V_VT(v) == VT_DISPATCH && !V_DISPATCH(v));
392 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, BOOL *ret)
394 IObjectIdentity *identity;
395 IUnknown *unk1, *unk2;
396 HRESULT hres;
398 if(disp1 == disp2) {
399 *ret = TRUE;
400 return S_OK;
403 if(!disp1 || !disp2) {
404 *ret = FALSE;
405 return S_OK;
408 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
409 if(FAILED(hres))
410 return hres;
412 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
413 if(FAILED(hres)) {
414 IUnknown_Release(unk1);
415 return hres;
418 if(unk1 == unk2) {
419 *ret = TRUE;
420 }else {
421 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
422 if(SUCCEEDED(hres)) {
423 hres = IObjectIdentity_IsEqualObject(identity, unk2);
424 IObjectIdentity_Release(identity);
425 *ret = hres == S_OK;
426 }else {
427 *ret = FALSE;
431 IUnknown_Release(unk1);
432 IUnknown_Release(unk2);
433 return S_OK;
436 /* ECMA-262 3rd Edition 11.9.6 */
437 static HRESULT equal2_values(VARIANT *lval, VARIANT *rval, BOOL *ret)
439 TRACE("\n");
441 if(V_VT(lval) != V_VT(rval)) {
442 if(is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval)))
443 *ret = num_val(lval) == num_val(rval);
444 else if(is_null(lval))
445 *ret = is_null(rval);
446 else
447 *ret = FALSE;
448 return S_OK;
451 switch(V_VT(lval)) {
452 case VT_EMPTY:
453 case VT_NULL:
454 *ret = VARIANT_TRUE;
455 break;
456 case VT_I4:
457 *ret = V_I4(lval) == V_I4(rval);
458 break;
459 case VT_R8:
460 *ret = V_R8(lval) == V_R8(rval);
461 break;
462 case VT_BSTR:
463 if(!V_BSTR(lval))
464 *ret = SysStringLen(V_BSTR(rval))?FALSE:TRUE;
465 else if(!V_BSTR(rval))
466 *ret = SysStringLen(V_BSTR(lval))?FALSE:TRUE;
467 else
468 *ret = !strcmpW(V_BSTR(lval), V_BSTR(rval));
469 break;
470 case VT_DISPATCH:
471 return disp_cmp(V_DISPATCH(lval), V_DISPATCH(rval), ret);
472 case VT_BOOL:
473 *ret = !V_BOOL(lval) == !V_BOOL(rval);
474 break;
475 default:
476 FIXME("unimplemented vt %d\n", V_VT(lval));
477 return E_NOTIMPL;
480 return S_OK;
483 static BOOL lookup_global_members(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
485 named_item_t *item;
486 DISPID id;
487 HRESULT hres;
489 for(item = ctx->named_items; item; item = item->next) {
490 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
491 hres = disp_get_id(ctx, item->disp, identifier, 0, &id);
492 if(SUCCEEDED(hres)) {
493 if(ret)
494 exprval_set_idref(ret, item->disp, id);
495 return TRUE;
500 return FALSE;
503 /* ECMA-262 3rd Edition 10.1.4 */
504 static HRESULT identifier_eval(script_ctx_t *ctx, BSTR identifier, exprval_t *ret)
506 scope_chain_t *scope;
507 named_item_t *item;
508 DISPID id = 0;
509 HRESULT hres;
511 TRACE("%s\n", debugstr_w(identifier));
513 for(scope = ctx->exec_ctx->scope_chain; scope; scope = scope->next) {
514 hres = jsdisp_get_id(scope->obj, identifier, 0, &id);
515 if(SUCCEEDED(hres)) {
516 exprval_set_idref(ret, to_disp(scope->obj), id);
517 return S_OK;
521 hres = jsdisp_get_id(ctx->global, identifier, 0, &id);
522 if(SUCCEEDED(hres)) {
523 exprval_set_idref(ret, to_disp(ctx->global), id);
524 return S_OK;
527 for(item = ctx->named_items; item; item = item->next) {
528 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpW(item->name, identifier)) {
529 if(!item->disp) {
530 IUnknown *unk;
532 if(!ctx->site)
533 break;
535 hres = IActiveScriptSite_GetItemInfo(ctx->site, identifier,
536 SCRIPTINFO_IUNKNOWN, &unk, NULL);
537 if(FAILED(hres)) {
538 WARN("GetItemInfo failed: %08x\n", hres);
539 break;
542 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
543 IUnknown_Release(unk);
544 if(FAILED(hres)) {
545 WARN("object does not implement IDispatch\n");
546 break;
550 ret->type = EXPRVAL_VARIANT;
551 V_VT(&ret->u.var) = VT_DISPATCH;
552 V_DISPATCH(&ret->u.var) = item->disp;
553 IDispatch_AddRef(item->disp);
554 return S_OK;
558 if(lookup_global_members(ctx, identifier, ret))
559 return S_OK;
561 ret->type = EXPRVAL_INVALID;
562 return S_OK;
565 static inline BSTR get_op_bstr(exec_ctx_t *ctx, int i){
566 return ctx->code->instrs[ctx->ip].u.arg[i].bstr;
569 static inline unsigned get_op_uint(exec_ctx_t *ctx, int i){
570 return ctx->code->instrs[ctx->ip].u.arg[i].uint;
573 static inline unsigned get_op_int(exec_ctx_t *ctx, int i){
574 return ctx->code->instrs[ctx->ip].u.arg[i].lng;
577 static inline const WCHAR *get_op_str(exec_ctx_t *ctx, int i){
578 return ctx->code->instrs[ctx->ip].u.arg[i].str;
581 static inline double get_op_double(exec_ctx_t *ctx){
582 return ctx->code->instrs[ctx->ip].u.dbl;
585 /* ECMA-262 3rd Edition 12.2 */
586 static HRESULT interp_var_set(exec_ctx_t *ctx)
588 const BSTR name = get_op_bstr(ctx, 0);
589 VARIANT *v;
590 HRESULT hres;
592 TRACE("%s\n", debugstr_w(name));
594 v = stack_pop(ctx);
595 hres = jsdisp_propput_name(ctx->var_disp, name, v, ctx->ei);
596 VariantClear(v);
597 return hres;
600 /* ECMA-262 3rd Edition 12.6.4 */
601 static HRESULT interp_forin(exec_ctx_t *ctx)
603 const HRESULT arg = get_op_uint(ctx, 0);
604 IDispatch *var_obj, *obj = NULL;
605 IDispatchEx *dispex;
606 DISPID id, var_id;
607 BSTR name = NULL;
608 VARIANT *val;
609 HRESULT hres;
611 TRACE("\n");
613 val = stack_pop(ctx);
615 assert(V_VT(stack_top(ctx)) == VT_I4);
616 id = V_I4(stack_top(ctx));
618 var_obj = stack_topn_objid(ctx, 1, &var_id);
619 if(!var_obj) {
620 FIXME("invalid ref\n");
621 VariantClear(val);
622 return E_FAIL;
625 if(V_VT(stack_topn(ctx, 3)) == VT_DISPATCH)
626 obj = V_DISPATCH(stack_topn(ctx, 3));
628 if(obj) {
629 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
630 if(SUCCEEDED(hres)) {
631 hres = IDispatchEx_GetNextDispID(dispex, fdexEnumDefault, id, &id);
632 if(hres == S_OK)
633 hres = IDispatchEx_GetMemberName(dispex, id, &name);
634 IDispatchEx_Release(dispex);
635 if(FAILED(hres)) {
636 VariantClear(val);
637 return hres;
639 }else {
640 TRACE("No IDispatchEx\n");
644 if(name) {
645 VARIANT v;
647 VariantClear(val);
649 V_I4(stack_top(ctx)) = id;
651 V_VT(&v) = VT_BSTR;
652 V_BSTR(&v) = name;
653 hres = disp_propput(ctx->script, var_obj, var_id, &v, ctx->ei);
654 SysFreeString(name);
655 if(FAILED(hres))
656 return hres;
658 ctx->ip++;
659 }else {
660 stack_popn(ctx, 4);
661 ctx->ip = arg;
662 return stack_push(ctx, val);
664 return S_OK;
667 /* ECMA-262 3rd Edition 12.10 */
668 static HRESULT interp_push_scope(exec_ctx_t *ctx)
670 IDispatch *disp;
671 jsdisp_t *obj;
672 VARIANT *v;
673 HRESULT hres;
675 TRACE("\n");
677 v = stack_pop(ctx);
678 hres = to_object(ctx->script, v, &disp);
679 VariantClear(v);
680 if(FAILED(hres))
681 return hres;
683 obj = to_jsdisp(disp);
684 if(!obj) {
685 IDispatch_Release(disp);
686 FIXME("disp is not jsdisp\n");
687 return E_NOTIMPL;
690 hres = scope_push(ctx->scope_chain, obj, &ctx->scope_chain);
691 jsdisp_release(obj);
692 return hres;
695 /* ECMA-262 3rd Edition 12.10 */
696 static HRESULT interp_pop_scope(exec_ctx_t *ctx)
698 TRACE("\n");
700 scope_pop(&ctx->scope_chain);
701 return S_OK;
704 /* ECMA-262 3rd Edition 12.13 */
705 static HRESULT interp_case(exec_ctx_t *ctx)
707 const unsigned arg = get_op_uint(ctx, 0);
708 VARIANT *v;
709 BOOL b;
710 HRESULT hres;
712 TRACE("\n");
714 v = stack_pop(ctx);
715 hres = equal2_values(stack_top(ctx), v, &b);
716 VariantClear(v);
717 if(FAILED(hres))
718 return hres;
720 if(b) {
721 stack_popn(ctx, 1);
722 ctx->ip = arg;
723 }else {
724 ctx->ip++;
726 return S_OK;
729 /* ECMA-262 3rd Edition 12.13 */
730 static HRESULT interp_throw(exec_ctx_t *ctx)
732 TRACE("\n");
734 ctx->ei->var = *stack_pop(ctx);
735 return DISP_E_EXCEPTION;
738 static HRESULT interp_throw_ref(exec_ctx_t *ctx)
740 const HRESULT arg = get_op_uint(ctx, 0);
742 TRACE("%08x\n", arg);
744 return throw_reference_error(ctx->script, ctx->ei, arg, NULL);
747 static HRESULT interp_throw_type(exec_ctx_t *ctx)
749 const HRESULT hres = get_op_uint(ctx, 0);
750 const WCHAR *str = get_op_str(ctx, 1);
752 TRACE("%08x %s\n", hres, debugstr_w(str));
754 return throw_type_error(ctx->script, ctx->ei, hres, str);
757 /* ECMA-262 3rd Edition 12.14 */
758 static HRESULT interp_push_except(exec_ctx_t *ctx)
760 const unsigned arg1 = get_op_uint(ctx, 0);
761 const BSTR arg2 = get_op_bstr(ctx, 1);
762 except_frame_t *except;
763 unsigned stack_top;
765 TRACE("\n");
767 stack_top = ctx->top;
769 if(!arg2) {
770 HRESULT hres;
772 hres = stack_push_bool(ctx, TRUE);
773 if(FAILED(hres))
774 return hres;
775 hres = stack_push_bool(ctx, TRUE);
776 if(FAILED(hres))
777 return hres;
780 except = heap_alloc(sizeof(*except));
781 if(!except)
782 return E_OUTOFMEMORY;
784 except->stack_top = stack_top;
785 except->scope = ctx->scope_chain;
786 except->catch_off = arg1;
787 except->ident = arg2;
788 except->next = ctx->except_frame;
789 ctx->except_frame = except;
790 return S_OK;
793 /* ECMA-262 3rd Edition 12.14 */
794 static HRESULT interp_pop_except(exec_ctx_t *ctx)
796 except_frame_t *except;
798 TRACE("\n");
800 except = ctx->except_frame;
801 assert(except != NULL);
803 ctx->except_frame = except->next;
804 heap_free(except);
805 return S_OK;
808 /* ECMA-262 3rd Edition 12.14 */
809 static HRESULT interp_end_finally(exec_ctx_t *ctx)
811 VARIANT *v;
813 TRACE("\n");
815 v = stack_pop(ctx);
817 assert(V_VT(stack_top(ctx)) == VT_BOOL);
818 if(!V_BOOL(stack_top(ctx))) {
819 TRACE("passing exception\n");
821 VariantClear(v);
822 stack_popn(ctx, 1);
823 ctx->ei->var = *stack_pop(ctx);
824 return DISP_E_EXCEPTION;
827 stack_popn(ctx, 2);
828 return stack_push(ctx, v);
831 /* ECMA-262 3rd Edition 13 */
832 static HRESULT interp_func(exec_ctx_t *ctx)
834 unsigned func_idx = get_op_uint(ctx, 0);
835 jsdisp_t *dispex;
836 VARIANT v;
837 HRESULT hres;
839 TRACE("%d\n", func_idx);
841 hres = create_source_function(ctx->script, ctx->code, ctx->func_code->funcs+func_idx,
842 ctx->scope_chain, &dispex);
843 if(FAILED(hres))
844 return hres;
846 var_set_jsdisp(&v, dispex);
847 return stack_push(ctx, &v);
850 /* ECMA-262 3rd Edition 11.2.1 */
851 static HRESULT interp_array(exec_ctx_t *ctx)
853 VARIANT v, *namev;
854 IDispatch *obj;
855 DISPID id;
856 BSTR name;
857 HRESULT hres;
859 TRACE("\n");
861 namev = stack_pop(ctx);
863 hres = stack_pop_object(ctx, &obj);
864 if(FAILED(hres)) {
865 VariantClear(namev);
866 return hres;
869 hres = to_string(ctx->script, namev, ctx->ei, &name);
870 VariantClear(namev);
871 if(FAILED(hres)) {
872 IDispatch_Release(obj);
873 return hres;
876 hres = disp_get_id(ctx->script, obj, name, 0, &id);
877 SysFreeString(name);
878 if(SUCCEEDED(hres)) {
879 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
880 }else if(hres == DISP_E_UNKNOWNNAME) {
881 V_VT(&v) = VT_EMPTY;
882 hres = S_OK;
884 IDispatch_Release(obj);
885 if(FAILED(hres))
886 return hres;
888 return stack_push(ctx, &v);
891 /* ECMA-262 3rd Edition 11.2.1 */
892 static HRESULT interp_member(exec_ctx_t *ctx)
894 const BSTR arg = get_op_bstr(ctx, 0);
895 IDispatch *obj;
896 VARIANT v;
897 DISPID id;
898 HRESULT hres;
900 TRACE("\n");
902 hres = stack_pop_object(ctx, &obj);
903 if(FAILED(hres))
904 return hres;
906 hres = disp_get_id(ctx->script, obj, arg, 0, &id);
907 if(SUCCEEDED(hres)) {
908 V_VT(&v) = VT_EMPTY;
909 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
910 }else if(hres == DISP_E_UNKNOWNNAME) {
911 V_VT(&v) = VT_EMPTY;
912 hres = S_OK;
914 IDispatch_Release(obj);
915 if(FAILED(hres))
916 return hres;
918 return stack_push(ctx, &v);
921 /* ECMA-262 3rd Edition 11.2.1 */
922 static HRESULT interp_memberid(exec_ctx_t *ctx)
924 const unsigned arg = get_op_uint(ctx, 0);
925 VARIANT *objv, *namev;
926 IDispatch *obj;
927 BSTR name;
928 DISPID id;
929 HRESULT hres;
931 TRACE("%x\n", arg);
933 namev = stack_pop(ctx);
934 objv = stack_pop(ctx);
936 hres = to_object(ctx->script, objv, &obj);
937 VariantClear(objv);
938 if(SUCCEEDED(hres)) {
939 hres = to_string(ctx->script, namev, ctx->ei, &name);
940 if(FAILED(hres))
941 IDispatch_Release(obj);
943 VariantClear(namev);
944 if(FAILED(hres))
945 return hres;
947 hres = disp_get_id(ctx->script, obj, name, arg, &id);
948 SysFreeString(name);
949 if(FAILED(hres)) {
950 IDispatch_Release(obj);
951 if(hres == DISP_E_UNKNOWNNAME && !(arg & fdexNameEnsure)) {
952 obj = NULL;
953 id = JS_E_INVALID_PROPERTY;
954 }else {
955 return hres;
959 return stack_push_objid(ctx, obj, id);
962 /* ECMA-262 3rd Edition 11.2.1 */
963 static HRESULT interp_refval(exec_ctx_t *ctx)
965 IDispatch *disp;
966 VARIANT v;
967 DISPID id;
968 HRESULT hres;
970 TRACE("\n");
972 disp = stack_topn_objid(ctx, 0, &id);
973 if(!disp)
974 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
976 hres = disp_propget(ctx->script, disp, id, &v, ctx->ei);
977 if(FAILED(hres))
978 return hres;
980 return stack_push(ctx, &v);
983 /* ECMA-262 3rd Edition 11.2.2 */
984 static HRESULT interp_new(exec_ctx_t *ctx)
986 const unsigned arg = get_op_uint(ctx, 0);
987 VARIANT *constr, v;
988 HRESULT hres;
990 TRACE("%d\n", arg);
992 constr = stack_topn(ctx, arg);
994 /* NOTE: Should use to_object here */
996 if(V_VT(constr) == VT_NULL)
997 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
998 else if(V_VT(constr) != VT_DISPATCH)
999 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_ACTION, NULL);
1000 else if(!V_DISPATCH(constr))
1001 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1003 hres = disp_call_value(ctx->script, V_DISPATCH(constr), NULL, DISPATCH_CONSTRUCT, arg, stack_args(ctx, arg), &v, ctx->ei);
1004 if(FAILED(hres))
1005 return hres;
1007 stack_popn(ctx, arg+1);
1008 return stack_push(ctx, &v);
1011 /* ECMA-262 3rd Edition 11.2.3 */
1012 static HRESULT interp_call(exec_ctx_t *ctx)
1014 const unsigned argn = get_op_uint(ctx, 0);
1015 const int do_ret = get_op_int(ctx, 1);
1016 VARIANT v, *objv;
1017 HRESULT hres;
1019 TRACE("%d %d\n", argn, do_ret);
1021 objv = stack_topn(ctx, argn);
1022 if(V_VT(objv) != VT_DISPATCH)
1023 return throw_type_error(ctx->script, ctx->ei, JS_E_INVALID_PROPERTY, NULL);
1025 hres = disp_call_value(ctx->script, V_DISPATCH(objv), NULL, DISPATCH_METHOD, argn, stack_args(ctx, argn),
1026 do_ret ? &v : NULL, ctx->ei);
1027 if(FAILED(hres))
1028 return hres;
1030 stack_popn(ctx, argn+1);
1031 return do_ret ? stack_push(ctx, &v) : S_OK;
1035 /* ECMA-262 3rd Edition 11.2.3 */
1036 static HRESULT interp_call_member(exec_ctx_t *ctx)
1038 const unsigned argn = get_op_uint(ctx, 0);
1039 const int do_ret = get_op_int(ctx, 1);
1040 IDispatch *obj;
1041 VARIANT v;
1042 DISPID id;
1043 HRESULT hres;
1045 TRACE("%d %d\n", argn, do_ret);
1047 obj = stack_topn_objid(ctx, argn, &id);
1048 if(!obj)
1049 return throw_type_error(ctx->script, ctx->ei, id, NULL);
1051 hres = disp_call(ctx->script, obj, id, DISPATCH_METHOD, argn, stack_args(ctx, argn), do_ret ? &v : NULL, ctx->ei);
1052 if(FAILED(hres))
1053 return hres;
1055 stack_popn(ctx, argn+2);
1056 return do_ret ? stack_push(ctx, &v) : S_OK;
1060 /* ECMA-262 3rd Edition 11.1.1 */
1061 static HRESULT interp_this(exec_ctx_t *ctx)
1063 VARIANT v;
1065 TRACE("\n");
1067 V_VT(&v) = VT_DISPATCH;
1068 V_DISPATCH(&v) = ctx->this_obj;
1069 IDispatch_AddRef(ctx->this_obj);
1070 return stack_push(ctx, &v);
1073 /* ECMA-262 3rd Edition 10.1.4 */
1074 static HRESULT interp_ident(exec_ctx_t *ctx)
1076 const BSTR arg = get_op_bstr(ctx, 0);
1077 exprval_t exprval;
1078 VARIANT v;
1079 HRESULT hres;
1081 TRACE("%s\n", debugstr_w(arg));
1083 hres = identifier_eval(ctx->script, arg, &exprval);
1084 if(FAILED(hres))
1085 return hres;
1087 if(exprval.type == EXPRVAL_INVALID)
1088 return throw_type_error(ctx->script, ctx->ei, JS_E_UNDEFINED_VARIABLE, arg);
1090 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1091 exprval_release(&exprval);
1092 if(FAILED(hres))
1093 return hres;
1095 return stack_push(ctx, &v);
1098 /* ECMA-262 3rd Edition 10.1.4 */
1099 static HRESULT interp_identid(exec_ctx_t *ctx)
1101 const BSTR arg = get_op_bstr(ctx, 0);
1102 const unsigned flags = get_op_uint(ctx, 1);
1103 exprval_t exprval;
1104 HRESULT hres;
1106 TRACE("%s %x\n", debugstr_w(arg), flags);
1108 hres = identifier_eval(ctx->script, arg, &exprval);
1109 if(FAILED(hres))
1110 return hres;
1112 if(exprval.type == EXPRVAL_INVALID && (flags & fdexNameEnsure)) {
1113 DISPID id;
1115 hres = jsdisp_get_id(ctx->script->global, arg, fdexNameEnsure, &id);
1116 if(FAILED(hres))
1117 return hres;
1119 exprval_set_idref(&exprval, to_disp(ctx->script->global), id);
1122 if(exprval.type != EXPRVAL_IDREF) {
1123 WARN("invalid ref\n");
1124 exprval_release(&exprval);
1125 return stack_push_objid(ctx, NULL, JS_E_OBJECT_EXPECTED);
1128 return stack_push_objid(ctx, exprval.u.idref.disp, exprval.u.idref.id);
1131 /* ECMA-262 3rd Edition 7.8.1 */
1132 static HRESULT interp_null(exec_ctx_t *ctx)
1134 VARIANT v;
1136 TRACE("\n");
1138 V_VT(&v) = VT_NULL;
1139 return stack_push(ctx, &v);
1142 /* ECMA-262 3rd Edition 7.8.2 */
1143 static HRESULT interp_bool(exec_ctx_t *ctx)
1145 const int arg = get_op_int(ctx, 0);
1147 TRACE("%s\n", arg ? "true" : "false");
1149 return stack_push_bool(ctx, arg);
1152 /* ECMA-262 3rd Edition 7.8.3 */
1153 static HRESULT interp_int(exec_ctx_t *ctx)
1155 const int arg = get_op_int(ctx, 0);
1156 VARIANT v;
1158 TRACE("%d\n", arg);
1160 V_VT(&v) = VT_I4;
1161 V_I4(&v) = arg;
1162 return stack_push(ctx, &v);
1165 /* ECMA-262 3rd Edition 7.8.3 */
1166 static HRESULT interp_double(exec_ctx_t *ctx)
1168 const double arg = get_op_double(ctx);
1170 TRACE("%lf\n", arg);
1172 return stack_push_number(ctx, arg);
1175 /* ECMA-262 3rd Edition 7.8.4 */
1176 static HRESULT interp_str(exec_ctx_t *ctx)
1178 const WCHAR *str = get_op_str(ctx, 0);
1179 VARIANT v;
1181 TRACE("%s\n", debugstr_w(str));
1183 V_VT(&v) = VT_BSTR;
1184 V_BSTR(&v) = SysAllocString(str);
1185 if(!V_BSTR(&v))
1186 return E_OUTOFMEMORY;
1188 return stack_push(ctx, &v);
1191 /* ECMA-262 3rd Edition 7.8 */
1192 static HRESULT interp_regexp(exec_ctx_t *ctx)
1194 const WCHAR *source = get_op_str(ctx, 0);
1195 const unsigned flags = get_op_uint(ctx, 1);
1196 jsdisp_t *regexp;
1197 VARIANT v;
1198 HRESULT hres;
1200 TRACE("%s %x\n", debugstr_w(source), flags);
1202 hres = create_regexp(ctx->script, source, strlenW(source), flags, &regexp);
1203 if(FAILED(hres))
1204 return hres;
1206 var_set_jsdisp(&v, regexp);
1207 return stack_push(ctx, &v);
1210 /* ECMA-262 3rd Edition 11.1.4 */
1211 static HRESULT interp_carray(exec_ctx_t *ctx)
1213 const unsigned arg = get_op_uint(ctx, 0);
1214 jsdisp_t *array;
1215 VARIANT *v, r;
1216 unsigned i;
1217 HRESULT hres;
1219 TRACE("%u\n", arg);
1221 hres = create_array(ctx->script, arg, &array);
1222 if(FAILED(hres))
1223 return hres;
1225 i = arg;
1226 while(i--) {
1227 v = stack_pop(ctx);
1228 hres = jsdisp_propput_idx(array, i, v, ctx->ei);
1229 VariantClear(v);
1230 if(FAILED(hres)) {
1231 jsdisp_release(array);
1232 return hres;
1236 var_set_jsdisp(&r, array);
1237 return stack_push(ctx, &r);
1240 /* ECMA-262 3rd Edition 11.1.5 */
1241 static HRESULT interp_new_obj(exec_ctx_t *ctx)
1243 jsdisp_t *obj;
1244 VARIANT v;
1245 HRESULT hres;
1247 TRACE("\n");
1249 hres = create_object(ctx->script, NULL, &obj);
1250 if(FAILED(hres))
1251 return hres;
1253 var_set_jsdisp(&v, obj);
1254 return stack_push(ctx, &v);
1257 /* ECMA-262 3rd Edition 11.1.5 */
1258 static HRESULT interp_obj_prop(exec_ctx_t *ctx)
1260 const BSTR name = get_op_bstr(ctx, 0);
1261 jsdisp_t *obj;
1262 VARIANT *v;
1263 HRESULT hres;
1265 TRACE("%s\n", debugstr_w(name));
1267 v = stack_pop(ctx);
1269 assert(V_VT(stack_top(ctx)) == VT_DISPATCH);
1270 obj = as_jsdisp(V_DISPATCH(stack_top(ctx)));
1272 hres = jsdisp_propput_name(obj, name, v, ctx->ei);
1273 VariantClear(v);
1274 return hres;
1277 /* ECMA-262 3rd Edition 11.11 */
1278 static HRESULT interp_cnd_nz(exec_ctx_t *ctx)
1280 const unsigned arg = get_op_uint(ctx, 0);
1281 VARIANT_BOOL b;
1282 HRESULT hres;
1284 TRACE("\n");
1286 hres = to_boolean(stack_top(ctx), &b);
1287 if(FAILED(hres))
1288 return hres;
1290 if(b) {
1291 ctx->ip = arg;
1292 }else {
1293 stack_popn(ctx, 1);
1294 ctx->ip++;
1296 return S_OK;
1299 /* ECMA-262 3rd Edition 11.11 */
1300 static HRESULT interp_cnd_z(exec_ctx_t *ctx)
1302 const unsigned arg = get_op_uint(ctx, 0);
1303 VARIANT_BOOL b;
1304 HRESULT hres;
1306 TRACE("\n");
1308 hres = to_boolean(stack_top(ctx), &b);
1309 if(FAILED(hres))
1310 return hres;
1312 if(b) {
1313 stack_popn(ctx, 1);
1314 ctx->ip++;
1315 }else {
1316 ctx->ip = arg;
1318 return S_OK;
1321 /* ECMA-262 3rd Edition 11.10 */
1322 static HRESULT interp_or(exec_ctx_t *ctx)
1324 INT l, r;
1325 HRESULT hres;
1327 TRACE("\n");
1329 hres = stack_pop_int(ctx, &r);
1330 if(FAILED(hres))
1331 return hres;
1333 hres = stack_pop_int(ctx, &l);
1334 if(FAILED(hres))
1335 return hres;
1337 return stack_push_int(ctx, l|r);
1340 /* ECMA-262 3rd Edition 11.10 */
1341 static HRESULT interp_xor(exec_ctx_t *ctx)
1343 INT l, r;
1344 HRESULT hres;
1346 TRACE("\n");
1348 hres = stack_pop_int(ctx, &r);
1349 if(FAILED(hres))
1350 return hres;
1352 hres = stack_pop_int(ctx, &l);
1353 if(FAILED(hres))
1354 return hres;
1356 return stack_push_int(ctx, l^r);
1359 /* ECMA-262 3rd Edition 11.10 */
1360 static HRESULT interp_and(exec_ctx_t *ctx)
1362 INT l, r;
1363 HRESULT hres;
1365 TRACE("\n");
1367 hres = stack_pop_int(ctx, &r);
1368 if(FAILED(hres))
1369 return hres;
1371 hres = stack_pop_int(ctx, &l);
1372 if(FAILED(hres))
1373 return hres;
1375 return stack_push_int(ctx, l&r);
1378 /* ECMA-262 3rd Edition 11.8.6 */
1379 static HRESULT interp_instanceof(exec_ctx_t *ctx)
1381 jsdisp_t *obj, *iter, *tmp = NULL;
1382 VARIANT prot, *v;
1383 BOOL ret = FALSE;
1384 HRESULT hres;
1386 static const WCHAR prototypeW[] = {'p','r','o','t','o','t', 'y', 'p','e',0};
1388 v = stack_pop(ctx);
1389 if(V_VT(v) != VT_DISPATCH || !V_DISPATCH(v)) {
1390 VariantClear(v);
1391 return throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1394 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1395 IDispatch_Release(V_DISPATCH(v));
1396 if(!obj) {
1397 FIXME("non-jsdisp objects not supported\n");
1398 return E_FAIL;
1401 if(is_class(obj, JSCLASS_FUNCTION)) {
1402 hres = jsdisp_propget_name(obj, prototypeW, &prot, ctx->ei);
1403 }else {
1404 hres = throw_type_error(ctx->script, ctx->ei, JS_E_FUNCTION_EXPECTED, NULL);
1406 jsdisp_release(obj);
1407 if(FAILED(hres))
1408 return hres;
1410 v = stack_pop(ctx);
1412 if(V_VT(&prot) == VT_DISPATCH) {
1413 if(V_VT(v) == VT_DISPATCH)
1414 tmp = iface_to_jsdisp((IUnknown*)V_DISPATCH(v));
1415 for(iter = tmp; !ret && iter; iter = iter->prototype) {
1416 hres = disp_cmp(V_DISPATCH(&prot), to_disp(iter), &ret);
1417 if(FAILED(hres))
1418 break;
1421 if(tmp)
1422 jsdisp_release(tmp);
1423 }else {
1424 FIXME("prototype is not an object\n");
1425 hres = E_FAIL;
1428 VariantClear(&prot);
1429 VariantClear(v);
1430 if(FAILED(hres))
1431 return hres;
1433 return stack_push_bool(ctx, ret);
1436 /* ECMA-262 3rd Edition 11.8.7 */
1437 static HRESULT interp_in(exec_ctx_t *ctx)
1439 VARIANT *obj, *v;
1440 DISPID id = 0;
1441 BOOL ret;
1442 BSTR str;
1443 HRESULT hres;
1445 TRACE("\n");
1447 obj = stack_pop(ctx);
1448 v = stack_pop(ctx);
1450 if(V_VT(obj) != VT_DISPATCH || !V_DISPATCH(obj)) {
1451 VariantClear(obj);
1452 VariantClear(v);
1453 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1456 hres = to_string(ctx->script, v, ctx->ei, &str);
1457 VariantClear(v);
1458 if(FAILED(hres)) {
1459 IDispatch_Release(V_DISPATCH(obj));
1460 return hres;
1463 hres = disp_get_id(ctx->script, V_DISPATCH(obj), str, 0, &id);
1464 IDispatch_Release(V_DISPATCH(obj));
1465 SysFreeString(str);
1466 if(SUCCEEDED(hres))
1467 ret = TRUE;
1468 else if(hres == DISP_E_UNKNOWNNAME)
1469 ret = FALSE;
1470 else
1471 return hres;
1473 return stack_push_bool(ctx, ret);
1476 /* ECMA-262 3rd Edition 11.6.1 */
1477 static HRESULT add_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, VARIANT *retv)
1479 VARIANT r, l;
1480 HRESULT hres;
1482 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
1483 if(FAILED(hres))
1484 return hres;
1486 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
1487 if(FAILED(hres)) {
1488 VariantClear(&l);
1489 return hres;
1492 if(V_VT(&l) == VT_BSTR || V_VT(&r) == VT_BSTR) {
1493 BSTR lstr = NULL, rstr = NULL;
1495 if(V_VT(&l) == VT_BSTR)
1496 lstr = V_BSTR(&l);
1497 else
1498 hres = to_string(ctx, &l, ei, &lstr);
1500 if(SUCCEEDED(hres)) {
1501 if(V_VT(&r) == VT_BSTR)
1502 rstr = V_BSTR(&r);
1503 else
1504 hres = to_string(ctx, &r, ei, &rstr);
1507 if(SUCCEEDED(hres)) {
1508 int len1, len2;
1510 len1 = SysStringLen(lstr);
1511 len2 = SysStringLen(rstr);
1513 V_VT(retv) = VT_BSTR;
1514 V_BSTR(retv) = SysAllocStringLen(NULL, len1+len2);
1515 if(len1)
1516 memcpy(V_BSTR(retv), lstr, len1*sizeof(WCHAR));
1517 if(len2)
1518 memcpy(V_BSTR(retv)+len1, rstr, len2*sizeof(WCHAR));
1519 V_BSTR(retv)[len1+len2] = 0;
1522 if(V_VT(&l) != VT_BSTR)
1523 SysFreeString(lstr);
1524 if(V_VT(&r) != VT_BSTR)
1525 SysFreeString(rstr);
1526 }else {
1527 double nl, nr;
1529 hres = to_number(ctx, &l, ei, &nl);
1530 if(SUCCEEDED(hres)) {
1531 hres = to_number(ctx, &r, ei, &nr);
1532 if(SUCCEEDED(hres))
1533 num_set_val(retv, nl + nr);
1537 VariantClear(&r);
1538 VariantClear(&l);
1539 return hres;
1542 /* ECMA-262 3rd Edition 11.6.1 */
1543 static HRESULT interp_add(exec_ctx_t *ctx)
1545 VARIANT *l, *r, ret;
1546 HRESULT hres;
1548 r = stack_pop(ctx);
1549 l = stack_pop(ctx);
1551 TRACE("%s + %s\n", debugstr_variant(l), debugstr_variant(r));
1553 hres = add_eval(ctx->script, l, r, ctx->ei, &ret);
1554 VariantClear(l);
1555 VariantClear(r);
1556 if(FAILED(hres))
1557 return hres;
1559 return stack_push(ctx, &ret);
1562 /* ECMA-262 3rd Edition 11.6.2 */
1563 static HRESULT interp_sub(exec_ctx_t *ctx)
1565 double l, r;
1566 HRESULT hres;
1568 TRACE("\n");
1570 hres = stack_pop_number(ctx, &r);
1571 if(FAILED(hres))
1572 return hres;
1574 hres = stack_pop_number(ctx, &l);
1575 if(FAILED(hres))
1576 return hres;
1578 return stack_push_number(ctx, l-r);
1581 /* ECMA-262 3rd Edition 11.5.1 */
1582 static HRESULT interp_mul(exec_ctx_t *ctx)
1584 double l, r;
1585 HRESULT hres;
1587 TRACE("\n");
1589 hres = stack_pop_number(ctx, &r);
1590 if(FAILED(hres))
1591 return hres;
1593 hres = stack_pop_number(ctx, &l);
1594 if(FAILED(hres))
1595 return hres;
1597 return stack_push_number(ctx, l*r);
1600 /* ECMA-262 3rd Edition 11.5.2 */
1601 static HRESULT interp_div(exec_ctx_t *ctx)
1603 double l, r;
1604 HRESULT hres;
1606 TRACE("\n");
1608 hres = stack_pop_number(ctx, &r);
1609 if(FAILED(hres))
1610 return hres;
1612 hres = stack_pop_number(ctx, &l);
1613 if(FAILED(hres))
1614 return hres;
1616 return stack_push_number(ctx, l/r);
1619 /* ECMA-262 3rd Edition 11.5.3 */
1620 static HRESULT interp_mod(exec_ctx_t *ctx)
1622 double l, r;
1623 HRESULT hres;
1625 TRACE("\n");
1627 hres = stack_pop_number(ctx, &r);
1628 if(FAILED(hres))
1629 return hres;
1631 hres = stack_pop_number(ctx, &l);
1632 if(FAILED(hres))
1633 return hres;
1635 return stack_push_number(ctx, fmod(l, r));
1638 /* ECMA-262 3rd Edition 11.4.2 */
1639 static HRESULT interp_delete(exec_ctx_t *ctx)
1641 VARIANT *obj_var, *name_var;
1642 IDispatchEx *dispex;
1643 IDispatch *obj;
1644 BSTR name;
1645 BOOL ret;
1646 HRESULT hres;
1648 TRACE("\n");
1650 name_var = stack_pop(ctx);
1651 obj_var = stack_pop(ctx);
1653 hres = to_object(ctx->script, obj_var, &obj);
1654 VariantClear(obj_var);
1655 if(FAILED(hres)) {
1656 VariantClear(name_var);
1657 return hres;
1660 hres = to_string(ctx->script, name_var, ctx->ei, &name);
1661 VariantClear(name_var);
1662 if(FAILED(hres)) {
1663 IDispatch_Release(obj);
1664 return hres;
1667 hres = IDispatch_QueryInterface(obj, &IID_IDispatchEx, (void**)&dispex);
1668 if(SUCCEEDED(hres)) {
1669 hres = IDispatchEx_DeleteMemberByName(dispex, name, make_grfdex(ctx->script, fdexNameCaseSensitive));
1670 ret = TRUE;
1671 IDispatchEx_Release(dispex);
1672 }else {
1673 hres = S_OK;
1674 ret = FALSE;
1677 IDispatch_Release(obj);
1678 SysFreeString(name);
1679 if(FAILED(hres))
1680 return hres;
1682 return stack_push_bool(ctx, ret);
1685 /* ECMA-262 3rd Edition 11.4.2 */
1686 static HRESULT interp_delete_ident(exec_ctx_t *ctx)
1688 const BSTR arg = get_op_bstr(ctx, 0);
1689 IDispatchEx *dispex;
1690 exprval_t exprval;
1691 BOOL ret = FALSE;
1692 HRESULT hres;
1694 TRACE("%s\n", debugstr_w(arg));
1696 hres = identifier_eval(ctx->script, arg, &exprval);
1697 if(FAILED(hres))
1698 return hres;
1700 if(exprval.type != EXPRVAL_IDREF) {
1701 FIXME("Unsupported exprval\n");
1702 exprval_release(&exprval);
1703 return E_NOTIMPL;
1706 hres = IDispatch_QueryInterface(exprval.u.idref.disp, &IID_IDispatchEx, (void**)&dispex);
1707 IDispatch_Release(exprval.u.idref.disp);
1708 if(SUCCEEDED(hres)) {
1709 hres = IDispatchEx_DeleteMemberByDispID(dispex, exprval.u.idref.id);
1710 IDispatchEx_Release(dispex);
1711 if(FAILED(hres))
1712 return hres;
1714 ret = TRUE;
1717 return stack_push_bool(ctx, ret);
1720 /* ECMA-262 3rd Edition 11.4.2 */
1721 static HRESULT interp_void(exec_ctx_t *ctx)
1723 VARIANT v;
1725 TRACE("\n");
1727 stack_popn(ctx, 1);
1729 V_VT(&v) = VT_EMPTY;
1730 return stack_push(ctx, &v);
1733 /* ECMA-262 3rd Edition 11.4.3 */
1734 static HRESULT typeof_string(VARIANT *v, const WCHAR **ret)
1736 switch(V_VT(v)) {
1737 case VT_EMPTY:
1738 *ret = undefinedW;
1739 break;
1740 case VT_NULL:
1741 *ret = objectW;
1742 break;
1743 case VT_BOOL:
1744 *ret = booleanW;
1745 break;
1746 case VT_I4:
1747 case VT_R8:
1748 *ret = numberW;
1749 break;
1750 case VT_BSTR:
1751 *ret = stringW;
1752 break;
1753 case VT_DISPATCH: {
1754 jsdisp_t *dispex;
1756 if(V_DISPATCH(v) && (dispex = iface_to_jsdisp((IUnknown*)V_DISPATCH(v)))) {
1757 *ret = is_class(dispex, JSCLASS_FUNCTION) ? functionW : objectW;
1758 jsdisp_release(dispex);
1759 }else {
1760 *ret = objectW;
1762 break;
1764 default:
1765 FIXME("unhandled vt %d\n", V_VT(v));
1766 return E_NOTIMPL;
1769 return S_OK;
1772 /* ECMA-262 3rd Edition 11.4.3 */
1773 static HRESULT interp_typeofid(exec_ctx_t *ctx)
1775 const WCHAR *ret;
1776 IDispatch *obj;
1777 VARIANT v;
1778 DISPID id;
1779 HRESULT hres;
1781 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
1783 TRACE("\n");
1785 obj = stack_pop_objid(ctx, &id);
1786 if(!obj)
1787 return stack_push_string(ctx, undefinedW);
1789 V_VT(&v) = VT_EMPTY;
1790 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1791 IDispatch_Release(obj);
1792 if(FAILED(hres))
1793 return stack_push_string(ctx, unknownW);
1795 hres = typeof_string(&v, &ret);
1796 VariantClear(&v);
1797 if(FAILED(hres))
1798 return hres;
1800 return stack_push_string(ctx, ret);
1803 /* ECMA-262 3rd Edition 11.4.3 */
1804 static HRESULT interp_typeofident(exec_ctx_t *ctx)
1806 const BSTR arg = get_op_bstr(ctx, 0);
1807 exprval_t exprval;
1808 const WCHAR *ret;
1809 VARIANT v;
1810 HRESULT hres;
1812 TRACE("%s\n", debugstr_w(arg));
1814 hres = identifier_eval(ctx->script, arg, &exprval);
1815 if(FAILED(hres))
1816 return hres;
1818 if(exprval.type == EXPRVAL_INVALID) {
1819 hres = stack_push_string(ctx, undefinedW);
1820 exprval_release(&exprval);
1821 return hres;
1824 hres = exprval_to_value(ctx->script, &exprval, ctx->ei, &v);
1825 exprval_release(&exprval);
1826 if(FAILED(hres))
1827 return hres;
1829 hres = typeof_string(&v, &ret);
1830 VariantClear(&v);
1831 if(FAILED(hres))
1832 return hres;
1834 return stack_push_string(ctx, ret);
1837 /* ECMA-262 3rd Edition 11.4.3 */
1838 static HRESULT interp_typeof(exec_ctx_t *ctx)
1840 const WCHAR *ret;
1841 VARIANT *v;
1842 HRESULT hres;
1844 TRACE("\n");
1846 v = stack_pop(ctx);
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.7 */
1856 static HRESULT interp_minus(exec_ctx_t *ctx)
1858 double n;
1859 HRESULT hres;
1861 TRACE("\n");
1863 hres = stack_pop_number(ctx, &n);
1864 if(FAILED(hres))
1865 return hres;
1867 return stack_push_number(ctx, -n);
1870 /* ECMA-262 3rd Edition 11.4.6 */
1871 static HRESULT interp_tonum(exec_ctx_t *ctx)
1873 VARIANT *v;
1874 double n;
1875 HRESULT hres;
1877 TRACE("\n");
1879 v = stack_pop(ctx);
1880 hres = to_number(ctx->script, v, ctx->ei, &n);
1881 VariantClear(v);
1882 if(FAILED(hres))
1883 return hres;
1885 return stack_push_number(ctx, n);
1888 /* ECMA-262 3rd Edition 11.3.1 */
1889 static HRESULT interp_postinc(exec_ctx_t *ctx)
1891 const int arg = get_op_int(ctx, 0);
1892 IDispatch *obj;
1893 DISPID id;
1894 VARIANT v;
1895 HRESULT hres;
1897 TRACE("%d\n", arg);
1899 obj = stack_pop_objid(ctx, &id);
1900 if(!obj)
1901 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1903 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1904 if(SUCCEEDED(hres)) {
1905 VARIANT inc;
1906 double n;
1908 hres = to_number(ctx->script, &v, ctx->ei, &n);
1909 if(SUCCEEDED(hres)) {
1910 num_set_val(&inc, n+(double)arg);
1911 hres = disp_propput(ctx->script, obj, id, &inc, ctx->ei);
1913 if(FAILED(hres))
1914 VariantClear(&v);
1916 IDispatch_Release(obj);
1917 if(FAILED(hres))
1918 return hres;
1920 return stack_push(ctx, &v);
1923 /* ECMA-262 3rd Edition 11.4.4, 11.4.5 */
1924 static HRESULT interp_preinc(exec_ctx_t *ctx)
1926 const int arg = get_op_int(ctx, 0);
1927 IDispatch *obj;
1928 DISPID id;
1929 VARIANT v;
1930 HRESULT hres;
1932 TRACE("%d\n", arg);
1934 obj = stack_pop_objid(ctx, &id);
1935 if(!obj)
1936 return throw_type_error(ctx->script, ctx->ei, JS_E_OBJECT_EXPECTED, NULL);
1938 hres = disp_propget(ctx->script, obj, id, &v, ctx->ei);
1939 if(SUCCEEDED(hres)) {
1940 double n;
1942 hres = to_number(ctx->script, &v, ctx->ei, &n);
1943 VariantClear(&v);
1944 if(SUCCEEDED(hres)) {
1945 num_set_val(&v, n+(double)arg);
1946 hres = disp_propput(ctx->script, obj, id, &v, ctx->ei);
1949 IDispatch_Release(obj);
1950 if(FAILED(hres))
1951 return hres;
1953 return stack_push(ctx, &v);
1956 /* ECMA-262 3rd Edition 11.9.3 */
1957 static HRESULT equal_values(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, jsexcept_t *ei, BOOL *ret)
1959 if(V_VT(lval) == V_VT(rval) || (is_num_vt(V_VT(lval)) && is_num_vt(V_VT(rval))))
1960 return equal2_values(lval, rval, ret);
1962 /* FIXME: NULL disps should be handled in more general way */
1963 if(V_VT(lval) == VT_DISPATCH && !V_DISPATCH(lval)) {
1964 VARIANT v;
1965 V_VT(&v) = VT_NULL;
1966 return equal_values(ctx, &v, rval, ei, ret);
1969 if(V_VT(rval) == VT_DISPATCH && !V_DISPATCH(rval)) {
1970 VARIANT v;
1971 V_VT(&v) = VT_NULL;
1972 return equal_values(ctx, lval, &v, ei, ret);
1975 if((V_VT(lval) == VT_NULL && V_VT(rval) == VT_EMPTY) ||
1976 (V_VT(lval) == VT_EMPTY && V_VT(rval) == VT_NULL)) {
1977 *ret = TRUE;
1978 return S_OK;
1981 if(V_VT(lval) == VT_BSTR && is_num_vt(V_VT(rval))) {
1982 VARIANT v;
1983 double n;
1984 HRESULT hres;
1986 hres = to_number(ctx, lval, ei, &n);
1987 if(FAILED(hres))
1988 return hres;
1990 /* FIXME: optimize */
1991 num_set_val(&v, n);
1993 return equal_values(ctx, &v, rval, ei, ret);
1996 if(V_VT(rval) == VT_BSTR && is_num_vt(V_VT(lval))) {
1997 VARIANT v;
1998 double n;
1999 HRESULT hres;
2001 hres = to_number(ctx, rval, ei, &n);
2002 if(FAILED(hres))
2003 return hres;
2005 /* FIXME: optimize */
2006 num_set_val(&v, n);
2008 return equal_values(ctx, lval, &v, ei, ret);
2011 if(V_VT(rval) == VT_BOOL) {
2012 VARIANT v;
2014 num_set_int(&v, V_BOOL(rval) ? 1 : 0);
2015 return equal_values(ctx, lval, &v, ei, ret);
2018 if(V_VT(lval) == VT_BOOL) {
2019 VARIANT v;
2021 num_set_int(&v, V_BOOL(lval) ? 1 : 0);
2022 return equal_values(ctx, &v, rval, ei, ret);
2026 if(V_VT(rval) == VT_DISPATCH && (V_VT(lval) == VT_BSTR || is_num_vt(V_VT(lval)))) {
2027 VARIANT v;
2028 HRESULT hres;
2030 hres = to_primitive(ctx, rval, ei, &v, NO_HINT);
2031 if(FAILED(hres))
2032 return hres;
2034 hres = equal_values(ctx, lval, &v, ei, ret);
2036 VariantClear(&v);
2037 return hres;
2041 if(V_VT(lval) == VT_DISPATCH && (V_VT(rval) == VT_BSTR || is_num_vt(V_VT(rval)))) {
2042 VARIANT v;
2043 HRESULT hres;
2045 hres = to_primitive(ctx, lval, ei, &v, NO_HINT);
2046 if(FAILED(hres))
2047 return hres;
2049 hres = equal_values(ctx, &v, rval, ei, ret);
2051 VariantClear(&v);
2052 return hres;
2056 *ret = FALSE;
2057 return S_OK;
2060 /* ECMA-262 3rd Edition 11.9.1 */
2061 static HRESULT interp_eq(exec_ctx_t *ctx)
2063 VARIANT *l, *r;
2064 BOOL b;
2065 HRESULT hres;
2067 r = stack_pop(ctx);
2068 l = stack_pop(ctx);
2070 TRACE("%s == %s\n", debugstr_variant(l), debugstr_variant(r));
2072 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2073 VariantClear(l);
2074 VariantClear(r);
2075 if(FAILED(hres))
2076 return hres;
2078 return stack_push_bool(ctx, b);
2081 /* ECMA-262 3rd Edition 11.9.2 */
2082 static HRESULT interp_neq(exec_ctx_t *ctx)
2084 VARIANT *l, *r;
2085 BOOL b;
2086 HRESULT hres;
2088 r = stack_pop(ctx);
2089 l = stack_pop(ctx);
2091 TRACE("%s != %s\n", debugstr_variant(l), debugstr_variant(r));
2093 hres = equal_values(ctx->script, l, r, ctx->ei, &b);
2094 VariantClear(l);
2095 VariantClear(r);
2096 if(FAILED(hres))
2097 return hres;
2099 return stack_push_bool(ctx, !b);
2102 /* ECMA-262 3rd Edition 11.9.4 */
2103 static HRESULT interp_eq2(exec_ctx_t *ctx)
2105 VARIANT *l, *r;
2106 BOOL b;
2107 HRESULT hres;
2109 TRACE("\n");
2111 r = stack_pop(ctx);
2112 l = stack_pop(ctx);
2114 hres = equal2_values(r, l, &b);
2115 VariantClear(l);
2116 VariantClear(r);
2117 if(FAILED(hres))
2118 return hres;
2120 return stack_push_bool(ctx, b);
2123 /* ECMA-262 3rd Edition 11.9.5 */
2124 static HRESULT interp_neq2(exec_ctx_t *ctx)
2126 VARIANT *l, *r;
2127 BOOL b;
2128 HRESULT hres;
2130 TRACE("\n");
2132 r = stack_pop(ctx);
2133 l = stack_pop(ctx);
2135 hres = equal2_values(r, l, &b);
2136 VariantClear(l);
2137 VariantClear(r);
2138 if(FAILED(hres))
2139 return hres;
2141 return stack_push_bool(ctx, !b);
2144 /* ECMA-262 3rd Edition 11.8.5 */
2145 static HRESULT less_eval(script_ctx_t *ctx, VARIANT *lval, VARIANT *rval, BOOL greater, jsexcept_t *ei, BOOL *ret)
2147 double ln, rn;
2148 VARIANT l, r;
2149 HRESULT hres;
2151 hres = to_primitive(ctx, lval, ei, &l, NO_HINT);
2152 if(FAILED(hres))
2153 return hres;
2155 hres = to_primitive(ctx, rval, ei, &r, NO_HINT);
2156 if(FAILED(hres)) {
2157 VariantClear(&l);
2158 return hres;
2161 if(V_VT(&l) == VT_BSTR && V_VT(&r) == VT_BSTR) {
2162 *ret = (strcmpW(V_BSTR(&l), V_BSTR(&r)) < 0) ^ greater;
2163 SysFreeString(V_BSTR(&l));
2164 SysFreeString(V_BSTR(&r));
2165 return S_OK;
2168 hres = to_number(ctx, &l, ei, &ln);
2169 VariantClear(&l);
2170 if(SUCCEEDED(hres))
2171 hres = to_number(ctx, &r, ei, &rn);
2172 VariantClear(&r);
2173 if(FAILED(hres))
2174 return hres;
2176 *ret = !isnan(ln) && !isnan(rn) && ((ln < rn) ^ greater);
2177 return S_OK;
2180 /* ECMA-262 3rd Edition 11.8.1 */
2181 static HRESULT interp_lt(exec_ctx_t *ctx)
2183 VARIANT *l, *r;
2184 BOOL b;
2185 HRESULT hres;
2187 r = stack_pop(ctx);
2188 l = stack_pop(ctx);
2190 TRACE("%s < %s\n", debugstr_variant(l), debugstr_variant(r));
2192 hres = less_eval(ctx->script, l, r, FALSE, ctx->ei, &b);
2193 VariantClear(l);
2194 VariantClear(r);
2195 if(FAILED(hres))
2196 return hres;
2198 return stack_push_bool(ctx, b);
2201 /* ECMA-262 3rd Edition 11.8.1 */
2202 static HRESULT interp_lteq(exec_ctx_t *ctx)
2204 VARIANT *l, *r;
2205 BOOL b;
2206 HRESULT hres;
2208 r = stack_pop(ctx);
2209 l = stack_pop(ctx);
2211 TRACE("%s <= %s\n", debugstr_variant(l), debugstr_variant(r));
2213 hres = less_eval(ctx->script, r, l, TRUE, ctx->ei, &b);
2214 VariantClear(l);
2215 VariantClear(r);
2216 if(FAILED(hres))
2217 return hres;
2219 return stack_push_bool(ctx, b);
2222 /* ECMA-262 3rd Edition 11.8.2 */
2223 static HRESULT interp_gt(exec_ctx_t *ctx)
2225 VARIANT *l, *r;
2226 BOOL b;
2227 HRESULT hres;
2229 r = stack_pop(ctx);
2230 l = stack_pop(ctx);
2232 TRACE("%s > %s\n", debugstr_variant(l), debugstr_variant(r));
2234 hres = less_eval(ctx->script, r, l, FALSE, ctx->ei, &b);
2235 VariantClear(l);
2236 VariantClear(r);
2237 if(FAILED(hres))
2238 return hres;
2240 return stack_push_bool(ctx, b);
2243 /* ECMA-262 3rd Edition 11.8.4 */
2244 static HRESULT interp_gteq(exec_ctx_t *ctx)
2246 VARIANT *l, *r;
2247 BOOL b;
2248 HRESULT hres;
2250 r = stack_pop(ctx);
2251 l = stack_pop(ctx);
2253 TRACE("%s >= %s\n", debugstr_variant(l), debugstr_variant(r));
2255 hres = less_eval(ctx->script, l, r, TRUE, ctx->ei, &b);
2256 VariantClear(l);
2257 VariantClear(r);
2258 if(FAILED(hres))
2259 return hres;
2261 return stack_push_bool(ctx, b);
2264 /* ECMA-262 3rd Edition 11.4.8 */
2265 static HRESULT interp_bneg(exec_ctx_t *ctx)
2267 VARIANT *v;
2268 INT i;
2269 HRESULT hres;
2271 TRACE("\n");
2273 v = stack_pop(ctx);
2274 hres = to_int32(ctx->script, v, ctx->ei, &i);
2275 VariantClear(v);
2276 if(FAILED(hres))
2277 return hres;
2279 return stack_push_int(ctx, ~i);
2282 /* ECMA-262 3rd Edition 11.4.9 */
2283 static HRESULT interp_neg(exec_ctx_t *ctx)
2285 VARIANT *v;
2286 VARIANT_BOOL b;
2287 HRESULT hres;
2289 TRACE("\n");
2291 v = stack_pop(ctx);
2292 hres = to_boolean(v, &b);
2293 VariantClear(v);
2294 if(FAILED(hres))
2295 return hres;
2297 return stack_push_bool(ctx, !b);
2300 /* ECMA-262 3rd Edition 11.7.1 */
2301 static HRESULT interp_lshift(exec_ctx_t *ctx)
2303 DWORD r;
2304 INT l;
2305 HRESULT hres;
2307 hres = stack_pop_uint(ctx, &r);
2308 if(FAILED(hres))
2309 return hres;
2311 hres = stack_pop_int(ctx, &l);
2312 if(FAILED(hres))
2313 return hres;
2315 return stack_push_int(ctx, l << (r&0x1f));
2318 /* ECMA-262 3rd Edition 11.7.2 */
2319 static HRESULT interp_rshift(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.3 */
2337 static HRESULT interp_rshift2(exec_ctx_t *ctx)
2339 DWORD r, l;
2340 HRESULT hres;
2342 hres = stack_pop_uint(ctx, &r);
2343 if(FAILED(hres))
2344 return hres;
2346 hres = stack_pop_uint(ctx, &l);
2347 if(FAILED(hres))
2348 return hres;
2350 return stack_push_int(ctx, l >> (r&0x1f));
2353 /* ECMA-262 3rd Edition 11.13.1 */
2354 static HRESULT interp_assign(exec_ctx_t *ctx)
2356 IDispatch *disp;
2357 DISPID id;
2358 VARIANT *v;
2359 HRESULT hres;
2361 TRACE("\n");
2363 v = stack_pop(ctx);
2364 disp = stack_pop_objid(ctx, &id);
2366 if(!disp)
2367 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2369 hres = disp_propput(ctx->script, disp, id, v, ctx->ei);
2370 IDispatch_Release(disp);
2371 if(FAILED(hres)) {
2372 VariantClear(v);
2373 return hres;
2376 return stack_push(ctx, v);
2379 /* JScript extension */
2380 static HRESULT interp_assign_call(exec_ctx_t *ctx)
2382 const unsigned arg = get_op_uint(ctx, 0);
2383 IDispatch *disp;
2384 VARIANT *v;
2385 DISPID id;
2386 HRESULT hres;
2388 TRACE("%u\n", arg);
2390 disp = stack_topn_objid(ctx, arg+1, &id);
2391 if(!disp)
2392 return throw_reference_error(ctx->script, ctx->ei, JS_E_ILLEGAL_ASSIGN, NULL);
2394 hres = disp_call(ctx->script, disp, id, DISPATCH_PROPERTYPUT, arg+1, stack_args(ctx,arg+1), NULL, ctx->ei);
2395 if(FAILED(hres))
2396 return hres;
2398 v = stack_pop(ctx);
2399 stack_popn(ctx, arg+2);
2400 return stack_push(ctx, v);
2403 static HRESULT interp_undefined(exec_ctx_t *ctx)
2405 VARIANT v;
2407 TRACE("\n");
2409 V_VT(&v) = VT_EMPTY;
2410 return stack_push(ctx, &v);
2413 static HRESULT interp_jmp(exec_ctx_t *ctx)
2415 const unsigned arg = get_op_uint(ctx, 0);
2417 TRACE("%u\n", arg);
2419 ctx->ip = arg;
2420 return S_OK;
2423 static HRESULT interp_jmp_z(exec_ctx_t *ctx)
2425 const unsigned arg = get_op_uint(ctx, 0);
2426 VARIANT_BOOL b;
2427 VARIANT *v;
2428 HRESULT hres;
2430 TRACE("\n");
2432 v = stack_pop(ctx);
2433 hres = to_boolean(v, &b);
2434 VariantClear(v);
2435 if(FAILED(hres))
2436 return hres;
2438 if(b)
2439 ctx->ip++;
2440 else
2441 ctx->ip = arg;
2442 return S_OK;
2445 static HRESULT interp_pop(exec_ctx_t *ctx)
2447 TRACE("\n");
2449 stack_popn(ctx, 1);
2450 return S_OK;
2453 static HRESULT interp_ret(exec_ctx_t *ctx)
2455 TRACE("\n");
2457 ctx->ip = -1;
2458 return S_OK;
2461 typedef HRESULT (*op_func_t)(exec_ctx_t*);
2463 static const op_func_t op_funcs[] = {
2464 #define X(x,a,b,c) interp_##x,
2465 OP_LIST
2466 #undef X
2469 static const unsigned op_move[] = {
2470 #define X(a,x,b,c) x,
2471 OP_LIST
2472 #undef X
2475 static HRESULT unwind_exception(exec_ctx_t *ctx)
2477 except_frame_t *except_frame;
2478 VARIANT except_val;
2479 BSTR ident;
2480 HRESULT hres;
2482 except_frame = ctx->except_frame;
2483 ctx->except_frame = except_frame->next;
2485 assert(except_frame->stack_top <= ctx->top);
2486 stack_popn(ctx, ctx->top - except_frame->stack_top);
2488 while(except_frame->scope != ctx->scope_chain)
2489 scope_pop(&ctx->scope_chain);
2491 ctx->ip = except_frame->catch_off;
2493 except_val = ctx->ei->var;
2494 memset(ctx->ei, 0, sizeof(*ctx->ei));
2496 ident = except_frame->ident;
2497 heap_free(except_frame);
2499 if(ident) {
2500 jsdisp_t *scope_obj;
2502 hres = create_dispex(ctx->script, NULL, NULL, &scope_obj);
2503 if(SUCCEEDED(hres)) {
2504 hres = jsdisp_propput_name(scope_obj, ident, &except_val, ctx->ei);
2505 if(FAILED(hres))
2506 jsdisp_release(scope_obj);
2508 VariantClear(&except_val);
2509 if(FAILED(hres))
2510 return hres;
2512 hres = scope_push(ctx->scope_chain, scope_obj, &ctx->scope_chain);
2513 jsdisp_release(scope_obj);
2514 }else {
2515 VARIANT v;
2517 hres = stack_push(ctx, &except_val);
2518 if(FAILED(hres))
2519 return hres;
2521 hres = stack_push_bool(ctx, FALSE);
2522 if(FAILED(hres))
2523 return hres;
2525 V_VT(&v) = VT_EMPTY;
2526 hres = stack_push(ctx, &v);
2529 return hres;
2532 static HRESULT enter_bytecode(script_ctx_t *ctx, bytecode_t *code, function_code_t *func, jsexcept_t *ei, VARIANT *ret)
2534 exec_ctx_t *exec_ctx = ctx->exec_ctx;
2535 except_frame_t *prev_except_frame;
2536 function_code_t *prev_func;
2537 unsigned prev_ip, prev_top;
2538 scope_chain_t *prev_scope;
2539 bytecode_t *prev_code;
2540 jsexcept_t *prev_ei;
2541 jsop_t op;
2542 HRESULT hres = S_OK;
2544 TRACE("\n");
2546 prev_top = exec_ctx->top;
2547 prev_scope = exec_ctx->scope_chain;
2548 prev_except_frame = exec_ctx->except_frame;
2549 prev_ip = exec_ctx->ip;
2550 prev_ei = exec_ctx->ei;
2551 prev_code = exec_ctx->code;
2552 prev_func = exec_ctx->func_code;
2553 exec_ctx->ip = func->instr_off;
2554 exec_ctx->ei = ei;
2555 exec_ctx->except_frame = NULL;
2556 exec_ctx->code = code;
2557 exec_ctx->func_code = func;
2559 while(exec_ctx->ip != -1) {
2560 op = code->instrs[exec_ctx->ip].op;
2561 hres = op_funcs[op](exec_ctx);
2562 if(FAILED(hres)) {
2563 TRACE("EXCEPTION\n");
2565 if(!exec_ctx->except_frame)
2566 break;
2568 hres = unwind_exception(exec_ctx);
2569 if(FAILED(hres))
2570 break;
2571 }else {
2572 exec_ctx->ip += op_move[op];
2576 exec_ctx->ip = prev_ip;
2577 exec_ctx->ei = prev_ei;
2578 exec_ctx->except_frame = prev_except_frame;
2579 exec_ctx->code = prev_code;
2580 exec_ctx->func_code = prev_func;
2582 if(FAILED(hres)) {
2583 while(exec_ctx->scope_chain != prev_scope)
2584 scope_pop(&exec_ctx->scope_chain);
2585 stack_popn(exec_ctx, exec_ctx->top-prev_top);
2586 return hres;
2589 assert(exec_ctx->top == prev_top+1 || exec_ctx->top == prev_top);
2590 assert(exec_ctx->scope_chain == prev_scope);
2592 if(exec_ctx->top == prev_top)
2593 V_VT(ret) = VT_EMPTY;
2594 else
2595 *ret = *stack_pop(exec_ctx);
2596 return S_OK;
2599 HRESULT exec_source(exec_ctx_t *ctx, bytecode_t *code, function_code_t *func, BOOL from_eval,
2600 jsexcept_t *ei, VARIANT *retv)
2602 exec_ctx_t *prev_ctx;
2603 VARIANT val;
2604 unsigned i;
2605 HRESULT hres = S_OK;
2607 for(i = 0; i < func->func_cnt; i++) {
2608 jsdisp_t *func_obj;
2609 VARIANT var;
2611 if(!func->funcs[i].name)
2612 continue;
2614 hres = create_source_function(ctx->script, code, func->funcs+i, ctx->scope_chain, &func_obj);
2615 if(FAILED(hres))
2616 return hres;
2618 var_set_jsdisp(&var, func_obj);
2619 hres = jsdisp_propput_name(ctx->var_disp, func->funcs[i].name, &var, ei);
2620 jsdisp_release(func_obj);
2621 if(FAILED(hres))
2622 return hres;
2625 for(i=0; i < func->var_cnt; i++) {
2626 if(!ctx->is_global || !lookup_global_members(ctx->script, func->variables[i], NULL)) {
2627 DISPID id = 0;
2629 hres = jsdisp_get_id(ctx->var_disp, func->variables[i], fdexNameEnsure, &id);
2630 if(FAILED(hres))
2631 return hres;
2635 prev_ctx = ctx->script->exec_ctx;
2636 ctx->script->exec_ctx = ctx;
2638 hres = enter_bytecode(ctx->script, code, func, ei, &val);
2639 assert(ctx->script->exec_ctx == ctx);
2640 ctx->script->exec_ctx = prev_ctx;
2641 if(FAILED(hres))
2642 return hres;
2644 if(retv)
2645 *retv = val;
2646 else
2647 VariantClear(&val);
2648 return S_OK;