2 * Copyright 2008 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
23 #include "wine/unicode.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 * This IID is used to get jsdisp_t objecto from interface.
30 * We might consider using private insteface instead.
32 static const IID IID_IDispatchJS =
33 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
35 #define FDEX_VERSION_MASK 0xf0000000
36 #define GOLDEN_RATIO 0x9E3779B9U
45 struct _dispex_prop_t {
53 const builtin_prop_t *p;
61 static inline DISPID prop_to_id(jsdisp_t *This, dispex_prop_t *prop)
63 return prop - This->props;
66 static inline dispex_prop_t *get_prop(jsdisp_t *This, DISPID id)
68 if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
71 return This->props+id;
74 static DWORD get_flags(jsdisp_t *This, dispex_prop_t *prop)
76 if(prop->type == PROP_PROTREF) {
77 dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
79 prop->type = PROP_DELETED;
83 return get_flags(This->prototype, parent);
89 static const builtin_prop_t *find_builtin_prop(jsdisp_t *This, const WCHAR *name)
91 int min = 0, max, i, r;
93 max = This->builtin_info->props_cnt-1;
97 r = strcmpW(name, This->builtin_info->props[i].name);
99 return This->builtin_info->props + i;
110 static inline unsigned string_hash(const WCHAR *name)
114 h = (h>>(sizeof(unsigned)*8-4)) ^ (h<<4) ^ tolowerW(*name);
118 static inline unsigned get_props_idx(jsdisp_t *This, unsigned hash)
120 return (hash*GOLDEN_RATIO) & (This->buf_size-1);
123 static inline HRESULT resize_props(jsdisp_t *This)
125 dispex_prop_t *props;
128 if(This->buf_size != This->prop_cnt)
131 props = heap_realloc(This->props, sizeof(dispex_prop_t)*This->buf_size*2);
133 return E_OUTOFMEMORY;
137 for(i=0; i<This->buf_size; i++) {
138 This->props[i].bucket_head = 0;
139 This->props[i].bucket_next = 0;
142 for(i=1; i<This->prop_cnt; i++) {
143 props = This->props+i;
145 bucket = get_props_idx(This, props->hash);
146 props->bucket_next = This->props[bucket].bucket_head;
147 This->props[bucket].bucket_head = i;
153 static inline dispex_prop_t* alloc_prop(jsdisp_t *This, const WCHAR *name, prop_type_t type, DWORD flags)
158 if(FAILED(resize_props(This)))
161 prop = &This->props[This->prop_cnt];
162 prop->name = heap_strdupW(name);
167 prop->hash = string_hash(name);
169 bucket = get_props_idx(This, prop->hash);
170 prop->bucket_next = This->props[bucket].bucket_head;
171 This->props[bucket].bucket_head = This->prop_cnt++;
175 static dispex_prop_t *alloc_protref(jsdisp_t *This, const WCHAR *name, DWORD ref)
179 ret = alloc_prop(This, name, PROP_PROTREF, 0);
187 static HRESULT find_prop_name(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
189 const builtin_prop_t *builtin;
190 unsigned bucket, pos, prev = 0;
193 bucket = get_props_idx(This, hash);
194 pos = This->props[bucket].bucket_head;
196 if(!strcmpW(name, This->props[pos].name)) {
198 This->props[prev].bucket_next = This->props[pos].bucket_next;
199 This->props[pos].bucket_next = This->props[bucket].bucket_head;
200 This->props[bucket].bucket_head = pos;
203 *ret = &This->props[pos];
208 pos = This->props[pos].bucket_next;
211 builtin = find_builtin_prop(This, name);
213 prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
215 return E_OUTOFMEMORY;
226 static HRESULT find_prop_name_prot(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
228 dispex_prop_t *prop, *del=NULL;
231 hres = find_prop_name(This, hash, name, &prop);
234 if(prop && prop->type==PROP_DELETED) {
241 if(This->prototype) {
242 hres = find_prop_name_prot(This->prototype, hash, name, &prop);
247 del->type = PROP_PROTREF;
249 del->u.ref = prop - This->prototype->props;
252 prop = alloc_protref(This, prop->name, prop - This->prototype->props);
254 return E_OUTOFMEMORY;
266 static HRESULT ensure_prop_name(jsdisp_t *This, const WCHAR *name, BOOL search_prot, DWORD create_flags, dispex_prop_t **ret)
272 hres = find_prop_name_prot(This, string_hash(name), name, &prop);
274 hres = find_prop_name(This, string_hash(name), name, &prop);
275 if(SUCCEEDED(hres) && (!prop || prop->type==PROP_DELETED)) {
276 TRACE("creating prop %s\n", debugstr_w(name));
279 prop->type = PROP_VARIANT;
280 prop->flags = create_flags;
282 prop = alloc_prop(This, name, PROP_VARIANT, create_flags);
284 return E_OUTOFMEMORY;
287 VariantInit(&prop->u.var);
294 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
299 static DISPID this_id = DISPID_THIS;
303 for(i = 0; i < dp->cNamedArgs; i++) {
304 if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
308 oldargs = dp->rgvarg;
309 dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
311 return E_OUTOFMEMORY;
312 memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
313 V_VT(dp->rgvarg) = VT_DISPATCH;
314 V_DISPATCH(dp->rgvarg) = jsthis;
318 DISPID *old = dp->rgdispidNamedArgs;
319 dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
320 if(!dp->rgdispidNamedArgs) {
321 heap_free(dp->rgvarg);
322 return E_OUTOFMEMORY;
325 memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
326 dp->rgdispidNamedArgs[0] = DISPID_THIS;
329 dp->rgdispidNamedArgs = &this_id;
336 static HRESULT invoke_prop_func(jsdisp_t *This, jsdisp_t *jsthis, dispex_prop_t *prop, WORD flags,
337 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
345 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
346 WARN("%s is not a constructor\n", debugstr_w(prop->name));
350 set_jsdisp(&vthis, jsthis);
351 hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei);
352 vdisp_release(&vthis);
356 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
360 if(V_VT(&prop->u.var) != VT_DISPATCH) {
361 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
365 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
367 hres = set_this(&new_dp, dp, to_disp(jsthis));
371 hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei);
373 if(new_dp.rgvarg != dp->rgvarg) {
374 heap_free(new_dp.rgvarg);
375 if(new_dp.cNamedArgs > 1)
376 heap_free(new_dp.rgdispidNamedArgs);
382 ERR("type %d\n", prop->type);
388 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
389 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
395 if(prop->u.p->flags & PROPF_METHOD) {
397 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
398 prop->u.p->flags, NULL, &obj);
402 prop->type = PROP_VARIANT;
403 var_set_jsdisp(&prop->u.var, obj);
404 hres = VariantCopy(retv, &prop->u.var);
408 set_jsdisp(&vthis, This);
409 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei);
410 vdisp_release(&vthis);
414 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
417 hres = VariantCopy(retv, &prop->u.var);
420 ERR("type %d\n", prop->type);
425 TRACE("fail %08x\n", hres);
429 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
433 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
434 jsexcept_t *ei, IServiceProvider *caller)
438 if(prop->flags & PROPF_CONST)
443 if(!(prop->flags & PROPF_METHOD)) {
444 DISPPARAMS dp = {val, NULL, 1, 0};
447 set_jsdisp(&vthis, This);
448 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei);
449 vdisp_release(&vthis);
453 prop->type = PROP_VARIANT;
454 prop->flags = PROPF_ENUM;
455 V_VT(&prop->u.var) = VT_EMPTY;
458 VariantClear(&prop->u.var);
461 ERR("type %d\n", prop->type);
465 hres = VariantCopy(&prop->u.var, val);
469 if(This->builtin_info->on_put)
470 This->builtin_info->on_put(This, prop->name);
472 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
476 static HRESULT fill_protrefs(jsdisp_t *This)
478 dispex_prop_t *iter, *prop;
484 fill_protrefs(This->prototype);
486 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
489 hres = find_prop_name(This, iter->hash, iter->name, &prop);
492 if(!prop || prop->type==PROP_DELETED) {
494 prop->type = PROP_PROTREF;
496 prop->u.ref = iter - This->prototype->props;
498 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
500 return E_OUTOFMEMORY;
508 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
510 return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
513 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
515 jsdisp_t *This = impl_from_IDispatchEx(iface);
517 if(IsEqualGUID(&IID_IUnknown, riid)) {
518 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
519 *ppv = &This->IDispatchEx_iface;
520 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
521 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
522 *ppv = &This->IDispatchEx_iface;
523 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
524 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
525 *ppv = &This->IDispatchEx_iface;
526 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
527 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
532 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
534 return E_NOINTERFACE;
537 IUnknown_AddRef((IUnknown*)*ppv);
541 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
543 jsdisp_t *This = impl_from_IDispatchEx(iface);
544 LONG ref = InterlockedIncrement(&This->ref);
546 TRACE("(%p) ref=%d\n", This, ref);
551 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
553 jsdisp_t *This = impl_from_IDispatchEx(iface);
554 LONG ref = InterlockedDecrement(&This->ref);
556 TRACE("(%p) ref=%d\n", This, ref);
561 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
562 if(prop->type == PROP_VARIANT)
563 VariantClear(&prop->u.var);
564 heap_free(prop->name);
566 heap_free(This->props);
567 script_release(This->ctx);
569 jsdisp_release(This->prototype);
571 if(This->builtin_info->destructor)
572 This->builtin_info->destructor(This);
580 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
582 jsdisp_t *This = impl_from_IDispatchEx(iface);
584 TRACE("(%p)->(%p)\n", This, pctinfo);
590 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
593 jsdisp_t *This = impl_from_IDispatchEx(iface);
594 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
598 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
599 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
602 jsdisp_t *This = impl_from_IDispatchEx(iface);
606 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
609 for(i=0; i < cNames; i++) {
610 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
618 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
619 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
620 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
622 jsdisp_t *This = impl_from_IDispatchEx(iface);
624 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
625 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
627 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
628 pDispParams, pVarResult, pExcepInfo, NULL);
631 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
633 jsdisp_t *This = impl_from_IDispatchEx(iface);
635 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
637 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
638 FIXME("Unsupported grfdex %x\n", grfdex);
642 return jsdisp_get_id(This, bstrName, grfdex, pid);
645 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
646 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
648 jsdisp_t *This = impl_from_IDispatchEx(iface);
653 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
656 V_VT(pvarRes) = VT_EMPTY;
658 prop = get_prop(This, id);
659 if(!prop || prop->type == PROP_DELETED) {
660 TRACE("invalid id\n");
661 return DISP_E_MEMBERNOTFOUND;
664 memset(&jsexcept, 0, sizeof(jsexcept));
667 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
668 wFlags = DISPATCH_METHOD;
670 case DISPATCH_METHOD:
671 case DISPATCH_CONSTRUCT:
672 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
674 case DISPATCH_PROPERTYGET:
675 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
677 case DISPATCH_PROPERTYPUT: {
680 for(i=0; i < pdp->cNamedArgs; i++) {
681 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
685 if(i == pdp->cNamedArgs) {
686 TRACE("no value to set\n");
687 return DISP_E_PARAMNOTOPTIONAL;
690 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
694 FIXME("Unimplemented flags %x\n", wFlags);
704 static HRESULT delete_prop(dispex_prop_t *prop)
706 if(prop->type == PROP_VARIANT) {
707 VariantClear(&prop->u.var);
708 prop->type = PROP_DELETED;
713 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
715 jsdisp_t *This = impl_from_IDispatchEx(iface);
719 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
721 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
722 FIXME("Unsupported grfdex %x\n", grfdex);
724 hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
728 TRACE("not found\n");
732 return delete_prop(prop);
735 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
737 jsdisp_t *This = impl_from_IDispatchEx(iface);
740 TRACE("(%p)->(%x)\n", This, id);
742 prop = get_prop(This, id);
744 WARN("invalid id\n");
745 return DISP_E_MEMBERNOTFOUND;
748 return delete_prop(prop);
751 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
753 jsdisp_t *This = impl_from_IDispatchEx(iface);
754 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
758 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
760 jsdisp_t *This = impl_from_IDispatchEx(iface);
763 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
765 prop = get_prop(This, id);
766 if(!prop || !prop->name || prop->type == PROP_DELETED)
767 return DISP_E_MEMBERNOTFOUND;
769 *pbstrName = SysAllocString(prop->name);
771 return E_OUTOFMEMORY;
776 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
778 jsdisp_t *This = impl_from_IDispatchEx(iface);
782 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
784 if(id == DISPID_STARTENUM) {
785 hres = fill_protrefs(This);
790 if(id+1>=0 && id+1<This->prop_cnt) {
791 iter = &This->props[id+1];
793 *pid = DISPID_STARTENUM;
797 while(iter < This->props + This->prop_cnt) {
798 if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
799 *pid = prop_to_id(This, iter);
805 *pid = DISPID_STARTENUM;
809 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
811 jsdisp_t *This = impl_from_IDispatchEx(iface);
812 FIXME("(%p)->(%p)\n", This, ppunk);
816 static IDispatchExVtbl DispatchExVtbl = {
817 DispatchEx_QueryInterface,
820 DispatchEx_GetTypeInfoCount,
821 DispatchEx_GetTypeInfo,
822 DispatchEx_GetIDsOfNames,
824 DispatchEx_GetDispID,
826 DispatchEx_DeleteMemberByName,
827 DispatchEx_DeleteMemberByDispID,
828 DispatchEx_GetMemberProperties,
829 DispatchEx_GetMemberName,
830 DispatchEx_GetNextDispID,
831 DispatchEx_GetNameSpaceParent
834 jsdisp_t *as_jsdisp(IDispatch *disp)
836 assert(disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl);
837 return impl_from_IDispatchEx((IDispatchEx*)disp);
840 jsdisp_t *to_jsdisp(IDispatch *disp)
842 return disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl ? impl_from_IDispatchEx((IDispatchEx*)disp) : NULL;
845 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
847 TRACE("%p (%p)\n", dispex, prototype);
849 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
851 dispex->builtin_info = builtin_info;
853 dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
855 return E_OUTOFMEMORY;
857 dispex->prototype = prototype;
859 jsdisp_addref(prototype);
861 dispex->prop_cnt = 1;
862 if(builtin_info->value_prop.invoke) {
863 dispex->props[0].type = PROP_BUILTIN;
864 dispex->props[0].u.p = &builtin_info->value_prop;
866 dispex->props[0].type = PROP_DELETED;
875 static const builtin_info_t dispex_info = {
883 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
888 ret = heap_alloc_zero(sizeof(jsdisp_t));
890 return E_OUTOFMEMORY;
892 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
900 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
902 jsdisp_t *prot = NULL;
906 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
907 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
909 hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
910 if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
914 V_VT(&var) = VT_EMPTY;
915 memset(&jsexcept, 0, sizeof(jsexcept));
916 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
918 ERR("Could not get prototype\n");
922 if(V_VT(&var) == VT_DISPATCH)
923 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
927 hres = init_dispex(dispex, ctx, builtin_info, prot);
930 jsdisp_release(prot);
934 hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
935 if(SUCCEEDED(hres)) {
939 var_set_jsdisp(&var, constr);
940 memset(&jsexcept, 0, sizeof(jsexcept));
941 hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
944 jsdisp_release(dispex);
949 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
954 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
961 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
966 if(flags & fdexNameEnsure)
967 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
969 hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
973 if(prop && prop->type!=PROP_DELETED) {
974 *id = prop_to_id(jsdisp, prop);
978 TRACE("not found %s\n", debugstr_w(name));
979 return DISP_E_UNKNOWNNAME;
982 HRESULT jsdisp_call_value(jsdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
987 set_jsdisp(&vdisp, jsthis);
988 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei);
989 vdisp_release(&vdisp);
993 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
997 memset(ei, 0, sizeof(*ei));
999 V_VT(retv) = VT_EMPTY;
1001 prop = get_prop(disp, id);
1003 return DISP_E_MEMBERNOTFOUND;
1005 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, NULL);
1008 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
1011 dispex_prop_t *prop;
1014 hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1018 memset(ei, 0, sizeof(*ei));
1020 V_VT(retv) = VT_EMPTY;
1022 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, NULL);
1025 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1028 IDispatchEx *dispex;
1031 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1033 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei);
1034 jsdisp_release(jsdisp);
1038 memset(ei, 0, sizeof(*ei));
1039 if(retv && arg_cnt(dp))
1040 flags |= DISPATCH_PROPERTYGET;
1043 V_VT(retv) = VT_EMPTY;
1044 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1048 if(flags == DISPATCH_CONSTRUCT) {
1049 WARN("IDispatch cannot be constructor\n");
1050 return DISP_E_MEMBERNOTFOUND;
1053 TRACE("using IDispatch\n");
1054 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
1057 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, &ctx->jscaller->IServiceProvider_iface);
1058 IDispatchEx_Release(dispex);
1063 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei)
1065 dispex_prop_t *prop;
1068 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1072 return prop_put(obj, prop, val, ei, NULL);
1075 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1077 dispex_prop_t *prop;
1080 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1084 return VariantCopy(&prop->u.var, val);
1087 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei)
1091 static const WCHAR formatW[] = {'%','d',0};
1093 sprintfW(buf, formatW, idx);
1094 return jsdisp_propput_name(obj, buf, val, ei);
1097 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1102 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1104 dispex_prop_t *prop;
1106 prop = get_prop(jsdisp, id);
1108 hres = prop_put(jsdisp, prop, val, ei, NULL);
1110 hres = DISP_E_MEMBERNOTFOUND;
1112 jsdisp_release(jsdisp);
1114 DISPID dispid = DISPID_PROPERTYPUT;
1115 DISPPARAMS dp = {val, &dispid, 1, 1};
1116 IDispatchEx *dispex;
1118 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1119 if(SUCCEEDED(hres)) {
1120 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei,
1121 &ctx->jscaller->IServiceProvider_iface);
1122 IDispatchEx_Release(dispex);
1126 TRACE("using IDispatch\n");
1127 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1134 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei)
1136 DISPPARAMS dp = {NULL, NULL, 0, 0};
1137 dispex_prop_t *prop;
1140 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1144 V_VT(var) = VT_EMPTY;
1145 if(!prop || prop->type==PROP_DELETED)
1148 return prop_get(obj, prop, &dp, var, ei, NULL);
1151 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei)
1154 DISPPARAMS dp = {NULL, NULL, 0, 0};
1155 dispex_prop_t *prop;
1158 static const WCHAR formatW[] = {'%','d',0};
1160 sprintfW(name, formatW, idx);
1162 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1166 V_VT(var) = VT_EMPTY;
1167 if(!prop || prop->type==PROP_DELETED)
1168 return DISP_E_UNKNOWNNAME;
1170 return prop_get(obj, prop, &dp, var, ei, NULL);
1173 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei)
1175 DISPPARAMS dp = {NULL,NULL,0,0};
1176 dispex_prop_t *prop;
1178 prop = get_prop(jsdisp, id);
1180 return DISP_E_MEMBERNOTFOUND;
1182 V_VT(val) = VT_EMPTY;
1183 return prop_get(jsdisp, prop, &dp, val, ei, NULL);
1186 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1188 DISPPARAMS dp = {NULL,NULL,0,0};
1189 IDispatchEx *dispex;
1193 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1195 hres = jsdisp_propget(jsdisp, id, val, ei);
1196 jsdisp_release(jsdisp);
1200 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1204 TRACE("using IDispatch\n");
1205 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1208 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei,
1209 &ctx->jscaller->IServiceProvider_iface);
1210 IDispatchEx_Release(dispex);
1215 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1217 static const WCHAR formatW[] = {'%','d',0};
1219 dispex_prop_t *prop;
1222 sprintfW(buf, formatW, idx);
1224 hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1225 if(FAILED(hres) || !prop)
1228 return delete_prop(prop);
1231 VARIANT_BOOL jsdisp_is_own_prop(jsdisp_t *obj, BSTR name)
1233 dispex_prop_t *prop;
1236 hres = find_prop_name(obj, string_hash(name), name, &prop);
1238 return VARIANT_FALSE;
1240 return VARIANT_FALSE;
1242 return prop->type==PROP_VARIANT ? VARIANT_TRUE : VARIANT_FALSE;