jscript: Added member expression implementation.
[wine] / dlls / jscript / dispex.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "jscript.h"
20
21 #include "wine/unicode.h"
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25
26 /*
27  * This IID is used to get DispatchEx objecto from interface.
28  * We might consider using private insteface instead.
29  */
30 static const IID IID_IDispatchJS =
31         {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
32
33 typedef enum {
34     PROP_VARIANT,
35     PROP_BUILTIN,
36     PROP_PROTREF,
37     PROP_DELETED
38 } prop_type_t;
39
40 struct _dispex_prop_t {
41     WCHAR *name;
42     prop_type_t type;
43     DWORD flags;
44
45     union {
46         VARIANT var;
47         const builtin_prop_t *p;
48         DWORD ref;
49     } u;
50 };
51
52 static inline DISPID prop_to_id(DispatchEx *This, dispex_prop_t *prop)
53 {
54     return prop - This->props;
55 }
56
57 static inline dispex_prop_t *get_prop(DispatchEx *This, DISPID id)
58 {
59     if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
60         return NULL;
61
62     return This->props+id;
63 }
64
65 static DWORD get_flags(DispatchEx *This, dispex_prop_t *prop)
66 {
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;
72         }
73
74         return get_flags(This->prototype, parent);
75     }
76
77     return prop->flags;
78 }
79
80 static const builtin_prop_t *find_builtin_prop(DispatchEx *This, const WCHAR *name)
81 {
82     int min = 0, max, i, r;
83
84     max = This->builtin_info->props_cnt-1;
85     while(min <= max) {
86         i = (min+max)/2;
87
88         r = strcmpW(name, This->builtin_info->props[i].name);
89         if(!r)
90             return This->builtin_info->props + i;
91
92         if(r < 0)
93             max = i-1;
94         else
95             min = i+1;
96     }
97
98     return NULL;
99 }
100
101 static dispex_prop_t *alloc_prop(DispatchEx *This, const WCHAR *name, prop_type_t type, DWORD flags)
102 {
103     dispex_prop_t *ret;
104
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;
110     }
111
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;
118
119     return ret;
120 }
121
122 static dispex_prop_t *alloc_protref(DispatchEx *This, const WCHAR *name, DWORD ref)
123 {
124     dispex_prop_t *ret;
125
126     ret = alloc_prop(This, name, PROP_PROTREF, 0);
127     if(!ret)
128         return NULL;
129
130     ret->u.ref = ref;
131     return ret;
132 }
133
134 static HRESULT find_prop_name(DispatchEx *This, const WCHAR *name, dispex_prop_t **ret)
135 {
136     const builtin_prop_t *builtin;
137     dispex_prop_t *prop;
138
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;
143         }
144     }
145
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;
151
152         prop->u.p = builtin;
153         *ret = prop;
154         return S_OK;
155     }
156
157     *ret = NULL;
158     return S_OK;
159 }
160
161 static HRESULT find_prop_name_prot(DispatchEx *This, const WCHAR *name, BOOL alloc, dispex_prop_t **ret)
162 {
163     dispex_prop_t *prop;
164     HRESULT hres;
165
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;
172     }
173
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;
184         }
185     }
186
187     if(alloc) {
188         TRACE("creating prop %s\n", debugstr_w(name));
189
190         prop = alloc_prop(This, name, PROP_VARIANT, PROPF_ENUM);
191         if(!prop)
192             return E_OUTOFMEMORY;
193         VariantInit(&prop->u.var);
194     }
195
196     *ret = prop;
197     return S_OK;
198 }
199
200 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
201 {
202     VARIANTARG *oldargs;
203     int i;
204
205     static DISPID this_id = DISPID_THIS;
206
207     *dp = *olddp;
208
209     for(i = 0; i < dp->cNamedArgs; i++) {
210         if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
211             return S_OK;
212     }
213
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++;
222
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;
229         }
230
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;
237     }
238
239     return S_OK;
240 }
241
242 static HRESULT invoke_prop_func(DispatchEx *This, DispatchEx *jsthis, dispex_prop_t *prop, LCID lcid, WORD flags,
243         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
244 {
245     HRESULT hres;
246
247     switch(prop->type) {
248     case PROP_BUILTIN:
249         if(flags == DISPATCH_CONSTRUCT && (prop->flags & DISPATCH_METHOD)) {
250             WARN("%s is not a constructor\n", debugstr_w(prop->name));
251             return E_INVALIDARG;
252         }
253         return prop->u.p->invoke(jsthis, lcid, flags, dp, retv, ei, caller);
254     case PROP_PROTREF:
255         return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, lcid, flags, dp, retv, ei, caller);
256     case PROP_VARIANT: {
257         DISPPARAMS new_dp;
258
259         if(V_VT(&prop->u.var) != VT_DISPATCH) {
260             FIXME("invoke vt %d\n", V_VT(&prop->u.var));
261             return E_FAIL;
262         }
263
264         TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
265
266         hres = set_this(&new_dp, dp, (IDispatch*)_IDispatchEx_(jsthis));
267         if(FAILED(hres))
268             return hres;
269
270         hres = disp_call(V_DISPATCH(&prop->u.var), DISPID_VALUE, lcid, flags, &new_dp, retv, ei, caller);
271
272         if(new_dp.rgvarg != dp->rgvarg) {
273             heap_free(new_dp.rgvarg);
274             if(new_dp.cNamedArgs > 1)
275                 heap_free(new_dp.rgdispidNamedArgs);
276         }
277
278         return hres;
279     }
280     default:
281         ERR("type %d\n", prop->type);
282     }
283
284     return E_FAIL;
285 }
286
287 static HRESULT prop_get(DispatchEx *This, dispex_prop_t *prop, LCID lcid, DISPPARAMS *dp,
288         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
289 {
290     HRESULT hres;
291
292     switch(prop->type) {
293     case PROP_BUILTIN:
294         if(prop->u.p->flags & PROPF_METHOD) {
295             DispatchEx *obj;
296             hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->flags, NULL, &obj);
297             if(FAILED(hres))
298                 break;
299
300             prop->type = PROP_VARIANT;
301             V_VT(&prop->u.var) = VT_DISPATCH;
302             V_DISPATCH(&prop->u.var) = (IDispatch*)_IDispatchEx_(obj);
303
304             hres = VariantCopy(retv, &prop->u.var);
305         }else {
306             hres = prop->u.p->invoke(This, lcid, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
307         }
308         break;
309     case PROP_PROTREF:
310         hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, lcid, dp, retv, ei, caller);
311         break;
312     case PROP_VARIANT:
313         hres = VariantCopy(retv, &prop->u.var);
314         break;
315     default:
316         ERR("type %d\n", prop->type);
317         return E_FAIL;
318     }
319
320     if(FAILED(hres)) {
321         TRACE("fail %08x\n", hres);
322         return hres;
323     }
324
325     TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
326     return hres;
327 }
328
329 static HRESULT prop_put(DispatchEx *This, dispex_prop_t *prop, LCID lcid, DISPPARAMS *dp,
330         jsexcept_t *ei, IServiceProvider *caller)
331 {
332     DWORD i;
333     HRESULT hres;
334
335     switch(prop->type) {
336     case PROP_BUILTIN:
337         if(!(prop->flags & PROPF_METHOD))
338             return prop->u.p->invoke(This, lcid, DISPATCH_PROPERTYPUT, dp, NULL, ei, caller);
339     case PROP_PROTREF:
340         prop->type = PROP_VARIANT;
341         prop->flags = PROPF_ENUM;
342         V_VT(&prop->u.var) = VT_EMPTY;
343         break;
344     case PROP_VARIANT:
345         VariantClear(&prop->u.var);
346         break;
347     default:
348         ERR("type %d\n", prop->type);
349         return E_FAIL;
350     }
351
352     for(i=0; i < dp->cNamedArgs; i++) {
353         if(dp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
354             break;
355     }
356
357     if(i == dp->cNamedArgs) {
358         TRACE("no value to set\n");
359         return DISP_E_PARAMNOTOPTIONAL;
360     }
361
362     hres = VariantCopy(&prop->u.var, dp->rgvarg+i);
363     if(FAILED(hres))
364         return hres;
365
366     if(This->builtin_info->on_put)
367         This->builtin_info->on_put(This, prop->name);
368
369     TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(dp->rgvarg+i));
370     return S_OK;
371 }
372
373 static HRESULT fill_protrefs(DispatchEx *This)
374 {
375     dispex_prop_t *iter, *prop;
376     HRESULT hres;
377
378     if(!This->prototype)
379         return S_OK;
380
381     fill_protrefs(This->prototype);
382
383     for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
384         hres = find_prop_name(This, iter->name, &prop);
385         if(FAILED(hres))
386             return hres;
387         if(!prop) {
388             prop = alloc_protref(This, iter->name, iter - This->prototype->props);
389             if(!prop)
390                 return E_OUTOFMEMORY;
391         }
392     }
393
394     return S_OK;
395 }
396
397 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
398
399 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
400 {
401     DispatchEx *This = DISPATCHEX_THIS(iface);
402
403     if(IsEqualGUID(&IID_IUnknown, riid)) {
404         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
405         *ppv = _IDispatchEx_(This);
406     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
407         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
408         *ppv = _IDispatchEx_(This);
409     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
410         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
411         *ppv = _IDispatchEx_(This);
412     }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
413         TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
414         IUnknown_AddRef(_IDispatchEx_(This));
415         *ppv = This;
416         return S_OK;
417     }else {
418         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
419         *ppv = NULL;
420         return E_NOINTERFACE;
421     }
422
423     IUnknown_AddRef((IUnknown*)*ppv);
424     return S_OK;
425 }
426
427 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
428 {
429     DispatchEx *This = DISPATCHEX_THIS(iface);
430     LONG ref = InterlockedIncrement(&This->ref);
431
432     TRACE("(%p) ref=%d\n", This, ref);
433
434     return ref;
435 }
436
437 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
438 {
439     DispatchEx *This = DISPATCHEX_THIS(iface);
440     LONG ref = InterlockedDecrement(&This->ref);
441
442     TRACE("(%p) ref=%d\n", This, ref);
443
444     if(!ref) {
445         dispex_prop_t *prop;
446
447         for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
448             if(prop->type == PROP_VARIANT)
449                 VariantClear(&prop->u.var);
450             heap_free(prop->name);
451         }
452         heap_free(This->props);
453         script_release(This->ctx);
454
455         if(This->builtin_info->destructor)
456             This->builtin_info->destructor(This);
457         else
458             heap_free(This);
459     }
460
461     return ref;
462 }
463
464 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
465 {
466     DispatchEx *This = DISPATCHEX_THIS(iface);
467
468     TRACE("(%p)->(%p)\n", This, pctinfo);
469
470     *pctinfo = 1;
471     return S_OK;
472 }
473
474 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
475                                               LCID lcid, ITypeInfo **ppTInfo)
476 {
477     DispatchEx *This = DISPATCHEX_THIS(iface);
478     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
479     return E_NOTIMPL;
480 }
481
482 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
483                                                 LPOLESTR *rgszNames, UINT cNames,
484                                                 LCID lcid, DISPID *rgDispId)
485 {
486     DispatchEx *This = DISPATCHEX_THIS(iface);
487     UINT i;
488     HRESULT hres;
489
490     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
491           lcid, rgDispId);
492
493     for(i=0; i < cNames; i++) {
494         hres = IDispatchEx_GetDispID(_IDispatchEx_(This), rgszNames[i], 0, rgDispId+i);
495         if(FAILED(hres))
496             return hres;
497     }
498
499     return S_OK;
500 }
501
502 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
503                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
504                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
505 {
506     DispatchEx *This = DISPATCHEX_THIS(iface);
507
508     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
509           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
510
511     return IDispatchEx_InvokeEx(_IDispatchEx_(This), dispIdMember, lcid, wFlags,
512             pDispParams, pVarResult, pExcepInfo, NULL);
513 }
514
515 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
516 {
517     DispatchEx *This = DISPATCHEX_THIS(iface);
518     dispex_prop_t *prop;
519     HRESULT hres;
520
521     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
522
523     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit)) {
524         FIXME("Unsupported grfdex %x\n", grfdex);
525         return E_NOTIMPL;
526     }
527
528     hres = find_prop_name_prot(This, bstrName, (grfdex&fdexNameEnsure) != 0, &prop);
529     if(FAILED(hres))
530         return hres;
531     if(prop) {
532         *pid = prop_to_id(This, prop);
533         return S_OK;
534     }
535
536     TRACE("not found %s\n", debugstr_w(bstrName));
537     return DISP_E_UNKNOWNNAME;
538 }
539
540 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
541         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
542 {
543     DispatchEx *This = DISPATCHEX_THIS(iface);
544     dispex_prop_t *prop;
545     jsexcept_t jsexcept;
546     HRESULT hres;
547
548     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
549
550     if(pvarRes)
551         V_VT(pvarRes) = VT_EMPTY;
552
553     prop = get_prop(This, id);
554     if(!prop || prop->type == PROP_DELETED) {
555         TRACE("invalid id\n");
556         return DISP_E_MEMBERNOTFOUND;
557     }
558
559     memset(&jsexcept, 0, sizeof(jsexcept));
560
561     switch(wFlags) {
562     case DISPATCH_METHOD:
563     case DISPATCH_CONSTRUCT:
564         hres = invoke_prop_func(This, This, prop, lcid, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
565         break;
566     case DISPATCH_PROPERTYGET:
567         hres = prop_get(This, prop, lcid, pdp, pvarRes, &jsexcept, pspCaller);
568         break;
569     case DISPATCH_PROPERTYPUT:
570         hres = prop_put(This, prop, lcid, pdp, &jsexcept, pspCaller);
571         break;
572     default:
573         FIXME("Unimplemented flags %x\n", wFlags);
574         return E_INVALIDARG;
575     }
576
577     if(pei)
578         *pei = jsexcept.ei;
579
580     return hres;
581 }
582
583 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
584 {
585     DispatchEx *This = DISPATCHEX_THIS(iface);
586     dispex_prop_t *prop;
587     HRESULT hres;
588
589     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
590
591     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
592         FIXME("Unsupported grfdex %x\n", grfdex);
593
594     hres = find_prop_name(This, bstrName, &prop);
595     if(FAILED(hres))
596         return hres;
597     if(!prop) {
598         TRACE("not found\n");
599         return S_OK;
600     }
601
602     heap_free(prop->name);
603     prop->name = NULL;
604     prop->type = PROP_DELETED;
605     return S_OK;
606 }
607
608 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
609 {
610     DispatchEx *This = DISPATCHEX_THIS(iface);
611     FIXME("(%p)->(%x)\n", This, id);
612     return E_NOTIMPL;
613 }
614
615 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
616 {
617     DispatchEx *This = DISPATCHEX_THIS(iface);
618     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
619     return E_NOTIMPL;
620 }
621
622 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
623 {
624     DispatchEx *This = DISPATCHEX_THIS(iface);
625     dispex_prop_t *prop;
626
627     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
628
629     prop = get_prop(This, id);
630     if(!prop || !prop->name || prop->type == PROP_DELETED)
631         return DISP_E_MEMBERNOTFOUND;
632
633     *pbstrName = SysAllocString(prop->name);
634     if(!*pbstrName)
635         return E_OUTOFMEMORY;
636
637     return S_OK;
638 }
639
640 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
641 {
642     DispatchEx *This = DISPATCHEX_THIS(iface);
643     dispex_prop_t *iter;
644     HRESULT hres;
645
646     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
647
648     if(id == DISPID_STARTENUM) {
649         hres = fill_protrefs(This);
650         if(FAILED(hres))
651             return hres;
652     }
653
654     iter = get_prop(This, id+1);
655     if(!iter) {
656         *pid = DISPID_STARTENUM;
657         return S_FALSE;
658     }
659
660     while(iter < This->props + This->prop_cnt) {
661         if(iter->name && (get_flags(This, iter) & PROPF_ENUM)) {
662             *pid = prop_to_id(This, iter);
663             return S_OK;
664         }
665         iter++;
666     }
667
668     *pid = DISPID_STARTENUM;
669     return S_FALSE;
670 }
671
672 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
673 {
674     DispatchEx *This = DISPATCHEX_THIS(iface);
675     FIXME("(%p)->(%p)\n", This, ppunk);
676     return E_NOTIMPL;
677 }
678
679 #undef DISPATCHEX_THIS
680
681 static IDispatchExVtbl DispatchExVtbl = {
682     DispatchEx_QueryInterface,
683     DispatchEx_AddRef,
684     DispatchEx_Release,
685     DispatchEx_GetTypeInfoCount,
686     DispatchEx_GetTypeInfo,
687     DispatchEx_GetIDsOfNames,
688     DispatchEx_Invoke,
689     DispatchEx_GetDispID,
690     DispatchEx_InvokeEx,
691     DispatchEx_DeleteMemberByName,
692     DispatchEx_DeleteMemberByDispID,
693     DispatchEx_GetMemberProperties,
694     DispatchEx_GetMemberName,
695     DispatchEx_GetNextDispID,
696     DispatchEx_GetNameSpaceParent
697 };
698
699 HRESULT jsdisp_set_prototype(DispatchEx *dispex, DispatchEx *prototype)
700 {
701     VARIANT *var;
702
703     if(!dispex->props[1].name)
704         return E_OUTOFMEMORY;
705
706     dispex->props[1].type = PROP_VARIANT;
707     dispex->props[1].flags = 0;
708
709     var = &dispex->props[1].u.var;
710     V_VT(var) = VT_DISPATCH;
711     V_DISPATCH(var) = (IDispatch*)_IDispatchEx_(prototype);
712
713     return S_OK;
714 }
715
716 HRESULT init_dispex(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype)
717 {
718     static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
719
720     TRACE("%p (%p)\n", dispex, prototype);
721
722     dispex->lpIDispatchExVtbl = &DispatchExVtbl;
723     dispex->ref = 1;
724     dispex->builtin_info = builtin_info;
725
726     dispex->props = heap_alloc((dispex->buf_size=4) * sizeof(dispex_prop_t));
727     if(!dispex->props)
728         return E_OUTOFMEMORY;
729
730     dispex->prototype = prototype;
731     if(prototype)
732         IDispatchEx_AddRef(_IDispatchEx_(prototype));
733
734     dispex->prop_cnt = 2;
735     dispex->props[0].name = NULL;
736     dispex->props[0].flags = 0;
737     if(builtin_info->value_prop.invoke) {
738         dispex->props[0].type = PROP_BUILTIN;
739         dispex->props[0].u.p = &builtin_info->value_prop;
740     }else {
741         dispex->props[0].type = PROP_DELETED;
742     }
743
744     dispex->props[1].type = PROP_DELETED;
745     dispex->props[1].name = SysAllocString(prototypeW);
746     dispex->props[1].flags = 0;
747
748     if(prototype) {
749         HRESULT hres;
750
751         hres = jsdisp_set_prototype(dispex, prototype);
752         if(FAILED(hres)) {
753             IDispatchEx_Release(_IDispatchEx_(dispex));
754             return hres;
755         }
756     }
757
758     script_addref(ctx);
759     dispex->ctx = ctx;
760
761     return S_OK;
762 }
763
764 static const builtin_info_t dispex_info = {
765     JSCLASS_NONE,
766     {NULL, NULL, 0},
767     0, NULL,
768     NULL,
769     NULL
770 };
771
772 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype, DispatchEx **dispex)
773 {
774     DispatchEx *ret;
775     HRESULT hres;
776
777     ret = heap_alloc_zero(sizeof(DispatchEx));
778     if(!ret)
779         return E_OUTOFMEMORY;
780
781     hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
782     if(FAILED(hres))
783         return hres;
784
785     *dispex = ret;
786     return S_OK;
787 }
788
789 DispatchEx *iface_to_jsdisp(IUnknown *iface)
790 {
791     DispatchEx *ret;
792     HRESULT hres;
793
794     hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
795     if(FAILED(hres))
796         return NULL;
797
798     return ret;
799 }
800
801 HRESULT jsdisp_call(DispatchEx *disp, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv,
802         jsexcept_t *ei, IServiceProvider *caller)
803 {
804     dispex_prop_t *prop;
805
806     memset(ei, 0, sizeof(*ei));
807     if(retv)
808         V_VT(retv) = VT_EMPTY;
809
810     prop = get_prop(disp, id);
811     if(!prop)
812         return DISP_E_MEMBERNOTFOUND;
813
814     return invoke_prop_func(disp, disp, prop, lcid, flags, dp, retv, ei, caller);
815 }
816
817 HRESULT disp_call(IDispatch *disp, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv,
818         jsexcept_t *ei, IServiceProvider *caller)
819 {
820     DispatchEx *jsdisp;
821     IDispatchEx *dispex;
822     HRESULT hres;
823
824     jsdisp = iface_to_jsdisp((IUnknown*)disp);
825     if(jsdisp) {
826         hres = jsdisp_call(jsdisp, id, lcid, flags, dp, retv, ei, caller);
827         IDispatchEx_Release(_IDispatchEx_(jsdisp));
828         return hres;
829     }
830
831     memset(ei, 0, sizeof(*ei));
832
833     if(retv)
834         V_VT(retv) = VT_EMPTY;
835     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
836     if(FAILED(hres)) {
837         UINT err = 0;
838
839         TRACE("using IDispatch\n");
840         return IDispatch_Invoke(disp, id, &IID_NULL, lcid, flags, dp, retv, &ei->ei, &err);
841     }
842
843     hres = IDispatchEx_InvokeEx(dispex, id, lcid, flags, dp, retv, &ei->ei, caller);
844     IDispatchEx_Release(dispex);
845
846     return hres;
847 }
848
849 HRESULT jsdisp_propput_name(DispatchEx *obj, const WCHAR *name, LCID lcid, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
850 {
851     DISPID named_arg = DISPID_PROPERTYPUT;
852     DISPPARAMS dp = {val, &named_arg, 1, 1};
853     dispex_prop_t *prop;
854     HRESULT hres;
855
856     hres = find_prop_name_prot(obj, name, TRUE, &prop);
857     if(FAILED(hres))
858         return hres;
859
860     return prop_put(obj, prop, lcid, &dp, ei, caller);
861 }
862
863 HRESULT disp_propput(IDispatch *disp, DISPID id, LCID lcid, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
864 {
865     DISPID dispid = DISPID_PROPERTYPUT;
866     DISPPARAMS dp  = {val, &dispid, 1, 1};
867     IDispatchEx *dispex;
868     DispatchEx *jsdisp;
869     HRESULT hres;
870
871     jsdisp = iface_to_jsdisp((IUnknown*)disp);
872     if(jsdisp) {
873         dispex_prop_t *prop;
874
875         prop = get_prop(jsdisp, id);
876         if(prop)
877             hres = prop_put(jsdisp, prop, lcid, &dp, ei, caller);
878         else
879             hres = DISP_E_MEMBERNOTFOUND;
880
881         IDispatchEx_Release(_IDispatchEx_(jsdisp));
882         return hres;
883     }
884
885     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
886     if(FAILED(hres)) {
887         ULONG err = 0;
888
889         TRACE("using IDispatch\n");
890         return IDispatch_Invoke(disp, id, &IID_NULL, DISPATCH_PROPERTYPUT, lcid, &dp, NULL, &ei->ei, &err);
891     }
892
893     hres = IDispatchEx_InvokeEx(dispex, id, lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
894
895     IDispatchEx_Release(dispex);
896     return hres;
897 }
898
899 HRESULT disp_propget(IDispatch *disp, DISPID id, LCID lcid, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
900 {
901     DISPPARAMS dp  = {NULL,NULL,0,0};
902     IDispatchEx *dispex;
903     DispatchEx *jsdisp;
904     HRESULT hres;
905
906     jsdisp = iface_to_jsdisp((IUnknown*)disp);
907     if(jsdisp) {
908         dispex_prop_t *prop;
909
910         prop = get_prop(jsdisp, id);
911         if(prop)
912             hres = prop_get(jsdisp, prop, lcid, &dp, val, ei, caller);
913         else
914             hres = DISP_E_MEMBERNOTFOUND;
915
916         IDispatchEx_Release(_IDispatchEx_(jsdisp));
917         return hres;
918     }
919
920     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
921     if(FAILED(hres)) {
922         ULONG err = 0;
923
924         TRACE("uding IDispatch\n");
925         return IDispatchEx_Invoke(dispex, id, &IID_NULL, lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
926     }
927
928     hres = IDispatchEx_InvokeEx(dispex, id, lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
929     IDispatchEx_Release(dispex);
930
931     return hres;
932 }