Release 1.9.16.
[wine.git] / dlls / vbscript / interp.c
blobbc348604695effd15161e796d4e1f913ffd3db53
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 **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 cell_off = 0, dim_size = 1, i;
485 unsigned argc = arg_cnt(dp);
486 VARIANT *data;
487 LONG idx;
488 HRESULT hres;
490 if(!array) {
491 FIXME("NULL array\n");
492 return E_FAIL;
495 if(array->cDims != argc) {
496 FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
497 return E_FAIL;
500 for(i=0; i < argc; i++) {
501 hres = to_int(get_arg(dp, i), &idx);
502 if(FAILED(hres))
503 return hres;
505 idx -= array->rgsabound[i].lLbound;
506 if(idx >= array->rgsabound[i].cElements) {
507 FIXME("out of bound element %d in dim %d of size %d\n", idx, i+1, array->rgsabound[i].cElements);
508 return E_FAIL;
511 cell_off += idx*dim_size;
512 dim_size *= array->rgsabound[i].cElements;
515 hres = SafeArrayAccessData(array, (void**)&data);
516 if(FAILED(hres))
517 return hres;
519 *ret = data+cell_off;
521 SafeArrayUnaccessData(array);
522 return S_OK;
525 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
527 BSTR identifier = ctx->instr->arg1.bstr;
528 const unsigned arg_cnt = ctx->instr->arg2.uint;
529 DISPPARAMS dp;
530 ref_t ref;
531 HRESULT hres;
533 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
534 if(FAILED(hres))
535 return hres;
537 switch(ref.type) {
538 case REF_VAR:
539 case REF_CONST: {
540 VARIANT *v;
542 if(!res) {
543 FIXME("REF_VAR no res\n");
544 return E_NOTIMPL;
547 v = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
549 if(arg_cnt) {
550 SAFEARRAY *array = NULL;
552 switch(V_VT(v)) {
553 case VT_ARRAY|VT_BYREF|VT_VARIANT:
554 array = *V_ARRAYREF(ref.u.v);
555 break;
556 case VT_ARRAY|VT_VARIANT:
557 array = V_ARRAY(ref.u.v);
558 break;
559 case VT_DISPATCH:
560 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
561 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_VALUE, &dp, res);
562 if(FAILED(hres))
563 return hres;
564 break;
565 default:
566 FIXME("arguments not implemented\n");
567 return E_NOTIMPL;
570 if(!array)
571 break;
573 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
574 hres = array_access(ctx, array, &dp, &v);
575 if(FAILED(hres))
576 return hres;
579 V_VT(res) = VT_BYREF|VT_VARIANT;
580 V_BYREF(res) = v;
581 break;
583 case REF_DISP:
584 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
585 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
586 if(FAILED(hres))
587 return hres;
588 break;
589 case REF_FUNC:
590 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
591 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
592 if(FAILED(hres))
593 return hres;
594 break;
595 case REF_OBJ:
596 if(arg_cnt) {
597 FIXME("arguments on object\n");
598 return E_NOTIMPL;
601 if(res) {
602 IDispatch_AddRef(ref.u.obj);
603 V_VT(res) = VT_DISPATCH;
604 V_DISPATCH(res) = ref.u.obj;
606 break;
607 case REF_NONE:
608 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
609 VARIANT *new;
610 hres = add_dynamic_var(ctx, identifier, FALSE, &new);
611 if(FAILED(hres))
612 return hres;
613 V_VT(res) = VT_BYREF|VT_VARIANT;
614 V_BYREF(res) = new;
615 break;
617 FIXME("%s not found\n", debugstr_w(identifier));
618 return DISP_E_UNKNOWNNAME;
621 stack_popn(ctx, arg_cnt);
622 return S_OK;
625 static HRESULT interp_icall(exec_ctx_t *ctx)
627 VARIANT v;
628 HRESULT hres;
630 TRACE("\n");
632 hres = do_icall(ctx, &v);
633 if(FAILED(hres))
634 return hres;
636 return stack_push(ctx, &v);
639 static HRESULT interp_icallv(exec_ctx_t *ctx)
641 TRACE("\n");
642 return do_icall(ctx, NULL);
645 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
647 const BSTR identifier = ctx->instr->arg1.bstr;
648 const unsigned arg_cnt = ctx->instr->arg2.uint;
649 IDispatch *obj;
650 DISPPARAMS dp;
651 DISPID id;
652 HRESULT hres;
654 hres = stack_pop_disp(ctx, &obj);
655 if(FAILED(hres))
656 return hres;
658 if(!obj) {
659 FIXME("NULL obj\n");
660 return E_FAIL;
663 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
665 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
666 if(SUCCEEDED(hres))
667 hres = disp_call(ctx->script, obj, id, &dp, res);
668 IDispatch_Release(obj);
669 if(FAILED(hres))
670 return hres;
672 stack_popn(ctx, arg_cnt);
673 return S_OK;
676 static HRESULT interp_mcall(exec_ctx_t *ctx)
678 VARIANT res;
679 HRESULT hres;
681 TRACE("\n");
683 hres = do_mcall(ctx, &res);
684 if(FAILED(hres))
685 return hres;
687 return stack_push(ctx, &res);
690 static HRESULT interp_mcallv(exec_ctx_t *ctx)
692 TRACE("\n");
694 return do_mcall(ctx, NULL);
697 static HRESULT assign_value(exec_ctx_t *ctx, VARIANT *dst, VARIANT *src, WORD flags)
699 HRESULT hres;
701 hres = VariantCopyInd(dst, src);
702 if(FAILED(hres))
703 return hres;
705 if(V_VT(dst) == VT_DISPATCH && !(flags & DISPATCH_PROPERTYPUTREF)) {
706 VARIANT value;
708 hres = get_disp_value(ctx->script, V_DISPATCH(dst), &value);
709 IDispatch_Release(V_DISPATCH(dst));
710 if(FAILED(hres))
711 return hres;
713 *dst = value;
716 return S_OK;
719 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, WORD flags, DISPPARAMS *dp)
721 ref_t ref;
722 HRESULT hres;
724 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
725 if(FAILED(hres))
726 return hres;
728 switch(ref.type) {
729 case REF_VAR: {
730 VARIANT *v = ref.u.v;
732 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
733 v = V_VARIANTREF(v);
735 if(arg_cnt(dp)) {
736 SAFEARRAY *array;
738 if(!(V_VT(v) & VT_ARRAY)) {
739 FIXME("array assign on type %d\n", V_VT(v));
740 return E_FAIL;
743 switch(V_VT(v)) {
744 case VT_ARRAY|VT_BYREF|VT_VARIANT:
745 array = *V_ARRAYREF(v);
746 break;
747 case VT_ARRAY|VT_VARIANT:
748 array = V_ARRAY(v);
749 break;
750 default:
751 FIXME("Unsupported array type %x\n", V_VT(v));
752 return E_NOTIMPL;
755 if(!array) {
756 FIXME("null array\n");
757 return E_FAIL;
760 hres = array_access(ctx, array, dp, &v);
761 if(FAILED(hres))
762 return hres;
763 }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
764 FIXME("non-array assign\n");
765 return E_NOTIMPL;
768 hres = assign_value(ctx, v, dp->rgvarg, flags);
769 break;
771 case REF_DISP:
772 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, flags, dp);
773 break;
774 case REF_FUNC:
775 FIXME("functions not implemented\n");
776 return E_NOTIMPL;
777 case REF_OBJ:
778 FIXME("REF_OBJ\n");
779 return E_NOTIMPL;
780 case REF_CONST:
781 FIXME("REF_CONST\n");
782 return E_NOTIMPL;
783 case REF_NONE:
784 if(ctx->func->code_ctx->option_explicit) {
785 FIXME("throw exception\n");
786 hres = E_FAIL;
787 }else {
788 VARIANT *new_var;
790 if(arg_cnt(dp)) {
791 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
792 return E_NOTIMPL;
795 TRACE("creating variable %s\n", debugstr_w(name));
796 hres = add_dynamic_var(ctx, name, FALSE, &new_var);
797 if(SUCCEEDED(hres))
798 hres = assign_value(ctx, new_var, dp->rgvarg, flags);
802 return hres;
805 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
807 const BSTR arg = ctx->instr->arg1.bstr;
808 const unsigned arg_cnt = ctx->instr->arg2.uint;
809 DISPPARAMS dp;
810 HRESULT hres;
812 TRACE("%s\n", debugstr_w(arg));
814 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
815 hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUT, &dp);
816 if(FAILED(hres))
817 return hres;
819 stack_popn(ctx, arg_cnt+1);
820 return S_OK;
823 static HRESULT interp_set_ident(exec_ctx_t *ctx)
825 const BSTR arg = ctx->instr->arg1.bstr;
826 const unsigned arg_cnt = ctx->instr->arg2.uint;
827 DISPPARAMS dp;
828 HRESULT hres;
830 TRACE("%s\n", debugstr_w(arg));
832 if(arg_cnt) {
833 FIXME("arguments not supported\n");
834 return E_NOTIMPL;
837 hres = stack_assume_disp(ctx, 0, NULL);
838 if(FAILED(hres))
839 return hres;
841 vbstack_to_dp(ctx, 0, TRUE, &dp);
842 hres = assign_ident(ctx, ctx->instr->arg1.bstr, DISPATCH_PROPERTYPUTREF, &dp);
843 if(FAILED(hres))
844 return hres;
846 stack_popn(ctx, 1);
847 return S_OK;
850 static HRESULT interp_assign_member(exec_ctx_t *ctx)
852 BSTR identifier = ctx->instr->arg1.bstr;
853 const unsigned arg_cnt = ctx->instr->arg2.uint;
854 IDispatch *obj;
855 DISPPARAMS dp;
856 DISPID id;
857 HRESULT hres;
859 TRACE("%s\n", debugstr_w(identifier));
861 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
862 if(FAILED(hres))
863 return hres;
865 if(!obj) {
866 FIXME("NULL obj\n");
867 return E_FAIL;
870 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
871 if(SUCCEEDED(hres)) {
872 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
873 hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUT, &dp);
875 if(FAILED(hres))
876 return hres;
878 stack_popn(ctx, arg_cnt+2);
879 return S_OK;
882 static HRESULT interp_set_member(exec_ctx_t *ctx)
884 BSTR identifier = ctx->instr->arg1.bstr;
885 const unsigned arg_cnt = ctx->instr->arg2.uint;
886 IDispatch *obj;
887 DISPPARAMS dp;
888 DISPID id;
889 HRESULT hres;
891 TRACE("%s\n", debugstr_w(identifier));
893 if(arg_cnt) {
894 FIXME("arguments not supported\n");
895 return E_NOTIMPL;
898 hres = stack_assume_disp(ctx, 1, &obj);
899 if(FAILED(hres))
900 return hres;
902 if(!obj) {
903 FIXME("NULL obj\n");
904 return E_FAIL;
907 hres = stack_assume_disp(ctx, 0, NULL);
908 if(FAILED(hres))
909 return hres;
911 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
912 if(SUCCEEDED(hres)) {
913 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
914 hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUTREF, &dp);
916 if(FAILED(hres))
917 return hres;
919 stack_popn(ctx, 2);
920 return S_OK;
923 static HRESULT interp_const(exec_ctx_t *ctx)
925 BSTR arg = ctx->instr->arg1.bstr;
926 VARIANT *v;
927 ref_t ref;
928 HRESULT hres;
930 TRACE("%s\n", debugstr_w(arg));
932 assert(ctx->func->type == FUNC_GLOBAL);
934 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
935 if(FAILED(hres))
936 return hres;
938 if(ref.type != REF_NONE) {
939 FIXME("%s already defined\n", debugstr_w(arg));
940 return E_FAIL;
943 hres = stack_assume_val(ctx, 0);
944 if(FAILED(hres))
945 return hres;
947 hres = add_dynamic_var(ctx, arg, TRUE, &v);
948 if(FAILED(hres))
949 return hres;
951 *v = *stack_pop(ctx);
952 return S_OK;
955 static HRESULT interp_val(exec_ctx_t *ctx)
957 variant_val_t val;
958 VARIANT v;
959 HRESULT hres;
961 TRACE("\n");
963 hres = stack_pop_val(ctx, &val);
964 if(FAILED(hres))
965 return hres;
967 if(!val.owned) {
968 V_VT(&v) = VT_EMPTY;
969 hres = VariantCopy(&v, val.v);
970 if(FAILED(hres))
971 return hres;
974 return stack_push(ctx, val.owned ? val.v : &v);
977 static HRESULT interp_pop(exec_ctx_t *ctx)
979 const unsigned n = ctx->instr->arg1.uint;
981 TRACE("%u\n", n);
983 stack_popn(ctx, n);
984 return S_OK;
987 static HRESULT interp_new(exec_ctx_t *ctx)
989 const WCHAR *arg = ctx->instr->arg1.bstr;
990 class_desc_t *class_desc;
991 vbdisp_t *obj;
992 VARIANT v;
993 HRESULT hres;
995 static const WCHAR regexpW[] = {'r','e','g','e','x','p',0};
997 TRACE("%s\n", debugstr_w(arg));
999 if(!strcmpiW(arg, regexpW)) {
1000 V_VT(&v) = VT_DISPATCH;
1001 hres = create_regexp(&V_DISPATCH(&v));
1002 if(FAILED(hres))
1003 return hres;
1005 return stack_push(ctx, &v);
1008 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
1009 if(!strcmpiW(class_desc->name, arg))
1010 break;
1012 if(!class_desc) {
1013 FIXME("Class %s not found\n", debugstr_w(arg));
1014 return E_FAIL;
1017 hres = create_vbdisp(class_desc, &obj);
1018 if(FAILED(hres))
1019 return hres;
1021 V_VT(&v) = VT_DISPATCH;
1022 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
1023 return stack_push(ctx, &v);
1026 static HRESULT interp_dim(exec_ctx_t *ctx)
1028 const BSTR ident = ctx->instr->arg1.bstr;
1029 const unsigned array_id = ctx->instr->arg2.uint;
1030 const array_desc_t *array_desc;
1031 ref_t ref;
1032 HRESULT hres;
1034 TRACE("%s\n", debugstr_w(ident));
1036 assert(array_id < ctx->func->array_cnt);
1037 if(!ctx->arrays) {
1038 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
1039 if(!ctx->arrays)
1040 return E_OUTOFMEMORY;
1043 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1044 if(FAILED(hres)) {
1045 FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
1046 return hres;
1049 if(ref.type != REF_VAR) {
1050 FIXME("got ref.type = %d\n", ref.type);
1051 return E_FAIL;
1054 if(ctx->arrays[array_id]) {
1055 FIXME("Array already initialized\n");
1056 return E_FAIL;
1059 array_desc = ctx->func->array_descs + array_id;
1060 if(array_desc->dim_cnt) {
1061 ctx->arrays[array_id] = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1062 if(!ctx->arrays[array_id])
1063 return E_OUTOFMEMORY;
1066 V_VT(ref.u.v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1067 V_ARRAYREF(ref.u.v) = ctx->arrays+array_id;
1068 return S_OK;
1071 static HRESULT interp_step(exec_ctx_t *ctx)
1073 const BSTR ident = ctx->instr->arg2.bstr;
1074 BOOL gteq_zero;
1075 VARIANT zero;
1076 ref_t ref;
1077 HRESULT hres;
1079 TRACE("%s\n", debugstr_w(ident));
1081 V_VT(&zero) = VT_I2;
1082 V_I2(&zero) = 0;
1083 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1084 if(FAILED(hres))
1085 return hres;
1087 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1089 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1090 if(FAILED(hres))
1091 return hres;
1093 if(ref.type != REF_VAR) {
1094 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1095 return E_FAIL;
1098 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1099 if(FAILED(hres))
1100 return hres;
1102 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1103 ctx->instr++;
1104 }else {
1105 stack_popn(ctx, 2);
1106 instr_jmp(ctx, ctx->instr->arg1.uint);
1108 return S_OK;
1111 static HRESULT interp_newenum(exec_ctx_t *ctx)
1113 variant_val_t v;
1114 VARIANT *r;
1115 HRESULT hres;
1117 TRACE("\n");
1119 stack_pop_deref(ctx, &v);
1120 assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
1121 r = stack_top(ctx, 0);
1123 switch(V_VT(v.v)) {
1124 case VT_DISPATCH|VT_BYREF:
1125 case VT_DISPATCH: {
1126 IEnumVARIANT *iter;
1127 DISPPARAMS dp = {0};
1128 VARIANT iterv;
1130 hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
1131 release_val(&v);
1132 if(FAILED(hres))
1133 return hres;
1135 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1136 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1137 VariantClear(&iterv);
1138 return hres;
1141 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1142 IUnknown_Release(V_UNKNOWN(&iterv));
1143 if(FAILED(hres)) {
1144 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
1145 return hres;
1148 V_VT(r) = VT_UNKNOWN;
1149 V_UNKNOWN(r) = (IUnknown*)iter;
1150 break;
1152 default:
1153 FIXME("Unsupported for %s\n", debugstr_variant(v.v));
1154 release_val(&v);
1155 return E_NOTIMPL;
1158 return S_OK;
1161 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1163 const unsigned loop_end = ctx->instr->arg1.uint;
1164 const BSTR ident = ctx->instr->arg2.bstr;
1165 VARIANT v;
1166 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1167 IEnumVARIANT *iter;
1168 BOOL do_continue;
1169 HRESULT hres;
1171 TRACE("\n");
1173 if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
1174 FIXME("uninitialized\n");
1175 return E_FAIL;
1178 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1179 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1181 V_VT(&v) = VT_EMPTY;
1182 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1183 if(FAILED(hres))
1184 return hres;
1186 do_continue = hres == S_OK;
1187 hres = assign_ident(ctx, ident, DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &dp);
1188 VariantClear(&v);
1189 if(FAILED(hres))
1190 return hres;
1192 if(do_continue) {
1193 ctx->instr++;
1194 }else {
1195 stack_pop(ctx);
1196 instr_jmp(ctx, loop_end);
1198 return S_OK;
1201 static HRESULT interp_jmp(exec_ctx_t *ctx)
1203 const unsigned arg = ctx->instr->arg1.uint;
1205 TRACE("%u\n", arg);
1207 instr_jmp(ctx, arg);
1208 return S_OK;
1211 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1213 const unsigned arg = ctx->instr->arg1.uint;
1214 HRESULT hres;
1215 BOOL b;
1217 TRACE("%u\n", arg);
1219 hres = stack_pop_bool(ctx, &b);
1220 if(FAILED(hres))
1221 return hres;
1223 if(b)
1224 ctx->instr++;
1225 else
1226 instr_jmp(ctx, ctx->instr->arg1.uint);
1227 return S_OK;
1230 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1232 const unsigned arg = ctx->instr->arg1.uint;
1233 HRESULT hres;
1234 BOOL b;
1236 TRACE("%u\n", arg);
1238 hres = stack_pop_bool(ctx, &b);
1239 if(FAILED(hres))
1240 return hres;
1242 if(b)
1243 instr_jmp(ctx, ctx->instr->arg1.uint);
1244 else
1245 ctx->instr++;
1246 return S_OK;
1249 static HRESULT interp_ret(exec_ctx_t *ctx)
1251 TRACE("\n");
1253 ctx->instr = NULL;
1254 return S_OK;
1257 static HRESULT interp_stop(exec_ctx_t *ctx)
1259 WARN("\n");
1261 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1262 return S_OK;
1265 static HRESULT interp_me(exec_ctx_t *ctx)
1267 VARIANT v;
1269 TRACE("\n");
1271 IDispatch_AddRef(ctx->this_obj);
1272 V_VT(&v) = VT_DISPATCH;
1273 V_DISPATCH(&v) = ctx->this_obj;
1274 return stack_push(ctx, &v);
1277 static HRESULT interp_bool(exec_ctx_t *ctx)
1279 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1280 VARIANT v;
1282 TRACE("%s\n", arg ? "true" : "false");
1284 V_VT(&v) = VT_BOOL;
1285 V_BOOL(&v) = arg;
1286 return stack_push(ctx, &v);
1289 static HRESULT interp_errmode(exec_ctx_t *ctx)
1291 const int err_mode = ctx->instr->arg1.uint;
1293 TRACE("%d\n", err_mode);
1295 ctx->resume_next = err_mode;
1296 ctx->script->err_number = S_OK;
1297 return S_OK;
1300 static HRESULT interp_string(exec_ctx_t *ctx)
1302 VARIANT v;
1304 TRACE("\n");
1306 V_VT(&v) = VT_BSTR;
1307 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1308 if(!V_BSTR(&v))
1309 return E_OUTOFMEMORY;
1311 return stack_push(ctx, &v);
1314 static HRESULT interp_long(exec_ctx_t *ctx)
1316 const LONG arg = ctx->instr->arg1.lng;
1317 VARIANT v;
1319 TRACE("%d\n", arg);
1321 V_VT(&v) = VT_I4;
1322 V_I4(&v) = arg;
1323 return stack_push(ctx, &v);
1326 static HRESULT interp_short(exec_ctx_t *ctx)
1328 const LONG arg = ctx->instr->arg1.lng;
1329 VARIANT v;
1331 TRACE("%d\n", arg);
1333 V_VT(&v) = VT_I2;
1334 V_I2(&v) = arg;
1335 return stack_push(ctx, &v);
1338 static HRESULT interp_double(exec_ctx_t *ctx)
1340 const DOUBLE *arg = ctx->instr->arg1.dbl;
1341 VARIANT v;
1343 TRACE("%lf\n", *arg);
1345 V_VT(&v) = VT_R8;
1346 V_R8(&v) = *arg;
1347 return stack_push(ctx, &v);
1350 static HRESULT interp_empty(exec_ctx_t *ctx)
1352 VARIANT v;
1354 TRACE("\n");
1356 V_VT(&v) = VT_EMPTY;
1357 return stack_push(ctx, &v);
1360 static HRESULT interp_null(exec_ctx_t *ctx)
1362 TRACE("\n");
1363 return stack_push_null(ctx);
1366 static HRESULT interp_nothing(exec_ctx_t *ctx)
1368 VARIANT v;
1370 TRACE("\n");
1372 V_VT(&v) = VT_DISPATCH;
1373 V_DISPATCH(&v) = NULL;
1374 return stack_push(ctx, &v);
1377 static HRESULT interp_hres(exec_ctx_t *ctx)
1379 const unsigned arg = ctx->instr->arg1.uint;
1380 VARIANT v;
1382 TRACE("%d\n", arg);
1384 V_VT(&v) = VT_ERROR;
1385 V_ERROR(&v) = arg;
1386 return stack_push(ctx, &v);
1389 static HRESULT interp_not(exec_ctx_t *ctx)
1391 variant_val_t val;
1392 VARIANT v;
1393 HRESULT hres;
1395 TRACE("\n");
1397 hres = stack_pop_val(ctx, &val);
1398 if(FAILED(hres))
1399 return hres;
1401 hres = VarNot(val.v, &v);
1402 release_val(&val);
1403 if(FAILED(hres))
1404 return hres;
1406 return stack_push(ctx, &v);
1409 static HRESULT interp_and(exec_ctx_t *ctx)
1411 variant_val_t r, l;
1412 VARIANT v;
1413 HRESULT hres;
1415 TRACE("\n");
1417 hres = stack_pop_val(ctx, &r);
1418 if(FAILED(hres))
1419 return hres;
1421 hres = stack_pop_val(ctx, &l);
1422 if(SUCCEEDED(hres)) {
1423 hres = VarAnd(l.v, r.v, &v);
1424 release_val(&l);
1426 release_val(&r);
1427 if(FAILED(hres))
1428 return hres;
1430 return stack_push(ctx, &v);
1433 static HRESULT interp_or(exec_ctx_t *ctx)
1435 variant_val_t r, l;
1436 VARIANT v;
1437 HRESULT hres;
1439 TRACE("\n");
1441 hres = stack_pop_val(ctx, &r);
1442 if(FAILED(hres))
1443 return hres;
1445 hres = stack_pop_val(ctx, &l);
1446 if(SUCCEEDED(hres)) {
1447 hres = VarOr(l.v, r.v, &v);
1448 release_val(&l);
1450 release_val(&r);
1451 if(FAILED(hres))
1452 return hres;
1454 return stack_push(ctx, &v);
1457 static HRESULT interp_xor(exec_ctx_t *ctx)
1459 variant_val_t r, l;
1460 VARIANT v;
1461 HRESULT hres;
1463 TRACE("\n");
1465 hres = stack_pop_val(ctx, &r);
1466 if(FAILED(hres))
1467 return hres;
1469 hres = stack_pop_val(ctx, &l);
1470 if(SUCCEEDED(hres)) {
1471 hres = VarXor(l.v, r.v, &v);
1472 release_val(&l);
1474 release_val(&r);
1475 if(FAILED(hres))
1476 return hres;
1478 return stack_push(ctx, &v);
1481 static HRESULT interp_eqv(exec_ctx_t *ctx)
1483 variant_val_t r, l;
1484 VARIANT v;
1485 HRESULT hres;
1487 TRACE("\n");
1489 hres = stack_pop_val(ctx, &r);
1490 if(FAILED(hres))
1491 return hres;
1493 hres = stack_pop_val(ctx, &l);
1494 if(SUCCEEDED(hres)) {
1495 hres = VarEqv(l.v, r.v, &v);
1496 release_val(&l);
1498 release_val(&r);
1499 if(FAILED(hres))
1500 return hres;
1502 return stack_push(ctx, &v);
1505 static HRESULT interp_imp(exec_ctx_t *ctx)
1507 variant_val_t r, l;
1508 VARIANT v;
1509 HRESULT hres;
1511 TRACE("\n");
1513 hres = stack_pop_val(ctx, &r);
1514 if(FAILED(hres))
1515 return hres;
1517 hres = stack_pop_val(ctx, &l);
1518 if(SUCCEEDED(hres)) {
1519 hres = VarImp(l.v, r.v, &v);
1520 release_val(&l);
1522 release_val(&r);
1523 if(FAILED(hres))
1524 return hres;
1526 return stack_push(ctx, &v);
1529 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1531 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1533 /* FIXME: Fix comparing string to number */
1535 return VarCmp(l, r, ctx->script->lcid, 0);
1538 static HRESULT cmp_oper(exec_ctx_t *ctx)
1540 variant_val_t l, r;
1541 HRESULT hres;
1543 hres = stack_pop_val(ctx, &r);
1544 if(FAILED(hres))
1545 return hres;
1547 hres = stack_pop_val(ctx, &l);
1548 if(SUCCEEDED(hres)) {
1549 hres = var_cmp(ctx, l.v, r.v);
1550 release_val(&l);
1553 release_val(&r);
1554 return hres;
1557 static HRESULT interp_equal(exec_ctx_t *ctx)
1559 VARIANT v;
1560 HRESULT hres;
1562 TRACE("\n");
1564 hres = cmp_oper(ctx);
1565 if(FAILED(hres))
1566 return hres;
1567 if(hres == VARCMP_NULL)
1568 return stack_push_null(ctx);
1570 V_VT(&v) = VT_BOOL;
1571 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1572 return stack_push(ctx, &v);
1575 static HRESULT interp_nequal(exec_ctx_t *ctx)
1577 VARIANT v;
1578 HRESULT hres;
1580 TRACE("\n");
1582 hres = cmp_oper(ctx);
1583 if(FAILED(hres))
1584 return hres;
1585 if(hres == VARCMP_NULL)
1586 return stack_push_null(ctx);
1588 V_VT(&v) = VT_BOOL;
1589 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1590 return stack_push(ctx, &v);
1593 static HRESULT interp_gt(exec_ctx_t *ctx)
1595 VARIANT v;
1596 HRESULT hres;
1598 TRACE("\n");
1600 hres = cmp_oper(ctx);
1601 if(FAILED(hres))
1602 return hres;
1603 if(hres == VARCMP_NULL)
1604 return stack_push_null(ctx);
1606 V_VT(&v) = VT_BOOL;
1607 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1608 return stack_push(ctx, &v);
1611 static HRESULT interp_gteq(exec_ctx_t *ctx)
1613 VARIANT v;
1614 HRESULT hres;
1616 TRACE("\n");
1618 hres = cmp_oper(ctx);
1619 if(FAILED(hres))
1620 return hres;
1621 if(hres == VARCMP_NULL)
1622 return stack_push_null(ctx);
1624 V_VT(&v) = VT_BOOL;
1625 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1626 return stack_push(ctx, &v);
1629 static HRESULT interp_lt(exec_ctx_t *ctx)
1631 VARIANT v;
1632 HRESULT hres;
1634 TRACE("\n");
1636 hres = cmp_oper(ctx);
1637 if(FAILED(hres))
1638 return hres;
1639 if(hres == VARCMP_NULL)
1640 return stack_push_null(ctx);
1642 V_VT(&v) = VT_BOOL;
1643 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1644 return stack_push(ctx, &v);
1647 static HRESULT interp_lteq(exec_ctx_t *ctx)
1649 VARIANT v;
1650 HRESULT hres;
1652 TRACE("\n");
1654 hres = cmp_oper(ctx);
1655 if(FAILED(hres))
1656 return hres;
1657 if(hres == VARCMP_NULL)
1658 return stack_push_null(ctx);
1660 V_VT(&v) = VT_BOOL;
1661 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1662 return stack_push(ctx, &v);
1665 static HRESULT interp_case(exec_ctx_t *ctx)
1667 const unsigned arg = ctx->instr->arg1.uint;
1668 variant_val_t v;
1669 HRESULT hres;
1671 TRACE("%d\n", arg);
1673 hres = stack_pop_val(ctx, &v);
1674 if(FAILED(hres))
1675 return hres;
1677 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
1678 release_val(&v);
1679 if(FAILED(hres))
1680 return hres;
1682 if(hres == VARCMP_EQ) {
1683 stack_popn(ctx, 1);
1684 instr_jmp(ctx, arg);
1685 }else {
1686 ctx->instr++;
1689 return S_OK;
1692 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1694 IObjectIdentity *identity;
1695 IUnknown *unk1, *unk2;
1696 HRESULT hres;
1698 if(disp1 == disp2) {
1699 *ret = VARIANT_TRUE;
1700 return S_OK;
1703 if(!disp1 || !disp2) {
1704 *ret = VARIANT_FALSE;
1705 return S_OK;
1708 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1709 if(FAILED(hres))
1710 return hres;
1712 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1713 if(FAILED(hres)) {
1714 IUnknown_Release(unk1);
1715 return hres;
1718 if(unk1 == unk2) {
1719 *ret = VARIANT_TRUE;
1720 }else {
1721 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1722 if(SUCCEEDED(hres)) {
1723 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1724 IObjectIdentity_Release(identity);
1725 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1726 }else {
1727 *ret = VARIANT_FALSE;
1731 IUnknown_Release(unk1);
1732 IUnknown_Release(unk2);
1733 return S_OK;
1736 static HRESULT interp_is(exec_ctx_t *ctx)
1738 IDispatch *l, *r;
1739 VARIANT v;
1740 HRESULT hres;
1742 TRACE("\n");
1744 hres = stack_pop_disp(ctx, &r);
1745 if(FAILED(hres))
1746 return hres;
1748 hres = stack_pop_disp(ctx, &l);
1749 if(SUCCEEDED(hres)) {
1750 V_VT(&v) = VT_BOOL;
1751 hres = disp_cmp(l, r, &V_BOOL(&v));
1752 if(l)
1753 IDispatch_Release(l);
1755 if(r)
1756 IDispatch_Release(r);
1757 if(FAILED(hres))
1758 return hres;
1760 return stack_push(ctx, &v);
1763 static HRESULT interp_concat(exec_ctx_t *ctx)
1765 variant_val_t r, l;
1766 VARIANT v;
1767 HRESULT hres;
1769 TRACE("\n");
1771 hres = stack_pop_val(ctx, &r);
1772 if(FAILED(hres))
1773 return hres;
1775 hres = stack_pop_val(ctx, &l);
1776 if(SUCCEEDED(hres)) {
1777 hres = VarCat(l.v, r.v, &v);
1778 release_val(&l);
1780 release_val(&r);
1781 if(FAILED(hres))
1782 return hres;
1784 return stack_push(ctx, &v);
1787 static HRESULT interp_add(exec_ctx_t *ctx)
1789 variant_val_t r, l;
1790 VARIANT v;
1791 HRESULT hres;
1793 TRACE("\n");
1795 hres = stack_pop_val(ctx, &r);
1796 if(FAILED(hres))
1797 return hres;
1799 hres = stack_pop_val(ctx, &l);
1800 if(SUCCEEDED(hres)) {
1801 hres = VarAdd(l.v, r.v, &v);
1802 release_val(&l);
1804 release_val(&r);
1805 if(FAILED(hres))
1806 return hres;
1808 return stack_push(ctx, &v);
1811 static HRESULT interp_sub(exec_ctx_t *ctx)
1813 variant_val_t r, l;
1814 VARIANT v;
1815 HRESULT hres;
1817 TRACE("\n");
1819 hres = stack_pop_val(ctx, &r);
1820 if(FAILED(hres))
1821 return hres;
1823 hres = stack_pop_val(ctx, &l);
1824 if(SUCCEEDED(hres)) {
1825 hres = VarSub(l.v, r.v, &v);
1826 release_val(&l);
1828 release_val(&r);
1829 if(FAILED(hres))
1830 return hres;
1832 return stack_push(ctx, &v);
1835 static HRESULT interp_mod(exec_ctx_t *ctx)
1837 variant_val_t r, l;
1838 VARIANT v;
1839 HRESULT hres;
1841 TRACE("\n");
1843 hres = stack_pop_val(ctx, &r);
1844 if(FAILED(hres))
1845 return hres;
1847 hres = stack_pop_val(ctx, &l);
1848 if(SUCCEEDED(hres)) {
1849 hres = VarMod(l.v, r.v, &v);
1850 release_val(&l);
1852 release_val(&r);
1853 if(FAILED(hres))
1854 return hres;
1856 return stack_push(ctx, &v);
1859 static HRESULT interp_idiv(exec_ctx_t *ctx)
1861 variant_val_t r, l;
1862 VARIANT v;
1863 HRESULT hres;
1865 TRACE("\n");
1867 hres = stack_pop_val(ctx, &r);
1868 if(FAILED(hres))
1869 return hres;
1871 hres = stack_pop_val(ctx, &l);
1872 if(SUCCEEDED(hres)) {
1873 hres = VarIdiv(l.v, r.v, &v);
1874 release_val(&l);
1876 release_val(&r);
1877 if(FAILED(hres))
1878 return hres;
1880 return stack_push(ctx, &v);
1883 static HRESULT interp_div(exec_ctx_t *ctx)
1885 variant_val_t r, l;
1886 VARIANT v;
1887 HRESULT hres;
1889 TRACE("\n");
1891 hres = stack_pop_val(ctx, &r);
1892 if(FAILED(hres))
1893 return hres;
1895 hres = stack_pop_val(ctx, &l);
1896 if(SUCCEEDED(hres)) {
1897 hres = VarDiv(l.v, r.v, &v);
1898 release_val(&l);
1900 release_val(&r);
1901 if(FAILED(hres))
1902 return hres;
1904 return stack_push(ctx, &v);
1907 static HRESULT interp_mul(exec_ctx_t *ctx)
1909 variant_val_t r, l;
1910 VARIANT v;
1911 HRESULT hres;
1913 TRACE("\n");
1915 hres = stack_pop_val(ctx, &r);
1916 if(FAILED(hres))
1917 return hres;
1919 hres = stack_pop_val(ctx, &l);
1920 if(SUCCEEDED(hres)) {
1921 hres = VarMul(l.v, r.v, &v);
1922 release_val(&l);
1924 release_val(&r);
1925 if(FAILED(hres))
1926 return hres;
1928 return stack_push(ctx, &v);
1931 static HRESULT interp_exp(exec_ctx_t *ctx)
1933 variant_val_t r, l;
1934 VARIANT v;
1935 HRESULT hres;
1937 TRACE("\n");
1939 hres = stack_pop_val(ctx, &r);
1940 if(FAILED(hres))
1941 return hres;
1943 hres = stack_pop_val(ctx, &l);
1944 if(SUCCEEDED(hres)) {
1945 hres = VarPow(l.v, r.v, &v);
1946 release_val(&l);
1948 release_val(&r);
1949 if(FAILED(hres))
1950 return hres;
1952 return stack_push(ctx, &v);
1955 static HRESULT interp_neg(exec_ctx_t *ctx)
1957 variant_val_t val;
1958 VARIANT v;
1959 HRESULT hres;
1961 hres = stack_pop_val(ctx, &val);
1962 if(FAILED(hres))
1963 return hres;
1965 hres = VarNeg(val.v, &v);
1966 release_val(&val);
1967 if(FAILED(hres))
1968 return hres;
1970 return stack_push(ctx, &v);
1973 static HRESULT interp_incc(exec_ctx_t *ctx)
1975 const BSTR ident = ctx->instr->arg1.bstr;
1976 VARIANT v;
1977 ref_t ref;
1978 HRESULT hres;
1980 TRACE("\n");
1982 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1983 if(FAILED(hres))
1984 return hres;
1986 if(ref.type != REF_VAR) {
1987 FIXME("ref.type is not REF_VAR\n");
1988 return E_FAIL;
1991 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1992 if(FAILED(hres))
1993 return hres;
1995 VariantClear(ref.u.v);
1996 *ref.u.v = v;
1997 return S_OK;
2000 static HRESULT interp_catch(exec_ctx_t *ctx)
2002 /* Nothing to do here, the OP is for unwinding only. */
2003 return S_OK;
2006 static const instr_func_t op_funcs[] = {
2007 #define X(x,n,a,b) interp_ ## x,
2008 OP_LIST
2009 #undef X
2012 static const unsigned op_move[] = {
2013 #define X(x,n,a,b) n,
2014 OP_LIST
2015 #undef X
2018 void release_dynamic_vars(dynamic_var_t *var)
2020 while(var) {
2021 VariantClear(&var->v);
2022 var = var->next;
2026 static void release_exec(exec_ctx_t *ctx)
2028 unsigned i;
2030 VariantClear(&ctx->ret_val);
2031 release_dynamic_vars(ctx->dynamic_vars);
2033 if(ctx->this_obj)
2034 IDispatch_Release(ctx->this_obj);
2036 if(ctx->args) {
2037 for(i=0; i < ctx->func->arg_cnt; i++)
2038 VariantClear(ctx->args+i);
2041 if(ctx->vars) {
2042 for(i=0; i < ctx->func->var_cnt; i++)
2043 VariantClear(ctx->vars+i);
2046 if(ctx->arrays) {
2047 for(i=0; i < ctx->func->var_cnt; i++) {
2048 if(ctx->arrays[i])
2049 SafeArrayDestroy(ctx->arrays[i]);
2051 heap_free(ctx->arrays);
2054 heap_pool_free(&ctx->heap);
2055 heap_free(ctx->args);
2056 heap_free(ctx->vars);
2057 heap_free(ctx->stack);
2060 HRESULT exec_script(script_ctx_t *ctx, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2062 exec_ctx_t exec = {func->code_ctx};
2063 vbsop_t op;
2064 HRESULT hres = S_OK;
2066 exec.code = func->code_ctx;
2068 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2069 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2070 return E_FAIL;
2073 heap_pool_init(&exec.heap);
2075 if(func->arg_cnt) {
2076 VARIANT *v;
2077 unsigned i;
2079 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2080 if(!exec.args) {
2081 release_exec(&exec);
2082 return E_OUTOFMEMORY;
2085 for(i=0; i < func->arg_cnt; i++) {
2086 v = get_arg(dp, i);
2087 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2088 if(func->args[i].by_ref)
2089 exec.args[i] = *v;
2090 else
2091 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2092 }else {
2093 hres = VariantCopyInd(exec.args+i, v);
2095 if(FAILED(hres)) {
2096 release_exec(&exec);
2097 return hres;
2100 }else {
2101 exec.args = NULL;
2104 if(func->var_cnt) {
2105 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2106 if(!exec.vars) {
2107 release_exec(&exec);
2108 return E_OUTOFMEMORY;
2110 }else {
2111 exec.vars = NULL;
2114 exec.stack_size = 16;
2115 exec.top = 0;
2116 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2117 if(!exec.stack) {
2118 release_exec(&exec);
2119 return E_OUTOFMEMORY;
2122 if(vbthis) {
2123 exec.this_obj = (IDispatch*)&vbthis->IDispatchEx_iface;
2124 exec.vbthis = vbthis;
2125 }else if (ctx->host_global) {
2126 exec.this_obj = ctx->host_global;
2127 }else {
2128 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
2130 IDispatch_AddRef(exec.this_obj);
2132 exec.instr = exec.code->instrs + func->code_off;
2133 exec.script = ctx;
2134 exec.func = func;
2136 while(exec.instr) {
2137 op = exec.instr->op;
2138 hres = op_funcs[op](&exec);
2139 if(FAILED(hres)) {
2140 ctx->err_number = hres = map_hres(hres);
2142 if(exec.resume_next) {
2143 unsigned stack_off;
2145 WARN("Failed %08x in resume next mode\n", hres);
2148 * Unwinding here is simple. We need to find the next OP_catch, which contains
2149 * information about expected stack size and jump offset on error. Generated
2150 * bytecode needs to guarantee, that simple jump and stack adjustment will
2151 * guarantee proper execution continuation.
2153 while((++exec.instr)->op != OP_catch);
2155 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2157 stack_off = exec.instr->arg2.uint;
2158 instr_jmp(&exec, exec.instr->arg1.uint);
2160 if(exec.top > stack_off) {
2161 stack_popn(&exec, exec.top-stack_off);
2162 }else if(exec.top < stack_off) {
2163 VARIANT v;
2165 V_VT(&v) = VT_EMPTY;
2166 while(exec.top < stack_off) {
2167 hres = stack_push(&exec, &v);
2168 if(FAILED(hres))
2169 break;
2173 continue;
2174 }else {
2175 WARN("Failed %08x\n", hres);
2176 stack_popn(&exec, exec.top);
2177 break;
2181 exec.instr += op_move[op];
2184 assert(!exec.top);
2185 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
2186 assert(V_VT(&exec.ret_val) == VT_EMPTY);
2188 if(SUCCEEDED(hres) && res) {
2189 *res = exec.ret_val;
2190 V_VT(&exec.ret_val) = VT_EMPTY;
2193 release_exec(&exec);
2194 return hres;