advapi32: Return a fake handle from EventRegister.
[wine/multimedia.git] / dlls / vbscript / interp.c
blob67e10707f32b6e0baaa89573fdaa702adc86b1f8
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 typedef struct {
28 vbscode_t *code;
29 instr_t *instr;
30 script_ctx_t *script;
31 function_t *func;
32 IDispatch *this_obj;
34 VARIANT *args;
35 VARIANT *vars;
37 dynamic_var_t *dynamic_vars;
38 vbsheap_t heap;
40 BOOL resume_next;
42 unsigned stack_size;
43 unsigned top;
44 VARIANT *stack;
46 VARIANT ret_val;
47 } exec_ctx_t;
49 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
51 typedef enum {
52 REF_NONE,
53 REF_DISP,
54 REF_VAR,
55 REF_OBJ,
56 REF_CONST,
57 REF_FUNC
58 } ref_type_t;
60 typedef struct {
61 ref_type_t type;
62 union {
63 struct {
64 IDispatch *disp;
65 DISPID id;
66 } d;
67 VARIANT *v;
68 function_t *f;
69 IDispatch *obj;
70 } u;
71 } ref_t;
73 typedef struct {
74 VARIANT *v;
75 VARIANT store;
76 BOOL owned;
77 } variant_val_t;
79 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
81 while(var) {
82 if(!strcmpiW(var->name, name)) {
83 ref->type = var->is_const ? REF_CONST : REF_VAR;
84 ref->u.v = &var->v;
85 return TRUE;
88 var = var->next;
91 return FALSE;
94 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
96 named_item_t *item;
97 function_t *func;
98 unsigned i;
99 DISPID id;
100 HRESULT hres;
102 static const WCHAR errW[] = {'e','r','r',0};
104 if(invoke_type == VBDISP_LET
105 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
106 && !strcmpiW(name, ctx->func->name)) {
107 ref->type = REF_VAR;
108 ref->u.v = &ctx->ret_val;
109 return S_OK;
112 for(i=0; i < ctx->func->var_cnt; i++) {
113 if(!strcmpiW(ctx->func->vars[i].name, name)) {
114 ref->type = REF_VAR;
115 ref->u.v = ctx->vars+i;
116 return TRUE;
120 for(i=0; i < ctx->func->arg_cnt; i++) {
121 if(!strcmpiW(ctx->func->args[i].name, name)) {
122 ref->type = REF_VAR;
123 ref->u.v = ctx->args+i;
124 return S_OK;
128 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
129 return S_OK;
131 if(ctx->func->type != FUNC_GLOBAL) {
132 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
133 if(SUCCEEDED(hres)) {
134 ref->type = REF_DISP;
135 ref->u.d.disp = ctx->this_obj;
136 ref->u.d.id = id;
137 return S_OK;
141 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
142 return S_OK;
144 for(func = ctx->script->global_funcs; func; func = func->next) {
145 if(!strcmpiW(func->name, name)) {
146 ref->type = REF_FUNC;
147 ref->u.f = func;
148 return S_OK;
152 if(!strcmpiW(name, errW)) {
153 ref->type = REF_OBJ;
154 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
155 return S_OK;
158 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
159 if(SUCCEEDED(hres)) {
160 ref->type = REF_DISP;
161 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
162 ref->u.d.id = id;
163 return S_OK;
166 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
167 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
168 if(!item->disp) {
169 IUnknown *unk;
171 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
172 if(FAILED(hres)) {
173 WARN("GetItemInfo failed: %08x\n", hres);
174 continue;
177 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
178 IUnknown_Release(unk);
179 if(FAILED(hres)) {
180 WARN("object does not implement IDispatch\n");
181 continue;
185 ref->type = REF_OBJ;
186 ref->u.obj = item->disp;
187 return S_OK;
191 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
192 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
193 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
194 if(SUCCEEDED(hres)) {
195 ref->type = REF_DISP;
196 ref->u.d.disp = item->disp;
197 ref->u.d.id = id;
198 return S_OK;
203 ref->type = REF_NONE;
204 return S_OK;
207 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
209 dynamic_var_t *new_var;
210 vbsheap_t *heap;
211 WCHAR *str;
212 unsigned size;
213 HRESULT hres;
215 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
217 new_var = vbsheap_alloc(heap, sizeof(*new_var));
218 if(!new_var)
219 return E_OUTOFMEMORY;
221 size = (strlenW(name)+1)*sizeof(WCHAR);
222 str = vbsheap_alloc(heap, size);
223 if(!str)
224 return E_OUTOFMEMORY;
225 memcpy(str, name, size);
226 new_var->name = str;
227 new_var->is_const = is_const;
229 if(own_val) {
230 new_var->v = *val;
231 }else {
232 V_VT(&new_var->v) = VT_EMPTY;
233 hres = VariantCopy(&new_var->v, val);
234 if(FAILED(hres))
235 return hres;
238 if(ctx->func->type == FUNC_GLOBAL) {
239 new_var->next = ctx->script->global_vars;
240 ctx->script->global_vars = new_var;
241 }else {
242 new_var->next = ctx->dynamic_vars;
243 ctx->dynamic_vars = new_var;
246 return S_OK;
249 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
251 assert(ctx->top);
252 return ctx->stack + --ctx->top;
255 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
257 assert(ctx->top >= n);
258 return ctx->stack + (ctx->top-n-1);
261 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
263 if(ctx->stack_size == ctx->top) {
264 VARIANT *new_stack;
266 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
267 if(!new_stack) {
268 VariantClear(v);
269 return E_OUTOFMEMORY;
272 ctx->stack = new_stack;
273 ctx->stack_size *= 2;
276 ctx->stack[ctx->top++] = *v;
277 return S_OK;
280 static void stack_popn(exec_ctx_t *ctx, unsigned n)
282 while(n--)
283 VariantClear(stack_pop(ctx));
286 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
288 VARIANT *var;
290 var = stack_pop(ctx);
292 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
293 v->owned = FALSE;
294 var = V_VARIANTREF(var);
295 }else {
296 v->owned = TRUE;
299 if(V_VT(var) == VT_DISPATCH) {
300 DISPPARAMS dp = {0};
301 HRESULT hres;
303 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
304 if(v->owned)
305 IDispatch_Release(V_DISPATCH(var));
306 if(FAILED(hres))
307 return hres;
309 v->owned = TRUE;
310 v->v = &v->store;
311 }else {
312 v->v = var;
315 return S_OK;
318 static HRESULT stack_assume_val(exec_ctx_t *ctx, unsigned n)
320 VARIANT *v = stack_top(ctx, n);
321 HRESULT hres;
323 if(V_VT(v) == (VT_BYREF|VT_VARIANT)) {
324 VARIANT *ref = V_VARIANTREF(v);
326 V_VT(v) = VT_EMPTY;
327 hres = VariantCopy(v, ref);
328 if(FAILED(hres))
329 return hres;
332 if(V_VT(v) == VT_DISPATCH) {
333 DISPPARAMS dp = {0};
334 IDispatch *disp;
336 disp = V_DISPATCH(v);
337 V_VT(v) = VT_EMPTY;
338 hres = disp_call(ctx->script, disp, DISPID_VALUE, &dp, v);
339 IDispatch_Release(disp);
340 if(FAILED(hres))
341 return hres;
344 return S_OK;
347 static inline void release_val(variant_val_t *v)
349 if(v->owned)
350 VariantClear(v->v);
353 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
355 VARIANT *v = stack_pop(ctx);
357 if(V_VT(v) == VT_DISPATCH) {
358 *ret = V_DISPATCH(v);
359 return S_OK;
362 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
363 FIXME("not supported type: %s\n", debugstr_variant(v));
364 VariantClear(v);
365 return E_FAIL;
368 v = V_BYREF(v);
369 if(V_VT(v) != VT_DISPATCH) {
370 FIXME("not disp %s\n", debugstr_variant(v));
371 return E_FAIL;
374 if(V_DISPATCH(v))
375 IDispatch_AddRef(V_DISPATCH(v));
376 *ret = V_DISPATCH(v);
377 return S_OK;
380 static HRESULT stack_assume_disp(exec_ctx_t *ctx, unsigned n, IDispatch **disp)
382 VARIANT *v = stack_top(ctx, n), *ref;
384 if(V_VT(v) != VT_DISPATCH) {
385 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
386 FIXME("not supported type: %s\n", debugstr_variant(v));
387 return E_FAIL;
390 ref = V_VARIANTREF(v);
391 if(V_VT(ref) != VT_DISPATCH) {
392 FIXME("not disp %s\n", debugstr_variant(ref));
393 return E_FAIL;
396 V_VT(v) = VT_DISPATCH;
397 V_DISPATCH(v) = V_DISPATCH(ref);
398 if(V_DISPATCH(v))
399 IDispatch_AddRef(V_DISPATCH(v));
402 if(disp)
403 *disp = V_DISPATCH(v);
404 return S_OK;
407 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
409 ctx->instr = ctx->code->instrs + addr;
412 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, BOOL is_propput, DISPPARAMS *dp)
414 static DISPID propput_dispid = DISPID_PROPERTYPUT;
416 dp->cNamedArgs = is_propput ? 1 : 0;
417 dp->cArgs = arg_cnt + dp->cNamedArgs;
418 dp->rgdispidNamedArgs = is_propput ? &propput_dispid : NULL;
420 if(arg_cnt) {
421 VARIANT tmp;
422 unsigned i;
424 assert(ctx->top >= arg_cnt);
426 for(i=1; i*2 <= arg_cnt; i++) {
427 tmp = ctx->stack[ctx->top-i];
428 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
429 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
432 dp->rgvarg = ctx->stack + ctx->top-dp->cArgs;
433 }else {
434 dp->rgvarg = is_propput ? ctx->stack+ctx->top-1 : NULL;
438 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
440 BSTR identifier = ctx->instr->arg1.bstr;
441 const unsigned arg_cnt = ctx->instr->arg2.uint;
442 DISPPARAMS dp;
443 ref_t ref;
444 HRESULT hres;
446 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
447 if(FAILED(hres))
448 return hres;
450 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
452 switch(ref.type) {
453 case REF_VAR:
454 case REF_CONST:
455 if(!res) {
456 FIXME("REF_VAR no res\n");
457 return E_NOTIMPL;
460 if(arg_cnt) {
461 FIXME("arguments not implemented\n");
462 return E_NOTIMPL;
465 V_VT(res) = VT_BYREF|VT_VARIANT;
466 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
467 break;
468 case REF_DISP:
469 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
470 if(FAILED(hres))
471 return hres;
472 break;
473 case REF_FUNC:
474 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
475 if(FAILED(hres))
476 return hres;
477 break;
478 case REF_OBJ:
479 if(arg_cnt) {
480 FIXME("arguments on object\n");
481 return E_NOTIMPL;
484 if(res) {
485 IDispatch_AddRef(ref.u.obj);
486 V_VT(res) = VT_DISPATCH;
487 V_DISPATCH(res) = ref.u.obj;
489 break;
490 case REF_NONE:
491 FIXME("%s not found\n", debugstr_w(identifier));
492 return DISP_E_UNKNOWNNAME;
495 stack_popn(ctx, arg_cnt);
496 return S_OK;
499 static HRESULT interp_icall(exec_ctx_t *ctx)
501 VARIANT v;
502 HRESULT hres;
504 TRACE("\n");
506 hres = do_icall(ctx, &v);
507 if(FAILED(hres))
508 return hres;
510 return stack_push(ctx, &v);
513 static HRESULT interp_icallv(exec_ctx_t *ctx)
515 TRACE("\n");
516 return do_icall(ctx, NULL);
519 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
521 const BSTR identifier = ctx->instr->arg1.bstr;
522 const unsigned arg_cnt = ctx->instr->arg2.uint;
523 IDispatch *obj;
524 DISPPARAMS dp;
525 DISPID id;
526 HRESULT hres;
528 hres = stack_pop_disp(ctx, &obj);
529 if(FAILED(hres))
530 return hres;
532 if(!obj) {
533 FIXME("NULL obj\n");
534 return E_FAIL;
537 vbstack_to_dp(ctx, arg_cnt, FALSE, &dp);
539 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
540 if(SUCCEEDED(hres))
541 hres = disp_call(ctx->script, obj, id, &dp, res);
542 IDispatch_Release(obj);
543 if(FAILED(hres))
544 return hres;
546 stack_popn(ctx, arg_cnt);
547 return S_OK;
550 static HRESULT interp_mcall(exec_ctx_t *ctx)
552 VARIANT res;
553 HRESULT hres;
555 TRACE("\n");
557 hres = do_mcall(ctx, &res);
558 if(FAILED(hres))
559 return hres;
561 return stack_push(ctx, &res);
564 static HRESULT interp_mcallv(exec_ctx_t *ctx)
566 TRACE("\n");
568 return do_mcall(ctx, NULL);
571 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, DISPPARAMS *dp)
573 ref_t ref;
574 HRESULT hres;
576 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
577 if(FAILED(hres))
578 return hres;
580 switch(ref.type) {
581 case REF_VAR: {
582 VARIANT *v = ref.u.v;
584 if(arg_cnt(dp)) {
585 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
586 return E_NOTIMPL;
589 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
590 v = V_VARIANTREF(v);
592 hres = VariantCopy(v, dp->rgvarg);
593 break;
595 case REF_DISP:
596 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, dp);
597 break;
598 case REF_FUNC:
599 FIXME("functions not implemented\n");
600 return E_NOTIMPL;
601 case REF_OBJ:
602 FIXME("REF_OBJ\n");
603 return E_NOTIMPL;
604 case REF_CONST:
605 FIXME("REF_CONST\n");
606 return E_NOTIMPL;
607 case REF_NONE:
608 if(ctx->func->code_ctx->option_explicit) {
609 FIXME("throw exception\n");
610 hres = E_FAIL;
611 }else {
612 if(arg_cnt(dp)) {
613 FIXME("arg_cnt %d not supported\n", arg_cnt(dp));
614 return E_NOTIMPL;
617 TRACE("creating variable %s\n", debugstr_w(name));
618 hres = add_dynamic_var(ctx, name, FALSE, dp->rgvarg, FALSE);
622 return hres;
625 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
627 const BSTR arg = ctx->instr->arg1.bstr;
628 const unsigned arg_cnt = ctx->instr->arg2.uint;
629 DISPPARAMS dp;
630 HRESULT hres;
632 TRACE("%s\n", debugstr_w(arg));
634 hres = stack_assume_val(ctx, arg_cnt);
635 if(FAILED(hres))
636 return hres;
638 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
639 hres = assign_ident(ctx, arg, &dp);
640 if(FAILED(hres))
641 return hres;
643 stack_popn(ctx, arg_cnt+1);
644 return S_OK;
647 static HRESULT interp_set_ident(exec_ctx_t *ctx)
649 const BSTR arg = ctx->instr->arg1.bstr;
650 const unsigned arg_cnt = ctx->instr->arg2.uint;
651 DISPPARAMS dp;
652 HRESULT hres;
654 TRACE("%s\n", debugstr_w(arg));
656 if(arg_cnt) {
657 FIXME("arguments not supported\n");
658 return E_NOTIMPL;
661 hres = stack_assume_disp(ctx, 0, NULL);
662 if(FAILED(hres))
663 return hres;
665 vbstack_to_dp(ctx, 0, TRUE, &dp);
666 hres = assign_ident(ctx, ctx->instr->arg1.bstr, &dp);
667 if(FAILED(hres))
668 return hres;
670 stack_popn(ctx, 1);
671 return S_OK;
674 static HRESULT interp_assign_member(exec_ctx_t *ctx)
676 BSTR identifier = ctx->instr->arg1.bstr;
677 const unsigned arg_cnt = ctx->instr->arg2.uint;
678 IDispatch *obj;
679 DISPPARAMS dp;
680 DISPID id;
681 HRESULT hres;
683 TRACE("%s\n", debugstr_w(identifier));
685 hres = stack_assume_disp(ctx, arg_cnt+1, &obj);
686 if(FAILED(hres))
687 return hres;
689 if(!obj) {
690 FIXME("NULL obj\n");
691 return E_FAIL;
694 hres = stack_assume_val(ctx, arg_cnt);
695 if(FAILED(hres))
696 return hres;
698 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
699 if(SUCCEEDED(hres)) {
700 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
701 hres = disp_propput(ctx->script, obj, id, &dp);
703 if(FAILED(hres))
704 return hres;
706 stack_popn(ctx, arg_cnt+2);
707 return S_OK;
710 static HRESULT interp_set_member(exec_ctx_t *ctx)
712 BSTR identifier = ctx->instr->arg1.bstr;
713 const unsigned arg_cnt = ctx->instr->arg2.uint;
714 IDispatch *obj;
715 DISPPARAMS dp;
716 DISPID id;
717 HRESULT hres;
719 TRACE("%s\n", debugstr_w(identifier));
721 if(arg_cnt) {
722 FIXME("arguments not supported\n");
723 return E_NOTIMPL;
726 hres = stack_assume_disp(ctx, 1, &obj);
727 if(FAILED(hres))
728 return hres;
730 if(!obj) {
731 FIXME("NULL obj\n");
732 return E_FAIL;
735 hres = stack_assume_disp(ctx, 0, NULL);
736 if(FAILED(hres))
737 return hres;
739 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
740 if(SUCCEEDED(hres)) {
741 vbstack_to_dp(ctx, arg_cnt, TRUE, &dp);
742 hres = disp_propput(ctx->script, obj, id, &dp);
744 if(FAILED(hres))
745 return hres;
747 stack_popn(ctx, 2);
748 return S_OK;
751 static HRESULT interp_const(exec_ctx_t *ctx)
753 BSTR arg = ctx->instr->arg1.bstr;
754 variant_val_t val;
755 ref_t ref;
756 HRESULT hres;
758 TRACE("%s\n", debugstr_w(arg));
760 assert(ctx->func->type == FUNC_GLOBAL);
762 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
763 if(FAILED(hres))
764 return hres;
766 if(ref.type != REF_NONE) {
767 FIXME("%s already defined\n", debugstr_w(arg));
768 return E_FAIL;
771 hres = stack_pop_val(ctx, &val);
772 if(FAILED(hres))
773 return hres;
775 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
778 static HRESULT interp_val(exec_ctx_t *ctx)
780 variant_val_t val;
781 VARIANT v;
782 HRESULT hres;
784 TRACE("\n");
786 hres = stack_pop_val(ctx, &val);
787 if(FAILED(hres))
788 return hres;
790 if(!val.owned) {
791 V_VT(&v) = VT_EMPTY;
792 hres = VariantCopy(&v, val.v);
793 if(FAILED(hres))
794 return hres;
797 return stack_push(ctx, val.owned ? val.v : &v);
800 static HRESULT interp_pop(exec_ctx_t *ctx)
802 const unsigned n = ctx->instr->arg1.uint;
804 TRACE("%u\n", n);
806 stack_popn(ctx, n);
807 return S_OK;
810 static HRESULT interp_new(exec_ctx_t *ctx)
812 const WCHAR *arg = ctx->instr->arg1.bstr;
813 class_desc_t *class_desc;
814 vbdisp_t *obj;
815 VARIANT v;
816 HRESULT hres;
818 TRACE("%s\n", debugstr_w(arg));
820 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
821 if(!strcmpiW(class_desc->name, arg))
822 break;
824 if(!class_desc) {
825 FIXME("Class %s not found\n", debugstr_w(arg));
826 return E_FAIL;
829 hres = create_vbdisp(class_desc, &obj);
830 if(FAILED(hres))
831 return hres;
833 V_VT(&v) = VT_DISPATCH;
834 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
835 return stack_push(ctx, &v);
838 static HRESULT interp_step(exec_ctx_t *ctx)
840 const BSTR ident = ctx->instr->arg2.bstr;
841 BOOL gteq_zero;
842 VARIANT zero;
843 ref_t ref;
844 HRESULT hres;
846 TRACE("%s\n", debugstr_w(ident));
848 V_VT(&zero) = VT_I2;
849 V_I2(&zero) = 0;
850 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
851 if(FAILED(hres))
852 return hres;
854 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
856 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
857 if(FAILED(hres))
858 return hres;
860 if(ref.type != REF_VAR) {
861 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
862 return E_FAIL;
865 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
866 if(FAILED(hres))
867 return hres;
869 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
870 ctx->instr++;
871 else
872 instr_jmp(ctx, ctx->instr->arg1.uint);
873 return S_OK;
876 static HRESULT interp_jmp(exec_ctx_t *ctx)
878 const unsigned arg = ctx->instr->arg1.uint;
880 TRACE("%u\n", arg);
882 instr_jmp(ctx, arg);
883 return S_OK;
886 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
888 const unsigned arg = ctx->instr->arg1.uint;
889 variant_val_t val;
890 HRESULT hres;
892 TRACE("%u\n", arg);
894 hres = stack_pop_val(ctx, &val);
895 if(FAILED(hres))
896 return hres;
898 if(V_VT(val.v) != VT_BOOL) {
899 FIXME("unsupported for %s\n", debugstr_variant(val.v));
900 release_val(&val);
901 return E_NOTIMPL;
904 if(V_BOOL(val.v))
905 ctx->instr++;
906 else
907 instr_jmp(ctx, ctx->instr->arg1.uint);
908 return S_OK;
911 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
913 const unsigned arg = ctx->instr->arg1.uint;
914 variant_val_t val;
915 HRESULT hres;
917 TRACE("%u\n", arg);
919 hres = stack_pop_val(ctx, &val);
920 if(FAILED(hres))
921 return hres;
923 if(V_VT(val.v) != VT_BOOL) {
924 FIXME("unsupported for %s\n", debugstr_variant(val.v));
925 release_val(&val);
926 return E_NOTIMPL;
929 if(V_BOOL(val.v))
930 instr_jmp(ctx, ctx->instr->arg1.uint);
931 else
932 ctx->instr++;
933 return S_OK;
936 static HRESULT interp_ret(exec_ctx_t *ctx)
938 TRACE("\n");
940 ctx->instr = NULL;
941 return S_OK;
944 static HRESULT interp_stop(exec_ctx_t *ctx)
946 WARN("\n");
948 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
949 return S_OK;
952 static HRESULT interp_me(exec_ctx_t *ctx)
954 VARIANT v;
956 TRACE("\n");
958 IDispatch_AddRef(ctx->this_obj);
959 V_VT(&v) = VT_DISPATCH;
960 V_DISPATCH(&v) = ctx->this_obj;
961 return stack_push(ctx, &v);
964 static HRESULT interp_bool(exec_ctx_t *ctx)
966 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
967 VARIANT v;
969 TRACE("%s\n", arg ? "true" : "false");
971 V_VT(&v) = VT_BOOL;
972 V_BOOL(&v) = arg;
973 return stack_push(ctx, &v);
976 static HRESULT interp_errmode(exec_ctx_t *ctx)
978 const int err_mode = ctx->instr->arg1.uint;
980 TRACE("%d\n", err_mode);
982 ctx->resume_next = err_mode;
983 return S_OK;
986 static HRESULT interp_string(exec_ctx_t *ctx)
988 VARIANT v;
990 TRACE("\n");
992 V_VT(&v) = VT_BSTR;
993 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
994 if(!V_BSTR(&v))
995 return E_OUTOFMEMORY;
997 return stack_push(ctx, &v);
1000 static HRESULT interp_long(exec_ctx_t *ctx)
1002 const LONG arg = ctx->instr->arg1.lng;
1003 VARIANT v;
1005 TRACE("%d\n", arg);
1007 V_VT(&v) = VT_I4;
1008 V_I4(&v) = arg;
1009 return stack_push(ctx, &v);
1012 static HRESULT interp_short(exec_ctx_t *ctx)
1014 const LONG arg = ctx->instr->arg1.lng;
1015 VARIANT v;
1017 TRACE("%d\n", arg);
1019 V_VT(&v) = VT_I2;
1020 V_I2(&v) = arg;
1021 return stack_push(ctx, &v);
1024 static HRESULT interp_double(exec_ctx_t *ctx)
1026 const DOUBLE *arg = ctx->instr->arg1.dbl;
1027 VARIANT v;
1029 TRACE("%lf\n", *arg);
1031 V_VT(&v) = VT_R8;
1032 V_R8(&v) = *arg;
1033 return stack_push(ctx, &v);
1036 static HRESULT interp_empty(exec_ctx_t *ctx)
1038 VARIANT v;
1040 TRACE("\n");
1042 V_VT(&v) = VT_EMPTY;
1043 return stack_push(ctx, &v);
1046 static HRESULT interp_null(exec_ctx_t *ctx)
1048 VARIANT v;
1050 TRACE("\n");
1052 V_VT(&v) = VT_NULL;
1053 return stack_push(ctx, &v);
1056 static HRESULT interp_nothing(exec_ctx_t *ctx)
1058 VARIANT v;
1060 TRACE("\n");
1062 V_VT(&v) = VT_DISPATCH;
1063 V_DISPATCH(&v) = NULL;
1064 return stack_push(ctx, &v);
1067 static HRESULT interp_not(exec_ctx_t *ctx)
1069 variant_val_t val;
1070 VARIANT v;
1071 HRESULT hres;
1073 TRACE("\n");
1075 hres = stack_pop_val(ctx, &val);
1076 if(FAILED(hres))
1077 return hres;
1079 hres = VarNot(val.v, &v);
1080 release_val(&val);
1081 if(FAILED(hres))
1082 return hres;
1084 return stack_push(ctx, &v);
1087 static HRESULT interp_and(exec_ctx_t *ctx)
1089 variant_val_t r, l;
1090 VARIANT v;
1091 HRESULT hres;
1093 TRACE("\n");
1095 hres = stack_pop_val(ctx, &r);
1096 if(FAILED(hres))
1097 return hres;
1099 hres = stack_pop_val(ctx, &l);
1100 if(SUCCEEDED(hres)) {
1101 hres = VarAnd(l.v, r.v, &v);
1102 release_val(&l);
1104 release_val(&r);
1105 if(FAILED(hres))
1106 return hres;
1108 return stack_push(ctx, &v);
1111 static HRESULT interp_or(exec_ctx_t *ctx)
1113 variant_val_t r, l;
1114 VARIANT v;
1115 HRESULT hres;
1117 TRACE("\n");
1119 hres = stack_pop_val(ctx, &r);
1120 if(FAILED(hres))
1121 return hres;
1123 hres = stack_pop_val(ctx, &l);
1124 if(SUCCEEDED(hres)) {
1125 hres = VarOr(l.v, r.v, &v);
1126 release_val(&l);
1128 release_val(&r);
1129 if(FAILED(hres))
1130 return hres;
1132 return stack_push(ctx, &v);
1135 static HRESULT interp_xor(exec_ctx_t *ctx)
1137 variant_val_t r, l;
1138 VARIANT v;
1139 HRESULT hres;
1141 TRACE("\n");
1143 hres = stack_pop_val(ctx, &r);
1144 if(FAILED(hres))
1145 return hres;
1147 hres = stack_pop_val(ctx, &l);
1148 if(SUCCEEDED(hres)) {
1149 hres = VarXor(l.v, r.v, &v);
1150 release_val(&l);
1152 release_val(&r);
1153 if(FAILED(hres))
1154 return hres;
1156 return stack_push(ctx, &v);
1159 static HRESULT interp_eqv(exec_ctx_t *ctx)
1161 variant_val_t r, l;
1162 VARIANT v;
1163 HRESULT hres;
1165 TRACE("\n");
1167 hres = stack_pop_val(ctx, &r);
1168 if(FAILED(hres))
1169 return hres;
1171 hres = stack_pop_val(ctx, &l);
1172 if(SUCCEEDED(hres)) {
1173 hres = VarEqv(l.v, r.v, &v);
1174 release_val(&l);
1176 release_val(&r);
1177 if(FAILED(hres))
1178 return hres;
1180 return stack_push(ctx, &v);
1183 static HRESULT interp_imp(exec_ctx_t *ctx)
1185 variant_val_t r, l;
1186 VARIANT v;
1187 HRESULT hres;
1189 TRACE("\n");
1191 hres = stack_pop_val(ctx, &r);
1192 if(FAILED(hres))
1193 return hres;
1195 hres = stack_pop_val(ctx, &l);
1196 if(SUCCEEDED(hres)) {
1197 hres = VarImp(l.v, r.v, &v);
1198 release_val(&l);
1200 release_val(&r);
1201 if(FAILED(hres))
1202 return hres;
1204 return stack_push(ctx, &v);
1207 static HRESULT cmp_oper(exec_ctx_t *ctx)
1209 variant_val_t l, r;
1210 HRESULT hres;
1212 hres = stack_pop_val(ctx, &r);
1213 if(FAILED(hres))
1214 return hres;
1216 hres = stack_pop_val(ctx, &l);
1217 if(SUCCEEDED(hres)) {
1218 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1219 FIXME("comparing nulls is not implemented\n");
1220 hres = E_NOTIMPL;
1221 }else {
1222 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1226 release_val(&r);
1227 release_val(&l);
1228 return hres;
1231 static HRESULT interp_equal(exec_ctx_t *ctx)
1233 VARIANT v;
1234 HRESULT hres;
1236 TRACE("\n");
1238 hres = cmp_oper(ctx);
1239 if(FAILED(hres))
1240 return hres;
1242 V_VT(&v) = VT_BOOL;
1243 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1244 return stack_push(ctx, &v);
1247 static HRESULT interp_nequal(exec_ctx_t *ctx)
1249 VARIANT v;
1250 HRESULT hres;
1252 TRACE("\n");
1254 hres = cmp_oper(ctx);
1255 if(FAILED(hres))
1256 return hres;
1258 V_VT(&v) = VT_BOOL;
1259 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1260 return stack_push(ctx, &v);
1263 static HRESULT interp_gt(exec_ctx_t *ctx)
1265 VARIANT v;
1266 HRESULT hres;
1268 TRACE("\n");
1270 hres = cmp_oper(ctx);
1271 if(FAILED(hres))
1272 return hres;
1274 V_VT(&v) = VT_BOOL;
1275 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1276 return stack_push(ctx, &v);
1279 static HRESULT interp_gteq(exec_ctx_t *ctx)
1281 VARIANT v;
1282 HRESULT hres;
1284 TRACE("\n");
1286 hres = cmp_oper(ctx);
1287 if(FAILED(hres))
1288 return hres;
1290 V_VT(&v) = VT_BOOL;
1291 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1292 return stack_push(ctx, &v);
1295 static HRESULT interp_lt(exec_ctx_t *ctx)
1297 VARIANT v;
1298 HRESULT hres;
1300 TRACE("\n");
1302 hres = cmp_oper(ctx);
1303 if(FAILED(hres))
1304 return hres;
1306 V_VT(&v) = VT_BOOL;
1307 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1308 return stack_push(ctx, &v);
1311 static HRESULT interp_lteq(exec_ctx_t *ctx)
1313 VARIANT v;
1314 HRESULT hres;
1316 TRACE("\n");
1318 hres = cmp_oper(ctx);
1319 if(FAILED(hres))
1320 return hres;
1322 V_VT(&v) = VT_BOOL;
1323 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1324 return stack_push(ctx, &v);
1327 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1329 IObjectIdentity *identity;
1330 IUnknown *unk1, *unk2;
1331 HRESULT hres;
1333 if(disp1 == disp2) {
1334 *ret = VARIANT_TRUE;
1335 return S_OK;
1338 if(!disp1 || !disp2) {
1339 *ret = VARIANT_FALSE;
1340 return S_OK;
1343 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1344 if(FAILED(hres))
1345 return hres;
1347 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1348 if(FAILED(hres)) {
1349 IUnknown_Release(unk1);
1350 return hres;
1353 if(unk1 == unk2) {
1354 *ret = VARIANT_TRUE;
1355 }else {
1356 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1357 if(SUCCEEDED(hres)) {
1358 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1359 IObjectIdentity_Release(identity);
1360 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1361 }else {
1362 *ret = VARIANT_FALSE;
1366 IUnknown_Release(unk1);
1367 IUnknown_Release(unk2);
1368 return S_OK;
1371 static HRESULT interp_is(exec_ctx_t *ctx)
1373 IDispatch *l, *r;
1374 VARIANT v;
1375 HRESULT hres;
1377 TRACE("\n");
1379 hres = stack_pop_disp(ctx, &r);
1380 if(FAILED(hres))
1381 return hres;
1383 hres = stack_pop_disp(ctx, &l);
1384 if(SUCCEEDED(hres)) {
1385 V_VT(&v) = VT_BOOL;
1386 hres = disp_cmp(l, r, &V_BOOL(&v));
1387 if(l)
1388 IDispatch_Release(l);
1390 if(r)
1391 IDispatch_Release(r);
1392 if(FAILED(hres))
1393 return hres;
1395 return stack_push(ctx, &v);
1398 static HRESULT interp_concat(exec_ctx_t *ctx)
1400 variant_val_t r, l;
1401 VARIANT v;
1402 HRESULT hres;
1404 TRACE("\n");
1406 hres = stack_pop_val(ctx, &r);
1407 if(FAILED(hres))
1408 return hres;
1410 hres = stack_pop_val(ctx, &l);
1411 if(SUCCEEDED(hres)) {
1412 hres = VarCat(l.v, r.v, &v);
1413 release_val(&l);
1415 release_val(&r);
1416 if(FAILED(hres))
1417 return hres;
1419 return stack_push(ctx, &v);
1422 static HRESULT interp_add(exec_ctx_t *ctx)
1424 variant_val_t r, l;
1425 VARIANT v;
1426 HRESULT hres;
1428 TRACE("\n");
1430 hres = stack_pop_val(ctx, &r);
1431 if(FAILED(hres))
1432 return hres;
1434 hres = stack_pop_val(ctx, &l);
1435 if(SUCCEEDED(hres)) {
1436 hres = VarAdd(l.v, r.v, &v);
1437 release_val(&l);
1439 release_val(&r);
1440 if(FAILED(hres))
1441 return hres;
1443 return stack_push(ctx, &v);
1446 static HRESULT interp_sub(exec_ctx_t *ctx)
1448 variant_val_t r, l;
1449 VARIANT v;
1450 HRESULT hres;
1452 TRACE("\n");
1454 hres = stack_pop_val(ctx, &r);
1455 if(FAILED(hres))
1456 return hres;
1458 hres = stack_pop_val(ctx, &l);
1459 if(SUCCEEDED(hres)) {
1460 hres = VarSub(l.v, r.v, &v);
1461 release_val(&l);
1463 release_val(&r);
1464 if(FAILED(hres))
1465 return hres;
1467 return stack_push(ctx, &v);
1470 static HRESULT interp_mod(exec_ctx_t *ctx)
1472 variant_val_t r, l;
1473 VARIANT v;
1474 HRESULT hres;
1476 TRACE("\n");
1478 hres = stack_pop_val(ctx, &r);
1479 if(FAILED(hres))
1480 return hres;
1482 hres = stack_pop_val(ctx, &l);
1483 if(SUCCEEDED(hres)) {
1484 hres = VarMod(l.v, r.v, &v);
1485 release_val(&l);
1487 release_val(&r);
1488 if(FAILED(hres))
1489 return hres;
1491 return stack_push(ctx, &v);
1494 static HRESULT interp_idiv(exec_ctx_t *ctx)
1496 variant_val_t r, l;
1497 VARIANT v;
1498 HRESULT hres;
1500 TRACE("\n");
1502 hres = stack_pop_val(ctx, &r);
1503 if(FAILED(hres))
1504 return hres;
1506 hres = stack_pop_val(ctx, &l);
1507 if(SUCCEEDED(hres)) {
1508 hres = VarIdiv(l.v, r.v, &v);
1509 release_val(&l);
1511 release_val(&r);
1512 if(FAILED(hres))
1513 return hres;
1515 return stack_push(ctx, &v);
1518 static HRESULT interp_div(exec_ctx_t *ctx)
1520 variant_val_t r, l;
1521 VARIANT v;
1522 HRESULT hres;
1524 TRACE("\n");
1526 hres = stack_pop_val(ctx, &r);
1527 if(FAILED(hres))
1528 return hres;
1530 hres = stack_pop_val(ctx, &l);
1531 if(SUCCEEDED(hres)) {
1532 hres = VarDiv(l.v, r.v, &v);
1533 release_val(&l);
1535 release_val(&r);
1536 if(FAILED(hres))
1537 return hres;
1539 return stack_push(ctx, &v);
1542 static HRESULT interp_mul(exec_ctx_t *ctx)
1544 variant_val_t r, l;
1545 VARIANT v;
1546 HRESULT hres;
1548 TRACE("\n");
1550 hres = stack_pop_val(ctx, &r);
1551 if(FAILED(hres))
1552 return hres;
1554 hres = stack_pop_val(ctx, &l);
1555 if(SUCCEEDED(hres)) {
1556 hres = VarMul(l.v, r.v, &v);
1557 release_val(&l);
1559 release_val(&r);
1560 if(FAILED(hres))
1561 return hres;
1563 return stack_push(ctx, &v);
1566 static HRESULT interp_exp(exec_ctx_t *ctx)
1568 variant_val_t r, l;
1569 VARIANT v;
1570 HRESULT hres;
1572 TRACE("\n");
1574 hres = stack_pop_val(ctx, &r);
1575 if(FAILED(hres))
1576 return hres;
1578 hres = stack_pop_val(ctx, &l);
1579 if(SUCCEEDED(hres)) {
1580 hres = VarPow(l.v, r.v, &v);
1581 release_val(&l);
1583 release_val(&r);
1584 if(FAILED(hres))
1585 return hres;
1587 return stack_push(ctx, &v);
1590 static HRESULT interp_neg(exec_ctx_t *ctx)
1592 variant_val_t val;
1593 VARIANT v;
1594 HRESULT hres;
1596 hres = stack_pop_val(ctx, &val);
1597 if(FAILED(hres))
1598 return hres;
1600 hres = VarNeg(val.v, &v);
1601 release_val(&val);
1602 if(FAILED(hres))
1603 return hres;
1605 return stack_push(ctx, &v);
1608 static HRESULT interp_incc(exec_ctx_t *ctx)
1610 const BSTR ident = ctx->instr->arg1.bstr;
1611 VARIANT v;
1612 ref_t ref;
1613 HRESULT hres;
1615 TRACE("\n");
1617 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1618 if(FAILED(hres))
1619 return hres;
1621 if(ref.type != REF_VAR) {
1622 FIXME("ref.type is not REF_VAR\n");
1623 return E_FAIL;
1626 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1627 if(FAILED(hres))
1628 return hres;
1630 VariantClear(ref.u.v);
1631 *ref.u.v = v;
1632 return S_OK;
1635 static const instr_func_t op_funcs[] = {
1636 #define X(x,n,a,b) interp_ ## x,
1637 OP_LIST
1638 #undef X
1641 static const unsigned op_move[] = {
1642 #define X(x,n,a,b) n,
1643 OP_LIST
1644 #undef X
1647 void release_dynamic_vars(dynamic_var_t *var)
1649 while(var) {
1650 VariantClear(&var->v);
1651 var = var->next;
1655 static void release_exec(exec_ctx_t *ctx)
1657 unsigned i;
1659 VariantClear(&ctx->ret_val);
1660 release_dynamic_vars(ctx->dynamic_vars);
1662 if(ctx->this_obj)
1663 IDispatch_Release(ctx->this_obj);
1665 if(ctx->args) {
1666 for(i=0; i < ctx->func->arg_cnt; i++)
1667 VariantClear(ctx->args+i);
1670 if(ctx->vars) {
1671 for(i=0; i < ctx->func->var_cnt; i++)
1672 VariantClear(ctx->vars+i);
1675 vbsheap_free(&ctx->heap);
1676 heap_free(ctx->args);
1677 heap_free(ctx->vars);
1678 heap_free(ctx->stack);
1681 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1683 exec_ctx_t exec = {func->code_ctx};
1684 vbsop_t op;
1685 HRESULT hres = S_OK;
1687 exec.code = func->code_ctx;
1689 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1690 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1691 return E_FAIL;
1694 vbsheap_init(&exec.heap);
1696 if(func->arg_cnt) {
1697 VARIANT *v;
1698 unsigned i;
1700 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1701 if(!exec.args) {
1702 release_exec(&exec);
1703 return E_OUTOFMEMORY;
1706 for(i=0; i < func->arg_cnt; i++) {
1707 v = get_arg(dp, i);
1708 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1709 if(func->args[i].by_ref)
1710 exec.args[i] = *v;
1711 else
1712 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1713 }else {
1714 hres = VariantCopy(exec.args+i, v);
1716 if(FAILED(hres)) {
1717 release_exec(&exec);
1718 return hres;
1721 }else {
1722 exec.args = NULL;
1725 if(func->var_cnt) {
1726 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1727 if(!exec.vars) {
1728 release_exec(&exec);
1729 return E_OUTOFMEMORY;
1731 }else {
1732 exec.vars = NULL;
1735 exec.stack_size = 16;
1736 exec.top = 0;
1737 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1738 if(!exec.stack) {
1739 release_exec(&exec);
1740 return E_OUTOFMEMORY;
1743 if(this_obj)
1744 exec.this_obj = this_obj;
1745 else if (ctx->host_global)
1746 exec.this_obj = ctx->host_global;
1747 else
1748 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1749 IDispatch_AddRef(exec.this_obj);
1751 exec.instr = exec.code->instrs + func->code_off;
1752 exec.script = ctx;
1753 exec.func = func;
1755 while(exec.instr) {
1756 op = exec.instr->op;
1757 hres = op_funcs[op](&exec);
1758 if(FAILED(hres)) {
1759 if(exec.resume_next)
1760 FIXME("Failed %08x in resume next mode\n", hres);
1761 else
1762 WARN("Failed %08x\n", hres);
1763 stack_popn(&exec, exec.top);
1764 break;
1767 exec.instr += op_move[op];
1770 assert(!exec.top);
1771 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1772 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1774 if(SUCCEEDED(hres) && res) {
1775 *res = exec.ret_val;
1776 V_VT(&exec.ret_val) = VT_EMPTY;
1779 release_exec(&exec);
1780 return hres;