ddraw: Move the wined3d_texture_update_desc() call into ddraw_surface_create_wined3d_...
[wine.git] / dlls / vbscript / interp.c
blobeb1dcab5f01b9c5cfb0b777e6e2797cd0a058d12
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 = realloc(script_obj->global_vars, cnt * 2 * sizeof(*new_vars));
271 else
272 new_vars = malloc(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 = 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 HRESULT array_access(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 = malloc(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 free(indices);
569 SafeArrayUnlock(array);
570 return hres;
574 hres = SafeArrayPtrOfIndex(array, indices, (void**)ret);
575 SafeArrayUnlock(array);
576 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(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(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 = calloc(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;
1249 (*array_ref)->fFeatures |= (FADF_FIXEDSIZE | FADF_STATIC);
1252 V_VT(v) = VT_ARRAY|VT_BYREF|VT_VARIANT;
1253 V_ARRAYREF(v) = array_ref;
1254 return S_OK;
1257 static HRESULT array_bounds_from_stack(exec_ctx_t *ctx, unsigned dim_cnt, SAFEARRAYBOUND **ret)
1259 SAFEARRAYBOUND *bounds;
1260 unsigned i;
1261 int dim;
1262 HRESULT hres;
1264 if(!(bounds = malloc(dim_cnt * sizeof(*bounds))))
1265 return E_OUTOFMEMORY;
1267 for(i = 0; i < dim_cnt; i++) {
1268 hres = to_int(stack_top(ctx, dim_cnt - i - 1), &dim);
1269 if(FAILED(hres)) {
1270 free(bounds);
1271 return hres;
1274 bounds[i].cElements = dim + 1;
1275 bounds[i].lLbound = 0;
1278 stack_popn(ctx, dim_cnt);
1279 *ret = bounds;
1280 return S_OK;
1283 static HRESULT interp_redim(exec_ctx_t *ctx)
1285 BSTR identifier = ctx->instr->arg1.bstr;
1286 const unsigned dim_cnt = ctx->instr->arg2.uint;
1287 VARIANT *v;
1288 SAFEARRAYBOUND *bounds;
1289 SAFEARRAY *array;
1290 ref_t ref;
1291 HRESULT hres;
1293 TRACE("%s %u\n", debugstr_w(identifier), dim_cnt);
1295 hres = lookup_identifier(ctx, identifier, VBDISP_LET, &ref);
1296 if(FAILED(hres)) {
1297 FIXME("lookup %s failed: %08lx\n", debugstr_w(identifier), hres);
1298 return hres;
1301 if(ref.type != REF_VAR) {
1302 FIXME("got ref.type = %d\n", ref.type);
1303 return E_FAIL;
1306 v = ref.u.v;
1308 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1309 v = V_VARIANTREF(v);
1312 if(V_ISARRAY(v)) {
1313 SAFEARRAY *sa = V_ISBYREF(v) ? *V_ARRAYREF(v) : V_ARRAY(v);
1314 if(sa->fFeatures & FADF_FIXEDSIZE)
1315 return MAKE_VBSERROR(VBSE_ARRAY_LOCKED);
1318 hres = array_bounds_from_stack(ctx, dim_cnt, &bounds);
1319 if(FAILED(hres))
1320 return hres;
1322 array = SafeArrayCreate(VT_VARIANT, dim_cnt, bounds);
1323 free(bounds);
1324 if(!array)
1325 return E_OUTOFMEMORY;
1327 VariantClear(v);
1328 V_VT(v) = VT_ARRAY|VT_VARIANT;
1329 V_ARRAY(v) = array;
1331 return S_OK;
1334 static HRESULT interp_redim_preserve(exec_ctx_t *ctx)
1336 BSTR identifier = ctx->instr->arg1.bstr;
1337 const unsigned dim_cnt = ctx->instr->arg2.uint;
1338 unsigned i;
1339 VARIANT *v;
1340 SAFEARRAYBOUND *bounds;
1341 SAFEARRAY *array;
1342 ref_t ref;
1343 HRESULT hres;
1345 TRACE("%s %u\n", debugstr_w(identifier), dim_cnt);
1347 hres = lookup_identifier(ctx, identifier, VBDISP_LET, &ref);
1348 if(FAILED(hres)) {
1349 FIXME("lookup %s failed: %08lx\n", debugstr_w(identifier), hres);
1350 return hres;
1353 if(ref.type != REF_VAR) {
1354 FIXME("got ref.type = %d\n", ref.type);
1355 return E_FAIL;
1358 v = ref.u.v;
1360 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1361 v = V_VARIANTREF(v);
1364 if(!(V_VT(v) & VT_ARRAY)) {
1365 FIXME("ReDim Preserve not valid on type %d\n", V_VT(v));
1366 return E_FAIL;
1369 array = V_ISBYREF(v) ? *V_ARRAYREF(v) : V_ARRAY(v);
1371 hres = array_bounds_from_stack(ctx, dim_cnt, &bounds);
1372 if(FAILED(hres))
1373 return hres;
1375 if(array == NULL || array->cDims == 0) {
1376 /* can initially allocate the array */
1377 array = SafeArrayCreate(VT_VARIANT, dim_cnt, bounds);
1378 if(!array)
1379 hres = E_OUTOFMEMORY;
1380 else {
1381 VariantClear(v);
1382 V_VT(v) = VT_ARRAY|VT_VARIANT;
1383 V_ARRAY(v) = array;
1385 } else if(array->cDims != dim_cnt) {
1386 /* can't otherwise change the number of dimensions */
1387 TRACE("Can't resize %s, cDims %d != %d\n", debugstr_w(identifier), array->cDims, dim_cnt);
1388 hres = MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS);
1389 } else {
1390 /* can resize the last dimensions (if others match */
1391 for(i = 0; i+1 < dim_cnt; ++i) {
1392 if(array->rgsabound[array->cDims - 1 - i].cElements != bounds[i].cElements) {
1393 TRACE("Can't resize %s, bound[%d] %ld != %ld\n", debugstr_w(identifier), i, array->rgsabound[i].cElements, bounds[i].cElements);
1394 hres = MAKE_VBSERROR(VBSE_OUT_OF_BOUNDS);
1395 break;
1398 if(SUCCEEDED(hres))
1399 hres = SafeArrayRedim(array, &bounds[dim_cnt-1]);
1401 free(bounds);
1402 return hres;
1405 static HRESULT interp_step(exec_ctx_t *ctx)
1407 const BSTR ident = ctx->instr->arg2.bstr;
1408 BOOL gteq_zero;
1409 VARIANT zero;
1410 ref_t ref;
1411 HRESULT hres;
1413 TRACE("%s\n", debugstr_w(ident));
1415 V_VT(&zero) = VT_I2;
1416 V_I2(&zero) = 0;
1417 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
1418 if(FAILED(hres))
1419 return hres;
1421 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
1423 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
1424 if(FAILED(hres))
1425 return hres;
1427 if(ref.type != REF_VAR) {
1428 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
1429 return E_FAIL;
1432 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
1433 if(FAILED(hres))
1434 return hres;
1436 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT)) {
1437 ctx->instr++;
1438 }else {
1439 stack_popn(ctx, 2);
1440 instr_jmp(ctx, ctx->instr->arg1.uint);
1442 return S_OK;
1445 static HRESULT interp_newenum(exec_ctx_t *ctx)
1447 variant_val_t v;
1448 VARIANT *r;
1449 HRESULT hres;
1451 TRACE("\n");
1453 stack_pop_deref(ctx, &v);
1454 assert(V_VT(stack_top(ctx, 0)) == VT_EMPTY);
1455 r = stack_top(ctx, 0);
1457 switch(V_VT(v.v)) {
1458 case VT_DISPATCH|VT_BYREF:
1459 case VT_DISPATCH: {
1460 IEnumVARIANT *iter;
1461 DISPPARAMS dp = {0};
1462 VARIANT iterv;
1464 hres = disp_call(ctx->script, V_ISBYREF(v.v) ? *V_DISPATCHREF(v.v) : V_DISPATCH(v.v), DISPID_NEWENUM, &dp, &iterv);
1465 release_val(&v);
1466 if(FAILED(hres))
1467 return hres;
1469 if(V_VT(&iterv) != VT_UNKNOWN && V_VT(&iterv) != VT_DISPATCH) {
1470 FIXME("Unsupported iterv %s\n", debugstr_variant(&iterv));
1471 VariantClear(&iterv);
1472 return hres;
1475 hres = IUnknown_QueryInterface(V_UNKNOWN(&iterv), &IID_IEnumVARIANT, (void**)&iter);
1476 IUnknown_Release(V_UNKNOWN(&iterv));
1477 if(FAILED(hres)) {
1478 FIXME("Could not get IEnumVARIANT iface: %08lx\n", hres);
1479 return hres;
1482 V_VT(r) = VT_UNKNOWN;
1483 V_UNKNOWN(r) = (IUnknown*)iter;
1484 break;
1486 case VT_VARIANT|VT_ARRAY:
1487 case VT_VARIANT|VT_ARRAY|VT_BYREF: {
1488 IEnumVARIANT *iter;
1490 hres = create_safearray_iter(V_ISBYREF(v.v) ? *V_ARRAYREF(v.v) : V_ARRAY(v.v), v.owned && !V_ISBYREF(v.v), &iter);
1491 if(FAILED(hres))
1492 return hres;
1494 V_VT(r) = VT_UNKNOWN;
1495 V_UNKNOWN(r) = (IUnknown*)iter;
1496 break;
1498 default:
1499 FIXME("Unsupported for %s\n", debugstr_variant(v.v));
1500 release_val(&v);
1501 return E_NOTIMPL;
1504 return S_OK;
1507 static HRESULT interp_enumnext(exec_ctx_t *ctx)
1509 const unsigned loop_end = ctx->instr->arg1.uint;
1510 const BSTR ident = ctx->instr->arg2.bstr;
1511 VARIANT v;
1512 DISPPARAMS dp = {&v, &propput_dispid, 1, 1};
1513 IEnumVARIANT *iter;
1514 BOOL do_continue;
1515 HRESULT hres;
1517 TRACE("\n");
1519 if(V_VT(stack_top(ctx, 0)) == VT_EMPTY) {
1520 FIXME("uninitialized\n");
1521 return E_FAIL;
1524 assert(V_VT(stack_top(ctx, 0)) == VT_UNKNOWN);
1525 iter = (IEnumVARIANT*)V_UNKNOWN(stack_top(ctx, 0));
1527 V_VT(&v) = VT_EMPTY;
1528 hres = IEnumVARIANT_Next(iter, 1, &v, NULL);
1529 if(FAILED(hres))
1530 return hres;
1532 do_continue = hres == S_OK;
1533 hres = assign_ident(ctx, ident, DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &dp);
1534 VariantClear(&v);
1535 if(FAILED(hres))
1536 return hres;
1538 if(do_continue) {
1539 ctx->instr++;
1540 }else {
1541 stack_popn(ctx, 1);
1542 instr_jmp(ctx, loop_end);
1544 return S_OK;
1547 static HRESULT interp_jmp(exec_ctx_t *ctx)
1549 const unsigned arg = ctx->instr->arg1.uint;
1551 TRACE("%u\n", arg);
1553 instr_jmp(ctx, arg);
1554 return S_OK;
1557 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
1559 const unsigned arg = ctx->instr->arg1.uint;
1560 HRESULT hres;
1561 BOOL b;
1563 TRACE("%u\n", arg);
1565 hres = stack_pop_bool(ctx, &b);
1566 if(FAILED(hres))
1567 return hres;
1569 if(b)
1570 ctx->instr++;
1571 else
1572 instr_jmp(ctx, ctx->instr->arg1.uint);
1573 return S_OK;
1576 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
1578 const unsigned arg = ctx->instr->arg1.uint;
1579 HRESULT hres;
1580 BOOL b;
1582 TRACE("%u\n", arg);
1584 hres = stack_pop_bool(ctx, &b);
1585 if(FAILED(hres))
1586 return hres;
1588 if(b)
1589 instr_jmp(ctx, ctx->instr->arg1.uint);
1590 else
1591 ctx->instr++;
1592 return S_OK;
1595 static HRESULT interp_ret(exec_ctx_t *ctx)
1597 TRACE("\n");
1599 ctx->instr = NULL;
1600 return S_OK;
1603 static HRESULT interp_retval(exec_ctx_t *ctx)
1605 variant_val_t val;
1606 HRESULT hres;
1608 TRACE("\n");
1610 stack_pop_deref(ctx, &val);
1612 if(val.owned) {
1613 VariantClear(&ctx->ret_val);
1614 ctx->ret_val = *val.v;
1616 else {
1617 hres = VariantCopy(&ctx->ret_val, val.v);
1618 if(FAILED(hres))
1619 return hres;
1622 return S_OK;
1625 static HRESULT interp_stop(exec_ctx_t *ctx)
1627 WARN("\n");
1629 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
1630 return S_OK;
1633 static HRESULT interp_me(exec_ctx_t *ctx)
1635 IDispatch *disp;
1636 VARIANT v;
1638 TRACE("\n");
1640 if(ctx->vbthis) {
1641 disp = (IDispatch*)&ctx->vbthis->IDispatchEx_iface;
1642 }else if(ctx->code->named_item) {
1643 disp = (ctx->code->named_item->flags & SCRIPTITEM_CODEONLY)
1644 ? (IDispatch*)&ctx->code->named_item->script_obj->IDispatchEx_iface
1645 : ctx->code->named_item->disp;
1646 }else {
1647 named_item_t *item;
1648 disp = NULL;
1649 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
1650 if(!(item->flags & SCRIPTITEM_GLOBALMEMBERS)) continue;
1651 disp = item->disp;
1652 break;
1654 if(!disp)
1655 disp = (IDispatch*)&ctx->script->script_obj->IDispatchEx_iface;
1658 IDispatch_AddRef(disp);
1659 V_VT(&v) = VT_DISPATCH;
1660 V_DISPATCH(&v) = disp;
1661 return stack_push(ctx, &v);
1664 static HRESULT interp_bool(exec_ctx_t *ctx)
1666 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
1667 VARIANT v;
1669 TRACE("%s\n", arg ? "true" : "false");
1671 V_VT(&v) = VT_BOOL;
1672 V_BOOL(&v) = arg;
1673 return stack_push(ctx, &v);
1676 static HRESULT interp_errmode(exec_ctx_t *ctx)
1678 const int err_mode = ctx->instr->arg1.uint;
1680 TRACE("%d\n", err_mode);
1682 ctx->resume_next = err_mode;
1683 clear_ei(&ctx->script->ei);
1684 return S_OK;
1687 static HRESULT interp_string(exec_ctx_t *ctx)
1689 VARIANT v;
1691 TRACE("\n");
1693 V_VT(&v) = VT_BSTR;
1694 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
1695 if(!V_BSTR(&v))
1696 return E_OUTOFMEMORY;
1698 return stack_push(ctx, &v);
1701 static HRESULT interp_date(exec_ctx_t *ctx)
1703 const DATE *d = ctx->instr->arg1.date;
1704 VARIANT v;
1706 TRACE("%lf\n",*d);
1708 V_VT(&v) = VT_DATE;
1709 V_DATE(&v) = *d;
1711 return stack_push(ctx, &v);
1714 static HRESULT interp_int(exec_ctx_t *ctx)
1716 const LONG arg = ctx->instr->arg1.lng;
1717 VARIANT v;
1719 TRACE("%ld\n", arg);
1721 if(arg == (INT16)arg) {
1722 V_VT(&v) = VT_I2;
1723 V_I2(&v) = arg;
1724 }else {
1725 V_VT(&v) = VT_I4;
1726 V_I4(&v) = arg;
1728 return stack_push(ctx, &v);
1731 static HRESULT interp_double(exec_ctx_t *ctx)
1733 const DOUBLE *arg = ctx->instr->arg1.dbl;
1734 VARIANT v;
1736 TRACE("%lf\n", *arg);
1738 V_VT(&v) = VT_R8;
1739 V_R8(&v) = *arg;
1740 return stack_push(ctx, &v);
1743 static HRESULT interp_empty(exec_ctx_t *ctx)
1745 VARIANT v;
1747 TRACE("\n");
1749 V_VT(&v) = VT_EMPTY;
1750 return stack_push(ctx, &v);
1753 static HRESULT interp_null(exec_ctx_t *ctx)
1755 TRACE("\n");
1756 return stack_push_null(ctx);
1759 static HRESULT interp_nothing(exec_ctx_t *ctx)
1761 VARIANT v;
1763 TRACE("\n");
1765 V_VT(&v) = VT_DISPATCH;
1766 V_DISPATCH(&v) = NULL;
1767 return stack_push(ctx, &v);
1770 static HRESULT interp_hres(exec_ctx_t *ctx)
1772 const unsigned arg = ctx->instr->arg1.uint;
1773 VARIANT v;
1775 TRACE("%d\n", arg);
1777 V_VT(&v) = VT_ERROR;
1778 V_ERROR(&v) = arg;
1779 return stack_push(ctx, &v);
1782 static HRESULT interp_not(exec_ctx_t *ctx)
1784 variant_val_t val;
1785 VARIANT v;
1786 HRESULT hres;
1788 TRACE("\n");
1790 hres = stack_pop_val(ctx, &val);
1791 if(FAILED(hres))
1792 return hres;
1794 hres = VarNot(val.v, &v);
1795 release_val(&val);
1796 if(FAILED(hres))
1797 return hres;
1799 return stack_push(ctx, &v);
1802 static HRESULT interp_and(exec_ctx_t *ctx)
1804 variant_val_t r, l;
1805 VARIANT v;
1806 HRESULT hres;
1808 TRACE("\n");
1810 hres = stack_pop_val(ctx, &r);
1811 if(FAILED(hres))
1812 return hres;
1814 hres = stack_pop_val(ctx, &l);
1815 if(SUCCEEDED(hres)) {
1816 hres = VarAnd(l.v, r.v, &v);
1817 release_val(&l);
1819 release_val(&r);
1820 if(FAILED(hres))
1821 return hres;
1823 return stack_push(ctx, &v);
1826 static HRESULT interp_or(exec_ctx_t *ctx)
1828 variant_val_t r, l;
1829 VARIANT v;
1830 HRESULT hres;
1832 TRACE("\n");
1834 hres = stack_pop_val(ctx, &r);
1835 if(FAILED(hres))
1836 return hres;
1838 hres = stack_pop_val(ctx, &l);
1839 if(SUCCEEDED(hres)) {
1840 hres = VarOr(l.v, r.v, &v);
1841 release_val(&l);
1843 release_val(&r);
1844 if(FAILED(hres))
1845 return hres;
1847 return stack_push(ctx, &v);
1850 static HRESULT interp_xor(exec_ctx_t *ctx)
1852 variant_val_t r, l;
1853 VARIANT v;
1854 HRESULT hres;
1856 TRACE("\n");
1858 hres = stack_pop_val(ctx, &r);
1859 if(FAILED(hres))
1860 return hres;
1862 hres = stack_pop_val(ctx, &l);
1863 if(SUCCEEDED(hres)) {
1864 hres = VarXor(l.v, r.v, &v);
1865 release_val(&l);
1867 release_val(&r);
1868 if(FAILED(hres))
1869 return hres;
1871 return stack_push(ctx, &v);
1874 static HRESULT interp_eqv(exec_ctx_t *ctx)
1876 variant_val_t r, l;
1877 VARIANT v;
1878 HRESULT hres;
1880 TRACE("\n");
1882 hres = stack_pop_val(ctx, &r);
1883 if(FAILED(hres))
1884 return hres;
1886 hres = stack_pop_val(ctx, &l);
1887 if(SUCCEEDED(hres)) {
1888 hres = VarEqv(l.v, r.v, &v);
1889 release_val(&l);
1891 release_val(&r);
1892 if(FAILED(hres))
1893 return hres;
1895 return stack_push(ctx, &v);
1898 static HRESULT interp_imp(exec_ctx_t *ctx)
1900 variant_val_t r, l;
1901 VARIANT v;
1902 HRESULT hres;
1904 TRACE("\n");
1906 hres = stack_pop_val(ctx, &r);
1907 if(FAILED(hres))
1908 return hres;
1910 hres = stack_pop_val(ctx, &l);
1911 if(SUCCEEDED(hres)) {
1912 hres = VarImp(l.v, r.v, &v);
1913 release_val(&l);
1915 release_val(&r);
1916 if(FAILED(hres))
1917 return hres;
1919 return stack_push(ctx, &v);
1922 static HRESULT var_cmp(exec_ctx_t *ctx, VARIANT *l, VARIANT *r)
1924 TRACE("%s %s\n", debugstr_variant(l), debugstr_variant(r));
1926 /* FIXME: Fix comparing string to number */
1928 return VarCmp(l, r, ctx->script->lcid, 0);
1931 static HRESULT cmp_oper(exec_ctx_t *ctx)
1933 variant_val_t l, r;
1934 HRESULT hres;
1936 hres = stack_pop_val(ctx, &r);
1937 if(FAILED(hres))
1938 return hres;
1940 hres = stack_pop_val(ctx, &l);
1941 if(SUCCEEDED(hres)) {
1942 hres = var_cmp(ctx, l.v, r.v);
1943 release_val(&l);
1946 release_val(&r);
1947 return hres;
1950 static HRESULT interp_equal(exec_ctx_t *ctx)
1952 VARIANT v;
1953 HRESULT hres;
1955 TRACE("\n");
1957 hres = cmp_oper(ctx);
1958 if(FAILED(hres))
1959 return hres;
1960 if(hres == VARCMP_NULL)
1961 return stack_push_null(ctx);
1963 V_VT(&v) = VT_BOOL;
1964 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1965 return stack_push(ctx, &v);
1968 static HRESULT interp_nequal(exec_ctx_t *ctx)
1970 VARIANT v;
1971 HRESULT hres;
1973 TRACE("\n");
1975 hres = cmp_oper(ctx);
1976 if(FAILED(hres))
1977 return hres;
1978 if(hres == VARCMP_NULL)
1979 return stack_push_null(ctx);
1981 V_VT(&v) = VT_BOOL;
1982 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1983 return stack_push(ctx, &v);
1986 static HRESULT interp_gt(exec_ctx_t *ctx)
1988 VARIANT v;
1989 HRESULT hres;
1991 TRACE("\n");
1993 hres = cmp_oper(ctx);
1994 if(FAILED(hres))
1995 return hres;
1996 if(hres == VARCMP_NULL)
1997 return stack_push_null(ctx);
1999 V_VT(&v) = VT_BOOL;
2000 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
2001 return stack_push(ctx, &v);
2004 static HRESULT interp_gteq(exec_ctx_t *ctx)
2006 VARIANT v;
2007 HRESULT hres;
2009 TRACE("\n");
2011 hres = cmp_oper(ctx);
2012 if(FAILED(hres))
2013 return hres;
2014 if(hres == VARCMP_NULL)
2015 return stack_push_null(ctx);
2017 V_VT(&v) = VT_BOOL;
2018 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
2019 return stack_push(ctx, &v);
2022 static HRESULT interp_lt(exec_ctx_t *ctx)
2024 VARIANT v;
2025 HRESULT hres;
2027 TRACE("\n");
2029 hres = cmp_oper(ctx);
2030 if(FAILED(hres))
2031 return hres;
2032 if(hres == VARCMP_NULL)
2033 return stack_push_null(ctx);
2035 V_VT(&v) = VT_BOOL;
2036 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
2037 return stack_push(ctx, &v);
2040 static HRESULT interp_lteq(exec_ctx_t *ctx)
2042 VARIANT v;
2043 HRESULT hres;
2045 TRACE("\n");
2047 hres = cmp_oper(ctx);
2048 if(FAILED(hres))
2049 return hres;
2050 if(hres == VARCMP_NULL)
2051 return stack_push_null(ctx);
2053 V_VT(&v) = VT_BOOL;
2054 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
2055 return stack_push(ctx, &v);
2058 static HRESULT interp_case(exec_ctx_t *ctx)
2060 const unsigned arg = ctx->instr->arg1.uint;
2061 variant_val_t v;
2062 HRESULT hres;
2064 TRACE("%d\n", arg);
2066 hres = stack_pop_val(ctx, &v);
2067 if(FAILED(hres))
2068 return hres;
2070 hres = var_cmp(ctx, stack_top(ctx, 0), v.v);
2071 release_val(&v);
2072 if(FAILED(hres))
2073 return hres;
2075 if(hres == VARCMP_EQ) {
2076 stack_popn(ctx, 1);
2077 instr_jmp(ctx, arg);
2078 }else {
2079 ctx->instr++;
2082 return S_OK;
2085 static HRESULT interp_is(exec_ctx_t *ctx)
2087 IUnknown *l = NULL, *r = NULL;
2088 variant_val_t v;
2089 HRESULT hres = S_OK;
2091 TRACE("\n");
2093 stack_pop_deref(ctx, &v);
2094 if(V_VT(v.v) != VT_DISPATCH && V_VT(v.v) != VT_UNKNOWN) {
2095 FIXME("Unhandled type %s\n", debugstr_variant(v.v));
2096 hres = E_NOTIMPL;
2097 }else if(V_UNKNOWN(v.v)) {
2098 hres = IUnknown_QueryInterface(V_UNKNOWN(v.v), &IID_IUnknown, (void**)&r);
2100 if(v.owned) VariantClear(v.v);
2101 if(FAILED(hres))
2102 return hres;
2104 stack_pop_deref(ctx, &v);
2105 if(V_VT(v.v) != VT_DISPATCH && V_VT(v.v) != VT_UNKNOWN) {
2106 FIXME("Unhandled type %s\n", debugstr_variant(v.v));
2107 hres = E_NOTIMPL;
2108 }else if(V_UNKNOWN(v.v)) {
2109 hres = IUnknown_QueryInterface(V_UNKNOWN(v.v), &IID_IUnknown, (void**)&l);
2111 if(v.owned) VariantClear(v.v);
2113 if(SUCCEEDED(hres)) {
2114 VARIANT res;
2115 V_VT(&res) = VT_BOOL;
2116 if(r == l)
2117 V_BOOL(&res) = VARIANT_TRUE;
2118 else if(!r || !l)
2119 V_BOOL(&res) = VARIANT_FALSE;
2120 else {
2121 IObjectIdentity *identity;
2122 hres = IUnknown_QueryInterface(l, &IID_IObjectIdentity, (void**)&identity);
2123 if(SUCCEEDED(hres)) {
2124 hres = IObjectIdentity_IsEqualObject(identity, r);
2125 IObjectIdentity_Release(identity);
2127 V_BOOL(&res) = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
2129 hres = stack_push(ctx, &res);
2131 if(r)
2132 IUnknown_Release(r);
2133 if(l)
2134 IUnknown_Release(l);
2135 return hres;
2138 static HRESULT interp_concat(exec_ctx_t *ctx)
2140 variant_val_t r, l;
2141 VARIANT v;
2142 HRESULT hres;
2144 TRACE("\n");
2146 hres = stack_pop_val(ctx, &r);
2147 if(FAILED(hres))
2148 return hres;
2150 hres = stack_pop_val(ctx, &l);
2151 if(SUCCEEDED(hres)) {
2152 hres = VarCat(l.v, r.v, &v);
2153 release_val(&l);
2155 release_val(&r);
2156 if(FAILED(hres))
2157 return hres;
2159 return stack_push(ctx, &v);
2162 static HRESULT interp_add(exec_ctx_t *ctx)
2164 variant_val_t r, l;
2165 VARIANT v;
2166 HRESULT hres;
2168 TRACE("\n");
2170 hres = stack_pop_val(ctx, &r);
2171 if(FAILED(hres))
2172 return hres;
2174 hres = stack_pop_val(ctx, &l);
2175 if(SUCCEEDED(hres)) {
2176 hres = VarAdd(l.v, r.v, &v);
2177 release_val(&l);
2179 release_val(&r);
2180 if(FAILED(hres))
2181 return hres;
2183 return stack_push(ctx, &v);
2186 static HRESULT interp_sub(exec_ctx_t *ctx)
2188 variant_val_t r, l;
2189 VARIANT v;
2190 HRESULT hres;
2192 TRACE("\n");
2194 hres = stack_pop_val(ctx, &r);
2195 if(FAILED(hres))
2196 return hres;
2198 hres = stack_pop_val(ctx, &l);
2199 if(SUCCEEDED(hres)) {
2200 hres = VarSub(l.v, r.v, &v);
2201 release_val(&l);
2203 release_val(&r);
2204 if(FAILED(hres))
2205 return hres;
2207 return stack_push(ctx, &v);
2210 static HRESULT interp_mod(exec_ctx_t *ctx)
2212 variant_val_t r, l;
2213 VARIANT v;
2214 HRESULT hres;
2216 TRACE("\n");
2218 hres = stack_pop_val(ctx, &r);
2219 if(FAILED(hres))
2220 return hres;
2222 hres = stack_pop_val(ctx, &l);
2223 if(SUCCEEDED(hres)) {
2224 hres = VarMod(l.v, r.v, &v);
2225 release_val(&l);
2227 release_val(&r);
2228 if(FAILED(hres))
2229 return hres;
2231 return stack_push(ctx, &v);
2234 static HRESULT interp_idiv(exec_ctx_t *ctx)
2236 variant_val_t r, l;
2237 VARIANT v;
2238 HRESULT hres;
2240 TRACE("\n");
2242 hres = stack_pop_val(ctx, &r);
2243 if(FAILED(hres))
2244 return hres;
2246 hres = stack_pop_val(ctx, &l);
2247 if(SUCCEEDED(hres)) {
2248 hres = VarIdiv(l.v, r.v, &v);
2249 release_val(&l);
2251 release_val(&r);
2252 if(FAILED(hres))
2253 return hres;
2255 return stack_push(ctx, &v);
2258 static HRESULT interp_div(exec_ctx_t *ctx)
2260 variant_val_t r, l;
2261 VARIANT v;
2262 HRESULT hres;
2264 TRACE("\n");
2266 hres = stack_pop_val(ctx, &r);
2267 if(FAILED(hres))
2268 return hres;
2270 hres = stack_pop_val(ctx, &l);
2271 if(SUCCEEDED(hres)) {
2272 hres = VarDiv(l.v, r.v, &v);
2273 release_val(&l);
2275 release_val(&r);
2276 if(FAILED(hres))
2277 return hres;
2279 return stack_push(ctx, &v);
2282 static HRESULT interp_mul(exec_ctx_t *ctx)
2284 variant_val_t r, l;
2285 VARIANT v;
2286 HRESULT hres;
2288 TRACE("\n");
2290 hres = stack_pop_val(ctx, &r);
2291 if(FAILED(hres))
2292 return hres;
2294 hres = stack_pop_val(ctx, &l);
2295 if(SUCCEEDED(hres)) {
2296 hres = VarMul(l.v, r.v, &v);
2297 release_val(&l);
2299 release_val(&r);
2300 if(FAILED(hres))
2301 return hres;
2303 return stack_push(ctx, &v);
2306 static HRESULT interp_exp(exec_ctx_t *ctx)
2308 variant_val_t r, l;
2309 VARIANT v;
2310 HRESULT hres;
2312 TRACE("\n");
2314 hres = stack_pop_val(ctx, &r);
2315 if(FAILED(hres))
2316 return hres;
2318 hres = stack_pop_val(ctx, &l);
2319 if(SUCCEEDED(hres)) {
2320 hres = VarPow(l.v, r.v, &v);
2321 release_val(&l);
2323 release_val(&r);
2324 if(FAILED(hres))
2325 return hres;
2327 return stack_push(ctx, &v);
2330 static HRESULT interp_neg(exec_ctx_t *ctx)
2332 variant_val_t val;
2333 VARIANT v;
2334 HRESULT hres;
2336 hres = stack_pop_val(ctx, &val);
2337 if(FAILED(hres))
2338 return hres;
2340 hres = VarNeg(val.v, &v);
2341 release_val(&val);
2342 if(FAILED(hres))
2343 return hres;
2345 return stack_push(ctx, &v);
2348 static HRESULT interp_incc(exec_ctx_t *ctx)
2350 const BSTR ident = ctx->instr->arg1.bstr;
2351 VARIANT v;
2352 ref_t ref;
2353 HRESULT hres;
2355 TRACE("\n");
2357 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
2358 if(FAILED(hres))
2359 return hres;
2361 if(ref.type != REF_VAR) {
2362 FIXME("ref.type is not REF_VAR\n");
2363 return E_FAIL;
2366 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
2367 if(FAILED(hres))
2368 return hres;
2370 VariantClear(ref.u.v);
2371 *ref.u.v = v;
2372 return S_OK;
2375 static HRESULT interp_catch(exec_ctx_t *ctx)
2377 /* Nothing to do here, the OP is for unwinding only. */
2378 return S_OK;
2381 static const instr_func_t op_funcs[] = {
2382 #define X(x,n,a,b) interp_ ## x,
2383 OP_LIST
2384 #undef X
2387 static const unsigned op_move[] = {
2388 #define X(x,n,a,b) n,
2389 OP_LIST
2390 #undef X
2393 void release_dynamic_var(dynamic_var_t *var)
2395 VariantClear(&var->v);
2396 if(var->array)
2397 SafeArrayDestroy(var->array);
2400 static void release_exec(exec_ctx_t *ctx)
2402 dynamic_var_t *var;
2403 unsigned i;
2405 VariantClear(&ctx->ret_val);
2407 for(var = ctx->dynamic_vars; var; var = var->next)
2408 release_dynamic_var(var);
2410 if(ctx->vbthis)
2411 IDispatchEx_Release(&ctx->vbthis->IDispatchEx_iface);
2413 if(ctx->args) {
2414 for(i=0; i < ctx->func->arg_cnt; i++)
2415 VariantClear(ctx->args+i);
2418 if(ctx->vars) {
2419 for(i=0; i < ctx->func->var_cnt; i++)
2420 VariantClear(ctx->vars+i);
2423 if(ctx->arrays) {
2424 for(i=0; i < ctx->func->array_cnt; i++) {
2425 if(ctx->arrays[i])
2426 SafeArrayDestroy(ctx->arrays[i]);
2428 free(ctx->arrays);
2431 heap_pool_free(&ctx->heap);
2432 free(ctx->args);
2433 free(ctx->vars);
2434 free(ctx->stack);
2437 HRESULT exec_script(script_ctx_t *ctx, BOOL extern_caller, function_t *func, vbdisp_t *vbthis, DISPPARAMS *dp, VARIANT *res)
2439 exec_ctx_t exec = {func->code_ctx};
2440 vbsop_t op;
2441 HRESULT hres = S_OK;
2443 exec.code = func->code_ctx;
2445 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
2446 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
2447 return E_FAIL;
2450 heap_pool_init(&exec.heap);
2452 TRACE("%s args=%u\n", debugstr_w(func->name),func->arg_cnt);
2453 if(func->arg_cnt) {
2454 VARIANT *v;
2455 unsigned i;
2457 exec.args = calloc(func->arg_cnt, sizeof(VARIANT));
2458 if(!exec.args) {
2459 release_exec(&exec);
2460 return E_OUTOFMEMORY;
2463 for(i=0; i < func->arg_cnt; i++) {
2464 v = get_arg(dp, i);
2465 TRACE(" [%d] %s\n", i, debugstr_variant(v));
2466 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
2467 if(func->args[i].by_ref)
2468 exec.args[i] = *v;
2469 else
2470 hres = VariantCopyInd(exec.args+i, V_VARIANTREF(v));
2471 }else {
2472 hres = VariantCopyInd(exec.args+i, v);
2474 if(FAILED(hres)) {
2475 release_exec(&exec);
2476 return hres;
2479 }else {
2480 exec.args = NULL;
2483 if(func->var_cnt) {
2484 exec.vars = calloc(func->var_cnt, sizeof(VARIANT));
2485 if(!exec.vars) {
2486 release_exec(&exec);
2487 return E_OUTOFMEMORY;
2489 }else {
2490 exec.vars = NULL;
2493 exec.stack_size = 16;
2494 exec.top = 0;
2495 exec.stack = malloc(exec.stack_size * sizeof(VARIANT));
2496 if(!exec.stack) {
2497 release_exec(&exec);
2498 return E_OUTOFMEMORY;
2501 if(extern_caller)
2502 IActiveScriptSite_OnEnterScript(ctx->site);
2504 if(vbthis) {
2505 IDispatchEx_AddRef(&vbthis->IDispatchEx_iface);
2506 exec.vbthis = vbthis;
2509 exec.instr = exec.code->instrs + func->code_off;
2510 exec.script = ctx;
2511 exec.func = func;
2513 while(exec.instr) {
2514 op = exec.instr->op;
2515 hres = op_funcs[op](&exec);
2516 if(FAILED(hres)) {
2517 if(hres != SCRIPT_E_RECORDED) {
2518 /* SCRIPT_E_RECORDED means ctx->ei is already populated */
2519 clear_ei(&ctx->ei);
2520 ctx->ei.scode = hres;
2523 if(!ctx->ei.bstrDescription)
2524 map_vbs_exception(&ctx->ei);
2526 if(exec.resume_next) {
2527 unsigned stack_off;
2529 WARN("Failed %08lx in resume next mode\n", ctx->ei.scode);
2532 * Unwinding here is simple. We need to find the next OP_catch, which contains
2533 * information about expected stack size and jump offset on error. Generated
2534 * bytecode needs to guarantee, that simple jump and stack adjustment will
2535 * guarantee proper execution continuation.
2537 while((++exec.instr)->op != OP_catch);
2539 TRACE("unwind jmp %d stack_off %d\n", exec.instr->arg1.uint, exec.instr->arg2.uint);
2541 clear_error_loc(ctx);
2542 stack_off = exec.instr->arg2.uint;
2543 instr_jmp(&exec, exec.instr->arg1.uint);
2545 if(exec.top > stack_off) {
2546 stack_popn(&exec, exec.top-stack_off);
2547 }else if(exec.top < stack_off) {
2548 VARIANT v;
2550 V_VT(&v) = VT_EMPTY;
2551 while(exec.top < stack_off) {
2552 hres = stack_push(&exec, &v);
2553 if(FAILED(hres))
2554 break;
2558 continue;
2559 }else {
2560 if(!ctx->error_loc_code) {
2561 grab_vbscode(exec.code);
2562 ctx->error_loc_code = exec.code;
2563 ctx->error_loc_offset = exec.instr->loc;
2565 stack_popn(&exec, exec.top);
2566 break;
2570 exec.instr += op_move[op];
2573 assert(!exec.top);
2575 if(extern_caller) {
2576 if(FAILED(hres)) {
2577 if(!ctx->ei.scode)
2578 ctx->ei.scode = hres;
2579 hres = report_script_error(ctx, ctx->error_loc_code, ctx->error_loc_offset);
2580 clear_error_loc(ctx);
2582 IActiveScriptSite_OnLeaveScript(ctx->site);
2585 if(SUCCEEDED(hres) && res) {
2586 *res = exec.ret_val;
2587 V_VT(&exec.ret_val) = VT_EMPTY;
2590 release_exec(&exec);
2591 return hres;