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
&& !need_conversion
; i
++) {
346 switch(V_VT(get_arg(dp
, i
))) {
349 need_conversion
= TRUE
;
358 ret
->rgvarg
= heap_alloc(ret
->cArgs
* sizeof(VARIANT
));
360 return E_OUTOFMEMORY
;
365 for(i
= 0; i
< ret
->cArgs
; i
++) {
385 static HRESULT
invoke_prop_func(jsdisp_t
*This
, jsdisp_t
*jsthis
, dispex_prop_t
*prop
, WORD flags
,
386 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
396 if(flags
== DISPATCH_CONSTRUCT
&& (prop
->flags
& PROPF_METHOD
)) {
397 WARN("%s is not a constructor\n", debugstr_w(prop
->name
));
401 hres
= convert_params(dp
, buf
, ¶ms
);
405 set_jsdisp(&vthis
, jsthis
);
406 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, flags
, ¶ms
, retv
, ei
);
407 vdisp_release(&vthis
);
408 if(params
.rgvarg
!= buf
&& params
.rgvarg
!= dp
->rgvarg
)
409 heap_free(params
.rgvarg
);
413 return invoke_prop_func(This
->prototype
, jsthis
, This
->prototype
->props
+prop
->u
.ref
, flags
, dp
, retv
, ei
, caller
);
417 if(V_VT(&prop
->u
.var
) != VT_DISPATCH
) {
418 FIXME("invoke vt %d\n", V_VT(&prop
->u
.var
));
422 TRACE("call %s %p\n", debugstr_w(prop
->name
), V_DISPATCH(&prop
->u
.var
));
424 hres
= set_this(&new_dp
, dp
, to_disp(jsthis
));
428 hres
= disp_call(This
->ctx
, V_DISPATCH(&prop
->u
.var
), DISPID_VALUE
, flags
, &new_dp
, retv
, ei
);
430 if(new_dp
.rgvarg
!= dp
->rgvarg
) {
431 heap_free(new_dp
.rgvarg
);
432 if(new_dp
.cNamedArgs
> 1)
433 heap_free(new_dp
.rgdispidNamedArgs
);
439 ERR("type %d\n", prop
->type
);
445 static HRESULT
prop_get(jsdisp_t
*This
, dispex_prop_t
*prop
, DISPPARAMS
*dp
,
446 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
452 if(prop
->u
.p
->flags
& PROPF_METHOD
) {
454 hres
= create_builtin_function(This
->ctx
, prop
->u
.p
->invoke
, prop
->u
.p
->name
, NULL
,
455 prop
->u
.p
->flags
, NULL
, &obj
);
459 prop
->type
= PROP_VARIANT
;
460 var_set_jsdisp(&prop
->u
.var
, obj
);
461 hres
= VariantCopy(retv
, &prop
->u
.var
);
465 set_jsdisp(&vthis
, This
);
466 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, DISPATCH_PROPERTYGET
, dp
, retv
, ei
);
467 vdisp_release(&vthis
);
471 hres
= prop_get(This
->prototype
, This
->prototype
->props
+prop
->u
.ref
, dp
, retv
, ei
, caller
);
474 hres
= VariantCopy(retv
, &prop
->u
.var
);
477 ERR("type %d\n", prop
->type
);
482 TRACE("fail %08x\n", hres
);
486 TRACE("%s ret %s\n", debugstr_w(prop
->name
), debugstr_variant(retv
));
490 static HRESULT
prop_put(jsdisp_t
*This
, dispex_prop_t
*prop
, VARIANT
*val
,
491 jsexcept_t
*ei
, IServiceProvider
*caller
)
495 if(prop
->flags
& PROPF_CONST
)
500 if(!(prop
->flags
& PROPF_METHOD
)) {
501 DISPPARAMS dp
= {val
, NULL
, 1, 0};
504 set_jsdisp(&vthis
, This
);
505 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, ei
);
506 vdisp_release(&vthis
);
510 prop
->type
= PROP_VARIANT
;
511 prop
->flags
= PROPF_ENUM
;
512 V_VT(&prop
->u
.var
) = VT_EMPTY
;
515 VariantClear(&prop
->u
.var
);
518 ERR("type %d\n", prop
->type
);
522 hres
= VariantCopy(&prop
->u
.var
, val
);
526 if(This
->builtin_info
->on_put
)
527 This
->builtin_info
->on_put(This
, prop
->name
);
529 TRACE("%s = %s\n", debugstr_w(prop
->name
), debugstr_variant(val
));
533 static HRESULT
fill_protrefs(jsdisp_t
*This
)
535 dispex_prop_t
*iter
, *prop
;
541 fill_protrefs(This
->prototype
);
543 for(iter
= This
->prototype
->props
; iter
< This
->prototype
->props
+This
->prototype
->prop_cnt
; iter
++) {
546 hres
= find_prop_name(This
, iter
->hash
, iter
->name
, &prop
);
549 if(!prop
|| prop
->type
==PROP_DELETED
) {
551 prop
->type
= PROP_PROTREF
;
553 prop
->u
.ref
= iter
- This
->prototype
->props
;
555 prop
= alloc_protref(This
, iter
->name
, iter
- This
->prototype
->props
);
557 return E_OUTOFMEMORY
;
565 static inline jsdisp_t
*impl_from_IDispatchEx(IDispatchEx
*iface
)
567 return CONTAINING_RECORD(iface
, jsdisp_t
, IDispatchEx_iface
);
570 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
572 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
574 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
575 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
576 *ppv
= &This
->IDispatchEx_iface
;
577 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
578 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
579 *ppv
= &This
->IDispatchEx_iface
;
580 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
581 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
582 *ppv
= &This
->IDispatchEx_iface
;
583 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
584 TRACE("(%p)->(IID_IDispatchJS %p)\n", This
, ppv
);
589 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
591 return E_NOINTERFACE
;
594 IUnknown_AddRef((IUnknown
*)*ppv
);
598 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
600 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
601 LONG ref
= InterlockedIncrement(&This
->ref
);
603 TRACE("(%p) ref=%d\n", This
, ref
);
608 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
610 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
611 LONG ref
= InterlockedDecrement(&This
->ref
);
613 TRACE("(%p) ref=%d\n", This
, ref
);
618 for(prop
= This
->props
; prop
< This
->props
+This
->prop_cnt
; prop
++) {
619 if(prop
->type
== PROP_VARIANT
)
620 VariantClear(&prop
->u
.var
);
621 heap_free(prop
->name
);
623 heap_free(This
->props
);
624 script_release(This
->ctx
);
626 jsdisp_release(This
->prototype
);
628 if(This
->builtin_info
->destructor
)
629 This
->builtin_info
->destructor(This
);
637 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
639 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
641 TRACE("(%p)->(%p)\n", This
, pctinfo
);
647 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
, LCID lcid
,
650 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
651 FIXME("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
655 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
656 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
,
659 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
663 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
666 for(i
=0; i
< cNames
; i
++) {
667 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[i
], 0, rgDispId
+i
);
675 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
676 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
677 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
679 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
681 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
682 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
684 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
,
685 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
688 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
690 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
692 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
694 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
)) {
695 FIXME("Unsupported grfdex %x\n", grfdex
);
699 return jsdisp_get_id(This
, bstrName
, grfdex
, pid
);
702 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
703 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
705 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
710 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
713 V_VT(pvarRes
) = VT_EMPTY
;
715 prop
= get_prop(This
, id
);
716 if(!prop
|| prop
->type
== PROP_DELETED
) {
717 TRACE("invalid id\n");
718 return DISP_E_MEMBERNOTFOUND
;
721 memset(&jsexcept
, 0, sizeof(jsexcept
));
724 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
725 wFlags
= DISPATCH_METHOD
;
727 case DISPATCH_METHOD
:
728 case DISPATCH_CONSTRUCT
:
729 hres
= invoke_prop_func(This
, This
, prop
, wFlags
, pdp
, pvarRes
, &jsexcept
, pspCaller
);
731 case DISPATCH_PROPERTYGET
:
732 hres
= prop_get(This
, prop
, pdp
, pvarRes
, &jsexcept
, pspCaller
);
734 case DISPATCH_PROPERTYPUT
: {
737 for(i
=0; i
< pdp
->cNamedArgs
; i
++) {
738 if(pdp
->rgdispidNamedArgs
[i
] == DISPID_PROPERTYPUT
)
742 if(i
== pdp
->cNamedArgs
) {
743 TRACE("no value to set\n");
744 return DISP_E_PARAMNOTOPTIONAL
;
747 hres
= prop_put(This
, prop
, pdp
->rgvarg
+i
, &jsexcept
, pspCaller
);
751 FIXME("Unimplemented flags %x\n", wFlags
);
761 static HRESULT
delete_prop(dispex_prop_t
*prop
)
763 if(prop
->type
== PROP_VARIANT
) {
764 VariantClear(&prop
->u
.var
);
765 prop
->type
= PROP_DELETED
;
770 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
772 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
776 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
778 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
779 FIXME("Unsupported grfdex %x\n", grfdex
);
781 hres
= find_prop_name(This
, string_hash(bstrName
), bstrName
, &prop
);
785 TRACE("not found\n");
789 return delete_prop(prop
);
792 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
794 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
797 TRACE("(%p)->(%x)\n", This
, id
);
799 prop
= get_prop(This
, id
);
801 WARN("invalid id\n");
802 return DISP_E_MEMBERNOTFOUND
;
805 return delete_prop(prop
);
808 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
810 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
811 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
815 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
817 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
820 TRACE("(%p)->(%x %p)\n", This
, id
, pbstrName
);
822 prop
= get_prop(This
, id
);
823 if(!prop
|| !prop
->name
|| prop
->type
== PROP_DELETED
)
824 return DISP_E_MEMBERNOTFOUND
;
826 *pbstrName
= SysAllocString(prop
->name
);
828 return E_OUTOFMEMORY
;
833 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
835 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
839 TRACE("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
841 if(id
== DISPID_STARTENUM
) {
842 hres
= fill_protrefs(This
);
847 if(id
+1>=0 && id
+1<This
->prop_cnt
) {
848 iter
= &This
->props
[id
+1];
850 *pid
= DISPID_STARTENUM
;
854 while(iter
< This
->props
+ This
->prop_cnt
) {
855 if(iter
->name
&& (get_flags(This
, iter
) & PROPF_ENUM
) && iter
->type
!=PROP_DELETED
) {
856 *pid
= prop_to_id(This
, iter
);
862 *pid
= DISPID_STARTENUM
;
866 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
868 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
869 FIXME("(%p)->(%p)\n", This
, ppunk
);
873 static IDispatchExVtbl DispatchExVtbl
= {
874 DispatchEx_QueryInterface
,
877 DispatchEx_GetTypeInfoCount
,
878 DispatchEx_GetTypeInfo
,
879 DispatchEx_GetIDsOfNames
,
881 DispatchEx_GetDispID
,
883 DispatchEx_DeleteMemberByName
,
884 DispatchEx_DeleteMemberByDispID
,
885 DispatchEx_GetMemberProperties
,
886 DispatchEx_GetMemberName
,
887 DispatchEx_GetNextDispID
,
888 DispatchEx_GetNameSpaceParent
891 jsdisp_t
*as_jsdisp(IDispatch
*disp
)
893 assert(disp
->lpVtbl
== (IDispatchVtbl
*)&DispatchExVtbl
);
894 return impl_from_IDispatchEx((IDispatchEx
*)disp
);
897 jsdisp_t
*to_jsdisp(IDispatch
*disp
)
899 return disp
->lpVtbl
== (IDispatchVtbl
*)&DispatchExVtbl
? impl_from_IDispatchEx((IDispatchEx
*)disp
) : NULL
;
902 HRESULT
init_dispex(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
)
904 TRACE("%p (%p)\n", dispex
, prototype
);
906 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
908 dispex
->builtin_info
= builtin_info
;
910 dispex
->props
= heap_alloc_zero(sizeof(dispex_prop_t
)*(dispex
->buf_size
=4));
912 return E_OUTOFMEMORY
;
914 dispex
->prototype
= prototype
;
916 jsdisp_addref(prototype
);
918 dispex
->prop_cnt
= 1;
919 if(builtin_info
->value_prop
.invoke
) {
920 dispex
->props
[0].type
= PROP_BUILTIN
;
921 dispex
->props
[0].u
.p
= &builtin_info
->value_prop
;
923 dispex
->props
[0].type
= PROP_DELETED
;
932 static const builtin_info_t dispex_info
= {
940 HRESULT
create_dispex(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
, jsdisp_t
**dispex
)
945 ret
= heap_alloc_zero(sizeof(jsdisp_t
));
947 return E_OUTOFMEMORY
;
949 hres
= init_dispex(ret
, ctx
, builtin_info
? builtin_info
: &dispex_info
, prototype
);
957 HRESULT
init_dispex_from_constr(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*constr
)
959 jsdisp_t
*prot
= NULL
;
963 static const WCHAR constructorW
[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
964 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t','y','p','e',0};
966 hres
= find_prop_name_prot(constr
, string_hash(prototypeW
), prototypeW
, &prop
);
967 if(SUCCEEDED(hres
) && prop
&& prop
->type
!=PROP_DELETED
) {
971 V_VT(&var
) = VT_EMPTY
;
972 memset(&jsexcept
, 0, sizeof(jsexcept
));
973 hres
= prop_get(constr
, prop
, NULL
, &var
, &jsexcept
, NULL
/*FIXME*/);
975 ERR("Could not get prototype\n");
979 if(V_VT(&var
) == VT_DISPATCH
)
980 prot
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&var
));
984 hres
= init_dispex(dispex
, ctx
, builtin_info
, prot
);
987 jsdisp_release(prot
);
991 hres
= ensure_prop_name(dispex
, constructorW
, FALSE
, 0, &prop
);
992 if(SUCCEEDED(hres
)) {
996 var_set_jsdisp(&var
, constr
);
997 memset(&jsexcept
, 0, sizeof(jsexcept
));
998 hres
= prop_put(dispex
, prop
, &var
, &jsexcept
, NULL
/*FIXME*/);
1001 jsdisp_release(dispex
);
1006 jsdisp_t
*iface_to_jsdisp(IUnknown
*iface
)
1011 hres
= IUnknown_QueryInterface(iface
, &IID_IDispatchJS
, (void**)&ret
);
1018 static void ensure_retval_type(VARIANT
*v
)
1031 HRESULT
jsdisp_get_id(jsdisp_t
*jsdisp
, const WCHAR
*name
, DWORD flags
, DISPID
*id
)
1033 dispex_prop_t
*prop
;
1036 if(flags
& fdexNameEnsure
)
1037 hres
= ensure_prop_name(jsdisp
, name
, TRUE
, PROPF_ENUM
, &prop
);
1039 hres
= find_prop_name_prot(jsdisp
, string_hash(name
), name
, &prop
);
1043 if(prop
&& prop
->type
!=PROP_DELETED
) {
1044 *id
= prop_to_id(jsdisp
, prop
);
1048 TRACE("not found %s\n", debugstr_w(name
));
1049 return DISP_E_UNKNOWNNAME
;
1052 HRESULT
jsdisp_call_value(jsdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
1057 set_jsdisp(&vdisp
, jsthis
);
1058 hres
= jsthis
->builtin_info
->value_prop
.invoke(jsthis
->ctx
, &vdisp
, flags
, dp
, retv
, ei
);
1059 vdisp_release(&vdisp
);
1063 HRESULT
jsdisp_call(jsdisp_t
*disp
, DISPID id
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
1065 dispex_prop_t
*prop
;
1067 memset(ei
, 0, sizeof(*ei
));
1069 V_VT(retv
) = VT_EMPTY
;
1071 prop
= get_prop(disp
, id
);
1073 return DISP_E_MEMBERNOTFOUND
;
1075 return invoke_prop_func(disp
, disp
, prop
, flags
, dp
, retv
, ei
, NULL
);
1078 HRESULT
jsdisp_call_name(jsdisp_t
*disp
, const WCHAR
*name
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
1081 dispex_prop_t
*prop
;
1084 hres
= find_prop_name_prot(disp
, string_hash(name
), name
, &prop
);
1088 memset(ei
, 0, sizeof(*ei
));
1090 V_VT(retv
) = VT_EMPTY
;
1092 return invoke_prop_func(disp
, disp
, prop
, flags
, dp
, retv
, ei
, NULL
);
1095 HRESULT
disp_call(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
)
1098 IDispatchEx
*dispex
;
1101 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1103 if(flags
& DISPATCH_PROPERTYPUT
) {
1104 FIXME("disp_call(propput) on builtin object\n");
1108 hres
= jsdisp_call(jsdisp
, id
, flags
, dp
, retv
, ei
);
1109 jsdisp_release(jsdisp
);
1113 memset(ei
, 0, sizeof(*ei
));
1114 if(retv
&& arg_cnt(dp
))
1115 flags
|= DISPATCH_PROPERTYGET
;
1118 V_VT(retv
) = VT_EMPTY
;
1119 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1120 if(SUCCEEDED(hres
)) {
1121 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, dp
, retv
, &ei
->ei
, &ctx
->jscaller
->IServiceProvider_iface
);
1122 IDispatchEx_Release(dispex
);
1126 if(flags
== DISPATCH_CONSTRUCT
) {
1127 WARN("IDispatch cannot be constructor\n");
1128 return DISP_E_MEMBERNOTFOUND
;
1131 TRACE("using IDispatch\n");
1132 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, dp
, retv
, &ei
->ei
, &err
);
1139 ensure_retval_type(retv
);
1143 HRESULT
jsdisp_propput_name(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*val
, jsexcept_t
*ei
)
1145 dispex_prop_t
*prop
;
1148 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_ENUM
, &prop
);
1152 return prop_put(obj
, prop
, val
, ei
, NULL
);
1155 HRESULT
jsdisp_propput_const(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*val
)
1157 dispex_prop_t
*prop
;
1160 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_ENUM
|PROPF_CONST
, &prop
);
1164 return VariantCopy(&prop
->u
.var
, val
);
1167 HRESULT
jsdisp_propput_idx(jsdisp_t
*obj
, DWORD idx
, VARIANT
*val
, jsexcept_t
*ei
)
1171 static const WCHAR formatW
[] = {'%','d',0};
1173 sprintfW(buf
, formatW
, idx
);
1174 return jsdisp_propput_name(obj
, buf
, val
, ei
);
1177 HRESULT
disp_propput(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
)
1182 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1184 dispex_prop_t
*prop
;
1186 prop
= get_prop(jsdisp
, id
);
1188 hres
= prop_put(jsdisp
, prop
, val
, ei
, NULL
);
1190 hres
= DISP_E_MEMBERNOTFOUND
;
1192 jsdisp_release(jsdisp
);
1194 DISPID dispid
= DISPID_PROPERTYPUT
;
1195 DISPPARAMS dp
= {val
, &dispid
, 1, 1};
1196 IDispatchEx
*dispex
;
1198 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1199 if(SUCCEEDED(hres
)) {
1200 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
->ei
,
1201 &ctx
->jscaller
->IServiceProvider_iface
);
1202 IDispatchEx_Release(dispex
);
1206 TRACE("using IDispatch\n");
1207 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
->ei
, &err
);
1214 HRESULT
jsdisp_propget_name(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*var
, jsexcept_t
*ei
)
1216 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1217 dispex_prop_t
*prop
;
1220 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1224 V_VT(var
) = VT_EMPTY
;
1225 if(!prop
|| prop
->type
==PROP_DELETED
)
1228 return prop_get(obj
, prop
, &dp
, var
, ei
, NULL
);
1231 HRESULT
jsdisp_get_idx(jsdisp_t
*obj
, DWORD idx
, VARIANT
*var
, jsexcept_t
*ei
)
1234 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1235 dispex_prop_t
*prop
;
1238 static const WCHAR formatW
[] = {'%','d',0};
1240 sprintfW(name
, formatW
, idx
);
1242 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1246 V_VT(var
) = VT_EMPTY
;
1247 if(!prop
|| prop
->type
==PROP_DELETED
)
1248 return DISP_E_UNKNOWNNAME
;
1250 return prop_get(obj
, prop
, &dp
, var
, ei
, NULL
);
1253 HRESULT
jsdisp_propget(jsdisp_t
*jsdisp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
)
1255 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1256 dispex_prop_t
*prop
;
1258 prop
= get_prop(jsdisp
, id
);
1260 return DISP_E_MEMBERNOTFOUND
;
1262 V_VT(val
) = VT_EMPTY
;
1263 return prop_get(jsdisp
, prop
, &dp
, val
, ei
, NULL
);
1266 HRESULT
disp_propget(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
)
1268 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1269 IDispatchEx
*dispex
;
1273 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1275 hres
= jsdisp_propget(jsdisp
, id
, val
, ei
);
1276 jsdisp_release(jsdisp
);
1280 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1281 if(SUCCEEDED(hres
)) {
1282 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, val
, &ei
->ei
,
1283 &ctx
->jscaller
->IServiceProvider_iface
);
1284 IDispatchEx_Release(dispex
);
1288 TRACE("using IDispatch\n");
1289 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, val
, &ei
->ei
, &err
);
1294 ensure_retval_type(val
);
1298 HRESULT
jsdisp_delete_idx(jsdisp_t
*obj
, DWORD idx
)
1300 static const WCHAR formatW
[] = {'%','d',0};
1302 dispex_prop_t
*prop
;
1305 sprintfW(buf
, formatW
, idx
);
1307 hres
= find_prop_name(obj
, string_hash(buf
), buf
, &prop
);
1308 if(FAILED(hres
) || !prop
)
1311 return delete_prop(prop
);
1314 VARIANT_BOOL
jsdisp_is_own_prop(jsdisp_t
*obj
, BSTR name
)
1316 dispex_prop_t
*prop
;
1319 hres
= find_prop_name(obj
, string_hash(name
), name
, &prop
);
1321 return VARIANT_FALSE
;
1323 return VARIANT_FALSE
;
1325 return prop
->type
==PROP_VARIANT
? VARIANT_TRUE
: VARIANT_FALSE
;