vbscript: Evaluate object expression before value expression in member assignment.
[wine/multimedia.git] / dlls / vbscript / interp.c
blob6e4827dd540c1925c7065bb91314245315a08dab
1 /*
2 * Copyright 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 <assert.h>
21 #include "vbscript.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
27 typedef struct {
28 vbscode_t *code;
29 instr_t *instr;
30 script_ctx_t *script;
31 function_t *func;
32 IDispatch *this_obj;
34 VARIANT *args;
35 VARIANT *vars;
37 dynamic_var_t *dynamic_vars;
38 vbsheap_t heap;
40 BOOL resume_next;
42 unsigned stack_size;
43 unsigned top;
44 VARIANT *stack;
46 VARIANT ret_val;
47 } exec_ctx_t;
49 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
51 typedef enum {
52 REF_NONE,
53 REF_DISP,
54 REF_VAR,
55 REF_OBJ,
56 REF_CONST,
57 REF_FUNC
58 } ref_type_t;
60 typedef struct {
61 ref_type_t type;
62 union {
63 struct {
64 IDispatch *disp;
65 DISPID id;
66 } d;
67 VARIANT *v;
68 function_t *f;
69 IDispatch *obj;
70 } u;
71 } ref_t;
73 typedef struct {
74 VARIANT *v;
75 VARIANT store;
76 BOOL owned;
77 } variant_val_t;
79 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
81 while(var) {
82 if(!strcmpiW(var->name, name)) {
83 ref->type = var->is_const ? REF_CONST : REF_VAR;
84 ref->u.v = &var->v;
85 return TRUE;
88 var = var->next;
91 return FALSE;
94 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
96 named_item_t *item;
97 function_t *func;
98 unsigned i;
99 DISPID id;
100 HRESULT hres;
102 static const WCHAR errW[] = {'e','r','r',0};
104 if(invoke_type == VBDISP_LET
105 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
106 && !strcmpiW(name, ctx->func->name)) {
107 ref->type = REF_VAR;
108 ref->u.v = &ctx->ret_val;
109 return S_OK;
112 for(i=0; i < ctx->func->var_cnt; i++) {
113 if(!strcmpiW(ctx->func->vars[i].name, name)) {
114 ref->type = REF_VAR;
115 ref->u.v = ctx->vars+i;
116 return TRUE;
120 for(i=0; i < ctx->func->arg_cnt; i++) {
121 if(!strcmpiW(ctx->func->args[i].name, name)) {
122 ref->type = REF_VAR;
123 ref->u.v = ctx->args+i;
124 return S_OK;
128 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
129 return S_OK;
131 if(ctx->func->type != FUNC_GLOBAL) {
132 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
133 if(SUCCEEDED(hres)) {
134 ref->type = REF_DISP;
135 ref->u.d.disp = ctx->this_obj;
136 ref->u.d.id = id;
137 return S_OK;
141 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
142 return S_OK;
144 for(func = ctx->script->global_funcs; func; func = func->next) {
145 if(!strcmpiW(func->name, name)) {
146 ref->type = REF_FUNC;
147 ref->u.f = func;
148 return S_OK;
152 if(!strcmpiW(name, errW)) {
153 ref->type = REF_OBJ;
154 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
155 return S_OK;
158 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
159 if(SUCCEEDED(hres)) {
160 ref->type = REF_DISP;
161 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
162 ref->u.d.id = id;
163 return S_OK;
166 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
167 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
168 if(!item->disp) {
169 IUnknown *unk;
171 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
172 if(FAILED(hres)) {
173 WARN("GetItemInfo failed: %08x\n", hres);
174 continue;
177 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
178 IUnknown_Release(unk);
179 if(FAILED(hres)) {
180 WARN("object does not implement IDispatch\n");
181 continue;
185 ref->type = REF_OBJ;
186 ref->u.obj = item->disp;
187 return S_OK;
191 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
192 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
193 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
194 if(SUCCEEDED(hres)) {
195 ref->type = REF_DISP;
196 ref->u.d.disp = item->disp;
197 ref->u.d.id = id;
198 return S_OK;
203 ref->type = REF_NONE;
204 return S_OK;
207 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
209 dynamic_var_t *new_var;
210 vbsheap_t *heap;
211 WCHAR *str;
212 unsigned size;
213 HRESULT hres;
215 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
217 new_var = vbsheap_alloc(heap, sizeof(*new_var));
218 if(!new_var)
219 return E_OUTOFMEMORY;
221 size = (strlenW(name)+1)*sizeof(WCHAR);
222 str = vbsheap_alloc(heap, size);
223 if(!str)
224 return E_OUTOFMEMORY;
225 memcpy(str, name, size);
226 new_var->name = str;
227 new_var->is_const = is_const;
229 if(own_val) {
230 new_var->v = *val;
231 }else {
232 V_VT(&new_var->v) = VT_EMPTY;
233 hres = VariantCopy(&new_var->v, val);
234 if(FAILED(hres))
235 return hres;
238 if(ctx->func->type == FUNC_GLOBAL) {
239 new_var->next = ctx->script->global_vars;
240 ctx->script->global_vars = new_var;
241 }else {
242 new_var->next = ctx->dynamic_vars;
243 ctx->dynamic_vars = new_var;
246 return S_OK;
249 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
251 assert(ctx->top);
252 return ctx->stack + --ctx->top;
255 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
257 assert(ctx->top >= n);
258 return ctx->stack + (ctx->top-n-1);
261 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
263 if(ctx->stack_size == ctx->top) {
264 VARIANT *new_stack;
266 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
267 if(!new_stack) {
268 VariantClear(v);
269 return E_OUTOFMEMORY;
272 ctx->stack = new_stack;
273 ctx->stack_size *= 2;
276 ctx->stack[ctx->top++] = *v;
277 return S_OK;
280 static void stack_popn(exec_ctx_t *ctx, unsigned n)
282 while(n--)
283 VariantClear(stack_pop(ctx));
286 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
288 VARIANT *var;
290 var = stack_pop(ctx);
292 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
293 v->owned = FALSE;
294 var = V_VARIANTREF(var);
295 }else {
296 v->owned = TRUE;
299 if(V_VT(var) == VT_DISPATCH) {
300 DISPPARAMS dp = {0};
301 HRESULT hres;
303 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
304 if(v->owned)
305 IDispatch_Release(V_DISPATCH(var));
306 if(FAILED(hres))
307 return hres;
309 v->owned = TRUE;
310 v->v = &v->store;
311 }else {
312 v->v = var;
315 return S_OK;
318 static inline void release_val(variant_val_t *v)
320 if(v->owned)
321 VariantClear(v->v);
324 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
326 VARIANT *v = stack_pop(ctx);
328 if(V_VT(v) == VT_DISPATCH) {
329 *ret = V_DISPATCH(v);
330 return S_OK;
333 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
334 FIXME("not supported type: %s\n", debugstr_variant(v));
335 VariantClear(v);
336 return E_FAIL;
339 v = V_BYREF(v);
340 if(V_VT(v) != VT_DISPATCH) {
341 FIXME("not disp %s\n", debugstr_variant(v));
342 return E_FAIL;
345 if(V_DISPATCH(v))
346 IDispatch_AddRef(V_DISPATCH(v));
347 *ret = V_DISPATCH(v);
348 return S_OK;
351 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
353 VARIANT *v = stack_top(ctx, n), *ref;
355 if(V_VT(v) != VT_DISPATCH) {
356 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
357 FIXME("not supported type: %s\n", debugstr_variant(v));
358 return E_FAIL;
361 ref = V_VARIANTREF(v);
362 if(V_VT(ref) != VT_DISPATCH) {
363 FIXME("not disp %s\n", debugstr_variant(ref));
364 return E_FAIL;
367 V_VT(v) = VT_DISPATCH;
368 V_DISPATCH(v) = V_DISPATCH(ref);
369 if(V_DISPATCH(v))
370 IDispatch_AddRef(V_DISPATCH(v));
373 if(disp)
374 *disp = V_DISPATCH(v);
375 return S_OK;
378 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
380 ctx->instr = ctx->code->instrs + addr;
383 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
385 dp->cArgs = arg_cnt;
386 dp->rgdispidNamedArgs = NULL;
387 dp->cNamedArgs = 0;
389 if(arg_cnt) {
390 VARIANT tmp;
391 unsigned i;
393 assert(ctx->top >= arg_cnt);
395 for(i=1; i*2 <= arg_cnt; i++) {
396 tmp = ctx->stack[ctx->top-i];
397 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
398 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
401 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
402 }else {
403 dp->rgvarg = NULL;
407 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
409 BSTR identifier = ctx->instr->arg1.bstr;
410 const unsigned arg_cnt = ctx->instr->arg2.uint;
411 DISPPARAMS dp;
412 ref_t ref;
413 HRESULT hres;
415 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
416 if(FAILED(hres))
417 return hres;
419 vbstack_to_dp(ctx, arg_cnt, &dp);
421 switch(ref.type) {
422 case REF_VAR:
423 case REF_CONST:
424 if(!res) {
425 FIXME("REF_VAR no res\n");
426 return E_NOTIMPL;
429 if(arg_cnt) {
430 FIXME("arguments not implemented\n");
431 return E_NOTIMPL;
434 V_VT(res) = VT_BYREF|VT_VARIANT;
435 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
436 break;
437 case REF_DISP:
438 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
439 if(FAILED(hres))
440 return hres;
441 break;
442 case REF_FUNC:
443 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
444 if(FAILED(hres))
445 return hres;
446 break;
447 case REF_OBJ:
448 if(arg_cnt) {
449 FIXME("arguments on object\n");
450 return E_NOTIMPL;
453 if(res) {
454 IDispatch_AddRef(ref.u.obj);
455 V_VT(res) = VT_DISPATCH;
456 V_DISPATCH(res) = ref.u.obj;
458 break;
459 case REF_NONE:
460 FIXME("%s not found\n", debugstr_w(identifier));
461 return DISP_E_UNKNOWNNAME;
464 stack_popn(ctx, arg_cnt);
465 return S_OK;
468 static HRESULT interp_icall(exec_ctx_t *ctx)
470 VARIANT v;
471 HRESULT hres;
473 TRACE("\n");
475 hres = do_icall(ctx, &v);
476 if(FAILED(hres))
477 return hres;
479 return stack_push(ctx, &v);
482 static HRESULT interp_icallv(exec_ctx_t *ctx)
484 TRACE("\n");
485 return do_icall(ctx, NULL);
488 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
490 const BSTR identifier = ctx->instr->arg1.bstr;
491 const unsigned arg_cnt = ctx->instr->arg2.uint;
492 IDispatch *obj;
493 DISPPARAMS dp;
494 DISPID id;
495 HRESULT hres;
497 hres = stack_pop_disp(ctx, &obj);
498 if(FAILED(hres))
499 return hres;
501 if(!obj) {
502 FIXME("NULL obj\n");
503 return E_FAIL;
506 vbstack_to_dp(ctx, arg_cnt, &dp);
508 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
509 if(SUCCEEDED(hres))
510 hres = disp_call(ctx->script, obj, id, &dp, res);
511 IDispatch_Release(obj);
512 if(FAILED(hres))
513 return hres;
515 stack_popn(ctx, arg_cnt);
516 return S_OK;
519 static HRESULT interp_mcall(exec_ctx_t *ctx)
521 VARIANT res;
522 HRESULT hres;
524 TRACE("\n");
526 hres = do_mcall(ctx, &res);
527 if(FAILED(hres))
528 return hres;
530 return stack_push(ctx, &res);
533 static HRESULT interp_mcallv(exec_ctx_t *ctx)
535 TRACE("\n");
537 return do_mcall(ctx, NULL);
540 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
542 ref_t ref;
543 HRESULT hres;
545 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
546 if(FAILED(hres))
547 return hres;
549 switch(ref.type) {
550 case REF_VAR: {
551 VARIANT *v = ref.u.v;
553 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
554 v = V_VARIANTREF(v);
556 if(own_val) {
557 VariantClear(v);
558 *v = *val;
559 hres = S_OK;
560 }else {
561 hres = VariantCopy(v, val);
563 break;
565 case REF_DISP:
566 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
567 if(own_val)
568 VariantClear(val);
569 break;
570 case REF_FUNC:
571 FIXME("functions not implemented\n");
572 return E_NOTIMPL;
573 case REF_OBJ:
574 FIXME("REF_OBJ\n");
575 return E_NOTIMPL;
576 case REF_CONST:
577 FIXME("REF_CONST\n");
578 return E_NOTIMPL;
579 case REF_NONE:
580 if(ctx->func->code_ctx->option_explicit) {
581 FIXME("throw exception\n");
582 hres = E_FAIL;
583 }else {
584 TRACE("creating variable %s\n", debugstr_w(name));
585 hres = add_dynamic_var(ctx, name, FALSE, val, own_val);
589 return hres;
592 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
594 const BSTR arg = ctx->instr->arg1.bstr;
595 const unsigned arg_cnt = ctx->instr->arg2.uint;
596 variant_val_t v;
597 HRESULT hres;
599 TRACE("%s\n", debugstr_w(arg));
601 if(arg_cnt) {
602 FIXME("arguments not supported\n");
603 return E_NOTIMPL;
606 hres = stack_pop_val(ctx, &v);
607 if(FAILED(hres))
608 return hres;
610 return assign_ident(ctx, arg, v.v, v.owned);
613 static HRESULT interp_set_ident(exec_ctx_t *ctx)
615 const BSTR arg = ctx->instr->arg1.bstr;
616 const unsigned arg_cnt = ctx->instr->arg2.uint;
617 IDispatch *disp;
618 VARIANT v;
619 HRESULT hres;
621 TRACE("%s\n", debugstr_w(arg));
623 if(arg_cnt) {
624 FIXME("arguments not supported\n");
625 return E_NOTIMPL;
628 hres = stack_pop_disp(ctx, &disp);
629 if(FAILED(hres))
630 return hres;
632 V_VT(&v) = VT_DISPATCH;
633 V_DISPATCH(&v) = disp;
634 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
637 static HRESULT interp_assign_member(exec_ctx_t *ctx)
639 BSTR identifier = ctx->instr->arg1.bstr;
640 const unsigned arg_cnt = ctx->instr->arg2.uint;
641 variant_val_t val;
642 IDispatch *obj;
643 DISPID id;
644 HRESULT hres;
646 TRACE("%s\n", debugstr_w(identifier));
648 if(arg_cnt) {
649 FIXME("arguments not supported\n");
650 return E_NOTIMPL;
653 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
654 if(FAILED(hres))
655 return hres;
657 if(!obj) {
658 FIXME("NULL obj\n");
659 return E_FAIL;
662 hres = stack_pop_val(ctx, &val);
663 if(FAILED(hres))
664 return hres;
666 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
667 if(SUCCEEDED(hres))
668 hres = disp_propput(ctx->script, obj, id, val.v);
669 release_val(&val);
670 if(FAILED(hres))
671 return hres;
673 stack_popn(ctx, 1);
674 return S_OK;
677 static HRESULT interp_set_member(exec_ctx_t *ctx)
679 BSTR identifier = ctx->instr->arg1.bstr;
680 const unsigned arg_cnt = ctx->instr->arg2.uint;
681 IDispatch *obj;
682 DISPID id;
683 HRESULT hres;
685 TRACE("%s\n", debugstr_w(identifier));
687 if(arg_cnt) {
688 FIXME("arguments not supported\n");
689 return E_NOTIMPL;
692 hres = stack_assume_disp(ctx, 1, &obj);
693 if(FAILED(hres))
694 return hres;
696 if(!obj) {
697 FIXME("NULL obj\n");
698 return E_FAIL;
701 hres = stack_assume_disp(ctx, 0, NULL);
702 if(FAILED(hres))
703 return hres;
705 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
706 if(SUCCEEDED(hres))
707 hres = disp_propput(ctx->script, obj, id, stack_top(ctx, 0));
708 if(FAILED(hres))
709 return hres;
711 stack_popn(ctx, 2);
712 return S_OK;
715 static HRESULT interp_const(exec_ctx_t *ctx)
717 BSTR arg = ctx->instr->arg1.bstr;
718 variant_val_t val;
719 ref_t ref;
720 HRESULT hres;
722 TRACE("%s\n", debugstr_w(arg));
724 assert(ctx->func->type == FUNC_GLOBAL);
726 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
727 if(FAILED(hres))
728 return hres;
730 if(ref.type != REF_NONE) {
731 FIXME("%s already defined\n", debugstr_w(arg));
732 return E_FAIL;
735 hres = stack_pop_val(ctx, &val);
736 if(FAILED(hres))
737 return hres;
739 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
742 static HRESULT interp_val(exec_ctx_t *ctx)
744 variant_val_t val;
745 VARIANT v;
746 HRESULT hres;
748 TRACE("\n");
750 hres = stack_pop_val(ctx, &val);
751 if(FAILED(hres))
752 return hres;
754 if(!val.owned) {
755 V_VT(&v) = VT_EMPTY;
756 hres = VariantCopy(&v, val.v);
757 if(FAILED(hres))
758 return hres;
761 return stack_push(ctx, val.owned ? val.v : &v);
764 static HRESULT interp_pop(exec_ctx_t *ctx)
766 const unsigned n = ctx->instr->arg1.uint;
768 TRACE("%u\n", n);
770 stack_popn(ctx, n);
771 return S_OK;
774 static HRESULT interp_new(exec_ctx_t *ctx)
776 const WCHAR *arg = ctx->instr->arg1.bstr;
777 class_desc_t *class_desc;
778 vbdisp_t *obj;
779 VARIANT v;
780 HRESULT hres;
782 TRACE("%s\n", debugstr_w(arg));
784 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
785 if(!strcmpiW(class_desc->name, arg))
786 break;
788 if(!class_desc) {
789 FIXME("Class %s not found\n", debugstr_w(arg));
790 return E_FAIL;
793 hres = create_vbdisp(class_desc, &obj);
794 if(FAILED(hres))
795 return hres;
797 V_VT(&v) = VT_DISPATCH;
798 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
799 return stack_push(ctx, &v);
802 static HRESULT interp_step(exec_ctx_t *ctx)
804 const BSTR ident = ctx->instr->arg2.bstr;
805 BOOL gteq_zero;
806 VARIANT zero;
807 ref_t ref;
808 HRESULT hres;
810 TRACE("%s\n", debugstr_w(ident));
812 V_VT(&zero) = VT_I2;
813 V_I2(&zero) = 0;
814 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
815 if(FAILED(hres))
816 return hres;
818 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
820 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
821 if(FAILED(hres))
822 return hres;
824 if(ref.type != REF_VAR) {
825 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
826 return E_FAIL;
829 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
830 if(FAILED(hres))
831 return hres;
833 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
834 ctx->instr++;
835 else
836 instr_jmp(ctx, ctx->instr->arg1.uint);
837 return S_OK;
840 static HRESULT interp_jmp(exec_ctx_t *ctx)
842 const unsigned arg = ctx->instr->arg1.uint;
844 TRACE("%u\n", arg);
846 instr_jmp(ctx, arg);
847 return S_OK;
850 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
852 const unsigned arg = ctx->instr->arg1.uint;
853 variant_val_t val;
854 HRESULT hres;
856 TRACE("%u\n", arg);
858 hres = stack_pop_val(ctx, &val);
859 if(FAILED(hres))
860 return hres;
862 if(V_VT(val.v) != VT_BOOL) {
863 FIXME("unsupported for %s\n", debugstr_variant(val.v));
864 release_val(&val);
865 return E_NOTIMPL;
868 if(V_BOOL(val.v))
869 ctx->instr++;
870 else
871 instr_jmp(ctx, ctx->instr->arg1.uint);
872 return S_OK;
875 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
877 const unsigned arg = ctx->instr->arg1.uint;
878 variant_val_t val;
879 HRESULT hres;
881 TRACE("%u\n", arg);
883 hres = stack_pop_val(ctx, &val);
884 if(FAILED(hres))
885 return hres;
887 if(V_VT(val.v) != VT_BOOL) {
888 FIXME("unsupported for %s\n", debugstr_variant(val.v));
889 release_val(&val);
890 return E_NOTIMPL;
893 if(V_BOOL(val.v))
894 instr_jmp(ctx, ctx->instr->arg1.uint);
895 else
896 ctx->instr++;
897 return S_OK;
900 static HRESULT interp_ret(exec_ctx_t *ctx)
902 TRACE("\n");
904 ctx->instr = NULL;
905 return S_OK;
908 static HRESULT interp_stop(exec_ctx_t *ctx)
910 WARN("\n");
912 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
913 return S_OK;
916 static HRESULT interp_me(exec_ctx_t *ctx)
918 VARIANT v;
920 TRACE("\n");
922 IDispatch_AddRef(ctx->this_obj);
923 V_VT(&v) = VT_DISPATCH;
924 V_DISPATCH(&v) = ctx->this_obj;
925 return stack_push(ctx, &v);
928 static HRESULT interp_bool(exec_ctx_t *ctx)
930 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
931 VARIANT v;
933 TRACE("%s\n", arg ? "true" : "false");
935 V_VT(&v) = VT_BOOL;
936 V_BOOL(&v) = arg;
937 return stack_push(ctx, &v);
940 static HRESULT interp_errmode(exec_ctx_t *ctx)
942 const int err_mode = ctx->instr->arg1.uint;
944 TRACE("%d\n", err_mode);
946 ctx->resume_next = err_mode;
947 return S_OK;
950 static HRESULT interp_string(exec_ctx_t *ctx)
952 VARIANT v;
954 TRACE("\n");
956 V_VT(&v) = VT_BSTR;
957 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
958 if(!V_BSTR(&v))
959 return E_OUTOFMEMORY;
961 return stack_push(ctx, &v);
964 static HRESULT interp_long(exec_ctx_t *ctx)
966 const LONG arg = ctx->instr->arg1.lng;
967 VARIANT v;
969 TRACE("%d\n", arg);
971 V_VT(&v) = VT_I4;
972 V_I4(&v) = arg;
973 return stack_push(ctx, &v);
976 static HRESULT interp_short(exec_ctx_t *ctx)
978 const LONG arg = ctx->instr->arg1.lng;
979 VARIANT v;
981 TRACE("%d\n", arg);
983 V_VT(&v) = VT_I2;
984 V_I2(&v) = arg;
985 return stack_push(ctx, &v);
988 static HRESULT interp_double(exec_ctx_t *ctx)
990 const DOUBLE *arg = ctx->instr->arg1.dbl;
991 VARIANT v;
993 TRACE("%lf\n", *arg);
995 V_VT(&v) = VT_R8;
996 V_R8(&v) = *arg;
997 return stack_push(ctx, &v);
1000 static HRESULT interp_empty(exec_ctx_t *ctx)
1002 VARIANT v;
1004 TRACE("\n");
1006 V_VT(&v) = VT_EMPTY;
1007 return stack_push(ctx, &v);
1010 static HRESULT interp_null(exec_ctx_t *ctx)
1012 VARIANT v;
1014 TRACE("\n");
1016 V_VT(&v) = VT_NULL;
1017 return stack_push(ctx, &v);
1020 static HRESULT interp_nothing(exec_ctx_t *ctx)
1022 VARIANT v;
1024 TRACE("\n");
1026 V_VT(&v) = VT_DISPATCH;
1027 V_DISPATCH(&v) = NULL;
1028 return stack_push(ctx, &v);
1031 static HRESULT interp_not(exec_ctx_t *ctx)
1033 variant_val_t val;
1034 VARIANT v;
1035 HRESULT hres;
1037 TRACE("\n");
1039 hres = stack_pop_val(ctx, &val);
1040 if(FAILED(hres))
1041 return hres;
1043 hres = VarNot(val.v, &v);
1044 release_val(&val);
1045 if(FAILED(hres))
1046 return hres;
1048 return stack_push(ctx, &v);
1051 static HRESULT interp_and(exec_ctx_t *ctx)
1053 variant_val_t r, l;
1054 VARIANT v;
1055 HRESULT hres;
1057 TRACE("\n");
1059 hres = stack_pop_val(ctx, &r);
1060 if(FAILED(hres))
1061 return hres;
1063 hres = stack_pop_val(ctx, &l);
1064 if(SUCCEEDED(hres)) {
1065 hres = VarAnd(l.v, r.v, &v);
1066 release_val(&l);
1068 release_val(&r);
1069 if(FAILED(hres))
1070 return hres;
1072 return stack_push(ctx, &v);
1075 static HRESULT interp_or(exec_ctx_t *ctx)
1077 variant_val_t r, l;
1078 VARIANT v;
1079 HRESULT hres;
1081 TRACE("\n");
1083 hres = stack_pop_val(ctx, &r);
1084 if(FAILED(hres))
1085 return hres;
1087 hres = stack_pop_val(ctx, &l);
1088 if(SUCCEEDED(hres)) {
1089 hres = VarOr(l.v, r.v, &v);
1090 release_val(&l);
1092 release_val(&r);
1093 if(FAILED(hres))
1094 return hres;
1096 return stack_push(ctx, &v);
1099 static HRESULT interp_xor(exec_ctx_t *ctx)
1101 variant_val_t r, l;
1102 VARIANT v;
1103 HRESULT hres;
1105 TRACE("\n");
1107 hres = stack_pop_val(ctx, &r);
1108 if(FAILED(hres))
1109 return hres;
1111 hres = stack_pop_val(ctx, &l);
1112 if(SUCCEEDED(hres)) {
1113 hres = VarXor(l.v, r.v, &v);
1114 release_val(&l);
1116 release_val(&r);
1117 if(FAILED(hres))
1118 return hres;
1120 return stack_push(ctx, &v);
1123 static HRESULT interp_eqv(exec_ctx_t *ctx)
1125 variant_val_t r, l;
1126 VARIANT v;
1127 HRESULT hres;
1129 TRACE("\n");
1131 hres = stack_pop_val(ctx, &r);
1132 if(FAILED(hres))
1133 return hres;
1135 hres = stack_pop_val(ctx, &l);
1136 if(SUCCEEDED(hres)) {
1137 hres = VarEqv(l.v, r.v, &v);
1138 release_val(&l);
1140 release_val(&r);
1141 if(FAILED(hres))
1142 return hres;
1144 return stack_push(ctx, &v);
1147 static HRESULT interp_imp(exec_ctx_t *ctx)
1149 variant_val_t r, l;
1150 VARIANT v;
1151 HRESULT hres;
1153 TRACE("\n");
1155 hres = stack_pop_val(ctx, &r);
1156 if(FAILED(hres))
1157 return hres;
1159 hres = stack_pop_val(ctx, &l);
1160 if(SUCCEEDED(hres)) {
1161 hres = VarImp(l.v, r.v, &v);
1162 release_val(&l);
1164 release_val(&r);
1165 if(FAILED(hres))
1166 return hres;
1168 return stack_push(ctx, &v);
1171 static HRESULT cmp_oper(exec_ctx_t *ctx)
1173 variant_val_t l, r;
1174 HRESULT hres;
1176 hres = stack_pop_val(ctx, &r);
1177 if(FAILED(hres))
1178 return hres;
1180 hres = stack_pop_val(ctx, &l);
1181 if(SUCCEEDED(hres)) {
1182 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1183 FIXME("comparing nulls is not implemented\n");
1184 hres = E_NOTIMPL;
1185 }else {
1186 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1190 release_val(&r);
1191 release_val(&l);
1192 return hres;
1195 static HRESULT interp_equal(exec_ctx_t *ctx)
1197 VARIANT v;
1198 HRESULT hres;
1200 TRACE("\n");
1202 hres = cmp_oper(ctx);
1203 if(FAILED(hres))
1204 return hres;
1206 V_VT(&v) = VT_BOOL;
1207 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1208 return stack_push(ctx, &v);
1211 static HRESULT interp_nequal(exec_ctx_t *ctx)
1213 VARIANT v;
1214 HRESULT hres;
1216 TRACE("\n");
1218 hres = cmp_oper(ctx);
1219 if(FAILED(hres))
1220 return hres;
1222 V_VT(&v) = VT_BOOL;
1223 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1224 return stack_push(ctx, &v);
1227 static HRESULT interp_gt(exec_ctx_t *ctx)
1229 VARIANT v;
1230 HRESULT hres;
1232 TRACE("\n");
1234 hres = cmp_oper(ctx);
1235 if(FAILED(hres))
1236 return hres;
1238 V_VT(&v) = VT_BOOL;
1239 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1240 return stack_push(ctx, &v);
1243 static HRESULT interp_gteq(exec_ctx_t *ctx)
1245 VARIANT v;
1246 HRESULT hres;
1248 TRACE("\n");
1250 hres = cmp_oper(ctx);
1251 if(FAILED(hres))
1252 return hres;
1254 V_VT(&v) = VT_BOOL;
1255 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1256 return stack_push(ctx, &v);
1259 static HRESULT interp_lt(exec_ctx_t *ctx)
1261 VARIANT v;
1262 HRESULT hres;
1264 TRACE("\n");
1266 hres = cmp_oper(ctx);
1267 if(FAILED(hres))
1268 return hres;
1270 V_VT(&v) = VT_BOOL;
1271 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1272 return stack_push(ctx, &v);
1275 static HRESULT interp_lteq(exec_ctx_t *ctx)
1277 VARIANT v;
1278 HRESULT hres;
1280 TRACE("\n");
1282 hres = cmp_oper(ctx);
1283 if(FAILED(hres))
1284 return hres;
1286 V_VT(&v) = VT_BOOL;
1287 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1288 return stack_push(ctx, &v);
1291 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1293 IObjectIdentity *identity;
1294 IUnknown *unk1, *unk2;
1295 HRESULT hres;
1297 if(disp1 == disp2) {
1298 *ret = VARIANT_TRUE;
1299 return S_OK;
1302 if(!disp1 || !disp2) {
1303 *ret = VARIANT_FALSE;
1304 return S_OK;
1307 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1308 if(FAILED(hres))
1309 return hres;
1311 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1312 if(FAILED(hres)) {
1313 IUnknown_Release(unk1);
1314 return hres;
1317 if(unk1 == unk2) {
1318 *ret = VARIANT_TRUE;
1319 }else {
1320 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1321 if(SUCCEEDED(hres)) {
1322 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1323 IObjectIdentity_Release(identity);
1324 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1325 }else {
1326 *ret = VARIANT_FALSE;
1330 IUnknown_Release(unk1);
1331 IUnknown_Release(unk2);
1332 return S_OK;
1335 static HRESULT interp_is(exec_ctx_t *ctx)
1337 IDispatch *l, *r;
1338 VARIANT v;
1339 HRESULT hres;
1341 TRACE("\n");
1343 hres = stack_pop_disp(ctx, &r);
1344 if(FAILED(hres))
1345 return hres;
1347 hres = stack_pop_disp(ctx, &l);
1348 if(SUCCEEDED(hres)) {
1349 V_VT(&v) = VT_BOOL;
1350 hres = disp_cmp(l, r, &V_BOOL(&v));
1351 if(l)
1352 IDispatch_Release(l);
1354 if(r)
1355 IDispatch_Release(r);
1356 if(FAILED(hres))
1357 return hres;
1359 return stack_push(ctx, &v);
1362 static HRESULT interp_concat(exec_ctx_t *ctx)
1364 variant_val_t r, l;
1365 VARIANT v;
1366 HRESULT hres;
1368 TRACE("\n");
1370 hres = stack_pop_val(ctx, &r);
1371 if(FAILED(hres))
1372 return hres;
1374 hres = stack_pop_val(ctx, &l);
1375 if(SUCCEEDED(hres)) {
1376 hres = VarCat(l.v, r.v, &v);
1377 release_val(&l);
1379 release_val(&r);
1380 if(FAILED(hres))
1381 return hres;
1383 return stack_push(ctx, &v);
1386 static HRESULT interp_add(exec_ctx_t *ctx)
1388 variant_val_t r, l;
1389 VARIANT v;
1390 HRESULT hres;
1392 TRACE("\n");
1394 hres = stack_pop_val(ctx, &r);
1395 if(FAILED(hres))
1396 return hres;
1398 hres = stack_pop_val(ctx, &l);
1399 if(SUCCEEDED(hres)) {
1400 hres = VarAdd(l.v, r.v, &v);
1401 release_val(&l);
1403 release_val(&r);
1404 if(FAILED(hres))
1405 return hres;
1407 return stack_push(ctx, &v);
1410 static HRESULT interp_sub(exec_ctx_t *ctx)
1412 variant_val_t r, l;
1413 VARIANT v;
1414 HRESULT hres;
1416 TRACE("\n");
1418 hres = stack_pop_val(ctx, &r);
1419 if(FAILED(hres))
1420 return hres;
1422 hres = stack_pop_val(ctx, &l);
1423 if(SUCCEEDED(hres)) {
1424 hres = VarSub(l.v, r.v, &v);
1425 release_val(&l);
1427 release_val(&r);
1428 if(FAILED(hres))
1429 return hres;
1431 return stack_push(ctx, &v);
1434 static HRESULT interp_mod(exec_ctx_t *ctx)
1436 variant_val_t r, l;
1437 VARIANT v;
1438 HRESULT hres;
1440 TRACE("\n");
1442 hres = stack_pop_val(ctx, &r);
1443 if(FAILED(hres))
1444 return hres;
1446 hres = stack_pop_val(ctx, &l);
1447 if(SUCCEEDED(hres)) {
1448 hres = VarMod(l.v, r.v, &v);
1449 release_val(&l);
1451 release_val(&r);
1452 if(FAILED(hres))
1453 return hres;
1455 return stack_push(ctx, &v);
1458 static HRESULT interp_idiv(exec_ctx_t *ctx)
1460 variant_val_t r, l;
1461 VARIANT v;
1462 HRESULT hres;
1464 TRACE("\n");
1466 hres = stack_pop_val(ctx, &r);
1467 if(FAILED(hres))
1468 return hres;
1470 hres = stack_pop_val(ctx, &l);
1471 if(SUCCEEDED(hres)) {
1472 hres = VarIdiv(l.v, r.v, &v);
1473 release_val(&l);
1475 release_val(&r);
1476 if(FAILED(hres))
1477 return hres;
1479 return stack_push(ctx, &v);
1482 static HRESULT interp_div(exec_ctx_t *ctx)
1484 variant_val_t r, l;
1485 VARIANT v;
1486 HRESULT hres;
1488 TRACE("\n");
1490 hres = stack_pop_val(ctx, &r);
1491 if(FAILED(hres))
1492 return hres;
1494 hres = stack_pop_val(ctx, &l);
1495 if(SUCCEEDED(hres)) {
1496 hres = VarDiv(l.v, r.v, &v);
1497 release_val(&l);
1499 release_val(&r);
1500 if(FAILED(hres))
1501 return hres;
1503 return stack_push(ctx, &v);
1506 static HRESULT interp_mul(exec_ctx_t *ctx)
1508 variant_val_t r, l;
1509 VARIANT v;
1510 HRESULT hres;
1512 TRACE("\n");
1514 hres = stack_pop_val(ctx, &r);
1515 if(FAILED(hres))
1516 return hres;
1518 hres = stack_pop_val(ctx, &l);
1519 if(SUCCEEDED(hres)) {
1520 hres = VarMul(l.v, r.v, &v);
1521 release_val(&l);
1523 release_val(&r);
1524 if(FAILED(hres))
1525 return hres;
1527 return stack_push(ctx, &v);
1530 static HRESULT interp_exp(exec_ctx_t *ctx)
1532 variant_val_t r, l;
1533 VARIANT v;
1534 HRESULT hres;
1536 TRACE("\n");
1538 hres = stack_pop_val(ctx, &r);
1539 if(FAILED(hres))
1540 return hres;
1542 hres = stack_pop_val(ctx, &l);
1543 if(SUCCEEDED(hres)) {
1544 hres = VarPow(l.v, r.v, &v);
1545 release_val(&l);
1547 release_val(&r);
1548 if(FAILED(hres))
1549 return hres;
1551 return stack_push(ctx, &v);
1554 static HRESULT interp_neg(exec_ctx_t *ctx)
1556 variant_val_t val;
1557 VARIANT v;
1558 HRESULT hres;
1560 hres = stack_pop_val(ctx, &val);
1561 if(FAILED(hres))
1562 return hres;
1564 hres = VarNeg(val.v, &v);
1565 release_val(&val);
1566 if(FAILED(hres))
1567 return hres;
1569 return stack_push(ctx, &v);
1572 static HRESULT interp_incc(exec_ctx_t *ctx)
1574 const BSTR ident = ctx->instr->arg1.bstr;
1575 VARIANT v;
1576 ref_t ref;
1577 HRESULT hres;
1579 TRACE("\n");
1581 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1582 if(FAILED(hres))
1583 return hres;
1585 if(ref.type != REF_VAR) {
1586 FIXME("ref.type is not REF_VAR\n");
1587 return E_FAIL;
1590 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1591 if(FAILED(hres))
1592 return hres;
1594 VariantClear(ref.u.v);
1595 *ref.u.v = v;
1596 return S_OK;
1599 static const instr_func_t op_funcs[] = {
1600 #define X(x,n,a,b) interp_ ## x,
1601 OP_LIST
1602 #undef X
1605 static const unsigned op_move[] = {
1606 #define X(x,n,a,b) n,
1607 OP_LIST
1608 #undef X
1611 void release_dynamic_vars(dynamic_var_t *var)
1613 while(var) {
1614 VariantClear(&var->v);
1615 var = var->next;
1619 static void release_exec(exec_ctx_t *ctx)
1621 unsigned i;
1623 VariantClear(&ctx->ret_val);
1624 release_dynamic_vars(ctx->dynamic_vars);
1626 if(ctx->this_obj)
1627 IDispatch_Release(ctx->this_obj);
1629 if(ctx->args) {
1630 for(i=0; i < ctx->func->arg_cnt; i++)
1631 VariantClear(ctx->args+i);
1634 if(ctx->vars) {
1635 for(i=0; i < ctx->func->var_cnt; i++)
1636 VariantClear(ctx->vars+i);
1639 vbsheap_free(&ctx->heap);
1640 heap_free(ctx->args);
1641 heap_free(ctx->vars);
1642 heap_free(ctx->stack);
1645 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1647 exec_ctx_t exec = {func->code_ctx};
1648 vbsop_t op;
1649 HRESULT hres = S_OK;
1651 exec.code = func->code_ctx;
1653 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1654 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1655 return E_FAIL;
1658 vbsheap_init(&exec.heap);
1660 if(func->arg_cnt) {
1661 VARIANT *v;
1662 unsigned i;
1664 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1665 if(!exec.args) {
1666 release_exec(&exec);
1667 return E_OUTOFMEMORY;
1670 for(i=0; i < func->arg_cnt; i++) {
1671 v = get_arg(dp, i);
1672 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1673 if(func->args[i].by_ref)
1674 exec.args[i] = *v;
1675 else
1676 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1677 }else {
1678 hres = VariantCopy(exec.args+i, v);
1680 if(FAILED(hres)) {
1681 release_exec(&exec);
1682 return hres;
1685 }else {
1686 exec.args = NULL;
1689 if(func->var_cnt) {
1690 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1691 if(!exec.vars) {
1692 release_exec(&exec);
1693 return E_OUTOFMEMORY;
1695 }else {
1696 exec.vars = NULL;
1699 exec.stack_size = 16;
1700 exec.top = 0;
1701 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1702 if(!exec.stack) {
1703 release_exec(&exec);
1704 return E_OUTOFMEMORY;
1707 if(this_obj)
1708 exec.this_obj = this_obj;
1709 else if (ctx->host_global)
1710 exec.this_obj = ctx->host_global;
1711 else
1712 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1713 IDispatch_AddRef(exec.this_obj);
1715 exec.instr = exec.code->instrs + func->code_off;
1716 exec.script = ctx;
1717 exec.func = func;
1719 while(exec.instr) {
1720 op = exec.instr->op;
1721 hres = op_funcs[op](&exec);
1722 if(FAILED(hres)) {
1723 if(exec.resume_next)
1724 FIXME("Failed %08x in resume next mode\n", hres);
1725 else
1726 WARN("Failed %08x\n", hres);
1727 stack_popn(&exec, exec.top);
1728 break;
1731 exec.instr += op_move[op];
1734 assert(!exec.top);
1735 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1736 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1738 if(SUCCEEDED(hres) && res) {
1739 *res = exec.ret_val;
1740 V_VT(&exec.ret_val) = VT_EMPTY;
1743 release_exec(&exec);
1744 return hres;