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);
41 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
68 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
71 if(!strcmpiW(var->name, name)) {
83 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
91 for(i=0; i < ctx->func->arg_cnt; i++) {
92 if(!strcmpiW(ctx->func->args[i].name, name)) {
94 ref->u.v = ctx->args+i;
99 if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
102 for(func = ctx->script->global_funcs; func; func = func->next) {
103 if(!strcmpiW(func->name, name)) {
104 ref->type = REF_FUNC;
110 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
111 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
112 hres = disp_get_id(item->disp, name, &id);
113 if(SUCCEEDED(hres)) {
114 ref->type = REF_DISP;
115 ref->u.d.disp = item->disp;
122 if(!ctx->func->code_ctx->option_explicit)
123 FIXME("create an attempt to set\n");
125 ref->type = REF_NONE;
129 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
132 return ctx->stack + --ctx->top;
135 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
137 if(ctx->stack_size == ctx->top) {
140 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
143 return E_OUTOFMEMORY;
146 ctx->stack = new_stack;
147 ctx->stack_size *= 2;
150 ctx->stack[ctx->top++] = *v;
154 static void stack_popn(exec_ctx_t *ctx, unsigned n)
157 VariantClear(stack_pop(ctx));
160 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
164 var = stack_pop(ctx);
166 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
168 var = V_VARIANTREF(var);
173 if(V_VT(var) == VT_DISPATCH) {
174 FIXME("got dispatch - get its default value\n");
183 static inline void release_val(variant_val_t *v)
189 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
191 VARIANT *v = stack_pop(ctx);
193 if(V_VT(v) == VT_DISPATCH) {
194 *ret = V_DISPATCH(v);
198 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
199 FIXME("not supported type: %s\n", debugstr_variant(v));
205 if(V_VT(v) != VT_DISPATCH) {
206 FIXME("not disp %s\n", debugstr_variant(v));
211 IDispatch_AddRef(V_DISPATCH(v));
212 *ret = V_DISPATCH(v);
216 static inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
218 ctx->instr = ctx->code->instrs + addr;
221 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
224 dp->rgdispidNamedArgs = NULL;
231 assert(ctx->top >= arg_cnt);
233 for(i=1; i*2 <= arg_cnt; i++) {
234 tmp = ctx->stack[ctx->top-i];
235 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
236 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
239 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
245 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
247 BSTR identifier = ctx->instr->arg1.bstr;
248 const unsigned arg_cnt = ctx->instr->arg2.uint;
253 hres = lookup_identifier(ctx, identifier, &ref);
257 vbstack_to_dp(ctx, arg_cnt, &dp);
262 FIXME("REF_VAR no res\n");
267 FIXME("arguments not implemented\n");
271 V_VT(res) = VT_BYREF|VT_VARIANT;
272 V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
275 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
280 hres = exec_script(ctx->script, ref.u.f, &dp, res);
285 FIXME("%s not found\n", debugstr_w(identifier));
286 return DISP_E_UNKNOWNNAME;
289 stack_popn(ctx, arg_cnt);
293 static HRESULT interp_icall(exec_ctx_t *ctx)
300 hres = do_icall(ctx, &v);
304 return stack_push(ctx, &v);
307 static HRESULT interp_icallv(exec_ctx_t *ctx)
310 return do_icall(ctx, NULL);
313 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
318 hres = lookup_identifier(ctx, name, &ref);
324 VARIANT *v = ref.u.v;
326 if(V_VT(v) == (VT_VARIANT|VT_BYREF))
334 hres = VariantCopy(v, val);
339 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
344 FIXME("functions not implemented\n");
347 FIXME("%s not found\n", debugstr_w(name));
350 return DISP_E_UNKNOWNNAME;
356 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
358 const BSTR arg = ctx->instr->arg1.bstr;
362 TRACE("%s\n", debugstr_w(arg));
364 hres = stack_pop_val(ctx, &v);
368 return assign_ident(ctx, arg, v.v, v.owned);
371 static HRESULT interp_assign_member(exec_ctx_t *ctx)
373 BSTR identifier = ctx->instr->arg1.bstr;
379 TRACE("%s\n", debugstr_w(identifier));
381 hres = stack_pop_disp(ctx, &obj);
390 hres = stack_pop_val(ctx, &val);
392 IDispatch_Release(obj);
396 hres = disp_get_id(obj, identifier, &id);
398 hres = disp_propput(ctx->script, obj, id, val.v);
401 IDispatch_Release(obj);
405 static HRESULT interp_jmp(exec_ctx_t *ctx)
407 const unsigned arg = ctx->instr->arg1.uint;
415 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
417 const unsigned arg = ctx->instr->arg1.uint;
423 hres = stack_pop_val(ctx, &val);
427 if(V_VT(val.v) != VT_BOOL) {
428 FIXME("unsupported for %s\n", debugstr_variant(val.v));
436 instr_jmp(ctx, ctx->instr->arg1.uint);
440 static HRESULT interp_ret(exec_ctx_t *ctx)
448 static HRESULT interp_bool(exec_ctx_t *ctx)
450 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
453 TRACE("%s\n", arg ? "true" : "false");
457 return stack_push(ctx, &v);
460 static HRESULT interp_string(exec_ctx_t *ctx)
467 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
469 return E_OUTOFMEMORY;
471 return stack_push(ctx, &v);
474 static HRESULT interp_long(exec_ctx_t *ctx)
476 const LONG arg = ctx->instr->arg1.lng;
483 return stack_push(ctx, &v);
486 static HRESULT interp_short(exec_ctx_t *ctx)
488 const LONG arg = ctx->instr->arg1.lng;
495 return stack_push(ctx, &v);
498 static HRESULT interp_double(exec_ctx_t *ctx)
500 const DOUBLE *arg = ctx->instr->arg1.dbl;
503 TRACE("%lf\n", *arg);
507 return stack_push(ctx, &v);
510 static HRESULT interp_empty(exec_ctx_t *ctx)
517 return stack_push(ctx, &v);
520 static HRESULT interp_null(exec_ctx_t *ctx)
527 return stack_push(ctx, &v);
530 static HRESULT interp_not(exec_ctx_t *ctx)
538 hres = stack_pop_val(ctx, &val);
542 hres = VarNot(val.v, &v);
547 return stack_push(ctx, &v);
550 static HRESULT cmp_oper(exec_ctx_t *ctx)
555 hres = stack_pop_val(ctx, &r);
559 hres = stack_pop_val(ctx, &l);
560 if(SUCCEEDED(hres)) {
561 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
562 FIXME("comparing nulls is not implemented\n");
565 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
574 static HRESULT interp_equal(exec_ctx_t *ctx)
581 hres = cmp_oper(ctx);
586 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
587 return stack_push(ctx, &v);
590 static HRESULT interp_nequal(exec_ctx_t *ctx)
597 hres = cmp_oper(ctx);
602 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
603 return stack_push(ctx, &v);
606 static HRESULT interp_concat(exec_ctx_t *ctx)
614 hres = stack_pop_val(ctx, &r);
618 hres = stack_pop_val(ctx, &l);
619 if(SUCCEEDED(hres)) {
620 hres = VarCat(l.v, r.v, &v);
627 return stack_push(ctx, &v);
630 static HRESULT interp_add(exec_ctx_t *ctx)
638 hres = stack_pop_val(ctx, &r);
642 hres = stack_pop_val(ctx, &l);
643 if(SUCCEEDED(hres)) {
644 hres = VarAdd(l.v, r.v, &v);
651 return stack_push(ctx, &v);
654 static HRESULT interp_sub(exec_ctx_t *ctx)
662 hres = stack_pop_val(ctx, &r);
666 hres = stack_pop_val(ctx, &l);
667 if(SUCCEEDED(hres)) {
668 hres = VarSub(l.v, r.v, &v);
675 return stack_push(ctx, &v);
678 static HRESULT interp_mod(exec_ctx_t *ctx)
686 hres = stack_pop_val(ctx, &r);
690 hres = stack_pop_val(ctx, &l);
691 if(SUCCEEDED(hres)) {
692 hres = VarMod(l.v, r.v, &v);
699 return stack_push(ctx, &v);
702 static HRESULT interp_idiv(exec_ctx_t *ctx)
710 hres = stack_pop_val(ctx, &r);
714 hres = stack_pop_val(ctx, &l);
715 if(SUCCEEDED(hres)) {
716 hres = VarIdiv(l.v, r.v, &v);
723 return stack_push(ctx, &v);
726 static HRESULT interp_div(exec_ctx_t *ctx)
734 hres = stack_pop_val(ctx, &r);
738 hres = stack_pop_val(ctx, &l);
739 if(SUCCEEDED(hres)) {
740 hres = VarDiv(l.v, r.v, &v);
747 return stack_push(ctx, &v);
750 static HRESULT interp_mul(exec_ctx_t *ctx)
758 hres = stack_pop_val(ctx, &r);
762 hres = stack_pop_val(ctx, &l);
763 if(SUCCEEDED(hres)) {
764 hres = VarMul(l.v, r.v, &v);
771 return stack_push(ctx, &v);
774 static HRESULT interp_exp(exec_ctx_t *ctx)
782 hres = stack_pop_val(ctx, &r);
786 hres = stack_pop_val(ctx, &l);
787 if(SUCCEEDED(hres)) {
788 hres = VarPow(l.v, r.v, &v);
795 return stack_push(ctx, &v);
798 static HRESULT interp_neg(exec_ctx_t *ctx)
804 hres = stack_pop_val(ctx, &val);
808 hres = VarNeg(val.v, &v);
813 return stack_push(ctx, &v);
816 static const instr_func_t op_funcs[] = {
817 #define X(x,n,a,b) interp_ ## x,
822 static const unsigned op_move[] = {
823 #define X(x,n,a,b) n,
828 static void release_exec(exec_ctx_t *ctx)
833 for(i=0; i < ctx->func->arg_cnt; i++)
834 VariantClear(ctx->args+i);
837 heap_free(ctx->args);
838 heap_free(ctx->stack);
841 HRESULT exec_script(script_ctx_t *ctx, function_t *func, DISPPARAMS *dp, VARIANT *res)
843 exec_ctx_t exec = {func->code_ctx};
848 FIXME("returning value is not implemented\n");
852 exec.code = func->code_ctx;
854 if(dp ? func->arg_cnt != arg_cnt(dp) : func->arg_cnt) {
855 FIXME("wrong arg_cnt %d, expected %d\n", dp ? arg_cnt(dp) : 0, func->arg_cnt);
863 exec.args = heap_alloc_zero(func->arg_cnt * sizeof(VARIANT));
866 return E_OUTOFMEMORY;
869 for(i=0; i < func->arg_cnt; i++) {
871 if(V_VT(v) == (VT_VARIANT|VT_BYREF)) {
872 if(func->args[i].by_ref)
875 hres = VariantCopy(exec.args+i, V_VARIANTREF(v));
877 hres = VariantCopy(exec.args+i, v);
888 exec.stack_size = 16;
890 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
893 return E_OUTOFMEMORY;
896 exec.instr = exec.code->instrs + func->code_off;
902 hres = op_funcs[op](&exec);
904 FIXME("Failed %08x\n", hres);
905 stack_popn(&exec, exec.top);
909 exec.instr += op_move[op];