vbscript: Use default object value for objets in stack_pop_val.
[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, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
31 {
32     unsigned i;
33
34     for(i = invoke_type == VBDISP_ANY ? 0 : 1; i < This->desc->func_cnt; i++) {
35         if(invoke_type == VBDISP_ANY) {
36             if(!search_private && !This->desc->funcs[i].is_public)
37                 continue;
38             if(!i && !This->desc->funcs[0].name) /* default value may not exist */
39                 continue;
40         }else {
41             if(!This->desc->funcs[i].entries[invoke_type]
42                 || (!search_private && !This->desc->funcs[i].entries[invoke_type]->is_public))
43                 continue;
44         }
45
46         if(!strcmpiW(This->desc->funcs[i].name, name)) {
47             *id = i;
48             return TRUE;
49         }
50     }
51
52     return FALSE;
53 }
54
55 HRESULT vbdisp_get_id(vbdisp_t *This, BSTR name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
56 {
57     unsigned i;
58
59     if(get_func_id(This, name, invoke_type, search_private, id))
60         return S_OK;
61
62     for(i=0; i < This->desc->prop_cnt; i++) {
63         if(!search_private && !This->desc->props[i].is_public)
64             continue;
65
66         if(!strcmpiW(This->desc->props[i].name, name)) {
67             *id = i + This->desc->func_cnt;
68             return S_OK;
69         }
70     }
71
72     *id = -1;
73     return DISP_E_UNKNOWNNAME;
74 }
75
76 static VARIANT *get_propput_arg(const DISPPARAMS *dp)
77 {
78     unsigned i;
79
80     for(i=0; i < dp->cNamedArgs; i++) {
81         if(dp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
82             return dp->rgvarg+i;
83     }
84
85     return NULL;
86 }
87
88 static HRESULT invoke_variant_prop(vbdisp_t *This, VARIANT *v, WORD flags, DISPPARAMS *dp, VARIANT *res)
89 {
90     HRESULT hres;
91
92     switch(flags) {
93     case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
94         if(dp->cArgs) {
95             WARN("called with arguments\n");
96             return DISP_E_MEMBERNOTFOUND; /* That's what tests show */
97         }
98
99         hres = VariantCopy(res, v);
100         break;
101
102     case DISPATCH_PROPERTYPUT: {
103         VARIANT *put_val;
104
105         put_val = get_propput_arg(dp);
106         if(!put_val) {
107             WARN("no value to set\n");
108             return DISP_E_PARAMNOTOPTIONAL;
109         }
110
111         if(res)
112             V_VT(res) = VT_EMPTY;
113
114         hres = VariantCopy(v, put_val);
115         break;
116     }
117
118     default:
119         FIXME("unimplemented flags %x\n", flags);
120         return E_NOTIMPL;
121     }
122
123     return hres;
124 }
125
126 static void clean_props(vbdisp_t *This)
127 {
128     unsigned i;
129
130     for(i=0; i < This->desc->prop_cnt; i++)
131         VariantClear(This->props+i);
132 }
133
134 static inline vbdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
135 {
136     return CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface);
137 }
138
139 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
140 {
141     vbdisp_t *This = impl_from_IDispatchEx(iface);
142
143     if(IsEqualGUID(&IID_IUnknown, riid)) {
144         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
145         *ppv = &This->IDispatchEx_iface;
146     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
147         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
148         *ppv = &This->IDispatchEx_iface;
149     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
150         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
151         *ppv = &This->IDispatchEx_iface;
152     }else {
153         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
154         *ppv = NULL;
155         return E_NOINTERFACE;
156     }
157
158     IUnknown_AddRef((IUnknown*)*ppv);
159     return S_OK;
160 }
161
162 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
163 {
164     vbdisp_t *This = impl_from_IDispatchEx(iface);
165     LONG ref = InterlockedIncrement(&This->ref);
166
167     TRACE("(%p) ref=%d\n", This, ref);
168
169     return ref;
170 }
171
172 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
173 {
174     vbdisp_t *This = impl_from_IDispatchEx(iface);
175     LONG ref = InterlockedIncrement(&This->ref);
176
177     if(!ref) {
178         clean_props(This);
179         heap_free(This);
180     }
181
182     return ref;
183 }
184
185 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
186 {
187     vbdisp_t *This = impl_from_IDispatchEx(iface);
188
189     TRACE("(%p)->(%p)\n", This, pctinfo);
190
191     *pctinfo = 1;
192     return S_OK;
193 }
194
195 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
196                                               ITypeInfo **ppTInfo)
197 {
198     vbdisp_t *This = impl_from_IDispatchEx(iface);
199     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
200     return E_NOTIMPL;
201 }
202
203 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
204                                                 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
205                                                 DISPID *rgDispId)
206 {
207     vbdisp_t *This = impl_from_IDispatchEx(iface);
208     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
209           lcid, rgDispId);
210     return E_NOTIMPL;
211 }
212
213 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
214                                         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
215                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
216 {
217     vbdisp_t *This = impl_from_IDispatchEx(iface);
218     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
219           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
220     return E_NOTIMPL;
221 }
222
223 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
224 {
225     vbdisp_t *This = impl_from_IDispatchEx(iface);
226
227     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
228
229     if(grfdex & ~(fdexNameEnsure|fdexNameCaseInsensitive)) {
230         FIXME("unsupported flags %x\n", grfdex);
231         return E_NOTIMPL;
232     }
233
234     return vbdisp_get_id(This, bstrName, VBDISP_ANY, FALSE, pid);
235 }
236
237 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
238         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
239 {
240     vbdisp_t *This = impl_from_IDispatchEx(iface);
241
242     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
243
244     if(pvarRes)
245         V_VT(pvarRes) = VT_EMPTY;
246
247     if(id < 0)
248         return DISP_E_MEMBERNOTFOUND;
249
250     if(is_func_id(This, id)) {
251         function_t *func;
252
253         switch(wFlags) {
254         case DISPATCH_METHOD:
255         case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
256             func = This->desc->funcs[id].entries[VBDISP_CALLGET];
257             if(!func) {
258                 FIXME("no invoke/getter\n");
259                 return DISP_E_MEMBERNOTFOUND;
260             }
261
262             return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, pdp, pvarRes);
263         case DISPATCH_PROPERTYPUT: {
264             VARIANT *put_val;
265             DISPPARAMS dp = {NULL, NULL, 1, 0};
266
267             if(arg_cnt(pdp)) {
268                 FIXME("arguments not implemented\n");
269                 return E_NOTIMPL;
270             }
271
272             put_val = get_propput_arg(pdp);
273             if(!put_val) {
274                 WARN("no value to set\n");
275                 return DISP_E_PARAMNOTOPTIONAL;
276             }
277
278             dp.rgvarg = put_val;
279             func = This->desc->funcs[id].entries[V_VT(put_val) == VT_DISPATCH ? VBDISP_SET : VBDISP_LET];
280             if(!func) {
281                 FIXME("no letter/setter\n");
282                 return DISP_E_MEMBERNOTFOUND;
283             }
284
285             return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, &dp, NULL);
286         }
287         default:
288             FIXME("flags %x\n", wFlags);
289             return DISP_E_MEMBERNOTFOUND;
290         }
291     }
292
293     if(id < This->desc->prop_cnt + This->desc->func_cnt)
294         return invoke_variant_prop(This, This->props+(id-This->desc->func_cnt), wFlags, pdp, pvarRes);
295
296     return DISP_E_MEMBERNOTFOUND;
297 }
298
299 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
300 {
301     vbdisp_t *This = impl_from_IDispatchEx(iface);
302     FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
303     return E_NOTIMPL;
304 }
305
306 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
307 {
308     vbdisp_t *This = impl_from_IDispatchEx(iface);
309     FIXME("(%p)->(%x)\n", This, id);
310     return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
314 {
315     vbdisp_t *This = impl_from_IDispatchEx(iface);
316     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
317     return E_NOTIMPL;
318 }
319
320 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
321 {
322     vbdisp_t *This = impl_from_IDispatchEx(iface);
323     FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
324     return E_NOTIMPL;
325 }
326
327 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
328 {
329     vbdisp_t *This = impl_from_IDispatchEx(iface);
330     FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
331     return E_NOTIMPL;
332 }
333
334 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
335 {
336     vbdisp_t *This = impl_from_IDispatchEx(iface);
337     FIXME("(%p)->(%p)\n", This, ppunk);
338     return E_NOTIMPL;
339 }
340
341 static IDispatchExVtbl DispatchExVtbl = {
342     DispatchEx_QueryInterface,
343     DispatchEx_AddRef,
344     DispatchEx_Release,
345     DispatchEx_GetTypeInfoCount,
346     DispatchEx_GetTypeInfo,
347     DispatchEx_GetIDsOfNames,
348     DispatchEx_Invoke,
349     DispatchEx_GetDispID,
350     DispatchEx_InvokeEx,
351     DispatchEx_DeleteMemberByName,
352     DispatchEx_DeleteMemberByDispID,
353     DispatchEx_GetMemberProperties,
354     DispatchEx_GetMemberName,
355     DispatchEx_GetNextDispID,
356     DispatchEx_GetNameSpaceParent
357 };
358
359 static inline vbdisp_t *unsafe_impl_from_IDispatch(IDispatch *iface)
360 {
361     return iface->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl
362         ? CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface)
363         : NULL;
364 }
365
366 HRESULT create_vbdisp(const class_desc_t *desc, vbdisp_t **ret)
367 {
368     vbdisp_t *vbdisp;
369
370     vbdisp = heap_alloc_zero(sizeof(*vbdisp) + (desc->prop_cnt-1)*sizeof(VARIANT));
371     if(!vbdisp)
372         return E_OUTOFMEMORY;
373
374     vbdisp->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
375     vbdisp->ref = 1;
376     vbdisp->desc = desc;
377
378     *ret = vbdisp;
379     return S_OK;
380 }
381
382 HRESULT init_global(script_ctx_t *ctx)
383 {
384     ctx->script_desc.ctx = ctx;
385     return create_vbdisp(&ctx->script_desc, &ctx->script_obj);
386 }
387
388 HRESULT disp_get_id(IDispatch *disp, BSTR name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
389 {
390     IDispatchEx *dispex;
391     vbdisp_t *vbdisp;
392     HRESULT hres;
393
394     vbdisp = unsafe_impl_from_IDispatch(disp);
395     if(vbdisp)
396         return vbdisp_get_id(vbdisp, name, invoke_type, search_private, id);
397
398     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
399     if(FAILED(hres)) {
400         TRACE("unsing IDispatch\n");
401         return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
402     }
403
404     hres = IDispatchEx_GetDispID(dispex, name, fdexNameCaseInsensitive, id);
405     IDispatchEx_Release(dispex);
406     return hres;
407 }
408
409 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, DISPPARAMS *dp, VARIANT *retv)
410 {
411     const WORD flags = DISPATCH_METHOD|(retv ? DISPATCH_PROPERTYGET : 0);
412     IDispatchEx *dispex;
413     EXCEPINFO ei;
414     HRESULT hres;
415
416     memset(&ei, 0, sizeof(ei));
417     if(retv)
418         V_VT(retv) = VT_EMPTY;
419
420     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
421     if(FAILED(hres)) {
422         UINT err = 0;
423
424         TRACE("using IDispatch\n");
425         return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei, &err);
426     }
427
428     hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei, NULL /* CALLER_FIXME */);
429     IDispatchEx_Release(dispex);
430     return hres;
431 }
432
433 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val)
434 {
435     DISPID propput_dispid = DISPID_PROPERTYPUT;
436     DISPPARAMS dp  = {val, &propput_dispid, 1, 1};
437     IDispatchEx *dispex;
438     EXCEPINFO ei = {0};
439     HRESULT hres;
440
441     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
442     if(SUCCEEDED(hres)) {
443         hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, NULL /* FIXME! */);
444         IDispatchEx_Release(dispex);
445     }else {
446         ULONG err = 0;
447
448         TRACE("using IDispatch\n");
449         hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei, &err);
450     }
451
452     return hres;
453 }