vbscript: Added interp_set_ident 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     IDispatch *disp;
392     VARIANT v;
393     HRESULT hres;
394
395     TRACE("%s\n", debugstr_w(arg));
396
397     hres = stack_pop_disp(ctx, &disp);
398     if(FAILED(hres))
399         return hres;
400
401     V_VT(&v) = VT_DISPATCH;
402     V_DISPATCH(&v) = disp;
403     return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
404 }
405
406 static HRESULT interp_assign_member(exec_ctx_t *ctx)
407 {
408     BSTR identifier = ctx->instr->arg1.bstr;
409     variant_val_t val;
410     IDispatch *obj;
411     DISPID id;
412     HRESULT hres;
413
414     TRACE("%s\n", debugstr_w(identifier));
415
416     hres = stack_pop_disp(ctx, &obj);
417     if(FAILED(hres))
418         return hres;
419
420     if(!obj) {
421         FIXME("NULL obj\n");
422         return E_FAIL;
423     }
424
425     hres = stack_pop_val(ctx, &val);
426     if(FAILED(hres)) {
427         IDispatch_Release(obj);
428         return hres;
429     }
430
431     hres = disp_get_id(obj, identifier, &id);
432     if(SUCCEEDED(hres))
433         hres = disp_propput(ctx->script, obj, id, val.v);
434
435     release_val(&val);
436     IDispatch_Release(obj);
437     return hres;
438 }
439
440 static HRESULT interp_set_member(exec_ctx_t *ctx)
441 {
442     BSTR identifier = ctx->instr->arg1.bstr;
443     FIXME("%s\n", debugstr_w(identifier));
444     return E_NOTIMPL;
445 }
446
447 static HRESULT interp_jmp(exec_ctx_t *ctx)
448 {
449     const unsigned arg = ctx->instr->arg1.uint;
450
451     TRACE("%u\n", arg);
452
453     instr_jmp(ctx, arg);
454     return S_OK;
455 }
456
457 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
458 {
459     const unsigned arg = ctx->instr->arg1.uint;
460     variant_val_t val;
461     HRESULT hres;
462
463     TRACE("%u\n", arg);
464
465     hres = stack_pop_val(ctx, &val);
466     if(FAILED(hres))
467         return hres;
468
469     if(V_VT(val.v) != VT_BOOL) {
470         FIXME("unsupported for %s\n", debugstr_variant(val.v));
471         release_val(&val);
472         return E_NOTIMPL;
473     }
474
475     if(V_BOOL(val.v))
476         ctx->instr++;
477     else
478         instr_jmp(ctx, ctx->instr->arg1.uint);
479     return S_OK;
480 }
481
482 static HRESULT interp_ret(exec_ctx_t *ctx)
483 {
484     TRACE("\n");
485
486     ctx->instr = NULL;
487     return S_OK;
488 }
489
490 static HRESULT interp_bool(exec_ctx_t *ctx)
491 {
492     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
493     VARIANT v;
494
495     TRACE("%s\n", arg ? "true" : "false");
496
497     V_VT(&v) = VT_BOOL;
498     V_BOOL(&v) = arg;
499     return stack_push(ctx, &v);
500 }
501
502 static HRESULT interp_string(exec_ctx_t *ctx)
503 {
504     VARIANT v;
505
506     TRACE("\n");
507
508     V_VT(&v) = VT_BSTR;
509     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
510     if(!V_BSTR(&v))
511         return E_OUTOFMEMORY;
512
513     return stack_push(ctx, &v);
514 }
515
516 static HRESULT interp_long(exec_ctx_t *ctx)
517 {
518     const LONG arg = ctx->instr->arg1.lng;
519     VARIANT v;
520
521     TRACE("%d\n", arg);
522
523     V_VT(&v) = VT_I4;
524     V_I4(&v) = arg;
525     return stack_push(ctx, &v);
526 }
527
528 static HRESULT interp_short(exec_ctx_t *ctx)
529 {
530     const LONG arg = ctx->instr->arg1.lng;
531     VARIANT v;
532
533     TRACE("%d\n", arg);
534
535     V_VT(&v) = VT_I2;
536     V_I2(&v) = arg;
537     return stack_push(ctx, &v);
538 }
539
540 static HRESULT interp_double(exec_ctx_t *ctx)
541 {
542     const DOUBLE *arg = ctx->instr->arg1.dbl;
543     VARIANT v;
544
545     TRACE("%lf\n", *arg);
546
547     V_VT(&v) = VT_R8;
548     V_R8(&v) = *arg;
549     return stack_push(ctx, &v);
550 }
551
552 static HRESULT interp_empty(exec_ctx_t *ctx)
553 {
554     VARIANT v;
555
556     TRACE("\n");
557
558     V_VT(&v) = VT_EMPTY;
559     return stack_push(ctx, &v);
560 }
561
562 static HRESULT interp_null(exec_ctx_t *ctx)
563 {
564     VARIANT v;
565
566     TRACE("\n");
567
568     V_VT(&v) = VT_NULL;
569     return stack_push(ctx, &v);
570 }
571
572 static HRESULT interp_not(exec_ctx_t *ctx)
573 {
574     variant_val_t val;
575     VARIANT v;
576     HRESULT hres;
577
578     TRACE("\n");
579
580     hres = stack_pop_val(ctx, &val);
581     if(FAILED(hres))
582         return hres;
583
584     hres = VarNot(val.v, &v);
585     release_val(&val);
586     if(FAILED(hres))
587         return hres;
588
589     return stack_push(ctx, &v);
590 }
591
592 static HRESULT interp_and(exec_ctx_t *ctx)
593 {
594     variant_val_t r, l;
595     VARIANT v;
596     HRESULT hres;
597
598     TRACE("\n");
599
600     hres = stack_pop_val(ctx, &r);
601     if(FAILED(hres))
602         return hres;
603
604     hres = stack_pop_val(ctx, &l);
605     if(SUCCEEDED(hres)) {
606         hres = VarAnd(l.v, r.v, &v);
607         release_val(&l);
608     }
609     release_val(&r);
610     if(FAILED(hres))
611         return hres;
612
613     return stack_push(ctx, &v);
614 }
615
616 static HRESULT interp_or(exec_ctx_t *ctx)
617 {
618     variant_val_t r, l;
619     VARIANT v;
620     HRESULT hres;
621
622     TRACE("\n");
623
624     hres = stack_pop_val(ctx, &r);
625     if(FAILED(hres))
626         return hres;
627
628     hres = stack_pop_val(ctx, &l);
629     if(SUCCEEDED(hres)) {
630         hres = VarOr(l.v, r.v, &v);
631         release_val(&l);
632     }
633     release_val(&r);
634     if(FAILED(hres))
635         return hres;
636
637     return stack_push(ctx, &v);
638 }
639
640 static HRESULT interp_xor(exec_ctx_t *ctx)
641 {
642     variant_val_t r, l;
643     VARIANT v;
644     HRESULT hres;
645
646     TRACE("\n");
647
648     hres = stack_pop_val(ctx, &r);
649     if(FAILED(hres))
650         return hres;
651
652     hres = stack_pop_val(ctx, &l);
653     if(SUCCEEDED(hres)) {
654         hres = VarXor(l.v, r.v, &v);
655         release_val(&l);
656     }
657     release_val(&r);
658     if(FAILED(hres))
659         return hres;
660
661     return stack_push(ctx, &v);
662 }
663
664 static HRESULT interp_eqv(exec_ctx_t *ctx)
665 {
666     variant_val_t r, l;
667     VARIANT v;
668     HRESULT hres;
669
670     TRACE("\n");
671
672     hres = stack_pop_val(ctx, &r);
673     if(FAILED(hres))
674         return hres;
675
676     hres = stack_pop_val(ctx, &l);
677     if(SUCCEEDED(hres)) {
678         hres = VarEqv(l.v, r.v, &v);
679         release_val(&l);
680     }
681     release_val(&r);
682     if(FAILED(hres))
683         return hres;
684
685     return stack_push(ctx, &v);
686 }
687
688 static HRESULT interp_imp(exec_ctx_t *ctx)
689 {
690     variant_val_t r, l;
691     VARIANT v;
692     HRESULT hres;
693
694     TRACE("\n");
695
696     hres = stack_pop_val(ctx, &r);
697     if(FAILED(hres))
698         return hres;
699
700     hres = stack_pop_val(ctx, &l);
701     if(SUCCEEDED(hres)) {
702         hres = VarImp(l.v, r.v, &v);
703         release_val(&l);
704     }
705     release_val(&r);
706     if(FAILED(hres))
707         return hres;
708
709     return stack_push(ctx, &v);
710 }
711
712 static HRESULT cmp_oper(exec_ctx_t *ctx)
713 {
714     variant_val_t l, r;
715     HRESULT hres;
716
717     hres = stack_pop_val(ctx, &r);
718     if(FAILED(hres))
719         return hres;
720
721     hres = stack_pop_val(ctx, &l);
722     if(SUCCEEDED(hres)) {
723         if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
724             FIXME("comparing nulls is not implemented\n");
725             hres = E_NOTIMPL;
726         }else {
727             hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
728         }
729     }
730
731     release_val(&r);
732     release_val(&l);
733     return hres;
734 }
735
736 static HRESULT interp_equal(exec_ctx_t *ctx)
737 {
738     VARIANT v;
739     HRESULT hres;
740
741     TRACE("\n");
742
743     hres = cmp_oper(ctx);
744     if(FAILED(hres))
745         return hres;
746
747     V_VT(&v) = VT_BOOL;
748     V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
749     return stack_push(ctx, &v);
750 }
751
752 static HRESULT interp_nequal(exec_ctx_t *ctx)
753 {
754     VARIANT v;
755     HRESULT hres;
756
757     TRACE("\n");
758
759     hres = cmp_oper(ctx);
760     if(FAILED(hres))
761         return hres;
762
763     V_VT(&v) = VT_BOOL;
764     V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
765     return stack_push(ctx, &v);
766 }
767
768 static HRESULT interp_concat(exec_ctx_t *ctx)
769 {
770     variant_val_t r, l;
771     VARIANT v;
772     HRESULT hres;
773
774     TRACE("\n");
775
776     hres = stack_pop_val(ctx, &r);
777     if(FAILED(hres))
778         return hres;
779
780     hres = stack_pop_val(ctx, &l);
781     if(SUCCEEDED(hres)) {
782         hres = VarCat(l.v, r.v, &v);
783         release_val(&l);
784     }
785     release_val(&r);
786     if(FAILED(hres))
787         return hres;
788
789     return stack_push(ctx, &v);
790 }
791
792 static HRESULT interp_add(exec_ctx_t *ctx)
793 {
794     variant_val_t r, l;
795     VARIANT v;
796     HRESULT hres;
797
798     TRACE("\n");
799
800     hres = stack_pop_val(ctx, &r);
801     if(FAILED(hres))
802         return hres;
803
804     hres = stack_pop_val(ctx, &l);
805     if(SUCCEEDED(hres)) {
806         hres = VarAdd(l.v, r.v, &v);
807         release_val(&l);
808     }
809     release_val(&r);
810     if(FAILED(hres))
811         return hres;
812
813     return stack_push(ctx, &v);
814 }
815
816 static HRESULT interp_sub(exec_ctx_t *ctx)
817 {
818     variant_val_t r, l;
819     VARIANT v;
820     HRESULT hres;
821
822     TRACE("\n");
823
824     hres = stack_pop_val(ctx, &r);
825     if(FAILED(hres))
826         return hres;
827
828     hres = stack_pop_val(ctx, &l);
829     if(SUCCEEDED(hres)) {
830         hres = VarSub(l.v, r.v, &v);
831         release_val(&l);
832     }
833     release_val(&r);
834     if(FAILED(hres))
835         return hres;
836
837     return stack_push(ctx, &v);
838 }
839
840 static HRESULT interp_mod(exec_ctx_t *ctx)
841 {
842     variant_val_t r, l;
843     VARIANT v;
844     HRESULT hres;
845
846     TRACE("\n");
847
848     hres = stack_pop_val(ctx, &r);
849     if(FAILED(hres))
850         return hres;
851
852     hres = stack_pop_val(ctx, &l);
853     if(SUCCEEDED(hres)) {
854         hres = VarMod(l.v, r.v, &v);
855         release_val(&l);
856     }
857     release_val(&r);
858     if(FAILED(hres))
859         return hres;
860
861     return stack_push(ctx, &v);
862 }
863
864 static HRESULT interp_idiv(exec_ctx_t *ctx)
865 {
866     variant_val_t r, l;
867     VARIANT v;
868     HRESULT hres;
869
870     TRACE("\n");
871
872     hres = stack_pop_val(ctx, &r);
873     if(FAILED(hres))
874         return hres;
875
876     hres = stack_pop_val(ctx, &l);
877     if(SUCCEEDED(hres)) {
878         hres = VarIdiv(l.v, r.v, &v);
879         release_val(&l);
880     }
881     release_val(&r);
882     if(FAILED(hres))
883         return hres;
884
885     return stack_push(ctx, &v);
886 }
887
888 static HRESULT interp_div(exec_ctx_t *ctx)
889 {
890     variant_val_t r, l;
891     VARIANT v;
892     HRESULT hres;
893
894     TRACE("\n");
895
896     hres = stack_pop_val(ctx, &r);
897     if(FAILED(hres))
898         return hres;
899
900     hres = stack_pop_val(ctx, &l);
901     if(SUCCEEDED(hres)) {
902         hres = VarDiv(l.v, r.v, &v);
903         release_val(&l);
904     }
905     release_val(&r);
906     if(FAILED(hres))
907         return hres;
908
909     return stack_push(ctx, &v);
910 }
911
912 static HRESULT interp_mul(exec_ctx_t *ctx)
913 {
914     variant_val_t r, l;
915     VARIANT v;
916     HRESULT hres;
917
918     TRACE("\n");
919
920     hres = stack_pop_val(ctx, &r);
921     if(FAILED(hres))
922         return hres;
923
924     hres = stack_pop_val(ctx, &l);
925     if(SUCCEEDED(hres)) {
926         hres = VarMul(l.v, r.v, &v);
927         release_val(&l);
928     }
929     release_val(&r);
930     if(FAILED(hres))
931         return hres;
932
933     return stack_push(ctx, &v);
934 }
935
936 static HRESULT interp_exp(exec_ctx_t *ctx)
937 {
938     variant_val_t r, l;
939     VARIANT v;
940     HRESULT hres;
941
942     TRACE("\n");
943
944     hres = stack_pop_val(ctx, &r);
945     if(FAILED(hres))
946         return hres;
947
948     hres = stack_pop_val(ctx, &l);
949     if(SUCCEEDED(hres)) {
950         hres = VarPow(l.v, r.v, &v);
951         release_val(&l);
952     }
953     release_val(&r);
954     if(FAILED(hres))
955         return hres;
956
957     return stack_push(ctx, &v);
958 }
959
960 static HRESULT interp_neg(exec_ctx_t *ctx)
961 {
962     variant_val_t val;
963     VARIANT v;
964     HRESULT hres;
965
966     hres = stack_pop_val(ctx, &val);
967     if(FAILED(hres))
968         return hres;
969
970     hres = VarNeg(val.v, &v);
971     release_val(&val);
972     if(FAILED(hres))
973         return hres;
974
975     return stack_push(ctx, &v);
976 }
977
978 static const instr_func_t op_funcs[] = {
979 #define X(x,n,a,b) interp_ ## x,
980 OP_LIST
981 #undef X
982 };
983
984 static const unsigned op_move[] = {
985 #define X(x,n,a,b) n,
986 OP_LIST
987 #undef X
988 };
989
990 static void release_exec(exec_ctx_t *ctx)
991 {
992     unsigned i;
993
994     VariantClear(&ctx->ret_val);
995
996     if(ctx->args) {
997         for(i=0; i < ctx->func->arg_cnt; i++)
998             VariantClear(ctx->args+i);
999     }
1000
1001     if(ctx->vars) {
1002         for(i=0; i < ctx->func->var_cnt; i++)
1003             VariantClear(ctx->vars+i);
1004     }
1005
1006     heap_free(ctx->args);
1007     heap_free(ctx->vars);
1008     heap_free(ctx->stack);
1009 }
1010
1011 HRESULT exec_script(script_ctx_t *ctx, function_t *func, DISPPARAMS *dp, VARIANT *res)
1012 {
1013     exec_ctx_t exec = {func->code_ctx};
1014     vbsop_t op;
1015     HRESULT hres = S_OK;
1016
1017     exec.code = func->code_ctx;
1018
1019     if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1020         FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1021         return E_FAIL;
1022     }
1023
1024     if(func->arg_cnt) {
1025         VARIANT *v;
1026         unsigned i;
1027
1028         exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1029         if(!exec.args) {
1030             release_exec(&exec);
1031             return E_OUTOFMEMORY;
1032         }
1033
1034         for(i=0; i < func->arg_cnt; i++) {
1035             v = get_arg(dp, i);
1036             if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1037                 if(func->args[i].by_ref)
1038                     exec.args[i] = *v;
1039                 else
1040                     hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1041             }else {
1042                 hres = VariantCopy(exec.args+i, v);
1043             }
1044             if(FAILED(hres)) {
1045                 release_exec(&exec);
1046                 return hres;
1047             }
1048         }
1049     }else {
1050         exec.args = NULL;
1051     }
1052
1053     if(func->var_cnt) {
1054         exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1055         if(!exec.vars) {
1056             release_exec(&exec);
1057             return E_OUTOFMEMORY;
1058         }
1059     }else {
1060         exec.vars = NULL;
1061     }
1062
1063     exec.stack_size = 16;
1064     exec.top = 0;
1065     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1066     if(!exec.stack) {
1067         release_exec(&exec);
1068         return E_OUTOFMEMORY;
1069     }
1070
1071     exec.instr = exec.code->instrs + func->code_off;
1072     exec.script = ctx;
1073     exec.func = func;
1074
1075     while(exec.instr) {
1076         op = exec.instr->op;
1077         hres = op_funcs[op](&exec);
1078         if(FAILED(hres)) {
1079             FIXME("Failed %08x\n", hres);
1080             stack_popn(&exec, exec.top);
1081             break;
1082         }
1083
1084         exec.instr += op_move[op];
1085     }
1086
1087     assert(!exec.top);
1088     if(func->type != FUNC_FUNCTION)
1089         assert(V_VT(&exec.ret_val) == VT_EMPTY);
1090
1091     if(SUCCEEDED(hres) && res) {
1092         *res = exec.ret_val;
1093         V_VT(&exec.ret_val) = VT_EMPTY;
1094     }
1095
1096     release_exec(&exec);
1097
1098     return hres;
1099 }