vbscript: Added compiler support for boolean literals.
[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 } exec_ctx_t;
33
34 typedef HRESULT (*instr_func_t)(exec_ctx_t*);
35
36 typedef enum {
37     REF_NONE,
38     REF_DISP
39 } ref_type_t;
40
41 typedef struct {
42     ref_type_t type;
43     union {
44         struct {
45             IDispatch *disp;
46             DISPID id;
47         } d;
48     } u;
49 } ref_t;
50
51 static HRESULT lookup_identifier(exec_ctx_t *ctx, BSTR name, ref_t *ref)
52 {
53     named_item_t *item;
54     DISPID id;
55     HRESULT hres;
56
57     LIST_FOR_EACH_ENTRY(item, &ctx->script->named_items, named_item_t, entry) {
58         if(item->flags & SCRIPTITEM_GLOBALMEMBERS) {
59             hres = disp_get_id(item->disp, name, &id);
60             if(SUCCEEDED(hres)) {
61                 ref->type = REF_DISP;
62                 ref->u.d.disp = item->disp;
63                 ref->u.d.id = id;
64                 return S_OK;
65             }
66         }
67     }
68
69     FIXME("create if no option explicit\n");
70
71     ref->type = REF_NONE;
72     return S_OK;
73 }
74
75 static HRESULT interp_icallv(exec_ctx_t *ctx)
76 {
77     BSTR identifier = ctx->instr->arg1.bstr;
78     const unsigned arg_cnt = ctx->instr->arg2.uint;
79     DISPPARAMS dp = {0};
80     ref_t ref;
81     HRESULT hres;
82
83     TRACE("\n");
84
85     if(arg_cnt) {
86         FIXME("arguments not implemented\n");
87         return E_NOTIMPL;
88     }
89
90     hres = lookup_identifier(ctx, identifier, &ref);
91     if(FAILED(hres))
92         return hres;
93
94     switch(ref.type) {
95     case REF_DISP:
96         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, NULL);
97         if(FAILED(hres))
98             return hres;
99         break;
100     default:
101         FIXME("%s not found\n", debugstr_w(identifier));
102         return DISP_E_UNKNOWNNAME;
103     }
104
105     return S_OK;
106 }
107
108 static HRESULT interp_ret(exec_ctx_t *ctx)
109 {
110     TRACE("\n");
111
112     ctx->instr = NULL;
113     return S_OK;
114 }
115
116 static HRESULT interp_bool(exec_ctx_t *ctx)
117 {
118     FIXME("\n");
119     return E_NOTIMPL;
120 }
121
122 static const instr_func_t op_funcs[] = {
123 #define X(x,n,a,b) interp_ ## x,
124 OP_LIST
125 #undef X
126 };
127
128 static const unsigned op_move[] = {
129 #define X(x,n,a,b) n,
130 OP_LIST
131 #undef X
132 };
133
134 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
135 {
136     exec_ctx_t exec;
137     vbsop_t op;
138     HRESULT hres = S_OK;
139
140     exec.code = func->code_ctx;
141     exec.instr = exec.code->instrs + func->code_off;
142     exec.script = ctx;
143
144     while(exec.instr) {
145         op = exec.instr->op;
146         hres = op_funcs[op](&exec);
147         if(FAILED(hres)) {
148             FIXME("Failed %08x\n", hres);
149             break;
150         }
151
152         exec.instr += op_move[op];
153     }
154
155     return hres;
156 }