vbscript: Added simple call test.
[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     DISPPARAMS dp = {0};
79     ref_t ref;
80     HRESULT hres;
81
82     TRACE("\n");
83
84     hres = lookup_identifier(ctx, identifier, &ref);
85     if(FAILED(hres))
86         return hres;
87
88     switch(ref.type) {
89     case REF_DISP:
90         hres = disp_call(ctx->script, ref.u.d.disp, ref.u.d.id, &dp, NULL);
91         if(FAILED(hres))
92             return hres;
93         break;
94     default:
95         FIXME("%s not found\n", debugstr_w(identifier));
96         return DISP_E_UNKNOWNNAME;
97     }
98
99     return S_OK;
100 }
101
102 static HRESULT interp_ret(exec_ctx_t *ctx)
103 {
104     TRACE("\n");
105
106     ctx->instr = NULL;
107     return S_OK;
108 }
109
110 static const instr_func_t op_funcs[] = {
111 #define X(x,n,a,b) interp_ ## x,
112 OP_LIST
113 #undef X
114 };
115
116 static const unsigned op_move[] = {
117 #define X(x,n,a,b) n,
118 OP_LIST
119 #undef X
120 };
121
122 HRESULT exec_script(script_ctx_t *ctx, function_t *func)
123 {
124     exec_ctx_t exec;
125     vbsop_t op;
126     HRESULT hres = S_OK;
127
128     exec.code = func->code_ctx;
129     exec.instr = exec.code->instrs + func->code_off;
130     exec.script = ctx;
131
132     while(exec.instr) {
133         op = exec.instr->op;
134         hres = op_funcs[op](&exec);
135         if(FAILED(hres)) {
136             FIXME("Failed %08x\n", hres);
137             break;
138         }
139
140         exec.instr += op_move[op];
141     }
142
143     return hres;
144 }