vbscript: Added global object's isObject function stub implementation.
[wine.git] / dlls / vbscript / interp.c
blob7c3df7db48be72483b5f8b42b9297fbd1ccac084
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <assert.h>
21 #include "vbscript.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 typedef struct {
29 vbscode_t *code;
30 instr_t *instr;
31 script_ctx_t *script;
32 function_t *func;
33 IDispatch *this_obj;
35 VARIANT *args;
36 VARIANT *vars;
38 unsigned stack_size;
39 unsigned top;
40 VARIANT *stack;
42 VARIANT ret_val;
43 } exec_ctx_t;
45 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
47 typedef enum {
48 REF_NONE,
49 REF_DISP,
50 REF_VAR,
51 REF_FUNC
52 } ref_type_t;
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;
66 typedef struct {
67 VARIANT *v;
68 VARIANT store;
69 BOOL owned;
70 } variant_val_t;
72 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
74 while(var) {
75 if(!strcmpiW(var->name, name)) {
76 ref->type = REF_VAR;
77 ref->u.v = &var->v;
78 return TRUE;
81 var = var->next;
84 return FALSE;
87 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
89 named_item_t *item;
90 function_t *func;
91 unsigned i;
92 DISPID id;
93 HRESULT hres;
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;
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;
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;
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;
127 if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
128 return S_OK;
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;
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;
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;
158 if(!ctx->func->code_ctx->option_explicit)
159 FIXME("create an attempt to set\n");
161 ref->type = REF_NONE;
162 return S_OK;
165 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
167 assert(ctx->top);
168 return ctx->stack + --ctx->top;
171 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
173 if(ctx->stack_size == ctx->top) {
174 VARIANT *new_stack;
176 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
177 if(!new_stack) {
178 VariantClear(v);
179 return E_OUTOFMEMORY;
182 ctx->stack = new_stack;
183 ctx->stack_size *= 2;
186 ctx->stack[ctx->top++] = *v;
187 return S_OK;
190 static void stack_popn(exec_ctx_t *ctx, unsigned n)
192 while(n--)
193 VariantClear(stack_pop(ctx));
196 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
198 VARIANT *var;
200 var = stack_pop(ctx);
202 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
203 v->owned = FALSE;
204 var = V_VARIANTREF(var);
205 }else {
206 v->owned = TRUE;
209 if(V_VT(var) == VT_DISPATCH) {
210 DISPPARAMS dp = {0};
211 HRESULT hres;
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;
219 v->owned = TRUE;
220 v->v = &v->store;
221 }else {
222 v->v = var;
225 return S_OK;
228 static inline void release_val(variant_val_t *v)
230 if(v->owned)
231 VariantClear(v->v);
234 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
236 VARIANT *v = stack_pop(ctx);
238 if(V_VT(v) == VT_DISPATCH) {
239 *ret = V_DISPATCH(v);
240 return S_OK;
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;
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;
255 if(V_DISPATCH(v))
256 IDispatch_AddRef(V_DISPATCH(v));
257 *ret = V_DISPATCH(v);
258 return S_OK;
261 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
263 ctx->instr = ctx->code->instrs + addr;
266 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
268 dp->cArgs = arg_cnt;
269 dp->rgdispidNamedArgs = NULL;
270 dp->cNamedArgs = 0;
272 if(arg_cnt) {
273 VARIANT tmp;
274 unsigned i;
276 assert(ctx->top >= arg_cnt);
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;
284 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
285 }else {
286 dp->rgvarg = NULL;
290 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
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;
298 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
299 if(FAILED(hres))
300 return hres;
302 vbstack_to_dp(ctx, arg_cnt, &dp);
304 switch(ref.type) {
305 case REF_VAR:
306 if(!res) {
307 FIXME("REF_VAR no res\n");
308 return E_NOTIMPL;
311 if(arg_cnt) {
312 FIXME("arguments not implemented\n");
313 return E_NOTIMPL;
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;
334 stack_popn(ctx, arg_cnt);
335 return S_OK;
338 static HRESULT interp_icall(exec_ctx_t *ctx)
340 VARIANT v;
341 HRESULT hres;
343 TRACE("\n");
345 hres = do_icall(ctx, &v);
346 if(FAILED(hres))
347 return hres;
349 return stack_push(ctx, &v);
352 static HRESULT interp_icallv(exec_ctx_t *ctx)
354 TRACE("\n");
355 return do_icall(ctx, NULL);
358 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
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;
367 hres = stack_pop_disp(ctx, &obj);
368 if(FAILED(hres))
369 return hres;
371 if(!obj) {
372 FIXME("NULL obj\n");
373 return E_FAIL;
376 vbstack_to_dp(ctx, arg_cnt, &dp);
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;
385 stack_popn(ctx, arg_cnt);
386 return S_OK;
389 static HRESULT interp_mcall(exec_ctx_t *ctx)
391 VARIANT res;
392 HRESULT hres;
394 TRACE("\n");
396 hres = do_mcall(ctx, &res);
397 if(FAILED(hres))
398 return hres;
400 return stack_push(ctx, &res);
403 static HRESULT interp_mcallv(exec_ctx_t *ctx)
405 TRACE("\n");
407 return do_mcall(ctx, NULL);
410 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
412 ref_t ref;
413 HRESULT hres;
415 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
416 if(FAILED(hres))
417 return hres;
419 switch(ref.type) {
420 case REF_VAR: {
421 VARIANT *v = ref.u.v;
423 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
424 v = V_VARIANTREF(v);
426 if(own_val) {
427 VariantClear(v);
428 *v = *val;
429 hres = S_OK;
430 }else {
431 hres = VariantCopy(v, val);
433 break;
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;
450 return hres;
453 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
455 const BSTR arg = ctx->instr->arg1.bstr;
456 variant_val_t v;
457 HRESULT hres;
459 TRACE("%s\n", debugstr_w(arg));
461 hres = stack_pop_val(ctx, &v);
462 if(FAILED(hres))
463 return hres;
465 return assign_ident(ctx, arg, v.v, v.owned);
468 static HRESULT interp_set_ident(exec_ctx_t *ctx)
470 const BSTR arg = ctx->instr->arg1.bstr;
471 IDispatch *disp;
472 VARIANT v;
473 HRESULT hres;
475 TRACE("%s\n", debugstr_w(arg));
477 hres = stack_pop_disp(ctx, &disp);
478 if(FAILED(hres))
479 return hres;
481 V_VT(&v) = VT_DISPATCH;
482 V_DISPATCH(&v) = disp;
483 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
486 static HRESULT interp_assign_member(exec_ctx_t *ctx)
488 BSTR identifier = ctx->instr->arg1.bstr;
489 variant_val_t val;
490 IDispatch *obj;
491 DISPID id;
492 HRESULT hres;
494 TRACE("%s\n", debugstr_w(identifier));
496 hres = stack_pop_disp(ctx, &obj);
497 if(FAILED(hres))
498 return hres;
500 if(!obj) {
501 FIXME("NULL obj\n");
502 return E_FAIL;
505 hres = stack_pop_val(ctx, &val);
506 if(FAILED(hres)) {
507 IDispatch_Release(obj);
508 return hres;
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);
515 release_val(&val);
516 IDispatch_Release(obj);
517 return hres;
520 static HRESULT interp_set_member(exec_ctx_t *ctx)
522 BSTR identifier = ctx->instr->arg1.bstr;
523 IDispatch *obj, *val;
524 DISPID id;
525 HRESULT hres;
527 TRACE("%s\n", debugstr_w(identifier));
529 hres = stack_pop_disp(ctx, &obj);
530 if(FAILED(hres))
531 return hres;
533 if(!obj) {
534 FIXME("NULL obj\n");
535 return E_FAIL;
538 hres = stack_pop_disp(ctx, &val);
539 if(FAILED(hres)) {
540 IDispatch_Release(obj);
541 return hres;
544 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
545 if(SUCCEEDED(hres)) {
546 VARIANT v;
548 V_VT(&v) = VT_DISPATCH;
549 V_DISPATCH(&v) = val;
550 hres = disp_propput(ctx->script, obj, id, &v);
553 if(val)
554 IDispatch_Release(val);
555 IDispatch_Release(obj);
556 return hres;
559 static HRESULT interp_new(exec_ctx_t *ctx)
561 const WCHAR *arg = ctx->instr->arg1.bstr;
562 class_desc_t *class_desc;
563 vbdisp_t *obj;
564 VARIANT v;
565 HRESULT hres;
567 TRACE("%s\n", debugstr_w(arg));
569 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
570 if(!strcmpiW(class_desc->name, arg))
571 break;
573 if(!class_desc) {
574 FIXME("Class %s not found\n", debugstr_w(arg));
575 return E_FAIL;
578 hres = create_vbdisp(class_desc, &obj);
579 if(FAILED(hres))
580 return hres;
582 V_VT(&v) = VT_DISPATCH;
583 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
584 return stack_push(ctx, &v);
587 static HRESULT interp_jmp(exec_ctx_t *ctx)
589 const unsigned arg = ctx->instr->arg1.uint;
591 TRACE("%u\n", arg);
593 instr_jmp(ctx, arg);
594 return S_OK;
597 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
599 const unsigned arg = ctx->instr->arg1.uint;
600 variant_val_t val;
601 HRESULT hres;
603 TRACE("%u\n", arg);
605 hres = stack_pop_val(ctx, &val);
606 if(FAILED(hres))
607 return hres;
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;
615 if(V_BOOL(val.v))
616 ctx->instr++;
617 else
618 instr_jmp(ctx, ctx->instr->arg1.uint);
619 return S_OK;
622 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
624 const unsigned arg = ctx->instr->arg1.uint;
625 variant_val_t val;
626 HRESULT hres;
628 TRACE("%u\n", arg);
630 hres = stack_pop_val(ctx, &val);
631 if(FAILED(hres))
632 return hres;
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;
640 if(V_BOOL(val.v))
641 instr_jmp(ctx, ctx->instr->arg1.uint);
642 else
643 ctx->instr++;
644 return S_OK;
647 static HRESULT interp_ret(exec_ctx_t *ctx)
649 TRACE("\n");
651 ctx->instr = NULL;
652 return S_OK;
655 static HRESULT interp_stop(exec_ctx_t *ctx)
657 WARN("\n");
659 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
660 return S_OK;
663 static HRESULT interp_bool(exec_ctx_t *ctx)
665 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
666 VARIANT v;
668 TRACE("%s\n", arg ? "true" : "false");
670 V_VT(&v) = VT_BOOL;
671 V_BOOL(&v) = arg;
672 return stack_push(ctx, &v);
675 static HRESULT interp_string(exec_ctx_t *ctx)
677 VARIANT v;
679 TRACE("\n");
681 V_VT(&v) = VT_BSTR;
682 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
683 if(!V_BSTR(&v))
684 return E_OUTOFMEMORY;
686 return stack_push(ctx, &v);
689 static HRESULT interp_long(exec_ctx_t *ctx)
691 const LONG arg = ctx->instr->arg1.lng;
692 VARIANT v;
694 TRACE("%d\n", arg);
696 V_VT(&v) = VT_I4;
697 V_I4(&v) = arg;
698 return stack_push(ctx, &v);
701 static HRESULT interp_short(exec_ctx_t *ctx)
703 const LONG arg = ctx->instr->arg1.lng;
704 VARIANT v;
706 TRACE("%d\n", arg);
708 V_VT(&v) = VT_I2;
709 V_I2(&v) = arg;
710 return stack_push(ctx, &v);
713 static HRESULT interp_double(exec_ctx_t *ctx)
715 const DOUBLE *arg = ctx->instr->arg1.dbl;
716 VARIANT v;
718 TRACE("%lf\n", *arg);
720 V_VT(&v) = VT_R8;
721 V_R8(&v) = *arg;
722 return stack_push(ctx, &v);
725 static HRESULT interp_empty(exec_ctx_t *ctx)
727 VARIANT v;
729 TRACE("\n");
731 V_VT(&v) = VT_EMPTY;
732 return stack_push(ctx, &v);
735 static HRESULT interp_null(exec_ctx_t *ctx)
737 VARIANT v;
739 TRACE("\n");
741 V_VT(&v) = VT_NULL;
742 return stack_push(ctx, &v);
745 static HRESULT interp_nothing(exec_ctx_t *ctx)
747 VARIANT v;
749 TRACE("\n");
751 V_VT(&v) = VT_DISPATCH;
752 V_DISPATCH(&v) = NULL;
753 return stack_push(ctx, &v);
756 static HRESULT interp_not(exec_ctx_t *ctx)
758 variant_val_t val;
759 VARIANT v;
760 HRESULT hres;
762 TRACE("\n");
764 hres = stack_pop_val(ctx, &val);
765 if(FAILED(hres))
766 return hres;
768 hres = VarNot(val.v, &v);
769 release_val(&val);
770 if(FAILED(hres))
771 return hres;
773 return stack_push(ctx, &v);
776 static HRESULT interp_and(exec_ctx_t *ctx)
778 variant_val_t r, l;
779 VARIANT v;
780 HRESULT hres;
782 TRACE("\n");
784 hres = stack_pop_val(ctx, &r);
785 if(FAILED(hres))
786 return hres;
788 hres = stack_pop_val(ctx, &l);
789 if(SUCCEEDED(hres)) {
790 hres = VarAnd(l.v, r.v, &v);
791 release_val(&l);
793 release_val(&r);
794 if(FAILED(hres))
795 return hres;
797 return stack_push(ctx, &v);
800 static HRESULT interp_or(exec_ctx_t *ctx)
802 variant_val_t r, l;
803 VARIANT v;
804 HRESULT hres;
806 TRACE("\n");
808 hres = stack_pop_val(ctx, &r);
809 if(FAILED(hres))
810 return hres;
812 hres = stack_pop_val(ctx, &l);
813 if(SUCCEEDED(hres)) {
814 hres = VarOr(l.v, r.v, &v);
815 release_val(&l);
817 release_val(&r);
818 if(FAILED(hres))
819 return hres;
821 return stack_push(ctx, &v);
824 static HRESULT interp_xor(exec_ctx_t *ctx)
826 variant_val_t r, l;
827 VARIANT v;
828 HRESULT hres;
830 TRACE("\n");
832 hres = stack_pop_val(ctx, &r);
833 if(FAILED(hres))
834 return hres;
836 hres = stack_pop_val(ctx, &l);
837 if(SUCCEEDED(hres)) {
838 hres = VarXor(l.v, r.v, &v);
839 release_val(&l);
841 release_val(&r);
842 if(FAILED(hres))
843 return hres;
845 return stack_push(ctx, &v);
848 static HRESULT interp_eqv(exec_ctx_t *ctx)
850 variant_val_t r, l;
851 VARIANT v;
852 HRESULT hres;
854 TRACE("\n");
856 hres = stack_pop_val(ctx, &r);
857 if(FAILED(hres))
858 return hres;
860 hres = stack_pop_val(ctx, &l);
861 if(SUCCEEDED(hres)) {
862 hres = VarEqv(l.v, r.v, &v);
863 release_val(&l);
865 release_val(&r);
866 if(FAILED(hres))
867 return hres;
869 return stack_push(ctx, &v);
872 static HRESULT interp_imp(exec_ctx_t *ctx)
874 variant_val_t r, l;
875 VARIANT v;
876 HRESULT hres;
878 TRACE("\n");
880 hres = stack_pop_val(ctx, &r);
881 if(FAILED(hres))
882 return hres;
884 hres = stack_pop_val(ctx, &l);
885 if(SUCCEEDED(hres)) {
886 hres = VarImp(l.v, r.v, &v);
887 release_val(&l);
889 release_val(&r);
890 if(FAILED(hres))
891 return hres;
893 return stack_push(ctx, &v);
896 static HRESULT cmp_oper(exec_ctx_t *ctx)
898 variant_val_t l, r;
899 HRESULT hres;
901 hres = stack_pop_val(ctx, &r);
902 if(FAILED(hres))
903 return hres;
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);
915 release_val(&r);
916 release_val(&l);
917 return hres;
920 static HRESULT interp_equal(exec_ctx_t *ctx)
922 VARIANT v;
923 HRESULT hres;
925 TRACE("\n");
927 hres = cmp_oper(ctx);
928 if(FAILED(hres))
929 return hres;
931 V_VT(&v) = VT_BOOL;
932 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
933 return stack_push(ctx, &v);
936 static HRESULT interp_nequal(exec_ctx_t *ctx)
938 VARIANT v;
939 HRESULT hres;
941 TRACE("\n");
943 hres = cmp_oper(ctx);
944 if(FAILED(hres))
945 return hres;
947 V_VT(&v) = VT_BOOL;
948 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
949 return stack_push(ctx, &v);
952 static HRESULT interp_gt(exec_ctx_t *ctx)
954 VARIANT v;
955 HRESULT hres;
957 TRACE("\n");
959 hres = cmp_oper(ctx);
960 if(FAILED(hres))
961 return hres;
963 V_VT(&v) = VT_BOOL;
964 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
965 return stack_push(ctx, &v);
968 static HRESULT interp_gteq(exec_ctx_t *ctx)
970 VARIANT v;
971 HRESULT hres;
973 TRACE("\n");
975 hres = cmp_oper(ctx);
976 if(FAILED(hres))
977 return hres;
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);
984 static HRESULT interp_lt(exec_ctx_t *ctx)
986 VARIANT v;
987 HRESULT hres;
989 TRACE("\n");
991 hres = cmp_oper(ctx);
992 if(FAILED(hres))
993 return hres;
995 V_VT(&v) = VT_BOOL;
996 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
997 return stack_push(ctx, &v);
1000 static HRESULT interp_lteq(exec_ctx_t *ctx)
1002 VARIANT v;
1003 HRESULT hres;
1005 TRACE("\n");
1007 hres = cmp_oper(ctx);
1008 if(FAILED(hres))
1009 return hres;
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);
1016 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1018 IObjectIdentity *identity;
1019 IUnknown *unk1, *unk2;
1020 HRESULT hres;
1022 if(disp1 == disp2) {
1023 *ret = VARIANT_TRUE;
1024 return S_OK;
1027 if(!disp1 || !disp2) {
1028 *ret = VARIANT_FALSE;
1029 return S_OK;
1032 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1033 if(FAILED(hres))
1034 return hres;
1036 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1037 if(FAILED(hres)) {
1038 IUnknown_Release(unk1);
1039 return hres;
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;
1055 IUnknown_Release(unk1);
1056 IUnknown_Release(unk2);
1057 return S_OK;
1060 static HRESULT interp_is(exec_ctx_t *ctx)
1062 IDispatch *l, *r;
1063 VARIANT v;
1064 HRESULT hres;
1066 TRACE("\n");
1068 hres = stack_pop_disp(ctx, &r);
1069 if(FAILED(hres))
1070 return hres;
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);
1079 if(r)
1080 IDispatch_Release(r);
1081 if(FAILED(hres))
1082 return hres;
1084 return stack_push(ctx, &v);
1087 static HRESULT interp_concat(exec_ctx_t *ctx)
1089 variant_val_t r, l;
1090 VARIANT v;
1091 HRESULT hres;
1093 TRACE("\n");
1095 hres = stack_pop_val(ctx, &r);
1096 if(FAILED(hres))
1097 return hres;
1099 hres = stack_pop_val(ctx, &l);
1100 if(SUCCEEDED(hres)) {
1101 hres = VarCat(l.v, r.v, &v);
1102 release_val(&l);
1104 release_val(&r);
1105 if(FAILED(hres))
1106 return hres;
1108 return stack_push(ctx, &v);
1111 static HRESULT interp_add(exec_ctx_t *ctx)
1113 variant_val_t r, l;
1114 VARIANT v;
1115 HRESULT hres;
1117 TRACE("\n");
1119 hres = stack_pop_val(ctx, &r);
1120 if(FAILED(hres))
1121 return hres;
1123 hres = stack_pop_val(ctx, &l);
1124 if(SUCCEEDED(hres)) {
1125 hres = VarAdd(l.v, r.v, &v);
1126 release_val(&l);
1128 release_val(&r);
1129 if(FAILED(hres))
1130 return hres;
1132 return stack_push(ctx, &v);
1135 static HRESULT interp_sub(exec_ctx_t *ctx)
1137 variant_val_t r, l;
1138 VARIANT v;
1139 HRESULT hres;
1141 TRACE("\n");
1143 hres = stack_pop_val(ctx, &r);
1144 if(FAILED(hres))
1145 return hres;
1147 hres = stack_pop_val(ctx, &l);
1148 if(SUCCEEDED(hres)) {
1149 hres = VarSub(l.v, r.v, &v);
1150 release_val(&l);
1152 release_val(&r);
1153 if(FAILED(hres))
1154 return hres;
1156 return stack_push(ctx, &v);
1159 static HRESULT interp_mod(exec_ctx_t *ctx)
1161 variant_val_t r, l;
1162 VARIANT v;
1163 HRESULT hres;
1165 TRACE("\n");
1167 hres = stack_pop_val(ctx, &r);
1168 if(FAILED(hres))
1169 return hres;
1171 hres = stack_pop_val(ctx, &l);
1172 if(SUCCEEDED(hres)) {
1173 hres = VarMod(l.v, r.v, &v);
1174 release_val(&l);
1176 release_val(&r);
1177 if(FAILED(hres))
1178 return hres;
1180 return stack_push(ctx, &v);
1183 static HRESULT interp_idiv(exec_ctx_t *ctx)
1185 variant_val_t r, l;
1186 VARIANT v;
1187 HRESULT hres;
1189 TRACE("\n");
1191 hres = stack_pop_val(ctx, &r);
1192 if(FAILED(hres))
1193 return hres;
1195 hres = stack_pop_val(ctx, &l);
1196 if(SUCCEEDED(hres)) {
1197 hres = VarIdiv(l.v, r.v, &v);
1198 release_val(&l);
1200 release_val(&r);
1201 if(FAILED(hres))
1202 return hres;
1204 return stack_push(ctx, &v);
1207 static HRESULT interp_div(exec_ctx_t *ctx)
1209 variant_val_t r, l;
1210 VARIANT v;
1211 HRESULT hres;
1213 TRACE("\n");
1215 hres = stack_pop_val(ctx, &r);
1216 if(FAILED(hres))
1217 return hres;
1219 hres = stack_pop_val(ctx, &l);
1220 if(SUCCEEDED(hres)) {
1221 hres = VarDiv(l.v, r.v, &v);
1222 release_val(&l);
1224 release_val(&r);
1225 if(FAILED(hres))
1226 return hres;
1228 return stack_push(ctx, &v);
1231 static HRESULT interp_mul(exec_ctx_t *ctx)
1233 variant_val_t r, l;
1234 VARIANT v;
1235 HRESULT hres;
1237 TRACE("\n");
1239 hres = stack_pop_val(ctx, &r);
1240 if(FAILED(hres))
1241 return hres;
1243 hres = stack_pop_val(ctx, &l);
1244 if(SUCCEEDED(hres)) {
1245 hres = VarMul(l.v, r.v, &v);
1246 release_val(&l);
1248 release_val(&r);
1249 if(FAILED(hres))
1250 return hres;
1252 return stack_push(ctx, &v);
1255 static HRESULT interp_exp(exec_ctx_t *ctx)
1257 variant_val_t r, l;
1258 VARIANT v;
1259 HRESULT hres;
1261 TRACE("\n");
1263 hres = stack_pop_val(ctx, &r);
1264 if(FAILED(hres))
1265 return hres;
1267 hres = stack_pop_val(ctx, &l);
1268 if(SUCCEEDED(hres)) {
1269 hres = VarPow(l.v, r.v, &v);
1270 release_val(&l);
1272 release_val(&r);
1273 if(FAILED(hres))
1274 return hres;
1276 return stack_push(ctx, &v);
1279 static HRESULT interp_neg(exec_ctx_t *ctx)
1281 variant_val_t val;
1282 VARIANT v;
1283 HRESULT hres;
1285 hres = stack_pop_val(ctx, &val);
1286 if(FAILED(hres))
1287 return hres;
1289 hres = VarNeg(val.v, &v);
1290 release_val(&val);
1291 if(FAILED(hres))
1292 return hres;
1294 return stack_push(ctx, &v);
1297 static const instr_func_t op_funcs[] = {
1298 #define X(x,n,a,b) interp_ ## x,
1299 OP_LIST
1300 #undef X
1303 static const unsigned op_move[] = {
1304 #define X(x,n,a,b) n,
1305 OP_LIST
1306 #undef X
1309 static void release_exec(exec_ctx_t *ctx)
1311 unsigned i;
1313 VariantClear(&ctx->ret_val);
1315 if(ctx->this_obj)
1316 IDispatch_Release(ctx->this_obj);
1318 if(ctx->args) {
1319 for(i=0; i < ctx->func->arg_cnt; i++)
1320 VariantClear(ctx->args+i);
1323 if(ctx->vars) {
1324 for(i=0; i < ctx->func->var_cnt; i++)
1325 VariantClear(ctx->vars+i);
1328 heap_free(ctx->args);
1329 heap_free(ctx->vars);
1330 heap_free(ctx->stack);
1333 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1335 exec_ctx_t exec = {func->code_ctx};
1336 vbsop_t op;
1337 HRESULT hres = S_OK;
1339 exec.code = func->code_ctx;
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;
1346 if(func->arg_cnt) {
1347 VARIANT *v;
1348 unsigned i;
1350 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1351 if(!exec.args) {
1352 release_exec(&exec);
1353 return E_OUTOFMEMORY;
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);
1366 if(FAILED(hres)) {
1367 release_exec(&exec);
1368 return hres;
1371 }else {
1372 exec.args = NULL;
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;
1381 }else {
1382 exec.vars = NULL;
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;
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);
1401 exec.instr = exec.code->instrs + func->code_off;
1402 exec.script = ctx;
1403 exec.func = func;
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;
1414 exec.instr += op_move[op];
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);
1421 if(SUCCEEDED(hres) && res) {
1422 *res = exec.ret_val;
1423 V_VT(&exec.ret_val) = VT_EMPTY;
1426 release_exec(&exec);
1428 return hres;