Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / jscript / dispex.c
blob1b52ba49596d49f69d2de04d695a18512b57f20c
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 #define FDEX_VERSION_MASK 0xf0000000
35 typedef enum {
36 PROP_VARIANT,
37 PROP_BUILTIN,
38 PROP_PROTREF,
39 PROP_DELETED
40 } prop_type_t;
42 struct _dispex_prop_t {
43 WCHAR *name;
44 prop_type_t type;
45 DWORD flags;
47 union {
48 VARIANT var;
49 const builtin_prop_t *p;
50 DWORD ref;
51 } u;
54 static inline DISPID prop_to_id(DispatchEx *This, dispex_prop_t *prop)
56 return prop - This->props;
59 static inline dispex_prop_t *get_prop(DispatchEx *This, DISPID id)
61 if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
62 return NULL;
64 return This->props+id;
67 static DWORD get_flags(DispatchEx *This, dispex_prop_t *prop)
69 if(prop->type == PROP_PROTREF) {
70 dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
71 if(!parent) {
72 prop->type = PROP_DELETED;
73 return 0;
76 return get_flags(This->prototype, parent);
79 return prop->flags;
82 static const builtin_prop_t *find_builtin_prop(DispatchEx *This, const WCHAR *name)
84 int min = 0, max, i, r;
86 max = This->builtin_info->props_cnt-1;
87 while(min <= max) {
88 i = (min+max)/2;
90 r = strcmpW(name, This->builtin_info->props[i].name);
91 if(!r)
92 return This->builtin_info->props + i;
94 if(r < 0)
95 max = i-1;
96 else
97 min = i+1;
100 return NULL;
103 static dispex_prop_t *alloc_prop(DispatchEx *This, const WCHAR *name, prop_type_t type, DWORD flags)
105 dispex_prop_t *ret;
107 if(This->buf_size == This->prop_cnt) {
108 dispex_prop_t *tmp = heap_realloc(This->props, (This->buf_size<<=1)*sizeof(*This->props));
109 if(!tmp)
110 return NULL;
111 This->props = tmp;
114 ret = This->props + This->prop_cnt++;
115 ret->type = type;
116 ret->flags = flags;
117 ret->name = heap_strdupW(name);
118 if(!ret->name)
119 return NULL;
121 return ret;
124 static dispex_prop_t *alloc_protref(DispatchEx *This, const WCHAR *name, DWORD ref)
126 dispex_prop_t *ret;
128 ret = alloc_prop(This, name, PROP_PROTREF, 0);
129 if(!ret)
130 return NULL;
132 ret->u.ref = ref;
133 return ret;
136 static HRESULT find_prop_name(DispatchEx *This, const WCHAR *name, dispex_prop_t **ret)
138 const builtin_prop_t *builtin;
139 dispex_prop_t *prop;
141 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
142 if(prop->name && !strcmpW(prop->name, name)) {
143 *ret = prop;
144 return S_OK;
148 builtin = find_builtin_prop(This, name);
149 if(builtin) {
150 prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
151 if(!prop)
152 return E_OUTOFMEMORY;
154 prop->u.p = builtin;
155 *ret = prop;
156 return S_OK;
159 *ret = NULL;
160 return S_OK;
163 static HRESULT find_prop_name_prot(DispatchEx *This, const WCHAR *name, BOOL alloc, dispex_prop_t **ret)
165 dispex_prop_t *prop;
166 HRESULT hres;
168 hres = find_prop_name(This, name, &prop);
169 if(FAILED(hres))
170 return hres;
171 if(prop) {
172 *ret = prop;
173 return S_OK;
176 if(This->prototype) {
177 hres = find_prop_name_prot(This->prototype, name, FALSE, &prop);
178 if(FAILED(hres))
179 return hres;
180 if(prop) {
181 prop = alloc_protref(This, prop->name, prop - This->prototype->props);
182 if(!prop)
183 return E_OUTOFMEMORY;
184 *ret = prop;
185 return S_OK;
189 if(alloc) {
190 TRACE("creating prop %s\n", debugstr_w(name));
192 prop = alloc_prop(This, name, PROP_VARIANT, PROPF_ENUM);
193 if(!prop)
194 return E_OUTOFMEMORY;
195 VariantInit(&prop->u.var);
198 *ret = prop;
199 return S_OK;
202 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
204 VARIANTARG *oldargs;
205 int i;
207 static DISPID this_id = DISPID_THIS;
209 *dp = *olddp;
211 for(i = 0; i < dp->cNamedArgs; i++) {
212 if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
213 return S_OK;
216 oldargs = dp->rgvarg;
217 dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
218 if(!dp->rgvarg)
219 return E_OUTOFMEMORY;
220 memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
221 V_VT(dp->rgvarg) = VT_DISPATCH;
222 V_DISPATCH(dp->rgvarg) = jsthis;
223 dp->cArgs++;
225 if(dp->cNamedArgs) {
226 DISPID *old = dp->rgdispidNamedArgs;
227 dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
228 if(!dp->rgdispidNamedArgs) {
229 heap_free(dp->rgvarg);
230 return E_OUTOFMEMORY;
233 memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
234 dp->rgdispidNamedArgs[0] = DISPID_THIS;
235 dp->cNamedArgs++;
236 }else {
237 dp->rgdispidNamedArgs = &this_id;
238 dp->cNamedArgs = 1;
241 return S_OK;
244 static HRESULT invoke_prop_func(DispatchEx *This, DispatchEx *jsthis, dispex_prop_t *prop, WORD flags,
245 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
247 HRESULT hres;
249 switch(prop->type) {
250 case PROP_BUILTIN: {
251 vdisp_t vthis;
253 if(flags == DISPATCH_CONSTRUCT && (prop->flags & DISPATCH_METHOD)) {
254 WARN("%s is not a constructor\n", debugstr_w(prop->name));
255 return E_INVALIDARG;
258 set_jsdisp(&vthis, jsthis);
259 hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei, caller);
260 vdisp_release(&vthis);
261 return hres;
263 case PROP_PROTREF:
264 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
265 case PROP_VARIANT: {
266 DISPPARAMS new_dp;
268 if(V_VT(&prop->u.var) != VT_DISPATCH) {
269 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
270 return E_FAIL;
273 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
275 hres = set_this(&new_dp, dp, (IDispatch*)_IDispatchEx_(jsthis));
276 if(FAILED(hres))
277 return hres;
279 hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei, caller);
281 if(new_dp.rgvarg != dp->rgvarg) {
282 heap_free(new_dp.rgvarg);
283 if(new_dp.cNamedArgs > 1)
284 heap_free(new_dp.rgdispidNamedArgs);
287 return hres;
289 default:
290 ERR("type %d\n", prop->type);
293 return E_FAIL;
296 static HRESULT prop_get(DispatchEx *This, dispex_prop_t *prop, DISPPARAMS *dp,
297 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
299 HRESULT hres;
301 switch(prop->type) {
302 case PROP_BUILTIN:
303 if(prop->u.p->flags & PROPF_METHOD) {
304 DispatchEx *obj;
305 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
306 prop->u.p->flags, NULL, &obj);
307 if(FAILED(hres))
308 break;
310 prop->type = PROP_VARIANT;
311 V_VT(&prop->u.var) = VT_DISPATCH;
312 V_DISPATCH(&prop->u.var) = (IDispatch*)_IDispatchEx_(obj);
314 hres = VariantCopy(retv, &prop->u.var);
315 }else {
316 vdisp_t vthis;
318 set_jsdisp(&vthis, This);
319 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
320 vdisp_release(&vthis);
322 break;
323 case PROP_PROTREF:
324 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
325 break;
326 case PROP_VARIANT:
327 hres = VariantCopy(retv, &prop->u.var);
328 break;
329 default:
330 ERR("type %d\n", prop->type);
331 return E_FAIL;
334 if(FAILED(hres)) {
335 TRACE("fail %08x\n", hres);
336 return hres;
339 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
340 return hres;
343 static HRESULT prop_put(DispatchEx *This, dispex_prop_t *prop, DISPPARAMS *dp,
344 jsexcept_t *ei, IServiceProvider *caller)
346 DWORD i;
347 HRESULT hres;
349 switch(prop->type) {
350 case PROP_BUILTIN:
351 if(!(prop->flags & PROPF_METHOD)) {
352 vdisp_t vthis;
354 set_jsdisp(&vthis, This);
355 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, dp, NULL, ei, caller);
356 vdisp_release(&vthis);
357 return hres;
359 case PROP_PROTREF:
360 prop->type = PROP_VARIANT;
361 prop->flags = PROPF_ENUM;
362 V_VT(&prop->u.var) = VT_EMPTY;
363 break;
364 case PROP_VARIANT:
365 VariantClear(&prop->u.var);
366 break;
367 default:
368 ERR("type %d\n", prop->type);
369 return E_FAIL;
372 for(i=0; i < dp->cNamedArgs; i++) {
373 if(dp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
374 break;
377 if(i == dp->cNamedArgs) {
378 TRACE("no value to set\n");
379 return DISP_E_PARAMNOTOPTIONAL;
382 hres = VariantCopy(&prop->u.var, dp->rgvarg+i);
383 if(FAILED(hres))
384 return hres;
386 if(This->builtin_info->on_put)
387 This->builtin_info->on_put(This, prop->name);
389 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(dp->rgvarg+i));
390 return S_OK;
393 static HRESULT fill_protrefs(DispatchEx *This)
395 dispex_prop_t *iter, *prop;
396 HRESULT hres;
398 if(!This->prototype)
399 return S_OK;
401 fill_protrefs(This->prototype);
403 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
404 if(!iter->name)
405 continue;
406 hres = find_prop_name(This, iter->name, &prop);
407 if(FAILED(hres))
408 return hres;
409 if(!prop) {
410 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
411 if(!prop)
412 return E_OUTOFMEMORY;
416 return S_OK;
419 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
421 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
423 DispatchEx *This = DISPATCHEX_THIS(iface);
425 if(IsEqualGUID(&IID_IUnknown, riid)) {
426 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
427 *ppv = _IDispatchEx_(This);
428 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
429 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
430 *ppv = _IDispatchEx_(This);
431 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
432 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
433 *ppv = _IDispatchEx_(This);
434 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
435 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
436 IUnknown_AddRef(_IDispatchEx_(This));
437 *ppv = This;
438 return S_OK;
439 }else {
440 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
441 *ppv = NULL;
442 return E_NOINTERFACE;
445 IUnknown_AddRef((IUnknown*)*ppv);
446 return S_OK;
449 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
451 DispatchEx *This = DISPATCHEX_THIS(iface);
452 LONG ref = InterlockedIncrement(&This->ref);
454 TRACE("(%p) ref=%d\n", This, ref);
456 return ref;
459 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
461 DispatchEx *This = DISPATCHEX_THIS(iface);
462 LONG ref = InterlockedDecrement(&This->ref);
464 TRACE("(%p) ref=%d\n", This, ref);
466 if(!ref) {
467 dispex_prop_t *prop;
469 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
470 if(prop->type == PROP_VARIANT)
471 VariantClear(&prop->u.var);
472 heap_free(prop->name);
474 heap_free(This->props);
475 script_release(This->ctx);
476 if(This->prototype)
477 jsdisp_release(This->prototype);
479 if(This->builtin_info->destructor)
480 This->builtin_info->destructor(This);
481 else
482 heap_free(This);
485 return ref;
488 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
490 DispatchEx *This = DISPATCHEX_THIS(iface);
492 TRACE("(%p)->(%p)\n", This, pctinfo);
494 *pctinfo = 1;
495 return S_OK;
498 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
499 ITypeInfo **ppTInfo)
501 DispatchEx *This = DISPATCHEX_THIS(iface);
502 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
503 return E_NOTIMPL;
506 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
507 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
508 DISPID *rgDispId)
510 DispatchEx *This = DISPATCHEX_THIS(iface);
511 UINT i;
512 HRESULT hres;
514 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
515 lcid, rgDispId);
517 for(i=0; i < cNames; i++) {
518 hres = IDispatchEx_GetDispID(_IDispatchEx_(This), rgszNames[i], 0, rgDispId+i);
519 if(FAILED(hres))
520 return hres;
523 return S_OK;
526 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
527 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
528 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
530 DispatchEx *This = DISPATCHEX_THIS(iface);
532 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
533 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
535 return IDispatchEx_InvokeEx(_IDispatchEx_(This), dispIdMember, lcid, wFlags,
536 pDispParams, pVarResult, pExcepInfo, NULL);
539 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
541 DispatchEx *This = DISPATCHEX_THIS(iface);
543 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
545 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
546 FIXME("Unsupported grfdex %x\n", grfdex);
547 return E_NOTIMPL;
550 return jsdisp_get_id(This, bstrName, grfdex, pid);
553 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
554 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
556 DispatchEx *This = DISPATCHEX_THIS(iface);
557 dispex_prop_t *prop;
558 jsexcept_t jsexcept;
559 HRESULT hres;
561 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
563 if(pvarRes)
564 V_VT(pvarRes) = VT_EMPTY;
566 prop = get_prop(This, id);
567 if(!prop || prop->type == PROP_DELETED) {
568 TRACE("invalid id\n");
569 return DISP_E_MEMBERNOTFOUND;
572 memset(&jsexcept, 0, sizeof(jsexcept));
574 switch(wFlags) {
575 case DISPATCH_METHOD:
576 case DISPATCH_CONSTRUCT:
577 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
578 break;
579 case DISPATCH_PROPERTYGET:
580 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
581 break;
582 case DISPATCH_PROPERTYPUT:
583 hres = prop_put(This, prop, pdp, &jsexcept, pspCaller);
584 break;
585 default:
586 FIXME("Unimplemented flags %x\n", wFlags);
587 return E_INVALIDARG;
590 if(pei)
591 *pei = jsexcept.ei;
593 return hres;
596 static HRESULT delete_prop(dispex_prop_t *prop)
598 heap_free(prop->name);
599 prop->name = NULL;
600 prop->type = PROP_DELETED;
602 return S_OK;
605 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
607 DispatchEx *This = DISPATCHEX_THIS(iface);
608 dispex_prop_t *prop;
609 HRESULT hres;
611 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
613 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
614 FIXME("Unsupported grfdex %x\n", grfdex);
616 hres = find_prop_name(This, bstrName, &prop);
617 if(FAILED(hres))
618 return hres;
619 if(!prop) {
620 TRACE("not found\n");
621 return S_OK;
624 return delete_prop(prop);
627 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
629 DispatchEx *This = DISPATCHEX_THIS(iface);
630 dispex_prop_t *prop;
632 TRACE("(%p)->(%x)\n", This, id);
634 prop = get_prop(This, id);
635 if(!prop) {
636 WARN("invalid id\n");
637 return DISP_E_MEMBERNOTFOUND;
640 return delete_prop(prop);
643 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
645 DispatchEx *This = DISPATCHEX_THIS(iface);
646 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
647 return E_NOTIMPL;
650 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
652 DispatchEx *This = DISPATCHEX_THIS(iface);
653 dispex_prop_t *prop;
655 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
657 prop = get_prop(This, id);
658 if(!prop || !prop->name || prop->type == PROP_DELETED)
659 return DISP_E_MEMBERNOTFOUND;
661 *pbstrName = SysAllocString(prop->name);
662 if(!*pbstrName)
663 return E_OUTOFMEMORY;
665 return S_OK;
668 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
670 DispatchEx *This = DISPATCHEX_THIS(iface);
671 dispex_prop_t *iter;
672 HRESULT hres;
674 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
676 if(id == DISPID_STARTENUM) {
677 hres = fill_protrefs(This);
678 if(FAILED(hres))
679 return hres;
682 iter = get_prop(This, id+1);
683 if(!iter) {
684 *pid = DISPID_STARTENUM;
685 return S_FALSE;
688 while(iter < This->props + This->prop_cnt) {
689 if(iter->name && (get_flags(This, iter) & PROPF_ENUM)) {
690 *pid = prop_to_id(This, iter);
691 return S_OK;
693 iter++;
696 *pid = DISPID_STARTENUM;
697 return S_FALSE;
700 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
702 DispatchEx *This = DISPATCHEX_THIS(iface);
703 FIXME("(%p)->(%p)\n", This, ppunk);
704 return E_NOTIMPL;
707 #undef DISPATCHEX_THIS
709 static IDispatchExVtbl DispatchExVtbl = {
710 DispatchEx_QueryInterface,
711 DispatchEx_AddRef,
712 DispatchEx_Release,
713 DispatchEx_GetTypeInfoCount,
714 DispatchEx_GetTypeInfo,
715 DispatchEx_GetIDsOfNames,
716 DispatchEx_Invoke,
717 DispatchEx_GetDispID,
718 DispatchEx_InvokeEx,
719 DispatchEx_DeleteMemberByName,
720 DispatchEx_DeleteMemberByDispID,
721 DispatchEx_GetMemberProperties,
722 DispatchEx_GetMemberName,
723 DispatchEx_GetNextDispID,
724 DispatchEx_GetNameSpaceParent
727 HRESULT init_dispex(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype)
729 TRACE("%p (%p)\n", dispex, prototype);
731 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
732 dispex->ref = 1;
733 dispex->builtin_info = builtin_info;
735 dispex->props = heap_alloc((dispex->buf_size=4) * sizeof(dispex_prop_t));
736 if(!dispex->props)
737 return E_OUTOFMEMORY;
739 dispex->prototype = prototype;
740 if(prototype)
741 IDispatchEx_AddRef(_IDispatchEx_(prototype));
743 dispex->prop_cnt = 1;
744 dispex->props[0].name = NULL;
745 dispex->props[0].flags = 0;
746 if(builtin_info->value_prop.invoke) {
747 dispex->props[0].type = PROP_BUILTIN;
748 dispex->props[0].u.p = &builtin_info->value_prop;
749 }else {
750 dispex->props[0].type = PROP_DELETED;
753 script_addref(ctx);
754 dispex->ctx = ctx;
756 return S_OK;
759 static const builtin_info_t dispex_info = {
760 JSCLASS_NONE,
761 {NULL, NULL, 0},
762 0, NULL,
763 NULL,
764 NULL
767 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype, DispatchEx **dispex)
769 DispatchEx *ret;
770 HRESULT hres;
772 ret = heap_alloc_zero(sizeof(DispatchEx));
773 if(!ret)
774 return E_OUTOFMEMORY;
776 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
777 if(FAILED(hres))
778 return hres;
780 *dispex = ret;
781 return S_OK;
784 HRESULT init_dispex_from_constr(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *constr)
786 DispatchEx *prot = NULL;
787 dispex_prop_t *prop;
788 HRESULT hres;
790 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
792 hres = find_prop_name_prot(constr, prototypeW, FALSE, &prop);
793 if(SUCCEEDED(hres) && prop) {
794 jsexcept_t jsexcept;
795 VARIANT var;
797 V_VT(&var) = VT_EMPTY;
798 memset(&jsexcept, 0, sizeof(jsexcept));
799 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
800 if(FAILED(hres)) {
801 ERR("Could not get prototype\n");
802 return hres;
805 if(V_VT(&var) == VT_DISPATCH)
806 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
807 VariantClear(&var);
810 hres = init_dispex(dispex, ctx, builtin_info, prot);
812 if(prot)
813 jsdisp_release(prot);
814 return hres;
817 DispatchEx *iface_to_jsdisp(IUnknown *iface)
819 DispatchEx *ret;
820 HRESULT hres;
822 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
823 if(FAILED(hres))
824 return NULL;
826 return ret;
829 HRESULT jsdisp_get_id(DispatchEx *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
831 dispex_prop_t *prop;
832 HRESULT hres;
834 hres = find_prop_name_prot(jsdisp, name, (flags&fdexNameEnsure) != 0, &prop);
835 if(FAILED(hres))
836 return hres;
838 if(prop) {
839 *id = prop_to_id(jsdisp, prop);
840 return S_OK;
843 TRACE("not found %s\n", debugstr_w(name));
844 return DISP_E_UNKNOWNNAME;
847 HRESULT jsdisp_call_value(DispatchEx *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv,
848 jsexcept_t *ei, IServiceProvider *caller)
850 vdisp_t vdisp;
851 HRESULT hres;
853 set_jsdisp(&vdisp, jsthis);
854 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei, caller);
855 vdisp_release(&vdisp);
856 return hres;
859 HRESULT jsdisp_call(DispatchEx *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
860 jsexcept_t *ei, IServiceProvider *caller)
862 dispex_prop_t *prop;
864 memset(ei, 0, sizeof(*ei));
865 if(retv)
866 V_VT(retv) = VT_EMPTY;
868 prop = get_prop(disp, id);
869 if(!prop)
870 return DISP_E_MEMBERNOTFOUND;
872 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
875 HRESULT jsdisp_call_name(DispatchEx *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
876 jsexcept_t *ei, IServiceProvider *caller)
878 dispex_prop_t *prop;
879 HRESULT hres;
881 hres = find_prop_name_prot(disp, name, TRUE, &prop);
882 if(FAILED(hres))
883 return hres;
885 memset(ei, 0, sizeof(*ei));
886 if(retv)
887 V_VT(retv) = VT_EMPTY;
889 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
892 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
893 jsexcept_t *ei, IServiceProvider *caller)
895 DispatchEx *jsdisp;
896 IDispatchEx *dispex;
897 HRESULT hres;
899 jsdisp = iface_to_jsdisp((IUnknown*)disp);
900 if(jsdisp) {
901 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei, caller);
902 jsdisp_release(jsdisp);
903 return hres;
906 memset(ei, 0, sizeof(*ei));
908 if(retv)
909 V_VT(retv) = VT_EMPTY;
910 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
911 if(FAILED(hres)) {
912 UINT err = 0;
914 if(flags == DISPATCH_CONSTRUCT) {
915 WARN("IDispatch cannot be constructor\n");
916 return DISP_E_MEMBERNOTFOUND;
919 TRACE("using IDispatch\n");
920 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
923 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, caller);
924 IDispatchEx_Release(dispex);
926 return hres;
929 HRESULT jsdisp_propput_name(DispatchEx *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
931 DISPID named_arg = DISPID_PROPERTYPUT;
932 DISPPARAMS dp = {val, &named_arg, 1, 1};
933 dispex_prop_t *prop;
934 HRESULT hres;
936 hres = find_prop_name_prot(obj, name, TRUE, &prop);
937 if(FAILED(hres))
938 return hres;
940 return prop_put(obj, prop, &dp, ei, caller);
943 HRESULT jsdisp_propput_idx(DispatchEx *obj, DWORD idx, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
945 WCHAR buf[12];
947 static const WCHAR formatW[] = {'%','d',0};
949 sprintfW(buf, formatW, idx);
950 return jsdisp_propput_name(obj, buf, val, ei, caller);
953 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
955 DISPID dispid = DISPID_PROPERTYPUT;
956 DISPPARAMS dp = {val, &dispid, 1, 1};
957 IDispatchEx *dispex;
958 DispatchEx *jsdisp;
959 HRESULT hres;
961 jsdisp = iface_to_jsdisp((IUnknown*)disp);
962 if(jsdisp) {
963 dispex_prop_t *prop;
965 prop = get_prop(jsdisp, id);
966 if(prop)
967 hres = prop_put(jsdisp, prop, &dp, ei, caller);
968 else
969 hres = DISP_E_MEMBERNOTFOUND;
971 jsdisp_release(jsdisp);
972 return hres;
975 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
976 if(FAILED(hres)) {
977 ULONG err = 0;
979 TRACE("using IDispatch\n");
980 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
983 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
985 IDispatchEx_Release(dispex);
986 return hres;
989 HRESULT jsdisp_propget_name(DispatchEx *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
991 DISPPARAMS dp = {NULL, NULL, 0, 0};
992 dispex_prop_t *prop;
993 HRESULT hres;
995 hres = find_prop_name_prot(obj, name, FALSE, &prop);
996 if(FAILED(hres))
997 return hres;
999 V_VT(var) = VT_EMPTY;
1000 if(!prop)
1001 return S_OK;
1003 return prop_get(obj, prop, &dp, var, ei, caller);
1006 HRESULT jsdisp_propget_idx(DispatchEx *obj, DWORD idx, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1008 WCHAR buf[12];
1010 static const WCHAR formatW[] = {'%','d',0};
1012 sprintfW(buf, formatW, idx);
1013 return jsdisp_propget_name(obj, buf, var, ei, caller);
1016 HRESULT jsdisp_propget(DispatchEx *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1018 DISPPARAMS dp = {NULL,NULL,0,0};
1019 dispex_prop_t *prop;
1021 prop = get_prop(jsdisp, id);
1022 if(!prop)
1023 return DISP_E_MEMBERNOTFOUND;
1025 V_VT(val) = VT_EMPTY;
1026 return prop_get(jsdisp, prop, &dp, val, ei, caller);
1029 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1031 DISPPARAMS dp = {NULL,NULL,0,0};
1032 IDispatchEx *dispex;
1033 DispatchEx *jsdisp;
1034 HRESULT hres;
1036 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1037 if(jsdisp) {
1038 hres = jsdisp_propget(jsdisp, id, val, ei, caller);
1039 jsdisp_release(jsdisp);
1040 return hres;
1043 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1044 if(FAILED(hres)) {
1045 ULONG err = 0;
1047 TRACE("using IDispatch\n");
1048 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1051 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
1052 IDispatchEx_Release(dispex);
1054 return hres;
1057 HRESULT jsdisp_delete_idx(DispatchEx *obj, DWORD idx)
1059 static const WCHAR formatW[] = {'%','d',0};
1060 WCHAR buf[12];
1061 dispex_prop_t *prop;
1062 HRESULT hres;
1064 sprintfW(buf, formatW, idx);
1066 hres = find_prop_name(obj, buf, &prop);
1067 if(FAILED(hres) || !prop)
1068 return hres;
1070 return delete_prop(prop);