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_not(exec_ctx_t *ctx)
608 hres = stack_pop_val(ctx, &val);
612 hres = VarNot(val.v, &v);
617 return stack_push(ctx, &v);
620 static HRESULT interp_and(exec_ctx_t *ctx)
628 hres = stack_pop_val(ctx, &r);
632 hres = stack_pop_val(ctx, &l);
633 if(SUCCEEDED(hres)) {
634 hres = VarAnd(l.v, r.v, &v);
641 return stack_push(ctx, &v);
644 static HRESULT interp_or(exec_ctx_t *ctx)
652 hres = stack_pop_val(ctx, &r);
656 hres = stack_pop_val(ctx, &l);
657 if(SUCCEEDED(hres)) {
658 hres = VarOr(l.v, r.v, &v);
665 return stack_push(ctx, &v);
668 static HRESULT interp_xor(exec_ctx_t *ctx)
676 hres = stack_pop_val(ctx, &r);
680 hres = stack_pop_val(ctx, &l);
681 if(SUCCEEDED(hres)) {
682 hres = VarXor(l.v, r.v, &v);
689 return stack_push(ctx, &v);
692 static HRESULT interp_eqv(exec_ctx_t *ctx)
700 hres = stack_pop_val(ctx, &r);
704 hres = stack_pop_val(ctx, &l);
705 if(SUCCEEDED(hres)) {
706 hres = VarEqv(l.v, r.v, &v);
713 return stack_push(ctx, &v);
716 static HRESULT interp_imp(exec_ctx_t *ctx)
724 hres = stack_pop_val(ctx, &r);
728 hres = stack_pop_val(ctx, &l);
729 if(SUCCEEDED(hres)) {
730 hres = VarImp(l.v, r.v, &v);
737 return stack_push(ctx, &v);
740 static HRESULT cmp_oper(exec_ctx_t *ctx)
745 hres = stack_pop_val(ctx, &r);
749 hres = stack_pop_val(ctx, &l);
750 if(SUCCEEDED(hres)) {
751 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
752 FIXME("comparing nulls is not implemented\n");
755 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
764 static HRESULT interp_equal(exec_ctx_t *ctx)
771 hres = cmp_oper(ctx);
776 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
777 return stack_push(ctx, &v);
780 static HRESULT interp_nequal(exec_ctx_t *ctx)
787 hres = cmp_oper(ctx);
792 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
793 return stack_push(ctx, &v);
796 static HRESULT interp_concat(exec_ctx_t *ctx)
804 hres = stack_pop_val(ctx, &r);
808 hres = stack_pop_val(ctx, &l);
809 if(SUCCEEDED(hres)) {
810 hres = VarCat(l.v, r.v, &v);
817 return stack_push(ctx, &v);
820 static HRESULT interp_add(exec_ctx_t *ctx)
828 hres = stack_pop_val(ctx, &r);
832 hres = stack_pop_val(ctx, &l);
833 if(SUCCEEDED(hres)) {
834 hres = VarAdd(l.v, r.v, &v);
841 return stack_push(ctx, &v);
844 static HRESULT interp_sub(exec_ctx_t *ctx)
852 hres = stack_pop_val(ctx, &r);
856 hres = stack_pop_val(ctx, &l);
857 if(SUCCEEDED(hres)) {
858 hres = VarSub(l.v, r.v, &v);
865 return stack_push(ctx, &v);
868 static HRESULT interp_mod(exec_ctx_t *ctx)
876 hres = stack_pop_val(ctx, &r);
880 hres = stack_pop_val(ctx, &l);
881 if(SUCCEEDED(hres)) {
882 hres = VarMod(l.v, r.v, &v);
889 return stack_push(ctx, &v);
892 static HRESULT interp_idiv(exec_ctx_t *ctx)
900 hres = stack_pop_val(ctx, &r);
904 hres = stack_pop_val(ctx, &l);
905 if(SUCCEEDED(hres)) {
906 hres = VarIdiv(l.v, r.v, &v);
913 return stack_push(ctx, &v);
916 static HRESULT interp_div(exec_ctx_t *ctx)
924 hres = stack_pop_val(ctx, &r);
928 hres = stack_pop_val(ctx, &l);
929 if(SUCCEEDED(hres)) {
930 hres = VarDiv(l.v, r.v, &v);
937 return stack_push(ctx, &v);
940 static HRESULT interp_mul(exec_ctx_t *ctx)
948 hres = stack_pop_val(ctx, &r);
952 hres = stack_pop_val(ctx, &l);
953 if(SUCCEEDED(hres)) {
954 hres = VarMul(l.v, r.v, &v);
961 return stack_push(ctx, &v);
964 static HRESULT interp_exp(exec_ctx_t *ctx)
972 hres = stack_pop_val(ctx, &r);
976 hres = stack_pop_val(ctx, &l);
977 if(SUCCEEDED(hres)) {
978 hres = VarPow(l.v, r.v, &v);
985 return stack_push(ctx, &v);
988 static HRESULT interp_neg(exec_ctx_t *ctx)
994 hres = stack_pop_val(ctx, &val);
998 hres = VarNeg(val.v, &v);
1003 return stack_push(ctx, &v);
1006 static const instr_func_t op_funcs[] = {
1007 #define X(x,n,a,b) interp_ ## x,
1012 static const unsigned op_move[] = {
1013 #define X(x,n,a,b) n,
1018 static void release_exec(exec_ctx_t *ctx)
1022 VariantClear(&ctx->ret_val);
1025 for(i=0; i < ctx->func->arg_cnt; i++)
1026 VariantClear(ctx->args+i);
1030 for(i=0; i < ctx->func->var_cnt; i++)
1031 VariantClear(ctx->vars+i);
1034 heap_free(ctx->args);
1035 heap_free(ctx->vars);
1036 heap_free(ctx->stack);
1039 HRESULT exec_script(script_ctx_t *ctx, function_t *func, DISPPARAMS *dp, VARIANT *res)
1041 exec_ctx_t exec = {func->code_ctx};
1043 HRESULT hres = S_OK;
1045 exec.code = func->code_ctx;
1047 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
1048 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
1056 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
1058 release_exec(&exec);
1059 return E_OUTOFMEMORY;
1062 for(i=0; i < func->arg_cnt; i++) {
1064 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
1065 if(func->args[i].by_ref)
1068 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
1070 hres = VariantCopy(exec.args+i, v);
1073 release_exec(&exec);
1082 exec.vars = heap_alloc_zero(func->var_cnt * sizeof(VARIANT));
1084 release_exec(&exec);
1085 return E_OUTOFMEMORY;
1091 exec.stack_size = 16;
1093 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
1095 release_exec(&exec);
1096 return E_OUTOFMEMORY;
1099 exec.instr = exec.code->instrs + func->code_off;
1104 op = exec.instr->op;
1105 hres = op_funcs[op](&exec);
1107 FIXME("Failed %08x\n", hres);
1108 stack_popn(&exec, exec.top);
1112 exec.instr += op_move[op];
1116 if(func->type != FUNC_FUNCTION)
1117 assert(V_VT(&exec.ret_val) == VT_EMPTY);
1119 if(SUCCEEDED(hres) && res) {
1120 *res = exec.ret_val;
1121 V_VT(&exec.ret_val) = VT_EMPTY;
1124 release_exec(&exec);