ole2disp.dll16: Implement SafeArrayGet[UBound|LBound].
[wine.git] / dlls / vbscript / interp.c
blob9ec329924c5d9c0a30ca624db9c706ae279f778f
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;
35 vbdisp_t *vbthis;
37 VARIANT *args;
38 VARIANT *vars;
39 SAFEARRAY **arrays;
41 dynamic_var_t *dynamic_vars;
42 heap_pool_t heap;
44 BOOL resume_next;
46 unsigned stack_size;
47 unsigned top;
48 VARIANT *stack;
50 VARIANT ret_val;
51 } exec_ctx_t;
53 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
55 typedef enum {
56 REF_NONE,
57 REF_DISP,
58 REF_VAR,
59 REF_OBJ,
60 REF_CONST,
61 REF_FUNC
62 } ref_type_t;
64 typedef struct {
65 ref_type_t type;
66 union {
67 struct {
68 IDispatch *disp;
69 DISPID id;
70 } d;
71 VARIANT *v;
72 function_t *f;
73 IDispatch *obj;
74 } u;
75 } ref_t;
77 typedef struct {
78 VARIANT *v;
79 VARIANT store;
80 BOOL owned;
81 } variant_val_t;
83 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
85 while(var) {
86 if(!strcmpiW(var->name, name)) {
87 ref->type = var->is_const ? REF_CONST : REF_VAR;
88 ref->u.v = &var->v;
89 return TRUE;
92 var = var->next;
95 return FALSE;
98 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
100 named_item_t *item;
101 function_t *func;
102 unsigned i;
103 DISPID id;
104 HRESULT hres;
106 static const WCHAR errW[] = {'e','r','r',0};
108 if(invoke_type == VBDISP_LET
109 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
110 && !strcmpiW(name, ctx->func->name)) {
111 ref->type = REF_VAR;
112 ref->u.v = &ctx->ret_val;
113 return S_OK;
116 for(i=0; i < ctx->func->var_cnt; i++) {
117 if(!strcmpiW(ctx->func->vars[i].name, name)) {
118 ref->type = REF_VAR;
119 ref->u.v = ctx->vars+i;
120 return TRUE;
124 for(i=0; i < ctx->func->arg_cnt; i++) {
125 if(!strcmpiW(ctx->func->args[i].name, name)) {
126 ref->type = REF_VAR;
127 ref->u.v = ctx->args+i;
128 return S_OK;
132 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
133 return S_OK;
135 if(ctx->func->type != FUNC_GLOBAL) {
136 if(ctx->vbthis) {
137 /* FIXME: Bind such identifier while generating bytecode. */
138 for(i=0; i < ctx->vbthis->desc->prop_cnt; i++) {
139 if(!strcmpiW(ctx->vbthis->desc->props[i].name, name)) {
140 ref->type = REF_VAR;
141 ref->u.v = ctx->vbthis->props+i;
142 return S_OK;
147 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
148 if(SUCCEEDED(hres)) {
149 ref->type = REF_DISP;
150 ref->u.d.disp = ctx->this_obj;
151 ref->u.d.id = id;
152 return S_OK;
156 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
157 return S_OK;
159 for(func = ctx->script->global_funcs; func; func = func->next) {
160 if(!strcmpiW(func->name, name)) {
161 ref->type = REF_FUNC;
162 ref->u.f = func;
163 return S_OK;
167 if(!strcmpiW(name, errW)) {
168 ref->type = REF_OBJ;
169 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
170 return S_OK;
173 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
174 if(SUCCEEDED(hres)) {
175 ref->type = REF_DISP;
176 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
177 ref->u.d.id = id;
178 return S_OK;
181 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
182 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
183 if(!item->disp) {
184 IUnknown *unk;
186 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, item->name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
187 if(FAILED(hres)) {
188 WARN("GetItemInfo failed: %08x\n", hres);
189 continue;
192 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
193 IUnknown_Release(unk);
194 if(FAILED(hres)) {
195 WARN("object does not implement IDispatch\n");
196 continue;
200 ref->type = REF_OBJ;
201 ref->u.obj = item->disp;
202 return S_OK;
206 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
207 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
208 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
209 if(SUCCEEDED(hres)) {
210 ref->type = REF_DISP;
211 ref->u.d.disp = item->disp;
212 ref->u.d.id = id;
213 return S_OK;
218 ref->type = REF_NONE;
219 return S_OK;
222 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
223 BOOL is_const, VARIANT **out_var)
225 dynamic_var_t *new_var;
226 heap_pool_t *heap;
227 WCHAR *str;
228 unsigned size;
230 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
232 new_var = heap_pool_alloc(heap, sizeof(*new_var));
233 if(!new_var)
234 return E_OUTOFMEMORY;
236 size = (strlenW(name)+1)*sizeof(WCHAR);
237 str = heap_pool_alloc(heap, size);
238 if(!str)
239 return E_OUTOFMEMORY;
240 memcpy(str, name, size);
241 new_var->name = str;
242 new_var->is_const = is_const;
243 V_VT(&new_var->v) = VT_EMPTY;
245 if(ctx->func->type == FUNC_GLOBAL) {
246 new_var->next = ctx->script->global_vars;
247 ctx->script->global_vars = new_var;
248 }else {
249 new_var->next = ctx->dynamic_vars;
250 ctx->dynamic_vars = new_var;
253 *out_var = &new_var->v;
254 return S_OK;
257 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
259 assert(ctx->top);
260 return ctx->stack + --ctx->top;
263 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
265 assert(ctx->top >= n);
266 return ctx->stack + (ctx->top-n-1);
269 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
271 if(ctx->stack_size == ctx->top) {
272 VARIANT *new_stack;
274 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
275 if(!new_stack) {
276 VariantClear(v);
277 return E_OUTOFMEMORY;
280 ctx->stack = new_stack;
281 ctx->stack_size *= 2;
284 ctx->stack[ctx->top++] = *v;
285 return S_OK;
288 static inline HRESULT stack_push_null(exec_ctx_t *ctx)
290 VARIANT v;
291 V_VT(&v) = VT_NULL;
292 return stack_push(ctx, &v);
295 static void stack_popn(exec_ctx_t *ctx, unsigned n)
297 while(n--)
298 VariantClear(stack_pop(ctx));
301 static void stack_pop_deref(exec_ctx_t *ctx, variant_val_t *r)
303 VARIANT *v;
305 v = stack_pop(ctx);
306 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
307 r->owned = FALSE;
308 r->v = V_VARIANTREF(v);
309 }else {
310 r->owned = TRUE;
311 r->v = v;
315 static inline void release_val(variant_val_t *v)
317 if(v->owned)
318 VariantClear(v->v);
321 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *r)
323 stack_pop_deref(ctx, r);
325 if(V_VT(r->v) == VT_DISPATCH) {
326 HRESULT hres;
328 hres = get_disp_value(ctx->script, V_DISPATCH(r->v), &r->store);
329 if(r->owned)
330 IDispatch_Release(V_DISPATCH(r->v));
331 if(FAILED(hres))
332 return hres;
334 r->owned = TRUE;
335 r->v = &r->store;
338 return S_OK;
341 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
343 VARIANT *v = stack_top(ctx, n);
344 HRESULT hres;
346 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
347 VARIANT *ref = V_VARIANTREF(v);
349 V_VT(v) = VT_EMPTY;
350 hres = VariantCopy(v, ref);
351 if(FAILED(hres))
352 return hres;
355 if(V_VT(v) == VT_DISPATCH) {
356 IDispatch *disp;
358 disp = V_DISPATCH(v);
359 hres = get_disp_value(ctx->script, disp, v);
360 IDispatch_Release(disp);
361 if(FAILED(hres))
362 return hres;
365 return S_OK;
368 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
370 variant_val_t val;
371 HRESULT hres;
373 hres = stack_pop_val(ctx, &val);
374 if(FAILED(hres))
375 return hres;
377 switch (V_VT(val.v))
379 case VT_BOOL:
380 *b = V_BOOL(val.v);
381 break;
382 case VT_NULL:
383 *b = FALSE;
384 break;
385 case VT_I2:
386 *b = V_I2(val.v);
387 break;
388 case VT_I4:
389 *b = V_I4(val.v);
390 break;
391 default:
392 FIXME("unsupported for %s\n", debugstr_variant(val.v));
393 release_val(&val);
394 return E_NOTIMPL;
396 return S_OK;
399 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
401 VARIANT *v = stack_pop(ctx);
403 if(V_VT(v) == VT_DISPATCH) {
404 *ret = V_DISPATCH(v);
405 return S_OK;
408 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
409 FIXME("not supported type: %s\n", debugstr_variant(v));
410 VariantClear(v);
411 return E_FAIL;
414 v = V_BYREF(v);
415 if(V_VT(v) != VT_DISPATCH) {
416 FIXME("not disp %s\n", debugstr_variant(v));
417 return E_FAIL;
420 if(V_DISPATCH(v))
421 IDispatch_AddRef(V_DISPATCH(v));
422 *ret = V_DISPATCH(v);
423 return S_OK;
426 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
428 VARIANT *v = stack_top(ctx, n), *ref;
430 if(V_VT(v) != VT_DISPATCH) {
431 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
432 FIXME("not supported type: %s\n", debugstr_variant(v));
433 return E_FAIL;
436 ref = V_VARIANTREF(v);
437 if(V_VT(ref) != VT_DISPATCH) {
438 FIXME("not disp %s\n", debugstr_variant(ref));
439 return E_FAIL;
442 V_VT(v) = VT_DISPATCH;
443 V_DISPATCH(v) = V_DISPATCH(ref);
444 if(V_DISPATCH(v))
445 IDispatch_AddRef(V_DISPATCH(v));
448 if(disp)
449 *disp = V_DISPATCH(v);
450 return S_OK;
453 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
455 ctx->instr = ctx->code->instrs + addr;
458 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
460 dp->cNamedArgs = is_propput ? 1 : 0;
461 dp->cArgs = arg_cnt + dp->cNamedArgs;
462 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
464 if(arg_cnt) {
465 VARIANT tmp;
466 unsigned i;
468 assert(ctx->top >= arg_cnt);
470 for(i=1; i*2 <= arg_cnt; i++) {
471 tmp = ctx->stack[ctx->top-i];
472 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
473 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
476 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
477 }else {
478 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
482 static HRESULT array_access(exec_ctx_t *ctx, SAFEARRAY *array, DISPPARAMS *dp, VARIANT **ret)
484 unsigned i, argc = arg_cnt(dp);
485 LONG *indices;
486 HRESULT hres;
488 if(!array) {
489 FIXME("NULL array\n");
490 return E_FAIL;
493 hres = SafeArrayLock(array);
494 if(FAILED(hres))
495 return hres;
497 if(array->cDims != argc) {
498 FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
499 SafeArrayUnlock(array);
500 return E_FAIL;
503 indices = heap_alloc(sizeof(*indices) * argc);
504 if(!indices) {
505 SafeArrayUnlock(array);
506 return E_OUTOFMEMORY;
509 for(i=0; i<argc; i++) {
510 hres = to_int(get_arg(dp, i), indices+i);
511 if(FAILED(hres)) {
512 heap_free(indices);
513 SafeArrayUnlock(array);
514 return hres;
518 hres = SafeArrayPtrOfIndex(array, indices, (void**)ret);
519 SafeArrayUnlock(array);
520 heap_free(indices);
521 return hres;
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 = NULL;
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 case VT_DISPATCH:
559 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
560 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_VALUE, &dp, res);
561 if(FAILED(hres))
562 return hres;
563 break;
564 default:
565 FIXME("arguments not implemented\n");
566 return E_NOTIMPL;
569 if(!array)
570 break;
572 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
573 hres = array_access(ctx, array, &dp, &v);
574 if(FAILED(hres))
575 return hres;
578 V_VT(res) = VT_BYREF|VT_VARIANT;
579 V_BYREF(res) = v;
580 break;
582 case REF_DISP:
583 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
584 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
585 if(FAILED(hres))
586 return hres;
587 break;
588 case REF_FUNC:
589 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
590 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
591 if(FAILED(hres))
592 return hres;
593 break;
594 case REF_OBJ:
595 if(arg_cnt) {
596 FIXME("arguments on object\n");
597 return E_NOTIMPL;
600 if(res) {
601 IDispatch_AddRef(ref.u.obj);
602 V_VT(res) = VT_DISPATCH;
603 V_DISPATCH(res) = ref.u.obj;
605 break;
606 case REF_NONE:
607 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
608 VARIANT *new;
609 hres = add_dynamic_var(ctx, identifier, FALSE, &new);
610 if(FAILED(hres))
611 return hres;
612 V_VT(res) = VT_BYREF|VT_VARIANT;
613 V_BYREF(res) = new;
614 break;
616 FIXME("%s not found\n", debugstr_w(identifier));
617 return DISP_E_UNKNOWNNAME;
620 stack_popn(ctx, arg_cnt);
621 return S_OK;
624 static HRESULT interp_icall(exec_ctx_t *ctx)
626 VARIANT v;
627 HRESULT hres;
629 TRACE("\n");
631 hres = do_icall(ctx, &v);
632 if(FAILED(hres))
633 return hres;
635 return stack_push(ctx, &v);
638 static HRESULT interp_icallv(exec_ctx_t *ctx)
640 TRACE("\n");
641 return do_icall(ctx, NULL);
644 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
646 const BSTR identifier = ctx->instr->arg1.bstr;
647 const unsigned arg_cnt = ctx->instr->arg2.uint;
648 IDispatch *obj;
649 DISPPARAMS dp;
650 DISPID id;
651 HRESULT hres;
653 hres = stack_pop_disp(ctx, &obj);
654 if(FAILED(hres))
655 return hres;
657 if(!obj) {
658 FIXME("NULL obj\n");
659 return E_FAIL;
662 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
664 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
665 if(SUCCEEDED(hres))
666 hres = disp_call(ctx->script, obj, id, &dp, res);
667 IDispatch_Release(obj);
668 if(FAILED(hres))
669 return hres;
671 stack_popn(ctx, arg_cnt);
672 return S_OK;
675 static HRESULT interp_mcall(exec_ctx_t *ctx)
677 VARIANT res;
678 HRESULT hres;
680 TRACE("\n");
682 hres = do_mcall(ctx, &res);
683 if(FAILED(hres))
684 return hres;
686 return stack_push(ctx, &res);
689 static HRESULT interp_mcallv(exec_ctx_t *ctx)
691 TRACE("\n");
693 return do_mcall(ctx, NULL);
696 static HRESULT assign_value(exec_ctx_t *ctx, VARIANT *dst, VARIANT *src, WORD flags)
698 HRESULT hres;
700 hres = VariantCopyInd(dst, src);
701 if(FAILED(hres))
702 return hres;
704 if(V_VT(dst) == VT_DISPATCH && !(flags & DISPATCH_PROPERTYPUTREF)) {
705 VARIANT value;
707 hres = get_disp_value(ctx->script, V_DISPATCH(dst), &value);
708 IDispatch_Release(V_DISPATCH(dst));
709 if(FAILED(hres))
710 return hres;
712 *dst = value;
715 return S_OK;
718 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, WORD flags, DISPPARAMS *dp)
720 ref_t ref;
721 HRESULT hres;
723 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
724 if(FAILED(hres))
725 return hres;
727 switch(ref.type) {
728 case REF_VAR: {
729 VARIANT *v = ref.u.v;
731 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
732 v = V_VARIANTREF(v);
734 if(arg_cnt(dp)) {
735 SAFEARRAY *array;
737 if(!(V_VT(v) & VT_ARRAY)) {
738 FIXME("array assign on type %d\n", V_VT(v));
739 return E_FAIL;
742 switch(V_VT(v)) {
743 case VT_ARRAY|VT_BYREF|VT_VARIANT:
744 array = *V_ARRAYREF(v);
745 break;
746 case VT_ARRAY|VT_VARIANT:
747 array = V_ARRAY(v);
748 break;
749 default:
750 FIXME("Unsupported array type %x\n", V_VT(v));
751 return E_NOTIMPL;
754 if(!array) {
755 FIXME("null array\n");
756 return E_FAIL;
759 hres = array_access(ctx, array, dp, &v);
760 if(FAILED(hres))
761 return hres;
762 }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
763 FIXME("non-array assign\n");
764 return E_NOTIMPL;
767 hres = assign_value(ctx, v, dp->rgvarg, flags);
768 break;
770 case REF_DISP:
771 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, flags, dp);
772 break;
773 case REF_FUNC:
774 FIXME("functions not implemented\n");
775 return E_NOTIMPL;
776 case REF_OBJ:
777 FIXME("REF_OBJ\n");
778 return E_NOTIMPL;
779 case REF_CONST:
780 FIXME("REF_CONST\n");
781 return E_NOTIMPL;
782 case REF_NONE:
783 if(ctx->func->code_ctx->option_explicit) {
784 FIXME("throw exception\n");
785 hres = E_FAIL;
786 }else {
787 VARIANT *new_var;
789 if(arg_cnt(dp)) {
790 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
791 return E_NOTIMPL;
794 TRACE("creating variable %s\n", debugstr_w(name));
795 hres = add_dynamic_var(ctx, name, FALSE, &new_var);
796 if(SUCCEEDED(hres))
797 hres = assign_value(ctx, new_var, dp->rgvarg, flags);
801 return hres;
804 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
806 const BSTR arg = ctx->instr->arg1.bstr;
807 const unsigned arg_cnt = ctx->instr->arg2.uint;
808 DISPPARAMS dp;
809 HRESULT hres;
811 TRACE("%s\n", debugstr_w(arg));
813 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
814 hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUT, &dp);
815 if(FAILED(hres))
816 return hres;
818 stack_popn(ctx, arg_cnt+1);
819 return S_OK;
822 static HRESULT interp_set_ident(exec_ctx_t *ctx)
824 const BSTR arg = ctx->instr->arg1.bstr;
825 const unsigned arg_cnt = ctx->instr->arg2.uint;
826 DISPPARAMS dp;
827 HRESULT hres;
829 TRACE("%s\n", debugstr_w(arg));
831 if(arg_cnt) {
832 FIXME("arguments not supported\n");
833 return E_NOTIMPL;
836 hres = stack_assume_disp(ctx, 0, NULL);
837 if(FAILED(hres))
838 return hres;
840 vbstack_to_dp(ctx, 0, TRUE, &dp);
841 hres = assign_ident(ctx, ctx->instr->arg1.bstr, DISPATCH_PROPERTYPUTREF, &dp);
842 if(FAILED(hres))
843 return hres;
845 stack_popn(ctx, 1);
846 return S_OK;
849 static HRESULT interp_assign_member(exec_ctx_t *ctx)
851 BSTR identifier = ctx->instr->arg1.bstr;
852 const unsigned arg_cnt = ctx->instr->arg2.uint;
853 IDispatch *obj;
854 DISPPARAMS dp;
855 DISPID id;
856 HRESULT hres;
858 TRACE("%s\n", debugstr_w(identifier));
860 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
861 if(FAILED(hres))
862 return hres;
864 if(!obj) {
865 FIXME("NULL obj\n");
866 return E_FAIL;
869 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
870 if(SUCCEEDED(hres)) {
871 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
872 hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUT, &dp);
874 if(FAILED(hres))
875 return hres;
877 stack_popn(ctx, arg_cnt+2);
878 return S_OK;
881 static HRESULT interp_set_member(exec_ctx_t *ctx)
883 BSTR identifier = ctx->instr->arg1.bstr;
884 const unsigned arg_cnt = ctx->instr->arg2.uint;
885 IDispatch *obj;
886 DISPPARAMS dp;
887 DISPID id;
888 HRESULT hres;
890 TRACE("%s\n", debugstr_w(identifier));
892 if(arg_cnt) {
893 FIXME("arguments not supported\n");
894 return E_NOTIMPL;
897 hres = stack_assume_disp(ctx, 1, &obj);
898 if(FAILED(hres))
899 return hres;
901 if(!obj) {
902 FIXME("NULL obj\n");
903 return E_FAIL;
906 hres = stack_assume_disp(ctx, 0, NULL);
907 if(FAILED(hres))
908 return hres;
910 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
911 if(SUCCEEDED(hres)) {
912 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
913 hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUTREF, &dp);
915 if(FAILED(hres))
916 return hres;
918 stack_popn(ctx, 2);
919 return S_OK;
922 static HRESULT interp_const(exec_ctx_t *ctx)
924 BSTR arg = ctx->instr->arg1.bstr;
925 VARIANT *v;
926 ref_t ref;
927 HRESULT hres;
929 TRACE("%s\n", debugstr_w(arg));
931 assert(ctx->func->type == FUNC_GLOBAL);
933 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
934 if(FAILED(hres))
935 return hres;
937 if(ref.type != REF_NONE) {
938 FIXME("%s already defined\n", debugstr_w(arg));
939 return E_FAIL;
942 hres = stack_assume_val(ctx, 0);
943 if(FAILED(hres))
944 return hres;
946 hres = add_dynamic_var(ctx, arg, TRUE, &v);
947 if(FAILED(hres))
948 return hres;
950 *v = *stack_pop(ctx);
951 return S_OK;
954 static HRESULT interp_val(exec_ctx_t *ctx)
956 variant_val_t val;
957 VARIANT v;
958 HRESULT hres;
960 TRACE("\n");
962 hres = stack_pop_val(ctx, &val);
963 if(FAILED(hres))
964 return hres;
966 if(!val.owned) {
967 V_VT(&v) = VT_EMPTY;
968 hres = VariantCopy(&v, val.v);
969 if(FAILED(hres))
970 return hres;
973 return stack_push(ctx, val.owned ? val.v : &v);
976 static HRESULT interp_pop(exec_ctx_t *ctx)
978 const unsigned n = ctx->instr->arg1.uint;
980 TRACE("%u\n", n);
982 stack_popn(ctx, n);
983 return S_OK;
986 static HRESULT interp_new(exec_ctx_t *ctx)
988 const WCHAR *arg = ctx->instr->arg1.bstr;
989 class_desc_t *class_desc;
990 vbdisp_t *obj;
991 VARIANT v;
992 HRESULT hres;
994 static const WCHAR regexpW[] = {'r','e','g','e','x','p',0};
996 TRACE("%s\n", debugstr_w(arg));
998 if(!strcmpiW(arg, regexpW)) {
999 V_VT(&v) = VT_DISPATCH;
1000 hres = create_regexp(&V_DISPATCH(&v));
1001 if(FAILED(hres))
1002 return hres;
1004 return stack_push(ctx, &v);
1007 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
1008 if(!strcmpiW(class_desc->name, arg))
1009 break;
1011 if(!class_desc) {
1012 FIXME("Class %s not found\n", debugstr_w(arg));
1013 return E_FAIL;
1016 hres = create_vbdisp(class_desc, &obj);
1017 if(FAILED(hres))
1018 return hres;
1020 V_VT(&v) = VT_DISPATCH;
1021 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
1022 return stack_push(ctx, &v);
1025 static HRESULT interp_dim(exec_ctx_t *ctx)
1027 const BSTR ident = ctx->instr->arg1.bstr;
1028 const unsigned array_id = ctx->instr->arg2.uint;
1029 const array_desc_t *array_desc;
1030 ref_t ref;
1031 HRESULT hres;
1033 TRACE("%s\n", debugstr_w(ident));
1035 assert(array_id < ctx->func->array_cnt);
1036 if(!ctx->arrays) {
1037 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
1038 if(!ctx->arrays)
1039 return E_OUTOFMEMORY;
1042 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1043 if(FAILED(hres)) {
1044 FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
1045 return hres;
1048 if(ref.type != REF_VAR) {
1049 FIXME("got ref.type = %d\n", ref.type);
1050 return E_FAIL;
1053 if(ctx->arrays[array_id]) {
1054 FIXME("Array already initialized\n");
1055 return E_FAIL;
1058 array_desc = ctx->func->array_descs + array_id;
1059 if(array_desc->dim_cnt) {
1060 ctx->arrays[array_id] = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1061 if(!ctx->arrays[array_id])
1062 return E_OUTOFMEMORY;
1065 V_VT(ref.u.v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1066 V_ARRAYREF(ref.u.v) = ctx->arrays+array_id;
1067 return S_OK;
1070 static HRESULT interp_step(exec_ctx_t *ctx)
1072 const BSTR ident = ctx->instr->arg2.bstr;
1073 BOOL gteq_zero;
1074 VARIANT zero;
1075 ref_t ref;
1076 HRESULT hres;
1078 TRACE("%s\n", debugstr_w(ident));
1080 V_VT(&zero) = VT_I2;
1081 V_I2(&zero) = 0;
1082 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1083 if(FAILED(hres))
1084 return hres;
1086 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1088 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1089 if(FAILED(hres))
1090 return hres;
1092 if(ref.type != REF_VAR) {
1093 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1094 return E_FAIL;
1097 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1098 if(FAILED(hres))
1099 return hres;
1101 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1102 ctx->instr++;
1103 }else {
1104 stack_popn(ctx, 2);
1105 instr_jmp(ctx, ctx->instr->arg1.uint);
1107 return S_OK;
1110 static HRESULT interp_newenum(exec_ctx_t *ctx)
1112 variant_val_t v;
1113 VARIANT *r;
1114 HRESULT hres;
1116 TRACE("\n");
1118 stack_pop_deref(ctx, &v);
1119 assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
1120 r = stack_top(ctx, 0);
1122 switch(V_VT(v.v)) {
1123 case VT_DISPATCH|VT_BYREF:
1124 case VT_DISPATCH: {
1125 IEnumVARIANT *iter;
1126 DISPPARAMS dp = {0};
1127 VARIANT iterv;
1129 hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
1130 release_val(&v);
1131 if(FAILED(hres))
1132 return hres;
1134 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1135 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1136 VariantClear(&iterv);
1137 return hres;
1140 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1141 IUnknown_Release(V_UNKNOWN(&iterv));
1142 if(FAILED(hres)) {
1143 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
1144 return hres;
1147 V_VT(r) = VT_UNKNOWN;
1148 V_UNKNOWN(r) = (IUnknown*)iter;
1149 break;
1151 case VT_VARIANT|VT_ARRAY:
1152 case VT_VARIANT|VT_ARRAY|VT_BYREF: {
1153 IEnumVARIANT *iter;
1155 hres = create_safearray_iter(V_ISBYREF(v.v) ? *V_ARRAYREF(v.v) : V_ARRAY(v.v), &iter);
1156 if(FAILED(hres))
1157 return hres;
1159 V_VT(r) = VT_UNKNOWN;
1160 V_UNKNOWN(r) = (IUnknown*)iter;
1161 break;
1163 default:
1164 FIXME("Unsupported for %s\n", debugstr_variant(v.v));
1165 release_val(&v);
1166 return E_NOTIMPL;
1169 return S_OK;
1172 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1174 const unsigned loop_end = ctx->instr->arg1.uint;
1175 const BSTR ident = ctx->instr->arg2.bstr;
1176 VARIANT v;
1177 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1178 IEnumVARIANT *iter;
1179 BOOL do_continue;
1180 HRESULT hres;
1182 TRACE("\n");
1184 if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
1185 FIXME("uninitialized\n");
1186 return E_FAIL;
1189 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1190 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1192 V_VT(&v) = VT_EMPTY;
1193 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1194 if(FAILED(hres))
1195 return hres;
1197 do_continue = hres == S_OK;
1198 hres = assign_ident(ctx, ident, DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &dp);
1199 VariantClear(&v);
1200 if(FAILED(hres))
1201 return hres;
1203 if(do_continue) {
1204 ctx->instr++;
1205 }else {
1206 stack_popn(ctx, 1);
1207 instr_jmp(ctx, loop_end);
1209 return S_OK;
1212 static HRESULT interp_jmp(exec_ctx_t *ctx)
1214 const unsigned arg = ctx->instr->arg1.uint;
1216 TRACE("%u\n", arg);
1218 instr_jmp(ctx, arg);
1219 return S_OK;
1222 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1224 const unsigned arg = ctx->instr->arg1.uint;
1225 HRESULT hres;
1226 BOOL b;
1228 TRACE("%u\n", arg);
1230 hres = stack_pop_bool(ctx, &b);
1231 if(FAILED(hres))
1232 return hres;
1234 if(b)
1235 ctx->instr++;
1236 else
1237 instr_jmp(ctx, ctx->instr->arg1.uint);
1238 return S_OK;
1241 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1243 const unsigned arg = ctx->instr->arg1.uint;
1244 HRESULT hres;
1245 BOOL b;
1247 TRACE("%u\n", arg);
1249 hres = stack_pop_bool(ctx, &b);
1250 if(FAILED(hres))
1251 return hres;
1253 if(b)
1254 instr_jmp(ctx, ctx->instr->arg1.uint);
1255 else
1256 ctx->instr++;
1257 return S_OK;
1260 static HRESULT interp_ret(exec_ctx_t *ctx)
1262 TRACE("\n");
1264 ctx->instr = NULL;
1265 return S_OK;
1268 static HRESULT interp_stop(exec_ctx_t *ctx)
1270 WARN("\n");
1272 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1273 return S_OK;
1276 static HRESULT interp_me(exec_ctx_t *ctx)
1278 VARIANT v;
1280 TRACE("\n");
1282 IDispatch_AddRef(ctx->this_obj);
1283 V_VT(&v) = VT_DISPATCH;
1284 V_DISPATCH(&v) = ctx->this_obj;
1285 return stack_push(ctx, &v);
1288 static HRESULT interp_bool(exec_ctx_t *ctx)
1290 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1291 VARIANT v;
1293 TRACE("%s\n", arg ? "true" : "false");
1295 V_VT(&v) = VT_BOOL;
1296 V_BOOL(&v) = arg;
1297 return stack_push(ctx, &v);
1300 static HRESULT interp_errmode(exec_ctx_t *ctx)
1302 const int err_mode = ctx->instr->arg1.uint;
1304 TRACE("%d\n", err_mode);
1306 ctx->resume_next = err_mode;
1307 ctx->script->err_number = S_OK;
1308 return S_OK;
1311 static HRESULT interp_string(exec_ctx_t *ctx)
1313 VARIANT v;
1315 TRACE("\n");
1317 V_VT(&v) = VT_BSTR;
1318 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1319 if(!V_BSTR(&v))
1320 return E_OUTOFMEMORY;
1322 return stack_push(ctx, &v);
1325 static HRESULT interp_long(exec_ctx_t *ctx)
1327 const LONG arg = ctx->instr->arg1.lng;
1328 VARIANT v;
1330 TRACE("%d\n", arg);
1332 V_VT(&v) = VT_I4;
1333 V_I4(&v) = arg;
1334 return stack_push(ctx, &v);
1337 static HRESULT interp_short(exec_ctx_t *ctx)
1339 const LONG arg = ctx->instr->arg1.lng;
1340 VARIANT v;
1342 TRACE("%d\n", arg);
1344 V_VT(&v) = VT_I2;
1345 V_I2(&v) = arg;
1346 return stack_push(ctx, &v);
1349 static HRESULT interp_double(exec_ctx_t *ctx)
1351 const DOUBLE *arg = ctx->instr->arg1.dbl;
1352 VARIANT v;
1354 TRACE("%lf\n", *arg);
1356 V_VT(&v) = VT_R8;
1357 V_R8(&v) = *arg;
1358 return stack_push(ctx, &v);
1361 static HRESULT interp_empty(exec_ctx_t *ctx)
1363 VARIANT v;
1365 TRACE("\n");
1367 V_VT(&v) = VT_EMPTY;
1368 return stack_push(ctx, &v);
1371 static HRESULT interp_null(exec_ctx_t *ctx)
1373 TRACE("\n");
1374 return stack_push_null(ctx);
1377 static HRESULT interp_nothing(exec_ctx_t *ctx)
1379 VARIANT v;
1381 TRACE("\n");
1383 V_VT(&v) = VT_DISPATCH;
1384 V_DISPATCH(&v) = NULL;
1385 return stack_push(ctx, &v);
1388 static HRESULT interp_hres(exec_ctx_t *ctx)
1390 const unsigned arg = ctx->instr->arg1.uint;
1391 VARIANT v;
1393 TRACE("%d\n", arg);
1395 V_VT(&v) = VT_ERROR;
1396 V_ERROR(&v) = arg;
1397 return stack_push(ctx, &v);
1400 static HRESULT interp_not(exec_ctx_t *ctx)
1402 variant_val_t val;
1403 VARIANT v;
1404 HRESULT hres;
1406 TRACE("\n");
1408 hres = stack_pop_val(ctx, &val);
1409 if(FAILED(hres))
1410 return hres;
1412 hres = VarNot(val.v, &v);
1413 release_val(&val);
1414 if(FAILED(hres))
1415 return hres;
1417 return stack_push(ctx, &v);
1420 static HRESULT interp_and(exec_ctx_t *ctx)
1422 variant_val_t r, l;
1423 VARIANT v;
1424 HRESULT hres;
1426 TRACE("\n");
1428 hres = stack_pop_val(ctx, &r);
1429 if(FAILED(hres))
1430 return hres;
1432 hres = stack_pop_val(ctx, &l);
1433 if(SUCCEEDED(hres)) {
1434 hres = VarAnd(l.v, r.v, &v);
1435 release_val(&l);
1437 release_val(&r);
1438 if(FAILED(hres))
1439 return hres;
1441 return stack_push(ctx, &v);
1444 static HRESULT interp_or(exec_ctx_t *ctx)
1446 variant_val_t r, l;
1447 VARIANT v;
1448 HRESULT hres;
1450 TRACE("\n");
1452 hres = stack_pop_val(ctx, &r);
1453 if(FAILED(hres))
1454 return hres;
1456 hres = stack_pop_val(ctx, &l);
1457 if(SUCCEEDED(hres)) {
1458 hres = VarOr(l.v, r.v, &v);
1459 release_val(&l);
1461 release_val(&r);
1462 if(FAILED(hres))
1463 return hres;
1465 return stack_push(ctx, &v);
1468 static HRESULT interp_xor(exec_ctx_t *ctx)
1470 variant_val_t r, l;
1471 VARIANT v;
1472 HRESULT hres;
1474 TRACE("\n");
1476 hres = stack_pop_val(ctx, &r);
1477 if(FAILED(hres))
1478 return hres;
1480 hres = stack_pop_val(ctx, &l);
1481 if(SUCCEEDED(hres)) {
1482 hres = VarXor(l.v, r.v, &v);
1483 release_val(&l);
1485 release_val(&r);
1486 if(FAILED(hres))
1487 return hres;
1489 return stack_push(ctx, &v);
1492 static HRESULT interp_eqv(exec_ctx_t *ctx)
1494 variant_val_t r, l;
1495 VARIANT v;
1496 HRESULT hres;
1498 TRACE("\n");
1500 hres = stack_pop_val(ctx, &r);
1501 if(FAILED(hres))
1502 return hres;
1504 hres = stack_pop_val(ctx, &l);
1505 if(SUCCEEDED(hres)) {
1506 hres = VarEqv(l.v, r.v, &v);
1507 release_val(&l);
1509 release_val(&r);
1510 if(FAILED(hres))
1511 return hres;
1513 return stack_push(ctx, &v);
1516 static HRESULT interp_imp(exec_ctx_t *ctx)
1518 variant_val_t r, l;
1519 VARIANT v;
1520 HRESULT hres;
1522 TRACE("\n");
1524 hres = stack_pop_val(ctx, &r);
1525 if(FAILED(hres))
1526 return hres;
1528 hres = stack_pop_val(ctx, &l);
1529 if(SUCCEEDED(hres)) {
1530 hres = VarImp(l.v, r.v, &v);
1531 release_val(&l);
1533 release_val(&r);
1534 if(FAILED(hres))
1535 return hres;
1537 return stack_push(ctx, &v);
1540 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1542 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1544 /* FIXME: Fix comparing string to number */
1546 return VarCmp(l, r, ctx->script->lcid, 0);
1549 static HRESULT cmp_oper(exec_ctx_t *ctx)
1551 variant_val_t l, r;
1552 HRESULT hres;
1554 hres = stack_pop_val(ctx, &r);
1555 if(FAILED(hres))
1556 return hres;
1558 hres = stack_pop_val(ctx, &l);
1559 if(SUCCEEDED(hres)) {
1560 hres = var_cmp(ctx, l.v, r.v);
1561 release_val(&l);
1564 release_val(&r);
1565 return hres;
1568 static HRESULT interp_equal(exec_ctx_t *ctx)
1570 VARIANT v;
1571 HRESULT hres;
1573 TRACE("\n");
1575 hres = cmp_oper(ctx);
1576 if(FAILED(hres))
1577 return hres;
1578 if(hres == VARCMP_NULL)
1579 return stack_push_null(ctx);
1581 V_VT(&v) = VT_BOOL;
1582 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1583 return stack_push(ctx, &v);
1586 static HRESULT interp_nequal(exec_ctx_t *ctx)
1588 VARIANT v;
1589 HRESULT hres;
1591 TRACE("\n");
1593 hres = cmp_oper(ctx);
1594 if(FAILED(hres))
1595 return hres;
1596 if(hres == VARCMP_NULL)
1597 return stack_push_null(ctx);
1599 V_VT(&v) = VT_BOOL;
1600 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1601 return stack_push(ctx, &v);
1604 static HRESULT interp_gt(exec_ctx_t *ctx)
1606 VARIANT v;
1607 HRESULT hres;
1609 TRACE("\n");
1611 hres = cmp_oper(ctx);
1612 if(FAILED(hres))
1613 return hres;
1614 if(hres == VARCMP_NULL)
1615 return stack_push_null(ctx);
1617 V_VT(&v) = VT_BOOL;
1618 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1619 return stack_push(ctx, &v);
1622 static HRESULT interp_gteq(exec_ctx_t *ctx)
1624 VARIANT v;
1625 HRESULT hres;
1627 TRACE("\n");
1629 hres = cmp_oper(ctx);
1630 if(FAILED(hres))
1631 return hres;
1632 if(hres == VARCMP_NULL)
1633 return stack_push_null(ctx);
1635 V_VT(&v) = VT_BOOL;
1636 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1637 return stack_push(ctx, &v);
1640 static HRESULT interp_lt(exec_ctx_t *ctx)
1642 VARIANT v;
1643 HRESULT hres;
1645 TRACE("\n");
1647 hres = cmp_oper(ctx);
1648 if(FAILED(hres))
1649 return hres;
1650 if(hres == VARCMP_NULL)
1651 return stack_push_null(ctx);
1653 V_VT(&v) = VT_BOOL;
1654 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1655 return stack_push(ctx, &v);
1658 static HRESULT interp_lteq(exec_ctx_t *ctx)
1660 VARIANT v;
1661 HRESULT hres;
1663 TRACE("\n");
1665 hres = cmp_oper(ctx);
1666 if(FAILED(hres))
1667 return hres;
1668 if(hres == VARCMP_NULL)
1669 return stack_push_null(ctx);
1671 V_VT(&v) = VT_BOOL;
1672 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1673 return stack_push(ctx, &v);
1676 static HRESULT interp_case(exec_ctx_t *ctx)
1678 const unsigned arg = ctx->instr->arg1.uint;
1679 variant_val_t v;
1680 HRESULT hres;
1682 TRACE("%d\n", arg);
1684 hres = stack_pop_val(ctx, &v);
1685 if(FAILED(hres))
1686 return hres;
1688 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1689 release_val(&v);
1690 if(FAILED(hres))
1691 return hres;
1693 if(hres == VARCMP_EQ) {
1694 stack_popn(ctx, 1);
1695 instr_jmp(ctx, arg);
1696 }else {
1697 ctx->instr++;
1700 return S_OK;
1703 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1705 IObjectIdentity *identity;
1706 IUnknown *unk1, *unk2;
1707 HRESULT hres;
1709 if(disp1 == disp2) {
1710 *ret = VARIANT_TRUE;
1711 return S_OK;
1714 if(!disp1 || !disp2) {
1715 *ret = VARIANT_FALSE;
1716 return S_OK;
1719 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1720 if(FAILED(hres))
1721 return hres;
1723 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1724 if(FAILED(hres)) {
1725 IUnknown_Release(unk1);
1726 return hres;
1729 if(unk1 == unk2) {
1730 *ret = VARIANT_TRUE;
1731 }else {
1732 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1733 if(SUCCEEDED(hres)) {
1734 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1735 IObjectIdentity_Release(identity);
1736 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1737 }else {
1738 *ret = VARIANT_FALSE;
1742 IUnknown_Release(unk1);
1743 IUnknown_Release(unk2);
1744 return S_OK;
1747 static HRESULT interp_is(exec_ctx_t *ctx)
1749 IDispatch *l, *r;
1750 VARIANT v;
1751 HRESULT hres;
1753 TRACE("\n");
1755 hres = stack_pop_disp(ctx, &r);
1756 if(FAILED(hres))
1757 return hres;
1759 hres = stack_pop_disp(ctx, &l);
1760 if(SUCCEEDED(hres)) {
1761 V_VT(&v) = VT_BOOL;
1762 hres = disp_cmp(l, r, &V_BOOL(&v));
1763 if(l)
1764 IDispatch_Release(l);
1766 if(r)
1767 IDispatch_Release(r);
1768 if(FAILED(hres))
1769 return hres;
1771 return stack_push(ctx, &v);
1774 static HRESULT interp_concat(exec_ctx_t *ctx)
1776 variant_val_t r, l;
1777 VARIANT v;
1778 HRESULT hres;
1780 TRACE("\n");
1782 hres = stack_pop_val(ctx, &r);
1783 if(FAILED(hres))
1784 return hres;
1786 hres = stack_pop_val(ctx, &l);
1787 if(SUCCEEDED(hres)) {
1788 hres = VarCat(l.v, r.v, &v);
1789 release_val(&l);
1791 release_val(&r);
1792 if(FAILED(hres))
1793 return hres;
1795 return stack_push(ctx, &v);
1798 static HRESULT interp_add(exec_ctx_t *ctx)
1800 variant_val_t r, l;
1801 VARIANT v;
1802 HRESULT hres;
1804 TRACE("\n");
1806 hres = stack_pop_val(ctx, &r);
1807 if(FAILED(hres))
1808 return hres;
1810 hres = stack_pop_val(ctx, &l);
1811 if(SUCCEEDED(hres)) {
1812 hres = VarAdd(l.v, r.v, &v);
1813 release_val(&l);
1815 release_val(&r);
1816 if(FAILED(hres))
1817 return hres;
1819 return stack_push(ctx, &v);
1822 static HRESULT interp_sub(exec_ctx_t *ctx)
1824 variant_val_t r, l;
1825 VARIANT v;
1826 HRESULT hres;
1828 TRACE("\n");
1830 hres = stack_pop_val(ctx, &r);
1831 if(FAILED(hres))
1832 return hres;
1834 hres = stack_pop_val(ctx, &l);
1835 if(SUCCEEDED(hres)) {
1836 hres = VarSub(l.v, r.v, &v);
1837 release_val(&l);
1839 release_val(&r);
1840 if(FAILED(hres))
1841 return hres;
1843 return stack_push(ctx, &v);
1846 static HRESULT interp_mod(exec_ctx_t *ctx)
1848 variant_val_t r, l;
1849 VARIANT v;
1850 HRESULT hres;
1852 TRACE("\n");
1854 hres = stack_pop_val(ctx, &r);
1855 if(FAILED(hres))
1856 return hres;
1858 hres = stack_pop_val(ctx, &l);
1859 if(SUCCEEDED(hres)) {
1860 hres = VarMod(l.v, r.v, &v);
1861 release_val(&l);
1863 release_val(&r);
1864 if(FAILED(hres))
1865 return hres;
1867 return stack_push(ctx, &v);
1870 static HRESULT interp_idiv(exec_ctx_t *ctx)
1872 variant_val_t r, l;
1873 VARIANT v;
1874 HRESULT hres;
1876 TRACE("\n");
1878 hres = stack_pop_val(ctx, &r);
1879 if(FAILED(hres))
1880 return hres;
1882 hres = stack_pop_val(ctx, &l);
1883 if(SUCCEEDED(hres)) {
1884 hres = VarIdiv(l.v, r.v, &v);
1885 release_val(&l);
1887 release_val(&r);
1888 if(FAILED(hres))
1889 return hres;
1891 return stack_push(ctx, &v);
1894 static HRESULT interp_div(exec_ctx_t *ctx)
1896 variant_val_t r, l;
1897 VARIANT v;
1898 HRESULT hres;
1900 TRACE("\n");
1902 hres = stack_pop_val(ctx, &r);
1903 if(FAILED(hres))
1904 return hres;
1906 hres = stack_pop_val(ctx, &l);
1907 if(SUCCEEDED(hres)) {
1908 hres = VarDiv(l.v, r.v, &v);
1909 release_val(&l);
1911 release_val(&r);
1912 if(FAILED(hres))
1913 return hres;
1915 return stack_push(ctx, &v);
1918 static HRESULT interp_mul(exec_ctx_t *ctx)
1920 variant_val_t r, l;
1921 VARIANT v;
1922 HRESULT hres;
1924 TRACE("\n");
1926 hres = stack_pop_val(ctx, &r);
1927 if(FAILED(hres))
1928 return hres;
1930 hres = stack_pop_val(ctx, &l);
1931 if(SUCCEEDED(hres)) {
1932 hres = VarMul(l.v, r.v, &v);
1933 release_val(&l);
1935 release_val(&r);
1936 if(FAILED(hres))
1937 return hres;
1939 return stack_push(ctx, &v);
1942 static HRESULT interp_exp(exec_ctx_t *ctx)
1944 variant_val_t r, l;
1945 VARIANT v;
1946 HRESULT hres;
1948 TRACE("\n");
1950 hres = stack_pop_val(ctx, &r);
1951 if(FAILED(hres))
1952 return hres;
1954 hres = stack_pop_val(ctx, &l);
1955 if(SUCCEEDED(hres)) {
1956 hres = VarPow(l.v, r.v, &v);
1957 release_val(&l);
1959 release_val(&r);
1960 if(FAILED(hres))
1961 return hres;
1963 return stack_push(ctx, &v);
1966 static HRESULT interp_neg(exec_ctx_t *ctx)
1968 variant_val_t val;
1969 VARIANT v;
1970 HRESULT hres;
1972 hres = stack_pop_val(ctx, &val);
1973 if(FAILED(hres))
1974 return hres;
1976 hres = VarNeg(val.v, &v);
1977 release_val(&val);
1978 if(FAILED(hres))
1979 return hres;
1981 return stack_push(ctx, &v);
1984 static HRESULT interp_incc(exec_ctx_t *ctx)
1986 const BSTR ident = ctx->instr->arg1.bstr;
1987 VARIANT v;
1988 ref_t ref;
1989 HRESULT hres;
1991 TRACE("\n");
1993 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1994 if(FAILED(hres))
1995 return hres;
1997 if(ref.type != REF_VAR) {
1998 FIXME("ref.type is not REF_VAR\n");
1999 return E_FAIL;
2002 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
2003 if(FAILED(hres))
2004 return hres;
2006 VariantClear(ref.u.v);
2007 *ref.u.v = v;
2008 return S_OK;
2011 static HRESULT interp_catch(exec_ctx_t *ctx)
2013 /* Nothing to do here, the OP is for unwinding only. */
2014 return S_OK;
2017 static const instr_func_t op_funcs[] = {
2018 #define X(x,n,a,b) interp_ ## x,
2019 OP_LIST
2020 #undef X
2023 static const unsigned op_move[] = {
2024 #define X(x,n,a,b) n,
2025 OP_LIST
2026 #undef X
2029 void release_dynamic_vars(dynamic_var_t *var)
2031 while(var) {
2032 VariantClear(&var->v);
2033 var = var->next;
2037 static void release_exec(exec_ctx_t *ctx)
2039 unsigned i;
2041 VariantClear(&ctx->ret_val);
2042 release_dynamic_vars(ctx->dynamic_vars);
2044 if(ctx->this_obj)
2045 IDispatch_Release(ctx->this_obj);
2047 if(ctx->args) {
2048 for(i=0; i < ctx->func->arg_cnt; i++)
2049 VariantClear(ctx->args+i);
2052 if(ctx->vars) {
2053 for(i=0; i < ctx->func->var_cnt; i++)
2054 VariantClear(ctx->vars+i);
2057 if(ctx->arrays) {
2058 for(i=0; i < ctx->func->var_cnt; i++) {
2059 if(ctx->arrays[i])
2060 SafeArrayDestroy(ctx->arrays[i]);
2062 heap_free(ctx->arrays);
2065 heap_pool_free(&ctx->heap);
2066 heap_free(ctx->args);
2067 heap_free(ctx->vars);
2068 heap_free(ctx->stack);
2071 HRESULT exec_script(script_ctx_t *ctx, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2073 exec_ctx_t exec = {func->code_ctx};
2074 vbsop_t op;
2075 HRESULT hres = S_OK;
2077 exec.code = func->code_ctx;
2079 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2080 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2081 return E_FAIL;
2084 heap_pool_init(&exec.heap);
2086 if(func->arg_cnt) {
2087 VARIANT *v;
2088 unsigned i;
2090 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2091 if(!exec.args) {
2092 release_exec(&exec);
2093 return E_OUTOFMEMORY;
2096 for(i=0; i < func->arg_cnt; i++) {
2097 v = get_arg(dp, i);
2098 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2099 if(func->args[i].by_ref)
2100 exec.args[i] = *v;
2101 else
2102 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2103 }else {
2104 hres = VariantCopyInd(exec.args+i, v);
2106 if(FAILED(hres)) {
2107 release_exec(&exec);
2108 return hres;
2111 }else {
2112 exec.args = NULL;
2115 if(func->var_cnt) {
2116 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2117 if(!exec.vars) {
2118 release_exec(&exec);
2119 return E_OUTOFMEMORY;
2121 }else {
2122 exec.vars = NULL;
2125 exec.stack_size = 16;
2126 exec.top = 0;
2127 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2128 if(!exec.stack) {
2129 release_exec(&exec);
2130 return E_OUTOFMEMORY;
2133 if(vbthis) {
2134 exec.this_obj = (IDispatch*)&vbthis->IDispatchEx_iface;
2135 exec.vbthis = vbthis;
2136 }else if (ctx->host_global) {
2137 exec.this_obj = ctx->host_global;
2138 }else {
2139 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2141 IDispatch_AddRef(exec.this_obj);
2143 exec.instr = exec.code->instrs + func->code_off;
2144 exec.script = ctx;
2145 exec.func = func;
2147 while(exec.instr) {
2148 op = exec.instr->op;
2149 hres = op_funcs[op](&exec);
2150 if(FAILED(hres)) {
2151 ctx->err_number = hres = map_hres(hres);
2153 if(exec.resume_next) {
2154 unsigned stack_off;
2156 WARN("Failed %08x in resume next mode\n", hres);
2159 * Unwinding here is simple. We need to find the next OP_catch, which contains
2160 * information about expected stack size and jump offset on error. Generated
2161 * bytecode needs to guarantee, that simple jump and stack adjustment will
2162 * guarantee proper execution continuation.
2164 while((++exec.instr)->op != OP_catch);
2166 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2168 stack_off = exec.instr->arg2.uint;
2169 instr_jmp(&exec, exec.instr->arg1.uint);
2171 if(exec.top > stack_off) {
2172 stack_popn(&exec, exec.top-stack_off);
2173 }else if(exec.top < stack_off) {
2174 VARIANT v;
2176 V_VT(&v) = VT_EMPTY;
2177 while(exec.top < stack_off) {
2178 hres = stack_push(&exec, &v);
2179 if(FAILED(hres))
2180 break;
2184 continue;
2185 }else {
2186 WARN("Failed %08x\n", hres);
2187 stack_popn(&exec, exec.top);
2188 break;
2192 exec.instr += op_move[op];
2195 assert(!exec.top);
2196 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2197 assert(V_VT(&exec.ret_val) == VT_EMPTY);
2199 if(SUCCEEDED(hres) && res) {
2200 *res = exec.ret_val;
2201 V_VT(&exec.ret_val) = VT_EMPTY;
2204 release_exec(&exec);
2205 return hres;