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);
39 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
62 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
68 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
69 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
70 hres = disp_get_id(item->disp, name, &id);
73 ref->u.d.disp = item->disp;
80 if(!ctx->func->code_ctx->option_explicit)
81 FIXME("create an attempt to set\n");
87 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
90 return ctx->stack + --ctx->top;
93 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
95 if(ctx->stack_size == ctx->top) {
98 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
101 return E_OUTOFMEMORY;
104 ctx->stack = new_stack;
105 ctx->stack_size *= 2;
108 ctx->stack[ctx->top++] = *v;
112 static void stack_popn(exec_ctx_t *ctx, unsigned n)
115 VariantClear(stack_pop(ctx));
118 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
122 var = stack_pop(ctx);
124 if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
126 var = V_VARIANTREF(var);
131 if(V_VT(var) == VT_DISPATCH) {
132 FIXME("got dispatch - get its default value\n");
141 static inline void release_val(variant_val_t *v)
147 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
149 VARIANT *v = stack_pop(ctx);
151 if(V_VT(v) == VT_DISPATCH) {
152 *ret = V_DISPATCH(v);
156 if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
157 FIXME("not supported type: %s\n", debugstr_variant(v));
163 if(V_VT(v) != VT_DISPATCH) {
164 FIXME("not disp %s\n", debugstr_variant(v));
169 IDispatch_AddRef(V_DISPATCH(v));
170 *ret = V_DISPATCH(v);
174 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
177 dp->rgdispidNamedArgs = NULL;
184 assert(ctx->top >= arg_cnt);
186 for(i=1; i*2 <= arg_cnt; i++) {
187 tmp = ctx->stack[ctx->top-i];
188 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
189 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
192 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
198 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
200 BSTR identifier = ctx->instr->arg1.bstr;
201 const unsigned arg_cnt = ctx->instr->arg2.uint;
206 hres = lookup_identifier(ctx, identifier, &ref);
210 vbstack_to_dp(ctx, arg_cnt, &dp);
214 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
219 FIXME("%s not found\n", debugstr_w(identifier));
220 return DISP_E_UNKNOWNNAME;
223 stack_popn(ctx, arg_cnt);
227 static HRESULT interp_icall(exec_ctx_t *ctx)
234 hres = do_icall(ctx, &v);
238 return stack_push(ctx, &v);
241 static HRESULT interp_icallv(exec_ctx_t *ctx)
244 return do_icall(ctx, NULL);
247 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
252 hres = lookup_identifier(ctx, name, &ref);
258 hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
263 FIXME("%s not found\n", debugstr_w(name));
266 return DISP_E_UNKNOWNNAME;
272 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
274 const BSTR arg = ctx->instr->arg1.bstr;
278 TRACE("%s\n", debugstr_w(arg));
280 hres = stack_pop_val(ctx, &v);
284 return assign_ident(ctx, arg, v.v, v.owned);
287 static HRESULT interp_assign_member(exec_ctx_t *ctx)
289 BSTR identifier = ctx->instr->arg1.bstr;
295 TRACE("%s\n", debugstr_w(identifier));
297 hres = stack_pop_disp(ctx, &obj);
306 hres = stack_pop_val(ctx, &val);
308 IDispatch_Release(obj);
312 hres = disp_get_id(obj, identifier, &id);
314 hres = disp_propput(ctx->script, obj, id, val.v);
317 IDispatch_Release(obj);
321 static HRESULT interp_ret(exec_ctx_t *ctx)
329 static HRESULT interp_bool(exec_ctx_t *ctx)
331 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
334 TRACE("%s\n", arg ? "true" : "false");
338 return stack_push(ctx, &v);
341 static HRESULT interp_string(exec_ctx_t *ctx)
348 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
350 return E_OUTOFMEMORY;
352 return stack_push(ctx, &v);
355 static HRESULT interp_long(exec_ctx_t *ctx)
357 const LONG arg = ctx->instr->arg1.lng;
364 return stack_push(ctx, &v);
367 static HRESULT interp_short(exec_ctx_t *ctx)
369 const LONG arg = ctx->instr->arg1.lng;
376 return stack_push(ctx, &v);
379 static HRESULT interp_double(exec_ctx_t *ctx)
381 const DOUBLE *arg = ctx->instr->arg1.dbl;
384 TRACE("%lf\n", *arg);
388 return stack_push(ctx, &v);
391 static HRESULT interp_empty(exec_ctx_t *ctx)
398 return stack_push(ctx, &v);
401 static HRESULT interp_null(exec_ctx_t *ctx)
408 return stack_push(ctx, &v);
411 static HRESULT interp_not(exec_ctx_t *ctx)
419 hres = stack_pop_val(ctx, &val);
423 hres = VarNot(val.v, &v);
428 return stack_push(ctx, &v);
431 static HRESULT cmp_oper(exec_ctx_t *ctx)
436 hres = stack_pop_val(ctx, &r);
440 hres = stack_pop_val(ctx, &l);
441 if(SUCCEEDED(hres)) {
442 if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
443 FIXME("comparing nulls is not implemented\n");
446 hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
455 static HRESULT interp_equal(exec_ctx_t *ctx)
462 hres = cmp_oper(ctx);
467 V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
468 return stack_push(ctx, &v);
471 static HRESULT interp_nequal(exec_ctx_t *ctx)
478 hres = cmp_oper(ctx);
483 V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
484 return stack_push(ctx, &v);
487 static HRESULT interp_concat(exec_ctx_t *ctx)
495 hres = stack_pop_val(ctx, &r);
499 hres = stack_pop_val(ctx, &l);
500 if(SUCCEEDED(hres)) {
501 hres = VarCat(l.v, r.v, &v);
508 return stack_push(ctx, &v);
511 static HRESULT interp_add(exec_ctx_t *ctx)
519 hres = stack_pop_val(ctx, &r);
523 hres = stack_pop_val(ctx, &l);
524 if(SUCCEEDED(hres)) {
525 hres = VarAdd(l.v, r.v, &v);
532 return stack_push(ctx, &v);
535 static HRESULT interp_sub(exec_ctx_t *ctx)
543 hres = stack_pop_val(ctx, &r);
547 hres = stack_pop_val(ctx, &l);
548 if(SUCCEEDED(hres)) {
549 hres = VarSub(l.v, r.v, &v);
556 return stack_push(ctx, &v);
559 static HRESULT interp_neg(exec_ctx_t *ctx)
565 hres = stack_pop_val(ctx, &val);
569 hres = VarNeg(val.v, &v);
574 return stack_push(ctx, &v);
577 static const instr_func_t op_funcs[] = {
578 #define X(x,n,a,b) interp_ ## x,
583 static const unsigned op_move[] = {
584 #define X(x,n,a,b) n,
589 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
595 exec.stack_size = 16;
597 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
599 return E_OUTOFMEMORY;
601 exec.code = func->code_ctx;
602 exec.instr = exec.code->instrs + func->code_off;
608 hres = op_funcs[op](&exec);
610 FIXME("Failed %08x\n", hres);
611 stack_popn(&exec, exec.top);
615 exec.instr += op_move[op];
619 heap_free(exec.stack);