gdiplus: Mark the Graphics object as busy before freeing it.
[wine/multimedia.git] / dlls / vbscript / interp.c
blob63654619bedaaa4f1b5734be8f8d32ae627b6944
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;
38 SAFEARRAY **arrays;
40 dynamic_var_t *dynamic_vars;
41 heap_pool_t heap;
43 BOOL resume_next;
45 unsigned stack_size;
46 unsigned top;
47 VARIANT *stack;
49 VARIANT ret_val;
50 } exec_ctx_t;
52 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
54 typedef enum {
55 REF_NONE,
56 REF_DISP,
57 REF_VAR,
58 REF_OBJ,
59 REF_CONST,
60 REF_FUNC
61 } ref_type_t;
63 typedef struct {
64 ref_type_t type;
65 union {
66 struct {
67 IDispatch *disp;
68 DISPID id;
69 } d;
70 VARIANT *v;
71 function_t *f;
72 IDispatch *obj;
73 } u;
74 } ref_t;
76 typedef struct {
77 VARIANT *v;
78 VARIANT store;
79 BOOL owned;
80 } variant_val_t;
82 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
84 while(var) {
85 if(!strcmpiW(var->name, name)) {
86 ref->type = var->is_const ? REF_CONST : REF_VAR;
87 ref->u.v = &var->v;
88 return TRUE;
91 var = var->next;
94 return FALSE;
97 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
99 named_item_t *item;
100 function_t *func;
101 unsigned i;
102 DISPID id;
103 HRESULT hres;
105 static const WCHAR errW[] = {'e','r','r',0};
107 if(invoke_type == VBDISP_LET
108 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
109 && !strcmpiW(name, ctx->func->name)) {
110 ref->type = REF_VAR;
111 ref->u.v = &ctx->ret_val;
112 return S_OK;
115 for(i=0; i < ctx->func->var_cnt; i++) {
116 if(!strcmpiW(ctx->func->vars[i].name, name)) {
117 ref->type = REF_VAR;
118 ref->u.v = ctx->vars+i;
119 return TRUE;
123 for(i=0; i < ctx->func->arg_cnt; i++) {
124 if(!strcmpiW(ctx->func->args[i].name, name)) {
125 ref->type = REF_VAR;
126 ref->u.v = ctx->args+i;
127 return S_OK;
131 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
132 return S_OK;
134 if(ctx->func->type != FUNC_GLOBAL) {
135 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
136 if(SUCCEEDED(hres)) {
137 ref->type = REF_DISP;
138 ref->u.d.disp = ctx->this_obj;
139 ref->u.d.id = id;
140 return S_OK;
144 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
145 return S_OK;
147 for(func = ctx->script->global_funcs; func; func = func->next) {
148 if(!strcmpiW(func->name, name)) {
149 ref->type = REF_FUNC;
150 ref->u.f = func;
151 return S_OK;
155 if(!strcmpiW(name, errW)) {
156 ref->type = REF_OBJ;
157 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
158 return S_OK;
161 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
162 if(SUCCEEDED(hres)) {
163 ref->type = REF_DISP;
164 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
165 ref->u.d.id = id;
166 return S_OK;
169 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
170 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
171 if(!item->disp) {
172 IUnknown *unk;
174 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
175 if(FAILED(hres)) {
176 WARN("GetItemInfo failed: %08x\n", hres);
177 continue;
180 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
181 IUnknown_Release(unk);
182 if(FAILED(hres)) {
183 WARN("object does not implement IDispatch\n");
184 continue;
188 ref->type = REF_OBJ;
189 ref->u.obj = item->disp;
190 return S_OK;
194 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
195 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
196 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
197 if(SUCCEEDED(hres)) {
198 ref->type = REF_DISP;
199 ref->u.d.disp = item->disp;
200 ref->u.d.id = id;
201 return S_OK;
206 ref->type = REF_NONE;
207 return S_OK;
210 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
211 BOOL is_const, VARIANT *val, BOOL own_val, VARIANT **out_var)
213 dynamic_var_t *new_var;
214 heap_pool_t *heap;
215 WCHAR *str;
216 unsigned size;
217 HRESULT hres;
219 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
221 new_var = heap_pool_alloc(heap, sizeof(*new_var));
222 if(!new_var)
223 return E_OUTOFMEMORY;
225 size = (strlenW(name)+1)*sizeof(WCHAR);
226 str = heap_pool_alloc(heap, size);
227 if(!str)
228 return E_OUTOFMEMORY;
229 memcpy(str, name, size);
230 new_var->name = str;
231 new_var->is_const = is_const;
233 if(own_val) {
234 new_var->v = *val;
235 }else {
236 V_VT(&new_var->v) = VT_EMPTY;
237 hres = VariantCopy(&new_var->v, val);
238 if(FAILED(hres))
239 return hres;
242 if(ctx->func->type == FUNC_GLOBAL) {
243 new_var->next = ctx->script->global_vars;
244 ctx->script->global_vars = new_var;
245 }else {
246 new_var->next = ctx->dynamic_vars;
247 ctx->dynamic_vars = new_var;
250 if(out_var)
251 *out_var = &new_var->v;
253 return S_OK;
256 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
258 assert(ctx->top);
259 return ctx->stack + --ctx->top;
262 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
264 assert(ctx->top >= n);
265 return ctx->stack + (ctx->top-n-1);
268 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
270 if(ctx->stack_size == ctx->top) {
271 VARIANT *new_stack;
273 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
274 if(!new_stack) {
275 VariantClear(v);
276 return E_OUTOFMEMORY;
279 ctx->stack = new_stack;
280 ctx->stack_size *= 2;
283 ctx->stack[ctx->top++] = *v;
284 return S_OK;
287 static inline HRESULT stack_push_null(exec_ctx_t *ctx)
289 VARIANT v;
290 V_VT(&v) = VT_NULL;
291 return stack_push(ctx, &v);
294 static void stack_popn(exec_ctx_t *ctx, unsigned n)
296 while(n--)
297 VariantClear(stack_pop(ctx));
300 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
302 VARIANT *var;
304 var = stack_pop(ctx);
306 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
307 v->owned = FALSE;
308 var = V_VARIANTREF(var);
309 }else {
310 v->owned = TRUE;
313 if(V_VT(var) == VT_DISPATCH) {
314 DISPPARAMS dp = {0};
315 HRESULT hres;
317 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
318 if(v->owned)
319 IDispatch_Release(V_DISPATCH(var));
320 if(FAILED(hres))
321 return hres;
323 v->owned = TRUE;
324 v->v = &v->store;
325 }else {
326 v->v = var;
329 return S_OK;
332 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
334 VARIANT *v = stack_top(ctx, n);
335 HRESULT hres;
337 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
338 VARIANT *ref = V_VARIANTREF(v);
340 V_VT(v) = VT_EMPTY;
341 hres = VariantCopy(v, ref);
342 if(FAILED(hres))
343 return hres;
346 if(V_VT(v) == VT_DISPATCH) {
347 DISPPARAMS dp = {0};
348 IDispatch *disp;
350 disp = V_DISPATCH(v);
351 V_VT(v) = VT_EMPTY;
352 hres = disp_call(ctx->script, disp, DISPID_VALUE, &dp, v);
353 IDispatch_Release(disp);
354 if(FAILED(hres))
355 return hres;
358 return S_OK;
361 static inline void release_val(variant_val_t *v)
363 if(v->owned)
364 VariantClear(v->v);
367 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
369 variant_val_t val;
370 HRESULT hres;
372 hres = stack_pop_val(ctx, &val);
373 if(FAILED(hres))
374 return hres;
376 switch (V_VT(val.v))
378 case VT_BOOL:
379 *b = V_BOOL(val.v);
380 break;
381 case VT_NULL:
382 *b = FALSE;
383 break;
384 case VT_I2:
385 *b = V_I2(val.v);
386 break;
387 case VT_I4:
388 *b = V_I4(val.v);
389 break;
390 default:
391 FIXME("unsupported for %s\n", debugstr_variant(val.v));
392 release_val(&val);
393 return E_NOTIMPL;
395 return S_OK;
398 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
400 VARIANT *v = stack_pop(ctx);
402 if(V_VT(v) == VT_DISPATCH) {
403 *ret = V_DISPATCH(v);
404 return S_OK;
407 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
408 FIXME("not supported type: %s\n", debugstr_variant(v));
409 VariantClear(v);
410 return E_FAIL;
413 v = V_BYREF(v);
414 if(V_VT(v) != VT_DISPATCH) {
415 FIXME("not disp %s\n", debugstr_variant(v));
416 return E_FAIL;
419 if(V_DISPATCH(v))
420 IDispatch_AddRef(V_DISPATCH(v));
421 *ret = V_DISPATCH(v);
422 return S_OK;
425 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
427 VARIANT *v = stack_top(ctx, n), *ref;
429 if(V_VT(v) != VT_DISPATCH) {
430 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
431 FIXME("not supported type: %s\n", debugstr_variant(v));
432 return E_FAIL;
435 ref = V_VARIANTREF(v);
436 if(V_VT(ref) != VT_DISPATCH) {
437 FIXME("not disp %s\n", debugstr_variant(ref));
438 return E_FAIL;
441 V_VT(v) = VT_DISPATCH;
442 V_DISPATCH(v) = V_DISPATCH(ref);
443 if(V_DISPATCH(v))
444 IDispatch_AddRef(V_DISPATCH(v));
447 if(disp)
448 *disp = V_DISPATCH(v);
449 return S_OK;
452 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
454 ctx->instr = ctx->code->instrs + addr;
457 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
459 dp->cNamedArgs = is_propput ? 1 : 0;
460 dp->cArgs = arg_cnt + dp->cNamedArgs;
461 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
463 if(arg_cnt) {
464 VARIANT tmp;
465 unsigned i;
467 assert(ctx->top >= arg_cnt);
469 for(i=1; i*2 <= arg_cnt; i++) {
470 tmp = ctx->stack[ctx->top-i];
471 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
472 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
475 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
476 }else {
477 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
481 static HRESULT array_access(exec_ctx_t *ctx, SAFEARRAY *array, DISPPARAMS *dp, VARIANT **ret)
483 unsigned cell_off = 0, dim_size = 1, i;
484 unsigned argc = arg_cnt(dp);
485 VARIANT *data;
486 LONG idx;
487 HRESULT hres;
489 if(!array) {
490 FIXME("NULL array\n");
491 return E_FAIL;
494 if(array->cDims != argc) {
495 FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
496 return E_FAIL;
499 for(i=0; i < argc; i++) {
500 hres = to_int(get_arg(dp, i), &idx);
501 if(FAILED(hres))
502 return hres;
504 idx -= array->rgsabound[i].lLbound;
505 if(idx >= array->rgsabound[i].cElements) {
506 FIXME("out of bound element %d in dim %d of size %d\n", idx, i+1, array->rgsabound[i].cElements);
507 return E_FAIL;
510 cell_off += idx*dim_size;
511 dim_size *= array->rgsabound[i].cElements;
514 hres = SafeArrayAccessData(array, (void**)&data);
515 if(FAILED(hres))
516 return hres;
518 *ret = data+cell_off;
520 SafeArrayUnaccessData(array);
521 return S_OK;
524 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
526 BSTR identifier = ctx->instr->arg1.bstr;
527 const unsigned arg_cnt = ctx->instr->arg2.uint;
528 DISPPARAMS dp;
529 ref_t ref;
530 HRESULT hres;
532 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
533 if(FAILED(hres))
534 return hres;
536 switch(ref.type) {
537 case REF_VAR:
538 case REF_CONST: {
539 VARIANT *v;
541 if(!res) {
542 FIXME("REF_VAR no res\n");
543 return E_NOTIMPL;
546 v = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
548 if(arg_cnt) {
549 SAFEARRAY *array;
551 switch(V_VT(v)) {
552 case VT_ARRAY|VT_BYREF|VT_VARIANT:
553 array = *V_ARRAYREF(ref.u.v);
554 break;
555 case VT_ARRAY|VT_VARIANT:
556 array = V_ARRAY(ref.u.v);
557 break;
558 default:
559 FIXME("arguments not implemented\n");
560 return E_NOTIMPL;
563 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
564 hres = array_access(ctx, array, &dp, &v);
565 if(FAILED(hres))
566 return hres;
569 V_VT(res) = VT_BYREF|VT_VARIANT;
570 V_BYREF(res) = v;
571 break;
573 case REF_DISP:
574 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
575 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
576 if(FAILED(hres))
577 return hres;
578 break;
579 case REF_FUNC:
580 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
581 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
582 if(FAILED(hres))
583 return hres;
584 break;
585 case REF_OBJ:
586 if(arg_cnt) {
587 FIXME("arguments on object\n");
588 return E_NOTIMPL;
591 if(res) {
592 IDispatch_AddRef(ref.u.obj);
593 V_VT(res) = VT_DISPATCH;
594 V_DISPATCH(res) = ref.u.obj;
596 break;
597 case REF_NONE:
598 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
599 VARIANT v, *new;
600 VariantInit(&v);
601 hres = add_dynamic_var(ctx, identifier, FALSE, &v, FALSE, &new);
602 if(FAILED(hres))
603 return hres;
604 V_VT(res) = VT_BYREF|VT_VARIANT;
605 V_BYREF(res) = new;
606 break;
608 FIXME("%s not found\n", debugstr_w(identifier));
609 return DISP_E_UNKNOWNNAME;
612 stack_popn(ctx, arg_cnt);
613 return S_OK;
616 static HRESULT interp_icall(exec_ctx_t *ctx)
618 VARIANT v;
619 HRESULT hres;
621 TRACE("\n");
623 hres = do_icall(ctx, &v);
624 if(FAILED(hres))
625 return hres;
627 return stack_push(ctx, &v);
630 static HRESULT interp_icallv(exec_ctx_t *ctx)
632 TRACE("\n");
633 return do_icall(ctx, NULL);
636 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
638 const BSTR identifier = ctx->instr->arg1.bstr;
639 const unsigned arg_cnt = ctx->instr->arg2.uint;
640 IDispatch *obj;
641 DISPPARAMS dp;
642 DISPID id;
643 HRESULT hres;
645 hres = stack_pop_disp(ctx, &obj);
646 if(FAILED(hres))
647 return hres;
649 if(!obj) {
650 FIXME("NULL obj\n");
651 return E_FAIL;
654 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
656 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
657 if(SUCCEEDED(hres))
658 hres = disp_call(ctx->script, obj, id, &dp, res);
659 IDispatch_Release(obj);
660 if(FAILED(hres))
661 return hres;
663 stack_popn(ctx, arg_cnt);
664 return S_OK;
667 static HRESULT interp_mcall(exec_ctx_t *ctx)
669 VARIANT res;
670 HRESULT hres;
672 TRACE("\n");
674 hres = do_mcall(ctx, &res);
675 if(FAILED(hres))
676 return hres;
678 return stack_push(ctx, &res);
681 static HRESULT interp_mcallv(exec_ctx_t *ctx)
683 TRACE("\n");
685 return do_mcall(ctx, NULL);
688 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
690 ref_t ref;
691 HRESULT hres;
693 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
694 if(FAILED(hres))
695 return hres;
697 switch(ref.type) {
698 case REF_VAR: {
699 VARIANT *v = ref.u.v;
701 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
702 v = V_VARIANTREF(v);
704 if(arg_cnt(dp)) {
705 SAFEARRAY *array;
707 if(!(V_VT(v) & VT_ARRAY)) {
708 FIXME("array assign on type %d\n", V_VT(v));
709 return E_FAIL;
712 switch(V_VT(v)) {
713 case VT_ARRAY|VT_BYREF|VT_VARIANT:
714 array = *V_ARRAYREF(v);
715 break;
716 case VT_ARRAY|VT_VARIANT:
717 array = V_ARRAY(v);
718 break;
719 default:
720 FIXME("Unsupported array type %x\n", V_VT(v));
721 return E_NOTIMPL;
724 if(!array) {
725 FIXME("null array\n");
726 return E_FAIL;
729 hres = array_access(ctx, array, dp, &v);
730 if(FAILED(hres))
731 return hres;
732 }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
733 FIXME("non-array assign\n");
734 return E_NOTIMPL;
737 hres = VariantCopyInd(v, dp->rgvarg);
738 break;
740 case REF_DISP:
741 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
742 break;
743 case REF_FUNC:
744 FIXME("functions not implemented\n");
745 return E_NOTIMPL;
746 case REF_OBJ:
747 FIXME("REF_OBJ\n");
748 return E_NOTIMPL;
749 case REF_CONST:
750 FIXME("REF_CONST\n");
751 return E_NOTIMPL;
752 case REF_NONE:
753 if(ctx->func->code_ctx->option_explicit) {
754 FIXME("throw exception\n");
755 hres = E_FAIL;
756 }else {
757 if(arg_cnt(dp)) {
758 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
759 return E_NOTIMPL;
762 TRACE("creating variable %s\n", debugstr_w(name));
763 hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE, NULL);
767 return hres;
770 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
772 const BSTR arg = ctx->instr->arg1.bstr;
773 const unsigned arg_cnt = ctx->instr->arg2.uint;
774 DISPPARAMS dp;
775 HRESULT hres;
777 TRACE("%s\n", debugstr_w(arg));
779 hres = stack_assume_val(ctx, arg_cnt);
780 if(FAILED(hres))
781 return hres;
783 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
784 hres = assign_ident(ctx, arg, &dp);
785 if(FAILED(hres))
786 return hres;
788 stack_popn(ctx, arg_cnt+1);
789 return S_OK;
792 static HRESULT interp_set_ident(exec_ctx_t *ctx)
794 const BSTR arg = ctx->instr->arg1.bstr;
795 const unsigned arg_cnt = ctx->instr->arg2.uint;
796 DISPPARAMS dp;
797 HRESULT hres;
799 TRACE("%s\n", debugstr_w(arg));
801 if(arg_cnt) {
802 FIXME("arguments not supported\n");
803 return E_NOTIMPL;
806 hres = stack_assume_disp(ctx, 0, NULL);
807 if(FAILED(hres))
808 return hres;
810 vbstack_to_dp(ctx, 0, TRUE, &dp);
811 hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
812 if(FAILED(hres))
813 return hres;
815 stack_popn(ctx, 1);
816 return S_OK;
819 static HRESULT interp_assign_member(exec_ctx_t *ctx)
821 BSTR identifier = ctx->instr->arg1.bstr;
822 const unsigned arg_cnt = ctx->instr->arg2.uint;
823 IDispatch *obj;
824 DISPPARAMS dp;
825 DISPID id;
826 HRESULT hres;
828 TRACE("%s\n", debugstr_w(identifier));
830 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
831 if(FAILED(hres))
832 return hres;
834 if(!obj) {
835 FIXME("NULL obj\n");
836 return E_FAIL;
839 hres = stack_assume_val(ctx, arg_cnt);
840 if(FAILED(hres))
841 return hres;
843 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
844 if(SUCCEEDED(hres)) {
845 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
846 hres = disp_propput(ctx->script, obj, id, &dp);
848 if(FAILED(hres))
849 return hres;
851 stack_popn(ctx, arg_cnt+2);
852 return S_OK;
855 static HRESULT interp_set_member(exec_ctx_t *ctx)
857 BSTR identifier = ctx->instr->arg1.bstr;
858 const unsigned arg_cnt = ctx->instr->arg2.uint;
859 IDispatch *obj;
860 DISPPARAMS dp;
861 DISPID id;
862 HRESULT hres;
864 TRACE("%s\n", debugstr_w(identifier));
866 if(arg_cnt) {
867 FIXME("arguments not supported\n");
868 return E_NOTIMPL;
871 hres = stack_assume_disp(ctx, 1, &obj);
872 if(FAILED(hres))
873 return hres;
875 if(!obj) {
876 FIXME("NULL obj\n");
877 return E_FAIL;
880 hres = stack_assume_disp(ctx, 0, NULL);
881 if(FAILED(hres))
882 return hres;
884 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
885 if(SUCCEEDED(hres)) {
886 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
887 hres = disp_propput(ctx->script, obj, id, &dp);
889 if(FAILED(hres))
890 return hres;
892 stack_popn(ctx, 2);
893 return S_OK;
896 static HRESULT interp_const(exec_ctx_t *ctx)
898 BSTR arg = ctx->instr->arg1.bstr;
899 variant_val_t val;
900 ref_t ref;
901 HRESULT hres;
903 TRACE("%s\n", debugstr_w(arg));
905 assert(ctx->func->type == FUNC_GLOBAL);
907 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
908 if(FAILED(hres))
909 return hres;
911 if(ref.type != REF_NONE) {
912 FIXME("%s already defined\n", debugstr_w(arg));
913 return E_FAIL;
916 hres = stack_pop_val(ctx, &val);
917 if(FAILED(hres))
918 return hres;
920 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned, NULL);
923 static HRESULT interp_val(exec_ctx_t *ctx)
925 variant_val_t val;
926 VARIANT v;
927 HRESULT hres;
929 TRACE("\n");
931 hres = stack_pop_val(ctx, &val);
932 if(FAILED(hres))
933 return hres;
935 if(!val.owned) {
936 V_VT(&v) = VT_EMPTY;
937 hres = VariantCopy(&v, val.v);
938 if(FAILED(hres))
939 return hres;
942 return stack_push(ctx, val.owned ? val.v : &v);
945 static HRESULT interp_pop(exec_ctx_t *ctx)
947 const unsigned n = ctx->instr->arg1.uint;
949 TRACE("%u\n", n);
951 stack_popn(ctx, n);
952 return S_OK;
955 static HRESULT interp_new(exec_ctx_t *ctx)
957 const WCHAR *arg = ctx->instr->arg1.bstr;
958 class_desc_t *class_desc;
959 vbdisp_t *obj;
960 VARIANT v;
961 HRESULT hres;
963 TRACE("%s\n", debugstr_w(arg));
965 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
966 if(!strcmpiW(class_desc->name, arg))
967 break;
969 if(!class_desc) {
970 FIXME("Class %s not found\n", debugstr_w(arg));
971 return E_FAIL;
974 hres = create_vbdisp(class_desc, &obj);
975 if(FAILED(hres))
976 return hres;
978 V_VT(&v) = VT_DISPATCH;
979 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
980 return stack_push(ctx, &v);
983 static HRESULT interp_dim(exec_ctx_t *ctx)
985 const BSTR ident = ctx->instr->arg1.bstr;
986 const unsigned array_id = ctx->instr->arg2.uint;
987 const array_desc_t *array_desc;
988 ref_t ref;
989 HRESULT hres;
991 TRACE("%s\n", debugstr_w(ident));
993 assert(array_id < ctx->func->array_cnt);
994 if(!ctx->arrays) {
995 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
996 if(!ctx->arrays)
997 return E_OUTOFMEMORY;
1000 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1001 if(FAILED(hres)) {
1002 FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
1003 return hres;
1006 if(ref.type != REF_VAR) {
1007 FIXME("got ref.type = %d\n", ref.type);
1008 return E_FAIL;
1011 if(ctx->arrays[array_id]) {
1012 FIXME("Array already initialized\n");
1013 return E_FAIL;
1016 array_desc = ctx->func->array_descs + array_id;
1017 if(array_desc->dim_cnt) {
1018 ctx->arrays[array_id] = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1019 if(!ctx->arrays[array_id])
1020 return E_OUTOFMEMORY;
1023 V_VT(ref.u.v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1024 V_ARRAYREF(ref.u.v) = ctx->arrays+array_id;
1025 return S_OK;
1028 static HRESULT interp_step(exec_ctx_t *ctx)
1030 const BSTR ident = ctx->instr->arg2.bstr;
1031 BOOL gteq_zero;
1032 VARIANT zero;
1033 ref_t ref;
1034 HRESULT hres;
1036 TRACE("%s\n", debugstr_w(ident));
1038 V_VT(&zero) = VT_I2;
1039 V_I2(&zero) = 0;
1040 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1041 if(FAILED(hres))
1042 return hres;
1044 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1046 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1047 if(FAILED(hres))
1048 return hres;
1050 if(ref.type != REF_VAR) {
1051 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1052 return E_FAIL;
1055 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1056 if(FAILED(hres))
1057 return hres;
1059 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1060 ctx->instr++;
1061 }else {
1062 stack_popn(ctx, 2);
1063 instr_jmp(ctx, ctx->instr->arg1.uint);
1065 return S_OK;
1068 static HRESULT interp_newenum(exec_ctx_t *ctx)
1070 VARIANT *v, r;
1071 HRESULT hres;
1073 TRACE("\n");
1075 v = stack_pop(ctx);
1076 switch(V_VT(v)) {
1077 case VT_DISPATCH: {
1078 IEnumVARIANT *iter;
1079 DISPPARAMS dp = {0};
1080 VARIANT iterv;
1082 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_NEWENUM, &dp, &iterv);
1083 VariantClear(v);
1084 if(FAILED(hres))
1085 return hres;
1087 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1088 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1089 VariantClear(&iterv);
1090 return hres;
1093 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1094 IUnknown_Release(V_UNKNOWN(&iterv));
1095 if(FAILED(hres)) {
1096 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
1097 return hres;
1100 V_VT(&r) = VT_UNKNOWN;
1101 V_UNKNOWN(&r) = (IUnknown*)iter;
1102 break;
1104 default:
1105 FIXME("Unsupported for %s\n", debugstr_variant(v));
1106 VariantClear(v);
1107 return E_NOTIMPL;
1110 return stack_push(ctx, &r);
1113 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1115 const unsigned loop_end = ctx->instr->arg1.uint;
1116 const BSTR ident = ctx->instr->arg2.bstr;
1117 VARIANT v;
1118 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1119 IEnumVARIANT *iter;
1120 BOOL do_continue;
1121 HRESULT hres;
1123 TRACE("\n");
1125 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1126 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1128 V_VT(&v) = VT_EMPTY;
1129 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1130 if(FAILED(hres))
1131 return hres;
1133 do_continue = hres == S_OK;
1134 hres = assign_ident(ctx, ident, &dp);
1135 VariantClear(&v);
1136 if(FAILED(hres))
1137 return hres;
1139 if(do_continue) {
1140 ctx->instr++;
1141 }else {
1142 stack_pop(ctx);
1143 instr_jmp(ctx, loop_end);
1145 return S_OK;
1148 static HRESULT interp_jmp(exec_ctx_t *ctx)
1150 const unsigned arg = ctx->instr->arg1.uint;
1152 TRACE("%u\n", arg);
1154 instr_jmp(ctx, arg);
1155 return S_OK;
1158 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1160 const unsigned arg = ctx->instr->arg1.uint;
1161 HRESULT hres;
1162 BOOL b;
1164 TRACE("%u\n", arg);
1166 hres = stack_pop_bool(ctx, &b);
1167 if(FAILED(hres))
1168 return hres;
1170 if(b)
1171 ctx->instr++;
1172 else
1173 instr_jmp(ctx, ctx->instr->arg1.uint);
1174 return S_OK;
1177 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1179 const unsigned arg = ctx->instr->arg1.uint;
1180 HRESULT hres;
1181 BOOL b;
1183 TRACE("%u\n", arg);
1185 hres = stack_pop_bool(ctx, &b);
1186 if(FAILED(hres))
1187 return hres;
1189 if(b)
1190 instr_jmp(ctx, ctx->instr->arg1.uint);
1191 else
1192 ctx->instr++;
1193 return S_OK;
1196 static HRESULT interp_ret(exec_ctx_t *ctx)
1198 TRACE("\n");
1200 ctx->instr = NULL;
1201 return S_OK;
1204 static HRESULT interp_stop(exec_ctx_t *ctx)
1206 WARN("\n");
1208 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1209 return S_OK;
1212 static HRESULT interp_me(exec_ctx_t *ctx)
1214 VARIANT v;
1216 TRACE("\n");
1218 IDispatch_AddRef(ctx->this_obj);
1219 V_VT(&v) = VT_DISPATCH;
1220 V_DISPATCH(&v) = ctx->this_obj;
1221 return stack_push(ctx, &v);
1224 static HRESULT interp_bool(exec_ctx_t *ctx)
1226 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1227 VARIANT v;
1229 TRACE("%s\n", arg ? "true" : "false");
1231 V_VT(&v) = VT_BOOL;
1232 V_BOOL(&v) = arg;
1233 return stack_push(ctx, &v);
1236 static HRESULT interp_errmode(exec_ctx_t *ctx)
1238 const int err_mode = ctx->instr->arg1.uint;
1240 TRACE("%d\n", err_mode);
1242 ctx->resume_next = err_mode;
1243 return S_OK;
1246 static HRESULT interp_string(exec_ctx_t *ctx)
1248 VARIANT v;
1250 TRACE("\n");
1252 V_VT(&v) = VT_BSTR;
1253 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1254 if(!V_BSTR(&v))
1255 return E_OUTOFMEMORY;
1257 return stack_push(ctx, &v);
1260 static HRESULT interp_long(exec_ctx_t *ctx)
1262 const LONG arg = ctx->instr->arg1.lng;
1263 VARIANT v;
1265 TRACE("%d\n", arg);
1267 V_VT(&v) = VT_I4;
1268 V_I4(&v) = arg;
1269 return stack_push(ctx, &v);
1272 static HRESULT interp_short(exec_ctx_t *ctx)
1274 const LONG arg = ctx->instr->arg1.lng;
1275 VARIANT v;
1277 TRACE("%d\n", arg);
1279 V_VT(&v) = VT_I2;
1280 V_I2(&v) = arg;
1281 return stack_push(ctx, &v);
1284 static HRESULT interp_double(exec_ctx_t *ctx)
1286 const DOUBLE *arg = ctx->instr->arg1.dbl;
1287 VARIANT v;
1289 TRACE("%lf\n", *arg);
1291 V_VT(&v) = VT_R8;
1292 V_R8(&v) = *arg;
1293 return stack_push(ctx, &v);
1296 static HRESULT interp_empty(exec_ctx_t *ctx)
1298 VARIANT v;
1300 TRACE("\n");
1302 V_VT(&v) = VT_EMPTY;
1303 return stack_push(ctx, &v);
1306 static HRESULT interp_null(exec_ctx_t *ctx)
1308 TRACE("\n");
1309 return stack_push_null(ctx);
1312 static HRESULT interp_nothing(exec_ctx_t *ctx)
1314 VARIANT v;
1316 TRACE("\n");
1318 V_VT(&v) = VT_DISPATCH;
1319 V_DISPATCH(&v) = NULL;
1320 return stack_push(ctx, &v);
1323 static HRESULT interp_not(exec_ctx_t *ctx)
1325 variant_val_t val;
1326 VARIANT v;
1327 HRESULT hres;
1329 TRACE("\n");
1331 hres = stack_pop_val(ctx, &val);
1332 if(FAILED(hres))
1333 return hres;
1335 hres = VarNot(val.v, &v);
1336 release_val(&val);
1337 if(FAILED(hres))
1338 return hres;
1340 return stack_push(ctx, &v);
1343 static HRESULT interp_and(exec_ctx_t *ctx)
1345 variant_val_t r, l;
1346 VARIANT v;
1347 HRESULT hres;
1349 TRACE("\n");
1351 hres = stack_pop_val(ctx, &r);
1352 if(FAILED(hres))
1353 return hres;
1355 hres = stack_pop_val(ctx, &l);
1356 if(SUCCEEDED(hres)) {
1357 hres = VarAnd(l.v, r.v, &v);
1358 release_val(&l);
1360 release_val(&r);
1361 if(FAILED(hres))
1362 return hres;
1364 return stack_push(ctx, &v);
1367 static HRESULT interp_or(exec_ctx_t *ctx)
1369 variant_val_t r, l;
1370 VARIANT v;
1371 HRESULT hres;
1373 TRACE("\n");
1375 hres = stack_pop_val(ctx, &r);
1376 if(FAILED(hres))
1377 return hres;
1379 hres = stack_pop_val(ctx, &l);
1380 if(SUCCEEDED(hres)) {
1381 hres = VarOr(l.v, r.v, &v);
1382 release_val(&l);
1384 release_val(&r);
1385 if(FAILED(hres))
1386 return hres;
1388 return stack_push(ctx, &v);
1391 static HRESULT interp_xor(exec_ctx_t *ctx)
1393 variant_val_t r, l;
1394 VARIANT v;
1395 HRESULT hres;
1397 TRACE("\n");
1399 hres = stack_pop_val(ctx, &r);
1400 if(FAILED(hres))
1401 return hres;
1403 hres = stack_pop_val(ctx, &l);
1404 if(SUCCEEDED(hres)) {
1405 hres = VarXor(l.v, r.v, &v);
1406 release_val(&l);
1408 release_val(&r);
1409 if(FAILED(hres))
1410 return hres;
1412 return stack_push(ctx, &v);
1415 static HRESULT interp_eqv(exec_ctx_t *ctx)
1417 variant_val_t r, l;
1418 VARIANT v;
1419 HRESULT hres;
1421 TRACE("\n");
1423 hres = stack_pop_val(ctx, &r);
1424 if(FAILED(hres))
1425 return hres;
1427 hres = stack_pop_val(ctx, &l);
1428 if(SUCCEEDED(hres)) {
1429 hres = VarEqv(l.v, r.v, &v);
1430 release_val(&l);
1432 release_val(&r);
1433 if(FAILED(hres))
1434 return hres;
1436 return stack_push(ctx, &v);
1439 static HRESULT interp_imp(exec_ctx_t *ctx)
1441 variant_val_t r, l;
1442 VARIANT v;
1443 HRESULT hres;
1445 TRACE("\n");
1447 hres = stack_pop_val(ctx, &r);
1448 if(FAILED(hres))
1449 return hres;
1451 hres = stack_pop_val(ctx, &l);
1452 if(SUCCEEDED(hres)) {
1453 hres = VarImp(l.v, r.v, &v);
1454 release_val(&l);
1456 release_val(&r);
1457 if(FAILED(hres))
1458 return hres;
1460 return stack_push(ctx, &v);
1463 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1465 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1467 /* FIXME: Fix comparing string to number */
1469 return VarCmp(l, r, ctx->script->lcid, 0);
1472 static HRESULT cmp_oper(exec_ctx_t *ctx)
1474 variant_val_t l, r;
1475 HRESULT hres;
1477 hres = stack_pop_val(ctx, &r);
1478 if(FAILED(hres))
1479 return hres;
1481 hres = stack_pop_val(ctx, &l);
1482 if(SUCCEEDED(hres)) {
1483 hres = var_cmp(ctx, l.v, r.v);
1484 release_val(&l);
1487 release_val(&r);
1488 return hres;
1491 static HRESULT interp_equal(exec_ctx_t *ctx)
1493 VARIANT v;
1494 HRESULT hres;
1496 TRACE("\n");
1498 hres = cmp_oper(ctx);
1499 if(FAILED(hres))
1500 return hres;
1501 if(hres == VARCMP_NULL)
1502 return stack_push_null(ctx);
1504 V_VT(&v) = VT_BOOL;
1505 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1506 return stack_push(ctx, &v);
1509 static HRESULT interp_nequal(exec_ctx_t *ctx)
1511 VARIANT v;
1512 HRESULT hres;
1514 TRACE("\n");
1516 hres = cmp_oper(ctx);
1517 if(FAILED(hres))
1518 return hres;
1519 if(hres == VARCMP_NULL)
1520 return stack_push_null(ctx);
1522 V_VT(&v) = VT_BOOL;
1523 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1524 return stack_push(ctx, &v);
1527 static HRESULT interp_gt(exec_ctx_t *ctx)
1529 VARIANT v;
1530 HRESULT hres;
1532 TRACE("\n");
1534 hres = cmp_oper(ctx);
1535 if(FAILED(hres))
1536 return hres;
1537 if(hres == VARCMP_NULL)
1538 return stack_push_null(ctx);
1540 V_VT(&v) = VT_BOOL;
1541 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1542 return stack_push(ctx, &v);
1545 static HRESULT interp_gteq(exec_ctx_t *ctx)
1547 VARIANT v;
1548 HRESULT hres;
1550 TRACE("\n");
1552 hres = cmp_oper(ctx);
1553 if(FAILED(hres))
1554 return hres;
1555 if(hres == VARCMP_NULL)
1556 return stack_push_null(ctx);
1558 V_VT(&v) = VT_BOOL;
1559 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1560 return stack_push(ctx, &v);
1563 static HRESULT interp_lt(exec_ctx_t *ctx)
1565 VARIANT v;
1566 HRESULT hres;
1568 TRACE("\n");
1570 hres = cmp_oper(ctx);
1571 if(FAILED(hres))
1572 return hres;
1573 if(hres == VARCMP_NULL)
1574 return stack_push_null(ctx);
1576 V_VT(&v) = VT_BOOL;
1577 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1578 return stack_push(ctx, &v);
1581 static HRESULT interp_lteq(exec_ctx_t *ctx)
1583 VARIANT v;
1584 HRESULT hres;
1586 TRACE("\n");
1588 hres = cmp_oper(ctx);
1589 if(FAILED(hres))
1590 return hres;
1591 if(hres == VARCMP_NULL)
1592 return stack_push_null(ctx);
1594 V_VT(&v) = VT_BOOL;
1595 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1596 return stack_push(ctx, &v);
1599 static HRESULT interp_case(exec_ctx_t *ctx)
1601 const unsigned arg = ctx->instr->arg1.uint;
1602 variant_val_t v;
1603 HRESULT hres;
1605 TRACE("%d\n", arg);
1607 hres = stack_pop_val(ctx, &v);
1608 if(FAILED(hres))
1609 return hres;
1611 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1612 release_val(&v);
1613 if(FAILED(hres))
1614 return hres;
1616 if(hres == VARCMP_EQ) {
1617 stack_popn(ctx, 1);
1618 instr_jmp(ctx, arg);
1619 }else {
1620 ctx->instr++;
1623 return S_OK;
1626 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1628 IObjectIdentity *identity;
1629 IUnknown *unk1, *unk2;
1630 HRESULT hres;
1632 if(disp1 == disp2) {
1633 *ret = VARIANT_TRUE;
1634 return S_OK;
1637 if(!disp1 || !disp2) {
1638 *ret = VARIANT_FALSE;
1639 return S_OK;
1642 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1643 if(FAILED(hres))
1644 return hres;
1646 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1647 if(FAILED(hres)) {
1648 IUnknown_Release(unk1);
1649 return hres;
1652 if(unk1 == unk2) {
1653 *ret = VARIANT_TRUE;
1654 }else {
1655 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1656 if(SUCCEEDED(hres)) {
1657 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1658 IObjectIdentity_Release(identity);
1659 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1660 }else {
1661 *ret = VARIANT_FALSE;
1665 IUnknown_Release(unk1);
1666 IUnknown_Release(unk2);
1667 return S_OK;
1670 static HRESULT interp_is(exec_ctx_t *ctx)
1672 IDispatch *l, *r;
1673 VARIANT v;
1674 HRESULT hres;
1676 TRACE("\n");
1678 hres = stack_pop_disp(ctx, &r);
1679 if(FAILED(hres))
1680 return hres;
1682 hres = stack_pop_disp(ctx, &l);
1683 if(SUCCEEDED(hres)) {
1684 V_VT(&v) = VT_BOOL;
1685 hres = disp_cmp(l, r, &V_BOOL(&v));
1686 if(l)
1687 IDispatch_Release(l);
1689 if(r)
1690 IDispatch_Release(r);
1691 if(FAILED(hres))
1692 return hres;
1694 return stack_push(ctx, &v);
1697 static HRESULT interp_concat(exec_ctx_t *ctx)
1699 variant_val_t r, l;
1700 VARIANT v;
1701 HRESULT hres;
1703 TRACE("\n");
1705 hres = stack_pop_val(ctx, &r);
1706 if(FAILED(hres))
1707 return hres;
1709 hres = stack_pop_val(ctx, &l);
1710 if(SUCCEEDED(hres)) {
1711 hres = VarCat(l.v, r.v, &v);
1712 release_val(&l);
1714 release_val(&r);
1715 if(FAILED(hres))
1716 return hres;
1718 return stack_push(ctx, &v);
1721 static HRESULT interp_add(exec_ctx_t *ctx)
1723 variant_val_t r, l;
1724 VARIANT v;
1725 HRESULT hres;
1727 TRACE("\n");
1729 hres = stack_pop_val(ctx, &r);
1730 if(FAILED(hres))
1731 return hres;
1733 hres = stack_pop_val(ctx, &l);
1734 if(SUCCEEDED(hres)) {
1735 hres = VarAdd(l.v, r.v, &v);
1736 release_val(&l);
1738 release_val(&r);
1739 if(FAILED(hres))
1740 return hres;
1742 return stack_push(ctx, &v);
1745 static HRESULT interp_sub(exec_ctx_t *ctx)
1747 variant_val_t r, l;
1748 VARIANT v;
1749 HRESULT hres;
1751 TRACE("\n");
1753 hres = stack_pop_val(ctx, &r);
1754 if(FAILED(hres))
1755 return hres;
1757 hres = stack_pop_val(ctx, &l);
1758 if(SUCCEEDED(hres)) {
1759 hres = VarSub(l.v, r.v, &v);
1760 release_val(&l);
1762 release_val(&r);
1763 if(FAILED(hres))
1764 return hres;
1766 return stack_push(ctx, &v);
1769 static HRESULT interp_mod(exec_ctx_t *ctx)
1771 variant_val_t r, l;
1772 VARIANT v;
1773 HRESULT hres;
1775 TRACE("\n");
1777 hres = stack_pop_val(ctx, &r);
1778 if(FAILED(hres))
1779 return hres;
1781 hres = stack_pop_val(ctx, &l);
1782 if(SUCCEEDED(hres)) {
1783 hres = VarMod(l.v, r.v, &v);
1784 release_val(&l);
1786 release_val(&r);
1787 if(FAILED(hres))
1788 return hres;
1790 return stack_push(ctx, &v);
1793 static HRESULT interp_idiv(exec_ctx_t *ctx)
1795 variant_val_t r, l;
1796 VARIANT v;
1797 HRESULT hres;
1799 TRACE("\n");
1801 hres = stack_pop_val(ctx, &r);
1802 if(FAILED(hres))
1803 return hres;
1805 hres = stack_pop_val(ctx, &l);
1806 if(SUCCEEDED(hres)) {
1807 hres = VarIdiv(l.v, r.v, &v);
1808 release_val(&l);
1810 release_val(&r);
1811 if(FAILED(hres))
1812 return hres;
1814 return stack_push(ctx, &v);
1817 static HRESULT interp_div(exec_ctx_t *ctx)
1819 variant_val_t r, l;
1820 VARIANT v;
1821 HRESULT hres;
1823 TRACE("\n");
1825 hres = stack_pop_val(ctx, &r);
1826 if(FAILED(hres))
1827 return hres;
1829 hres = stack_pop_val(ctx, &l);
1830 if(SUCCEEDED(hres)) {
1831 hres = VarDiv(l.v, r.v, &v);
1832 release_val(&l);
1834 release_val(&r);
1835 if(FAILED(hres))
1836 return hres;
1838 return stack_push(ctx, &v);
1841 static HRESULT interp_mul(exec_ctx_t *ctx)
1843 variant_val_t r, l;
1844 VARIANT v;
1845 HRESULT hres;
1847 TRACE("\n");
1849 hres = stack_pop_val(ctx, &r);
1850 if(FAILED(hres))
1851 return hres;
1853 hres = stack_pop_val(ctx, &l);
1854 if(SUCCEEDED(hres)) {
1855 hres = VarMul(l.v, r.v, &v);
1856 release_val(&l);
1858 release_val(&r);
1859 if(FAILED(hres))
1860 return hres;
1862 return stack_push(ctx, &v);
1865 static HRESULT interp_exp(exec_ctx_t *ctx)
1867 variant_val_t r, l;
1868 VARIANT v;
1869 HRESULT hres;
1871 TRACE("\n");
1873 hres = stack_pop_val(ctx, &r);
1874 if(FAILED(hres))
1875 return hres;
1877 hres = stack_pop_val(ctx, &l);
1878 if(SUCCEEDED(hres)) {
1879 hres = VarPow(l.v, r.v, &v);
1880 release_val(&l);
1882 release_val(&r);
1883 if(FAILED(hres))
1884 return hres;
1886 return stack_push(ctx, &v);
1889 static HRESULT interp_neg(exec_ctx_t *ctx)
1891 variant_val_t val;
1892 VARIANT v;
1893 HRESULT hres;
1895 hres = stack_pop_val(ctx, &val);
1896 if(FAILED(hres))
1897 return hres;
1899 hres = VarNeg(val.v, &v);
1900 release_val(&val);
1901 if(FAILED(hres))
1902 return hres;
1904 return stack_push(ctx, &v);
1907 static HRESULT interp_incc(exec_ctx_t *ctx)
1909 const BSTR ident = ctx->instr->arg1.bstr;
1910 VARIANT v;
1911 ref_t ref;
1912 HRESULT hres;
1914 TRACE("\n");
1916 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1917 if(FAILED(hres))
1918 return hres;
1920 if(ref.type != REF_VAR) {
1921 FIXME("ref.type is not REF_VAR\n");
1922 return E_FAIL;
1925 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1926 if(FAILED(hres))
1927 return hres;
1929 VariantClear(ref.u.v);
1930 *ref.u.v = v;
1931 return S_OK;
1934 static const instr_func_t op_funcs[] = {
1935 #define X(x,n,a,b) interp_ ## x,
1936 OP_LIST
1937 #undef X
1940 static const unsigned op_move[] = {
1941 #define X(x,n,a,b) n,
1942 OP_LIST
1943 #undef X
1946 void release_dynamic_vars(dynamic_var_t *var)
1948 while(var) {
1949 VariantClear(&var->v);
1950 var = var->next;
1954 static void release_exec(exec_ctx_t *ctx)
1956 unsigned i;
1958 VariantClear(&ctx->ret_val);
1959 release_dynamic_vars(ctx->dynamic_vars);
1961 if(ctx->this_obj)
1962 IDispatch_Release(ctx->this_obj);
1964 if(ctx->args) {
1965 for(i=0; i < ctx->func->arg_cnt; i++)
1966 VariantClear(ctx->args+i);
1969 if(ctx->vars) {
1970 for(i=0; i < ctx->func->var_cnt; i++)
1971 VariantClear(ctx->vars+i);
1974 if(ctx->arrays) {
1975 for(i=0; i < ctx->func->var_cnt; i++) {
1976 if(ctx->arrays[i])
1977 SafeArrayDestroy(ctx->arrays[i]);
1979 heap_free(ctx->arrays);
1982 heap_pool_free(&ctx->heap);
1983 heap_free(ctx->args);
1984 heap_free(ctx->vars);
1985 heap_free(ctx->stack);
1988 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1990 exec_ctx_t exec = {func->code_ctx};
1991 vbsop_t op;
1992 HRESULT hres = S_OK;
1994 exec.code = func->code_ctx;
1996 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1997 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1998 return E_FAIL;
2001 heap_pool_init(&exec.heap);
2003 if(func->arg_cnt) {
2004 VARIANT *v;
2005 unsigned i;
2007 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2008 if(!exec.args) {
2009 release_exec(&exec);
2010 return E_OUTOFMEMORY;
2013 for(i=0; i < func->arg_cnt; i++) {
2014 v = get_arg(dp, i);
2015 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2016 if(func->args[i].by_ref)
2017 exec.args[i] = *v;
2018 else
2019 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
2020 }else {
2021 hres = VariantCopy(exec.args+i, v);
2023 if(FAILED(hres)) {
2024 release_exec(&exec);
2025 return hres;
2028 }else {
2029 exec.args = NULL;
2032 if(func->var_cnt) {
2033 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2034 if(!exec.vars) {
2035 release_exec(&exec);
2036 return E_OUTOFMEMORY;
2038 }else {
2039 exec.vars = NULL;
2042 exec.stack_size = 16;
2043 exec.top = 0;
2044 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2045 if(!exec.stack) {
2046 release_exec(&exec);
2047 return E_OUTOFMEMORY;
2050 if(this_obj)
2051 exec.this_obj = this_obj;
2052 else if (ctx->host_global)
2053 exec.this_obj = ctx->host_global;
2054 else
2055 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2056 IDispatch_AddRef(exec.this_obj);
2058 exec.instr = exec.code->instrs + func->code_off;
2059 exec.script = ctx;
2060 exec.func = func;
2062 while(exec.instr) {
2063 op = exec.instr->op;
2064 hres = op_funcs[op](&exec);
2065 if(FAILED(hres)) {
2066 if(exec.resume_next)
2067 FIXME("Failed %08x in resume next mode\n", hres);
2068 else
2069 WARN("Failed %08x\n", hres);
2070 stack_popn(&exec, exec.top);
2071 break;
2074 exec.instr += op_move[op];
2077 assert(!exec.top);
2078 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2079 assert(V_VT(&exec.ret_val) == VT_EMPTY);
2081 if(SUCCEEDED(hres) && res) {
2082 *res = exec.ret_val;
2083 V_VT(&exec.ret_val) = VT_EMPTY;
2086 release_exec(&exec);
2087 return hres;