winealsa.drv: Only report endpoints for "default" if they can be opened.
[wine.git] / dlls / jscript / dispex.c
blob0d584f0e645f9662da8dd5a7427e00f26e3e2773
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 "jscript.h"
21 #include "wine/unicode.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27 * This IID is used to get jsdisp_t objecto from interface.
28 * We might consider using private insteface instead.
30 static const IID IID_IDispatchJS =
31 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
33 #define FDEX_VERSION_MASK 0xf0000000
34 #define GOLDEN_RATIO 0x9E3779B9U
36 typedef enum {
37 PROP_VARIANT,
38 PROP_BUILTIN,
39 PROP_PROTREF,
40 PROP_DELETED
41 } prop_type_t;
43 struct _dispex_prop_t {
44 WCHAR *name;
45 unsigned hash;
46 prop_type_t type;
47 DWORD flags;
49 union {
50 VARIANT var;
51 const builtin_prop_t *p;
52 DWORD ref;
53 } u;
55 int bucket_head;
56 int bucket_next;
59 static inline DISPID prop_to_id(jsdisp_t *This, dispex_prop_t *prop)
61 return prop - This->props;
64 static inline dispex_prop_t *get_prop(jsdisp_t *This, DISPID id)
66 if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
67 return NULL;
69 return This->props+id;
72 static DWORD get_flags(jsdisp_t *This, dispex_prop_t *prop)
74 if(prop->type == PROP_PROTREF) {
75 dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
76 if(!parent) {
77 prop->type = PROP_DELETED;
78 return 0;
81 return get_flags(This->prototype, parent);
84 return prop->flags;
87 static const builtin_prop_t *find_builtin_prop(jsdisp_t *This, const WCHAR *name)
89 int min = 0, max, i, r;
91 max = This->builtin_info->props_cnt-1;
92 while(min <= max) {
93 i = (min+max)/2;
95 r = strcmpW(name, This->builtin_info->props[i].name);
96 if(!r)
97 return This->builtin_info->props + i;
99 if(r < 0)
100 max = i-1;
101 else
102 min = i+1;
105 return NULL;
108 static inline unsigned string_hash(const WCHAR *name)
110 unsigned h = 0;
111 for(; *name; name++)
112 h = (h>>(sizeof(unsigned)*8-4)) ^ (h<<4) ^ tolowerW(*name);
113 return h;
116 static inline unsigned get_props_idx(jsdisp_t *This, unsigned hash)
118 return (hash*GOLDEN_RATIO) & (This->buf_size-1);
121 static inline HRESULT resize_props(jsdisp_t *This)
123 dispex_prop_t *props;
124 int i, bucket;
126 if(This->buf_size != This->prop_cnt)
127 return S_FALSE;
129 props = heap_realloc(This->props, sizeof(dispex_prop_t)*This->buf_size*2);
130 if(!props)
131 return E_OUTOFMEMORY;
132 This->buf_size *= 2;
133 This->props = props;
135 for(i=0; i<This->buf_size; i++) {
136 This->props[i].bucket_head = 0;
137 This->props[i].bucket_next = 0;
140 for(i=1; i<This->prop_cnt; i++) {
141 props = This->props+i;
143 bucket = get_props_idx(This, props->hash);
144 props->bucket_next = This->props[bucket].bucket_head;
145 This->props[bucket].bucket_head = i;
148 return S_OK;
151 static inline dispex_prop_t* alloc_prop(jsdisp_t *This, const WCHAR *name, prop_type_t type, DWORD flags)
153 dispex_prop_t *prop;
154 unsigned bucket;
156 if(FAILED(resize_props(This)))
157 return NULL;
159 prop = &This->props[This->prop_cnt];
160 prop->name = heap_strdupW(name);
161 if(!prop->name)
162 return NULL;
163 prop->type = type;
164 prop->flags = flags;
165 prop->hash = string_hash(name);
167 bucket = get_props_idx(This, prop->hash);
168 prop->bucket_next = This->props[bucket].bucket_head;
169 This->props[bucket].bucket_head = This->prop_cnt++;
170 return prop;
173 static dispex_prop_t *alloc_protref(jsdisp_t *This, const WCHAR *name, DWORD ref)
175 dispex_prop_t *ret;
177 ret = alloc_prop(This, name, PROP_PROTREF, 0);
178 if(!ret)
179 return NULL;
181 ret->u.ref = ref;
182 return ret;
185 static HRESULT find_prop_name(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
187 const builtin_prop_t *builtin;
188 unsigned bucket, pos, prev = 0;
189 dispex_prop_t *prop;
191 bucket = get_props_idx(This, hash);
192 pos = This->props[bucket].bucket_head;
193 while(pos != 0) {
194 if(!strcmpW(name, This->props[pos].name)) {
195 if(prev != 0) {
196 This->props[prev].bucket_next = This->props[pos].bucket_next;
197 This->props[pos].bucket_next = This->props[bucket].bucket_head;
198 This->props[bucket].bucket_head = pos;
201 *ret = &This->props[pos];
202 return S_OK;
205 prev = pos;
206 pos = This->props[pos].bucket_next;
209 builtin = find_builtin_prop(This, name);
210 if(builtin) {
211 prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
212 if(!prop)
213 return E_OUTOFMEMORY;
215 prop->u.p = builtin;
216 *ret = prop;
217 return S_OK;
220 *ret = NULL;
221 return S_OK;
224 static HRESULT find_prop_name_prot(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
226 dispex_prop_t *prop, *del=NULL;
227 HRESULT hres;
229 hres = find_prop_name(This, hash, name, &prop);
230 if(FAILED(hres))
231 return hres;
232 if(prop && prop->type==PROP_DELETED) {
233 del = prop;
234 } else if(prop) {
235 *ret = prop;
236 return S_OK;
239 if(This->prototype) {
240 hres = find_prop_name_prot(This->prototype, hash, name, &prop);
241 if(FAILED(hres))
242 return hres;
243 if(prop) {
244 if(del) {
245 del->type = PROP_PROTREF;
246 del->flags = 0;
247 del->u.ref = prop - This->prototype->props;
248 prop = del;
249 }else {
250 prop = alloc_protref(This, prop->name, prop - This->prototype->props);
251 if(!prop)
252 return E_OUTOFMEMORY;
255 *ret = prop;
256 return S_OK;
260 *ret = del;
261 return S_OK;
264 static HRESULT ensure_prop_name(jsdisp_t *This, const WCHAR *name, BOOL search_prot, DWORD create_flags, dispex_prop_t **ret)
266 dispex_prop_t *prop;
267 HRESULT hres;
269 if(search_prot)
270 hres = find_prop_name_prot(This, string_hash(name), name, &prop);
271 else
272 hres = find_prop_name(This, string_hash(name), name, &prop);
273 if(SUCCEEDED(hres) && (!prop || prop->type==PROP_DELETED)) {
274 TRACE("creating prop %s\n", debugstr_w(name));
276 if(prop) {
277 prop->type = PROP_VARIANT;
278 prop->flags = create_flags;
279 }else {
280 prop = alloc_prop(This, name, PROP_VARIANT, create_flags);
281 if(!prop)
282 return E_OUTOFMEMORY;
285 VariantInit(&prop->u.var);
288 *ret = prop;
289 return hres;
292 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
294 VARIANTARG *oldargs;
295 int i;
297 static DISPID this_id = DISPID_THIS;
299 *dp = *olddp;
301 for(i = 0; i < dp->cNamedArgs; i++) {
302 if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
303 return S_OK;
306 oldargs = dp->rgvarg;
307 dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
308 if(!dp->rgvarg)
309 return E_OUTOFMEMORY;
310 memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
311 V_VT(dp->rgvarg) = VT_DISPATCH;
312 V_DISPATCH(dp->rgvarg) = jsthis;
313 dp->cArgs++;
315 if(dp->cNamedArgs) {
316 DISPID *old = dp->rgdispidNamedArgs;
317 dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
318 if(!dp->rgdispidNamedArgs) {
319 heap_free(dp->rgvarg);
320 return E_OUTOFMEMORY;
323 memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
324 dp->rgdispidNamedArgs[0] = DISPID_THIS;
325 dp->cNamedArgs++;
326 }else {
327 dp->rgdispidNamedArgs = &this_id;
328 dp->cNamedArgs = 1;
331 return S_OK;
334 static HRESULT invoke_prop_func(jsdisp_t *This, jsdisp_t *jsthis, dispex_prop_t *prop, WORD flags,
335 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
337 HRESULT hres;
339 switch(prop->type) {
340 case PROP_BUILTIN: {
341 vdisp_t vthis;
343 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
344 WARN("%s is not a constructor\n", debugstr_w(prop->name));
345 return E_INVALIDARG;
348 set_jsdisp(&vthis, jsthis);
349 hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei, caller);
350 vdisp_release(&vthis);
351 return hres;
353 case PROP_PROTREF:
354 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
355 case PROP_VARIANT: {
356 DISPPARAMS new_dp;
358 if(V_VT(&prop->u.var) != VT_DISPATCH) {
359 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
360 return E_FAIL;
363 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
365 hres = set_this(&new_dp, dp, to_disp(jsthis));
366 if(FAILED(hres))
367 return hres;
369 hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei, caller);
371 if(new_dp.rgvarg != dp->rgvarg) {
372 heap_free(new_dp.rgvarg);
373 if(new_dp.cNamedArgs > 1)
374 heap_free(new_dp.rgdispidNamedArgs);
377 return hres;
379 default:
380 ERR("type %d\n", prop->type);
383 return E_FAIL;
386 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
387 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
389 HRESULT hres;
391 switch(prop->type) {
392 case PROP_BUILTIN:
393 if(prop->u.p->flags & PROPF_METHOD) {
394 jsdisp_t *obj;
395 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
396 prop->u.p->flags, NULL, &obj);
397 if(FAILED(hres))
398 break;
400 prop->type = PROP_VARIANT;
401 var_set_jsdisp(&prop->u.var, obj);
402 hres = VariantCopy(retv, &prop->u.var);
403 }else {
404 vdisp_t vthis;
406 set_jsdisp(&vthis, This);
407 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
408 vdisp_release(&vthis);
410 break;
411 case PROP_PROTREF:
412 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
413 break;
414 case PROP_VARIANT:
415 hres = VariantCopy(retv, &prop->u.var);
416 break;
417 default:
418 ERR("type %d\n", prop->type);
419 return E_FAIL;
422 if(FAILED(hres)) {
423 TRACE("fail %08x\n", hres);
424 return hres;
427 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
428 return hres;
431 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
432 jsexcept_t *ei, IServiceProvider *caller)
434 HRESULT hres;
436 if(prop->flags & PROPF_CONST)
437 return S_OK;
439 switch(prop->type) {
440 case PROP_BUILTIN:
441 if(!(prop->flags & PROPF_METHOD)) {
442 DISPPARAMS dp = {val, NULL, 1, 0};
443 vdisp_t vthis;
445 set_jsdisp(&vthis, This);
446 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei, caller);
447 vdisp_release(&vthis);
448 return hres;
450 case PROP_PROTREF:
451 prop->type = PROP_VARIANT;
452 prop->flags = PROPF_ENUM;
453 V_VT(&prop->u.var) = VT_EMPTY;
454 break;
455 case PROP_VARIANT:
456 VariantClear(&prop->u.var);
457 break;
458 default:
459 ERR("type %d\n", prop->type);
460 return E_FAIL;
463 hres = VariantCopy(&prop->u.var, val);
464 if(FAILED(hres))
465 return hres;
467 if(This->builtin_info->on_put)
468 This->builtin_info->on_put(This, prop->name);
470 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
471 return S_OK;
474 static HRESULT fill_protrefs(jsdisp_t *This)
476 dispex_prop_t *iter, *prop;
477 HRESULT hres;
479 if(!This->prototype)
480 return S_OK;
482 fill_protrefs(This->prototype);
484 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
485 if(!iter->name)
486 continue;
487 hres = find_prop_name(This, iter->hash, iter->name, &prop);
488 if(FAILED(hres))
489 return hres;
490 if(!prop || prop->type==PROP_DELETED) {
491 if(prop) {
492 prop->type = PROP_PROTREF;
493 prop->flags = 0;
494 prop->u.ref = iter - This->prototype->props;
495 }else {
496 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
497 if(!prop)
498 return E_OUTOFMEMORY;
503 return S_OK;
506 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
508 return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
511 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
513 jsdisp_t *This = impl_from_IDispatchEx(iface);
515 if(IsEqualGUID(&IID_IUnknown, riid)) {
516 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
517 *ppv = &This->IDispatchEx_iface;
518 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
519 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
520 *ppv = &This->IDispatchEx_iface;
521 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
522 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
523 *ppv = &This->IDispatchEx_iface;
524 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
525 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
526 jsdisp_addref(This);
527 *ppv = This;
528 return S_OK;
529 }else {
530 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
531 *ppv = NULL;
532 return E_NOINTERFACE;
535 IUnknown_AddRef((IUnknown*)*ppv);
536 return S_OK;
539 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
541 jsdisp_t *This = impl_from_IDispatchEx(iface);
542 LONG ref = InterlockedIncrement(&This->ref);
544 TRACE("(%p) ref=%d\n", This, ref);
546 return ref;
549 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
551 jsdisp_t *This = impl_from_IDispatchEx(iface);
552 LONG ref = InterlockedDecrement(&This->ref);
554 TRACE("(%p) ref=%d\n", This, ref);
556 if(!ref) {
557 dispex_prop_t *prop;
559 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
560 if(prop->type == PROP_VARIANT)
561 VariantClear(&prop->u.var);
562 heap_free(prop->name);
564 heap_free(This->props);
565 script_release(This->ctx);
566 if(This->prototype)
567 jsdisp_release(This->prototype);
569 if(This->builtin_info->destructor)
570 This->builtin_info->destructor(This);
571 else
572 heap_free(This);
575 return ref;
578 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
580 jsdisp_t *This = impl_from_IDispatchEx(iface);
582 TRACE("(%p)->(%p)\n", This, pctinfo);
584 *pctinfo = 1;
585 return S_OK;
588 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
589 ITypeInfo **ppTInfo)
591 jsdisp_t *This = impl_from_IDispatchEx(iface);
592 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
593 return E_NOTIMPL;
596 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
597 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
598 DISPID *rgDispId)
600 jsdisp_t *This = impl_from_IDispatchEx(iface);
601 UINT i;
602 HRESULT hres;
604 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
605 lcid, rgDispId);
607 for(i=0; i < cNames; i++) {
608 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
609 if(FAILED(hres))
610 return hres;
613 return S_OK;
616 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
617 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
618 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
620 jsdisp_t *This = impl_from_IDispatchEx(iface);
622 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
623 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
625 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
626 pDispParams, pVarResult, pExcepInfo, NULL);
629 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
631 jsdisp_t *This = impl_from_IDispatchEx(iface);
633 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
635 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
636 FIXME("Unsupported grfdex %x\n", grfdex);
637 return E_NOTIMPL;
640 return jsdisp_get_id(This, bstrName, grfdex, pid);
643 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
644 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
646 jsdisp_t *This = impl_from_IDispatchEx(iface);
647 dispex_prop_t *prop;
648 jsexcept_t jsexcept;
649 HRESULT hres;
651 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
653 if(pvarRes)
654 V_VT(pvarRes) = VT_EMPTY;
656 prop = get_prop(This, id);
657 if(!prop || prop->type == PROP_DELETED) {
658 TRACE("invalid id\n");
659 return DISP_E_MEMBERNOTFOUND;
662 memset(&jsexcept, 0, sizeof(jsexcept));
664 switch(wFlags) {
665 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
666 wFlags = DISPATCH_METHOD;
667 case DISPATCH_METHOD:
668 case DISPATCH_CONSTRUCT:
669 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
670 break;
671 case DISPATCH_PROPERTYGET:
672 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
673 break;
674 case DISPATCH_PROPERTYPUT: {
675 DWORD i;
677 for(i=0; i < pdp->cNamedArgs; i++) {
678 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
679 break;
682 if(i == pdp->cNamedArgs) {
683 TRACE("no value to set\n");
684 return DISP_E_PARAMNOTOPTIONAL;
687 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
688 break;
690 default:
691 FIXME("Unimplemented flags %x\n", wFlags);
692 return E_INVALIDARG;
695 if(pei)
696 *pei = jsexcept.ei;
698 return hres;
701 static HRESULT delete_prop(dispex_prop_t *prop)
703 if(prop->type == PROP_VARIANT) {
704 VariantClear(&prop->u.var);
705 prop->type = PROP_DELETED;
707 return S_OK;
710 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
712 jsdisp_t *This = impl_from_IDispatchEx(iface);
713 dispex_prop_t *prop;
714 HRESULT hres;
716 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
718 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
719 FIXME("Unsupported grfdex %x\n", grfdex);
721 hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
722 if(FAILED(hres))
723 return hres;
724 if(!prop) {
725 TRACE("not found\n");
726 return S_OK;
729 return delete_prop(prop);
732 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
734 jsdisp_t *This = impl_from_IDispatchEx(iface);
735 dispex_prop_t *prop;
737 TRACE("(%p)->(%x)\n", This, id);
739 prop = get_prop(This, id);
740 if(!prop) {
741 WARN("invalid id\n");
742 return DISP_E_MEMBERNOTFOUND;
745 return delete_prop(prop);
748 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
750 jsdisp_t *This = impl_from_IDispatchEx(iface);
751 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
752 return E_NOTIMPL;
755 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
757 jsdisp_t *This = impl_from_IDispatchEx(iface);
758 dispex_prop_t *prop;
760 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
762 prop = get_prop(This, id);
763 if(!prop || !prop->name || prop->type == PROP_DELETED)
764 return DISP_E_MEMBERNOTFOUND;
766 *pbstrName = SysAllocString(prop->name);
767 if(!*pbstrName)
768 return E_OUTOFMEMORY;
770 return S_OK;
773 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
775 jsdisp_t *This = impl_from_IDispatchEx(iface);
776 dispex_prop_t *iter;
777 HRESULT hres;
779 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
781 if(id == DISPID_STARTENUM) {
782 hres = fill_protrefs(This);
783 if(FAILED(hres))
784 return hres;
787 if(id+1>=0 && id+1<This->prop_cnt) {
788 iter = &This->props[id+1];
789 }else {
790 *pid = DISPID_STARTENUM;
791 return S_FALSE;
794 while(iter < This->props + This->prop_cnt) {
795 if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
796 *pid = prop_to_id(This, iter);
797 return S_OK;
799 iter++;
802 *pid = DISPID_STARTENUM;
803 return S_FALSE;
806 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
808 jsdisp_t *This = impl_from_IDispatchEx(iface);
809 FIXME("(%p)->(%p)\n", This, ppunk);
810 return E_NOTIMPL;
813 static IDispatchExVtbl DispatchExVtbl = {
814 DispatchEx_QueryInterface,
815 DispatchEx_AddRef,
816 DispatchEx_Release,
817 DispatchEx_GetTypeInfoCount,
818 DispatchEx_GetTypeInfo,
819 DispatchEx_GetIDsOfNames,
820 DispatchEx_Invoke,
821 DispatchEx_GetDispID,
822 DispatchEx_InvokeEx,
823 DispatchEx_DeleteMemberByName,
824 DispatchEx_DeleteMemberByDispID,
825 DispatchEx_GetMemberProperties,
826 DispatchEx_GetMemberName,
827 DispatchEx_GetNextDispID,
828 DispatchEx_GetNameSpaceParent
831 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
833 TRACE("%p (%p)\n", dispex, prototype);
835 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
836 dispex->ref = 1;
837 dispex->builtin_info = builtin_info;
839 dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
840 if(!dispex->props)
841 return E_OUTOFMEMORY;
843 dispex->prototype = prototype;
844 if(prototype)
845 jsdisp_addref(prototype);
847 dispex->prop_cnt = 1;
848 if(builtin_info->value_prop.invoke) {
849 dispex->props[0].type = PROP_BUILTIN;
850 dispex->props[0].u.p = &builtin_info->value_prop;
851 }else {
852 dispex->props[0].type = PROP_DELETED;
855 script_addref(ctx);
856 dispex->ctx = ctx;
858 return S_OK;
861 static const builtin_info_t dispex_info = {
862 JSCLASS_NONE,
863 {NULL, NULL, 0},
864 0, NULL,
865 NULL,
866 NULL
869 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
871 jsdisp_t *ret;
872 HRESULT hres;
874 ret = heap_alloc_zero(sizeof(jsdisp_t));
875 if(!ret)
876 return E_OUTOFMEMORY;
878 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
879 if(FAILED(hres))
880 return hres;
882 *dispex = ret;
883 return S_OK;
886 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
888 jsdisp_t *prot = NULL;
889 dispex_prop_t *prop;
890 HRESULT hres;
892 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
893 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
895 hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
896 if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
897 jsexcept_t jsexcept;
898 VARIANT var;
900 V_VT(&var) = VT_EMPTY;
901 memset(&jsexcept, 0, sizeof(jsexcept));
902 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
903 if(FAILED(hres)) {
904 ERR("Could not get prototype\n");
905 return hres;
908 if(V_VT(&var) == VT_DISPATCH)
909 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
910 VariantClear(&var);
913 hres = init_dispex(dispex, ctx, builtin_info, prot);
915 if(prot)
916 jsdisp_release(prot);
917 if(FAILED(hres))
918 return hres;
920 hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
921 if(SUCCEEDED(hres)) {
922 jsexcept_t jsexcept;
923 VARIANT var;
925 var_set_jsdisp(&var, constr);
926 memset(&jsexcept, 0, sizeof(jsexcept));
927 hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
929 if(FAILED(hres))
930 jsdisp_release(dispex);
932 return hres;
935 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
937 jsdisp_t *ret;
938 HRESULT hres;
940 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
941 if(FAILED(hres))
942 return NULL;
944 return ret;
947 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
949 dispex_prop_t *prop;
950 HRESULT hres;
952 if(flags & fdexNameEnsure)
953 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
954 else
955 hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
956 if(FAILED(hres))
957 return hres;
959 if(prop && prop->type!=PROP_DELETED) {
960 *id = prop_to_id(jsdisp, prop);
961 return S_OK;
964 TRACE("not found %s\n", debugstr_w(name));
965 return DISP_E_UNKNOWNNAME;
968 HRESULT jsdisp_call_value(jsdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv,
969 jsexcept_t *ei, IServiceProvider *caller)
971 vdisp_t vdisp;
972 HRESULT hres;
974 set_jsdisp(&vdisp, jsthis);
975 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei, caller);
976 vdisp_release(&vdisp);
977 return hres;
980 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
981 jsexcept_t *ei, IServiceProvider *caller)
983 dispex_prop_t *prop;
985 memset(ei, 0, sizeof(*ei));
986 if(retv)
987 V_VT(retv) = VT_EMPTY;
989 prop = get_prop(disp, id);
990 if(!prop)
991 return DISP_E_MEMBERNOTFOUND;
993 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
996 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
997 jsexcept_t *ei, IServiceProvider *caller)
999 dispex_prop_t *prop;
1000 HRESULT hres;
1002 hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1003 if(FAILED(hres))
1004 return hres;
1006 memset(ei, 0, sizeof(*ei));
1007 if(retv)
1008 V_VT(retv) = VT_EMPTY;
1010 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
1013 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
1014 jsexcept_t *ei, IServiceProvider *caller)
1016 jsdisp_t *jsdisp;
1017 IDispatchEx *dispex;
1018 HRESULT hres;
1020 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1021 if(jsdisp) {
1022 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei, caller);
1023 jsdisp_release(jsdisp);
1024 return hres;
1027 memset(ei, 0, sizeof(*ei));
1028 if(retv && arg_cnt(dp))
1029 flags |= DISPATCH_PROPERTYGET;
1031 if(retv)
1032 V_VT(retv) = VT_EMPTY;
1033 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1034 if(FAILED(hres)) {
1035 UINT err = 0;
1037 if(flags == DISPATCH_CONSTRUCT) {
1038 WARN("IDispatch cannot be constructor\n");
1039 return DISP_E_MEMBERNOTFOUND;
1042 TRACE("using IDispatch\n");
1043 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
1046 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, caller);
1047 IDispatchEx_Release(dispex);
1049 return hres;
1052 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1054 dispex_prop_t *prop;
1055 HRESULT hres;
1057 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1058 if(FAILED(hres))
1059 return hres;
1061 return prop_put(obj, prop, val, ei, caller);
1064 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1066 dispex_prop_t *prop;
1067 HRESULT hres;
1069 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1070 if(FAILED(hres))
1071 return hres;
1073 return VariantCopy(&prop->u.var, val);
1076 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1078 WCHAR buf[12];
1080 static const WCHAR formatW[] = {'%','d',0};
1082 sprintfW(buf, formatW, idx);
1083 return jsdisp_propput_name(obj, buf, val, ei, caller);
1086 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1088 jsdisp_t *jsdisp;
1089 HRESULT hres;
1091 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1092 if(jsdisp) {
1093 dispex_prop_t *prop;
1095 prop = get_prop(jsdisp, id);
1096 if(prop)
1097 hres = prop_put(jsdisp, prop, val, ei, caller);
1098 else
1099 hres = DISP_E_MEMBERNOTFOUND;
1101 jsdisp_release(jsdisp);
1102 }else {
1103 DISPID dispid = DISPID_PROPERTYPUT;
1104 DISPPARAMS dp = {val, &dispid, 1, 1};
1105 IDispatchEx *dispex;
1107 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1108 if(SUCCEEDED(hres)) {
1109 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
1110 IDispatchEx_Release(dispex);
1111 }else {
1112 ULONG err = 0;
1114 TRACE("using IDispatch\n");
1115 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1119 return hres;
1122 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1124 DISPPARAMS dp = {NULL, NULL, 0, 0};
1125 dispex_prop_t *prop;
1126 HRESULT hres;
1128 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1129 if(FAILED(hres))
1130 return hres;
1132 V_VT(var) = VT_EMPTY;
1133 if(!prop || prop->type==PROP_DELETED)
1134 return S_OK;
1136 return prop_get(obj, prop, &dp, var, ei, caller);
1139 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1141 WCHAR name[12];
1142 DISPPARAMS dp = {NULL, NULL, 0, 0};
1143 dispex_prop_t *prop;
1144 HRESULT hres;
1146 static const WCHAR formatW[] = {'%','d',0};
1148 sprintfW(name, formatW, idx);
1150 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1151 if(FAILED(hres))
1152 return hres;
1154 V_VT(var) = VT_EMPTY;
1155 if(!prop || prop->type==PROP_DELETED)
1156 return DISP_E_UNKNOWNNAME;
1158 return prop_get(obj, prop, &dp, var, ei, caller);
1161 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1163 DISPPARAMS dp = {NULL,NULL,0,0};
1164 dispex_prop_t *prop;
1166 prop = get_prop(jsdisp, id);
1167 if(!prop)
1168 return DISP_E_MEMBERNOTFOUND;
1170 V_VT(val) = VT_EMPTY;
1171 return prop_get(jsdisp, prop, &dp, val, ei, caller);
1174 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1176 DISPPARAMS dp = {NULL,NULL,0,0};
1177 IDispatchEx *dispex;
1178 jsdisp_t *jsdisp;
1179 HRESULT hres;
1181 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1182 if(jsdisp) {
1183 hres = jsdisp_propget(jsdisp, id, val, ei, caller);
1184 jsdisp_release(jsdisp);
1185 return hres;
1188 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1189 if(FAILED(hres)) {
1190 ULONG err = 0;
1192 TRACE("using IDispatch\n");
1193 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1196 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
1197 IDispatchEx_Release(dispex);
1199 return hres;
1202 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1204 static const WCHAR formatW[] = {'%','d',0};
1205 WCHAR buf[12];
1206 dispex_prop_t *prop;
1207 HRESULT hres;
1209 sprintfW(buf, formatW, idx);
1211 hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1212 if(FAILED(hres) || !prop)
1213 return hres;
1215 return delete_prop(prop);
1218 VARIANT_BOOL jsdisp_is_own_prop(jsdisp_t *obj, BSTR name)
1220 dispex_prop_t *prop;
1221 HRESULT hres;
1223 hres = find_prop_name(obj, string_hash(name), name, &prop);
1224 if(FAILED(hres))
1225 return VARIANT_FALSE;
1226 else if(!prop)
1227 return VARIANT_FALSE;
1229 return prop->type==PROP_VARIANT ? VARIANT_TRUE : VARIANT_FALSE;