vbscript: Added compiler support for |not| expression.
[wine/multimedia.git] / dlls / vbscript / interp.c
blobf582e8069e1a6359f8272beab914d2e301a1ca05
1 /*
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
19 #include <assert.h>
21 #include "vbscript.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 typedef struct {
29 vbscode_t *code;
30 instr_t *instr;
31 script_ctx_t *script;
32 function_t *func;
34 unsigned stack_size;
35 unsigned top;
36 VARIANT *stack;
37 } exec_ctx_t;
39 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
41 typedef enum {
42 REF_NONE,
43 REF_DISP
44 } ref_type_t;
46 typedef struct {
47 ref_type_t type;
48 union {
49 struct {
50 IDispatch *disp;
51 DISPID id;
52 } d;
53 } u;
54 } ref_t;
56 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
58 named_item_t *item;
59 DISPID id;
60 HRESULT hres;
62 LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
63 if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
64 hres = disp_get_id(item->disp, name, &id);
65 if(SUCCEEDED(hres)) {
66 ref->type = REF_DISP;
67 ref->u.d.disp = item->disp;
68 ref->u.d.id = id;
69 return S_OK;
74 if(!ctx->func->code_ctx->option_explicit)
75 FIXME("create an attempt to set\n");
77 ref->type = REF_NONE;
78 return S_OK;
81 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
83 assert(ctx->top);
84 return ctx->stack + --ctx->top;
87 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
89 if(ctx->stack_size == ctx->top) {
90 VARIANT *new_stack;
92 new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
93 if(!new_stack) {
94 VariantClear(v);
95 return E_OUTOFMEMORY;
98 ctx->stack = new_stack;
99 ctx->stack_size *= 2;
102 ctx->stack[ctx->top++] = *v;
103 return S_OK;
106 static void stack_popn(exec_ctx_t *ctx, unsigned n)
108 while(n--)
109 VariantClear(stack_pop(ctx));
112 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
114 dp->cArgs = arg_cnt;
115 dp->rgdispidNamedArgs = NULL;
116 dp->cNamedArgs = 0;
118 if(arg_cnt) {
119 VARIANT tmp;
120 unsigned i;
122 assert(ctx->top >= arg_cnt);
124 for(i=1; i*2 <= arg_cnt; i++) {
125 tmp = ctx->stack[ctx->top-i];
126 ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
127 ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
130 dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
131 }else {
132 dp->rgvarg = NULL;
136 static HRESULT interp_icallv(exec_ctx_t *ctx)
138 BSTR identifier = ctx->instr->arg1.bstr;
139 const unsigned arg_cnt = ctx->instr->arg2.uint;
140 ref_t ref = {0};
141 DISPPARAMS dp;
142 HRESULT hres;
144 TRACE("\n");
146 hres = lookup_identifier(ctx, identifier, &ref);
147 if(FAILED(hres))
148 return hres;
150 vbstack_to_dp(ctx, arg_cnt, &dp);
152 switch(ref.type) {
153 case REF_DISP:
154 hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, NULL);
155 if(FAILED(hres))
156 return hres;
157 break;
158 default:
159 FIXME("%s not found\n", debugstr_w(identifier));
160 return DISP_E_UNKNOWNNAME;
163 stack_popn(ctx, arg_cnt);
164 return S_OK;
167 static HRESULT interp_ret(exec_ctx_t *ctx)
169 TRACE("\n");
171 ctx->instr = NULL;
172 return S_OK;
175 static HRESULT interp_bool(exec_ctx_t *ctx)
177 const VARIANT_BOOL arg = ctx->instr->arg1.lng;
178 VARIANT v;
180 TRACE("%s\n", arg ? "true" : "false");
182 V_VT(&v) = VT_BOOL;
183 V_BOOL(&v) = arg;
184 return stack_push(ctx, &v);
187 static HRESULT interp_string(exec_ctx_t *ctx)
189 VARIANT v;
191 TRACE("\n");
193 V_VT(&v) = VT_BSTR;
194 V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
195 if(!V_BSTR(&v))
196 return E_OUTOFMEMORY;
198 return stack_push(ctx, &v);
201 static HRESULT interp_not(exec_ctx_t *ctx)
203 FIXME("\n");
204 return E_NOTIMPL;
207 static const instr_func_t op_funcs[] = {
208 #define X(x,n,a,b) interp_ ## x,
209 OP_LIST
210 #undef X
213 static const unsigned op_move[] = {
214 #define X(x,n,a,b) n,
215 OP_LIST
216 #undef X
219 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
221 exec_ctx_t exec;
222 vbsop_t op;
223 HRESULT hres = S_OK;
225 exec.stack_size = 16;
226 exec.top = 0;
227 exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
228 if(!exec.stack)
229 return E_OUTOFMEMORY;
231 exec.code = func->code_ctx;
232 exec.instr = exec.code->instrs + func->code_off;
233 exec.script = ctx;
234 exec.func = func;
236 while(exec.instr) {
237 op = exec.instr->op;
238 hres = op_funcs[op](&exec);
239 if(FAILED(hres)) {
240 FIXME("Failed %08x\n", hres);
241 stack_popn(&exec, exec.top);
242 break;
245 exec.instr += op_move[op];
248 assert(!exec.top);
249 heap_free(exec.stack);
251 return hres;