vbscript: Added variable value expression support.
[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         FIXME("REF_VAR not implemented\n");
293         hres = E_NOTIMPL;
294         break;
295     case REF_DISP:
296         hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
297         if(own_val)
298             VariantClear(val);
299         break;
300     case REF_NONE:
301         FIXME("%s not found\n", debugstr_w(name));
302         if(own_val)
303             VariantClear(val);
304         return DISP_E_UNKNOWNNAME;
305     }
306
307     return hres;
308 }
309
310 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
311 {
312     const BSTR arg = ctx->instr->arg1.bstr;
313     variant_val_t v;
314     HRESULT hres;
315
316     TRACE("%s\n", debugstr_w(arg));
317
318     hres = stack_pop_val(ctx, &v);
319     if(FAILED(hres))
320         return hres;
321
322     return assign_ident(ctx, arg, v.v, v.owned);
323 }
324
325 static HRESULT interp_assign_member(exec_ctx_t *ctx)
326 {
327     BSTR identifier = ctx->instr->arg1.bstr;
328     variant_val_t val;
329     IDispatch *obj;
330     DISPID id;
331     HRESULT hres;
332
333     TRACE("%s\n", debugstr_w(identifier));
334
335     hres = stack_pop_disp(ctx, &obj);
336     if(FAILED(hres))
337         return hres;
338
339     if(!obj) {
340         FIXME("NULL obj\n");
341         return E_FAIL;
342     }
343
344     hres = stack_pop_val(ctx, &val);
345     if(FAILED(hres)) {
346         IDispatch_Release(obj);
347         return hres;
348     }
349
350     hres = disp_get_id(obj, identifier, &id);
351     if(SUCCEEDED(hres))
352         hres = disp_propput(ctx->script, obj, id, val.v);
353
354     release_val(&val);
355     IDispatch_Release(obj);
356     return hres;
357 }
358
359 static HRESULT interp_ret(exec_ctx_t *ctx)
360 {
361     TRACE("\n");
362
363     ctx->instr = NULL;
364     return S_OK;
365 }
366
367 static HRESULT interp_bool(exec_ctx_t *ctx)
368 {
369     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
370     VARIANT v;
371
372     TRACE("%s\n", arg ? "true" : "false");
373
374     V_VT(&v) = VT_BOOL;
375     V_BOOL(&v) = arg;
376     return stack_push(ctx, &v);
377 }
378
379 static HRESULT interp_string(exec_ctx_t *ctx)
380 {
381     VARIANT v;
382
383     TRACE("\n");
384
385     V_VT(&v) = VT_BSTR;
386     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
387     if(!V_BSTR(&v))
388         return E_OUTOFMEMORY;
389
390     return stack_push(ctx, &v);
391 }
392
393 static HRESULT interp_long(exec_ctx_t *ctx)
394 {
395     const LONG arg = ctx->instr->arg1.lng;
396     VARIANT v;
397
398     TRACE("%d\n", arg);
399
400     V_VT(&v) = VT_I4;
401     V_I4(&v) = arg;
402     return stack_push(ctx, &v);
403 }
404
405 static HRESULT interp_short(exec_ctx_t *ctx)
406 {
407     const LONG arg = ctx->instr->arg1.lng;
408     VARIANT v;
409
410     TRACE("%d\n", arg);
411
412     V_VT(&v) = VT_I2;
413     V_I2(&v) = arg;
414     return stack_push(ctx, &v);
415 }
416
417 static HRESULT interp_double(exec_ctx_t *ctx)
418 {
419     const DOUBLE *arg = ctx->instr->arg1.dbl;
420     VARIANT v;
421
422     TRACE("%lf\n", *arg);
423
424     V_VT(&v) = VT_R8;
425     V_R8(&v) = *arg;
426     return stack_push(ctx, &v);
427 }
428
429 static HRESULT interp_empty(exec_ctx_t *ctx)
430 {
431     VARIANT v;
432
433     TRACE("\n");
434
435     V_VT(&v) = VT_EMPTY;
436     return stack_push(ctx, &v);
437 }
438
439 static HRESULT interp_null(exec_ctx_t *ctx)
440 {
441     VARIANT v;
442
443     TRACE("\n");
444
445     V_VT(&v) = VT_NULL;
446     return stack_push(ctx, &v);
447 }
448
449 static HRESULT interp_not(exec_ctx_t *ctx)
450 {
451     variant_val_t val;
452     VARIANT v;
453     HRESULT hres;
454
455     TRACE("\n");
456
457     hres = stack_pop_val(ctx, &val);
458     if(FAILED(hres))
459         return hres;
460
461     hres = VarNot(val.v, &v);
462     release_val(&val);
463     if(FAILED(hres))
464         return hres;
465
466     return stack_push(ctx, &v);
467 }
468
469 static HRESULT cmp_oper(exec_ctx_t *ctx)
470 {
471     variant_val_t l, r;
472     HRESULT hres;
473
474     hres = stack_pop_val(ctx, &r);
475     if(FAILED(hres))
476         return hres;
477
478     hres = stack_pop_val(ctx, &l);
479     if(SUCCEEDED(hres)) {
480         if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
481             FIXME("comparing nulls is not implemented\n");
482             hres = E_NOTIMPL;
483         }else {
484             hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
485         }
486     }
487
488     release_val(&r);
489     release_val(&l);
490     return hres;
491 }
492
493 static HRESULT interp_equal(exec_ctx_t *ctx)
494 {
495     VARIANT v;
496     HRESULT hres;
497
498     TRACE("\n");
499
500     hres = cmp_oper(ctx);
501     if(FAILED(hres))
502         return hres;
503
504     V_VT(&v) = VT_BOOL;
505     V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
506     return stack_push(ctx, &v);
507 }
508
509 static HRESULT interp_nequal(exec_ctx_t *ctx)
510 {
511     VARIANT v;
512     HRESULT hres;
513
514     TRACE("\n");
515
516     hres = cmp_oper(ctx);
517     if(FAILED(hres))
518         return hres;
519
520     V_VT(&v) = VT_BOOL;
521     V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
522     return stack_push(ctx, &v);
523 }
524
525 static HRESULT interp_concat(exec_ctx_t *ctx)
526 {
527     variant_val_t r, l;
528     VARIANT v;
529     HRESULT hres;
530
531     TRACE("\n");
532
533     hres = stack_pop_val(ctx, &r);
534     if(FAILED(hres))
535         return hres;
536
537     hres = stack_pop_val(ctx, &l);
538     if(SUCCEEDED(hres)) {
539         hres = VarCat(l.v, r.v, &v);
540         release_val(&l);
541     }
542     release_val(&r);
543     if(FAILED(hres))
544         return hres;
545
546     return stack_push(ctx, &v);
547 }
548
549 static HRESULT interp_add(exec_ctx_t *ctx)
550 {
551     variant_val_t r, l;
552     VARIANT v;
553     HRESULT hres;
554
555     TRACE("\n");
556
557     hres = stack_pop_val(ctx, &r);
558     if(FAILED(hres))
559         return hres;
560
561     hres = stack_pop_val(ctx, &l);
562     if(SUCCEEDED(hres)) {
563         hres = VarAdd(l.v, r.v, &v);
564         release_val(&l);
565     }
566     release_val(&r);
567     if(FAILED(hres))
568         return hres;
569
570     return stack_push(ctx, &v);
571 }
572
573 static HRESULT interp_sub(exec_ctx_t *ctx)
574 {
575     variant_val_t r, l;
576     VARIANT v;
577     HRESULT hres;
578
579     TRACE("\n");
580
581     hres = stack_pop_val(ctx, &r);
582     if(FAILED(hres))
583         return hres;
584
585     hres = stack_pop_val(ctx, &l);
586     if(SUCCEEDED(hres)) {
587         hres = VarSub(l.v, r.v, &v);
588         release_val(&l);
589     }
590     release_val(&r);
591     if(FAILED(hres))
592         return hres;
593
594     return stack_push(ctx, &v);
595 }
596
597 static HRESULT interp_neg(exec_ctx_t *ctx)
598 {
599     variant_val_t val;
600     VARIANT v;
601     HRESULT hres;
602
603     hres = stack_pop_val(ctx, &val);
604     if(FAILED(hres))
605         return hres;
606
607     hres = VarNeg(val.v, &v);
608     release_val(&val);
609     if(FAILED(hres))
610         return hres;
611
612     return stack_push(ctx, &v);
613 }
614
615 static const instr_func_t op_funcs[] = {
616 #define X(x,n,a,b) interp_ ## x,
617 OP_LIST
618 #undef X
619 };
620
621 static const unsigned op_move[] = {
622 #define X(x,n,a,b) n,
623 OP_LIST
624 #undef X
625 };
626
627 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
628 {
629     exec_ctx_t exec;
630     vbsop_t op;
631     HRESULT hres = S_OK;
632
633     exec.stack_size = 16;
634     exec.top = 0;
635     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
636     if(!exec.stack)
637         return E_OUTOFMEMORY;
638
639     exec.code = func->code_ctx;
640     exec.instr = exec.code->instrs + func->code_off;
641     exec.script = ctx;
642     exec.func = func;
643
644     while(exec.instr) {
645         op = exec.instr->op;
646         hres = op_funcs[op](&exec);
647         if(FAILED(hres)) {
648             FIXME("Failed %08x\n", hres);
649             stack_popn(&exec, exec.top);
650             break;
651         }
652
653         exec.instr += op_move[op];
654     }
655
656     assert(!exec.top);
657     heap_free(exec.stack);
658
659     return hres;
660 }