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