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 interface 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, const 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 IDispatch *get_this(DISPPARAMS *dp)
340 for(i=0; i < dp->cNamedArgs; i++) {
341 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
342 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
343 return V_DISPATCH(dp->rgvarg+i);
345 WARN("This is not VT_DISPATCH\n");
350 TRACE("no this passed\n");
354 static HRESULT convert_params(const DISPPARAMS *dp, VARIANT *buf, DISPPARAMS *ret)
356 BOOL need_conversion = FALSE;
363 for(i = 0; i < ret->cArgs && !need_conversion; i++) {
364 switch(V_VT(get_arg(dp, i))) {
367 need_conversion = TRUE;
376 ret->rgvarg = heap_alloc(ret->cArgs * sizeof(VARIANT));
378 return E_OUTOFMEMORY;
383 for(i = 0; i < ret->cArgs; i++) {
403 static HRESULT invoke_prop_func(jsdisp_t *This, IDispatch *jsthis, dispex_prop_t *prop, WORD flags,
404 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
410 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
411 WARN("%s is not a constructor\n", debugstr_w(prop->name));
415 if(prop->name || This->builtin_info->class != JSCLASS_FUNCTION) {
419 set_disp(&vthis, jsthis);
421 set_jsdisp(&vthis, This);
422 hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei);
423 vdisp_release(&vthis);
425 /* Function object calls are special case */
426 hres = Function_invoke(This, jsthis, flags, dp, retv, ei);
431 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
433 if(V_VT(&prop->u.var) != VT_DISPATCH) {
434 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
438 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
440 return disp_call_value(This->ctx, V_DISPATCH(&prop->u.var), jsthis, flags, dp, retv, ei);
443 ERR("type %d\n", prop->type);
449 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
450 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
456 if(prop->u.p->flags & PROPF_METHOD) {
458 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
459 prop->u.p->flags, NULL, &obj);
463 prop->type = PROP_VARIANT;
464 var_set_jsdisp(&prop->u.var, obj);
465 hres = VariantCopy(retv, &prop->u.var);
469 set_jsdisp(&vthis, This);
470 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei);
471 vdisp_release(&vthis);
475 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
478 hres = VariantCopy(retv, &prop->u.var);
481 ERR("type %d\n", prop->type);
486 TRACE("fail %08x\n", hres);
490 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
494 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
495 jsexcept_t *ei, IServiceProvider *caller)
499 if(prop->flags & PROPF_CONST)
504 if(!(prop->flags & PROPF_METHOD)) {
505 DISPPARAMS dp = {val, NULL, 1, 0};
508 set_jsdisp(&vthis, This);
509 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei);
510 vdisp_release(&vthis);
514 prop->type = PROP_VARIANT;
515 prop->flags = PROPF_ENUM;
516 V_VT(&prop->u.var) = VT_EMPTY;
519 VariantClear(&prop->u.var);
522 ERR("type %d\n", prop->type);
526 hres = VariantCopy(&prop->u.var, val);
530 if(This->builtin_info->on_put)
531 This->builtin_info->on_put(This, prop->name);
533 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
537 static HRESULT fill_protrefs(jsdisp_t *This)
539 dispex_prop_t *iter, *prop;
545 fill_protrefs(This->prototype);
547 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
550 hres = find_prop_name(This, iter->hash, iter->name, &prop);
553 if(!prop || prop->type==PROP_DELETED) {
555 prop->type = PROP_PROTREF;
557 prop->u.ref = iter - This->prototype->props;
559 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
561 return E_OUTOFMEMORY;
569 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
571 return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
574 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
576 jsdisp_t *This = impl_from_IDispatchEx(iface);
578 if(IsEqualGUID(&IID_IUnknown, riid)) {
579 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
580 *ppv = &This->IDispatchEx_iface;
581 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
582 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
583 *ppv = &This->IDispatchEx_iface;
584 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
585 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
586 *ppv = &This->IDispatchEx_iface;
587 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
588 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
593 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
595 return E_NOINTERFACE;
598 IUnknown_AddRef((IUnknown*)*ppv);
602 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
604 jsdisp_t *This = impl_from_IDispatchEx(iface);
605 LONG ref = InterlockedIncrement(&This->ref);
607 TRACE("(%p) ref=%d\n", This, ref);
612 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
614 jsdisp_t *This = impl_from_IDispatchEx(iface);
615 LONG ref = InterlockedDecrement(&This->ref);
617 TRACE("(%p) ref=%d\n", This, ref);
622 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
623 if(prop->type == PROP_VARIANT)
624 VariantClear(&prop->u.var);
625 heap_free(prop->name);
627 heap_free(This->props);
628 script_release(This->ctx);
630 jsdisp_release(This->prototype);
632 if(This->builtin_info->destructor)
633 This->builtin_info->destructor(This);
641 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
643 jsdisp_t *This = impl_from_IDispatchEx(iface);
645 TRACE("(%p)->(%p)\n", This, pctinfo);
651 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
654 jsdisp_t *This = impl_from_IDispatchEx(iface);
655 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
659 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
660 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
663 jsdisp_t *This = impl_from_IDispatchEx(iface);
667 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
670 for(i=0; i < cNames; i++) {
671 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
679 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
680 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
681 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
683 jsdisp_t *This = impl_from_IDispatchEx(iface);
685 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
686 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
688 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
689 pDispParams, pVarResult, pExcepInfo, NULL);
692 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
694 jsdisp_t *This = impl_from_IDispatchEx(iface);
696 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
698 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
699 FIXME("Unsupported grfdex %x\n", grfdex);
703 return jsdisp_get_id(This, bstrName, grfdex, pid);
706 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
707 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
709 jsdisp_t *This = impl_from_IDispatchEx(iface);
714 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
717 V_VT(pvarRes) = VT_EMPTY;
719 prop = get_prop(This, id);
720 if(!prop || prop->type == PROP_DELETED) {
721 TRACE("invalid id\n");
722 return DISP_E_MEMBERNOTFOUND;
725 memset(&jsexcept, 0, sizeof(jsexcept));
728 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
729 wFlags = DISPATCH_METHOD;
731 case DISPATCH_METHOD:
732 case DISPATCH_CONSTRUCT: {
736 hres = convert_params(pdp, buf, ¶ms);
740 hres = invoke_prop_func(This, get_this(pdp), prop, wFlags, ¶ms, pvarRes, &jsexcept, pspCaller);
741 if(params.rgvarg != buf && params.rgvarg != pdp->rgvarg)
742 heap_free(params.rgvarg);
745 case DISPATCH_PROPERTYGET:
746 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
748 case DISPATCH_PROPERTYPUT: {
751 for(i=0; i < pdp->cNamedArgs; i++) {
752 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
756 if(i == pdp->cNamedArgs) {
757 TRACE("no value to set\n");
758 return DISP_E_PARAMNOTOPTIONAL;
761 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
765 FIXME("Unimplemented flags %x\n", wFlags);
775 static HRESULT delete_prop(dispex_prop_t *prop)
777 if(prop->type == PROP_VARIANT) {
778 VariantClear(&prop->u.var);
779 prop->type = PROP_DELETED;
784 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
786 jsdisp_t *This = impl_from_IDispatchEx(iface);
790 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
792 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
793 FIXME("Unsupported grfdex %x\n", grfdex);
795 hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
799 TRACE("not found\n");
803 return delete_prop(prop);
806 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
808 jsdisp_t *This = impl_from_IDispatchEx(iface);
811 TRACE("(%p)->(%x)\n", This, id);
813 prop = get_prop(This, id);
815 WARN("invalid id\n");
816 return DISP_E_MEMBERNOTFOUND;
819 return delete_prop(prop);
822 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
824 jsdisp_t *This = impl_from_IDispatchEx(iface);
825 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
829 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
831 jsdisp_t *This = impl_from_IDispatchEx(iface);
834 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
836 prop = get_prop(This, id);
837 if(!prop || !prop->name || prop->type == PROP_DELETED)
838 return DISP_E_MEMBERNOTFOUND;
840 *pbstrName = SysAllocString(prop->name);
842 return E_OUTOFMEMORY;
847 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
849 jsdisp_t *This = impl_from_IDispatchEx(iface);
853 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
855 if(id == DISPID_STARTENUM) {
856 hres = fill_protrefs(This);
861 if(id+1>=0 && id+1<This->prop_cnt) {
862 iter = &This->props[id+1];
864 *pid = DISPID_STARTENUM;
868 while(iter < This->props + This->prop_cnt) {
869 if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
870 *pid = prop_to_id(This, iter);
876 *pid = DISPID_STARTENUM;
880 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
882 jsdisp_t *This = impl_from_IDispatchEx(iface);
883 FIXME("(%p)->(%p)\n", This, ppunk);
887 static IDispatchExVtbl DispatchExVtbl = {
888 DispatchEx_QueryInterface,
891 DispatchEx_GetTypeInfoCount,
892 DispatchEx_GetTypeInfo,
893 DispatchEx_GetIDsOfNames,
895 DispatchEx_GetDispID,
897 DispatchEx_DeleteMemberByName,
898 DispatchEx_DeleteMemberByDispID,
899 DispatchEx_GetMemberProperties,
900 DispatchEx_GetMemberName,
901 DispatchEx_GetNextDispID,
902 DispatchEx_GetNameSpaceParent
905 jsdisp_t *as_jsdisp(IDispatch *disp)
907 assert(disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl);
908 return impl_from_IDispatchEx((IDispatchEx*)disp);
911 jsdisp_t *to_jsdisp(IDispatch *disp)
913 return disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl ? impl_from_IDispatchEx((IDispatchEx*)disp) : NULL;
916 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
918 TRACE("%p (%p)\n", dispex, prototype);
920 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
922 dispex->builtin_info = builtin_info;
924 dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
926 return E_OUTOFMEMORY;
928 dispex->prototype = prototype;
930 jsdisp_addref(prototype);
932 dispex->prop_cnt = 1;
933 if(builtin_info->value_prop.invoke) {
934 dispex->props[0].type = PROP_BUILTIN;
935 dispex->props[0].u.p = &builtin_info->value_prop;
937 dispex->props[0].type = PROP_DELETED;
946 static const builtin_info_t dispex_info = {
954 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
959 ret = heap_alloc_zero(sizeof(jsdisp_t));
961 return E_OUTOFMEMORY;
963 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
971 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
973 jsdisp_t *prot = NULL;
977 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
978 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
980 hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
981 if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
985 V_VT(&var) = VT_EMPTY;
986 memset(&jsexcept, 0, sizeof(jsexcept));
987 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
989 ERR("Could not get prototype\n");
993 if(V_VT(&var) == VT_DISPATCH)
994 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
998 hres = init_dispex(dispex, ctx, builtin_info, prot);
1001 jsdisp_release(prot);
1005 hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
1006 if(SUCCEEDED(hres)) {
1007 jsexcept_t jsexcept;
1010 var_set_jsdisp(&var, constr);
1011 memset(&jsexcept, 0, sizeof(jsexcept));
1012 hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
1015 jsdisp_release(dispex);
1020 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
1025 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
1032 static void ensure_retval_type(VARIANT *v)
1045 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
1047 dispex_prop_t *prop;
1050 if(flags & fdexNameEnsure)
1051 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
1053 hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
1057 if(prop && prop->type!=PROP_DELETED) {
1058 *id = prop_to_id(jsdisp, prop);
1062 TRACE("not found %s\n", debugstr_w(name));
1063 return DISP_E_UNKNOWNNAME;
1066 HRESULT jsdisp_call_value(jsdisp_t *jsfunc, IDispatch *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1070 TRACE("args %p\n", dp->rgvarg);
1072 if(is_class(jsfunc, JSCLASS_FUNCTION)) {
1073 hres = Function_invoke(jsfunc, jsthis, flags, dp, retv, ei);
1077 set_disp(&vdisp, jsthis);
1078 hres = jsfunc->builtin_info->value_prop.invoke(jsfunc->ctx, &vdisp, flags, dp, retv, ei);
1079 vdisp_release(&vdisp);
1084 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1086 dispex_prop_t *prop;
1088 memset(ei, 0, sizeof(*ei));
1090 V_VT(retv) = VT_EMPTY;
1092 prop = get_prop(disp, id);
1094 return DISP_E_MEMBERNOTFOUND;
1096 return invoke_prop_func(disp, to_disp(disp), prop, flags, dp, retv, ei, NULL);
1099 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
1102 dispex_prop_t *prop;
1105 hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1109 memset(ei, 0, sizeof(*ei));
1111 V_VT(retv) = VT_EMPTY;
1113 return invoke_prop_func(disp, to_disp(disp), prop, flags, dp, retv, ei, NULL);
1116 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1119 IDispatchEx *dispex;
1122 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1124 if(flags & DISPATCH_PROPERTYPUT) {
1125 FIXME("disp_call(propput) on builtin object\n");
1129 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei);
1130 jsdisp_release(jsdisp);
1134 memset(ei, 0, sizeof(*ei));
1135 if(retv && arg_cnt(dp))
1136 flags |= DISPATCH_PROPERTYGET;
1139 V_VT(retv) = VT_EMPTY;
1140 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1141 if(SUCCEEDED(hres)) {
1142 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, &ctx->jscaller->IServiceProvider_iface);
1143 IDispatchEx_Release(dispex);
1147 if(flags == DISPATCH_CONSTRUCT) {
1148 WARN("IDispatch cannot be constructor\n");
1149 return DISP_E_MEMBERNOTFOUND;
1152 TRACE("using IDispatch\n");
1153 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
1160 ensure_retval_type(retv);
1164 HRESULT disp_call_value(script_ctx_t *ctx, IDispatch *disp, IDispatch *jsthis, WORD flags, DISPPARAMS *dp,
1165 VARIANT *retv, jsexcept_t *ei)
1169 IDispatchEx *dispex;
1172 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1174 if(flags & DISPATCH_PROPERTYPUT) {
1175 FIXME("disp_call(propput) on builtin object\n");
1179 hres = jsdisp_call_value(jsdisp, jsthis, flags, dp, retv, ei);
1180 jsdisp_release(jsdisp);
1184 memset(ei, 0, sizeof(*ei));
1185 if(retv && arg_cnt(dp))
1186 flags |= DISPATCH_PROPERTYGET;
1189 hres = set_this(¶ms, dp, jsthis);
1197 V_VT(retv) = VT_EMPTY;
1198 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1199 if(SUCCEEDED(hres)) {
1200 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, ctx->lcid, flags, ¶ms, retv, &ei->ei,
1201 &ctx->jscaller->IServiceProvider_iface);
1202 IDispatchEx_Release(dispex);
1206 if(flags == DISPATCH_CONSTRUCT) {
1207 WARN("IDispatch cannot be constructor\n");
1208 return DISP_E_MEMBERNOTFOUND;
1211 TRACE("using IDispatch\n");
1212 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, ctx->lcid, flags, ¶ms, retv, &ei->ei, &err);
1215 if(params.rgvarg != dp->rgvarg) {
1216 heap_free(params.rgvarg);
1217 if(params.cNamedArgs > 1)
1218 heap_free(params.rgdispidNamedArgs);
1225 ensure_retval_type(retv);
1229 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei)
1231 dispex_prop_t *prop;
1234 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1238 return prop_put(obj, prop, val, ei, NULL);
1241 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1243 dispex_prop_t *prop;
1246 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1250 return VariantCopy(&prop->u.var, val);
1253 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei)
1257 static const WCHAR formatW[] = {'%','d',0};
1259 sprintfW(buf, formatW, idx);
1260 return jsdisp_propput_name(obj, buf, val, ei);
1263 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1268 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1270 dispex_prop_t *prop;
1272 prop = get_prop(jsdisp, id);
1274 hres = prop_put(jsdisp, prop, val, ei, NULL);
1276 hres = DISP_E_MEMBERNOTFOUND;
1278 jsdisp_release(jsdisp);
1280 DISPID dispid = DISPID_PROPERTYPUT;
1281 DISPPARAMS dp = {val, &dispid, 1, 1};
1282 IDispatchEx *dispex;
1284 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1285 if(SUCCEEDED(hres)) {
1286 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei,
1287 &ctx->jscaller->IServiceProvider_iface);
1288 IDispatchEx_Release(dispex);
1292 TRACE("using IDispatch\n");
1293 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1300 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei)
1302 DISPPARAMS dp = {NULL, NULL, 0, 0};
1303 dispex_prop_t *prop;
1306 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1310 V_VT(var) = VT_EMPTY;
1311 if(!prop || prop->type==PROP_DELETED)
1314 return prop_get(obj, prop, &dp, var, ei, NULL);
1317 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei)
1320 DISPPARAMS dp = {NULL, NULL, 0, 0};
1321 dispex_prop_t *prop;
1324 static const WCHAR formatW[] = {'%','d',0};
1326 sprintfW(name, formatW, idx);
1328 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1332 V_VT(var) = VT_EMPTY;
1333 if(!prop || prop->type==PROP_DELETED)
1334 return DISP_E_UNKNOWNNAME;
1336 return prop_get(obj, prop, &dp, var, ei, NULL);
1339 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei)
1341 DISPPARAMS dp = {NULL,NULL,0,0};
1342 dispex_prop_t *prop;
1344 prop = get_prop(jsdisp, id);
1346 return DISP_E_MEMBERNOTFOUND;
1348 V_VT(val) = VT_EMPTY;
1349 return prop_get(jsdisp, prop, &dp, val, ei, NULL);
1352 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1354 DISPPARAMS dp = {NULL,NULL,0,0};
1355 IDispatchEx *dispex;
1359 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1361 hres = jsdisp_propget(jsdisp, id, val, ei);
1362 jsdisp_release(jsdisp);
1366 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1367 if(SUCCEEDED(hres)) {
1368 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei,
1369 &ctx->jscaller->IServiceProvider_iface);
1370 IDispatchEx_Release(dispex);
1374 TRACE("using IDispatch\n");
1375 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1380 ensure_retval_type(val);
1384 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1386 static const WCHAR formatW[] = {'%','d',0};
1388 dispex_prop_t *prop;
1391 sprintfW(buf, formatW, idx);
1393 hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1394 if(FAILED(hres) || !prop)
1397 return delete_prop(prop);
1400 VARIANT_BOOL jsdisp_is_own_prop(jsdisp_t *obj, BSTR name)
1402 dispex_prop_t *prop;
1405 hres = find_prop_name(obj, string_hash(name), name, &prop);
1407 return VARIANT_FALSE;
1409 return VARIANT_FALSE;
1411 return prop->type==PROP_VARIANT ? VARIANT_TRUE : VARIANT_FALSE;