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
21 #include "wine/unicode.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
27 * This IID is used to get jsdisp_t objecto from interface.
28 * We might consider using private insteface instead.
30 static const IID IID_IDispatchJS
=
31 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
33 #define FDEX_VERSION_MASK 0xf0000000
34 #define GOLDEN_RATIO 0x9E3779B9U
43 struct _dispex_prop_t
{
51 const builtin_prop_t
*p
;
59 static inline DISPID
prop_to_id(jsdisp_t
*This
, dispex_prop_t
*prop
)
61 return prop
- This
->props
;
64 static inline dispex_prop_t
*get_prop(jsdisp_t
*This
, DISPID id
)
66 if(id
< 0 || id
>= This
->prop_cnt
|| This
->props
[id
].type
== PROP_DELETED
)
69 return This
->props
+id
;
72 static DWORD
get_flags(jsdisp_t
*This
, dispex_prop_t
*prop
)
74 if(prop
->type
== PROP_PROTREF
) {
75 dispex_prop_t
*parent
= get_prop(This
->prototype
, prop
->u
.ref
);
77 prop
->type
= PROP_DELETED
;
81 return get_flags(This
->prototype
, parent
);
87 static const builtin_prop_t
*find_builtin_prop(jsdisp_t
*This
, const WCHAR
*name
)
89 int min
= 0, max
, i
, r
;
91 max
= This
->builtin_info
->props_cnt
-1;
95 r
= strcmpW(name
, This
->builtin_info
->props
[i
].name
);
97 return This
->builtin_info
->props
+ i
;
108 static inline unsigned string_hash(const WCHAR
*name
)
112 h
= (h
>>(sizeof(unsigned)*8-4)) ^ (h
<<4) ^ tolowerW(*name
);
116 static inline unsigned get_props_idx(jsdisp_t
*This
, unsigned hash
)
118 return (hash
*GOLDEN_RATIO
) & (This
->buf_size
-1);
121 static inline HRESULT
resize_props(jsdisp_t
*This
)
123 dispex_prop_t
*props
;
126 if(This
->buf_size
!= This
->prop_cnt
)
129 props
= heap_realloc(This
->props
, sizeof(dispex_prop_t
)*This
->buf_size
*2);
131 return E_OUTOFMEMORY
;
135 for(i
=0; i
<This
->buf_size
; i
++) {
136 This
->props
[i
].bucket_head
= 0;
137 This
->props
[i
].bucket_next
= 0;
140 for(i
=1; i
<This
->prop_cnt
; i
++) {
141 props
= This
->props
+i
;
143 bucket
= get_props_idx(This
, props
->hash
);
144 props
->bucket_next
= This
->props
[bucket
].bucket_head
;
145 This
->props
[bucket
].bucket_head
= i
;
151 static inline dispex_prop_t
* alloc_prop(jsdisp_t
*This
, const WCHAR
*name
, prop_type_t type
, DWORD flags
)
156 if(FAILED(resize_props(This
)))
159 prop
= &This
->props
[This
->prop_cnt
];
160 prop
->name
= heap_strdupW(name
);
165 prop
->hash
= string_hash(name
);
167 bucket
= get_props_idx(This
, prop
->hash
);
168 prop
->bucket_next
= This
->props
[bucket
].bucket_head
;
169 This
->props
[bucket
].bucket_head
= This
->prop_cnt
++;
173 static dispex_prop_t
*alloc_protref(jsdisp_t
*This
, const WCHAR
*name
, DWORD ref
)
177 ret
= alloc_prop(This
, name
, PROP_PROTREF
, 0);
185 static HRESULT
find_prop_name(jsdisp_t
*This
, unsigned hash
, const WCHAR
*name
, dispex_prop_t
**ret
)
187 const builtin_prop_t
*builtin
;
188 unsigned bucket
, pos
, prev
= 0;
191 bucket
= get_props_idx(This
, hash
);
192 pos
= This
->props
[bucket
].bucket_head
;
194 if(!strcmpW(name
, This
->props
[pos
].name
)) {
196 This
->props
[prev
].bucket_next
= This
->props
[pos
].bucket_next
;
197 This
->props
[pos
].bucket_next
= This
->props
[bucket
].bucket_head
;
198 This
->props
[bucket
].bucket_head
= pos
;
201 *ret
= &This
->props
[pos
];
206 pos
= This
->props
[pos
].bucket_next
;
209 builtin
= find_builtin_prop(This
, name
);
211 prop
= alloc_prop(This
, name
, PROP_BUILTIN
, builtin
->flags
);
213 return E_OUTOFMEMORY
;
224 static HRESULT
find_prop_name_prot(jsdisp_t
*This
, unsigned hash
, const WCHAR
*name
, dispex_prop_t
**ret
)
226 dispex_prop_t
*prop
, *del
=NULL
;
229 hres
= find_prop_name(This
, hash
, name
, &prop
);
232 if(prop
&& prop
->type
==PROP_DELETED
) {
239 if(This
->prototype
) {
240 hres
= find_prop_name_prot(This
->prototype
, hash
, name
, &prop
);
245 del
->type
= PROP_PROTREF
;
247 del
->u
.ref
= prop
- This
->prototype
->props
;
250 prop
= alloc_protref(This
, prop
->name
, prop
- This
->prototype
->props
);
252 return E_OUTOFMEMORY
;
264 static HRESULT
ensure_prop_name(jsdisp_t
*This
, const WCHAR
*name
, BOOL search_prot
, DWORD create_flags
, dispex_prop_t
**ret
)
270 hres
= find_prop_name_prot(This
, string_hash(name
), name
, &prop
);
272 hres
= find_prop_name(This
, string_hash(name
), name
, &prop
);
273 if(SUCCEEDED(hres
) && (!prop
|| prop
->type
==PROP_DELETED
)) {
274 TRACE("creating prop %s\n", debugstr_w(name
));
277 prop
->type
= PROP_VARIANT
;
278 prop
->flags
= create_flags
;
280 prop
= alloc_prop(This
, name
, PROP_VARIANT
, create_flags
);
282 return E_OUTOFMEMORY
;
285 VariantInit(&prop
->u
.var
);
292 static HRESULT
set_this(DISPPARAMS
*dp
, DISPPARAMS
*olddp
, IDispatch
*jsthis
)
297 static DISPID this_id
= DISPID_THIS
;
301 for(i
= 0; i
< dp
->cNamedArgs
; i
++) {
302 if(dp
->rgdispidNamedArgs
[i
] == DISPID_THIS
)
306 oldargs
= dp
->rgvarg
;
307 dp
->rgvarg
= heap_alloc((dp
->cArgs
+1) * sizeof(VARIANTARG
));
309 return E_OUTOFMEMORY
;
310 memcpy(dp
->rgvarg
+1, oldargs
, dp
->cArgs
*sizeof(VARIANTARG
));
311 V_VT(dp
->rgvarg
) = VT_DISPATCH
;
312 V_DISPATCH(dp
->rgvarg
) = jsthis
;
316 DISPID
*old
= dp
->rgdispidNamedArgs
;
317 dp
->rgdispidNamedArgs
= heap_alloc((dp
->cNamedArgs
+1)*sizeof(DISPID
));
318 if(!dp
->rgdispidNamedArgs
) {
319 heap_free(dp
->rgvarg
);
320 return E_OUTOFMEMORY
;
323 memcpy(dp
->rgdispidNamedArgs
+1, old
, dp
->cNamedArgs
*sizeof(DISPID
));
324 dp
->rgdispidNamedArgs
[0] = DISPID_THIS
;
327 dp
->rgdispidNamedArgs
= &this_id
;
334 static HRESULT
invoke_prop_func(jsdisp_t
*This
, jsdisp_t
*jsthis
, dispex_prop_t
*prop
, WORD flags
,
335 DISPPARAMS
*dp
, VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
343 if(flags
== DISPATCH_CONSTRUCT
&& (prop
->flags
& PROPF_METHOD
)) {
344 WARN("%s is not a constructor\n", debugstr_w(prop
->name
));
348 set_jsdisp(&vthis
, jsthis
);
349 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, flags
, dp
, retv
, ei
, caller
);
350 vdisp_release(&vthis
);
354 return invoke_prop_func(This
->prototype
, jsthis
, This
->prototype
->props
+prop
->u
.ref
, flags
, dp
, retv
, ei
, caller
);
358 if(V_VT(&prop
->u
.var
) != VT_DISPATCH
) {
359 FIXME("invoke vt %d\n", V_VT(&prop
->u
.var
));
363 TRACE("call %s %p\n", debugstr_w(prop
->name
), V_DISPATCH(&prop
->u
.var
));
365 hres
= set_this(&new_dp
, dp
, to_disp(jsthis
));
369 hres
= disp_call(This
->ctx
, V_DISPATCH(&prop
->u
.var
), DISPID_VALUE
, flags
, &new_dp
, retv
, ei
, caller
);
371 if(new_dp
.rgvarg
!= dp
->rgvarg
) {
372 heap_free(new_dp
.rgvarg
);
373 if(new_dp
.cNamedArgs
> 1)
374 heap_free(new_dp
.rgdispidNamedArgs
);
380 ERR("type %d\n", prop
->type
);
386 static HRESULT
prop_get(jsdisp_t
*This
, dispex_prop_t
*prop
, DISPPARAMS
*dp
,
387 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
393 if(prop
->u
.p
->flags
& PROPF_METHOD
) {
395 hres
= create_builtin_function(This
->ctx
, prop
->u
.p
->invoke
, prop
->u
.p
->name
, NULL
,
396 prop
->u
.p
->flags
, NULL
, &obj
);
400 prop
->type
= PROP_VARIANT
;
401 var_set_jsdisp(&prop
->u
.var
, obj
);
402 hres
= VariantCopy(retv
, &prop
->u
.var
);
406 set_jsdisp(&vthis
, This
);
407 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, DISPATCH_PROPERTYGET
, dp
, retv
, ei
, caller
);
408 vdisp_release(&vthis
);
412 hres
= prop_get(This
->prototype
, This
->prototype
->props
+prop
->u
.ref
, dp
, retv
, ei
, caller
);
415 hres
= VariantCopy(retv
, &prop
->u
.var
);
418 ERR("type %d\n", prop
->type
);
423 TRACE("fail %08x\n", hres
);
427 TRACE("%s ret %s\n", debugstr_w(prop
->name
), debugstr_variant(retv
));
431 static HRESULT
prop_put(jsdisp_t
*This
, dispex_prop_t
*prop
, VARIANT
*val
,
432 jsexcept_t
*ei
, IServiceProvider
*caller
)
436 if(prop
->flags
& PROPF_CONST
)
441 if(!(prop
->flags
& PROPF_METHOD
)) {
442 DISPPARAMS dp
= {val
, NULL
, 1, 0};
445 set_jsdisp(&vthis
, This
);
446 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, ei
, caller
);
447 vdisp_release(&vthis
);
451 prop
->type
= PROP_VARIANT
;
452 prop
->flags
= PROPF_ENUM
;
453 V_VT(&prop
->u
.var
) = VT_EMPTY
;
456 VariantClear(&prop
->u
.var
);
459 ERR("type %d\n", prop
->type
);
463 hres
= VariantCopy(&prop
->u
.var
, val
);
467 if(This
->builtin_info
->on_put
)
468 This
->builtin_info
->on_put(This
, prop
->name
);
470 TRACE("%s = %s\n", debugstr_w(prop
->name
), debugstr_variant(val
));
474 static HRESULT
fill_protrefs(jsdisp_t
*This
)
476 dispex_prop_t
*iter
, *prop
;
482 fill_protrefs(This
->prototype
);
484 for(iter
= This
->prototype
->props
; iter
< This
->prototype
->props
+This
->prototype
->prop_cnt
; iter
++) {
487 hres
= find_prop_name(This
, iter
->hash
, iter
->name
, &prop
);
490 if(!prop
|| prop
->type
==PROP_DELETED
) {
492 prop
->type
= PROP_PROTREF
;
494 prop
->u
.ref
= iter
- This
->prototype
->props
;
496 prop
= alloc_protref(This
, iter
->name
, iter
- This
->prototype
->props
);
498 return E_OUTOFMEMORY
;
506 static inline jsdisp_t
*impl_from_IDispatchEx(IDispatchEx
*iface
)
508 return CONTAINING_RECORD(iface
, jsdisp_t
, IDispatchEx_iface
);
511 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
513 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
515 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
516 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
517 *ppv
= &This
->IDispatchEx_iface
;
518 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
519 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
520 *ppv
= &This
->IDispatchEx_iface
;
521 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
522 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
523 *ppv
= &This
->IDispatchEx_iface
;
524 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
525 TRACE("(%p)->(IID_IDispatchJS %p)\n", This
, ppv
);
530 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
532 return E_NOINTERFACE
;
535 IUnknown_AddRef((IUnknown
*)*ppv
);
539 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
541 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
542 LONG ref
= InterlockedIncrement(&This
->ref
);
544 TRACE("(%p) ref=%d\n", This
, ref
);
549 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
551 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
552 LONG ref
= InterlockedDecrement(&This
->ref
);
554 TRACE("(%p) ref=%d\n", This
, ref
);
559 for(prop
= This
->props
; prop
< This
->props
+This
->prop_cnt
; prop
++) {
560 if(prop
->type
== PROP_VARIANT
)
561 VariantClear(&prop
->u
.var
);
562 heap_free(prop
->name
);
564 heap_free(This
->props
);
565 script_release(This
->ctx
);
567 jsdisp_release(This
->prototype
);
569 if(This
->builtin_info
->destructor
)
570 This
->builtin_info
->destructor(This
);
578 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
580 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
582 TRACE("(%p)->(%p)\n", This
, pctinfo
);
588 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
, LCID lcid
,
591 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
592 FIXME("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
596 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
597 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
,
600 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
604 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
607 for(i
=0; i
< cNames
; i
++) {
608 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[i
], 0, rgDispId
+i
);
616 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
617 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
618 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
620 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
622 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
623 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
625 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
,
626 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
629 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
631 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
633 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
635 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
)) {
636 FIXME("Unsupported grfdex %x\n", grfdex
);
640 return jsdisp_get_id(This
, bstrName
, grfdex
, pid
);
643 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
644 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
646 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
651 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
654 V_VT(pvarRes
) = VT_EMPTY
;
656 prop
= get_prop(This
, id
);
657 if(!prop
|| prop
->type
== PROP_DELETED
) {
658 TRACE("invalid id\n");
659 return DISP_E_MEMBERNOTFOUND
;
662 memset(&jsexcept
, 0, sizeof(jsexcept
));
665 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
666 wFlags
= DISPATCH_METHOD
;
667 case DISPATCH_METHOD
:
668 case DISPATCH_CONSTRUCT
:
669 hres
= invoke_prop_func(This
, This
, prop
, wFlags
, pdp
, pvarRes
, &jsexcept
, pspCaller
);
671 case DISPATCH_PROPERTYGET
:
672 hres
= prop_get(This
, prop
, pdp
, pvarRes
, &jsexcept
, pspCaller
);
674 case DISPATCH_PROPERTYPUT
: {
677 for(i
=0; i
< pdp
->cNamedArgs
; i
++) {
678 if(pdp
->rgdispidNamedArgs
[i
] == DISPID_PROPERTYPUT
)
682 if(i
== pdp
->cNamedArgs
) {
683 TRACE("no value to set\n");
684 return DISP_E_PARAMNOTOPTIONAL
;
687 hres
= prop_put(This
, prop
, pdp
->rgvarg
+i
, &jsexcept
, pspCaller
);
691 FIXME("Unimplemented flags %x\n", wFlags
);
701 static HRESULT
delete_prop(dispex_prop_t
*prop
)
703 if(prop
->type
== PROP_VARIANT
) {
704 VariantClear(&prop
->u
.var
);
705 prop
->type
= PROP_DELETED
;
710 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
712 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
716 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
718 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
719 FIXME("Unsupported grfdex %x\n", grfdex
);
721 hres
= find_prop_name(This
, string_hash(bstrName
), bstrName
, &prop
);
725 TRACE("not found\n");
729 return delete_prop(prop
);
732 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
734 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
737 TRACE("(%p)->(%x)\n", This
, id
);
739 prop
= get_prop(This
, id
);
741 WARN("invalid id\n");
742 return DISP_E_MEMBERNOTFOUND
;
745 return delete_prop(prop
);
748 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
750 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
751 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
755 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
757 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
760 TRACE("(%p)->(%x %p)\n", This
, id
, pbstrName
);
762 prop
= get_prop(This
, id
);
763 if(!prop
|| !prop
->name
|| prop
->type
== PROP_DELETED
)
764 return DISP_E_MEMBERNOTFOUND
;
766 *pbstrName
= SysAllocString(prop
->name
);
768 return E_OUTOFMEMORY
;
773 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
775 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
779 TRACE("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
781 if(id
== DISPID_STARTENUM
) {
782 hres
= fill_protrefs(This
);
787 if(id
+1>=0 && id
+1<This
->prop_cnt
) {
788 iter
= &This
->props
[id
+1];
790 *pid
= DISPID_STARTENUM
;
794 while(iter
< This
->props
+ This
->prop_cnt
) {
795 if(iter
->name
&& (get_flags(This
, iter
) & PROPF_ENUM
) && iter
->type
!=PROP_DELETED
) {
796 *pid
= prop_to_id(This
, iter
);
802 *pid
= DISPID_STARTENUM
;
806 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
808 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
809 FIXME("(%p)->(%p)\n", This
, ppunk
);
813 static IDispatchExVtbl DispatchExVtbl
= {
814 DispatchEx_QueryInterface
,
817 DispatchEx_GetTypeInfoCount
,
818 DispatchEx_GetTypeInfo
,
819 DispatchEx_GetIDsOfNames
,
821 DispatchEx_GetDispID
,
823 DispatchEx_DeleteMemberByName
,
824 DispatchEx_DeleteMemberByDispID
,
825 DispatchEx_GetMemberProperties
,
826 DispatchEx_GetMemberName
,
827 DispatchEx_GetNextDispID
,
828 DispatchEx_GetNameSpaceParent
831 HRESULT
init_dispex(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
)
833 TRACE("%p (%p)\n", dispex
, prototype
);
835 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
837 dispex
->builtin_info
= builtin_info
;
839 dispex
->props
= heap_alloc_zero(sizeof(dispex_prop_t
)*(dispex
->buf_size
=4));
841 return E_OUTOFMEMORY
;
843 dispex
->prototype
= prototype
;
845 jsdisp_addref(prototype
);
847 dispex
->prop_cnt
= 1;
848 if(builtin_info
->value_prop
.invoke
) {
849 dispex
->props
[0].type
= PROP_BUILTIN
;
850 dispex
->props
[0].u
.p
= &builtin_info
->value_prop
;
852 dispex
->props
[0].type
= PROP_DELETED
;
861 static const builtin_info_t dispex_info
= {
869 HRESULT
create_dispex(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
, jsdisp_t
**dispex
)
874 ret
= heap_alloc_zero(sizeof(jsdisp_t
));
876 return E_OUTOFMEMORY
;
878 hres
= init_dispex(ret
, ctx
, builtin_info
? builtin_info
: &dispex_info
, prototype
);
886 HRESULT
init_dispex_from_constr(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*constr
)
888 jsdisp_t
*prot
= NULL
;
892 static const WCHAR constructorW
[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
893 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t','y','p','e',0};
895 hres
= find_prop_name_prot(constr
, string_hash(prototypeW
), prototypeW
, &prop
);
896 if(SUCCEEDED(hres
) && prop
&& prop
->type
!=PROP_DELETED
) {
900 V_VT(&var
) = VT_EMPTY
;
901 memset(&jsexcept
, 0, sizeof(jsexcept
));
902 hres
= prop_get(constr
, prop
, NULL
, &var
, &jsexcept
, NULL
/*FIXME*/);
904 ERR("Could not get prototype\n");
908 if(V_VT(&var
) == VT_DISPATCH
)
909 prot
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&var
));
913 hres
= init_dispex(dispex
, ctx
, builtin_info
, prot
);
916 jsdisp_release(prot
);
920 hres
= ensure_prop_name(dispex
, constructorW
, FALSE
, 0, &prop
);
921 if(SUCCEEDED(hres
)) {
925 var_set_jsdisp(&var
, constr
);
926 memset(&jsexcept
, 0, sizeof(jsexcept
));
927 hres
= prop_put(dispex
, prop
, &var
, &jsexcept
, NULL
/*FIXME*/);
930 jsdisp_release(dispex
);
935 jsdisp_t
*iface_to_jsdisp(IUnknown
*iface
)
940 hres
= IUnknown_QueryInterface(iface
, &IID_IDispatchJS
, (void**)&ret
);
947 HRESULT
jsdisp_get_id(jsdisp_t
*jsdisp
, const WCHAR
*name
, DWORD flags
, DISPID
*id
)
952 if(flags
& fdexNameEnsure
)
953 hres
= ensure_prop_name(jsdisp
, name
, TRUE
, PROPF_ENUM
, &prop
);
955 hres
= find_prop_name_prot(jsdisp
, string_hash(name
), name
, &prop
);
959 if(prop
&& prop
->type
!=PROP_DELETED
) {
960 *id
= prop_to_id(jsdisp
, prop
);
964 TRACE("not found %s\n", debugstr_w(name
));
965 return DISP_E_UNKNOWNNAME
;
968 HRESULT
jsdisp_call_value(jsdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
969 jsexcept_t
*ei
, IServiceProvider
*caller
)
974 set_jsdisp(&vdisp
, jsthis
);
975 hres
= jsthis
->builtin_info
->value_prop
.invoke(jsthis
->ctx
, &vdisp
, flags
, dp
, retv
, ei
, caller
);
976 vdisp_release(&vdisp
);
980 HRESULT
jsdisp_call(jsdisp_t
*disp
, DISPID id
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
981 jsexcept_t
*ei
, IServiceProvider
*caller
)
985 memset(ei
, 0, sizeof(*ei
));
987 V_VT(retv
) = VT_EMPTY
;
989 prop
= get_prop(disp
, id
);
991 return DISP_E_MEMBERNOTFOUND
;
993 return invoke_prop_func(disp
, disp
, prop
, flags
, dp
, retv
, ei
, caller
);
996 HRESULT
jsdisp_call_name(jsdisp_t
*disp
, const WCHAR
*name
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
997 jsexcept_t
*ei
, IServiceProvider
*caller
)
1002 hres
= find_prop_name_prot(disp
, string_hash(name
), name
, &prop
);
1006 memset(ei
, 0, sizeof(*ei
));
1008 V_VT(retv
) = VT_EMPTY
;
1010 return invoke_prop_func(disp
, disp
, prop
, flags
, dp
, retv
, ei
, caller
);
1013 HRESULT
disp_call(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
1014 jsexcept_t
*ei
, IServiceProvider
*caller
)
1017 IDispatchEx
*dispex
;
1020 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1022 hres
= jsdisp_call(jsdisp
, id
, flags
, dp
, retv
, ei
, caller
);
1023 jsdisp_release(jsdisp
);
1027 memset(ei
, 0, sizeof(*ei
));
1028 if(retv
&& arg_cnt(dp
))
1029 flags
|= DISPATCH_PROPERTYGET
;
1032 V_VT(retv
) = VT_EMPTY
;
1033 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1037 if(flags
== DISPATCH_CONSTRUCT
) {
1038 WARN("IDispatch cannot be constructor\n");
1039 return DISP_E_MEMBERNOTFOUND
;
1042 TRACE("using IDispatch\n");
1043 return IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, dp
, retv
, &ei
->ei
, &err
);
1046 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, dp
, retv
, &ei
->ei
, caller
);
1047 IDispatchEx_Release(dispex
);
1052 HRESULT
jsdisp_propput_name(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1054 dispex_prop_t
*prop
;
1057 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_ENUM
, &prop
);
1061 return prop_put(obj
, prop
, val
, ei
, caller
);
1064 HRESULT
jsdisp_propput_const(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*val
)
1066 dispex_prop_t
*prop
;
1069 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_ENUM
|PROPF_CONST
, &prop
);
1073 return VariantCopy(&prop
->u
.var
, val
);
1076 HRESULT
jsdisp_propput_idx(jsdisp_t
*obj
, DWORD idx
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1080 static const WCHAR formatW
[] = {'%','d',0};
1082 sprintfW(buf
, formatW
, idx
);
1083 return jsdisp_propput_name(obj
, buf
, val
, ei
, caller
);
1086 HRESULT
disp_propput(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1091 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1093 dispex_prop_t
*prop
;
1095 prop
= get_prop(jsdisp
, id
);
1097 hres
= prop_put(jsdisp
, prop
, val
, ei
, caller
);
1099 hres
= DISP_E_MEMBERNOTFOUND
;
1101 jsdisp_release(jsdisp
);
1103 DISPID dispid
= DISPID_PROPERTYPUT
;
1104 DISPPARAMS dp
= {val
, &dispid
, 1, 1};
1105 IDispatchEx
*dispex
;
1107 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1108 if(SUCCEEDED(hres
)) {
1109 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
->ei
, caller
);
1110 IDispatchEx_Release(dispex
);
1114 TRACE("using IDispatch\n");
1115 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
->ei
, &err
);
1122 HRESULT
jsdisp_propget_name(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*var
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1124 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1125 dispex_prop_t
*prop
;
1128 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1132 V_VT(var
) = VT_EMPTY
;
1133 if(!prop
|| prop
->type
==PROP_DELETED
)
1136 return prop_get(obj
, prop
, &dp
, var
, ei
, caller
);
1139 HRESULT
jsdisp_get_idx(jsdisp_t
*obj
, DWORD idx
, VARIANT
*var
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1142 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1143 dispex_prop_t
*prop
;
1146 static const WCHAR formatW
[] = {'%','d',0};
1148 sprintfW(name
, formatW
, idx
);
1150 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1154 V_VT(var
) = VT_EMPTY
;
1155 if(!prop
|| prop
->type
==PROP_DELETED
)
1156 return DISP_E_UNKNOWNNAME
;
1158 return prop_get(obj
, prop
, &dp
, var
, ei
, caller
);
1161 HRESULT
jsdisp_propget(jsdisp_t
*jsdisp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1163 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1164 dispex_prop_t
*prop
;
1166 prop
= get_prop(jsdisp
, id
);
1168 return DISP_E_MEMBERNOTFOUND
;
1170 V_VT(val
) = VT_EMPTY
;
1171 return prop_get(jsdisp
, prop
, &dp
, val
, ei
, caller
);
1174 HRESULT
disp_propget(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1176 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1177 IDispatchEx
*dispex
;
1181 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1183 hres
= jsdisp_propget(jsdisp
, id
, val
, ei
, caller
);
1184 jsdisp_release(jsdisp
);
1188 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1192 TRACE("using IDispatch\n");
1193 return IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, val
, &ei
->ei
, &err
);
1196 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, val
, &ei
->ei
, caller
);
1197 IDispatchEx_Release(dispex
);
1202 HRESULT
jsdisp_delete_idx(jsdisp_t
*obj
, DWORD idx
)
1204 static const WCHAR formatW
[] = {'%','d',0};
1206 dispex_prop_t
*prop
;
1209 sprintfW(buf
, formatW
, idx
);
1211 hres
= find_prop_name(obj
, string_hash(buf
), buf
, &prop
);
1212 if(FAILED(hres
) || !prop
)
1215 return delete_prop(prop
);
1218 VARIANT_BOOL
jsdisp_is_own_prop(jsdisp_t
*obj
, BSTR name
)
1220 dispex_prop_t
*prop
;
1223 hres
= find_prop_name(obj
, string_hash(name
), name
, &prop
);
1225 return VARIANT_FALSE
;
1227 return VARIANT_FALSE
;
1229 return prop
->type
==PROP_VARIANT
? VARIANT_TRUE
: VARIANT_FALSE
;