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_JSVAL;
280 prop->flags = create_flags;
282 prop = alloc_prop(This, name, PROP_JSVAL, create_flags);
284 return E_OUTOFMEMORY;
287 prop->u.val = jsval_undefined();
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, jsval_t *buf, unsigned *argc, jsval_t **ret)
319 cnt = dp->cArgs - dp->cNamedArgs;
322 argv = heap_alloc(cnt * sizeof(*argv));
324 return E_OUTOFMEMORY;
329 for(i = 0; i < cnt; i++) {
330 hres = variant_to_jsval(dp->rgvarg+dp->cArgs-i-1, argv+i);
333 jsval_release(argv[i]);
345 static HRESULT invoke_prop_func(jsdisp_t *This, IDispatch *jsthis, dispex_prop_t *prop, WORD flags,
346 unsigned argc, jsval_t *argv, jsval_t *r, jsexcept_t *ei, IServiceProvider *caller)
352 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
353 WARN("%s is not a constructor\n", debugstr_w(prop->name));
357 if(prop->name || This->builtin_info->class != JSCLASS_FUNCTION) {
361 set_disp(&vthis, jsthis);
363 set_jsdisp(&vthis, This);
364 hres = prop->u.p->invoke(This->ctx, &vthis, flags, argc, argv, r, ei);
365 vdisp_release(&vthis);
367 /* Function object calls are special case */
368 hres = Function_invoke(This, jsthis, flags, argc, argv, r, ei);
373 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref,
374 flags, argc, argv, r, ei, caller);
376 if(!is_object_instance(prop->u.val)) {
377 FIXME("invoke %s\n", debugstr_jsval(prop->u.val));
381 TRACE("call %s %p\n", debugstr_w(prop->name), get_object(prop->u.val));
383 return disp_call_value(This->ctx, get_object(prop->u.val), jsthis, flags, argc, argv, r, ei);
386 ERR("type %d\n", prop->type);
392 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
393 jsval_t *r, jsexcept_t *ei, IServiceProvider *caller)
399 if(prop->u.p->flags & PROPF_METHOD) {
401 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
402 prop->u.p->flags, NULL, &obj);
406 prop->type = PROP_JSVAL;
407 prop->u.val = jsval_obj(obj);
414 set_jsdisp(&vthis, This);
415 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, 0, NULL, r, ei);
416 vdisp_release(&vthis);
420 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, r, ei, caller);
423 hres = jsval_copy(prop->u.val, r);
426 ERR("type %d\n", prop->type);
431 TRACE("fail %08x\n", hres);
435 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_jsval(*r));
439 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, jsval_t val,
440 jsexcept_t *ei, IServiceProvider *caller)
444 if(prop->flags & PROPF_CONST)
449 if(!(prop->flags & PROPF_METHOD)) {
452 set_jsdisp(&vthis, This);
453 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, 1, &val, NULL, ei);
454 vdisp_release(&vthis);
458 prop->type = PROP_JSVAL;
459 prop->flags = PROPF_ENUM;
460 prop->u.val = jsval_undefined();
463 jsval_release(prop->u.val);
466 ERR("type %d\n", prop->type);
470 hres = jsval_copy(val, &prop->u.val);
472 prop->u.val = jsval_undefined();
476 if(This->builtin_info->on_put)
477 This->builtin_info->on_put(This, prop->name);
479 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_jsval(val));
483 static HRESULT fill_protrefs(jsdisp_t *This)
485 dispex_prop_t *iter, *prop;
491 fill_protrefs(This->prototype);
493 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
496 hres = find_prop_name(This, iter->hash, iter->name, &prop);
499 if(!prop || prop->type==PROP_DELETED) {
501 prop->type = PROP_PROTREF;
503 prop->u.ref = iter - This->prototype->props;
505 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
507 return E_OUTOFMEMORY;
515 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
517 return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
520 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
522 jsdisp_t *This = impl_from_IDispatchEx(iface);
524 if(IsEqualGUID(&IID_IUnknown, riid)) {
525 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
526 *ppv = &This->IDispatchEx_iface;
527 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
528 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
529 *ppv = &This->IDispatchEx_iface;
530 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
531 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
532 *ppv = &This->IDispatchEx_iface;
533 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
534 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
539 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
541 return E_NOINTERFACE;
544 IUnknown_AddRef((IUnknown*)*ppv);
548 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
550 jsdisp_t *This = impl_from_IDispatchEx(iface);
551 LONG ref = InterlockedIncrement(&This->ref);
553 TRACE("(%p) ref=%d\n", This, ref);
558 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
560 jsdisp_t *This = impl_from_IDispatchEx(iface);
561 LONG ref = InterlockedDecrement(&This->ref);
563 TRACE("(%p) ref=%d\n", This, ref);
568 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
569 if(prop->type == PROP_JSVAL)
570 jsval_release(prop->u.val);
571 heap_free(prop->name);
573 heap_free(This->props);
574 script_release(This->ctx);
576 jsdisp_release(This->prototype);
578 if(This->builtin_info->destructor)
579 This->builtin_info->destructor(This);
587 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
589 jsdisp_t *This = impl_from_IDispatchEx(iface);
591 TRACE("(%p)->(%p)\n", This, pctinfo);
597 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
600 jsdisp_t *This = impl_from_IDispatchEx(iface);
601 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
605 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
606 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
609 jsdisp_t *This = impl_from_IDispatchEx(iface);
613 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
616 for(i=0; i < cNames; i++) {
617 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
625 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
626 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
627 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
629 jsdisp_t *This = impl_from_IDispatchEx(iface);
631 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
632 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
634 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
635 pDispParams, pVarResult, pExcepInfo, NULL);
638 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
640 jsdisp_t *This = impl_from_IDispatchEx(iface);
642 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
644 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
645 FIXME("Unsupported grfdex %x\n", grfdex);
649 return jsdisp_get_id(This, bstrName, grfdex, pid);
652 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
653 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
655 jsdisp_t *This = impl_from_IDispatchEx(iface);
660 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
663 V_VT(pvarRes) = VT_EMPTY;
665 prop = get_prop(This, id);
666 if(!prop || prop->type == PROP_DELETED) {
667 TRACE("invalid id\n");
668 return DISP_E_MEMBERNOTFOUND;
671 memset(&jsexcept, 0, sizeof(jsexcept));
674 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
675 wFlags = DISPATCH_METHOD;
677 case DISPATCH_METHOD:
678 case DISPATCH_CONSTRUCT: {
679 jsval_t *argv, buf[6], r;
682 hres = convert_params(pdp, buf, &argc, &argv);
686 hres = invoke_prop_func(This, get_this(pdp), prop, wFlags, argc, argv, pvarRes ? &r : NULL, &jsexcept, pspCaller);
689 if(SUCCEEDED(hres) && pvarRes) {
690 hres = jsval_to_variant(r, pvarRes);
695 case DISPATCH_PROPERTYGET: {
698 hres = prop_get(This, prop, pdp, &r, &jsexcept, pspCaller);
699 if(SUCCEEDED(hres)) {
700 hres = jsval_to_variant(r, pvarRes);
705 case DISPATCH_PROPERTYPUT: {
709 for(i=0; i < pdp->cNamedArgs; i++) {
710 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
714 if(i == pdp->cNamedArgs) {
715 TRACE("no value to set\n");
716 return DISP_E_PARAMNOTOPTIONAL;
719 hres = variant_to_jsval(pdp->rgvarg+i, &val);
723 hres = prop_put(This, prop, val, &jsexcept, pspCaller);
728 FIXME("Unimplemented flags %x\n", wFlags);
738 static HRESULT delete_prop(dispex_prop_t *prop)
740 if(prop->type == PROP_JSVAL) {
741 jsval_release(prop->u.val);
742 prop->type = PROP_DELETED;
747 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
749 jsdisp_t *This = impl_from_IDispatchEx(iface);
753 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
755 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
756 FIXME("Unsupported grfdex %x\n", grfdex);
758 hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
762 TRACE("not found\n");
766 return delete_prop(prop);
769 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
771 jsdisp_t *This = impl_from_IDispatchEx(iface);
774 TRACE("(%p)->(%x)\n", This, id);
776 prop = get_prop(This, id);
778 WARN("invalid id\n");
779 return DISP_E_MEMBERNOTFOUND;
782 return delete_prop(prop);
785 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
787 jsdisp_t *This = impl_from_IDispatchEx(iface);
788 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
792 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
794 jsdisp_t *This = impl_from_IDispatchEx(iface);
797 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
799 prop = get_prop(This, id);
800 if(!prop || !prop->name || prop->type == PROP_DELETED)
801 return DISP_E_MEMBERNOTFOUND;
803 *pbstrName = SysAllocString(prop->name);
805 return E_OUTOFMEMORY;
810 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
812 jsdisp_t *This = impl_from_IDispatchEx(iface);
816 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
818 if(id == DISPID_STARTENUM) {
819 hres = fill_protrefs(This);
824 if(id+1>=0 && id+1<This->prop_cnt) {
825 iter = &This->props[id+1];
827 *pid = DISPID_STARTENUM;
831 while(iter < This->props + This->prop_cnt) {
832 if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
833 *pid = prop_to_id(This, iter);
839 *pid = DISPID_STARTENUM;
843 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
845 jsdisp_t *This = impl_from_IDispatchEx(iface);
846 FIXME("(%p)->(%p)\n", This, ppunk);
850 static IDispatchExVtbl DispatchExVtbl = {
851 DispatchEx_QueryInterface,
854 DispatchEx_GetTypeInfoCount,
855 DispatchEx_GetTypeInfo,
856 DispatchEx_GetIDsOfNames,
858 DispatchEx_GetDispID,
860 DispatchEx_DeleteMemberByName,
861 DispatchEx_DeleteMemberByDispID,
862 DispatchEx_GetMemberProperties,
863 DispatchEx_GetMemberName,
864 DispatchEx_GetNextDispID,
865 DispatchEx_GetNameSpaceParent
868 jsdisp_t *as_jsdisp(IDispatch *disp)
870 assert(disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl);
871 return impl_from_IDispatchEx((IDispatchEx*)disp);
874 jsdisp_t *to_jsdisp(IDispatch *disp)
876 return disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl ? impl_from_IDispatchEx((IDispatchEx*)disp) : NULL;
879 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
881 TRACE("%p (%p)\n", dispex, prototype);
883 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
885 dispex->builtin_info = builtin_info;
887 dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
889 return E_OUTOFMEMORY;
891 dispex->prototype = prototype;
893 jsdisp_addref(prototype);
895 dispex->prop_cnt = 1;
896 if(builtin_info->value_prop.invoke) {
897 dispex->props[0].type = PROP_BUILTIN;
898 dispex->props[0].u.p = &builtin_info->value_prop;
900 dispex->props[0].type = PROP_DELETED;
909 static const builtin_info_t dispex_info = {
917 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
922 ret = heap_alloc_zero(sizeof(jsdisp_t));
924 return E_OUTOFMEMORY;
926 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
934 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
936 jsdisp_t *prot = NULL;
940 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
942 hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
943 if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
947 memset(&jsexcept, 0, sizeof(jsexcept));
948 hres = prop_get(constr, prop, NULL, &val, &jsexcept, NULL/*FIXME*/);
950 ERR("Could not get prototype\n");
954 if(is_object_instance(val))
955 prot = iface_to_jsdisp((IUnknown*)get_object(val));
959 hres = init_dispex(dispex, ctx, builtin_info, prot);
962 jsdisp_release(prot);
966 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
971 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
978 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
983 if(flags & fdexNameEnsure)
984 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
986 hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
990 if(prop && prop->type!=PROP_DELETED) {
991 *id = prop_to_id(jsdisp, prop);
995 TRACE("not found %s\n", debugstr_w(name));
996 return DISP_E_UNKNOWNNAME;
999 HRESULT jsdisp_call_value(jsdisp_t *jsfunc, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r,
1004 if(is_class(jsfunc, JSCLASS_FUNCTION)) {
1005 hres = Function_invoke(jsfunc, jsthis, flags, argc, argv, r, ei);
1009 set_disp(&vdisp, jsthis);
1010 hres = jsfunc->builtin_info->value_prop.invoke(jsfunc->ctx, &vdisp, flags, argc, argv, r, ei);
1011 vdisp_release(&vdisp);
1016 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r, jsexcept_t *ei)
1018 dispex_prop_t *prop;
1020 prop = get_prop(disp, id);
1022 return DISP_E_MEMBERNOTFOUND;
1024 memset(ei, 0, sizeof(*ei));
1025 return invoke_prop_func(disp, to_disp(disp), prop, flags, argc, argv, r, ei, NULL);
1028 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r,
1031 dispex_prop_t *prop;
1034 hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1038 memset(ei, 0, sizeof(*ei));
1039 return invoke_prop_func(disp, to_disp(disp), prop, flags, argc, argv, r, ei, NULL);
1042 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, unsigned argc, jsval_t *argv,
1043 jsval_t *ret, jsexcept_t *ei)
1045 IDispatchEx *dispex;
1047 VARIANT buf[6], retv;
1052 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1054 if(flags & DISPATCH_PROPERTYPUT) {
1055 FIXME("disp_call(propput) on builtin object\n");
1059 hres = jsdisp_call(jsdisp, id, flags, argc, argv, ret, ei);
1060 jsdisp_release(jsdisp);
1064 memset(ei, 0, sizeof(*ei));
1066 flags |= DISPATCH_PROPERTYGET;
1070 if(flags & DISPATCH_PROPERTYPUT) {
1071 static DISPID propput_dispid = DISPID_PROPERTYPUT;
1074 dp.rgdispidNamedArgs = &propput_dispid;
1077 dp.rgdispidNamedArgs = NULL;
1081 dp.rgvarg = heap_alloc(argc*sizeof(VARIANT));
1083 return E_OUTOFMEMORY;
1088 for(i=0; i<argc; i++) {
1089 hres = jsval_to_variant(argv[i], dp.rgvarg+argc-i-1);
1092 VariantClear(dp.rgvarg+argc-i-1);
1093 if(dp.rgvarg != buf)
1094 heap_free(dp.rgvarg);
1099 V_VT(&retv) = VT_EMPTY;
1100 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1101 if(SUCCEEDED(hres)) {
1102 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, &dp, ret ? &retv : NULL, &ei->ei,
1103 &ctx->jscaller->IServiceProvider_iface);
1104 IDispatchEx_Release(dispex);
1108 if(flags == DISPATCH_CONSTRUCT) {
1109 WARN("IDispatch cannot be constructor\n");
1110 return DISP_E_MEMBERNOTFOUND;
1113 TRACE("using IDispatch\n");
1114 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, &dp, ret ? &retv : NULL, &ei->ei, &err);
1117 for(i=0; i<argc; i++)
1118 VariantClear(dp.rgvarg+argc-i-1);
1119 if(dp.rgvarg != buf)
1120 heap_free(dp.rgvarg);
1125 hres = variant_to_jsval(&retv, ret);
1126 VariantClear(&retv);
1131 HRESULT disp_call_value(script_ctx_t *ctx, IDispatch *disp, IDispatch *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1132 jsval_t *r, jsexcept_t *ei)
1135 IDispatchEx *dispex;
1136 VARIANT buf[6], retv;
1141 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1143 if(flags & DISPATCH_PROPERTYPUT) {
1144 FIXME("disp_call(propput) on builtin object\n");
1148 hres = jsdisp_call_value(jsdisp, jsthis, flags, argc, argv, r, ei);
1149 jsdisp_release(jsdisp);
1153 memset(ei, 0, sizeof(*ei));
1155 flags |= DISPATCH_PROPERTYGET;
1159 static DISPID this_id = DISPID_THIS;
1163 dp.rgdispidNamedArgs = &this_id;
1167 dp.rgdispidNamedArgs = NULL;
1170 if(dp.cArgs > sizeof(buf)/sizeof(*buf)) {
1171 dp.rgvarg = heap_alloc(dp.cArgs*sizeof(VARIANT));
1173 return E_OUTOFMEMORY;
1178 for(i=0; i<argc; i++) {
1179 hres = jsval_to_variant(argv[i], dp.rgvarg+dp.cArgs-i-1);
1182 VariantClear(dp.rgvarg+dp.cArgs-i-1);
1183 if(dp.rgvarg != buf)
1184 heap_free(dp.rgvarg);
1189 V_VT(dp.rgvarg) = VT_DISPATCH;
1190 V_DISPATCH(dp.rgvarg) = jsthis;
1193 V_VT(&retv) = VT_EMPTY;
1194 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1195 if(SUCCEEDED(hres)) {
1196 hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, ctx->lcid, flags, &dp, r ? &retv : NULL, &ei->ei,
1197 &ctx->jscaller->IServiceProvider_iface);
1198 IDispatchEx_Release(dispex);
1202 if(flags == DISPATCH_CONSTRUCT) {
1203 WARN("IDispatch cannot be constructor\n");
1204 return DISP_E_MEMBERNOTFOUND;
1207 TRACE("using IDispatch\n");
1208 hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, ctx->lcid, flags, &dp, r ? &retv : NULL, &ei->ei, &err);
1211 for(i=0; i<argc; i++)
1212 VariantClear(dp.rgvarg+dp.cArgs-i-1);
1213 if(dp.rgvarg != buf)
1214 heap_free(dp.rgvarg);
1221 hres = variant_to_jsval(&retv, r);
1222 VariantClear(&retv);
1226 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, jsval_t val, jsexcept_t *ei)
1228 dispex_prop_t *prop;
1231 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1235 return prop_put(obj, prop, val, ei, NULL);
1238 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, jsval_t val)
1240 dispex_prop_t *prop;
1243 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1247 return jsval_copy(val, &prop->u.val);
1250 HRESULT jsdisp_propput_dontenum(jsdisp_t *obj, const WCHAR *name, jsval_t val)
1252 dispex_prop_t *prop;
1255 hres = ensure_prop_name(obj, name, FALSE, 0, &prop);
1259 return jsval_copy(val, &prop->u.val);
1262 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, jsval_t val, jsexcept_t *ei)
1266 static const WCHAR formatW[] = {'%','d',0};
1268 sprintfW(buf, formatW, idx);
1269 return jsdisp_propput_name(obj, buf, val, ei);
1272 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, jsval_t val, jsexcept_t *ei)
1277 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1279 dispex_prop_t *prop;
1281 prop = get_prop(jsdisp, id);
1283 hres = prop_put(jsdisp, prop, val, ei, NULL);
1285 hres = DISP_E_MEMBERNOTFOUND;
1287 jsdisp_release(jsdisp);
1289 DISPID dispid = DISPID_PROPERTYPUT;
1291 DISPPARAMS dp = {&var, &dispid, 1, 1};
1292 IDispatchEx *dispex;
1294 hres = jsval_to_variant(val, &var);
1298 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1299 if(SUCCEEDED(hres)) {
1300 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei,
1301 &ctx->jscaller->IServiceProvider_iface);
1302 IDispatchEx_Release(dispex);
1306 TRACE("using IDispatch\n");
1307 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1316 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, jsval_t *val, jsexcept_t *ei)
1318 DISPPARAMS dp = {NULL, NULL, 0, 0};
1319 dispex_prop_t *prop;
1322 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1326 if(!prop || prop->type==PROP_DELETED) {
1327 *val = jsval_undefined();
1331 return prop_get(obj, prop, &dp, val, ei, NULL);
1334 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, jsval_t *r, jsexcept_t *ei)
1337 DISPPARAMS dp = {NULL, NULL, 0, 0};
1338 dispex_prop_t *prop;
1341 static const WCHAR formatW[] = {'%','d',0};
1343 sprintfW(name, formatW, idx);
1345 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1349 if(!prop || prop->type==PROP_DELETED) {
1350 *r = jsval_undefined();
1351 return DISP_E_UNKNOWNNAME;
1354 return prop_get(obj, prop, &dp, r, ei, NULL);
1357 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, jsval_t *val, jsexcept_t *ei)
1359 DISPPARAMS dp = {NULL,NULL,0,0};
1360 dispex_prop_t *prop;
1362 prop = get_prop(jsdisp, id);
1364 return DISP_E_MEMBERNOTFOUND;
1366 return prop_get(jsdisp, prop, &dp, val, ei, NULL);
1369 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, jsval_t *val, jsexcept_t *ei)
1371 DISPPARAMS dp = {NULL,NULL,0,0};
1372 IDispatchEx *dispex;
1377 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1379 hres = jsdisp_propget(jsdisp, id, val, ei);
1380 jsdisp_release(jsdisp);
1384 V_VT(&var) = VT_EMPTY;
1385 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1386 if(SUCCEEDED(hres)) {
1387 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, &var, &ei->ei,
1388 &ctx->jscaller->IServiceProvider_iface);
1389 IDispatchEx_Release(dispex);
1393 TRACE("using IDispatch\n");
1394 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, &var, &ei->ei, &err);
1399 return variant_to_jsval(&var, val);
1402 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1404 static const WCHAR formatW[] = {'%','d',0};
1406 dispex_prop_t *prop;
1409 sprintfW(buf, formatW, idx);
1411 hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1412 if(FAILED(hres) || !prop)
1415 return delete_prop(prop);
1418 HRESULT jsdisp_is_own_prop(jsdisp_t *obj, BSTR name, VARIANT_BOOL *ret)
1420 dispex_prop_t *prop;
1423 hres = find_prop_name(obj, string_hash(name), name, &prop);
1427 *ret = prop && (prop->type == PROP_JSVAL || prop->type == PROP_BUILTIN) ? VARIANT_TRUE : VARIANT_FALSE;