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 IDispatch *obj, *val;
509 TRACE("%s\n", debugstr_w(identifier));
511 hres = stack_pop_disp(ctx, &obj);
520 hres = stack_pop_disp(ctx, &val);
522 IDispatch_Release(obj);
526 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
527 if(SUCCEEDED(hres)) {
530 V_VT(&v) = VT_DISPATCH;
531 V_DISPATCH(&v) = val;
532 hres = disp_propput(ctx->script, obj, id, &v);
536 IDispatch_Release(val);
537 IDispatch_Release(obj);
541 static HRESULT interp_new(exec_ctx_t *ctx)
543 const WCHAR *arg = ctx->instr->arg1.bstr;
544 class_desc_t *class_desc;
549 TRACE("%s\n", debugstr_w(arg));
551 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
552 if(!strcmpiW(class_desc->name, arg))
556 FIXME("Class %s not found\n", debugstr_w(arg));
560 hres = create_vbdisp(class_desc, &obj);
564 V_VT(&v) = VT_DISPATCH;
565 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
566 return stack_push(ctx, &v);
569 static HRESULT interp_jmp(exec_ctx_t *ctx)
571 const unsigned arg = ctx->instr->arg1.uint;
579 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
581 const unsigned arg = ctx->instr->arg1.uint;
587 hres = stack_pop_val(ctx, &val);
591 if(V_VT(val.v) != VT_BOOL) {
592 FIXME("unsupported for %s\n", debugstr_variant(val.v));
600 instr_jmp(ctx, ctx->instr->arg1.uint);
604 static HRESULT interp_ret(exec_ctx_t *ctx)
612 static HRESULT interp_stop(exec_ctx_t *ctx)
616 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
620 static HRESULT interp_bool(exec_ctx_t *ctx)
622 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
625 TRACE("%s\n", arg ? "true" : "false");
629 return stack_push(ctx, &v);
632 static HRESULT interp_string(exec_ctx_t *ctx)
639 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
641 return E_OUTOFMEMORY;
643 return stack_push(ctx, &v);
646 static HRESULT interp_long(exec_ctx_t *ctx)
648 const LONG arg = ctx->instr->arg1.lng;
655 return stack_push(ctx, &v);
658 static HRESULT interp_short(exec_ctx_t *ctx)
660 const LONG arg = ctx->instr->arg1.lng;
667 return stack_push(ctx, &v);
670 static HRESULT interp_double(exec_ctx_t *ctx)
672 const DOUBLE *arg = ctx->instr->arg1.dbl;
675 TRACE("%lf\n", *arg);
679 return stack_push(ctx, &v);
682 static HRESULT interp_empty(exec_ctx_t *ctx)
689 return stack_push(ctx, &v);
692 static HRESULT interp_null(exec_ctx_t *ctx)
699 return stack_push(ctx, &v);
702 static HRESULT interp_nothing(exec_ctx_t *ctx)
708 V_VT(&v) = VT_DISPATCH;
709 V_DISPATCH(&v) = NULL;
710 return stack_push(ctx, &v);
713 static HRESULT interp_not(exec_ctx_t *ctx)
721 hres = stack_pop_val(ctx, &val);
725 hres = VarNot(val.v, &v);
730 return stack_push(ctx, &v);
733 static HRESULT interp_and(exec_ctx_t *ctx)
741 hres = stack_pop_val(ctx, &r);
745 hres = stack_pop_val(ctx, &l);
746 if(SUCCEEDED(hres)) {
747 hres = VarAnd(l.v, r.v, &v);
754 return stack_push(ctx, &v);
757 static HRESULT interp_or(exec_ctx_t *ctx)
765 hres = stack_pop_val(ctx, &r);
769 hres = stack_pop_val(ctx, &l);
770 if(SUCCEEDED(hres)) {
771 hres = VarOr(l.v, r.v, &v);
778 return stack_push(ctx, &v);
781 static HRESULT interp_xor(exec_ctx_t *ctx)
789 hres = stack_pop_val(ctx, &r);
793 hres = stack_pop_val(ctx, &l);
794 if(SUCCEEDED(hres)) {
795 hres = VarXor(l.v, r.v, &v);
802 return stack_push(ctx, &v);
805 static HRESULT interp_eqv(exec_ctx_t *ctx)
813 hres = stack_pop_val(ctx, &r);
817 hres = stack_pop_val(ctx, &l);
818 if(SUCCEEDED(hres)) {
819 hres = VarEqv(l.v, r.v, &v);
826 return stack_push(ctx, &v);
829 static HRESULT interp_imp(exec_ctx_t *ctx)
837 hres = stack_pop_val(ctx, &r);
841 hres = stack_pop_val(ctx, &l);
842 if(SUCCEEDED(hres)) {
843 hres = VarImp(l.v, r.v, &v);
850 return stack_push(ctx, &v);
853 static HRESULT cmp_oper(exec_ctx_t *ctx)
858 hres = stack_pop_val(ctx, &r);
862 hres = stack_pop_val(ctx, &l);
863 if(SUCCEEDED(hres)) {
864 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
865 FIXME("comparing nulls is not implemented\n");
868 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
877 static HRESULT interp_equal(exec_ctx_t *ctx)
884 hres = cmp_oper(ctx);
889 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
890 return stack_push(ctx, &v);
893 static HRESULT interp_nequal(exec_ctx_t *ctx)
900 hres = cmp_oper(ctx);
905 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
906 return stack_push(ctx, &v);
909 static HRESULT interp_concat(exec_ctx_t *ctx)
917 hres = stack_pop_val(ctx, &r);
921 hres = stack_pop_val(ctx, &l);
922 if(SUCCEEDED(hres)) {
923 hres = VarCat(l.v, r.v, &v);
930 return stack_push(ctx, &v);
933 static HRESULT interp_add(exec_ctx_t *ctx)
941 hres = stack_pop_val(ctx, &r);
945 hres = stack_pop_val(ctx, &l);
946 if(SUCCEEDED(hres)) {
947 hres = VarAdd(l.v, r.v, &v);
954 return stack_push(ctx, &v);
957 static HRESULT interp_sub(exec_ctx_t *ctx)
965 hres = stack_pop_val(ctx, &r);
969 hres = stack_pop_val(ctx, &l);
970 if(SUCCEEDED(hres)) {
971 hres = VarSub(l.v, r.v, &v);
978 return stack_push(ctx, &v);
981 static HRESULT interp_mod(exec_ctx_t *ctx)
989 hres = stack_pop_val(ctx, &r);
993 hres = stack_pop_val(ctx, &l);
994 if(SUCCEEDED(hres)) {
995 hres = VarMod(l.v, r.v, &v);
1002 return stack_push(ctx, &v);
1005 static HRESULT interp_idiv(exec_ctx_t *ctx)
1013 hres = stack_pop_val(ctx, &r);
1017 hres = stack_pop_val(ctx, &l);
1018 if(SUCCEEDED(hres)) {
1019 hres = VarIdiv(l.v, r.v, &v);
1026 return stack_push(ctx, &v);
1029 static HRESULT interp_div(exec_ctx_t *ctx)
1037 hres = stack_pop_val(ctx, &r);
1041 hres = stack_pop_val(ctx, &l);
1042 if(SUCCEEDED(hres)) {
1043 hres = VarDiv(l.v, r.v, &v);
1050 return stack_push(ctx, &v);
1053 static HRESULT interp_mul(exec_ctx_t *ctx)
1061 hres = stack_pop_val(ctx, &r);
1065 hres = stack_pop_val(ctx, &l);
1066 if(SUCCEEDED(hres)) {
1067 hres = VarMul(l.v, r.v, &v);
1074 return stack_push(ctx, &v);
1077 static HRESULT interp_exp(exec_ctx_t *ctx)
1085 hres = stack_pop_val(ctx, &r);
1089 hres = stack_pop_val(ctx, &l);
1090 if(SUCCEEDED(hres)) {
1091 hres = VarPow(l.v, r.v, &v);
1098 return stack_push(ctx, &v);
1101 static HRESULT interp_neg(exec_ctx_t *ctx)
1107 hres = stack_pop_val(ctx, &val);
1111 hres = VarNeg(val.v, &v);
1116 return stack_push(ctx, &v);
1119 static const instr_func_t op_funcs[] = {
1120 #define X(x,n,a,b) interp_ ## x,
1125 static const unsigned op_move[] = {
1126 #define X(x,n,a,b) n,
1131 static void release_exec(exec_ctx_t *ctx)
1135 VariantClear(&ctx->ret_val);
1138 IDispatch_Release(ctx->this_obj);
1141 for(i=0; i < ctx->func->arg_cnt; i++)
1142 VariantClear(ctx->args+i);
1146 for(i=0; i < ctx->func->var_cnt; i++)
1147 VariantClear(ctx->vars+i);
1150 heap_free(ctx->args);
1151 heap_free(ctx->vars);
1152 heap_free(ctx->stack);
1155 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1157 exec_ctx_t exec = {func->code_ctx};
1159 HRESULT hres = S_OK;
1161 exec.code = func->code_ctx;
1163 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1164 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1172 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1174 release_exec(&exec);
1175 return E_OUTOFMEMORY;
1178 for(i=0; i < func->arg_cnt; i++) {
1180 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1181 if(func->args[i].by_ref)
1184 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1186 hres = VariantCopy(exec.args+i, v);
1189 release_exec(&exec);
1198 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1200 release_exec(&exec);
1201 return E_OUTOFMEMORY;
1207 exec.stack_size = 16;
1209 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1211 release_exec(&exec);
1212 return E_OUTOFMEMORY;
1216 exec.this_obj = this_obj;
1217 else if (ctx->host_global)
1218 exec.this_obj = ctx->host_global;
1220 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1221 IDispatch_AddRef(exec.this_obj);
1223 exec.instr = exec.code->instrs + func->code_off;
1228 op = exec.instr->op;
1229 hres = op_funcs[op](&exec);
1231 FIXME("Failed %08x\n", hres);
1232 stack_popn(&exec, exec.top);
1236 exec.instr += op_move[op];
1240 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET)
1241 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1243 if(SUCCEEDED(hres) && res) {
1244 *res = exec.ret_val;
1245 V_VT(&exec.ret_val) = VT_EMPTY;
1248 release_exec(&exec);