msvcp90: Return last index in string::find_last_not_of_cstr_substr if input is empty.
[wine.git] / dlls / vbscript / interp.c
blob07b2a92c7f4f1212eed305099e5002c392426670
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\v", 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 ref_t ref;
988 HRESULT hres;
990 TRACE("%s\n", debugstr_w(ident));
992 assert(array_id < ctx->func->array_cnt);
993 if(!ctx->arrays) {
994 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
995 if(!ctx->arrays)
996 return E_OUTOFMEMORY;
999 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1000 if(FAILED(hres)) {
1001 FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
1002 return hres;
1005 if(ref.type != REF_VAR) {
1006 FIXME("got ref.type = %d\n", ref.type);
1007 return E_FAIL;
1010 if(!ctx->arrays[array_id]) {
1011 const array_desc_t *array_desc;
1013 array_desc = ctx->func->array_descs + array_id;
1014 if(array_desc->dim_cnt) {
1015 ctx->arrays[array_id] = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1016 if(!ctx->arrays[array_id])
1017 return E_OUTOFMEMORY;
1021 V_VT(ref.u.v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1022 V_ARRAYREF(ref.u.v) = ctx->arrays+array_id;
1023 return S_OK;
1026 static HRESULT interp_step(exec_ctx_t *ctx)
1028 const BSTR ident = ctx->instr->arg2.bstr;
1029 BOOL gteq_zero;
1030 VARIANT zero;
1031 ref_t ref;
1032 HRESULT hres;
1034 TRACE("%s\n", debugstr_w(ident));
1036 V_VT(&zero) = VT_I2;
1037 V_I2(&zero) = 0;
1038 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1039 if(FAILED(hres))
1040 return hres;
1042 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1044 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1045 if(FAILED(hres))
1046 return hres;
1048 if(ref.type != REF_VAR) {
1049 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1050 return E_FAIL;
1053 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1054 if(FAILED(hres))
1055 return hres;
1057 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1058 ctx->instr++;
1059 }else {
1060 stack_popn(ctx, 2);
1061 instr_jmp(ctx, ctx->instr->arg1.uint);
1063 return S_OK;
1066 static HRESULT interp_newenum(exec_ctx_t *ctx)
1068 VARIANT *v, r;
1069 HRESULT hres;
1071 TRACE("\n");
1073 v = stack_pop(ctx);
1074 switch(V_VT(v)) {
1075 case VT_DISPATCH: {
1076 IEnumVARIANT *iter;
1077 DISPPARAMS dp = {0};
1078 VARIANT iterv;
1080 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_NEWENUM, &dp, &iterv);
1081 VariantClear(v);
1082 if(FAILED(hres))
1083 return hres;
1085 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1086 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1087 VariantClear(&iterv);
1088 return hres;
1091 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1092 IUnknown_Release(V_UNKNOWN(&iterv));
1093 if(FAILED(hres)) {
1094 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
1095 return hres;
1098 V_VT(&r) = VT_UNKNOWN;
1099 V_UNKNOWN(&r) = (IUnknown*)iter;
1100 break;
1102 default:
1103 FIXME("Unsupported for %s\n", debugstr_variant(v));
1104 VariantClear(v);
1105 return E_NOTIMPL;
1108 return stack_push(ctx, &r);
1111 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1113 const unsigned loop_end = ctx->instr->arg1.uint;
1114 const BSTR ident = ctx->instr->arg2.bstr;
1115 VARIANT v;
1116 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1117 IEnumVARIANT *iter;
1118 BOOL do_continue;
1119 HRESULT hres;
1121 TRACE("\n");
1123 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1124 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1126 V_VT(&v) = VT_EMPTY;
1127 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1128 if(FAILED(hres))
1129 return hres;
1131 do_continue = hres == S_OK;
1132 hres = assign_ident(ctx, ident, &dp);
1133 VariantClear(&v);
1134 if(FAILED(hres))
1135 return hres;
1137 if(do_continue) {
1138 ctx->instr++;
1139 }else {
1140 stack_pop(ctx);
1141 instr_jmp(ctx, loop_end);
1143 return S_OK;
1146 static HRESULT interp_jmp(exec_ctx_t *ctx)
1148 const unsigned arg = ctx->instr->arg1.uint;
1150 TRACE("%u\n", arg);
1152 instr_jmp(ctx, arg);
1153 return S_OK;
1156 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1158 const unsigned arg = ctx->instr->arg1.uint;
1159 HRESULT hres;
1160 BOOL b;
1162 TRACE("%u\n", arg);
1164 hres = stack_pop_bool(ctx, &b);
1165 if(FAILED(hres))
1166 return hres;
1168 if(b)
1169 ctx->instr++;
1170 else
1171 instr_jmp(ctx, ctx->instr->arg1.uint);
1172 return S_OK;
1175 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1177 const unsigned arg = ctx->instr->arg1.uint;
1178 HRESULT hres;
1179 BOOL b;
1181 TRACE("%u\n", arg);
1183 hres = stack_pop_bool(ctx, &b);
1184 if(FAILED(hres))
1185 return hres;
1187 if(b)
1188 instr_jmp(ctx, ctx->instr->arg1.uint);
1189 else
1190 ctx->instr++;
1191 return S_OK;
1194 static HRESULT interp_ret(exec_ctx_t *ctx)
1196 TRACE("\n");
1198 ctx->instr = NULL;
1199 return S_OK;
1202 static HRESULT interp_stop(exec_ctx_t *ctx)
1204 WARN("\n");
1206 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1207 return S_OK;
1210 static HRESULT interp_me(exec_ctx_t *ctx)
1212 VARIANT v;
1214 TRACE("\n");
1216 IDispatch_AddRef(ctx->this_obj);
1217 V_VT(&v) = VT_DISPATCH;
1218 V_DISPATCH(&v) = ctx->this_obj;
1219 return stack_push(ctx, &v);
1222 static HRESULT interp_bool(exec_ctx_t *ctx)
1224 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1225 VARIANT v;
1227 TRACE("%s\n", arg ? "true" : "false");
1229 V_VT(&v) = VT_BOOL;
1230 V_BOOL(&v) = arg;
1231 return stack_push(ctx, &v);
1234 static HRESULT interp_errmode(exec_ctx_t *ctx)
1236 const int err_mode = ctx->instr->arg1.uint;
1238 TRACE("%d\n", err_mode);
1240 ctx->resume_next = err_mode;
1241 return S_OK;
1244 static HRESULT interp_string(exec_ctx_t *ctx)
1246 VARIANT v;
1248 TRACE("\n");
1250 V_VT(&v) = VT_BSTR;
1251 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1252 if(!V_BSTR(&v))
1253 return E_OUTOFMEMORY;
1255 return stack_push(ctx, &v);
1258 static HRESULT interp_long(exec_ctx_t *ctx)
1260 const LONG arg = ctx->instr->arg1.lng;
1261 VARIANT v;
1263 TRACE("%d\n", arg);
1265 V_VT(&v) = VT_I4;
1266 V_I4(&v) = arg;
1267 return stack_push(ctx, &v);
1270 static HRESULT interp_short(exec_ctx_t *ctx)
1272 const LONG arg = ctx->instr->arg1.lng;
1273 VARIANT v;
1275 TRACE("%d\n", arg);
1277 V_VT(&v) = VT_I2;
1278 V_I2(&v) = arg;
1279 return stack_push(ctx, &v);
1282 static HRESULT interp_double(exec_ctx_t *ctx)
1284 const DOUBLE *arg = ctx->instr->arg1.dbl;
1285 VARIANT v;
1287 TRACE("%lf\n", *arg);
1289 V_VT(&v) = VT_R8;
1290 V_R8(&v) = *arg;
1291 return stack_push(ctx, &v);
1294 static HRESULT interp_empty(exec_ctx_t *ctx)
1296 VARIANT v;
1298 TRACE("\n");
1300 V_VT(&v) = VT_EMPTY;
1301 return stack_push(ctx, &v);
1304 static HRESULT interp_null(exec_ctx_t *ctx)
1306 TRACE("\n");
1307 return stack_push_null(ctx);
1310 static HRESULT interp_nothing(exec_ctx_t *ctx)
1312 VARIANT v;
1314 TRACE("\n");
1316 V_VT(&v) = VT_DISPATCH;
1317 V_DISPATCH(&v) = NULL;
1318 return stack_push(ctx, &v);
1321 static HRESULT interp_not(exec_ctx_t *ctx)
1323 variant_val_t val;
1324 VARIANT v;
1325 HRESULT hres;
1327 TRACE("\n");
1329 hres = stack_pop_val(ctx, &val);
1330 if(FAILED(hres))
1331 return hres;
1333 hres = VarNot(val.v, &v);
1334 release_val(&val);
1335 if(FAILED(hres))
1336 return hres;
1338 return stack_push(ctx, &v);
1341 static HRESULT interp_and(exec_ctx_t *ctx)
1343 variant_val_t r, l;
1344 VARIANT v;
1345 HRESULT hres;
1347 TRACE("\n");
1349 hres = stack_pop_val(ctx, &r);
1350 if(FAILED(hres))
1351 return hres;
1353 hres = stack_pop_val(ctx, &l);
1354 if(SUCCEEDED(hres)) {
1355 hres = VarAnd(l.v, r.v, &v);
1356 release_val(&l);
1358 release_val(&r);
1359 if(FAILED(hres))
1360 return hres;
1362 return stack_push(ctx, &v);
1365 static HRESULT interp_or(exec_ctx_t *ctx)
1367 variant_val_t r, l;
1368 VARIANT v;
1369 HRESULT hres;
1371 TRACE("\n");
1373 hres = stack_pop_val(ctx, &r);
1374 if(FAILED(hres))
1375 return hres;
1377 hres = stack_pop_val(ctx, &l);
1378 if(SUCCEEDED(hres)) {
1379 hres = VarOr(l.v, r.v, &v);
1380 release_val(&l);
1382 release_val(&r);
1383 if(FAILED(hres))
1384 return hres;
1386 return stack_push(ctx, &v);
1389 static HRESULT interp_xor(exec_ctx_t *ctx)
1391 variant_val_t r, l;
1392 VARIANT v;
1393 HRESULT hres;
1395 TRACE("\n");
1397 hres = stack_pop_val(ctx, &r);
1398 if(FAILED(hres))
1399 return hres;
1401 hres = stack_pop_val(ctx, &l);
1402 if(SUCCEEDED(hres)) {
1403 hres = VarXor(l.v, r.v, &v);
1404 release_val(&l);
1406 release_val(&r);
1407 if(FAILED(hres))
1408 return hres;
1410 return stack_push(ctx, &v);
1413 static HRESULT interp_eqv(exec_ctx_t *ctx)
1415 variant_val_t r, l;
1416 VARIANT v;
1417 HRESULT hres;
1419 TRACE("\n");
1421 hres = stack_pop_val(ctx, &r);
1422 if(FAILED(hres))
1423 return hres;
1425 hres = stack_pop_val(ctx, &l);
1426 if(SUCCEEDED(hres)) {
1427 hres = VarEqv(l.v, r.v, &v);
1428 release_val(&l);
1430 release_val(&r);
1431 if(FAILED(hres))
1432 return hres;
1434 return stack_push(ctx, &v);
1437 static HRESULT interp_imp(exec_ctx_t *ctx)
1439 variant_val_t r, l;
1440 VARIANT v;
1441 HRESULT hres;
1443 TRACE("\n");
1445 hres = stack_pop_val(ctx, &r);
1446 if(FAILED(hres))
1447 return hres;
1449 hres = stack_pop_val(ctx, &l);
1450 if(SUCCEEDED(hres)) {
1451 hres = VarImp(l.v, r.v, &v);
1452 release_val(&l);
1454 release_val(&r);
1455 if(FAILED(hres))
1456 return hres;
1458 return stack_push(ctx, &v);
1461 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1463 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1465 /* FIXME: Fix comparing string to number */
1467 return VarCmp(l, r, ctx->script->lcid, 0);
1470 static HRESULT cmp_oper(exec_ctx_t *ctx)
1472 variant_val_t l, r;
1473 HRESULT hres;
1475 hres = stack_pop_val(ctx, &r);
1476 if(FAILED(hres))
1477 return hres;
1479 hres = stack_pop_val(ctx, &l);
1480 if(SUCCEEDED(hres)) {
1481 hres = var_cmp(ctx, l.v, r.v);
1482 release_val(&l);
1485 release_val(&r);
1486 return hres;
1489 static HRESULT interp_equal(exec_ctx_t *ctx)
1491 VARIANT v;
1492 HRESULT hres;
1494 TRACE("\n");
1496 hres = cmp_oper(ctx);
1497 if(FAILED(hres))
1498 return hres;
1499 if(hres == VARCMP_NULL)
1500 return stack_push_null(ctx);
1502 V_VT(&v) = VT_BOOL;
1503 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1504 return stack_push(ctx, &v);
1507 static HRESULT interp_nequal(exec_ctx_t *ctx)
1509 VARIANT v;
1510 HRESULT hres;
1512 TRACE("\n");
1514 hres = cmp_oper(ctx);
1515 if(FAILED(hres))
1516 return hres;
1517 if(hres == VARCMP_NULL)
1518 return stack_push_null(ctx);
1520 V_VT(&v) = VT_BOOL;
1521 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1522 return stack_push(ctx, &v);
1525 static HRESULT interp_gt(exec_ctx_t *ctx)
1527 VARIANT v;
1528 HRESULT hres;
1530 TRACE("\n");
1532 hres = cmp_oper(ctx);
1533 if(FAILED(hres))
1534 return hres;
1535 if(hres == VARCMP_NULL)
1536 return stack_push_null(ctx);
1538 V_VT(&v) = VT_BOOL;
1539 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1540 return stack_push(ctx, &v);
1543 static HRESULT interp_gteq(exec_ctx_t *ctx)
1545 VARIANT v;
1546 HRESULT hres;
1548 TRACE("\n");
1550 hres = cmp_oper(ctx);
1551 if(FAILED(hres))
1552 return hres;
1553 if(hres == VARCMP_NULL)
1554 return stack_push_null(ctx);
1556 V_VT(&v) = VT_BOOL;
1557 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1558 return stack_push(ctx, &v);
1561 static HRESULT interp_lt(exec_ctx_t *ctx)
1563 VARIANT v;
1564 HRESULT hres;
1566 TRACE("\n");
1568 hres = cmp_oper(ctx);
1569 if(FAILED(hres))
1570 return hres;
1571 if(hres == VARCMP_NULL)
1572 return stack_push_null(ctx);
1574 V_VT(&v) = VT_BOOL;
1575 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1576 return stack_push(ctx, &v);
1579 static HRESULT interp_lteq(exec_ctx_t *ctx)
1581 VARIANT v;
1582 HRESULT hres;
1584 TRACE("\n");
1586 hres = cmp_oper(ctx);
1587 if(FAILED(hres))
1588 return hres;
1589 if(hres == VARCMP_NULL)
1590 return stack_push_null(ctx);
1592 V_VT(&v) = VT_BOOL;
1593 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1594 return stack_push(ctx, &v);
1597 static HRESULT interp_case(exec_ctx_t *ctx)
1599 const unsigned arg = ctx->instr->arg1.uint;
1600 variant_val_t v;
1601 HRESULT hres;
1603 TRACE("%d\n", arg);
1605 hres = stack_pop_val(ctx, &v);
1606 if(FAILED(hres))
1607 return hres;
1609 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1610 release_val(&v);
1611 if(FAILED(hres))
1612 return hres;
1614 if(hres == VARCMP_EQ) {
1615 stack_popn(ctx, 1);
1616 instr_jmp(ctx, arg);
1617 }else {
1618 ctx->instr++;
1621 return S_OK;
1624 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1626 IObjectIdentity *identity;
1627 IUnknown *unk1, *unk2;
1628 HRESULT hres;
1630 if(disp1 == disp2) {
1631 *ret = VARIANT_TRUE;
1632 return S_OK;
1635 if(!disp1 || !disp2) {
1636 *ret = VARIANT_FALSE;
1637 return S_OK;
1640 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1641 if(FAILED(hres))
1642 return hres;
1644 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1645 if(FAILED(hres)) {
1646 IUnknown_Release(unk1);
1647 return hres;
1650 if(unk1 == unk2) {
1651 *ret = VARIANT_TRUE;
1652 }else {
1653 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1654 if(SUCCEEDED(hres)) {
1655 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1656 IObjectIdentity_Release(identity);
1657 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1658 }else {
1659 *ret = VARIANT_FALSE;
1663 IUnknown_Release(unk1);
1664 IUnknown_Release(unk2);
1665 return S_OK;
1668 static HRESULT interp_is(exec_ctx_t *ctx)
1670 IDispatch *l, *r;
1671 VARIANT v;
1672 HRESULT hres;
1674 TRACE("\n");
1676 hres = stack_pop_disp(ctx, &r);
1677 if(FAILED(hres))
1678 return hres;
1680 hres = stack_pop_disp(ctx, &l);
1681 if(SUCCEEDED(hres)) {
1682 V_VT(&v) = VT_BOOL;
1683 hres = disp_cmp(l, r, &V_BOOL(&v));
1684 if(l)
1685 IDispatch_Release(l);
1687 if(r)
1688 IDispatch_Release(r);
1689 if(FAILED(hres))
1690 return hres;
1692 return stack_push(ctx, &v);
1695 static HRESULT interp_concat(exec_ctx_t *ctx)
1697 variant_val_t r, l;
1698 VARIANT v;
1699 HRESULT hres;
1701 TRACE("\n");
1703 hres = stack_pop_val(ctx, &r);
1704 if(FAILED(hres))
1705 return hres;
1707 hres = stack_pop_val(ctx, &l);
1708 if(SUCCEEDED(hres)) {
1709 hres = VarCat(l.v, r.v, &v);
1710 release_val(&l);
1712 release_val(&r);
1713 if(FAILED(hres))
1714 return hres;
1716 return stack_push(ctx, &v);
1719 static HRESULT interp_add(exec_ctx_t *ctx)
1721 variant_val_t r, l;
1722 VARIANT v;
1723 HRESULT hres;
1725 TRACE("\n");
1727 hres = stack_pop_val(ctx, &r);
1728 if(FAILED(hres))
1729 return hres;
1731 hres = stack_pop_val(ctx, &l);
1732 if(SUCCEEDED(hres)) {
1733 hres = VarAdd(l.v, r.v, &v);
1734 release_val(&l);
1736 release_val(&r);
1737 if(FAILED(hres))
1738 return hres;
1740 return stack_push(ctx, &v);
1743 static HRESULT interp_sub(exec_ctx_t *ctx)
1745 variant_val_t r, l;
1746 VARIANT v;
1747 HRESULT hres;
1749 TRACE("\n");
1751 hres = stack_pop_val(ctx, &r);
1752 if(FAILED(hres))
1753 return hres;
1755 hres = stack_pop_val(ctx, &l);
1756 if(SUCCEEDED(hres)) {
1757 hres = VarSub(l.v, r.v, &v);
1758 release_val(&l);
1760 release_val(&r);
1761 if(FAILED(hres))
1762 return hres;
1764 return stack_push(ctx, &v);
1767 static HRESULT interp_mod(exec_ctx_t *ctx)
1769 variant_val_t r, l;
1770 VARIANT v;
1771 HRESULT hres;
1773 TRACE("\n");
1775 hres = stack_pop_val(ctx, &r);
1776 if(FAILED(hres))
1777 return hres;
1779 hres = stack_pop_val(ctx, &l);
1780 if(SUCCEEDED(hres)) {
1781 hres = VarMod(l.v, r.v, &v);
1782 release_val(&l);
1784 release_val(&r);
1785 if(FAILED(hres))
1786 return hres;
1788 return stack_push(ctx, &v);
1791 static HRESULT interp_idiv(exec_ctx_t *ctx)
1793 variant_val_t r, l;
1794 VARIANT v;
1795 HRESULT hres;
1797 TRACE("\n");
1799 hres = stack_pop_val(ctx, &r);
1800 if(FAILED(hres))
1801 return hres;
1803 hres = stack_pop_val(ctx, &l);
1804 if(SUCCEEDED(hres)) {
1805 hres = VarIdiv(l.v, r.v, &v);
1806 release_val(&l);
1808 release_val(&r);
1809 if(FAILED(hres))
1810 return hres;
1812 return stack_push(ctx, &v);
1815 static HRESULT interp_div(exec_ctx_t *ctx)
1817 variant_val_t r, l;
1818 VARIANT v;
1819 HRESULT hres;
1821 TRACE("\n");
1823 hres = stack_pop_val(ctx, &r);
1824 if(FAILED(hres))
1825 return hres;
1827 hres = stack_pop_val(ctx, &l);
1828 if(SUCCEEDED(hres)) {
1829 hres = VarDiv(l.v, r.v, &v);
1830 release_val(&l);
1832 release_val(&r);
1833 if(FAILED(hres))
1834 return hres;
1836 return stack_push(ctx, &v);
1839 static HRESULT interp_mul(exec_ctx_t *ctx)
1841 variant_val_t r, l;
1842 VARIANT v;
1843 HRESULT hres;
1845 TRACE("\n");
1847 hres = stack_pop_val(ctx, &r);
1848 if(FAILED(hres))
1849 return hres;
1851 hres = stack_pop_val(ctx, &l);
1852 if(SUCCEEDED(hres)) {
1853 hres = VarMul(l.v, r.v, &v);
1854 release_val(&l);
1856 release_val(&r);
1857 if(FAILED(hres))
1858 return hres;
1860 return stack_push(ctx, &v);
1863 static HRESULT interp_exp(exec_ctx_t *ctx)
1865 variant_val_t r, l;
1866 VARIANT v;
1867 HRESULT hres;
1869 TRACE("\n");
1871 hres = stack_pop_val(ctx, &r);
1872 if(FAILED(hres))
1873 return hres;
1875 hres = stack_pop_val(ctx, &l);
1876 if(SUCCEEDED(hres)) {
1877 hres = VarPow(l.v, r.v, &v);
1878 release_val(&l);
1880 release_val(&r);
1881 if(FAILED(hres))
1882 return hres;
1884 return stack_push(ctx, &v);
1887 static HRESULT interp_neg(exec_ctx_t *ctx)
1889 variant_val_t val;
1890 VARIANT v;
1891 HRESULT hres;
1893 hres = stack_pop_val(ctx, &val);
1894 if(FAILED(hres))
1895 return hres;
1897 hres = VarNeg(val.v, &v);
1898 release_val(&val);
1899 if(FAILED(hres))
1900 return hres;
1902 return stack_push(ctx, &v);
1905 static HRESULT interp_incc(exec_ctx_t *ctx)
1907 const BSTR ident = ctx->instr->arg1.bstr;
1908 VARIANT v;
1909 ref_t ref;
1910 HRESULT hres;
1912 TRACE("\n");
1914 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1915 if(FAILED(hres))
1916 return hres;
1918 if(ref.type != REF_VAR) {
1919 FIXME("ref.type is not REF_VAR\n");
1920 return E_FAIL;
1923 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1924 if(FAILED(hres))
1925 return hres;
1927 VariantClear(ref.u.v);
1928 *ref.u.v = v;
1929 return S_OK;
1932 static const instr_func_t op_funcs[] = {
1933 #define X(x,n,a,b) interp_ ## x,
1934 OP_LIST
1935 #undef X
1938 static const unsigned op_move[] = {
1939 #define X(x,n,a,b) n,
1940 OP_LIST
1941 #undef X
1944 void release_dynamic_vars(dynamic_var_t *var)
1946 while(var) {
1947 VariantClear(&var->v);
1948 var = var->next;
1952 static void release_exec(exec_ctx_t *ctx)
1954 unsigned i;
1956 VariantClear(&ctx->ret_val);
1957 release_dynamic_vars(ctx->dynamic_vars);
1959 if(ctx->this_obj)
1960 IDispatch_Release(ctx->this_obj);
1962 if(ctx->args) {
1963 for(i=0; i < ctx->func->arg_cnt; i++)
1964 VariantClear(ctx->args+i);
1967 if(ctx->vars) {
1968 for(i=0; i < ctx->func->var_cnt; i++)
1969 VariantClear(ctx->vars+i);
1972 if(ctx->arrays) {
1973 for(i=0; i < ctx->func->var_cnt; i++) {
1974 if(ctx->arrays[i])
1975 SafeArrayDestroy(ctx->arrays[i]);
1977 heap_free(ctx->arrays);
1980 heap_pool_free(&ctx->heap);
1981 heap_free(ctx->args);
1982 heap_free(ctx->vars);
1983 heap_free(ctx->stack);
1986 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1988 exec_ctx_t exec = {func->code_ctx};
1989 vbsop_t op;
1990 HRESULT hres = S_OK;
1992 exec.code = func->code_ctx;
1994 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1995 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1996 return E_FAIL;
1999 heap_pool_init(&exec.heap);
2001 if(func->arg_cnt) {
2002 VARIANT *v;
2003 unsigned i;
2005 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2006 if(!exec.args) {
2007 release_exec(&exec);
2008 return E_OUTOFMEMORY;
2011 for(i=0; i < func->arg_cnt; i++) {
2012 v = get_arg(dp, i);
2013 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2014 if(func->args[i].by_ref)
2015 exec.args[i] = *v;
2016 else
2017 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
2018 }else {
2019 hres = VariantCopy(exec.args+i, v);
2021 if(FAILED(hres)) {
2022 release_exec(&exec);
2023 return hres;
2026 }else {
2027 exec.args = NULL;
2030 if(func->var_cnt) {
2031 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2032 if(!exec.vars) {
2033 release_exec(&exec);
2034 return E_OUTOFMEMORY;
2036 }else {
2037 exec.vars = NULL;
2040 exec.stack_size = 16;
2041 exec.top = 0;
2042 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2043 if(!exec.stack) {
2044 release_exec(&exec);
2045 return E_OUTOFMEMORY;
2048 if(this_obj)
2049 exec.this_obj = this_obj;
2050 else if (ctx->host_global)
2051 exec.this_obj = ctx->host_global;
2052 else
2053 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2054 IDispatch_AddRef(exec.this_obj);
2056 exec.instr = exec.code->instrs + func->code_off;
2057 exec.script = ctx;
2058 exec.func = func;
2060 while(exec.instr) {
2061 op = exec.instr->op;
2062 hres = op_funcs[op](&exec);
2063 if(FAILED(hres)) {
2064 if(exec.resume_next)
2065 FIXME("Failed %08x in resume next mode\n", hres);
2066 else
2067 WARN("Failed %08x\n", hres);
2068 stack_popn(&exec, exec.top);
2069 break;
2072 exec.instr += op_move[op];
2075 assert(!exec.top);
2076 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2077 assert(V_VT(&exec.ret_val) == VT_EMPTY);
2079 if(SUCCEEDED(hres) && res) {
2080 *res = exec.ret_val;
2081 V_VT(&exec.ret_val) = VT_EMPTY;
2084 release_exec(&exec);
2085 return hres;