push 8b07bf1f08b23b9893a622b47d2be359556765b1
[wine/hacks.git] / dlls / jscript / dispex.c
blob307efd97a22c7eb9b9acb4afcd28fcbe92565435
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 DispatchEx 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 typedef enum {
34 PROP_VARIANT,
35 PROP_BUILTIN,
36 PROP_PROTREF,
37 PROP_DELETED
38 } prop_type_t;
40 struct _dispex_prop_t {
41 WCHAR *name;
42 prop_type_t type;
43 DWORD flags;
45 union {
46 VARIANT var;
47 const builtin_prop_t *p;
48 DWORD ref;
49 } u;
52 static inline DISPID prop_to_id(DispatchEx *This, dispex_prop_t *prop)
54 return prop - This->props;
57 static inline dispex_prop_t *get_prop(DispatchEx *This, DISPID id)
59 if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
60 return NULL;
62 return This->props+id;
65 static DWORD get_flags(DispatchEx *This, dispex_prop_t *prop)
67 if(prop->type == PROP_PROTREF) {
68 dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
69 if(!parent) {
70 prop->type = PROP_DELETED;
71 return 0;
74 return get_flags(This->prototype, parent);
77 return prop->flags;
80 static const builtin_prop_t *find_builtin_prop(DispatchEx *This, const WCHAR *name)
82 int min = 0, max, i, r;
84 max = This->builtin_info->props_cnt-1;
85 while(min <= max) {
86 i = (min+max)/2;
88 r = strcmpW(name, This->builtin_info->props[i].name);
89 if(!r)
90 return This->builtin_info->props + i;
92 if(r < 0)
93 max = i-1;
94 else
95 min = i+1;
98 return NULL;
101 static dispex_prop_t *alloc_prop(DispatchEx *This, const WCHAR *name, prop_type_t type, DWORD flags)
103 dispex_prop_t *ret;
105 if(This->buf_size == This->prop_cnt) {
106 dispex_prop_t *tmp = heap_realloc(This->props, (This->buf_size<<=1)*sizeof(*This->props));
107 if(!tmp)
108 return NULL;
109 This->props = tmp;
112 ret = This->props + This->prop_cnt++;
113 ret->type = type;
114 ret->flags = flags;
115 ret->name = heap_strdupW(name);
116 if(!ret->name)
117 return NULL;
119 return ret;
122 static dispex_prop_t *alloc_protref(DispatchEx *This, const WCHAR *name, DWORD ref)
124 dispex_prop_t *ret;
126 ret = alloc_prop(This, name, PROP_PROTREF, 0);
127 if(!ret)
128 return NULL;
130 ret->u.ref = ref;
131 return ret;
134 static HRESULT find_prop_name(DispatchEx *This, const WCHAR *name, dispex_prop_t **ret)
136 const builtin_prop_t *builtin;
137 dispex_prop_t *prop;
139 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
140 if(prop->name && !strcmpW(prop->name, name)) {
141 *ret = prop;
142 return S_OK;
146 builtin = find_builtin_prop(This, name);
147 if(builtin) {
148 prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
149 if(!prop)
150 return E_OUTOFMEMORY;
152 prop->u.p = builtin;
153 *ret = prop;
154 return S_OK;
157 *ret = NULL;
158 return S_OK;
161 static HRESULT find_prop_name_prot(DispatchEx *This, const WCHAR *name, BOOL alloc, dispex_prop_t **ret)
163 dispex_prop_t *prop;
164 HRESULT hres;
166 hres = find_prop_name(This, name, &prop);
167 if(FAILED(hres))
168 return hres;
169 if(prop) {
170 *ret = prop;
171 return S_OK;
174 if(This->prototype) {
175 hres = find_prop_name_prot(This->prototype, name, FALSE, &prop);
176 if(FAILED(hres))
177 return hres;
178 if(prop) {
179 prop = alloc_protref(This, prop->name, prop - This->prototype->props);
180 if(!prop)
181 return E_OUTOFMEMORY;
182 *ret = prop;
183 return S_OK;
187 if(alloc) {
188 TRACE("creating prop %s\n", debugstr_w(name));
190 prop = alloc_prop(This, name, PROP_VARIANT, PROPF_ENUM);
191 if(!prop)
192 return E_OUTOFMEMORY;
193 VariantInit(&prop->u.var);
196 *ret = prop;
197 return S_OK;
200 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
202 VARIANTARG *oldargs;
203 int i;
205 static DISPID this_id = DISPID_THIS;
207 *dp = *olddp;
209 for(i = 0; i < dp->cNamedArgs; i++) {
210 if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
211 return S_OK;
214 oldargs = dp->rgvarg;
215 dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
216 if(!dp->rgvarg)
217 return E_OUTOFMEMORY;
218 memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
219 V_VT(dp->rgvarg) = VT_DISPATCH;
220 V_DISPATCH(dp->rgvarg) = jsthis;
221 dp->cArgs++;
223 if(dp->cNamedArgs) {
224 DISPID *old = dp->rgdispidNamedArgs;
225 dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
226 if(!dp->rgdispidNamedArgs) {
227 heap_free(dp->rgvarg);
228 return E_OUTOFMEMORY;
231 memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
232 dp->rgdispidNamedArgs[0] = DISPID_THIS;
233 dp->cNamedArgs++;
234 }else {
235 dp->rgdispidNamedArgs = &this_id;
236 dp->cNamedArgs = 1;
239 return S_OK;
242 static HRESULT invoke_prop_func(DispatchEx *This, DispatchEx *jsthis, dispex_prop_t *prop, WORD flags,
243 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
245 HRESULT hres;
247 switch(prop->type) {
248 case PROP_BUILTIN: {
249 vdisp_t vthis;
251 if(flags == DISPATCH_CONSTRUCT && (prop->flags & DISPATCH_METHOD)) {
252 WARN("%s is not a constructor\n", debugstr_w(prop->name));
253 return E_INVALIDARG;
256 set_jsdisp(&vthis, jsthis);
257 hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei, caller);
258 vdisp_release(&vthis);
259 return hres;
261 case PROP_PROTREF:
262 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
263 case PROP_VARIANT: {
264 DISPPARAMS new_dp;
266 if(V_VT(&prop->u.var) != VT_DISPATCH) {
267 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
268 return E_FAIL;
271 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
273 hres = set_this(&new_dp, dp, (IDispatch*)_IDispatchEx_(jsthis));
274 if(FAILED(hres))
275 return hres;
277 hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei, caller);
279 if(new_dp.rgvarg != dp->rgvarg) {
280 heap_free(new_dp.rgvarg);
281 if(new_dp.cNamedArgs > 1)
282 heap_free(new_dp.rgdispidNamedArgs);
285 return hres;
287 default:
288 ERR("type %d\n", prop->type);
291 return E_FAIL;
294 static HRESULT prop_get(DispatchEx *This, dispex_prop_t *prop, DISPPARAMS *dp,
295 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
297 HRESULT hres;
299 switch(prop->type) {
300 case PROP_BUILTIN:
301 if(prop->u.p->flags & PROPF_METHOD) {
302 DispatchEx *obj;
303 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
304 prop->u.p->flags, NULL, &obj);
305 if(FAILED(hres))
306 break;
308 prop->type = PROP_VARIANT;
309 V_VT(&prop->u.var) = VT_DISPATCH;
310 V_DISPATCH(&prop->u.var) = (IDispatch*)_IDispatchEx_(obj);
312 hres = VariantCopy(retv, &prop->u.var);
313 }else {
314 vdisp_t vthis;
316 set_jsdisp(&vthis, This);
317 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
318 vdisp_release(&vthis);
320 break;
321 case PROP_PROTREF:
322 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
323 break;
324 case PROP_VARIANT:
325 hres = VariantCopy(retv, &prop->u.var);
326 break;
327 default:
328 ERR("type %d\n", prop->type);
329 return E_FAIL;
332 if(FAILED(hres)) {
333 TRACE("fail %08x\n", hres);
334 return hres;
337 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
338 return hres;
341 static HRESULT prop_put(DispatchEx *This, dispex_prop_t *prop, DISPPARAMS *dp,
342 jsexcept_t *ei, IServiceProvider *caller)
344 DWORD i;
345 HRESULT hres;
347 switch(prop->type) {
348 case PROP_BUILTIN:
349 if(!(prop->flags & PROPF_METHOD)) {
350 vdisp_t vthis;
352 set_jsdisp(&vthis, This);
353 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, dp, NULL, ei, caller);
354 vdisp_release(&vthis);
355 return hres;
357 case PROP_PROTREF:
358 prop->type = PROP_VARIANT;
359 prop->flags = PROPF_ENUM;
360 V_VT(&prop->u.var) = VT_EMPTY;
361 break;
362 case PROP_VARIANT:
363 VariantClear(&prop->u.var);
364 break;
365 default:
366 ERR("type %d\n", prop->type);
367 return E_FAIL;
370 for(i=0; i < dp->cNamedArgs; i++) {
371 if(dp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
372 break;
375 if(i == dp->cNamedArgs) {
376 TRACE("no value to set\n");
377 return DISP_E_PARAMNOTOPTIONAL;
380 hres = VariantCopy(&prop->u.var, dp->rgvarg+i);
381 if(FAILED(hres))
382 return hres;
384 if(This->builtin_info->on_put)
385 This->builtin_info->on_put(This, prop->name);
387 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(dp->rgvarg+i));
388 return S_OK;
391 static HRESULT fill_protrefs(DispatchEx *This)
393 dispex_prop_t *iter, *prop;
394 HRESULT hres;
396 if(!This->prototype)
397 return S_OK;
399 fill_protrefs(This->prototype);
401 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
402 if(!iter->name)
403 continue;
404 hres = find_prop_name(This, iter->name, &prop);
405 if(FAILED(hres))
406 return hres;
407 if(!prop) {
408 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
409 if(!prop)
410 return E_OUTOFMEMORY;
414 return S_OK;
417 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
419 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
421 DispatchEx *This = DISPATCHEX_THIS(iface);
423 if(IsEqualGUID(&IID_IUnknown, riid)) {
424 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
425 *ppv = _IDispatchEx_(This);
426 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
427 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
428 *ppv = _IDispatchEx_(This);
429 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
430 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
431 *ppv = _IDispatchEx_(This);
432 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
433 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
434 IUnknown_AddRef(_IDispatchEx_(This));
435 *ppv = This;
436 return S_OK;
437 }else {
438 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
439 *ppv = NULL;
440 return E_NOINTERFACE;
443 IUnknown_AddRef((IUnknown*)*ppv);
444 return S_OK;
447 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
449 DispatchEx *This = DISPATCHEX_THIS(iface);
450 LONG ref = InterlockedIncrement(&This->ref);
452 TRACE("(%p) ref=%d\n", This, ref);
454 return ref;
457 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
459 DispatchEx *This = DISPATCHEX_THIS(iface);
460 LONG ref = InterlockedDecrement(&This->ref);
462 TRACE("(%p) ref=%d\n", This, ref);
464 if(!ref) {
465 dispex_prop_t *prop;
467 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
468 if(prop->type == PROP_VARIANT)
469 VariantClear(&prop->u.var);
470 heap_free(prop->name);
472 heap_free(This->props);
473 script_release(This->ctx);
475 if(This->builtin_info->destructor)
476 This->builtin_info->destructor(This);
477 else
478 heap_free(This);
481 return ref;
484 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
486 DispatchEx *This = DISPATCHEX_THIS(iface);
488 TRACE("(%p)->(%p)\n", This, pctinfo);
490 *pctinfo = 1;
491 return S_OK;
494 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
495 ITypeInfo **ppTInfo)
497 DispatchEx *This = DISPATCHEX_THIS(iface);
498 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
499 return E_NOTIMPL;
502 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
503 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
504 DISPID *rgDispId)
506 DispatchEx *This = DISPATCHEX_THIS(iface);
507 UINT i;
508 HRESULT hres;
510 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
511 lcid, rgDispId);
513 for(i=0; i < cNames; i++) {
514 hres = IDispatchEx_GetDispID(_IDispatchEx_(This), rgszNames[i], 0, rgDispId+i);
515 if(FAILED(hres))
516 return hres;
519 return S_OK;
522 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
523 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
524 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
526 DispatchEx *This = DISPATCHEX_THIS(iface);
528 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
529 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
531 return IDispatchEx_InvokeEx(_IDispatchEx_(This), dispIdMember, lcid, wFlags,
532 pDispParams, pVarResult, pExcepInfo, NULL);
535 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
537 DispatchEx *This = DISPATCHEX_THIS(iface);
539 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
541 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit)) {
542 FIXME("Unsupported grfdex %x\n", grfdex);
543 return E_NOTIMPL;
546 return jsdisp_get_id(This, bstrName, grfdex, pid);
549 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
550 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
552 DispatchEx *This = DISPATCHEX_THIS(iface);
553 dispex_prop_t *prop;
554 jsexcept_t jsexcept;
555 HRESULT hres;
557 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
559 if(pvarRes)
560 V_VT(pvarRes) = VT_EMPTY;
562 prop = get_prop(This, id);
563 if(!prop || prop->type == PROP_DELETED) {
564 TRACE("invalid id\n");
565 return DISP_E_MEMBERNOTFOUND;
568 memset(&jsexcept, 0, sizeof(jsexcept));
570 switch(wFlags) {
571 case DISPATCH_METHOD:
572 case DISPATCH_CONSTRUCT:
573 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
574 break;
575 case DISPATCH_PROPERTYGET:
576 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
577 break;
578 case DISPATCH_PROPERTYPUT:
579 hres = prop_put(This, prop, pdp, &jsexcept, pspCaller);
580 break;
581 default:
582 FIXME("Unimplemented flags %x\n", wFlags);
583 return E_INVALIDARG;
586 if(pei)
587 *pei = jsexcept.ei;
589 return hres;
592 static HRESULT delete_prop(dispex_prop_t *prop)
594 heap_free(prop->name);
595 prop->name = NULL;
596 prop->type = PROP_DELETED;
598 return S_OK;
601 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
603 DispatchEx *This = DISPATCHEX_THIS(iface);
604 dispex_prop_t *prop;
605 HRESULT hres;
607 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
609 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
610 FIXME("Unsupported grfdex %x\n", grfdex);
612 hres = find_prop_name(This, bstrName, &prop);
613 if(FAILED(hres))
614 return hres;
615 if(!prop) {
616 TRACE("not found\n");
617 return S_OK;
620 return delete_prop(prop);
623 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
625 DispatchEx *This = DISPATCHEX_THIS(iface);
626 dispex_prop_t *prop;
628 TRACE("(%p)->(%x)\n", This, id);
630 prop = get_prop(This, id);
631 if(!prop) {
632 WARN("invalid id\n");
633 return DISP_E_MEMBERNOTFOUND;
636 return delete_prop(prop);
639 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
641 DispatchEx *This = DISPATCHEX_THIS(iface);
642 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
643 return E_NOTIMPL;
646 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
648 DispatchEx *This = DISPATCHEX_THIS(iface);
649 dispex_prop_t *prop;
651 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
653 prop = get_prop(This, id);
654 if(!prop || !prop->name || prop->type == PROP_DELETED)
655 return DISP_E_MEMBERNOTFOUND;
657 *pbstrName = SysAllocString(prop->name);
658 if(!*pbstrName)
659 return E_OUTOFMEMORY;
661 return S_OK;
664 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
666 DispatchEx *This = DISPATCHEX_THIS(iface);
667 dispex_prop_t *iter;
668 HRESULT hres;
670 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
672 if(id == DISPID_STARTENUM) {
673 hres = fill_protrefs(This);
674 if(FAILED(hres))
675 return hres;
678 iter = get_prop(This, id+1);
679 if(!iter) {
680 *pid = DISPID_STARTENUM;
681 return S_FALSE;
684 while(iter < This->props + This->prop_cnt) {
685 if(iter->name && (get_flags(This, iter) & PROPF_ENUM)) {
686 *pid = prop_to_id(This, iter);
687 return S_OK;
689 iter++;
692 *pid = DISPID_STARTENUM;
693 return S_FALSE;
696 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
698 DispatchEx *This = DISPATCHEX_THIS(iface);
699 FIXME("(%p)->(%p)\n", This, ppunk);
700 return E_NOTIMPL;
703 #undef DISPATCHEX_THIS
705 static IDispatchExVtbl DispatchExVtbl = {
706 DispatchEx_QueryInterface,
707 DispatchEx_AddRef,
708 DispatchEx_Release,
709 DispatchEx_GetTypeInfoCount,
710 DispatchEx_GetTypeInfo,
711 DispatchEx_GetIDsOfNames,
712 DispatchEx_Invoke,
713 DispatchEx_GetDispID,
714 DispatchEx_InvokeEx,
715 DispatchEx_DeleteMemberByName,
716 DispatchEx_DeleteMemberByDispID,
717 DispatchEx_GetMemberProperties,
718 DispatchEx_GetMemberName,
719 DispatchEx_GetNextDispID,
720 DispatchEx_GetNameSpaceParent
723 HRESULT init_dispex(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype)
725 TRACE("%p (%p)\n", dispex, prototype);
727 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
728 dispex->ref = 1;
729 dispex->builtin_info = builtin_info;
731 dispex->props = heap_alloc((dispex->buf_size=4) * sizeof(dispex_prop_t));
732 if(!dispex->props)
733 return E_OUTOFMEMORY;
735 dispex->prototype = prototype;
736 if(prototype)
737 IDispatchEx_AddRef(_IDispatchEx_(prototype));
739 dispex->prop_cnt = 1;
740 dispex->props[0].name = NULL;
741 dispex->props[0].flags = 0;
742 if(builtin_info->value_prop.invoke) {
743 dispex->props[0].type = PROP_BUILTIN;
744 dispex->props[0].u.p = &builtin_info->value_prop;
745 }else {
746 dispex->props[0].type = PROP_DELETED;
749 script_addref(ctx);
750 dispex->ctx = ctx;
752 return S_OK;
755 static const builtin_info_t dispex_info = {
756 JSCLASS_NONE,
757 {NULL, NULL, 0},
758 0, NULL,
759 NULL,
760 NULL
763 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype, DispatchEx **dispex)
765 DispatchEx *ret;
766 HRESULT hres;
768 ret = heap_alloc_zero(sizeof(DispatchEx));
769 if(!ret)
770 return E_OUTOFMEMORY;
772 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
773 if(FAILED(hres))
774 return hres;
776 *dispex = ret;
777 return S_OK;
780 HRESULT init_dispex_from_constr(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *constr)
782 DispatchEx *prot = NULL;
783 dispex_prop_t *prop;
784 HRESULT hres;
786 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
788 hres = find_prop_name_prot(constr, prototypeW, FALSE, &prop);
789 if(SUCCEEDED(hres) && prop) {
790 jsexcept_t jsexcept;
791 VARIANT var;
793 V_VT(&var) = VT_EMPTY;
794 memset(&jsexcept, 0, sizeof(jsexcept));
795 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
796 if(FAILED(hres)) {
797 ERR("Could not get prototype\n");
798 return hres;
801 if(V_VT(&var) == VT_DISPATCH)
802 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
803 VariantClear(&var);
806 hres = init_dispex(dispex, ctx, builtin_info, prot);
808 if(prot)
809 jsdisp_release(prot);
810 return hres;
813 DispatchEx *iface_to_jsdisp(IUnknown *iface)
815 DispatchEx *ret;
816 HRESULT hres;
818 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
819 if(FAILED(hres))
820 return NULL;
822 return ret;
825 HRESULT jsdisp_get_id(DispatchEx *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
827 dispex_prop_t *prop;
828 HRESULT hres;
830 hres = find_prop_name_prot(jsdisp, name, (flags&fdexNameEnsure) != 0, &prop);
831 if(FAILED(hres))
832 return hres;
834 if(prop) {
835 *id = prop_to_id(jsdisp, prop);
836 return S_OK;
839 TRACE("not found %s\n", debugstr_w(name));
840 return DISP_E_UNKNOWNNAME;
843 HRESULT jsdisp_call_value(DispatchEx *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv,
844 jsexcept_t *ei, IServiceProvider *caller)
846 vdisp_t vdisp;
847 HRESULT hres;
849 set_jsdisp(&vdisp, jsthis);
850 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei, caller);
851 vdisp_release(&vdisp);
852 return hres;
855 HRESULT jsdisp_call(DispatchEx *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
856 jsexcept_t *ei, IServiceProvider *caller)
858 dispex_prop_t *prop;
860 memset(ei, 0, sizeof(*ei));
861 if(retv)
862 V_VT(retv) = VT_EMPTY;
864 prop = get_prop(disp, id);
865 if(!prop)
866 return DISP_E_MEMBERNOTFOUND;
868 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
871 HRESULT jsdisp_call_name(DispatchEx *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
872 jsexcept_t *ei, IServiceProvider *caller)
874 dispex_prop_t *prop;
875 HRESULT hres;
877 hres = find_prop_name_prot(disp, name, TRUE, &prop);
878 if(FAILED(hres))
879 return hres;
881 memset(ei, 0, sizeof(*ei));
882 if(retv)
883 V_VT(retv) = VT_EMPTY;
885 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
888 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
889 jsexcept_t *ei, IServiceProvider *caller)
891 DispatchEx *jsdisp;
892 IDispatchEx *dispex;
893 HRESULT hres;
895 jsdisp = iface_to_jsdisp((IUnknown*)disp);
896 if(jsdisp) {
897 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei, caller);
898 jsdisp_release(jsdisp);
899 return hres;
902 memset(ei, 0, sizeof(*ei));
904 if(retv)
905 V_VT(retv) = VT_EMPTY;
906 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
907 if(FAILED(hres)) {
908 UINT err = 0;
910 if(flags == DISPATCH_CONSTRUCT) {
911 WARN("IDispatch cannot be constructor\n");
912 return DISP_E_MEMBERNOTFOUND;
915 TRACE("using IDispatch\n");
916 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
919 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, caller);
920 IDispatchEx_Release(dispex);
922 return hres;
925 HRESULT jsdisp_propput_name(DispatchEx *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
927 DISPID named_arg = DISPID_PROPERTYPUT;
928 DISPPARAMS dp = {val, &named_arg, 1, 1};
929 dispex_prop_t *prop;
930 HRESULT hres;
932 hres = find_prop_name_prot(obj, name, TRUE, &prop);
933 if(FAILED(hres))
934 return hres;
936 return prop_put(obj, prop, &dp, ei, caller);
939 HRESULT jsdisp_propput_idx(DispatchEx *obj, DWORD idx, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
941 WCHAR buf[12];
943 static const WCHAR formatW[] = {'%','d',0};
945 sprintfW(buf, formatW, idx);
946 return jsdisp_propput_name(obj, buf, val, ei, caller);
949 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
951 DISPID dispid = DISPID_PROPERTYPUT;
952 DISPPARAMS dp = {val, &dispid, 1, 1};
953 IDispatchEx *dispex;
954 DispatchEx *jsdisp;
955 HRESULT hres;
957 jsdisp = iface_to_jsdisp((IUnknown*)disp);
958 if(jsdisp) {
959 dispex_prop_t *prop;
961 prop = get_prop(jsdisp, id);
962 if(prop)
963 hres = prop_put(jsdisp, prop, &dp, ei, caller);
964 else
965 hres = DISP_E_MEMBERNOTFOUND;
967 jsdisp_release(jsdisp);
968 return hres;
971 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
972 if(FAILED(hres)) {
973 ULONG err = 0;
975 TRACE("using IDispatch\n");
976 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
979 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
981 IDispatchEx_Release(dispex);
982 return hres;
985 HRESULT jsdisp_propget_name(DispatchEx *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
987 DISPPARAMS dp = {NULL, NULL, 0, 0};
988 dispex_prop_t *prop;
989 HRESULT hres;
991 hres = find_prop_name_prot(obj, name, FALSE, &prop);
992 if(FAILED(hres))
993 return hres;
995 V_VT(var) = VT_EMPTY;
996 if(!prop)
997 return S_OK;
999 return prop_get(obj, prop, &dp, var, ei, caller);
1002 HRESULT jsdisp_propget_idx(DispatchEx *obj, DWORD idx, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1004 WCHAR buf[12];
1006 static const WCHAR formatW[] = {'%','d',0};
1008 sprintfW(buf, formatW, idx);
1009 return jsdisp_propget_name(obj, buf, var, ei, caller);
1012 HRESULT jsdisp_propget(DispatchEx *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1014 DISPPARAMS dp = {NULL,NULL,0,0};
1015 dispex_prop_t *prop;
1017 prop = get_prop(jsdisp, id);
1018 if(!prop)
1019 return DISP_E_MEMBERNOTFOUND;
1021 V_VT(val) = VT_EMPTY;
1022 return prop_get(jsdisp, prop, &dp, val, ei, caller);
1025 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1027 DISPPARAMS dp = {NULL,NULL,0,0};
1028 IDispatchEx *dispex;
1029 DispatchEx *jsdisp;
1030 HRESULT hres;
1032 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1033 if(jsdisp) {
1034 hres = jsdisp_propget(jsdisp, id, val, ei, caller);
1035 jsdisp_release(jsdisp);
1036 return hres;
1039 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1040 if(FAILED(hres)) {
1041 ULONG err = 0;
1043 TRACE("using IDispatch\n");
1044 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1047 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
1048 IDispatchEx_Release(dispex);
1050 return hres;
1053 HRESULT jsdisp_delete_idx(DispatchEx *obj, DWORD idx)
1055 static const WCHAR formatW[] = {'%','d',0};
1056 WCHAR buf[12];
1057 dispex_prop_t *prop;
1058 HRESULT hres;
1060 sprintfW(buf, formatW, idx);
1062 hres = find_prop_name(obj, buf, &prop);
1063 if(FAILED(hres) || !prop)
1064 return hres;
1066 return delete_prop(prop);