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
;
668 case DISPATCH_METHOD
:
669 case DISPATCH_CONSTRUCT
:
670 hres
= invoke_prop_func(This
, This
, prop
, wFlags
, pdp
, pvarRes
, &jsexcept
, pspCaller
);
672 case DISPATCH_PROPERTYGET
:
673 hres
= prop_get(This
, prop
, pdp
, pvarRes
, &jsexcept
, pspCaller
);
675 case DISPATCH_PROPERTYPUT
: {
678 for(i
=0; i
< pdp
->cNamedArgs
; i
++) {
679 if(pdp
->rgdispidNamedArgs
[i
] == DISPID_PROPERTYPUT
)
683 if(i
== pdp
->cNamedArgs
) {
684 TRACE("no value to set\n");
685 return DISP_E_PARAMNOTOPTIONAL
;
688 hres
= prop_put(This
, prop
, pdp
->rgvarg
+i
, &jsexcept
, pspCaller
);
692 FIXME("Unimplemented flags %x\n", wFlags
);
702 static HRESULT
delete_prop(dispex_prop_t
*prop
)
704 if(prop
->type
== PROP_VARIANT
) {
705 VariantClear(&prop
->u
.var
);
706 prop
->type
= PROP_DELETED
;
711 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
713 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
717 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
719 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
720 FIXME("Unsupported grfdex %x\n", grfdex
);
722 hres
= find_prop_name(This
, string_hash(bstrName
), bstrName
, &prop
);
726 TRACE("not found\n");
730 return delete_prop(prop
);
733 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
735 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
738 TRACE("(%p)->(%x)\n", This
, id
);
740 prop
= get_prop(This
, id
);
742 WARN("invalid id\n");
743 return DISP_E_MEMBERNOTFOUND
;
746 return delete_prop(prop
);
749 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
751 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
752 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
756 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
758 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
761 TRACE("(%p)->(%x %p)\n", This
, id
, pbstrName
);
763 prop
= get_prop(This
, id
);
764 if(!prop
|| !prop
->name
|| prop
->type
== PROP_DELETED
)
765 return DISP_E_MEMBERNOTFOUND
;
767 *pbstrName
= SysAllocString(prop
->name
);
769 return E_OUTOFMEMORY
;
774 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
776 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
780 TRACE("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
782 if(id
== DISPID_STARTENUM
) {
783 hres
= fill_protrefs(This
);
788 if(id
+1>=0 && id
+1<This
->prop_cnt
) {
789 iter
= &This
->props
[id
+1];
791 *pid
= DISPID_STARTENUM
;
795 while(iter
< This
->props
+ This
->prop_cnt
) {
796 if(iter
->name
&& (get_flags(This
, iter
) & PROPF_ENUM
) && iter
->type
!=PROP_DELETED
) {
797 *pid
= prop_to_id(This
, iter
);
803 *pid
= DISPID_STARTENUM
;
807 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
809 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
810 FIXME("(%p)->(%p)\n", This
, ppunk
);
814 static IDispatchExVtbl DispatchExVtbl
= {
815 DispatchEx_QueryInterface
,
818 DispatchEx_GetTypeInfoCount
,
819 DispatchEx_GetTypeInfo
,
820 DispatchEx_GetIDsOfNames
,
822 DispatchEx_GetDispID
,
824 DispatchEx_DeleteMemberByName
,
825 DispatchEx_DeleteMemberByDispID
,
826 DispatchEx_GetMemberProperties
,
827 DispatchEx_GetMemberName
,
828 DispatchEx_GetNextDispID
,
829 DispatchEx_GetNameSpaceParent
832 HRESULT
init_dispex(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
)
834 TRACE("%p (%p)\n", dispex
, prototype
);
836 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
838 dispex
->builtin_info
= builtin_info
;
840 dispex
->props
= heap_alloc_zero(sizeof(dispex_prop_t
)*(dispex
->buf_size
=4));
842 return E_OUTOFMEMORY
;
844 dispex
->prototype
= prototype
;
846 jsdisp_addref(prototype
);
848 dispex
->prop_cnt
= 1;
849 if(builtin_info
->value_prop
.invoke
) {
850 dispex
->props
[0].type
= PROP_BUILTIN
;
851 dispex
->props
[0].u
.p
= &builtin_info
->value_prop
;
853 dispex
->props
[0].type
= PROP_DELETED
;
862 static const builtin_info_t dispex_info
= {
870 HRESULT
create_dispex(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
, jsdisp_t
**dispex
)
875 ret
= heap_alloc_zero(sizeof(jsdisp_t
));
877 return E_OUTOFMEMORY
;
879 hres
= init_dispex(ret
, ctx
, builtin_info
? builtin_info
: &dispex_info
, prototype
);
887 HRESULT
init_dispex_from_constr(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*constr
)
889 jsdisp_t
*prot
= NULL
;
893 static const WCHAR constructorW
[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
894 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t','y','p','e',0};
896 hres
= find_prop_name_prot(constr
, string_hash(prototypeW
), prototypeW
, &prop
);
897 if(SUCCEEDED(hres
) && prop
&& prop
->type
!=PROP_DELETED
) {
901 V_VT(&var
) = VT_EMPTY
;
902 memset(&jsexcept
, 0, sizeof(jsexcept
));
903 hres
= prop_get(constr
, prop
, NULL
, &var
, &jsexcept
, NULL
/*FIXME*/);
905 ERR("Could not get prototype\n");
909 if(V_VT(&var
) == VT_DISPATCH
)
910 prot
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(&var
));
914 hres
= init_dispex(dispex
, ctx
, builtin_info
, prot
);
917 jsdisp_release(prot
);
921 hres
= ensure_prop_name(dispex
, constructorW
, FALSE
, 0, &prop
);
922 if(SUCCEEDED(hres
)) {
926 var_set_jsdisp(&var
, constr
);
927 memset(&jsexcept
, 0, sizeof(jsexcept
));
928 hres
= prop_put(dispex
, prop
, &var
, &jsexcept
, NULL
/*FIXME*/);
931 jsdisp_release(dispex
);
936 jsdisp_t
*iface_to_jsdisp(IUnknown
*iface
)
941 hres
= IUnknown_QueryInterface(iface
, &IID_IDispatchJS
, (void**)&ret
);
948 HRESULT
jsdisp_get_id(jsdisp_t
*jsdisp
, const WCHAR
*name
, DWORD flags
, DISPID
*id
)
953 if(flags
& fdexNameEnsure
)
954 hres
= ensure_prop_name(jsdisp
, name
, TRUE
, PROPF_ENUM
, &prop
);
956 hres
= find_prop_name_prot(jsdisp
, string_hash(name
), name
, &prop
);
960 if(prop
&& prop
->type
!=PROP_DELETED
) {
961 *id
= prop_to_id(jsdisp
, prop
);
965 TRACE("not found %s\n", debugstr_w(name
));
966 return DISP_E_UNKNOWNNAME
;
969 HRESULT
jsdisp_call_value(jsdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
970 jsexcept_t
*ei
, IServiceProvider
*caller
)
975 set_jsdisp(&vdisp
, jsthis
);
976 hres
= jsthis
->builtin_info
->value_prop
.invoke(jsthis
->ctx
, &vdisp
, flags
, dp
, retv
, ei
, caller
);
977 vdisp_release(&vdisp
);
981 HRESULT
jsdisp_call(jsdisp_t
*disp
, DISPID id
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
982 jsexcept_t
*ei
, IServiceProvider
*caller
)
986 memset(ei
, 0, sizeof(*ei
));
988 V_VT(retv
) = VT_EMPTY
;
990 prop
= get_prop(disp
, id
);
992 return DISP_E_MEMBERNOTFOUND
;
994 return invoke_prop_func(disp
, disp
, prop
, flags
, dp
, retv
, ei
, caller
);
997 HRESULT
jsdisp_call_name(jsdisp_t
*disp
, const WCHAR
*name
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
998 jsexcept_t
*ei
, IServiceProvider
*caller
)
1000 dispex_prop_t
*prop
;
1003 hres
= find_prop_name_prot(disp
, string_hash(name
), name
, &prop
);
1007 memset(ei
, 0, sizeof(*ei
));
1009 V_VT(retv
) = VT_EMPTY
;
1011 return invoke_prop_func(disp
, disp
, prop
, flags
, dp
, retv
, ei
, caller
);
1014 HRESULT
disp_call(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, WORD flags
, DISPPARAMS
*dp
, VARIANT
*retv
,
1015 jsexcept_t
*ei
, IServiceProvider
*caller
)
1018 IDispatchEx
*dispex
;
1021 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1023 hres
= jsdisp_call(jsdisp
, id
, flags
, dp
, retv
, ei
, caller
);
1024 jsdisp_release(jsdisp
);
1028 memset(ei
, 0, sizeof(*ei
));
1029 if(retv
&& arg_cnt(dp
))
1030 flags
|= DISPATCH_PROPERTYGET
;
1033 V_VT(retv
) = VT_EMPTY
;
1034 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1038 if(flags
== DISPATCH_CONSTRUCT
) {
1039 WARN("IDispatch cannot be constructor\n");
1040 return DISP_E_MEMBERNOTFOUND
;
1043 TRACE("using IDispatch\n");
1044 return IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, dp
, retv
, &ei
->ei
, &err
);
1047 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, dp
, retv
, &ei
->ei
, caller
);
1048 IDispatchEx_Release(dispex
);
1053 HRESULT
jsdisp_propput_name(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1055 dispex_prop_t
*prop
;
1058 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_ENUM
, &prop
);
1062 return prop_put(obj
, prop
, val
, ei
, caller
);
1065 HRESULT
jsdisp_propput_const(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*val
)
1067 dispex_prop_t
*prop
;
1070 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_ENUM
|PROPF_CONST
, &prop
);
1074 return VariantCopy(&prop
->u
.var
, val
);
1077 HRESULT
jsdisp_propput_idx(jsdisp_t
*obj
, DWORD idx
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1081 static const WCHAR formatW
[] = {'%','d',0};
1083 sprintfW(buf
, formatW
, idx
);
1084 return jsdisp_propput_name(obj
, buf
, val
, ei
, caller
);
1087 HRESULT
disp_propput(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1092 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1094 dispex_prop_t
*prop
;
1096 prop
= get_prop(jsdisp
, id
);
1098 hres
= prop_put(jsdisp
, prop
, val
, ei
, caller
);
1100 hres
= DISP_E_MEMBERNOTFOUND
;
1102 jsdisp_release(jsdisp
);
1104 DISPID dispid
= DISPID_PROPERTYPUT
;
1105 DISPPARAMS dp
= {val
, &dispid
, 1, 1};
1106 IDispatchEx
*dispex
;
1108 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1109 if(SUCCEEDED(hres
)) {
1110 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
->ei
, caller
);
1111 IDispatchEx_Release(dispex
);
1115 TRACE("using IDispatch\n");
1116 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, DISPATCH_PROPERTYPUT
, &dp
, NULL
, &ei
->ei
, &err
);
1123 HRESULT
jsdisp_propget_name(jsdisp_t
*obj
, const WCHAR
*name
, VARIANT
*var
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1125 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1126 dispex_prop_t
*prop
;
1129 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1133 V_VT(var
) = VT_EMPTY
;
1134 if(!prop
|| prop
->type
==PROP_DELETED
)
1137 return prop_get(obj
, prop
, &dp
, var
, ei
, caller
);
1140 HRESULT
jsdisp_get_idx(jsdisp_t
*obj
, DWORD idx
, VARIANT
*var
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1143 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1144 dispex_prop_t
*prop
;
1147 static const WCHAR formatW
[] = {'%','d',0};
1149 sprintfW(name
, formatW
, idx
);
1151 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1155 V_VT(var
) = VT_EMPTY
;
1156 if(!prop
|| prop
->type
==PROP_DELETED
)
1157 return DISP_E_UNKNOWNNAME
;
1159 return prop_get(obj
, prop
, &dp
, var
, ei
, caller
);
1162 HRESULT
jsdisp_propget(jsdisp_t
*jsdisp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1164 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1165 dispex_prop_t
*prop
;
1167 prop
= get_prop(jsdisp
, id
);
1169 return DISP_E_MEMBERNOTFOUND
;
1171 V_VT(val
) = VT_EMPTY
;
1172 return prop_get(jsdisp
, prop
, &dp
, val
, ei
, caller
);
1175 HRESULT
disp_propget(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, VARIANT
*val
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1177 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1178 IDispatchEx
*dispex
;
1182 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1184 hres
= jsdisp_propget(jsdisp
, id
, val
, ei
, caller
);
1185 jsdisp_release(jsdisp
);
1189 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1193 TRACE("using IDispatch\n");
1194 return IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, val
, &ei
->ei
, &err
);
1197 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, val
, &ei
->ei
, caller
);
1198 IDispatchEx_Release(dispex
);
1203 HRESULT
jsdisp_delete_idx(jsdisp_t
*obj
, DWORD idx
)
1205 static const WCHAR formatW
[] = {'%','d',0};
1207 dispex_prop_t
*prop
;
1210 sprintfW(buf
, formatW
, idx
);
1212 hres
= find_prop_name(obj
, string_hash(buf
), buf
, &prop
);
1213 if(FAILED(hres
) || !prop
)
1216 return delete_prop(prop
);
1219 VARIANT_BOOL
jsdisp_is_own_prop(jsdisp_t
*obj
, BSTR name
)
1221 dispex_prop_t
*prop
;
1224 hres
= find_prop_name(obj
, string_hash(name
), name
, &prop
);
1226 return VARIANT_FALSE
;
1228 return VARIANT_FALSE
;
1230 return prop
->type
==PROP_VARIANT
? VARIANT_TRUE
: VARIANT_FALSE
;