dpnet/tests: Add a trailing '\n' to some ok() calls.
[wine.git] / dlls / vbscript / interp.c
blob7c660962fa4b2f7091fb60231cb91ca33c9474dc
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, 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 *val, BOOL own_val, VARIANT **out_var)
225 dynamic_var_t *new_var;
226 heap_pool_t *heap;
227 WCHAR *str;
228 unsigned size;
229 HRESULT hres;
231 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
233 new_var = heap_pool_alloc(heap, sizeof(*new_var));
234 if(!new_var)
235 return E_OUTOFMEMORY;
237 size = (strlenW(name)+1)*sizeof(WCHAR);
238 str = heap_pool_alloc(heap, size);
239 if(!str)
240 return E_OUTOFMEMORY;
241 memcpy(str, name, size);
242 new_var->name = str;
243 new_var->is_const = is_const;
245 if(own_val) {
246 new_var->v = *val;
247 }else {
248 V_VT(&new_var->v) = VT_EMPTY;
249 hres = VariantCopy(&new_var->v, val);
250 if(FAILED(hres))
251 return hres;
254 if(ctx->func->type == FUNC_GLOBAL) {
255 new_var->next = ctx->script->global_vars;
256 ctx->script->global_vars = new_var;
257 }else {
258 new_var->next = ctx->dynamic_vars;
259 ctx->dynamic_vars = new_var;
262 if(out_var)
263 *out_var = &new_var->v;
265 return S_OK;
268 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
270 assert(ctx->top);
271 return ctx->stack + --ctx->top;
274 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
276 assert(ctx->top >= n);
277 return ctx->stack + (ctx->top-n-1);
280 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
282 if(ctx->stack_size == ctx->top) {
283 VARIANT *new_stack;
285 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
286 if(!new_stack) {
287 VariantClear(v);
288 return E_OUTOFMEMORY;
291 ctx->stack = new_stack;
292 ctx->stack_size *= 2;
295 ctx->stack[ctx->top++] = *v;
296 return S_OK;
299 static inline HRESULT stack_push_null(exec_ctx_t *ctx)
301 VARIANT v;
302 V_VT(&v) = VT_NULL;
303 return stack_push(ctx, &v);
306 static void stack_popn(exec_ctx_t *ctx, unsigned n)
308 while(n--)
309 VariantClear(stack_pop(ctx));
312 static void stack_pop_deref(exec_ctx_t *ctx, variant_val_t *r)
314 VARIANT *v;
316 v = stack_pop(ctx);
317 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
318 r->owned = FALSE;
319 r->v = V_VARIANTREF(v);
320 }else {
321 r->owned = TRUE;
322 r->v = v;
326 static inline void release_val(variant_val_t *v)
328 if(v->owned)
329 VariantClear(v->v);
332 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *r)
334 stack_pop_deref(ctx, r);
336 if(V_VT(r->v) == VT_DISPATCH) {
337 DISPPARAMS dp = {0};
338 HRESULT hres;
340 hres = disp_call(ctx->script, V_DISPATCH(r->v), DISPID_VALUE, &dp, &r->store);
341 if(r->owned)
342 IDispatch_Release(V_DISPATCH(r->v));
343 if(FAILED(hres))
344 return hres;
346 r->owned = TRUE;
347 r->v = &r->store;
350 return S_OK;
353 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
355 VARIANT *v = stack_top(ctx, n);
356 HRESULT hres;
358 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
359 VARIANT *ref = V_VARIANTREF(v);
361 V_VT(v) = VT_EMPTY;
362 hres = VariantCopy(v, ref);
363 if(FAILED(hres))
364 return hres;
367 if(V_VT(v) == VT_DISPATCH) {
368 DISPPARAMS dp = {0};
369 IDispatch *disp;
371 disp = V_DISPATCH(v);
372 V_VT(v) = VT_EMPTY;
373 hres = disp_call(ctx->script, disp, DISPID_VALUE, &dp, v);
374 IDispatch_Release(disp);
375 if(FAILED(hres))
376 return hres;
379 return S_OK;
382 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
384 variant_val_t val;
385 HRESULT hres;
387 hres = stack_pop_val(ctx, &val);
388 if(FAILED(hres))
389 return hres;
391 switch (V_VT(val.v))
393 case VT_BOOL:
394 *b = V_BOOL(val.v);
395 break;
396 case VT_NULL:
397 *b = FALSE;
398 break;
399 case VT_I2:
400 *b = V_I2(val.v);
401 break;
402 case VT_I4:
403 *b = V_I4(val.v);
404 break;
405 default:
406 FIXME("unsupported for %s\n", debugstr_variant(val.v));
407 release_val(&val);
408 return E_NOTIMPL;
410 return S_OK;
413 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
415 VARIANT *v = stack_pop(ctx);
417 if(V_VT(v) == VT_DISPATCH) {
418 *ret = V_DISPATCH(v);
419 return S_OK;
422 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
423 FIXME("not supported type: %s\n", debugstr_variant(v));
424 VariantClear(v);
425 return E_FAIL;
428 v = V_BYREF(v);
429 if(V_VT(v) != VT_DISPATCH) {
430 FIXME("not disp %s\n", debugstr_variant(v));
431 return E_FAIL;
434 if(V_DISPATCH(v))
435 IDispatch_AddRef(V_DISPATCH(v));
436 *ret = V_DISPATCH(v);
437 return S_OK;
440 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
442 VARIANT *v = stack_top(ctx, n), *ref;
444 if(V_VT(v) != VT_DISPATCH) {
445 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
446 FIXME("not supported type: %s\n", debugstr_variant(v));
447 return E_FAIL;
450 ref = V_VARIANTREF(v);
451 if(V_VT(ref) != VT_DISPATCH) {
452 FIXME("not disp %s\n", debugstr_variant(ref));
453 return E_FAIL;
456 V_VT(v) = VT_DISPATCH;
457 V_DISPATCH(v) = V_DISPATCH(ref);
458 if(V_DISPATCH(v))
459 IDispatch_AddRef(V_DISPATCH(v));
462 if(disp)
463 *disp = V_DISPATCH(v);
464 return S_OK;
467 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
469 ctx->instr = ctx->code->instrs + addr;
472 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
474 dp->cNamedArgs = is_propput ? 1 : 0;
475 dp->cArgs = arg_cnt + dp->cNamedArgs;
476 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
478 if(arg_cnt) {
479 VARIANT tmp;
480 unsigned i;
482 assert(ctx->top >= arg_cnt);
484 for(i=1; i*2 <= arg_cnt; i++) {
485 tmp = ctx->stack[ctx->top-i];
486 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
487 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
490 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
491 }else {
492 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
496 static HRESULT array_access(exec_ctx_t *ctx, SAFEARRAY *array, DISPPARAMS *dp, VARIANT **ret)
498 unsigned cell_off = 0, dim_size = 1, i;
499 unsigned argc = arg_cnt(dp);
500 VARIANT *data;
501 LONG idx;
502 HRESULT hres;
504 if(!array) {
505 FIXME("NULL array\n");
506 return E_FAIL;
509 if(array->cDims != argc) {
510 FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
511 return E_FAIL;
514 for(i=0; i < argc; i++) {
515 hres = to_int(get_arg(dp, i), &idx);
516 if(FAILED(hres))
517 return hres;
519 idx -= array->rgsabound[i].lLbound;
520 if(idx >= array->rgsabound[i].cElements) {
521 FIXME("out of bound element %d in dim %d of size %d\n", idx, i+1, array->rgsabound[i].cElements);
522 return E_FAIL;
525 cell_off += idx*dim_size;
526 dim_size *= array->rgsabound[i].cElements;
529 hres = SafeArrayAccessData(array, (void**)&data);
530 if(FAILED(hres))
531 return hres;
533 *ret = data+cell_off;
535 SafeArrayUnaccessData(array);
536 return S_OK;
539 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
541 BSTR identifier = ctx->instr->arg1.bstr;
542 const unsigned arg_cnt = ctx->instr->arg2.uint;
543 DISPPARAMS dp;
544 ref_t ref;
545 HRESULT hres;
547 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
548 if(FAILED(hres))
549 return hres;
551 switch(ref.type) {
552 case REF_VAR:
553 case REF_CONST: {
554 VARIANT *v;
556 if(!res) {
557 FIXME("REF_VAR no res\n");
558 return E_NOTIMPL;
561 v = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
563 if(arg_cnt) {
564 SAFEARRAY *array = NULL;
566 switch(V_VT(v)) {
567 case VT_ARRAY|VT_BYREF|VT_VARIANT:
568 array = *V_ARRAYREF(ref.u.v);
569 break;
570 case VT_ARRAY|VT_VARIANT:
571 array = V_ARRAY(ref.u.v);
572 break;
573 case VT_DISPATCH:
574 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
575 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_VALUE, &dp, res);
576 if(FAILED(hres))
577 return hres;
578 break;
579 default:
580 FIXME("arguments not implemented\n");
581 return E_NOTIMPL;
584 if(!array)
585 break;
587 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
588 hres = array_access(ctx, array, &dp, &v);
589 if(FAILED(hres))
590 return hres;
593 V_VT(res) = VT_BYREF|VT_VARIANT;
594 V_BYREF(res) = v;
595 break;
597 case REF_DISP:
598 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
599 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
600 if(FAILED(hres))
601 return hres;
602 break;
603 case REF_FUNC:
604 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
605 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
606 if(FAILED(hres))
607 return hres;
608 break;
609 case REF_OBJ:
610 if(arg_cnt) {
611 FIXME("arguments on object\n");
612 return E_NOTIMPL;
615 if(res) {
616 IDispatch_AddRef(ref.u.obj);
617 V_VT(res) = VT_DISPATCH;
618 V_DISPATCH(res) = ref.u.obj;
620 break;
621 case REF_NONE:
622 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
623 VARIANT v, *new;
624 VariantInit(&v);
625 hres = add_dynamic_var(ctx, identifier, FALSE, &v, FALSE, &new);
626 if(FAILED(hres))
627 return hres;
628 V_VT(res) = VT_BYREF|VT_VARIANT;
629 V_BYREF(res) = new;
630 break;
632 FIXME("%s not found\n", debugstr_w(identifier));
633 return DISP_E_UNKNOWNNAME;
636 stack_popn(ctx, arg_cnt);
637 return S_OK;
640 static HRESULT interp_icall(exec_ctx_t *ctx)
642 VARIANT v;
643 HRESULT hres;
645 TRACE("\n");
647 hres = do_icall(ctx, &v);
648 if(FAILED(hres))
649 return hres;
651 return stack_push(ctx, &v);
654 static HRESULT interp_icallv(exec_ctx_t *ctx)
656 TRACE("\n");
657 return do_icall(ctx, NULL);
660 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
662 const BSTR identifier = ctx->instr->arg1.bstr;
663 const unsigned arg_cnt = ctx->instr->arg2.uint;
664 IDispatch *obj;
665 DISPPARAMS dp;
666 DISPID id;
667 HRESULT hres;
669 hres = stack_pop_disp(ctx, &obj);
670 if(FAILED(hres))
671 return hres;
673 if(!obj) {
674 FIXME("NULL obj\n");
675 return E_FAIL;
678 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
680 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
681 if(SUCCEEDED(hres))
682 hres = disp_call(ctx->script, obj, id, &dp, res);
683 IDispatch_Release(obj);
684 if(FAILED(hres))
685 return hres;
687 stack_popn(ctx, arg_cnt);
688 return S_OK;
691 static HRESULT interp_mcall(exec_ctx_t *ctx)
693 VARIANT res;
694 HRESULT hres;
696 TRACE("\n");
698 hres = do_mcall(ctx, &res);
699 if(FAILED(hres))
700 return hres;
702 return stack_push(ctx, &res);
705 static HRESULT interp_mcallv(exec_ctx_t *ctx)
707 TRACE("\n");
709 return do_mcall(ctx, NULL);
712 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
714 ref_t ref;
715 HRESULT hres;
717 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
718 if(FAILED(hres))
719 return hres;
721 switch(ref.type) {
722 case REF_VAR: {
723 VARIANT *v = ref.u.v;
725 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
726 v = V_VARIANTREF(v);
728 if(arg_cnt(dp)) {
729 SAFEARRAY *array;
731 if(!(V_VT(v) & VT_ARRAY)) {
732 FIXME("array assign on type %d\n", V_VT(v));
733 return E_FAIL;
736 switch(V_VT(v)) {
737 case VT_ARRAY|VT_BYREF|VT_VARIANT:
738 array = *V_ARRAYREF(v);
739 break;
740 case VT_ARRAY|VT_VARIANT:
741 array = V_ARRAY(v);
742 break;
743 default:
744 FIXME("Unsupported array type %x\n", V_VT(v));
745 return E_NOTIMPL;
748 if(!array) {
749 FIXME("null array\n");
750 return E_FAIL;
753 hres = array_access(ctx, array, dp, &v);
754 if(FAILED(hres))
755 return hres;
756 }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
757 FIXME("non-array assign\n");
758 return E_NOTIMPL;
761 hres = VariantCopyInd(v, dp->rgvarg);
762 break;
764 case REF_DISP:
765 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
766 break;
767 case REF_FUNC:
768 FIXME("functions not implemented\n");
769 return E_NOTIMPL;
770 case REF_OBJ:
771 FIXME("REF_OBJ\n");
772 return E_NOTIMPL;
773 case REF_CONST:
774 FIXME("REF_CONST\n");
775 return E_NOTIMPL;
776 case REF_NONE:
777 if(ctx->func->code_ctx->option_explicit) {
778 FIXME("throw exception\n");
779 hres = E_FAIL;
780 }else {
781 if(arg_cnt(dp)) {
782 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
783 return E_NOTIMPL;
786 TRACE("creating variable %s\n", debugstr_w(name));
787 hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE, NULL);
791 return hres;
794 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
796 const BSTR arg = ctx->instr->arg1.bstr;
797 const unsigned arg_cnt = ctx->instr->arg2.uint;
798 DISPPARAMS dp;
799 HRESULT hres;
801 TRACE("%s\n", debugstr_w(arg));
803 hres = stack_assume_val(ctx, arg_cnt);
804 if(FAILED(hres))
805 return hres;
807 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
808 hres = assign_ident(ctx, arg, &dp);
809 if(FAILED(hres))
810 return hres;
812 stack_popn(ctx, arg_cnt+1);
813 return S_OK;
816 static HRESULT interp_set_ident(exec_ctx_t *ctx)
818 const BSTR arg = ctx->instr->arg1.bstr;
819 const unsigned arg_cnt = ctx->instr->arg2.uint;
820 DISPPARAMS dp;
821 HRESULT hres;
823 TRACE("%s\n", debugstr_w(arg));
825 if(arg_cnt) {
826 FIXME("arguments not supported\n");
827 return E_NOTIMPL;
830 hres = stack_assume_disp(ctx, 0, NULL);
831 if(FAILED(hres))
832 return hres;
834 vbstack_to_dp(ctx, 0, TRUE, &dp);
835 hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
836 if(FAILED(hres))
837 return hres;
839 stack_popn(ctx, 1);
840 return S_OK;
843 static HRESULT interp_assign_member(exec_ctx_t *ctx)
845 BSTR identifier = ctx->instr->arg1.bstr;
846 const unsigned arg_cnt = ctx->instr->arg2.uint;
847 IDispatch *obj;
848 DISPPARAMS dp;
849 DISPID id;
850 HRESULT hres;
852 TRACE("%s\n", debugstr_w(identifier));
854 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
855 if(FAILED(hres))
856 return hres;
858 if(!obj) {
859 FIXME("NULL obj\n");
860 return E_FAIL;
863 hres = stack_assume_val(ctx, arg_cnt);
864 if(FAILED(hres))
865 return hres;
867 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
868 if(SUCCEEDED(hres)) {
869 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
870 hres = disp_propput(ctx->script, obj, id, &dp);
872 if(FAILED(hres))
873 return hres;
875 stack_popn(ctx, arg_cnt+2);
876 return S_OK;
879 static HRESULT interp_set_member(exec_ctx_t *ctx)
881 BSTR identifier = ctx->instr->arg1.bstr;
882 const unsigned arg_cnt = ctx->instr->arg2.uint;
883 IDispatch *obj;
884 DISPPARAMS dp;
885 DISPID id;
886 HRESULT hres;
888 TRACE("%s\n", debugstr_w(identifier));
890 if(arg_cnt) {
891 FIXME("arguments not supported\n");
892 return E_NOTIMPL;
895 hres = stack_assume_disp(ctx, 1, &obj);
896 if(FAILED(hres))
897 return hres;
899 if(!obj) {
900 FIXME("NULL obj\n");
901 return E_FAIL;
904 hres = stack_assume_disp(ctx, 0, NULL);
905 if(FAILED(hres))
906 return hres;
908 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
909 if(SUCCEEDED(hres)) {
910 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
911 hres = disp_propput(ctx->script, obj, id, &dp);
913 if(FAILED(hres))
914 return hres;
916 stack_popn(ctx, 2);
917 return S_OK;
920 static HRESULT interp_const(exec_ctx_t *ctx)
922 BSTR arg = ctx->instr->arg1.bstr;
923 variant_val_t val;
924 ref_t ref;
925 HRESULT hres;
927 TRACE("%s\n", debugstr_w(arg));
929 assert(ctx->func->type == FUNC_GLOBAL);
931 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
932 if(FAILED(hres))
933 return hres;
935 if(ref.type != REF_NONE) {
936 FIXME("%s already defined\n", debugstr_w(arg));
937 return E_FAIL;
940 hres = stack_pop_val(ctx, &val);
941 if(FAILED(hres))
942 return hres;
944 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned, NULL);
947 static HRESULT interp_val(exec_ctx_t *ctx)
949 variant_val_t val;
950 VARIANT v;
951 HRESULT hres;
953 TRACE("\n");
955 hres = stack_pop_val(ctx, &val);
956 if(FAILED(hres))
957 return hres;
959 if(!val.owned) {
960 V_VT(&v) = VT_EMPTY;
961 hres = VariantCopy(&v, val.v);
962 if(FAILED(hres))
963 return hres;
966 return stack_push(ctx, val.owned ? val.v : &v);
969 static HRESULT interp_pop(exec_ctx_t *ctx)
971 const unsigned n = ctx->instr->arg1.uint;
973 TRACE("%u\n", n);
975 stack_popn(ctx, n);
976 return S_OK;
979 static HRESULT interp_new(exec_ctx_t *ctx)
981 const WCHAR *arg = ctx->instr->arg1.bstr;
982 class_desc_t *class_desc;
983 vbdisp_t *obj;
984 VARIANT v;
985 HRESULT hres;
987 static const WCHAR regexpW[] = {'r','e','g','e','x','p',0};
989 TRACE("%s\n", debugstr_w(arg));
991 if(!strcmpiW(arg, regexpW)) {
992 V_VT(&v) = VT_DISPATCH;
993 hres = create_regexp(&V_DISPATCH(&v));
994 if(FAILED(hres))
995 return hres;
997 return stack_push(ctx, &v);
1000 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
1001 if(!strcmpiW(class_desc->name, arg))
1002 break;
1004 if(!class_desc) {
1005 FIXME("Class %s not found\n", debugstr_w(arg));
1006 return E_FAIL;
1009 hres = create_vbdisp(class_desc, &obj);
1010 if(FAILED(hres))
1011 return hres;
1013 V_VT(&v) = VT_DISPATCH;
1014 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
1015 return stack_push(ctx, &v);
1018 static HRESULT interp_dim(exec_ctx_t *ctx)
1020 const BSTR ident = ctx->instr->arg1.bstr;
1021 const unsigned array_id = ctx->instr->arg2.uint;
1022 const array_desc_t *array_desc;
1023 ref_t ref;
1024 HRESULT hres;
1026 TRACE("%s\n", debugstr_w(ident));
1028 assert(array_id < ctx->func->array_cnt);
1029 if(!ctx->arrays) {
1030 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
1031 if(!ctx->arrays)
1032 return E_OUTOFMEMORY;
1035 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1036 if(FAILED(hres)) {
1037 FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
1038 return hres;
1041 if(ref.type != REF_VAR) {
1042 FIXME("got ref.type = %d\n", ref.type);
1043 return E_FAIL;
1046 if(ctx->arrays[array_id]) {
1047 FIXME("Array already initialized\n");
1048 return E_FAIL;
1051 array_desc = ctx->func->array_descs + array_id;
1052 if(array_desc->dim_cnt) {
1053 ctx->arrays[array_id] = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1054 if(!ctx->arrays[array_id])
1055 return E_OUTOFMEMORY;
1058 V_VT(ref.u.v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1059 V_ARRAYREF(ref.u.v) = ctx->arrays+array_id;
1060 return S_OK;
1063 static HRESULT interp_step(exec_ctx_t *ctx)
1065 const BSTR ident = ctx->instr->arg2.bstr;
1066 BOOL gteq_zero;
1067 VARIANT zero;
1068 ref_t ref;
1069 HRESULT hres;
1071 TRACE("%s\n", debugstr_w(ident));
1073 V_VT(&zero) = VT_I2;
1074 V_I2(&zero) = 0;
1075 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1076 if(FAILED(hres))
1077 return hres;
1079 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1081 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1082 if(FAILED(hres))
1083 return hres;
1085 if(ref.type != REF_VAR) {
1086 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1087 return E_FAIL;
1090 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1091 if(FAILED(hres))
1092 return hres;
1094 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1095 ctx->instr++;
1096 }else {
1097 stack_popn(ctx, 2);
1098 instr_jmp(ctx, ctx->instr->arg1.uint);
1100 return S_OK;
1103 static HRESULT interp_newenum(exec_ctx_t *ctx)
1105 variant_val_t v;
1106 VARIANT *r;
1107 HRESULT hres;
1109 TRACE("\n");
1111 stack_pop_deref(ctx, &v);
1112 assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
1113 r = stack_top(ctx, 0);
1115 switch(V_VT(v.v)) {
1116 case VT_DISPATCH|VT_BYREF:
1117 case VT_DISPATCH: {
1118 IEnumVARIANT *iter;
1119 DISPPARAMS dp = {0};
1120 VARIANT iterv;
1122 hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
1123 release_val(&v);
1124 if(FAILED(hres))
1125 return hres;
1127 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1128 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1129 VariantClear(&iterv);
1130 return hres;
1133 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1134 IUnknown_Release(V_UNKNOWN(&iterv));
1135 if(FAILED(hres)) {
1136 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
1137 return hres;
1140 V_VT(r) = VT_UNKNOWN;
1141 V_UNKNOWN(r) = (IUnknown*)iter;
1142 break;
1144 default:
1145 FIXME("Unsupported for %s\n", debugstr_variant(v.v));
1146 release_val(&v);
1147 return E_NOTIMPL;
1150 return S_OK;
1153 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1155 const unsigned loop_end = ctx->instr->arg1.uint;
1156 const BSTR ident = ctx->instr->arg2.bstr;
1157 VARIANT v;
1158 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1159 IEnumVARIANT *iter;
1160 BOOL do_continue;
1161 HRESULT hres;
1163 TRACE("\n");
1165 if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
1166 FIXME("uninitialized\n");
1167 return E_FAIL;
1170 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1171 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1173 V_VT(&v) = VT_EMPTY;
1174 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1175 if(FAILED(hres))
1176 return hres;
1178 do_continue = hres == S_OK;
1179 hres = assign_ident(ctx, ident, &dp);
1180 VariantClear(&v);
1181 if(FAILED(hres))
1182 return hres;
1184 if(do_continue) {
1185 ctx->instr++;
1186 }else {
1187 stack_pop(ctx);
1188 instr_jmp(ctx, loop_end);
1190 return S_OK;
1193 static HRESULT interp_jmp(exec_ctx_t *ctx)
1195 const unsigned arg = ctx->instr->arg1.uint;
1197 TRACE("%u\n", arg);
1199 instr_jmp(ctx, arg);
1200 return S_OK;
1203 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1205 const unsigned arg = ctx->instr->arg1.uint;
1206 HRESULT hres;
1207 BOOL b;
1209 TRACE("%u\n", arg);
1211 hres = stack_pop_bool(ctx, &b);
1212 if(FAILED(hres))
1213 return hres;
1215 if(b)
1216 ctx->instr++;
1217 else
1218 instr_jmp(ctx, ctx->instr->arg1.uint);
1219 return S_OK;
1222 static HRESULT interp_jmp_true(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 instr_jmp(ctx, ctx->instr->arg1.uint);
1236 else
1237 ctx->instr++;
1238 return S_OK;
1241 static HRESULT interp_ret(exec_ctx_t *ctx)
1243 TRACE("\n");
1245 ctx->instr = NULL;
1246 return S_OK;
1249 static HRESULT interp_stop(exec_ctx_t *ctx)
1251 WARN("\n");
1253 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1254 return S_OK;
1257 static HRESULT interp_me(exec_ctx_t *ctx)
1259 VARIANT v;
1261 TRACE("\n");
1263 IDispatch_AddRef(ctx->this_obj);
1264 V_VT(&v) = VT_DISPATCH;
1265 V_DISPATCH(&v) = ctx->this_obj;
1266 return stack_push(ctx, &v);
1269 static HRESULT interp_bool(exec_ctx_t *ctx)
1271 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1272 VARIANT v;
1274 TRACE("%s\n", arg ? "true" : "false");
1276 V_VT(&v) = VT_BOOL;
1277 V_BOOL(&v) = arg;
1278 return stack_push(ctx, &v);
1281 static HRESULT interp_errmode(exec_ctx_t *ctx)
1283 const int err_mode = ctx->instr->arg1.uint;
1285 TRACE("%d\n", err_mode);
1287 ctx->resume_next = err_mode;
1288 ctx->script->err_number = S_OK;
1289 return S_OK;
1292 static HRESULT interp_string(exec_ctx_t *ctx)
1294 VARIANT v;
1296 TRACE("\n");
1298 V_VT(&v) = VT_BSTR;
1299 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1300 if(!V_BSTR(&v))
1301 return E_OUTOFMEMORY;
1303 return stack_push(ctx, &v);
1306 static HRESULT interp_long(exec_ctx_t *ctx)
1308 const LONG arg = ctx->instr->arg1.lng;
1309 VARIANT v;
1311 TRACE("%d\n", arg);
1313 V_VT(&v) = VT_I4;
1314 V_I4(&v) = arg;
1315 return stack_push(ctx, &v);
1318 static HRESULT interp_short(exec_ctx_t *ctx)
1320 const LONG arg = ctx->instr->arg1.lng;
1321 VARIANT v;
1323 TRACE("%d\n", arg);
1325 V_VT(&v) = VT_I2;
1326 V_I2(&v) = arg;
1327 return stack_push(ctx, &v);
1330 static HRESULT interp_double(exec_ctx_t *ctx)
1332 const DOUBLE *arg = ctx->instr->arg1.dbl;
1333 VARIANT v;
1335 TRACE("%lf\n", *arg);
1337 V_VT(&v) = VT_R8;
1338 V_R8(&v) = *arg;
1339 return stack_push(ctx, &v);
1342 static HRESULT interp_empty(exec_ctx_t *ctx)
1344 VARIANT v;
1346 TRACE("\n");
1348 V_VT(&v) = VT_EMPTY;
1349 return stack_push(ctx, &v);
1352 static HRESULT interp_null(exec_ctx_t *ctx)
1354 TRACE("\n");
1355 return stack_push_null(ctx);
1358 static HRESULT interp_nothing(exec_ctx_t *ctx)
1360 VARIANT v;
1362 TRACE("\n");
1364 V_VT(&v) = VT_DISPATCH;
1365 V_DISPATCH(&v) = NULL;
1366 return stack_push(ctx, &v);
1369 static HRESULT interp_not(exec_ctx_t *ctx)
1371 variant_val_t val;
1372 VARIANT v;
1373 HRESULT hres;
1375 TRACE("\n");
1377 hres = stack_pop_val(ctx, &val);
1378 if(FAILED(hres))
1379 return hres;
1381 hres = VarNot(val.v, &v);
1382 release_val(&val);
1383 if(FAILED(hres))
1384 return hres;
1386 return stack_push(ctx, &v);
1389 static HRESULT interp_and(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 = VarAnd(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_or(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 = VarOr(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_xor(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 = VarXor(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 interp_eqv(exec_ctx_t *ctx)
1463 variant_val_t r, l;
1464 VARIANT v;
1465 HRESULT hres;
1467 TRACE("\n");
1469 hres = stack_pop_val(ctx, &r);
1470 if(FAILED(hres))
1471 return hres;
1473 hres = stack_pop_val(ctx, &l);
1474 if(SUCCEEDED(hres)) {
1475 hres = VarEqv(l.v, r.v, &v);
1476 release_val(&l);
1478 release_val(&r);
1479 if(FAILED(hres))
1480 return hres;
1482 return stack_push(ctx, &v);
1485 static HRESULT interp_imp(exec_ctx_t *ctx)
1487 variant_val_t r, l;
1488 VARIANT v;
1489 HRESULT hres;
1491 TRACE("\n");
1493 hres = stack_pop_val(ctx, &r);
1494 if(FAILED(hres))
1495 return hres;
1497 hres = stack_pop_val(ctx, &l);
1498 if(SUCCEEDED(hres)) {
1499 hres = VarImp(l.v, r.v, &v);
1500 release_val(&l);
1502 release_val(&r);
1503 if(FAILED(hres))
1504 return hres;
1506 return stack_push(ctx, &v);
1509 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1511 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1513 /* FIXME: Fix comparing string to number */
1515 return VarCmp(l, r, ctx->script->lcid, 0);
1518 static HRESULT cmp_oper(exec_ctx_t *ctx)
1520 variant_val_t l, r;
1521 HRESULT hres;
1523 hres = stack_pop_val(ctx, &r);
1524 if(FAILED(hres))
1525 return hres;
1527 hres = stack_pop_val(ctx, &l);
1528 if(SUCCEEDED(hres)) {
1529 hres = var_cmp(ctx, l.v, r.v);
1530 release_val(&l);
1533 release_val(&r);
1534 return hres;
1537 static HRESULT interp_equal(exec_ctx_t *ctx)
1539 VARIANT v;
1540 HRESULT hres;
1542 TRACE("\n");
1544 hres = cmp_oper(ctx);
1545 if(FAILED(hres))
1546 return hres;
1547 if(hres == VARCMP_NULL)
1548 return stack_push_null(ctx);
1550 V_VT(&v) = VT_BOOL;
1551 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1552 return stack_push(ctx, &v);
1555 static HRESULT interp_nequal(exec_ctx_t *ctx)
1557 VARIANT v;
1558 HRESULT hres;
1560 TRACE("\n");
1562 hres = cmp_oper(ctx);
1563 if(FAILED(hres))
1564 return hres;
1565 if(hres == VARCMP_NULL)
1566 return stack_push_null(ctx);
1568 V_VT(&v) = VT_BOOL;
1569 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1570 return stack_push(ctx, &v);
1573 static HRESULT interp_gt(exec_ctx_t *ctx)
1575 VARIANT v;
1576 HRESULT hres;
1578 TRACE("\n");
1580 hres = cmp_oper(ctx);
1581 if(FAILED(hres))
1582 return hres;
1583 if(hres == VARCMP_NULL)
1584 return stack_push_null(ctx);
1586 V_VT(&v) = VT_BOOL;
1587 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1588 return stack_push(ctx, &v);
1591 static HRESULT interp_gteq(exec_ctx_t *ctx)
1593 VARIANT v;
1594 HRESULT hres;
1596 TRACE("\n");
1598 hres = cmp_oper(ctx);
1599 if(FAILED(hres))
1600 return hres;
1601 if(hres == VARCMP_NULL)
1602 return stack_push_null(ctx);
1604 V_VT(&v) = VT_BOOL;
1605 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1606 return stack_push(ctx, &v);
1609 static HRESULT interp_lt(exec_ctx_t *ctx)
1611 VARIANT v;
1612 HRESULT hres;
1614 TRACE("\n");
1616 hres = cmp_oper(ctx);
1617 if(FAILED(hres))
1618 return hres;
1619 if(hres == VARCMP_NULL)
1620 return stack_push_null(ctx);
1622 V_VT(&v) = VT_BOOL;
1623 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1624 return stack_push(ctx, &v);
1627 static HRESULT interp_lteq(exec_ctx_t *ctx)
1629 VARIANT v;
1630 HRESULT hres;
1632 TRACE("\n");
1634 hres = cmp_oper(ctx);
1635 if(FAILED(hres))
1636 return hres;
1637 if(hres == VARCMP_NULL)
1638 return stack_push_null(ctx);
1640 V_VT(&v) = VT_BOOL;
1641 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1642 return stack_push(ctx, &v);
1645 static HRESULT interp_case(exec_ctx_t *ctx)
1647 const unsigned arg = ctx->instr->arg1.uint;
1648 variant_val_t v;
1649 HRESULT hres;
1651 TRACE("%d\n", arg);
1653 hres = stack_pop_val(ctx, &v);
1654 if(FAILED(hres))
1655 return hres;
1657 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1658 release_val(&v);
1659 if(FAILED(hres))
1660 return hres;
1662 if(hres == VARCMP_EQ) {
1663 stack_popn(ctx, 1);
1664 instr_jmp(ctx, arg);
1665 }else {
1666 ctx->instr++;
1669 return S_OK;
1672 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1674 IObjectIdentity *identity;
1675 IUnknown *unk1, *unk2;
1676 HRESULT hres;
1678 if(disp1 == disp2) {
1679 *ret = VARIANT_TRUE;
1680 return S_OK;
1683 if(!disp1 || !disp2) {
1684 *ret = VARIANT_FALSE;
1685 return S_OK;
1688 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1689 if(FAILED(hres))
1690 return hres;
1692 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1693 if(FAILED(hres)) {
1694 IUnknown_Release(unk1);
1695 return hres;
1698 if(unk1 == unk2) {
1699 *ret = VARIANT_TRUE;
1700 }else {
1701 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1702 if(SUCCEEDED(hres)) {
1703 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1704 IObjectIdentity_Release(identity);
1705 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1706 }else {
1707 *ret = VARIANT_FALSE;
1711 IUnknown_Release(unk1);
1712 IUnknown_Release(unk2);
1713 return S_OK;
1716 static HRESULT interp_is(exec_ctx_t *ctx)
1718 IDispatch *l, *r;
1719 VARIANT v;
1720 HRESULT hres;
1722 TRACE("\n");
1724 hres = stack_pop_disp(ctx, &r);
1725 if(FAILED(hres))
1726 return hres;
1728 hres = stack_pop_disp(ctx, &l);
1729 if(SUCCEEDED(hres)) {
1730 V_VT(&v) = VT_BOOL;
1731 hres = disp_cmp(l, r, &V_BOOL(&v));
1732 if(l)
1733 IDispatch_Release(l);
1735 if(r)
1736 IDispatch_Release(r);
1737 if(FAILED(hres))
1738 return hres;
1740 return stack_push(ctx, &v);
1743 static HRESULT interp_concat(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 = VarCat(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_add(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 = VarAdd(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_sub(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 = VarSub(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_mod(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 = VarMod(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_idiv(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 = VarIdiv(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_div(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 = VarDiv(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_mul(exec_ctx_t *ctx)
1889 variant_val_t r, l;
1890 VARIANT v;
1891 HRESULT hres;
1893 TRACE("\n");
1895 hres = stack_pop_val(ctx, &r);
1896 if(FAILED(hres))
1897 return hres;
1899 hres = stack_pop_val(ctx, &l);
1900 if(SUCCEEDED(hres)) {
1901 hres = VarMul(l.v, r.v, &v);
1902 release_val(&l);
1904 release_val(&r);
1905 if(FAILED(hres))
1906 return hres;
1908 return stack_push(ctx, &v);
1911 static HRESULT interp_exp(exec_ctx_t *ctx)
1913 variant_val_t r, l;
1914 VARIANT v;
1915 HRESULT hres;
1917 TRACE("\n");
1919 hres = stack_pop_val(ctx, &r);
1920 if(FAILED(hres))
1921 return hres;
1923 hres = stack_pop_val(ctx, &l);
1924 if(SUCCEEDED(hres)) {
1925 hres = VarPow(l.v, r.v, &v);
1926 release_val(&l);
1928 release_val(&r);
1929 if(FAILED(hres))
1930 return hres;
1932 return stack_push(ctx, &v);
1935 static HRESULT interp_neg(exec_ctx_t *ctx)
1937 variant_val_t val;
1938 VARIANT v;
1939 HRESULT hres;
1941 hres = stack_pop_val(ctx, &val);
1942 if(FAILED(hres))
1943 return hres;
1945 hres = VarNeg(val.v, &v);
1946 release_val(&val);
1947 if(FAILED(hres))
1948 return hres;
1950 return stack_push(ctx, &v);
1953 static HRESULT interp_incc(exec_ctx_t *ctx)
1955 const BSTR ident = ctx->instr->arg1.bstr;
1956 VARIANT v;
1957 ref_t ref;
1958 HRESULT hres;
1960 TRACE("\n");
1962 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1963 if(FAILED(hres))
1964 return hres;
1966 if(ref.type != REF_VAR) {
1967 FIXME("ref.type is not REF_VAR\n");
1968 return E_FAIL;
1971 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1972 if(FAILED(hres))
1973 return hres;
1975 VariantClear(ref.u.v);
1976 *ref.u.v = v;
1977 return S_OK;
1980 static HRESULT interp_catch(exec_ctx_t *ctx)
1982 /* Nothing to do here, the OP is for unwinding only. */
1983 return S_OK;
1986 static const instr_func_t op_funcs[] = {
1987 #define X(x,n,a,b) interp_ ## x,
1988 OP_LIST
1989 #undef X
1992 static const unsigned op_move[] = {
1993 #define X(x,n,a,b) n,
1994 OP_LIST
1995 #undef X
1998 void release_dynamic_vars(dynamic_var_t *var)
2000 while(var) {
2001 VariantClear(&var->v);
2002 var = var->next;
2006 static void release_exec(exec_ctx_t *ctx)
2008 unsigned i;
2010 VariantClear(&ctx->ret_val);
2011 release_dynamic_vars(ctx->dynamic_vars);
2013 if(ctx->this_obj)
2014 IDispatch_Release(ctx->this_obj);
2016 if(ctx->args) {
2017 for(i=0; i < ctx->func->arg_cnt; i++)
2018 VariantClear(ctx->args+i);
2021 if(ctx->vars) {
2022 for(i=0; i < ctx->func->var_cnt; i++)
2023 VariantClear(ctx->vars+i);
2026 if(ctx->arrays) {
2027 for(i=0; i < ctx->func->var_cnt; i++) {
2028 if(ctx->arrays[i])
2029 SafeArrayDestroy(ctx->arrays[i]);
2031 heap_free(ctx->arrays);
2034 heap_pool_free(&ctx->heap);
2035 heap_free(ctx->args);
2036 heap_free(ctx->vars);
2037 heap_free(ctx->stack);
2040 HRESULT exec_script(script_ctx_t *ctx, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2042 exec_ctx_t exec = {func->code_ctx};
2043 vbsop_t op;
2044 HRESULT hres = S_OK;
2046 exec.code = func->code_ctx;
2048 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2049 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2050 return E_FAIL;
2053 heap_pool_init(&exec.heap);
2055 if(func->arg_cnt) {
2056 VARIANT *v;
2057 unsigned i;
2059 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2060 if(!exec.args) {
2061 release_exec(&exec);
2062 return E_OUTOFMEMORY;
2065 for(i=0; i < func->arg_cnt; i++) {
2066 v = get_arg(dp, i);
2067 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2068 if(func->args[i].by_ref)
2069 exec.args[i] = *v;
2070 else
2071 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2072 }else {
2073 hres = VariantCopyInd(exec.args+i, v);
2075 if(FAILED(hres)) {
2076 release_exec(&exec);
2077 return hres;
2080 }else {
2081 exec.args = NULL;
2084 if(func->var_cnt) {
2085 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2086 if(!exec.vars) {
2087 release_exec(&exec);
2088 return E_OUTOFMEMORY;
2090 }else {
2091 exec.vars = NULL;
2094 exec.stack_size = 16;
2095 exec.top = 0;
2096 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2097 if(!exec.stack) {
2098 release_exec(&exec);
2099 return E_OUTOFMEMORY;
2102 if(vbthis) {
2103 exec.this_obj = (IDispatch*)&vbthis->IDispatchEx_iface;
2104 exec.vbthis = vbthis;
2105 }else if (ctx->host_global) {
2106 exec.this_obj = ctx->host_global;
2107 }else {
2108 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2110 IDispatch_AddRef(exec.this_obj);
2112 exec.instr = exec.code->instrs + func->code_off;
2113 exec.script = ctx;
2114 exec.func = func;
2116 while(exec.instr) {
2117 op = exec.instr->op;
2118 hres = op_funcs[op](&exec);
2119 if(FAILED(hres)) {
2120 ctx->err_number = hres = map_hres(hres);
2122 if(exec.resume_next) {
2123 unsigned stack_off;
2125 WARN("Failed %08x in resume next mode\n", hres);
2128 * Unwinding here is simple. We need to find the next OP_catch, which contains
2129 * information about expected stack size and jump offset on error. Generated
2130 * bytecode needs to guarantee, that simple jump and stack adjustment will
2131 * guarantee proper execution continuation.
2133 while((++exec.instr)->op != OP_catch);
2135 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2137 stack_off = exec.instr->arg2.uint;
2138 instr_jmp(&exec, exec.instr->arg1.uint);
2140 if(exec.top > stack_off) {
2141 stack_popn(&exec, exec.top-stack_off);
2142 }else if(exec.top < stack_off) {
2143 VARIANT v;
2145 V_VT(&v) = VT_EMPTY;
2146 while(exec.top < stack_off) {
2147 hres = stack_push(&exec, &v);
2148 if(FAILED(hres))
2149 break;
2153 continue;
2154 }else {
2155 WARN("Failed %08x\n", hres);
2156 stack_popn(&exec, exec.top);
2157 break;
2161 exec.instr += op_move[op];
2164 assert(!exec.top);
2165 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2166 assert(V_VT(&exec.ret_val) == VT_EMPTY);
2168 if(SUCCEEDED(hres) && res) {
2169 *res = exec.ret_val;
2170 V_VT(&exec.ret_val) = VT_EMPTY;
2173 release_exec(&exec);
2174 return hres;