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
);
28 #define FDEX_VERSION_MASK 0xf0000000
29 #define GOLDEN_RATIO 0x9E3779B9U
39 struct _dispex_prop_t
{
47 const builtin_prop_t
*p
;
56 static inline DISPID
prop_to_id(jsdisp_t
*This
, dispex_prop_t
*prop
)
58 return prop
- This
->props
;
61 static inline dispex_prop_t
*get_prop(jsdisp_t
*This
, DISPID id
)
63 if(id
< 0 || id
>= This
->prop_cnt
|| This
->props
[id
].type
== PROP_DELETED
)
66 return This
->props
+id
;
69 static DWORD
get_flags(jsdisp_t
*This
, dispex_prop_t
*prop
)
71 if(prop
->type
== PROP_PROTREF
) {
72 dispex_prop_t
*parent
= get_prop(This
->prototype
, prop
->u
.ref
);
74 prop
->type
= PROP_DELETED
;
78 return get_flags(This
->prototype
, parent
);
84 static const builtin_prop_t
*find_builtin_prop(jsdisp_t
*This
, const WCHAR
*name
)
86 int min
= 0, max
, i
, r
;
88 max
= This
->builtin_info
->props_cnt
-1;
92 r
= strcmpW(name
, This
->builtin_info
->props
[i
].name
);
94 return This
->builtin_info
->props
+ i
;
105 static inline unsigned string_hash(const WCHAR
*name
)
109 h
= (h
>>(sizeof(unsigned)*8-4)) ^ (h
<<4) ^ tolowerW(*name
);
113 static inline unsigned get_props_idx(jsdisp_t
*This
, unsigned hash
)
115 return (hash
*GOLDEN_RATIO
) & (This
->buf_size
-1);
118 static inline HRESULT
resize_props(jsdisp_t
*This
)
120 dispex_prop_t
*props
;
123 if(This
->buf_size
!= This
->prop_cnt
)
126 props
= heap_realloc(This
->props
, sizeof(dispex_prop_t
)*This
->buf_size
*2);
128 return E_OUTOFMEMORY
;
132 for(i
=0; i
<This
->buf_size
; i
++) {
133 This
->props
[i
].bucket_head
= 0;
134 This
->props
[i
].bucket_next
= 0;
137 for(i
=1; i
<This
->prop_cnt
; i
++) {
138 props
= This
->props
+i
;
140 bucket
= get_props_idx(This
, props
->hash
);
141 props
->bucket_next
= This
->props
[bucket
].bucket_head
;
142 This
->props
[bucket
].bucket_head
= i
;
148 static inline dispex_prop_t
* alloc_prop(jsdisp_t
*This
, const WCHAR
*name
, prop_type_t type
, DWORD flags
)
153 if(FAILED(resize_props(This
)))
156 prop
= &This
->props
[This
->prop_cnt
];
157 prop
->name
= heap_strdupW(name
);
162 prop
->hash
= string_hash(name
);
164 bucket
= get_props_idx(This
, prop
->hash
);
165 prop
->bucket_next
= This
->props
[bucket
].bucket_head
;
166 This
->props
[bucket
].bucket_head
= This
->prop_cnt
++;
170 static dispex_prop_t
*alloc_protref(jsdisp_t
*This
, const WCHAR
*name
, DWORD ref
)
174 ret
= alloc_prop(This
, name
, PROP_PROTREF
, 0);
182 static HRESULT
find_prop_name(jsdisp_t
*This
, unsigned hash
, const WCHAR
*name
, dispex_prop_t
**ret
)
184 const builtin_prop_t
*builtin
;
185 unsigned bucket
, pos
, prev
= 0;
188 bucket
= get_props_idx(This
, hash
);
189 pos
= This
->props
[bucket
].bucket_head
;
191 if(!strcmpW(name
, This
->props
[pos
].name
)) {
193 This
->props
[prev
].bucket_next
= This
->props
[pos
].bucket_next
;
194 This
->props
[pos
].bucket_next
= This
->props
[bucket
].bucket_head
;
195 This
->props
[bucket
].bucket_head
= pos
;
198 *ret
= &This
->props
[pos
];
203 pos
= This
->props
[pos
].bucket_next
;
206 builtin
= find_builtin_prop(This
, name
);
208 prop
= alloc_prop(This
, name
, PROP_BUILTIN
, builtin
->flags
);
210 return E_OUTOFMEMORY
;
217 if(This
->builtin_info
->idx_length
) {
221 for(ptr
= name
; isdigitW(*ptr
) && idx
< 0x10000; ptr
++)
222 idx
= idx
*10 + (*ptr
-'0');
223 if(!*ptr
&& idx
< This
->builtin_info
->idx_length(This
)) {
224 prop
= alloc_prop(This
, name
, PROP_IDX
, This
->builtin_info
->idx_put
? 0 : PROPF_CONST
);
226 return E_OUTOFMEMORY
;
238 static HRESULT
find_prop_name_prot(jsdisp_t
*This
, unsigned hash
, const WCHAR
*name
, dispex_prop_t
**ret
)
240 dispex_prop_t
*prop
, *del
=NULL
;
243 hres
= find_prop_name(This
, hash
, name
, &prop
);
246 if(prop
&& prop
->type
==PROP_DELETED
) {
253 if(This
->prototype
) {
254 hres
= find_prop_name_prot(This
->prototype
, hash
, name
, &prop
);
259 del
->type
= PROP_PROTREF
;
261 del
->u
.ref
= prop
- This
->prototype
->props
;
264 prop
= alloc_protref(This
, prop
->name
, prop
- This
->prototype
->props
);
266 return E_OUTOFMEMORY
;
278 static HRESULT
ensure_prop_name(jsdisp_t
*This
, const WCHAR
*name
, BOOL search_prot
, DWORD create_flags
, dispex_prop_t
**ret
)
284 hres
= find_prop_name_prot(This
, string_hash(name
), name
, &prop
);
286 hres
= find_prop_name(This
, string_hash(name
), name
, &prop
);
287 if(SUCCEEDED(hres
) && (!prop
|| prop
->type
== PROP_DELETED
)) {
288 TRACE("creating prop %s flags %x\n", debugstr_w(name
), create_flags
);
291 prop
->type
= PROP_JSVAL
;
292 prop
->flags
= create_flags
;
293 prop
->u
.val
= jsval_undefined();
295 prop
= alloc_prop(This
, name
, PROP_JSVAL
, create_flags
);
297 return E_OUTOFMEMORY
;
300 prop
->u
.val
= jsval_undefined();
307 static IDispatch
*get_this(DISPPARAMS
*dp
)
311 for(i
=0; i
< dp
->cNamedArgs
; i
++) {
312 if(dp
->rgdispidNamedArgs
[i
] == DISPID_THIS
) {
313 if(V_VT(dp
->rgvarg
+i
) == VT_DISPATCH
)
314 return V_DISPATCH(dp
->rgvarg
+i
);
316 WARN("This is not VT_DISPATCH\n");
321 TRACE("no this passed\n");
325 static HRESULT
convert_params(const DISPPARAMS
*dp
, jsval_t
*buf
, unsigned *argc
, jsval_t
**ret
)
332 cnt
= dp
->cArgs
- dp
->cNamedArgs
;
335 argv
= heap_alloc(cnt
* sizeof(*argv
));
337 return E_OUTOFMEMORY
;
342 for(i
= 0; i
< cnt
; i
++) {
343 hres
= variant_to_jsval(dp
->rgvarg
+dp
->cArgs
-i
-1, argv
+i
);
346 jsval_release(argv
[i
]);
358 static HRESULT
invoke_prop_func(jsdisp_t
*This
, IDispatch
*jsthis
, dispex_prop_t
*prop
, WORD flags
,
359 unsigned argc
, jsval_t
*argv
, jsval_t
*r
, IServiceProvider
*caller
)
365 if(flags
== DISPATCH_CONSTRUCT
&& (prop
->flags
& PROPF_METHOD
)) {
366 WARN("%s is not a constructor\n", debugstr_w(prop
->name
));
370 if(prop
->name
|| This
->builtin_info
->class != JSCLASS_FUNCTION
) {
373 if(This
->builtin_info
->class != JSCLASS_FUNCTION
&& prop
->u
.p
->invoke
!= JSGlobal_eval
)
374 flags
&= ~DISPATCH_JSCRIPT_INTERNAL_MASK
;
376 set_disp(&vthis
, jsthis
);
378 set_jsdisp(&vthis
, This
);
379 hres
= prop
->u
.p
->invoke(This
->ctx
, &vthis
, flags
, argc
, argv
, r
);
380 vdisp_release(&vthis
);
382 /* Function object calls are special case */
383 hres
= Function_invoke(This
, jsthis
, flags
, argc
, argv
, r
);
388 return invoke_prop_func(This
->prototype
, jsthis
, This
->prototype
->props
+prop
->u
.ref
,
389 flags
, argc
, argv
, r
, caller
);
391 if(!is_object_instance(prop
->u
.val
)) {
392 FIXME("invoke %s\n", debugstr_jsval(prop
->u
.val
));
396 TRACE("call %s %p\n", debugstr_w(prop
->name
), get_object(prop
->u
.val
));
398 return disp_call_value(This
->ctx
, get_object(prop
->u
.val
), jsthis
, flags
, argc
, argv
, r
);
401 FIXME("Invoking PROP_IDX not yet supported\n");
411 static HRESULT
prop_get(jsdisp_t
*This
, dispex_prop_t
*prop
, DISPPARAMS
*dp
,
412 jsval_t
*r
, IServiceProvider
*caller
)
418 if(prop
->u
.p
->getter
) {
419 hres
= prop
->u
.p
->getter(This
->ctx
, This
, r
);
423 assert(prop
->u
.p
->invoke
!= NULL
);
424 hres
= create_builtin_function(This
->ctx
, prop
->u
.p
->invoke
, prop
->u
.p
->name
, NULL
,
425 prop
->u
.p
->flags
, NULL
, &obj
);
429 prop
->type
= PROP_JSVAL
;
430 prop
->u
.val
= jsval_obj(obj
);
437 hres
= prop_get(This
->prototype
, This
->prototype
->props
+prop
->u
.ref
, dp
, r
, caller
);
440 hres
= jsval_copy(prop
->u
.val
, r
);
443 hres
= This
->builtin_info
->idx_get(This
, prop
->u
.idx
, r
);
446 ERR("type %d\n", prop
->type
);
451 TRACE("fail %08x\n", hres
);
455 TRACE("%s ret %s\n", debugstr_w(prop
->name
), debugstr_jsval(*r
));
459 static HRESULT
prop_put(jsdisp_t
*This
, dispex_prop_t
*prop
, jsval_t val
, IServiceProvider
*caller
)
463 if(prop
->flags
& PROPF_CONST
)
468 if(prop
->u
.p
->setter
)
469 return prop
->u
.p
->setter(This
->ctx
, This
, val
);
471 if(prop
->u
.p
->setter
) {
472 FIXME("getter with no setter\n");
477 prop
->type
= PROP_JSVAL
;
478 prop
->flags
= PROPF_ENUM
;
479 prop
->u
.val
= jsval_undefined();
482 jsval_release(prop
->u
.val
);
485 return This
->builtin_info
->idx_put(This
, prop
->u
.idx
, val
);
487 ERR("type %d\n", prop
->type
);
491 TRACE("%s = %s\n", debugstr_w(prop
->name
), debugstr_jsval(val
));
493 hres
= jsval_copy(val
, &prop
->u
.val
);
497 if(This
->builtin_info
->on_put
)
498 This
->builtin_info
->on_put(This
, prop
->name
);
503 HRESULT
builtin_set_const(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
505 TRACE("%p %s\n", jsthis
, debugstr_jsval(value
));
509 static HRESULT
fill_protrefs(jsdisp_t
*This
)
511 dispex_prop_t
*iter
, *prop
;
517 fill_protrefs(This
->prototype
);
519 for(iter
= This
->prototype
->props
; iter
< This
->prototype
->props
+This
->prototype
->prop_cnt
; iter
++) {
522 hres
= find_prop_name(This
, iter
->hash
, iter
->name
, &prop
);
525 if(!prop
|| prop
->type
==PROP_DELETED
) {
527 prop
->type
= PROP_PROTREF
;
529 prop
->u
.ref
= iter
- This
->prototype
->props
;
531 prop
= alloc_protref(This
, iter
->name
, iter
- This
->prototype
->props
);
533 return E_OUTOFMEMORY
;
541 static inline jsdisp_t
*impl_from_IDispatchEx(IDispatchEx
*iface
)
543 return CONTAINING_RECORD(iface
, jsdisp_t
, IDispatchEx_iface
);
546 static HRESULT WINAPI
DispatchEx_QueryInterface(IDispatchEx
*iface
, REFIID riid
, void **ppv
)
548 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
550 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
551 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
552 *ppv
= &This
->IDispatchEx_iface
;
553 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
554 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
555 *ppv
= &This
->IDispatchEx_iface
;
556 }else if(IsEqualGUID(&IID_IDispatchEx
, riid
)) {
557 TRACE("(%p)->(IID_IDispatchEx %p)\n", This
, ppv
);
558 *ppv
= &This
->IDispatchEx_iface
;
560 WARN("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
562 return E_NOINTERFACE
;
569 static ULONG WINAPI
DispatchEx_AddRef(IDispatchEx
*iface
)
571 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
576 static ULONG WINAPI
DispatchEx_Release(IDispatchEx
*iface
)
578 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
579 ULONG ref
= --This
->ref
;
580 TRACE("(%p) ref=%d\n", This
, ref
);
586 static HRESULT WINAPI
DispatchEx_GetTypeInfoCount(IDispatchEx
*iface
, UINT
*pctinfo
)
588 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
590 TRACE("(%p)->(%p)\n", This
, pctinfo
);
596 static HRESULT WINAPI
DispatchEx_GetTypeInfo(IDispatchEx
*iface
, UINT iTInfo
, LCID lcid
,
599 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
600 FIXME("(%p)->(%u %u %p)\n", This
, iTInfo
, lcid
, ppTInfo
);
604 static HRESULT WINAPI
DispatchEx_GetIDsOfNames(IDispatchEx
*iface
, REFIID riid
,
605 LPOLESTR
*rgszNames
, UINT cNames
, LCID lcid
,
608 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
612 TRACE("(%p)->(%s %p %u %u %p)\n", This
, debugstr_guid(riid
), rgszNames
, cNames
,
615 for(i
=0; i
< cNames
; i
++) {
616 hres
= IDispatchEx_GetDispID(&This
->IDispatchEx_iface
, rgszNames
[i
], 0, rgDispId
+i
);
624 static HRESULT WINAPI
DispatchEx_Invoke(IDispatchEx
*iface
, DISPID dispIdMember
,
625 REFIID riid
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
,
626 VARIANT
*pVarResult
, EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
628 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
630 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This
, dispIdMember
, debugstr_guid(riid
),
631 lcid
, wFlags
, pDispParams
, pVarResult
, pExcepInfo
, puArgErr
);
633 return IDispatchEx_InvokeEx(&This
->IDispatchEx_iface
, dispIdMember
, lcid
, wFlags
,
634 pDispParams
, pVarResult
, pExcepInfo
, NULL
);
637 static HRESULT WINAPI
DispatchEx_GetDispID(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
, DISPID
*pid
)
639 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
641 TRACE("(%p)->(%s %x %p)\n", This
, debugstr_w(bstrName
), grfdex
, pid
);
643 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
)) {
644 FIXME("Unsupported grfdex %x\n", grfdex
);
648 return jsdisp_get_id(This
, bstrName
, grfdex
, pid
);
651 static HRESULT WINAPI
DispatchEx_InvokeEx(IDispatchEx
*iface
, DISPID id
, LCID lcid
, WORD wFlags
, DISPPARAMS
*pdp
,
652 VARIANT
*pvarRes
, EXCEPINFO
*pei
, IServiceProvider
*pspCaller
)
654 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
658 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This
, id
, lcid
, wFlags
, pdp
, pvarRes
, pei
, pspCaller
);
661 V_VT(pvarRes
) = VT_EMPTY
;
663 prop
= get_prop(This
, id
);
664 if(!prop
|| prop
->type
== PROP_DELETED
) {
665 TRACE("invalid id\n");
666 return DISP_E_MEMBERNOTFOUND
;
672 case DISPATCH_METHOD
|DISPATCH_PROPERTYGET
:
673 wFlags
= DISPATCH_METHOD
;
675 case DISPATCH_METHOD
:
676 case DISPATCH_CONSTRUCT
: {
677 jsval_t
*argv
, buf
[6], r
;
680 hres
= convert_params(pdp
, buf
, &argc
, &argv
);
684 hres
= invoke_prop_func(This
, get_this(pdp
), prop
, wFlags
, argc
, argv
, pvarRes
? &r
: NULL
, pspCaller
);
687 if(SUCCEEDED(hres
) && pvarRes
) {
688 hres
= jsval_to_variant(r
, pvarRes
);
693 case DISPATCH_PROPERTYGET
: {
696 hres
= prop_get(This
, prop
, pdp
, &r
, pspCaller
);
697 if(SUCCEEDED(hres
)) {
698 hres
= jsval_to_variant(r
, pvarRes
);
703 case DISPATCH_PROPERTYPUT
: {
707 for(i
=0; i
< pdp
->cNamedArgs
; i
++) {
708 if(pdp
->rgdispidNamedArgs
[i
] == DISPID_PROPERTYPUT
)
712 if(i
== pdp
->cNamedArgs
) {
713 TRACE("no value to set\n");
714 return DISP_E_PARAMNOTOPTIONAL
;
717 hres
= variant_to_jsval(pdp
->rgvarg
+i
, &val
);
721 hres
= prop_put(This
, prop
, val
, pspCaller
);
726 FIXME("Unimplemented flags %x\n", wFlags
);
731 *pei
= This
->ctx
->ei
.ei
;
735 static HRESULT
delete_prop(dispex_prop_t
*prop
, BOOL
*ret
)
737 if(prop
->flags
& PROPF_DONTDELETE
) {
742 *ret
= TRUE
; /* FIXME: not exactly right */
744 if(prop
->type
== PROP_JSVAL
) {
745 jsval_release(prop
->u
.val
);
746 prop
->type
= PROP_DELETED
;
751 static HRESULT WINAPI
DispatchEx_DeleteMemberByName(IDispatchEx
*iface
, BSTR bstrName
, DWORD grfdex
)
753 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
758 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(bstrName
), grfdex
);
760 if(grfdex
& ~(fdexNameCaseSensitive
|fdexNameEnsure
|fdexNameImplicit
|FDEX_VERSION_MASK
))
761 FIXME("Unsupported grfdex %x\n", grfdex
);
763 hres
= find_prop_name(This
, string_hash(bstrName
), bstrName
, &prop
);
767 TRACE("not found\n");
771 return delete_prop(prop
, &b
);
774 static HRESULT WINAPI
DispatchEx_DeleteMemberByDispID(IDispatchEx
*iface
, DISPID id
)
776 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
780 TRACE("(%p)->(%x)\n", This
, id
);
782 prop
= get_prop(This
, id
);
784 WARN("invalid id\n");
785 return DISP_E_MEMBERNOTFOUND
;
788 return delete_prop(prop
, &b
);
791 static HRESULT WINAPI
DispatchEx_GetMemberProperties(IDispatchEx
*iface
, DISPID id
, DWORD grfdexFetch
, DWORD
*pgrfdex
)
793 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
794 FIXME("(%p)->(%x %x %p)\n", This
, id
, grfdexFetch
, pgrfdex
);
798 static HRESULT WINAPI
DispatchEx_GetMemberName(IDispatchEx
*iface
, DISPID id
, BSTR
*pbstrName
)
800 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
803 TRACE("(%p)->(%x %p)\n", This
, id
, pbstrName
);
805 prop
= get_prop(This
, id
);
806 if(!prop
|| !prop
->name
|| prop
->type
== PROP_DELETED
)
807 return DISP_E_MEMBERNOTFOUND
;
809 *pbstrName
= SysAllocString(prop
->name
);
811 return E_OUTOFMEMORY
;
816 static HRESULT WINAPI
DispatchEx_GetNextDispID(IDispatchEx
*iface
, DWORD grfdex
, DISPID id
, DISPID
*pid
)
818 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
822 TRACE("(%p)->(%x %x %p)\n", This
, grfdex
, id
, pid
);
824 if(id
== DISPID_STARTENUM
) {
825 hres
= fill_protrefs(This
);
830 if(id
+1>=0 && id
+1<This
->prop_cnt
) {
831 iter
= &This
->props
[id
+1];
833 *pid
= DISPID_STARTENUM
;
837 while(iter
< This
->props
+ This
->prop_cnt
) {
838 if(iter
->name
&& (get_flags(This
, iter
) & PROPF_ENUM
) && iter
->type
!=PROP_DELETED
) {
839 *pid
= prop_to_id(This
, iter
);
845 *pid
= DISPID_STARTENUM
;
849 static HRESULT WINAPI
DispatchEx_GetNameSpaceParent(IDispatchEx
*iface
, IUnknown
**ppunk
)
851 jsdisp_t
*This
= impl_from_IDispatchEx(iface
);
852 FIXME("(%p)->(%p)\n", This
, ppunk
);
856 static IDispatchExVtbl DispatchExVtbl
= {
857 DispatchEx_QueryInterface
,
860 DispatchEx_GetTypeInfoCount
,
861 DispatchEx_GetTypeInfo
,
862 DispatchEx_GetIDsOfNames
,
864 DispatchEx_GetDispID
,
866 DispatchEx_DeleteMemberByName
,
867 DispatchEx_DeleteMemberByDispID
,
868 DispatchEx_GetMemberProperties
,
869 DispatchEx_GetMemberName
,
870 DispatchEx_GetNextDispID
,
871 DispatchEx_GetNameSpaceParent
874 jsdisp_t
*as_jsdisp(IDispatch
*disp
)
876 assert(disp
->lpVtbl
== (IDispatchVtbl
*)&DispatchExVtbl
);
877 return impl_from_IDispatchEx((IDispatchEx
*)disp
);
880 jsdisp_t
*to_jsdisp(IDispatch
*disp
)
882 return disp
->lpVtbl
== (IDispatchVtbl
*)&DispatchExVtbl
? impl_from_IDispatchEx((IDispatchEx
*)disp
) : NULL
;
885 HRESULT
init_dispex(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
)
887 TRACE("%p (%p)\n", dispex
, prototype
);
889 dispex
->IDispatchEx_iface
.lpVtbl
= &DispatchExVtbl
;
891 dispex
->builtin_info
= builtin_info
;
893 dispex
->props
= heap_alloc_zero(sizeof(dispex_prop_t
)*(dispex
->buf_size
=4));
895 return E_OUTOFMEMORY
;
897 dispex
->prototype
= prototype
;
899 jsdisp_addref(prototype
);
901 dispex
->prop_cnt
= 1;
902 if(builtin_info
->value_prop
.invoke
|| builtin_info
->value_prop
.getter
) {
903 dispex
->props
[0].type
= PROP_BUILTIN
;
904 dispex
->props
[0].u
.p
= &builtin_info
->value_prop
;
906 dispex
->props
[0].type
= PROP_DELETED
;
915 static const builtin_info_t dispex_info
= {
923 HRESULT
create_dispex(script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*prototype
, jsdisp_t
**dispex
)
928 ret
= heap_alloc_zero(sizeof(jsdisp_t
));
930 return E_OUTOFMEMORY
;
932 hres
= init_dispex(ret
, ctx
, builtin_info
? builtin_info
: &dispex_info
, prototype
);
942 void jsdisp_free(jsdisp_t
*obj
)
946 TRACE("(%p)\n", obj
);
948 for(prop
= obj
->props
; prop
< obj
->props
+obj
->prop_cnt
; prop
++) {
949 if(prop
->type
== PROP_JSVAL
)
950 jsval_release(prop
->u
.val
);
951 heap_free(prop
->name
);
953 heap_free(obj
->props
);
954 script_release(obj
->ctx
);
956 jsdisp_release(obj
->prototype
);
958 if(obj
->builtin_info
->destructor
)
959 obj
->builtin_info
->destructor(obj
);
966 jsdisp_t
*jsdisp_addref(jsdisp_t
*jsdisp
)
968 ULONG ref
= ++jsdisp
->ref
;
969 TRACE("(%p) ref=%d\n", jsdisp
, ref
);
973 void jsdisp_release(jsdisp_t
*jsdisp
)
975 ULONG ref
= --jsdisp
->ref
;
977 TRACE("(%p) ref=%d\n", jsdisp
, ref
);
985 HRESULT
init_dispex_from_constr(jsdisp_t
*dispex
, script_ctx_t
*ctx
, const builtin_info_t
*builtin_info
, jsdisp_t
*constr
)
987 jsdisp_t
*prot
= NULL
;
991 static const WCHAR prototypeW
[] = {'p','r','o','t','o','t','y','p','e',0};
993 hres
= find_prop_name_prot(constr
, string_hash(prototypeW
), prototypeW
, &prop
);
994 if(SUCCEEDED(hres
) && prop
&& prop
->type
!=PROP_DELETED
) {
997 hres
= prop_get(constr
, prop
, NULL
, &val
, NULL
);
999 ERR("Could not get prototype\n");
1003 if(is_object_instance(val
))
1004 prot
= iface_to_jsdisp(get_object(val
));
1008 hres
= init_dispex(dispex
, ctx
, builtin_info
, prot
);
1011 jsdisp_release(prot
);
1015 jsdisp_t
*iface_to_jsdisp(IDispatch
*iface
)
1017 return iface
->lpVtbl
== (const IDispatchVtbl
*)&DispatchExVtbl
1018 ? jsdisp_addref( impl_from_IDispatchEx((IDispatchEx
*)iface
))
1022 HRESULT
jsdisp_get_id(jsdisp_t
*jsdisp
, const WCHAR
*name
, DWORD flags
, DISPID
*id
)
1024 dispex_prop_t
*prop
;
1027 if(flags
& fdexNameEnsure
)
1028 hres
= ensure_prop_name(jsdisp
, name
, TRUE
, PROPF_ENUM
, &prop
);
1030 hres
= find_prop_name_prot(jsdisp
, string_hash(name
), name
, &prop
);
1034 if(prop
&& prop
->type
!=PROP_DELETED
) {
1035 *id
= prop_to_id(jsdisp
, prop
);
1039 TRACE("not found %s\n", debugstr_w(name
));
1040 return DISP_E_UNKNOWNNAME
;
1043 HRESULT
jsdisp_call_value(jsdisp_t
*jsfunc
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1047 assert(!(flags
& ~(DISPATCH_METHOD
|DISPATCH_CONSTRUCT
|DISPATCH_JSCRIPT_INTERNAL_MASK
)));
1049 if(is_class(jsfunc
, JSCLASS_FUNCTION
)) {
1050 hres
= Function_invoke(jsfunc
, jsthis
, flags
, argc
, argv
, r
);
1054 if(!jsfunc
->builtin_info
->value_prop
.invoke
) {
1055 WARN("Not a function\n");
1056 return throw_type_error(jsfunc
->ctx
, JS_E_FUNCTION_EXPECTED
, NULL
);
1059 set_disp(&vdisp
, jsthis
);
1060 flags
&= ~DISPATCH_JSCRIPT_INTERNAL_MASK
;
1061 hres
= jsfunc
->builtin_info
->value_prop
.invoke(jsfunc
->ctx
, &vdisp
, flags
, argc
, argv
, r
);
1062 vdisp_release(&vdisp
);
1067 HRESULT
jsdisp_call(jsdisp_t
*disp
, DISPID id
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1069 dispex_prop_t
*prop
;
1071 prop
= get_prop(disp
, id
);
1073 return DISP_E_MEMBERNOTFOUND
;
1075 return invoke_prop_func(disp
, to_disp(disp
), prop
, flags
, argc
, argv
, r
, NULL
);
1078 HRESULT
jsdisp_call_name(jsdisp_t
*disp
, const WCHAR
*name
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1080 dispex_prop_t
*prop
;
1083 hres
= find_prop_name_prot(disp
, string_hash(name
), name
, &prop
);
1087 return invoke_prop_func(disp
, to_disp(disp
), prop
, flags
, argc
, argv
, r
, NULL
);
1090 HRESULT
disp_call(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*ret
)
1092 IDispatchEx
*dispex
;
1094 VARIANT buf
[6], retv
;
1099 jsdisp
= iface_to_jsdisp(disp
);
1101 if(flags
& DISPATCH_PROPERTYPUT
) {
1102 FIXME("disp_call(propput) on builtin object\n");
1106 hres
= jsdisp_call(jsdisp
, id
, flags
, argc
, argv
, ret
);
1107 jsdisp_release(jsdisp
);
1111 flags
&= ~DISPATCH_JSCRIPT_INTERNAL_MASK
;
1113 flags
|= DISPATCH_PROPERTYGET
;
1117 if(flags
& DISPATCH_PROPERTYPUT
) {
1118 static DISPID propput_dispid
= DISPID_PROPERTYPUT
;
1121 dp
.rgdispidNamedArgs
= &propput_dispid
;
1124 dp
.rgdispidNamedArgs
= NULL
;
1128 dp
.rgvarg
= heap_alloc(argc
*sizeof(VARIANT
));
1130 return E_OUTOFMEMORY
;
1135 for(i
=0; i
<argc
; i
++) {
1136 hres
= jsval_to_variant(argv
[i
], dp
.rgvarg
+argc
-i
-1);
1139 VariantClear(dp
.rgvarg
+argc
-i
-1);
1140 if(dp
.rgvarg
!= buf
)
1141 heap_free(dp
.rgvarg
);
1146 V_VT(&retv
) = VT_EMPTY
;
1148 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1149 if(SUCCEEDED(hres
)) {
1150 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, &dp
, ret
? &retv
: NULL
, &ctx
->ei
.ei
,
1151 &ctx
->jscaller
->IServiceProvider_iface
);
1152 IDispatchEx_Release(dispex
);
1156 if(flags
== DISPATCH_CONSTRUCT
) {
1157 WARN("IDispatch cannot be constructor\n");
1158 return DISP_E_MEMBERNOTFOUND
;
1161 TRACE("using IDispatch\n");
1162 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, &dp
, ret
? &retv
: NULL
, &ctx
->ei
.ei
, &err
);
1165 for(i
=0; i
<argc
; i
++)
1166 VariantClear(dp
.rgvarg
+argc
-i
-1);
1167 if(dp
.rgvarg
!= buf
)
1168 heap_free(dp
.rgvarg
);
1173 hres
= variant_to_jsval(&retv
, ret
);
1174 VariantClear(&retv
);
1180 HRESULT
disp_call_value(script_ctx_t
*ctx
, IDispatch
*disp
, IDispatch
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1184 IDispatchEx
*dispex
;
1185 VARIANT buf
[6], retv
;
1190 assert(!(flags
& ~(DISPATCH_METHOD
|DISPATCH_CONSTRUCT
|DISPATCH_JSCRIPT_INTERNAL_MASK
)));
1192 jsdisp
= iface_to_jsdisp(disp
);
1194 hres
= jsdisp_call_value(jsdisp
, jsthis
, flags
, argc
, argv
, r
);
1195 jsdisp_release(jsdisp
);
1199 flags
&= ~DISPATCH_JSCRIPT_INTERNAL_MASK
;
1200 if(r
&& argc
&& flags
== DISPATCH_METHOD
)
1201 flags
|= DISPATCH_PROPERTYGET
;
1203 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1205 TRACE("using IDispatch\n");
1211 static DISPID this_id
= DISPID_THIS
;
1215 dp
.rgdispidNamedArgs
= &this_id
;
1219 dp
.rgdispidNamedArgs
= NULL
;
1222 if(dp
.cArgs
> sizeof(buf
)/sizeof(*buf
)) {
1223 dp
.rgvarg
= heap_alloc(dp
.cArgs
*sizeof(VARIANT
));
1226 IDispatchEx_Release(dispex
);
1227 return E_OUTOFMEMORY
;
1233 for(i
=0; i
<argc
; i
++) {
1234 hres
= jsval_to_variant(argv
[i
], dp
.rgvarg
+dp
.cArgs
-i
-1);
1237 VariantClear(dp
.rgvarg
+dp
.cArgs
-i
-1);
1238 if(dp
.rgvarg
!= buf
)
1239 heap_free(dp
.rgvarg
);
1241 IDispatchEx_Release(dispex
);
1246 V_VT(dp
.rgvarg
) = VT_DISPATCH
;
1247 V_DISPATCH(dp
.rgvarg
) = jsthis
;
1250 V_VT(&retv
) = VT_EMPTY
;
1253 hres
= IDispatchEx_InvokeEx(dispex
, DISPID_VALUE
, ctx
->lcid
, flags
, &dp
, r
? &retv
: NULL
, &ctx
->ei
.ei
,
1254 &ctx
->jscaller
->IServiceProvider_iface
);
1255 IDispatchEx_Release(dispex
);
1259 if(flags
== DISPATCH_CONSTRUCT
) {
1260 WARN("IDispatch cannot be constructor\n");
1261 return DISP_E_MEMBERNOTFOUND
;
1264 hres
= IDispatch_Invoke(disp
, DISPID_VALUE
, &IID_NULL
, ctx
->lcid
, flags
, &dp
, r
? &retv
: NULL
, &ctx
->ei
.ei
, &err
);
1267 for(i
=0; i
<argc
; i
++)
1268 VariantClear(dp
.rgvarg
+dp
.cArgs
-i
-1);
1269 if(dp
.rgvarg
!= buf
)
1270 heap_free(dp
.rgvarg
);
1277 hres
= variant_to_jsval(&retv
, r
);
1278 VariantClear(&retv
);
1282 HRESULT
jsdisp_propput(jsdisp_t
*obj
, const WCHAR
*name
, DWORD flags
, jsval_t val
)
1284 dispex_prop_t
*prop
;
1287 hres
= ensure_prop_name(obj
, name
, FALSE
, flags
, &prop
);
1291 return prop_put(obj
, prop
, val
, NULL
);
1294 HRESULT
jsdisp_propput_name(jsdisp_t
*obj
, const WCHAR
*name
, jsval_t val
)
1296 return jsdisp_propput(obj
, name
, PROPF_ENUM
, val
);
1299 HRESULT
jsdisp_propput_const(jsdisp_t
*obj
, const WCHAR
*name
, jsval_t val
)
1301 dispex_prop_t
*prop
;
1304 hres
= ensure_prop_name(obj
, name
, FALSE
, PROPF_CONST
, &prop
);
1308 return jsval_copy(val
, &prop
->u
.val
);
1311 HRESULT
jsdisp_propput_dontenum(jsdisp_t
*obj
, const WCHAR
*name
, jsval_t val
)
1313 return jsdisp_propput(obj
, name
, 0, val
);
1316 HRESULT
jsdisp_propput_idx(jsdisp_t
*obj
, DWORD idx
, jsval_t val
)
1320 static const WCHAR formatW
[] = {'%','d',0};
1322 sprintfW(buf
, formatW
, idx
);
1323 return jsdisp_propput_name(obj
, buf
, val
);
1326 HRESULT
disp_propput(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, jsval_t val
)
1331 jsdisp
= iface_to_jsdisp(disp
);
1333 dispex_prop_t
*prop
;
1335 prop
= get_prop(jsdisp
, id
);
1337 hres
= prop_put(jsdisp
, prop
, val
, NULL
);
1339 hres
= DISP_E_MEMBERNOTFOUND
;
1341 jsdisp_release(jsdisp
);
1343 DISPID dispid
= DISPID_PROPERTYPUT
;
1344 DWORD flags
= DISPATCH_PROPERTYPUT
;
1346 DISPPARAMS dp
= {&var
, &dispid
, 1, 1};
1347 IDispatchEx
*dispex
;
1349 hres
= jsval_to_variant(val
, &var
);
1353 if(V_VT(&var
) == VT_DISPATCH
)
1354 flags
|= DISPATCH_PROPERTYPUTREF
;
1357 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1358 if(SUCCEEDED(hres
)) {
1359 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, flags
, &dp
, NULL
, &ctx
->ei
.ei
,
1360 &ctx
->jscaller
->IServiceProvider_iface
);
1361 IDispatchEx_Release(dispex
);
1365 TRACE("using IDispatch\n");
1366 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, flags
, &dp
, NULL
, &ctx
->ei
.ei
, &err
);
1375 HRESULT
jsdisp_propget_name(jsdisp_t
*obj
, const WCHAR
*name
, jsval_t
*val
)
1377 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1378 dispex_prop_t
*prop
;
1381 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1385 if(!prop
|| prop
->type
==PROP_DELETED
) {
1386 *val
= jsval_undefined();
1390 return prop_get(obj
, prop
, &dp
, val
, NULL
);
1393 HRESULT
jsdisp_get_idx(jsdisp_t
*obj
, DWORD idx
, jsval_t
*r
)
1396 DISPPARAMS dp
= {NULL
, NULL
, 0, 0};
1397 dispex_prop_t
*prop
;
1400 static const WCHAR formatW
[] = {'%','d',0};
1402 sprintfW(name
, formatW
, idx
);
1404 hres
= find_prop_name_prot(obj
, string_hash(name
), name
, &prop
);
1408 if(!prop
|| prop
->type
==PROP_DELETED
) {
1409 *r
= jsval_undefined();
1410 return DISP_E_UNKNOWNNAME
;
1413 return prop_get(obj
, prop
, &dp
, r
, NULL
);
1416 HRESULT
jsdisp_propget(jsdisp_t
*jsdisp
, DISPID id
, jsval_t
*val
)
1418 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1419 dispex_prop_t
*prop
;
1421 prop
= get_prop(jsdisp
, id
);
1423 return DISP_E_MEMBERNOTFOUND
;
1425 return prop_get(jsdisp
, prop
, &dp
, val
, NULL
);
1428 HRESULT
disp_propget(script_ctx_t
*ctx
, IDispatch
*disp
, DISPID id
, jsval_t
*val
)
1430 DISPPARAMS dp
= {NULL
,NULL
,0,0};
1431 IDispatchEx
*dispex
;
1436 jsdisp
= iface_to_jsdisp(disp
);
1438 hres
= jsdisp_propget(jsdisp
, id
, val
);
1439 jsdisp_release(jsdisp
);
1443 V_VT(&var
) = VT_EMPTY
;
1445 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1446 if(SUCCEEDED(hres
)) {
1447 hres
= IDispatchEx_InvokeEx(dispex
, id
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, &var
, &ctx
->ei
.ei
,
1448 &ctx
->jscaller
->IServiceProvider_iface
);
1449 IDispatchEx_Release(dispex
);
1453 TRACE("using IDispatch\n");
1454 hres
= IDispatch_Invoke(disp
, id
, &IID_NULL
, ctx
->lcid
, INVOKE_PROPERTYGET
, &dp
, &var
, &ctx
->ei
.ei
, &err
);
1459 hres
= variant_to_jsval(&var
, val
);
1464 HRESULT
jsdisp_delete_idx(jsdisp_t
*obj
, DWORD idx
)
1466 static const WCHAR formatW
[] = {'%','d',0};
1468 dispex_prop_t
*prop
;
1472 sprintfW(buf
, formatW
, idx
);
1474 hres
= find_prop_name(obj
, string_hash(buf
), buf
, &prop
);
1475 if(FAILED(hres
) || !prop
)
1478 return delete_prop(prop
, &b
);
1481 HRESULT
disp_delete(IDispatch
*disp
, DISPID id
, BOOL
*ret
)
1483 IDispatchEx
*dispex
;
1487 jsdisp
= iface_to_jsdisp(disp
);
1489 dispex_prop_t
*prop
;
1491 prop
= get_prop(jsdisp
, id
);
1493 hres
= delete_prop(prop
, ret
);
1495 hres
= DISP_E_MEMBERNOTFOUND
;
1497 jsdisp_release(jsdisp
);
1501 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1507 hres
= IDispatchEx_DeleteMemberByDispID(dispex
, id
);
1508 IDispatchEx_Release(dispex
);
1512 *ret
= hres
== S_OK
;
1516 HRESULT
disp_delete_name(script_ctx_t
*ctx
, IDispatch
*disp
, jsstr_t
*name
, BOOL
*ret
)
1518 IDispatchEx
*dispex
;
1523 jsdisp
= iface_to_jsdisp(disp
);
1525 dispex_prop_t
*prop
;
1528 ptr
= jsstr_flatten(name
);
1530 jsdisp_release(jsdisp
);
1531 return E_OUTOFMEMORY
;
1534 hres
= find_prop_name(jsdisp
, string_hash(ptr
), ptr
, &prop
);
1536 hres
= delete_prop(prop
, ret
);
1542 jsdisp_release(jsdisp
);
1546 bstr
= SysAllocStringLen(NULL
, jsstr_length(name
));
1548 return E_OUTOFMEMORY
;
1549 jsstr_flush(name
, bstr
);
1551 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1552 if(SUCCEEDED(hres
)) {
1553 hres
= IDispatchEx_DeleteMemberByName(dispex
, bstr
, make_grfdex(ctx
, fdexNameCaseSensitive
));
1555 *ret
= hres
== S_OK
;
1556 IDispatchEx_Release(dispex
);
1560 hres
= IDispatch_GetIDsOfNames(disp
, &IID_NULL
, &bstr
, 1, 0, &id
);
1561 if(SUCCEEDED(hres
)) {
1562 /* Property exists and we can't delete it from pure IDispatch interface, so return false. */
1564 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1565 /* Property doesn't exist, so nothing to delete */
1571 SysFreeString(bstr
);
1575 HRESULT
jsdisp_is_own_prop(jsdisp_t
*obj
, const WCHAR
*name
, BOOL
*ret
)
1577 dispex_prop_t
*prop
;
1580 hres
= find_prop_name(obj
, string_hash(name
), name
, &prop
);
1584 *ret
= prop
&& (prop
->type
== PROP_JSVAL
|| prop
->type
== PROP_BUILTIN
);
1588 HRESULT
jsdisp_is_enumerable(jsdisp_t
*obj
, const WCHAR
*name
, BOOL
*ret
)
1590 dispex_prop_t
*prop
;
1593 hres
= find_prop_name(obj
, string_hash(name
), name
, &prop
);
1597 *ret
= prop
&& (prop
->flags
& PROPF_ENUM
) && prop
->type
!= PROP_PROTREF
;