Release 1.5.13.
[wine.git] / dlls / vbscript / vbdisp.c
blob1b2b8fecac055c5c8382d35f50ce205f6cd40fe9
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <assert.h>
21 #include "vbscript.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
27 #define FDEX_VERSION_MASK 0xf0000000
29 static inline BOOL is_func_id(vbdisp_t *This, DISPID id)
31 return id < This->desc->func_cnt;
34 static BOOL get_func_id(vbdisp_t *This, const WCHAR *name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
36 unsigned i;
38 for(i = invoke_type == VBDISP_ANY ? 0 : 1; i < This->desc->func_cnt; i++) {
39 if(invoke_type == VBDISP_ANY) {
40 if(!search_private && !This->desc->funcs[i].is_public)
41 continue;
42 if(!i && !This->desc->funcs[0].name) /* default value may not exist */
43 continue;
44 }else {
45 if(!This->desc->funcs[i].entries[invoke_type]
46 || (!search_private && !This->desc->funcs[i].entries[invoke_type]->is_public))
47 continue;
50 if(!strcmpiW(This->desc->funcs[i].name, name)) {
51 *id = i;
52 return TRUE;
56 return FALSE;
59 HRESULT vbdisp_get_id(vbdisp_t *This, BSTR name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
61 unsigned i;
63 if(get_func_id(This, name, invoke_type, search_private, id))
64 return S_OK;
66 for(i=0; i < This->desc->prop_cnt; i++) {
67 if(!search_private && !This->desc->props[i].is_public)
68 continue;
70 if(!strcmpiW(This->desc->props[i].name, name)) {
71 *id = i + This->desc->func_cnt;
72 return S_OK;
76 if(This->desc->typeinfo) {
77 HRESULT hres;
79 hres = ITypeInfo_GetIDsOfNames(This->desc->typeinfo, &name, 1, id);
80 if(SUCCEEDED(hres))
81 return S_OK;
84 *id = -1;
85 return DISP_E_UNKNOWNNAME;
88 static VARIANT *get_propput_arg(const DISPPARAMS *dp)
90 unsigned i;
92 for(i=0; i < dp->cNamedArgs; i++) {
93 if(dp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
94 return dp->rgvarg+i;
97 return NULL;
100 static HRESULT invoke_variant_prop(VARIANT *v, WORD flags, DISPPARAMS *dp, VARIANT *res)
102 HRESULT hres;
104 switch(flags) {
105 case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
106 case DISPATCH_PROPERTYGET:
107 if(dp->cArgs) {
108 WARN("called with arguments\n");
109 return DISP_E_MEMBERNOTFOUND; /* That's what tests show */
112 hres = VariantCopy(res, v);
113 break;
115 case DISPATCH_PROPERTYPUT: {
116 VARIANT *put_val;
118 put_val = get_propput_arg(dp);
119 if(!put_val) {
120 WARN("no value to set\n");
121 return DISP_E_PARAMNOTOPTIONAL;
124 if(res)
125 V_VT(res) = VT_EMPTY;
127 hres = VariantCopy(v, put_val);
128 break;
131 default:
132 FIXME("unimplemented flags %x\n", flags);
133 return E_NOTIMPL;
136 return hres;
139 static HRESULT invoke_builtin(vbdisp_t *This, const builtin_prop_t *prop, WORD flags, DISPPARAMS *dp, VARIANT *res)
141 VARIANT *args, arg_buf[8];
142 unsigned argn;
144 switch(flags) {
145 case DISPATCH_PROPERTYGET:
146 if(!(prop->flags & (BP_GET|BP_GETPUT))) {
147 FIXME("property does not support DISPATCH_PROPERTYGET\n");
148 return E_FAIL;
150 break;
151 case DISPATCH_PROPERTYGET|DISPATCH_METHOD:
152 break;
153 case DISPATCH_METHOD:
154 if(prop->flags & (BP_GET|BP_GETPUT)) {
155 FIXME("Call on property\n");
156 return E_FAIL;
158 break;
159 case DISPATCH_PROPERTYPUT:
160 if(!(prop->flags & (BP_GET|BP_GETPUT))) {
161 FIXME("property does not support DISPATCH_PROPERTYPUT\n");
162 return E_FAIL;
165 FIXME("call put\n");
166 return E_NOTIMPL;
167 default:
168 FIXME("unsupported flags %x\n", flags);
169 return E_NOTIMPL;
172 argn = arg_cnt(dp);
174 if(argn < prop->min_args || argn > (prop->max_args ? prop->max_args : prop->min_args)) {
175 FIXME("invalid number of arguments\n");
176 return E_FAIL;
179 args = dp->rgvarg;
181 if(argn == 1) {
182 if(V_VT(dp->rgvarg) == (VT_BYREF|VT_VARIANT))
183 args = V_VARIANTREF(dp->rgvarg);
184 }else {
185 unsigned i;
187 assert(argn < sizeof(arg_buf)/sizeof(*arg_buf));
189 for(i=0; i < argn; i++) {
190 if(V_VT(dp->rgvarg+i) == (VT_BYREF|VT_VARIANT)) {
191 for(i=0; i < argn; i++) {
192 if(V_VT(dp->rgvarg+i) == (VT_BYREF|VT_VARIANT))
193 arg_buf[i] = *V_VARIANTREF(dp->rgvarg+i);
194 else
195 arg_buf[i] = dp->rgvarg[i];
197 args = arg_buf;
198 break;
203 return prop->proc(This, args, dp->cArgs, res);
206 static BOOL run_terminator(vbdisp_t *This)
208 DISPPARAMS dp = {0};
210 if(This->terminator_ran)
211 return TRUE;
212 This->terminator_ran = TRUE;
214 if(!This->desc->class_terminate_id)
215 return TRUE;
217 This->ref++;
218 exec_script(This->desc->ctx, This->desc->funcs[This->desc->class_terminate_id].entries[VBDISP_CALLGET],
219 (IDispatch*)&This->IDispatchEx_iface, &dp, NULL);
220 return !--This->ref;
223 static void clean_props(vbdisp_t *This)
225 unsigned i;
227 if(!This->desc)
228 return;
230 for(i=0; i < This->desc->prop_cnt; i++)
231 VariantClear(This->props+i);
234 static inline vbdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
236 return CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface);
239 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
241 vbdisp_t *This = impl_from_IDispatchEx(iface);
243 if(IsEqualGUID(&IID_IUnknown, riid)) {
244 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
245 *ppv = &This->IDispatchEx_iface;
246 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
247 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
248 *ppv = &This->IDispatchEx_iface;
249 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
250 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
251 *ppv = &This->IDispatchEx_iface;
252 }else {
253 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
254 *ppv = NULL;
255 return E_NOINTERFACE;
258 IUnknown_AddRef((IUnknown*)*ppv);
259 return S_OK;
262 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
264 vbdisp_t *This = impl_from_IDispatchEx(iface);
265 LONG ref = InterlockedIncrement(&This->ref);
267 TRACE("(%p) ref=%d\n", This, ref);
269 return ref;
272 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
274 vbdisp_t *This = impl_from_IDispatchEx(iface);
275 LONG ref = InterlockedDecrement(&This->ref);
277 TRACE("(%p) ref=%d\n", This, ref);
279 if(!ref && run_terminator(This)) {
280 clean_props(This);
281 list_remove(&This->entry);
282 heap_free(This);
285 return ref;
288 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
290 vbdisp_t *This = impl_from_IDispatchEx(iface);
292 TRACE("(%p)->(%p)\n", This, pctinfo);
294 *pctinfo = 1;
295 return S_OK;
298 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
299 ITypeInfo **ppTInfo)
301 vbdisp_t *This = impl_from_IDispatchEx(iface);
302 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
303 return E_NOTIMPL;
306 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
307 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
308 DISPID *rgDispId)
310 vbdisp_t *This = impl_from_IDispatchEx(iface);
311 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
312 lcid, rgDispId);
313 return E_NOTIMPL;
316 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
317 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
318 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
320 vbdisp_t *This = impl_from_IDispatchEx(iface);
321 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
322 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
323 return E_NOTIMPL;
326 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
328 vbdisp_t *This = impl_from_IDispatchEx(iface);
330 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
332 grfdex &= ~FDEX_VERSION_MASK;
334 if(!This->desc)
335 return E_UNEXPECTED;
337 /* Tests show that fdexNameCaseSensitive is ignored */
339 if(grfdex & ~(fdexNameEnsure|fdexNameCaseInsensitive|fdexNameCaseSensitive)) {
340 FIXME("unsupported flags %x\n", grfdex);
341 return E_NOTIMPL;
344 return vbdisp_get_id(This, bstrName, VBDISP_ANY, FALSE, pid);
347 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
348 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
350 vbdisp_t *This = impl_from_IDispatchEx(iface);
352 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
354 if(!This->desc)
355 return E_UNEXPECTED;
357 if(pvarRes)
358 V_VT(pvarRes) = VT_EMPTY;
360 if(id < 0)
361 return DISP_E_MEMBERNOTFOUND;
363 if(is_func_id(This, id)) {
364 function_t *func;
366 switch(wFlags) {
367 case DISPATCH_METHOD:
368 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
369 func = This->desc->funcs[id].entries[VBDISP_CALLGET];
370 if(!func) {
371 FIXME("no invoke/getter\n");
372 return DISP_E_MEMBERNOTFOUND;
375 return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, pdp, pvarRes);
376 case DISPATCH_PROPERTYPUT: {
377 VARIANT *put_val;
378 DISPPARAMS dp = {NULL, NULL, 1, 0};
380 if(arg_cnt(pdp)) {
381 FIXME("arguments not implemented\n");
382 return E_NOTIMPL;
385 put_val = get_propput_arg(pdp);
386 if(!put_val) {
387 WARN("no value to set\n");
388 return DISP_E_PARAMNOTOPTIONAL;
391 dp.rgvarg = put_val;
392 func = This->desc->funcs[id].entries[V_VT(put_val) == VT_DISPATCH ? VBDISP_SET : VBDISP_LET];
393 if(!func) {
394 FIXME("no letter/setter\n");
395 return DISP_E_MEMBERNOTFOUND;
398 return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, &dp, NULL);
400 default:
401 FIXME("flags %x\n", wFlags);
402 return DISP_E_MEMBERNOTFOUND;
406 if(id < This->desc->prop_cnt + This->desc->func_cnt)
407 return invoke_variant_prop(This->props+(id-This->desc->func_cnt), wFlags, pdp, pvarRes);
409 if(This->desc->builtin_prop_cnt) {
410 unsigned min = 0, max = This->desc->builtin_prop_cnt-1, i;
412 while(min <= max) {
413 i = (min+max)/2;
414 if(This->desc->builtin_props[i].id == id)
415 return invoke_builtin(This, This->desc->builtin_props+i, wFlags, pdp, pvarRes);
416 if(This->desc->builtin_props[i].id < id)
417 min = i+1;
418 else
419 max = i-1;
423 return DISP_E_MEMBERNOTFOUND;
426 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
428 vbdisp_t *This = impl_from_IDispatchEx(iface);
429 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
430 return E_NOTIMPL;
433 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
435 vbdisp_t *This = impl_from_IDispatchEx(iface);
436 FIXME("(%p)->(%x)\n", This, id);
437 return E_NOTIMPL;
440 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
442 vbdisp_t *This = impl_from_IDispatchEx(iface);
443 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
444 return E_NOTIMPL;
447 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
449 vbdisp_t *This = impl_from_IDispatchEx(iface);
450 FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
451 return E_NOTIMPL;
454 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
456 vbdisp_t *This = impl_from_IDispatchEx(iface);
457 FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
458 return E_NOTIMPL;
461 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
463 vbdisp_t *This = impl_from_IDispatchEx(iface);
464 FIXME("(%p)->(%p)\n", This, ppunk);
465 return E_NOTIMPL;
468 static IDispatchExVtbl DispatchExVtbl = {
469 DispatchEx_QueryInterface,
470 DispatchEx_AddRef,
471 DispatchEx_Release,
472 DispatchEx_GetTypeInfoCount,
473 DispatchEx_GetTypeInfo,
474 DispatchEx_GetIDsOfNames,
475 DispatchEx_Invoke,
476 DispatchEx_GetDispID,
477 DispatchEx_InvokeEx,
478 DispatchEx_DeleteMemberByName,
479 DispatchEx_DeleteMemberByDispID,
480 DispatchEx_GetMemberProperties,
481 DispatchEx_GetMemberName,
482 DispatchEx_GetNextDispID,
483 DispatchEx_GetNameSpaceParent
486 static inline vbdisp_t *unsafe_impl_from_IDispatch(IDispatch *iface)
488 return iface->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl
489 ? CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface)
490 : NULL;
493 HRESULT create_vbdisp(const class_desc_t *desc, vbdisp_t **ret)
495 vbdisp_t *vbdisp;
497 vbdisp = heap_alloc_zero( FIELD_OFFSET( vbdisp_t, props[desc->prop_cnt] ));
498 if(!vbdisp)
499 return E_OUTOFMEMORY;
501 vbdisp->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
502 vbdisp->ref = 1;
503 vbdisp->desc = desc;
505 list_add_tail(&desc->ctx->objects, &vbdisp->entry);
507 if(desc->class_initialize_id) {
508 DISPPARAMS dp = {0};
509 HRESULT hres;
511 hres = exec_script(desc->ctx, desc->funcs[desc->class_initialize_id].entries[VBDISP_CALLGET],
512 (IDispatch*)&vbdisp->IDispatchEx_iface, &dp, NULL);
513 if(FAILED(hres)) {
514 IDispatchEx_Release(&vbdisp->IDispatchEx_iface);
515 return hres;
519 *ret = vbdisp;
520 return S_OK;
523 static HRESULT Procedure_invoke(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
525 script_ctx_t *ctx = This->desc->ctx;
526 HRESULT hres;
528 TRACE("\n");
530 IActiveScriptSite_OnEnterScript(ctx->site);
531 hres = exec_script(ctx, This->desc->value_func, NULL, NULL, NULL);
532 IActiveScriptSite_OnLeaveScript(ctx->site);
534 return hres;
537 static const builtin_prop_t procedure_props[] = {
538 {DISPID_VALUE, Procedure_invoke, 0}
541 HRESULT create_procedure_disp(script_ctx_t *ctx, vbscode_t *code, IDispatch **ret)
543 class_desc_t *desc;
544 vbdisp_t *vbdisp;
545 HRESULT hres;
547 desc = heap_alloc_zero(sizeof(*desc));
548 if(!desc)
549 return E_OUTOFMEMORY;
551 desc->ctx = ctx;
552 desc->builtin_prop_cnt = sizeof(procedure_props)/sizeof(*procedure_props);
553 desc->builtin_props = procedure_props;
554 desc->value_func = &code->main_code;
556 hres = create_vbdisp(desc, &vbdisp);
557 if(FAILED(hres)) {
558 heap_free(desc);
559 return hres;
562 desc->next = ctx->procs;
563 ctx->procs = desc;
565 *ret = (IDispatch*)&vbdisp->IDispatchEx_iface;
566 return S_OK;
569 struct _ident_map_t {
570 const WCHAR *name;
571 BOOL is_var;
572 union {
573 dynamic_var_t *var;
574 function_t *func;
575 } u;
578 static inline DISPID ident_to_id(ScriptDisp *This, ident_map_t *ident)
580 return (ident-This->ident_map)+1;
583 static inline ident_map_t *id_to_ident(ScriptDisp *This, DISPID id)
585 return 0 < id && id <= This->ident_map_cnt ? This->ident_map+id-1 : NULL;
588 static ident_map_t *add_ident(ScriptDisp *This, const WCHAR *name)
590 ident_map_t *ret;
592 if(!This->ident_map_size) {
593 This->ident_map = heap_alloc(4 * sizeof(*This->ident_map));
594 if(!This->ident_map)
595 return NULL;
596 This->ident_map_size = 4;
597 }else if(This->ident_map_cnt == This->ident_map_size) {
598 ident_map_t *new_map;
600 new_map = heap_realloc(This->ident_map, 2*This->ident_map_size*sizeof(*new_map));
601 if(!new_map)
602 return NULL;
603 This->ident_map = new_map;
604 This->ident_map_size *= 2;
607 ret = This->ident_map + This->ident_map_cnt++;
608 ret->name = name;
609 return ret;
612 static inline ScriptDisp *ScriptDisp_from_IDispatchEx(IDispatchEx *iface)
614 return CONTAINING_RECORD(iface, ScriptDisp, IDispatchEx_iface);
617 static HRESULT WINAPI ScriptDisp_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
619 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
621 if(IsEqualGUID(&IID_IUnknown, riid)) {
622 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
623 *ppv = &This->IDispatchEx_iface;
624 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
625 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
626 *ppv = &This->IDispatchEx_iface;
627 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
628 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
629 *ppv = &This->IDispatchEx_iface;
630 }else {
631 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
632 *ppv = NULL;
633 return E_NOINTERFACE;
636 IUnknown_AddRef((IUnknown*)*ppv);
637 return S_OK;
640 static ULONG WINAPI ScriptDisp_AddRef(IDispatchEx *iface)
642 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
643 LONG ref = InterlockedIncrement(&This->ref);
645 TRACE("(%p) ref=%d\n", This, ref);
647 return ref;
650 static ULONG WINAPI ScriptDisp_Release(IDispatchEx *iface)
652 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
653 LONG ref = InterlockedDecrement(&This->ref);
655 TRACE("(%p) ref=%d\n", This, ref);
657 if(!ref) {
658 assert(!This->ctx);
659 heap_free(This->ident_map);
660 heap_free(This);
663 return ref;
666 static HRESULT WINAPI ScriptDisp_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
668 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
670 TRACE("(%p)->(%p)\n", This, pctinfo);
672 *pctinfo = 1;
673 return S_OK;
676 static HRESULT WINAPI ScriptDisp_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
677 ITypeInfo **ppTInfo)
679 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
680 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
681 return E_NOTIMPL;
684 static HRESULT WINAPI ScriptDisp_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
685 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
686 DISPID *rgDispId)
688 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
689 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
690 lcid, rgDispId);
691 return E_NOTIMPL;
694 static HRESULT WINAPI ScriptDisp_Invoke(IDispatchEx *iface, DISPID dispIdMember,
695 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
696 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
698 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
699 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
700 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
701 return E_NOTIMPL;
704 static HRESULT WINAPI ScriptDisp_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
706 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
707 dynamic_var_t *var;
708 ident_map_t *ident;
709 function_t *func;
711 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
713 if(!This->ctx)
714 return E_UNEXPECTED;
716 for(ident = This->ident_map; ident < This->ident_map+This->ident_map_cnt; ident++) {
717 if(!strcmpiW(ident->name, bstrName)) {
718 *pid = ident_to_id(This, ident);
719 return S_OK;
723 for(var = This->ctx->global_vars; var; var = var->next) {
724 if(!strcmpiW(var->name, bstrName)) {
725 ident = add_ident(This, var->name);
726 if(!ident)
727 return E_OUTOFMEMORY;
729 ident->is_var = TRUE;
730 ident->u.var = var;
731 *pid = ident_to_id(This, ident);
732 return S_OK;
736 for(func = This->ctx->global_funcs; func; func = func->next) {
737 if(!strcmpiW(func->name, bstrName)) {
738 ident = add_ident(This, func->name);
739 if(!ident)
740 return E_OUTOFMEMORY;
742 ident->is_var = FALSE;
743 ident->u.func = func;
744 *pid = ident_to_id(This, ident);
745 return S_OK;
749 *pid = -1;
750 return DISP_E_UNKNOWNNAME;
753 static HRESULT WINAPI ScriptDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
754 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
756 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
757 ident_map_t *ident;
758 HRESULT hres;
760 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
762 ident = id_to_ident(This, id);
763 if(!ident)
764 return DISP_E_MEMBERNOTFOUND;
766 if(ident->is_var) {
767 if(ident->u.var->is_const) {
768 FIXME("const not supported\n");
769 return E_NOTIMPL;
772 return invoke_variant_prop(&ident->u.var->v, wFlags, pdp, pvarRes);
776 IActiveScriptSite_OnEnterScript(This->ctx->site);
777 hres = exec_script(This->ctx, ident->u.func, NULL, pdp, pvarRes);
778 IActiveScriptSite_OnLeaveScript(This->ctx->site);
780 return hres;
783 static HRESULT WINAPI ScriptDisp_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
785 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
786 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
787 return E_NOTIMPL;
790 static HRESULT WINAPI ScriptDisp_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
792 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
793 FIXME("(%p)->(%x)\n", This, id);
794 return E_NOTIMPL;
797 static HRESULT WINAPI ScriptDisp_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
799 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
800 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
801 return E_NOTIMPL;
804 static HRESULT WINAPI ScriptDisp_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
806 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
807 FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
808 return E_NOTIMPL;
811 static HRESULT WINAPI ScriptDisp_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
813 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
814 FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
815 return E_NOTIMPL;
818 static HRESULT WINAPI ScriptDisp_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
820 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
821 FIXME("(%p)->(%p)\n", This, ppunk);
822 return E_NOTIMPL;
825 static IDispatchExVtbl ScriptDispVtbl = {
826 ScriptDisp_QueryInterface,
827 ScriptDisp_AddRef,
828 ScriptDisp_Release,
829 ScriptDisp_GetTypeInfoCount,
830 ScriptDisp_GetTypeInfo,
831 ScriptDisp_GetIDsOfNames,
832 ScriptDisp_Invoke,
833 ScriptDisp_GetDispID,
834 ScriptDisp_InvokeEx,
835 ScriptDisp_DeleteMemberByName,
836 ScriptDisp_DeleteMemberByDispID,
837 ScriptDisp_GetMemberProperties,
838 ScriptDisp_GetMemberName,
839 ScriptDisp_GetNextDispID,
840 ScriptDisp_GetNameSpaceParent
843 HRESULT create_script_disp(script_ctx_t *ctx, ScriptDisp **ret)
845 ScriptDisp *script_disp;
847 script_disp = heap_alloc_zero(sizeof(*script_disp));
848 if(!script_disp)
849 return E_OUTOFMEMORY;
851 script_disp->IDispatchEx_iface.lpVtbl = &ScriptDispVtbl;
852 script_disp->ref = 1;
853 script_disp->ctx = ctx;
855 *ret = script_disp;
856 return S_OK;
859 void collect_objects(script_ctx_t *ctx)
861 vbdisp_t *iter, *iter2;
863 LIST_FOR_EACH_ENTRY_SAFE(iter, iter2, &ctx->objects, vbdisp_t, entry)
864 run_terminator(iter);
866 while(!list_empty(&ctx->objects)) {
867 iter = LIST_ENTRY(list_head(&ctx->objects), vbdisp_t, entry);
869 IDispatchEx_AddRef(&iter->IDispatchEx_iface);
870 clean_props(iter);
871 iter->desc = NULL;
872 list_remove(&iter->entry);
873 list_init(&iter->entry);
874 IDispatchEx_Release(&iter->IDispatchEx_iface);
878 HRESULT disp_get_id(IDispatch *disp, BSTR name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
880 IDispatchEx *dispex;
881 vbdisp_t *vbdisp;
882 HRESULT hres;
884 vbdisp = unsafe_impl_from_IDispatch(disp);
885 if(vbdisp)
886 return vbdisp_get_id(vbdisp, name, invoke_type, search_private, id);
888 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
889 if(FAILED(hres)) {
890 TRACE("using IDispatch\n");
891 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
894 hres = IDispatchEx_GetDispID(dispex, name, fdexNameCaseInsensitive, id);
895 IDispatchEx_Release(dispex);
896 return hres;
899 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, DISPPARAMS *dp, VARIANT *retv)
901 const WORD flags = DISPATCH_METHOD|(retv ? DISPATCH_PROPERTYGET : 0);
902 IDispatchEx *dispex;
903 EXCEPINFO ei;
904 HRESULT hres;
906 memset(&ei, 0, sizeof(ei));
907 if(retv)
908 V_VT(retv) = VT_EMPTY;
910 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
911 if(FAILED(hres)) {
912 UINT err = 0;
914 TRACE("using IDispatch\n");
915 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei, &err);
918 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei, NULL /* CALLER_FIXME */);
919 IDispatchEx_Release(dispex);
920 return hres;
923 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, DISPPARAMS *dp)
925 IDispatchEx *dispex;
926 EXCEPINFO ei = {0};
927 HRESULT hres;
929 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
930 if(SUCCEEDED(hres)) {
931 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, dp, NULL, &ei, NULL /* FIXME! */);
932 IDispatchEx_Release(dispex);
933 }else {
934 ULONG err = 0;
936 TRACE("using IDispatch\n");
937 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, dp, NULL, &ei, &err);
940 return hres;