crypt32: Added addref to store vtbl and use it instead of directly accessing ref.
[wine.git] / dlls / vbscript / interp.c
blobb3f424467feab4b88d20dad6df119bc829a3cac4
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 static DISPID propput_dispid = DISPID_PROPERTYPUT;
29 typedef struct {
30 vbscode_t *code;
31 instr_t *instr;
32 script_ctx_t *script;
33 function_t *func;
34 IDispatch *this_obj;
36 VARIANT *args;
37 VARIANT *vars;
39 dynamic_var_t *dynamic_vars;
40 heap_pool_t heap;
42 BOOL resume_next;
44 unsigned stack_size;
45 unsigned top;
46 VARIANT *stack;
48 VARIANT ret_val;
49 } exec_ctx_t;
51 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
53 typedef enum {
54 REF_NONE,
55 REF_DISP,
56 REF_VAR,
57 REF_OBJ,
58 REF_CONST,
59 REF_FUNC
60 } ref_type_t;
62 typedef struct {
63 ref_type_t type;
64 union {
65 struct {
66 IDispatch *disp;
67 DISPID id;
68 } d;
69 VARIANT *v;
70 function_t *f;
71 IDispatch *obj;
72 } u;
73 } ref_t;
75 typedef struct {
76 VARIANT *v;
77 VARIANT store;
78 BOOL owned;
79 } variant_val_t;
81 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
83 while(var) {
84 if(!strcmpiW(var->name, name)) {
85 ref->type = var->is_const ? REF_CONST : REF_VAR;
86 ref->u.v = &var->v;
87 return TRUE;
90 var = var->next;
93 return FALSE;
96 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
98 named_item_t *item;
99 function_t *func;
100 unsigned i;
101 DISPID id;
102 HRESULT hres;
104 static const WCHAR errW[] = {'e','r','r',0};
106 if(invoke_type == VBDISP_LET
107 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
108 && !strcmpiW(name, ctx->func->name)) {
109 ref->type = REF_VAR;
110 ref->u.v = &ctx->ret_val;
111 return S_OK;
114 for(i=0; i < ctx->func->var_cnt; i++) {
115 if(!strcmpiW(ctx->func->vars[i].name, name)) {
116 ref->type = REF_VAR;
117 ref->u.v = ctx->vars+i;
118 return TRUE;
122 for(i=0; i < ctx->func->arg_cnt; i++) {
123 if(!strcmpiW(ctx->func->args[i].name, name)) {
124 ref->type = REF_VAR;
125 ref->u.v = ctx->args+i;
126 return S_OK;
130 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
131 return S_OK;
133 if(ctx->func->type != FUNC_GLOBAL) {
134 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
135 if(SUCCEEDED(hres)) {
136 ref->type = REF_DISP;
137 ref->u.d.disp = ctx->this_obj;
138 ref->u.d.id = id;
139 return S_OK;
143 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
144 return S_OK;
146 for(func = ctx->script->global_funcs; func; func = func->next) {
147 if(!strcmpiW(func->name, name)) {
148 ref->type = REF_FUNC;
149 ref->u.f = func;
150 return S_OK;
154 if(!strcmpiW(name, errW)) {
155 ref->type = REF_OBJ;
156 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
157 return S_OK;
160 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
161 if(SUCCEEDED(hres)) {
162 ref->type = REF_DISP;
163 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
164 ref->u.d.id = id;
165 return S_OK;
168 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
169 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
170 if(!item->disp) {
171 IUnknown *unk;
173 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
174 if(FAILED(hres)) {
175 WARN("GetItemInfo failed: %08x\n", hres);
176 continue;
179 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
180 IUnknown_Release(unk);
181 if(FAILED(hres)) {
182 WARN("object does not implement IDispatch\n");
183 continue;
187 ref->type = REF_OBJ;
188 ref->u.obj = item->disp;
189 return S_OK;
193 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
194 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
195 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
196 if(SUCCEEDED(hres)) {
197 ref->type = REF_DISP;
198 ref->u.d.disp = item->disp;
199 ref->u.d.id = id;
200 return S_OK;
205 ref->type = REF_NONE;
206 return S_OK;
209 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
210 BOOL is_const, VARIANT *val, BOOL own_val, VARIANT **out_var)
212 dynamic_var_t *new_var;
213 heap_pool_t *heap;
214 WCHAR *str;
215 unsigned size;
216 HRESULT hres;
218 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
220 new_var = heap_pool_alloc(heap, sizeof(*new_var));
221 if(!new_var)
222 return E_OUTOFMEMORY;
224 size = (strlenW(name)+1)*sizeof(WCHAR);
225 str = heap_pool_alloc(heap, size);
226 if(!str)
227 return E_OUTOFMEMORY;
228 memcpy(str, name, size);
229 new_var->name = str;
230 new_var->is_const = is_const;
232 if(own_val) {
233 new_var->v = *val;
234 }else {
235 V_VT(&new_var->v) = VT_EMPTY;
236 hres = VariantCopy(&new_var->v, val);
237 if(FAILED(hres))
238 return hres;
241 if(ctx->func->type == FUNC_GLOBAL) {
242 new_var->next = ctx->script->global_vars;
243 ctx->script->global_vars = new_var;
244 }else {
245 new_var->next = ctx->dynamic_vars;
246 ctx->dynamic_vars = new_var;
249 if(out_var)
250 *out_var = &new_var->v;
252 return S_OK;
255 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
257 assert(ctx->top);
258 return ctx->stack + --ctx->top;
261 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
263 assert(ctx->top >= n);
264 return ctx->stack + (ctx->top-n-1);
267 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
269 if(ctx->stack_size == ctx->top) {
270 VARIANT *new_stack;
272 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
273 if(!new_stack) {
274 VariantClear(v);
275 return E_OUTOFMEMORY;
278 ctx->stack = new_stack;
279 ctx->stack_size *= 2;
282 ctx->stack[ctx->top++] = *v;
283 return S_OK;
286 static inline HRESULT stack_push_null(exec_ctx_t *ctx)
288 VARIANT v;
289 V_VT(&v) = VT_NULL;
290 return stack_push(ctx, &v);
293 static void stack_popn(exec_ctx_t *ctx, unsigned n)
295 while(n--)
296 VariantClear(stack_pop(ctx));
299 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
301 VARIANT *var;
303 var = stack_pop(ctx);
305 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
306 v->owned = FALSE;
307 var = V_VARIANTREF(var);
308 }else {
309 v->owned = TRUE;
312 if(V_VT(var) == VT_DISPATCH) {
313 DISPPARAMS dp = {0};
314 HRESULT hres;
316 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
317 if(v->owned)
318 IDispatch_Release(V_DISPATCH(var));
319 if(FAILED(hres))
320 return hres;
322 v->owned = TRUE;
323 v->v = &v->store;
324 }else {
325 v->v = var;
328 return S_OK;
331 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
333 VARIANT *v = stack_top(ctx, n);
334 HRESULT hres;
336 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
337 VARIANT *ref = V_VARIANTREF(v);
339 V_VT(v) = VT_EMPTY;
340 hres = VariantCopy(v, ref);
341 if(FAILED(hres))
342 return hres;
345 if(V_VT(v) == VT_DISPATCH) {
346 DISPPARAMS dp = {0};
347 IDispatch *disp;
349 disp = V_DISPATCH(v);
350 V_VT(v) = VT_EMPTY;
351 hres = disp_call(ctx->script, disp, DISPID_VALUE, &dp, v);
352 IDispatch_Release(disp);
353 if(FAILED(hres))
354 return hres;
357 return S_OK;
360 static inline void release_val(variant_val_t *v)
362 if(v->owned)
363 VariantClear(v->v);
366 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
368 variant_val_t val;
369 HRESULT hres;
371 hres = stack_pop_val(ctx, &val);
372 if(FAILED(hres))
373 return hres;
375 switch (V_VT(val.v))
377 case VT_BOOL:
378 *b = V_BOOL(val.v);
379 break;
380 case VT_NULL:
381 *b = FALSE;
382 break;
383 case VT_I2:
384 *b = V_I2(val.v);
385 break;
386 case VT_I4:
387 *b = V_I4(val.v);
388 break;
389 default:
390 FIXME("unsupported for %s\n", debugstr_variant(val.v));
391 release_val(&val);
392 return E_NOTIMPL;
394 return S_OK;
397 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
399 VARIANT *v = stack_pop(ctx);
401 if(V_VT(v) == VT_DISPATCH) {
402 *ret = V_DISPATCH(v);
403 return S_OK;
406 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
407 FIXME("not supported type: %s\n", debugstr_variant(v));
408 VariantClear(v);
409 return E_FAIL;
412 v = V_BYREF(v);
413 if(V_VT(v) != VT_DISPATCH) {
414 FIXME("not disp %s\n", debugstr_variant(v));
415 return E_FAIL;
418 if(V_DISPATCH(v))
419 IDispatch_AddRef(V_DISPATCH(v));
420 *ret = V_DISPATCH(v);
421 return S_OK;
424 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
426 VARIANT *v = stack_top(ctx, n), *ref;
428 if(V_VT(v) != VT_DISPATCH) {
429 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
430 FIXME("not supported type: %s\n", debugstr_variant(v));
431 return E_FAIL;
434 ref = V_VARIANTREF(v);
435 if(V_VT(ref) != VT_DISPATCH) {
436 FIXME("not disp %s\n", debugstr_variant(ref));
437 return E_FAIL;
440 V_VT(v) = VT_DISPATCH;
441 V_DISPATCH(v) = V_DISPATCH(ref);
442 if(V_DISPATCH(v))
443 IDispatch_AddRef(V_DISPATCH(v));
446 if(disp)
447 *disp = V_DISPATCH(v);
448 return S_OK;
451 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
453 ctx->instr = ctx->code->instrs + addr;
456 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
458 dp->cNamedArgs = is_propput ? 1 : 0;
459 dp->cArgs = arg_cnt + dp->cNamedArgs;
460 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
462 if(arg_cnt) {
463 VARIANT tmp;
464 unsigned i;
466 assert(ctx->top >= arg_cnt);
468 for(i=1; i*2 <= arg_cnt; i++) {
469 tmp = ctx->stack[ctx->top-i];
470 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
471 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
474 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
475 }else {
476 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
480 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
482 BSTR identifier = ctx->instr->arg1.bstr;
483 const unsigned arg_cnt = ctx->instr->arg2.uint;
484 DISPPARAMS dp;
485 ref_t ref;
486 HRESULT hres;
488 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
489 if(FAILED(hres))
490 return hres;
492 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
494 switch(ref.type) {
495 case REF_VAR:
496 case REF_CONST:
497 if(!res) {
498 FIXME("REF_VAR no res\n");
499 return E_NOTIMPL;
502 if(arg_cnt) {
503 FIXME("arguments not implemented\n");
504 return E_NOTIMPL;
507 V_VT(res) = VT_BYREF|VT_VARIANT;
508 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
509 break;
510 case REF_DISP:
511 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
512 if(FAILED(hres))
513 return hres;
514 break;
515 case REF_FUNC:
516 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
517 if(FAILED(hres))
518 return hres;
519 break;
520 case REF_OBJ:
521 if(arg_cnt) {
522 FIXME("arguments on object\n");
523 return E_NOTIMPL;
526 if(res) {
527 IDispatch_AddRef(ref.u.obj);
528 V_VT(res) = VT_DISPATCH;
529 V_DISPATCH(res) = ref.u.obj;
531 break;
532 case REF_NONE:
533 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
534 VARIANT v, *new;
535 VariantInit(&v);
536 hres = add_dynamic_var(ctx, identifier, FALSE, &v, FALSE, &new);
537 if(FAILED(hres))
538 return hres;
539 V_VT(res) = VT_BYREF|VT_VARIANT;
540 V_BYREF(res) = new;
541 break;
543 FIXME("%s not found\n", debugstr_w(identifier));
544 return DISP_E_UNKNOWNNAME;
547 stack_popn(ctx, arg_cnt);
548 return S_OK;
551 static HRESULT interp_icall(exec_ctx_t *ctx)
553 VARIANT v;
554 HRESULT hres;
556 TRACE("\n");
558 hres = do_icall(ctx, &v);
559 if(FAILED(hres))
560 return hres;
562 return stack_push(ctx, &v);
565 static HRESULT interp_icallv(exec_ctx_t *ctx)
567 TRACE("\n");
568 return do_icall(ctx, NULL);
571 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
573 const BSTR identifier = ctx->instr->arg1.bstr;
574 const unsigned arg_cnt = ctx->instr->arg2.uint;
575 IDispatch *obj;
576 DISPPARAMS dp;
577 DISPID id;
578 HRESULT hres;
580 hres = stack_pop_disp(ctx, &obj);
581 if(FAILED(hres))
582 return hres;
584 if(!obj) {
585 FIXME("NULL obj\n");
586 return E_FAIL;
589 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
591 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
592 if(SUCCEEDED(hres))
593 hres = disp_call(ctx->script, obj, id, &dp, res);
594 IDispatch_Release(obj);
595 if(FAILED(hres))
596 return hres;
598 stack_popn(ctx, arg_cnt);
599 return S_OK;
602 static HRESULT interp_mcall(exec_ctx_t *ctx)
604 VARIANT res;
605 HRESULT hres;
607 TRACE("\n");
609 hres = do_mcall(ctx, &res);
610 if(FAILED(hres))
611 return hres;
613 return stack_push(ctx, &res);
616 static HRESULT interp_mcallv(exec_ctx_t *ctx)
618 TRACE("\n");
620 return do_mcall(ctx, NULL);
623 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
625 ref_t ref;
626 HRESULT hres;
628 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
629 if(FAILED(hres))
630 return hres;
632 switch(ref.type) {
633 case REF_VAR: {
634 VARIANT *v = ref.u.v;
636 if(arg_cnt(dp)) {
637 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
638 return E_NOTIMPL;
641 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
642 v = V_VARIANTREF(v);
644 hres = VariantCopy(v, dp->rgvarg);
645 break;
647 case REF_DISP:
648 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
649 break;
650 case REF_FUNC:
651 FIXME("functions not implemented\n");
652 return E_NOTIMPL;
653 case REF_OBJ:
654 FIXME("REF_OBJ\n");
655 return E_NOTIMPL;
656 case REF_CONST:
657 FIXME("REF_CONST\n");
658 return E_NOTIMPL;
659 case REF_NONE:
660 if(ctx->func->code_ctx->option_explicit) {
661 FIXME("throw exception\n");
662 hres = E_FAIL;
663 }else {
664 if(arg_cnt(dp)) {
665 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
666 return E_NOTIMPL;
669 TRACE("creating variable %s\n", debugstr_w(name));
670 hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE, NULL);
674 return hres;
677 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
679 const BSTR arg = ctx->instr->arg1.bstr;
680 const unsigned arg_cnt = ctx->instr->arg2.uint;
681 DISPPARAMS dp;
682 HRESULT hres;
684 TRACE("%s\n", debugstr_w(arg));
686 hres = stack_assume_val(ctx, arg_cnt);
687 if(FAILED(hres))
688 return hres;
690 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
691 hres = assign_ident(ctx, arg, &dp);
692 if(FAILED(hres))
693 return hres;
695 stack_popn(ctx, arg_cnt+1);
696 return S_OK;
699 static HRESULT interp_set_ident(exec_ctx_t *ctx)
701 const BSTR arg = ctx->instr->arg1.bstr;
702 const unsigned arg_cnt = ctx->instr->arg2.uint;
703 DISPPARAMS dp;
704 HRESULT hres;
706 TRACE("%s\n", debugstr_w(arg));
708 if(arg_cnt) {
709 FIXME("arguments not supported\n");
710 return E_NOTIMPL;
713 hres = stack_assume_disp(ctx, 0, NULL);
714 if(FAILED(hres))
715 return hres;
717 vbstack_to_dp(ctx, 0, TRUE, &dp);
718 hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
719 if(FAILED(hres))
720 return hres;
722 stack_popn(ctx, 1);
723 return S_OK;
726 static HRESULT interp_assign_member(exec_ctx_t *ctx)
728 BSTR identifier = ctx->instr->arg1.bstr;
729 const unsigned arg_cnt = ctx->instr->arg2.uint;
730 IDispatch *obj;
731 DISPPARAMS dp;
732 DISPID id;
733 HRESULT hres;
735 TRACE("%s\n", debugstr_w(identifier));
737 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
738 if(FAILED(hres))
739 return hres;
741 if(!obj) {
742 FIXME("NULL obj\n");
743 return E_FAIL;
746 hres = stack_assume_val(ctx, arg_cnt);
747 if(FAILED(hres))
748 return hres;
750 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
751 if(SUCCEEDED(hres)) {
752 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
753 hres = disp_propput(ctx->script, obj, id, &dp);
755 if(FAILED(hres))
756 return hres;
758 stack_popn(ctx, arg_cnt+2);
759 return S_OK;
762 static HRESULT interp_set_member(exec_ctx_t *ctx)
764 BSTR identifier = ctx->instr->arg1.bstr;
765 const unsigned arg_cnt = ctx->instr->arg2.uint;
766 IDispatch *obj;
767 DISPPARAMS dp;
768 DISPID id;
769 HRESULT hres;
771 TRACE("%s\n", debugstr_w(identifier));
773 if(arg_cnt) {
774 FIXME("arguments not supported\n");
775 return E_NOTIMPL;
778 hres = stack_assume_disp(ctx, 1, &obj);
779 if(FAILED(hres))
780 return hres;
782 if(!obj) {
783 FIXME("NULL obj\n");
784 return E_FAIL;
787 hres = stack_assume_disp(ctx, 0, NULL);
788 if(FAILED(hres))
789 return hres;
791 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
792 if(SUCCEEDED(hres)) {
793 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
794 hres = disp_propput(ctx->script, obj, id, &dp);
796 if(FAILED(hres))
797 return hres;
799 stack_popn(ctx, 2);
800 return S_OK;
803 static HRESULT interp_const(exec_ctx_t *ctx)
805 BSTR arg = ctx->instr->arg1.bstr;
806 variant_val_t val;
807 ref_t ref;
808 HRESULT hres;
810 TRACE("%s\n", debugstr_w(arg));
812 assert(ctx->func->type == FUNC_GLOBAL);
814 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
815 if(FAILED(hres))
816 return hres;
818 if(ref.type != REF_NONE) {
819 FIXME("%s already defined\n", debugstr_w(arg));
820 return E_FAIL;
823 hres = stack_pop_val(ctx, &val);
824 if(FAILED(hres))
825 return hres;
827 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned, NULL);
830 static HRESULT interp_val(exec_ctx_t *ctx)
832 variant_val_t val;
833 VARIANT v;
834 HRESULT hres;
836 TRACE("\n");
838 hres = stack_pop_val(ctx, &val);
839 if(FAILED(hres))
840 return hres;
842 if(!val.owned) {
843 V_VT(&v) = VT_EMPTY;
844 hres = VariantCopy(&v, val.v);
845 if(FAILED(hres))
846 return hres;
849 return stack_push(ctx, val.owned ? val.v : &v);
852 static HRESULT interp_pop(exec_ctx_t *ctx)
854 const unsigned n = ctx->instr->arg1.uint;
856 TRACE("%u\n", n);
858 stack_popn(ctx, n);
859 return S_OK;
862 static HRESULT interp_new(exec_ctx_t *ctx)
864 const WCHAR *arg = ctx->instr->arg1.bstr;
865 class_desc_t *class_desc;
866 vbdisp_t *obj;
867 VARIANT v;
868 HRESULT hres;
870 TRACE("%s\n", debugstr_w(arg));
872 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
873 if(!strcmpiW(class_desc->name, arg))
874 break;
876 if(!class_desc) {
877 FIXME("Class %s not found\n", debugstr_w(arg));
878 return E_FAIL;
881 hres = create_vbdisp(class_desc, &obj);
882 if(FAILED(hres))
883 return hres;
885 V_VT(&v) = VT_DISPATCH;
886 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
887 return stack_push(ctx, &v);
890 static HRESULT interp_step(exec_ctx_t *ctx)
892 const BSTR ident = ctx->instr->arg2.bstr;
893 BOOL gteq_zero;
894 VARIANT zero;
895 ref_t ref;
896 HRESULT hres;
898 TRACE("%s\n", debugstr_w(ident));
900 V_VT(&zero) = VT_I2;
901 V_I2(&zero) = 0;
902 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
903 if(FAILED(hres))
904 return hres;
906 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
908 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
909 if(FAILED(hres))
910 return hres;
912 if(ref.type != REF_VAR) {
913 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
914 return E_FAIL;
917 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
918 if(FAILED(hres))
919 return hres;
921 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
922 ctx->instr++;
923 }else {
924 stack_popn(ctx, 2);
925 instr_jmp(ctx, ctx->instr->arg1.uint);
927 return S_OK;
930 static HRESULT interp_newenum(exec_ctx_t *ctx)
932 VARIANT *v, r;
933 HRESULT hres;
935 TRACE("\n");
937 v = stack_pop(ctx);
938 switch(V_VT(v)) {
939 case VT_DISPATCH: {
940 IEnumVARIANT *iter;
941 DISPPARAMS dp = {0};
942 VARIANT iterv;
944 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_NEWENUM, &dp, &iterv);
945 VariantClear(v);
946 if(FAILED(hres))
947 return hres;
949 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
950 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
951 VariantClear(&iterv);
952 return hres;
955 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
956 IUnknown_Release(V_UNKNOWN(&iterv));
957 if(FAILED(hres)) {
958 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
959 return hres;
962 V_VT(&r) = VT_UNKNOWN;
963 V_UNKNOWN(&r) = (IUnknown*)iter;
964 break;
966 default:
967 FIXME("Unsupported for %s\n", debugstr_variant(v));
968 VariantClear(v);
969 return E_NOTIMPL;
972 return stack_push(ctx, &r);
975 static HRESULT interp_enumnext(exec_ctx_t *ctx)
977 const unsigned loop_end = ctx->instr->arg1.uint;
978 const BSTR ident = ctx->instr->arg2.bstr;
979 VARIANT v;
980 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
981 IEnumVARIANT *iter;
982 BOOL do_continue;
983 HRESULT hres;
985 TRACE("\n");
987 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
988 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
990 V_VT(&v) = VT_EMPTY;
991 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
992 if(FAILED(hres))
993 return hres;
995 do_continue = hres == S_OK;
996 hres = assign_ident(ctx, ident, &dp);
997 VariantClear(&v);
998 if(FAILED(hres))
999 return hres;
1001 if(do_continue) {
1002 ctx->instr++;
1003 }else {
1004 stack_pop(ctx);
1005 instr_jmp(ctx, loop_end);
1007 return S_OK;
1010 static HRESULT interp_jmp(exec_ctx_t *ctx)
1012 const unsigned arg = ctx->instr->arg1.uint;
1014 TRACE("%u\n", arg);
1016 instr_jmp(ctx, arg);
1017 return S_OK;
1020 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1022 const unsigned arg = ctx->instr->arg1.uint;
1023 HRESULT hres;
1024 BOOL b;
1026 TRACE("%u\n", arg);
1028 hres = stack_pop_bool(ctx, &b);
1029 if(FAILED(hres))
1030 return hres;
1032 if(b)
1033 ctx->instr++;
1034 else
1035 instr_jmp(ctx, ctx->instr->arg1.uint);
1036 return S_OK;
1039 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1041 const unsigned arg = ctx->instr->arg1.uint;
1042 HRESULT hres;
1043 BOOL b;
1045 TRACE("%u\n", arg);
1047 hres = stack_pop_bool(ctx, &b);
1048 if(FAILED(hres))
1049 return hres;
1051 if(b)
1052 instr_jmp(ctx, ctx->instr->arg1.uint);
1053 else
1054 ctx->instr++;
1055 return S_OK;
1058 static HRESULT interp_ret(exec_ctx_t *ctx)
1060 TRACE("\n");
1062 ctx->instr = NULL;
1063 return S_OK;
1066 static HRESULT interp_stop(exec_ctx_t *ctx)
1068 WARN("\n");
1070 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1071 return S_OK;
1074 static HRESULT interp_me(exec_ctx_t *ctx)
1076 VARIANT v;
1078 TRACE("\n");
1080 IDispatch_AddRef(ctx->this_obj);
1081 V_VT(&v) = VT_DISPATCH;
1082 V_DISPATCH(&v) = ctx->this_obj;
1083 return stack_push(ctx, &v);
1086 static HRESULT interp_bool(exec_ctx_t *ctx)
1088 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1089 VARIANT v;
1091 TRACE("%s\n", arg ? "true" : "false");
1093 V_VT(&v) = VT_BOOL;
1094 V_BOOL(&v) = arg;
1095 return stack_push(ctx, &v);
1098 static HRESULT interp_errmode(exec_ctx_t *ctx)
1100 const int err_mode = ctx->instr->arg1.uint;
1102 TRACE("%d\n", err_mode);
1104 ctx->resume_next = err_mode;
1105 return S_OK;
1108 static HRESULT interp_string(exec_ctx_t *ctx)
1110 VARIANT v;
1112 TRACE("\n");
1114 V_VT(&v) = VT_BSTR;
1115 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1116 if(!V_BSTR(&v))
1117 return E_OUTOFMEMORY;
1119 return stack_push(ctx, &v);
1122 static HRESULT interp_long(exec_ctx_t *ctx)
1124 const LONG arg = ctx->instr->arg1.lng;
1125 VARIANT v;
1127 TRACE("%d\n", arg);
1129 V_VT(&v) = VT_I4;
1130 V_I4(&v) = arg;
1131 return stack_push(ctx, &v);
1134 static HRESULT interp_short(exec_ctx_t *ctx)
1136 const LONG arg = ctx->instr->arg1.lng;
1137 VARIANT v;
1139 TRACE("%d\n", arg);
1141 V_VT(&v) = VT_I2;
1142 V_I2(&v) = arg;
1143 return stack_push(ctx, &v);
1146 static HRESULT interp_double(exec_ctx_t *ctx)
1148 const DOUBLE *arg = ctx->instr->arg1.dbl;
1149 VARIANT v;
1151 TRACE("%lf\n", *arg);
1153 V_VT(&v) = VT_R8;
1154 V_R8(&v) = *arg;
1155 return stack_push(ctx, &v);
1158 static HRESULT interp_empty(exec_ctx_t *ctx)
1160 VARIANT v;
1162 TRACE("\n");
1164 V_VT(&v) = VT_EMPTY;
1165 return stack_push(ctx, &v);
1168 static HRESULT interp_null(exec_ctx_t *ctx)
1170 TRACE("\n");
1171 return stack_push_null(ctx);
1174 static HRESULT interp_nothing(exec_ctx_t *ctx)
1176 VARIANT v;
1178 TRACE("\n");
1180 V_VT(&v) = VT_DISPATCH;
1181 V_DISPATCH(&v) = NULL;
1182 return stack_push(ctx, &v);
1185 static HRESULT interp_not(exec_ctx_t *ctx)
1187 variant_val_t val;
1188 VARIANT v;
1189 HRESULT hres;
1191 TRACE("\n");
1193 hres = stack_pop_val(ctx, &val);
1194 if(FAILED(hres))
1195 return hres;
1197 hres = VarNot(val.v, &v);
1198 release_val(&val);
1199 if(FAILED(hres))
1200 return hres;
1202 return stack_push(ctx, &v);
1205 static HRESULT interp_and(exec_ctx_t *ctx)
1207 variant_val_t r, l;
1208 VARIANT v;
1209 HRESULT hres;
1211 TRACE("\n");
1213 hres = stack_pop_val(ctx, &r);
1214 if(FAILED(hres))
1215 return hres;
1217 hres = stack_pop_val(ctx, &l);
1218 if(SUCCEEDED(hres)) {
1219 hres = VarAnd(l.v, r.v, &v);
1220 release_val(&l);
1222 release_val(&r);
1223 if(FAILED(hres))
1224 return hres;
1226 return stack_push(ctx, &v);
1229 static HRESULT interp_or(exec_ctx_t *ctx)
1231 variant_val_t r, l;
1232 VARIANT v;
1233 HRESULT hres;
1235 TRACE("\n");
1237 hres = stack_pop_val(ctx, &r);
1238 if(FAILED(hres))
1239 return hres;
1241 hres = stack_pop_val(ctx, &l);
1242 if(SUCCEEDED(hres)) {
1243 hres = VarOr(l.v, r.v, &v);
1244 release_val(&l);
1246 release_val(&r);
1247 if(FAILED(hres))
1248 return hres;
1250 return stack_push(ctx, &v);
1253 static HRESULT interp_xor(exec_ctx_t *ctx)
1255 variant_val_t r, l;
1256 VARIANT v;
1257 HRESULT hres;
1259 TRACE("\n");
1261 hres = stack_pop_val(ctx, &r);
1262 if(FAILED(hres))
1263 return hres;
1265 hres = stack_pop_val(ctx, &l);
1266 if(SUCCEEDED(hres)) {
1267 hres = VarXor(l.v, r.v, &v);
1268 release_val(&l);
1270 release_val(&r);
1271 if(FAILED(hres))
1272 return hres;
1274 return stack_push(ctx, &v);
1277 static HRESULT interp_eqv(exec_ctx_t *ctx)
1279 variant_val_t r, l;
1280 VARIANT v;
1281 HRESULT hres;
1283 TRACE("\n");
1285 hres = stack_pop_val(ctx, &r);
1286 if(FAILED(hres))
1287 return hres;
1289 hres = stack_pop_val(ctx, &l);
1290 if(SUCCEEDED(hres)) {
1291 hres = VarEqv(l.v, r.v, &v);
1292 release_val(&l);
1294 release_val(&r);
1295 if(FAILED(hres))
1296 return hres;
1298 return stack_push(ctx, &v);
1301 static HRESULT interp_imp(exec_ctx_t *ctx)
1303 variant_val_t r, l;
1304 VARIANT v;
1305 HRESULT hres;
1307 TRACE("\n");
1309 hres = stack_pop_val(ctx, &r);
1310 if(FAILED(hres))
1311 return hres;
1313 hres = stack_pop_val(ctx, &l);
1314 if(SUCCEEDED(hres)) {
1315 hres = VarImp(l.v, r.v, &v);
1316 release_val(&l);
1318 release_val(&r);
1319 if(FAILED(hres))
1320 return hres;
1322 return stack_push(ctx, &v);
1325 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1327 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1329 /* FIXME: Fix comparing string to number */
1331 return VarCmp(l, r, ctx->script->lcid, 0);
1334 static HRESULT cmp_oper(exec_ctx_t *ctx)
1336 variant_val_t l, r;
1337 HRESULT hres;
1339 hres = stack_pop_val(ctx, &r);
1340 if(FAILED(hres))
1341 return hres;
1343 hres = stack_pop_val(ctx, &l);
1344 if(SUCCEEDED(hres)) {
1345 hres = var_cmp(ctx, l.v, r.v);
1346 release_val(&l);
1349 release_val(&r);
1350 return hres;
1353 static HRESULT interp_equal(exec_ctx_t *ctx)
1355 VARIANT v;
1356 HRESULT hres;
1358 TRACE("\n");
1360 hres = cmp_oper(ctx);
1361 if(FAILED(hres))
1362 return hres;
1363 if(hres == VARCMP_NULL)
1364 return stack_push_null(ctx);
1366 V_VT(&v) = VT_BOOL;
1367 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1368 return stack_push(ctx, &v);
1371 static HRESULT interp_nequal(exec_ctx_t *ctx)
1373 VARIANT v;
1374 HRESULT hres;
1376 TRACE("\n");
1378 hres = cmp_oper(ctx);
1379 if(FAILED(hres))
1380 return hres;
1381 if(hres == VARCMP_NULL)
1382 return stack_push_null(ctx);
1384 V_VT(&v) = VT_BOOL;
1385 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1386 return stack_push(ctx, &v);
1389 static HRESULT interp_gt(exec_ctx_t *ctx)
1391 VARIANT v;
1392 HRESULT hres;
1394 TRACE("\n");
1396 hres = cmp_oper(ctx);
1397 if(FAILED(hres))
1398 return hres;
1399 if(hres == VARCMP_NULL)
1400 return stack_push_null(ctx);
1402 V_VT(&v) = VT_BOOL;
1403 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1404 return stack_push(ctx, &v);
1407 static HRESULT interp_gteq(exec_ctx_t *ctx)
1409 VARIANT v;
1410 HRESULT hres;
1412 TRACE("\n");
1414 hres = cmp_oper(ctx);
1415 if(FAILED(hres))
1416 return hres;
1417 if(hres == VARCMP_NULL)
1418 return stack_push_null(ctx);
1420 V_VT(&v) = VT_BOOL;
1421 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1422 return stack_push(ctx, &v);
1425 static HRESULT interp_lt(exec_ctx_t *ctx)
1427 VARIANT v;
1428 HRESULT hres;
1430 TRACE("\n");
1432 hres = cmp_oper(ctx);
1433 if(FAILED(hres))
1434 return hres;
1435 if(hres == VARCMP_NULL)
1436 return stack_push_null(ctx);
1438 V_VT(&v) = VT_BOOL;
1439 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1440 return stack_push(ctx, &v);
1443 static HRESULT interp_lteq(exec_ctx_t *ctx)
1445 VARIANT v;
1446 HRESULT hres;
1448 TRACE("\n");
1450 hres = cmp_oper(ctx);
1451 if(FAILED(hres))
1452 return hres;
1453 if(hres == VARCMP_NULL)
1454 return stack_push_null(ctx);
1456 V_VT(&v) = VT_BOOL;
1457 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1458 return stack_push(ctx, &v);
1461 static HRESULT interp_case(exec_ctx_t *ctx)
1463 const unsigned arg = ctx->instr->arg1.uint;
1464 variant_val_t v;
1465 HRESULT hres;
1467 TRACE("%d\n", arg);
1469 hres = stack_pop_val(ctx, &v);
1470 if(FAILED(hres))
1471 return hres;
1473 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1474 release_val(&v);
1475 if(FAILED(hres))
1476 return hres;
1478 if(hres == VARCMP_EQ) {
1479 stack_popn(ctx, 1);
1480 instr_jmp(ctx, arg);
1481 }else {
1482 ctx->instr++;
1485 return S_OK;
1488 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1490 IObjectIdentity *identity;
1491 IUnknown *unk1, *unk2;
1492 HRESULT hres;
1494 if(disp1 == disp2) {
1495 *ret = VARIANT_TRUE;
1496 return S_OK;
1499 if(!disp1 || !disp2) {
1500 *ret = VARIANT_FALSE;
1501 return S_OK;
1504 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1505 if(FAILED(hres))
1506 return hres;
1508 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1509 if(FAILED(hres)) {
1510 IUnknown_Release(unk1);
1511 return hres;
1514 if(unk1 == unk2) {
1515 *ret = VARIANT_TRUE;
1516 }else {
1517 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1518 if(SUCCEEDED(hres)) {
1519 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1520 IObjectIdentity_Release(identity);
1521 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1522 }else {
1523 *ret = VARIANT_FALSE;
1527 IUnknown_Release(unk1);
1528 IUnknown_Release(unk2);
1529 return S_OK;
1532 static HRESULT interp_is(exec_ctx_t *ctx)
1534 IDispatch *l, *r;
1535 VARIANT v;
1536 HRESULT hres;
1538 TRACE("\n");
1540 hres = stack_pop_disp(ctx, &r);
1541 if(FAILED(hres))
1542 return hres;
1544 hres = stack_pop_disp(ctx, &l);
1545 if(SUCCEEDED(hres)) {
1546 V_VT(&v) = VT_BOOL;
1547 hres = disp_cmp(l, r, &V_BOOL(&v));
1548 if(l)
1549 IDispatch_Release(l);
1551 if(r)
1552 IDispatch_Release(r);
1553 if(FAILED(hres))
1554 return hres;
1556 return stack_push(ctx, &v);
1559 static HRESULT interp_concat(exec_ctx_t *ctx)
1561 variant_val_t r, l;
1562 VARIANT v;
1563 HRESULT hres;
1565 TRACE("\n");
1567 hres = stack_pop_val(ctx, &r);
1568 if(FAILED(hres))
1569 return hres;
1571 hres = stack_pop_val(ctx, &l);
1572 if(SUCCEEDED(hres)) {
1573 hres = VarCat(l.v, r.v, &v);
1574 release_val(&l);
1576 release_val(&r);
1577 if(FAILED(hres))
1578 return hres;
1580 return stack_push(ctx, &v);
1583 static HRESULT interp_add(exec_ctx_t *ctx)
1585 variant_val_t r, l;
1586 VARIANT v;
1587 HRESULT hres;
1589 TRACE("\n");
1591 hres = stack_pop_val(ctx, &r);
1592 if(FAILED(hres))
1593 return hres;
1595 hres = stack_pop_val(ctx, &l);
1596 if(SUCCEEDED(hres)) {
1597 hres = VarAdd(l.v, r.v, &v);
1598 release_val(&l);
1600 release_val(&r);
1601 if(FAILED(hres))
1602 return hres;
1604 return stack_push(ctx, &v);
1607 static HRESULT interp_sub(exec_ctx_t *ctx)
1609 variant_val_t r, l;
1610 VARIANT v;
1611 HRESULT hres;
1613 TRACE("\n");
1615 hres = stack_pop_val(ctx, &r);
1616 if(FAILED(hres))
1617 return hres;
1619 hres = stack_pop_val(ctx, &l);
1620 if(SUCCEEDED(hres)) {
1621 hres = VarSub(l.v, r.v, &v);
1622 release_val(&l);
1624 release_val(&r);
1625 if(FAILED(hres))
1626 return hres;
1628 return stack_push(ctx, &v);
1631 static HRESULT interp_mod(exec_ctx_t *ctx)
1633 variant_val_t r, l;
1634 VARIANT v;
1635 HRESULT hres;
1637 TRACE("\n");
1639 hres = stack_pop_val(ctx, &r);
1640 if(FAILED(hres))
1641 return hres;
1643 hres = stack_pop_val(ctx, &l);
1644 if(SUCCEEDED(hres)) {
1645 hres = VarMod(l.v, r.v, &v);
1646 release_val(&l);
1648 release_val(&r);
1649 if(FAILED(hres))
1650 return hres;
1652 return stack_push(ctx, &v);
1655 static HRESULT interp_idiv(exec_ctx_t *ctx)
1657 variant_val_t r, l;
1658 VARIANT v;
1659 HRESULT hres;
1661 TRACE("\n");
1663 hres = stack_pop_val(ctx, &r);
1664 if(FAILED(hres))
1665 return hres;
1667 hres = stack_pop_val(ctx, &l);
1668 if(SUCCEEDED(hres)) {
1669 hres = VarIdiv(l.v, r.v, &v);
1670 release_val(&l);
1672 release_val(&r);
1673 if(FAILED(hres))
1674 return hres;
1676 return stack_push(ctx, &v);
1679 static HRESULT interp_div(exec_ctx_t *ctx)
1681 variant_val_t r, l;
1682 VARIANT v;
1683 HRESULT hres;
1685 TRACE("\n");
1687 hres = stack_pop_val(ctx, &r);
1688 if(FAILED(hres))
1689 return hres;
1691 hres = stack_pop_val(ctx, &l);
1692 if(SUCCEEDED(hres)) {
1693 hres = VarDiv(l.v, r.v, &v);
1694 release_val(&l);
1696 release_val(&r);
1697 if(FAILED(hres))
1698 return hres;
1700 return stack_push(ctx, &v);
1703 static HRESULT interp_mul(exec_ctx_t *ctx)
1705 variant_val_t r, l;
1706 VARIANT v;
1707 HRESULT hres;
1709 TRACE("\n");
1711 hres = stack_pop_val(ctx, &r);
1712 if(FAILED(hres))
1713 return hres;
1715 hres = stack_pop_val(ctx, &l);
1716 if(SUCCEEDED(hres)) {
1717 hres = VarMul(l.v, r.v, &v);
1718 release_val(&l);
1720 release_val(&r);
1721 if(FAILED(hres))
1722 return hres;
1724 return stack_push(ctx, &v);
1727 static HRESULT interp_exp(exec_ctx_t *ctx)
1729 variant_val_t r, l;
1730 VARIANT v;
1731 HRESULT hres;
1733 TRACE("\n");
1735 hres = stack_pop_val(ctx, &r);
1736 if(FAILED(hres))
1737 return hres;
1739 hres = stack_pop_val(ctx, &l);
1740 if(SUCCEEDED(hres)) {
1741 hres = VarPow(l.v, r.v, &v);
1742 release_val(&l);
1744 release_val(&r);
1745 if(FAILED(hres))
1746 return hres;
1748 return stack_push(ctx, &v);
1751 static HRESULT interp_neg(exec_ctx_t *ctx)
1753 variant_val_t val;
1754 VARIANT v;
1755 HRESULT hres;
1757 hres = stack_pop_val(ctx, &val);
1758 if(FAILED(hres))
1759 return hres;
1761 hres = VarNeg(val.v, &v);
1762 release_val(&val);
1763 if(FAILED(hres))
1764 return hres;
1766 return stack_push(ctx, &v);
1769 static HRESULT interp_incc(exec_ctx_t *ctx)
1771 const BSTR ident = ctx->instr->arg1.bstr;
1772 VARIANT v;
1773 ref_t ref;
1774 HRESULT hres;
1776 TRACE("\n");
1778 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1779 if(FAILED(hres))
1780 return hres;
1782 if(ref.type != REF_VAR) {
1783 FIXME("ref.type is not REF_VAR\n");
1784 return E_FAIL;
1787 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1788 if(FAILED(hres))
1789 return hres;
1791 VariantClear(ref.u.v);
1792 *ref.u.v = v;
1793 return S_OK;
1796 static const instr_func_t op_funcs[] = {
1797 #define X(x,n,a,b) interp_ ## x,
1798 OP_LIST
1799 #undef X
1802 static const unsigned op_move[] = {
1803 #define X(x,n,a,b) n,
1804 OP_LIST
1805 #undef X
1808 void release_dynamic_vars(dynamic_var_t *var)
1810 while(var) {
1811 VariantClear(&var->v);
1812 var = var->next;
1816 static void release_exec(exec_ctx_t *ctx)
1818 unsigned i;
1820 VariantClear(&ctx->ret_val);
1821 release_dynamic_vars(ctx->dynamic_vars);
1823 if(ctx->this_obj)
1824 IDispatch_Release(ctx->this_obj);
1826 if(ctx->args) {
1827 for(i=0; i < ctx->func->arg_cnt; i++)
1828 VariantClear(ctx->args+i);
1831 if(ctx->vars) {
1832 for(i=0; i < ctx->func->var_cnt; i++)
1833 VariantClear(ctx->vars+i);
1836 heap_pool_free(&ctx->heap);
1837 heap_free(ctx->args);
1838 heap_free(ctx->vars);
1839 heap_free(ctx->stack);
1842 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1844 exec_ctx_t exec = {func->code_ctx};
1845 vbsop_t op;
1846 HRESULT hres = S_OK;
1848 exec.code = func->code_ctx;
1850 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1851 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1852 return E_FAIL;
1855 heap_pool_init(&exec.heap);
1857 if(func->arg_cnt) {
1858 VARIANT *v;
1859 unsigned i;
1861 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1862 if(!exec.args) {
1863 release_exec(&exec);
1864 return E_OUTOFMEMORY;
1867 for(i=0; i < func->arg_cnt; i++) {
1868 v = get_arg(dp, i);
1869 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1870 if(func->args[i].by_ref)
1871 exec.args[i] = *v;
1872 else
1873 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1874 }else {
1875 hres = VariantCopy(exec.args+i, v);
1877 if(FAILED(hres)) {
1878 release_exec(&exec);
1879 return hres;
1882 }else {
1883 exec.args = NULL;
1886 if(func->var_cnt) {
1887 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1888 if(!exec.vars) {
1889 release_exec(&exec);
1890 return E_OUTOFMEMORY;
1892 }else {
1893 exec.vars = NULL;
1896 exec.stack_size = 16;
1897 exec.top = 0;
1898 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1899 if(!exec.stack) {
1900 release_exec(&exec);
1901 return E_OUTOFMEMORY;
1904 if(this_obj)
1905 exec.this_obj = this_obj;
1906 else if (ctx->host_global)
1907 exec.this_obj = ctx->host_global;
1908 else
1909 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1910 IDispatch_AddRef(exec.this_obj);
1912 exec.instr = exec.code->instrs + func->code_off;
1913 exec.script = ctx;
1914 exec.func = func;
1916 while(exec.instr) {
1917 op = exec.instr->op;
1918 hres = op_funcs[op](&exec);
1919 if(FAILED(hres)) {
1920 if(exec.resume_next)
1921 FIXME("Failed %08x in resume next mode\n", hres);
1922 else
1923 WARN("Failed %08x\n", hres);
1924 stack_popn(&exec, exec.top);
1925 break;
1928 exec.instr += op_move[op];
1931 assert(!exec.top);
1932 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1933 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1935 if(SUCCEEDED(hres) && res) {
1936 *res = exec.ret_val;
1937 V_VT(&exec.ret_val) = VT_EMPTY;
1940 release_exec(&exec);
1941 return hres;