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
96 && (ctx->func->type == FUNC_FUNCTION || ctx->func->type == FUNC_PROPGET || ctx->func->type == FUNC_DEFGET)
97 && !strcmpiW(name, ctx->func->name)) {
99 ref->u.v = &ctx->ret_val;
103 for(i=0; i < ctx->func->var_cnt; i++) {
104 if(!strcmpiW(ctx->func->vars[i].name, name)) {
106 ref->u.v = ctx->vars+i;
111 for(i=0; i < ctx->func->arg_cnt; i++) {
112 if(!strcmpiW(ctx->func->args[i].name, name)) {
114 ref->u.v = ctx->args+i;
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;
127 if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
130 for(func = ctx->script->global_funcs; func; func = func->next) {
131 if(!strcmpiW(func->name, name)) {
132 ref->type = REF_FUNC;
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;
150 if(!ctx->func->code_ctx->option_explicit)
151 FIXME("create an attempt to set\n");
153 ref->type = REF_NONE;
157 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
160 return ctx->stack + --ctx->top;
163 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
165 if(ctx->stack_size == ctx->top) {
168 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
171 return E_OUTOFMEMORY;
174 ctx->stack = new_stack;
175 ctx->stack_size *= 2;
178 ctx->stack[ctx->top++] = *v;
182 static void stack_popn(exec_ctx_t *ctx, unsigned n)
185 VariantClear(stack_pop(ctx));
188 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
192 var = stack_pop(ctx);
194 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
196 var = V_VARIANTREF(var);
201 if(V_VT(var) == VT_DISPATCH) {
205 hres = disp_call(ctx->script, V_DISPATCH(var), DISPID_VALUE, &dp, &v->store);
207 IDispatch_Release(V_DISPATCH(var));
220 static inline void release_val(variant_val_t *v)
226 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
228 VARIANT *v = stack_pop(ctx);
230 if(V_VT(v) == VT_DISPATCH) {
231 *ret = V_DISPATCH(v);
235 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
236 FIXME("not supported type: %s\n", debugstr_variant(v));
242 if(V_VT(v) != VT_DISPATCH) {
243 FIXME("not disp %s\n", debugstr_variant(v));
248 IDispatch_AddRef(V_DISPATCH(v));
249 *ret = V_DISPATCH(v);
253 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
255 ctx->instr = ctx->code->instrs + addr;
258 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
261 dp->rgdispidNamedArgs = NULL;
268 assert(ctx->top >= arg_cnt);
270 for(i=1; i*2 <= arg_cnt; i++) {
271 tmp = ctx->stack[ctx->top-i];
272 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
273 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
276 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
282 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
284 BSTR identifier = ctx->instr->arg1.bstr;
285 const unsigned arg_cnt = ctx->instr->arg2.uint;
290 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
294 vbstack_to_dp(ctx, arg_cnt, &dp);
299 FIXME("REF_VAR no res\n");
304 FIXME("arguments not implemented\n");
308 V_VT(res) = VT_BYREF|VT_VARIANT;
309 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
312 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
317 hres = exec_script(ctx->script, ref.u.f, NULL, &dp, res);
322 FIXME("%s not found\n", debugstr_w(identifier));
323 return DISP_E_UNKNOWNNAME;
326 stack_popn(ctx, arg_cnt);
330 static HRESULT interp_icall(exec_ctx_t *ctx)
337 hres = do_icall(ctx, &v);
341 return stack_push(ctx, &v);
344 static HRESULT interp_icallv(exec_ctx_t *ctx)
347 return do_icall(ctx, NULL);
350 static HRESULT do_mcall(exec_ctx_t *ctx, VARIANT *res)
352 const BSTR identifier = ctx->instr->arg1.bstr;
353 const unsigned arg_cnt = ctx->instr->arg2.uint;
359 hres = stack_pop_disp(ctx, &obj);
368 vbstack_to_dp(ctx, arg_cnt, &dp);
370 hres = disp_get_id(obj, identifier, VBDISP_CALLGET, FALSE, &id);
372 hres = disp_call(ctx->script, obj, id, &dp, res);
373 IDispatch_Release(obj);
377 stack_popn(ctx, arg_cnt);
381 static HRESULT interp_mcall(exec_ctx_t *ctx)
388 hres = do_mcall(ctx, &res);
392 return stack_push(ctx, &res);
395 static HRESULT interp_mcallv(exec_ctx_t *ctx)
399 return do_mcall(ctx, NULL);
402 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
407 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
413 VARIANT *v = ref.u.v;
415 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
423 hres = VariantCopy(v, val);
428 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
433 FIXME("functions not implemented\n");
436 FIXME("%s not found\n", debugstr_w(name));
439 return DISP_E_UNKNOWNNAME;
445 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
447 const BSTR arg = ctx->instr->arg1.bstr;
451 TRACE("%s\n", debugstr_w(arg));
453 hres = stack_pop_val(ctx, &v);
457 return assign_ident(ctx, arg, v.v, v.owned);
460 static HRESULT interp_set_ident(exec_ctx_t *ctx)
462 const BSTR arg = ctx->instr->arg1.bstr;
467 TRACE("%s\n", debugstr_w(arg));
469 hres = stack_pop_disp(ctx, &disp);
473 V_VT(&v) = VT_DISPATCH;
474 V_DISPATCH(&v) = disp;
475 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
478 static HRESULT interp_assign_member(exec_ctx_t *ctx)
480 BSTR identifier = ctx->instr->arg1.bstr;
486 TRACE("%s\n", debugstr_w(identifier));
488 hres = stack_pop_disp(ctx, &obj);
497 hres = stack_pop_val(ctx, &val);
499 IDispatch_Release(obj);
503 hres = disp_get_id(obj, identifier, VBDISP_LET, FALSE, &id);
505 hres = disp_propput(ctx->script, obj, id, val.v);
508 IDispatch_Release(obj);
512 static HRESULT interp_set_member(exec_ctx_t *ctx)
514 BSTR identifier = ctx->instr->arg1.bstr;
515 IDispatch *obj, *val;
519 TRACE("%s\n", debugstr_w(identifier));
521 hres = stack_pop_disp(ctx, &obj);
530 hres = stack_pop_disp(ctx, &val);
532 IDispatch_Release(obj);
536 hres = disp_get_id(obj, identifier, VBDISP_SET, FALSE, &id);
537 if(SUCCEEDED(hres)) {
540 V_VT(&v) = VT_DISPATCH;
541 V_DISPATCH(&v) = val;
542 hres = disp_propput(ctx->script, obj, id, &v);
546 IDispatch_Release(val);
547 IDispatch_Release(obj);
551 static HRESULT interp_new(exec_ctx_t *ctx)
553 const WCHAR *arg = ctx->instr->arg1.bstr;
554 class_desc_t *class_desc;
559 TRACE("%s\n", debugstr_w(arg));
561 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
562 if(!strcmpiW(class_desc->name, arg))
566 FIXME("Class %s not found\n", debugstr_w(arg));
570 hres = create_vbdisp(class_desc, &obj);
574 V_VT(&v) = VT_DISPATCH;
575 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
576 return stack_push(ctx, &v);
579 static HRESULT interp_jmp(exec_ctx_t *ctx)
581 const unsigned arg = ctx->instr->arg1.uint;
589 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
591 const unsigned arg = ctx->instr->arg1.uint;
597 hres = stack_pop_val(ctx, &val);
601 if(V_VT(val.v) != VT_BOOL) {
602 FIXME("unsupported for %s\n", debugstr_variant(val.v));
610 instr_jmp(ctx, ctx->instr->arg1.uint);
614 static HRESULT interp_jmp_true(exec_ctx_t *ctx)
616 const unsigned arg = ctx->instr->arg1.uint;
622 hres = stack_pop_val(ctx, &val);
626 if(V_VT(val.v) != VT_BOOL) {
627 FIXME("unsupported for %s\n", debugstr_variant(val.v));
633 instr_jmp(ctx, ctx->instr->arg1.uint);
639 static HRESULT interp_ret(exec_ctx_t *ctx)
647 static HRESULT interp_stop(exec_ctx_t *ctx)
651 /* NOTE: this should have effect in debugging mode (that we don't support yet) */
655 static HRESULT interp_bool(exec_ctx_t *ctx)
657 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
660 TRACE("%s\n", arg ? "true" : "false");
664 return stack_push(ctx, &v);
667 static HRESULT interp_string(exec_ctx_t *ctx)
674 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
676 return E_OUTOFMEMORY;
678 return stack_push(ctx, &v);
681 static HRESULT interp_long(exec_ctx_t *ctx)
683 const LONG arg = ctx->instr->arg1.lng;
690 return stack_push(ctx, &v);
693 static HRESULT interp_short(exec_ctx_t *ctx)
695 const LONG arg = ctx->instr->arg1.lng;
702 return stack_push(ctx, &v);
705 static HRESULT interp_double(exec_ctx_t *ctx)
707 const DOUBLE *arg = ctx->instr->arg1.dbl;
710 TRACE("%lf\n", *arg);
714 return stack_push(ctx, &v);
717 static HRESULT interp_empty(exec_ctx_t *ctx)
724 return stack_push(ctx, &v);
727 static HRESULT interp_null(exec_ctx_t *ctx)
734 return stack_push(ctx, &v);
737 static HRESULT interp_nothing(exec_ctx_t *ctx)
743 V_VT(&v) = VT_DISPATCH;
744 V_DISPATCH(&v) = NULL;
745 return stack_push(ctx, &v);
748 static HRESULT interp_not(exec_ctx_t *ctx)
756 hres = stack_pop_val(ctx, &val);
760 hres = VarNot(val.v, &v);
765 return stack_push(ctx, &v);
768 static HRESULT interp_and(exec_ctx_t *ctx)
776 hres = stack_pop_val(ctx, &r);
780 hres = stack_pop_val(ctx, &l);
781 if(SUCCEEDED(hres)) {
782 hres = VarAnd(l.v, r.v, &v);
789 return stack_push(ctx, &v);
792 static HRESULT interp_or(exec_ctx_t *ctx)
800 hres = stack_pop_val(ctx, &r);
804 hres = stack_pop_val(ctx, &l);
805 if(SUCCEEDED(hres)) {
806 hres = VarOr(l.v, r.v, &v);
813 return stack_push(ctx, &v);
816 static HRESULT interp_xor(exec_ctx_t *ctx)
824 hres = stack_pop_val(ctx, &r);
828 hres = stack_pop_val(ctx, &l);
829 if(SUCCEEDED(hres)) {
830 hres = VarXor(l.v, r.v, &v);
837 return stack_push(ctx, &v);
840 static HRESULT interp_eqv(exec_ctx_t *ctx)
848 hres = stack_pop_val(ctx, &r);
852 hres = stack_pop_val(ctx, &l);
853 if(SUCCEEDED(hres)) {
854 hres = VarEqv(l.v, r.v, &v);
861 return stack_push(ctx, &v);
864 static HRESULT interp_imp(exec_ctx_t *ctx)
872 hres = stack_pop_val(ctx, &r);
876 hres = stack_pop_val(ctx, &l);
877 if(SUCCEEDED(hres)) {
878 hres = VarImp(l.v, r.v, &v);
885 return stack_push(ctx, &v);
888 static HRESULT cmp_oper(exec_ctx_t *ctx)
893 hres = stack_pop_val(ctx, &r);
897 hres = stack_pop_val(ctx, &l);
898 if(SUCCEEDED(hres)) {
899 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
900 FIXME("comparing nulls is not implemented\n");
903 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
912 static HRESULT interp_equal(exec_ctx_t *ctx)
919 hres = cmp_oper(ctx);
924 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
925 return stack_push(ctx, &v);
928 static HRESULT interp_nequal(exec_ctx_t *ctx)
935 hres = cmp_oper(ctx);
940 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
941 return stack_push(ctx, &v);
944 static HRESULT interp_concat(exec_ctx_t *ctx)
952 hres = stack_pop_val(ctx, &r);
956 hres = stack_pop_val(ctx, &l);
957 if(SUCCEEDED(hres)) {
958 hres = VarCat(l.v, r.v, &v);
965 return stack_push(ctx, &v);
968 static HRESULT interp_add(exec_ctx_t *ctx)
976 hres = stack_pop_val(ctx, &r);
980 hres = stack_pop_val(ctx, &l);
981 if(SUCCEEDED(hres)) {
982 hres = VarAdd(l.v, r.v, &v);
989 return stack_push(ctx, &v);
992 static HRESULT interp_sub(exec_ctx_t *ctx)
1000 hres = stack_pop_val(ctx, &r);
1004 hres = stack_pop_val(ctx, &l);
1005 if(SUCCEEDED(hres)) {
1006 hres = VarSub(l.v, r.v, &v);
1013 return stack_push(ctx, &v);
1016 static HRESULT interp_mod(exec_ctx_t *ctx)
1024 hres = stack_pop_val(ctx, &r);
1028 hres = stack_pop_val(ctx, &l);
1029 if(SUCCEEDED(hres)) {
1030 hres = VarMod(l.v, r.v, &v);
1037 return stack_push(ctx, &v);
1040 static HRESULT interp_idiv(exec_ctx_t *ctx)
1048 hres = stack_pop_val(ctx, &r);
1052 hres = stack_pop_val(ctx, &l);
1053 if(SUCCEEDED(hres)) {
1054 hres = VarIdiv(l.v, r.v, &v);
1061 return stack_push(ctx, &v);
1064 static HRESULT interp_div(exec_ctx_t *ctx)
1072 hres = stack_pop_val(ctx, &r);
1076 hres = stack_pop_val(ctx, &l);
1077 if(SUCCEEDED(hres)) {
1078 hres = VarDiv(l.v, r.v, &v);
1085 return stack_push(ctx, &v);
1088 static HRESULT interp_mul(exec_ctx_t *ctx)
1096 hres = stack_pop_val(ctx, &r);
1100 hres = stack_pop_val(ctx, &l);
1101 if(SUCCEEDED(hres)) {
1102 hres = VarMul(l.v, r.v, &v);
1109 return stack_push(ctx, &v);
1112 static HRESULT interp_exp(exec_ctx_t *ctx)
1120 hres = stack_pop_val(ctx, &r);
1124 hres = stack_pop_val(ctx, &l);
1125 if(SUCCEEDED(hres)) {
1126 hres = VarPow(l.v, r.v, &v);
1133 return stack_push(ctx, &v);
1136 static HRESULT interp_neg(exec_ctx_t *ctx)
1142 hres = stack_pop_val(ctx, &val);
1146 hres = VarNeg(val.v, &v);
1151 return stack_push(ctx, &v);
1154 static const instr_func_t op_funcs[] = {
1155 #define X(x,n,a,b) interp_ ## x,
1160 static const unsigned op_move[] = {
1161 #define X(x,n,a,b) n,
1166 static void release_exec(exec_ctx_t *ctx)
1170 VariantClear(&ctx->ret_val);
1173 IDispatch_Release(ctx->this_obj);
1176 for(i=0; i < ctx->func->arg_cnt; i++)
1177 VariantClear(ctx->args+i);
1181 for(i=0; i < ctx->func->var_cnt; i++)
1182 VariantClear(ctx->vars+i);
1185 heap_free(ctx->args);
1186 heap_free(ctx->vars);
1187 heap_free(ctx->stack);
1190 HRESULT exec_script(script_ctx_t *ctx, function_t *func, IDispatch *this_obj, DISPPARAMS *dp, VARIANT *res)
1192 exec_ctx_t exec = {func->code_ctx};
1194 HRESULT hres = S_OK;
1196 exec.code = func->code_ctx;
1198 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1199 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1207 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1209 release_exec(&exec);
1210 return E_OUTOFMEMORY;
1213 for(i=0; i < func->arg_cnt; i++) {
1215 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1216 if(func->args[i].by_ref)
1219 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1221 hres = VariantCopy(exec.args+i, v);
1224 release_exec(&exec);
1233 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1235 release_exec(&exec);
1236 return E_OUTOFMEMORY;
1242 exec.stack_size = 16;
1244 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1246 release_exec(&exec);
1247 return E_OUTOFMEMORY;
1251 exec.this_obj = this_obj;
1252 else if (ctx->host_global)
1253 exec.this_obj = ctx->host_global;
1255 exec.this_obj = (IDispatch*)&ctx->script_obj->IDispatchEx_iface;
1256 IDispatch_AddRef(exec.this_obj);
1258 exec.instr = exec.code->instrs + func->code_off;
1263 op = exec.instr->op;
1264 hres = op_funcs[op](&exec);
1266 FIXME("Failed %08x\n", hres);
1267 stack_popn(&exec, exec.top);
1271 exec.instr += op_move[op];
1275 if(func->type != FUNC_FUNCTION && func->type != FUNC_PROPGET && func->type != FUNC_DEFGET)
1276 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1278 if(SUCCEEDED(hres) && res) {
1279 *res = exec.ret_val;
1280 V_VT(&exec.ret_val) = VT_EMPTY;
1283 release_exec(&exec);