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
46 struct _dispex_prop_t
{
54 const builtin_prop_t
*p
;
63 static inline DISPID
prop_to_id(jsdisp_t
*This
, dispex_prop_t
*prop
)
65 return prop
- This
->props
;
68 static inline dispex_prop_t
*get_prop(jsdisp_t
*This
, DISPID id
)
70 if(id
< 0 || id
>= This
->prop_cnt
|| This
->props
[id
].type
== PROP_DELETED
)
73 return This
->props
+id
;
76 static DWORD
get_flags(jsdisp_t
*This
, dispex_prop_t
*prop
)
78 if(prop
->type
== PROP_PROTREF
) {
79 dispex_prop_t
*parent
= get_prop(This
->prototype
, prop
->u
.ref
);
81 prop
->type
= PROP_DELETED
;
85 return get_flags(This
->prototype
, parent
);
91 static const builtin_prop_t
*find_builtin_prop(jsdisp_t
*This
, const WCHAR
*name
)
93 int min
= 0, max
, i
, r
;
95 max
= This
->builtin_info
->props_cnt
-1;
99 r
= strcmpW(name
, This
->builtin_info
->props
[i
].name
);
101 return This
->builtin_info
->props
+ i
;
112 static inline unsigned string_hash(const WCHAR
*name
)
116 h
= (h
>>(sizeof(unsigned)*8-4)) ^ (h
<<4) ^ tolowerW(*name
);
120 static inline unsigned get_props_idx(jsdisp_t
*This
, unsigned hash
)
122 return (hash
*GOLDEN_RATIO
) & (This
->buf_size
-1);
125 static inline HRESULT
resize_props(jsdisp_t
*This
)
127 dispex_prop_t
*props
;
130 if(This
->buf_size
!= This
->prop_cnt
)
133 props
= heap_realloc(This
->props
, sizeof(dispex_prop_t
)*This
->buf_size
*2);
135 return E_OUTOFMEMORY
;
139 for(i
=0; i
<This
->buf_size
; i
++) {
140 This
->props
[i
].bucket_head
= 0;
141 This
->props
[i
].bucket_next
= 0;
144 for(i
=1; i
<This
->prop_cnt
; i
++) {
145 props
= This
->props
+i
;
147 bucket
= get_props_idx(This
, props
->hash
);
148 props
->bucket_next
= This
->props
[bucket
].bucket_head
;
149 This
->props
[bucket
].bucket_head
= i
;
155 static inline dispex_prop_t
* alloc_prop(jsdisp_t
*This
, const WCHAR
*name
, prop_type_t type
, DWORD flags
)
160 if(FAILED(resize_props(This
)))
163 prop
= &This
->props
[This
->prop_cnt
];
164 prop
->name
= heap_strdupW(name
);
169 prop
->hash
= string_hash(name
);
171 bucket
= get_props_idx(This
, prop
->hash
);
172 prop
->bucket_next
= This
->props
[bucket
].bucket_head
;
173 This
->props
[bucket
].bucket_head
= This
->prop_cnt
++;
177 static dispex_prop_t
*alloc_protref(jsdisp_t
*This
, const WCHAR
*name
, DWORD ref
)
181 ret
= alloc_prop(This
, name
, PROP_PROTREF
, 0);
189 static HRESULT
find_prop_name(jsdisp_t
*This
, unsigned hash
, const WCHAR
*name
, dispex_prop_t
**ret
)
191 const builtin_prop_t
*builtin
;
192 unsigned bucket
, pos
, prev
= 0;
195 bucket
= get_props_idx(This
, hash
);
196 pos
= This
->props
[bucket
].bucket_head
;
198 if(!strcmpW(name
, This
->props
[pos
].name
)) {
200 This
->props
[prev
].bucket_next
= This
->props
[pos
].bucket_next
;
201 This
->props
[pos
].bucket_next
= This
->props
[bucket
].bucket_head
;
202 This
->props
[bucket
].bucket_head
= pos
;
205 *ret
= &This
->props
[pos
];
210 pos
= This
->props
[pos
].bucket_next
;
213 builtin
= find_builtin_prop(This
, name
);
215 prop
= alloc_prop(This
, name
, PROP_BUILTIN
, builtin
->flags
);
217 return E_OUTOFMEMORY
;
224 if(This
->builtin_info
->idx_length
) {
228 for(ptr
= name
; isdigitW(*ptr
) && idx
< 0x10000; ptr
++)
229 idx
= idx
*10 + (*ptr
-'0');
230 if(!*ptr
&& idx
< This
->builtin_info
->idx_length(This
)) {
231 prop
= alloc_prop(This
, name
, PROP_IDX
, This
->builtin_info
->idx_put
? 0 : PROPF_CONST
);
233 return E_OUTOFMEMORY
;
245 static HRESULT
find_prop_name_prot(jsdisp_t
*This
, unsigned hash
, const WCHAR
*name
, dispex_prop_t
**ret
)
247 dispex_prop_t
*prop
, *del
=NULL
;
250 hres
= find_prop_name(This
, hash
, name
, &prop
);
253 if(prop
&& prop
->type
==PROP_DELETED
) {
260 if(This
->prototype
) {
261 hres
= find_prop_name_prot(This
->prototype
, hash
, name
, &prop
);
266 del
->type
= PROP_PROTREF
;
268 del
->u
.ref
= prop
- This
->prototype
->props
;
271 prop
= alloc_protref(This
, prop
->name
, prop
- This
->prototype
->props
);
273 return E_OUTOFMEMORY
;
285 static HRESULT
ensure_prop_name(jsdisp_t
*This
, const WCHAR
*name
, BOOL search_prot
, DWORD create_flags
, dispex_prop_t
**ret
)
291 hres
= find_prop_name_prot(This
, string_hash(name
), name
, &prop
);
293 hres
= find_prop_name(This
, string_hash(name
), name
, &prop
);
294 if(SUCCEEDED(hres
) && (!prop
|| prop
->type
== PROP_DELETED
)) {
295 TRACE("creating prop %s flags %x\n", debugstr_w(name
), create_flags
);
298 prop
->type
= PROP_JSVAL
;
299 prop
->flags
= create_flags
;
300 prop
->u
.val
= jsval_undefined();
302 prop
= alloc_prop(This
, name
, PROP_JSVAL
, create_flags
);
304 return E_OUTOFMEMORY
;
307 prop
->u
.val
= jsval_undefined();
314 static IDispatch
*get_this(DISPPARAMS
*dp
)
318 for(i
=0; i
< dp
->cNamedArgs
; i
++) {
319 if(dp
->rgdispidNamedArgs
[i
] == DISPID_THIS
) {
320 if(V_VT(dp
->rgvarg
+i
) == VT_DISPATCH
)
321 return V_DISPATCH(dp
->rgvarg
+i
);
323 WARN("This is not VT_DISPATCH\n");
328 TRACE("no this passed\n");
332 static HRESULT
convert_params(const DISPPARAMS
*dp
, jsval_t
*buf
, unsigned *argc
, jsval_t
**ret
)
339 cnt
= dp
->cArgs
- dp
->cNamedArgs
;
342 argv
= heap_alloc(cnt
* sizeof(*argv
));
344 return E_OUTOFMEMORY
;
349 for(i
= 0; i
< cnt
; i
++) {
350 hres
= variant_to_jsval(dp
->rgvarg
+dp
->cArgs
-i
-1, argv
+i
);
353 jsval_release(argv
[i
]);
365 static HRESULT
invoke_prop_func(jsdisp_t
*This
, IDispatch
*jsthis
, dispex_prop_t
*prop
, WORD flags
,
366 unsigned argc
, jsval_t
*argv
, jsval_t
*r
, IServiceProvider
*caller
)
372 if(flags
== DISPATCH_CONSTRUCT
&& (prop
->flags
& PROPF_METHOD
)) {
373 WARN("%s is not a constructor\n", debugstr_w(prop
->name
));
377 if(prop
->name
|| This
->builtin_info
->class != JSCLASS_FUNCTION
) {
381 set_disp(&vthis
, jsthis
);
383 set_jsdisp(&vthis
, This
);
384 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, flags
, argc
, argv
, r
);
385 vdisp_release(&vthis
);
387 /* Function object calls are special case */
388 hres
= Function_invoke(This
, jsthis
, flags
, argc
, argv
, r
);
393 return invoke_prop_func(This
->prototype
, jsthis
, This
->prototype
->props
+prop
->u
.ref
,
394 flags
, argc
, argv
, r
, caller
);
396 if(!is_object_instance(prop
->u
.val
)) {
397 FIXME("invoke %s\n", debugstr_jsval(prop
->u
.val
));
401 TRACE("call %s %p\n", debugstr_w(prop
->name
), get_object(prop
->u
.val
));
403 return disp_call_value(This
->ctx
, get_object(prop
->u
.val
), jsthis
, flags
, argc
, argv
, r
);
406 FIXME("Invoking PROP_IDX not yet supported\n");
416 static HRESULT
prop_get(jsdisp_t
*This
, dispex_prop_t
*prop
, DISPPARAMS
*dp
,
417 jsval_t
*r
, IServiceProvider
*caller
)
423 if(prop
->u
.p
->getter
) {
424 hres
= prop
->u
.p
->getter(This
->ctx
, This
, r
);
428 assert(prop
->u
.p
->invoke
!= NULL
);
429 hres
= create_builtin_function(This
->ctx
, prop
->u
.p
->invoke
, prop
->u
.p
->name
, NULL
,
430 prop
->u
.p
->flags
, NULL
, &obj
);
434 prop
->type
= PROP_JSVAL
;
435 prop
->u
.val
= jsval_obj(obj
);
442 hres
= prop_get(This
->prototype
, This
->prototype
->props
+prop
->u
.ref
, dp
, r
, caller
);
445 hres
= jsval_copy(prop
->u
.val
, r
);
448 hres
= This
->builtin_info
->idx_get(This
, prop
->u
.idx
, r
);
451 ERR("type %d\n", prop
->type
);
456 TRACE("fail %08x\n", hres
);
460 TRACE("%s ret %s\n", debugstr_w(prop
->name
), debugstr_jsval(*r
));
464 static HRESULT
prop_put(jsdisp_t
*This
, dispex_prop_t
*prop
, jsval_t val
, IServiceProvider
*caller
)
468 if(prop
->flags
& PROPF_CONST
)
473 if(prop
->u
.p
->setter
)
474 return prop
->u
.p
->setter(This
->ctx
, This
, val
);
476 if(prop
->u
.p
->setter
) {
477 FIXME("getter with no setter\n");
482 prop
->type
= PROP_JSVAL
;
483 prop
->flags
= PROPF_ENUM
;
484 prop
->u
.val
= jsval_undefined();
487 jsval_release(prop
->u
.val
);
490 return This
->builtin_info
->idx_put(This
, prop
->u
.idx
, val
);
492 ERR("type %d\n", prop
->type
);
496 TRACE("%s = %s\n", debugstr_w(prop
->name
), debugstr_jsval(val
));
498 hres
= jsval_copy(val
, &prop
->u
.val
);
500 prop
->u
.val
= jsval_undefined();
504 if(This
->builtin_info
->on_put
)
505 This
->builtin_info
->on_put(This
, prop
->name
);
510 HRESULT
builtin_set_const(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
512 TRACE("%p %s\n", jsthis
, debugstr_jsval(value
));
516 static HRESULT
fill_protrefs(jsdisp_t
*This
)
518 dispex_prop_t
*iter
, *prop
;
524 fill_protrefs(This
->prototype
);
526 for(iter
= This
->prototype
->props
; iter
< This
->prototype
->props
+This
->prototype
->prop_cnt
; iter
++) {
529 hres
= find_prop_name(This
, iter
->hash
, iter
->name
, &prop
);
532 if(!prop
|| prop
->type
==PROP_DELETED
) {
534 prop
->type
= PROP_PROTREF
;
536 prop
->u
.ref
= iter
- This
->prototype
->props
;
538 prop
= alloc_protref(This
, iter
->name
, iter
- This
->prototype
->props
);
540 return E_OUTOFMEMORY
;
548 static inline jsdisp_t
*impl_from_IDispatchEx(IDispatchEx
*iface
)
550 return CONTAINING_RECORD(iface
, jsdisp_t
, IDispatchEx_iface
);
553 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
555 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
557 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
558 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
559 *ppv
= &This
->IDispatchEx_iface
;
560 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
561 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
562 *ppv
= &This
->IDispatchEx_iface
;
563 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
564 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
565 *ppv
= &This
->IDispatchEx_iface
;
566 }else if(IsEqualGUID(&IID_IDispatchJS
, riid
)) {
567 TRACE("(%p)->(IID_IDispatchJS %p)\n", This
, ppv
);
572 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
574 return E_NOINTERFACE
;
581 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
583 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
588 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
590 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
591 ULONG ref
= --This
->ref
;
597 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
599 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
601 TRACE("(%p)->(%p)\n", This
, pctinfo
);
607 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
, LCID lcid
,
610 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
611 FIXME("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
615 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
616 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
,
619 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
623 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
626 for(i
=0; i
< cNames
; i
++) {
627 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[i
], 0, rgDispId
+i
);
635 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
636 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
637 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
639 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
641 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
642 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
644 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
,
645 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
648 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
650 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
652 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
654 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
)) {
655 FIXME("Unsupported grfdex %x\n", grfdex
);
659 return jsdisp_get_id(This
, bstrName
, grfdex
, pid
);
662 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
663 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
665 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
669 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
672 V_VT(pvarRes
) = VT_EMPTY
;
674 prop
= get_prop(This
, id
);
675 if(!prop
|| prop
->type
== PROP_DELETED
) {
676 TRACE("invalid id\n");
677 return DISP_E_MEMBERNOTFOUND
;
683 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
684 wFlags
= DISPATCH_METHOD
;
686 case DISPATCH_METHOD
:
687 case DISPATCH_CONSTRUCT
: {
688 jsval_t
*argv
, buf
[6], r
;
691 hres
= convert_params(pdp
, buf
, &argc
, &argv
);
695 hres
= invoke_prop_func(This
, get_this(pdp
), prop
, wFlags
, argc
, argv
, pvarRes
? &r
: NULL
, pspCaller
);
698 if(SUCCEEDED(hres
) && pvarRes
) {
699 hres
= jsval_to_variant(r
, pvarRes
);
704 case DISPATCH_PROPERTYGET
: {
707 hres
= prop_get(This
, prop
, pdp
, &r
, pspCaller
);
708 if(SUCCEEDED(hres
)) {
709 hres
= jsval_to_variant(r
, pvarRes
);
714 case DISPATCH_PROPERTYPUT
: {
718 for(i
=0; i
< pdp
->cNamedArgs
; i
++) {
719 if(pdp
->rgdispidNamedArgs
[i
] == DISPID_PROPERTYPUT
)
723 if(i
== pdp
->cNamedArgs
) {
724 TRACE("no value to set\n");
725 return DISP_E_PARAMNOTOPTIONAL
;
728 hres
= variant_to_jsval(pdp
->rgvarg
+i
, &val
);
732 hres
= prop_put(This
, prop
, val
, pspCaller
);
737 FIXME("Unimplemented flags %x\n", wFlags
);
742 *pei
= This
->ctx
->ei
.ei
;
746 static HRESULT
delete_prop(dispex_prop_t
*prop
, BOOL
*ret
)
748 if(prop
->flags
& PROPF_DONTDELETE
) {
753 *ret
= TRUE
; /* FIXME: not exactly right */
755 if(prop
->type
== PROP_JSVAL
) {
756 jsval_release(prop
->u
.val
);
757 prop
->type
= PROP_DELETED
;
762 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
764 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
769 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
771 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
772 FIXME("Unsupported grfdex %x\n", grfdex
);
774 hres
= find_prop_name(This
, string_hash(bstrName
), bstrName
, &prop
);
778 TRACE("not found\n");
782 return delete_prop(prop
, &b
);
785 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
787 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
, &b
);
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
|| builtin_info
->value_prop
.getter
) {
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
);
953 void jsdisp_free(jsdisp_t
*obj
)
957 TRACE("(%p)\n", obj
);
959 for(prop
= obj
->props
; prop
< obj
->props
+obj
->prop_cnt
; prop
++) {
960 if(prop
->type
== PROP_JSVAL
)
961 jsval_release(prop
->u
.val
);
962 heap_free(prop
->name
);
964 heap_free(obj
->props
);
965 script_release(obj
->ctx
);
967 jsdisp_release(obj
->prototype
);
969 if(obj
->builtin_info
->destructor
)
970 obj
->builtin_info
->destructor(obj
);
977 jsdisp_t
*jsdisp_addref(jsdisp_t
*jsdisp
)
979 ULONG ref
= ++jsdisp
->ref
;
980 TRACE("(%p) ref=%d\n", jsdisp
, ref
);
984 void jsdisp_release(jsdisp_t
*jsdisp
)
986 ULONG ref
= --jsdisp
->ref
;
988 TRACE("(%p) ref=%d\n", jsdisp
, ref
);
996 HRESULT
init_dispex_from_constr(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*constr
)
998 jsdisp_t
*prot
= NULL
;
1002 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t','y','p','e',0};
1004 hres
= find_prop_name_prot(constr
, string_hash(prototypeW
), prototypeW
, &prop
);
1005 if(SUCCEEDED(hres
) && prop
&& prop
->type
!=PROP_DELETED
) {
1008 hres
= prop_get(constr
, prop
, NULL
, &val
, NULL
);
1010 ERR("Could not get prototype\n");
1014 if(is_object_instance(val
))
1015 prot
= iface_to_jsdisp((IUnknown
*)get_object(val
));
1019 hres
= init_dispex(dispex
, ctx
, builtin_info
, prot
);
1022 jsdisp_release(prot
);
1026 jsdisp_t
*iface_to_jsdisp(IUnknown
*iface
)
1031 hres
= IUnknown_QueryInterface(iface
, &IID_IDispatchJS
, (void**)&ret
);
1038 HRESULT
jsdisp_get_id(jsdisp_t
*jsdisp
, const WCHAR
*name
, DWORD flags
, DISPID
*id
)
1040 dispex_prop_t
*prop
;
1043 if(flags
& fdexNameEnsure
)
1044 hres
= ensure_prop_name(jsdisp
, name
, TRUE
, PROPF_ENUM
, &prop
);
1046 hres
= find_prop_name_prot(jsdisp
, string_hash(name
), name
, &prop
);
1050 if(prop
&& prop
->type
!=PROP_DELETED
) {
1051 *id
= prop_to_id(jsdisp
, prop
);
1055 TRACE("not found %s\n", debugstr_w(name
));
1056 return DISP_E_UNKNOWNNAME
;
1059 HRESULT
jsdisp_call_value(jsdisp_t
*jsfunc
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1063 assert(!(flags
& ~(DISPATCH_METHOD
|DISPATCH_CONSTRUCT
)));
1065 if(is_class(jsfunc
, JSCLASS_FUNCTION
)) {
1066 hres
= Function_invoke(jsfunc
, jsthis
, flags
, argc
, argv
, r
);
1070 if(!jsfunc
->builtin_info
->value_prop
.invoke
) {
1071 WARN("Not a function\n");
1072 return throw_type_error(jsfunc
->ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1075 set_disp(&vdisp
, jsthis
);
1076 hres
= jsfunc
->builtin_info
->value_prop
.invoke(jsfunc
->ctx
, &vdisp
, flags
, argc
, argv
, r
);
1077 vdisp_release(&vdisp
);
1082 HRESULT
jsdisp_call(jsdisp_t
*disp
, DISPID id
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1084 dispex_prop_t
*prop
;
1086 prop
= get_prop(disp
, id
);
1088 return DISP_E_MEMBERNOTFOUND
;
1090 return invoke_prop_func(disp
, to_disp(disp
), prop
, flags
, argc
, argv
, r
, NULL
);
1093 HRESULT
jsdisp_call_name(jsdisp_t
*disp
, const WCHAR
*name
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1095 dispex_prop_t
*prop
;
1098 hres
= find_prop_name_prot(disp
, string_hash(name
), name
, &prop
);
1102 return invoke_prop_func(disp
, to_disp(disp
), prop
, flags
, argc
, argv
, r
, NULL
);
1105 HRESULT
disp_call(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*ret
)
1107 IDispatchEx
*dispex
;
1109 VARIANT buf
[6], retv
;
1114 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1116 if(flags
& DISPATCH_PROPERTYPUT
) {
1117 FIXME("disp_call(propput) on builtin object\n");
1121 hres
= jsdisp_call(jsdisp
, id
, flags
, argc
, argv
, ret
);
1122 jsdisp_release(jsdisp
);
1127 flags
|= DISPATCH_PROPERTYGET
;
1131 if(flags
& DISPATCH_PROPERTYPUT
) {
1132 static DISPID propput_dispid
= DISPID_PROPERTYPUT
;
1135 dp
.rgdispidNamedArgs
= &propput_dispid
;
1138 dp
.rgdispidNamedArgs
= NULL
;
1142 dp
.rgvarg
= heap_alloc(argc
*sizeof(VARIANT
));
1144 return E_OUTOFMEMORY
;
1149 for(i
=0; i
<argc
; i
++) {
1150 hres
= jsval_to_variant(argv
[i
], dp
.rgvarg
+argc
-i
-1);
1153 VariantClear(dp
.rgvarg
+argc
-i
-1);
1154 if(dp
.rgvarg
!= buf
)
1155 heap_free(dp
.rgvarg
);
1160 V_VT(&retv
) = VT_EMPTY
;
1162 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1163 if(SUCCEEDED(hres
)) {
1164 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, &dp
, ret
? &retv
: NULL
, &ctx
->ei
.ei
,
1165 &ctx
->jscaller
->IServiceProvider_iface
);
1166 IDispatchEx_Release(dispex
);
1170 if(flags
== DISPATCH_CONSTRUCT
) {
1171 WARN("IDispatch cannot be constructor\n");
1172 return DISP_E_MEMBERNOTFOUND
;
1175 TRACE("using IDispatch\n");
1176 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, &dp
, ret
? &retv
: NULL
, &ctx
->ei
.ei
, &err
);
1179 for(i
=0; i
<argc
; i
++)
1180 VariantClear(dp
.rgvarg
+argc
-i
-1);
1181 if(dp
.rgvarg
!= buf
)
1182 heap_free(dp
.rgvarg
);
1187 hres
= variant_to_jsval(&retv
, ret
);
1188 VariantClear(&retv
);
1193 HRESULT
disp_call_value(script_ctx_t
*ctx
, IDispatch
*disp
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1197 IDispatchEx
*dispex
;
1198 VARIANT buf
[6], retv
;
1203 assert(!(flags
& ~(DISPATCH_METHOD
|DISPATCH_CONSTRUCT
)));
1205 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1207 hres
= jsdisp_call_value(jsdisp
, jsthis
, flags
, argc
, argv
, r
);
1208 jsdisp_release(jsdisp
);
1212 if(r
&& argc
&& flags
== DISPATCH_METHOD
)
1213 flags
|= DISPATCH_PROPERTYGET
;
1215 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1217 TRACE("using IDispatch\n");
1223 static DISPID this_id
= DISPID_THIS
;
1227 dp
.rgdispidNamedArgs
= &this_id
;
1231 dp
.rgdispidNamedArgs
= NULL
;
1234 if(dp
.cArgs
> sizeof(buf
)/sizeof(*buf
)) {
1235 dp
.rgvarg
= heap_alloc(dp
.cArgs
*sizeof(VARIANT
));
1238 IDispatchEx_Release(dispex
);
1239 return E_OUTOFMEMORY
;
1245 for(i
=0; i
<argc
; i
++) {
1246 hres
= jsval_to_variant(argv
[i
], dp
.rgvarg
+dp
.cArgs
-i
-1);
1249 VariantClear(dp
.rgvarg
+dp
.cArgs
-i
-1);
1250 if(dp
.rgvarg
!= buf
)
1251 heap_free(dp
.rgvarg
);
1253 IDispatchEx_Release(dispex
);
1258 V_VT(dp
.rgvarg
) = VT_DISPATCH
;
1259 V_DISPATCH(dp
.rgvarg
) = jsthis
;
1262 V_VT(&retv
) = VT_EMPTY
;
1265 hres
= IDispatchEx_InvokeEx(dispex
, DISPID_VALUE
, ctx
->lcid
, flags
, &dp
, r
? &retv
: NULL
, &ctx
->ei
.ei
,
1266 &ctx
->jscaller
->IServiceProvider_iface
);
1267 IDispatchEx_Release(dispex
);
1271 if(flags
== DISPATCH_CONSTRUCT
) {
1272 WARN("IDispatch cannot be constructor\n");
1273 return DISP_E_MEMBERNOTFOUND
;
1276 hres
= IDispatch_Invoke(disp
, DISPID_VALUE
, &IID_NULL
, ctx
->lcid
, flags
, &dp
, r
? &retv
: NULL
, &ctx
->ei
.ei
, &err
);
1279 for(i
=0; i
<argc
; i
++)
1280 VariantClear(dp
.rgvarg
+dp
.cArgs
-i
-1);
1281 if(dp
.rgvarg
!= buf
)
1282 heap_free(dp
.rgvarg
);
1289 hres
= variant_to_jsval(&retv
, r
);
1290 VariantClear(&retv
);
1294 HRESULT
jsdisp_propput(jsdisp_t
*obj
, const WCHAR
*name
, DWORD flags
, jsval_t val
)
1296 dispex_prop_t
*prop
;
1299 hres
= ensure_prop_name(obj
, name
, FALSE
, flags
, &prop
);
1303 return prop_put(obj
, prop
, val
, NULL
);
1306 HRESULT
jsdisp_propput_name(jsdisp_t
*obj
, const WCHAR
*name
, jsval_t val
)
1308 return jsdisp_propput(obj
, name
, PROPF_ENUM
, val
);
1311 HRESULT
jsdisp_propput_const(jsdisp_t
*obj
, const WCHAR
*name
, jsval_t val
)
1313 dispex_prop_t
*prop
;
1316 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_CONST
, &prop
);
1320 return jsval_copy(val
, &prop
->u
.val
);
1323 HRESULT
jsdisp_propput_dontenum(jsdisp_t
*obj
, const WCHAR
*name
, jsval_t val
)
1325 return jsdisp_propput(obj
, name
, 0, val
);
1328 HRESULT
jsdisp_propput_idx(jsdisp_t
*obj
, DWORD idx
, jsval_t val
)
1332 static const WCHAR formatW
[] = {'%','d',0};
1334 sprintfW(buf
, formatW
, idx
);
1335 return jsdisp_propput_name(obj
, buf
, val
);
1338 HRESULT
disp_propput(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, jsval_t val
)
1343 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1345 dispex_prop_t
*prop
;
1347 prop
= get_prop(jsdisp
, id
);
1349 hres
= prop_put(jsdisp
, prop
, val
, NULL
);
1351 hres
= DISP_E_MEMBERNOTFOUND
;
1353 jsdisp_release(jsdisp
);
1355 DISPID dispid
= DISPID_PROPERTYPUT
;
1356 DWORD flags
= DISPATCH_PROPERTYPUT
;
1358 DISPPARAMS dp
= {&var
, &dispid
, 1, 1};
1359 IDispatchEx
*dispex
;
1361 hres
= jsval_to_variant(val
, &var
);
1365 if(V_VT(&var
) == VT_DISPATCH
)
1366 flags
|= DISPATCH_PROPERTYPUTREF
;
1369 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1370 if(SUCCEEDED(hres
)) {
1371 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, &dp
, NULL
, &ctx
->ei
.ei
,
1372 &ctx
->jscaller
->IServiceProvider_iface
);
1373 IDispatchEx_Release(dispex
);
1377 TRACE("using IDispatch\n");
1378 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, &dp
, NULL
, &ctx
->ei
.ei
, &err
);
1387 HRESULT
jsdisp_propget_name(jsdisp_t
*obj
, const WCHAR
*name
, jsval_t
*val
)
1389 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1390 dispex_prop_t
*prop
;
1393 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1397 if(!prop
|| prop
->type
==PROP_DELETED
) {
1398 *val
= jsval_undefined();
1402 return prop_get(obj
, prop
, &dp
, val
, NULL
);
1405 HRESULT
jsdisp_get_idx(jsdisp_t
*obj
, DWORD idx
, jsval_t
*r
)
1408 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1409 dispex_prop_t
*prop
;
1412 static const WCHAR formatW
[] = {'%','d',0};
1414 sprintfW(name
, formatW
, idx
);
1416 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1420 if(!prop
|| prop
->type
==PROP_DELETED
) {
1421 *r
= jsval_undefined();
1422 return DISP_E_UNKNOWNNAME
;
1425 return prop_get(obj
, prop
, &dp
, r
, NULL
);
1428 HRESULT
jsdisp_propget(jsdisp_t
*jsdisp
, DISPID id
, jsval_t
*val
)
1430 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1431 dispex_prop_t
*prop
;
1433 prop
= get_prop(jsdisp
, id
);
1435 return DISP_E_MEMBERNOTFOUND
;
1437 return prop_get(jsdisp
, prop
, &dp
, val
, NULL
);
1440 HRESULT
disp_propget(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, jsval_t
*val
)
1442 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1443 IDispatchEx
*dispex
;
1448 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1450 hres
= jsdisp_propget(jsdisp
, id
, val
);
1451 jsdisp_release(jsdisp
);
1455 V_VT(&var
) = VT_EMPTY
;
1457 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1458 if(SUCCEEDED(hres
)) {
1459 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, &var
, &ctx
->ei
.ei
,
1460 &ctx
->jscaller
->IServiceProvider_iface
);
1461 IDispatchEx_Release(dispex
);
1465 TRACE("using IDispatch\n");
1466 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, &var
, &ctx
->ei
.ei
, &err
);
1471 hres
= variant_to_jsval(&var
, val
);
1476 HRESULT
jsdisp_delete_idx(jsdisp_t
*obj
, DWORD idx
)
1478 static const WCHAR formatW
[] = {'%','d',0};
1480 dispex_prop_t
*prop
;
1484 sprintfW(buf
, formatW
, idx
);
1486 hres
= find_prop_name(obj
, string_hash(buf
), buf
, &prop
);
1487 if(FAILED(hres
) || !prop
)
1490 return delete_prop(prop
, &b
);
1493 HRESULT
disp_delete(IDispatch
*disp
, DISPID id
, BOOL
*ret
)
1495 IDispatchEx
*dispex
;
1499 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1501 dispex_prop_t
*prop
;
1503 prop
= get_prop(jsdisp
, id
);
1505 hres
= delete_prop(prop
, ret
);
1507 hres
= DISP_E_MEMBERNOTFOUND
;
1509 jsdisp_release(jsdisp
);
1513 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1519 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, id
);
1520 IDispatchEx_Release(dispex
);
1524 *ret
= hres
== S_OK
;
1528 HRESULT
disp_delete_name(script_ctx_t
*ctx
, IDispatch
*disp
, jsstr_t
*name
, BOOL
*ret
)
1530 IDispatchEx
*dispex
;
1535 jsdisp
= iface_to_jsdisp((IUnknown
*)disp
);
1537 dispex_prop_t
*prop
;
1540 ptr
= jsstr_flatten(name
);
1542 jsdisp_release(jsdisp
);
1543 return E_OUTOFMEMORY
;
1546 hres
= find_prop_name(jsdisp
, string_hash(ptr
), ptr
, &prop
);
1548 hres
= delete_prop(prop
, ret
);
1554 jsdisp_release(jsdisp
);
1558 bstr
= SysAllocStringLen(NULL
, jsstr_length(name
));
1560 return E_OUTOFMEMORY
;
1561 jsstr_flush(name
, bstr
);
1563 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1564 if(SUCCEEDED(hres
)) {
1565 hres
= IDispatchEx_DeleteMemberByName(dispex
, bstr
, make_grfdex(ctx
, fdexNameCaseSensitive
));
1567 *ret
= hres
== S_OK
;
1568 IDispatchEx_Release(dispex
);
1572 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, &id
);
1573 if(SUCCEEDED(hres
)) {
1574 /* Property exists and we can't delete it from pure IDispatch interface, so return false. */
1576 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1577 /* Property doesn't exist, so nothing to delete */
1583 SysFreeString(bstr
);
1587 HRESULT
jsdisp_is_own_prop(jsdisp_t
*obj
, const WCHAR
*name
, BOOL
*ret
)
1589 dispex_prop_t
*prop
;
1592 hres
= find_prop_name(obj
, string_hash(name
), name
, &prop
);
1596 *ret
= prop
&& (prop
->type
== PROP_JSVAL
|| prop
->type
== PROP_BUILTIN
);
1600 HRESULT
jsdisp_is_enumerable(jsdisp_t
*obj
, const WCHAR
*name
, BOOL
*ret
)
1602 dispex_prop_t
*prop
;
1605 hres
= find_prop_name(obj
, string_hash(name
), name
, &prop
);
1609 *ret
= prop
&& (prop
->flags
& PROPF_ENUM
) && prop
->type
!= PROP_PROTREF
;