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 IDispatch *get_this(DISPPARAMS *dp)
298 for(i=0; i < dp->cNamedArgs; i++) {
299 if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
300 if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
301 return V_DISPATCH(dp->rgvarg+i);
303 WARN("This is not VT_DISPATCH\n");
308 TRACE("no this passed\n");
312 static HRESULT convert_params(const DISPPARAMS *dp, VARIANT *buf, unsigned *argc, VARIANT **ret)
319 cnt = dp->cArgs - dp->cNamedArgs;
322 argv = heap_alloc(cnt * sizeof(VARIANT));
324 return E_OUTOFMEMORY;
329 for(i = 0; i < cnt; i++) {
330 s = dp->rgvarg+dp->cArgs-i-1;
333 V_VT(argv+i) = VT_I4;
334 V_I4(argv+i) = V_I2(s);
337 V_VT(argv+i) = VT_I4;
338 V_I4(argv+i) = V_INT(s);
350 static HRESULT invoke_prop_func(jsdisp_t *This, IDispatch *jsthis, dispex_prop_t *prop, WORD flags,
351 unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
357 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
358 WARN("%s is not a constructor\n", debugstr_w(prop->name));
362 if(prop->name || This->builtin_info->class != JSCLASS_FUNCTION) {
366 set_disp(&vthis, jsthis);
368 set_jsdisp(&vthis, This);
369 hres = prop->u.p->invoke(This->ctx, &vthis, flags, argc, argv, retv, ei);
370 vdisp_release(&vthis);
372 /* Function object calls are special case */
373 hres = Function_invoke(This, jsthis, flags, argc, argv, retv, ei);
378 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref,
379 flags, argc, argv, retv, ei, caller);
381 if(V_VT(&prop->u.var) != VT_DISPATCH) {
382 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
386 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
388 return disp_call_value(This->ctx, V_DISPATCH(&prop->u.var), jsthis, flags, argc, argv, retv, ei);
391 ERR("type %d\n", prop->type);
397 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
398 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
404 if(prop->u.p->flags & PROPF_METHOD) {
406 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
407 prop->u.p->flags, NULL, &obj);
411 prop->type = PROP_VARIANT;
412 var_set_jsdisp(&prop->u.var, obj);
413 hres = VariantCopy(retv, &prop->u.var);
417 set_jsdisp(&vthis, This);
418 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, 0, NULL, retv, ei);
419 vdisp_release(&vthis);
423 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
426 hres = VariantCopy(retv, &prop->u.var);
429 ERR("type %d\n", prop->type);
434 TRACE("fail %08x\n", hres);
438 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
442 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
443 jsexcept_t *ei, IServiceProvider *caller)
447 if(prop->flags & PROPF_CONST)
452 if(!(prop->flags & PROPF_METHOD)) {
455 set_jsdisp(&vthis, This);
456 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, 1, val, NULL, ei);
457 vdisp_release(&vthis);
461 prop->type = PROP_VARIANT;
462 prop->flags = PROPF_ENUM;
463 V_VT(&prop->u.var) = VT_EMPTY;
466 VariantClear(&prop->u.var);
469 ERR("type %d\n", prop->type);
473 hres = VariantCopy(&prop->u.var, val);
477 if(This->builtin_info->on_put)
478 This->builtin_info->on_put(This, prop->name);
480 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
484 static HRESULT fill_protrefs(jsdisp_t *This)
486 dispex_prop_t *iter, *prop;
492 fill_protrefs(This->prototype);
494 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
497 hres = find_prop_name(This, iter->hash, iter->name, &prop);
500 if(!prop || prop->type==PROP_DELETED) {
502 prop->type = PROP_PROTREF;
504 prop->u.ref = iter - This->prototype->props;
506 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
508 return E_OUTOFMEMORY;
516 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
518 return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
521 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
523 jsdisp_t *This = impl_from_IDispatchEx(iface);
525 if(IsEqualGUID(&IID_IUnknown, riid)) {
526 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
527 *ppv = &This->IDispatchEx_iface;
528 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
529 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
530 *ppv = &This->IDispatchEx_iface;
531 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
532 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
533 *ppv = &This->IDispatchEx_iface;
534 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
535 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
540 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
542 return E_NOINTERFACE;
545 IUnknown_AddRef((IUnknown*)*ppv);
549 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
551 jsdisp_t *This = impl_from_IDispatchEx(iface);
552 LONG ref = InterlockedIncrement(&This->ref);
554 TRACE("(%p) ref=%d\n", This, ref);
559 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
561 jsdisp_t *This = impl_from_IDispatchEx(iface);
562 LONG ref = InterlockedDecrement(&This->ref);
564 TRACE("(%p) ref=%d\n", This, ref);
569 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
570 if(prop->type == PROP_VARIANT)
571 VariantClear(&prop->u.var);
572 heap_free(prop->name);
574 heap_free(This->props);
575 script_release(This->ctx);
577 jsdisp_release(This->prototype);
579 if(This->builtin_info->destructor)
580 This->builtin_info->destructor(This);
588 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
590 jsdisp_t *This = impl_from_IDispatchEx(iface);
592 TRACE("(%p)->(%p)\n", This, pctinfo);
598 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
601 jsdisp_t *This = impl_from_IDispatchEx(iface);
602 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
606 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
607 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
610 jsdisp_t *This = impl_from_IDispatchEx(iface);
614 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
617 for(i=0; i < cNames; i++) {
618 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
626 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
627 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
628 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
630 jsdisp_t *This = impl_from_IDispatchEx(iface);
632 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
633 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
635 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
636 pDispParams, pVarResult, pExcepInfo, NULL);
639 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
641 jsdisp_t *This = impl_from_IDispatchEx(iface);
643 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
645 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
646 FIXME("Unsupported grfdex %x\n", grfdex);
650 return jsdisp_get_id(This, bstrName, grfdex, pid);
653 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
654 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
656 jsdisp_t *This = impl_from_IDispatchEx(iface);
661 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
664 V_VT(pvarRes) = VT_EMPTY;
666 prop = get_prop(This, id);
667 if(!prop || prop->type == PROP_DELETED) {
668 TRACE("invalid id\n");
669 return DISP_E_MEMBERNOTFOUND;
672 memset(&jsexcept, 0, sizeof(jsexcept));
675 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
676 wFlags = DISPATCH_METHOD;
678 case DISPATCH_METHOD:
679 case DISPATCH_CONSTRUCT: {
684 hres = convert_params(pdp, buf, &argc, &argv);
688 hres = invoke_prop_func(This, get_this(pdp), prop, wFlags, argc, argv, pvarRes, &jsexcept, pspCaller);
693 case DISPATCH_PROPERTYGET:
694 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
696 case DISPATCH_PROPERTYPUT: {
699 for(i=0; i < pdp->cNamedArgs; i++) {
700 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
704 if(i == pdp->cNamedArgs) {
705 TRACE("no value to set\n");
706 return DISP_E_PARAMNOTOPTIONAL;
709 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
713 FIXME("Unimplemented flags %x\n", wFlags);
723 static HRESULT delete_prop(dispex_prop_t *prop)
725 if(prop->type == PROP_VARIANT) {
726 VariantClear(&prop->u.var);
727 prop->type = PROP_DELETED;
732 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
734 jsdisp_t *This = impl_from_IDispatchEx(iface);
738 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
740 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
741 FIXME("Unsupported grfdex %x\n", grfdex);
743 hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
747 TRACE("not found\n");
751 return delete_prop(prop);
754 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
756 jsdisp_t *This = impl_from_IDispatchEx(iface);
759 TRACE("(%p)->(%x)\n", This, id);
761 prop = get_prop(This, id);
763 WARN("invalid id\n");
764 return DISP_E_MEMBERNOTFOUND;
767 return delete_prop(prop);
770 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
772 jsdisp_t *This = impl_from_IDispatchEx(iface);
773 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
777 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
779 jsdisp_t *This = impl_from_IDispatchEx(iface);
782 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
784 prop = get_prop(This, id);
785 if(!prop || !prop->name || prop->type == PROP_DELETED)
786 return DISP_E_MEMBERNOTFOUND;
788 *pbstrName = SysAllocString(prop->name);
790 return E_OUTOFMEMORY;
795 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
797 jsdisp_t *This = impl_from_IDispatchEx(iface);
801 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
803 if(id == DISPID_STARTENUM) {
804 hres = fill_protrefs(This);
809 if(id+1>=0 && id+1<This->prop_cnt) {
810 iter = &This->props[id+1];
812 *pid = DISPID_STARTENUM;
816 while(iter < This->props + This->prop_cnt) {
817 if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
818 *pid = prop_to_id(This, iter);
824 *pid = DISPID_STARTENUM;
828 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
830 jsdisp_t *This = impl_from_IDispatchEx(iface);
831 FIXME("(%p)->(%p)\n", This, ppunk);
835 static IDispatchExVtbl DispatchExVtbl = {
836 DispatchEx_QueryInterface,
839 DispatchEx_GetTypeInfoCount,
840 DispatchEx_GetTypeInfo,
841 DispatchEx_GetIDsOfNames,
843 DispatchEx_GetDispID,
845 DispatchEx_DeleteMemberByName,
846 DispatchEx_DeleteMemberByDispID,
847 DispatchEx_GetMemberProperties,
848 DispatchEx_GetMemberName,
849 DispatchEx_GetNextDispID,
850 DispatchEx_GetNameSpaceParent
853 jsdisp_t *as_jsdisp(IDispatch *disp)
855 assert(disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl);
856 return impl_from_IDispatchEx((IDispatchEx*)disp);
859 jsdisp_t *to_jsdisp(IDispatch *disp)
861 return disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl ? impl_from_IDispatchEx((IDispatchEx*)disp) : NULL;
864 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
866 TRACE("%p (%p)\n", dispex, prototype);
868 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
870 dispex->builtin_info = builtin_info;
872 dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
874 return E_OUTOFMEMORY;
876 dispex->prototype = prototype;
878 jsdisp_addref(prototype);
880 dispex->prop_cnt = 1;
881 if(builtin_info->value_prop.invoke) {
882 dispex->props[0].type = PROP_BUILTIN;
883 dispex->props[0].u.p = &builtin_info->value_prop;
885 dispex->props[0].type = PROP_DELETED;
894 static const builtin_info_t dispex_info = {
902 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
907 ret = heap_alloc_zero(sizeof(jsdisp_t));
909 return E_OUTOFMEMORY;
911 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
919 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
921 jsdisp_t *prot = NULL;
925 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
927 hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
928 if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
932 V_VT(&var) = VT_EMPTY;
933 memset(&jsexcept, 0, sizeof(jsexcept));
934 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
936 ERR("Could not get prototype\n");
940 if(V_VT(&var) == VT_DISPATCH)
941 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
945 hres = init_dispex(dispex, ctx, builtin_info, prot);
948 jsdisp_release(prot);
952 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
957 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
964 static void ensure_retval_type(VARIANT *v)
977 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
982 if(flags & fdexNameEnsure)
983 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
985 hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
989 if(prop && prop->type!=PROP_DELETED) {
990 *id = prop_to_id(jsdisp, prop);
994 TRACE("not found %s\n", debugstr_w(name));
995 return DISP_E_UNKNOWNNAME;
998 HRESULT jsdisp_call_value(jsdisp_t *jsfunc, IDispatch *jsthis, WORD flags, unsigned argc, VARIANT *argv, VARIANT *retv,
1003 if(is_class(jsfunc, JSCLASS_FUNCTION)) {
1004 hres = Function_invoke(jsfunc, jsthis, flags, argc, argv, retv, ei);
1008 set_disp(&vdisp, jsthis);
1009 hres = jsfunc->builtin_info->value_prop.invoke(jsfunc->ctx, &vdisp, flags, argc, argv, retv, ei);
1010 vdisp_release(&vdisp);
1015 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
1017 dispex_prop_t *prop;
1019 memset(ei, 0, sizeof(*ei));
1021 V_VT(retv) = VT_EMPTY;
1023 prop = get_prop(disp, id);
1025 return DISP_E_MEMBERNOTFOUND;
1027 return invoke_prop_func(disp, to_disp(disp), prop, flags, argc, argv, retv, ei, NULL);
1030 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, unsigned argc, VARIANT *argv, VARIANT *retv,
1033 dispex_prop_t *prop;
1036 hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1040 memset(ei, 0, sizeof(*ei));
1042 V_VT(retv) = VT_EMPTY;
1044 return invoke_prop_func(disp, to_disp(disp), prop, flags, argc, argv, retv, ei, NULL);
1047 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, unsigned argc, VARIANT *argv,
1048 VARIANT *retv, jsexcept_t *ei)
1050 IDispatchEx *dispex;
1057 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1059 if(flags & DISPATCH_PROPERTYPUT) {
1060 FIXME("disp_call(propput) on builtin object\n");
1064 hres = jsdisp_call(jsdisp, id, flags, argc, argv, retv, ei);
1065 jsdisp_release(jsdisp);
1069 memset(ei, 0, sizeof(*ei));
1071 flags |= DISPATCH_PROPERTYGET;
1075 if(flags & DISPATCH_PROPERTYPUT) {
1076 static DISPID propput_dispid = DISPID_PROPERTYPUT;
1079 dp.rgdispidNamedArgs = &propput_dispid;
1082 dp.rgdispidNamedArgs = NULL;
1086 dp.rgvarg = heap_alloc(argc*sizeof(VARIANT));
1088 return E_OUTOFMEMORY;
1093 for(i=0; i<argc; i++)
1094 dp.rgvarg[argc-i-1] = argv[i];
1097 V_VT(retv) = VT_EMPTY;
1099 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1100 if(SUCCEEDED(hres)) {
1101 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, &dp, retv, &ei->ei, &ctx->jscaller->IServiceProvider_iface);
1102 IDispatchEx_Release(dispex);
1106 if(flags == DISPATCH_CONSTRUCT) {
1107 WARN("IDispatch cannot be constructor\n");
1108 return DISP_E_MEMBERNOTFOUND;
1111 TRACE("using IDispatch\n");
1112 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, &dp, retv, &ei->ei, &err);
1115 if(dp.rgvarg != buf)
1116 heap_free(dp.rgvarg);
1121 ensure_retval_type(retv);
1125 HRESULT disp_call_value(script_ctx_t *ctx, IDispatch *disp, IDispatch *jsthis, WORD flags, unsigned argc, VARIANT *argv,
1126 VARIANT *retv, jsexcept_t *ei)
1129 IDispatchEx *dispex;
1135 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1137 if(flags & DISPATCH_PROPERTYPUT) {
1138 FIXME("disp_call(propput) on builtin object\n");
1142 hres = jsdisp_call_value(jsdisp, jsthis, flags, argc, argv, retv, ei);
1143 jsdisp_release(jsdisp);
1147 memset(ei, 0, sizeof(*ei));
1149 flags |= DISPATCH_PROPERTYGET;
1153 static DISPID this_id = DISPID_THIS;
1157 dp.rgdispidNamedArgs = &this_id;
1161 dp.rgdispidNamedArgs = NULL;
1164 if(dp.cArgs > sizeof(buf)/sizeof(*buf)) {
1165 dp.rgvarg = heap_alloc(dp.cArgs*sizeof(VARIANT));
1167 return E_OUTOFMEMORY;
1172 for(i=0; i<argc; i++)
1173 dp.rgvarg[argc-i-1] = argv[i];
1175 V_VT(dp.rgvarg) = VT_DISPATCH;
1176 V_DISPATCH(dp.rgvarg) = jsthis;
1180 V_VT(retv) = VT_EMPTY;
1181 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1182 if(SUCCEEDED(hres)) {
1183 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, ctx->lcid, flags, &dp, retv, &ei->ei,
1184 &ctx->jscaller->IServiceProvider_iface);
1185 IDispatchEx_Release(dispex);
1189 if(flags == DISPATCH_CONSTRUCT) {
1190 WARN("IDispatch cannot be constructor\n");
1191 return DISP_E_MEMBERNOTFOUND;
1194 TRACE("using IDispatch\n");
1195 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, ctx->lcid, flags, &dp, retv, &ei->ei, &err);
1198 if(dp.rgvarg != buf)
1199 heap_free(dp.rgvarg);
1204 ensure_retval_type(retv);
1208 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei)
1210 dispex_prop_t *prop;
1213 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1217 return prop_put(obj, prop, val, ei, NULL);
1220 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1222 dispex_prop_t *prop;
1225 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1229 return VariantCopy(&prop->u.var, val);
1232 HRESULT jsdisp_propput_dontenum(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1234 dispex_prop_t *prop;
1237 hres = ensure_prop_name(obj, name, FALSE, 0, &prop);
1241 return VariantCopy(&prop->u.var, val);
1244 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei)
1248 static const WCHAR formatW[] = {'%','d',0};
1250 sprintfW(buf, formatW, idx);
1251 return jsdisp_propput_name(obj, buf, val, ei);
1254 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1259 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1261 dispex_prop_t *prop;
1263 prop = get_prop(jsdisp, id);
1265 hres = prop_put(jsdisp, prop, val, ei, NULL);
1267 hres = DISP_E_MEMBERNOTFOUND;
1269 jsdisp_release(jsdisp);
1271 DISPID dispid = DISPID_PROPERTYPUT;
1272 DISPPARAMS dp = {val, &dispid, 1, 1};
1273 IDispatchEx *dispex;
1275 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1276 if(SUCCEEDED(hres)) {
1277 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei,
1278 &ctx->jscaller->IServiceProvider_iface);
1279 IDispatchEx_Release(dispex);
1283 TRACE("using IDispatch\n");
1284 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1291 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei)
1293 DISPPARAMS dp = {NULL, NULL, 0, 0};
1294 dispex_prop_t *prop;
1297 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1301 V_VT(var) = VT_EMPTY;
1302 if(!prop || prop->type==PROP_DELETED)
1305 return prop_get(obj, prop, &dp, var, ei, NULL);
1308 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei)
1311 DISPPARAMS dp = {NULL, NULL, 0, 0};
1312 dispex_prop_t *prop;
1315 static const WCHAR formatW[] = {'%','d',0};
1317 sprintfW(name, formatW, idx);
1319 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1323 V_VT(var) = VT_EMPTY;
1324 if(!prop || prop->type==PROP_DELETED)
1325 return DISP_E_UNKNOWNNAME;
1327 return prop_get(obj, prop, &dp, var, ei, NULL);
1330 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei)
1332 DISPPARAMS dp = {NULL,NULL,0,0};
1333 dispex_prop_t *prop;
1335 prop = get_prop(jsdisp, id);
1337 return DISP_E_MEMBERNOTFOUND;
1339 V_VT(val) = VT_EMPTY;
1340 return prop_get(jsdisp, prop, &dp, val, ei, NULL);
1343 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1345 DISPPARAMS dp = {NULL,NULL,0,0};
1346 IDispatchEx *dispex;
1350 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1352 hres = jsdisp_propget(jsdisp, id, val, ei);
1353 jsdisp_release(jsdisp);
1357 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1358 if(SUCCEEDED(hres)) {
1359 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei,
1360 &ctx->jscaller->IServiceProvider_iface);
1361 IDispatchEx_Release(dispex);
1365 TRACE("using IDispatch\n");
1366 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1371 ensure_retval_type(val);
1375 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1377 static const WCHAR formatW[] = {'%','d',0};
1379 dispex_prop_t *prop;
1382 sprintfW(buf, formatW, idx);
1384 hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1385 if(FAILED(hres) || !prop)
1388 return delete_prop(prop);
1391 HRESULT jsdisp_is_own_prop(jsdisp_t *obj, BSTR name, VARIANT_BOOL *ret)
1393 dispex_prop_t *prop;
1396 hres = find_prop_name(obj, string_hash(name), name, &prop);
1400 *ret = prop && (prop->type == PROP_VARIANT || prop->type == PROP_BUILTIN) ? VARIANT_TRUE : VARIANT_FALSE;