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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
45 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
72 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
75 if(!strcmpiW(var->name, name)) {
87 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
95 if(invoke_type == VBDISP_LET && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET)
96 && !strcmpiW(name, ctx->func->name)) {
98 ref->u.v = &ctx->ret_val;
102 for(i=0; i < ctx->func->var_cnt; i++) {
103 if(!strcmpiW(ctx->func->vars[i].name, name)) {
105 ref->u.v = ctx->vars+i;
110 for(i=0; i < ctx->func->arg_cnt; i++) {
111 if(!strcmpiW(ctx->func->args[i].name, name)) {
113 ref->u.v = ctx->args+i;
118 hres = disp_get_id(ctx->this_obj, name, invoke_type, TRUE, &id);
119 if(SUCCEEDED(hres)) {
120 ref->type = REF_DISP;
121 ref->u.d.disp = ctx->this_obj;
126 if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
129 for(func = ctx->script->global_funcs; func; func = func->next) {
130 if(!strcmpiW(func->name, name)) {
131 ref->type = REF_FUNC;
137 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
138 if((item->flags & SCRIPTITEM_GLOBALMEMBERS) && item->disp != ctx->this_obj) {
139 hres = disp_get_id(item->disp, name, invoke_type, FALSE, &id);
140 if(SUCCEEDED(hres)) {
141 ref->type = REF_DISP;
142 ref->u.d.disp = item->disp;
149 if(!ctx->func->code_ctx->option_explicit)
150 FIXME("create an attempt to set\n");
152 ref->type = REF_NONE;
156 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
159 return ctx->stack + --ctx->top;
162 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
164 if(ctx->stack_size == ctx->top) {
167 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
170 return E_OUTOFMEMORY;
173 ctx->stack = new_stack;
174 ctx->stack_size *= 2;
177 ctx->stack[ctx->top++] = *v;
181 static void stack_popn(exec_ctx_t *ctx, unsigned n)
184 VariantClear(stack_pop(ctx));
187 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
191 var = stack_pop(ctx);
193 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
195 var = V_VARIANTREF(var);
200 if(V_VT(var) == VT_DISPATCH) {
201 FIXME("got dispatch - get its default value\n");
210 static inline void release_val(variant_val_t *v)
216 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
218 VARIANT *v = stack_pop(ctx);
220 if(V_VT(v) == VT_DISPATCH) {
221 *ret = V_DISPATCH(v);
225 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
226 FIXME("not supported type: %s\n", debugstr_variant(v));
232 if(V_VT(v) != VT_DISPATCH) {
233 FIXME("not disp %s\n", debugstr_variant(v));
238 IDispatch_AddRef(V_DISPATCH(v));
239 *ret = V_DISPATCH(v);
243 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
245 ctx->instr = ctx->code->instrs + addr;
248 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
251 dp->rgdispidNamedArgs = NULL;
258 assert(ctx->top >= arg_cnt);
260 for(i=1; i*2 <= arg_cnt; i++) {
261 tmp = ctx->stack[ctx->top-i];
262 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
263 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
266 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
272 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
274 BSTR identifier = ctx->instr->arg1.bstr;
275 const unsigned arg_cnt = ctx->instr->arg2.uint;
280 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
284 vbstack_to_dp(ctx, arg_cnt, &dp);
289 FIXME("REF_VAR no res\n");
294 FIXME("arguments not implemented\n");
298 V_VT(res) = VT_BYREF|VT_VARIANT;
299 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
302 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
307 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
312 FIXME("%s not found\n", debugstr_w(identifier));
313 return DISP_E_UNKNOWNNAME;
316 stack_popn(ctx, arg_cnt);
320 static HRESULT interp_icall(exec_ctx_t *ctx)
327 hres = do_icall(ctx, &v);
331 return stack_push(ctx, &v);
334 static HRESULT interp_icallv(exec_ctx_t *ctx)
337 return do_icall(ctx, NULL);
340 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
342 const BSTR identifier = ctx->instr->arg1.bstr;
343 const unsigned arg_cnt = ctx->instr->arg2.uint;
349 hres = stack_pop_disp(ctx, &obj);
358 vbstack_to_dp(ctx, arg_cnt, &dp);
360 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
362 hres = disp_call(ctx->script, obj, id, &dp, res);
363 IDispatch_Release(obj);
367 stack_popn(ctx, arg_cnt);
371 static HRESULT interp_mcall(exec_ctx_t *ctx)
378 hres = do_mcall(ctx, &res);
382 return stack_push(ctx, &res);
385 static HRESULT interp_mcallv(exec_ctx_t *ctx)
389 return do_mcall(ctx, NULL);
392 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
397 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
403 VARIANT *v = ref.u.v;
405 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
413 hres = VariantCopy(v, val);
418 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
423 FIXME("functions not implemented\n");
426 FIXME("%s not found\n", debugstr_w(name));
429 return DISP_E_UNKNOWNNAME;
435 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
437 const BSTR arg = ctx->instr->arg1.bstr;
441 TRACE("%s\n", debugstr_w(arg));
443 hres = stack_pop_val(ctx, &v);
447 return assign_ident(ctx, arg, v.v, v.owned);
450 static HRESULT interp_set_ident(exec_ctx_t *ctx)
452 const BSTR arg = ctx->instr->arg1.bstr;
457 TRACE("%s\n", debugstr_w(arg));
459 hres = stack_pop_disp(ctx, &disp);
463 V_VT(&v) = VT_DISPATCH;
464 V_DISPATCH(&v) = disp;
465 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
468 static HRESULT interp_assign_member(exec_ctx_t *ctx)
470 BSTR identifier = ctx->instr->arg1.bstr;
476 TRACE("%s\n", debugstr_w(identifier));
478 hres = stack_pop_disp(ctx, &obj);
487 hres = stack_pop_val(ctx, &val);
489 IDispatch_Release(obj);
493 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
495 hres = disp_propput(ctx->script, obj, id, val.v);
498 IDispatch_Release(obj);
502 static HRESULT interp_set_member(exec_ctx_t *ctx)
504 BSTR identifier = ctx->instr->arg1.bstr;
505 FIXME("%s\n", debugstr_w(identifier));
509 static HRESULT interp_new(exec_ctx_t *ctx)
511 const WCHAR *arg = ctx->instr->arg1.bstr;
512 class_desc_t *class_desc;
517 TRACE("%s\n", debugstr_w(arg));
519 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
520 if(!strcmpiW(class_desc->name, arg))
524 FIXME("Class %s not found\n", debugstr_w(arg));
528 hres = create_vbdisp(class_desc, &obj);
532 V_VT(&v) = VT_DISPATCH;
533 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
534 return stack_push(ctx, &v);
537 static HRESULT interp_jmp(exec_ctx_t *ctx)
539 const unsigned arg = ctx->instr->arg1.uint;
547 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
549 const unsigned arg = ctx->instr->arg1.uint;
555 hres = stack_pop_val(ctx, &val);
559 if(V_VT(val.v) != VT_BOOL) {
560 FIXME("unsupported for %s\n", debugstr_variant(val.v));
568 instr_jmp(ctx, ctx->instr->arg1.uint);
572 static HRESULT interp_ret(exec_ctx_t *ctx)
580 static HRESULT interp_stop(exec_ctx_t *ctx)
584 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
588 static HRESULT interp_bool(exec_ctx_t *ctx)
590 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
593 TRACE("%s\n", arg ? "true" : "false");
597 return stack_push(ctx, &v);
600 static HRESULT interp_string(exec_ctx_t *ctx)
607 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
609 return E_OUTOFMEMORY;
611 return stack_push(ctx, &v);
614 static HRESULT interp_long(exec_ctx_t *ctx)
616 const LONG arg = ctx->instr->arg1.lng;
623 return stack_push(ctx, &v);
626 static HRESULT interp_short(exec_ctx_t *ctx)
628 const LONG arg = ctx->instr->arg1.lng;
635 return stack_push(ctx, &v);
638 static HRESULT interp_double(exec_ctx_t *ctx)
640 const DOUBLE *arg = ctx->instr->arg1.dbl;
643 TRACE("%lf\n", *arg);
647 return stack_push(ctx, &v);
650 static HRESULT interp_empty(exec_ctx_t *ctx)
657 return stack_push(ctx, &v);
660 static HRESULT interp_null(exec_ctx_t *ctx)
667 return stack_push(ctx, &v);
670 static HRESULT interp_nothing(exec_ctx_t *ctx)
676 V_VT(&v) = VT_DISPATCH;
677 V_DISPATCH(&v) = NULL;
678 return stack_push(ctx, &v);
681 static HRESULT interp_not(exec_ctx_t *ctx)
689 hres = stack_pop_val(ctx, &val);
693 hres = VarNot(val.v, &v);
698 return stack_push(ctx, &v);
701 static HRESULT interp_and(exec_ctx_t *ctx)
709 hres = stack_pop_val(ctx, &r);
713 hres = stack_pop_val(ctx, &l);
714 if(SUCCEEDED(hres)) {
715 hres = VarAnd(l.v, r.v, &v);
722 return stack_push(ctx, &v);
725 static HRESULT interp_or(exec_ctx_t *ctx)
733 hres = stack_pop_val(ctx, &r);
737 hres = stack_pop_val(ctx, &l);
738 if(SUCCEEDED(hres)) {
739 hres = VarOr(l.v, r.v, &v);
746 return stack_push(ctx, &v);
749 static HRESULT interp_xor(exec_ctx_t *ctx)
757 hres = stack_pop_val(ctx, &r);
761 hres = stack_pop_val(ctx, &l);
762 if(SUCCEEDED(hres)) {
763 hres = VarXor(l.v, r.v, &v);
770 return stack_push(ctx, &v);
773 static HRESULT interp_eqv(exec_ctx_t *ctx)
781 hres = stack_pop_val(ctx, &r);
785 hres = stack_pop_val(ctx, &l);
786 if(SUCCEEDED(hres)) {
787 hres = VarEqv(l.v, r.v, &v);
794 return stack_push(ctx, &v);
797 static HRESULT interp_imp(exec_ctx_t *ctx)
805 hres = stack_pop_val(ctx, &r);
809 hres = stack_pop_val(ctx, &l);
810 if(SUCCEEDED(hres)) {
811 hres = VarImp(l.v, r.v, &v);
818 return stack_push(ctx, &v);
821 static HRESULT cmp_oper(exec_ctx_t *ctx)
826 hres = stack_pop_val(ctx, &r);
830 hres = stack_pop_val(ctx, &l);
831 if(SUCCEEDED(hres)) {
832 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
833 FIXME("comparing nulls is not implemented\n");
836 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
845 static HRESULT interp_equal(exec_ctx_t *ctx)
852 hres = cmp_oper(ctx);
857 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
858 return stack_push(ctx, &v);
861 static HRESULT interp_nequal(exec_ctx_t *ctx)
868 hres = cmp_oper(ctx);
873 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
874 return stack_push(ctx, &v);
877 static HRESULT interp_concat(exec_ctx_t *ctx)
885 hres = stack_pop_val(ctx, &r);
889 hres = stack_pop_val(ctx, &l);
890 if(SUCCEEDED(hres)) {
891 hres = VarCat(l.v, r.v, &v);
898 return stack_push(ctx, &v);
901 static HRESULT interp_add(exec_ctx_t *ctx)
909 hres = stack_pop_val(ctx, &r);
913 hres = stack_pop_val(ctx, &l);
914 if(SUCCEEDED(hres)) {
915 hres = VarAdd(l.v, r.v, &v);
922 return stack_push(ctx, &v);
925 static HRESULT interp_sub(exec_ctx_t *ctx)
933 hres = stack_pop_val(ctx, &r);
937 hres = stack_pop_val(ctx, &l);
938 if(SUCCEEDED(hres)) {
939 hres = VarSub(l.v, r.v, &v);
946 return stack_push(ctx, &v);
949 static HRESULT interp_mod(exec_ctx_t *ctx)
957 hres = stack_pop_val(ctx, &r);
961 hres = stack_pop_val(ctx, &l);
962 if(SUCCEEDED(hres)) {
963 hres = VarMod(l.v, r.v, &v);
970 return stack_push(ctx, &v);
973 static HRESULT interp_idiv(exec_ctx_t *ctx)
981 hres = stack_pop_val(ctx, &r);
985 hres = stack_pop_val(ctx, &l);
986 if(SUCCEEDED(hres)) {
987 hres = VarIdiv(l.v, r.v, &v);
994 return stack_push(ctx, &v);
997 static HRESULT interp_div(exec_ctx_t *ctx)
1005 hres = stack_pop_val(ctx, &r);
1009 hres = stack_pop_val(ctx, &l);
1010 if(SUCCEEDED(hres)) {
1011 hres = VarDiv(l.v, r.v, &v);
1018 return stack_push(ctx, &v);
1021 static HRESULT interp_mul(exec_ctx_t *ctx)
1029 hres = stack_pop_val(ctx, &r);
1033 hres = stack_pop_val(ctx, &l);
1034 if(SUCCEEDED(hres)) {
1035 hres = VarMul(l.v, r.v, &v);
1042 return stack_push(ctx, &v);
1045 static HRESULT interp_exp(exec_ctx_t *ctx)
1053 hres = stack_pop_val(ctx, &r);
1057 hres = stack_pop_val(ctx, &l);
1058 if(SUCCEEDED(hres)) {
1059 hres = VarPow(l.v, r.v, &v);
1066 return stack_push(ctx, &v);
1069 static HRESULT interp_neg(exec_ctx_t *ctx)
1075 hres = stack_pop_val(ctx, &val);
1079 hres = VarNeg(val.v, &v);
1084 return stack_push(ctx, &v);
1087 static const instr_func_t op_funcs[] = {
1088 #define X(x,n,a,b) interp_ ## x,
1093 static const unsigned op_move[] = {
1094 #define X(x,n,a,b) n,
1099 static void release_exec(exec_ctx_t *ctx)
1103 VariantClear(&ctx->ret_val);
1106 IDispatch_Release(ctx->this_obj);
1109 for(i=0; i < ctx->func->arg_cnt; i++)
1110 VariantClear(ctx->args+i);
1114 for(i=0; i < ctx->func->var_cnt; i++)
1115 VariantClear(ctx->vars+i);
1118 heap_free(ctx->args);
1119 heap_free(ctx->vars);
1120 heap_free(ctx->stack);
1123 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1125 exec_ctx_t exec = {func->code_ctx};
1127 HRESULT hres = S_OK;
1129 exec.code = func->code_ctx;
1131 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1132 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1140 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1142 release_exec(&exec);
1143 return E_OUTOFMEMORY;
1146 for(i=0; i < func->arg_cnt; i++) {
1148 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1149 if(func->args[i].by_ref)
1152 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1154 hres = VariantCopy(exec.args+i, v);
1157 release_exec(&exec);
1166 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1168 release_exec(&exec);
1169 return E_OUTOFMEMORY;
1175 exec.stack_size = 16;
1177 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1179 release_exec(&exec);
1180 return E_OUTOFMEMORY;
1184 exec.this_obj = this_obj;
1185 else if (ctx->host_global)
1186 exec.this_obj = ctx->host_global;
1188 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1189 IDispatch_AddRef(exec.this_obj);
1191 exec.instr = exec.code->instrs + func->code_off;
1196 op = exec.instr->op;
1197 hres = op_funcs[op](&exec);
1199 FIXME("Failed %08x\n", hres);
1200 stack_popn(&exec, exec.top);
1204 exec.instr += op_move[op];
1208 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET)
1209 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1211 if(SUCCEEDED(hres) && res) {
1212 *res = exec.ret_val;
1213 V_VT(&exec.ret_val) = VT_EMPTY;
1216 release_exec(&exec);