vbscript: Added support for variable assignment statements.
[wine] / dlls / vbscript / interp.c
1 /*
2  * Copyright 2011 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <assert.h>
20
21 #include "vbscript.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
26
27
28 typedef struct {
29     vbscode_t *code;
30     instr_t *instr;
31     script_ctx_t *script;
32     function_t *func;
33
34     unsigned stack_size;
35     unsigned top;
36     VARIANT *stack;
37 } exec_ctx_t;
38
39 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
40
41 typedef enum {
42     REF_NONE,
43     REF_DISP,
44     REF_VAR
45 } ref_type_t;
46
47 typedef struct {
48     ref_type_t type;
49     union {
50         struct {
51             IDispatch *disp;
52             DISPID id;
53         } d;
54         VARIANT *v;
55     } u;
56 } ref_t;
57
58 typedef struct {
59     VARIANT *v;
60     VARIANT store;
61     BOOL owned;
62 } variant_val_t;
63
64 static BOOL lookup_dynamic_vars(dynamic_var_t *var, const WCHAR *name, ref_t *ref)
65 {
66     while(var) {
67         if(!strcmpiW(var->name, name)) {
68             ref->type = REF_VAR;
69             ref->u.v = &var->v;
70             return TRUE;
71         }
72
73         var = var->next;
74     }
75
76     return FALSE;
77 }
78
79 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
80 {
81     named_item_t *item;
82     DISPID id;
83     HRESULT hres;
84
85     if(lookup_dynamic_vars(ctx->script->global_vars, name, ref))
86         return S_OK;
87
88     LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
89         if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
90             hres = disp_get_id(item->disp, name, &id);
91             if(SUCCEEDED(hres)) {
92                 ref->type = REF_DISP;
93                 ref->u.d.disp = item->disp;
94                 ref->u.d.id = id;
95                 return S_OK;
96             }
97         }
98     }
99
100     if(!ctx->func->code_ctx->option_explicit)
101         FIXME("create an attempt to set\n");
102
103     ref->type = REF_NONE;
104     return S_OK;
105 }
106
107 static inline VARIANT *stack_pop(exec_ctx_t *ctx)
108 {
109     assert(ctx->top);
110     return ctx->stack + --ctx->top;
111 }
112
113 static HRESULT stack_push(exec_ctx_t *ctx, VARIANT *v)
114 {
115     if(ctx->stack_size == ctx->top) {
116         VARIANT *new_stack;
117
118         new_stack = heap_realloc(ctx->stack, ctx->stack_size*2);
119         if(!new_stack) {
120             VariantClear(v);
121             return E_OUTOFMEMORY;
122         }
123
124         ctx->stack = new_stack;
125         ctx->stack_size *= 2;
126     }
127
128     ctx->stack[ctx->top++] = *v;
129     return S_OK;
130 }
131
132 static void stack_popn(exec_ctx_t *ctx, unsigned n)
133 {
134     while(n--)
135         VariantClear(stack_pop(ctx));
136 }
137
138 static HRESULT stack_pop_val(exec_ctx_t *ctx, variant_val_t *v)
139 {
140     VARIANT *var;
141
142     var = stack_pop(ctx);
143
144     if(V_VT(var) == (VT_BYREF|VT_VARIANT)) {
145         v->owned = FALSE;
146         var = V_VARIANTREF(var);
147     }else {
148         v->owned = TRUE;
149     }
150
151     if(V_VT(var) == VT_DISPATCH) {
152         FIXME("got dispatch - get its default value\n");
153         return E_NOTIMPL;
154     }else {
155         v->v = var;
156     }
157
158     return S_OK;
159 }
160
161 static inline void release_val(variant_val_t *v)
162 {
163     if(v->owned)
164         VariantClear(v->v);
165 }
166
167 static HRESULT stack_pop_disp(exec_ctx_t *ctx, IDispatch **ret)
168 {
169     VARIANT *v = stack_pop(ctx);
170
171     if(V_VT(v) == VT_DISPATCH) {
172         *ret = V_DISPATCH(v);
173         return S_OK;
174     }
175
176     if(V_VT(v) != (VT_VARIANT|VT_BYREF)) {
177         FIXME("not supported type: %s\n", debugstr_variant(v));
178         VariantClear(v);
179         return E_FAIL;
180     }
181
182     v = V_BYREF(v);
183     if(V_VT(v) != VT_DISPATCH) {
184         FIXME("not disp %s\n", debugstr_variant(v));
185         return E_FAIL;
186     }
187
188     if(V_DISPATCH(v))
189         IDispatch_AddRef(V_DISPATCH(v));
190     *ret = V_DISPATCH(v);
191     return S_OK;
192 }
193
194 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
195 {
196     dp->cArgs = arg_cnt;
197     dp->rgdispidNamedArgs = NULL;
198     dp->cNamedArgs = 0;
199
200     if(arg_cnt) {
201         VARIANT tmp;
202         unsigned i;
203
204         assert(ctx->top >= arg_cnt);
205
206         for(i=1; i*2 <= arg_cnt; i++) {
207             tmp = ctx->stack[ctx->top-i];
208             ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
209             ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
210         }
211
212         dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
213     }else {
214         dp->rgvarg = NULL;
215     }
216 }
217
218 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
219 {
220     BSTR identifier = ctx->instr->arg1.bstr;
221     const unsigned arg_cnt = ctx->instr->arg2.uint;
222     ref_t ref = {0};
223     DISPPARAMS dp;
224     HRESULT hres;
225
226     hres = lookup_identifier(ctx, identifier, &ref);
227     if(FAILED(hres))
228         return hres;
229
230     vbstack_to_dp(ctx, arg_cnt, &dp);
231
232     switch(ref.type) {
233     case REF_VAR:
234         if(!res) {
235             FIXME("REF_VAR no res\n");
236             return E_NOTIMPL;
237         }
238
239         if(arg_cnt) {
240             FIXME("arguments not implemented\n");
241             return E_NOTIMPL;
242         }
243
244         V_VT(res) = VT_BYREF|VT_VARIANT;
245         V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
246         break;
247     case REF_DISP:
248         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
249         if(FAILED(hres))
250             return hres;
251         break;
252     case REF_NONE:
253         FIXME("%s not found\n", debugstr_w(identifier));
254         return DISP_E_UNKNOWNNAME;
255     }
256
257     stack_popn(ctx, arg_cnt);
258     return S_OK;
259 }
260
261 static HRESULT interp_icall(exec_ctx_t *ctx)
262 {
263     VARIANT v;
264     HRESULT hres;
265
266     TRACE("\n");
267
268     hres = do_icall(ctx, &v);
269     if(FAILED(hres))
270         return hres;
271
272     return stack_push(ctx, &v);
273 }
274
275 static HRESULT interp_icallv(exec_ctx_t *ctx)
276 {
277     TRACE("\n");
278     return do_icall(ctx, NULL);
279 }
280
281 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
282 {
283     ref_t ref;
284     HRESULT hres;
285
286     hres = lookup_identifier(ctx, name, &ref);
287     if(FAILED(hres))
288         return hres;
289
290     switch(ref.type) {
291     case REF_VAR: {
292         VARIANT *v = ref.u.v;
293
294         if(V_VT(v) == (VT_VARIANT|VT_BYREF))
295             v = V_VARIANTREF(v);
296
297         if(own_val) {
298             VariantClear(v);
299             *v = *val;
300             hres = S_OK;
301         }else {
302             hres = VariantCopy(v, val);
303         }
304         break;
305     }
306     case REF_DISP:
307         hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
308         if(own_val)
309             VariantClear(val);
310         break;
311     case REF_NONE:
312         FIXME("%s not found\n", debugstr_w(name));
313         if(own_val)
314             VariantClear(val);
315         return DISP_E_UNKNOWNNAME;
316     }
317
318     return hres;
319 }
320
321 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
322 {
323     const BSTR arg = ctx->instr->arg1.bstr;
324     variant_val_t v;
325     HRESULT hres;
326
327     TRACE("%s\n", debugstr_w(arg));
328
329     hres = stack_pop_val(ctx, &v);
330     if(FAILED(hres))
331         return hres;
332
333     return assign_ident(ctx, arg, v.v, v.owned);
334 }
335
336 static HRESULT interp_assign_member(exec_ctx_t *ctx)
337 {
338     BSTR identifier = ctx->instr->arg1.bstr;
339     variant_val_t val;
340     IDispatch *obj;
341     DISPID id;
342     HRESULT hres;
343
344     TRACE("%s\n", debugstr_w(identifier));
345
346     hres = stack_pop_disp(ctx, &obj);
347     if(FAILED(hres))
348         return hres;
349
350     if(!obj) {
351         FIXME("NULL obj\n");
352         return E_FAIL;
353     }
354
355     hres = stack_pop_val(ctx, &val);
356     if(FAILED(hres)) {
357         IDispatch_Release(obj);
358         return hres;
359     }
360
361     hres = disp_get_id(obj, identifier, &id);
362     if(SUCCEEDED(hres))
363         hres = disp_propput(ctx->script, obj, id, val.v);
364
365     release_val(&val);
366     IDispatch_Release(obj);
367     return hres;
368 }
369
370 static HRESULT interp_ret(exec_ctx_t *ctx)
371 {
372     TRACE("\n");
373
374     ctx->instr = NULL;
375     return S_OK;
376 }
377
378 static HRESULT interp_bool(exec_ctx_t *ctx)
379 {
380     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
381     VARIANT v;
382
383     TRACE("%s\n", arg ? "true" : "false");
384
385     V_VT(&v) = VT_BOOL;
386     V_BOOL(&v) = arg;
387     return stack_push(ctx, &v);
388 }
389
390 static HRESULT interp_string(exec_ctx_t *ctx)
391 {
392     VARIANT v;
393
394     TRACE("\n");
395
396     V_VT(&v) = VT_BSTR;
397     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
398     if(!V_BSTR(&v))
399         return E_OUTOFMEMORY;
400
401     return stack_push(ctx, &v);
402 }
403
404 static HRESULT interp_long(exec_ctx_t *ctx)
405 {
406     const LONG arg = ctx->instr->arg1.lng;
407     VARIANT v;
408
409     TRACE("%d\n", arg);
410
411     V_VT(&v) = VT_I4;
412     V_I4(&v) = arg;
413     return stack_push(ctx, &v);
414 }
415
416 static HRESULT interp_short(exec_ctx_t *ctx)
417 {
418     const LONG arg = ctx->instr->arg1.lng;
419     VARIANT v;
420
421     TRACE("%d\n", arg);
422
423     V_VT(&v) = VT_I2;
424     V_I2(&v) = arg;
425     return stack_push(ctx, &v);
426 }
427
428 static HRESULT interp_double(exec_ctx_t *ctx)
429 {
430     const DOUBLE *arg = ctx->instr->arg1.dbl;
431     VARIANT v;
432
433     TRACE("%lf\n", *arg);
434
435     V_VT(&v) = VT_R8;
436     V_R8(&v) = *arg;
437     return stack_push(ctx, &v);
438 }
439
440 static HRESULT interp_empty(exec_ctx_t *ctx)
441 {
442     VARIANT v;
443
444     TRACE("\n");
445
446     V_VT(&v) = VT_EMPTY;
447     return stack_push(ctx, &v);
448 }
449
450 static HRESULT interp_null(exec_ctx_t *ctx)
451 {
452     VARIANT v;
453
454     TRACE("\n");
455
456     V_VT(&v) = VT_NULL;
457     return stack_push(ctx, &v);
458 }
459
460 static HRESULT interp_not(exec_ctx_t *ctx)
461 {
462     variant_val_t val;
463     VARIANT v;
464     HRESULT hres;
465
466     TRACE("\n");
467
468     hres = stack_pop_val(ctx, &val);
469     if(FAILED(hres))
470         return hres;
471
472     hres = VarNot(val.v, &v);
473     release_val(&val);
474     if(FAILED(hres))
475         return hres;
476
477     return stack_push(ctx, &v);
478 }
479
480 static HRESULT cmp_oper(exec_ctx_t *ctx)
481 {
482     variant_val_t l, r;
483     HRESULT hres;
484
485     hres = stack_pop_val(ctx, &r);
486     if(FAILED(hres))
487         return hres;
488
489     hres = stack_pop_val(ctx, &l);
490     if(SUCCEEDED(hres)) {
491         if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
492             FIXME("comparing nulls is not implemented\n");
493             hres = E_NOTIMPL;
494         }else {
495             hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
496         }
497     }
498
499     release_val(&r);
500     release_val(&l);
501     return hres;
502 }
503
504 static HRESULT interp_equal(exec_ctx_t *ctx)
505 {
506     VARIANT v;
507     HRESULT hres;
508
509     TRACE("\n");
510
511     hres = cmp_oper(ctx);
512     if(FAILED(hres))
513         return hres;
514
515     V_VT(&v) = VT_BOOL;
516     V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
517     return stack_push(ctx, &v);
518 }
519
520 static HRESULT interp_nequal(exec_ctx_t *ctx)
521 {
522     VARIANT v;
523     HRESULT hres;
524
525     TRACE("\n");
526
527     hres = cmp_oper(ctx);
528     if(FAILED(hres))
529         return hres;
530
531     V_VT(&v) = VT_BOOL;
532     V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
533     return stack_push(ctx, &v);
534 }
535
536 static HRESULT interp_concat(exec_ctx_t *ctx)
537 {
538     variant_val_t r, l;
539     VARIANT v;
540     HRESULT hres;
541
542     TRACE("\n");
543
544     hres = stack_pop_val(ctx, &r);
545     if(FAILED(hres))
546         return hres;
547
548     hres = stack_pop_val(ctx, &l);
549     if(SUCCEEDED(hres)) {
550         hres = VarCat(l.v, r.v, &v);
551         release_val(&l);
552     }
553     release_val(&r);
554     if(FAILED(hres))
555         return hres;
556
557     return stack_push(ctx, &v);
558 }
559
560 static HRESULT interp_add(exec_ctx_t *ctx)
561 {
562     variant_val_t r, l;
563     VARIANT v;
564     HRESULT hres;
565
566     TRACE("\n");
567
568     hres = stack_pop_val(ctx, &r);
569     if(FAILED(hres))
570         return hres;
571
572     hres = stack_pop_val(ctx, &l);
573     if(SUCCEEDED(hres)) {
574         hres = VarAdd(l.v, r.v, &v);
575         release_val(&l);
576     }
577     release_val(&r);
578     if(FAILED(hres))
579         return hres;
580
581     return stack_push(ctx, &v);
582 }
583
584 static HRESULT interp_sub(exec_ctx_t *ctx)
585 {
586     variant_val_t r, l;
587     VARIANT v;
588     HRESULT hres;
589
590     TRACE("\n");
591
592     hres = stack_pop_val(ctx, &r);
593     if(FAILED(hres))
594         return hres;
595
596     hres = stack_pop_val(ctx, &l);
597     if(SUCCEEDED(hres)) {
598         hres = VarSub(l.v, r.v, &v);
599         release_val(&l);
600     }
601     release_val(&r);
602     if(FAILED(hres))
603         return hres;
604
605     return stack_push(ctx, &v);
606 }
607
608 static HRESULT interp_neg(exec_ctx_t *ctx)
609 {
610     variant_val_t val;
611     VARIANT v;
612     HRESULT hres;
613
614     hres = stack_pop_val(ctx, &val);
615     if(FAILED(hres))
616         return hres;
617
618     hres = VarNeg(val.v, &v);
619     release_val(&val);
620     if(FAILED(hres))
621         return hres;
622
623     return stack_push(ctx, &v);
624 }
625
626 static const instr_func_t op_funcs[] = {
627 #define X(x,n,a,b) interp_ ## x,
628 OP_LIST
629 #undef X
630 };
631
632 static const unsigned op_move[] = {
633 #define X(x,n,a,b) n,
634 OP_LIST
635 #undef X
636 };
637
638 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
639 {
640     exec_ctx_t exec;
641     vbsop_t op;
642     HRESULT hres = S_OK;
643
644     exec.stack_size = 16;
645     exec.top = 0;
646     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
647     if(!exec.stack)
648         return E_OUTOFMEMORY;
649
650     exec.code = func->code_ctx;
651     exec.instr = exec.code->instrs + func->code_off;
652     exec.script = ctx;
653     exec.func = func;
654
655     while(exec.instr) {
656         op = exec.instr->op;
657         hres = op_funcs[op](&exec);
658         if(FAILED(hres)) {
659             FIXME("Failed %08x\n", hres);
660             stack_popn(&exec, exec.top);
661             break;
662         }
663
664         exec.instr += op_move[op];
665     }
666
667     assert(!exec.top);
668     heap_free(exec.stack);
669
670     return hres;
671 }