mscoree: Redirect .NET 1.0 to .NET 2.0.
[wine/multimedia.git] / dlls / vbscript / vbdisp.c
blobf0751633d24e22f47fbe7c9aaf4c75ed366dfdec
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[8];
142 unsigned argn, i;
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 if(!prop->proc && prop->flags == BP_GET) {
153 const int vt = prop->min_args, val = prop->max_args;
154 switch(vt) {
155 case VT_I2:
156 V_VT(res) = VT_I2;
157 V_I2(res) = val;
158 break;
159 case VT_I4:
160 V_VT(res) = VT_I4;
161 V_I4(res) = val;
162 break;
163 case VT_BSTR: {
164 const string_constant_t *str = (const string_constant_t*)prop->max_args;
165 BSTR ret;
167 ret = SysAllocStringLen(str->buf, str->len);
168 if(!ret)
169 return E_OUTOFMEMORY;
171 V_VT(res) = VT_BSTR;
172 V_BSTR(res) = ret;
173 break;
175 DEFAULT_UNREACHABLE;
177 return S_OK;
179 break;
180 case DISPATCH_METHOD:
181 if(prop->flags & (BP_GET|BP_GETPUT)) {
182 FIXME("Call on property\n");
183 return E_FAIL;
185 break;
186 case DISPATCH_PROPERTYPUT:
187 if(!(prop->flags & BP_GETPUT)) {
188 FIXME("property does not support DISPATCH_PROPERTYPUT\n");
189 return E_FAIL;
192 FIXME("call put\n");
193 return E_NOTIMPL;
194 default:
195 FIXME("unsupported flags %x\n", flags);
196 return E_NOTIMPL;
199 argn = arg_cnt(dp);
201 if(argn < prop->min_args || argn > (prop->max_args ? prop->max_args : prop->min_args)) {
202 FIXME("invalid number of arguments\n");
203 return E_FAIL;
206 assert(argn < sizeof(args)/sizeof(*args));
208 for(i=0; i < argn; i++) {
209 if(V_VT(dp->rgvarg+dp->cArgs-i-1) == (VT_BYREF|VT_VARIANT))
210 args[i] = *V_VARIANTREF(dp->rgvarg+dp->cArgs-i-1);
211 else
212 args[i] = dp->rgvarg[dp->cArgs-i-1];
215 return prop->proc(This, args, dp->cArgs, res);
218 static BOOL run_terminator(vbdisp_t *This)
220 DISPPARAMS dp = {0};
222 if(This->terminator_ran)
223 return TRUE;
224 This->terminator_ran = TRUE;
226 if(!This->desc->class_terminate_id)
227 return TRUE;
229 This->ref++;
230 exec_script(This->desc->ctx, This->desc->funcs[This->desc->class_terminate_id].entries[VBDISP_CALLGET],
231 (IDispatch*)&This->IDispatchEx_iface, &dp, NULL);
232 return !--This->ref;
235 static void clean_props(vbdisp_t *This)
237 unsigned i;
239 if(!This->desc)
240 return;
242 for(i=0; i < This->desc->prop_cnt; i++)
243 VariantClear(This->props+i);
246 static inline vbdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
248 return CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface);
251 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
253 vbdisp_t *This = impl_from_IDispatchEx(iface);
255 if(IsEqualGUID(&IID_IUnknown, riid)) {
256 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
257 *ppv = &This->IDispatchEx_iface;
258 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
259 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
260 *ppv = &This->IDispatchEx_iface;
261 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
262 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
263 *ppv = &This->IDispatchEx_iface;
264 }else {
265 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
266 *ppv = NULL;
267 return E_NOINTERFACE;
270 IUnknown_AddRef((IUnknown*)*ppv);
271 return S_OK;
274 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
276 vbdisp_t *This = impl_from_IDispatchEx(iface);
277 LONG ref = InterlockedIncrement(&This->ref);
279 TRACE("(%p) ref=%d\n", This, ref);
281 return ref;
284 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
286 vbdisp_t *This = impl_from_IDispatchEx(iface);
287 LONG ref = InterlockedDecrement(&This->ref);
289 TRACE("(%p) ref=%d\n", This, ref);
291 if(!ref && run_terminator(This)) {
292 clean_props(This);
293 list_remove(&This->entry);
294 heap_free(This);
297 return ref;
300 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
302 vbdisp_t *This = impl_from_IDispatchEx(iface);
304 TRACE("(%p)->(%p)\n", This, pctinfo);
306 *pctinfo = 1;
307 return S_OK;
310 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
311 ITypeInfo **ppTInfo)
313 vbdisp_t *This = impl_from_IDispatchEx(iface);
314 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
315 return E_NOTIMPL;
318 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
319 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
320 DISPID *rgDispId)
322 vbdisp_t *This = impl_from_IDispatchEx(iface);
323 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
324 lcid, rgDispId);
325 return E_NOTIMPL;
328 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
329 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
330 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
332 vbdisp_t *This = impl_from_IDispatchEx(iface);
333 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
334 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
335 return E_NOTIMPL;
338 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
340 vbdisp_t *This = impl_from_IDispatchEx(iface);
342 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
344 grfdex &= ~FDEX_VERSION_MASK;
346 if(!This->desc)
347 return E_UNEXPECTED;
349 /* Tests show that fdexNameCaseSensitive is ignored */
351 if(grfdex & ~(fdexNameEnsure|fdexNameCaseInsensitive|fdexNameCaseSensitive)) {
352 FIXME("unsupported flags %x\n", grfdex);
353 return E_NOTIMPL;
356 return vbdisp_get_id(This, bstrName, VBDISP_ANY, FALSE, pid);
359 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
360 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
362 vbdisp_t *This = impl_from_IDispatchEx(iface);
364 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
366 if(!This->desc)
367 return E_UNEXPECTED;
369 if(pvarRes)
370 V_VT(pvarRes) = VT_EMPTY;
372 if(id < 0)
373 return DISP_E_MEMBERNOTFOUND;
375 if(is_func_id(This, id)) {
376 function_t *func;
378 switch(wFlags) {
379 case DISPATCH_METHOD:
380 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
381 func = This->desc->funcs[id].entries[VBDISP_CALLGET];
382 if(!func) {
383 FIXME("no invoke/getter\n");
384 return DISP_E_MEMBERNOTFOUND;
387 return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, pdp, pvarRes);
388 case DISPATCH_PROPERTYPUT: {
389 VARIANT *put_val;
390 DISPPARAMS dp = {NULL, NULL, 1, 0};
392 if(arg_cnt(pdp)) {
393 FIXME("arguments not implemented\n");
394 return E_NOTIMPL;
397 put_val = get_propput_arg(pdp);
398 if(!put_val) {
399 WARN("no value to set\n");
400 return DISP_E_PARAMNOTOPTIONAL;
403 dp.rgvarg = put_val;
404 func = This->desc->funcs[id].entries[V_VT(put_val) == VT_DISPATCH ? VBDISP_SET : VBDISP_LET];
405 if(!func) {
406 FIXME("no letter/setter\n");
407 return DISP_E_MEMBERNOTFOUND;
410 return exec_script(This->desc->ctx, func, (IDispatch*)&This->IDispatchEx_iface, &dp, NULL);
412 default:
413 FIXME("flags %x\n", wFlags);
414 return DISP_E_MEMBERNOTFOUND;
418 if(id < This->desc->prop_cnt + This->desc->func_cnt)
419 return invoke_variant_prop(This->props+(id-This->desc->func_cnt), wFlags, pdp, pvarRes);
421 if(This->desc->builtin_prop_cnt) {
422 unsigned min = 0, max = This->desc->builtin_prop_cnt-1, i;
424 while(min <= max) {
425 i = (min+max)/2;
426 if(This->desc->builtin_props[i].id == id)
427 return invoke_builtin(This, This->desc->builtin_props+i, wFlags, pdp, pvarRes);
428 if(This->desc->builtin_props[i].id < id)
429 min = i+1;
430 else
431 max = i-1;
435 return DISP_E_MEMBERNOTFOUND;
438 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
440 vbdisp_t *This = impl_from_IDispatchEx(iface);
441 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
442 return E_NOTIMPL;
445 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
447 vbdisp_t *This = impl_from_IDispatchEx(iface);
448 FIXME("(%p)->(%x)\n", This, id);
449 return E_NOTIMPL;
452 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
454 vbdisp_t *This = impl_from_IDispatchEx(iface);
455 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
456 return E_NOTIMPL;
459 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
461 vbdisp_t *This = impl_from_IDispatchEx(iface);
462 FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
463 return E_NOTIMPL;
466 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
468 vbdisp_t *This = impl_from_IDispatchEx(iface);
469 FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
470 return E_NOTIMPL;
473 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
475 vbdisp_t *This = impl_from_IDispatchEx(iface);
476 FIXME("(%p)->(%p)\n", This, ppunk);
477 return E_NOTIMPL;
480 static IDispatchExVtbl DispatchExVtbl = {
481 DispatchEx_QueryInterface,
482 DispatchEx_AddRef,
483 DispatchEx_Release,
484 DispatchEx_GetTypeInfoCount,
485 DispatchEx_GetTypeInfo,
486 DispatchEx_GetIDsOfNames,
487 DispatchEx_Invoke,
488 DispatchEx_GetDispID,
489 DispatchEx_InvokeEx,
490 DispatchEx_DeleteMemberByName,
491 DispatchEx_DeleteMemberByDispID,
492 DispatchEx_GetMemberProperties,
493 DispatchEx_GetMemberName,
494 DispatchEx_GetNextDispID,
495 DispatchEx_GetNameSpaceParent
498 static inline vbdisp_t *unsafe_impl_from_IDispatch(IDispatch *iface)
500 return iface->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl
501 ? CONTAINING_RECORD(iface, vbdisp_t, IDispatchEx_iface)
502 : NULL;
505 HRESULT create_vbdisp(const class_desc_t *desc, vbdisp_t **ret)
507 vbdisp_t *vbdisp;
509 vbdisp = heap_alloc_zero( FIELD_OFFSET( vbdisp_t, props[desc->prop_cnt] ));
510 if(!vbdisp)
511 return E_OUTOFMEMORY;
513 vbdisp->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
514 vbdisp->ref = 1;
515 vbdisp->desc = desc;
517 list_add_tail(&desc->ctx->objects, &vbdisp->entry);
519 if(desc->class_initialize_id) {
520 DISPPARAMS dp = {0};
521 HRESULT hres;
523 hres = exec_script(desc->ctx, desc->funcs[desc->class_initialize_id].entries[VBDISP_CALLGET],
524 (IDispatch*)&vbdisp->IDispatchEx_iface, &dp, NULL);
525 if(FAILED(hres)) {
526 IDispatchEx_Release(&vbdisp->IDispatchEx_iface);
527 return hres;
531 *ret = vbdisp;
532 return S_OK;
535 static HRESULT Procedure_invoke(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
537 script_ctx_t *ctx = This->desc->ctx;
538 HRESULT hres;
540 TRACE("\n");
542 IActiveScriptSite_OnEnterScript(ctx->site);
543 hres = exec_script(ctx, This->desc->value_func, NULL, NULL, NULL);
544 IActiveScriptSite_OnLeaveScript(ctx->site);
546 return hres;
549 static const builtin_prop_t procedure_props[] = {
550 {DISPID_VALUE, Procedure_invoke, 0}
553 HRESULT create_procedure_disp(script_ctx_t *ctx, vbscode_t *code, IDispatch **ret)
555 class_desc_t *desc;
556 vbdisp_t *vbdisp;
557 HRESULT hres;
559 desc = heap_alloc_zero(sizeof(*desc));
560 if(!desc)
561 return E_OUTOFMEMORY;
563 desc->ctx = ctx;
564 desc->builtin_prop_cnt = sizeof(procedure_props)/sizeof(*procedure_props);
565 desc->builtin_props = procedure_props;
566 desc->value_func = &code->main_code;
568 hres = create_vbdisp(desc, &vbdisp);
569 if(FAILED(hres)) {
570 heap_free(desc);
571 return hres;
574 desc->next = ctx->procs;
575 ctx->procs = desc;
577 *ret = (IDispatch*)&vbdisp->IDispatchEx_iface;
578 return S_OK;
581 struct _ident_map_t {
582 const WCHAR *name;
583 BOOL is_var;
584 union {
585 dynamic_var_t *var;
586 function_t *func;
587 } u;
590 static inline DISPID ident_to_id(ScriptDisp *This, ident_map_t *ident)
592 return (ident-This->ident_map)+1;
595 static inline ident_map_t *id_to_ident(ScriptDisp *This, DISPID id)
597 return 0 < id && id <= This->ident_map_cnt ? This->ident_map+id-1 : NULL;
600 static ident_map_t *add_ident(ScriptDisp *This, const WCHAR *name)
602 ident_map_t *ret;
604 if(!This->ident_map_size) {
605 This->ident_map = heap_alloc(4 * sizeof(*This->ident_map));
606 if(!This->ident_map)
607 return NULL;
608 This->ident_map_size = 4;
609 }else if(This->ident_map_cnt == This->ident_map_size) {
610 ident_map_t *new_map;
612 new_map = heap_realloc(This->ident_map, 2*This->ident_map_size*sizeof(*new_map));
613 if(!new_map)
614 return NULL;
615 This->ident_map = new_map;
616 This->ident_map_size *= 2;
619 ret = This->ident_map + This->ident_map_cnt++;
620 ret->name = name;
621 return ret;
624 static inline ScriptDisp *ScriptDisp_from_IDispatchEx(IDispatchEx *iface)
626 return CONTAINING_RECORD(iface, ScriptDisp, IDispatchEx_iface);
629 static HRESULT WINAPI ScriptDisp_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
631 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
633 if(IsEqualGUID(&IID_IUnknown, riid)) {
634 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
635 *ppv = &This->IDispatchEx_iface;
636 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
637 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
638 *ppv = &This->IDispatchEx_iface;
639 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
640 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
641 *ppv = &This->IDispatchEx_iface;
642 }else {
643 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
644 *ppv = NULL;
645 return E_NOINTERFACE;
648 IUnknown_AddRef((IUnknown*)*ppv);
649 return S_OK;
652 static ULONG WINAPI ScriptDisp_AddRef(IDispatchEx *iface)
654 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
655 LONG ref = InterlockedIncrement(&This->ref);
657 TRACE("(%p) ref=%d\n", This, ref);
659 return ref;
662 static ULONG WINAPI ScriptDisp_Release(IDispatchEx *iface)
664 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
665 LONG ref = InterlockedDecrement(&This->ref);
667 TRACE("(%p) ref=%d\n", This, ref);
669 if(!ref) {
670 assert(!This->ctx);
671 heap_free(This->ident_map);
672 heap_free(This);
675 return ref;
678 static HRESULT WINAPI ScriptDisp_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
680 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
682 TRACE("(%p)->(%p)\n", This, pctinfo);
684 *pctinfo = 1;
685 return S_OK;
688 static HRESULT WINAPI ScriptDisp_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
689 ITypeInfo **ppTInfo)
691 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
692 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
693 return E_NOTIMPL;
696 static HRESULT WINAPI ScriptDisp_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
697 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
699 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
700 UINT i;
701 HRESULT hres;
703 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
704 lcid, rgDispId);
706 for(i=0; i < cNames; i++) {
707 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
708 if(FAILED(hres))
709 return hres;
712 return S_OK;
715 static HRESULT WINAPI ScriptDisp_Invoke(IDispatchEx *iface, DISPID dispIdMember, REFIID riid, LCID lcid,
716 WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
718 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
720 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
721 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
723 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
724 pDispParams, pVarResult, pExcepInfo, NULL);
727 static HRESULT WINAPI ScriptDisp_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
729 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
730 dynamic_var_t *var;
731 ident_map_t *ident;
732 function_t *func;
734 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
736 if(!This->ctx)
737 return E_UNEXPECTED;
739 for(ident = This->ident_map; ident < This->ident_map+This->ident_map_cnt; ident++) {
740 if(!strcmpiW(ident->name, bstrName)) {
741 *pid = ident_to_id(This, ident);
742 return S_OK;
746 for(var = This->ctx->global_vars; var; var = var->next) {
747 if(!strcmpiW(var->name, bstrName)) {
748 ident = add_ident(This, var->name);
749 if(!ident)
750 return E_OUTOFMEMORY;
752 ident->is_var = TRUE;
753 ident->u.var = var;
754 *pid = ident_to_id(This, ident);
755 return S_OK;
759 for(func = This->ctx->global_funcs; func; func = func->next) {
760 if(!strcmpiW(func->name, bstrName)) {
761 ident = add_ident(This, func->name);
762 if(!ident)
763 return E_OUTOFMEMORY;
765 ident->is_var = FALSE;
766 ident->u.func = func;
767 *pid = ident_to_id(This, ident);
768 return S_OK;
772 *pid = -1;
773 return DISP_E_UNKNOWNNAME;
776 static HRESULT WINAPI ScriptDisp_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
777 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
779 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
780 ident_map_t *ident;
781 HRESULT hres;
783 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
785 ident = id_to_ident(This, id);
786 if(!ident)
787 return DISP_E_MEMBERNOTFOUND;
789 if(ident->is_var) {
790 if(ident->u.var->is_const) {
791 FIXME("const not supported\n");
792 return E_NOTIMPL;
795 return invoke_variant_prop(&ident->u.var->v, wFlags, pdp, pvarRes);
798 switch(wFlags) {
799 case DISPATCH_METHOD:
800 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
801 IActiveScriptSite_OnEnterScript(This->ctx->site);
802 hres = exec_script(This->ctx, ident->u.func, NULL, pdp, pvarRes);
803 IActiveScriptSite_OnLeaveScript(This->ctx->site);
804 break;
805 default:
806 FIXME("Unsupported flags %x\n", wFlags);
807 hres = E_NOTIMPL;
810 return hres;
813 static HRESULT WINAPI ScriptDisp_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
815 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
816 FIXME("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
817 return E_NOTIMPL;
820 static HRESULT WINAPI ScriptDisp_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
822 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
823 FIXME("(%p)->(%x)\n", This, id);
824 return E_NOTIMPL;
827 static HRESULT WINAPI ScriptDisp_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
829 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
830 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
831 return E_NOTIMPL;
834 static HRESULT WINAPI ScriptDisp_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
836 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
837 FIXME("(%p)->(%x %p)\n", This, id, pbstrName);
838 return E_NOTIMPL;
841 static HRESULT WINAPI ScriptDisp_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
843 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
844 FIXME("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
845 return E_NOTIMPL;
848 static HRESULT WINAPI ScriptDisp_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
850 ScriptDisp *This = ScriptDisp_from_IDispatchEx(iface);
851 FIXME("(%p)->(%p)\n", This, ppunk);
852 return E_NOTIMPL;
855 static IDispatchExVtbl ScriptDispVtbl = {
856 ScriptDisp_QueryInterface,
857 ScriptDisp_AddRef,
858 ScriptDisp_Release,
859 ScriptDisp_GetTypeInfoCount,
860 ScriptDisp_GetTypeInfo,
861 ScriptDisp_GetIDsOfNames,
862 ScriptDisp_Invoke,
863 ScriptDisp_GetDispID,
864 ScriptDisp_InvokeEx,
865 ScriptDisp_DeleteMemberByName,
866 ScriptDisp_DeleteMemberByDispID,
867 ScriptDisp_GetMemberProperties,
868 ScriptDisp_GetMemberName,
869 ScriptDisp_GetNextDispID,
870 ScriptDisp_GetNameSpaceParent
873 HRESULT create_script_disp(script_ctx_t *ctx, ScriptDisp **ret)
875 ScriptDisp *script_disp;
877 script_disp = heap_alloc_zero(sizeof(*script_disp));
878 if(!script_disp)
879 return E_OUTOFMEMORY;
881 script_disp->IDispatchEx_iface.lpVtbl = &ScriptDispVtbl;
882 script_disp->ref = 1;
883 script_disp->ctx = ctx;
885 *ret = script_disp;
886 return S_OK;
889 void collect_objects(script_ctx_t *ctx)
891 vbdisp_t *iter, *iter2;
893 LIST_FOR_EACH_ENTRY_SAFE(iter, iter2, &ctx->objects, vbdisp_t, entry)
894 run_terminator(iter);
896 while(!list_empty(&ctx->objects)) {
897 iter = LIST_ENTRY(list_head(&ctx->objects), vbdisp_t, entry);
899 IDispatchEx_AddRef(&iter->IDispatchEx_iface);
900 clean_props(iter);
901 iter->desc = NULL;
902 list_remove(&iter->entry);
903 list_init(&iter->entry);
904 IDispatchEx_Release(&iter->IDispatchEx_iface);
908 HRESULT disp_get_id(IDispatch *disp, BSTR name, vbdisp_invoke_type_t invoke_type, BOOL search_private, DISPID *id)
910 IDispatchEx *dispex;
911 vbdisp_t *vbdisp;
912 HRESULT hres;
914 vbdisp = unsafe_impl_from_IDispatch(disp);
915 if(vbdisp)
916 return vbdisp_get_id(vbdisp, name, invoke_type, search_private, id);
918 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
919 if(FAILED(hres)) {
920 TRACE("using IDispatch\n");
921 return IDispatch_GetIDsOfNames(disp, &IID_NULL, &name, 1, 0, id);
924 hres = IDispatchEx_GetDispID(dispex, name, fdexNameCaseInsensitive, id);
925 IDispatchEx_Release(dispex);
926 return hres;
929 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, DISPPARAMS *dp, VARIANT *retv)
931 const WORD flags = DISPATCH_METHOD|(retv ? DISPATCH_PROPERTYGET : 0);
932 IDispatchEx *dispex;
933 EXCEPINFO ei;
934 HRESULT hres;
936 memset(&ei, 0, sizeof(ei));
937 if(retv)
938 V_VT(retv) = VT_EMPTY;
940 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
941 if(FAILED(hres)) {
942 UINT err = 0;
944 TRACE("using IDispatch\n");
945 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei, &err);
948 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei, NULL /* CALLER_FIXME */);
949 IDispatchEx_Release(dispex);
950 return hres;
953 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, DISPPARAMS *dp)
955 IDispatchEx *dispex;
956 EXCEPINFO ei = {0};
957 HRESULT hres;
959 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
960 if(SUCCEEDED(hres)) {
961 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, dp, NULL, &ei, NULL /* FIXME! */);
962 IDispatchEx_Release(dispex);
963 }else {
964 ULONG err = 0;
966 TRACE("using IDispatch\n");
967 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, dp, NULL, &ei, &err);
970 return hres;