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