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);
44 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
71 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
74 if(!strcmpiW(var->name, name)) {
86 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, vbdisp_invoke_type_t invoke_type, ref_t *ref)
94 if(invoke_type == VBDISP_LET && ctx->func->type == FUNC_FUNCTION && !strcmpiW(name, ctx->func->name)) {
96 ref->u.v = &ctx->ret_val;
100 for(i=0; i < ctx->func->var_cnt; i++) {
101 if(!strcmpiW(ctx->func->vars[i].name, name)) {
103 ref->u.v = ctx->vars+i;
108 for(i=0; i < ctx->func->arg_cnt; i++) {
109 if(!strcmpiW(ctx->func->args[i].name, name)) {
111 ref->u.v = ctx->args+i;
116 if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
119 for(func = ctx->script->global_funcs; func; func = func->next) {
120 if(!strcmpiW(func->name, name)) {
121 ref->type = REF_FUNC;
127 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
128 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
129 hres = disp_get_id(item->disp, name, &id);
130 if(SUCCEEDED(hres)) {
131 ref->type = REF_DISP;
132 ref->u.d.disp = item->disp;
139 if(!ctx->func->code_ctx->option_explicit)
140 FIXME("create an attempt to set\n");
142 ref->type = REF_NONE;
146 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
149 return ctx->stack + --ctx->top;
152 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
154 if(ctx->stack_size == ctx->top) {
157 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
160 return E_OUTOFMEMORY;
163 ctx->stack = new_stack;
164 ctx->stack_size *= 2;
167 ctx->stack[ctx->top++] = *v;
171 static void stack_popn(exec_ctx_t *ctx, unsigned n)
174 VariantClear(stack_pop(ctx));
177 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
181 var = stack_pop(ctx);
183 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
185 var = V_VARIANTREF(var);
190 if(V_VT(var) == VT_DISPATCH) {
191 FIXME("got dispatch - get its default value\n");
200 static inline void release_val(variant_val_t *v)
206 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
208 VARIANT *v = stack_pop(ctx);
210 if(V_VT(v) == VT_DISPATCH) {
211 *ret = V_DISPATCH(v);
215 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
216 FIXME("not supported type: %s\n", debugstr_variant(v));
222 if(V_VT(v) != VT_DISPATCH) {
223 FIXME("not disp %s\n", debugstr_variant(v));
228 IDispatch_AddRef(V_DISPATCH(v));
229 *ret = V_DISPATCH(v);
233 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
235 ctx->instr = ctx->code->instrs + addr;
238 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
241 dp->rgdispidNamedArgs = NULL;
248 assert(ctx->top >= arg_cnt);
250 for(i=1; i*2 <= arg_cnt; i++) {
251 tmp = ctx->stack[ctx->top-i];
252 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
253 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
256 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
262 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
264 BSTR identifier = ctx->instr->arg1.bstr;
265 const unsigned arg_cnt = ctx->instr->arg2.uint;
270 hres = lookup_identifier(ctx, identifier, VBDISP_CALLGET, &ref);
274 vbstack_to_dp(ctx, arg_cnt, &dp);
279 FIXME("REF_VAR no res\n");
284 FIXME("arguments not implemented\n");
288 V_VT(res) = VT_BYREF|VT_VARIANT;
289 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
292 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
297 hres = exec_script(ctx->script, ref.u.f, &dp, res);
302 FIXME("%s not found\n", debugstr_w(identifier));
303 return DISP_E_UNKNOWNNAME;
306 stack_popn(ctx, arg_cnt);
310 static HRESULT interp_icall(exec_ctx_t *ctx)
317 hres = do_icall(ctx, &v);
321 return stack_push(ctx, &v);
324 static HRESULT interp_icallv(exec_ctx_t *ctx)
327 return do_icall(ctx, NULL);
330 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
335 hres = lookup_identifier(ctx, name, VBDISP_LET, &ref);
341 VARIANT *v = ref.u.v;
343 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
351 hres = VariantCopy(v, val);
356 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
361 FIXME("functions not implemented\n");
364 FIXME("%s not found\n", debugstr_w(name));
367 return DISP_E_UNKNOWNNAME;
373 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
375 const BSTR arg = ctx->instr->arg1.bstr;
379 TRACE("%s\n", debugstr_w(arg));
381 hres = stack_pop_val(ctx, &v);
385 return assign_ident(ctx, arg, v.v, v.owned);
388 static HRESULT interp_set_ident(exec_ctx_t *ctx)
390 const BSTR arg = ctx->instr->arg1.bstr;
395 TRACE("%s\n", debugstr_w(arg));
397 hres = stack_pop_disp(ctx, &disp);
401 V_VT(&v) = VT_DISPATCH;
402 V_DISPATCH(&v) = disp;
403 return assign_ident(ctx, ctx->instr->arg1.bstr, &v, TRUE);
406 static HRESULT interp_assign_member(exec_ctx_t *ctx)
408 BSTR identifier = ctx->instr->arg1.bstr;
414 TRACE("%s\n", debugstr_w(identifier));
416 hres = stack_pop_disp(ctx, &obj);
425 hres = stack_pop_val(ctx, &val);
427 IDispatch_Release(obj);
431 hres = disp_get_id(obj, identifier, &id);
433 hres = disp_propput(ctx->script, obj, id, val.v);
436 IDispatch_Release(obj);
440 static HRESULT interp_set_member(exec_ctx_t *ctx)
442 BSTR identifier = ctx->instr->arg1.bstr;
443 FIXME("%s\n", debugstr_w(identifier));
447 static HRESULT interp_new(exec_ctx_t *ctx)
449 const WCHAR *arg = ctx->instr->arg1.bstr;
450 class_desc_t *class_desc;
455 TRACE("%s\n", debugstr_w(arg));
457 for(class_desc = ctx->script->classes; class_desc; class_desc = class_desc->next) {
458 if(!strcmpiW(class_desc->name, arg))
462 FIXME("Class %s not found\n", debugstr_w(arg));
466 hres = create_vbdisp(class_desc, &obj);
470 V_VT(&v) = VT_DISPATCH;
471 V_DISPATCH(&v) = (IDispatch*)&obj->IDispatchEx_iface;
472 return stack_push(ctx, &v);
475 static HRESULT interp_jmp(exec_ctx_t *ctx)
477 const unsigned arg = ctx->instr->arg1.uint;
485 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
487 const unsigned arg = ctx->instr->arg1.uint;
493 hres = stack_pop_val(ctx, &val);
497 if(V_VT(val.v) != VT_BOOL) {
498 FIXME("unsupported for %s\n", debugstr_variant(val.v));
506 instr_jmp(ctx, ctx->instr->arg1.uint);
510 static HRESULT interp_ret(exec_ctx_t *ctx)
518 static HRESULT interp_bool(exec_ctx_t *ctx)
520 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
523 TRACE("%s\n", arg ? "true" : "false");
527 return stack_push(ctx, &v);
530 static HRESULT interp_string(exec_ctx_t *ctx)
537 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
539 return E_OUTOFMEMORY;
541 return stack_push(ctx, &v);
544 static HRESULT interp_long(exec_ctx_t *ctx)
546 const LONG arg = ctx->instr->arg1.lng;
553 return stack_push(ctx, &v);
556 static HRESULT interp_short(exec_ctx_t *ctx)
558 const LONG arg = ctx->instr->arg1.lng;
565 return stack_push(ctx, &v);
568 static HRESULT interp_double(exec_ctx_t *ctx)
570 const DOUBLE *arg = ctx->instr->arg1.dbl;
573 TRACE("%lf\n", *arg);
577 return stack_push(ctx, &v);
580 static HRESULT interp_empty(exec_ctx_t *ctx)
587 return stack_push(ctx, &v);
590 static HRESULT interp_null(exec_ctx_t *ctx)
597 return stack_push(ctx, &v);
600 static HRESULT interp_nothing(exec_ctx_t *ctx)
606 V_VT(&v) = VT_DISPATCH;
607 V_DISPATCH(&v) = NULL;
608 return stack_push(ctx, &v);
611 static HRESULT interp_not(exec_ctx_t *ctx)
619 hres = stack_pop_val(ctx, &val);
623 hres = VarNot(val.v, &v);
628 return stack_push(ctx, &v);
631 static HRESULT interp_and(exec_ctx_t *ctx)
639 hres = stack_pop_val(ctx, &r);
643 hres = stack_pop_val(ctx, &l);
644 if(SUCCEEDED(hres)) {
645 hres = VarAnd(l.v, r.v, &v);
652 return stack_push(ctx, &v);
655 static HRESULT interp_or(exec_ctx_t *ctx)
663 hres = stack_pop_val(ctx, &r);
667 hres = stack_pop_val(ctx, &l);
668 if(SUCCEEDED(hres)) {
669 hres = VarOr(l.v, r.v, &v);
676 return stack_push(ctx, &v);
679 static HRESULT interp_xor(exec_ctx_t *ctx)
687 hres = stack_pop_val(ctx, &r);
691 hres = stack_pop_val(ctx, &l);
692 if(SUCCEEDED(hres)) {
693 hres = VarXor(l.v, r.v, &v);
700 return stack_push(ctx, &v);
703 static HRESULT interp_eqv(exec_ctx_t *ctx)
711 hres = stack_pop_val(ctx, &r);
715 hres = stack_pop_val(ctx, &l);
716 if(SUCCEEDED(hres)) {
717 hres = VarEqv(l.v, r.v, &v);
724 return stack_push(ctx, &v);
727 static HRESULT interp_imp(exec_ctx_t *ctx)
735 hres = stack_pop_val(ctx, &r);
739 hres = stack_pop_val(ctx, &l);
740 if(SUCCEEDED(hres)) {
741 hres = VarImp(l.v, r.v, &v);
748 return stack_push(ctx, &v);
751 static HRESULT cmp_oper(exec_ctx_t *ctx)
756 hres = stack_pop_val(ctx, &r);
760 hres = stack_pop_val(ctx, &l);
761 if(SUCCEEDED(hres)) {
762 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
763 FIXME("comparing nulls is not implemented\n");
766 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
775 static HRESULT interp_equal(exec_ctx_t *ctx)
782 hres = cmp_oper(ctx);
787 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
788 return stack_push(ctx, &v);
791 static HRESULT interp_nequal(exec_ctx_t *ctx)
798 hres = cmp_oper(ctx);
803 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
804 return stack_push(ctx, &v);
807 static HRESULT interp_concat(exec_ctx_t *ctx)
815 hres = stack_pop_val(ctx, &r);
819 hres = stack_pop_val(ctx, &l);
820 if(SUCCEEDED(hres)) {
821 hres = VarCat(l.v, r.v, &v);
828 return stack_push(ctx, &v);
831 static HRESULT interp_add(exec_ctx_t *ctx)
839 hres = stack_pop_val(ctx, &r);
843 hres = stack_pop_val(ctx, &l);
844 if(SUCCEEDED(hres)) {
845 hres = VarAdd(l.v, r.v, &v);
852 return stack_push(ctx, &v);
855 static HRESULT interp_sub(exec_ctx_t *ctx)
863 hres = stack_pop_val(ctx, &r);
867 hres = stack_pop_val(ctx, &l);
868 if(SUCCEEDED(hres)) {
869 hres = VarSub(l.v, r.v, &v);
876 return stack_push(ctx, &v);
879 static HRESULT interp_mod(exec_ctx_t *ctx)
887 hres = stack_pop_val(ctx, &r);
891 hres = stack_pop_val(ctx, &l);
892 if(SUCCEEDED(hres)) {
893 hres = VarMod(l.v, r.v, &v);
900 return stack_push(ctx, &v);
903 static HRESULT interp_idiv(exec_ctx_t *ctx)
911 hres = stack_pop_val(ctx, &r);
915 hres = stack_pop_val(ctx, &l);
916 if(SUCCEEDED(hres)) {
917 hres = VarIdiv(l.v, r.v, &v);
924 return stack_push(ctx, &v);
927 static HRESULT interp_div(exec_ctx_t *ctx)
935 hres = stack_pop_val(ctx, &r);
939 hres = stack_pop_val(ctx, &l);
940 if(SUCCEEDED(hres)) {
941 hres = VarDiv(l.v, r.v, &v);
948 return stack_push(ctx, &v);
951 static HRESULT interp_mul(exec_ctx_t *ctx)
959 hres = stack_pop_val(ctx, &r);
963 hres = stack_pop_val(ctx, &l);
964 if(SUCCEEDED(hres)) {
965 hres = VarMul(l.v, r.v, &v);
972 return stack_push(ctx, &v);
975 static HRESULT interp_exp(exec_ctx_t *ctx)
983 hres = stack_pop_val(ctx, &r);
987 hres = stack_pop_val(ctx, &l);
988 if(SUCCEEDED(hres)) {
989 hres = VarPow(l.v, r.v, &v);
996 return stack_push(ctx, &v);
999 static HRESULT interp_neg(exec_ctx_t *ctx)
1005 hres = stack_pop_val(ctx, &val);
1009 hres = VarNeg(val.v, &v);
1014 return stack_push(ctx, &v);
1017 static const instr_func_t op_funcs[] = {
1018 #define X(x,n,a,b) interp_ ## x,
1023 static const unsigned op_move[] = {
1024 #define X(x,n,a,b) n,
1029 static void release_exec(exec_ctx_t *ctx)
1033 VariantClear(&ctx->ret_val);
1036 for(i=0; i < ctx->func->arg_cnt; i++)
1037 VariantClear(ctx->args+i);
1041 for(i=0; i < ctx->func->var_cnt; i++)
1042 VariantClear(ctx->vars+i);
1045 heap_free(ctx->args);
1046 heap_free(ctx->vars);
1047 heap_free(ctx->stack);
1050 HRESULT exec_script(script_ctx_t *ctx, function_t *func, DISPPARAMS *dp, VARIANT *res)
1052 exec_ctx_t exec = {func->code_ctx};
1054 HRESULT hres = S_OK;
1056 exec.code = func->code_ctx;
1058 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1059 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1067 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1069 release_exec(&exec);
1070 return E_OUTOFMEMORY;
1073 for(i=0; i < func->arg_cnt; i++) {
1075 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1076 if(func->args[i].by_ref)
1079 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1081 hres = VariantCopy(exec.args+i, v);
1084 release_exec(&exec);
1093 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1095 release_exec(&exec);
1096 return E_OUTOFMEMORY;
1102 exec.stack_size = 16;
1104 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1106 release_exec(&exec);
1107 return E_OUTOFMEMORY;
1110 exec.instr = exec.code->instrs + func->code_off;
1115 op = exec.instr->op;
1116 hres = op_funcs[op](&exec);
1118 FIXME("Failed %08x\n", hres);
1119 stack_popn(&exec, exec.top);
1123 exec.instr += op_move[op];
1127 if(func->type != FUNC_FUNCTION)
1128 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1130 if(SUCCEEDED(hres) && res) {
1131 *res = exec.ret_val;
1132 V_VT(&exec.ret_val) = VT_EMPTY;
1135 release_exec(&exec);