windowscodecs: Fix ICO palette size calculation.
[wine/multimedia.git] / dlls / jscript / dispex.c
blob6894eae051533dc44d9fba8203f04b50ce9177d6
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, 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(DispatchEx *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(DispatchEx *This, DispatchEx *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 & DISPATCH_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, (IDispatch*)_IDispatchEx_(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(DispatchEx *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 DispatchEx *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 V_VT(&prop->u.var) = VT_DISPATCH;
325 V_DISPATCH(&prop->u.var) = (IDispatch*)_IDispatchEx_(obj);
327 hres = VariantCopy(retv, &prop->u.var);
328 }else {
329 vdisp_t vthis;
331 set_jsdisp(&vthis, This);
332 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
333 vdisp_release(&vthis);
335 break;
336 case PROP_PROTREF:
337 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
338 break;
339 case PROP_VARIANT:
340 hres = VariantCopy(retv, &prop->u.var);
341 break;
342 default:
343 ERR("type %d\n", prop->type);
344 return E_FAIL;
347 if(FAILED(hres)) {
348 TRACE("fail %08x\n", hres);
349 return hres;
352 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
353 return hres;
356 static HRESULT prop_put(DispatchEx *This, dispex_prop_t *prop, VARIANT *val,
357 jsexcept_t *ei, IServiceProvider *caller)
359 HRESULT hres;
361 switch(prop->type) {
362 case PROP_BUILTIN:
363 if(!(prop->flags & PROPF_METHOD)) {
364 DISPPARAMS dp = {val, NULL, 1, 0};
365 vdisp_t vthis;
367 set_jsdisp(&vthis, This);
368 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei, caller);
369 vdisp_release(&vthis);
370 return hres;
372 case PROP_PROTREF:
373 prop->type = PROP_VARIANT;
374 prop->flags = PROPF_ENUM;
375 V_VT(&prop->u.var) = VT_EMPTY;
376 break;
377 case PROP_VARIANT:
378 VariantClear(&prop->u.var);
379 break;
380 default:
381 ERR("type %d\n", prop->type);
382 return E_FAIL;
385 hres = VariantCopy(&prop->u.var, val);
386 if(FAILED(hres))
387 return hres;
389 if(This->builtin_info->on_put)
390 This->builtin_info->on_put(This, prop->name);
392 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
393 return S_OK;
396 static HRESULT fill_protrefs(DispatchEx *This)
398 dispex_prop_t *iter, *prop;
399 HRESULT hres;
401 if(!This->prototype)
402 return S_OK;
404 fill_protrefs(This->prototype);
406 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
407 if(!iter->name)
408 continue;
409 hres = find_prop_name(This, iter->name, &prop);
410 if(FAILED(hres))
411 return hres;
412 if(!prop) {
413 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
414 if(!prop)
415 return E_OUTOFMEMORY;
419 return S_OK;
422 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
424 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
426 DispatchEx *This = DISPATCHEX_THIS(iface);
428 if(IsEqualGUID(&IID_IUnknown, riid)) {
429 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
430 *ppv = _IDispatchEx_(This);
431 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
432 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
433 *ppv = _IDispatchEx_(This);
434 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
435 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
436 *ppv = _IDispatchEx_(This);
437 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
438 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
439 IUnknown_AddRef(_IDispatchEx_(This));
440 *ppv = This;
441 return S_OK;
442 }else {
443 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
444 *ppv = NULL;
445 return E_NOINTERFACE;
448 IUnknown_AddRef((IUnknown*)*ppv);
449 return S_OK;
452 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
454 DispatchEx *This = DISPATCHEX_THIS(iface);
455 LONG ref = InterlockedIncrement(&This->ref);
457 TRACE("(%p) ref=%d\n", This, ref);
459 return ref;
462 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
464 DispatchEx *This = DISPATCHEX_THIS(iface);
465 LONG ref = InterlockedDecrement(&This->ref);
467 TRACE("(%p) ref=%d\n", This, ref);
469 if(!ref) {
470 dispex_prop_t *prop;
472 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
473 if(prop->type == PROP_VARIANT)
474 VariantClear(&prop->u.var);
475 heap_free(prop->name);
477 heap_free(This->props);
478 script_release(This->ctx);
479 if(This->prototype)
480 jsdisp_release(This->prototype);
482 if(This->builtin_info->destructor)
483 This->builtin_info->destructor(This);
484 else
485 heap_free(This);
488 return ref;
491 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
493 DispatchEx *This = DISPATCHEX_THIS(iface);
495 TRACE("(%p)->(%p)\n", This, pctinfo);
497 *pctinfo = 1;
498 return S_OK;
501 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
502 ITypeInfo **ppTInfo)
504 DispatchEx *This = DISPATCHEX_THIS(iface);
505 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
506 return E_NOTIMPL;
509 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
510 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
511 DISPID *rgDispId)
513 DispatchEx *This = DISPATCHEX_THIS(iface);
514 UINT i;
515 HRESULT hres;
517 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
518 lcid, rgDispId);
520 for(i=0; i < cNames; i++) {
521 hres = IDispatchEx_GetDispID(_IDispatchEx_(This), rgszNames[i], 0, rgDispId+i);
522 if(FAILED(hres))
523 return hres;
526 return S_OK;
529 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
530 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
531 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
533 DispatchEx *This = DISPATCHEX_THIS(iface);
535 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
536 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
538 return IDispatchEx_InvokeEx(_IDispatchEx_(This), dispIdMember, lcid, wFlags,
539 pDispParams, pVarResult, pExcepInfo, NULL);
542 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
544 DispatchEx *This = DISPATCHEX_THIS(iface);
546 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
548 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
549 FIXME("Unsupported grfdex %x\n", grfdex);
550 return E_NOTIMPL;
553 return jsdisp_get_id(This, bstrName, grfdex, pid);
556 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
557 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
559 DispatchEx *This = DISPATCHEX_THIS(iface);
560 dispex_prop_t *prop;
561 jsexcept_t jsexcept;
562 HRESULT hres;
564 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
566 if(pvarRes)
567 V_VT(pvarRes) = VT_EMPTY;
569 prop = get_prop(This, id);
570 if(!prop || prop->type == PROP_DELETED) {
571 TRACE("invalid id\n");
572 return DISP_E_MEMBERNOTFOUND;
575 memset(&jsexcept, 0, sizeof(jsexcept));
577 switch(wFlags) {
578 case DISPATCH_METHOD:
579 case DISPATCH_CONSTRUCT:
580 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
581 break;
582 case DISPATCH_PROPERTYGET:
583 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
584 break;
585 case DISPATCH_PROPERTYPUT: {
586 DWORD i;
588 for(i=0; i < pdp->cNamedArgs; i++) {
589 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
590 break;
593 if(i == pdp->cNamedArgs) {
594 TRACE("no value to set\n");
595 return DISP_E_PARAMNOTOPTIONAL;
598 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
599 break;
601 default:
602 FIXME("Unimplemented flags %x\n", wFlags);
603 return E_INVALIDARG;
606 if(pei)
607 *pei = jsexcept.ei;
609 return hres;
612 static HRESULT delete_prop(dispex_prop_t *prop)
614 heap_free(prop->name);
615 prop->name = NULL;
616 prop->type = PROP_DELETED;
618 return S_OK;
621 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
623 DispatchEx *This = DISPATCHEX_THIS(iface);
624 dispex_prop_t *prop;
625 HRESULT hres;
627 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
629 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
630 FIXME("Unsupported grfdex %x\n", grfdex);
632 hres = find_prop_name(This, bstrName, &prop);
633 if(FAILED(hres))
634 return hres;
635 if(!prop) {
636 TRACE("not found\n");
637 return S_OK;
640 return delete_prop(prop);
643 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
645 DispatchEx *This = DISPATCHEX_THIS(iface);
646 dispex_prop_t *prop;
648 TRACE("(%p)->(%x)\n", This, id);
650 prop = get_prop(This, id);
651 if(!prop) {
652 WARN("invalid id\n");
653 return DISP_E_MEMBERNOTFOUND;
656 return delete_prop(prop);
659 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
661 DispatchEx *This = DISPATCHEX_THIS(iface);
662 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
663 return E_NOTIMPL;
666 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
668 DispatchEx *This = DISPATCHEX_THIS(iface);
669 dispex_prop_t *prop;
671 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
673 prop = get_prop(This, id);
674 if(!prop || !prop->name || prop->type == PROP_DELETED)
675 return DISP_E_MEMBERNOTFOUND;
677 *pbstrName = SysAllocString(prop->name);
678 if(!*pbstrName)
679 return E_OUTOFMEMORY;
681 return S_OK;
684 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
686 DispatchEx *This = DISPATCHEX_THIS(iface);
687 dispex_prop_t *iter;
688 HRESULT hres;
690 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
692 if(id == DISPID_STARTENUM) {
693 hres = fill_protrefs(This);
694 if(FAILED(hres))
695 return hres;
698 iter = get_prop(This, id+1);
699 if(!iter) {
700 *pid = DISPID_STARTENUM;
701 return S_FALSE;
704 while(iter < This->props + This->prop_cnt) {
705 if(iter->name && (get_flags(This, iter) & PROPF_ENUM)) {
706 *pid = prop_to_id(This, iter);
707 return S_OK;
709 iter++;
712 *pid = DISPID_STARTENUM;
713 return S_FALSE;
716 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
718 DispatchEx *This = DISPATCHEX_THIS(iface);
719 FIXME("(%p)->(%p)\n", This, ppunk);
720 return E_NOTIMPL;
723 #undef DISPATCHEX_THIS
725 static IDispatchExVtbl DispatchExVtbl = {
726 DispatchEx_QueryInterface,
727 DispatchEx_AddRef,
728 DispatchEx_Release,
729 DispatchEx_GetTypeInfoCount,
730 DispatchEx_GetTypeInfo,
731 DispatchEx_GetIDsOfNames,
732 DispatchEx_Invoke,
733 DispatchEx_GetDispID,
734 DispatchEx_InvokeEx,
735 DispatchEx_DeleteMemberByName,
736 DispatchEx_DeleteMemberByDispID,
737 DispatchEx_GetMemberProperties,
738 DispatchEx_GetMemberName,
739 DispatchEx_GetNextDispID,
740 DispatchEx_GetNameSpaceParent
743 HRESULT init_dispex(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype)
745 TRACE("%p (%p)\n", dispex, prototype);
747 dispex->lpIDispatchExVtbl = &DispatchExVtbl;
748 dispex->ref = 1;
749 dispex->builtin_info = builtin_info;
751 dispex->props = heap_alloc((dispex->buf_size=4) * sizeof(dispex_prop_t));
752 if(!dispex->props)
753 return E_OUTOFMEMORY;
755 dispex->prototype = prototype;
756 if(prototype)
757 IDispatchEx_AddRef(_IDispatchEx_(prototype));
759 dispex->prop_cnt = 1;
760 dispex->props[0].name = NULL;
761 dispex->props[0].flags = 0;
762 if(builtin_info->value_prop.invoke) {
763 dispex->props[0].type = PROP_BUILTIN;
764 dispex->props[0].u.p = &builtin_info->value_prop;
765 }else {
766 dispex->props[0].type = PROP_DELETED;
769 script_addref(ctx);
770 dispex->ctx = ctx;
772 return S_OK;
775 static const builtin_info_t dispex_info = {
776 JSCLASS_NONE,
777 {NULL, NULL, 0},
778 0, NULL,
779 NULL,
780 NULL
783 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype, DispatchEx **dispex)
785 DispatchEx *ret;
786 HRESULT hres;
788 ret = heap_alloc_zero(sizeof(DispatchEx));
789 if(!ret)
790 return E_OUTOFMEMORY;
792 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
793 if(FAILED(hres))
794 return hres;
796 *dispex = ret;
797 return S_OK;
800 HRESULT init_dispex_from_constr(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *constr)
802 DispatchEx *prot = NULL;
803 dispex_prop_t *prop;
804 HRESULT hres;
806 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r'};
807 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
809 hres = find_prop_name_prot(constr, prototypeW, &prop);
810 if(SUCCEEDED(hres) && prop) {
811 jsexcept_t jsexcept;
812 VARIANT var;
814 V_VT(&var) = VT_EMPTY;
815 memset(&jsexcept, 0, sizeof(jsexcept));
816 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
817 if(FAILED(hres)) {
818 ERR("Could not get prototype\n");
819 return hres;
822 if(V_VT(&var) == VT_DISPATCH)
823 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
824 VariantClear(&var);
827 hres = init_dispex(dispex, ctx, builtin_info, prot);
829 if(prot)
830 jsdisp_release(prot);
831 if(FAILED(hres))
832 return hres;
834 hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
835 if(SUCCEEDED(hres)) {
836 jsexcept_t jsexcept;
837 VARIANT var;
839 V_VT(&var) = VT_DISPATCH;
840 V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(constr);
841 memset(&jsexcept, 0, sizeof(jsexcept));
842 hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
844 if(FAILED(hres))
845 jsdisp_release(dispex);
847 return hres;
850 DispatchEx *iface_to_jsdisp(IUnknown *iface)
852 DispatchEx *ret;
853 HRESULT hres;
855 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
856 if(FAILED(hres))
857 return NULL;
859 return ret;
862 HRESULT jsdisp_get_id(DispatchEx *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
864 dispex_prop_t *prop;
865 HRESULT hres;
867 if(flags & fdexNameEnsure)
868 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
869 else
870 hres = find_prop_name_prot(jsdisp, name, &prop);
871 if(FAILED(hres))
872 return hres;
874 if(prop) {
875 *id = prop_to_id(jsdisp, prop);
876 return S_OK;
879 TRACE("not found %s\n", debugstr_w(name));
880 return DISP_E_UNKNOWNNAME;
883 HRESULT jsdisp_call_value(DispatchEx *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv,
884 jsexcept_t *ei, IServiceProvider *caller)
886 vdisp_t vdisp;
887 HRESULT hres;
889 set_jsdisp(&vdisp, jsthis);
890 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei, caller);
891 vdisp_release(&vdisp);
892 return hres;
895 HRESULT jsdisp_call(DispatchEx *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
896 jsexcept_t *ei, IServiceProvider *caller)
898 dispex_prop_t *prop;
900 memset(ei, 0, sizeof(*ei));
901 if(retv)
902 V_VT(retv) = VT_EMPTY;
904 prop = get_prop(disp, id);
905 if(!prop)
906 return DISP_E_MEMBERNOTFOUND;
908 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
911 HRESULT jsdisp_call_name(DispatchEx *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
912 jsexcept_t *ei, IServiceProvider *caller)
914 dispex_prop_t *prop;
915 HRESULT hres;
917 hres = find_prop_name_prot(disp, name, &prop);
918 if(FAILED(hres))
919 return hres;
921 memset(ei, 0, sizeof(*ei));
922 if(retv)
923 V_VT(retv) = VT_EMPTY;
925 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
928 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
929 jsexcept_t *ei, IServiceProvider *caller)
931 DispatchEx *jsdisp;
932 IDispatchEx *dispex;
933 HRESULT hres;
935 jsdisp = iface_to_jsdisp((IUnknown*)disp);
936 if(jsdisp) {
937 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei, caller);
938 jsdisp_release(jsdisp);
939 return hres;
942 memset(ei, 0, sizeof(*ei));
944 if(retv)
945 V_VT(retv) = VT_EMPTY;
946 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
947 if(FAILED(hres)) {
948 UINT err = 0;
950 if(flags == DISPATCH_CONSTRUCT) {
951 WARN("IDispatch cannot be constructor\n");
952 return DISP_E_MEMBERNOTFOUND;
955 TRACE("using IDispatch\n");
956 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
959 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, caller);
960 IDispatchEx_Release(dispex);
962 return hres;
965 HRESULT jsdisp_propput_name(DispatchEx *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
967 dispex_prop_t *prop;
968 HRESULT hres;
970 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
971 if(FAILED(hres))
972 return hres;
974 return prop_put(obj, prop, val, ei, caller);
977 HRESULT jsdisp_propput_idx(DispatchEx *obj, DWORD idx, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
979 WCHAR buf[12];
981 static const WCHAR formatW[] = {'%','d',0};
983 sprintfW(buf, formatW, idx);
984 return jsdisp_propput_name(obj, buf, val, ei, caller);
987 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
989 DispatchEx *jsdisp;
990 HRESULT hres;
992 jsdisp = iface_to_jsdisp((IUnknown*)disp);
993 if(jsdisp) {
994 dispex_prop_t *prop;
996 prop = get_prop(jsdisp, id);
997 if(prop)
998 hres = prop_put(jsdisp, prop, val, ei, caller);
999 else
1000 hres = DISP_E_MEMBERNOTFOUND;
1002 jsdisp_release(jsdisp);
1003 }else {
1004 DISPID dispid = DISPID_PROPERTYPUT;
1005 DISPPARAMS dp = {val, &dispid, 1, 1};
1006 IDispatchEx *dispex;
1008 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1009 if(SUCCEEDED(hres)) {
1010 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
1011 IDispatchEx_Release(dispex);
1012 }else {
1013 ULONG err = 0;
1015 TRACE("using IDispatch\n");
1016 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1020 return hres;
1023 HRESULT jsdisp_propget_name(DispatchEx *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1025 DISPPARAMS dp = {NULL, NULL, 0, 0};
1026 dispex_prop_t *prop;
1027 HRESULT hres;
1029 hres = find_prop_name_prot(obj, name, &prop);
1030 if(FAILED(hres))
1031 return hres;
1033 V_VT(var) = VT_EMPTY;
1034 if(!prop)
1035 return S_OK;
1037 return prop_get(obj, prop, &dp, var, ei, caller);
1040 HRESULT jsdisp_get_idx(DispatchEx *obj, DWORD idx, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1042 WCHAR name[12];
1043 DISPPARAMS dp = {NULL, NULL, 0, 0};
1044 dispex_prop_t *prop;
1045 HRESULT hres;
1047 static const WCHAR formatW[] = {'%','d',0};
1049 sprintfW(name, formatW, idx);
1051 hres = find_prop_name_prot(obj, name, &prop);
1052 if(FAILED(hres))
1053 return hres;
1055 V_VT(var) = VT_EMPTY;
1056 if(!prop)
1057 return DISP_E_UNKNOWNNAME;
1059 return prop_get(obj, prop, &dp, var, ei, caller);
1062 HRESULT jsdisp_propget(DispatchEx *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1064 DISPPARAMS dp = {NULL,NULL,0,0};
1065 dispex_prop_t *prop;
1067 prop = get_prop(jsdisp, id);
1068 if(!prop)
1069 return DISP_E_MEMBERNOTFOUND;
1071 V_VT(val) = VT_EMPTY;
1072 return prop_get(jsdisp, prop, &dp, val, ei, caller);
1075 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1077 DISPPARAMS dp = {NULL,NULL,0,0};
1078 IDispatchEx *dispex;
1079 DispatchEx *jsdisp;
1080 HRESULT hres;
1082 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1083 if(jsdisp) {
1084 hres = jsdisp_propget(jsdisp, id, val, ei, caller);
1085 jsdisp_release(jsdisp);
1086 return hres;
1089 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1090 if(FAILED(hres)) {
1091 ULONG err = 0;
1093 TRACE("using IDispatch\n");
1094 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1097 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
1098 IDispatchEx_Release(dispex);
1100 return hres;
1103 HRESULT jsdisp_delete_idx(DispatchEx *obj, DWORD idx)
1105 static const WCHAR formatW[] = {'%','d',0};
1106 WCHAR buf[12];
1107 dispex_prop_t *prop;
1108 HRESULT hres;
1110 sprintfW(buf, formatW, idx);
1112 hres = find_prop_name(obj, buf, &prop);
1113 if(FAILED(hres) || !prop)
1114 return hres;
1116 return delete_prop(prop);