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