vbscript: Use wide-char string literals.
[wine.git] / dlls / vbscript / interp.c
blob560d3b16e690d5c9e7baa8ed46cf640dff923489
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((ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET)
138 && !wcsicmp(name, ctx->func->name)) {
139 ref->type = REF_VAR;
140 ref->u.v = &ctx->ret_val;
141 return S_OK;
144 if(ctx->func->type != FUNC_GLOBAL) {
145 for(i=0; i < ctx->func->var_cnt; i++) {
146 if(!wcsicmp(ctx->func->vars[i].name, name)) {
147 ref->type = REF_VAR;
148 ref->u.v = ctx->vars+i;
149 return S_OK;
153 for(i=0; i < ctx->func->arg_cnt; i++) {
154 if(!wcsicmp(ctx->func->args[i].name, name)) {
155 ref->type = REF_VAR;
156 ref->u.v = ctx->args+i;
157 return S_OK;
161 if(lookup_dynamic_vars(ctx->dynamic_vars, name, ref))
162 return S_OK;
164 if(ctx->vbthis) {
165 /* FIXME: Bind such identifier while generating bytecode. */
166 for(i=0; i < ctx->vbthis->desc->prop_cnt; i++) {
167 if(!wcsicmp(ctx->vbthis->desc->props[i].name, name)) {
168 ref->type = REF_VAR;
169 ref->u.v = ctx->vbthis->props+i;
170 return S_OK;
174 hres = vbdisp_get_id(ctx->vbthis, name, invoke_type, TRUE, &id);
175 if(SUCCEEDED(hres)) {
176 ref->type = REF_DISP;
177 ref->u.d.disp = (IDispatch*)&ctx->vbthis->IDispatchEx_iface;
178 ref->u.d.id = id;
179 return S_OK;
184 if(ctx->code->named_item) {
185 if(lookup_global_vars(ctx->code->named_item->script_obj, name, ref))
186 return S_OK;
187 if(lookup_global_funcs(ctx->code->named_item->script_obj, name, ref))
188 return S_OK;
191 if(ctx->func->code_ctx->named_item && ctx->func->code_ctx->named_item->disp &&
192 !(ctx->func->code_ctx->named_item->flags & SCRIPTITEM_CODEONLY))
194 hres = disp_get_id(ctx->func->code_ctx->named_item->disp, name, invoke_type, TRUE, &id);
195 if(SUCCEEDED(hres)) {
196 ref->type = REF_DISP;
197 ref->u.d.disp = ctx->func->code_ctx->named_item->disp;
198 ref->u.d.id = id;
199 return S_OK;
203 if(lookup_global_vars(script_obj, name, ref))
204 return S_OK;
205 if(lookup_global_funcs(script_obj, name, ref))
206 return S_OK;
208 hres = get_builtin_id(ctx->script->global_obj, name, &id);
209 if(SUCCEEDED(hres)) {
210 ref->type = REF_DISP;
211 ref->u.d.disp = &ctx->script->global_obj->IDispatch_iface;
212 ref->u.d.id = id;
213 return S_OK;
216 item = lookup_named_item(ctx->script, name, SCRIPTITEM_ISVISIBLE);
217 if(item && item->disp) {
218 ref->type = REF_OBJ;
219 ref->u.obj = item->disp;
220 return S_OK;
223 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
224 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
225 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
226 if(SUCCEEDED(hres)) {
227 ref->type = REF_DISP;
228 ref->u.d.disp = item->disp;
229 ref->u.d.id = id;
230 return S_OK;
235 ref->type = REF_NONE;
236 return S_OK;
239 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name,
240 BOOL is_const, VARIANT **out_var)
242 ScriptDisp *script_obj = ctx->code->named_item ? ctx->code->named_item->script_obj : ctx->script->script_obj;
243 dynamic_var_t *new_var;
244 heap_pool_t *heap;
245 WCHAR *str;
246 unsigned size;
248 heap = ctx->func->type == FUNC_GLOBAL ? &script_obj->heap : &ctx->heap;
250 new_var = heap_pool_alloc(heap, sizeof(*new_var));
251 if(!new_var)
252 return E_OUTOFMEMORY;
254 size = (lstrlenW(name)+1)*sizeof(WCHAR);
255 str = heap_pool_alloc(heap, size);
256 if(!str)
257 return E_OUTOFMEMORY;
258 memcpy(str, name, size);
259 new_var->name = str;
260 new_var->is_const = is_const;
261 new_var->array = NULL;
262 V_VT(&new_var->v) = VT_EMPTY;
264 if(ctx->func->type == FUNC_GLOBAL) {
265 size_t cnt = script_obj->global_vars_cnt + 1;
266 if(cnt > script_obj->global_vars_size) {
267 dynamic_var_t **new_vars;
268 if(script_obj->global_vars)
269 new_vars = heap_realloc(script_obj->global_vars, cnt * 2 * sizeof(*new_vars));
270 else
271 new_vars = heap_alloc(cnt * 2 * sizeof(*new_vars));
272 if(!new_vars)
273 return E_OUTOFMEMORY;
274 script_obj->global_vars = new_vars;
275 script_obj->global_vars_size = cnt * 2;
277 script_obj->global_vars[script_obj->global_vars_cnt++] = new_var;
278 }else {
279 new_var->next = ctx->dynamic_vars;
280 ctx->dynamic_vars = new_var;
283 *out_var = &new_var->v;
284 return S_OK;
287 void clear_ei(EXCEPINFO *ei)
289 SysFreeString(ei->bstrSource);
290 SysFreeString(ei->bstrDescription);
291 SysFreeString(ei->bstrHelpFile);
292 memset(ei, 0, sizeof(*ei));
295 static void clear_error_loc(script_ctx_t *ctx)
297 if(ctx->error_loc_code) {
298 release_vbscode(ctx->error_loc_code);
299 ctx->error_loc_code = NULL;
303 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
305 assert(ctx->top);
306 return ctx->stack + --ctx->top;
309 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
311 assert(ctx->top >= n);
312 return ctx->stack + (ctx->top-n-1);
315 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
317 if(ctx->stack_size == ctx->top) {
318 VARIANT *new_stack;
320 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
321 if(!new_stack) {
322 VariantClear(v);
323 return E_OUTOFMEMORY;
326 ctx->stack = new_stack;
327 ctx->stack_size *= 2;
330 ctx->stack[ctx->top++] = *v;
331 return S_OK;
334 static inline HRESULT stack_push_null(exec_ctx_t *ctx)
336 VARIANT v;
337 V_VT(&v) = VT_NULL;
338 return stack_push(ctx, &v);
341 static void stack_popn(exec_ctx_t *ctx, unsigned n)
343 while(n--)
344 VariantClear(stack_pop(ctx));
347 static void stack_pop_deref(exec_ctx_t *ctx, variant_val_t *r)
349 VARIANT *v;
351 v = stack_pop(ctx);
352 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
353 r->owned = FALSE;
354 r->v = V_VARIANTREF(v);
355 }else {
356 r->owned = TRUE;
357 r->v = v;
361 static inline void release_val(variant_val_t *v)
363 if(v->owned)
364 VariantClear(v->v);
367 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *r)
369 stack_pop_deref(ctx, r);
371 if(V_VT(r->v) == VT_DISPATCH) {
372 HRESULT hres;
374 hres = get_disp_value(ctx->script, V_DISPATCH(r->v), &r->store);
375 if(r->owned && V_DISPATCH(r->v))
376 IDispatch_Release(V_DISPATCH(r->v));
377 if(FAILED(hres))
378 return hres;
380 r->owned = TRUE;
381 r->v = &r->store;
384 return S_OK;
387 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
389 VARIANT *v = stack_top(ctx, n);
390 HRESULT hres;
392 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
393 VARIANT *ref = V_VARIANTREF(v);
395 V_VT(v) = VT_EMPTY;
396 hres = VariantCopy(v, ref);
397 if(FAILED(hres))
398 return hres;
401 if(V_VT(v) == VT_DISPATCH) {
402 IDispatch *disp;
404 disp = V_DISPATCH(v);
405 hres = get_disp_value(ctx->script, disp, v);
406 if(disp)
407 IDispatch_Release(disp);
408 if(FAILED(hres))
409 return hres;
412 return S_OK;
415 static int stack_pop_bool(exec_ctx_t *ctx, BOOL *b)
417 variant_val_t val;
418 HRESULT hres;
420 hres = stack_pop_val(ctx, &val);
421 if(FAILED(hres))
422 return hres;
424 switch (V_VT(val.v))
426 case VT_BOOL:
427 *b = V_BOOL(val.v);
428 break;
429 case VT_NULL:
430 case VT_EMPTY:
431 *b = FALSE;
432 break;
433 case VT_I2:
434 *b = V_I2(val.v);
435 break;
436 case VT_I4:
437 *b = V_I4(val.v);
438 break;
439 default:
440 FIXME("unsupported for %s\n", debugstr_variant(val.v));
441 release_val(&val);
442 return E_NOTIMPL;
444 return S_OK;
447 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
449 VARIANT *v = stack_pop(ctx);
451 if(V_VT(v) == VT_DISPATCH) {
452 *ret = V_DISPATCH(v);
453 return S_OK;
456 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
457 FIXME("not supported type: %s\n", debugstr_variant(v));
458 VariantClear(v);
459 return E_FAIL;
462 v = V_BYREF(v);
463 if(V_VT(v) != VT_DISPATCH) {
464 FIXME("not disp %s\n", debugstr_variant(v));
465 return E_FAIL;
468 if(V_DISPATCH(v))
469 IDispatch_AddRef(V_DISPATCH(v));
470 *ret = V_DISPATCH(v);
471 return S_OK;
474 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
476 VARIANT *v = stack_top(ctx, n), *ref;
478 if(V_VT(v) != VT_DISPATCH && (disp || V_VT(v) != VT_UNKNOWN)) {
479 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
480 FIXME("not supported type: %s\n", debugstr_variant(v));
481 return E_FAIL;
484 ref = V_VARIANTREF(v);
485 if(V_VT(ref) != VT_DISPATCH && (disp || V_VT(ref) != VT_UNKNOWN)) {
486 FIXME("not disp %s\n", debugstr_variant(ref));
487 return E_FAIL;
490 V_VT(v) = V_VT(ref);
491 V_UNKNOWN(v) = V_UNKNOWN(ref);
492 if(V_UNKNOWN(v))
493 IUnknown_AddRef(V_UNKNOWN(v));
496 if(disp)
497 *disp = V_DISPATCH(v);
498 return S_OK;
501 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
503 ctx->instr = ctx->code->instrs + addr;
506 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
508 dp->cNamedArgs = is_propput ? 1 : 0;
509 dp->cArgs = arg_cnt + dp->cNamedArgs;
510 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
512 if(arg_cnt) {
513 VARIANT tmp;
514 unsigned i;
516 assert(ctx->top >= arg_cnt);
518 for(i=1; i*2 <= arg_cnt; i++) {
519 tmp = ctx->stack[ctx->top-i];
520 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
521 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
524 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
525 }else {
526 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
530 static HRESULT array_access(exec_ctx_t *ctx, SAFEARRAY *array, DISPPARAMS *dp, VARIANT **ret)
532 unsigned i, argc = arg_cnt(dp);
533 LONG *indices;
534 HRESULT hres;
536 if(!array) {
537 FIXME("NULL array\n");
538 return E_FAIL;
541 hres = SafeArrayLock(array);
542 if(FAILED(hres))
543 return hres;
545 if(array->cDims != argc) {
546 FIXME("argc %d does not match cDims %d\n", dp->cArgs, array->cDims);
547 SafeArrayUnlock(array);
548 return E_FAIL;
551 indices = heap_alloc(sizeof(*indices) * argc);
552 if(!indices) {
553 SafeArrayUnlock(array);
554 return E_OUTOFMEMORY;
557 for(i=0; i<argc; i++) {
558 hres = to_int(get_arg(dp, i), indices+i);
559 if(FAILED(hres)) {
560 heap_free(indices);
561 SafeArrayUnlock(array);
562 return hres;
566 hres = SafeArrayPtrOfIndex(array, indices, (void**)ret);
567 SafeArrayUnlock(array);
568 heap_free(indices);
569 return hres;
572 static HRESULT variant_call(exec_ctx_t *ctx, VARIANT *v, unsigned arg_cnt, VARIANT *res)
574 SAFEARRAY *array = NULL;
575 DISPPARAMS dp;
576 HRESULT hres;
578 TRACE("%s\n", debugstr_variant(v));
580 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
581 v = V_VARIANTREF(v);
583 switch(V_VT(v)) {
584 case VT_ARRAY|VT_BYREF|VT_VARIANT:
585 array = *V_ARRAYREF(v);
586 break;
587 case VT_ARRAY|VT_VARIANT:
588 array = V_ARRAY(v);
589 break;
590 case VT_DISPATCH:
591 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
592 hres = disp_call(ctx->script, V_DISPATCH(v), DISPID_VALUE, &dp, res);
593 break;
594 default:
595 FIXME("unsupported on %s\n", debugstr_variant(v));
596 return E_NOTIMPL;
599 if(array) {
600 if(!res) {
601 FIXME("no res\n");
602 return E_NOTIMPL;
605 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
606 hres = array_access(ctx, array, &dp, &v);
607 if(FAILED(hres))
608 return hres;
610 V_VT(res) = VT_BYREF|VT_VARIANT;
611 V_BYREF(res) = v;
614 stack_popn(ctx, arg_cnt);
615 return S_OK;
618 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
620 BSTR identifier = ctx->instr->arg1.bstr;
621 const unsigned arg_cnt = ctx->instr->arg2.uint;
622 DISPPARAMS dp;
623 ref_t ref;
624 HRESULT hres;
626 TRACE("%s %u\n", debugstr_w(identifier), arg_cnt);
628 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
629 if(FAILED(hres))
630 return hres;
632 switch(ref.type) {
633 case REF_VAR:
634 case REF_CONST:
635 if(arg_cnt)
636 return variant_call(ctx, ref.u.v, arg_cnt, res);
638 if(!res) {
639 FIXME("REF_VAR no res\n");
640 return E_NOTIMPL;
643 V_VT(res) = VT_BYREF|VT_VARIANT;
644 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
645 break;
646 case REF_DISP:
647 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
648 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
649 if(FAILED(hres))
650 return hres;
651 break;
652 case REF_FUNC:
653 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
654 hres = exec_script(ctx->script, FALSE, ref.u.f, NULL, &dp, res);
655 if(FAILED(hres))
656 return hres;
657 break;
658 case REF_OBJ:
659 if(arg_cnt) {
660 FIXME("arguments on object\n");
661 return E_NOTIMPL;
664 if(res) {
665 IDispatch_AddRef(ref.u.obj);
666 V_VT(res) = VT_DISPATCH;
667 V_DISPATCH(res) = ref.u.obj;
669 break;
670 case REF_NONE:
671 if(res && !ctx->func->code_ctx->option_explicit && arg_cnt == 0) {
672 VARIANT *new;
673 hres = add_dynamic_var(ctx, identifier, FALSE, &new);
674 if(FAILED(hres))
675 return hres;
676 V_VT(res) = VT_BYREF|VT_VARIANT;
677 V_BYREF(res) = new;
678 break;
680 FIXME("%s not found\n", debugstr_w(identifier));
681 return DISP_E_UNKNOWNNAME;
684 stack_popn(ctx, arg_cnt);
685 return S_OK;
688 static HRESULT interp_icall(exec_ctx_t *ctx)
690 VARIANT v;
691 HRESULT hres;
693 TRACE("\n");
695 hres = do_icall(ctx, &v);
696 if(FAILED(hres))
697 return hres;
699 return stack_push(ctx, &v);
702 static HRESULT interp_icallv(exec_ctx_t *ctx)
704 TRACE("\n");
705 return do_icall(ctx, NULL);
708 static HRESULT interp_vcall(exec_ctx_t *ctx)
710 const unsigned arg_cnt = ctx->instr->arg1.uint;
711 VARIANT res, *v;
712 HRESULT hres;
714 TRACE("\n");
716 v = stack_pop(ctx);
717 hres = variant_call(ctx, v, arg_cnt, &res);
718 VariantClear(v);
719 if(FAILED(hres))
720 return hres;
722 return stack_push(ctx, &res);
725 static HRESULT interp_vcallv(exec_ctx_t *ctx)
727 const unsigned arg_cnt = ctx->instr->arg1.uint;
728 VARIANT *v;
729 HRESULT hres;
731 TRACE("\n");
733 v = stack_pop(ctx);
734 hres = variant_call(ctx, v, arg_cnt, NULL);
735 VariantClear(v);
736 return hres;
739 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
741 const BSTR identifier = ctx->instr->arg1.bstr;
742 const unsigned arg_cnt = ctx->instr->arg2.uint;
743 IDispatch *obj;
744 DISPPARAMS dp;
745 DISPID id;
746 HRESULT hres;
748 hres = stack_pop_disp(ctx, &obj);
749 if(FAILED(hres))
750 return hres;
752 if(!obj) {
753 FIXME("NULL obj\n");
754 return E_FAIL;
757 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
759 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
760 if(SUCCEEDED(hres))
761 hres = disp_call(ctx->script, obj, id, &dp, res);
762 IDispatch_Release(obj);
763 if(FAILED(hres))
764 return hres;
766 stack_popn(ctx, arg_cnt);
767 return S_OK;
770 static HRESULT interp_mcall(exec_ctx_t *ctx)
772 VARIANT res;
773 HRESULT hres;
775 TRACE("\n");
777 hres = do_mcall(ctx, &res);
778 if(FAILED(hres))
779 return hres;
781 return stack_push(ctx, &res);
784 static HRESULT interp_mcallv(exec_ctx_t *ctx)
786 TRACE("\n");
788 return do_mcall(ctx, NULL);
791 static HRESULT assign_value(exec_ctx_t *ctx, VARIANT *dst, VARIANT *src, WORD flags)
793 VARIANT value;
794 HRESULT hres;
796 V_VT(&value) = VT_EMPTY;
797 hres = VariantCopyInd(&value, src);
798 if(FAILED(hres))
799 return hres;
801 if(V_VT(&value) == VT_DISPATCH && !(flags & DISPATCH_PROPERTYPUTREF)) {
802 IDispatch *disp = V_DISPATCH(&value);
804 V_VT(&value) = VT_EMPTY;
805 hres = get_disp_value(ctx->script, disp, &value);
806 if(disp)
807 IDispatch_Release(disp);
808 if(FAILED(hres))
809 return hres;
812 VariantClear(dst);
813 *dst = value;
814 return S_OK;
817 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, WORD flags, DISPPARAMS *dp)
819 ref_t ref;
820 HRESULT hres;
822 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
823 if(FAILED(hres))
824 return hres;
826 switch(ref.type) {
827 case REF_VAR: {
828 VARIANT *v = ref.u.v;
830 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
831 v = V_VARIANTREF(v);
833 if(arg_cnt(dp)) {
834 SAFEARRAY *array;
836 if(V_VT(v) == VT_DISPATCH) {
837 hres = disp_propput(ctx->script, V_DISPATCH(v), DISPID_VALUE, flags, dp);
838 break;
841 if(!(V_VT(v) & VT_ARRAY)) {
842 FIXME("array assign on type %d\n", V_VT(v));
843 return E_FAIL;
846 switch(V_VT(v)) {
847 case VT_ARRAY|VT_BYREF|VT_VARIANT:
848 array = *V_ARRAYREF(v);
849 break;
850 case VT_ARRAY|VT_VARIANT:
851 array = V_ARRAY(v);
852 break;
853 default:
854 FIXME("Unsupported array type %x\n", V_VT(v));
855 return E_NOTIMPL;
858 if(!array) {
859 FIXME("null array\n");
860 return E_FAIL;
863 hres = array_access(ctx, array, dp, &v);
864 if(FAILED(hres))
865 return hres;
866 }else if(V_VT(v) == (VT_ARRAY|VT_BYREF|VT_VARIANT)) {
867 FIXME("non-array assign\n");
868 return E_NOTIMPL;
871 hres = assign_value(ctx, v, dp->rgvarg, flags);
872 break;
874 case REF_DISP:
875 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, flags, dp);
876 break;
877 case REF_FUNC:
878 FIXME("functions not implemented\n");
879 return E_NOTIMPL;
880 case REF_OBJ:
881 FIXME("REF_OBJ\n");
882 return E_NOTIMPL;
883 case REF_CONST:
884 FIXME("REF_CONST\n");
885 return E_NOTIMPL;
886 case REF_NONE:
887 if(ctx->func->code_ctx->option_explicit) {
888 FIXME("throw exception\n");
889 hres = E_FAIL;
890 }else {
891 VARIANT *new_var;
893 if(arg_cnt(dp)) {
894 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
895 return E_NOTIMPL;
898 TRACE("creating variable %s\n", debugstr_w(name));
899 hres = add_dynamic_var(ctx, name, FALSE, &new_var);
900 if(SUCCEEDED(hres))
901 hres = assign_value(ctx, new_var, dp->rgvarg, flags);
905 return hres;
908 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
910 const BSTR arg = ctx->instr->arg1.bstr;
911 const unsigned arg_cnt = ctx->instr->arg2.uint;
912 DISPPARAMS dp;
913 HRESULT hres;
915 TRACE("%s\n", debugstr_w(arg));
917 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
918 hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUT, &dp);
919 if(FAILED(hres))
920 return hres;
922 stack_popn(ctx, arg_cnt+1);
923 return S_OK;
926 static HRESULT interp_set_ident(exec_ctx_t *ctx)
928 const BSTR arg = ctx->instr->arg1.bstr;
929 const unsigned arg_cnt = ctx->instr->arg2.uint;
930 DISPPARAMS dp;
931 HRESULT hres;
933 TRACE("%s %u\n", debugstr_w(arg), arg_cnt);
935 hres = stack_assume_disp(ctx, arg_cnt, NULL);
936 if(FAILED(hres))
937 return hres;
939 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
940 hres = assign_ident(ctx, arg, DISPATCH_PROPERTYPUTREF, &dp);
941 if(FAILED(hres))
942 return hres;
944 stack_popn(ctx, arg_cnt + 1);
945 return S_OK;
948 static HRESULT interp_assign_member(exec_ctx_t *ctx)
950 BSTR identifier = ctx->instr->arg1.bstr;
951 const unsigned arg_cnt = ctx->instr->arg2.uint;
952 IDispatch *obj;
953 DISPPARAMS dp;
954 DISPID id;
955 HRESULT hres;
957 TRACE("%s\n", debugstr_w(identifier));
959 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
960 if(FAILED(hres))
961 return hres;
963 if(!obj) {
964 FIXME("NULL obj\n");
965 return E_FAIL;
968 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
969 if(SUCCEEDED(hres)) {
970 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
971 hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUT, &dp);
973 if(FAILED(hres))
974 return hres;
976 stack_popn(ctx, arg_cnt+2);
977 return S_OK;
980 static HRESULT interp_set_member(exec_ctx_t *ctx)
982 BSTR identifier = ctx->instr->arg1.bstr;
983 const unsigned arg_cnt = ctx->instr->arg2.uint;
984 IDispatch *obj;
985 DISPPARAMS dp;
986 DISPID id;
987 HRESULT hres;
989 TRACE("%s\n", debugstr_w(identifier));
991 if(arg_cnt) {
992 FIXME("arguments not supported\n");
993 return E_NOTIMPL;
996 hres = stack_assume_disp(ctx, 1, &obj);
997 if(FAILED(hres))
998 return hres;
1000 if(!obj) {
1001 FIXME("NULL obj\n");
1002 return E_FAIL;
1005 hres = stack_assume_disp(ctx, 0, NULL);
1006 if(FAILED(hres))
1007 return hres;
1009 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
1010 if(SUCCEEDED(hres)) {
1011 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
1012 hres = disp_propput(ctx->script, obj, id, DISPATCH_PROPERTYPUTREF, &dp);
1014 if(FAILED(hres))
1015 return hres;
1017 stack_popn(ctx, 2);
1018 return S_OK;
1021 static HRESULT interp_const(exec_ctx_t *ctx)
1023 BSTR arg = ctx->instr->arg1.bstr;
1024 VARIANT *v;
1025 ref_t ref;
1026 HRESULT hres;
1028 TRACE("%s\n", debugstr_w(arg));
1030 assert(ctx->func->type == FUNC_GLOBAL);
1032 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
1033 if(FAILED(hres))
1034 return hres;
1036 if(ref.type != REF_NONE) {
1037 FIXME("%s already defined\n", debugstr_w(arg));
1038 return E_FAIL;
1041 hres = stack_assume_val(ctx, 0);
1042 if(FAILED(hres))
1043 return hres;
1045 hres = add_dynamic_var(ctx, arg, TRUE, &v);
1046 if(FAILED(hres))
1047 return hres;
1049 *v = *stack_pop(ctx);
1050 return S_OK;
1053 static HRESULT interp_val(exec_ctx_t *ctx)
1055 variant_val_t val;
1056 VARIANT v;
1057 HRESULT hres;
1059 TRACE("\n");
1061 hres = stack_pop_val(ctx, &val);
1062 if(FAILED(hres))
1063 return hres;
1065 if(!val.owned) {
1066 V_VT(&v) = VT_EMPTY;
1067 hres = VariantCopy(&v, val.v);
1068 if(FAILED(hres))
1069 return hres;
1072 return stack_push(ctx, val.owned ? val.v : &v);
1075 static HRESULT interp_pop(exec_ctx_t *ctx)
1077 const unsigned n = ctx->instr->arg1.uint;
1079 TRACE("%u\n", n);
1081 stack_popn(ctx, n);
1082 return S_OK;
1085 static HRESULT interp_stack(exec_ctx_t *ctx)
1087 const unsigned n = ctx->instr->arg1.uint;
1088 VARIANT v;
1089 HRESULT hres;
1091 TRACE("%#x\n", n);
1093 if(n == ~0)
1094 return MAKE_VBSERROR(505);
1095 assert(n < ctx->top);
1097 V_VT(&v) = VT_EMPTY;
1098 hres = VariantCopy(&v, ctx->stack + n);
1099 if(FAILED(hres))
1100 return hres;
1102 return stack_push(ctx, &v);
1105 static HRESULT interp_deref(exec_ctx_t *ctx)
1107 VARIANT copy, *v = stack_top(ctx, 0);
1108 HRESULT hres;
1110 TRACE("%s\n", debugstr_variant(v));
1112 if(V_VT(v) != (VT_BYREF|VT_VARIANT))
1113 return S_OK;
1115 V_VT(&copy) = VT_EMPTY;
1116 hres = VariantCopy(&copy, V_VARIANTREF(v));
1117 if(SUCCEEDED(hres))
1118 *v = copy;
1119 return hres;
1122 static HRESULT interp_new(exec_ctx_t *ctx)
1124 const WCHAR *arg = ctx->instr->arg1.bstr;
1125 class_desc_t *class_desc = NULL;
1126 vbdisp_t *obj;
1127 VARIANT v;
1128 HRESULT hres;
1130 TRACE("%s\n", debugstr_w(arg));
1132 if(!wcsicmp(arg, L"regexp")) {
1133 V_VT(&v) = VT_DISPATCH;
1134 hres = create_regexp(&V_DISPATCH(&v));
1135 if(FAILED(hres))
1136 return hres;
1138 return stack_push(ctx, &v);
1141 if(ctx->code->named_item)
1142 for(class_desc = ctx->code->named_item->script_obj->classes; class_desc; class_desc = class_desc->next)
1143 if(!wcsicmp(class_desc->name, arg))
1144 break;
1145 if(!class_desc)
1146 for(class_desc = ctx->script->script_obj->classes; class_desc; class_desc = class_desc->next)
1147 if(!wcsicmp(class_desc->name, arg))
1148 break;
1149 if(!class_desc) {
1150 FIXME("Class %s not found\n", debugstr_w(arg));
1151 return E_FAIL;
1154 hres = create_vbdisp(class_desc, &obj);
1155 if(FAILED(hres))
1156 return hres;
1158 V_VT(&v) = VT_DISPATCH;
1159 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
1160 return stack_push(ctx, &v);
1163 static HRESULT interp_dim(exec_ctx_t *ctx)
1165 ScriptDisp *script_obj = ctx->code->named_item ? ctx->code->named_item->script_obj : ctx->script->script_obj;
1166 const BSTR ident = ctx->instr->arg1.bstr;
1167 const unsigned array_id = ctx->instr->arg2.uint;
1168 const array_desc_t *array_desc;
1169 SAFEARRAY **array_ref;
1170 VARIANT *v;
1171 HRESULT hres;
1173 TRACE("%s\n", debugstr_w(ident));
1175 assert(array_id < ctx->func->array_cnt);
1177 if(ctx->func->type == FUNC_GLOBAL) {
1178 unsigned i;
1179 for(i = 0; i < script_obj->global_vars_cnt; i++) {
1180 if(!wcsicmp(script_obj->global_vars[i]->name, ident))
1181 break;
1183 assert(i < script_obj->global_vars_cnt);
1184 v = &script_obj->global_vars[i]->v;
1185 array_ref = &script_obj->global_vars[i]->array;
1186 }else {
1187 ref_t ref;
1189 if(!ctx->arrays) {
1190 ctx->arrays = heap_alloc_zero(ctx->func->array_cnt * sizeof(SAFEARRAY*));
1191 if(!ctx->arrays)
1192 return E_OUTOFMEMORY;
1195 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1196 if(FAILED(hres)) {
1197 FIXME("lookup %s failed: %08x\n", debugstr_w(ident), hres);
1198 return hres;
1201 if(ref.type != REF_VAR) {
1202 FIXME("got ref.type = %d\n", ref.type);
1203 return E_FAIL;
1206 v = ref.u.v;
1207 array_ref = ctx->arrays + array_id;
1210 if(*array_ref) {
1211 FIXME("Array already initialized\n");
1212 return E_FAIL;
1215 array_desc = ctx->func->array_descs + array_id;
1216 if(array_desc->dim_cnt) {
1217 *array_ref = SafeArrayCreate(VT_VARIANT, array_desc->dim_cnt, array_desc->bounds);
1218 if(!*array_ref)
1219 return E_OUTOFMEMORY;
1222 V_VT(v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1223 V_ARRAYREF(v) = array_ref;
1224 return S_OK;
1227 static HRESULT array_bounds_from_stack(exec_ctx_t *ctx, unsigned dim_cnt, SAFEARRAYBOUND **ret)
1229 SAFEARRAYBOUND *bounds;
1230 unsigned i;
1231 int dim;
1232 HRESULT hres;
1234 if(!(bounds = heap_alloc(dim_cnt * sizeof(*bounds))))
1235 return E_OUTOFMEMORY;
1237 for(i = 0; i < dim_cnt; i++) {
1238 hres = to_int(stack_top(ctx, dim_cnt - i - 1), &dim);
1239 if(FAILED(hres)) {
1240 heap_free(bounds);
1241 return hres;
1244 bounds[i].cElements = dim + 1;
1245 bounds[i].lLbound = 0;
1248 stack_popn(ctx, dim_cnt);
1249 *ret = bounds;
1250 return S_OK;
1253 static HRESULT interp_redim(exec_ctx_t *ctx)
1255 BSTR identifier = ctx->instr->arg1.bstr;
1256 const unsigned dim_cnt = ctx->instr->arg2.uint;
1257 SAFEARRAYBOUND *bounds;
1258 SAFEARRAY *array;
1259 ref_t ref;
1260 HRESULT hres;
1262 TRACE("%s %u\n", debugstr_w(identifier), dim_cnt);
1264 hres = lookup_identifier(ctx, identifier, VBDISP_LET, &ref);
1265 if(FAILED(hres)) {
1266 FIXME("lookup %s failed: %08x\n", debugstr_w(identifier), hres);
1267 return hres;
1270 if(ref.type != REF_VAR) {
1271 FIXME("got ref.type = %d\n", ref.type);
1272 return E_FAIL;
1275 hres = array_bounds_from_stack(ctx, dim_cnt, &bounds);
1276 if(FAILED(hres))
1277 return hres;
1279 array = SafeArrayCreate(VT_VARIANT, dim_cnt, bounds);
1280 heap_free(bounds);
1281 if(!array)
1282 return E_OUTOFMEMORY;
1284 /* FIXME: We should check if we're not modifying an existing static array here */
1286 VariantClear(ref.u.v);
1287 V_VT(ref.u.v) = VT_ARRAY|VT_VARIANT;
1288 V_ARRAY(ref.u.v) = array;
1289 return S_OK;
1292 static HRESULT interp_redim_preserve(exec_ctx_t *ctx)
1294 BSTR identifier = ctx->instr->arg1.bstr;
1295 const unsigned dim_cnt = ctx->instr->arg2.uint;
1296 unsigned i;
1297 SAFEARRAYBOUND *bounds;
1298 SAFEARRAY *array;
1299 ref_t ref;
1300 HRESULT hres;
1302 TRACE("%s %u\n", debugstr_w(identifier), dim_cnt);
1304 hres = lookup_identifier(ctx, identifier, VBDISP_LET, &ref);
1305 if(FAILED(hres)) {
1306 FIXME("lookup %s failed: %08x\n", debugstr_w(identifier), hres);
1307 return hres;
1310 if(ref.type != REF_VAR) {
1311 FIXME("got ref.type = %d\n", ref.type);
1312 return E_FAIL;
1315 if(!(V_VT(ref.u.v) & VT_ARRAY)) {
1316 FIXME("ReDim Preserve not valid on type %d\n", V_VT(ref.u.v));
1317 return E_FAIL;
1320 array = V_ARRAY(ref.u.v);
1322 hres = array_bounds_from_stack(ctx, dim_cnt, &bounds);
1323 if(FAILED(hres))
1324 return hres;
1326 if(array == NULL || array->cDims == 0) {
1327 /* can initially allocate the array */
1328 array = SafeArrayCreate(VT_VARIANT, dim_cnt, bounds);
1329 VariantClear(ref.u.v);
1330 V_VT(ref.u.v) = VT_ARRAY|VT_VARIANT;
1331 V_ARRAY(ref.u.v) = array;
1332 return S_OK;
1333 } else if(array->cDims != dim_cnt) {
1334 /* can't otherwise change the number of dimensions */
1335 TRACE("Can't resize %s, cDims %d != %d\n", debugstr_w(identifier), array->cDims, dim_cnt);
1336 return MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS);
1337 } else {
1338 /* can resize the last dimensions (if others match */
1339 for(i = 0; i+1 < dim_cnt; ++i) {
1340 if(array->rgsabound[array->cDims - 1 - i].cElements != bounds[i].cElements) {
1341 TRACE("Can't resize %s, bound[%d] %d != %d\n", debugstr_w(identifier), i, array->rgsabound[i].cElements, bounds[i].cElements);
1342 return MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS);
1345 return SafeArrayRedim(array, &bounds[dim_cnt-1]);
1349 static HRESULT interp_step(exec_ctx_t *ctx)
1351 const BSTR ident = ctx->instr->arg2.bstr;
1352 BOOL gteq_zero;
1353 VARIANT zero;
1354 ref_t ref;
1355 HRESULT hres;
1357 TRACE("%s\n", debugstr_w(ident));
1359 V_VT(&zero) = VT_I2;
1360 V_I2(&zero) = 0;
1361 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1362 if(FAILED(hres))
1363 return hres;
1365 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1367 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1368 if(FAILED(hres))
1369 return hres;
1371 if(ref.type != REF_VAR) {
1372 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1373 return E_FAIL;
1376 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1377 if(FAILED(hres))
1378 return hres;
1380 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1381 ctx->instr++;
1382 }else {
1383 stack_popn(ctx, 2);
1384 instr_jmp(ctx, ctx->instr->arg1.uint);
1386 return S_OK;
1389 static HRESULT interp_newenum(exec_ctx_t *ctx)
1391 variant_val_t v;
1392 VARIANT *r;
1393 HRESULT hres;
1395 TRACE("\n");
1397 stack_pop_deref(ctx, &v);
1398 assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
1399 r = stack_top(ctx, 0);
1401 switch(V_VT(v.v)) {
1402 case VT_DISPATCH|VT_BYREF:
1403 case VT_DISPATCH: {
1404 IEnumVARIANT *iter;
1405 DISPPARAMS dp = {0};
1406 VARIANT iterv;
1408 hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
1409 release_val(&v);
1410 if(FAILED(hres))
1411 return hres;
1413 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1414 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1415 VariantClear(&iterv);
1416 return hres;
1419 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1420 IUnknown_Release(V_UNKNOWN(&iterv));
1421 if(FAILED(hres)) {
1422 FIXME("Could not get IEnumVARIANT iface: %08x\n", hres);
1423 return hres;
1426 V_VT(r) = VT_UNKNOWN;
1427 V_UNKNOWN(r) = (IUnknown*)iter;
1428 break;
1430 case VT_VARIANT|VT_ARRAY:
1431 case VT_VARIANT|VT_ARRAY|VT_BYREF: {
1432 IEnumVARIANT *iter;
1434 hres = create_safearray_iter(V_ISBYREF(v.v) ? *V_ARRAYREF(v.v) : V_ARRAY(v.v), &iter);
1435 if(FAILED(hres))
1436 return hres;
1438 V_VT(r) = VT_UNKNOWN;
1439 V_UNKNOWN(r) = (IUnknown*)iter;
1440 break;
1442 default:
1443 FIXME("Unsupported for %s\n", debugstr_variant(v.v));
1444 release_val(&v);
1445 return E_NOTIMPL;
1448 return S_OK;
1451 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1453 const unsigned loop_end = ctx->instr->arg1.uint;
1454 const BSTR ident = ctx->instr->arg2.bstr;
1455 VARIANT v;
1456 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1457 IEnumVARIANT *iter;
1458 BOOL do_continue;
1459 HRESULT hres;
1461 TRACE("\n");
1463 if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
1464 FIXME("uninitialized\n");
1465 return E_FAIL;
1468 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1469 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1471 V_VT(&v) = VT_EMPTY;
1472 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1473 if(FAILED(hres))
1474 return hres;
1476 do_continue = hres == S_OK;
1477 hres = assign_ident(ctx, ident, DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &dp);
1478 VariantClear(&v);
1479 if(FAILED(hres))
1480 return hres;
1482 if(do_continue) {
1483 ctx->instr++;
1484 }else {
1485 stack_popn(ctx, 1);
1486 instr_jmp(ctx, loop_end);
1488 return S_OK;
1491 static HRESULT interp_jmp(exec_ctx_t *ctx)
1493 const unsigned arg = ctx->instr->arg1.uint;
1495 TRACE("%u\n", arg);
1497 instr_jmp(ctx, arg);
1498 return S_OK;
1501 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1503 const unsigned arg = ctx->instr->arg1.uint;
1504 HRESULT hres;
1505 BOOL b;
1507 TRACE("%u\n", arg);
1509 hres = stack_pop_bool(ctx, &b);
1510 if(FAILED(hres))
1511 return hres;
1513 if(b)
1514 ctx->instr++;
1515 else
1516 instr_jmp(ctx, ctx->instr->arg1.uint);
1517 return S_OK;
1520 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1522 const unsigned arg = ctx->instr->arg1.uint;
1523 HRESULT hres;
1524 BOOL b;
1526 TRACE("%u\n", arg);
1528 hres = stack_pop_bool(ctx, &b);
1529 if(FAILED(hres))
1530 return hres;
1532 if(b)
1533 instr_jmp(ctx, ctx->instr->arg1.uint);
1534 else
1535 ctx->instr++;
1536 return S_OK;
1539 static HRESULT interp_ret(exec_ctx_t *ctx)
1541 TRACE("\n");
1543 ctx->instr = NULL;
1544 return S_OK;
1547 static HRESULT interp_retval(exec_ctx_t *ctx)
1549 variant_val_t val;
1550 HRESULT hres;
1552 TRACE("\n");
1554 stack_pop_deref(ctx, &val);
1556 if(val.owned) {
1557 VariantClear(&ctx->ret_val);
1558 ctx->ret_val = *val.v;
1560 else {
1561 hres = VariantCopy(&ctx->ret_val, val.v);
1562 if(FAILED(hres))
1563 return hres;
1566 return S_OK;
1569 static HRESULT interp_stop(exec_ctx_t *ctx)
1571 WARN("\n");
1573 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1574 return S_OK;
1577 static HRESULT interp_me(exec_ctx_t *ctx)
1579 IDispatch *disp;
1580 VARIANT v;
1582 TRACE("\n");
1584 if(ctx->vbthis) {
1585 disp = (IDispatch*)&ctx->vbthis->IDispatchEx_iface;
1586 }else if(ctx->code->named_item) {
1587 disp = (ctx->code->named_item->flags & SCRIPTITEM_CODEONLY)
1588 ? (IDispatch*)&ctx->code->named_item->script_obj->IDispatchEx_iface
1589 : ctx->code->named_item->disp;
1590 }else {
1591 named_item_t *item;
1592 disp = NULL;
1593 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
1594 if(!(item->flags & SCRIPTITEM_GLOBALMEMBERS)) continue;
1595 disp = item->disp;
1596 break;
1598 if(!disp)
1599 disp = (IDispatch*)&ctx->script->script_obj->IDispatchEx_iface;
1602 IDispatch_AddRef(disp);
1603 V_VT(&v) = VT_DISPATCH;
1604 V_DISPATCH(&v) = disp;
1605 return stack_push(ctx, &v);
1608 static HRESULT interp_bool(exec_ctx_t *ctx)
1610 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1611 VARIANT v;
1613 TRACE("%s\n", arg ? "true" : "false");
1615 V_VT(&v) = VT_BOOL;
1616 V_BOOL(&v) = arg;
1617 return stack_push(ctx, &v);
1620 static HRESULT interp_errmode(exec_ctx_t *ctx)
1622 const int err_mode = ctx->instr->arg1.uint;
1624 TRACE("%d\n", err_mode);
1626 ctx->resume_next = err_mode;
1627 clear_ei(&ctx->script->ei);
1628 return S_OK;
1631 static HRESULT interp_string(exec_ctx_t *ctx)
1633 VARIANT v;
1635 TRACE("\n");
1637 V_VT(&v) = VT_BSTR;
1638 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1639 if(!V_BSTR(&v))
1640 return E_OUTOFMEMORY;
1642 return stack_push(ctx, &v);
1645 static HRESULT interp_int(exec_ctx_t *ctx)
1647 const LONG arg = ctx->instr->arg1.lng;
1648 VARIANT v;
1650 TRACE("%d\n", arg);
1652 if(arg == (INT16)arg) {
1653 V_VT(&v) = VT_I2;
1654 V_I2(&v) = arg;
1655 }else {
1656 V_VT(&v) = VT_I4;
1657 V_I4(&v) = arg;
1659 return stack_push(ctx, &v);
1662 static HRESULT interp_double(exec_ctx_t *ctx)
1664 const DOUBLE *arg = ctx->instr->arg1.dbl;
1665 VARIANT v;
1667 TRACE("%lf\n", *arg);
1669 V_VT(&v) = VT_R8;
1670 V_R8(&v) = *arg;
1671 return stack_push(ctx, &v);
1674 static HRESULT interp_empty(exec_ctx_t *ctx)
1676 VARIANT v;
1678 TRACE("\n");
1680 V_VT(&v) = VT_EMPTY;
1681 return stack_push(ctx, &v);
1684 static HRESULT interp_null(exec_ctx_t *ctx)
1686 TRACE("\n");
1687 return stack_push_null(ctx);
1690 static HRESULT interp_nothing(exec_ctx_t *ctx)
1692 VARIANT v;
1694 TRACE("\n");
1696 V_VT(&v) = VT_DISPATCH;
1697 V_DISPATCH(&v) = NULL;
1698 return stack_push(ctx, &v);
1701 static HRESULT interp_hres(exec_ctx_t *ctx)
1703 const unsigned arg = ctx->instr->arg1.uint;
1704 VARIANT v;
1706 TRACE("%d\n", arg);
1708 V_VT(&v) = VT_ERROR;
1709 V_ERROR(&v) = arg;
1710 return stack_push(ctx, &v);
1713 static HRESULT interp_not(exec_ctx_t *ctx)
1715 variant_val_t val;
1716 VARIANT v;
1717 HRESULT hres;
1719 TRACE("\n");
1721 hres = stack_pop_val(ctx, &val);
1722 if(FAILED(hres))
1723 return hres;
1725 hres = VarNot(val.v, &v);
1726 release_val(&val);
1727 if(FAILED(hres))
1728 return hres;
1730 return stack_push(ctx, &v);
1733 static HRESULT interp_and(exec_ctx_t *ctx)
1735 variant_val_t r, l;
1736 VARIANT v;
1737 HRESULT hres;
1739 TRACE("\n");
1741 hres = stack_pop_val(ctx, &r);
1742 if(FAILED(hres))
1743 return hres;
1745 hres = stack_pop_val(ctx, &l);
1746 if(SUCCEEDED(hres)) {
1747 hres = VarAnd(l.v, r.v, &v);
1748 release_val(&l);
1750 release_val(&r);
1751 if(FAILED(hres))
1752 return hres;
1754 return stack_push(ctx, &v);
1757 static HRESULT interp_or(exec_ctx_t *ctx)
1759 variant_val_t r, l;
1760 VARIANT v;
1761 HRESULT hres;
1763 TRACE("\n");
1765 hres = stack_pop_val(ctx, &r);
1766 if(FAILED(hres))
1767 return hres;
1769 hres = stack_pop_val(ctx, &l);
1770 if(SUCCEEDED(hres)) {
1771 hres = VarOr(l.v, r.v, &v);
1772 release_val(&l);
1774 release_val(&r);
1775 if(FAILED(hres))
1776 return hres;
1778 return stack_push(ctx, &v);
1781 static HRESULT interp_xor(exec_ctx_t *ctx)
1783 variant_val_t r, l;
1784 VARIANT v;
1785 HRESULT hres;
1787 TRACE("\n");
1789 hres = stack_pop_val(ctx, &r);
1790 if(FAILED(hres))
1791 return hres;
1793 hres = stack_pop_val(ctx, &l);
1794 if(SUCCEEDED(hres)) {
1795 hres = VarXor(l.v, r.v, &v);
1796 release_val(&l);
1798 release_val(&r);
1799 if(FAILED(hres))
1800 return hres;
1802 return stack_push(ctx, &v);
1805 static HRESULT interp_eqv(exec_ctx_t *ctx)
1807 variant_val_t r, l;
1808 VARIANT v;
1809 HRESULT hres;
1811 TRACE("\n");
1813 hres = stack_pop_val(ctx, &r);
1814 if(FAILED(hres))
1815 return hres;
1817 hres = stack_pop_val(ctx, &l);
1818 if(SUCCEEDED(hres)) {
1819 hres = VarEqv(l.v, r.v, &v);
1820 release_val(&l);
1822 release_val(&r);
1823 if(FAILED(hres))
1824 return hres;
1826 return stack_push(ctx, &v);
1829 static HRESULT interp_imp(exec_ctx_t *ctx)
1831 variant_val_t r, l;
1832 VARIANT v;
1833 HRESULT hres;
1835 TRACE("\n");
1837 hres = stack_pop_val(ctx, &r);
1838 if(FAILED(hres))
1839 return hres;
1841 hres = stack_pop_val(ctx, &l);
1842 if(SUCCEEDED(hres)) {
1843 hres = VarImp(l.v, r.v, &v);
1844 release_val(&l);
1846 release_val(&r);
1847 if(FAILED(hres))
1848 return hres;
1850 return stack_push(ctx, &v);
1853 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1855 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1857 /* FIXME: Fix comparing string to number */
1859 return VarCmp(l, r, ctx->script->lcid, 0);
1862 static HRESULT cmp_oper(exec_ctx_t *ctx)
1864 variant_val_t l, r;
1865 HRESULT hres;
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 = var_cmp(ctx, l.v, r.v);
1874 release_val(&l);
1877 release_val(&r);
1878 return hres;
1881 static HRESULT interp_equal(exec_ctx_t *ctx)
1883 VARIANT v;
1884 HRESULT hres;
1886 TRACE("\n");
1888 hres = cmp_oper(ctx);
1889 if(FAILED(hres))
1890 return hres;
1891 if(hres == VARCMP_NULL)
1892 return stack_push_null(ctx);
1894 V_VT(&v) = VT_BOOL;
1895 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1896 return stack_push(ctx, &v);
1899 static HRESULT interp_nequal(exec_ctx_t *ctx)
1901 VARIANT v;
1902 HRESULT hres;
1904 TRACE("\n");
1906 hres = cmp_oper(ctx);
1907 if(FAILED(hres))
1908 return hres;
1909 if(hres == VARCMP_NULL)
1910 return stack_push_null(ctx);
1912 V_VT(&v) = VT_BOOL;
1913 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1914 return stack_push(ctx, &v);
1917 static HRESULT interp_gt(exec_ctx_t *ctx)
1919 VARIANT v;
1920 HRESULT hres;
1922 TRACE("\n");
1924 hres = cmp_oper(ctx);
1925 if(FAILED(hres))
1926 return hres;
1927 if(hres == VARCMP_NULL)
1928 return stack_push_null(ctx);
1930 V_VT(&v) = VT_BOOL;
1931 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1932 return stack_push(ctx, &v);
1935 static HRESULT interp_gteq(exec_ctx_t *ctx)
1937 VARIANT v;
1938 HRESULT hres;
1940 TRACE("\n");
1942 hres = cmp_oper(ctx);
1943 if(FAILED(hres))
1944 return hres;
1945 if(hres == VARCMP_NULL)
1946 return stack_push_null(ctx);
1948 V_VT(&v) = VT_BOOL;
1949 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1950 return stack_push(ctx, &v);
1953 static HRESULT interp_lt(exec_ctx_t *ctx)
1955 VARIANT v;
1956 HRESULT hres;
1958 TRACE("\n");
1960 hres = cmp_oper(ctx);
1961 if(FAILED(hres))
1962 return hres;
1963 if(hres == VARCMP_NULL)
1964 return stack_push_null(ctx);
1966 V_VT(&v) = VT_BOOL;
1967 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1968 return stack_push(ctx, &v);
1971 static HRESULT interp_lteq(exec_ctx_t *ctx)
1973 VARIANT v;
1974 HRESULT hres;
1976 TRACE("\n");
1978 hres = cmp_oper(ctx);
1979 if(FAILED(hres))
1980 return hres;
1981 if(hres == VARCMP_NULL)
1982 return stack_push_null(ctx);
1984 V_VT(&v) = VT_BOOL;
1985 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1986 return stack_push(ctx, &v);
1989 static HRESULT interp_case(exec_ctx_t *ctx)
1991 const unsigned arg = ctx->instr->arg1.uint;
1992 variant_val_t v;
1993 HRESULT hres;
1995 TRACE("%d\n", arg);
1997 hres = stack_pop_val(ctx, &v);
1998 if(FAILED(hres))
1999 return hres;
2001 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
2002 release_val(&v);
2003 if(FAILED(hres))
2004 return hres;
2006 if(hres == VARCMP_EQ) {
2007 stack_popn(ctx, 1);
2008 instr_jmp(ctx, arg);
2009 }else {
2010 ctx->instr++;
2013 return S_OK;
2016 static HRESULT interp_is(exec_ctx_t *ctx)
2018 IUnknown *l = NULL, *r = NULL;
2019 variant_val_t v;
2020 HRESULT hres = S_OK;
2022 TRACE("\n");
2024 stack_pop_deref(ctx, &v);
2025 if(V_VT(v.v) != VT_DISPATCH && V_VT(v.v) != VT_UNKNOWN) {
2026 FIXME("Unhandled type %s\n", debugstr_variant(v.v));
2027 hres = E_NOTIMPL;
2028 }else if(V_UNKNOWN(v.v)) {
2029 hres = IUnknown_QueryInterface(V_UNKNOWN(v.v), &IID_IUnknown, (void**)&r);
2031 if(v.owned) VariantClear(v.v);
2032 if(FAILED(hres))
2033 return hres;
2035 stack_pop_deref(ctx, &v);
2036 if(V_VT(v.v) != VT_DISPATCH && V_VT(v.v) != VT_UNKNOWN) {
2037 FIXME("Unhandled type %s\n", debugstr_variant(v.v));
2038 hres = E_NOTIMPL;
2039 }else if(V_UNKNOWN(v.v)) {
2040 hres = IUnknown_QueryInterface(V_UNKNOWN(v.v), &IID_IUnknown, (void**)&l);
2042 if(v.owned) VariantClear(v.v);
2044 if(SUCCEEDED(hres)) {
2045 VARIANT res;
2046 V_VT(&res) = VT_BOOL;
2047 if(r == l)
2048 V_BOOL(&res) = VARIANT_TRUE;
2049 else if(!r || !l)
2050 V_BOOL(&res) = VARIANT_FALSE;
2051 else {
2052 IObjectIdentity *identity;
2053 hres = IUnknown_QueryInterface(l, &IID_IObjectIdentity, (void**)&identity);
2054 if(SUCCEEDED(hres)) {
2055 hres = IObjectIdentity_IsEqualObject(identity, r);
2056 IObjectIdentity_Release(identity);
2058 V_BOOL(&res) = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
2060 hres = stack_push(ctx, &res);
2062 if(r)
2063 IUnknown_Release(r);
2064 if(l)
2065 IUnknown_Release(l);
2066 return hres;
2069 static HRESULT interp_concat(exec_ctx_t *ctx)
2071 variant_val_t r, l;
2072 VARIANT v;
2073 HRESULT hres;
2075 TRACE("\n");
2077 hres = stack_pop_val(ctx, &r);
2078 if(FAILED(hres))
2079 return hres;
2081 hres = stack_pop_val(ctx, &l);
2082 if(SUCCEEDED(hres)) {
2083 hres = VarCat(l.v, r.v, &v);
2084 release_val(&l);
2086 release_val(&r);
2087 if(FAILED(hres))
2088 return hres;
2090 return stack_push(ctx, &v);
2093 static HRESULT interp_add(exec_ctx_t *ctx)
2095 variant_val_t r, l;
2096 VARIANT v;
2097 HRESULT hres;
2099 TRACE("\n");
2101 hres = stack_pop_val(ctx, &r);
2102 if(FAILED(hres))
2103 return hres;
2105 hres = stack_pop_val(ctx, &l);
2106 if(SUCCEEDED(hres)) {
2107 hres = VarAdd(l.v, r.v, &v);
2108 release_val(&l);
2110 release_val(&r);
2111 if(FAILED(hres))
2112 return hres;
2114 return stack_push(ctx, &v);
2117 static HRESULT interp_sub(exec_ctx_t *ctx)
2119 variant_val_t r, l;
2120 VARIANT v;
2121 HRESULT hres;
2123 TRACE("\n");
2125 hres = stack_pop_val(ctx, &r);
2126 if(FAILED(hres))
2127 return hres;
2129 hres = stack_pop_val(ctx, &l);
2130 if(SUCCEEDED(hres)) {
2131 hres = VarSub(l.v, r.v, &v);
2132 release_val(&l);
2134 release_val(&r);
2135 if(FAILED(hres))
2136 return hres;
2138 return stack_push(ctx, &v);
2141 static HRESULT interp_mod(exec_ctx_t *ctx)
2143 variant_val_t r, l;
2144 VARIANT v;
2145 HRESULT hres;
2147 TRACE("\n");
2149 hres = stack_pop_val(ctx, &r);
2150 if(FAILED(hres))
2151 return hres;
2153 hres = stack_pop_val(ctx, &l);
2154 if(SUCCEEDED(hres)) {
2155 hres = VarMod(l.v, r.v, &v);
2156 release_val(&l);
2158 release_val(&r);
2159 if(FAILED(hres))
2160 return hres;
2162 return stack_push(ctx, &v);
2165 static HRESULT interp_idiv(exec_ctx_t *ctx)
2167 variant_val_t r, l;
2168 VARIANT v;
2169 HRESULT hres;
2171 TRACE("\n");
2173 hres = stack_pop_val(ctx, &r);
2174 if(FAILED(hres))
2175 return hres;
2177 hres = stack_pop_val(ctx, &l);
2178 if(SUCCEEDED(hres)) {
2179 hres = VarIdiv(l.v, r.v, &v);
2180 release_val(&l);
2182 release_val(&r);
2183 if(FAILED(hres))
2184 return hres;
2186 return stack_push(ctx, &v);
2189 static HRESULT interp_div(exec_ctx_t *ctx)
2191 variant_val_t r, l;
2192 VARIANT v;
2193 HRESULT hres;
2195 TRACE("\n");
2197 hres = stack_pop_val(ctx, &r);
2198 if(FAILED(hres))
2199 return hres;
2201 hres = stack_pop_val(ctx, &l);
2202 if(SUCCEEDED(hres)) {
2203 hres = VarDiv(l.v, r.v, &v);
2204 release_val(&l);
2206 release_val(&r);
2207 if(FAILED(hres))
2208 return hres;
2210 return stack_push(ctx, &v);
2213 static HRESULT interp_mul(exec_ctx_t *ctx)
2215 variant_val_t r, l;
2216 VARIANT v;
2217 HRESULT hres;
2219 TRACE("\n");
2221 hres = stack_pop_val(ctx, &r);
2222 if(FAILED(hres))
2223 return hres;
2225 hres = stack_pop_val(ctx, &l);
2226 if(SUCCEEDED(hres)) {
2227 hres = VarMul(l.v, r.v, &v);
2228 release_val(&l);
2230 release_val(&r);
2231 if(FAILED(hres))
2232 return hres;
2234 return stack_push(ctx, &v);
2237 static HRESULT interp_exp(exec_ctx_t *ctx)
2239 variant_val_t r, l;
2240 VARIANT v;
2241 HRESULT hres;
2243 TRACE("\n");
2245 hres = stack_pop_val(ctx, &r);
2246 if(FAILED(hres))
2247 return hres;
2249 hres = stack_pop_val(ctx, &l);
2250 if(SUCCEEDED(hres)) {
2251 hres = VarPow(l.v, r.v, &v);
2252 release_val(&l);
2254 release_val(&r);
2255 if(FAILED(hres))
2256 return hres;
2258 return stack_push(ctx, &v);
2261 static HRESULT interp_neg(exec_ctx_t *ctx)
2263 variant_val_t val;
2264 VARIANT v;
2265 HRESULT hres;
2267 hres = stack_pop_val(ctx, &val);
2268 if(FAILED(hres))
2269 return hres;
2271 hres = VarNeg(val.v, &v);
2272 release_val(&val);
2273 if(FAILED(hres))
2274 return hres;
2276 return stack_push(ctx, &v);
2279 static HRESULT interp_incc(exec_ctx_t *ctx)
2281 const BSTR ident = ctx->instr->arg1.bstr;
2282 VARIANT v;
2283 ref_t ref;
2284 HRESULT hres;
2286 TRACE("\n");
2288 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
2289 if(FAILED(hres))
2290 return hres;
2292 if(ref.type != REF_VAR) {
2293 FIXME("ref.type is not REF_VAR\n");
2294 return E_FAIL;
2297 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
2298 if(FAILED(hres))
2299 return hres;
2301 VariantClear(ref.u.v);
2302 *ref.u.v = v;
2303 return S_OK;
2306 static HRESULT interp_catch(exec_ctx_t *ctx)
2308 /* Nothing to do here, the OP is for unwinding only. */
2309 return S_OK;
2312 static const instr_func_t op_funcs[] = {
2313 #define X(x,n,a,b) interp_ ## x,
2314 OP_LIST
2315 #undef X
2318 static const unsigned op_move[] = {
2319 #define X(x,n,a,b) n,
2320 OP_LIST
2321 #undef X
2324 void release_dynamic_var(dynamic_var_t *var)
2326 VariantClear(&var->v);
2327 if(var->array)
2328 SafeArrayDestroy(var->array);
2331 static void release_exec(exec_ctx_t *ctx)
2333 dynamic_var_t *var;
2334 unsigned i;
2336 VariantClear(&ctx->ret_val);
2338 for(var = ctx->dynamic_vars; var; var = var->next)
2339 release_dynamic_var(var);
2341 if(ctx->vbthis)
2342 IDispatchEx_Release(&ctx->vbthis->IDispatchEx_iface);
2344 if(ctx->args) {
2345 for(i=0; i < ctx->func->arg_cnt; i++)
2346 VariantClear(ctx->args+i);
2349 if(ctx->vars) {
2350 for(i=0; i < ctx->func->var_cnt; i++)
2351 VariantClear(ctx->vars+i);
2354 if(ctx->arrays) {
2355 for(i=0; i < ctx->func->array_cnt; i++) {
2356 if(ctx->arrays[i])
2357 SafeArrayDestroy(ctx->arrays[i]);
2359 heap_free(ctx->arrays);
2362 heap_pool_free(&ctx->heap);
2363 heap_free(ctx->args);
2364 heap_free(ctx->vars);
2365 heap_free(ctx->stack);
2368 HRESULT exec_script(script_ctx_t *ctx, BOOL extern_caller, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2370 exec_ctx_t exec = {func->code_ctx};
2371 vbsop_t op;
2372 HRESULT hres = S_OK;
2374 exec.code = func->code_ctx;
2376 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2377 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2378 return E_FAIL;
2381 heap_pool_init(&exec.heap);
2383 TRACE("%s(", debugstr_w(func->name));
2384 if(func->arg_cnt) {
2385 VARIANT *v;
2386 unsigned i;
2388 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
2389 if(!exec.args) {
2390 release_exec(&exec);
2391 return E_OUTOFMEMORY;
2394 for(i=0; i < func->arg_cnt; i++) {
2395 v = get_arg(dp, i);
2396 TRACE("%s%s", i ? ", " : "", debugstr_variant(v));
2397 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2398 if(func->args[i].by_ref)
2399 exec.args[i] = *v;
2400 else
2401 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2402 }else {
2403 hres = VariantCopyInd(exec.args+i, v);
2405 if(FAILED(hres)) {
2406 release_exec(&exec);
2407 return hres;
2410 }else {
2411 exec.args = NULL;
2413 TRACE(")\n");
2415 if(func->var_cnt) {
2416 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
2417 if(!exec.vars) {
2418 release_exec(&exec);
2419 return E_OUTOFMEMORY;
2421 }else {
2422 exec.vars = NULL;
2425 exec.stack_size = 16;
2426 exec.top = 0;
2427 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
2428 if(!exec.stack) {
2429 release_exec(&exec);
2430 return E_OUTOFMEMORY;
2433 if(extern_caller)
2434 IActiveScriptSite_OnEnterScript(ctx->site);
2436 if(vbthis) {
2437 IDispatchEx_AddRef(&vbthis->IDispatchEx_iface);
2438 exec.vbthis = vbthis;
2441 exec.instr = exec.code->instrs + func->code_off;
2442 exec.script = ctx;
2443 exec.func = func;
2445 while(exec.instr) {
2446 op = exec.instr->op;
2447 hres = op_funcs[op](&exec);
2448 if(FAILED(hres)) {
2449 if(hres != SCRIPT_E_RECORDED) {
2450 clear_ei(&ctx->ei);
2452 ctx->ei.scode = hres = map_hres(hres);
2453 ctx->ei.bstrSource = get_vbscript_string(VBS_RUNTIME_ERROR);
2454 ctx->ei.bstrDescription = get_vbscript_error_string(hres);
2455 }else {
2456 hres = ctx->ei.scode;
2459 if(exec.resume_next) {
2460 unsigned stack_off;
2462 WARN("Failed %08x in resume next mode\n", hres);
2465 * Unwinding here is simple. We need to find the next OP_catch, which contains
2466 * information about expected stack size and jump offset on error. Generated
2467 * bytecode needs to guarantee, that simple jump and stack adjustment will
2468 * guarantee proper execution continuation.
2470 while((++exec.instr)->op != OP_catch);
2472 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2474 clear_error_loc(ctx);
2475 stack_off = exec.instr->arg2.uint;
2476 instr_jmp(&exec, exec.instr->arg1.uint);
2478 if(exec.top > stack_off) {
2479 stack_popn(&exec, exec.top-stack_off);
2480 }else if(exec.top < stack_off) {
2481 VARIANT v;
2483 V_VT(&v) = VT_EMPTY;
2484 while(exec.top < stack_off) {
2485 hres = stack_push(&exec, &v);
2486 if(FAILED(hres))
2487 break;
2491 continue;
2492 }else {
2493 if(!ctx->error_loc_code) {
2494 grab_vbscode(exec.code);
2495 ctx->error_loc_code = exec.code;
2496 ctx->error_loc_offset = exec.instr->loc;
2498 stack_popn(&exec, exec.top);
2499 break;
2503 exec.instr += op_move[op];
2506 assert(!exec.top);
2508 if(extern_caller) {
2509 if(FAILED(hres)) {
2510 if(!ctx->ei.scode)
2511 ctx->ei.scode = hres;
2512 hres = report_script_error(ctx, ctx->error_loc_code, ctx->error_loc_offset);
2513 clear_error_loc(ctx);
2515 IActiveScriptSite_OnLeaveScript(ctx->site);
2518 if(SUCCEEDED(hres) && res) {
2519 *res = exec.ret_val;
2520 V_VT(&exec.ret_val) = VT_EMPTY;
2523 release_exec(&exec);
2524 return hres;