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
, 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
convert_params(const DISPPARAMS
*dp
, VARIANT
*buf
, DISPPARAMS
*ret
)
338 BOOL need_conversion
= FALSE
;
345 for(i
= 0; i
< ret
->cArgs
; i
++) {
346 if(V_VT(get_arg(dp
, i
)) == VT_I2
) {
347 need_conversion
= TRUE
;
356 ret
->rgvarg
= heap_alloc(ret
->cArgs
* sizeof(VARIANT
));
358 return E_OUTOFMEMORY
;
363 for(i
= 0; i
< ret
->cArgs
; i
++) {
379 static HRESULT
invoke_prop_func(jsdisp_t
*This
, jsdisp_t
*jsthis
, dispex_prop_t
*prop
, WORD flags
,
380 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
390 if(flags
== DISPATCH_CONSTRUCT
&& (prop
->flags
& PROPF_METHOD
)) {
391 WARN("%s is not a constructor\n", debugstr_w(prop
->name
));
395 hres
= convert_params(dp
, buf
, ¶ms
);
399 set_jsdisp(&vthis
, jsthis
);
400 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, flags
, ¶ms
, retv
, ei
);
401 vdisp_release(&vthis
);
402 if(params
.rgvarg
!= buf
&& params
.rgvarg
!= dp
->rgvarg
)
403 heap_free(params
.rgvarg
);
407 return invoke_prop_func(This
->prototype
, jsthis
, This
->prototype
->props
+prop
->u
.ref
, flags
, dp
, retv
, ei
, caller
);
411 if(V_VT(&prop
->u
.var
) != VT_DISPATCH
) {
412 FIXME("invoke vt %d\n", V_VT(&prop
->u
.var
));
416 TRACE("call %s %p\n", debugstr_w(prop
->name
), V_DISPATCH(&prop
->u
.var
));
418 hres
= set_this(&new_dp
, dp
, to_disp(jsthis
));
422 hres
= disp_call(This
->ctx
, V_DISPATCH(&prop
->u
.var
), DISPID_VALUE
, flags
, &new_dp
, retv
, ei
);
424 if(new_dp
.rgvarg
!= dp
->rgvarg
) {
425 heap_free(new_dp
.rgvarg
);
426 if(new_dp
.cNamedArgs
> 1)
427 heap_free(new_dp
.rgdispidNamedArgs
);
433 ERR("type %d\n", prop
->type
);
439 static HRESULT
prop_get(jsdisp_t
*This
, dispex_prop_t
*prop
, DISPPARAMS
*dp
,
440 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
446 if(prop
->u
.p
->flags
& PROPF_METHOD
) {
448 hres
= create_builtin_function(This
->ctx
, prop
->u
.p
->invoke
, prop
->u
.p
->name
, NULL
,
449 prop
->u
.p
->flags
, NULL
, &obj
);
453 prop
->type
= PROP_VARIANT
;
454 var_set_jsdisp(&prop
->u
.var
, obj
);
455 hres
= VariantCopy(retv
, &prop
->u
.var
);
459 set_jsdisp(&vthis
, This
);
460 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, DISPATCH_PROPERTYGET
, dp
, retv
, ei
);
461 vdisp_release(&vthis
);
465 hres
= prop_get(This
->prototype
, This
->prototype
->props
+prop
->u
.ref
, dp
, retv
, ei
, caller
);
468 hres
= VariantCopy(retv
, &prop
->u
.var
);
471 ERR("type %d\n", prop
->type
);
476 TRACE("fail %08x\n", hres
);
480 TRACE("%s ret %s\n", debugstr_w(prop
->name
), debugstr_variant(retv
));
484 static HRESULT
prop_put(jsdisp_t
*This
, dispex_prop_t
*prop
, VARIANT
*val
,
485 jsexcept_t
*ei
, IServiceProvider
*caller
)
489 if(prop
->flags
& PROPF_CONST
)
494 if(!(prop
->flags
& PROPF_METHOD
)) {
495 DISPPARAMS dp
= {val
, NULL
, 1, 0};
498 set_jsdisp(&vthis
, This
);
499 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, ei
);
500 vdisp_release(&vthis
);
504 prop
->type
= PROP_VARIANT
;
505 prop
->flags
= PROPF_ENUM
;
506 V_VT(&prop
->u
.var
) = VT_EMPTY
;
509 VariantClear(&prop
->u
.var
);
512 ERR("type %d\n", prop
->type
);
516 hres
= VariantCopy(&prop
->u
.var
, val
);
520 if(This
->builtin_info
->on_put
)
521 This
->builtin_info
->on_put(This
, prop
->name
);
523 TRACE("%s = %s\n", debugstr_w(prop
->name
), debugstr_variant(val
));
527 static HRESULT
fill_protrefs(jsdisp_t
*This
)
529 dispex_prop_t
*iter
, *prop
;
535 fill_protrefs(This
->prototype
);
537 for(iter
= This
->prototype
->props
; iter
< This
->prototype
->props
+This
->prototype
->prop_cnt
; iter
++) {
540 hres
= find_prop_name(This
, iter
->hash
, iter
->name
, &prop
);
543 if(!prop
|| prop
->type
==PROP_DELETED
) {
545 prop
->type
= PROP_PROTREF
;
547 prop
->u
.ref
= iter
- This
->prototype
->props
;
549 prop
= alloc_protref(This
, iter
->name
, iter
- This
->prototype
->props
);
551 return E_OUTOFMEMORY
;
559 static inline jsdisp_t
*impl_from_IDispatchEx(IDispatchEx
*iface
)
561 return CONTAINING_RECORD(iface
, jsdisp_t
, IDispatchEx_iface
);
564 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
566 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
568 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
569 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
570 *ppv
= &This
->IDispatchEx_iface
;
571 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
572 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
573 *ppv
= &This
->IDispatchEx_iface
;
574 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
575 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
576 *ppv
= &This
->IDispatchEx_iface
;
577 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
578 TRACE("(%p)->(IID_IDispatchJS %p)\n", This
, ppv
);
583 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
585 return E_NOINTERFACE
;
588 IUnknown_AddRef((IUnknown
*)*ppv
);
592 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
594 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
595 LONG ref
= InterlockedIncrement(&This
->ref
);
597 TRACE("(%p) ref=%d\n", This
, ref
);
602 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
604 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
605 LONG ref
= InterlockedDecrement(&This
->ref
);
607 TRACE("(%p) ref=%d\n", This
, ref
);
612 for(prop
= This
->props
; prop
< This
->props
+This
->prop_cnt
; prop
++) {
613 if(prop
->type
== PROP_VARIANT
)
614 VariantClear(&prop
->u
.var
);
615 heap_free(prop
->name
);
617 heap_free(This
->props
);
618 script_release(This
->ctx
);
620 jsdisp_release(This
->prototype
);
622 if(This
->builtin_info
->destructor
)
623 This
->builtin_info
->destructor(This
);
631 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
633 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
635 TRACE("(%p)->(%p)\n", This
, pctinfo
);
641 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
, LCID lcid
,
644 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
645 FIXME("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
649 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
650 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
,
653 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
657 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
660 for(i
=0; i
< cNames
; i
++) {
661 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[i
], 0, rgDispId
+i
);
669 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
670 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
671 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
673 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
675 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
676 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
678 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
,
679 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
682 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
684 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
686 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
688 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
)) {
689 FIXME("Unsupported grfdex %x\n", grfdex
);
693 return jsdisp_get_id(This
, bstrName
, grfdex
, pid
);
696 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
697 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
699 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
704 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
707 V_VT(pvarRes
) = VT_EMPTY
;
709 prop
= get_prop(This
, id
);
710 if(!prop
|| prop
->type
== PROP_DELETED
) {
711 TRACE("invalid id\n");
712 return DISP_E_MEMBERNOTFOUND
;
715 memset(&jsexcept
, 0, sizeof(jsexcept
));
718 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
719 wFlags
= DISPATCH_METHOD
;
721 case DISPATCH_METHOD
:
722 case DISPATCH_CONSTRUCT
:
723 hres
= invoke_prop_func(This
, This
, prop
, wFlags
, pdp
, pvarRes
, &jsexcept
, pspCaller
);
725 case DISPATCH_PROPERTYGET
:
726 hres
= prop_get(This
, prop
, pdp
, pvarRes
, &jsexcept
, pspCaller
);
728 case DISPATCH_PROPERTYPUT
: {
731 for(i
=0; i
< pdp
->cNamedArgs
; i
++) {
732 if(pdp
->rgdispidNamedArgs
[i
] == DISPID_PROPERTYPUT
)
736 if(i
== pdp
->cNamedArgs
) {
737 TRACE("no value to set\n");
738 return DISP_E_PARAMNOTOPTIONAL
;
741 hres
= prop_put(This
, prop
, pdp
->rgvarg
+i
, &jsexcept
, pspCaller
);
745 FIXME("Unimplemented flags %x\n", wFlags
);
755 static HRESULT
delete_prop(dispex_prop_t
*prop
)
757 if(prop
->type
== PROP_VARIANT
) {
758 VariantClear(&prop
->u
.var
);
759 prop
->type
= PROP_DELETED
;
764 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
766 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
770 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
772 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
773 FIXME("Unsupported grfdex %x\n", grfdex
);
775 hres
= find_prop_name(This
, string_hash(bstrName
), bstrName
, &prop
);
779 TRACE("not found\n");
783 return delete_prop(prop
);
786 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
788 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
791 TRACE("(%p)->(%x)\n", This
, id
);
793 prop
= get_prop(This
, id
);
795 WARN("invalid id\n");
796 return DISP_E_MEMBERNOTFOUND
;
799 return delete_prop(prop
);
802 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
804 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
805 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
809 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
811 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
814 TRACE("(%p)->(%x %p)\n", This
, id
, pbstrName
);
816 prop
= get_prop(This
, id
);
817 if(!prop
|| !prop
->name
|| prop
->type
== PROP_DELETED
)
818 return DISP_E_MEMBERNOTFOUND
;
820 *pbstrName
= SysAllocString(prop
->name
);
822 return E_OUTOFMEMORY
;
827 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
829 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
833 TRACE("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
835 if(id
== DISPID_STARTENUM
) {
836 hres
= fill_protrefs(This
);
841 if(id
+1>=0 && id
+1<This
->prop_cnt
) {
842 iter
= &This
->props
[id
+1];
844 *pid
= DISPID_STARTENUM
;
848 while(iter
< This
->props
+ This
->prop_cnt
) {
849 if(iter
->name
&& (get_flags(This
, iter
) & PROPF_ENUM
) && iter
->type
!=PROP_DELETED
) {
850 *pid
= prop_to_id(This
, iter
);
856 *pid
= DISPID_STARTENUM
;
860 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
862 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
863 FIXME("(%p)->(%p)\n", This
, ppunk
);
867 static IDispatchExVtbl DispatchExVtbl
= {
868 DispatchEx_QueryInterface
,
871 DispatchEx_GetTypeInfoCount
,
872 DispatchEx_GetTypeInfo
,
873 DispatchEx_GetIDsOfNames
,
875 DispatchEx_GetDispID
,
877 DispatchEx_DeleteMemberByName
,
878 DispatchEx_DeleteMemberByDispID
,
879 DispatchEx_GetMemberProperties
,
880 DispatchEx_GetMemberName
,
881 DispatchEx_GetNextDispID
,
882 DispatchEx_GetNameSpaceParent
885 jsdisp_t
*as_jsdisp(IDispatch
*disp
)
887 assert(disp
->lpVtbl
== (IDispatchVtbl
*)&DispatchExVtbl
);
888 return impl_from_IDispatchEx((IDispatchEx
*)disp
);
891 jsdisp_t
*to_jsdisp(IDispatch
*disp
)
893 return disp
->lpVtbl
== (IDispatchVtbl
*)&DispatchExVtbl
? impl_from_IDispatchEx((IDispatchEx
*)disp
) : NULL
;
896 HRESULT
init_dispex(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
)
898 TRACE("%p (%p)\n", dispex
, prototype
);
900 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
902 dispex
->builtin_info
= builtin_info
;
904 dispex
->props
= heap_alloc_zero(sizeof(dispex_prop_t
)*(dispex
->buf_size
=4));
906 return E_OUTOFMEMORY
;
908 dispex
->prototype
= prototype
;
910 jsdisp_addref(prototype
);
912 dispex
->prop_cnt
= 1;
913 if(builtin_info
->value_prop
.invoke
) {
914 dispex
->props
[0].type
= PROP_BUILTIN
;
915 dispex
->props
[0].u
.p
= &builtin_info
->value_prop
;
917 dispex
->props
[0].type
= PROP_DELETED
;
926 static const builtin_info_t dispex_info
= {
934 HRESULT
create_dispex(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
, jsdisp_t
**dispex
)
939 ret
= heap_alloc_zero(sizeof(jsdisp_t
));
941 return E_OUTOFMEMORY
;
943 hres
= init_dispex(ret
, ctx
, builtin_info
? builtin_info
: &dispex_info
, prototype
);
951 HRESULT
init_dispex_from_constr(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*constr
)
953 jsdisp_t
*prot
= NULL
;
957 static const WCHAR constructorW
[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
958 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t','y','p','e',0};
960 hres
= find_prop_name_prot(constr
, string_hash(prototypeW
), prototypeW
, &prop
);
961 if(SUCCEEDED(hres
) && prop
&& prop
->type
!=PROP_DELETED
) {
965 V_VT(&var
) = VT_EMPTY
;
966 memset(&jsexcept
, 0, sizeof(jsexcept
));
967 hres
= prop_get(constr
, prop
, NULL
, &var
, &jsexcept
, NULL
/*FIXME*/);
969 ERR("Could not get prototype\n");
973 if(V_VT(&var
) == VT_DISPATCH
)
974 prot
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&var
));
978 hres
= init_dispex(dispex
, ctx
, builtin_info
, prot
);
981 jsdisp_release(prot
);
985 hres
= ensure_prop_name(dispex
, constructorW
, FALSE
, 0, &prop
);
986 if(SUCCEEDED(hres
)) {
990 var_set_jsdisp(&var
, constr
);
991 memset(&jsexcept
, 0, sizeof(jsexcept
));
992 hres
= prop_put(dispex
, prop
, &var
, &jsexcept
, NULL
/*FIXME*/);
995 jsdisp_release(dispex
);
1000 jsdisp_t
*iface_to_jsdisp(IUnknown
*iface
)
1005 hres
= IUnknown_QueryInterface(iface
, &IID_IDispatchJS
, (void**)&ret
);
1012 static void ensure_retval_type(VARIANT
*v
)
1021 HRESULT
jsdisp_get_id(jsdisp_t
*jsdisp
, const WCHAR
*name
, DWORD flags
, DISPID
*id
)
1023 dispex_prop_t
*prop
;
1026 if(flags
& fdexNameEnsure
)
1027 hres
= ensure_prop_name(jsdisp
, name
, TRUE
, PROPF_ENUM
, &prop
);
1029 hres
= find_prop_name_prot(jsdisp
, string_hash(name
), name
, &prop
);
1033 if(prop
&& prop
->type
!=PROP_DELETED
) {
1034 *id
= prop_to_id(jsdisp
, prop
);
1038 TRACE("not found %s\n", debugstr_w(name
));
1039 return DISP_E_UNKNOWNNAME
;
1042 HRESULT
jsdisp_call_value(jsdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
1047 set_jsdisp(&vdisp
, jsthis
);
1048 hres
= jsthis
->builtin_info
->value_prop
.invoke(jsthis
->ctx
, &vdisp
, flags
, dp
, retv
, ei
);
1049 vdisp_release(&vdisp
);
1053 HRESULT
jsdisp_call(jsdisp_t
*disp
, DISPID id
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
1055 dispex_prop_t
*prop
;
1057 memset(ei
, 0, sizeof(*ei
));
1059 V_VT(retv
) = VT_EMPTY
;
1061 prop
= get_prop(disp
, id
);
1063 return DISP_E_MEMBERNOTFOUND
;
1065 return invoke_prop_func(disp
, disp
, prop
, flags
, dp
, retv
, ei
, NULL
);
1068 HRESULT
jsdisp_call_name(jsdisp_t
*disp
, const WCHAR
*name
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
1071 dispex_prop_t
*prop
;
1074 hres
= find_prop_name_prot(disp
, string_hash(name
), name
, &prop
);
1078 memset(ei
, 0, sizeof(*ei
));
1080 V_VT(retv
) = VT_EMPTY
;
1082 return invoke_prop_func(disp
, disp
, prop
, flags
, dp
, retv
, ei
, NULL
);
1085 HRESULT
disp_call(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
1088 IDispatchEx
*dispex
;
1091 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1093 if(flags
& DISPATCH_PROPERTYPUT
) {
1094 FIXME("disp_call(propput) on builtin object\n");
1098 hres
= jsdisp_call(jsdisp
, id
, flags
, dp
, retv
, ei
);
1099 jsdisp_release(jsdisp
);
1103 memset(ei
, 0, sizeof(*ei
));
1104 if(retv
&& arg_cnt(dp
))
1105 flags
|= DISPATCH_PROPERTYGET
;
1108 V_VT(retv
) = VT_EMPTY
;
1109 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1110 if(SUCCEEDED(hres
)) {
1111 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, dp
, retv
, &ei
->ei
, &ctx
->jscaller
->IServiceProvider_iface
);
1112 IDispatchEx_Release(dispex
);
1116 if(flags
== DISPATCH_CONSTRUCT
) {
1117 WARN("IDispatch cannot be constructor\n");
1118 return DISP_E_MEMBERNOTFOUND
;
1121 TRACE("using IDispatch\n");
1122 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, dp
, retv
, &ei
->ei
, &err
);
1129 ensure_retval_type(retv
);
1133 HRESULT
jsdisp_propput_name(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*val
, jsexcept_t
*ei
)
1135 dispex_prop_t
*prop
;
1138 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_ENUM
, &prop
);
1142 return prop_put(obj
, prop
, val
, ei
, NULL
);
1145 HRESULT
jsdisp_propput_const(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*val
)
1147 dispex_prop_t
*prop
;
1150 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_ENUM
|PROPF_CONST
, &prop
);
1154 return VariantCopy(&prop
->u
.var
, val
);
1157 HRESULT
jsdisp_propput_idx(jsdisp_t
*obj
, DWORD idx
, VARIANT
*val
, jsexcept_t
*ei
)
1161 static const WCHAR formatW
[] = {'%','d',0};
1163 sprintfW(buf
, formatW
, idx
);
1164 return jsdisp_propput_name(obj
, buf
, val
, ei
);
1167 HRESULT
disp_propput(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
)
1172 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1174 dispex_prop_t
*prop
;
1176 prop
= get_prop(jsdisp
, id
);
1178 hres
= prop_put(jsdisp
, prop
, val
, ei
, NULL
);
1180 hres
= DISP_E_MEMBERNOTFOUND
;
1182 jsdisp_release(jsdisp
);
1184 DISPID dispid
= DISPID_PROPERTYPUT
;
1185 DISPPARAMS dp
= {val
, &dispid
, 1, 1};
1186 IDispatchEx
*dispex
;
1188 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1189 if(SUCCEEDED(hres
)) {
1190 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
->ei
,
1191 &ctx
->jscaller
->IServiceProvider_iface
);
1192 IDispatchEx_Release(dispex
);
1196 TRACE("using IDispatch\n");
1197 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
->ei
, &err
);
1204 HRESULT
jsdisp_propget_name(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*var
, jsexcept_t
*ei
)
1206 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1207 dispex_prop_t
*prop
;
1210 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1214 V_VT(var
) = VT_EMPTY
;
1215 if(!prop
|| prop
->type
==PROP_DELETED
)
1218 return prop_get(obj
, prop
, &dp
, var
, ei
, NULL
);
1221 HRESULT
jsdisp_get_idx(jsdisp_t
*obj
, DWORD idx
, VARIANT
*var
, jsexcept_t
*ei
)
1224 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1225 dispex_prop_t
*prop
;
1228 static const WCHAR formatW
[] = {'%','d',0};
1230 sprintfW(name
, formatW
, idx
);
1232 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1236 V_VT(var
) = VT_EMPTY
;
1237 if(!prop
|| prop
->type
==PROP_DELETED
)
1238 return DISP_E_UNKNOWNNAME
;
1240 return prop_get(obj
, prop
, &dp
, var
, ei
, NULL
);
1243 HRESULT
jsdisp_propget(jsdisp_t
*jsdisp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
)
1245 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1246 dispex_prop_t
*prop
;
1248 prop
= get_prop(jsdisp
, id
);
1250 return DISP_E_MEMBERNOTFOUND
;
1252 V_VT(val
) = VT_EMPTY
;
1253 return prop_get(jsdisp
, prop
, &dp
, val
, ei
, NULL
);
1256 HRESULT
disp_propget(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
)
1258 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1259 IDispatchEx
*dispex
;
1263 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1265 hres
= jsdisp_propget(jsdisp
, id
, val
, ei
);
1266 jsdisp_release(jsdisp
);
1270 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1271 if(SUCCEEDED(hres
)) {
1272 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, val
, &ei
->ei
,
1273 &ctx
->jscaller
->IServiceProvider_iface
);
1274 IDispatchEx_Release(dispex
);
1278 TRACE("using IDispatch\n");
1279 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, val
, &ei
->ei
, &err
);
1284 ensure_retval_type(val
);
1288 HRESULT
jsdisp_delete_idx(jsdisp_t
*obj
, DWORD idx
)
1290 static const WCHAR formatW
[] = {'%','d',0};
1292 dispex_prop_t
*prop
;
1295 sprintfW(buf
, formatW
, idx
);
1297 hres
= find_prop_name(obj
, string_hash(buf
), buf
, &prop
);
1298 if(FAILED(hres
) || !prop
)
1301 return delete_prop(prop
);
1304 VARIANT_BOOL
jsdisp_is_own_prop(jsdisp_t
*obj
, BSTR name
)
1306 dispex_prop_t
*prop
;
1309 hres
= find_prop_name(obj
, string_hash(name
), name
, &prop
);
1311 return VARIANT_FALSE
;
1313 return VARIANT_FALSE
;
1315 return prop
->type
==PROP_VARIANT
? VARIANT_TRUE
: VARIANT_FALSE
;