vbscript: Added class properties compiler implementation.
[wine] / dlls / vbscript / vbdisp.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 "vbscript.h"
20
21 #include "wine/debug.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
24
25 static inline BOOL is_func_id(vbdisp_t *This, DISPID id)
26 {
27     return id < This->desc->func_cnt;
28 }
29
30 static BOOL get_func_id(vbdisp_t *This, const WCHAR *name, BOOL search_private, DISPID *id)
31 {
32     unsigned i;
33
34     for(i = 0; i < This->desc->func_cnt; i++) {
35         if(!search_private && !This->desc->funcs[i].is_public)
36             continue;
37         if(!This->desc->funcs[i].name) /* default value may not exist */
38             continue;
39
40         if(!strcmpiW(This->desc->funcs[i].name, name)) {
41             *id = i;
42             return TRUE;
43         }
44     }
45
46     return FALSE;
47 }
48
49 static HRESULT vbdisp_get_id(vbdisp_t *This, BSTR name, BOOL search_private, DISPID *id)
50 {
51     if(get_func_id(This, name, search_private, id))
52         return S_OK;
53
54     *id = -1;
55     return DISP_E_UNKNOWNNAME;
56 }
57
58 static inline vbdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
59 {
60     return CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface);
61 }
62
63 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
64 {
65     vbdisp_t *This = impl_from_IDispatchEx(iface);
66
67     if(IsEqualGUID(&IID_IUnknown, riid)) {
68         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
69         *ppv = &This->IDispatchEx_iface;
70     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
71         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
72         *ppv = &This->IDispatchEx_iface;
73     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
74         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
75         *ppv = &This->IDispatchEx_iface;
76     }else {
77         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
78         *ppv = NULL;
79         return E_NOINTERFACE;
80     }
81
82     IUnknown_AddRef((IUnknown*)*ppv);
83     return S_OK;
84 }
85
86 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
87 {
88     vbdisp_t *This = impl_from_IDispatchEx(iface);
89     LONG ref = InterlockedIncrement(&This->ref);
90
91     TRACE("(%p) ref=%d\n", This, ref);
92
93     return ref;
94 }
95
96 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
97 {
98     vbdisp_t *This = impl_from_IDispatchEx(iface);
99     LONG ref = InterlockedIncrement(&This->ref);
100
101     if(!ref)
102         heap_free(This);
103
104     return ref;
105 }
106
107 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
108 {
109     vbdisp_t *This = impl_from_IDispatchEx(iface);
110
111     TRACE("(%p)->(%p)\n", This, pctinfo);
112
113     *pctinfo = 1;
114     return S_OK;
115 }
116
117 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
118                                               ITypeInfo **ppTInfo)
119 {
120     vbdisp_t *This = impl_from_IDispatchEx(iface);
121     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
122     return E_NOTIMPL;
123 }
124
125 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
126                                                 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
127                                                 DISPID *rgDispId)
128 {
129     vbdisp_t *This = impl_from_IDispatchEx(iface);
130     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
131           lcid, rgDispId);
132     return E_NOTIMPL;
133 }
134
135 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
136                                         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
137                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
138 {
139     vbdisp_t *This = impl_from_IDispatchEx(iface);
140     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
141           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
142     return E_NOTIMPL;
143 }
144
145 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
146 {
147     vbdisp_t *This = impl_from_IDispatchEx(iface);
148
149     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
150
151     if(grfdex & ~(fdexNameEnsure|fdexNameCaseInsensitive)) {
152         FIXME("unsupported flags %x\n", grfdex);
153         return E_NOTIMPL;
154     }
155
156     return vbdisp_get_id(This, bstrName, FALSE, pid);
157 }
158
159 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
160         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
161 {
162     vbdisp_t *This = impl_from_IDispatchEx(iface);
163
164     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
165
166     if(pvarRes)
167         V_VT(pvarRes) = VT_EMPTY;
168
169     if(id < 0)
170         return DISP_E_MEMBERNOTFOUND;
171
172     if(is_func_id(This, id)) {
173         function_t *func;
174
175         switch(wFlags) {
176         case DISPATCH_METHOD:
177         case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
178             func = This->desc->funcs[id].entries[VBDISP_CALLGET];
179             if(!func) {
180                 FIXME("no invoke/getter\n");
181                 return DISP_E_MEMBERNOTFOUND;
182             }
183
184             return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, pdp, pvarRes);
185         default:
186             FIXME("flags %x\n", wFlags);
187             return DISP_E_MEMBERNOTFOUND;
188         }
189     }
190
191     FIXME("not implemented for non-function ids\n");
192     return DISP_E_MEMBERNOTFOUND;
193 }
194
195 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
196 {
197     vbdisp_t *This = impl_from_IDispatchEx(iface);
198     FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
199     return E_NOTIMPL;
200 }
201
202 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
203 {
204     vbdisp_t *This = impl_from_IDispatchEx(iface);
205     FIXME("(%p)->(%x)\n", This, id);
206     return E_NOTIMPL;
207 }
208
209 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
210 {
211     vbdisp_t *This = impl_from_IDispatchEx(iface);
212     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
213     return E_NOTIMPL;
214 }
215
216 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
217 {
218     vbdisp_t *This = impl_from_IDispatchEx(iface);
219     FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
220     return E_NOTIMPL;
221 }
222
223 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
224 {
225     vbdisp_t *This = impl_from_IDispatchEx(iface);
226     FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
227     return E_NOTIMPL;
228 }
229
230 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
231 {
232     vbdisp_t *This = impl_from_IDispatchEx(iface);
233     FIXME("(%p)->(%p)\n", This, ppunk);
234     return E_NOTIMPL;
235 }
236
237 static IDispatchExVtbl DispatchExVtbl = {
238     DispatchEx_QueryInterface,
239     DispatchEx_AddRef,
240     DispatchEx_Release,
241     DispatchEx_GetTypeInfoCount,
242     DispatchEx_GetTypeInfo,
243     DispatchEx_GetIDsOfNames,
244     DispatchEx_Invoke,
245     DispatchEx_GetDispID,
246     DispatchEx_InvokeEx,
247     DispatchEx_DeleteMemberByName,
248     DispatchEx_DeleteMemberByDispID,
249     DispatchEx_GetMemberProperties,
250     DispatchEx_GetMemberName,
251     DispatchEx_GetNextDispID,
252     DispatchEx_GetNameSpaceParent
253 };
254
255 static inline vbdisp_t *unsafe_impl_from_IDispatch(IDispatch *iface)
256 {
257     return iface->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl
258         ? CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface)
259         : NULL;
260 }
261
262 HRESULT create_vbdisp(const class_desc_t *desc, vbdisp_t **ret)
263 {
264     vbdisp_t *vbdisp;
265
266     vbdisp = heap_alloc_zero(sizeof(*vbdisp));
267     if(!vbdisp)
268         return E_OUTOFMEMORY;
269
270     vbdisp->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
271     vbdisp->ref = 1;
272     vbdisp->desc = desc;
273
274     *ret = vbdisp;
275     return S_OK;
276 }
277
278 HRESULT init_global(script_ctx_t *ctx)
279 {
280     ctx->script_desc.ctx = ctx;
281     return create_vbdisp(&ctx->script_desc, &ctx->script_obj);
282 }
283
284 HRESULT disp_get_id(IDispatch *disp, BSTR name, BOOL search_private, DISPID *id)
285 {
286     IDispatchEx *dispex;
287     vbdisp_t *vbdisp;
288     HRESULT hres;
289
290     vbdisp = unsafe_impl_from_IDispatch(disp);
291     if(vbdisp)
292         return vbdisp_get_id(vbdisp, name, search_private, id);
293
294     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
295     if(FAILED(hres)) {
296         TRACE("unsing IDispatch\n");
297         return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
298     }
299
300     hres = IDispatchEx_GetDispID(dispex, name, fdexNameCaseInsensitive, id);
301     IDispatchEx_Release(dispex);
302     return hres;
303 }
304
305 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, DISPPARAMS *dp, VARIANT *retv)
306 {
307     const WORD flags = DISPATCH_METHOD|(retv ? DISPATCH_PROPERTYGET : 0);
308     IDispatchEx *dispex;
309     EXCEPINFO ei;
310     HRESULT hres;
311
312     memset(&ei, 0, sizeof(ei));
313     if(retv)
314         V_VT(retv) = VT_EMPTY;
315
316     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
317     if(FAILED(hres)) {
318         UINT err = 0;
319
320         TRACE("using IDispatch\n");
321         return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei, &err);
322     }
323
324     hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei, NULL /* CALLER_FIXME */);
325     IDispatchEx_Release(dispex);
326     return hres;
327 }
328
329 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val)
330 {
331     DISPID propput_dispid = DISPID_PROPERTYPUT;
332     DISPPARAMS dp  = {val, &propput_dispid, 1, 1};
333     IDispatchEx *dispex;
334     EXCEPINFO ei = {0};
335     HRESULT hres;
336
337     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
338     if(SUCCEEDED(hres)) {
339         hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL /* FIXME! */);
340         IDispatchEx_Release(dispex);
341     }else {
342         ULONG err = 0;
343
344         TRACE("using IDispatch\n");
345         hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, &err);
346     }
347
348     return hres;
349 }