dinput: Fix printing NULL strings.
[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         DISPPARAMS dp = {0};
203         HRESULT hres;
204
205         hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
206         if(v->owned)
207             IDispatch_Release(V_DISPATCH(var));
208         if(FAILED(hres))
209             return hres;
210
211         v->owned = TRUE;
212         v->v = &v->store;
213     }else {
214         v->v = var;
215     }
216
217     return S_OK;
218 }
219
220 static inline void release_val(variant_val_t *v)
221 {
222     if(v->owned)
223         VariantClear(v->v);
224 }
225
226 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
227 {
228     VARIANT *v = stack_pop(ctx);
229
230     if(V_VT(v) == VT_DISPATCH) {
231         *ret = V_DISPATCH(v);
232         return S_OK;
233     }
234
235     if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
236         FIXME("not supported type: %s\n", debugstr_variant(v));
237         VariantClear(v);
238         return E_FAIL;
239     }
240
241     v = V_BYREF(v);
242     if(V_VT(v) != VT_DISPATCH) {
243         FIXME("not disp %s\n", debugstr_variant(v));
244         return E_FAIL;
245     }
246
247     if(V_DISPATCH(v))
248         IDispatch_AddRef(V_DISPATCH(v));
249     *ret = V_DISPATCH(v);
250     return S_OK;
251 }
252
253 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
254 {
255     ctx->instr = ctx->code->instrs + addr;
256 }
257
258 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
259 {
260     dp->cArgs = arg_cnt;
261     dp->rgdispidNamedArgs = NULL;
262     dp->cNamedArgs = 0;
263
264     if(arg_cnt) {
265         VARIANT tmp;
266         unsigned i;
267
268         assert(ctx->top >= arg_cnt);
269
270         for(i=1; i*2 <= arg_cnt; i++) {
271             tmp = ctx->stack[ctx->top-i];
272             ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
273             ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
274         }
275
276         dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
277     }else {
278         dp->rgvarg = NULL;
279     }
280 }
281
282 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
283 {
284     BSTR identifier = ctx->instr->arg1.bstr;
285     const unsigned arg_cnt = ctx->instr->arg2.uint;
286     ref_t ref = {0};
287     DISPPARAMS dp;
288     HRESULT hres;
289
290     hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
291     if(FAILED(hres))
292         return hres;
293
294     vbstack_to_dp(ctx, arg_cnt, &dp);
295
296     switch(ref.type) {
297     case REF_VAR:
298         if(!res) {
299             FIXME("REF_VAR no res\n");
300             return E_NOTIMPL;
301         }
302
303         if(arg_cnt) {
304             FIXME("arguments not implemented\n");
305             return E_NOTIMPL;
306         }
307
308         V_VT(res) = VT_BYREF|VT_VARIANT;
309         V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
310         break;
311     case REF_DISP:
312         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
313         if(FAILED(hres))
314             return hres;
315         break;
316     case REF_FUNC:
317         hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
318         if(FAILED(hres))
319             return hres;
320         break;
321     case REF_NONE:
322         FIXME("%s not found\n", debugstr_w(identifier));
323         return DISP_E_UNKNOWNNAME;
324     }
325
326     stack_popn(ctx, arg_cnt);
327     return S_OK;
328 }
329
330 static HRESULT interp_icall(exec_ctx_t *ctx)
331 {
332     VARIANT v;
333     HRESULT hres;
334
335     TRACE("\n");
336
337     hres = do_icall(ctx, &v);
338     if(FAILED(hres))
339         return hres;
340
341     return stack_push(ctx, &v);
342 }
343
344 static HRESULT interp_icallv(exec_ctx_t *ctx)
345 {
346     TRACE("\n");
347     return do_icall(ctx, NULL);
348 }
349
350 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
351 {
352     const BSTR identifier = ctx->instr->arg1.bstr;
353     const unsigned arg_cnt = ctx->instr->arg2.uint;
354     IDispatch *obj;
355     DISPPARAMS dp;
356     DISPID id;
357     HRESULT hres;
358
359     hres = stack_pop_disp(ctx, &obj);
360     if(FAILED(hres))
361         return hres;
362
363     if(!obj) {
364         FIXME("NULL obj\n");
365         return E_FAIL;
366     }
367
368     vbstack_to_dp(ctx, arg_cnt, &dp);
369
370     hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
371     if(SUCCEEDED(hres))
372         hres = disp_call(ctx->script, obj, id, &dp, res);
373     IDispatch_Release(obj);
374     if(FAILED(hres))
375         return hres;
376
377     stack_popn(ctx, arg_cnt);
378     return S_OK;
379 }
380
381 static HRESULT interp_mcall(exec_ctx_t *ctx)
382 {
383     VARIANT res;
384     HRESULT hres;
385
386     TRACE("\n");
387
388     hres = do_mcall(ctx, &res);
389     if(FAILED(hres))
390         return hres;
391
392     return stack_push(ctx, &res);
393 }
394
395 static HRESULT interp_mcallv(exec_ctx_t *ctx)
396 {
397     TRACE("\n");
398
399     return do_mcall(ctx, NULL);
400 }
401
402 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
403 {
404     ref_t ref;
405     HRESULT hres;
406
407     hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
408     if(FAILED(hres))
409         return hres;
410
411     switch(ref.type) {
412     case REF_VAR: {
413         VARIANT *v = ref.u.v;
414
415         if(V_VT(v) == (VT_VARIANT|VT_BYREF))
416             v = V_VARIANTREF(v);
417
418         if(own_val) {
419             VariantClear(v);
420             *v = *val;
421             hres = S_OK;
422         }else {
423             hres = VariantCopy(v, val);
424         }
425         break;
426     }
427     case REF_DISP:
428         hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
429         if(own_val)
430             VariantClear(val);
431         break;
432     case REF_FUNC:
433         FIXME("functions not implemented\n");
434         return E_NOTIMPL;
435     case REF_NONE:
436         FIXME("%s not found\n", debugstr_w(name));
437         if(own_val)
438             VariantClear(val);
439         return DISP_E_UNKNOWNNAME;
440     }
441
442     return hres;
443 }
444
445 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
446 {
447     const BSTR arg = ctx->instr->arg1.bstr;
448     variant_val_t v;
449     HRESULT hres;
450
451     TRACE("%s\n", debugstr_w(arg));
452
453     hres = stack_pop_val(ctx, &v);
454     if(FAILED(hres))
455         return hres;
456
457     return assign_ident(ctx, arg, v.v, v.owned);
458 }
459
460 static HRESULT interp_set_ident(exec_ctx_t *ctx)
461 {
462     const BSTR arg = ctx->instr->arg1.bstr;
463     IDispatch *disp;
464     VARIANT v;
465     HRESULT hres;
466
467     TRACE("%s\n", debugstr_w(arg));
468
469     hres = stack_pop_disp(ctx, &disp);
470     if(FAILED(hres))
471         return hres;
472
473     V_VT(&v) = VT_DISPATCH;
474     V_DISPATCH(&v) = disp;
475     return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
476 }
477
478 static HRESULT interp_assign_member(exec_ctx_t *ctx)
479 {
480     BSTR identifier = ctx->instr->arg1.bstr;
481     variant_val_t val;
482     IDispatch *obj;
483     DISPID id;
484     HRESULT hres;
485
486     TRACE("%s\n", debugstr_w(identifier));
487
488     hres = stack_pop_disp(ctx, &obj);
489     if(FAILED(hres))
490         return hres;
491
492     if(!obj) {
493         FIXME("NULL obj\n");
494         return E_FAIL;
495     }
496
497     hres = stack_pop_val(ctx, &val);
498     if(FAILED(hres)) {
499         IDispatch_Release(obj);
500         return hres;
501     }
502
503     hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
504     if(SUCCEEDED(hres))
505         hres = disp_propput(ctx->script, obj, id, val.v);
506
507     release_val(&val);
508     IDispatch_Release(obj);
509     return hres;
510 }
511
512 static HRESULT interp_set_member(exec_ctx_t *ctx)
513 {
514     BSTR identifier = ctx->instr->arg1.bstr;
515     IDispatch *obj, *val;
516     DISPID id;
517     HRESULT hres;
518
519     TRACE("%s\n", debugstr_w(identifier));
520
521     hres = stack_pop_disp(ctx, &obj);
522     if(FAILED(hres))
523         return hres;
524
525     if(!obj) {
526         FIXME("NULL obj\n");
527         return E_FAIL;
528     }
529
530     hres = stack_pop_disp(ctx, &val);
531     if(FAILED(hres)) {
532         IDispatch_Release(obj);
533         return hres;
534     }
535
536     hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
537     if(SUCCEEDED(hres)) {
538         VARIANT v;
539
540         V_VT(&v) = VT_DISPATCH;
541         V_DISPATCH(&v) = val;
542         hres = disp_propput(ctx->script, obj, id, &v);
543     }
544
545     if(val)
546         IDispatch_Release(val);
547     IDispatch_Release(obj);
548     return hres;
549 }
550
551 static HRESULT interp_new(exec_ctx_t *ctx)
552 {
553     const WCHAR *arg = ctx->instr->arg1.bstr;
554     class_desc_t *class_desc;
555     vbdisp_t *obj;
556     VARIANT v;
557     HRESULT hres;
558
559     TRACE("%s\n", debugstr_w(arg));
560
561     for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
562         if(!strcmpiW(class_desc->name, arg))
563             break;
564     }
565     if(!class_desc) {
566         FIXME("Class %s not found\n", debugstr_w(arg));
567         return E_FAIL;
568     }
569
570     hres = create_vbdisp(class_desc, &obj);
571     if(FAILED(hres))
572         return hres;
573
574     V_VT(&v) = VT_DISPATCH;
575     V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
576     return stack_push(ctx, &v);
577 }
578
579 static HRESULT interp_jmp(exec_ctx_t *ctx)
580 {
581     const unsigned arg = ctx->instr->arg1.uint;
582
583     TRACE("%u\n", arg);
584
585     instr_jmp(ctx, arg);
586     return S_OK;
587 }
588
589 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
590 {
591     const unsigned arg = ctx->instr->arg1.uint;
592     variant_val_t val;
593     HRESULT hres;
594
595     TRACE("%u\n", arg);
596
597     hres = stack_pop_val(ctx, &val);
598     if(FAILED(hres))
599         return hres;
600
601     if(V_VT(val.v) != VT_BOOL) {
602         FIXME("unsupported for %s\n", debugstr_variant(val.v));
603         release_val(&val);
604         return E_NOTIMPL;
605     }
606
607     if(V_BOOL(val.v))
608         ctx->instr++;
609     else
610         instr_jmp(ctx, ctx->instr->arg1.uint);
611     return S_OK;
612 }
613
614 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
615 {
616     const unsigned arg = ctx->instr->arg1.uint;
617     variant_val_t val;
618     HRESULT hres;
619
620     TRACE("%u\n", arg);
621
622     hres = stack_pop_val(ctx, &val);
623     if(FAILED(hres))
624         return hres;
625
626     if(V_VT(val.v) != VT_BOOL) {
627         FIXME("unsupported for %s\n", debugstr_variant(val.v));
628         release_val(&val);
629         return E_NOTIMPL;
630     }
631
632     if(V_BOOL(val.v))
633         instr_jmp(ctx, ctx->instr->arg1.uint);
634     else
635         ctx->instr++;
636     return S_OK;
637 }
638
639 static HRESULT interp_ret(exec_ctx_t *ctx)
640 {
641     TRACE("\n");
642
643     ctx->instr = NULL;
644     return S_OK;
645 }
646
647 static HRESULT interp_stop(exec_ctx_t *ctx)
648 {
649     WARN("\n");
650
651     /* NOTE: this should have effect in debugging mode (that we don't support yet) */
652     return S_OK;
653 }
654
655 static HRESULT interp_bool(exec_ctx_t *ctx)
656 {
657     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
658     VARIANT v;
659
660     TRACE("%s\n", arg ? "true" : "false");
661
662     V_VT(&v) = VT_BOOL;
663     V_BOOL(&v) = arg;
664     return stack_push(ctx, &v);
665 }
666
667 static HRESULT interp_string(exec_ctx_t *ctx)
668 {
669     VARIANT v;
670
671     TRACE("\n");
672
673     V_VT(&v) = VT_BSTR;
674     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
675     if(!V_BSTR(&v))
676         return E_OUTOFMEMORY;
677
678     return stack_push(ctx, &v);
679 }
680
681 static HRESULT interp_long(exec_ctx_t *ctx)
682 {
683     const LONG arg = ctx->instr->arg1.lng;
684     VARIANT v;
685
686     TRACE("%d\n", arg);
687
688     V_VT(&v) = VT_I4;
689     V_I4(&v) = arg;
690     return stack_push(ctx, &v);
691 }
692
693 static HRESULT interp_short(exec_ctx_t *ctx)
694 {
695     const LONG arg = ctx->instr->arg1.lng;
696     VARIANT v;
697
698     TRACE("%d\n", arg);
699
700     V_VT(&v) = VT_I2;
701     V_I2(&v) = arg;
702     return stack_push(ctx, &v);
703 }
704
705 static HRESULT interp_double(exec_ctx_t *ctx)
706 {
707     const DOUBLE *arg = ctx->instr->arg1.dbl;
708     VARIANT v;
709
710     TRACE("%lf\n", *arg);
711
712     V_VT(&v) = VT_R8;
713     V_R8(&v) = *arg;
714     return stack_push(ctx, &v);
715 }
716
717 static HRESULT interp_empty(exec_ctx_t *ctx)
718 {
719     VARIANT v;
720
721     TRACE("\n");
722
723     V_VT(&v) = VT_EMPTY;
724     return stack_push(ctx, &v);
725 }
726
727 static HRESULT interp_null(exec_ctx_t *ctx)
728 {
729     VARIANT v;
730
731     TRACE("\n");
732
733     V_VT(&v) = VT_NULL;
734     return stack_push(ctx, &v);
735 }
736
737 static HRESULT interp_nothing(exec_ctx_t *ctx)
738 {
739     VARIANT v;
740
741     TRACE("\n");
742
743     V_VT(&v) = VT_DISPATCH;
744     V_DISPATCH(&v) = NULL;
745     return stack_push(ctx, &v);
746 }
747
748 static HRESULT interp_not(exec_ctx_t *ctx)
749 {
750     variant_val_t val;
751     VARIANT v;
752     HRESULT hres;
753
754     TRACE("\n");
755
756     hres = stack_pop_val(ctx, &val);
757     if(FAILED(hres))
758         return hres;
759
760     hres = VarNot(val.v, &v);
761     release_val(&val);
762     if(FAILED(hres))
763         return hres;
764
765     return stack_push(ctx, &v);
766 }
767
768 static HRESULT interp_and(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 = VarAnd(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_or(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 = VarOr(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_xor(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 = VarXor(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_eqv(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 = VarEqv(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_imp(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 = VarImp(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 cmp_oper(exec_ctx_t *ctx)
889 {
890     variant_val_t l, r;
891     HRESULT hres;
892
893     hres = stack_pop_val(ctx, &r);
894     if(FAILED(hres))
895         return hres;
896
897     hres = stack_pop_val(ctx, &l);
898     if(SUCCEEDED(hres)) {
899         if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
900             FIXME("comparing nulls is not implemented\n");
901             hres = E_NOTIMPL;
902         }else {
903             hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
904         }
905     }
906
907     release_val(&r);
908     release_val(&l);
909     return hres;
910 }
911
912 static HRESULT interp_equal(exec_ctx_t *ctx)
913 {
914     VARIANT v;
915     HRESULT hres;
916
917     TRACE("\n");
918
919     hres = cmp_oper(ctx);
920     if(FAILED(hres))
921         return hres;
922
923     V_VT(&v) = VT_BOOL;
924     V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
925     return stack_push(ctx, &v);
926 }
927
928 static HRESULT interp_nequal(exec_ctx_t *ctx)
929 {
930     VARIANT v;
931     HRESULT hres;
932
933     TRACE("\n");
934
935     hres = cmp_oper(ctx);
936     if(FAILED(hres))
937         return hres;
938
939     V_VT(&v) = VT_BOOL;
940     V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
941     return stack_push(ctx, &v);
942 }
943
944 static HRESULT interp_concat(exec_ctx_t *ctx)
945 {
946     variant_val_t r, l;
947     VARIANT v;
948     HRESULT hres;
949
950     TRACE("\n");
951
952     hres = stack_pop_val(ctx, &r);
953     if(FAILED(hres))
954         return hres;
955
956     hres = stack_pop_val(ctx, &l);
957     if(SUCCEEDED(hres)) {
958         hres = VarCat(l.v, r.v, &v);
959         release_val(&l);
960     }
961     release_val(&r);
962     if(FAILED(hres))
963         return hres;
964
965     return stack_push(ctx, &v);
966 }
967
968 static HRESULT interp_add(exec_ctx_t *ctx)
969 {
970     variant_val_t r, l;
971     VARIANT v;
972     HRESULT hres;
973
974     TRACE("\n");
975
976     hres = stack_pop_val(ctx, &r);
977     if(FAILED(hres))
978         return hres;
979
980     hres = stack_pop_val(ctx, &l);
981     if(SUCCEEDED(hres)) {
982         hres = VarAdd(l.v, r.v, &v);
983         release_val(&l);
984     }
985     release_val(&r);
986     if(FAILED(hres))
987         return hres;
988
989     return stack_push(ctx, &v);
990 }
991
992 static HRESULT interp_sub(exec_ctx_t *ctx)
993 {
994     variant_val_t r, l;
995     VARIANT v;
996     HRESULT hres;
997
998     TRACE("\n");
999
1000     hres = stack_pop_val(ctx, &r);
1001     if(FAILED(hres))
1002         return hres;
1003
1004     hres = stack_pop_val(ctx, &l);
1005     if(SUCCEEDED(hres)) {
1006         hres = VarSub(l.v, r.v, &v);
1007         release_val(&l);
1008     }
1009     release_val(&r);
1010     if(FAILED(hres))
1011         return hres;
1012
1013     return stack_push(ctx, &v);
1014 }
1015
1016 static HRESULT interp_mod(exec_ctx_t *ctx)
1017 {
1018     variant_val_t r, l;
1019     VARIANT v;
1020     HRESULT hres;
1021
1022     TRACE("\n");
1023
1024     hres = stack_pop_val(ctx, &r);
1025     if(FAILED(hres))
1026         return hres;
1027
1028     hres = stack_pop_val(ctx, &l);
1029     if(SUCCEEDED(hres)) {
1030         hres = VarMod(l.v, r.v, &v);
1031         release_val(&l);
1032     }
1033     release_val(&r);
1034     if(FAILED(hres))
1035         return hres;
1036
1037     return stack_push(ctx, &v);
1038 }
1039
1040 static HRESULT interp_idiv(exec_ctx_t *ctx)
1041 {
1042     variant_val_t r, l;
1043     VARIANT v;
1044     HRESULT hres;
1045
1046     TRACE("\n");
1047
1048     hres = stack_pop_val(ctx, &r);
1049     if(FAILED(hres))
1050         return hres;
1051
1052     hres = stack_pop_val(ctx, &l);
1053     if(SUCCEEDED(hres)) {
1054         hres = VarIdiv(l.v, r.v, &v);
1055         release_val(&l);
1056     }
1057     release_val(&r);
1058     if(FAILED(hres))
1059         return hres;
1060
1061     return stack_push(ctx, &v);
1062 }
1063
1064 static HRESULT interp_div(exec_ctx_t *ctx)
1065 {
1066     variant_val_t r, l;
1067     VARIANT v;
1068     HRESULT hres;
1069
1070     TRACE("\n");
1071
1072     hres = stack_pop_val(ctx, &r);
1073     if(FAILED(hres))
1074         return hres;
1075
1076     hres = stack_pop_val(ctx, &l);
1077     if(SUCCEEDED(hres)) {
1078         hres = VarDiv(l.v, r.v, &v);
1079         release_val(&l);
1080     }
1081     release_val(&r);
1082     if(FAILED(hres))
1083         return hres;
1084
1085     return stack_push(ctx, &v);
1086 }
1087
1088 static HRESULT interp_mul(exec_ctx_t *ctx)
1089 {
1090     variant_val_t r, l;
1091     VARIANT v;
1092     HRESULT hres;
1093
1094     TRACE("\n");
1095
1096     hres = stack_pop_val(ctx, &r);
1097     if(FAILED(hres))
1098         return hres;
1099
1100     hres = stack_pop_val(ctx, &l);
1101     if(SUCCEEDED(hres)) {
1102         hres = VarMul(l.v, r.v, &v);
1103         release_val(&l);
1104     }
1105     release_val(&r);
1106     if(FAILED(hres))
1107         return hres;
1108
1109     return stack_push(ctx, &v);
1110 }
1111
1112 static HRESULT interp_exp(exec_ctx_t *ctx)
1113 {
1114     variant_val_t r, l;
1115     VARIANT v;
1116     HRESULT hres;
1117
1118     TRACE("\n");
1119
1120     hres = stack_pop_val(ctx, &r);
1121     if(FAILED(hres))
1122         return hres;
1123
1124     hres = stack_pop_val(ctx, &l);
1125     if(SUCCEEDED(hres)) {
1126         hres = VarPow(l.v, r.v, &v);
1127         release_val(&l);
1128     }
1129     release_val(&r);
1130     if(FAILED(hres))
1131         return hres;
1132
1133     return stack_push(ctx, &v);
1134 }
1135
1136 static HRESULT interp_neg(exec_ctx_t *ctx)
1137 {
1138     variant_val_t val;
1139     VARIANT v;
1140     HRESULT hres;
1141
1142     hres = stack_pop_val(ctx, &val);
1143     if(FAILED(hres))
1144         return hres;
1145
1146     hres = VarNeg(val.v, &v);
1147     release_val(&val);
1148     if(FAILED(hres))
1149         return hres;
1150
1151     return stack_push(ctx, &v);
1152 }
1153
1154 static const instr_func_t op_funcs[] = {
1155 #define X(x,n,a,b) interp_ ## x,
1156 OP_LIST
1157 #undef X
1158 };
1159
1160 static const unsigned op_move[] = {
1161 #define X(x,n,a,b) n,
1162 OP_LIST
1163 #undef X
1164 };
1165
1166 static void release_exec(exec_ctx_t *ctx)
1167 {
1168     unsigned i;
1169
1170     VariantClear(&ctx->ret_val);
1171
1172     if(ctx->this_obj)
1173         IDispatch_Release(ctx->this_obj);
1174
1175     if(ctx->args) {
1176         for(i=0; i < ctx->func->arg_cnt; i++)
1177             VariantClear(ctx->args+i);
1178     }
1179
1180     if(ctx->vars) {
1181         for(i=0; i < ctx->func->var_cnt; i++)
1182             VariantClear(ctx->vars+i);
1183     }
1184
1185     heap_free(ctx->args);
1186     heap_free(ctx->vars);
1187     heap_free(ctx->stack);
1188 }
1189
1190 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1191 {
1192     exec_ctx_t exec = {func->code_ctx};
1193     vbsop_t op;
1194     HRESULT hres = S_OK;
1195
1196     exec.code = func->code_ctx;
1197
1198     if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1199         FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1200         return E_FAIL;
1201     }
1202
1203     if(func->arg_cnt) {
1204         VARIANT *v;
1205         unsigned i;
1206
1207         exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1208         if(!exec.args) {
1209             release_exec(&exec);
1210             return E_OUTOFMEMORY;
1211         }
1212
1213         for(i=0; i < func->arg_cnt; i++) {
1214             v = get_arg(dp, i);
1215             if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1216                 if(func->args[i].by_ref)
1217                     exec.args[i] = *v;
1218                 else
1219                     hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1220             }else {
1221                 hres = VariantCopy(exec.args+i, v);
1222             }
1223             if(FAILED(hres)) {
1224                 release_exec(&exec);
1225                 return hres;
1226             }
1227         }
1228     }else {
1229         exec.args = NULL;
1230     }
1231
1232     if(func->var_cnt) {
1233         exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1234         if(!exec.vars) {
1235             release_exec(&exec);
1236             return E_OUTOFMEMORY;
1237         }
1238     }else {
1239         exec.vars = NULL;
1240     }
1241
1242     exec.stack_size = 16;
1243     exec.top = 0;
1244     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1245     if(!exec.stack) {
1246         release_exec(&exec);
1247         return E_OUTOFMEMORY;
1248     }
1249
1250     if(this_obj)
1251         exec.this_obj = this_obj;
1252     else if (ctx->host_global)
1253         exec.this_obj = ctx->host_global;
1254     else
1255         exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1256     IDispatch_AddRef(exec.this_obj);
1257
1258     exec.instr = exec.code->instrs + func->code_off;
1259     exec.script = ctx;
1260     exec.func = func;
1261
1262     while(exec.instr) {
1263         op = exec.instr->op;
1264         hres = op_funcs[op](&exec);
1265         if(FAILED(hres)) {
1266             FIXME("Failed %08x\n", hres);
1267             stack_popn(&exec, exec.top);
1268             break;
1269         }
1270
1271         exec.instr += op_move[op];
1272     }
1273
1274     assert(!exec.top);
1275     if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1276         assert(V_VT(&exec.ret_val) == VT_EMPTY);
1277
1278     if(SUCCEEDED(hres) && res) {
1279         *res = exec.ret_val;
1280         V_VT(&exec.ret_val) = VT_EMPTY;
1281     }
1282
1283     release_exec(&exec);
1284
1285     return hres;
1286 }