shdocvw: Get rid of more *_THIS macros.
[wine/multimedia.git] / dlls / jscript / dispex.c
blob99fad569def6f6592586b9322b57e01848d9b5c1
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
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(jsdisp_t *This, dispex_prop_t *prop)
56 return prop - This->props;
59 static inline dispex_prop_t *get_prop(jsdisp_t *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(jsdisp_t *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(jsdisp_t *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(jsdisp_t *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(jsdisp_t *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(jsdisp_t *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(jsdisp_t *This, const WCHAR *name, 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, &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 *ret = prop;
190 return S_OK;
193 static HRESULT ensure_prop_name(jsdisp_t *This, const WCHAR *name, BOOL search_prot, DWORD create_flags, dispex_prop_t **ret)
195 dispex_prop_t *prop;
196 HRESULT hres;
198 if(search_prot)
199 hres = find_prop_name_prot(This, name, &prop);
200 else
201 hres = find_prop_name(This, name, &prop);
202 if(SUCCEEDED(hres) && !prop) {
203 TRACE("creating prop %s\n", debugstr_w(name));
205 prop = alloc_prop(This, name, PROP_VARIANT, create_flags);
206 if(!prop)
207 return E_OUTOFMEMORY;
208 VariantInit(&prop->u.var);
211 *ret = prop;
212 return hres;
215 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
217 VARIANTARG *oldargs;
218 int i;
220 static DISPID this_id = DISPID_THIS;
222 *dp = *olddp;
224 for(i = 0; i < dp->cNamedArgs; i++) {
225 if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
226 return S_OK;
229 oldargs = dp->rgvarg;
230 dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
231 if(!dp->rgvarg)
232 return E_OUTOFMEMORY;
233 memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
234 V_VT(dp->rgvarg) = VT_DISPATCH;
235 V_DISPATCH(dp->rgvarg) = jsthis;
236 dp->cArgs++;
238 if(dp->cNamedArgs) {
239 DISPID *old = dp->rgdispidNamedArgs;
240 dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
241 if(!dp->rgdispidNamedArgs) {
242 heap_free(dp->rgvarg);
243 return E_OUTOFMEMORY;
246 memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
247 dp->rgdispidNamedArgs[0] = DISPID_THIS;
248 dp->cNamedArgs++;
249 }else {
250 dp->rgdispidNamedArgs = &this_id;
251 dp->cNamedArgs = 1;
254 return S_OK;
257 static HRESULT invoke_prop_func(jsdisp_t *This, jsdisp_t *jsthis, dispex_prop_t *prop, WORD flags,
258 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
260 HRESULT hres;
262 switch(prop->type) {
263 case PROP_BUILTIN: {
264 vdisp_t vthis;
266 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
267 WARN("%s is not a constructor\n", debugstr_w(prop->name));
268 return E_INVALIDARG;
271 set_jsdisp(&vthis, jsthis);
272 hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei, caller);
273 vdisp_release(&vthis);
274 return hres;
276 case PROP_PROTREF:
277 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
278 case PROP_VARIANT: {
279 DISPPARAMS new_dp;
281 if(V_VT(&prop->u.var) != VT_DISPATCH) {
282 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
283 return E_FAIL;
286 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
288 hres = set_this(&new_dp, dp, to_disp(jsthis));
289 if(FAILED(hres))
290 return hres;
292 hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei, caller);
294 if(new_dp.rgvarg != dp->rgvarg) {
295 heap_free(new_dp.rgvarg);
296 if(new_dp.cNamedArgs > 1)
297 heap_free(new_dp.rgdispidNamedArgs);
300 return hres;
302 default:
303 ERR("type %d\n", prop->type);
306 return E_FAIL;
309 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
310 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
312 HRESULT hres;
314 switch(prop->type) {
315 case PROP_BUILTIN:
316 if(prop->u.p->flags & PROPF_METHOD) {
317 jsdisp_t *obj;
318 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
319 prop->u.p->flags, NULL, &obj);
320 if(FAILED(hres))
321 break;
323 prop->type = PROP_VARIANT;
324 var_set_jsdisp(&prop->u.var, obj);
325 hres = VariantCopy(retv, &prop->u.var);
326 }else {
327 vdisp_t vthis;
329 set_jsdisp(&vthis, This);
330 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
331 vdisp_release(&vthis);
333 break;
334 case PROP_PROTREF:
335 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
336 break;
337 case PROP_VARIANT:
338 hres = VariantCopy(retv, &prop->u.var);
339 break;
340 default:
341 ERR("type %d\n", prop->type);
342 return E_FAIL;
345 if(FAILED(hres)) {
346 TRACE("fail %08x\n", hres);
347 return hres;
350 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
351 return hres;
354 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
355 jsexcept_t *ei, IServiceProvider *caller)
357 HRESULT hres;
359 if(prop->flags & PROPF_CONST)
360 return S_OK;
362 switch(prop->type) {
363 case PROP_BUILTIN:
364 if(!(prop->flags & PROPF_METHOD)) {
365 DISPPARAMS dp = {val, NULL, 1, 0};
366 vdisp_t vthis;
368 set_jsdisp(&vthis, This);
369 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei, caller);
370 vdisp_release(&vthis);
371 return hres;
373 case PROP_PROTREF:
374 prop->type = PROP_VARIANT;
375 prop->flags = PROPF_ENUM;
376 V_VT(&prop->u.var) = VT_EMPTY;
377 break;
378 case PROP_VARIANT:
379 VariantClear(&prop->u.var);
380 break;
381 default:
382 ERR("type %d\n", prop->type);
383 return E_FAIL;
386 hres = VariantCopy(&prop->u.var, val);
387 if(FAILED(hres))
388 return hres;
390 if(This->builtin_info->on_put)
391 This->builtin_info->on_put(This, prop->name);
393 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
394 return S_OK;
397 static HRESULT fill_protrefs(jsdisp_t *This)
399 dispex_prop_t *iter, *prop;
400 HRESULT hres;
402 if(!This->prototype)
403 return S_OK;
405 fill_protrefs(This->prototype);
407 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
408 if(!iter->name)
409 continue;
410 hres = find_prop_name(This, iter->name, &prop);
411 if(FAILED(hres))
412 return hres;
413 if(!prop) {
414 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
415 if(!prop)
416 return E_OUTOFMEMORY;
420 return S_OK;
423 #define DISPATCHEX_THIS(iface) DEFINE_THIS(jsdisp_t, IDispatchEx, iface)
425 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
427 jsdisp_t *This = DISPATCHEX_THIS(iface);
429 if(IsEqualGUID(&IID_IUnknown, riid)) {
430 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
431 *ppv = _IDispatchEx_(This);
432 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
433 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
434 *ppv = _IDispatchEx_(This);
435 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
436 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
437 *ppv = _IDispatchEx_(This);
438 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
439 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
440 jsdisp_addref(This);
441 *ppv = This;
442 return S_OK;
443 }else {
444 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
445 *ppv = NULL;
446 return E_NOINTERFACE;
449 IUnknown_AddRef((IUnknown*)*ppv);
450 return S_OK;
453 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
455 jsdisp_t *This = DISPATCHEX_THIS(iface);
456 LONG ref = InterlockedIncrement(&This->ref);
458 TRACE("(%p) ref=%d\n", This, ref);
460 return ref;
463 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
465 jsdisp_t *This = DISPATCHEX_THIS(iface);
466 LONG ref = InterlockedDecrement(&This->ref);
468 TRACE("(%p) ref=%d\n", This, ref);
470 if(!ref) {
471 dispex_prop_t *prop;
473 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
474 if(prop->type == PROP_VARIANT)
475 VariantClear(&prop->u.var);
476 heap_free(prop->name);
478 heap_free(This->props);
479 script_release(This->ctx);
480 if(This->prototype)
481 jsdisp_release(This->prototype);
483 if(This->builtin_info->destructor)
484 This->builtin_info->destructor(This);
485 else
486 heap_free(This);
489 return ref;
492 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
494 jsdisp_t *This = DISPATCHEX_THIS(iface);
496 TRACE("(%p)->(%p)\n", This, pctinfo);
498 *pctinfo = 1;
499 return S_OK;
502 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
503 ITypeInfo **ppTInfo)
505 jsdisp_t *This = DISPATCHEX_THIS(iface);
506 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
507 return E_NOTIMPL;
510 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
511 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
512 DISPID *rgDispId)
514 jsdisp_t *This = DISPATCHEX_THIS(iface);
515 UINT i;
516 HRESULT hres;
518 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
519 lcid, rgDispId);
521 for(i=0; i < cNames; i++) {
522 hres = IDispatchEx_GetDispID(_IDispatchEx_(This), rgszNames[i], 0, rgDispId+i);
523 if(FAILED(hres))
524 return hres;
527 return S_OK;
530 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
531 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
532 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
534 jsdisp_t *This = DISPATCHEX_THIS(iface);
536 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
537 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
539 return IDispatchEx_InvokeEx(_IDispatchEx_(This), dispIdMember, lcid, wFlags,
540 pDispParams, pVarResult, pExcepInfo, NULL);
543 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
545 jsdisp_t *This = DISPATCHEX_THIS(iface);
547 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
549 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
550 FIXME("Unsupported grfdex %x\n", grfdex);
551 return E_NOTIMPL;
554 return jsdisp_get_id(This, bstrName, grfdex, pid);
557 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
558 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
560 jsdisp_t *This = DISPATCHEX_THIS(iface);
561 dispex_prop_t *prop;
562 jsexcept_t jsexcept;
563 HRESULT hres;
565 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
567 if(pvarRes)
568 V_VT(pvarRes) = VT_EMPTY;
570 prop = get_prop(This, id);
571 if(!prop || prop->type == PROP_DELETED) {
572 TRACE("invalid id\n");
573 return DISP_E_MEMBERNOTFOUND;
576 memset(&jsexcept, 0, sizeof(jsexcept));
578 switch(wFlags) {
579 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
580 wFlags = DISPATCH_METHOD;
581 case DISPATCH_METHOD:
582 case DISPATCH_CONSTRUCT:
583 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
584 break;
585 case DISPATCH_PROPERTYGET:
586 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
587 break;
588 case DISPATCH_PROPERTYPUT: {
589 DWORD i;
591 for(i=0; i < pdp->cNamedArgs; i++) {
592 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
593 break;
596 if(i == pdp->cNamedArgs) {
597 TRACE("no value to set\n");
598 return DISP_E_PARAMNOTOPTIONAL;
601 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
602 break;
604 default:
605 FIXME("Unimplemented flags %x\n", wFlags);
606 return E_INVALIDARG;
609 if(pei)
610 *pei = jsexcept.ei;
612 return hres;
615 static HRESULT delete_prop(dispex_prop_t *prop)
617 heap_free(prop->name);
618 prop->name = NULL;
619 prop->type = PROP_DELETED;
621 return S_OK;
624 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
626 jsdisp_t *This = DISPATCHEX_THIS(iface);
627 dispex_prop_t *prop;
628 HRESULT hres;
630 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
632 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
633 FIXME("Unsupported grfdex %x\n", grfdex);
635 hres = find_prop_name(This, bstrName, &prop);
636 if(FAILED(hres))
637 return hres;
638 if(!prop) {
639 TRACE("not found\n");
640 return S_OK;
643 return delete_prop(prop);
646 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
648 jsdisp_t *This = DISPATCHEX_THIS(iface);
649 dispex_prop_t *prop;
651 TRACE("(%p)->(%x)\n", This, id);
653 prop = get_prop(This, id);
654 if(!prop) {
655 WARN("invalid id\n");
656 return DISP_E_MEMBERNOTFOUND;
659 return delete_prop(prop);
662 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
664 jsdisp_t *This = DISPATCHEX_THIS(iface);
665 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
666 return E_NOTIMPL;
669 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
671 jsdisp_t *This = DISPATCHEX_THIS(iface);
672 dispex_prop_t *prop;
674 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
676 prop = get_prop(This, id);
677 if(!prop || !prop->name || prop->type == PROP_DELETED)
678 return DISP_E_MEMBERNOTFOUND;
680 *pbstrName = SysAllocString(prop->name);
681 if(!*pbstrName)
682 return E_OUTOFMEMORY;
684 return S_OK;
687 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
689 jsdisp_t *This = DISPATCHEX_THIS(iface);
690 dispex_prop_t *iter;
691 HRESULT hres;
693 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
695 if(id == DISPID_STARTENUM) {
696 hres = fill_protrefs(This);
697 if(FAILED(hres))
698 return hres;
701 iter = get_prop(This, id+1);
702 if(!iter) {
703 *pid = DISPID_STARTENUM;
704 return S_FALSE;
707 while(iter < This->props + This->prop_cnt) {
708 if(iter->name && (get_flags(This, iter) & PROPF_ENUM)) {
709 *pid = prop_to_id(This, iter);
710 return S_OK;
712 iter++;
715 *pid = DISPID_STARTENUM;
716 return S_FALSE;
719 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
721 jsdisp_t *This = DISPATCHEX_THIS(iface);
722 FIXME("(%p)->(%p)\n", This, ppunk);
723 return E_NOTIMPL;
726 #undef DISPATCHEX_THIS
728 static IDispatchExVtbl DispatchExVtbl = {
729 DispatchEx_QueryInterface,
730 DispatchEx_AddRef,
731 DispatchEx_Release,
732 DispatchEx_GetTypeInfoCount,
733 DispatchEx_GetTypeInfo,
734 DispatchEx_GetIDsOfNames,
735 DispatchEx_Invoke,
736 DispatchEx_GetDispID,
737 DispatchEx_InvokeEx,
738 DispatchEx_DeleteMemberByName,
739 DispatchEx_DeleteMemberByDispID,
740 DispatchEx_GetMemberProperties,
741 DispatchEx_GetMemberName,
742 DispatchEx_GetNextDispID,
743 DispatchEx_GetNameSpaceParent
746 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
748 TRACE("%p (%p)\n", dispex, prototype);
750 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
751 dispex->ref = 1;
752 dispex->builtin_info = builtin_info;
754 dispex->props = heap_alloc((dispex->buf_size=4) * sizeof(dispex_prop_t));
755 if(!dispex->props)
756 return E_OUTOFMEMORY;
758 dispex->prototype = prototype;
759 if(prototype)
760 jsdisp_addref(prototype);
762 dispex->prop_cnt = 1;
763 dispex->props[0].name = NULL;
764 dispex->props[0].flags = 0;
765 if(builtin_info->value_prop.invoke) {
766 dispex->props[0].type = PROP_BUILTIN;
767 dispex->props[0].u.p = &builtin_info->value_prop;
768 }else {
769 dispex->props[0].type = PROP_DELETED;
772 script_addref(ctx);
773 dispex->ctx = ctx;
775 return S_OK;
778 static const builtin_info_t dispex_info = {
779 JSCLASS_NONE,
780 {NULL, NULL, 0},
781 0, NULL,
782 NULL,
783 NULL
786 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
788 jsdisp_t *ret;
789 HRESULT hres;
791 ret = heap_alloc_zero(sizeof(jsdisp_t));
792 if(!ret)
793 return E_OUTOFMEMORY;
795 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
796 if(FAILED(hres))
797 return hres;
799 *dispex = ret;
800 return S_OK;
803 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
805 jsdisp_t *prot = NULL;
806 dispex_prop_t *prop;
807 HRESULT hres;
809 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
810 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
812 hres = find_prop_name_prot(constr, prototypeW, &prop);
813 if(SUCCEEDED(hres) && prop) {
814 jsexcept_t jsexcept;
815 VARIANT var;
817 V_VT(&var) = VT_EMPTY;
818 memset(&jsexcept, 0, sizeof(jsexcept));
819 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
820 if(FAILED(hres)) {
821 ERR("Could not get prototype\n");
822 return hres;
825 if(V_VT(&var) == VT_DISPATCH)
826 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
827 VariantClear(&var);
830 hres = init_dispex(dispex, ctx, builtin_info, prot);
832 if(prot)
833 jsdisp_release(prot);
834 if(FAILED(hres))
835 return hres;
837 hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
838 if(SUCCEEDED(hres)) {
839 jsexcept_t jsexcept;
840 VARIANT var;
842 var_set_jsdisp(&var, constr);
843 memset(&jsexcept, 0, sizeof(jsexcept));
844 hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
846 if(FAILED(hres))
847 jsdisp_release(dispex);
849 return hres;
852 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
854 jsdisp_t *ret;
855 HRESULT hres;
857 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
858 if(FAILED(hres))
859 return NULL;
861 return ret;
864 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
866 dispex_prop_t *prop;
867 HRESULT hres;
869 if(flags & fdexNameEnsure)
870 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
871 else
872 hres = find_prop_name_prot(jsdisp, name, &prop);
873 if(FAILED(hres))
874 return hres;
876 if(prop) {
877 *id = prop_to_id(jsdisp, prop);
878 return S_OK;
881 TRACE("not found %s\n", debugstr_w(name));
882 return DISP_E_UNKNOWNNAME;
885 HRESULT jsdisp_call_value(jsdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv,
886 jsexcept_t *ei, IServiceProvider *caller)
888 vdisp_t vdisp;
889 HRESULT hres;
891 set_jsdisp(&vdisp, jsthis);
892 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei, caller);
893 vdisp_release(&vdisp);
894 return hres;
897 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
898 jsexcept_t *ei, IServiceProvider *caller)
900 dispex_prop_t *prop;
902 memset(ei, 0, sizeof(*ei));
903 if(retv)
904 V_VT(retv) = VT_EMPTY;
906 prop = get_prop(disp, id);
907 if(!prop)
908 return DISP_E_MEMBERNOTFOUND;
910 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
913 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
914 jsexcept_t *ei, IServiceProvider *caller)
916 dispex_prop_t *prop;
917 HRESULT hres;
919 hres = find_prop_name_prot(disp, name, &prop);
920 if(FAILED(hres))
921 return hres;
923 memset(ei, 0, sizeof(*ei));
924 if(retv)
925 V_VT(retv) = VT_EMPTY;
927 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
930 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
931 jsexcept_t *ei, IServiceProvider *caller)
933 jsdisp_t *jsdisp;
934 IDispatchEx *dispex;
935 HRESULT hres;
937 jsdisp = iface_to_jsdisp((IUnknown*)disp);
938 if(jsdisp) {
939 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei, caller);
940 jsdisp_release(jsdisp);
941 return hres;
944 memset(ei, 0, sizeof(*ei));
945 if(retv && arg_cnt(dp))
946 flags |= DISPATCH_PROPERTYGET;
948 if(retv)
949 V_VT(retv) = VT_EMPTY;
950 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
951 if(FAILED(hres)) {
952 UINT err = 0;
954 if(flags == DISPATCH_CONSTRUCT) {
955 WARN("IDispatch cannot be constructor\n");
956 return DISP_E_MEMBERNOTFOUND;
959 TRACE("using IDispatch\n");
960 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
963 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, caller);
964 IDispatchEx_Release(dispex);
966 return hres;
969 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
971 dispex_prop_t *prop;
972 HRESULT hres;
974 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
975 if(FAILED(hres))
976 return hres;
978 return prop_put(obj, prop, val, ei, caller);
981 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
983 dispex_prop_t *prop;
984 HRESULT hres;
986 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
987 if(FAILED(hres))
988 return hres;
990 return VariantCopy(&prop->u.var, val);
993 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
995 WCHAR buf[12];
997 static const WCHAR formatW[] = {'%','d',0};
999 sprintfW(buf, formatW, idx);
1000 return jsdisp_propput_name(obj, buf, val, ei, caller);
1003 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1005 jsdisp_t *jsdisp;
1006 HRESULT hres;
1008 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1009 if(jsdisp) {
1010 dispex_prop_t *prop;
1012 prop = get_prop(jsdisp, id);
1013 if(prop)
1014 hres = prop_put(jsdisp, prop, val, ei, caller);
1015 else
1016 hres = DISP_E_MEMBERNOTFOUND;
1018 jsdisp_release(jsdisp);
1019 }else {
1020 DISPID dispid = DISPID_PROPERTYPUT;
1021 DISPPARAMS dp = {val, &dispid, 1, 1};
1022 IDispatchEx *dispex;
1024 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1025 if(SUCCEEDED(hres)) {
1026 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
1027 IDispatchEx_Release(dispex);
1028 }else {
1029 ULONG err = 0;
1031 TRACE("using IDispatch\n");
1032 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1036 return hres;
1039 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1041 DISPPARAMS dp = {NULL, NULL, 0, 0};
1042 dispex_prop_t *prop;
1043 HRESULT hres;
1045 hres = find_prop_name_prot(obj, name, &prop);
1046 if(FAILED(hres))
1047 return hres;
1049 V_VT(var) = VT_EMPTY;
1050 if(!prop)
1051 return S_OK;
1053 return prop_get(obj, prop, &dp, var, ei, caller);
1056 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1058 WCHAR name[12];
1059 DISPPARAMS dp = {NULL, NULL, 0, 0};
1060 dispex_prop_t *prop;
1061 HRESULT hres;
1063 static const WCHAR formatW[] = {'%','d',0};
1065 sprintfW(name, formatW, idx);
1067 hres = find_prop_name_prot(obj, name, &prop);
1068 if(FAILED(hres))
1069 return hres;
1071 V_VT(var) = VT_EMPTY;
1072 if(!prop)
1073 return DISP_E_UNKNOWNNAME;
1075 return prop_get(obj, prop, &dp, var, ei, caller);
1078 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1080 DISPPARAMS dp = {NULL,NULL,0,0};
1081 dispex_prop_t *prop;
1083 prop = get_prop(jsdisp, id);
1084 if(!prop)
1085 return DISP_E_MEMBERNOTFOUND;
1087 V_VT(val) = VT_EMPTY;
1088 return prop_get(jsdisp, prop, &dp, val, ei, caller);
1091 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1093 DISPPARAMS dp = {NULL,NULL,0,0};
1094 IDispatchEx *dispex;
1095 jsdisp_t *jsdisp;
1096 HRESULT hres;
1098 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1099 if(jsdisp) {
1100 hres = jsdisp_propget(jsdisp, id, val, ei, caller);
1101 jsdisp_release(jsdisp);
1102 return hres;
1105 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1106 if(FAILED(hres)) {
1107 ULONG err = 0;
1109 TRACE("using IDispatch\n");
1110 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1113 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
1114 IDispatchEx_Release(dispex);
1116 return hres;
1119 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1121 static const WCHAR formatW[] = {'%','d',0};
1122 WCHAR buf[12];
1123 dispex_prop_t *prop;
1124 HRESULT hres;
1126 sprintfW(buf, formatW, idx);
1128 hres = find_prop_name(obj, buf, &prop);
1129 if(FAILED(hres) || !prop)
1130 return hres;
1132 return delete_prop(prop);