vbscript: Use 0 as invalid instruction offset.
[wine] / dlls / vbscript / interp.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <assert.h>
20
21 #include "vbscript.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
26
27 typedef struct {
28     vbscode_t *code;
29     instr_t *instr;
30     script_ctx_t *script;
31     function_t *func;
32     IDispatch *this_obj;
33
34     VARIANT *args;
35     VARIANT *vars;
36
37     dynamic_var_t *dynamic_vars;
38     vbsheap_t heap;
39
40     BOOL resume_next;
41
42     unsigned stack_size;
43     unsigned top;
44     VARIANT *stack;
45
46     VARIANT ret_val;
47 } exec_ctx_t;
48
49 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
50
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;
59
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;
72
73 typedef struct {
74     VARIANT *v;
75     VARIANT store;
76     BOOL owned;
77 } variant_val_t;
78
79 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
80 {
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;
86         }
87
88         var = var->next;
89     }
90
91     return FALSE;
92 }
93
94 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
95 {
96     named_item_t *item;
97     function_t *func;
98     unsigned i;
99     DISPID id;
100     HRESULT hres;
101
102     static const WCHAR errW[] = {'e','r','r',0};
103
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;
110     }
111
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;
117         }
118     }
119
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;
125         }
126     }
127
128     if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
129         return S_OK;
130
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;
138         }
139     }
140
141     if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
142         return S_OK;
143
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;
149         }
150     }
151
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;
156     }
157
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;
164     }
165
166     LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
167         if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
168             hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
169             if(SUCCEEDED(hres)) {
170                 ref->type = REF_DISP;
171                 ref->u.d.disp = item->disp;
172                 ref->u.d.id = id;
173                 return S_OK;
174             }
175         }
176
177         if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
178             if(!item->disp) {
179                 IUnknown *unk;
180
181                 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
182                 if(FAILED(hres)) {
183                     WARN("GetItemInfo failed: %08x\n", hres);
184                     continue;
185                 }
186
187                 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
188                 IUnknown_Release(unk);
189                 if(FAILED(hres)) {
190                     WARN("object does not implement IDispatch\n");
191                     continue;
192                 }
193             }
194
195             ref->type = REF_OBJ;
196             ref->u.obj = item->disp;
197             return S_OK;
198         }
199     }
200
201     ref->type = REF_NONE;
202     return S_OK;
203 }
204
205 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
206 {
207     dynamic_var_t *new_var;
208     vbsheap_t *heap;
209     WCHAR *str;
210     unsigned size;
211     HRESULT hres;
212
213     heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
214
215     new_var = vbsheap_alloc(heap, sizeof(*new_var));
216     if(!new_var)
217         return E_OUTOFMEMORY;
218
219     size = (strlenW(name)+1)*sizeof(WCHAR);
220     str = vbsheap_alloc(heap, size);
221     if(!str)
222         return E_OUTOFMEMORY;
223     memcpy(str, name, size);
224     new_var->name = str;
225     new_var->is_const = is_const;
226
227     if(own_val) {
228         new_var->v = *val;
229     }else {
230         hres = VariantCopy(&new_var->v, val);
231         if(FAILED(hres))
232             return hres;
233     }
234
235     if(ctx->func->type == FUNC_GLOBAL) {
236         new_var->next = ctx->script->global_vars;
237         ctx->script->global_vars = new_var;
238     }else {
239         new_var->next = ctx->dynamic_vars;
240         ctx->dynamic_vars = new_var;
241     }
242
243     return S_OK;
244 }
245
246 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
247 {
248     assert(ctx->top);
249     return ctx->stack + --ctx->top;
250 }
251
252 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
253 {
254     assert(ctx->top >= n);
255     return ctx->stack + (ctx->top-n-1);
256 }
257
258 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
259 {
260     if(ctx->stack_size == ctx->top) {
261         VARIANT *new_stack;
262
263         new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
264         if(!new_stack) {
265             VariantClear(v);
266             return E_OUTOFMEMORY;
267         }
268
269         ctx->stack = new_stack;
270         ctx->stack_size *= 2;
271     }
272
273     ctx->stack[ctx->top++] = *v;
274     return S_OK;
275 }
276
277 static void stack_popn(exec_ctx_t *ctx, unsigned n)
278 {
279     while(n--)
280         VariantClear(stack_pop(ctx));
281 }
282
283 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
284 {
285     VARIANT *var;
286
287     var = stack_pop(ctx);
288
289     if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
290         v->owned = FALSE;
291         var = V_VARIANTREF(var);
292     }else {
293         v->owned = TRUE;
294     }
295
296     if(V_VT(var) == VT_DISPATCH) {
297         DISPPARAMS dp = {0};
298         HRESULT hres;
299
300         hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
301         if(v->owned)
302             IDispatch_Release(V_DISPATCH(var));
303         if(FAILED(hres))
304             return hres;
305
306         v->owned = TRUE;
307         v->v = &v->store;
308     }else {
309         v->v = var;
310     }
311
312     return S_OK;
313 }
314
315 static inline void release_val(variant_val_t *v)
316 {
317     if(v->owned)
318         VariantClear(v->v);
319 }
320
321 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
322 {
323     VARIANT *v = stack_pop(ctx);
324
325     if(V_VT(v) == VT_DISPATCH) {
326         *ret = V_DISPATCH(v);
327         return S_OK;
328     }
329
330     if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
331         FIXME("not supported type: %s\n", debugstr_variant(v));
332         VariantClear(v);
333         return E_FAIL;
334     }
335
336     v = V_BYREF(v);
337     if(V_VT(v) != VT_DISPATCH) {
338         FIXME("not disp %s\n", debugstr_variant(v));
339         return E_FAIL;
340     }
341
342     if(V_DISPATCH(v))
343         IDispatch_AddRef(V_DISPATCH(v));
344     *ret = V_DISPATCH(v);
345     return S_OK;
346 }
347
348 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
349 {
350     ctx->instr = ctx->code->instrs + addr;
351 }
352
353 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
354 {
355     dp->cArgs = arg_cnt;
356     dp->rgdispidNamedArgs = NULL;
357     dp->cNamedArgs = 0;
358
359     if(arg_cnt) {
360         VARIANT tmp;
361         unsigned i;
362
363         assert(ctx->top >= arg_cnt);
364
365         for(i=1; i*2 <= arg_cnt; i++) {
366             tmp = ctx->stack[ctx->top-i];
367             ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
368             ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
369         }
370
371         dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
372     }else {
373         dp->rgvarg = NULL;
374     }
375 }
376
377 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
378 {
379     BSTR identifier = ctx->instr->arg1.bstr;
380     const unsigned arg_cnt = ctx->instr->arg2.uint;
381     DISPPARAMS dp;
382     ref_t ref;
383     HRESULT hres;
384
385     hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
386     if(FAILED(hres))
387         return hres;
388
389     vbstack_to_dp(ctx, arg_cnt, &dp);
390
391     switch(ref.type) {
392     case REF_VAR:
393     case REF_CONST:
394         if(!res) {
395             FIXME("REF_VAR no res\n");
396             return E_NOTIMPL;
397         }
398
399         if(arg_cnt) {
400             FIXME("arguments not implemented\n");
401             return E_NOTIMPL;
402         }
403
404         V_VT(res) = VT_BYREF|VT_VARIANT;
405         V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
406         break;
407     case REF_DISP:
408         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
409         if(FAILED(hres))
410             return hres;
411         break;
412     case REF_FUNC:
413         hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
414         if(FAILED(hres))
415             return hres;
416         break;
417     case REF_OBJ:
418         if(arg_cnt) {
419             FIXME("arguments on object\n");
420             return E_NOTIMPL;
421         }
422
423         if(res) {
424             IDispatch_AddRef(ref.u.obj);
425             V_VT(res) = VT_DISPATCH;
426             V_DISPATCH(res) = ref.u.obj;
427         }
428         break;
429     case REF_NONE:
430         FIXME("%s not found\n", debugstr_w(identifier));
431         return DISP_E_UNKNOWNNAME;
432     }
433
434     stack_popn(ctx, arg_cnt);
435     return S_OK;
436 }
437
438 static HRESULT interp_icall(exec_ctx_t *ctx)
439 {
440     VARIANT v;
441     HRESULT hres;
442
443     TRACE("\n");
444
445     hres = do_icall(ctx, &v);
446     if(FAILED(hres))
447         return hres;
448
449     return stack_push(ctx, &v);
450 }
451
452 static HRESULT interp_icallv(exec_ctx_t *ctx)
453 {
454     TRACE("\n");
455     return do_icall(ctx, NULL);
456 }
457
458 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
459 {
460     const BSTR identifier = ctx->instr->arg1.bstr;
461     const unsigned arg_cnt = ctx->instr->arg2.uint;
462     IDispatch *obj;
463     DISPPARAMS dp;
464     DISPID id;
465     HRESULT hres;
466
467     hres = stack_pop_disp(ctx, &obj);
468     if(FAILED(hres))
469         return hres;
470
471     if(!obj) {
472         FIXME("NULL obj\n");
473         return E_FAIL;
474     }
475
476     vbstack_to_dp(ctx, arg_cnt, &dp);
477
478     hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
479     if(SUCCEEDED(hres))
480         hres = disp_call(ctx->script, obj, id, &dp, res);
481     IDispatch_Release(obj);
482     if(FAILED(hres))
483         return hres;
484
485     stack_popn(ctx, arg_cnt);
486     return S_OK;
487 }
488
489 static HRESULT interp_mcall(exec_ctx_t *ctx)
490 {
491     VARIANT res;
492     HRESULT hres;
493
494     TRACE("\n");
495
496     hres = do_mcall(ctx, &res);
497     if(FAILED(hres))
498         return hres;
499
500     return stack_push(ctx, &res);
501 }
502
503 static HRESULT interp_mcallv(exec_ctx_t *ctx)
504 {
505     TRACE("\n");
506
507     return do_mcall(ctx, NULL);
508 }
509
510 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
511 {
512     ref_t ref;
513     HRESULT hres;
514
515     hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
516     if(FAILED(hres))
517         return hres;
518
519     switch(ref.type) {
520     case REF_VAR: {
521         VARIANT *v = ref.u.v;
522
523         if(V_VT(v) == (VT_VARIANT|VT_BYREF))
524             v = V_VARIANTREF(v);
525
526         if(own_val) {
527             VariantClear(v);
528             *v = *val;
529             hres = S_OK;
530         }else {
531             hres = VariantCopy(v, val);
532         }
533         break;
534     }
535     case REF_DISP:
536         hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
537         if(own_val)
538             VariantClear(val);
539         break;
540     case REF_FUNC:
541         FIXME("functions not implemented\n");
542         return E_NOTIMPL;
543     case REF_OBJ:
544         FIXME("REF_OBJ\n");
545         return E_NOTIMPL;
546     case REF_CONST:
547         FIXME("REF_CONST\n");
548         return E_NOTIMPL;
549     case REF_NONE:
550         if(ctx->func->code_ctx->option_explicit) {
551             FIXME("throw exception\n");
552             hres = E_FAIL;
553         }else {
554             TRACE("creating variable %s\n", debugstr_w(name));
555             hres = add_dynamic_var(ctx, name, FALSE, val, own_val);
556         }
557     }
558
559     return hres;
560 }
561
562 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
563 {
564     const BSTR arg = ctx->instr->arg1.bstr;
565     const unsigned arg_cnt = ctx->instr->arg2.uint;
566     variant_val_t v;
567     HRESULT hres;
568
569     TRACE("%s\n", debugstr_w(arg));
570
571     if(arg_cnt) {
572         FIXME("arguments not supported\n");
573         return E_NOTIMPL;
574     }
575
576     hres = stack_pop_val(ctx, &v);
577     if(FAILED(hres))
578         return hres;
579
580     return assign_ident(ctx, arg, v.v, v.owned);
581 }
582
583 static HRESULT interp_set_ident(exec_ctx_t *ctx)
584 {
585     const BSTR arg = ctx->instr->arg1.bstr;
586     const unsigned arg_cnt = ctx->instr->arg2.uint;
587     IDispatch *disp;
588     VARIANT v;
589     HRESULT hres;
590
591     TRACE("%s\n", debugstr_w(arg));
592
593     if(arg_cnt) {
594         FIXME("arguments not supported\n");
595         return E_NOTIMPL;
596     }
597
598     hres = stack_pop_disp(ctx, &disp);
599     if(FAILED(hres))
600         return hres;
601
602     V_VT(&v) = VT_DISPATCH;
603     V_DISPATCH(&v) = disp;
604     return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
605 }
606
607 static HRESULT interp_assign_member(exec_ctx_t *ctx)
608 {
609     BSTR identifier = ctx->instr->arg1.bstr;
610     const unsigned arg_cnt = ctx->instr->arg2.uint;
611     variant_val_t val;
612     IDispatch *obj;
613     DISPID id;
614     HRESULT hres;
615
616     TRACE("%s\n", debugstr_w(identifier));
617
618     if(arg_cnt) {
619         FIXME("arguments not supported\n");
620         return E_NOTIMPL;
621     }
622
623     hres = stack_pop_disp(ctx, &obj);
624     if(FAILED(hres))
625         return hres;
626
627     if(!obj) {
628         FIXME("NULL obj\n");
629         return E_FAIL;
630     }
631
632     hres = stack_pop_val(ctx, &val);
633     if(FAILED(hres)) {
634         IDispatch_Release(obj);
635         return hres;
636     }
637
638     hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
639     if(SUCCEEDED(hres))
640         hres = disp_propput(ctx->script, obj, id, val.v);
641
642     release_val(&val);
643     IDispatch_Release(obj);
644     return hres;
645 }
646
647 static HRESULT interp_set_member(exec_ctx_t *ctx)
648 {
649     BSTR identifier = ctx->instr->arg1.bstr;
650     const unsigned arg_cnt = ctx->instr->arg2.uint;
651     IDispatch *obj, *val;
652     DISPID id;
653     HRESULT hres;
654
655     TRACE("%s\n", debugstr_w(identifier));
656
657     if(arg_cnt) {
658         FIXME("arguments not supported\n");
659         return E_NOTIMPL;
660     }
661
662     hres = stack_pop_disp(ctx, &obj);
663     if(FAILED(hres))
664         return hres;
665
666     if(!obj) {
667         FIXME("NULL obj\n");
668         return E_FAIL;
669     }
670
671     hres = stack_pop_disp(ctx, &val);
672     if(FAILED(hres)) {
673         IDispatch_Release(obj);
674         return hres;
675     }
676
677     hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
678     if(SUCCEEDED(hres)) {
679         VARIANT v;
680
681         V_VT(&v) = VT_DISPATCH;
682         V_DISPATCH(&v) = val;
683         hres = disp_propput(ctx->script, obj, id, &v);
684     }
685
686     if(val)
687         IDispatch_Release(val);
688     IDispatch_Release(obj);
689     return hres;
690 }
691
692 static HRESULT interp_const(exec_ctx_t *ctx)
693 {
694     BSTR arg = ctx->instr->arg1.bstr;
695     variant_val_t val;
696     ref_t ref;
697     HRESULT hres;
698
699     TRACE("%s\n", debugstr_w(arg));
700
701     assert(ctx->func->type == FUNC_GLOBAL);
702
703     hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
704     if(FAILED(hres))
705         return hres;
706
707     if(ref.type != REF_NONE) {
708         FIXME("%s already defined\n", debugstr_w(arg));
709         return E_FAIL;
710     }
711
712     hres = stack_pop_val(ctx, &val);
713     if(FAILED(hres))
714         return hres;
715
716     return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
717 }
718
719 static HRESULT interp_val(exec_ctx_t *ctx)
720 {
721     variant_val_t val;
722     VARIANT v;
723     HRESULT hres;
724
725     TRACE("\n");
726
727     hres = stack_pop_val(ctx, &val);
728     if(FAILED(hres))
729         return hres;
730
731     if(!val.owned) {
732         V_VT(&v) = VT_EMPTY;
733         hres = VariantCopy(&v, val.v);
734         if(FAILED(hres))
735             return hres;
736     }
737
738     return stack_push(ctx, val.owned ? val.v : &v);
739 }
740
741 static HRESULT interp_pop(exec_ctx_t *ctx)
742 {
743     const unsigned n = ctx->instr->arg1.uint;
744
745     TRACE("%u\n", n);
746
747     stack_popn(ctx, n);
748     return S_OK;
749 }
750
751 static HRESULT interp_new(exec_ctx_t *ctx)
752 {
753     const WCHAR *arg = ctx->instr->arg1.bstr;
754     class_desc_t *class_desc;
755     vbdisp_t *obj;
756     VARIANT v;
757     HRESULT hres;
758
759     TRACE("%s\n", debugstr_w(arg));
760
761     for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
762         if(!strcmpiW(class_desc->name, arg))
763             break;
764     }
765     if(!class_desc) {
766         FIXME("Class %s not found\n", debugstr_w(arg));
767         return E_FAIL;
768     }
769
770     hres = create_vbdisp(class_desc, &obj);
771     if(FAILED(hres))
772         return hres;
773
774     V_VT(&v) = VT_DISPATCH;
775     V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
776     return stack_push(ctx, &v);
777 }
778
779 static HRESULT interp_step(exec_ctx_t *ctx)
780 {
781     const BSTR ident = ctx->instr->arg2.bstr;
782     BOOL gteq_zero;
783     VARIANT zero;
784     ref_t ref;
785     HRESULT hres;
786
787     TRACE("%s\n", debugstr_w(ident));
788
789     V_VT(&zero) = VT_I2;
790     V_I2(&zero) = 0;
791     hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
792     if(FAILED(hres))
793         return hres;
794
795     gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
796
797     hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
798     if(FAILED(hres))
799         return hres;
800
801     if(ref.type != REF_VAR) {
802         FIXME("%s is not REF_VAR\n", debugstr_w(ident));
803         return E_FAIL;
804     }
805
806     hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
807     if(FAILED(hres))
808         return hres;
809
810     if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
811         ctx->instr++;
812     else
813         instr_jmp(ctx, ctx->instr->arg1.uint);
814     return S_OK;
815 }
816
817 static HRESULT interp_jmp(exec_ctx_t *ctx)
818 {
819     const unsigned arg = ctx->instr->arg1.uint;
820
821     TRACE("%u\n", arg);
822
823     instr_jmp(ctx, arg);
824     return S_OK;
825 }
826
827 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
828 {
829     const unsigned arg = ctx->instr->arg1.uint;
830     variant_val_t val;
831     HRESULT hres;
832
833     TRACE("%u\n", arg);
834
835     hres = stack_pop_val(ctx, &val);
836     if(FAILED(hres))
837         return hres;
838
839     if(V_VT(val.v) != VT_BOOL) {
840         FIXME("unsupported for %s\n", debugstr_variant(val.v));
841         release_val(&val);
842         return E_NOTIMPL;
843     }
844
845     if(V_BOOL(val.v))
846         ctx->instr++;
847     else
848         instr_jmp(ctx, ctx->instr->arg1.uint);
849     return S_OK;
850 }
851
852 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
853 {
854     const unsigned arg = ctx->instr->arg1.uint;
855     variant_val_t val;
856     HRESULT hres;
857
858     TRACE("%u\n", arg);
859
860     hres = stack_pop_val(ctx, &val);
861     if(FAILED(hres))
862         return hres;
863
864     if(V_VT(val.v) != VT_BOOL) {
865         FIXME("unsupported for %s\n", debugstr_variant(val.v));
866         release_val(&val);
867         return E_NOTIMPL;
868     }
869
870     if(V_BOOL(val.v))
871         instr_jmp(ctx, ctx->instr->arg1.uint);
872     else
873         ctx->instr++;
874     return S_OK;
875 }
876
877 static HRESULT interp_ret(exec_ctx_t *ctx)
878 {
879     TRACE("\n");
880
881     ctx->instr = NULL;
882     return S_OK;
883 }
884
885 static HRESULT interp_stop(exec_ctx_t *ctx)
886 {
887     WARN("\n");
888
889     /* NOTE: this should have effect in debugging mode (that we don't support yet) */
890     return S_OK;
891 }
892
893 static HRESULT interp_me(exec_ctx_t *ctx)
894 {
895     VARIANT v;
896
897     TRACE("\n");
898
899     IDispatch_AddRef(ctx->this_obj);
900     V_VT(&v) = VT_DISPATCH;
901     V_DISPATCH(&v) = ctx->this_obj;
902     return stack_push(ctx, &v);
903 }
904
905 static HRESULT interp_bool(exec_ctx_t *ctx)
906 {
907     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
908     VARIANT v;
909
910     TRACE("%s\n", arg ? "true" : "false");
911
912     V_VT(&v) = VT_BOOL;
913     V_BOOL(&v) = arg;
914     return stack_push(ctx, &v);
915 }
916
917 static HRESULT interp_errmode(exec_ctx_t *ctx)
918 {
919     const int err_mode = ctx->instr->arg1.uint;
920
921     TRACE("%d\n", err_mode);
922
923     ctx->resume_next = err_mode;
924     return S_OK;
925 }
926
927 static HRESULT interp_string(exec_ctx_t *ctx)
928 {
929     VARIANT v;
930
931     TRACE("\n");
932
933     V_VT(&v) = VT_BSTR;
934     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
935     if(!V_BSTR(&v))
936         return E_OUTOFMEMORY;
937
938     return stack_push(ctx, &v);
939 }
940
941 static HRESULT interp_long(exec_ctx_t *ctx)
942 {
943     const LONG arg = ctx->instr->arg1.lng;
944     VARIANT v;
945
946     TRACE("%d\n", arg);
947
948     V_VT(&v) = VT_I4;
949     V_I4(&v) = arg;
950     return stack_push(ctx, &v);
951 }
952
953 static HRESULT interp_short(exec_ctx_t *ctx)
954 {
955     const LONG arg = ctx->instr->arg1.lng;
956     VARIANT v;
957
958     TRACE("%d\n", arg);
959
960     V_VT(&v) = VT_I2;
961     V_I2(&v) = arg;
962     return stack_push(ctx, &v);
963 }
964
965 static HRESULT interp_double(exec_ctx_t *ctx)
966 {
967     const DOUBLE *arg = ctx->instr->arg1.dbl;
968     VARIANT v;
969
970     TRACE("%lf\n", *arg);
971
972     V_VT(&v) = VT_R8;
973     V_R8(&v) = *arg;
974     return stack_push(ctx, &v);
975 }
976
977 static HRESULT interp_empty(exec_ctx_t *ctx)
978 {
979     VARIANT v;
980
981     TRACE("\n");
982
983     V_VT(&v) = VT_EMPTY;
984     return stack_push(ctx, &v);
985 }
986
987 static HRESULT interp_null(exec_ctx_t *ctx)
988 {
989     VARIANT v;
990
991     TRACE("\n");
992
993     V_VT(&v) = VT_NULL;
994     return stack_push(ctx, &v);
995 }
996
997 static HRESULT interp_nothing(exec_ctx_t *ctx)
998 {
999     VARIANT v;
1000
1001     TRACE("\n");
1002
1003     V_VT(&v) = VT_DISPATCH;
1004     V_DISPATCH(&v) = NULL;
1005     return stack_push(ctx, &v);
1006 }
1007
1008 static HRESULT interp_not(exec_ctx_t *ctx)
1009 {
1010     variant_val_t val;
1011     VARIANT v;
1012     HRESULT hres;
1013
1014     TRACE("\n");
1015
1016     hres = stack_pop_val(ctx, &val);
1017     if(FAILED(hres))
1018         return hres;
1019
1020     hres = VarNot(val.v, &v);
1021     release_val(&val);
1022     if(FAILED(hres))
1023         return hres;
1024
1025     return stack_push(ctx, &v);
1026 }
1027
1028 static HRESULT interp_and(exec_ctx_t *ctx)
1029 {
1030     variant_val_t r, l;
1031     VARIANT v;
1032     HRESULT hres;
1033
1034     TRACE("\n");
1035
1036     hres = stack_pop_val(ctx, &r);
1037     if(FAILED(hres))
1038         return hres;
1039
1040     hres = stack_pop_val(ctx, &l);
1041     if(SUCCEEDED(hres)) {
1042         hres = VarAnd(l.v, r.v, &v);
1043         release_val(&l);
1044     }
1045     release_val(&r);
1046     if(FAILED(hres))
1047         return hres;
1048
1049     return stack_push(ctx, &v);
1050 }
1051
1052 static HRESULT interp_or(exec_ctx_t *ctx)
1053 {
1054     variant_val_t r, l;
1055     VARIANT v;
1056     HRESULT hres;
1057
1058     TRACE("\n");
1059
1060     hres = stack_pop_val(ctx, &r);
1061     if(FAILED(hres))
1062         return hres;
1063
1064     hres = stack_pop_val(ctx, &l);
1065     if(SUCCEEDED(hres)) {
1066         hres = VarOr(l.v, r.v, &v);
1067         release_val(&l);
1068     }
1069     release_val(&r);
1070     if(FAILED(hres))
1071         return hres;
1072
1073     return stack_push(ctx, &v);
1074 }
1075
1076 static HRESULT interp_xor(exec_ctx_t *ctx)
1077 {
1078     variant_val_t r, l;
1079     VARIANT v;
1080     HRESULT hres;
1081
1082     TRACE("\n");
1083
1084     hres = stack_pop_val(ctx, &r);
1085     if(FAILED(hres))
1086         return hres;
1087
1088     hres = stack_pop_val(ctx, &l);
1089     if(SUCCEEDED(hres)) {
1090         hres = VarXor(l.v, r.v, &v);
1091         release_val(&l);
1092     }
1093     release_val(&r);
1094     if(FAILED(hres))
1095         return hres;
1096
1097     return stack_push(ctx, &v);
1098 }
1099
1100 static HRESULT interp_eqv(exec_ctx_t *ctx)
1101 {
1102     variant_val_t r, l;
1103     VARIANT v;
1104     HRESULT hres;
1105
1106     TRACE("\n");
1107
1108     hres = stack_pop_val(ctx, &r);
1109     if(FAILED(hres))
1110         return hres;
1111
1112     hres = stack_pop_val(ctx, &l);
1113     if(SUCCEEDED(hres)) {
1114         hres = VarEqv(l.v, r.v, &v);
1115         release_val(&l);
1116     }
1117     release_val(&r);
1118     if(FAILED(hres))
1119         return hres;
1120
1121     return stack_push(ctx, &v);
1122 }
1123
1124 static HRESULT interp_imp(exec_ctx_t *ctx)
1125 {
1126     variant_val_t r, l;
1127     VARIANT v;
1128     HRESULT hres;
1129
1130     TRACE("\n");
1131
1132     hres = stack_pop_val(ctx, &r);
1133     if(FAILED(hres))
1134         return hres;
1135
1136     hres = stack_pop_val(ctx, &l);
1137     if(SUCCEEDED(hres)) {
1138         hres = VarImp(l.v, r.v, &v);
1139         release_val(&l);
1140     }
1141     release_val(&r);
1142     if(FAILED(hres))
1143         return hres;
1144
1145     return stack_push(ctx, &v);
1146 }
1147
1148 static HRESULT cmp_oper(exec_ctx_t *ctx)
1149 {
1150     variant_val_t l, r;
1151     HRESULT hres;
1152
1153     hres = stack_pop_val(ctx, &r);
1154     if(FAILED(hres))
1155         return hres;
1156
1157     hres = stack_pop_val(ctx, &l);
1158     if(SUCCEEDED(hres)) {
1159         if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1160             FIXME("comparing nulls is not implemented\n");
1161             hres = E_NOTIMPL;
1162         }else {
1163             hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1164         }
1165     }
1166
1167     release_val(&r);
1168     release_val(&l);
1169     return hres;
1170 }
1171
1172 static HRESULT interp_equal(exec_ctx_t *ctx)
1173 {
1174     VARIANT v;
1175     HRESULT hres;
1176
1177     TRACE("\n");
1178
1179     hres = cmp_oper(ctx);
1180     if(FAILED(hres))
1181         return hres;
1182
1183     V_VT(&v) = VT_BOOL;
1184     V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1185     return stack_push(ctx, &v);
1186 }
1187
1188 static HRESULT interp_nequal(exec_ctx_t *ctx)
1189 {
1190     VARIANT v;
1191     HRESULT hres;
1192
1193     TRACE("\n");
1194
1195     hres = cmp_oper(ctx);
1196     if(FAILED(hres))
1197         return hres;
1198
1199     V_VT(&v) = VT_BOOL;
1200     V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1201     return stack_push(ctx, &v);
1202 }
1203
1204 static HRESULT interp_gt(exec_ctx_t *ctx)
1205 {
1206     VARIANT v;
1207     HRESULT hres;
1208
1209     TRACE("\n");
1210
1211     hres = cmp_oper(ctx);
1212     if(FAILED(hres))
1213         return hres;
1214
1215     V_VT(&v) = VT_BOOL;
1216     V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1217     return stack_push(ctx, &v);
1218 }
1219
1220 static HRESULT interp_gteq(exec_ctx_t *ctx)
1221 {
1222     VARIANT v;
1223     HRESULT hres;
1224
1225     TRACE("\n");
1226
1227     hres = cmp_oper(ctx);
1228     if(FAILED(hres))
1229         return hres;
1230
1231     V_VT(&v) = VT_BOOL;
1232     V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1233     return stack_push(ctx, &v);
1234 }
1235
1236 static HRESULT interp_lt(exec_ctx_t *ctx)
1237 {
1238     VARIANT v;
1239     HRESULT hres;
1240
1241     TRACE("\n");
1242
1243     hres = cmp_oper(ctx);
1244     if(FAILED(hres))
1245         return hres;
1246
1247     V_VT(&v) = VT_BOOL;
1248     V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1249     return stack_push(ctx, &v);
1250 }
1251
1252 static HRESULT interp_lteq(exec_ctx_t *ctx)
1253 {
1254     VARIANT v;
1255     HRESULT hres;
1256
1257     TRACE("\n");
1258
1259     hres = cmp_oper(ctx);
1260     if(FAILED(hres))
1261         return hres;
1262
1263     V_VT(&v) = VT_BOOL;
1264     V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1265     return stack_push(ctx, &v);
1266 }
1267
1268 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1269 {
1270     IObjectIdentity *identity;
1271     IUnknown *unk1, *unk2;
1272     HRESULT hres;
1273
1274     if(disp1 == disp2) {
1275         *ret = VARIANT_TRUE;
1276         return S_OK;
1277     }
1278
1279     if(!disp1 || !disp2) {
1280         *ret = VARIANT_FALSE;
1281         return S_OK;
1282     }
1283
1284     hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1285     if(FAILED(hres))
1286         return hres;
1287
1288     hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1289     if(FAILED(hres)) {
1290         IUnknown_Release(unk1);
1291         return hres;
1292     }
1293
1294     if(unk1 == unk2) {
1295         *ret = VARIANT_TRUE;
1296     }else {
1297         hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1298         if(SUCCEEDED(hres)) {
1299             hres = IObjectIdentity_IsEqualObject(identity, unk2);
1300             IObjectIdentity_Release(identity);
1301             *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1302         }else {
1303             *ret = VARIANT_FALSE;
1304         }
1305     }
1306
1307     IUnknown_Release(unk1);
1308     IUnknown_Release(unk2);
1309     return S_OK;
1310 }
1311
1312 static HRESULT interp_is(exec_ctx_t *ctx)
1313 {
1314     IDispatch *l, *r;
1315     VARIANT v;
1316     HRESULT hres;
1317
1318     TRACE("\n");
1319
1320     hres = stack_pop_disp(ctx, &r);
1321     if(FAILED(hres))
1322         return hres;
1323
1324     hres = stack_pop_disp(ctx, &l);
1325     if(SUCCEEDED(hres)) {
1326         V_VT(&v) = VT_BOOL;
1327         hres = disp_cmp(l, r, &V_BOOL(&v));
1328         if(l)
1329             IDispatch_Release(l);
1330     }
1331     if(r)
1332         IDispatch_Release(r);
1333     if(FAILED(hres))
1334         return hres;
1335
1336     return stack_push(ctx, &v);
1337 }
1338
1339 static HRESULT interp_concat(exec_ctx_t *ctx)
1340 {
1341     variant_val_t r, l;
1342     VARIANT v;
1343     HRESULT hres;
1344
1345     TRACE("\n");
1346
1347     hres = stack_pop_val(ctx, &r);
1348     if(FAILED(hres))
1349         return hres;
1350
1351     hres = stack_pop_val(ctx, &l);
1352     if(SUCCEEDED(hres)) {
1353         hres = VarCat(l.v, r.v, &v);
1354         release_val(&l);
1355     }
1356     release_val(&r);
1357     if(FAILED(hres))
1358         return hres;
1359
1360     return stack_push(ctx, &v);
1361 }
1362
1363 static HRESULT interp_add(exec_ctx_t *ctx)
1364 {
1365     variant_val_t r, l;
1366     VARIANT v;
1367     HRESULT hres;
1368
1369     TRACE("\n");
1370
1371     hres = stack_pop_val(ctx, &r);
1372     if(FAILED(hres))
1373         return hres;
1374
1375     hres = stack_pop_val(ctx, &l);
1376     if(SUCCEEDED(hres)) {
1377         hres = VarAdd(l.v, r.v, &v);
1378         release_val(&l);
1379     }
1380     release_val(&r);
1381     if(FAILED(hres))
1382         return hres;
1383
1384     return stack_push(ctx, &v);
1385 }
1386
1387 static HRESULT interp_sub(exec_ctx_t *ctx)
1388 {
1389     variant_val_t r, l;
1390     VARIANT v;
1391     HRESULT hres;
1392
1393     TRACE("\n");
1394
1395     hres = stack_pop_val(ctx, &r);
1396     if(FAILED(hres))
1397         return hres;
1398
1399     hres = stack_pop_val(ctx, &l);
1400     if(SUCCEEDED(hres)) {
1401         hres = VarSub(l.v, r.v, &v);
1402         release_val(&l);
1403     }
1404     release_val(&r);
1405     if(FAILED(hres))
1406         return hres;
1407
1408     return stack_push(ctx, &v);
1409 }
1410
1411 static HRESULT interp_mod(exec_ctx_t *ctx)
1412 {
1413     variant_val_t r, l;
1414     VARIANT v;
1415     HRESULT hres;
1416
1417     TRACE("\n");
1418
1419     hres = stack_pop_val(ctx, &r);
1420     if(FAILED(hres))
1421         return hres;
1422
1423     hres = stack_pop_val(ctx, &l);
1424     if(SUCCEEDED(hres)) {
1425         hres = VarMod(l.v, r.v, &v);
1426         release_val(&l);
1427     }
1428     release_val(&r);
1429     if(FAILED(hres))
1430         return hres;
1431
1432     return stack_push(ctx, &v);
1433 }
1434
1435 static HRESULT interp_idiv(exec_ctx_t *ctx)
1436 {
1437     variant_val_t r, l;
1438     VARIANT v;
1439     HRESULT hres;
1440
1441     TRACE("\n");
1442
1443     hres = stack_pop_val(ctx, &r);
1444     if(FAILED(hres))
1445         return hres;
1446
1447     hres = stack_pop_val(ctx, &l);
1448     if(SUCCEEDED(hres)) {
1449         hres = VarIdiv(l.v, r.v, &v);
1450         release_val(&l);
1451     }
1452     release_val(&r);
1453     if(FAILED(hres))
1454         return hres;
1455
1456     return stack_push(ctx, &v);
1457 }
1458
1459 static HRESULT interp_div(exec_ctx_t *ctx)
1460 {
1461     variant_val_t r, l;
1462     VARIANT v;
1463     HRESULT hres;
1464
1465     TRACE("\n");
1466
1467     hres = stack_pop_val(ctx, &r);
1468     if(FAILED(hres))
1469         return hres;
1470
1471     hres = stack_pop_val(ctx, &l);
1472     if(SUCCEEDED(hres)) {
1473         hres = VarDiv(l.v, r.v, &v);
1474         release_val(&l);
1475     }
1476     release_val(&r);
1477     if(FAILED(hres))
1478         return hres;
1479
1480     return stack_push(ctx, &v);
1481 }
1482
1483 static HRESULT interp_mul(exec_ctx_t *ctx)
1484 {
1485     variant_val_t r, l;
1486     VARIANT v;
1487     HRESULT hres;
1488
1489     TRACE("\n");
1490
1491     hres = stack_pop_val(ctx, &r);
1492     if(FAILED(hres))
1493         return hres;
1494
1495     hres = stack_pop_val(ctx, &l);
1496     if(SUCCEEDED(hres)) {
1497         hres = VarMul(l.v, r.v, &v);
1498         release_val(&l);
1499     }
1500     release_val(&r);
1501     if(FAILED(hres))
1502         return hres;
1503
1504     return stack_push(ctx, &v);
1505 }
1506
1507 static HRESULT interp_exp(exec_ctx_t *ctx)
1508 {
1509     variant_val_t r, l;
1510     VARIANT v;
1511     HRESULT hres;
1512
1513     TRACE("\n");
1514
1515     hres = stack_pop_val(ctx, &r);
1516     if(FAILED(hres))
1517         return hres;
1518
1519     hres = stack_pop_val(ctx, &l);
1520     if(SUCCEEDED(hres)) {
1521         hres = VarPow(l.v, r.v, &v);
1522         release_val(&l);
1523     }
1524     release_val(&r);
1525     if(FAILED(hres))
1526         return hres;
1527
1528     return stack_push(ctx, &v);
1529 }
1530
1531 static HRESULT interp_neg(exec_ctx_t *ctx)
1532 {
1533     variant_val_t val;
1534     VARIANT v;
1535     HRESULT hres;
1536
1537     hres = stack_pop_val(ctx, &val);
1538     if(FAILED(hres))
1539         return hres;
1540
1541     hres = VarNeg(val.v, &v);
1542     release_val(&val);
1543     if(FAILED(hres))
1544         return hres;
1545
1546     return stack_push(ctx, &v);
1547 }
1548
1549 static HRESULT interp_incc(exec_ctx_t *ctx)
1550 {
1551     const BSTR ident = ctx->instr->arg1.bstr;
1552     VARIANT v;
1553     ref_t ref;
1554     HRESULT hres;
1555
1556     TRACE("\n");
1557
1558     hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1559     if(FAILED(hres))
1560         return hres;
1561
1562     if(ref.type != REF_VAR) {
1563         FIXME("ref.type is not REF_VAR\n");
1564         return E_FAIL;
1565     }
1566
1567     hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1568     if(FAILED(hres))
1569         return hres;
1570
1571     VariantClear(ref.u.v);
1572     *ref.u.v = v;
1573     return S_OK;
1574 }
1575
1576 static const instr_func_t op_funcs[] = {
1577 #define X(x,n,a,b) interp_ ## x,
1578 OP_LIST
1579 #undef X
1580 };
1581
1582 static const unsigned op_move[] = {
1583 #define X(x,n,a,b) n,
1584 OP_LIST
1585 #undef X
1586 };
1587
1588 void release_dynamic_vars(dynamic_var_t *var)
1589 {
1590     while(var) {
1591         VariantClear(&var->v);
1592         var = var->next;
1593     }
1594 }
1595
1596 static void release_exec(exec_ctx_t *ctx)
1597 {
1598     unsigned i;
1599
1600     VariantClear(&ctx->ret_val);
1601     release_dynamic_vars(ctx->dynamic_vars);
1602
1603     if(ctx->this_obj)
1604         IDispatch_Release(ctx->this_obj);
1605
1606     if(ctx->args) {
1607         for(i=0; i < ctx->func->arg_cnt; i++)
1608             VariantClear(ctx->args+i);
1609     }
1610
1611     if(ctx->vars) {
1612         for(i=0; i < ctx->func->var_cnt; i++)
1613             VariantClear(ctx->vars+i);
1614     }
1615
1616     vbsheap_free(&ctx->heap);
1617     heap_free(ctx->args);
1618     heap_free(ctx->vars);
1619     heap_free(ctx->stack);
1620 }
1621
1622 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1623 {
1624     exec_ctx_t exec = {func->code_ctx};
1625     vbsop_t op;
1626     HRESULT hres = S_OK;
1627
1628     exec.code = func->code_ctx;
1629
1630     if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1631         FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1632         return E_FAIL;
1633     }
1634
1635     vbsheap_init(&exec.heap);
1636
1637     if(func->arg_cnt) {
1638         VARIANT *v;
1639         unsigned i;
1640
1641         exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1642         if(!exec.args) {
1643             release_exec(&exec);
1644             return E_OUTOFMEMORY;
1645         }
1646
1647         for(i=0; i < func->arg_cnt; i++) {
1648             v = get_arg(dp, i);
1649             if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1650                 if(func->args[i].by_ref)
1651                     exec.args[i] = *v;
1652                 else
1653                     hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1654             }else {
1655                 hres = VariantCopy(exec.args+i, v);
1656             }
1657             if(FAILED(hres)) {
1658                 release_exec(&exec);
1659                 return hres;
1660             }
1661         }
1662     }else {
1663         exec.args = NULL;
1664     }
1665
1666     if(func->var_cnt) {
1667         exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1668         if(!exec.vars) {
1669             release_exec(&exec);
1670             return E_OUTOFMEMORY;
1671         }
1672     }else {
1673         exec.vars = NULL;
1674     }
1675
1676     exec.stack_size = 16;
1677     exec.top = 0;
1678     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1679     if(!exec.stack) {
1680         release_exec(&exec);
1681         return E_OUTOFMEMORY;
1682     }
1683
1684     if(this_obj)
1685         exec.this_obj = this_obj;
1686     else if (ctx->host_global)
1687         exec.this_obj = ctx->host_global;
1688     else
1689         exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1690     IDispatch_AddRef(exec.this_obj);
1691
1692     exec.instr = exec.code->instrs + func->code_off;
1693     exec.script = ctx;
1694     exec.func = func;
1695
1696     while(exec.instr) {
1697         op = exec.instr->op;
1698         hres = op_funcs[op](&exec);
1699         if(FAILED(hres)) {
1700             if(exec.resume_next)
1701                 FIXME("Failed %08x in resume next mode\n", hres);
1702             else
1703                 WARN("Failed %08x\n", hres);
1704             stack_popn(&exec, exec.top);
1705             break;
1706         }
1707
1708         exec.instr += op_move[op];
1709     }
1710
1711     assert(!exec.top);
1712     if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1713         assert(V_VT(&exec.ret_val) == VT_EMPTY);
1714
1715     if(SUCCEEDED(hres) && res) {
1716         *res = exec.ret_val;
1717         V_VT(&exec.ret_val) = VT_EMPTY;
1718     }
1719
1720     release_exec(&exec);
1721     return hres;
1722 }