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