include/wine/test.h: Fix tests compilation with a PSDK compiler.
[wine/multimedia.git] / dlls / jscript / dispex.c
blobf214ea06c5c1b8e54bdcbc70bd581020cf25e534
1 /*
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
19 #include <assert.h>
21 #include "jscript.h"
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
38 typedef enum {
39 PROP_VARIANT,
40 PROP_BUILTIN,
41 PROP_PROTREF,
42 PROP_DELETED
43 } prop_type_t;
45 struct _dispex_prop_t {
46 WCHAR *name;
47 unsigned hash;
48 prop_type_t type;
49 DWORD flags;
51 union {
52 VARIANT var;
53 const builtin_prop_t *p;
54 DWORD ref;
55 } u;
57 int bucket_head;
58 int bucket_next;
61 static inline DISPID prop_to_id(jsdisp_t *This, dispex_prop_t *prop)
63 return prop - This->props;
66 static inline dispex_prop_t *get_prop(jsdisp_t *This, DISPID id)
68 if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
69 return NULL;
71 return This->props+id;
74 static DWORD get_flags(jsdisp_t *This, dispex_prop_t *prop)
76 if(prop->type == PROP_PROTREF) {
77 dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
78 if(!parent) {
79 prop->type = PROP_DELETED;
80 return 0;
83 return get_flags(This->prototype, parent);
86 return prop->flags;
89 static const builtin_prop_t *find_builtin_prop(jsdisp_t *This, const WCHAR *name)
91 int min = 0, max, i, r;
93 max = This->builtin_info->props_cnt-1;
94 while(min <= max) {
95 i = (min+max)/2;
97 r = strcmpW(name, This->builtin_info->props[i].name);
98 if(!r)
99 return This->builtin_info->props + i;
101 if(r < 0)
102 max = i-1;
103 else
104 min = i+1;
107 return NULL;
110 static inline unsigned string_hash(const WCHAR *name)
112 unsigned h = 0;
113 for(; *name; name++)
114 h = (h>>(sizeof(unsigned)*8-4)) ^ (h<<4) ^ tolowerW(*name);
115 return h;
118 static inline unsigned get_props_idx(jsdisp_t *This, unsigned hash)
120 return (hash*GOLDEN_RATIO) & (This->buf_size-1);
123 static inline HRESULT resize_props(jsdisp_t *This)
125 dispex_prop_t *props;
126 int i, bucket;
128 if(This->buf_size != This->prop_cnt)
129 return S_FALSE;
131 props = heap_realloc(This->props, sizeof(dispex_prop_t)*This->buf_size*2);
132 if(!props)
133 return E_OUTOFMEMORY;
134 This->buf_size *= 2;
135 This->props = props;
137 for(i=0; i<This->buf_size; i++) {
138 This->props[i].bucket_head = 0;
139 This->props[i].bucket_next = 0;
142 for(i=1; i<This->prop_cnt; i++) {
143 props = This->props+i;
145 bucket = get_props_idx(This, props->hash);
146 props->bucket_next = This->props[bucket].bucket_head;
147 This->props[bucket].bucket_head = i;
150 return S_OK;
153 static inline dispex_prop_t* alloc_prop(jsdisp_t *This, const WCHAR *name, prop_type_t type, DWORD flags)
155 dispex_prop_t *prop;
156 unsigned bucket;
158 if(FAILED(resize_props(This)))
159 return NULL;
161 prop = &This->props[This->prop_cnt];
162 prop->name = heap_strdupW(name);
163 if(!prop->name)
164 return NULL;
165 prop->type = type;
166 prop->flags = flags;
167 prop->hash = string_hash(name);
169 bucket = get_props_idx(This, prop->hash);
170 prop->bucket_next = This->props[bucket].bucket_head;
171 This->props[bucket].bucket_head = This->prop_cnt++;
172 return prop;
175 static dispex_prop_t *alloc_protref(jsdisp_t *This, const WCHAR *name, DWORD ref)
177 dispex_prop_t *ret;
179 ret = alloc_prop(This, name, PROP_PROTREF, 0);
180 if(!ret)
181 return NULL;
183 ret->u.ref = ref;
184 return ret;
187 static HRESULT find_prop_name(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
189 const builtin_prop_t *builtin;
190 unsigned bucket, pos, prev = 0;
191 dispex_prop_t *prop;
193 bucket = get_props_idx(This, hash);
194 pos = This->props[bucket].bucket_head;
195 while(pos != 0) {
196 if(!strcmpW(name, This->props[pos].name)) {
197 if(prev != 0) {
198 This->props[prev].bucket_next = This->props[pos].bucket_next;
199 This->props[pos].bucket_next = This->props[bucket].bucket_head;
200 This->props[bucket].bucket_head = pos;
203 *ret = &This->props[pos];
204 return S_OK;
207 prev = pos;
208 pos = This->props[pos].bucket_next;
211 builtin = find_builtin_prop(This, name);
212 if(builtin) {
213 prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
214 if(!prop)
215 return E_OUTOFMEMORY;
217 prop->u.p = builtin;
218 *ret = prop;
219 return S_OK;
222 *ret = NULL;
223 return S_OK;
226 static HRESULT find_prop_name_prot(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
228 dispex_prop_t *prop, *del=NULL;
229 HRESULT hres;
231 hres = find_prop_name(This, hash, name, &prop);
232 if(FAILED(hres))
233 return hres;
234 if(prop && prop->type==PROP_DELETED) {
235 del = prop;
236 } else if(prop) {
237 *ret = prop;
238 return S_OK;
241 if(This->prototype) {
242 hres = find_prop_name_prot(This->prototype, hash, name, &prop);
243 if(FAILED(hres))
244 return hres;
245 if(prop) {
246 if(del) {
247 del->type = PROP_PROTREF;
248 del->flags = 0;
249 del->u.ref = prop - This->prototype->props;
250 prop = del;
251 }else {
252 prop = alloc_protref(This, prop->name, prop - This->prototype->props);
253 if(!prop)
254 return E_OUTOFMEMORY;
257 *ret = prop;
258 return S_OK;
262 *ret = del;
263 return S_OK;
266 static HRESULT ensure_prop_name(jsdisp_t *This, const WCHAR *name, BOOL search_prot, DWORD create_flags, dispex_prop_t **ret)
268 dispex_prop_t *prop;
269 HRESULT hres;
271 if(search_prot)
272 hres = find_prop_name_prot(This, string_hash(name), name, &prop);
273 else
274 hres = find_prop_name(This, string_hash(name), name, &prop);
275 if(SUCCEEDED(hres) && (!prop || prop->type==PROP_DELETED)) {
276 TRACE("creating prop %s\n", debugstr_w(name));
278 if(prop) {
279 prop->type = PROP_VARIANT;
280 prop->flags = create_flags;
281 }else {
282 prop = alloc_prop(This, name, PROP_VARIANT, create_flags);
283 if(!prop)
284 return E_OUTOFMEMORY;
287 VariantInit(&prop->u.var);
290 *ret = prop;
291 return hres;
294 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
296 VARIANTARG *oldargs;
297 int i;
299 static DISPID this_id = DISPID_THIS;
301 *dp = *olddp;
303 for(i = 0; i < dp->cNamedArgs; i++) {
304 if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
305 return S_OK;
308 oldargs = dp->rgvarg;
309 dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
310 if(!dp->rgvarg)
311 return E_OUTOFMEMORY;
312 memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
313 V_VT(dp->rgvarg) = VT_DISPATCH;
314 V_DISPATCH(dp->rgvarg) = jsthis;
315 dp->cArgs++;
317 if(dp->cNamedArgs) {
318 DISPID *old = dp->rgdispidNamedArgs;
319 dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
320 if(!dp->rgdispidNamedArgs) {
321 heap_free(dp->rgvarg);
322 return E_OUTOFMEMORY;
325 memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
326 dp->rgdispidNamedArgs[0] = DISPID_THIS;
327 dp->cNamedArgs++;
328 }else {
329 dp->rgdispidNamedArgs = &this_id;
330 dp->cNamedArgs = 1;
333 return S_OK;
336 static HRESULT invoke_prop_func(jsdisp_t *This, jsdisp_t *jsthis, dispex_prop_t *prop, WORD flags,
337 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
339 HRESULT hres;
341 switch(prop->type) {
342 case PROP_BUILTIN: {
343 vdisp_t vthis;
345 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
346 WARN("%s is not a constructor\n", debugstr_w(prop->name));
347 return E_INVALIDARG;
350 set_jsdisp(&vthis, jsthis);
351 hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei);
352 vdisp_release(&vthis);
353 return hres;
355 case PROP_PROTREF:
356 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
357 case PROP_VARIANT: {
358 DISPPARAMS new_dp;
360 if(V_VT(&prop->u.var) != VT_DISPATCH) {
361 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
362 return E_FAIL;
365 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
367 hres = set_this(&new_dp, dp, to_disp(jsthis));
368 if(FAILED(hres))
369 return hres;
371 hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei);
373 if(new_dp.rgvarg != dp->rgvarg) {
374 heap_free(new_dp.rgvarg);
375 if(new_dp.cNamedArgs > 1)
376 heap_free(new_dp.rgdispidNamedArgs);
379 return hres;
381 default:
382 ERR("type %d\n", prop->type);
385 return E_FAIL;
388 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
389 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
391 HRESULT hres;
393 switch(prop->type) {
394 case PROP_BUILTIN:
395 if(prop->u.p->flags & PROPF_METHOD) {
396 jsdisp_t *obj;
397 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
398 prop->u.p->flags, NULL, &obj);
399 if(FAILED(hres))
400 break;
402 prop->type = PROP_VARIANT;
403 var_set_jsdisp(&prop->u.var, obj);
404 hres = VariantCopy(retv, &prop->u.var);
405 }else {
406 vdisp_t vthis;
408 set_jsdisp(&vthis, This);
409 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei);
410 vdisp_release(&vthis);
412 break;
413 case PROP_PROTREF:
414 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
415 break;
416 case PROP_VARIANT:
417 hres = VariantCopy(retv, &prop->u.var);
418 break;
419 default:
420 ERR("type %d\n", prop->type);
421 return E_FAIL;
424 if(FAILED(hres)) {
425 TRACE("fail %08x\n", hres);
426 return hres;
429 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
430 return hres;
433 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
434 jsexcept_t *ei, IServiceProvider *caller)
436 HRESULT hres;
438 if(prop->flags & PROPF_CONST)
439 return S_OK;
441 switch(prop->type) {
442 case PROP_BUILTIN:
443 if(!(prop->flags & PROPF_METHOD)) {
444 DISPPARAMS dp = {val, NULL, 1, 0};
445 vdisp_t vthis;
447 set_jsdisp(&vthis, This);
448 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei);
449 vdisp_release(&vthis);
450 return hres;
452 case PROP_PROTREF:
453 prop->type = PROP_VARIANT;
454 prop->flags = PROPF_ENUM;
455 V_VT(&prop->u.var) = VT_EMPTY;
456 break;
457 case PROP_VARIANT:
458 VariantClear(&prop->u.var);
459 break;
460 default:
461 ERR("type %d\n", prop->type);
462 return E_FAIL;
465 hres = VariantCopy(&prop->u.var, val);
466 if(FAILED(hres))
467 return hres;
469 if(This->builtin_info->on_put)
470 This->builtin_info->on_put(This, prop->name);
472 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
473 return S_OK;
476 static HRESULT fill_protrefs(jsdisp_t *This)
478 dispex_prop_t *iter, *prop;
479 HRESULT hres;
481 if(!This->prototype)
482 return S_OK;
484 fill_protrefs(This->prototype);
486 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
487 if(!iter->name)
488 continue;
489 hres = find_prop_name(This, iter->hash, iter->name, &prop);
490 if(FAILED(hres))
491 return hres;
492 if(!prop || prop->type==PROP_DELETED) {
493 if(prop) {
494 prop->type = PROP_PROTREF;
495 prop->flags = 0;
496 prop->u.ref = iter - This->prototype->props;
497 }else {
498 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
499 if(!prop)
500 return E_OUTOFMEMORY;
505 return S_OK;
508 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
510 return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
513 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
515 jsdisp_t *This = impl_from_IDispatchEx(iface);
517 if(IsEqualGUID(&IID_IUnknown, riid)) {
518 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
519 *ppv = &This->IDispatchEx_iface;
520 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
521 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
522 *ppv = &This->IDispatchEx_iface;
523 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
524 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
525 *ppv = &This->IDispatchEx_iface;
526 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
527 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
528 jsdisp_addref(This);
529 *ppv = This;
530 return S_OK;
531 }else {
532 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
533 *ppv = NULL;
534 return E_NOINTERFACE;
537 IUnknown_AddRef((IUnknown*)*ppv);
538 return S_OK;
541 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
543 jsdisp_t *This = impl_from_IDispatchEx(iface);
544 LONG ref = InterlockedIncrement(&This->ref);
546 TRACE("(%p) ref=%d\n", This, ref);
548 return ref;
551 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
553 jsdisp_t *This = impl_from_IDispatchEx(iface);
554 LONG ref = InterlockedDecrement(&This->ref);
556 TRACE("(%p) ref=%d\n", This, ref);
558 if(!ref) {
559 dispex_prop_t *prop;
561 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
562 if(prop->type == PROP_VARIANT)
563 VariantClear(&prop->u.var);
564 heap_free(prop->name);
566 heap_free(This->props);
567 script_release(This->ctx);
568 if(This->prototype)
569 jsdisp_release(This->prototype);
571 if(This->builtin_info->destructor)
572 This->builtin_info->destructor(This);
573 else
574 heap_free(This);
577 return ref;
580 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
582 jsdisp_t *This = impl_from_IDispatchEx(iface);
584 TRACE("(%p)->(%p)\n", This, pctinfo);
586 *pctinfo = 1;
587 return S_OK;
590 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
591 ITypeInfo **ppTInfo)
593 jsdisp_t *This = impl_from_IDispatchEx(iface);
594 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
595 return E_NOTIMPL;
598 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
599 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
600 DISPID *rgDispId)
602 jsdisp_t *This = impl_from_IDispatchEx(iface);
603 UINT i;
604 HRESULT hres;
606 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
607 lcid, rgDispId);
609 for(i=0; i < cNames; i++) {
610 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
611 if(FAILED(hres))
612 return hres;
615 return S_OK;
618 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
619 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
620 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
622 jsdisp_t *This = impl_from_IDispatchEx(iface);
624 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
625 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
627 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
628 pDispParams, pVarResult, pExcepInfo, NULL);
631 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
633 jsdisp_t *This = impl_from_IDispatchEx(iface);
635 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
637 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
638 FIXME("Unsupported grfdex %x\n", grfdex);
639 return E_NOTIMPL;
642 return jsdisp_get_id(This, bstrName, grfdex, pid);
645 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
646 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
648 jsdisp_t *This = impl_from_IDispatchEx(iface);
649 dispex_prop_t *prop;
650 jsexcept_t jsexcept;
651 HRESULT hres;
653 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
655 if(pvarRes)
656 V_VT(pvarRes) = VT_EMPTY;
658 prop = get_prop(This, id);
659 if(!prop || prop->type == PROP_DELETED) {
660 TRACE("invalid id\n");
661 return DISP_E_MEMBERNOTFOUND;
664 memset(&jsexcept, 0, sizeof(jsexcept));
666 switch(wFlags) {
667 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
668 wFlags = DISPATCH_METHOD;
669 /* fall through */
670 case DISPATCH_METHOD:
671 case DISPATCH_CONSTRUCT:
672 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
673 break;
674 case DISPATCH_PROPERTYGET:
675 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
676 break;
677 case DISPATCH_PROPERTYPUT: {
678 DWORD i;
680 for(i=0; i < pdp->cNamedArgs; i++) {
681 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
682 break;
685 if(i == pdp->cNamedArgs) {
686 TRACE("no value to set\n");
687 return DISP_E_PARAMNOTOPTIONAL;
690 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
691 break;
693 default:
694 FIXME("Unimplemented flags %x\n", wFlags);
695 return E_INVALIDARG;
698 if(pei)
699 *pei = jsexcept.ei;
701 return hres;
704 static HRESULT delete_prop(dispex_prop_t *prop)
706 if(prop->type == PROP_VARIANT) {
707 VariantClear(&prop->u.var);
708 prop->type = PROP_DELETED;
710 return S_OK;
713 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
715 jsdisp_t *This = impl_from_IDispatchEx(iface);
716 dispex_prop_t *prop;
717 HRESULT hres;
719 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
721 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
722 FIXME("Unsupported grfdex %x\n", grfdex);
724 hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
725 if(FAILED(hres))
726 return hres;
727 if(!prop) {
728 TRACE("not found\n");
729 return S_OK;
732 return delete_prop(prop);
735 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
737 jsdisp_t *This = impl_from_IDispatchEx(iface);
738 dispex_prop_t *prop;
740 TRACE("(%p)->(%x)\n", This, id);
742 prop = get_prop(This, id);
743 if(!prop) {
744 WARN("invalid id\n");
745 return DISP_E_MEMBERNOTFOUND;
748 return delete_prop(prop);
751 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
753 jsdisp_t *This = impl_from_IDispatchEx(iface);
754 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
755 return E_NOTIMPL;
758 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
760 jsdisp_t *This = impl_from_IDispatchEx(iface);
761 dispex_prop_t *prop;
763 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
765 prop = get_prop(This, id);
766 if(!prop || !prop->name || prop->type == PROP_DELETED)
767 return DISP_E_MEMBERNOTFOUND;
769 *pbstrName = SysAllocString(prop->name);
770 if(!*pbstrName)
771 return E_OUTOFMEMORY;
773 return S_OK;
776 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
778 jsdisp_t *This = impl_from_IDispatchEx(iface);
779 dispex_prop_t *iter;
780 HRESULT hres;
782 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
784 if(id == DISPID_STARTENUM) {
785 hres = fill_protrefs(This);
786 if(FAILED(hres))
787 return hres;
790 if(id+1>=0 && id+1<This->prop_cnt) {
791 iter = &This->props[id+1];
792 }else {
793 *pid = DISPID_STARTENUM;
794 return S_FALSE;
797 while(iter < This->props + This->prop_cnt) {
798 if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
799 *pid = prop_to_id(This, iter);
800 return S_OK;
802 iter++;
805 *pid = DISPID_STARTENUM;
806 return S_FALSE;
809 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
811 jsdisp_t *This = impl_from_IDispatchEx(iface);
812 FIXME("(%p)->(%p)\n", This, ppunk);
813 return E_NOTIMPL;
816 static IDispatchExVtbl DispatchExVtbl = {
817 DispatchEx_QueryInterface,
818 DispatchEx_AddRef,
819 DispatchEx_Release,
820 DispatchEx_GetTypeInfoCount,
821 DispatchEx_GetTypeInfo,
822 DispatchEx_GetIDsOfNames,
823 DispatchEx_Invoke,
824 DispatchEx_GetDispID,
825 DispatchEx_InvokeEx,
826 DispatchEx_DeleteMemberByName,
827 DispatchEx_DeleteMemberByDispID,
828 DispatchEx_GetMemberProperties,
829 DispatchEx_GetMemberName,
830 DispatchEx_GetNextDispID,
831 DispatchEx_GetNameSpaceParent
834 jsdisp_t *as_jsdisp(IDispatch *disp)
836 assert(disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl);
837 return impl_from_IDispatchEx((IDispatchEx*)disp);
840 jsdisp_t *to_jsdisp(IDispatch *disp)
842 return disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl ? impl_from_IDispatchEx((IDispatchEx*)disp) : NULL;
845 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
847 TRACE("%p (%p)\n", dispex, prototype);
849 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
850 dispex->ref = 1;
851 dispex->builtin_info = builtin_info;
853 dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
854 if(!dispex->props)
855 return E_OUTOFMEMORY;
857 dispex->prototype = prototype;
858 if(prototype)
859 jsdisp_addref(prototype);
861 dispex->prop_cnt = 1;
862 if(builtin_info->value_prop.invoke) {
863 dispex->props[0].type = PROP_BUILTIN;
864 dispex->props[0].u.p = &builtin_info->value_prop;
865 }else {
866 dispex->props[0].type = PROP_DELETED;
869 script_addref(ctx);
870 dispex->ctx = ctx;
872 return S_OK;
875 static const builtin_info_t dispex_info = {
876 JSCLASS_NONE,
877 {NULL, NULL, 0},
878 0, NULL,
879 NULL,
880 NULL
883 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
885 jsdisp_t *ret;
886 HRESULT hres;
888 ret = heap_alloc_zero(sizeof(jsdisp_t));
889 if(!ret)
890 return E_OUTOFMEMORY;
892 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
893 if(FAILED(hres))
894 return hres;
896 *dispex = ret;
897 return S_OK;
900 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
902 jsdisp_t *prot = NULL;
903 dispex_prop_t *prop;
904 HRESULT hres;
906 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
907 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
909 hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
910 if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
911 jsexcept_t jsexcept;
912 VARIANT var;
914 V_VT(&var) = VT_EMPTY;
915 memset(&jsexcept, 0, sizeof(jsexcept));
916 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
917 if(FAILED(hres)) {
918 ERR("Could not get prototype\n");
919 return hres;
922 if(V_VT(&var) == VT_DISPATCH)
923 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
924 VariantClear(&var);
927 hres = init_dispex(dispex, ctx, builtin_info, prot);
929 if(prot)
930 jsdisp_release(prot);
931 if(FAILED(hres))
932 return hres;
934 hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
935 if(SUCCEEDED(hres)) {
936 jsexcept_t jsexcept;
937 VARIANT var;
939 var_set_jsdisp(&var, constr);
940 memset(&jsexcept, 0, sizeof(jsexcept));
941 hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
943 if(FAILED(hres))
944 jsdisp_release(dispex);
946 return hres;
949 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
951 jsdisp_t *ret;
952 HRESULT hres;
954 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
955 if(FAILED(hres))
956 return NULL;
958 return ret;
961 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
963 dispex_prop_t *prop;
964 HRESULT hres;
966 if(flags & fdexNameEnsure)
967 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
968 else
969 hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
970 if(FAILED(hres))
971 return hres;
973 if(prop && prop->type!=PROP_DELETED) {
974 *id = prop_to_id(jsdisp, prop);
975 return S_OK;
978 TRACE("not found %s\n", debugstr_w(name));
979 return DISP_E_UNKNOWNNAME;
982 HRESULT jsdisp_call_value(jsdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
984 vdisp_t vdisp;
985 HRESULT hres;
987 set_jsdisp(&vdisp, jsthis);
988 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei);
989 vdisp_release(&vdisp);
990 return hres;
993 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
995 dispex_prop_t *prop;
997 memset(ei, 0, sizeof(*ei));
998 if(retv)
999 V_VT(retv) = VT_EMPTY;
1001 prop = get_prop(disp, id);
1002 if(!prop)
1003 return DISP_E_MEMBERNOTFOUND;
1005 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, NULL);
1008 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
1009 jsexcept_t *ei)
1011 dispex_prop_t *prop;
1012 HRESULT hres;
1014 hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1015 if(FAILED(hres))
1016 return hres;
1018 memset(ei, 0, sizeof(*ei));
1019 if(retv)
1020 V_VT(retv) = VT_EMPTY;
1022 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, NULL);
1025 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1027 jsdisp_t *jsdisp;
1028 IDispatchEx *dispex;
1029 HRESULT hres;
1031 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1032 if(jsdisp) {
1033 if(flags & DISPATCH_PROPERTYPUT) {
1034 FIXME("disp_call(propput) on builtin object\n");
1035 return E_FAIL;
1038 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei);
1039 jsdisp_release(jsdisp);
1040 return hres;
1043 memset(ei, 0, sizeof(*ei));
1044 if(retv && arg_cnt(dp))
1045 flags |= DISPATCH_PROPERTYGET;
1047 if(retv)
1048 V_VT(retv) = VT_EMPTY;
1049 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1050 if(FAILED(hres)) {
1051 UINT err = 0;
1053 if(flags == DISPATCH_CONSTRUCT) {
1054 WARN("IDispatch cannot be constructor\n");
1055 return DISP_E_MEMBERNOTFOUND;
1058 TRACE("using IDispatch\n");
1059 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
1062 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, &ctx->jscaller->IServiceProvider_iface);
1063 IDispatchEx_Release(dispex);
1065 return hres;
1068 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei)
1070 dispex_prop_t *prop;
1071 HRESULT hres;
1073 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1074 if(FAILED(hres))
1075 return hres;
1077 return prop_put(obj, prop, val, ei, NULL);
1080 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1082 dispex_prop_t *prop;
1083 HRESULT hres;
1085 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1086 if(FAILED(hres))
1087 return hres;
1089 return VariantCopy(&prop->u.var, val);
1092 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei)
1094 WCHAR buf[12];
1096 static const WCHAR formatW[] = {'%','d',0};
1098 sprintfW(buf, formatW, idx);
1099 return jsdisp_propput_name(obj, buf, val, ei);
1102 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1104 jsdisp_t *jsdisp;
1105 HRESULT hres;
1107 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1108 if(jsdisp) {
1109 dispex_prop_t *prop;
1111 prop = get_prop(jsdisp, id);
1112 if(prop)
1113 hres = prop_put(jsdisp, prop, val, ei, NULL);
1114 else
1115 hres = DISP_E_MEMBERNOTFOUND;
1117 jsdisp_release(jsdisp);
1118 }else {
1119 DISPID dispid = DISPID_PROPERTYPUT;
1120 DISPPARAMS dp = {val, &dispid, 1, 1};
1121 IDispatchEx *dispex;
1123 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1124 if(SUCCEEDED(hres)) {
1125 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei,
1126 &ctx->jscaller->IServiceProvider_iface);
1127 IDispatchEx_Release(dispex);
1128 }else {
1129 ULONG err = 0;
1131 TRACE("using IDispatch\n");
1132 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1136 return hres;
1139 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei)
1141 DISPPARAMS dp = {NULL, NULL, 0, 0};
1142 dispex_prop_t *prop;
1143 HRESULT hres;
1145 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1146 if(FAILED(hres))
1147 return hres;
1149 V_VT(var) = VT_EMPTY;
1150 if(!prop || prop->type==PROP_DELETED)
1151 return S_OK;
1153 return prop_get(obj, prop, &dp, var, ei, NULL);
1156 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei)
1158 WCHAR name[12];
1159 DISPPARAMS dp = {NULL, NULL, 0, 0};
1160 dispex_prop_t *prop;
1161 HRESULT hres;
1163 static const WCHAR formatW[] = {'%','d',0};
1165 sprintfW(name, formatW, idx);
1167 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1168 if(FAILED(hres))
1169 return hres;
1171 V_VT(var) = VT_EMPTY;
1172 if(!prop || prop->type==PROP_DELETED)
1173 return DISP_E_UNKNOWNNAME;
1175 return prop_get(obj, prop, &dp, var, ei, NULL);
1178 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei)
1180 DISPPARAMS dp = {NULL,NULL,0,0};
1181 dispex_prop_t *prop;
1183 prop = get_prop(jsdisp, id);
1184 if(!prop)
1185 return DISP_E_MEMBERNOTFOUND;
1187 V_VT(val) = VT_EMPTY;
1188 return prop_get(jsdisp, prop, &dp, val, ei, NULL);
1191 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1193 DISPPARAMS dp = {NULL,NULL,0,0};
1194 IDispatchEx *dispex;
1195 jsdisp_t *jsdisp;
1196 HRESULT hres;
1198 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1199 if(jsdisp) {
1200 hres = jsdisp_propget(jsdisp, id, val, ei);
1201 jsdisp_release(jsdisp);
1202 return hres;
1205 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1206 if(FAILED(hres)) {
1207 ULONG err = 0;
1209 TRACE("using IDispatch\n");
1210 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1213 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei,
1214 &ctx->jscaller->IServiceProvider_iface);
1215 IDispatchEx_Release(dispex);
1217 return hres;
1220 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1222 static const WCHAR formatW[] = {'%','d',0};
1223 WCHAR buf[12];
1224 dispex_prop_t *prop;
1225 HRESULT hres;
1227 sprintfW(buf, formatW, idx);
1229 hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1230 if(FAILED(hres) || !prop)
1231 return hres;
1233 return delete_prop(prop);
1236 VARIANT_BOOL jsdisp_is_own_prop(jsdisp_t *obj, BSTR name)
1238 dispex_prop_t *prop;
1239 HRESULT hres;
1241 hres = find_prop_name(obj, string_hash(name), name, &prop);
1242 if(FAILED(hres))
1243 return VARIANT_FALSE;
1244 else if(!prop)
1245 return VARIANT_FALSE;
1247 return prop->type==PROP_VARIANT ? VARIANT_TRUE : VARIANT_FALSE;