jscript: Added Date constructor object 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         if(!iter->name)
385             continue;
386         hres = find_prop_name(This, iter->name, &prop);
387         if(FAILED(hres))
388             return hres;
389         if(!prop) {
390             prop = alloc_protref(This, iter->name, iter - This->prototype->props);
391             if(!prop)
392                 return E_OUTOFMEMORY;
393         }
394     }
395
396     return S_OK;
397 }
398
399 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
400
401 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
402 {
403     DispatchEx *This = DISPATCHEX_THIS(iface);
404
405     if(IsEqualGUID(&IID_IUnknown, riid)) {
406         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
407         *ppv = _IDispatchEx_(This);
408     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
409         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
410         *ppv = _IDispatchEx_(This);
411     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
412         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
413         *ppv = _IDispatchEx_(This);
414     }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
415         TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
416         IUnknown_AddRef(_IDispatchEx_(This));
417         *ppv = This;
418         return S_OK;
419     }else {
420         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
421         *ppv = NULL;
422         return E_NOINTERFACE;
423     }
424
425     IUnknown_AddRef((IUnknown*)*ppv);
426     return S_OK;
427 }
428
429 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
430 {
431     DispatchEx *This = DISPATCHEX_THIS(iface);
432     LONG ref = InterlockedIncrement(&This->ref);
433
434     TRACE("(%p) ref=%d\n", This, ref);
435
436     return ref;
437 }
438
439 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
440 {
441     DispatchEx *This = DISPATCHEX_THIS(iface);
442     LONG ref = InterlockedDecrement(&This->ref);
443
444     TRACE("(%p) ref=%d\n", This, ref);
445
446     if(!ref) {
447         dispex_prop_t *prop;
448
449         for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
450             if(prop->type == PROP_VARIANT)
451                 VariantClear(&prop->u.var);
452             heap_free(prop->name);
453         }
454         heap_free(This->props);
455         script_release(This->ctx);
456
457         if(This->builtin_info->destructor)
458             This->builtin_info->destructor(This);
459         else
460             heap_free(This);
461     }
462
463     return ref;
464 }
465
466 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
467 {
468     DispatchEx *This = DISPATCHEX_THIS(iface);
469
470     TRACE("(%p)->(%p)\n", This, pctinfo);
471
472     *pctinfo = 1;
473     return S_OK;
474 }
475
476 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
477                                               LCID lcid, ITypeInfo **ppTInfo)
478 {
479     DispatchEx *This = DISPATCHEX_THIS(iface);
480     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
481     return E_NOTIMPL;
482 }
483
484 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
485                                                 LPOLESTR *rgszNames, UINT cNames,
486                                                 LCID lcid, DISPID *rgDispId)
487 {
488     DispatchEx *This = DISPATCHEX_THIS(iface);
489     UINT i;
490     HRESULT hres;
491
492     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
493           lcid, rgDispId);
494
495     for(i=0; i < cNames; i++) {
496         hres = IDispatchEx_GetDispID(_IDispatchEx_(This), rgszNames[i], 0, rgDispId+i);
497         if(FAILED(hres))
498             return hres;
499     }
500
501     return S_OK;
502 }
503
504 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
505                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
506                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
507 {
508     DispatchEx *This = DISPATCHEX_THIS(iface);
509
510     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
511           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
512
513     return IDispatchEx_InvokeEx(_IDispatchEx_(This), dispIdMember, lcid, wFlags,
514             pDispParams, pVarResult, pExcepInfo, NULL);
515 }
516
517 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
518 {
519     DispatchEx *This = DISPATCHEX_THIS(iface);
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     return jsdisp_get_id(This, bstrName, grfdex, pid);
529 }
530
531 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
532         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
533 {
534     DispatchEx *This = DISPATCHEX_THIS(iface);
535     dispex_prop_t *prop;
536     jsexcept_t jsexcept;
537     HRESULT hres;
538
539     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
540
541     if(pvarRes)
542         V_VT(pvarRes) = VT_EMPTY;
543
544     prop = get_prop(This, id);
545     if(!prop || prop->type == PROP_DELETED) {
546         TRACE("invalid id\n");
547         return DISP_E_MEMBERNOTFOUND;
548     }
549
550     memset(&jsexcept, 0, sizeof(jsexcept));
551
552     switch(wFlags) {
553     case DISPATCH_METHOD:
554     case DISPATCH_CONSTRUCT:
555         hres = invoke_prop_func(This, This, prop, lcid, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
556         break;
557     case DISPATCH_PROPERTYGET:
558         hres = prop_get(This, prop, lcid, pdp, pvarRes, &jsexcept, pspCaller);
559         break;
560     case DISPATCH_PROPERTYPUT:
561         hres = prop_put(This, prop, lcid, pdp, &jsexcept, pspCaller);
562         break;
563     default:
564         FIXME("Unimplemented flags %x\n", wFlags);
565         return E_INVALIDARG;
566     }
567
568     if(pei)
569         *pei = jsexcept.ei;
570
571     return hres;
572 }
573
574 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
575 {
576     DispatchEx *This = DISPATCHEX_THIS(iface);
577     dispex_prop_t *prop;
578     HRESULT hres;
579
580     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
581
582     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
583         FIXME("Unsupported grfdex %x\n", grfdex);
584
585     hres = find_prop_name(This, bstrName, &prop);
586     if(FAILED(hres))
587         return hres;
588     if(!prop) {
589         TRACE("not found\n");
590         return S_OK;
591     }
592
593     heap_free(prop->name);
594     prop->name = NULL;
595     prop->type = PROP_DELETED;
596     return S_OK;
597 }
598
599 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
600 {
601     DispatchEx *This = DISPATCHEX_THIS(iface);
602     FIXME("(%p)->(%x)\n", This, id);
603     return E_NOTIMPL;
604 }
605
606 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
607 {
608     DispatchEx *This = DISPATCHEX_THIS(iface);
609     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
610     return E_NOTIMPL;
611 }
612
613 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
614 {
615     DispatchEx *This = DISPATCHEX_THIS(iface);
616     dispex_prop_t *prop;
617
618     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
619
620     prop = get_prop(This, id);
621     if(!prop || !prop->name || prop->type == PROP_DELETED)
622         return DISP_E_MEMBERNOTFOUND;
623
624     *pbstrName = SysAllocString(prop->name);
625     if(!*pbstrName)
626         return E_OUTOFMEMORY;
627
628     return S_OK;
629 }
630
631 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
632 {
633     DispatchEx *This = DISPATCHEX_THIS(iface);
634     dispex_prop_t *iter;
635     HRESULT hres;
636
637     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
638
639     if(id == DISPID_STARTENUM) {
640         hres = fill_protrefs(This);
641         if(FAILED(hres))
642             return hres;
643     }
644
645     iter = get_prop(This, id+1);
646     if(!iter) {
647         *pid = DISPID_STARTENUM;
648         return S_FALSE;
649     }
650
651     while(iter < This->props + This->prop_cnt) {
652         if(iter->name && (get_flags(This, iter) & PROPF_ENUM)) {
653             *pid = prop_to_id(This, iter);
654             return S_OK;
655         }
656         iter++;
657     }
658
659     *pid = DISPID_STARTENUM;
660     return S_FALSE;
661 }
662
663 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
664 {
665     DispatchEx *This = DISPATCHEX_THIS(iface);
666     FIXME("(%p)->(%p)\n", This, ppunk);
667     return E_NOTIMPL;
668 }
669
670 #undef DISPATCHEX_THIS
671
672 static IDispatchExVtbl DispatchExVtbl = {
673     DispatchEx_QueryInterface,
674     DispatchEx_AddRef,
675     DispatchEx_Release,
676     DispatchEx_GetTypeInfoCount,
677     DispatchEx_GetTypeInfo,
678     DispatchEx_GetIDsOfNames,
679     DispatchEx_Invoke,
680     DispatchEx_GetDispID,
681     DispatchEx_InvokeEx,
682     DispatchEx_DeleteMemberByName,
683     DispatchEx_DeleteMemberByDispID,
684     DispatchEx_GetMemberProperties,
685     DispatchEx_GetMemberName,
686     DispatchEx_GetNextDispID,
687     DispatchEx_GetNameSpaceParent
688 };
689
690 HRESULT init_dispex(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype)
691 {
692     TRACE("%p (%p)\n", dispex, prototype);
693
694     dispex->lpIDispatchExVtbl = &DispatchExVtbl;
695     dispex->ref = 1;
696     dispex->builtin_info = builtin_info;
697
698     dispex->props = heap_alloc((dispex->buf_size=4) * sizeof(dispex_prop_t));
699     if(!dispex->props)
700         return E_OUTOFMEMORY;
701
702     dispex->prototype = prototype;
703     if(prototype)
704         IDispatchEx_AddRef(_IDispatchEx_(prototype));
705
706     dispex->prop_cnt = 1;
707     dispex->props[0].name = NULL;
708     dispex->props[0].flags = 0;
709     if(builtin_info->value_prop.invoke) {
710         dispex->props[0].type = PROP_BUILTIN;
711         dispex->props[0].u.p = &builtin_info->value_prop;
712     }else {
713         dispex->props[0].type = PROP_DELETED;
714     }
715
716     script_addref(ctx);
717     dispex->ctx = ctx;
718
719     return S_OK;
720 }
721
722 static const builtin_info_t dispex_info = {
723     JSCLASS_NONE,
724     {NULL, NULL, 0},
725     0, NULL,
726     NULL,
727     NULL
728 };
729
730 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype, DispatchEx **dispex)
731 {
732     DispatchEx *ret;
733     HRESULT hres;
734
735     ret = heap_alloc_zero(sizeof(DispatchEx));
736     if(!ret)
737         return E_OUTOFMEMORY;
738
739     hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
740     if(FAILED(hres))
741         return hres;
742
743     *dispex = ret;
744     return S_OK;
745 }
746
747 HRESULT init_dispex_from_constr(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *constr)
748 {
749     DispatchEx *prot = NULL;
750     dispex_prop_t *prop;
751     HRESULT hres;
752
753     static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
754
755     hres = find_prop_name_prot(constr, prototypeW, FALSE, &prop);
756     if(SUCCEEDED(hres) && prop) {
757         jsexcept_t jsexcept;
758         VARIANT var;
759
760         V_VT(&var) = VT_EMPTY;
761         memset(&jsexcept, 0, sizeof(jsexcept));
762         hres = prop_get(constr, prop, ctx->lcid, NULL, &var, &jsexcept, NULL/*FIXME*/);
763         if(FAILED(hres)) {
764             ERR("Could not get prototype\n");
765             return hres;
766         }
767
768         if(V_VT(&var) == VT_DISPATCH)
769             prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
770         VariantClear(&var);
771     }
772
773     hres = init_dispex(dispex, ctx, builtin_info, prot);
774
775     if(prot)
776         IDispatchEx_Release(_IDispatchEx_(prot));
777     return hres;
778 }
779
780 DispatchEx *iface_to_jsdisp(IUnknown *iface)
781 {
782     DispatchEx *ret;
783     HRESULT hres;
784
785     hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
786     if(FAILED(hres))
787         return NULL;
788
789     return ret;
790 }
791
792 HRESULT jsdisp_get_id(DispatchEx *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
793 {
794     dispex_prop_t *prop;
795     HRESULT hres;
796
797     hres = find_prop_name_prot(jsdisp, name, (flags&fdexNameEnsure) != 0, &prop);
798     if(FAILED(hres))
799         return hres;
800
801     if(prop) {
802         *id = prop_to_id(jsdisp, prop);
803         return S_OK;
804     }
805
806     TRACE("not found %s\n", debugstr_w(name));
807     return DISP_E_UNKNOWNNAME;
808 }
809
810 HRESULT jsdisp_call_value(DispatchEx *disp, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv,
811         jsexcept_t *ei, IServiceProvider *caller)
812 {
813     return disp->builtin_info->value_prop.invoke(disp, lcid, flags, dp, retv, ei, caller);
814 }
815
816 HRESULT jsdisp_call(DispatchEx *disp, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv,
817         jsexcept_t *ei, IServiceProvider *caller)
818 {
819     dispex_prop_t *prop;
820
821     memset(ei, 0, sizeof(*ei));
822     if(retv)
823         V_VT(retv) = VT_EMPTY;
824
825     prop = get_prop(disp, id);
826     if(!prop)
827         return DISP_E_MEMBERNOTFOUND;
828
829     return invoke_prop_func(disp, disp, prop, lcid, flags, dp, retv, ei, caller);
830 }
831
832 HRESULT disp_call(IDispatch *disp, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv,
833         jsexcept_t *ei, IServiceProvider *caller)
834 {
835     DispatchEx *jsdisp;
836     IDispatchEx *dispex;
837     HRESULT hres;
838
839     jsdisp = iface_to_jsdisp((IUnknown*)disp);
840     if(jsdisp) {
841         hres = jsdisp_call(jsdisp, id, lcid, flags, dp, retv, ei, caller);
842         IDispatchEx_Release(_IDispatchEx_(jsdisp));
843         return hres;
844     }
845
846     memset(ei, 0, sizeof(*ei));
847
848     if(retv)
849         V_VT(retv) = VT_EMPTY;
850     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
851     if(FAILED(hres)) {
852         UINT err = 0;
853
854         if(flags == DISPATCH_CONSTRUCT) {
855             WARN("IDispatch cannot be constructor\n");
856             return DISP_E_MEMBERNOTFOUND;
857         }
858
859         TRACE("using IDispatch\n");
860         return IDispatch_Invoke(disp, id, &IID_NULL, lcid, flags, dp, retv, &ei->ei, &err);
861     }
862
863     hres = IDispatchEx_InvokeEx(dispex, id, lcid, flags, dp, retv, &ei->ei, caller);
864     IDispatchEx_Release(dispex);
865
866     return hres;
867 }
868
869 HRESULT jsdisp_propput_name(DispatchEx *obj, const WCHAR *name, LCID lcid, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
870 {
871     DISPID named_arg = DISPID_PROPERTYPUT;
872     DISPPARAMS dp = {val, &named_arg, 1, 1};
873     dispex_prop_t *prop;
874     HRESULT hres;
875
876     hres = find_prop_name_prot(obj, name, TRUE, &prop);
877     if(FAILED(hres))
878         return hres;
879
880     return prop_put(obj, prop, lcid, &dp, ei, caller);
881 }
882
883 HRESULT jsdisp_propput_idx(DispatchEx *obj, DWORD idx, LCID lcid, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
884 {
885     WCHAR buf[12];
886
887     static const WCHAR formatW[] = {'%','d',0};
888
889     sprintfW(buf, formatW, idx);
890     return jsdisp_propput_name(obj, buf, lcid, val, ei, caller);
891 }
892
893 HRESULT disp_propput(IDispatch *disp, DISPID id, LCID lcid, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
894 {
895     DISPID dispid = DISPID_PROPERTYPUT;
896     DISPPARAMS dp  = {val, &dispid, 1, 1};
897     IDispatchEx *dispex;
898     DispatchEx *jsdisp;
899     HRESULT hres;
900
901     jsdisp = iface_to_jsdisp((IUnknown*)disp);
902     if(jsdisp) {
903         dispex_prop_t *prop;
904
905         prop = get_prop(jsdisp, id);
906         if(prop)
907             hres = prop_put(jsdisp, prop, lcid, &dp, ei, caller);
908         else
909             hres = DISP_E_MEMBERNOTFOUND;
910
911         IDispatchEx_Release(_IDispatchEx_(jsdisp));
912         return hres;
913     }
914
915     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
916     if(FAILED(hres)) {
917         ULONG err = 0;
918
919         TRACE("using IDispatch\n");
920         return IDispatch_Invoke(disp, id, &IID_NULL, DISPATCH_PROPERTYPUT, lcid, &dp, NULL, &ei->ei, &err);
921     }
922
923     hres = IDispatchEx_InvokeEx(dispex, id, lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
924
925     IDispatchEx_Release(dispex);
926     return hres;
927 }
928
929 static HRESULT jsdisp_propget_name(DispatchEx *obj, const WCHAR *name, LCID lcid, VARIANT *var,
930         jsexcept_t *ei, IServiceProvider *caller)
931 {
932     DISPPARAMS dp = {NULL, NULL, 0, 0};
933     dispex_prop_t *prop;
934     HRESULT hres;
935
936     hres = find_prop_name_prot(obj, name, FALSE, &prop);
937     if(FAILED(hres))
938         return hres;
939
940     V_VT(var) = VT_EMPTY;
941     if(!prop)
942         return S_OK;
943
944     return prop_get(obj, prop, lcid, &dp, var, ei, caller);
945 }
946
947 HRESULT jsdisp_propget_idx(DispatchEx *obj, DWORD idx, LCID lcid, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
948 {
949     WCHAR buf[12];
950
951     static const WCHAR formatW[] = {'%','d',0};
952
953     sprintfW(buf, formatW, idx);
954     return jsdisp_propget_name(obj, buf, lcid, var, ei, caller);
955 }
956
957 HRESULT disp_propget(IDispatch *disp, DISPID id, LCID lcid, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
958 {
959     DISPPARAMS dp  = {NULL,NULL,0,0};
960     IDispatchEx *dispex;
961     DispatchEx *jsdisp;
962     HRESULT hres;
963
964     jsdisp = iface_to_jsdisp((IUnknown*)disp);
965     if(jsdisp) {
966         dispex_prop_t *prop;
967
968         prop = get_prop(jsdisp, id);
969         if(prop)
970             hres = prop_get(jsdisp, prop, lcid, &dp, val, ei, caller);
971         else
972             hres = DISP_E_MEMBERNOTFOUND;
973
974         IDispatchEx_Release(_IDispatchEx_(jsdisp));
975         return hres;
976     }
977
978     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
979     if(FAILED(hres)) {
980         ULONG err = 0;
981
982         TRACE("uding IDispatch\n");
983         return IDispatchEx_Invoke(dispex, id, &IID_NULL, lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
984     }
985
986     hres = IDispatchEx_InvokeEx(dispex, id, lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
987     IDispatchEx_Release(dispex);
988
989     return hres;
990 }