jscript: Use bytecode for '>>=' expression.
[wine/multimedia.git] / dlls / vbscript / interp.c
blobf03655778daf3bf247e26853bc34b1bb29abe1f7
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_GLOBALMEMBERS)) {
168 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
169 if(SUCCEEDED(hres)) {
170 ref->type = REF_DISP;
171 ref->u.d.disp = item->disp;
172 ref->u.d.id = id;
173 return S_OK;
177 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
178 if(!item->disp) {
179 IUnknown *unk;
181 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
182 if(FAILED(hres)) {
183 WARN("GetItemInfo failed: %08x\n", hres);
184 continue;
187 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
188 IUnknown_Release(unk);
189 if(FAILED(hres)) {
190 WARN("object does not implement IDispatch\n");
191 continue;
195 ref->type = REF_OBJ;
196 ref->u.obj = item->disp;
197 return S_OK;
201 ref->type = REF_NONE;
202 return S_OK;
205 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
207 dynamic_var_t *new_var;
208 vbsheap_t *heap;
209 WCHAR *str;
210 unsigned size;
211 HRESULT hres;
213 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
215 new_var = vbsheap_alloc(heap, sizeof(*new_var));
216 if(!new_var)
217 return E_OUTOFMEMORY;
219 size = (strlenW(name)+1)*sizeof(WCHAR);
220 str = vbsheap_alloc(heap, size);
221 if(!str)
222 return E_OUTOFMEMORY;
223 memcpy(str, name, size);
224 new_var->name = str;
225 new_var->is_const = is_const;
227 if(own_val) {
228 new_var->v = *val;
229 }else {
230 hres = VariantCopy(&new_var->v, val);
231 if(FAILED(hres))
232 return hres;
235 if(ctx->func->type == FUNC_GLOBAL) {
236 new_var->next = ctx->script->global_vars;
237 ctx->script->global_vars = new_var;
238 }else {
239 new_var->next = ctx->dynamic_vars;
240 ctx->dynamic_vars = new_var;
243 return S_OK;
246 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
248 assert(ctx->top);
249 return ctx->stack + --ctx->top;
252 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
254 assert(ctx->top >= n);
255 return ctx->stack + (ctx->top-n-1);
258 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
260 if(ctx->stack_size == ctx->top) {
261 VARIANT *new_stack;
263 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
264 if(!new_stack) {
265 VariantClear(v);
266 return E_OUTOFMEMORY;
269 ctx->stack = new_stack;
270 ctx->stack_size *= 2;
273 ctx->stack[ctx->top++] = *v;
274 return S_OK;
277 static void stack_popn(exec_ctx_t *ctx, unsigned n)
279 while(n--)
280 VariantClear(stack_pop(ctx));
283 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
285 VARIANT *var;
287 var = stack_pop(ctx);
289 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
290 v->owned = FALSE;
291 var = V_VARIANTREF(var);
292 }else {
293 v->owned = TRUE;
296 if(V_VT(var) == VT_DISPATCH) {
297 DISPPARAMS dp = {0};
298 HRESULT hres;
300 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
301 if(v->owned)
302 IDispatch_Release(V_DISPATCH(var));
303 if(FAILED(hres))
304 return hres;
306 v->owned = TRUE;
307 v->v = &v->store;
308 }else {
309 v->v = var;
312 return S_OK;
315 static inline void release_val(variant_val_t *v)
317 if(v->owned)
318 VariantClear(v->v);
321 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
323 VARIANT *v = stack_pop(ctx);
325 if(V_VT(v) == VT_DISPATCH) {
326 *ret = V_DISPATCH(v);
327 return S_OK;
330 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
331 FIXME("not supported type: %s\n", debugstr_variant(v));
332 VariantClear(v);
333 return E_FAIL;
336 v = V_BYREF(v);
337 if(V_VT(v) != VT_DISPATCH) {
338 FIXME("not disp %s\n", debugstr_variant(v));
339 return E_FAIL;
342 if(V_DISPATCH(v))
343 IDispatch_AddRef(V_DISPATCH(v));
344 *ret = V_DISPATCH(v);
345 return S_OK;
348 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
350 ctx->instr = ctx->code->instrs + addr;
353 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
355 dp->cArgs = arg_cnt;
356 dp->rgdispidNamedArgs = NULL;
357 dp->cNamedArgs = 0;
359 if(arg_cnt) {
360 VARIANT tmp;
361 unsigned i;
363 assert(ctx->top >= arg_cnt);
365 for(i=1; i*2 <= arg_cnt; i++) {
366 tmp = ctx->stack[ctx->top-i];
367 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
368 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
371 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
372 }else {
373 dp->rgvarg = NULL;
377 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
379 BSTR identifier = ctx->instr->arg1.bstr;
380 const unsigned arg_cnt = ctx->instr->arg2.uint;
381 DISPPARAMS dp;
382 ref_t ref;
383 HRESULT hres;
385 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
386 if(FAILED(hres))
387 return hres;
389 vbstack_to_dp(ctx, arg_cnt, &dp);
391 switch(ref.type) {
392 case REF_VAR:
393 case REF_CONST:
394 if(!res) {
395 FIXME("REF_VAR no res\n");
396 return E_NOTIMPL;
399 if(arg_cnt) {
400 FIXME("arguments not implemented\n");
401 return E_NOTIMPL;
404 V_VT(res) = VT_BYREF|VT_VARIANT;
405 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
406 break;
407 case REF_DISP:
408 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
409 if(FAILED(hres))
410 return hres;
411 break;
412 case REF_FUNC:
413 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
414 if(FAILED(hres))
415 return hres;
416 break;
417 case REF_OBJ:
418 if(arg_cnt) {
419 FIXME("arguments on object\n");
420 return E_NOTIMPL;
423 if(res) {
424 IDispatch_AddRef(ref.u.obj);
425 V_VT(res) = VT_DISPATCH;
426 V_DISPATCH(res) = ref.u.obj;
428 break;
429 case REF_NONE:
430 FIXME("%s not found\n", debugstr_w(identifier));
431 return DISP_E_UNKNOWNNAME;
434 stack_popn(ctx, arg_cnt);
435 return S_OK;
438 static HRESULT interp_icall(exec_ctx_t *ctx)
440 VARIANT v;
441 HRESULT hres;
443 TRACE("\n");
445 hres = do_icall(ctx, &v);
446 if(FAILED(hres))
447 return hres;
449 return stack_push(ctx, &v);
452 static HRESULT interp_icallv(exec_ctx_t *ctx)
454 TRACE("\n");
455 return do_icall(ctx, NULL);
458 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
460 const BSTR identifier = ctx->instr->arg1.bstr;
461 const unsigned arg_cnt = ctx->instr->arg2.uint;
462 IDispatch *obj;
463 DISPPARAMS dp;
464 DISPID id;
465 HRESULT hres;
467 hres = stack_pop_disp(ctx, &obj);
468 if(FAILED(hres))
469 return hres;
471 if(!obj) {
472 FIXME("NULL obj\n");
473 return E_FAIL;
476 vbstack_to_dp(ctx, arg_cnt, &dp);
478 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
479 if(SUCCEEDED(hres))
480 hres = disp_call(ctx->script, obj, id, &dp, res);
481 IDispatch_Release(obj);
482 if(FAILED(hres))
483 return hres;
485 stack_popn(ctx, arg_cnt);
486 return S_OK;
489 static HRESULT interp_mcall(exec_ctx_t *ctx)
491 VARIANT res;
492 HRESULT hres;
494 TRACE("\n");
496 hres = do_mcall(ctx, &res);
497 if(FAILED(hres))
498 return hres;
500 return stack_push(ctx, &res);
503 static HRESULT interp_mcallv(exec_ctx_t *ctx)
505 TRACE("\n");
507 return do_mcall(ctx, NULL);
510 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
512 ref_t ref;
513 HRESULT hres;
515 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
516 if(FAILED(hres))
517 return hres;
519 switch(ref.type) {
520 case REF_VAR: {
521 VARIANT *v = ref.u.v;
523 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
524 v = V_VARIANTREF(v);
526 if(own_val) {
527 VariantClear(v);
528 *v = *val;
529 hres = S_OK;
530 }else {
531 hres = VariantCopy(v, val);
533 break;
535 case REF_DISP:
536 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
537 if(own_val)
538 VariantClear(val);
539 break;
540 case REF_FUNC:
541 FIXME("functions not implemented\n");
542 return E_NOTIMPL;
543 case REF_OBJ:
544 FIXME("REF_OBJ\n");
545 return E_NOTIMPL;
546 case REF_CONST:
547 FIXME("REF_CONST\n");
548 return E_NOTIMPL;
549 case REF_NONE:
550 if(ctx->func->code_ctx->option_explicit) {
551 FIXME("throw exception\n");
552 hres = E_FAIL;
553 }else {
554 TRACE("creating variable %s\n", debugstr_w(name));
555 hres = add_dynamic_var(ctx, name, FALSE, val, own_val);
559 return hres;
562 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
564 const BSTR arg = ctx->instr->arg1.bstr;
565 variant_val_t v;
566 HRESULT hres;
568 TRACE("%s\n", debugstr_w(arg));
570 hres = stack_pop_val(ctx, &v);
571 if(FAILED(hres))
572 return hres;
574 return assign_ident(ctx, arg, v.v, v.owned);
577 static HRESULT interp_set_ident(exec_ctx_t *ctx)
579 const BSTR arg = ctx->instr->arg1.bstr;
580 IDispatch *disp;
581 VARIANT v;
582 HRESULT hres;
584 TRACE("%s\n", debugstr_w(arg));
586 hres = stack_pop_disp(ctx, &disp);
587 if(FAILED(hres))
588 return hres;
590 V_VT(&v) = VT_DISPATCH;
591 V_DISPATCH(&v) = disp;
592 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
595 static HRESULT interp_assign_member(exec_ctx_t *ctx)
597 BSTR identifier = ctx->instr->arg1.bstr;
598 variant_val_t val;
599 IDispatch *obj;
600 DISPID id;
601 HRESULT hres;
603 TRACE("%s\n", debugstr_w(identifier));
605 hres = stack_pop_disp(ctx, &obj);
606 if(FAILED(hres))
607 return hres;
609 if(!obj) {
610 FIXME("NULL obj\n");
611 return E_FAIL;
614 hres = stack_pop_val(ctx, &val);
615 if(FAILED(hres)) {
616 IDispatch_Release(obj);
617 return hres;
620 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
621 if(SUCCEEDED(hres))
622 hres = disp_propput(ctx->script, obj, id, val.v);
624 release_val(&val);
625 IDispatch_Release(obj);
626 return hres;
629 static HRESULT interp_set_member(exec_ctx_t *ctx)
631 BSTR identifier = ctx->instr->arg1.bstr;
632 IDispatch *obj, *val;
633 DISPID id;
634 HRESULT hres;
636 TRACE("%s\n", debugstr_w(identifier));
638 hres = stack_pop_disp(ctx, &obj);
639 if(FAILED(hres))
640 return hres;
642 if(!obj) {
643 FIXME("NULL obj\n");
644 return E_FAIL;
647 hres = stack_pop_disp(ctx, &val);
648 if(FAILED(hres)) {
649 IDispatch_Release(obj);
650 return hres;
653 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
654 if(SUCCEEDED(hres)) {
655 VARIANT v;
657 V_VT(&v) = VT_DISPATCH;
658 V_DISPATCH(&v) = val;
659 hres = disp_propput(ctx->script, obj, id, &v);
662 if(val)
663 IDispatch_Release(val);
664 IDispatch_Release(obj);
665 return hres;
668 static HRESULT interp_const(exec_ctx_t *ctx)
670 BSTR arg = ctx->instr->arg1.bstr;
671 variant_val_t val;
672 ref_t ref;
673 HRESULT hres;
675 TRACE("%s\n", debugstr_w(arg));
677 assert(ctx->func->type == FUNC_GLOBAL);
679 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
680 if(FAILED(hres))
681 return hres;
683 if(ref.type != REF_NONE) {
684 FIXME("%s already defined\n", debugstr_w(arg));
685 return E_FAIL;
688 hres = stack_pop_val(ctx, &val);
689 if(FAILED(hres))
690 return hres;
692 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
695 static HRESULT interp_val(exec_ctx_t *ctx)
697 variant_val_t val;
698 VARIANT v;
699 HRESULT hres;
701 TRACE("\n");
703 hres = stack_pop_val(ctx, &val);
704 if(FAILED(hres))
705 return hres;
707 if(!val.owned) {
708 V_VT(&v) = VT_EMPTY;
709 hres = VariantCopy(&v, val.v);
710 if(FAILED(hres))
711 return hres;
714 return stack_push(ctx, val.owned ? val.v : &v);
717 static HRESULT interp_pop(exec_ctx_t *ctx)
719 const unsigned n = ctx->instr->arg1.uint;
721 TRACE("%u\n", n);
723 stack_popn(ctx, n);
724 return S_OK;
727 static HRESULT interp_new(exec_ctx_t *ctx)
729 const WCHAR *arg = ctx->instr->arg1.bstr;
730 class_desc_t *class_desc;
731 vbdisp_t *obj;
732 VARIANT v;
733 HRESULT hres;
735 TRACE("%s\n", debugstr_w(arg));
737 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
738 if(!strcmpiW(class_desc->name, arg))
739 break;
741 if(!class_desc) {
742 FIXME("Class %s not found\n", debugstr_w(arg));
743 return E_FAIL;
746 hres = create_vbdisp(class_desc, &obj);
747 if(FAILED(hres))
748 return hres;
750 V_VT(&v) = VT_DISPATCH;
751 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
752 return stack_push(ctx, &v);
755 static HRESULT interp_step(exec_ctx_t *ctx)
757 const BSTR ident = ctx->instr->arg2.bstr;
758 BOOL gteq_zero;
759 VARIANT zero;
760 ref_t ref;
761 HRESULT hres;
763 TRACE("%s\n", debugstr_w(ident));
765 V_VT(&zero) = VT_I2;
766 V_I2(&zero) = 0;
767 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
768 if(FAILED(hres))
769 return hres;
771 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
773 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
774 if(FAILED(hres))
775 return hres;
777 if(ref.type != REF_VAR) {
778 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
779 return E_FAIL;
782 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
783 if(FAILED(hres))
784 return hres;
786 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
787 ctx->instr++;
788 else
789 instr_jmp(ctx, ctx->instr->arg1.uint);
790 return S_OK;
793 static HRESULT interp_jmp(exec_ctx_t *ctx)
795 const unsigned arg = ctx->instr->arg1.uint;
797 TRACE("%u\n", arg);
799 instr_jmp(ctx, arg);
800 return S_OK;
803 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
805 const unsigned arg = ctx->instr->arg1.uint;
806 variant_val_t val;
807 HRESULT hres;
809 TRACE("%u\n", arg);
811 hres = stack_pop_val(ctx, &val);
812 if(FAILED(hres))
813 return hres;
815 if(V_VT(val.v) != VT_BOOL) {
816 FIXME("unsupported for %s\n", debugstr_variant(val.v));
817 release_val(&val);
818 return E_NOTIMPL;
821 if(V_BOOL(val.v))
822 ctx->instr++;
823 else
824 instr_jmp(ctx, ctx->instr->arg1.uint);
825 return S_OK;
828 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
830 const unsigned arg = ctx->instr->arg1.uint;
831 variant_val_t val;
832 HRESULT hres;
834 TRACE("%u\n", arg);
836 hres = stack_pop_val(ctx, &val);
837 if(FAILED(hres))
838 return hres;
840 if(V_VT(val.v) != VT_BOOL) {
841 FIXME("unsupported for %s\n", debugstr_variant(val.v));
842 release_val(&val);
843 return E_NOTIMPL;
846 if(V_BOOL(val.v))
847 instr_jmp(ctx, ctx->instr->arg1.uint);
848 else
849 ctx->instr++;
850 return S_OK;
853 static HRESULT interp_ret(exec_ctx_t *ctx)
855 TRACE("\n");
857 ctx->instr = NULL;
858 return S_OK;
861 static HRESULT interp_stop(exec_ctx_t *ctx)
863 WARN("\n");
865 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
866 return S_OK;
869 static HRESULT interp_me(exec_ctx_t *ctx)
871 VARIANT v;
873 TRACE("\n");
875 IDispatch_AddRef(ctx->this_obj);
876 V_VT(&v) = VT_DISPATCH;
877 V_DISPATCH(&v) = ctx->this_obj;
878 return stack_push(ctx, &v);
881 static HRESULT interp_bool(exec_ctx_t *ctx)
883 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
884 VARIANT v;
886 TRACE("%s\n", arg ? "true" : "false");
888 V_VT(&v) = VT_BOOL;
889 V_BOOL(&v) = arg;
890 return stack_push(ctx, &v);
893 static HRESULT interp_errmode(exec_ctx_t *ctx)
895 const int err_mode = ctx->instr->arg1.uint;
897 TRACE("%d\n", err_mode);
899 ctx->resume_next = err_mode;
900 return S_OK;
903 static HRESULT interp_string(exec_ctx_t *ctx)
905 VARIANT v;
907 TRACE("\n");
909 V_VT(&v) = VT_BSTR;
910 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
911 if(!V_BSTR(&v))
912 return E_OUTOFMEMORY;
914 return stack_push(ctx, &v);
917 static HRESULT interp_long(exec_ctx_t *ctx)
919 const LONG arg = ctx->instr->arg1.lng;
920 VARIANT v;
922 TRACE("%d\n", arg);
924 V_VT(&v) = VT_I4;
925 V_I4(&v) = arg;
926 return stack_push(ctx, &v);
929 static HRESULT interp_short(exec_ctx_t *ctx)
931 const LONG arg = ctx->instr->arg1.lng;
932 VARIANT v;
934 TRACE("%d\n", arg);
936 V_VT(&v) = VT_I2;
937 V_I2(&v) = arg;
938 return stack_push(ctx, &v);
941 static HRESULT interp_double(exec_ctx_t *ctx)
943 const DOUBLE *arg = ctx->instr->arg1.dbl;
944 VARIANT v;
946 TRACE("%lf\n", *arg);
948 V_VT(&v) = VT_R8;
949 V_R8(&v) = *arg;
950 return stack_push(ctx, &v);
953 static HRESULT interp_empty(exec_ctx_t *ctx)
955 VARIANT v;
957 TRACE("\n");
959 V_VT(&v) = VT_EMPTY;
960 return stack_push(ctx, &v);
963 static HRESULT interp_null(exec_ctx_t *ctx)
965 VARIANT v;
967 TRACE("\n");
969 V_VT(&v) = VT_NULL;
970 return stack_push(ctx, &v);
973 static HRESULT interp_nothing(exec_ctx_t *ctx)
975 VARIANT v;
977 TRACE("\n");
979 V_VT(&v) = VT_DISPATCH;
980 V_DISPATCH(&v) = NULL;
981 return stack_push(ctx, &v);
984 static HRESULT interp_not(exec_ctx_t *ctx)
986 variant_val_t val;
987 VARIANT v;
988 HRESULT hres;
990 TRACE("\n");
992 hres = stack_pop_val(ctx, &val);
993 if(FAILED(hres))
994 return hres;
996 hres = VarNot(val.v, &v);
997 release_val(&val);
998 if(FAILED(hres))
999 return hres;
1001 return stack_push(ctx, &v);
1004 static HRESULT interp_and(exec_ctx_t *ctx)
1006 variant_val_t r, l;
1007 VARIANT v;
1008 HRESULT hres;
1010 TRACE("\n");
1012 hres = stack_pop_val(ctx, &r);
1013 if(FAILED(hres))
1014 return hres;
1016 hres = stack_pop_val(ctx, &l);
1017 if(SUCCEEDED(hres)) {
1018 hres = VarAnd(l.v, r.v, &v);
1019 release_val(&l);
1021 release_val(&r);
1022 if(FAILED(hres))
1023 return hres;
1025 return stack_push(ctx, &v);
1028 static HRESULT interp_or(exec_ctx_t *ctx)
1030 variant_val_t r, l;
1031 VARIANT v;
1032 HRESULT hres;
1034 TRACE("\n");
1036 hres = stack_pop_val(ctx, &r);
1037 if(FAILED(hres))
1038 return hres;
1040 hres = stack_pop_val(ctx, &l);
1041 if(SUCCEEDED(hres)) {
1042 hres = VarOr(l.v, r.v, &v);
1043 release_val(&l);
1045 release_val(&r);
1046 if(FAILED(hres))
1047 return hres;
1049 return stack_push(ctx, &v);
1052 static HRESULT interp_xor(exec_ctx_t *ctx)
1054 variant_val_t r, l;
1055 VARIANT v;
1056 HRESULT hres;
1058 TRACE("\n");
1060 hres = stack_pop_val(ctx, &r);
1061 if(FAILED(hres))
1062 return hres;
1064 hres = stack_pop_val(ctx, &l);
1065 if(SUCCEEDED(hres)) {
1066 hres = VarXor(l.v, r.v, &v);
1067 release_val(&l);
1069 release_val(&r);
1070 if(FAILED(hres))
1071 return hres;
1073 return stack_push(ctx, &v);
1076 static HRESULT interp_eqv(exec_ctx_t *ctx)
1078 variant_val_t r, l;
1079 VARIANT v;
1080 HRESULT hres;
1082 TRACE("\n");
1084 hres = stack_pop_val(ctx, &r);
1085 if(FAILED(hres))
1086 return hres;
1088 hres = stack_pop_val(ctx, &l);
1089 if(SUCCEEDED(hres)) {
1090 hres = VarEqv(l.v, r.v, &v);
1091 release_val(&l);
1093 release_val(&r);
1094 if(FAILED(hres))
1095 return hres;
1097 return stack_push(ctx, &v);
1100 static HRESULT interp_imp(exec_ctx_t *ctx)
1102 variant_val_t r, l;
1103 VARIANT v;
1104 HRESULT hres;
1106 TRACE("\n");
1108 hres = stack_pop_val(ctx, &r);
1109 if(FAILED(hres))
1110 return hres;
1112 hres = stack_pop_val(ctx, &l);
1113 if(SUCCEEDED(hres)) {
1114 hres = VarImp(l.v, r.v, &v);
1115 release_val(&l);
1117 release_val(&r);
1118 if(FAILED(hres))
1119 return hres;
1121 return stack_push(ctx, &v);
1124 static HRESULT cmp_oper(exec_ctx_t *ctx)
1126 variant_val_t l, r;
1127 HRESULT hres;
1129 hres = stack_pop_val(ctx, &r);
1130 if(FAILED(hres))
1131 return hres;
1133 hres = stack_pop_val(ctx, &l);
1134 if(SUCCEEDED(hres)) {
1135 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1136 FIXME("comparing nulls is not implemented\n");
1137 hres = E_NOTIMPL;
1138 }else {
1139 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1143 release_val(&r);
1144 release_val(&l);
1145 return hres;
1148 static HRESULT interp_equal(exec_ctx_t *ctx)
1150 VARIANT v;
1151 HRESULT hres;
1153 TRACE("\n");
1155 hres = cmp_oper(ctx);
1156 if(FAILED(hres))
1157 return hres;
1159 V_VT(&v) = VT_BOOL;
1160 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1161 return stack_push(ctx, &v);
1164 static HRESULT interp_nequal(exec_ctx_t *ctx)
1166 VARIANT v;
1167 HRESULT hres;
1169 TRACE("\n");
1171 hres = cmp_oper(ctx);
1172 if(FAILED(hres))
1173 return hres;
1175 V_VT(&v) = VT_BOOL;
1176 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1177 return stack_push(ctx, &v);
1180 static HRESULT interp_gt(exec_ctx_t *ctx)
1182 VARIANT v;
1183 HRESULT hres;
1185 TRACE("\n");
1187 hres = cmp_oper(ctx);
1188 if(FAILED(hres))
1189 return hres;
1191 V_VT(&v) = VT_BOOL;
1192 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1193 return stack_push(ctx, &v);
1196 static HRESULT interp_gteq(exec_ctx_t *ctx)
1198 VARIANT v;
1199 HRESULT hres;
1201 TRACE("\n");
1203 hres = cmp_oper(ctx);
1204 if(FAILED(hres))
1205 return hres;
1207 V_VT(&v) = VT_BOOL;
1208 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1209 return stack_push(ctx, &v);
1212 static HRESULT interp_lt(exec_ctx_t *ctx)
1214 VARIANT v;
1215 HRESULT hres;
1217 TRACE("\n");
1219 hres = cmp_oper(ctx);
1220 if(FAILED(hres))
1221 return hres;
1223 V_VT(&v) = VT_BOOL;
1224 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1225 return stack_push(ctx, &v);
1228 static HRESULT interp_lteq(exec_ctx_t *ctx)
1230 VARIANT v;
1231 HRESULT hres;
1233 TRACE("\n");
1235 hres = cmp_oper(ctx);
1236 if(FAILED(hres))
1237 return hres;
1239 V_VT(&v) = VT_BOOL;
1240 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1241 return stack_push(ctx, &v);
1244 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1246 IObjectIdentity *identity;
1247 IUnknown *unk1, *unk2;
1248 HRESULT hres;
1250 if(disp1 == disp2) {
1251 *ret = VARIANT_TRUE;
1252 return S_OK;
1255 if(!disp1 || !disp2) {
1256 *ret = VARIANT_FALSE;
1257 return S_OK;
1260 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1261 if(FAILED(hres))
1262 return hres;
1264 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1265 if(FAILED(hres)) {
1266 IUnknown_Release(unk1);
1267 return hres;
1270 if(unk1 == unk2) {
1271 *ret = VARIANT_TRUE;
1272 }else {
1273 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1274 if(SUCCEEDED(hres)) {
1275 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1276 IObjectIdentity_Release(identity);
1277 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1278 }else {
1279 *ret = VARIANT_FALSE;
1283 IUnknown_Release(unk1);
1284 IUnknown_Release(unk2);
1285 return S_OK;
1288 static HRESULT interp_is(exec_ctx_t *ctx)
1290 IDispatch *l, *r;
1291 VARIANT v;
1292 HRESULT hres;
1294 TRACE("\n");
1296 hres = stack_pop_disp(ctx, &r);
1297 if(FAILED(hres))
1298 return hres;
1300 hres = stack_pop_disp(ctx, &l);
1301 if(SUCCEEDED(hres)) {
1302 V_VT(&v) = VT_BOOL;
1303 hres = disp_cmp(l, r, &V_BOOL(&v));
1304 if(l)
1305 IDispatch_Release(l);
1307 if(r)
1308 IDispatch_Release(r);
1309 if(FAILED(hres))
1310 return hres;
1312 return stack_push(ctx, &v);
1315 static HRESULT interp_concat(exec_ctx_t *ctx)
1317 variant_val_t r, l;
1318 VARIANT v;
1319 HRESULT hres;
1321 TRACE("\n");
1323 hres = stack_pop_val(ctx, &r);
1324 if(FAILED(hres))
1325 return hres;
1327 hres = stack_pop_val(ctx, &l);
1328 if(SUCCEEDED(hres)) {
1329 hres = VarCat(l.v, r.v, &v);
1330 release_val(&l);
1332 release_val(&r);
1333 if(FAILED(hres))
1334 return hres;
1336 return stack_push(ctx, &v);
1339 static HRESULT interp_add(exec_ctx_t *ctx)
1341 variant_val_t r, l;
1342 VARIANT v;
1343 HRESULT hres;
1345 TRACE("\n");
1347 hres = stack_pop_val(ctx, &r);
1348 if(FAILED(hres))
1349 return hres;
1351 hres = stack_pop_val(ctx, &l);
1352 if(SUCCEEDED(hres)) {
1353 hres = VarAdd(l.v, r.v, &v);
1354 release_val(&l);
1356 release_val(&r);
1357 if(FAILED(hres))
1358 return hres;
1360 return stack_push(ctx, &v);
1363 static HRESULT interp_sub(exec_ctx_t *ctx)
1365 variant_val_t r, l;
1366 VARIANT v;
1367 HRESULT hres;
1369 TRACE("\n");
1371 hres = stack_pop_val(ctx, &r);
1372 if(FAILED(hres))
1373 return hres;
1375 hres = stack_pop_val(ctx, &l);
1376 if(SUCCEEDED(hres)) {
1377 hres = VarSub(l.v, r.v, &v);
1378 release_val(&l);
1380 release_val(&r);
1381 if(FAILED(hres))
1382 return hres;
1384 return stack_push(ctx, &v);
1387 static HRESULT interp_mod(exec_ctx_t *ctx)
1389 variant_val_t r, l;
1390 VARIANT v;
1391 HRESULT hres;
1393 TRACE("\n");
1395 hres = stack_pop_val(ctx, &r);
1396 if(FAILED(hres))
1397 return hres;
1399 hres = stack_pop_val(ctx, &l);
1400 if(SUCCEEDED(hres)) {
1401 hres = VarMod(l.v, r.v, &v);
1402 release_val(&l);
1404 release_val(&r);
1405 if(FAILED(hres))
1406 return hres;
1408 return stack_push(ctx, &v);
1411 static HRESULT interp_idiv(exec_ctx_t *ctx)
1413 variant_val_t r, l;
1414 VARIANT v;
1415 HRESULT hres;
1417 TRACE("\n");
1419 hres = stack_pop_val(ctx, &r);
1420 if(FAILED(hres))
1421 return hres;
1423 hres = stack_pop_val(ctx, &l);
1424 if(SUCCEEDED(hres)) {
1425 hres = VarIdiv(l.v, r.v, &v);
1426 release_val(&l);
1428 release_val(&r);
1429 if(FAILED(hres))
1430 return hres;
1432 return stack_push(ctx, &v);
1435 static HRESULT interp_div(exec_ctx_t *ctx)
1437 variant_val_t r, l;
1438 VARIANT v;
1439 HRESULT hres;
1441 TRACE("\n");
1443 hres = stack_pop_val(ctx, &r);
1444 if(FAILED(hres))
1445 return hres;
1447 hres = stack_pop_val(ctx, &l);
1448 if(SUCCEEDED(hres)) {
1449 hres = VarDiv(l.v, r.v, &v);
1450 release_val(&l);
1452 release_val(&r);
1453 if(FAILED(hres))
1454 return hres;
1456 return stack_push(ctx, &v);
1459 static HRESULT interp_mul(exec_ctx_t *ctx)
1461 variant_val_t r, l;
1462 VARIANT v;
1463 HRESULT hres;
1465 TRACE("\n");
1467 hres = stack_pop_val(ctx, &r);
1468 if(FAILED(hres))
1469 return hres;
1471 hres = stack_pop_val(ctx, &l);
1472 if(SUCCEEDED(hres)) {
1473 hres = VarMul(l.v, r.v, &v);
1474 release_val(&l);
1476 release_val(&r);
1477 if(FAILED(hres))
1478 return hres;
1480 return stack_push(ctx, &v);
1483 static HRESULT interp_exp(exec_ctx_t *ctx)
1485 variant_val_t r, l;
1486 VARIANT v;
1487 HRESULT hres;
1489 TRACE("\n");
1491 hres = stack_pop_val(ctx, &r);
1492 if(FAILED(hres))
1493 return hres;
1495 hres = stack_pop_val(ctx, &l);
1496 if(SUCCEEDED(hres)) {
1497 hres = VarPow(l.v, r.v, &v);
1498 release_val(&l);
1500 release_val(&r);
1501 if(FAILED(hres))
1502 return hres;
1504 return stack_push(ctx, &v);
1507 static HRESULT interp_neg(exec_ctx_t *ctx)
1509 variant_val_t val;
1510 VARIANT v;
1511 HRESULT hres;
1513 hres = stack_pop_val(ctx, &val);
1514 if(FAILED(hres))
1515 return hres;
1517 hres = VarNeg(val.v, &v);
1518 release_val(&val);
1519 if(FAILED(hres))
1520 return hres;
1522 return stack_push(ctx, &v);
1525 static HRESULT interp_incc(exec_ctx_t *ctx)
1527 const BSTR ident = ctx->instr->arg1.bstr;
1528 VARIANT v;
1529 ref_t ref;
1530 HRESULT hres;
1532 TRACE("\n");
1534 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1535 if(FAILED(hres))
1536 return hres;
1538 if(ref.type != REF_VAR) {
1539 FIXME("ref.type is not REF_VAR\n");
1540 return E_FAIL;
1543 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1544 if(FAILED(hres))
1545 return hres;
1547 VariantClear(ref.u.v);
1548 *ref.u.v = v;
1549 return S_OK;
1552 static const instr_func_t op_funcs[] = {
1553 #define X(x,n,a,b) interp_ ## x,
1554 OP_LIST
1555 #undef X
1558 static const unsigned op_move[] = {
1559 #define X(x,n,a,b) n,
1560 OP_LIST
1561 #undef X
1564 void release_dynamic_vars(dynamic_var_t *var)
1566 while(var) {
1567 VariantClear(&var->v);
1568 var = var->next;
1572 static void release_exec(exec_ctx_t *ctx)
1574 unsigned i;
1576 VariantClear(&ctx->ret_val);
1577 release_dynamic_vars(ctx->dynamic_vars);
1579 if(ctx->this_obj)
1580 IDispatch_Release(ctx->this_obj);
1582 if(ctx->args) {
1583 for(i=0; i < ctx->func->arg_cnt; i++)
1584 VariantClear(ctx->args+i);
1587 if(ctx->vars) {
1588 for(i=0; i < ctx->func->var_cnt; i++)
1589 VariantClear(ctx->vars+i);
1592 vbsheap_free(&ctx->heap);
1593 heap_free(ctx->args);
1594 heap_free(ctx->vars);
1595 heap_free(ctx->stack);
1598 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1600 exec_ctx_t exec = {func->code_ctx};
1601 vbsop_t op;
1602 HRESULT hres = S_OK;
1604 exec.code = func->code_ctx;
1606 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1607 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1608 return E_FAIL;
1611 vbsheap_init(&exec.heap);
1613 if(func->arg_cnt) {
1614 VARIANT *v;
1615 unsigned i;
1617 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1618 if(!exec.args) {
1619 release_exec(&exec);
1620 return E_OUTOFMEMORY;
1623 for(i=0; i < func->arg_cnt; i++) {
1624 v = get_arg(dp, i);
1625 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1626 if(func->args[i].by_ref)
1627 exec.args[i] = *v;
1628 else
1629 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1630 }else {
1631 hres = VariantCopy(exec.args+i, v);
1633 if(FAILED(hres)) {
1634 release_exec(&exec);
1635 return hres;
1638 }else {
1639 exec.args = NULL;
1642 if(func->var_cnt) {
1643 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1644 if(!exec.vars) {
1645 release_exec(&exec);
1646 return E_OUTOFMEMORY;
1648 }else {
1649 exec.vars = NULL;
1652 exec.stack_size = 16;
1653 exec.top = 0;
1654 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1655 if(!exec.stack) {
1656 release_exec(&exec);
1657 return E_OUTOFMEMORY;
1660 if(this_obj)
1661 exec.this_obj = this_obj;
1662 else if (ctx->host_global)
1663 exec.this_obj = ctx->host_global;
1664 else
1665 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1666 IDispatch_AddRef(exec.this_obj);
1668 exec.instr = exec.code->instrs + func->code_off;
1669 exec.script = ctx;
1670 exec.func = func;
1672 while(exec.instr) {
1673 op = exec.instr->op;
1674 hres = op_funcs[op](&exec);
1675 if(FAILED(hres)) {
1676 if(exec.resume_next)
1677 FIXME("Failed %08x in resume next mode\n", hres);
1678 else
1679 WARN("Failed %08x\n", hres);
1680 stack_popn(&exec, exec.top);
1681 break;
1684 exec.instr += op_move[op];
1687 assert(!exec.top);
1688 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1689 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1691 if(SUCCEEDED(hres) && res) {
1692 *res = exec.ret_val;
1693 V_VT(&exec.ret_val) = VT_EMPTY;
1696 release_exec(&exec);
1697 return hres;