vbscript: Added set statement parser/compiler implementation.
[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
28 typedef struct {
29     vbscode_t *code;
30     instr_t *instr;
31     script_ctx_t *script;
32     function_t *func;
33
34     VARIANT *args;
35     VARIANT *vars;
36
37     unsigned stack_size;
38     unsigned top;
39     VARIANT *stack;
40
41     VARIANT ret_val;
42 } exec_ctx_t;
43
44 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
45
46 typedef enum {
47     REF_NONE,
48     REF_DISP,
49     REF_VAR,
50     REF_FUNC
51 } ref_type_t;
52
53 typedef struct {
54     ref_type_t type;
55     union {
56         struct {
57             IDispatch *disp;
58             DISPID id;
59         } d;
60         VARIANT *v;
61         function_t *f;
62     } u;
63 } ref_t;
64
65 typedef struct {
66     VARIANT *v;
67     VARIANT store;
68     BOOL owned;
69 } variant_val_t;
70
71 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
72 {
73     while(var) {
74         if(!strcmpiW(var->name, name)) {
75             ref->type = REF_VAR;
76             ref->u.v = &var->v;
77             return TRUE;
78         }
79
80         var = var->next;
81     }
82
83     return FALSE;
84 }
85
86 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
87 {
88     named_item_t *item;
89     function_t *func;
90     unsigned i;
91     DISPID id;
92     HRESULT hres;
93
94     if(invoke_type == VBDISP_LET && ctx->func->type == FUNC_FUNCTION && !strcmpiW(name, ctx->func->name)) {
95         ref->type = REF_VAR;
96         ref->u.v = &ctx->ret_val;
97         return S_OK;
98     }
99
100     for(i=0; i < ctx->func->var_cnt; i++) {
101         if(!strcmpiW(ctx->func->vars[i].name, name)) {
102             ref->type = REF_VAR;
103             ref->u.v = ctx->vars+i;
104             return TRUE;
105         }
106     }
107
108     for(i=0; i < ctx->func->arg_cnt; i++) {
109         if(!strcmpiW(ctx->func->args[i].name, name)) {
110             ref->type = REF_VAR;
111             ref->u.v = ctx->args+i;
112             return S_OK;
113         }
114     }
115
116     if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
117         return S_OK;
118
119     for(func = ctx->script->global_funcs; func; func = func->next) {
120         if(!strcmpiW(func->name, name)) {
121             ref->type = REF_FUNC;
122             ref->u.f = func;
123             return S_OK;
124         }
125     }
126
127     LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
128         if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
129             hres = disp_get_id(item->disp, name, &id);
130             if(SUCCEEDED(hres)) {
131                 ref->type = REF_DISP;
132                 ref->u.d.disp = item->disp;
133                 ref->u.d.id = id;
134                 return S_OK;
135             }
136         }
137     }
138
139     if(!ctx->func->code_ctx->option_explicit)
140         FIXME("create an attempt to set\n");
141
142     ref->type = REF_NONE;
143     return S_OK;
144 }
145
146 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
147 {
148     assert(ctx->top);
149     return ctx->stack + --ctx->top;
150 }
151
152 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
153 {
154     if(ctx->stack_size == ctx->top) {
155         VARIANT *new_stack;
156
157         new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
158         if(!new_stack) {
159             VariantClear(v);
160             return E_OUTOFMEMORY;
161         }
162
163         ctx->stack = new_stack;
164         ctx->stack_size *= 2;
165     }
166
167     ctx->stack[ctx->top++] = *v;
168     return S_OK;
169 }
170
171 static void stack_popn(exec_ctx_t *ctx, unsigned n)
172 {
173     while(n--)
174         VariantClear(stack_pop(ctx));
175 }
176
177 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
178 {
179     VARIANT *var;
180
181     var = stack_pop(ctx);
182
183     if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
184         v->owned = FALSE;
185         var = V_VARIANTREF(var);
186     }else {
187         v->owned = TRUE;
188     }
189
190     if(V_VT(var) == VT_DISPATCH) {
191         FIXME("got dispatch - get its default value\n");
192         return E_NOTIMPL;
193     }else {
194         v->v = var;
195     }
196
197     return S_OK;
198 }
199
200 static inline void release_val(variant_val_t *v)
201 {
202     if(v->owned)
203         VariantClear(v->v);
204 }
205
206 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
207 {
208     VARIANT *v = stack_pop(ctx);
209
210     if(V_VT(v) == VT_DISPATCH) {
211         *ret = V_DISPATCH(v);
212         return S_OK;
213     }
214
215     if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
216         FIXME("not supported type: %s\n", debugstr_variant(v));
217         VariantClear(v);
218         return E_FAIL;
219     }
220
221     v = V_BYREF(v);
222     if(V_VT(v) != VT_DISPATCH) {
223         FIXME("not disp %s\n", debugstr_variant(v));
224         return E_FAIL;
225     }
226
227     if(V_DISPATCH(v))
228         IDispatch_AddRef(V_DISPATCH(v));
229     *ret = V_DISPATCH(v);
230     return S_OK;
231 }
232
233 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
234 {
235     ctx->instr = ctx->code->instrs + addr;
236 }
237
238 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
239 {
240     dp->cArgs = arg_cnt;
241     dp->rgdispidNamedArgs = NULL;
242     dp->cNamedArgs = 0;
243
244     if(arg_cnt) {
245         VARIANT tmp;
246         unsigned i;
247
248         assert(ctx->top >= arg_cnt);
249
250         for(i=1; i*2 <= arg_cnt; i++) {
251             tmp = ctx->stack[ctx->top-i];
252             ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
253             ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
254         }
255
256         dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
257     }else {
258         dp->rgvarg = NULL;
259     }
260 }
261
262 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
263 {
264     BSTR identifier = ctx->instr->arg1.bstr;
265     const unsigned arg_cnt = ctx->instr->arg2.uint;
266     ref_t ref = {0};
267     DISPPARAMS dp;
268     HRESULT hres;
269
270     hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
271     if(FAILED(hres))
272         return hres;
273
274     vbstack_to_dp(ctx, arg_cnt, &dp);
275
276     switch(ref.type) {
277     case REF_VAR:
278         if(!res) {
279             FIXME("REF_VAR no res\n");
280             return E_NOTIMPL;
281         }
282
283         if(arg_cnt) {
284             FIXME("arguments not implemented\n");
285             return E_NOTIMPL;
286         }
287
288         V_VT(res) = VT_BYREF|VT_VARIANT;
289         V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
290         break;
291     case REF_DISP:
292         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
293         if(FAILED(hres))
294             return hres;
295         break;
296     case REF_FUNC:
297         hres = exec_script(ctx->script, ref.u.f, &dp, res);
298         if(FAILED(hres))
299             return hres;
300         break;
301     case REF_NONE:
302         FIXME("%s not found\n", debugstr_w(identifier));
303         return DISP_E_UNKNOWNNAME;
304     }
305
306     stack_popn(ctx, arg_cnt);
307     return S_OK;
308 }
309
310 static HRESULT interp_icall(exec_ctx_t *ctx)
311 {
312     VARIANT v;
313     HRESULT hres;
314
315     TRACE("\n");
316
317     hres = do_icall(ctx, &v);
318     if(FAILED(hres))
319         return hres;
320
321     return stack_push(ctx, &v);
322 }
323
324 static HRESULT interp_icallv(exec_ctx_t *ctx)
325 {
326     TRACE("\n");
327     return do_icall(ctx, NULL);
328 }
329
330 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
331 {
332     ref_t ref;
333     HRESULT hres;
334
335     hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
336     if(FAILED(hres))
337         return hres;
338
339     switch(ref.type) {
340     case REF_VAR: {
341         VARIANT *v = ref.u.v;
342
343         if(V_VT(v) == (VT_VARIANT|VT_BYREF))
344             v = V_VARIANTREF(v);
345
346         if(own_val) {
347             VariantClear(v);
348             *v = *val;
349             hres = S_OK;
350         }else {
351             hres = VariantCopy(v, val);
352         }
353         break;
354     }
355     case REF_DISP:
356         hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
357         if(own_val)
358             VariantClear(val);
359         break;
360     case REF_FUNC:
361         FIXME("functions not implemented\n");
362         return E_NOTIMPL;
363     case REF_NONE:
364         FIXME("%s not found\n", debugstr_w(name));
365         if(own_val)
366             VariantClear(val);
367         return DISP_E_UNKNOWNNAME;
368     }
369
370     return hres;
371 }
372
373 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
374 {
375     const BSTR arg = ctx->instr->arg1.bstr;
376     variant_val_t v;
377     HRESULT hres;
378
379     TRACE("%s\n", debugstr_w(arg));
380
381     hres = stack_pop_val(ctx, &v);
382     if(FAILED(hres))
383         return hres;
384
385     return assign_ident(ctx, arg, v.v, v.owned);
386 }
387
388 static HRESULT interp_set_ident(exec_ctx_t *ctx)
389 {
390     const BSTR arg = ctx->instr->arg1.bstr;
391     FIXME("%s\n", debugstr_w(arg));
392     return E_NOTIMPL;
393 }
394
395 static HRESULT interp_assign_member(exec_ctx_t *ctx)
396 {
397     BSTR identifier = ctx->instr->arg1.bstr;
398     variant_val_t val;
399     IDispatch *obj;
400     DISPID id;
401     HRESULT hres;
402
403     TRACE("%s\n", debugstr_w(identifier));
404
405     hres = stack_pop_disp(ctx, &obj);
406     if(FAILED(hres))
407         return hres;
408
409     if(!obj) {
410         FIXME("NULL obj\n");
411         return E_FAIL;
412     }
413
414     hres = stack_pop_val(ctx, &val);
415     if(FAILED(hres)) {
416         IDispatch_Release(obj);
417         return hres;
418     }
419
420     hres = disp_get_id(obj, identifier, &id);
421     if(SUCCEEDED(hres))
422         hres = disp_propput(ctx->script, obj, id, val.v);
423
424     release_val(&val);
425     IDispatch_Release(obj);
426     return hres;
427 }
428
429 static HRESULT interp_set_member(exec_ctx_t *ctx)
430 {
431     BSTR identifier = ctx->instr->arg1.bstr;
432     FIXME("%s\n", debugstr_w(identifier));
433     return E_NOTIMPL;
434 }
435
436 static HRESULT interp_jmp(exec_ctx_t *ctx)
437 {
438     const unsigned arg = ctx->instr->arg1.uint;
439
440     TRACE("%u\n", arg);
441
442     instr_jmp(ctx, arg);
443     return S_OK;
444 }
445
446 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
447 {
448     const unsigned arg = ctx->instr->arg1.uint;
449     variant_val_t val;
450     HRESULT hres;
451
452     TRACE("%u\n", arg);
453
454     hres = stack_pop_val(ctx, &val);
455     if(FAILED(hres))
456         return hres;
457
458     if(V_VT(val.v) != VT_BOOL) {
459         FIXME("unsupported for %s\n", debugstr_variant(val.v));
460         release_val(&val);
461         return E_NOTIMPL;
462     }
463
464     if(V_BOOL(val.v))
465         ctx->instr++;
466     else
467         instr_jmp(ctx, ctx->instr->arg1.uint);
468     return S_OK;
469 }
470
471 static HRESULT interp_ret(exec_ctx_t *ctx)
472 {
473     TRACE("\n");
474
475     ctx->instr = NULL;
476     return S_OK;
477 }
478
479 static HRESULT interp_bool(exec_ctx_t *ctx)
480 {
481     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
482     VARIANT v;
483
484     TRACE("%s\n", arg ? "true" : "false");
485
486     V_VT(&v) = VT_BOOL;
487     V_BOOL(&v) = arg;
488     return stack_push(ctx, &v);
489 }
490
491 static HRESULT interp_string(exec_ctx_t *ctx)
492 {
493     VARIANT v;
494
495     TRACE("\n");
496
497     V_VT(&v) = VT_BSTR;
498     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
499     if(!V_BSTR(&v))
500         return E_OUTOFMEMORY;
501
502     return stack_push(ctx, &v);
503 }
504
505 static HRESULT interp_long(exec_ctx_t *ctx)
506 {
507     const LONG arg = ctx->instr->arg1.lng;
508     VARIANT v;
509
510     TRACE("%d\n", arg);
511
512     V_VT(&v) = VT_I4;
513     V_I4(&v) = arg;
514     return stack_push(ctx, &v);
515 }
516
517 static HRESULT interp_short(exec_ctx_t *ctx)
518 {
519     const LONG arg = ctx->instr->arg1.lng;
520     VARIANT v;
521
522     TRACE("%d\n", arg);
523
524     V_VT(&v) = VT_I2;
525     V_I2(&v) = arg;
526     return stack_push(ctx, &v);
527 }
528
529 static HRESULT interp_double(exec_ctx_t *ctx)
530 {
531     const DOUBLE *arg = ctx->instr->arg1.dbl;
532     VARIANT v;
533
534     TRACE("%lf\n", *arg);
535
536     V_VT(&v) = VT_R8;
537     V_R8(&v) = *arg;
538     return stack_push(ctx, &v);
539 }
540
541 static HRESULT interp_empty(exec_ctx_t *ctx)
542 {
543     VARIANT v;
544
545     TRACE("\n");
546
547     V_VT(&v) = VT_EMPTY;
548     return stack_push(ctx, &v);
549 }
550
551 static HRESULT interp_null(exec_ctx_t *ctx)
552 {
553     VARIANT v;
554
555     TRACE("\n");
556
557     V_VT(&v) = VT_NULL;
558     return stack_push(ctx, &v);
559 }
560
561 static HRESULT interp_not(exec_ctx_t *ctx)
562 {
563     variant_val_t val;
564     VARIANT v;
565     HRESULT hres;
566
567     TRACE("\n");
568
569     hres = stack_pop_val(ctx, &val);
570     if(FAILED(hres))
571         return hres;
572
573     hres = VarNot(val.v, &v);
574     release_val(&val);
575     if(FAILED(hres))
576         return hres;
577
578     return stack_push(ctx, &v);
579 }
580
581 static HRESULT interp_and(exec_ctx_t *ctx)
582 {
583     variant_val_t r, l;
584     VARIANT v;
585     HRESULT hres;
586
587     TRACE("\n");
588
589     hres = stack_pop_val(ctx, &r);
590     if(FAILED(hres))
591         return hres;
592
593     hres = stack_pop_val(ctx, &l);
594     if(SUCCEEDED(hres)) {
595         hres = VarAnd(l.v, r.v, &v);
596         release_val(&l);
597     }
598     release_val(&r);
599     if(FAILED(hres))
600         return hres;
601
602     return stack_push(ctx, &v);
603 }
604
605 static HRESULT interp_or(exec_ctx_t *ctx)
606 {
607     variant_val_t r, l;
608     VARIANT v;
609     HRESULT hres;
610
611     TRACE("\n");
612
613     hres = stack_pop_val(ctx, &r);
614     if(FAILED(hres))
615         return hres;
616
617     hres = stack_pop_val(ctx, &l);
618     if(SUCCEEDED(hres)) {
619         hres = VarOr(l.v, r.v, &v);
620         release_val(&l);
621     }
622     release_val(&r);
623     if(FAILED(hres))
624         return hres;
625
626     return stack_push(ctx, &v);
627 }
628
629 static HRESULT interp_xor(exec_ctx_t *ctx)
630 {
631     variant_val_t r, l;
632     VARIANT v;
633     HRESULT hres;
634
635     TRACE("\n");
636
637     hres = stack_pop_val(ctx, &r);
638     if(FAILED(hres))
639         return hres;
640
641     hres = stack_pop_val(ctx, &l);
642     if(SUCCEEDED(hres)) {
643         hres = VarXor(l.v, r.v, &v);
644         release_val(&l);
645     }
646     release_val(&r);
647     if(FAILED(hres))
648         return hres;
649
650     return stack_push(ctx, &v);
651 }
652
653 static HRESULT interp_eqv(exec_ctx_t *ctx)
654 {
655     variant_val_t r, l;
656     VARIANT v;
657     HRESULT hres;
658
659     TRACE("\n");
660
661     hres = stack_pop_val(ctx, &r);
662     if(FAILED(hres))
663         return hres;
664
665     hres = stack_pop_val(ctx, &l);
666     if(SUCCEEDED(hres)) {
667         hres = VarEqv(l.v, r.v, &v);
668         release_val(&l);
669     }
670     release_val(&r);
671     if(FAILED(hres))
672         return hres;
673
674     return stack_push(ctx, &v);
675 }
676
677 static HRESULT interp_imp(exec_ctx_t *ctx)
678 {
679     variant_val_t r, l;
680     VARIANT v;
681     HRESULT hres;
682
683     TRACE("\n");
684
685     hres = stack_pop_val(ctx, &r);
686     if(FAILED(hres))
687         return hres;
688
689     hres = stack_pop_val(ctx, &l);
690     if(SUCCEEDED(hres)) {
691         hres = VarImp(l.v, r.v, &v);
692         release_val(&l);
693     }
694     release_val(&r);
695     if(FAILED(hres))
696         return hres;
697
698     return stack_push(ctx, &v);
699 }
700
701 static HRESULT cmp_oper(exec_ctx_t *ctx)
702 {
703     variant_val_t l, r;
704     HRESULT hres;
705
706     hres = stack_pop_val(ctx, &r);
707     if(FAILED(hres))
708         return hres;
709
710     hres = stack_pop_val(ctx, &l);
711     if(SUCCEEDED(hres)) {
712         if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
713             FIXME("comparing nulls is not implemented\n");
714             hres = E_NOTIMPL;
715         }else {
716             hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
717         }
718     }
719
720     release_val(&r);
721     release_val(&l);
722     return hres;
723 }
724
725 static HRESULT interp_equal(exec_ctx_t *ctx)
726 {
727     VARIANT v;
728     HRESULT hres;
729
730     TRACE("\n");
731
732     hres = cmp_oper(ctx);
733     if(FAILED(hres))
734         return hres;
735
736     V_VT(&v) = VT_BOOL;
737     V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
738     return stack_push(ctx, &v);
739 }
740
741 static HRESULT interp_nequal(exec_ctx_t *ctx)
742 {
743     VARIANT v;
744     HRESULT hres;
745
746     TRACE("\n");
747
748     hres = cmp_oper(ctx);
749     if(FAILED(hres))
750         return hres;
751
752     V_VT(&v) = VT_BOOL;
753     V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
754     return stack_push(ctx, &v);
755 }
756
757 static HRESULT interp_concat(exec_ctx_t *ctx)
758 {
759     variant_val_t r, l;
760     VARIANT v;
761     HRESULT hres;
762
763     TRACE("\n");
764
765     hres = stack_pop_val(ctx, &r);
766     if(FAILED(hres))
767         return hres;
768
769     hres = stack_pop_val(ctx, &l);
770     if(SUCCEEDED(hres)) {
771         hres = VarCat(l.v, r.v, &v);
772         release_val(&l);
773     }
774     release_val(&r);
775     if(FAILED(hres))
776         return hres;
777
778     return stack_push(ctx, &v);
779 }
780
781 static HRESULT interp_add(exec_ctx_t *ctx)
782 {
783     variant_val_t r, l;
784     VARIANT v;
785     HRESULT hres;
786
787     TRACE("\n");
788
789     hres = stack_pop_val(ctx, &r);
790     if(FAILED(hres))
791         return hres;
792
793     hres = stack_pop_val(ctx, &l);
794     if(SUCCEEDED(hres)) {
795         hres = VarAdd(l.v, r.v, &v);
796         release_val(&l);
797     }
798     release_val(&r);
799     if(FAILED(hres))
800         return hres;
801
802     return stack_push(ctx, &v);
803 }
804
805 static HRESULT interp_sub(exec_ctx_t *ctx)
806 {
807     variant_val_t r, l;
808     VARIANT v;
809     HRESULT hres;
810
811     TRACE("\n");
812
813     hres = stack_pop_val(ctx, &r);
814     if(FAILED(hres))
815         return hres;
816
817     hres = stack_pop_val(ctx, &l);
818     if(SUCCEEDED(hres)) {
819         hres = VarSub(l.v, r.v, &v);
820         release_val(&l);
821     }
822     release_val(&r);
823     if(FAILED(hres))
824         return hres;
825
826     return stack_push(ctx, &v);
827 }
828
829 static HRESULT interp_mod(exec_ctx_t *ctx)
830 {
831     variant_val_t r, l;
832     VARIANT v;
833     HRESULT hres;
834
835     TRACE("\n");
836
837     hres = stack_pop_val(ctx, &r);
838     if(FAILED(hres))
839         return hres;
840
841     hres = stack_pop_val(ctx, &l);
842     if(SUCCEEDED(hres)) {
843         hres = VarMod(l.v, r.v, &v);
844         release_val(&l);
845     }
846     release_val(&r);
847     if(FAILED(hres))
848         return hres;
849
850     return stack_push(ctx, &v);
851 }
852
853 static HRESULT interp_idiv(exec_ctx_t *ctx)
854 {
855     variant_val_t r, l;
856     VARIANT v;
857     HRESULT hres;
858
859     TRACE("\n");
860
861     hres = stack_pop_val(ctx, &r);
862     if(FAILED(hres))
863         return hres;
864
865     hres = stack_pop_val(ctx, &l);
866     if(SUCCEEDED(hres)) {
867         hres = VarIdiv(l.v, r.v, &v);
868         release_val(&l);
869     }
870     release_val(&r);
871     if(FAILED(hres))
872         return hres;
873
874     return stack_push(ctx, &v);
875 }
876
877 static HRESULT interp_div(exec_ctx_t *ctx)
878 {
879     variant_val_t r, l;
880     VARIANT v;
881     HRESULT hres;
882
883     TRACE("\n");
884
885     hres = stack_pop_val(ctx, &r);
886     if(FAILED(hres))
887         return hres;
888
889     hres = stack_pop_val(ctx, &l);
890     if(SUCCEEDED(hres)) {
891         hres = VarDiv(l.v, r.v, &v);
892         release_val(&l);
893     }
894     release_val(&r);
895     if(FAILED(hres))
896         return hres;
897
898     return stack_push(ctx, &v);
899 }
900
901 static HRESULT interp_mul(exec_ctx_t *ctx)
902 {
903     variant_val_t r, l;
904     VARIANT v;
905     HRESULT hres;
906
907     TRACE("\n");
908
909     hres = stack_pop_val(ctx, &r);
910     if(FAILED(hres))
911         return hres;
912
913     hres = stack_pop_val(ctx, &l);
914     if(SUCCEEDED(hres)) {
915         hres = VarMul(l.v, r.v, &v);
916         release_val(&l);
917     }
918     release_val(&r);
919     if(FAILED(hres))
920         return hres;
921
922     return stack_push(ctx, &v);
923 }
924
925 static HRESULT interp_exp(exec_ctx_t *ctx)
926 {
927     variant_val_t r, l;
928     VARIANT v;
929     HRESULT hres;
930
931     TRACE("\n");
932
933     hres = stack_pop_val(ctx, &r);
934     if(FAILED(hres))
935         return hres;
936
937     hres = stack_pop_val(ctx, &l);
938     if(SUCCEEDED(hres)) {
939         hres = VarPow(l.v, r.v, &v);
940         release_val(&l);
941     }
942     release_val(&r);
943     if(FAILED(hres))
944         return hres;
945
946     return stack_push(ctx, &v);
947 }
948
949 static HRESULT interp_neg(exec_ctx_t *ctx)
950 {
951     variant_val_t val;
952     VARIANT v;
953     HRESULT hres;
954
955     hres = stack_pop_val(ctx, &val);
956     if(FAILED(hres))
957         return hres;
958
959     hres = VarNeg(val.v, &v);
960     release_val(&val);
961     if(FAILED(hres))
962         return hres;
963
964     return stack_push(ctx, &v);
965 }
966
967 static const instr_func_t op_funcs[] = {
968 #define X(x,n,a,b) interp_ ## x,
969 OP_LIST
970 #undef X
971 };
972
973 static const unsigned op_move[] = {
974 #define X(x,n,a,b) n,
975 OP_LIST
976 #undef X
977 };
978
979 static void release_exec(exec_ctx_t *ctx)
980 {
981     unsigned i;
982
983     VariantClear(&ctx->ret_val);
984
985     if(ctx->args) {
986         for(i=0; i < ctx->func->arg_cnt; i++)
987             VariantClear(ctx->args+i);
988     }
989
990     if(ctx->vars) {
991         for(i=0; i < ctx->func->var_cnt; i++)
992             VariantClear(ctx->vars+i);
993     }
994
995     heap_free(ctx->args);
996     heap_free(ctx->vars);
997     heap_free(ctx->stack);
998 }
999
1000 HRESULT exec_script(script_ctx_t *ctx, function_t *func, DISPPARAMS *dp, VARIANT *res)
1001 {
1002     exec_ctx_t exec = {func->code_ctx};
1003     vbsop_t op;
1004     HRESULT hres = S_OK;
1005
1006     exec.code = func->code_ctx;
1007
1008     if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1009         FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1010         return E_FAIL;
1011     }
1012
1013     if(func->arg_cnt) {
1014         VARIANT *v;
1015         unsigned i;
1016
1017         exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1018         if(!exec.args) {
1019             release_exec(&exec);
1020             return E_OUTOFMEMORY;
1021         }
1022
1023         for(i=0; i < func->arg_cnt; i++) {
1024             v = get_arg(dp, i);
1025             if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1026                 if(func->args[i].by_ref)
1027                     exec.args[i] = *v;
1028                 else
1029                     hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1030             }else {
1031                 hres = VariantCopy(exec.args+i, v);
1032             }
1033             if(FAILED(hres)) {
1034                 release_exec(&exec);
1035                 return hres;
1036             }
1037         }
1038     }else {
1039         exec.args = NULL;
1040     }
1041
1042     if(func->var_cnt) {
1043         exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1044         if(!exec.vars) {
1045             release_exec(&exec);
1046             return E_OUTOFMEMORY;
1047         }
1048     }else {
1049         exec.vars = NULL;
1050     }
1051
1052     exec.stack_size = 16;
1053     exec.top = 0;
1054     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1055     if(!exec.stack) {
1056         release_exec(&exec);
1057         return E_OUTOFMEMORY;
1058     }
1059
1060     exec.instr = exec.code->instrs + func->code_off;
1061     exec.script = ctx;
1062     exec.func = func;
1063
1064     while(exec.instr) {
1065         op = exec.instr->op;
1066         hres = op_funcs[op](&exec);
1067         if(FAILED(hres)) {
1068             FIXME("Failed %08x\n", hres);
1069             stack_popn(&exec, exec.top);
1070             break;
1071         }
1072
1073         exec.instr += op_move[op];
1074     }
1075
1076     assert(!exec.top);
1077     if(func->type != FUNC_FUNCTION)
1078         assert(V_VT(&exec.ret_val) == VT_EMPTY);
1079
1080     if(SUCCEEDED(hres) && res) {
1081         *res = exec.ret_val;
1082         V_VT(&exec.ret_val) = VT_EMPTY;
1083     }
1084
1085     release_exec(&exec);
1086
1087     return hres;
1088 }