d3d8: Don't return a pointer to the implementation in IDirect3DVolume8Impl_QueryInter...
[wine/multimedia.git] / dlls / vbscript / interp.c
blob4be1d43081095dd741252a820bd131508880f39c
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);
27 typedef struct {
28 vbscode_t *code;
29 instr_t *instr;
30 script_ctx_t *script;
31 function_t *func;
32 IDispatch *this_obj;
34 VARIANT *args;
35 VARIANT *vars;
37 dynamic_var_t *dynamic_vars;
38 vbsheap_t heap;
40 BOOL resume_next;
42 unsigned stack_size;
43 unsigned top;
44 VARIANT *stack;
46 VARIANT ret_val;
47 } exec_ctx_t;
49 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
51 typedef enum {
52 REF_NONE,
53 REF_DISP,
54 REF_VAR,
55 REF_OBJ,
56 REF_CONST,
57 REF_FUNC
58 } ref_type_t;
60 typedef struct {
61 ref_type_t type;
62 union {
63 struct {
64 IDispatch *disp;
65 DISPID id;
66 } d;
67 VARIANT *v;
68 function_t *f;
69 IDispatch *obj;
70 } u;
71 } ref_t;
73 typedef struct {
74 VARIANT *v;
75 VARIANT store;
76 BOOL owned;
77 } variant_val_t;
79 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
81 while(var) {
82 if(!strcmpiW(var->name, name)) {
83 ref->type = var->is_const ? REF_CONST : REF_VAR;
84 ref->u.v = &var->v;
85 return TRUE;
88 var = var->next;
91 return FALSE;
94 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
96 named_item_t *item;
97 function_t *func;
98 unsigned i;
99 DISPID id;
100 HRESULT hres;
102 static const WCHAR errW[] = {'e','r','r',0};
104 if(invoke_type == VBDISP_LET
105 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
106 && !strcmpiW(name, ctx->func->name)) {
107 ref->type = REF_VAR;
108 ref->u.v = &ctx->ret_val;
109 return S_OK;
112 for(i=0; i < ctx->func->var_cnt; i++) {
113 if(!strcmpiW(ctx->func->vars[i].name, name)) {
114 ref->type = REF_VAR;
115 ref->u.v = ctx->vars+i;
116 return TRUE;
120 for(i=0; i < ctx->func->arg_cnt; i++) {
121 if(!strcmpiW(ctx->func->args[i].name, name)) {
122 ref->type = REF_VAR;
123 ref->u.v = ctx->args+i;
124 return S_OK;
128 if(lookup_dynamic_vars(ctx->func->type == FUNC_GLOBAL ? ctx->script->global_vars : ctx->dynamic_vars, name, ref))
129 return S_OK;
131 if(ctx->func->type != FUNC_GLOBAL) {
132 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
133 if(SUCCEEDED(hres)) {
134 ref->type = REF_DISP;
135 ref->u.d.disp = ctx->this_obj;
136 ref->u.d.id = id;
137 return S_OK;
141 if(ctx->func->type != FUNC_GLOBAL && lookup_dynamic_vars(ctx->script->global_vars, name, ref))
142 return S_OK;
144 for(func = ctx->script->global_funcs; func; func = func->next) {
145 if(!strcmpiW(func->name, name)) {
146 ref->type = REF_FUNC;
147 ref->u.f = func;
148 return S_OK;
152 if(!strcmpiW(name, errW)) {
153 ref->type = REF_OBJ;
154 ref->u.obj = (IDispatch*)&ctx->script->err_obj->IDispatchEx_iface;
155 return S_OK;
158 hres = vbdisp_get_id(ctx->script->global_obj, name, invoke_type, TRUE, &id);
159 if(SUCCEEDED(hres)) {
160 ref->type = REF_DISP;
161 ref->u.d.disp = (IDispatch*)&ctx->script->global_obj->IDispatchEx_iface;
162 ref->u.d.id = id;
163 return S_OK;
166 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
167 if((item->flags & SCRIPTITEM_GLOBALMEMBERS)) {
168 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
169 if(SUCCEEDED(hres)) {
170 ref->type = REF_DISP;
171 ref->u.d.disp = item->disp;
172 ref->u.d.id = id;
173 return S_OK;
177 if((item->flags & SCRIPTITEM_ISVISIBLE) && !strcmpiW(item->name, name)) {
178 if(!item->disp) {
179 IUnknown *unk;
181 hres = IActiveScriptSite_GetItemInfo(ctx->script->site, name, SCRIPTINFO_IUNKNOWN, &unk, NULL);
182 if(FAILED(hres)) {
183 WARN("GetItemInfo failed: %08x\n", hres);
184 continue;
187 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)&item->disp);
188 IUnknown_Release(unk);
189 if(FAILED(hres)) {
190 WARN("object does not implement IDispatch\n");
191 continue;
195 ref->type = REF_OBJ;
196 ref->u.obj = item->disp;
197 return S_OK;
201 ref->type = REF_NONE;
202 return S_OK;
205 static HRESULT add_dynamic_var(exec_ctx_t *ctx, const WCHAR *name, BOOL is_const, VARIANT *val, BOOL own_val)
207 dynamic_var_t *new_var;
208 vbsheap_t *heap;
209 WCHAR *str;
210 unsigned size;
211 HRESULT hres;
213 heap = ctx->func->type == FUNC_GLOBAL ? &ctx->script->heap : &ctx->heap;
215 new_var = vbsheap_alloc(heap, sizeof(*new_var));
216 if(!new_var)
217 return E_OUTOFMEMORY;
219 size = (strlenW(name)+1)*sizeof(WCHAR);
220 str = vbsheap_alloc(heap, size);
221 if(!str)
222 return E_OUTOFMEMORY;
223 memcpy(str, name, size);
224 new_var->name = str;
225 new_var->is_const = is_const;
227 if(own_val) {
228 new_var->v = *val;
229 }else {
230 hres = VariantCopy(&new_var->v, val);
231 if(FAILED(hres))
232 return hres;
235 if(ctx->func->type == FUNC_GLOBAL) {
236 new_var->next = ctx->script->global_vars;
237 ctx->script->global_vars = new_var;
238 }else {
239 new_var->next = ctx->dynamic_vars;
240 ctx->dynamic_vars = new_var;
243 return S_OK;
246 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
248 assert(ctx->top);
249 return ctx->stack + --ctx->top;
252 static inline VARIANT *stack_top(exec_ctx_t *ctx, unsigned n)
254 assert(ctx->top >= n);
255 return ctx->stack + (ctx->top-n-1);
258 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
260 if(ctx->stack_size == ctx->top) {
261 VARIANT *new_stack;
263 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2*sizeof(*ctx->stack));
264 if(!new_stack) {
265 VariantClear(v);
266 return E_OUTOFMEMORY;
269 ctx->stack = new_stack;
270 ctx->stack_size *= 2;
273 ctx->stack[ctx->top++] = *v;
274 return S_OK;
277 static void stack_popn(exec_ctx_t *ctx, unsigned n)
279 while(n--)
280 VariantClear(stack_pop(ctx));
283 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
285 VARIANT *var;
287 var = stack_pop(ctx);
289 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
290 v->owned = FALSE;
291 var = V_VARIANTREF(var);
292 }else {
293 v->owned = TRUE;
296 if(V_VT(var) == VT_DISPATCH) {
297 DISPPARAMS dp = {0};
298 HRESULT hres;
300 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
301 if(v->owned)
302 IDispatch_Release(V_DISPATCH(var));
303 if(FAILED(hres))
304 return hres;
306 v->owned = TRUE;
307 v->v = &v->store;
308 }else {
309 v->v = var;
312 return S_OK;
315 static inline void release_val(variant_val_t *v)
317 if(v->owned)
318 VariantClear(v->v);
321 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
323 VARIANT *v = stack_pop(ctx);
325 if(V_VT(v) == VT_DISPATCH) {
326 *ret = V_DISPATCH(v);
327 return S_OK;
330 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
331 FIXME("not supported type: %s\n", debugstr_variant(v));
332 VariantClear(v);
333 return E_FAIL;
336 v = V_BYREF(v);
337 if(V_VT(v) != VT_DISPATCH) {
338 FIXME("not disp %s\n", debugstr_variant(v));
339 return E_FAIL;
342 if(V_DISPATCH(v))
343 IDispatch_AddRef(V_DISPATCH(v));
344 *ret = V_DISPATCH(v);
345 return S_OK;
348 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
350 ctx->instr = ctx->code->instrs + addr;
353 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
355 dp->cArgs = arg_cnt;
356 dp->rgdispidNamedArgs = NULL;
357 dp->cNamedArgs = 0;
359 if(arg_cnt) {
360 VARIANT tmp;
361 unsigned i;
363 assert(ctx->top >= arg_cnt);
365 for(i=1; i*2 <= arg_cnt; i++) {
366 tmp = ctx->stack[ctx->top-i];
367 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
368 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
371 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
372 }else {
373 dp->rgvarg = NULL;
377 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
379 BSTR identifier = ctx->instr->arg1.bstr;
380 const unsigned arg_cnt = ctx->instr->arg2.uint;
381 DISPPARAMS dp;
382 ref_t ref;
383 HRESULT hres;
385 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
386 if(FAILED(hres))
387 return hres;
389 vbstack_to_dp(ctx, arg_cnt, &dp);
391 switch(ref.type) {
392 case REF_VAR:
393 case REF_CONST:
394 if(!res) {
395 FIXME("REF_VAR no res\n");
396 return E_NOTIMPL;
399 if(arg_cnt) {
400 FIXME("arguments not implemented\n");
401 return E_NOTIMPL;
404 V_VT(res) = VT_BYREF|VT_VARIANT;
405 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
406 break;
407 case REF_DISP:
408 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
409 if(FAILED(hres))
410 return hres;
411 break;
412 case REF_FUNC:
413 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
414 if(FAILED(hres))
415 return hres;
416 break;
417 case REF_OBJ:
418 if(arg_cnt) {
419 FIXME("arguments on object\n");
420 return E_NOTIMPL;
423 if(res) {
424 IDispatch_AddRef(ref.u.obj);
425 V_VT(res) = VT_DISPATCH;
426 V_DISPATCH(res) = ref.u.obj;
428 break;
429 case REF_NONE:
430 FIXME("%s not found\n", debugstr_w(identifier));
431 return DISP_E_UNKNOWNNAME;
434 stack_popn(ctx, arg_cnt);
435 return S_OK;
438 static HRESULT interp_icall(exec_ctx_t *ctx)
440 VARIANT v;
441 HRESULT hres;
443 TRACE("\n");
445 hres = do_icall(ctx, &v);
446 if(FAILED(hres))
447 return hres;
449 return stack_push(ctx, &v);
452 static HRESULT interp_icallv(exec_ctx_t *ctx)
454 TRACE("\n");
455 return do_icall(ctx, NULL);
458 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
460 const BSTR identifier = ctx->instr->arg1.bstr;
461 const unsigned arg_cnt = ctx->instr->arg2.uint;
462 IDispatch *obj;
463 DISPPARAMS dp;
464 DISPID id;
465 HRESULT hres;
467 hres = stack_pop_disp(ctx, &obj);
468 if(FAILED(hres))
469 return hres;
471 if(!obj) {
472 FIXME("NULL obj\n");
473 return E_FAIL;
476 vbstack_to_dp(ctx, arg_cnt, &dp);
478 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
479 if(SUCCEEDED(hres))
480 hres = disp_call(ctx->script, obj, id, &dp, res);
481 IDispatch_Release(obj);
482 if(FAILED(hres))
483 return hres;
485 stack_popn(ctx, arg_cnt);
486 return S_OK;
489 static HRESULT interp_mcall(exec_ctx_t *ctx)
491 VARIANT res;
492 HRESULT hres;
494 TRACE("\n");
496 hres = do_mcall(ctx, &res);
497 if(FAILED(hres))
498 return hres;
500 return stack_push(ctx, &res);
503 static HRESULT interp_mcallv(exec_ctx_t *ctx)
505 TRACE("\n");
507 return do_mcall(ctx, NULL);
510 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
512 ref_t ref;
513 HRESULT hres;
515 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
516 if(FAILED(hres))
517 return hres;
519 switch(ref.type) {
520 case REF_VAR: {
521 VARIANT *v = ref.u.v;
523 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
524 v = V_VARIANTREF(v);
526 if(own_val) {
527 VariantClear(v);
528 *v = *val;
529 hres = S_OK;
530 }else {
531 hres = VariantCopy(v, val);
533 break;
535 case REF_DISP:
536 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
537 if(own_val)
538 VariantClear(val);
539 break;
540 case REF_FUNC:
541 FIXME("functions not implemented\n");
542 return E_NOTIMPL;
543 case REF_OBJ:
544 FIXME("REF_OBJ\n");
545 return E_NOTIMPL;
546 case REF_CONST:
547 FIXME("REF_CONST\n");
548 return E_NOTIMPL;
549 case REF_NONE:
550 if(ctx->func->code_ctx->option_explicit) {
551 FIXME("throw exception\n");
552 hres = E_FAIL;
553 }else {
554 TRACE("creating variable %s\n", debugstr_w(name));
555 hres = add_dynamic_var(ctx, name, FALSE, val, own_val);
559 return hres;
562 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
564 const BSTR arg = ctx->instr->arg1.bstr;
565 const unsigned arg_cnt = ctx->instr->arg2.uint;
566 variant_val_t v;
567 HRESULT hres;
569 TRACE("%s\n", debugstr_w(arg));
571 if(arg_cnt) {
572 FIXME("arguments not supported\n");
573 return E_NOTIMPL;
576 hres = stack_pop_val(ctx, &v);
577 if(FAILED(hres))
578 return hres;
580 return assign_ident(ctx, arg, v.v, v.owned);
583 static HRESULT interp_set_ident(exec_ctx_t *ctx)
585 const BSTR arg = ctx->instr->arg1.bstr;
586 const unsigned arg_cnt = ctx->instr->arg2.uint;
587 IDispatch *disp;
588 VARIANT v;
589 HRESULT hres;
591 TRACE("%s\n", debugstr_w(arg));
593 if(arg_cnt) {
594 FIXME("arguments not supported\n");
595 return E_NOTIMPL;
598 hres = stack_pop_disp(ctx, &disp);
599 if(FAILED(hres))
600 return hres;
602 V_VT(&v) = VT_DISPATCH;
603 V_DISPATCH(&v) = disp;
604 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
607 static HRESULT interp_assign_member(exec_ctx_t *ctx)
609 BSTR identifier = ctx->instr->arg1.bstr;
610 const unsigned arg_cnt = ctx->instr->arg2.uint;
611 variant_val_t val;
612 IDispatch *obj;
613 DISPID id;
614 HRESULT hres;
616 TRACE("%s\n", debugstr_w(identifier));
618 if(arg_cnt) {
619 FIXME("arguments not supported\n");
620 return E_NOTIMPL;
623 hres = stack_pop_disp(ctx, &obj);
624 if(FAILED(hres))
625 return hres;
627 if(!obj) {
628 FIXME("NULL obj\n");
629 return E_FAIL;
632 hres = stack_pop_val(ctx, &val);
633 if(FAILED(hres)) {
634 IDispatch_Release(obj);
635 return hres;
638 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
639 if(SUCCEEDED(hres))
640 hres = disp_propput(ctx->script, obj, id, val.v);
642 release_val(&val);
643 IDispatch_Release(obj);
644 return hres;
647 static HRESULT interp_set_member(exec_ctx_t *ctx)
649 BSTR identifier = ctx->instr->arg1.bstr;
650 const unsigned arg_cnt = ctx->instr->arg2.uint;
651 IDispatch *obj, *val;
652 DISPID id;
653 HRESULT hres;
655 TRACE("%s\n", debugstr_w(identifier));
657 if(arg_cnt) {
658 FIXME("arguments not supported\n");
659 return E_NOTIMPL;
662 hres = stack_pop_disp(ctx, &obj);
663 if(FAILED(hres))
664 return hres;
666 if(!obj) {
667 FIXME("NULL obj\n");
668 return E_FAIL;
671 hres = stack_pop_disp(ctx, &val);
672 if(FAILED(hres)) {
673 IDispatch_Release(obj);
674 return hres;
677 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
678 if(SUCCEEDED(hres)) {
679 VARIANT v;
681 V_VT(&v) = VT_DISPATCH;
682 V_DISPATCH(&v) = val;
683 hres = disp_propput(ctx->script, obj, id, &v);
686 if(val)
687 IDispatch_Release(val);
688 IDispatch_Release(obj);
689 return hres;
692 static HRESULT interp_const(exec_ctx_t *ctx)
694 BSTR arg = ctx->instr->arg1.bstr;
695 variant_val_t val;
696 ref_t ref;
697 HRESULT hres;
699 TRACE("%s\n", debugstr_w(arg));
701 assert(ctx->func->type == FUNC_GLOBAL);
703 hres = lookup_identifier(ctx, arg, VBDISP_CALLGET, &ref);
704 if(FAILED(hres))
705 return hres;
707 if(ref.type != REF_NONE) {
708 FIXME("%s already defined\n", debugstr_w(arg));
709 return E_FAIL;
712 hres = stack_pop_val(ctx, &val);
713 if(FAILED(hres))
714 return hres;
716 return add_dynamic_var(ctx, arg, TRUE, val.v, val.owned);
719 static HRESULT interp_val(exec_ctx_t *ctx)
721 variant_val_t val;
722 VARIANT v;
723 HRESULT hres;
725 TRACE("\n");
727 hres = stack_pop_val(ctx, &val);
728 if(FAILED(hres))
729 return hres;
731 if(!val.owned) {
732 V_VT(&v) = VT_EMPTY;
733 hres = VariantCopy(&v, val.v);
734 if(FAILED(hres))
735 return hres;
738 return stack_push(ctx, val.owned ? val.v : &v);
741 static HRESULT interp_pop(exec_ctx_t *ctx)
743 const unsigned n = ctx->instr->arg1.uint;
745 TRACE("%u\n", n);
747 stack_popn(ctx, n);
748 return S_OK;
751 static HRESULT interp_new(exec_ctx_t *ctx)
753 const WCHAR *arg = ctx->instr->arg1.bstr;
754 class_desc_t *class_desc;
755 vbdisp_t *obj;
756 VARIANT v;
757 HRESULT hres;
759 TRACE("%s\n", debugstr_w(arg));
761 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
762 if(!strcmpiW(class_desc->name, arg))
763 break;
765 if(!class_desc) {
766 FIXME("Class %s not found\n", debugstr_w(arg));
767 return E_FAIL;
770 hres = create_vbdisp(class_desc, &obj);
771 if(FAILED(hres))
772 return hres;
774 V_VT(&v) = VT_DISPATCH;
775 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
776 return stack_push(ctx, &v);
779 static HRESULT interp_step(exec_ctx_t *ctx)
781 const BSTR ident = ctx->instr->arg2.bstr;
782 BOOL gteq_zero;
783 VARIANT zero;
784 ref_t ref;
785 HRESULT hres;
787 TRACE("%s\n", debugstr_w(ident));
789 V_VT(&zero) = VT_I2;
790 V_I2(&zero) = 0;
791 hres = VarCmp(stack_top(ctx, 0), &zero, ctx->script->lcid, 0);
792 if(FAILED(hres))
793 return hres;
795 gteq_zero = hres == VARCMP_GT || hres == VARCMP_EQ;
797 hres = lookup_identifier(ctx, ident, VBDISP_ANY, &ref);
798 if(FAILED(hres))
799 return hres;
801 if(ref.type != REF_VAR) {
802 FIXME("%s is not REF_VAR\n", debugstr_w(ident));
803 return E_FAIL;
806 hres = VarCmp(ref.u.v, stack_top(ctx, 1), ctx->script->lcid, 0);
807 if(FAILED(hres))
808 return hres;
810 if(hres == VARCMP_EQ || hres == (gteq_zero ? VARCMP_LT : VARCMP_GT))
811 ctx->instr++;
812 else
813 instr_jmp(ctx, ctx->instr->arg1.uint);
814 return S_OK;
817 static HRESULT interp_jmp(exec_ctx_t *ctx)
819 const unsigned arg = ctx->instr->arg1.uint;
821 TRACE("%u\n", arg);
823 instr_jmp(ctx, arg);
824 return S_OK;
827 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
829 const unsigned arg = ctx->instr->arg1.uint;
830 variant_val_t val;
831 HRESULT hres;
833 TRACE("%u\n", arg);
835 hres = stack_pop_val(ctx, &val);
836 if(FAILED(hres))
837 return hres;
839 if(V_VT(val.v) != VT_BOOL) {
840 FIXME("unsupported for %s\n", debugstr_variant(val.v));
841 release_val(&val);
842 return E_NOTIMPL;
845 if(V_BOOL(val.v))
846 ctx->instr++;
847 else
848 instr_jmp(ctx, ctx->instr->arg1.uint);
849 return S_OK;
852 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
854 const unsigned arg = ctx->instr->arg1.uint;
855 variant_val_t val;
856 HRESULT hres;
858 TRACE("%u\n", arg);
860 hres = stack_pop_val(ctx, &val);
861 if(FAILED(hres))
862 return hres;
864 if(V_VT(val.v) != VT_BOOL) {
865 FIXME("unsupported for %s\n", debugstr_variant(val.v));
866 release_val(&val);
867 return E_NOTIMPL;
870 if(V_BOOL(val.v))
871 instr_jmp(ctx, ctx->instr->arg1.uint);
872 else
873 ctx->instr++;
874 return S_OK;
877 static HRESULT interp_ret(exec_ctx_t *ctx)
879 TRACE("\n");
881 ctx->instr = NULL;
882 return S_OK;
885 static HRESULT interp_stop(exec_ctx_t *ctx)
887 WARN("\n");
889 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
890 return S_OK;
893 static HRESULT interp_me(exec_ctx_t *ctx)
895 VARIANT v;
897 TRACE("\n");
899 IDispatch_AddRef(ctx->this_obj);
900 V_VT(&v) = VT_DISPATCH;
901 V_DISPATCH(&v) = ctx->this_obj;
902 return stack_push(ctx, &v);
905 static HRESULT interp_bool(exec_ctx_t *ctx)
907 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
908 VARIANT v;
910 TRACE("%s\n", arg ? "true" : "false");
912 V_VT(&v) = VT_BOOL;
913 V_BOOL(&v) = arg;
914 return stack_push(ctx, &v);
917 static HRESULT interp_errmode(exec_ctx_t *ctx)
919 const int err_mode = ctx->instr->arg1.uint;
921 TRACE("%d\n", err_mode);
923 ctx->resume_next = err_mode;
924 return S_OK;
927 static HRESULT interp_string(exec_ctx_t *ctx)
929 VARIANT v;
931 TRACE("\n");
933 V_VT(&v) = VT_BSTR;
934 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
935 if(!V_BSTR(&v))
936 return E_OUTOFMEMORY;
938 return stack_push(ctx, &v);
941 static HRESULT interp_long(exec_ctx_t *ctx)
943 const LONG arg = ctx->instr->arg1.lng;
944 VARIANT v;
946 TRACE("%d\n", arg);
948 V_VT(&v) = VT_I4;
949 V_I4(&v) = arg;
950 return stack_push(ctx, &v);
953 static HRESULT interp_short(exec_ctx_t *ctx)
955 const LONG arg = ctx->instr->arg1.lng;
956 VARIANT v;
958 TRACE("%d\n", arg);
960 V_VT(&v) = VT_I2;
961 V_I2(&v) = arg;
962 return stack_push(ctx, &v);
965 static HRESULT interp_double(exec_ctx_t *ctx)
967 const DOUBLE *arg = ctx->instr->arg1.dbl;
968 VARIANT v;
970 TRACE("%lf\n", *arg);
972 V_VT(&v) = VT_R8;
973 V_R8(&v) = *arg;
974 return stack_push(ctx, &v);
977 static HRESULT interp_empty(exec_ctx_t *ctx)
979 VARIANT v;
981 TRACE("\n");
983 V_VT(&v) = VT_EMPTY;
984 return stack_push(ctx, &v);
987 static HRESULT interp_null(exec_ctx_t *ctx)
989 VARIANT v;
991 TRACE("\n");
993 V_VT(&v) = VT_NULL;
994 return stack_push(ctx, &v);
997 static HRESULT interp_nothing(exec_ctx_t *ctx)
999 VARIANT v;
1001 TRACE("\n");
1003 V_VT(&v) = VT_DISPATCH;
1004 V_DISPATCH(&v) = NULL;
1005 return stack_push(ctx, &v);
1008 static HRESULT interp_not(exec_ctx_t *ctx)
1010 variant_val_t val;
1011 VARIANT v;
1012 HRESULT hres;
1014 TRACE("\n");
1016 hres = stack_pop_val(ctx, &val);
1017 if(FAILED(hres))
1018 return hres;
1020 hres = VarNot(val.v, &v);
1021 release_val(&val);
1022 if(FAILED(hres))
1023 return hres;
1025 return stack_push(ctx, &v);
1028 static HRESULT interp_and(exec_ctx_t *ctx)
1030 variant_val_t r, l;
1031 VARIANT v;
1032 HRESULT hres;
1034 TRACE("\n");
1036 hres = stack_pop_val(ctx, &r);
1037 if(FAILED(hres))
1038 return hres;
1040 hres = stack_pop_val(ctx, &l);
1041 if(SUCCEEDED(hres)) {
1042 hres = VarAnd(l.v, r.v, &v);
1043 release_val(&l);
1045 release_val(&r);
1046 if(FAILED(hres))
1047 return hres;
1049 return stack_push(ctx, &v);
1052 static HRESULT interp_or(exec_ctx_t *ctx)
1054 variant_val_t r, l;
1055 VARIANT v;
1056 HRESULT hres;
1058 TRACE("\n");
1060 hres = stack_pop_val(ctx, &r);
1061 if(FAILED(hres))
1062 return hres;
1064 hres = stack_pop_val(ctx, &l);
1065 if(SUCCEEDED(hres)) {
1066 hres = VarOr(l.v, r.v, &v);
1067 release_val(&l);
1069 release_val(&r);
1070 if(FAILED(hres))
1071 return hres;
1073 return stack_push(ctx, &v);
1076 static HRESULT interp_xor(exec_ctx_t *ctx)
1078 variant_val_t r, l;
1079 VARIANT v;
1080 HRESULT hres;
1082 TRACE("\n");
1084 hres = stack_pop_val(ctx, &r);
1085 if(FAILED(hres))
1086 return hres;
1088 hres = stack_pop_val(ctx, &l);
1089 if(SUCCEEDED(hres)) {
1090 hres = VarXor(l.v, r.v, &v);
1091 release_val(&l);
1093 release_val(&r);
1094 if(FAILED(hres))
1095 return hres;
1097 return stack_push(ctx, &v);
1100 static HRESULT interp_eqv(exec_ctx_t *ctx)
1102 variant_val_t r, l;
1103 VARIANT v;
1104 HRESULT hres;
1106 TRACE("\n");
1108 hres = stack_pop_val(ctx, &r);
1109 if(FAILED(hres))
1110 return hres;
1112 hres = stack_pop_val(ctx, &l);
1113 if(SUCCEEDED(hres)) {
1114 hres = VarEqv(l.v, r.v, &v);
1115 release_val(&l);
1117 release_val(&r);
1118 if(FAILED(hres))
1119 return hres;
1121 return stack_push(ctx, &v);
1124 static HRESULT interp_imp(exec_ctx_t *ctx)
1126 variant_val_t r, l;
1127 VARIANT v;
1128 HRESULT hres;
1130 TRACE("\n");
1132 hres = stack_pop_val(ctx, &r);
1133 if(FAILED(hres))
1134 return hres;
1136 hres = stack_pop_val(ctx, &l);
1137 if(SUCCEEDED(hres)) {
1138 hres = VarImp(l.v, r.v, &v);
1139 release_val(&l);
1141 release_val(&r);
1142 if(FAILED(hres))
1143 return hres;
1145 return stack_push(ctx, &v);
1148 static HRESULT cmp_oper(exec_ctx_t *ctx)
1150 variant_val_t l, r;
1151 HRESULT hres;
1153 hres = stack_pop_val(ctx, &r);
1154 if(FAILED(hres))
1155 return hres;
1157 hres = stack_pop_val(ctx, &l);
1158 if(SUCCEEDED(hres)) {
1159 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
1160 FIXME("comparing nulls is not implemented\n");
1161 hres = E_NOTIMPL;
1162 }else {
1163 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
1167 release_val(&r);
1168 release_val(&l);
1169 return hres;
1172 static HRESULT interp_equal(exec_ctx_t *ctx)
1174 VARIANT v;
1175 HRESULT hres;
1177 TRACE("\n");
1179 hres = cmp_oper(ctx);
1180 if(FAILED(hres))
1181 return hres;
1183 V_VT(&v) = VT_BOOL;
1184 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1185 return stack_push(ctx, &v);
1188 static HRESULT interp_nequal(exec_ctx_t *ctx)
1190 VARIANT v;
1191 HRESULT hres;
1193 TRACE("\n");
1195 hres = cmp_oper(ctx);
1196 if(FAILED(hres))
1197 return hres;
1199 V_VT(&v) = VT_BOOL;
1200 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1201 return stack_push(ctx, &v);
1204 static HRESULT interp_gt(exec_ctx_t *ctx)
1206 VARIANT v;
1207 HRESULT hres;
1209 TRACE("\n");
1211 hres = cmp_oper(ctx);
1212 if(FAILED(hres))
1213 return hres;
1215 V_VT(&v) = VT_BOOL;
1216 V_BOOL(&v) = hres == VARCMP_GT ? VARIANT_TRUE : VARIANT_FALSE;
1217 return stack_push(ctx, &v);
1220 static HRESULT interp_gteq(exec_ctx_t *ctx)
1222 VARIANT v;
1223 HRESULT hres;
1225 TRACE("\n");
1227 hres = cmp_oper(ctx);
1228 if(FAILED(hres))
1229 return hres;
1231 V_VT(&v) = VT_BOOL;
1232 V_BOOL(&v) = hres == VARCMP_GT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1233 return stack_push(ctx, &v);
1236 static HRESULT interp_lt(exec_ctx_t *ctx)
1238 VARIANT v;
1239 HRESULT hres;
1241 TRACE("\n");
1243 hres = cmp_oper(ctx);
1244 if(FAILED(hres))
1245 return hres;
1247 V_VT(&v) = VT_BOOL;
1248 V_BOOL(&v) = hres == VARCMP_LT ? VARIANT_TRUE : VARIANT_FALSE;
1249 return stack_push(ctx, &v);
1252 static HRESULT interp_lteq(exec_ctx_t *ctx)
1254 VARIANT v;
1255 HRESULT hres;
1257 TRACE("\n");
1259 hres = cmp_oper(ctx);
1260 if(FAILED(hres))
1261 return hres;
1263 V_VT(&v) = VT_BOOL;
1264 V_BOOL(&v) = hres == VARCMP_LT || hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
1265 return stack_push(ctx, &v);
1268 static HRESULT disp_cmp(IDispatch *disp1, IDispatch *disp2, VARIANT_BOOL *ret)
1270 IObjectIdentity *identity;
1271 IUnknown *unk1, *unk2;
1272 HRESULT hres;
1274 if(disp1 == disp2) {
1275 *ret = VARIANT_TRUE;
1276 return S_OK;
1279 if(!disp1 || !disp2) {
1280 *ret = VARIANT_FALSE;
1281 return S_OK;
1284 hres = IDispatch_QueryInterface(disp1, &IID_IUnknown, (void**)&unk1);
1285 if(FAILED(hres))
1286 return hres;
1288 hres = IDispatch_QueryInterface(disp2, &IID_IUnknown, (void**)&unk2);
1289 if(FAILED(hres)) {
1290 IUnknown_Release(unk1);
1291 return hres;
1294 if(unk1 == unk2) {
1295 *ret = VARIANT_TRUE;
1296 }else {
1297 hres = IUnknown_QueryInterface(unk1, &IID_IObjectIdentity, (void**)&identity);
1298 if(SUCCEEDED(hres)) {
1299 hres = IObjectIdentity_IsEqualObject(identity, unk2);
1300 IObjectIdentity_Release(identity);
1301 *ret = hres == S_OK ? VARIANT_TRUE : VARIANT_FALSE;
1302 }else {
1303 *ret = VARIANT_FALSE;
1307 IUnknown_Release(unk1);
1308 IUnknown_Release(unk2);
1309 return S_OK;
1312 static HRESULT interp_is(exec_ctx_t *ctx)
1314 IDispatch *l, *r;
1315 VARIANT v;
1316 HRESULT hres;
1318 TRACE("\n");
1320 hres = stack_pop_disp(ctx, &r);
1321 if(FAILED(hres))
1322 return hres;
1324 hres = stack_pop_disp(ctx, &l);
1325 if(SUCCEEDED(hres)) {
1326 V_VT(&v) = VT_BOOL;
1327 hres = disp_cmp(l, r, &V_BOOL(&v));
1328 if(l)
1329 IDispatch_Release(l);
1331 if(r)
1332 IDispatch_Release(r);
1333 if(FAILED(hres))
1334 return hres;
1336 return stack_push(ctx, &v);
1339 static HRESULT interp_concat(exec_ctx_t *ctx)
1341 variant_val_t r, l;
1342 VARIANT v;
1343 HRESULT hres;
1345 TRACE("\n");
1347 hres = stack_pop_val(ctx, &r);
1348 if(FAILED(hres))
1349 return hres;
1351 hres = stack_pop_val(ctx, &l);
1352 if(SUCCEEDED(hres)) {
1353 hres = VarCat(l.v, r.v, &v);
1354 release_val(&l);
1356 release_val(&r);
1357 if(FAILED(hres))
1358 return hres;
1360 return stack_push(ctx, &v);
1363 static HRESULT interp_add(exec_ctx_t *ctx)
1365 variant_val_t r, l;
1366 VARIANT v;
1367 HRESULT hres;
1369 TRACE("\n");
1371 hres = stack_pop_val(ctx, &r);
1372 if(FAILED(hres))
1373 return hres;
1375 hres = stack_pop_val(ctx, &l);
1376 if(SUCCEEDED(hres)) {
1377 hres = VarAdd(l.v, r.v, &v);
1378 release_val(&l);
1380 release_val(&r);
1381 if(FAILED(hres))
1382 return hres;
1384 return stack_push(ctx, &v);
1387 static HRESULT interp_sub(exec_ctx_t *ctx)
1389 variant_val_t r, l;
1390 VARIANT v;
1391 HRESULT hres;
1393 TRACE("\n");
1395 hres = stack_pop_val(ctx, &r);
1396 if(FAILED(hres))
1397 return hres;
1399 hres = stack_pop_val(ctx, &l);
1400 if(SUCCEEDED(hres)) {
1401 hres = VarSub(l.v, r.v, &v);
1402 release_val(&l);
1404 release_val(&r);
1405 if(FAILED(hres))
1406 return hres;
1408 return stack_push(ctx, &v);
1411 static HRESULT interp_mod(exec_ctx_t *ctx)
1413 variant_val_t r, l;
1414 VARIANT v;
1415 HRESULT hres;
1417 TRACE("\n");
1419 hres = stack_pop_val(ctx, &r);
1420 if(FAILED(hres))
1421 return hres;
1423 hres = stack_pop_val(ctx, &l);
1424 if(SUCCEEDED(hres)) {
1425 hres = VarMod(l.v, r.v, &v);
1426 release_val(&l);
1428 release_val(&r);
1429 if(FAILED(hres))
1430 return hres;
1432 return stack_push(ctx, &v);
1435 static HRESULT interp_idiv(exec_ctx_t *ctx)
1437 variant_val_t r, l;
1438 VARIANT v;
1439 HRESULT hres;
1441 TRACE("\n");
1443 hres = stack_pop_val(ctx, &r);
1444 if(FAILED(hres))
1445 return hres;
1447 hres = stack_pop_val(ctx, &l);
1448 if(SUCCEEDED(hres)) {
1449 hres = VarIdiv(l.v, r.v, &v);
1450 release_val(&l);
1452 release_val(&r);
1453 if(FAILED(hres))
1454 return hres;
1456 return stack_push(ctx, &v);
1459 static HRESULT interp_div(exec_ctx_t *ctx)
1461 variant_val_t r, l;
1462 VARIANT v;
1463 HRESULT hres;
1465 TRACE("\n");
1467 hres = stack_pop_val(ctx, &r);
1468 if(FAILED(hres))
1469 return hres;
1471 hres = stack_pop_val(ctx, &l);
1472 if(SUCCEEDED(hres)) {
1473 hres = VarDiv(l.v, r.v, &v);
1474 release_val(&l);
1476 release_val(&r);
1477 if(FAILED(hres))
1478 return hres;
1480 return stack_push(ctx, &v);
1483 static HRESULT interp_mul(exec_ctx_t *ctx)
1485 variant_val_t r, l;
1486 VARIANT v;
1487 HRESULT hres;
1489 TRACE("\n");
1491 hres = stack_pop_val(ctx, &r);
1492 if(FAILED(hres))
1493 return hres;
1495 hres = stack_pop_val(ctx, &l);
1496 if(SUCCEEDED(hres)) {
1497 hres = VarMul(l.v, r.v, &v);
1498 release_val(&l);
1500 release_val(&r);
1501 if(FAILED(hres))
1502 return hres;
1504 return stack_push(ctx, &v);
1507 static HRESULT interp_exp(exec_ctx_t *ctx)
1509 variant_val_t r, l;
1510 VARIANT v;
1511 HRESULT hres;
1513 TRACE("\n");
1515 hres = stack_pop_val(ctx, &r);
1516 if(FAILED(hres))
1517 return hres;
1519 hres = stack_pop_val(ctx, &l);
1520 if(SUCCEEDED(hres)) {
1521 hres = VarPow(l.v, r.v, &v);
1522 release_val(&l);
1524 release_val(&r);
1525 if(FAILED(hres))
1526 return hres;
1528 return stack_push(ctx, &v);
1531 static HRESULT interp_neg(exec_ctx_t *ctx)
1533 variant_val_t val;
1534 VARIANT v;
1535 HRESULT hres;
1537 hres = stack_pop_val(ctx, &val);
1538 if(FAILED(hres))
1539 return hres;
1541 hres = VarNeg(val.v, &v);
1542 release_val(&val);
1543 if(FAILED(hres))
1544 return hres;
1546 return stack_push(ctx, &v);
1549 static HRESULT interp_incc(exec_ctx_t *ctx)
1551 const BSTR ident = ctx->instr->arg1.bstr;
1552 VARIANT v;
1553 ref_t ref;
1554 HRESULT hres;
1556 TRACE("\n");
1558 hres = lookup_identifier(ctx, ident, VBDISP_LET, &ref);
1559 if(FAILED(hres))
1560 return hres;
1562 if(ref.type != REF_VAR) {
1563 FIXME("ref.type is not REF_VAR\n");
1564 return E_FAIL;
1567 hres = VarAdd(stack_top(ctx, 0), ref.u.v, &v);
1568 if(FAILED(hres))
1569 return hres;
1571 VariantClear(ref.u.v);
1572 *ref.u.v = v;
1573 return S_OK;
1576 static const instr_func_t op_funcs[] = {
1577 #define X(x,n,a,b) interp_ ## x,
1578 OP_LIST
1579 #undef X
1582 static const unsigned op_move[] = {
1583 #define X(x,n,a,b) n,
1584 OP_LIST
1585 #undef X
1588 void release_dynamic_vars(dynamic_var_t *var)
1590 while(var) {
1591 VariantClear(&var->v);
1592 var = var->next;
1596 static void release_exec(exec_ctx_t *ctx)
1598 unsigned i;
1600 VariantClear(&ctx->ret_val);
1601 release_dynamic_vars(ctx->dynamic_vars);
1603 if(ctx->this_obj)
1604 IDispatch_Release(ctx->this_obj);
1606 if(ctx->args) {
1607 for(i=0; i < ctx->func->arg_cnt; i++)
1608 VariantClear(ctx->args+i);
1611 if(ctx->vars) {
1612 for(i=0; i < ctx->func->var_cnt; i++)
1613 VariantClear(ctx->vars+i);
1616 vbsheap_free(&ctx->heap);
1617 heap_free(ctx->args);
1618 heap_free(ctx->vars);
1619 heap_free(ctx->stack);
1622 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1624 exec_ctx_t exec = {func->code_ctx};
1625 vbsop_t op;
1626 HRESULT hres = S_OK;
1628 exec.code = func->code_ctx;
1630 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1631 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1632 return E_FAIL;
1635 vbsheap_init(&exec.heap);
1637 if(func->arg_cnt) {
1638 VARIANT *v;
1639 unsigned i;
1641 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1642 if(!exec.args) {
1643 release_exec(&exec);
1644 return E_OUTOFMEMORY;
1647 for(i=0; i < func->arg_cnt; i++) {
1648 v = get_arg(dp, i);
1649 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1650 if(func->args[i].by_ref)
1651 exec.args[i] = *v;
1652 else
1653 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1654 }else {
1655 hres = VariantCopy(exec.args+i, v);
1657 if(FAILED(hres)) {
1658 release_exec(&exec);
1659 return hres;
1662 }else {
1663 exec.args = NULL;
1666 if(func->var_cnt) {
1667 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1668 if(!exec.vars) {
1669 release_exec(&exec);
1670 return E_OUTOFMEMORY;
1672 }else {
1673 exec.vars = NULL;
1676 exec.stack_size = 16;
1677 exec.top = 0;
1678 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1679 if(!exec.stack) {
1680 release_exec(&exec);
1681 return E_OUTOFMEMORY;
1684 if(this_obj)
1685 exec.this_obj = this_obj;
1686 else if (ctx->host_global)
1687 exec.this_obj = ctx->host_global;
1688 else
1689 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1690 IDispatch_AddRef(exec.this_obj);
1692 exec.instr = exec.code->instrs + func->code_off;
1693 exec.script = ctx;
1694 exec.func = func;
1696 while(exec.instr) {
1697 op = exec.instr->op;
1698 hres = op_funcs[op](&exec);
1699 if(FAILED(hres)) {
1700 if(exec.resume_next)
1701 FIXME("Failed %08x in resume next mode\n", hres);
1702 else
1703 WARN("Failed %08x\n", hres);
1704 stack_popn(&exec, exec.top);
1705 break;
1708 exec.instr += op_move[op];
1711 assert(!exec.top);
1712 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1713 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1715 if(SUCCEEDED(hres) && res) {
1716 *res = exec.ret_val;
1717 V_VT(&exec.ret_val) = VT_EMPTY;
1720 release_exec(&exec);
1721 return hres;