gdi32: Return a new path from PATH_FlattenPath instead of replacing the DC path.
[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     variant_val_t v;
566     HRESULT hres;
567
568     TRACE("%s\n", debugstr_w(arg));
569
570     hres = stack_pop_val(ctx, &v);
571     if(FAILED(hres))
572         return hres;
573
574     return assign_ident(ctx, arg, v.v, v.owned);
575 }
576
577 static HRESULT interp_set_ident(exec_ctx_t *ctx)
578 {
579     const BSTR arg = ctx->instr->arg1.bstr;
580     IDispatch *disp;
581     VARIANT v;
582     HRESULT hres;
583
584     TRACE("%s\n", debugstr_w(arg));
585
586     hres = stack_pop_disp(ctx, &disp);
587     if(FAILED(hres))
588         return hres;
589
590     V_VT(&v) = VT_DISPATCH;
591     V_DISPATCH(&v) = disp;
592     return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
593 }
594
595 static HRESULT interp_assign_member(exec_ctx_t *ctx)
596 {
597     BSTR identifier = ctx->instr->arg1.bstr;
598     variant_val_t val;
599     IDispatch *obj;
600     DISPID id;
601     HRESULT hres;
602
603     TRACE("%s\n", debugstr_w(identifier));
604
605     hres = stack_pop_disp(ctx, &obj);
606     if(FAILED(hres))
607         return hres;
608
609     if(!obj) {
610         FIXME("NULL obj\n");
611         return E_FAIL;
612     }
613
614     hres = stack_pop_val(ctx, &val);
615     if(FAILED(hres)) {
616         IDispatch_Release(obj);
617         return hres;
618     }
619
620     hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
621     if(SUCCEEDED(hres))
622         hres = disp_propput(ctx->script, obj, id, val.v);
623
624     release_val(&val);
625     IDispatch_Release(obj);
626     return hres;
627 }
628
629 static HRESULT interp_set_member(exec_ctx_t *ctx)
630 {
631     BSTR identifier = ctx->instr->arg1.bstr;
632     IDispatch *obj, *val;
633     DISPID id;
634     HRESULT hres;
635
636     TRACE("%s\n", debugstr_w(identifier));
637
638     hres = stack_pop_disp(ctx, &obj);
639     if(FAILED(hres))
640         return hres;
641
642     if(!obj) {
643         FIXME("NULL obj\n");
644         return E_FAIL;
645     }
646
647     hres = stack_pop_disp(ctx, &val);
648     if(FAILED(hres)) {
649         IDispatch_Release(obj);
650         return hres;
651     }
652
653     hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
654     if(SUCCEEDED(hres)) {
655         VARIANT v;
656
657         V_VT(&v) = VT_DISPATCH;
658         V_DISPATCH(&v) = val;
659         hres = disp_propput(ctx->script, obj, id, &v);
660     }
661
662     if(val)
663         IDispatch_Release(val);
664     IDispatch_Release(obj);
665     return hres;
666 }
667
668 static HRESULT interp_const(exec_ctx_t *ctx)
669 {
670     BSTR arg = ctx->instr->arg1.bstr;
671     variant_val_t val;
672     ref_t ref;
673     HRESULT hres;
674
675     TRACE("%s\n", debugstr_w(arg));
676
677     assert(ctx->func->type == FUNC_GLOBAL);
678
679     hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
680     if(FAILED(hres))
681         return hres;
682
683     if(ref.type != REF_NONE) {
684         FIXME("%s already defined\n", debugstr_w(arg));
685         return E_FAIL;
686     }
687
688     hres = stack_pop_val(ctx, &val);
689     if(FAILED(hres))
690         return hres;
691
692     return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
693 }
694
695 static HRESULT interp_val(exec_ctx_t *ctx)
696 {
697     variant_val_t val;
698     VARIANT v;
699     HRESULT hres;
700
701     TRACE("\n");
702
703     hres = stack_pop_val(ctx, &val);
704     if(FAILED(hres))
705         return hres;
706
707     if(!val.owned) {
708         V_VT(&v) = VT_EMPTY;
709         hres = VariantCopy(&v, val.v);
710         if(FAILED(hres))
711             return hres;
712     }
713
714     return stack_push(ctx, val.owned ? val.v : &v);
715 }
716
717 static HRESULT interp_pop(exec_ctx_t *ctx)
718 {
719     const unsigned n = ctx->instr->arg1.uint;
720
721     TRACE("%u\n", n);
722
723     stack_popn(ctx, n);
724     return S_OK;
725 }
726
727 static HRESULT interp_new(exec_ctx_t *ctx)
728 {
729     const WCHAR *arg = ctx->instr->arg1.bstr;
730     class_desc_t *class_desc;
731     vbdisp_t *obj;
732     VARIANT v;
733     HRESULT hres;
734
735     TRACE("%s\n", debugstr_w(arg));
736
737     for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
738         if(!strcmpiW(class_desc->name, arg))
739             break;
740     }
741     if(!class_desc) {
742         FIXME("Class %s not found\n", debugstr_w(arg));
743         return E_FAIL;
744     }
745
746     hres = create_vbdisp(class_desc, &obj);
747     if(FAILED(hres))
748         return hres;
749
750     V_VT(&v) = VT_DISPATCH;
751     V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
752     return stack_push(ctx, &v);
753 }
754
755 static HRESULT interp_step(exec_ctx_t *ctx)
756 {
757     const BSTR ident = ctx->instr->arg2.bstr;
758     BOOL gteq_zero;
759     VARIANT zero;
760     ref_t ref;
761     HRESULT hres;
762
763     TRACE("%s\n", debugstr_w(ident));
764
765     V_VT(&zero) = VT_I2;
766     V_I2(&zero) = 0;
767     hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
768     if(FAILED(hres))
769         return hres;
770
771     gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
772
773     hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
774     if(FAILED(hres))
775         return hres;
776
777     if(ref.type != REF_VAR) {
778         FIXME("%s is not REF_VAR\n", debugstr_w(ident));
779         return E_FAIL;
780     }
781
782     hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
783     if(FAILED(hres))
784         return hres;
785
786     if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
787         ctx->instr++;
788     else
789         instr_jmp(ctx, ctx->instr->arg1.uint);
790     return S_OK;
791 }
792
793 static HRESULT interp_jmp(exec_ctx_t *ctx)
794 {
795     const unsigned arg = ctx->instr->arg1.uint;
796
797     TRACE("%u\n", arg);
798
799     instr_jmp(ctx, arg);
800     return S_OK;
801 }
802
803 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
804 {
805     const unsigned arg = ctx->instr->arg1.uint;
806     variant_val_t val;
807     HRESULT hres;
808
809     TRACE("%u\n", arg);
810
811     hres = stack_pop_val(ctx, &val);
812     if(FAILED(hres))
813         return hres;
814
815     if(V_VT(val.v) != VT_BOOL) {
816         FIXME("unsupported for %s\n", debugstr_variant(val.v));
817         release_val(&val);
818         return E_NOTIMPL;
819     }
820
821     if(V_BOOL(val.v))
822         ctx->instr++;
823     else
824         instr_jmp(ctx, ctx->instr->arg1.uint);
825     return S_OK;
826 }
827
828 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
829 {
830     const unsigned arg = ctx->instr->arg1.uint;
831     variant_val_t val;
832     HRESULT hres;
833
834     TRACE("%u\n", arg);
835
836     hres = stack_pop_val(ctx, &val);
837     if(FAILED(hres))
838         return hres;
839
840     if(V_VT(val.v) != VT_BOOL) {
841         FIXME("unsupported for %s\n", debugstr_variant(val.v));
842         release_val(&val);
843         return E_NOTIMPL;
844     }
845
846     if(V_BOOL(val.v))
847         instr_jmp(ctx, ctx->instr->arg1.uint);
848     else
849         ctx->instr++;
850     return S_OK;
851 }
852
853 static HRESULT interp_ret(exec_ctx_t *ctx)
854 {
855     TRACE("\n");
856
857     ctx->instr = NULL;
858     return S_OK;
859 }
860
861 static HRESULT interp_stop(exec_ctx_t *ctx)
862 {
863     WARN("\n");
864
865     /* NOTE: this should have effect in debugging mode (that we don't support yet) */
866     return S_OK;
867 }
868
869 static HRESULT interp_me(exec_ctx_t *ctx)
870 {
871     VARIANT v;
872
873     TRACE("\n");
874
875     IDispatch_AddRef(ctx->this_obj);
876     V_VT(&v) = VT_DISPATCH;
877     V_DISPATCH(&v) = ctx->this_obj;
878     return stack_push(ctx, &v);
879 }
880
881 static HRESULT interp_bool(exec_ctx_t *ctx)
882 {
883     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
884     VARIANT v;
885
886     TRACE("%s\n", arg ? "true" : "false");
887
888     V_VT(&v) = VT_BOOL;
889     V_BOOL(&v) = arg;
890     return stack_push(ctx, &v);
891 }
892
893 static HRESULT interp_errmode(exec_ctx_t *ctx)
894 {
895     const int err_mode = ctx->instr->arg1.uint;
896
897     TRACE("%d\n", err_mode);
898
899     ctx->resume_next = err_mode;
900     return S_OK;
901 }
902
903 static HRESULT interp_string(exec_ctx_t *ctx)
904 {
905     VARIANT v;
906
907     TRACE("\n");
908
909     V_VT(&v) = VT_BSTR;
910     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
911     if(!V_BSTR(&v))
912         return E_OUTOFMEMORY;
913
914     return stack_push(ctx, &v);
915 }
916
917 static HRESULT interp_long(exec_ctx_t *ctx)
918 {
919     const LONG arg = ctx->instr->arg1.lng;
920     VARIANT v;
921
922     TRACE("%d\n", arg);
923
924     V_VT(&v) = VT_I4;
925     V_I4(&v) = arg;
926     return stack_push(ctx, &v);
927 }
928
929 static HRESULT interp_short(exec_ctx_t *ctx)
930 {
931     const LONG arg = ctx->instr->arg1.lng;
932     VARIANT v;
933
934     TRACE("%d\n", arg);
935
936     V_VT(&v) = VT_I2;
937     V_I2(&v) = arg;
938     return stack_push(ctx, &v);
939 }
940
941 static HRESULT interp_double(exec_ctx_t *ctx)
942 {
943     const DOUBLE *arg = ctx->instr->arg1.dbl;
944     VARIANT v;
945
946     TRACE("%lf\n", *arg);
947
948     V_VT(&v) = VT_R8;
949     V_R8(&v) = *arg;
950     return stack_push(ctx, &v);
951 }
952
953 static HRESULT interp_empty(exec_ctx_t *ctx)
954 {
955     VARIANT v;
956
957     TRACE("\n");
958
959     V_VT(&v) = VT_EMPTY;
960     return stack_push(ctx, &v);
961 }
962
963 static HRESULT interp_null(exec_ctx_t *ctx)
964 {
965     VARIANT v;
966
967     TRACE("\n");
968
969     V_VT(&v) = VT_NULL;
970     return stack_push(ctx, &v);
971 }
972
973 static HRESULT interp_nothing(exec_ctx_t *ctx)
974 {
975     VARIANT v;
976
977     TRACE("\n");
978
979     V_VT(&v) = VT_DISPATCH;
980     V_DISPATCH(&v) = NULL;
981     return stack_push(ctx, &v);
982 }
983
984 static HRESULT interp_not(exec_ctx_t *ctx)
985 {
986     variant_val_t val;
987     VARIANT v;
988     HRESULT hres;
989
990     TRACE("\n");
991
992     hres = stack_pop_val(ctx, &val);
993     if(FAILED(hres))
994         return hres;
995
996     hres = VarNot(val.v, &v);
997     release_val(&val);
998     if(FAILED(hres))
999         return hres;
1000
1001     return stack_push(ctx, &v);
1002 }
1003
1004 static HRESULT interp_and(exec_ctx_t *ctx)
1005 {
1006     variant_val_t r, l;
1007     VARIANT v;
1008     HRESULT hres;
1009
1010     TRACE("\n");
1011
1012     hres = stack_pop_val(ctx, &r);
1013     if(FAILED(hres))
1014         return hres;
1015
1016     hres = stack_pop_val(ctx, &l);
1017     if(SUCCEEDED(hres)) {
1018         hres = VarAnd(l.v, r.v, &v);
1019         release_val(&l);
1020     }
1021     release_val(&r);
1022     if(FAILED(hres))
1023         return hres;
1024
1025     return stack_push(ctx, &v);
1026 }
1027
1028 static HRESULT interp_or(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 = VarOr(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_xor(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 = VarXor(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_eqv(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 = VarEqv(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_imp(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 = VarImp(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 cmp_oper(exec_ctx_t *ctx)
1125 {
1126     variant_val_t l, r;
1127     HRESULT hres;
1128
1129     hres = stack_pop_val(ctx, &r);
1130     if(FAILED(hres))
1131         return hres;
1132
1133     hres = stack_pop_val(ctx, &l);
1134     if(SUCCEEDED(hres)) {
1135         if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1136             FIXME("comparing nulls is not implemented\n");
1137             hres = E_NOTIMPL;
1138         }else {
1139             hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1140         }
1141     }
1142
1143     release_val(&r);
1144     release_val(&l);
1145     return hres;
1146 }
1147
1148 static HRESULT interp_equal(exec_ctx_t *ctx)
1149 {
1150     VARIANT v;
1151     HRESULT hres;
1152
1153     TRACE("\n");
1154
1155     hres = cmp_oper(ctx);
1156     if(FAILED(hres))
1157         return hres;
1158
1159     V_VT(&v) = VT_BOOL;
1160     V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1161     return stack_push(ctx, &v);
1162 }
1163
1164 static HRESULT interp_nequal(exec_ctx_t *ctx)
1165 {
1166     VARIANT v;
1167     HRESULT hres;
1168
1169     TRACE("\n");
1170
1171     hres = cmp_oper(ctx);
1172     if(FAILED(hres))
1173         return hres;
1174
1175     V_VT(&v) = VT_BOOL;
1176     V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1177     return stack_push(ctx, &v);
1178 }
1179
1180 static HRESULT interp_gt(exec_ctx_t *ctx)
1181 {
1182     VARIANT v;
1183     HRESULT hres;
1184
1185     TRACE("\n");
1186
1187     hres = cmp_oper(ctx);
1188     if(FAILED(hres))
1189         return hres;
1190
1191     V_VT(&v) = VT_BOOL;
1192     V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1193     return stack_push(ctx, &v);
1194 }
1195
1196 static HRESULT interp_gteq(exec_ctx_t *ctx)
1197 {
1198     VARIANT v;
1199     HRESULT hres;
1200
1201     TRACE("\n");
1202
1203     hres = cmp_oper(ctx);
1204     if(FAILED(hres))
1205         return hres;
1206
1207     V_VT(&v) = VT_BOOL;
1208     V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1209     return stack_push(ctx, &v);
1210 }
1211
1212 static HRESULT interp_lt(exec_ctx_t *ctx)
1213 {
1214     VARIANT v;
1215     HRESULT hres;
1216
1217     TRACE("\n");
1218
1219     hres = cmp_oper(ctx);
1220     if(FAILED(hres))
1221         return hres;
1222
1223     V_VT(&v) = VT_BOOL;
1224     V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1225     return stack_push(ctx, &v);
1226 }
1227
1228 static HRESULT interp_lteq(exec_ctx_t *ctx)
1229 {
1230     VARIANT v;
1231     HRESULT hres;
1232
1233     TRACE("\n");
1234
1235     hres = cmp_oper(ctx);
1236     if(FAILED(hres))
1237         return hres;
1238
1239     V_VT(&v) = VT_BOOL;
1240     V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1241     return stack_push(ctx, &v);
1242 }
1243
1244 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1245 {
1246     IObjectIdentity *identity;
1247     IUnknown *unk1, *unk2;
1248     HRESULT hres;
1249
1250     if(disp1 == disp2) {
1251         *ret = VARIANT_TRUE;
1252         return S_OK;
1253     }
1254
1255     if(!disp1 || !disp2) {
1256         *ret = VARIANT_FALSE;
1257         return S_OK;
1258     }
1259
1260     hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1261     if(FAILED(hres))
1262         return hres;
1263
1264     hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1265     if(FAILED(hres)) {
1266         IUnknown_Release(unk1);
1267         return hres;
1268     }
1269
1270     if(unk1 == unk2) {
1271         *ret = VARIANT_TRUE;
1272     }else {
1273         hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1274         if(SUCCEEDED(hres)) {
1275             hres = IObjectIdentity_IsEqualObject(identity, unk2);
1276             IObjectIdentity_Release(identity);
1277             *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1278         }else {
1279             *ret = VARIANT_FALSE;
1280         }
1281     }
1282
1283     IUnknown_Release(unk1);
1284     IUnknown_Release(unk2);
1285     return S_OK;
1286 }
1287
1288 static HRESULT interp_is(exec_ctx_t *ctx)
1289 {
1290     IDispatch *l, *r;
1291     VARIANT v;
1292     HRESULT hres;
1293
1294     TRACE("\n");
1295
1296     hres = stack_pop_disp(ctx, &r);
1297     if(FAILED(hres))
1298         return hres;
1299
1300     hres = stack_pop_disp(ctx, &l);
1301     if(SUCCEEDED(hres)) {
1302         V_VT(&v) = VT_BOOL;
1303         hres = disp_cmp(l, r, &V_BOOL(&v));
1304         if(l)
1305             IDispatch_Release(l);
1306     }
1307     if(r)
1308         IDispatch_Release(r);
1309     if(FAILED(hres))
1310         return hres;
1311
1312     return stack_push(ctx, &v);
1313 }
1314
1315 static HRESULT interp_concat(exec_ctx_t *ctx)
1316 {
1317     variant_val_t r, l;
1318     VARIANT v;
1319     HRESULT hres;
1320
1321     TRACE("\n");
1322
1323     hres = stack_pop_val(ctx, &r);
1324     if(FAILED(hres))
1325         return hres;
1326
1327     hres = stack_pop_val(ctx, &l);
1328     if(SUCCEEDED(hres)) {
1329         hres = VarCat(l.v, r.v, &v);
1330         release_val(&l);
1331     }
1332     release_val(&r);
1333     if(FAILED(hres))
1334         return hres;
1335
1336     return stack_push(ctx, &v);
1337 }
1338
1339 static HRESULT interp_add(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 = VarAdd(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_sub(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 = VarSub(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_mod(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 = VarMod(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_idiv(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 = VarIdiv(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_div(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 = VarDiv(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_mul(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 = VarMul(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_exp(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 = VarPow(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_neg(exec_ctx_t *ctx)
1508 {
1509     variant_val_t val;
1510     VARIANT v;
1511     HRESULT hres;
1512
1513     hres = stack_pop_val(ctx, &val);
1514     if(FAILED(hres))
1515         return hres;
1516
1517     hres = VarNeg(val.v, &v);
1518     release_val(&val);
1519     if(FAILED(hres))
1520         return hres;
1521
1522     return stack_push(ctx, &v);
1523 }
1524
1525 static HRESULT interp_incc(exec_ctx_t *ctx)
1526 {
1527     const BSTR ident = ctx->instr->arg1.bstr;
1528     VARIANT v;
1529     ref_t ref;
1530     HRESULT hres;
1531
1532     TRACE("\n");
1533
1534     hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1535     if(FAILED(hres))
1536         return hres;
1537
1538     if(ref.type != REF_VAR) {
1539         FIXME("ref.type is not REF_VAR\n");
1540         return E_FAIL;
1541     }
1542
1543     hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1544     if(FAILED(hres))
1545         return hres;
1546
1547     VariantClear(ref.u.v);
1548     *ref.u.v = v;
1549     return S_OK;
1550 }
1551
1552 static const instr_func_t op_funcs[] = {
1553 #define X(x,n,a,b) interp_ ## x,
1554 OP_LIST
1555 #undef X
1556 };
1557
1558 static const unsigned op_move[] = {
1559 #define X(x,n,a,b) n,
1560 OP_LIST
1561 #undef X
1562 };
1563
1564 void release_dynamic_vars(dynamic_var_t *var)
1565 {
1566     while(var) {
1567         VariantClear(&var->v);
1568         var = var->next;
1569     }
1570 }
1571
1572 static void release_exec(exec_ctx_t *ctx)
1573 {
1574     unsigned i;
1575
1576     VariantClear(&ctx->ret_val);
1577     release_dynamic_vars(ctx->dynamic_vars);
1578
1579     if(ctx->this_obj)
1580         IDispatch_Release(ctx->this_obj);
1581
1582     if(ctx->args) {
1583         for(i=0; i < ctx->func->arg_cnt; i++)
1584             VariantClear(ctx->args+i);
1585     }
1586
1587     if(ctx->vars) {
1588         for(i=0; i < ctx->func->var_cnt; i++)
1589             VariantClear(ctx->vars+i);
1590     }
1591
1592     vbsheap_free(&ctx->heap);
1593     heap_free(ctx->args);
1594     heap_free(ctx->vars);
1595     heap_free(ctx->stack);
1596 }
1597
1598 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1599 {
1600     exec_ctx_t exec = {func->code_ctx};
1601     vbsop_t op;
1602     HRESULT hres = S_OK;
1603
1604     exec.code = func->code_ctx;
1605
1606     if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1607         FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1608         return E_FAIL;
1609     }
1610
1611     vbsheap_init(&exec.heap);
1612
1613     if(func->arg_cnt) {
1614         VARIANT *v;
1615         unsigned i;
1616
1617         exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1618         if(!exec.args) {
1619             release_exec(&exec);
1620             return E_OUTOFMEMORY;
1621         }
1622
1623         for(i=0; i < func->arg_cnt; i++) {
1624             v = get_arg(dp, i);
1625             if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1626                 if(func->args[i].by_ref)
1627                     exec.args[i] = *v;
1628                 else
1629                     hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1630             }else {
1631                 hres = VariantCopy(exec.args+i, v);
1632             }
1633             if(FAILED(hres)) {
1634                 release_exec(&exec);
1635                 return hres;
1636             }
1637         }
1638     }else {
1639         exec.args = NULL;
1640     }
1641
1642     if(func->var_cnt) {
1643         exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1644         if(!exec.vars) {
1645             release_exec(&exec);
1646             return E_OUTOFMEMORY;
1647         }
1648     }else {
1649         exec.vars = NULL;
1650     }
1651
1652     exec.stack_size = 16;
1653     exec.top = 0;
1654     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1655     if(!exec.stack) {
1656         release_exec(&exec);
1657         return E_OUTOFMEMORY;
1658     }
1659
1660     if(this_obj)
1661         exec.this_obj = this_obj;
1662     else if (ctx->host_global)
1663         exec.this_obj = ctx->host_global;
1664     else
1665         exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1666     IDispatch_AddRef(exec.this_obj);
1667
1668     exec.instr = exec.code->instrs + func->code_off;
1669     exec.script = ctx;
1670     exec.func = func;
1671
1672     while(exec.instr) {
1673         op = exec.instr->op;
1674         hres = op_funcs[op](&exec);
1675         if(FAILED(hres)) {
1676             if(exec.resume_next)
1677                 FIXME("Failed %08x in resume next mode\n", hres);
1678             else
1679                 WARN("Failed %08x\n", hres);
1680             stack_popn(&exec, exec.top);
1681             break;
1682         }
1683
1684         exec.instr += op_move[op];
1685     }
1686
1687     assert(!exec.top);
1688     if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1689         assert(V_VT(&exec.ret_val) == VT_EMPTY);
1690
1691     if(SUCCEEDED(hres) && res) {
1692         *res = exec.ret_val;
1693         V_VT(&exec.ret_val) = VT_EMPTY;
1694     }
1695
1696     release_exec(&exec);
1697     return hres;
1698 }