kernel32/tests/pipe: Enable compilation with long types.
[wine.git] / dlls / vbscript / interp.c
blobc43b5383deeb5333985e7a9f02e856703622d340
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 vbdisp_t *vbthis;
36 VARIANT *args;
37 VARIANT *vars;
38 SAFEARRAY **arrays;
40 dynamic_var_t *dynamic_vars;
41 heap_pool_t heap;
43 BOOL resume_next;
45 unsigned stack_size;
46 unsigned top;
47 VARIANT *stack;
49 VARIANT ret_val;
50 } exec_ctx_t;
52 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
54 typedef enum {
55 REF_NONE,
56 REF_DISP,
57 REF_VAR,
58 REF_OBJ,
59 REF_CONST,
60 REF_FUNC
61 } ref_type_t;
63 typedef struct {
64 ref_type_t type;
65 union {
66 struct {
67 IDispatch *disp;
68 DISPID id;
69 } d;
70 VARIANT *v;
71 function_t *f;
72 IDispatch *obj;
73 } u;
74 } ref_t;
76 typedef struct {
77 VARIANT *v;
78 VARIANT store;
79 BOOL owned;
80 } variant_val_t;
82 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
84 while(var) {
85 if(!wcsicmp(var->name, name)) {
86 ref->type = var->is_const ? REF_CONST : REF_VAR;
87 ref->u.v = &var->v;
88 return TRUE;
91 var = var->next;
94 return FALSE;
97 static BOOL lookup_global_vars(ScriptDisp *script, const WCHAR *name, ref_t *ref)
99 dynamic_var_t **vars = script->global_vars;
100 size_t i, cnt = script->global_vars_cnt;
102 for(i = 0; i < cnt; i++) {
103 if(!wcsicmp(vars[i]->name, name)) {
104 ref->type = vars[i]->is_const ? REF_CONST : REF_VAR;
105 ref->u.v = &vars[i]->v;
106 return TRUE;
110 return FALSE;
113 static BOOL lookup_global_funcs(ScriptDisp *script, const WCHAR *name, ref_t *ref)
115 function_t **funcs = script->global_funcs;
116 size_t i, cnt = script->global_funcs_cnt;
118 for(i = 0; i < cnt; i++) {
119 if(!wcsicmp(funcs[i]->name, name)) {
120 ref->type = REF_FUNC;
121 ref->u.f = funcs[i];
122 return TRUE;
126 return FALSE;
129 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
131 ScriptDisp *script_obj = ctx->script->script_obj;
132 named_item_t *item;
133 unsigned i;
134 DISPID id;
135 HRESULT hres;
137 if(invoke_type != VBDISP_CALLGET
138 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET)
139 && !wcsicmp(name, ctx->func->name)) {
140 ref->type = REF_VAR;
141 ref->u.v = &ctx->ret_val;
142 return S_OK;
145 if(ctx->func->type != FUNC_GLOBAL) {
146 for(i=0; i < ctx->func->var_cnt; i++) {
147 if(!wcsicmp(ctx->func->vars[i].name, name)) {
148 ref->type = REF_VAR;
149 ref->u.v = ctx->vars+i;
150 return S_OK;
154 for(i=0; i < ctx->func->arg_cnt; i++) {
155 if(!wcsicmp(ctx->func->args[i].name, name)) {
156 ref->type = REF_VAR;
157 ref->u.v = ctx->args+i;
158 return S_OK;
162 if(lookup_dynamic_vars(ctx->dynamic_vars, name, ref))
163 return S_OK;
165 if(ctx->vbthis) {
166 /* FIXME: Bind such identifier while generating bytecode. */
167 for(i=0; i < ctx->vbthis->desc->prop_cnt; i++) {
168 if(!wcsicmp(ctx->vbthis->desc->props[i].name, name)) {
169 ref->type = REF_VAR;
170 ref->u.v = ctx->vbthis->props+i;
171 return S_OK;
175 hres = vbdisp_get_id(ctx->vbthis, name, invoke_type, TRUE, &id);
176 if(SUCCEEDED(hres)) {
177 ref->type = REF_DISP;
178 ref->u.d.disp = (IDispatch*)&ctx->vbthis->IDispatchEx_iface;
179 ref->u.d.id = id;
180 return S_OK;
185 if(ctx->code->named_item) {
186 if(lookup_global_vars(ctx->code->named_item->script_obj, name, ref))
187 return S_OK;
188 if(lookup_global_funcs(ctx->code->named_item->script_obj, name, ref))
189 return S_OK;
192 if(ctx->func->code_ctx->named_item && ctx->func->code_ctx->named_item->disp &&
193 !(ctx->func->code_ctx->named_item->flags & SCRIPTITEM_CODEONLY))
195 hres = disp_get_id(ctx->func->code_ctx->named_item->disp, name, invoke_type, TRUE, &id);
196 if(SUCCEEDED(hres)) {
197 ref->type = REF_DISP;
198 ref->u.d.disp = ctx->func->code_ctx->named_item->disp;
199 ref->u.d.id = id;
200 return S_OK;
204 if(lookup_global_vars(script_obj, name, ref))
205 return S_OK;
206 if(lookup_global_funcs(script_obj, name, ref))
207 return S_OK;
209 hres = get_builtin_id(ctx->script->global_obj, name, &id);
210 if(SUCCEEDED(hres)) {
211 ref->type = REF_DISP;
212 ref->u.d.disp = &ctx->script->global_obj->IDispatch_iface;
213 ref->u.d.id = id;
214 return S_OK;
217 item = lookup_named_item(ctx->script, name, SCRIPTITEM_ISVISIBLE);
218 if(item && item->disp) {
219 ref->type = REF_OBJ;
220 ref->u.obj = item->disp;
221 return S_OK;
224 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
225 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
226 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
227 if(SUCCEEDED(hres)) {
228 ref->type = REF_DISP;
229 ref->u.d.disp = item->disp;
230 ref->u.d.id = id;
231 return S_OK;
236 ref->type = REF_NONE;
237 return S_OK;
240 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
241 BOOL is_const, VARIANT **out_var)
243 ScriptDisp *script_obj = ctx->code->named_item ? ctx->code->named_item->script_obj : ctx->script->script_obj;
244 dynamic_var_t *new_var;
245 heap_pool_t *heap;
246 WCHAR *str;
247 unsigned size;
249 heap = ctx->func->type == FUNC_GLOBAL ? &script_obj->heap : &ctx->heap;
251 new_var = heap_pool_alloc(heap, sizeof(*new_var));
252 if(!new_var)
253 return E_OUTOFMEMORY;
255 size = (lstrlenW(name)+1)*sizeof(WCHAR);
256 str = heap_pool_alloc(heap, size);
257 if(!str)
258 return E_OUTOFMEMORY;
259 memcpy(str, name, size);
260 new_var->name = str;
261 new_var->is_const = is_const;
262 new_var->array = NULL;
263 V_VT(&new_var->v) = VT_EMPTY;
265 if(ctx->func->type == FUNC_GLOBAL) {
266 size_t cnt = script_obj->global_vars_cnt + 1;
267 if(cnt > script_obj->global_vars_size) {
268 dynamic_var_t **new_vars;
269 if(script_obj->global_vars)
270 new_vars = heap_realloc(script_obj->global_vars, cnt * 2 * sizeof(*new_vars));
271 else
272 new_vars = heap_alloc(cnt * 2 * sizeof(*new_vars));
273 if(!new_vars)
274 return E_OUTOFMEMORY;
275 script_obj->global_vars = new_vars;
276 script_obj->global_vars_size = cnt * 2;
278 script_obj->global_vars[script_obj->global_vars_cnt++] = new_var;
279 }else {
280 new_var->next = ctx->dynamic_vars;
281 ctx->dynamic_vars = new_var;
284 *out_var = &new_var->v;
285 return S_OK;
288 void clear_ei(EXCEPINFO *ei)
290 SysFreeString(ei->bstrSource);
291 SysFreeString(ei->bstrDescription);
292 SysFreeString(ei->bstrHelpFile);
293 memset(ei, 0, sizeof(*ei));
296 static void clear_error_loc(script_ctx_t *ctx)
298 if(ctx->error_loc_code) {
299 release_vbscode(ctx->error_loc_code);
300 ctx->error_loc_code = NULL;
304 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
306 assert(ctx->top);
307 return ctx->stack + --ctx->top;
310 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
312 assert(ctx->top >= n);
313 return ctx->stack + (ctx->top-n-1);
316 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
318 if(ctx->stack_size == ctx->top) {
319 VARIANT *new_stack;
321 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
322 if(!new_stack) {
323 VariantClear(v);
324 return E_OUTOFMEMORY;
327 ctx->stack = new_stack;
328 ctx->stack_size *= 2;
331 ctx->stack[ctx->top++] = *v;
332 return S_OK;
335 static inline HRESULT stack_push_null(exec_ctx_t *ctx)
337 VARIANT v;
338 V_VT(&v) = VT_NULL;
339 return stack_push(ctx, &v);
342 static void stack_popn(exec_ctx_t *ctx, unsigned n)
344 while(n--)
345 VariantClear(stack_pop(ctx));
348 static void stack_pop_deref(exec_ctx_t *ctx, variant_val_t *r)
350 VARIANT *v;
352 v = stack_pop(ctx);
353 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
354 r->owned = FALSE;
355 r->v = V_VARIANTREF(v);
356 }else {
357 r->owned = TRUE;
358 r->v = v;
362 static inline void release_val(variant_val_t *v)
364 if(v->owned)
365 VariantClear(v->v);
368 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *r)
370 stack_pop_deref(ctx, r);
372 if(V_VT(r->v) == VT_DISPATCH) {
373 HRESULT hres;
375 hres = get_disp_value(ctx->script, V_DISPATCH(r->v), &r->store);
376 if(r->owned && V_DISPATCH(r->v))
377 IDispatch_Release(V_DISPATCH(r->v));
378 if(FAILED(hres))
379 return hres;
381 r->owned = TRUE;
382 r->v = &r->store;
385 return S_OK;
388 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
390 VARIANT *v = stack_top(ctx, n);
391 HRESULT hres;
393 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
394 VARIANT *ref = V_VARIANTREF(v);
396 V_VT(v) = VT_EMPTY;
397 hres = VariantCopy(v, ref);
398 if(FAILED(hres))
399 return hres;
402 if(V_VT(v) == VT_DISPATCH) {
403 IDispatch *disp;
405 disp = V_DISPATCH(v);
406 hres = get_disp_value(ctx->script, disp, v);
407 if(disp)
408 IDispatch_Release(disp);
409 if(FAILED(hres))
410 return hres;
413 return S_OK;
416 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
418 variant_val_t val;
419 HRESULT hres;
420 VARIANT_BOOL vb;
422 hres = stack_pop_val(ctx, &val);
423 if(FAILED(hres))
424 return hres;
426 switch (V_VT(val.v))
428 case VT_BOOL:
429 *b = V_BOOL(val.v);
430 break;
431 case VT_NULL:
432 case VT_EMPTY:
433 *b = FALSE;
434 break;
435 case VT_I2:
436 *b = V_I2(val.v);
437 break;
438 case VT_I4:
439 *b = V_I4(val.v);
440 break;
441 case VT_BSTR:
442 hres = VarBoolFromStr(V_BSTR(val.v), ctx->script->lcid, 0, &vb);
443 if(FAILED(hres))
444 return hres;
445 *b=vb;
446 break;
447 default:
448 FIXME("unsupported for %s\n", debugstr_variant(val.v));
449 release_val(&val);
450 return E_NOTIMPL;
452 return S_OK;
455 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
457 VARIANT *v = stack_pop(ctx);
459 if(V_VT(v) == VT_DISPATCH) {
460 *ret = V_DISPATCH(v);
461 return S_OK;
464 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
465 FIXME("not supported type: %s\n", debugstr_variant(v));
466 VariantClear(v);
467 return E_FAIL;
470 v = V_BYREF(v);
471 if(V_VT(v) != VT_DISPATCH) {
472 FIXME("not disp %s\n", debugstr_variant(v));
473 return E_FAIL;
476 if(V_DISPATCH(v))
477 IDispatch_AddRef(V_DISPATCH(v));
478 *ret = V_DISPATCH(v);
479 return S_OK;
482 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
484 VARIANT *v = stack_top(ctx, n), *ref;
486 if(V_VT(v) != VT_DISPATCH && (disp || V_VT(v) != VT_UNKNOWN)) {
487 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
488 FIXME("not supported type: %s\n", debugstr_variant(v));
489 return E_FAIL;
492 ref = V_VARIANTREF(v);
493 if(V_VT(ref) != VT_DISPATCH && (disp || V_VT(ref) != VT_UNKNOWN)) {
494 FIXME("not disp %s\n", debugstr_variant(ref));
495 return E_FAIL;
498 V_VT(v) = V_VT(ref);
499 V_UNKNOWN(v) = V_UNKNOWN(ref);
500 if(V_UNKNOWN(v))
501 IUnknown_AddRef(V_UNKNOWN(v));
504 if(disp)
505 *disp = V_DISPATCH(v);
506 return S_OK;
509 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
511 ctx->instr = ctx->code->instrs + addr;
514 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
516 dp->cNamedArgs = is_propput ? 1 : 0;
517 dp->cArgs = arg_cnt + dp->cNamedArgs;
518 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
520 if(arg_cnt) {
521 VARIANT tmp;
522 unsigned i;
524 assert(ctx->top >= arg_cnt);
526 for(i=1; i*2 <= arg_cnt; i++) {
527 tmp = ctx->stack[ctx->top-i];
528 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
529 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
532 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
533 }else {
534 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
538 static HRESULT array_access(exec_ctx_t *ctx, SAFEARRAY *array, DISPPARAMS *dp, VARIANT **ret)
540 unsigned i, argc = arg_cnt(dp);
541 LONG *indices;
542 HRESULT hres;
544 if(!array) {
545 FIXME("NULL array\n");
546 return E_FAIL;
549 hres = SafeArrayLock(array);
550 if(FAILED(hres))
551 return hres;
553 if(array->cDims != argc) {
554 FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
555 SafeArrayUnlock(array);
556 return E_FAIL;
559 indices = heap_alloc(sizeof(*indices) * argc);
560 if(!indices) {
561 SafeArrayUnlock(array);
562 return E_OUTOFMEMORY;
565 for(i=0; i<argc; i++) {
566 hres = to_int(get_arg(dp, i), (int *)(indices+i));
567 if(FAILED(hres)) {
568 heap_free(indices);
569 SafeArrayUnlock(array);
570 return hres;
574 hres = SafeArrayPtrOfIndex(array, indices, (void**)ret);
575 SafeArrayUnlock(array);
576 heap_free(indices);
577 return hres;
580 static HRESULT variant_call(exec_ctx_t *ctx, VARIANT *v, unsigned arg_cnt, VARIANT *res)
582 SAFEARRAY *array = NULL;
583 DISPPARAMS dp;
584 HRESULT hres;
586 TRACE("%s\n", debugstr_variant(v));
588 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
589 v = V_VARIANTREF(v);
591 switch(V_VT(v)) {
592 case VT_ARRAY|VT_BYREF|VT_VARIANT:
593 array = *V_ARRAYREF(v);
594 break;
595 case VT_ARRAY|VT_VARIANT:
596 array = V_ARRAY(v);
597 break;
598 case VT_DISPATCH:
599 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
600 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_VALUE, &dp, res);
601 break;
602 default:
603 FIXME("unsupported on %s\n", debugstr_variant(v));
604 return E_NOTIMPL;
607 if(array) {
608 if(!res) {
609 FIXME("no res\n");
610 return E_NOTIMPL;
613 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
614 hres = array_access(ctx, array, &dp, &v);
615 if(FAILED(hres))
616 return hres;
618 V_VT(res) = VT_BYREF|VT_VARIANT;
619 V_BYREF(res) = v;
622 stack_popn(ctx, arg_cnt);
623 return S_OK;
626 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res, BSTR identifier, unsigned arg_cnt)
628 DISPPARAMS dp;
629 ref_t ref;
630 HRESULT hres;
632 TRACE("%s %u\n", debugstr_w(identifier), arg_cnt);
634 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
635 if(FAILED(hres))
636 return hres;
638 switch(ref.type) {
639 case REF_VAR:
640 case REF_CONST:
641 if(arg_cnt)
642 return variant_call(ctx, ref.u.v, arg_cnt, res);
644 if(!res) {
645 FIXME("REF_VAR no res\n");
646 return E_NOTIMPL;
649 V_VT(res) = VT_BYREF|VT_VARIANT;
650 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
651 break;
652 case REF_DISP:
653 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
654 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
655 if(FAILED(hres))
656 return hres;
657 break;
658 case REF_FUNC:
659 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
660 hres = exec_script(ctx->script, FALSE, ref.u.f, NULL, &dp, res);
661 if(FAILED(hres))
662 return hres;
663 break;
664 case REF_OBJ:
665 if(arg_cnt) {
666 FIXME("arguments on object\n");
667 return E_NOTIMPL;
670 if(res) {
671 IDispatch_AddRef(ref.u.obj);
672 V_VT(res) = VT_DISPATCH;
673 V_DISPATCH(res) = ref.u.obj;
675 break;
676 case REF_NONE:
677 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
678 VARIANT *new;
679 hres = add_dynamic_var(ctx, identifier, FALSE, &new);
680 if(FAILED(hres))
681 return hres;
682 V_VT(res) = VT_BYREF|VT_VARIANT;
683 V_BYREF(res) = new;
684 break;
686 FIXME("%s not found\n", debugstr_w(identifier));
687 return DISP_E_UNKNOWNNAME;
690 stack_popn(ctx, arg_cnt);
691 return S_OK;
694 static HRESULT interp_icall(exec_ctx_t *ctx)
696 BSTR identifier = ctx->instr->arg1.bstr;
697 const unsigned arg_cnt = ctx->instr->arg2.uint;
698 VARIANT v;
699 HRESULT hres;
701 TRACE("\n");
703 hres = do_icall(ctx, &v, identifier, arg_cnt);
704 if(FAILED(hres))
705 return hres;
707 return stack_push(ctx, &v);
710 static HRESULT interp_icallv(exec_ctx_t *ctx)
712 BSTR identifier = ctx->instr->arg1.bstr;
713 const unsigned arg_cnt = ctx->instr->arg2.uint;
715 TRACE("\n");
717 return do_icall(ctx, NULL, identifier, arg_cnt);
720 static HRESULT interp_vcall(exec_ctx_t *ctx)
722 const unsigned arg_cnt = ctx->instr->arg1.uint;
723 VARIANT res, *v;
724 HRESULT hres;
726 TRACE("\n");
728 v = stack_pop(ctx);
729 hres = variant_call(ctx, v, arg_cnt, &res);
730 VariantClear(v);
731 if(FAILED(hres))
732 return hres;
734 return stack_push(ctx, &res);
737 static HRESULT interp_vcallv(exec_ctx_t *ctx)
739 const unsigned arg_cnt = ctx->instr->arg1.uint;
740 VARIANT *v;
741 HRESULT hres;
743 TRACE("\n");
745 v = stack_pop(ctx);
746 hres = variant_call(ctx, v, arg_cnt, NULL);
747 VariantClear(v);
748 return hres;
751 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
753 const BSTR identifier = ctx->instr->arg1.bstr;
754 const unsigned arg_cnt = ctx->instr->arg2.uint;
755 IDispatch *obj;
756 DISPPARAMS dp;
757 DISPID id;
758 HRESULT hres;
760 hres = stack_pop_disp(ctx, &obj);
761 if(FAILED(hres))
762 return hres;
764 if(!obj) {
765 FIXME("NULL obj\n");
766 return E_FAIL;
769 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
771 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
772 if(SUCCEEDED(hres))
773 hres = disp_call(ctx->script, obj, id, &dp, res);
774 IDispatch_Release(obj);
775 if(FAILED(hres))
776 return hres;
778 stack_popn(ctx, arg_cnt);
779 return S_OK;
782 static HRESULT interp_mcall(exec_ctx_t *ctx)
784 VARIANT res;
785 HRESULT hres;
787 TRACE("\n");
789 hres = do_mcall(ctx, &res);
790 if(FAILED(hres))
791 return hres;
793 return stack_push(ctx, &res);
796 static HRESULT interp_mcallv(exec_ctx_t *ctx)
798 TRACE("\n");
800 return do_mcall(ctx, NULL);
803 static HRESULT interp_ident(exec_ctx_t *ctx)
805 BSTR identifier = ctx->instr->arg1.bstr;
806 VARIANT v;
807 HRESULT hres;
809 TRACE("%s\n", debugstr_w(identifier));
811 if((ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET)
812 && !wcsicmp(identifier, ctx->func->name)) {
813 V_VT(&v) = VT_BYREF|VT_VARIANT;
814 V_BYREF(&v) = &ctx->ret_val;
815 return stack_push(ctx, &v);
818 hres = do_icall(ctx, &v, identifier, 0);
819 if(FAILED(hres))
820 return hres;
822 return stack_push(ctx, &v);
825 static HRESULT assign_value(exec_ctx_t *ctx, VARIANT *dst, VARIANT *src, WORD flags)
827 VARIANT value;
828 HRESULT hres;
830 V_VT(&value) = VT_EMPTY;
831 hres = VariantCopyInd(&value, src);
832 if(FAILED(hres))
833 return hres;
835 if(V_VT(&value) == VT_DISPATCH && !(flags & DISPATCH_PROPERTYPUTREF)) {
836 IDispatch *disp = V_DISPATCH(&value);
838 V_VT(&value) = VT_EMPTY;
839 hres = get_disp_value(ctx->script, disp, &value);
840 if(disp)
841 IDispatch_Release(disp);
842 if(FAILED(hres))
843 return hres;
846 VariantClear(dst);
847 *dst = value;
848 return S_OK;
851 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, WORD flags, DISPPARAMS *dp)
853 ref_t ref;
854 HRESULT hres;
856 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
857 if(FAILED(hres))
858 return hres;
860 switch(ref.type) {
861 case REF_VAR: {
862 VARIANT *v = ref.u.v;
864 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
865 v = V_VARIANTREF(v);
867 if(arg_cnt(dp)) {
868 SAFEARRAY *array;
870 if(V_VT(v) == VT_DISPATCH) {
871 hres = disp_propput(ctx->script, V_DISPATCH(v), DISPID_VALUE, flags, dp);
872 break;
875 if(!(V_VT(v) & VT_ARRAY)) {
876 FIXME("array assign on type %d\n", V_VT(v));
877 return E_FAIL;
880 switch(V_VT(v)) {
881 case VT_ARRAY|VT_BYREF|VT_VARIANT:
882 array = *V_ARRAYREF(v);
883 break;
884 case VT_ARRAY|VT_VARIANT:
885 array = V_ARRAY(v);
886 break;
887 default:
888 FIXME("Unsupported array type %x\n", V_VT(v));
889 return E_NOTIMPL;
892 if(!array) {
893 FIXME("null array\n");
894 return E_FAIL;
897 hres = array_access(ctx, array, dp, &v);
898 if(FAILED(hres))
899 return hres;
900 }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
901 FIXME("non-array assign\n");
902 return E_NOTIMPL;
905 hres = assign_value(ctx, v, dp->rgvarg, flags);
906 break;
908 case REF_DISP:
909 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, flags, dp);
910 break;
911 case REF_FUNC:
912 FIXME("functions not implemented\n");
913 return E_NOTIMPL;
914 case REF_OBJ:
915 FIXME("REF_OBJ\n");
916 return E_NOTIMPL;
917 case REF_CONST:
918 FIXME("REF_CONST\n");
919 return E_NOTIMPL;
920 case REF_NONE:
921 if(ctx->func->code_ctx->option_explicit) {
922 FIXME("throw exception\n");
923 hres = E_FAIL;
924 }else {
925 VARIANT *new_var;
927 if(arg_cnt(dp)) {
928 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
929 return E_NOTIMPL;
932 TRACE("creating variable %s\n", debugstr_w(name));
933 hres = add_dynamic_var(ctx, name, FALSE, &new_var);
934 if(SUCCEEDED(hres))
935 hres = assign_value(ctx, new_var, dp->rgvarg, flags);
939 return hres;
942 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
944 const BSTR arg = ctx->instr->arg1.bstr;
945 const unsigned arg_cnt = ctx->instr->arg2.uint;
946 DISPPARAMS dp;
947 HRESULT hres;
949 TRACE("%s\n", debugstr_w(arg));
951 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
952 hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUT, &dp);
953 if(FAILED(hres))
954 return hres;
956 stack_popn(ctx, arg_cnt+1);
957 return S_OK;
960 static HRESULT interp_set_ident(exec_ctx_t *ctx)
962 const BSTR arg = ctx->instr->arg1.bstr;
963 const unsigned arg_cnt = ctx->instr->arg2.uint;
964 DISPPARAMS dp;
965 HRESULT hres;
967 TRACE("%s %u\n", debugstr_w(arg), arg_cnt);
969 hres = stack_assume_disp(ctx, arg_cnt, NULL);
970 if(FAILED(hres))
971 return hres;
973 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
974 hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUTREF, &dp);
975 if(FAILED(hres))
976 return hres;
978 stack_popn(ctx, arg_cnt + 1);
979 return S_OK;
982 static HRESULT interp_assign_member(exec_ctx_t *ctx)
984 BSTR identifier = ctx->instr->arg1.bstr;
985 const unsigned arg_cnt = ctx->instr->arg2.uint;
986 IDispatch *obj;
987 DISPPARAMS dp;
988 DISPID id;
989 HRESULT hres;
991 TRACE("%s\n", debugstr_w(identifier));
993 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
994 if(FAILED(hres))
995 return hres;
997 if(!obj) {
998 FIXME("NULL obj\n");
999 return E_FAIL;
1002 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
1003 if(SUCCEEDED(hres)) {
1004 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
1005 hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUT, &dp);
1007 if(FAILED(hres))
1008 return hres;
1010 stack_popn(ctx, arg_cnt+2);
1011 return S_OK;
1014 static HRESULT interp_set_member(exec_ctx_t *ctx)
1016 BSTR identifier = ctx->instr->arg1.bstr;
1017 const unsigned arg_cnt = ctx->instr->arg2.uint;
1018 IDispatch *obj;
1019 DISPPARAMS dp;
1020 DISPID id;
1021 HRESULT hres;
1023 TRACE("%s\n", debugstr_w(identifier));
1025 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
1026 if(FAILED(hres))
1027 return hres;
1029 if(!obj) {
1030 FIXME("NULL obj\n");
1031 return E_FAIL;
1034 hres = stack_assume_disp(ctx, arg_cnt, NULL);
1035 if(FAILED(hres))
1036 return hres;
1038 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
1039 if(SUCCEEDED(hres)) {
1040 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
1041 hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUTREF, &dp);
1043 if(FAILED(hres))
1044 return hres;
1046 stack_popn(ctx, arg_cnt+2);
1047 return S_OK;
1050 static HRESULT interp_const(exec_ctx_t *ctx)
1052 BSTR arg = ctx->instr->arg1.bstr;
1053 VARIANT *v;
1054 ref_t ref;
1055 HRESULT hres;
1057 TRACE("%s\n", debugstr_w(arg));
1059 assert(ctx->func->type == FUNC_GLOBAL);
1061 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
1062 if(FAILED(hres))
1063 return hres;
1065 if(ref.type != REF_NONE) {
1066 FIXME("%s already defined\n", debugstr_w(arg));
1067 return E_FAIL;
1070 hres = stack_assume_val(ctx, 0);
1071 if(FAILED(hres))
1072 return hres;
1074 hres = add_dynamic_var(ctx, arg, TRUE, &v);
1075 if(FAILED(hres))
1076 return hres;
1078 *v = *stack_pop(ctx);
1079 return S_OK;
1082 static HRESULT interp_val(exec_ctx_t *ctx)
1084 variant_val_t val;
1085 VARIANT v;
1086 HRESULT hres;
1088 TRACE("\n");
1090 hres = stack_pop_val(ctx, &val);
1091 if(FAILED(hres))
1092 return hres;
1094 if(!val.owned) {
1095 V_VT(&v) = VT_EMPTY;
1096 hres = VariantCopy(&v, val.v);
1097 if(FAILED(hres))
1098 return hres;
1101 return stack_push(ctx, val.owned ? val.v : &v);
1104 static HRESULT interp_pop(exec_ctx_t *ctx)
1106 const unsigned n = ctx->instr->arg1.uint;
1108 TRACE("%u\n", n);
1110 stack_popn(ctx, n);
1111 return S_OK;
1114 static HRESULT interp_stack(exec_ctx_t *ctx)
1116 const unsigned n = ctx->instr->arg1.uint;
1117 VARIANT v;
1118 HRESULT hres;
1120 TRACE("%#x\n", n);
1122 if(n == ~0)
1123 return MAKE_VBSERROR(505);
1124 assert(n < ctx->top);
1126 V_VT(&v) = VT_EMPTY;
1127 hres = VariantCopy(&v, ctx->stack + n);
1128 if(FAILED(hres))
1129 return hres;
1131 return stack_push(ctx, &v);
1134 static HRESULT interp_deref(exec_ctx_t *ctx)
1136 VARIANT copy, *v = stack_top(ctx, 0);
1137 HRESULT hres;
1139 TRACE("%s\n", debugstr_variant(v));
1141 if(V_VT(v) != (VT_BYREF|VT_VARIANT))
1142 return S_OK;
1144 V_VT(&copy) = VT_EMPTY;
1145 hres = VariantCopy(&copy, V_VARIANTREF(v));
1146 if(SUCCEEDED(hres))
1147 *v = copy;
1148 return hres;
1151 static HRESULT interp_new(exec_ctx_t *ctx)
1153 const WCHAR *arg = ctx->instr->arg1.bstr;
1154 class_desc_t *class_desc = NULL;
1155 vbdisp_t *obj;
1156 VARIANT v;
1157 HRESULT hres;
1159 TRACE("%s\n", debugstr_w(arg));
1161 if(!wcsicmp(arg, L"regexp")) {
1162 V_VT(&v) = VT_DISPATCH;
1163 hres = create_regexp(&V_DISPATCH(&v));
1164 if(FAILED(hres))
1165 return hres;
1167 return stack_push(ctx, &v);
1170 if(ctx->code->named_item)
1171 for(class_desc = ctx->code->named_item->script_obj->classes; class_desc; class_desc = class_desc->next)
1172 if(!wcsicmp(class_desc->name, arg))
1173 break;
1174 if(!class_desc)
1175 for(class_desc = ctx->script->script_obj->classes; class_desc; class_desc = class_desc->next)
1176 if(!wcsicmp(class_desc->name, arg))
1177 break;
1178 if(!class_desc) {
1179 FIXME("Class %s not found\n", debugstr_w(arg));
1180 return E_FAIL;
1183 hres = create_vbdisp(class_desc, &obj);
1184 if(FAILED(hres))
1185 return hres;
1187 V_VT(&v) = VT_DISPATCH;
1188 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
1189 return stack_push(ctx, &v);
1192 static HRESULT interp_dim(exec_ctx_t *ctx)
1194 ScriptDisp *script_obj = ctx->code->named_item ? ctx->code->named_item->script_obj : ctx->script->script_obj;
1195 const BSTR ident = ctx->instr->arg1.bstr;
1196 const unsigned array_id = ctx->instr->arg2.uint;
1197 const array_desc_t *array_desc;
1198 SAFEARRAY **array_ref;
1199 VARIANT *v;
1200 HRESULT hres;
1202 TRACE("%s\n", debugstr_w(ident));
1204 assert(array_id < ctx->func->array_cnt);
1206 if(ctx->func->type == FUNC_GLOBAL) {
1207 unsigned i;
1208 for(i = 0; i < script_obj->global_vars_cnt; i++) {
1209 if(!wcsicmp(script_obj->global_vars[i]->name, ident))
1210 break;
1212 assert(i < script_obj->global_vars_cnt);
1213 v = &script_obj->global_vars[i]->v;
1214 array_ref = &script_obj->global_vars[i]->array;
1215 }else {
1216 ref_t ref;
1218 if(!ctx->arrays) {
1219 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
1220 if(!ctx->arrays)
1221 return E_OUTOFMEMORY;
1224 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1225 if(FAILED(hres)) {
1226 FIXME("lookup %s failed: %08lx\n", debugstr_w(ident), hres);
1227 return hres;
1230 if(ref.type != REF_VAR) {
1231 FIXME("got ref.type = %d\n", ref.type);
1232 return E_FAIL;
1235 v = ref.u.v;
1236 array_ref = ctx->arrays + array_id;
1239 if(*array_ref) {
1240 FIXME("Array already initialized\n");
1241 return E_FAIL;
1244 array_desc = ctx->func->array_descs + array_id;
1245 if(array_desc->dim_cnt) {
1246 *array_ref = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1247 if(!*array_ref)
1248 return E_OUTOFMEMORY;
1251 V_VT(v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1252 V_ARRAYREF(v) = array_ref;
1253 return S_OK;
1256 static HRESULT array_bounds_from_stack(exec_ctx_t *ctx, unsigned dim_cnt, SAFEARRAYBOUND **ret)
1258 SAFEARRAYBOUND *bounds;
1259 unsigned i;
1260 int dim;
1261 HRESULT hres;
1263 if(!(bounds = heap_alloc(dim_cnt * sizeof(*bounds))))
1264 return E_OUTOFMEMORY;
1266 for(i = 0; i < dim_cnt; i++) {
1267 hres = to_int(stack_top(ctx, dim_cnt - i - 1), &dim);
1268 if(FAILED(hres)) {
1269 heap_free(bounds);
1270 return hres;
1273 bounds[i].cElements = dim + 1;
1274 bounds[i].lLbound = 0;
1277 stack_popn(ctx, dim_cnt);
1278 *ret = bounds;
1279 return S_OK;
1282 static HRESULT interp_redim(exec_ctx_t *ctx)
1284 BSTR identifier = ctx->instr->arg1.bstr;
1285 const unsigned dim_cnt = ctx->instr->arg2.uint;
1286 SAFEARRAYBOUND *bounds;
1287 SAFEARRAY *array;
1288 ref_t ref;
1289 HRESULT hres;
1291 TRACE("%s %u\n", debugstr_w(identifier), dim_cnt);
1293 hres = lookup_identifier(ctx, identifier, VBDISP_LET, &ref);
1294 if(FAILED(hres)) {
1295 FIXME("lookup %s failed: %08lx\n", debugstr_w(identifier), hres);
1296 return hres;
1299 if(ref.type != REF_VAR) {
1300 FIXME("got ref.type = %d\n", ref.type);
1301 return E_FAIL;
1304 hres = array_bounds_from_stack(ctx, dim_cnt, &bounds);
1305 if(FAILED(hres))
1306 return hres;
1308 array = SafeArrayCreate(VT_VARIANT, dim_cnt, bounds);
1309 heap_free(bounds);
1310 if(!array)
1311 return E_OUTOFMEMORY;
1313 /* FIXME: We should check if we're not modifying an existing static array here */
1315 VariantClear(ref.u.v);
1316 V_VT(ref.u.v) = VT_ARRAY|VT_VARIANT;
1317 V_ARRAY(ref.u.v) = array;
1318 return S_OK;
1321 static HRESULT interp_redim_preserve(exec_ctx_t *ctx)
1323 BSTR identifier = ctx->instr->arg1.bstr;
1324 const unsigned dim_cnt = ctx->instr->arg2.uint;
1325 unsigned i;
1326 SAFEARRAYBOUND *bounds;
1327 SAFEARRAY *array;
1328 ref_t ref;
1329 HRESULT hres;
1331 TRACE("%s %u\n", debugstr_w(identifier), dim_cnt);
1333 hres = lookup_identifier(ctx, identifier, VBDISP_LET, &ref);
1334 if(FAILED(hres)) {
1335 FIXME("lookup %s failed: %08lx\n", debugstr_w(identifier), hres);
1336 return hres;
1339 if(ref.type != REF_VAR) {
1340 FIXME("got ref.type = %d\n", ref.type);
1341 return E_FAIL;
1344 if(!(V_VT(ref.u.v) & VT_ARRAY)) {
1345 FIXME("ReDim Preserve not valid on type %d\n", V_VT(ref.u.v));
1346 return E_FAIL;
1349 array = V_ARRAY(ref.u.v);
1351 hres = array_bounds_from_stack(ctx, dim_cnt, &bounds);
1352 if(FAILED(hres))
1353 return hres;
1355 if(array == NULL || array->cDims == 0) {
1356 /* can initially allocate the array */
1357 array = SafeArrayCreate(VT_VARIANT, dim_cnt, bounds);
1358 VariantClear(ref.u.v);
1359 V_VT(ref.u.v) = VT_ARRAY|VT_VARIANT;
1360 V_ARRAY(ref.u.v) = array;
1361 return S_OK;
1362 } else if(array->cDims != dim_cnt) {
1363 /* can't otherwise change the number of dimensions */
1364 TRACE("Can't resize %s, cDims %d != %d\n", debugstr_w(identifier), array->cDims, dim_cnt);
1365 return MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS);
1366 } else {
1367 /* can resize the last dimensions (if others match */
1368 for(i = 0; i+1 < dim_cnt; ++i) {
1369 if(array->rgsabound[array->cDims - 1 - i].cElements != bounds[i].cElements) {
1370 TRACE("Can't resize %s, bound[%d] %ld != %ld\n", debugstr_w(identifier), i, array->rgsabound[i].cElements, bounds[i].cElements);
1371 return MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS);
1374 return SafeArrayRedim(array, &bounds[dim_cnt-1]);
1378 static HRESULT interp_step(exec_ctx_t *ctx)
1380 const BSTR ident = ctx->instr->arg2.bstr;
1381 BOOL gteq_zero;
1382 VARIANT zero;
1383 ref_t ref;
1384 HRESULT hres;
1386 TRACE("%s\n", debugstr_w(ident));
1388 V_VT(&zero) = VT_I2;
1389 V_I2(&zero) = 0;
1390 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1391 if(FAILED(hres))
1392 return hres;
1394 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1396 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1397 if(FAILED(hres))
1398 return hres;
1400 if(ref.type != REF_VAR) {
1401 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1402 return E_FAIL;
1405 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1406 if(FAILED(hres))
1407 return hres;
1409 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1410 ctx->instr++;
1411 }else {
1412 stack_popn(ctx, 2);
1413 instr_jmp(ctx, ctx->instr->arg1.uint);
1415 return S_OK;
1418 static HRESULT interp_newenum(exec_ctx_t *ctx)
1420 variant_val_t v;
1421 VARIANT *r;
1422 HRESULT hres;
1424 TRACE("\n");
1426 stack_pop_deref(ctx, &v);
1427 assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
1428 r = stack_top(ctx, 0);
1430 switch(V_VT(v.v)) {
1431 case VT_DISPATCH|VT_BYREF:
1432 case VT_DISPATCH: {
1433 IEnumVARIANT *iter;
1434 DISPPARAMS dp = {0};
1435 VARIANT iterv;
1437 hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
1438 release_val(&v);
1439 if(FAILED(hres))
1440 return hres;
1442 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1443 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1444 VariantClear(&iterv);
1445 return hres;
1448 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1449 IUnknown_Release(V_UNKNOWN(&iterv));
1450 if(FAILED(hres)) {
1451 FIXME("Could not get IEnumVARIANT iface: %08lx\n", hres);
1452 return hres;
1455 V_VT(r) = VT_UNKNOWN;
1456 V_UNKNOWN(r) = (IUnknown*)iter;
1457 break;
1459 case VT_VARIANT|VT_ARRAY:
1460 case VT_VARIANT|VT_ARRAY|VT_BYREF: {
1461 IEnumVARIANT *iter;
1463 hres = create_safearray_iter(V_ISBYREF(v.v) ? *V_ARRAYREF(v.v) : V_ARRAY(v.v), &iter);
1464 if(FAILED(hres))
1465 return hres;
1467 V_VT(r) = VT_UNKNOWN;
1468 V_UNKNOWN(r) = (IUnknown*)iter;
1469 break;
1471 default:
1472 FIXME("Unsupported for %s\n", debugstr_variant(v.v));
1473 release_val(&v);
1474 return E_NOTIMPL;
1477 return S_OK;
1480 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1482 const unsigned loop_end = ctx->instr->arg1.uint;
1483 const BSTR ident = ctx->instr->arg2.bstr;
1484 VARIANT v;
1485 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1486 IEnumVARIANT *iter;
1487 BOOL do_continue;
1488 HRESULT hres;
1490 TRACE("\n");
1492 if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
1493 FIXME("uninitialized\n");
1494 return E_FAIL;
1497 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1498 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1500 V_VT(&v) = VT_EMPTY;
1501 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1502 if(FAILED(hres))
1503 return hres;
1505 do_continue = hres == S_OK;
1506 hres = assign_ident(ctx, ident, DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &dp);
1507 VariantClear(&v);
1508 if(FAILED(hres))
1509 return hres;
1511 if(do_continue) {
1512 ctx->instr++;
1513 }else {
1514 stack_popn(ctx, 1);
1515 instr_jmp(ctx, loop_end);
1517 return S_OK;
1520 static HRESULT interp_jmp(exec_ctx_t *ctx)
1522 const unsigned arg = ctx->instr->arg1.uint;
1524 TRACE("%u\n", arg);
1526 instr_jmp(ctx, arg);
1527 return S_OK;
1530 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1532 const unsigned arg = ctx->instr->arg1.uint;
1533 HRESULT hres;
1534 BOOL b;
1536 TRACE("%u\n", arg);
1538 hres = stack_pop_bool(ctx, &b);
1539 if(FAILED(hres))
1540 return hres;
1542 if(b)
1543 ctx->instr++;
1544 else
1545 instr_jmp(ctx, ctx->instr->arg1.uint);
1546 return S_OK;
1549 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1551 const unsigned arg = ctx->instr->arg1.uint;
1552 HRESULT hres;
1553 BOOL b;
1555 TRACE("%u\n", arg);
1557 hres = stack_pop_bool(ctx, &b);
1558 if(FAILED(hres))
1559 return hres;
1561 if(b)
1562 instr_jmp(ctx, ctx->instr->arg1.uint);
1563 else
1564 ctx->instr++;
1565 return S_OK;
1568 static HRESULT interp_ret(exec_ctx_t *ctx)
1570 TRACE("\n");
1572 ctx->instr = NULL;
1573 return S_OK;
1576 static HRESULT interp_retval(exec_ctx_t *ctx)
1578 variant_val_t val;
1579 HRESULT hres;
1581 TRACE("\n");
1583 stack_pop_deref(ctx, &val);
1585 if(val.owned) {
1586 VariantClear(&ctx->ret_val);
1587 ctx->ret_val = *val.v;
1589 else {
1590 hres = VariantCopy(&ctx->ret_val, val.v);
1591 if(FAILED(hres))
1592 return hres;
1595 return S_OK;
1598 static HRESULT interp_stop(exec_ctx_t *ctx)
1600 WARN("\n");
1602 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1603 return S_OK;
1606 static HRESULT interp_me(exec_ctx_t *ctx)
1608 IDispatch *disp;
1609 VARIANT v;
1611 TRACE("\n");
1613 if(ctx->vbthis) {
1614 disp = (IDispatch*)&ctx->vbthis->IDispatchEx_iface;
1615 }else if(ctx->code->named_item) {
1616 disp = (ctx->code->named_item->flags & SCRIPTITEM_CODEONLY)
1617 ? (IDispatch*)&ctx->code->named_item->script_obj->IDispatchEx_iface
1618 : ctx->code->named_item->disp;
1619 }else {
1620 named_item_t *item;
1621 disp = NULL;
1622 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
1623 if(!(item->flags & SCRIPTITEM_GLOBALMEMBERS)) continue;
1624 disp = item->disp;
1625 break;
1627 if(!disp)
1628 disp = (IDispatch*)&ctx->script->script_obj->IDispatchEx_iface;
1631 IDispatch_AddRef(disp);
1632 V_VT(&v) = VT_DISPATCH;
1633 V_DISPATCH(&v) = disp;
1634 return stack_push(ctx, &v);
1637 static HRESULT interp_bool(exec_ctx_t *ctx)
1639 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1640 VARIANT v;
1642 TRACE("%s\n", arg ? "true" : "false");
1644 V_VT(&v) = VT_BOOL;
1645 V_BOOL(&v) = arg;
1646 return stack_push(ctx, &v);
1649 static HRESULT interp_errmode(exec_ctx_t *ctx)
1651 const int err_mode = ctx->instr->arg1.uint;
1653 TRACE("%d\n", err_mode);
1655 ctx->resume_next = err_mode;
1656 clear_ei(&ctx->script->ei);
1657 return S_OK;
1660 static HRESULT interp_string(exec_ctx_t *ctx)
1662 VARIANT v;
1664 TRACE("\n");
1666 V_VT(&v) = VT_BSTR;
1667 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1668 if(!V_BSTR(&v))
1669 return E_OUTOFMEMORY;
1671 return stack_push(ctx, &v);
1674 static HRESULT interp_date(exec_ctx_t *ctx)
1676 const DATE *d = ctx->instr->arg1.date;
1677 VARIANT v;
1679 TRACE("%lf\n",*d);
1681 V_VT(&v) = VT_DATE;
1682 V_DATE(&v) = *d;
1684 return stack_push(ctx, &v);
1687 static HRESULT interp_int(exec_ctx_t *ctx)
1689 const LONG arg = ctx->instr->arg1.lng;
1690 VARIANT v;
1692 TRACE("%ld\n", arg);
1694 if(arg == (INT16)arg) {
1695 V_VT(&v) = VT_I2;
1696 V_I2(&v) = arg;
1697 }else {
1698 V_VT(&v) = VT_I4;
1699 V_I4(&v) = arg;
1701 return stack_push(ctx, &v);
1704 static HRESULT interp_double(exec_ctx_t *ctx)
1706 const DOUBLE *arg = ctx->instr->arg1.dbl;
1707 VARIANT v;
1709 TRACE("%lf\n", *arg);
1711 V_VT(&v) = VT_R8;
1712 V_R8(&v) = *arg;
1713 return stack_push(ctx, &v);
1716 static HRESULT interp_empty(exec_ctx_t *ctx)
1718 VARIANT v;
1720 TRACE("\n");
1722 V_VT(&v) = VT_EMPTY;
1723 return stack_push(ctx, &v);
1726 static HRESULT interp_null(exec_ctx_t *ctx)
1728 TRACE("\n");
1729 return stack_push_null(ctx);
1732 static HRESULT interp_nothing(exec_ctx_t *ctx)
1734 VARIANT v;
1736 TRACE("\n");
1738 V_VT(&v) = VT_DISPATCH;
1739 V_DISPATCH(&v) = NULL;
1740 return stack_push(ctx, &v);
1743 static HRESULT interp_hres(exec_ctx_t *ctx)
1745 const unsigned arg = ctx->instr->arg1.uint;
1746 VARIANT v;
1748 TRACE("%d\n", arg);
1750 V_VT(&v) = VT_ERROR;
1751 V_ERROR(&v) = arg;
1752 return stack_push(ctx, &v);
1755 static HRESULT interp_not(exec_ctx_t *ctx)
1757 variant_val_t val;
1758 VARIANT v;
1759 HRESULT hres;
1761 TRACE("\n");
1763 hres = stack_pop_val(ctx, &val);
1764 if(FAILED(hres))
1765 return hres;
1767 hres = VarNot(val.v, &v);
1768 release_val(&val);
1769 if(FAILED(hres))
1770 return hres;
1772 return stack_push(ctx, &v);
1775 static HRESULT interp_and(exec_ctx_t *ctx)
1777 variant_val_t r, l;
1778 VARIANT v;
1779 HRESULT hres;
1781 TRACE("\n");
1783 hres = stack_pop_val(ctx, &r);
1784 if(FAILED(hres))
1785 return hres;
1787 hres = stack_pop_val(ctx, &l);
1788 if(SUCCEEDED(hres)) {
1789 hres = VarAnd(l.v, r.v, &v);
1790 release_val(&l);
1792 release_val(&r);
1793 if(FAILED(hres))
1794 return hres;
1796 return stack_push(ctx, &v);
1799 static HRESULT interp_or(exec_ctx_t *ctx)
1801 variant_val_t r, l;
1802 VARIANT v;
1803 HRESULT hres;
1805 TRACE("\n");
1807 hres = stack_pop_val(ctx, &r);
1808 if(FAILED(hres))
1809 return hres;
1811 hres = stack_pop_val(ctx, &l);
1812 if(SUCCEEDED(hres)) {
1813 hres = VarOr(l.v, r.v, &v);
1814 release_val(&l);
1816 release_val(&r);
1817 if(FAILED(hres))
1818 return hres;
1820 return stack_push(ctx, &v);
1823 static HRESULT interp_xor(exec_ctx_t *ctx)
1825 variant_val_t r, l;
1826 VARIANT v;
1827 HRESULT hres;
1829 TRACE("\n");
1831 hres = stack_pop_val(ctx, &r);
1832 if(FAILED(hres))
1833 return hres;
1835 hres = stack_pop_val(ctx, &l);
1836 if(SUCCEEDED(hres)) {
1837 hres = VarXor(l.v, r.v, &v);
1838 release_val(&l);
1840 release_val(&r);
1841 if(FAILED(hres))
1842 return hres;
1844 return stack_push(ctx, &v);
1847 static HRESULT interp_eqv(exec_ctx_t *ctx)
1849 variant_val_t r, l;
1850 VARIANT v;
1851 HRESULT hres;
1853 TRACE("\n");
1855 hres = stack_pop_val(ctx, &r);
1856 if(FAILED(hres))
1857 return hres;
1859 hres = stack_pop_val(ctx, &l);
1860 if(SUCCEEDED(hres)) {
1861 hres = VarEqv(l.v, r.v, &v);
1862 release_val(&l);
1864 release_val(&r);
1865 if(FAILED(hres))
1866 return hres;
1868 return stack_push(ctx, &v);
1871 static HRESULT interp_imp(exec_ctx_t *ctx)
1873 variant_val_t r, l;
1874 VARIANT v;
1875 HRESULT hres;
1877 TRACE("\n");
1879 hres = stack_pop_val(ctx, &r);
1880 if(FAILED(hres))
1881 return hres;
1883 hres = stack_pop_val(ctx, &l);
1884 if(SUCCEEDED(hres)) {
1885 hres = VarImp(l.v, r.v, &v);
1886 release_val(&l);
1888 release_val(&r);
1889 if(FAILED(hres))
1890 return hres;
1892 return stack_push(ctx, &v);
1895 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1897 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1899 /* FIXME: Fix comparing string to number */
1901 return VarCmp(l, r, ctx->script->lcid, 0);
1904 static HRESULT cmp_oper(exec_ctx_t *ctx)
1906 variant_val_t l, r;
1907 HRESULT hres;
1909 hres = stack_pop_val(ctx, &r);
1910 if(FAILED(hres))
1911 return hres;
1913 hres = stack_pop_val(ctx, &l);
1914 if(SUCCEEDED(hres)) {
1915 hres = var_cmp(ctx, l.v, r.v);
1916 release_val(&l);
1919 release_val(&r);
1920 return hres;
1923 static HRESULT interp_equal(exec_ctx_t *ctx)
1925 VARIANT v;
1926 HRESULT hres;
1928 TRACE("\n");
1930 hres = cmp_oper(ctx);
1931 if(FAILED(hres))
1932 return hres;
1933 if(hres == VARCMP_NULL)
1934 return stack_push_null(ctx);
1936 V_VT(&v) = VT_BOOL;
1937 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1938 return stack_push(ctx, &v);
1941 static HRESULT interp_nequal(exec_ctx_t *ctx)
1943 VARIANT v;
1944 HRESULT hres;
1946 TRACE("\n");
1948 hres = cmp_oper(ctx);
1949 if(FAILED(hres))
1950 return hres;
1951 if(hres == VARCMP_NULL)
1952 return stack_push_null(ctx);
1954 V_VT(&v) = VT_BOOL;
1955 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1956 return stack_push(ctx, &v);
1959 static HRESULT interp_gt(exec_ctx_t *ctx)
1961 VARIANT v;
1962 HRESULT hres;
1964 TRACE("\n");
1966 hres = cmp_oper(ctx);
1967 if(FAILED(hres))
1968 return hres;
1969 if(hres == VARCMP_NULL)
1970 return stack_push_null(ctx);
1972 V_VT(&v) = VT_BOOL;
1973 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1974 return stack_push(ctx, &v);
1977 static HRESULT interp_gteq(exec_ctx_t *ctx)
1979 VARIANT v;
1980 HRESULT hres;
1982 TRACE("\n");
1984 hres = cmp_oper(ctx);
1985 if(FAILED(hres))
1986 return hres;
1987 if(hres == VARCMP_NULL)
1988 return stack_push_null(ctx);
1990 V_VT(&v) = VT_BOOL;
1991 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1992 return stack_push(ctx, &v);
1995 static HRESULT interp_lt(exec_ctx_t *ctx)
1997 VARIANT v;
1998 HRESULT hres;
2000 TRACE("\n");
2002 hres = cmp_oper(ctx);
2003 if(FAILED(hres))
2004 return hres;
2005 if(hres == VARCMP_NULL)
2006 return stack_push_null(ctx);
2008 V_VT(&v) = VT_BOOL;
2009 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
2010 return stack_push(ctx, &v);
2013 static HRESULT interp_lteq(exec_ctx_t *ctx)
2015 VARIANT v;
2016 HRESULT hres;
2018 TRACE("\n");
2020 hres = cmp_oper(ctx);
2021 if(FAILED(hres))
2022 return hres;
2023 if(hres == VARCMP_NULL)
2024 return stack_push_null(ctx);
2026 V_VT(&v) = VT_BOOL;
2027 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
2028 return stack_push(ctx, &v);
2031 static HRESULT interp_case(exec_ctx_t *ctx)
2033 const unsigned arg = ctx->instr->arg1.uint;
2034 variant_val_t v;
2035 HRESULT hres;
2037 TRACE("%d\n", arg);
2039 hres = stack_pop_val(ctx, &v);
2040 if(FAILED(hres))
2041 return hres;
2043 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
2044 release_val(&v);
2045 if(FAILED(hres))
2046 return hres;
2048 if(hres == VARCMP_EQ) {
2049 stack_popn(ctx, 1);
2050 instr_jmp(ctx, arg);
2051 }else {
2052 ctx->instr++;
2055 return S_OK;
2058 static HRESULT interp_is(exec_ctx_t *ctx)
2060 IUnknown *l = NULL, *r = NULL;
2061 variant_val_t v;
2062 HRESULT hres = S_OK;
2064 TRACE("\n");
2066 stack_pop_deref(ctx, &v);
2067 if(V_VT(v.v) != VT_DISPATCH && V_VT(v.v) != VT_UNKNOWN) {
2068 FIXME("Unhandled type %s\n", debugstr_variant(v.v));
2069 hres = E_NOTIMPL;
2070 }else if(V_UNKNOWN(v.v)) {
2071 hres = IUnknown_QueryInterface(V_UNKNOWN(v.v), &IID_IUnknown, (void**)&r);
2073 if(v.owned) VariantClear(v.v);
2074 if(FAILED(hres))
2075 return hres;
2077 stack_pop_deref(ctx, &v);
2078 if(V_VT(v.v) != VT_DISPATCH && V_VT(v.v) != VT_UNKNOWN) {
2079 FIXME("Unhandled type %s\n", debugstr_variant(v.v));
2080 hres = E_NOTIMPL;
2081 }else if(V_UNKNOWN(v.v)) {
2082 hres = IUnknown_QueryInterface(V_UNKNOWN(v.v), &IID_IUnknown, (void**)&l);
2084 if(v.owned) VariantClear(v.v);
2086 if(SUCCEEDED(hres)) {
2087 VARIANT res;
2088 V_VT(&res) = VT_BOOL;
2089 if(r == l)
2090 V_BOOL(&res) = VARIANT_TRUE;
2091 else if(!r || !l)
2092 V_BOOL(&res) = VARIANT_FALSE;
2093 else {
2094 IObjectIdentity *identity;
2095 hres = IUnknown_QueryInterface(l, &IID_IObjectIdentity, (void**)&identity);
2096 if(SUCCEEDED(hres)) {
2097 hres = IObjectIdentity_IsEqualObject(identity, r);
2098 IObjectIdentity_Release(identity);
2100 V_BOOL(&res) = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
2102 hres = stack_push(ctx, &res);
2104 if(r)
2105 IUnknown_Release(r);
2106 if(l)
2107 IUnknown_Release(l);
2108 return hres;
2111 static HRESULT interp_concat(exec_ctx_t *ctx)
2113 variant_val_t r, l;
2114 VARIANT v;
2115 HRESULT hres;
2117 TRACE("\n");
2119 hres = stack_pop_val(ctx, &r);
2120 if(FAILED(hres))
2121 return hres;
2123 hres = stack_pop_val(ctx, &l);
2124 if(SUCCEEDED(hres)) {
2125 hres = VarCat(l.v, r.v, &v);
2126 release_val(&l);
2128 release_val(&r);
2129 if(FAILED(hres))
2130 return hres;
2132 return stack_push(ctx, &v);
2135 static HRESULT interp_add(exec_ctx_t *ctx)
2137 variant_val_t r, l;
2138 VARIANT v;
2139 HRESULT hres;
2141 TRACE("\n");
2143 hres = stack_pop_val(ctx, &r);
2144 if(FAILED(hres))
2145 return hres;
2147 hres = stack_pop_val(ctx, &l);
2148 if(SUCCEEDED(hres)) {
2149 hres = VarAdd(l.v, r.v, &v);
2150 release_val(&l);
2152 release_val(&r);
2153 if(FAILED(hres))
2154 return hres;
2156 return stack_push(ctx, &v);
2159 static HRESULT interp_sub(exec_ctx_t *ctx)
2161 variant_val_t r, l;
2162 VARIANT v;
2163 HRESULT hres;
2165 TRACE("\n");
2167 hres = stack_pop_val(ctx, &r);
2168 if(FAILED(hres))
2169 return hres;
2171 hres = stack_pop_val(ctx, &l);
2172 if(SUCCEEDED(hres)) {
2173 hres = VarSub(l.v, r.v, &v);
2174 release_val(&l);
2176 release_val(&r);
2177 if(FAILED(hres))
2178 return hres;
2180 return stack_push(ctx, &v);
2183 static HRESULT interp_mod(exec_ctx_t *ctx)
2185 variant_val_t r, l;
2186 VARIANT v;
2187 HRESULT hres;
2189 TRACE("\n");
2191 hres = stack_pop_val(ctx, &r);
2192 if(FAILED(hres))
2193 return hres;
2195 hres = stack_pop_val(ctx, &l);
2196 if(SUCCEEDED(hres)) {
2197 hres = VarMod(l.v, r.v, &v);
2198 release_val(&l);
2200 release_val(&r);
2201 if(FAILED(hres))
2202 return hres;
2204 return stack_push(ctx, &v);
2207 static HRESULT interp_idiv(exec_ctx_t *ctx)
2209 variant_val_t r, l;
2210 VARIANT v;
2211 HRESULT hres;
2213 TRACE("\n");
2215 hres = stack_pop_val(ctx, &r);
2216 if(FAILED(hres))
2217 return hres;
2219 hres = stack_pop_val(ctx, &l);
2220 if(SUCCEEDED(hres)) {
2221 hres = VarIdiv(l.v, r.v, &v);
2222 release_val(&l);
2224 release_val(&r);
2225 if(FAILED(hres))
2226 return hres;
2228 return stack_push(ctx, &v);
2231 static HRESULT interp_div(exec_ctx_t *ctx)
2233 variant_val_t r, l;
2234 VARIANT v;
2235 HRESULT hres;
2237 TRACE("\n");
2239 hres = stack_pop_val(ctx, &r);
2240 if(FAILED(hres))
2241 return hres;
2243 hres = stack_pop_val(ctx, &l);
2244 if(SUCCEEDED(hres)) {
2245 hres = VarDiv(l.v, r.v, &v);
2246 release_val(&l);
2248 release_val(&r);
2249 if(FAILED(hres))
2250 return hres;
2252 return stack_push(ctx, &v);
2255 static HRESULT interp_mul(exec_ctx_t *ctx)
2257 variant_val_t r, l;
2258 VARIANT v;
2259 HRESULT hres;
2261 TRACE("\n");
2263 hres = stack_pop_val(ctx, &r);
2264 if(FAILED(hres))
2265 return hres;
2267 hres = stack_pop_val(ctx, &l);
2268 if(SUCCEEDED(hres)) {
2269 hres = VarMul(l.v, r.v, &v);
2270 release_val(&l);
2272 release_val(&r);
2273 if(FAILED(hres))
2274 return hres;
2276 return stack_push(ctx, &v);
2279 static HRESULT interp_exp(exec_ctx_t *ctx)
2281 variant_val_t r, l;
2282 VARIANT v;
2283 HRESULT hres;
2285 TRACE("\n");
2287 hres = stack_pop_val(ctx, &r);
2288 if(FAILED(hres))
2289 return hres;
2291 hres = stack_pop_val(ctx, &l);
2292 if(SUCCEEDED(hres)) {
2293 hres = VarPow(l.v, r.v, &v);
2294 release_val(&l);
2296 release_val(&r);
2297 if(FAILED(hres))
2298 return hres;
2300 return stack_push(ctx, &v);
2303 static HRESULT interp_neg(exec_ctx_t *ctx)
2305 variant_val_t val;
2306 VARIANT v;
2307 HRESULT hres;
2309 hres = stack_pop_val(ctx, &val);
2310 if(FAILED(hres))
2311 return hres;
2313 hres = VarNeg(val.v, &v);
2314 release_val(&val);
2315 if(FAILED(hres))
2316 return hres;
2318 return stack_push(ctx, &v);
2321 static HRESULT interp_incc(exec_ctx_t *ctx)
2323 const BSTR ident = ctx->instr->arg1.bstr;
2324 VARIANT v;
2325 ref_t ref;
2326 HRESULT hres;
2328 TRACE("\n");
2330 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
2331 if(FAILED(hres))
2332 return hres;
2334 if(ref.type != REF_VAR) {
2335 FIXME("ref.type is not REF_VAR\n");
2336 return E_FAIL;
2339 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
2340 if(FAILED(hres))
2341 return hres;
2343 VariantClear(ref.u.v);
2344 *ref.u.v = v;
2345 return S_OK;
2348 static HRESULT interp_catch(exec_ctx_t *ctx)
2350 /* Nothing to do here, the OP is for unwinding only. */
2351 return S_OK;
2354 static const instr_func_t op_funcs[] = {
2355 #define X(x,n,a,b) interp_ ## x,
2356 OP_LIST
2357 #undef X
2360 static const unsigned op_move[] = {
2361 #define X(x,n,a,b) n,
2362 OP_LIST
2363 #undef X
2366 void release_dynamic_var(dynamic_var_t *var)
2368 VariantClear(&var->v);
2369 if(var->array)
2370 SafeArrayDestroy(var->array);
2373 static void release_exec(exec_ctx_t *ctx)
2375 dynamic_var_t *var;
2376 unsigned i;
2378 VariantClear(&ctx->ret_val);
2380 for(var = ctx->dynamic_vars; var; var = var->next)
2381 release_dynamic_var(var);
2383 if(ctx->vbthis)
2384 IDispatchEx_Release(&ctx->vbthis->IDispatchEx_iface);
2386 if(ctx->args) {
2387 for(i=0; i < ctx->func->arg_cnt; i++)
2388 VariantClear(ctx->args+i);
2391 if(ctx->vars) {
2392 for(i=0; i < ctx->func->var_cnt; i++)
2393 VariantClear(ctx->vars+i);
2396 if(ctx->arrays) {
2397 for(i=0; i < ctx->func->array_cnt; i++) {
2398 if(ctx->arrays[i])
2399 SafeArrayDestroy(ctx->arrays[i]);
2401 heap_free(ctx->arrays);
2404 heap_pool_free(&ctx->heap);
2405 heap_free(ctx->args);
2406 heap_free(ctx->vars);
2407 heap_free(ctx->stack);
2410 HRESULT exec_script(script_ctx_t *ctx, BOOL extern_caller, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2412 exec_ctx_t exec = {func->code_ctx};
2413 vbsop_t op;
2414 HRESULT hres = S_OK;
2416 exec.code = func->code_ctx;
2418 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2419 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2420 return E_FAIL;
2423 heap_pool_init(&exec.heap);
2425 TRACE("%s(", debugstr_w(func->name));
2426 if(func->arg_cnt) {
2427 VARIANT *v;
2428 unsigned i;
2430 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2431 if(!exec.args) {
2432 release_exec(&exec);
2433 return E_OUTOFMEMORY;
2436 for(i=0; i < func->arg_cnt; i++) {
2437 v = get_arg(dp, i);
2438 TRACE("%s%s", i ? ", " : "", debugstr_variant(v));
2439 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2440 if(func->args[i].by_ref)
2441 exec.args[i] = *v;
2442 else
2443 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2444 }else {
2445 hres = VariantCopyInd(exec.args+i, v);
2447 if(FAILED(hres)) {
2448 release_exec(&exec);
2449 return hres;
2452 }else {
2453 exec.args = NULL;
2455 TRACE(")\n");
2457 if(func->var_cnt) {
2458 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2459 if(!exec.vars) {
2460 release_exec(&exec);
2461 return E_OUTOFMEMORY;
2463 }else {
2464 exec.vars = NULL;
2467 exec.stack_size = 16;
2468 exec.top = 0;
2469 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2470 if(!exec.stack) {
2471 release_exec(&exec);
2472 return E_OUTOFMEMORY;
2475 if(extern_caller)
2476 IActiveScriptSite_OnEnterScript(ctx->site);
2478 if(vbthis) {
2479 IDispatchEx_AddRef(&vbthis->IDispatchEx_iface);
2480 exec.vbthis = vbthis;
2483 exec.instr = exec.code->instrs + func->code_off;
2484 exec.script = ctx;
2485 exec.func = func;
2487 while(exec.instr) {
2488 op = exec.instr->op;
2489 hres = op_funcs[op](&exec);
2490 if(FAILED(hres)) {
2491 if(hres != SCRIPT_E_RECORDED) {
2492 clear_ei(&ctx->ei);
2494 ctx->ei.scode = hres = map_hres(hres);
2495 ctx->ei.bstrSource = get_vbscript_string(VBS_RUNTIME_ERROR);
2496 ctx->ei.bstrDescription = get_vbscript_error_string(hres);
2497 }else {
2498 hres = ctx->ei.scode;
2501 if(exec.resume_next) {
2502 unsigned stack_off;
2504 WARN("Failed %08lx in resume next mode\n", hres);
2507 * Unwinding here is simple. We need to find the next OP_catch, which contains
2508 * information about expected stack size and jump offset on error. Generated
2509 * bytecode needs to guarantee, that simple jump and stack adjustment will
2510 * guarantee proper execution continuation.
2512 while((++exec.instr)->op != OP_catch);
2514 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2516 clear_error_loc(ctx);
2517 stack_off = exec.instr->arg2.uint;
2518 instr_jmp(&exec, exec.instr->arg1.uint);
2520 if(exec.top > stack_off) {
2521 stack_popn(&exec, exec.top-stack_off);
2522 }else if(exec.top < stack_off) {
2523 VARIANT v;
2525 V_VT(&v) = VT_EMPTY;
2526 while(exec.top < stack_off) {
2527 hres = stack_push(&exec, &v);
2528 if(FAILED(hres))
2529 break;
2533 continue;
2534 }else {
2535 if(!ctx->error_loc_code) {
2536 grab_vbscode(exec.code);
2537 ctx->error_loc_code = exec.code;
2538 ctx->error_loc_offset = exec.instr->loc;
2540 stack_popn(&exec, exec.top);
2541 break;
2545 exec.instr += op_move[op];
2548 assert(!exec.top);
2550 if(extern_caller) {
2551 if(FAILED(hres)) {
2552 if(!ctx->ei.scode)
2553 ctx->ei.scode = hres;
2554 hres = report_script_error(ctx, ctx->error_loc_code, ctx->error_loc_offset);
2555 clear_error_loc(ctx);
2557 IActiveScriptSite_OnLeaveScript(ctx->site);
2560 if(SUCCEEDED(hres) && res) {
2561 *res = exec.ret_val;
2562 V_VT(&exec.ret_val) = VT_EMPTY;
2565 release_exec(&exec);
2566 return hres;