jscript: Be more verbose about parser failure.
[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 <assert.h>
20
21 #include "jscript.h"
22
23 #include "wine/unicode.h"
24 #include "wine/debug.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27
28 /*
29  * This IID is used to get jsdisp_t objecto from interface.
30  * We might consider using private interface instead.
31  */
32 static const IID IID_IDispatchJS =
33         {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
34
35 #define FDEX_VERSION_MASK 0xf0000000
36 #define GOLDEN_RATIO 0x9E3779B9U
37
38 typedef enum {
39     PROP_VARIANT,
40     PROP_BUILTIN,
41     PROP_PROTREF,
42     PROP_DELETED
43 } prop_type_t;
44
45 struct _dispex_prop_t {
46     WCHAR *name;
47     unsigned hash;
48     prop_type_t type;
49     DWORD flags;
50
51     union {
52         VARIANT var;
53         const builtin_prop_t *p;
54         DWORD ref;
55     } u;
56
57     int bucket_head;
58     int bucket_next;
59 };
60
61 static inline DISPID prop_to_id(jsdisp_t *This, dispex_prop_t *prop)
62 {
63     return prop - This->props;
64 }
65
66 static inline dispex_prop_t *get_prop(jsdisp_t *This, DISPID id)
67 {
68     if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
69         return NULL;
70
71     return This->props+id;
72 }
73
74 static DWORD get_flags(jsdisp_t *This, dispex_prop_t *prop)
75 {
76     if(prop->type == PROP_PROTREF) {
77         dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
78         if(!parent) {
79             prop->type = PROP_DELETED;
80             return 0;
81         }
82
83         return get_flags(This->prototype, parent);
84     }
85
86     return prop->flags;
87 }
88
89 static const builtin_prop_t *find_builtin_prop(jsdisp_t *This, const WCHAR *name)
90 {
91     int min = 0, max, i, r;
92
93     max = This->builtin_info->props_cnt-1;
94     while(min <= max) {
95         i = (min+max)/2;
96
97         r = strcmpW(name, This->builtin_info->props[i].name);
98         if(!r)
99             return This->builtin_info->props + i;
100
101         if(r < 0)
102             max = i-1;
103         else
104             min = i+1;
105     }
106
107     return NULL;
108 }
109
110 static inline unsigned string_hash(const WCHAR *name)
111 {
112     unsigned h = 0;
113     for(; *name; name++)
114         h = (h>>(sizeof(unsigned)*8-4)) ^ (h<<4) ^ tolowerW(*name);
115     return h;
116 }
117
118 static inline unsigned get_props_idx(jsdisp_t *This, unsigned hash)
119 {
120     return (hash*GOLDEN_RATIO) & (This->buf_size-1);
121 }
122
123 static inline HRESULT resize_props(jsdisp_t *This)
124 {
125     dispex_prop_t *props;
126     int i, bucket;
127
128     if(This->buf_size != This->prop_cnt)
129         return S_FALSE;
130
131     props = heap_realloc(This->props, sizeof(dispex_prop_t)*This->buf_size*2);
132     if(!props)
133         return E_OUTOFMEMORY;
134     This->buf_size *= 2;
135     This->props = props;
136
137     for(i=0; i<This->buf_size; i++) {
138         This->props[i].bucket_head = 0;
139         This->props[i].bucket_next = 0;
140     }
141
142     for(i=1; i<This->prop_cnt; i++) {
143         props = This->props+i;
144
145         bucket = get_props_idx(This, props->hash);
146         props->bucket_next = This->props[bucket].bucket_head;
147         This->props[bucket].bucket_head = i;
148     }
149
150     return S_OK;
151 }
152
153 static inline dispex_prop_t* alloc_prop(jsdisp_t *This, const WCHAR *name, prop_type_t type, DWORD flags)
154 {
155     dispex_prop_t *prop;
156     unsigned bucket;
157
158     if(FAILED(resize_props(This)))
159         return NULL;
160
161     prop = &This->props[This->prop_cnt];
162     prop->name = heap_strdupW(name);
163     if(!prop->name)
164         return NULL;
165     prop->type = type;
166     prop->flags = flags;
167     prop->hash = string_hash(name);
168
169     bucket = get_props_idx(This, prop->hash);
170     prop->bucket_next = This->props[bucket].bucket_head;
171     This->props[bucket].bucket_head = This->prop_cnt++;
172     return prop;
173 }
174
175 static dispex_prop_t *alloc_protref(jsdisp_t *This, const WCHAR *name, DWORD ref)
176 {
177     dispex_prop_t *ret;
178
179     ret = alloc_prop(This, name, PROP_PROTREF, 0);
180     if(!ret)
181         return NULL;
182
183     ret->u.ref = ref;
184     return ret;
185 }
186
187 static HRESULT find_prop_name(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
188 {
189     const builtin_prop_t *builtin;
190     unsigned bucket, pos, prev = 0;
191     dispex_prop_t *prop;
192
193     bucket = get_props_idx(This, hash);
194     pos = This->props[bucket].bucket_head;
195     while(pos != 0) {
196         if(!strcmpW(name, This->props[pos].name)) {
197             if(prev != 0) {
198                 This->props[prev].bucket_next = This->props[pos].bucket_next;
199                 This->props[pos].bucket_next = This->props[bucket].bucket_head;
200                 This->props[bucket].bucket_head = pos;
201             }
202
203             *ret = &This->props[pos];
204             return S_OK;
205         }
206
207         prev = pos;
208         pos = This->props[pos].bucket_next;
209     }
210
211     builtin = find_builtin_prop(This, name);
212     if(builtin) {
213         prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
214         if(!prop)
215             return E_OUTOFMEMORY;
216
217         prop->u.p = builtin;
218         *ret = prop;
219         return S_OK;
220     }
221
222     *ret = NULL;
223     return S_OK;
224 }
225
226 static HRESULT find_prop_name_prot(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
227 {
228     dispex_prop_t *prop, *del=NULL;
229     HRESULT hres;
230
231     hres = find_prop_name(This, hash, name, &prop);
232     if(FAILED(hres))
233         return hres;
234     if(prop && prop->type==PROP_DELETED) {
235         del = prop;
236     } else if(prop) {
237         *ret = prop;
238         return S_OK;
239     }
240
241     if(This->prototype) {
242         hres = find_prop_name_prot(This->prototype, hash, name, &prop);
243         if(FAILED(hres))
244             return hres;
245         if(prop) {
246             if(del) {
247                 del->type = PROP_PROTREF;
248                 del->flags = 0;
249                 del->u.ref = prop - This->prototype->props;
250                 prop = del;
251             }else {
252                 prop = alloc_protref(This, prop->name, prop - This->prototype->props);
253                 if(!prop)
254                     return E_OUTOFMEMORY;
255             }
256
257             *ret = prop;
258             return S_OK;
259         }
260     }
261
262     *ret = del;
263     return S_OK;
264 }
265
266 static HRESULT ensure_prop_name(jsdisp_t *This, const WCHAR *name, BOOL search_prot, DWORD create_flags, dispex_prop_t **ret)
267 {
268     dispex_prop_t *prop;
269     HRESULT hres;
270
271     if(search_prot)
272         hres = find_prop_name_prot(This, string_hash(name), name, &prop);
273     else
274         hres = find_prop_name(This, string_hash(name), name, &prop);
275     if(SUCCEEDED(hres) && (!prop || prop->type==PROP_DELETED)) {
276         TRACE("creating prop %s\n", debugstr_w(name));
277
278         if(prop) {
279             prop->type = PROP_VARIANT;
280             prop->flags = create_flags;
281         }else {
282             prop = alloc_prop(This, name, PROP_VARIANT, create_flags);
283             if(!prop)
284                 return E_OUTOFMEMORY;
285         }
286
287         VariantInit(&prop->u.var);
288     }
289
290     *ret = prop;
291     return hres;
292 }
293
294 static IDispatch *get_this(DISPPARAMS *dp)
295 {
296     DWORD i;
297
298     for(i=0; i < dp->cNamedArgs; i++) {
299         if(dp->rgdispidNamedArgs[i] == DISPID_THIS) {
300             if(V_VT(dp->rgvarg+i) == VT_DISPATCH)
301                 return V_DISPATCH(dp->rgvarg+i);
302
303             WARN("This is not VT_DISPATCH\n");
304             return NULL;
305         }
306     }
307
308     TRACE("no this passed\n");
309     return NULL;
310 }
311
312 static HRESULT convert_params(const DISPPARAMS *dp, VARIANT *buf, unsigned *argc, VARIANT **ret)
313 {
314     const VARIANT *s;
315     VARIANT *argv;
316     unsigned cnt;
317     unsigned i;
318
319     cnt = dp->cArgs - dp->cNamedArgs;
320
321     if(cnt > 6) {
322         argv = heap_alloc(cnt * sizeof(VARIANT));
323         if(!argv)
324             return E_OUTOFMEMORY;
325     }else {
326         argv = buf;
327     }
328
329     for(i = 0; i < cnt; i++) {
330         s = dp->rgvarg+dp->cArgs-i-1;
331         switch(V_VT(s)) {
332         case VT_I2:
333             V_VT(argv+i) = VT_I4;
334             V_I4(argv+i) = V_I2(s);
335             break;
336         case VT_INT:
337             V_VT(argv+i) = VT_I4;
338             V_I4(argv+i) = V_INT(s);
339             break;
340         default:
341             argv[i] = *s;
342         }
343     }
344
345     *argc = cnt;
346     *ret = argv;
347     return S_OK;
348 }
349
350 static HRESULT invoke_prop_func(jsdisp_t *This, IDispatch *jsthis, dispex_prop_t *prop, WORD flags,
351         unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
352 {
353     HRESULT hres;
354
355     switch(prop->type) {
356     case PROP_BUILTIN: {
357         if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
358             WARN("%s is not a constructor\n", debugstr_w(prop->name));
359             return E_INVALIDARG;
360         }
361
362         if(prop->name || This->builtin_info->class != JSCLASS_FUNCTION) {
363             vdisp_t vthis;
364
365             if(jsthis)
366                 set_disp(&vthis, jsthis);
367             else
368                 set_jsdisp(&vthis, This);
369             hres = prop->u.p->invoke(This->ctx, &vthis, flags, argc, argv, retv, ei);
370             vdisp_release(&vthis);
371         }else {
372             /* Function object calls are special case */
373             hres = Function_invoke(This, jsthis, flags, argc, argv, retv, ei);
374         }
375         return hres;
376     }
377     case PROP_PROTREF:
378         return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref,
379                 flags, argc, argv, retv, ei, caller);
380     case PROP_VARIANT: {
381         if(V_VT(&prop->u.var) != VT_DISPATCH) {
382             FIXME("invoke vt %d\n", V_VT(&prop->u.var));
383             return E_FAIL;
384         }
385
386         TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
387
388         return disp_call_value(This->ctx, V_DISPATCH(&prop->u.var), jsthis, flags, argc, argv, retv, ei);
389     }
390     default:
391         ERR("type %d\n", prop->type);
392     }
393
394     return E_FAIL;
395 }
396
397 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
398         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
399 {
400     HRESULT hres;
401
402     switch(prop->type) {
403     case PROP_BUILTIN:
404         if(prop->u.p->flags & PROPF_METHOD) {
405             jsdisp_t *obj;
406             hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
407                     prop->u.p->flags, NULL, &obj);
408             if(FAILED(hres))
409                 break;
410
411             prop->type = PROP_VARIANT;
412             var_set_jsdisp(&prop->u.var, obj);
413             hres = VariantCopy(retv, &prop->u.var);
414         }else {
415             vdisp_t vthis;
416
417             set_jsdisp(&vthis, This);
418             hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, 0, NULL, retv, ei);
419             vdisp_release(&vthis);
420         }
421         break;
422     case PROP_PROTREF:
423         hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
424         break;
425     case PROP_VARIANT:
426         hres = VariantCopy(retv, &prop->u.var);
427         break;
428     default:
429         ERR("type %d\n", prop->type);
430         return E_FAIL;
431     }
432
433     if(FAILED(hres)) {
434         TRACE("fail %08x\n", hres);
435         return hres;
436     }
437
438     TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
439     return hres;
440 }
441
442 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
443         jsexcept_t *ei, IServiceProvider *caller)
444 {
445     HRESULT hres;
446
447     if(prop->flags & PROPF_CONST)
448         return S_OK;
449
450     switch(prop->type) {
451     case PROP_BUILTIN:
452         if(!(prop->flags & PROPF_METHOD)) {
453             vdisp_t vthis;
454
455             set_jsdisp(&vthis, This);
456             hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, 1, val, NULL, ei);
457             vdisp_release(&vthis);
458             return hres;
459         }
460     case PROP_PROTREF:
461         prop->type = PROP_VARIANT;
462         prop->flags = PROPF_ENUM;
463         V_VT(&prop->u.var) = VT_EMPTY;
464         break;
465     case PROP_VARIANT:
466         VariantClear(&prop->u.var);
467         break;
468     default:
469         ERR("type %d\n", prop->type);
470         return E_FAIL;
471     }
472
473     hres = VariantCopy(&prop->u.var, val);
474     if(FAILED(hres))
475         return hres;
476
477     if(This->builtin_info->on_put)
478         This->builtin_info->on_put(This, prop->name);
479
480     TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
481     return S_OK;
482 }
483
484 static HRESULT fill_protrefs(jsdisp_t *This)
485 {
486     dispex_prop_t *iter, *prop;
487     HRESULT hres;
488
489     if(!This->prototype)
490         return S_OK;
491
492     fill_protrefs(This->prototype);
493
494     for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
495         if(!iter->name)
496             continue;
497         hres = find_prop_name(This, iter->hash, iter->name, &prop);
498         if(FAILED(hres))
499             return hres;
500         if(!prop || prop->type==PROP_DELETED) {
501             if(prop) {
502                 prop->type = PROP_PROTREF;
503                 prop->flags = 0;
504                 prop->u.ref = iter - This->prototype->props;
505             }else {
506                 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
507                 if(!prop)
508                     return E_OUTOFMEMORY;
509             }
510         }
511     }
512
513     return S_OK;
514 }
515
516 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
517 {
518     return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
519 }
520
521 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
522 {
523     jsdisp_t *This = impl_from_IDispatchEx(iface);
524
525     if(IsEqualGUID(&IID_IUnknown, riid)) {
526         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
527         *ppv = &This->IDispatchEx_iface;
528     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
529         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
530         *ppv = &This->IDispatchEx_iface;
531     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
532         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
533         *ppv = &This->IDispatchEx_iface;
534     }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
535         TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
536         jsdisp_addref(This);
537         *ppv = This;
538         return S_OK;
539     }else {
540         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
541         *ppv = NULL;
542         return E_NOINTERFACE;
543     }
544
545     IUnknown_AddRef((IUnknown*)*ppv);
546     return S_OK;
547 }
548
549 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
550 {
551     jsdisp_t *This = impl_from_IDispatchEx(iface);
552     LONG ref = InterlockedIncrement(&This->ref);
553
554     TRACE("(%p) ref=%d\n", This, ref);
555
556     return ref;
557 }
558
559 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
560 {
561     jsdisp_t *This = impl_from_IDispatchEx(iface);
562     LONG ref = InterlockedDecrement(&This->ref);
563
564     TRACE("(%p) ref=%d\n", This, ref);
565
566     if(!ref) {
567         dispex_prop_t *prop;
568
569         for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
570             if(prop->type == PROP_VARIANT)
571                 VariantClear(&prop->u.var);
572             heap_free(prop->name);
573         }
574         heap_free(This->props);
575         script_release(This->ctx);
576         if(This->prototype)
577             jsdisp_release(This->prototype);
578
579         if(This->builtin_info->destructor)
580             This->builtin_info->destructor(This);
581         else
582             heap_free(This);
583     }
584
585     return ref;
586 }
587
588 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
589 {
590     jsdisp_t *This = impl_from_IDispatchEx(iface);
591
592     TRACE("(%p)->(%p)\n", This, pctinfo);
593
594     *pctinfo = 1;
595     return S_OK;
596 }
597
598 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
599                                               ITypeInfo **ppTInfo)
600 {
601     jsdisp_t *This = impl_from_IDispatchEx(iface);
602     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
603     return E_NOTIMPL;
604 }
605
606 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
607                                                 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
608                                                 DISPID *rgDispId)
609 {
610     jsdisp_t *This = impl_from_IDispatchEx(iface);
611     UINT i;
612     HRESULT hres;
613
614     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
615           lcid, rgDispId);
616
617     for(i=0; i < cNames; i++) {
618         hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
619         if(FAILED(hres))
620             return hres;
621     }
622
623     return S_OK;
624 }
625
626 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
627                                         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
628                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
629 {
630     jsdisp_t *This = impl_from_IDispatchEx(iface);
631
632     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
633           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
634
635     return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
636             pDispParams, pVarResult, pExcepInfo, NULL);
637 }
638
639 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
640 {
641     jsdisp_t *This = impl_from_IDispatchEx(iface);
642
643     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
644
645     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
646         FIXME("Unsupported grfdex %x\n", grfdex);
647         return E_NOTIMPL;
648     }
649
650     return jsdisp_get_id(This, bstrName, grfdex, pid);
651 }
652
653 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
654         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
655 {
656     jsdisp_t *This = impl_from_IDispatchEx(iface);
657     dispex_prop_t *prop;
658     jsexcept_t jsexcept;
659     HRESULT hres;
660
661     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
662
663     if(pvarRes)
664         V_VT(pvarRes) = VT_EMPTY;
665
666     prop = get_prop(This, id);
667     if(!prop || prop->type == PROP_DELETED) {
668         TRACE("invalid id\n");
669         return DISP_E_MEMBERNOTFOUND;
670     }
671
672     memset(&jsexcept, 0, sizeof(jsexcept));
673
674     switch(wFlags) {
675     case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
676         wFlags = DISPATCH_METHOD;
677         /* fall through */
678     case DISPATCH_METHOD:
679     case DISPATCH_CONSTRUCT: {
680         VARIANT *argv;
681         unsigned argc;
682         VARIANT buf[6];
683
684         hres = convert_params(pdp, buf, &argc, &argv);
685         if(FAILED(hres))
686             return hres;
687
688         hres = invoke_prop_func(This, get_this(pdp), prop, wFlags, argc, argv, pvarRes, &jsexcept, pspCaller);
689         if(argv != buf)
690             heap_free(argv);
691         break;
692     }
693     case DISPATCH_PROPERTYGET:
694         hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
695         break;
696     case DISPATCH_PROPERTYPUT: {
697         DWORD i;
698
699         for(i=0; i < pdp->cNamedArgs; i++) {
700             if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
701                 break;
702         }
703
704         if(i == pdp->cNamedArgs) {
705             TRACE("no value to set\n");
706             return DISP_E_PARAMNOTOPTIONAL;
707         }
708
709         hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
710         break;
711     }
712     default:
713         FIXME("Unimplemented flags %x\n", wFlags);
714         return E_INVALIDARG;
715     }
716
717     if(pei)
718         *pei = jsexcept.ei;
719
720     return hres;
721 }
722
723 static HRESULT delete_prop(dispex_prop_t *prop)
724 {
725     if(prop->type == PROP_VARIANT) {
726         VariantClear(&prop->u.var);
727         prop->type = PROP_DELETED;
728     }
729     return S_OK;
730 }
731
732 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
733 {
734     jsdisp_t *This = impl_from_IDispatchEx(iface);
735     dispex_prop_t *prop;
736     HRESULT hres;
737
738     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
739
740     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
741         FIXME("Unsupported grfdex %x\n", grfdex);
742
743     hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
744     if(FAILED(hres))
745         return hres;
746     if(!prop) {
747         TRACE("not found\n");
748         return S_OK;
749     }
750
751     return delete_prop(prop);
752 }
753
754 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
755 {
756     jsdisp_t *This = impl_from_IDispatchEx(iface);
757     dispex_prop_t *prop;
758
759     TRACE("(%p)->(%x)\n", This, id);
760
761     prop = get_prop(This, id);
762     if(!prop) {
763         WARN("invalid id\n");
764         return DISP_E_MEMBERNOTFOUND;
765     }
766
767     return delete_prop(prop);
768 }
769
770 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
771 {
772     jsdisp_t *This = impl_from_IDispatchEx(iface);
773     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
774     return E_NOTIMPL;
775 }
776
777 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
778 {
779     jsdisp_t *This = impl_from_IDispatchEx(iface);
780     dispex_prop_t *prop;
781
782     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
783
784     prop = get_prop(This, id);
785     if(!prop || !prop->name || prop->type == PROP_DELETED)
786         return DISP_E_MEMBERNOTFOUND;
787
788     *pbstrName = SysAllocString(prop->name);
789     if(!*pbstrName)
790         return E_OUTOFMEMORY;
791
792     return S_OK;
793 }
794
795 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
796 {
797     jsdisp_t *This = impl_from_IDispatchEx(iface);
798     dispex_prop_t *iter;
799     HRESULT hres;
800
801     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
802
803     if(id == DISPID_STARTENUM) {
804         hres = fill_protrefs(This);
805         if(FAILED(hres))
806             return hres;
807     }
808
809     if(id+1>=0 && id+1<This->prop_cnt) {
810         iter = &This->props[id+1];
811     }else {
812         *pid = DISPID_STARTENUM;
813         return S_FALSE;
814     }
815
816     while(iter < This->props + This->prop_cnt) {
817         if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
818             *pid = prop_to_id(This, iter);
819             return S_OK;
820         }
821         iter++;
822     }
823
824     *pid = DISPID_STARTENUM;
825     return S_FALSE;
826 }
827
828 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
829 {
830     jsdisp_t *This = impl_from_IDispatchEx(iface);
831     FIXME("(%p)->(%p)\n", This, ppunk);
832     return E_NOTIMPL;
833 }
834
835 static IDispatchExVtbl DispatchExVtbl = {
836     DispatchEx_QueryInterface,
837     DispatchEx_AddRef,
838     DispatchEx_Release,
839     DispatchEx_GetTypeInfoCount,
840     DispatchEx_GetTypeInfo,
841     DispatchEx_GetIDsOfNames,
842     DispatchEx_Invoke,
843     DispatchEx_GetDispID,
844     DispatchEx_InvokeEx,
845     DispatchEx_DeleteMemberByName,
846     DispatchEx_DeleteMemberByDispID,
847     DispatchEx_GetMemberProperties,
848     DispatchEx_GetMemberName,
849     DispatchEx_GetNextDispID,
850     DispatchEx_GetNameSpaceParent
851 };
852
853 jsdisp_t *as_jsdisp(IDispatch *disp)
854 {
855     assert(disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl);
856     return impl_from_IDispatchEx((IDispatchEx*)disp);
857 }
858
859 jsdisp_t *to_jsdisp(IDispatch *disp)
860 {
861     return disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl ? impl_from_IDispatchEx((IDispatchEx*)disp) : NULL;
862 }
863
864 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
865 {
866     TRACE("%p (%p)\n", dispex, prototype);
867
868     dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
869     dispex->ref = 1;
870     dispex->builtin_info = builtin_info;
871
872     dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
873     if(!dispex->props)
874         return E_OUTOFMEMORY;
875
876     dispex->prototype = prototype;
877     if(prototype)
878         jsdisp_addref(prototype);
879
880     dispex->prop_cnt = 1;
881     if(builtin_info->value_prop.invoke) {
882         dispex->props[0].type = PROP_BUILTIN;
883         dispex->props[0].u.p = &builtin_info->value_prop;
884     }else {
885         dispex->props[0].type = PROP_DELETED;
886     }
887
888     script_addref(ctx);
889     dispex->ctx = ctx;
890
891     return S_OK;
892 }
893
894 static const builtin_info_t dispex_info = {
895     JSCLASS_NONE,
896     {NULL, NULL, 0},
897     0, NULL,
898     NULL,
899     NULL
900 };
901
902 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
903 {
904     jsdisp_t *ret;
905     HRESULT hres;
906
907     ret = heap_alloc_zero(sizeof(jsdisp_t));
908     if(!ret)
909         return E_OUTOFMEMORY;
910
911     hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
912     if(FAILED(hres))
913         return hres;
914
915     *dispex = ret;
916     return S_OK;
917 }
918
919 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
920 {
921     jsdisp_t *prot = NULL;
922     dispex_prop_t *prop;
923     HRESULT hres;
924
925     static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
926
927     hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
928     if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
929         jsexcept_t jsexcept;
930         VARIANT var;
931
932         V_VT(&var) = VT_EMPTY;
933         memset(&jsexcept, 0, sizeof(jsexcept));
934         hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
935         if(FAILED(hres)) {
936             ERR("Could not get prototype\n");
937             return hres;
938         }
939
940         if(V_VT(&var) == VT_DISPATCH)
941             prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
942         VariantClear(&var);
943     }
944
945     hres = init_dispex(dispex, ctx, builtin_info, prot);
946
947     if(prot)
948         jsdisp_release(prot);
949     return hres;
950 }
951
952 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
953 {
954     jsdisp_t *ret;
955     HRESULT hres;
956
957     hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
958     if(FAILED(hres))
959         return NULL;
960
961     return ret;
962 }
963
964 static void ensure_retval_type(VARIANT *v)
965 {
966     switch(V_VT(v)) {
967     case VT_I2:
968         V_VT(v) = VT_I4;
969         V_I4(v) = V_I2(v);
970         break;
971     case VT_INT:
972         V_VT(v) = VT_I4;
973         V_I4(v) = V_INT(v);
974     }
975 }
976
977 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
978 {
979     dispex_prop_t *prop;
980     HRESULT hres;
981
982     if(flags & fdexNameEnsure)
983         hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
984     else
985         hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
986     if(FAILED(hres))
987         return hres;
988
989     if(prop && prop->type!=PROP_DELETED) {
990         *id = prop_to_id(jsdisp, prop);
991         return S_OK;
992     }
993
994     TRACE("not found %s\n", debugstr_w(name));
995     return DISP_E_UNKNOWNNAME;
996 }
997
998 HRESULT jsdisp_call_value(jsdisp_t *jsfunc, IDispatch *jsthis, WORD flags, unsigned argc, VARIANT *argv, VARIANT *retv,
999         jsexcept_t *ei)
1000 {
1001     HRESULT hres;
1002
1003     if(is_class(jsfunc, JSCLASS_FUNCTION)) {
1004         hres = Function_invoke(jsfunc, jsthis, flags, argc, argv, retv, ei);
1005     }else {
1006         vdisp_t vdisp;
1007
1008         set_disp(&vdisp, jsthis);
1009         hres = jsfunc->builtin_info->value_prop.invoke(jsfunc->ctx, &vdisp, flags, argc, argv, retv, ei);
1010         vdisp_release(&vdisp);
1011     }
1012     return hres;
1013 }
1014
1015 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, unsigned argc, VARIANT *argv, VARIANT *retv, jsexcept_t *ei)
1016 {
1017     dispex_prop_t *prop;
1018
1019     memset(ei, 0, sizeof(*ei));
1020     if(retv)
1021         V_VT(retv) = VT_EMPTY;
1022
1023     prop = get_prop(disp, id);
1024     if(!prop)
1025         return DISP_E_MEMBERNOTFOUND;
1026
1027     return invoke_prop_func(disp, to_disp(disp), prop, flags, argc, argv, retv, ei, NULL);
1028 }
1029
1030 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, unsigned argc, VARIANT *argv, VARIANT *retv,
1031         jsexcept_t *ei)
1032 {
1033     dispex_prop_t *prop;
1034     HRESULT hres;
1035
1036     hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1037     if(FAILED(hres))
1038         return hres;
1039
1040     memset(ei, 0, sizeof(*ei));
1041     if(retv)
1042         V_VT(retv) = VT_EMPTY;
1043
1044     return invoke_prop_func(disp, to_disp(disp), prop, flags, argc, argv, retv, ei, NULL);
1045 }
1046
1047 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, unsigned argc, VARIANT *argv,
1048         VARIANT *retv, jsexcept_t *ei)
1049 {
1050     IDispatchEx *dispex;
1051     jsdisp_t *jsdisp;
1052     VARIANT buf[6];
1053     DISPPARAMS dp;
1054     unsigned i;
1055     HRESULT hres;
1056
1057     jsdisp = iface_to_jsdisp((IUnknown*)disp);
1058     if(jsdisp) {
1059         if(flags & DISPATCH_PROPERTYPUT) {
1060             FIXME("disp_call(propput) on builtin object\n");
1061             return E_FAIL;
1062         }
1063
1064         hres = jsdisp_call(jsdisp, id, flags, argc, argv, retv, ei);
1065         jsdisp_release(jsdisp);
1066         return hres;
1067     }
1068
1069     memset(ei, 0, sizeof(*ei));
1070     if(retv && argc)
1071         flags |= DISPATCH_PROPERTYGET;
1072
1073     dp.cArgs = argc;
1074
1075     if(flags & DISPATCH_PROPERTYPUT) {
1076         static DISPID propput_dispid = DISPID_PROPERTYPUT;
1077
1078         dp.cNamedArgs = 1;
1079         dp.rgdispidNamedArgs = &propput_dispid;
1080     }else {
1081         dp.cNamedArgs = 0;
1082         dp.rgdispidNamedArgs = NULL;
1083     }
1084
1085     if(argc > 6) {
1086         dp.rgvarg = heap_alloc(argc*sizeof(VARIANT));
1087         if(!dp.rgvarg)
1088             return E_OUTOFMEMORY;
1089     }else {
1090         dp.rgvarg = buf;
1091     }
1092
1093     for(i=0; i<argc; i++)
1094         dp.rgvarg[argc-i-1] = argv[i];
1095
1096     if(retv)
1097         V_VT(retv) = VT_EMPTY;
1098
1099     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1100     if(SUCCEEDED(hres)) {
1101         hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, &dp, retv, &ei->ei, &ctx->jscaller->IServiceProvider_iface);
1102         IDispatchEx_Release(dispex);
1103     }else {
1104         UINT err = 0;
1105
1106         if(flags == DISPATCH_CONSTRUCT) {
1107             WARN("IDispatch cannot be constructor\n");
1108             return DISP_E_MEMBERNOTFOUND;
1109         }
1110
1111         TRACE("using IDispatch\n");
1112         hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, &dp, retv, &ei->ei, &err);
1113     }
1114
1115     if(dp.rgvarg != buf)
1116         heap_free(dp.rgvarg);
1117     if(FAILED(hres))
1118         return hres;
1119
1120     if(retv)
1121         ensure_retval_type(retv);
1122     return S_OK;
1123 }
1124
1125 HRESULT disp_call_value(script_ctx_t *ctx, IDispatch *disp, IDispatch *jsthis, WORD flags, unsigned argc, VARIANT *argv,
1126         VARIANT *retv, jsexcept_t *ei)
1127 {
1128     jsdisp_t *jsdisp;
1129     IDispatchEx *dispex;
1130     VARIANT buf[6];
1131     DISPPARAMS dp;
1132     unsigned i;
1133     HRESULT hres;
1134
1135     jsdisp = iface_to_jsdisp((IUnknown*)disp);
1136     if(jsdisp) {
1137         if(flags & DISPATCH_PROPERTYPUT) {
1138             FIXME("disp_call(propput) on builtin object\n");
1139             return E_FAIL;
1140         }
1141
1142         hres = jsdisp_call_value(jsdisp, jsthis, flags, argc, argv, retv, ei);
1143         jsdisp_release(jsdisp);
1144         return hres;
1145     }
1146
1147     memset(ei, 0, sizeof(*ei));
1148     if(retv && argc)
1149         flags |= DISPATCH_PROPERTYGET;
1150
1151
1152     if(jsthis) {
1153         static DISPID this_id = DISPID_THIS;
1154
1155         dp.cArgs = argc+1;
1156         dp.cNamedArgs = 1;
1157         dp.rgdispidNamedArgs = &this_id;
1158     }else {
1159         dp.cArgs = argc;
1160         dp.cNamedArgs = 0;
1161         dp.rgdispidNamedArgs = NULL;
1162     }
1163
1164     if(dp.cArgs > sizeof(buf)/sizeof(*buf)) {
1165         dp.rgvarg = heap_alloc(dp.cArgs*sizeof(VARIANT));
1166         if(!dp.rgvarg)
1167             return E_OUTOFMEMORY;
1168     }else {
1169         dp.rgvarg = buf;
1170     }
1171
1172     for(i=0; i<argc; i++)
1173         dp.rgvarg[dp.cArgs-i-1] = argv[i];
1174     if(jsthis) {
1175         V_VT(dp.rgvarg) = VT_DISPATCH;
1176         V_DISPATCH(dp.rgvarg) = jsthis;
1177     }
1178
1179     if(retv)
1180         V_VT(retv) = VT_EMPTY;
1181     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1182     if(SUCCEEDED(hres)) {
1183         hres = IDispatchEx_InvokeEx(dispex, DISPID_VALUE, ctx->lcid, flags, &dp, retv, &ei->ei,
1184                 &ctx->jscaller->IServiceProvider_iface);
1185         IDispatchEx_Release(dispex);
1186     }else {
1187         UINT err = 0;
1188
1189         if(flags == DISPATCH_CONSTRUCT) {
1190             WARN("IDispatch cannot be constructor\n");
1191             return DISP_E_MEMBERNOTFOUND;
1192         }
1193
1194         TRACE("using IDispatch\n");
1195         hres = IDispatch_Invoke(disp, DISPID_VALUE, &IID_NULL, ctx->lcid, flags, &dp, retv, &ei->ei, &err);
1196     }
1197
1198     if(dp.rgvarg != buf)
1199         heap_free(dp.rgvarg);
1200     if(FAILED(hres))
1201         return hres;
1202
1203     if(retv)
1204         ensure_retval_type(retv);
1205     return S_OK;
1206 }
1207
1208 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei)
1209 {
1210     dispex_prop_t *prop;
1211     HRESULT hres;
1212
1213     hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1214     if(FAILED(hres))
1215         return hres;
1216
1217     return prop_put(obj, prop, val, ei, NULL);
1218 }
1219
1220 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1221 {
1222     dispex_prop_t *prop;
1223     HRESULT hres;
1224
1225     hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1226     if(FAILED(hres))
1227         return hres;
1228
1229     return VariantCopy(&prop->u.var, val);
1230 }
1231
1232 HRESULT jsdisp_propput_dontenum(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1233 {
1234     dispex_prop_t *prop;
1235     HRESULT hres;
1236
1237     hres = ensure_prop_name(obj, name, FALSE, 0, &prop);
1238     if(FAILED(hres))
1239         return hres;
1240
1241     return VariantCopy(&prop->u.var, val);
1242 }
1243
1244 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei)
1245 {
1246     WCHAR buf[12];
1247
1248     static const WCHAR formatW[] = {'%','d',0};
1249
1250     sprintfW(buf, formatW, idx);
1251     return jsdisp_propput_name(obj, buf, val, ei);
1252 }
1253
1254 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1255 {
1256     jsdisp_t *jsdisp;
1257     HRESULT hres;
1258
1259     jsdisp = iface_to_jsdisp((IUnknown*)disp);
1260     if(jsdisp) {
1261         dispex_prop_t *prop;
1262
1263         prop = get_prop(jsdisp, id);
1264         if(prop)
1265             hres = prop_put(jsdisp, prop, val, ei, NULL);
1266         else
1267             hres = DISP_E_MEMBERNOTFOUND;
1268
1269         jsdisp_release(jsdisp);
1270     }else {
1271         DISPID dispid = DISPID_PROPERTYPUT;
1272         DISPPARAMS dp  = {val, &dispid, 1, 1};
1273         IDispatchEx *dispex;
1274
1275         hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1276         if(SUCCEEDED(hres)) {
1277             hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei,
1278                     &ctx->jscaller->IServiceProvider_iface);
1279             IDispatchEx_Release(dispex);
1280         }else {
1281             ULONG err = 0;
1282
1283             TRACE("using IDispatch\n");
1284             hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1285         }
1286     }
1287
1288     return hres;
1289 }
1290
1291 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei)
1292 {
1293     DISPPARAMS dp = {NULL, NULL, 0, 0};
1294     dispex_prop_t *prop;
1295     HRESULT hres;
1296
1297     hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1298     if(FAILED(hres))
1299         return hres;
1300
1301     V_VT(var) = VT_EMPTY;
1302     if(!prop || prop->type==PROP_DELETED)
1303         return S_OK;
1304
1305     return prop_get(obj, prop, &dp, var, ei, NULL);
1306 }
1307
1308 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei)
1309 {
1310     WCHAR name[12];
1311     DISPPARAMS dp = {NULL, NULL, 0, 0};
1312     dispex_prop_t *prop;
1313     HRESULT hres;
1314
1315     static const WCHAR formatW[] = {'%','d',0};
1316
1317     sprintfW(name, formatW, idx);
1318
1319     hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1320     if(FAILED(hres))
1321         return hres;
1322
1323     V_VT(var) = VT_EMPTY;
1324     if(!prop || prop->type==PROP_DELETED)
1325         return DISP_E_UNKNOWNNAME;
1326
1327     return prop_get(obj, prop, &dp, var, ei, NULL);
1328 }
1329
1330 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei)
1331 {
1332     DISPPARAMS dp  = {NULL,NULL,0,0};
1333     dispex_prop_t *prop;
1334
1335     prop = get_prop(jsdisp, id);
1336     if(!prop)
1337         return DISP_E_MEMBERNOTFOUND;
1338
1339     V_VT(val) = VT_EMPTY;
1340     return prop_get(jsdisp, prop, &dp, val, ei, NULL);
1341 }
1342
1343 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1344 {
1345     DISPPARAMS dp  = {NULL,NULL,0,0};
1346     IDispatchEx *dispex;
1347     jsdisp_t *jsdisp;
1348     HRESULT hres;
1349
1350     jsdisp = iface_to_jsdisp((IUnknown*)disp);
1351     if(jsdisp) {
1352         hres = jsdisp_propget(jsdisp, id, val, ei);
1353         jsdisp_release(jsdisp);
1354         return hres;
1355     }
1356
1357     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1358     if(SUCCEEDED(hres)) {
1359         hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei,
1360                 &ctx->jscaller->IServiceProvider_iface);
1361         IDispatchEx_Release(dispex);
1362     }else {
1363         ULONG err = 0;
1364
1365         TRACE("using IDispatch\n");
1366         hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1367     }
1368     if(FAILED(hres))
1369         return hres;
1370
1371     ensure_retval_type(val);
1372     return S_OK;
1373 }
1374
1375 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1376 {
1377     static const WCHAR formatW[] = {'%','d',0};
1378     WCHAR buf[12];
1379     dispex_prop_t *prop;
1380     HRESULT hres;
1381
1382     sprintfW(buf, formatW, idx);
1383
1384     hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1385     if(FAILED(hres) || !prop)
1386         return hres;
1387
1388     return delete_prop(prop);
1389 }
1390
1391 HRESULT jsdisp_is_own_prop(jsdisp_t *obj, BSTR name, VARIANT_BOOL *ret)
1392 {
1393     dispex_prop_t *prop;
1394     HRESULT hres;
1395
1396     hres = find_prop_name(obj, string_hash(name), name, &prop);
1397     if(FAILED(hres))
1398         return hres;
1399
1400     *ret = prop && (prop->type == PROP_VARIANT || prop->type == PROP_BUILTIN) ? VARIANT_TRUE : VARIANT_FALSE;
1401     return S_OK;
1402 }