vbscript: Added interp_jmp implementation.
[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 inline void instr_jmp(exec_ctx_t *ctx, unsigned addr)
195 {
196     ctx->instr = ctx->code->instrs + addr;
197 }
198
199 static void vbstack_to_dp(exec_ctx_t *ctx, unsigned arg_cnt, DISPPARAMS *dp)
200 {
201     dp->cArgs = arg_cnt;
202     dp->rgdispidNamedArgs = NULL;
203     dp->cNamedArgs = 0;
204
205     if(arg_cnt) {
206         VARIANT tmp;
207         unsigned i;
208
209         assert(ctx->top >= arg_cnt);
210
211         for(i=1; i*2 <= arg_cnt; i++) {
212             tmp = ctx->stack[ctx->top-i];
213             ctx->stack[ctx->top-i] = ctx->stack[ctx->top-arg_cnt+i-1];
214             ctx->stack[ctx->top-arg_cnt+i-1] = tmp;
215         }
216
217         dp->rgvarg = ctx->stack + ctx->top-arg_cnt;
218     }else {
219         dp->rgvarg = NULL;
220     }
221 }
222
223 static HRESULT do_icall(exec_ctx_t *ctx, VARIANT *res)
224 {
225     BSTR identifier = ctx->instr->arg1.bstr;
226     const unsigned arg_cnt = ctx->instr->arg2.uint;
227     ref_t ref = {0};
228     DISPPARAMS dp;
229     HRESULT hres;
230
231     hres = lookup_identifier(ctx, identifier, &ref);
232     if(FAILED(hres))
233         return hres;
234
235     vbstack_to_dp(ctx, arg_cnt, &dp);
236
237     switch(ref.type) {
238     case REF_VAR:
239         if(!res) {
240             FIXME("REF_VAR no res\n");
241             return E_NOTIMPL;
242         }
243
244         if(arg_cnt) {
245             FIXME("arguments not implemented\n");
246             return E_NOTIMPL;
247         }
248
249         V_VT(res) = VT_BYREF|VT_VARIANT;
250         V_BYREF(res) = V_VT(ref.u.v) == (VT_VARIANT|VT_BYREF) ? V_VARIANTREF(ref.u.v) : ref.u.v;
251         break;
252     case REF_DISP:
253         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, res);
254         if(FAILED(hres))
255             return hres;
256         break;
257     case REF_NONE:
258         FIXME("%s not found\n", debugstr_w(identifier));
259         return DISP_E_UNKNOWNNAME;
260     }
261
262     stack_popn(ctx, arg_cnt);
263     return S_OK;
264 }
265
266 static HRESULT interp_icall(exec_ctx_t *ctx)
267 {
268     VARIANT v;
269     HRESULT hres;
270
271     TRACE("\n");
272
273     hres = do_icall(ctx, &v);
274     if(FAILED(hres))
275         return hres;
276
277     return stack_push(ctx, &v);
278 }
279
280 static HRESULT interp_icallv(exec_ctx_t *ctx)
281 {
282     TRACE("\n");
283     return do_icall(ctx, NULL);
284 }
285
286 static HRESULT assign_ident(exec_ctx_t *ctx, BSTR name, VARIANT *val, BOOL own_val)
287 {
288     ref_t ref;
289     HRESULT hres;
290
291     hres = lookup_identifier(ctx, name, &ref);
292     if(FAILED(hres))
293         return hres;
294
295     switch(ref.type) {
296     case REF_VAR: {
297         VARIANT *v = ref.u.v;
298
299         if(V_VT(v) == (VT_VARIANT|VT_BYREF))
300             v = V_VARIANTREF(v);
301
302         if(own_val) {
303             VariantClear(v);
304             *v = *val;
305             hres = S_OK;
306         }else {
307             hres = VariantCopy(v, val);
308         }
309         break;
310     }
311     case REF_DISP:
312         hres = disp_propput(ctx->script, ref.u.d.disp, ref.u.d.id, val);
313         if(own_val)
314             VariantClear(val);
315         break;
316     case REF_NONE:
317         FIXME("%s not found\n", debugstr_w(name));
318         if(own_val)
319             VariantClear(val);
320         return DISP_E_UNKNOWNNAME;
321     }
322
323     return hres;
324 }
325
326 static HRESULT interp_assign_ident(exec_ctx_t *ctx)
327 {
328     const BSTR arg = ctx->instr->arg1.bstr;
329     variant_val_t v;
330     HRESULT hres;
331
332     TRACE("%s\n", debugstr_w(arg));
333
334     hres = stack_pop_val(ctx, &v);
335     if(FAILED(hres))
336         return hres;
337
338     return assign_ident(ctx, arg, v.v, v.owned);
339 }
340
341 static HRESULT interp_assign_member(exec_ctx_t *ctx)
342 {
343     BSTR identifier = ctx->instr->arg1.bstr;
344     variant_val_t val;
345     IDispatch *obj;
346     DISPID id;
347     HRESULT hres;
348
349     TRACE("%s\n", debugstr_w(identifier));
350
351     hres = stack_pop_disp(ctx, &obj);
352     if(FAILED(hres))
353         return hres;
354
355     if(!obj) {
356         FIXME("NULL obj\n");
357         return E_FAIL;
358     }
359
360     hres = stack_pop_val(ctx, &val);
361     if(FAILED(hres)) {
362         IDispatch_Release(obj);
363         return hres;
364     }
365
366     hres = disp_get_id(obj, identifier, &id);
367     if(SUCCEEDED(hres))
368         hres = disp_propput(ctx->script, obj, id, val.v);
369
370     release_val(&val);
371     IDispatch_Release(obj);
372     return hres;
373 }
374
375 static HRESULT interp_jmp(exec_ctx_t *ctx)
376 {
377     const unsigned arg = ctx->instr->arg1.uint;
378
379     TRACE("%u\n", arg);
380
381     instr_jmp(ctx, arg);
382     return S_OK;
383 }
384
385 static HRESULT interp_jmp_false(exec_ctx_t *ctx)
386 {
387     FIXME("\n");
388     return E_NOTIMPL;
389 }
390
391 static HRESULT interp_ret(exec_ctx_t *ctx)
392 {
393     TRACE("\n");
394
395     ctx->instr = NULL;
396     return S_OK;
397 }
398
399 static HRESULT interp_bool(exec_ctx_t *ctx)
400 {
401     const VARIANT_BOOL arg = ctx->instr->arg1.lng;
402     VARIANT v;
403
404     TRACE("%s\n", arg ? "true" : "false");
405
406     V_VT(&v) = VT_BOOL;
407     V_BOOL(&v) = arg;
408     return stack_push(ctx, &v);
409 }
410
411 static HRESULT interp_string(exec_ctx_t *ctx)
412 {
413     VARIANT v;
414
415     TRACE("\n");
416
417     V_VT(&v) = VT_BSTR;
418     V_BSTR(&v) = SysAllocString(ctx->instr->arg1.str);
419     if(!V_BSTR(&v))
420         return E_OUTOFMEMORY;
421
422     return stack_push(ctx, &v);
423 }
424
425 static HRESULT interp_long(exec_ctx_t *ctx)
426 {
427     const LONG arg = ctx->instr->arg1.lng;
428     VARIANT v;
429
430     TRACE("%d\n", arg);
431
432     V_VT(&v) = VT_I4;
433     V_I4(&v) = arg;
434     return stack_push(ctx, &v);
435 }
436
437 static HRESULT interp_short(exec_ctx_t *ctx)
438 {
439     const LONG arg = ctx->instr->arg1.lng;
440     VARIANT v;
441
442     TRACE("%d\n", arg);
443
444     V_VT(&v) = VT_I2;
445     V_I2(&v) = arg;
446     return stack_push(ctx, &v);
447 }
448
449 static HRESULT interp_double(exec_ctx_t *ctx)
450 {
451     const DOUBLE *arg = ctx->instr->arg1.dbl;
452     VARIANT v;
453
454     TRACE("%lf\n", *arg);
455
456     V_VT(&v) = VT_R8;
457     V_R8(&v) = *arg;
458     return stack_push(ctx, &v);
459 }
460
461 static HRESULT interp_empty(exec_ctx_t *ctx)
462 {
463     VARIANT v;
464
465     TRACE("\n");
466
467     V_VT(&v) = VT_EMPTY;
468     return stack_push(ctx, &v);
469 }
470
471 static HRESULT interp_null(exec_ctx_t *ctx)
472 {
473     VARIANT v;
474
475     TRACE("\n");
476
477     V_VT(&v) = VT_NULL;
478     return stack_push(ctx, &v);
479 }
480
481 static HRESULT interp_not(exec_ctx_t *ctx)
482 {
483     variant_val_t val;
484     VARIANT v;
485     HRESULT hres;
486
487     TRACE("\n");
488
489     hres = stack_pop_val(ctx, &val);
490     if(FAILED(hres))
491         return hres;
492
493     hres = VarNot(val.v, &v);
494     release_val(&val);
495     if(FAILED(hres))
496         return hres;
497
498     return stack_push(ctx, &v);
499 }
500
501 static HRESULT cmp_oper(exec_ctx_t *ctx)
502 {
503     variant_val_t l, r;
504     HRESULT hres;
505
506     hres = stack_pop_val(ctx, &r);
507     if(FAILED(hres))
508         return hres;
509
510     hres = stack_pop_val(ctx, &l);
511     if(SUCCEEDED(hres)) {
512         if(V_VT(l.v) == VT_NULL || V_VT(r.v) == VT_NULL) {
513             FIXME("comparing nulls is not implemented\n");
514             hres = E_NOTIMPL;
515         }else {
516             hres = VarCmp(l.v, r.v, ctx->script->lcid, 0);
517         }
518     }
519
520     release_val(&r);
521     release_val(&l);
522     return hres;
523 }
524
525 static HRESULT interp_equal(exec_ctx_t *ctx)
526 {
527     VARIANT v;
528     HRESULT hres;
529
530     TRACE("\n");
531
532     hres = cmp_oper(ctx);
533     if(FAILED(hres))
534         return hres;
535
536     V_VT(&v) = VT_BOOL;
537     V_BOOL(&v) = hres == VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
538     return stack_push(ctx, &v);
539 }
540
541 static HRESULT interp_nequal(exec_ctx_t *ctx)
542 {
543     VARIANT v;
544     HRESULT hres;
545
546     TRACE("\n");
547
548     hres = cmp_oper(ctx);
549     if(FAILED(hres))
550         return hres;
551
552     V_VT(&v) = VT_BOOL;
553     V_BOOL(&v) = hres != VARCMP_EQ ? VARIANT_TRUE : VARIANT_FALSE;
554     return stack_push(ctx, &v);
555 }
556
557 static HRESULT interp_concat(exec_ctx_t *ctx)
558 {
559     variant_val_t r, l;
560     VARIANT v;
561     HRESULT hres;
562
563     TRACE("\n");
564
565     hres = stack_pop_val(ctx, &r);
566     if(FAILED(hres))
567         return hres;
568
569     hres = stack_pop_val(ctx, &l);
570     if(SUCCEEDED(hres)) {
571         hres = VarCat(l.v, r.v, &v);
572         release_val(&l);
573     }
574     release_val(&r);
575     if(FAILED(hres))
576         return hres;
577
578     return stack_push(ctx, &v);
579 }
580
581 static HRESULT interp_add(exec_ctx_t *ctx)
582 {
583     variant_val_t r, l;
584     VARIANT v;
585     HRESULT hres;
586
587     TRACE("\n");
588
589     hres = stack_pop_val(ctx, &r);
590     if(FAILED(hres))
591         return hres;
592
593     hres = stack_pop_val(ctx, &l);
594     if(SUCCEEDED(hres)) {
595         hres = VarAdd(l.v, r.v, &v);
596         release_val(&l);
597     }
598     release_val(&r);
599     if(FAILED(hres))
600         return hres;
601
602     return stack_push(ctx, &v);
603 }
604
605 static HRESULT interp_sub(exec_ctx_t *ctx)
606 {
607     variant_val_t r, l;
608     VARIANT v;
609     HRESULT hres;
610
611     TRACE("\n");
612
613     hres = stack_pop_val(ctx, &r);
614     if(FAILED(hres))
615         return hres;
616
617     hres = stack_pop_val(ctx, &l);
618     if(SUCCEEDED(hres)) {
619         hres = VarSub(l.v, r.v, &v);
620         release_val(&l);
621     }
622     release_val(&r);
623     if(FAILED(hres))
624         return hres;
625
626     return stack_push(ctx, &v);
627 }
628
629 static HRESULT interp_neg(exec_ctx_t *ctx)
630 {
631     variant_val_t val;
632     VARIANT v;
633     HRESULT hres;
634
635     hres = stack_pop_val(ctx, &val);
636     if(FAILED(hres))
637         return hres;
638
639     hres = VarNeg(val.v, &v);
640     release_val(&val);
641     if(FAILED(hres))
642         return hres;
643
644     return stack_push(ctx, &v);
645 }
646
647 static const instr_func_t op_funcs[] = {
648 #define X(x,n,a,b) interp_ ## x,
649 OP_LIST
650 #undef X
651 };
652
653 static const unsigned op_move[] = {
654 #define X(x,n,a,b) n,
655 OP_LIST
656 #undef X
657 };
658
659 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
660 {
661     exec_ctx_t exec;
662     vbsop_t op;
663     HRESULT hres = S_OK;
664
665     exec.stack_size = 16;
666     exec.top = 0;
667     exec.stack = heap_alloc(exec.stack_size * sizeof(VARIANT));
668     if(!exec.stack)
669         return E_OUTOFMEMORY;
670
671     exec.code = func->code_ctx;
672     exec.instr = exec.code->instrs + func->code_off;
673     exec.script = ctx;
674     exec.func = func;
675
676     while(exec.instr) {
677         op = exec.instr->op;
678         hres = op_funcs[op](&exec);
679         if(FAILED(hres)) {
680             FIXME("Failed %08x\n", hres);
681             stack_popn(&exec, exec.top);
682             break;
683         }
684
685         exec.instr += op_move[op];
686     }
687
688     assert(!exec.top);
689     heap_free(exec.stack);
690
691     return hres;
692 }