jscript: Store builtin constructor's length in instance object.
[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 #define FDEX_VERSION_MASK 0xf0000000
34
35 typedef enum {
36     PROP_VARIANT,
37     PROP_BUILTIN,
38     PROP_PROTREF,
39     PROP_DELETED
40 } prop_type_t;
41
42 struct _dispex_prop_t {
43     WCHAR *name;
44     prop_type_t type;
45     DWORD flags;
46
47     union {
48         VARIANT var;
49         const builtin_prop_t *p;
50         DWORD ref;
51     } u;
52 };
53
54 static inline DISPID prop_to_id(DispatchEx *This, dispex_prop_t *prop)
55 {
56     return prop - This->props;
57 }
58
59 static inline dispex_prop_t *get_prop(DispatchEx *This, DISPID id)
60 {
61     if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
62         return NULL;
63
64     return This->props+id;
65 }
66
67 static DWORD get_flags(DispatchEx *This, dispex_prop_t *prop)
68 {
69     if(prop->type == PROP_PROTREF) {
70         dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
71         if(!parent) {
72             prop->type = PROP_DELETED;
73             return 0;
74         }
75
76         return get_flags(This->prototype, parent);
77     }
78
79     return prop->flags;
80 }
81
82 static const builtin_prop_t *find_builtin_prop(DispatchEx *This, const WCHAR *name)
83 {
84     int min = 0, max, i, r;
85
86     max = This->builtin_info->props_cnt-1;
87     while(min <= max) {
88         i = (min+max)/2;
89
90         r = strcmpW(name, This->builtin_info->props[i].name);
91         if(!r)
92             return This->builtin_info->props + i;
93
94         if(r < 0)
95             max = i-1;
96         else
97             min = i+1;
98     }
99
100     return NULL;
101 }
102
103 static dispex_prop_t *alloc_prop(DispatchEx *This, const WCHAR *name, prop_type_t type, DWORD flags)
104 {
105     dispex_prop_t *ret;
106
107     if(This->buf_size == This->prop_cnt) {
108         dispex_prop_t *tmp = heap_realloc(This->props, (This->buf_size<<=1)*sizeof(*This->props));
109         if(!tmp)
110             return NULL;
111         This->props = tmp;
112     }
113
114     ret = This->props + This->prop_cnt++;
115     ret->type = type;
116     ret->flags = flags;
117     ret->name = heap_strdupW(name);
118     if(!ret->name)
119         return NULL;
120
121     return ret;
122 }
123
124 static dispex_prop_t *alloc_protref(DispatchEx *This, const WCHAR *name, DWORD ref)
125 {
126     dispex_prop_t *ret;
127
128     ret = alloc_prop(This, name, PROP_PROTREF, 0);
129     if(!ret)
130         return NULL;
131
132     ret->u.ref = ref;
133     return ret;
134 }
135
136 static HRESULT find_prop_name(DispatchEx *This, const WCHAR *name, dispex_prop_t **ret)
137 {
138     const builtin_prop_t *builtin;
139     dispex_prop_t *prop;
140
141     for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
142         if(prop->name && !strcmpW(prop->name, name)) {
143             *ret = prop;
144             return S_OK;
145         }
146     }
147
148     builtin = find_builtin_prop(This, name);
149     if(builtin) {
150         prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
151         if(!prop)
152             return E_OUTOFMEMORY;
153
154         prop->u.p = builtin;
155         *ret = prop;
156         return S_OK;
157     }
158
159     *ret = NULL;
160     return S_OK;
161 }
162
163 static HRESULT find_prop_name_prot(DispatchEx *This, const WCHAR *name, dispex_prop_t **ret)
164 {
165     dispex_prop_t *prop;
166     HRESULT hres;
167
168     hres = find_prop_name(This, name, &prop);
169     if(FAILED(hres))
170         return hres;
171     if(prop) {
172         *ret = prop;
173         return S_OK;
174     }
175
176     if(This->prototype) {
177         hres = find_prop_name_prot(This->prototype, name, &prop);
178         if(FAILED(hres))
179             return hres;
180         if(prop) {
181             prop = alloc_protref(This, prop->name, prop - This->prototype->props);
182             if(!prop)
183                 return E_OUTOFMEMORY;
184             *ret = prop;
185             return S_OK;
186         }
187     }
188
189     *ret = prop;
190     return S_OK;
191 }
192
193 static HRESULT ensure_prop_name(DispatchEx *This, const WCHAR *name, BOOL search_prot, DWORD create_flags, dispex_prop_t **ret)
194 {
195     dispex_prop_t *prop;
196     HRESULT hres;
197
198     if(search_prot)
199         hres = find_prop_name_prot(This, name, &prop);
200     else
201         hres = find_prop_name(This, name, &prop);
202     if(SUCCEEDED(hres) && !prop) {
203         TRACE("creating prop %s\n", debugstr_w(name));
204
205         prop = alloc_prop(This, name, PROP_VARIANT, create_flags);
206         if(!prop)
207             return E_OUTOFMEMORY;
208         VariantInit(&prop->u.var);
209     }
210
211     *ret = prop;
212     return hres;
213 }
214
215 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
216 {
217     VARIANTARG *oldargs;
218     int i;
219
220     static DISPID this_id = DISPID_THIS;
221
222     *dp = *olddp;
223
224     for(i = 0; i < dp->cNamedArgs; i++) {
225         if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
226             return S_OK;
227     }
228
229     oldargs = dp->rgvarg;
230     dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
231     if(!dp->rgvarg)
232         return E_OUTOFMEMORY;
233     memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
234     V_VT(dp->rgvarg) = VT_DISPATCH;
235     V_DISPATCH(dp->rgvarg) = jsthis;
236     dp->cArgs++;
237
238     if(dp->cNamedArgs) {
239         DISPID *old = dp->rgdispidNamedArgs;
240         dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
241         if(!dp->rgdispidNamedArgs) {
242             heap_free(dp->rgvarg);
243             return E_OUTOFMEMORY;
244         }
245
246         memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
247         dp->rgdispidNamedArgs[0] = DISPID_THIS;
248         dp->cNamedArgs++;
249     }else {
250         dp->rgdispidNamedArgs = &this_id;
251         dp->cNamedArgs = 1;
252     }
253
254     return S_OK;
255 }
256
257 static HRESULT invoke_prop_func(DispatchEx *This, DispatchEx *jsthis, dispex_prop_t *prop, WORD flags,
258         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
259 {
260     HRESULT hres;
261
262     switch(prop->type) {
263     case PROP_BUILTIN: {
264         vdisp_t vthis;
265
266         if(flags == DISPATCH_CONSTRUCT && (prop->flags & DISPATCH_METHOD)) {
267             WARN("%s is not a constructor\n", debugstr_w(prop->name));
268             return E_INVALIDARG;
269         }
270
271         set_jsdisp(&vthis, jsthis);
272         hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei, caller);
273         vdisp_release(&vthis);
274         return hres;
275     }
276     case PROP_PROTREF:
277         return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
278     case PROP_VARIANT: {
279         DISPPARAMS new_dp;
280
281         if(V_VT(&prop->u.var) != VT_DISPATCH) {
282             FIXME("invoke vt %d\n", V_VT(&prop->u.var));
283             return E_FAIL;
284         }
285
286         TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
287
288         hres = set_this(&new_dp, dp, (IDispatch*)_IDispatchEx_(jsthis));
289         if(FAILED(hres))
290             return hres;
291
292         hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei, caller);
293
294         if(new_dp.rgvarg != dp->rgvarg) {
295             heap_free(new_dp.rgvarg);
296             if(new_dp.cNamedArgs > 1)
297                 heap_free(new_dp.rgdispidNamedArgs);
298         }
299
300         return hres;
301     }
302     default:
303         ERR("type %d\n", prop->type);
304     }
305
306     return E_FAIL;
307 }
308
309 static HRESULT prop_get(DispatchEx *This, dispex_prop_t *prop, DISPPARAMS *dp,
310         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
311 {
312     HRESULT hres;
313
314     switch(prop->type) {
315     case PROP_BUILTIN:
316         if(prop->u.p->flags & PROPF_METHOD) {
317             DispatchEx *obj;
318             hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
319                     prop->u.p->flags, NULL, &obj);
320             if(FAILED(hres))
321                 break;
322
323             prop->type = PROP_VARIANT;
324             V_VT(&prop->u.var) = VT_DISPATCH;
325             V_DISPATCH(&prop->u.var) = (IDispatch*)_IDispatchEx_(obj);
326
327             hres = VariantCopy(retv, &prop->u.var);
328         }else {
329             vdisp_t vthis;
330
331             set_jsdisp(&vthis, This);
332             hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
333             vdisp_release(&vthis);
334         }
335         break;
336     case PROP_PROTREF:
337         hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
338         break;
339     case PROP_VARIANT:
340         hres = VariantCopy(retv, &prop->u.var);
341         break;
342     default:
343         ERR("type %d\n", prop->type);
344         return E_FAIL;
345     }
346
347     if(FAILED(hres)) {
348         TRACE("fail %08x\n", hres);
349         return hres;
350     }
351
352     TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
353     return hres;
354 }
355
356 static HRESULT prop_put(DispatchEx *This, dispex_prop_t *prop, VARIANT *val,
357         jsexcept_t *ei, IServiceProvider *caller)
358 {
359     HRESULT hres;
360
361     if(prop->flags & PROPF_CONST)
362         return S_OK;
363
364     switch(prop->type) {
365     case PROP_BUILTIN:
366         if(!(prop->flags & PROPF_METHOD)) {
367             DISPPARAMS dp = {val, NULL, 1, 0};
368             vdisp_t vthis;
369
370             set_jsdisp(&vthis, This);
371             hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei, caller);
372             vdisp_release(&vthis);
373             return hres;
374         }
375     case PROP_PROTREF:
376         prop->type = PROP_VARIANT;
377         prop->flags = PROPF_ENUM;
378         V_VT(&prop->u.var) = VT_EMPTY;
379         break;
380     case PROP_VARIANT:
381         VariantClear(&prop->u.var);
382         break;
383     default:
384         ERR("type %d\n", prop->type);
385         return E_FAIL;
386     }
387
388     hres = VariantCopy(&prop->u.var, val);
389     if(FAILED(hres))
390         return hres;
391
392     if(This->builtin_info->on_put)
393         This->builtin_info->on_put(This, prop->name);
394
395     TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
396     return S_OK;
397 }
398
399 static HRESULT fill_protrefs(DispatchEx *This)
400 {
401     dispex_prop_t *iter, *prop;
402     HRESULT hres;
403
404     if(!This->prototype)
405         return S_OK;
406
407     fill_protrefs(This->prototype);
408
409     for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
410         if(!iter->name)
411             continue;
412         hres = find_prop_name(This, iter->name, &prop);
413         if(FAILED(hres))
414             return hres;
415         if(!prop) {
416             prop = alloc_protref(This, iter->name, iter - This->prototype->props);
417             if(!prop)
418                 return E_OUTOFMEMORY;
419         }
420     }
421
422     return S_OK;
423 }
424
425 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
426
427 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
428 {
429     DispatchEx *This = DISPATCHEX_THIS(iface);
430
431     if(IsEqualGUID(&IID_IUnknown, riid)) {
432         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
433         *ppv = _IDispatchEx_(This);
434     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
435         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
436         *ppv = _IDispatchEx_(This);
437     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
438         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
439         *ppv = _IDispatchEx_(This);
440     }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
441         TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
442         IUnknown_AddRef(_IDispatchEx_(This));
443         *ppv = This;
444         return S_OK;
445     }else {
446         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
447         *ppv = NULL;
448         return E_NOINTERFACE;
449     }
450
451     IUnknown_AddRef((IUnknown*)*ppv);
452     return S_OK;
453 }
454
455 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
456 {
457     DispatchEx *This = DISPATCHEX_THIS(iface);
458     LONG ref = InterlockedIncrement(&This->ref);
459
460     TRACE("(%p) ref=%d\n", This, ref);
461
462     return ref;
463 }
464
465 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
466 {
467     DispatchEx *This = DISPATCHEX_THIS(iface);
468     LONG ref = InterlockedDecrement(&This->ref);
469
470     TRACE("(%p) ref=%d\n", This, ref);
471
472     if(!ref) {
473         dispex_prop_t *prop;
474
475         for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
476             if(prop->type == PROP_VARIANT)
477                 VariantClear(&prop->u.var);
478             heap_free(prop->name);
479         }
480         heap_free(This->props);
481         script_release(This->ctx);
482         if(This->prototype)
483             jsdisp_release(This->prototype);
484
485         if(This->builtin_info->destructor)
486             This->builtin_info->destructor(This);
487         else
488             heap_free(This);
489     }
490
491     return ref;
492 }
493
494 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
495 {
496     DispatchEx *This = DISPATCHEX_THIS(iface);
497
498     TRACE("(%p)->(%p)\n", This, pctinfo);
499
500     *pctinfo = 1;
501     return S_OK;
502 }
503
504 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
505                                               ITypeInfo **ppTInfo)
506 {
507     DispatchEx *This = DISPATCHEX_THIS(iface);
508     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
509     return E_NOTIMPL;
510 }
511
512 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
513                                                 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
514                                                 DISPID *rgDispId)
515 {
516     DispatchEx *This = DISPATCHEX_THIS(iface);
517     UINT i;
518     HRESULT hres;
519
520     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
521           lcid, rgDispId);
522
523     for(i=0; i < cNames; i++) {
524         hres = IDispatchEx_GetDispID(_IDispatchEx_(This), rgszNames[i], 0, rgDispId+i);
525         if(FAILED(hres))
526             return hres;
527     }
528
529     return S_OK;
530 }
531
532 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
533                                         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
534                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
535 {
536     DispatchEx *This = DISPATCHEX_THIS(iface);
537
538     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
539           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
540
541     return IDispatchEx_InvokeEx(_IDispatchEx_(This), dispIdMember, lcid, wFlags,
542             pDispParams, pVarResult, pExcepInfo, NULL);
543 }
544
545 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
546 {
547     DispatchEx *This = DISPATCHEX_THIS(iface);
548
549     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
550
551     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
552         FIXME("Unsupported grfdex %x\n", grfdex);
553         return E_NOTIMPL;
554     }
555
556     return jsdisp_get_id(This, bstrName, grfdex, pid);
557 }
558
559 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
560         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
561 {
562     DispatchEx *This = DISPATCHEX_THIS(iface);
563     dispex_prop_t *prop;
564     jsexcept_t jsexcept;
565     HRESULT hres;
566
567     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
568
569     if(pvarRes)
570         V_VT(pvarRes) = VT_EMPTY;
571
572     prop = get_prop(This, id);
573     if(!prop || prop->type == PROP_DELETED) {
574         TRACE("invalid id\n");
575         return DISP_E_MEMBERNOTFOUND;
576     }
577
578     memset(&jsexcept, 0, sizeof(jsexcept));
579
580     switch(wFlags) {
581     case DISPATCH_METHOD:
582     case DISPATCH_CONSTRUCT:
583         hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
584         break;
585     case DISPATCH_PROPERTYGET:
586         hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
587         break;
588     case DISPATCH_PROPERTYPUT: {
589         DWORD i;
590
591         for(i=0; i < pdp->cNamedArgs; i++) {
592             if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
593                 break;
594         }
595
596         if(i == pdp->cNamedArgs) {
597             TRACE("no value to set\n");
598             return DISP_E_PARAMNOTOPTIONAL;
599         }
600
601         hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
602         break;
603     }
604     default:
605         FIXME("Unimplemented flags %x\n", wFlags);
606         return E_INVALIDARG;
607     }
608
609     if(pei)
610         *pei = jsexcept.ei;
611
612     return hres;
613 }
614
615 static HRESULT delete_prop(dispex_prop_t *prop)
616 {
617     heap_free(prop->name);
618     prop->name = NULL;
619     prop->type = PROP_DELETED;
620
621     return S_OK;
622 }
623
624 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
625 {
626     DispatchEx *This = DISPATCHEX_THIS(iface);
627     dispex_prop_t *prop;
628     HRESULT hres;
629
630     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
631
632     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
633         FIXME("Unsupported grfdex %x\n", grfdex);
634
635     hres = find_prop_name(This, bstrName, &prop);
636     if(FAILED(hres))
637         return hres;
638     if(!prop) {
639         TRACE("not found\n");
640         return S_OK;
641     }
642
643     return delete_prop(prop);
644 }
645
646 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
647 {
648     DispatchEx *This = DISPATCHEX_THIS(iface);
649     dispex_prop_t *prop;
650
651     TRACE("(%p)->(%x)\n", This, id);
652
653     prop = get_prop(This, id);
654     if(!prop) {
655         WARN("invalid id\n");
656         return DISP_E_MEMBERNOTFOUND;
657     }
658
659     return delete_prop(prop);
660 }
661
662 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
663 {
664     DispatchEx *This = DISPATCHEX_THIS(iface);
665     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
666     return E_NOTIMPL;
667 }
668
669 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
670 {
671     DispatchEx *This = DISPATCHEX_THIS(iface);
672     dispex_prop_t *prop;
673
674     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
675
676     prop = get_prop(This, id);
677     if(!prop || !prop->name || prop->type == PROP_DELETED)
678         return DISP_E_MEMBERNOTFOUND;
679
680     *pbstrName = SysAllocString(prop->name);
681     if(!*pbstrName)
682         return E_OUTOFMEMORY;
683
684     return S_OK;
685 }
686
687 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
688 {
689     DispatchEx *This = DISPATCHEX_THIS(iface);
690     dispex_prop_t *iter;
691     HRESULT hres;
692
693     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
694
695     if(id == DISPID_STARTENUM) {
696         hres = fill_protrefs(This);
697         if(FAILED(hres))
698             return hres;
699     }
700
701     iter = get_prop(This, id+1);
702     if(!iter) {
703         *pid = DISPID_STARTENUM;
704         return S_FALSE;
705     }
706
707     while(iter < This->props + This->prop_cnt) {
708         if(iter->name && (get_flags(This, iter) & PROPF_ENUM)) {
709             *pid = prop_to_id(This, iter);
710             return S_OK;
711         }
712         iter++;
713     }
714
715     *pid = DISPID_STARTENUM;
716     return S_FALSE;
717 }
718
719 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
720 {
721     DispatchEx *This = DISPATCHEX_THIS(iface);
722     FIXME("(%p)->(%p)\n", This, ppunk);
723     return E_NOTIMPL;
724 }
725
726 #undef DISPATCHEX_THIS
727
728 static IDispatchExVtbl DispatchExVtbl = {
729     DispatchEx_QueryInterface,
730     DispatchEx_AddRef,
731     DispatchEx_Release,
732     DispatchEx_GetTypeInfoCount,
733     DispatchEx_GetTypeInfo,
734     DispatchEx_GetIDsOfNames,
735     DispatchEx_Invoke,
736     DispatchEx_GetDispID,
737     DispatchEx_InvokeEx,
738     DispatchEx_DeleteMemberByName,
739     DispatchEx_DeleteMemberByDispID,
740     DispatchEx_GetMemberProperties,
741     DispatchEx_GetMemberName,
742     DispatchEx_GetNextDispID,
743     DispatchEx_GetNameSpaceParent
744 };
745
746 HRESULT init_dispex(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype)
747 {
748     TRACE("%p (%p)\n", dispex, prototype);
749
750     dispex->lpIDispatchExVtbl = &DispatchExVtbl;
751     dispex->ref = 1;
752     dispex->builtin_info = builtin_info;
753
754     dispex->props = heap_alloc((dispex->buf_size=4) * sizeof(dispex_prop_t));
755     if(!dispex->props)
756         return E_OUTOFMEMORY;
757
758     dispex->prototype = prototype;
759     if(prototype)
760         IDispatchEx_AddRef(_IDispatchEx_(prototype));
761
762     dispex->prop_cnt = 1;
763     dispex->props[0].name = NULL;
764     dispex->props[0].flags = 0;
765     if(builtin_info->value_prop.invoke) {
766         dispex->props[0].type = PROP_BUILTIN;
767         dispex->props[0].u.p = &builtin_info->value_prop;
768     }else {
769         dispex->props[0].type = PROP_DELETED;
770     }
771
772     script_addref(ctx);
773     dispex->ctx = ctx;
774
775     return S_OK;
776 }
777
778 static const builtin_info_t dispex_info = {
779     JSCLASS_NONE,
780     {NULL, NULL, 0},
781     0, NULL,
782     NULL,
783     NULL
784 };
785
786 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype, DispatchEx **dispex)
787 {
788     DispatchEx *ret;
789     HRESULT hres;
790
791     ret = heap_alloc_zero(sizeof(DispatchEx));
792     if(!ret)
793         return E_OUTOFMEMORY;
794
795     hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
796     if(FAILED(hres))
797         return hres;
798
799     *dispex = ret;
800     return S_OK;
801 }
802
803 HRESULT init_dispex_from_constr(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *constr)
804 {
805     DispatchEx *prot = NULL;
806     dispex_prop_t *prop;
807     HRESULT hres;
808
809     static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r'};
810     static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
811
812     hres = find_prop_name_prot(constr, prototypeW, &prop);
813     if(SUCCEEDED(hres) && prop) {
814         jsexcept_t jsexcept;
815         VARIANT var;
816
817         V_VT(&var) = VT_EMPTY;
818         memset(&jsexcept, 0, sizeof(jsexcept));
819         hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
820         if(FAILED(hres)) {
821             ERR("Could not get prototype\n");
822             return hres;
823         }
824
825         if(V_VT(&var) == VT_DISPATCH)
826             prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
827         VariantClear(&var);
828     }
829
830     hres = init_dispex(dispex, ctx, builtin_info, prot);
831
832     if(prot)
833         jsdisp_release(prot);
834     if(FAILED(hres))
835         return hres;
836
837     hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
838     if(SUCCEEDED(hres)) {
839         jsexcept_t jsexcept;
840         VARIANT var;
841
842         V_VT(&var) = VT_DISPATCH;
843         V_DISPATCH(&var) = (IDispatch*)_IDispatchEx_(constr);
844         memset(&jsexcept, 0, sizeof(jsexcept));
845         hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
846     }
847     if(FAILED(hres))
848         jsdisp_release(dispex);
849
850     return hres;
851 }
852
853 DispatchEx *iface_to_jsdisp(IUnknown *iface)
854 {
855     DispatchEx *ret;
856     HRESULT hres;
857
858     hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
859     if(FAILED(hres))
860         return NULL;
861
862     return ret;
863 }
864
865 HRESULT jsdisp_get_id(DispatchEx *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
866 {
867     dispex_prop_t *prop;
868     HRESULT hres;
869
870     if(flags & fdexNameEnsure)
871         hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
872     else
873         hres = find_prop_name_prot(jsdisp, name, &prop);
874     if(FAILED(hres))
875         return hres;
876
877     if(prop) {
878         *id = prop_to_id(jsdisp, prop);
879         return S_OK;
880     }
881
882     TRACE("not found %s\n", debugstr_w(name));
883     return DISP_E_UNKNOWNNAME;
884 }
885
886 HRESULT jsdisp_call_value(DispatchEx *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv,
887         jsexcept_t *ei, IServiceProvider *caller)
888 {
889     vdisp_t vdisp;
890     HRESULT hres;
891
892     set_jsdisp(&vdisp, jsthis);
893     hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei, caller);
894     vdisp_release(&vdisp);
895     return hres;
896 }
897
898 HRESULT jsdisp_call(DispatchEx *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
899         jsexcept_t *ei, IServiceProvider *caller)
900 {
901     dispex_prop_t *prop;
902
903     memset(ei, 0, sizeof(*ei));
904     if(retv)
905         V_VT(retv) = VT_EMPTY;
906
907     prop = get_prop(disp, id);
908     if(!prop)
909         return DISP_E_MEMBERNOTFOUND;
910
911     return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
912 }
913
914 HRESULT jsdisp_call_name(DispatchEx *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
915         jsexcept_t *ei, IServiceProvider *caller)
916 {
917     dispex_prop_t *prop;
918     HRESULT hres;
919
920     hres = find_prop_name_prot(disp, name, &prop);
921     if(FAILED(hres))
922         return hres;
923
924     memset(ei, 0, sizeof(*ei));
925     if(retv)
926         V_VT(retv) = VT_EMPTY;
927
928     return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
929 }
930
931 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
932         jsexcept_t *ei, IServiceProvider *caller)
933 {
934     DispatchEx *jsdisp;
935     IDispatchEx *dispex;
936     HRESULT hres;
937
938     jsdisp = iface_to_jsdisp((IUnknown*)disp);
939     if(jsdisp) {
940         hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei, caller);
941         jsdisp_release(jsdisp);
942         return hres;
943     }
944
945     memset(ei, 0, sizeof(*ei));
946
947     if(retv)
948         V_VT(retv) = VT_EMPTY;
949     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
950     if(FAILED(hres)) {
951         UINT err = 0;
952
953         if(flags == DISPATCH_CONSTRUCT) {
954             WARN("IDispatch cannot be constructor\n");
955             return DISP_E_MEMBERNOTFOUND;
956         }
957
958         TRACE("using IDispatch\n");
959         return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
960     }
961
962     hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, caller);
963     IDispatchEx_Release(dispex);
964
965     return hres;
966 }
967
968 HRESULT jsdisp_propput_name(DispatchEx *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
969 {
970     dispex_prop_t *prop;
971     HRESULT hres;
972
973     hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
974     if(FAILED(hres))
975         return hres;
976
977     return prop_put(obj, prop, val, ei, caller);
978 }
979
980 HRESULT jsdisp_propput_const(DispatchEx *obj, const WCHAR *name, VARIANT *val)
981 {
982     dispex_prop_t *prop;
983     HRESULT hres;
984
985     hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
986     if(FAILED(hres))
987         return hres;
988
989     return VariantCopy(&prop->u.var, val);
990 }
991
992 HRESULT jsdisp_propput_idx(DispatchEx *obj, DWORD idx, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
993 {
994     WCHAR buf[12];
995
996     static const WCHAR formatW[] = {'%','d',0};
997
998     sprintfW(buf, formatW, idx);
999     return jsdisp_propput_name(obj, buf, val, ei, caller);
1000 }
1001
1002 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1003 {
1004     DispatchEx *jsdisp;
1005     HRESULT hres;
1006
1007     jsdisp = iface_to_jsdisp((IUnknown*)disp);
1008     if(jsdisp) {
1009         dispex_prop_t *prop;
1010
1011         prop = get_prop(jsdisp, id);
1012         if(prop)
1013             hres = prop_put(jsdisp, prop, val, ei, caller);
1014         else
1015             hres = DISP_E_MEMBERNOTFOUND;
1016
1017         jsdisp_release(jsdisp);
1018     }else {
1019         DISPID dispid = DISPID_PROPERTYPUT;
1020         DISPPARAMS dp  = {val, &dispid, 1, 1};
1021         IDispatchEx *dispex;
1022
1023         hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1024         if(SUCCEEDED(hres)) {
1025             hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
1026             IDispatchEx_Release(dispex);
1027         }else {
1028             ULONG err = 0;
1029
1030             TRACE("using IDispatch\n");
1031             hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1032         }
1033     }
1034
1035     return hres;
1036 }
1037
1038 HRESULT jsdisp_propget_name(DispatchEx *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1039 {
1040     DISPPARAMS dp = {NULL, NULL, 0, 0};
1041     dispex_prop_t *prop;
1042     HRESULT hres;
1043
1044     hres = find_prop_name_prot(obj, name, &prop);
1045     if(FAILED(hres))
1046         return hres;
1047
1048     V_VT(var) = VT_EMPTY;
1049     if(!prop)
1050         return S_OK;
1051
1052     return prop_get(obj, prop, &dp, var, ei, caller);
1053 }
1054
1055 HRESULT jsdisp_get_idx(DispatchEx *obj, DWORD idx, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1056 {
1057     WCHAR name[12];
1058     DISPPARAMS dp = {NULL, NULL, 0, 0};
1059     dispex_prop_t *prop;
1060     HRESULT hres;
1061
1062     static const WCHAR formatW[] = {'%','d',0};
1063
1064     sprintfW(name, formatW, idx);
1065
1066     hres = find_prop_name_prot(obj, name, &prop);
1067     if(FAILED(hres))
1068         return hres;
1069
1070     V_VT(var) = VT_EMPTY;
1071     if(!prop)
1072         return DISP_E_UNKNOWNNAME;
1073
1074     return prop_get(obj, prop, &dp, var, ei, caller);
1075 }
1076
1077 HRESULT jsdisp_propget(DispatchEx *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1078 {
1079     DISPPARAMS dp  = {NULL,NULL,0,0};
1080     dispex_prop_t *prop;
1081
1082     prop = get_prop(jsdisp, id);
1083     if(!prop)
1084         return DISP_E_MEMBERNOTFOUND;
1085
1086     V_VT(val) = VT_EMPTY;
1087     return prop_get(jsdisp, prop, &dp, val, ei, caller);
1088 }
1089
1090 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1091 {
1092     DISPPARAMS dp  = {NULL,NULL,0,0};
1093     IDispatchEx *dispex;
1094     DispatchEx *jsdisp;
1095     HRESULT hres;
1096
1097     jsdisp = iface_to_jsdisp((IUnknown*)disp);
1098     if(jsdisp) {
1099         hres = jsdisp_propget(jsdisp, id, val, ei, caller);
1100         jsdisp_release(jsdisp);
1101         return hres;
1102     }
1103
1104     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1105     if(FAILED(hres)) {
1106         ULONG err = 0;
1107
1108         TRACE("using IDispatch\n");
1109         return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1110     }
1111
1112     hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
1113     IDispatchEx_Release(dispex);
1114
1115     return hres;
1116 }
1117
1118 HRESULT jsdisp_delete_idx(DispatchEx *obj, DWORD idx)
1119 {
1120     static const WCHAR formatW[] = {'%','d',0};
1121     WCHAR buf[12];
1122     dispex_prop_t *prop;
1123     HRESULT hres;
1124
1125     sprintfW(buf, formatW, idx);
1126
1127     hres = find_prop_name(obj, buf, &prop);
1128     if(FAILED(hres) || !prop)
1129         return hres;
1130
1131     return delete_prop(prop);
1132 }