jscript: Added expression statement implementation.
[wine] / dlls / jscript / dispex.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "jscript.h"
20
21 #include "wine/unicode.h"
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25
26 /*
27  * This IID is used to get DispatchEx objecto from interface.
28  * We might consider using private insteface instead.
29  */
30 static const IID IID_IDispatchJS =
31         {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
32
33 typedef enum {
34     PROP_VARIANT,
35     PROP_BUILTIN,
36     PROP_PROTREF,
37     PROP_DELETED
38 } prop_type_t;
39
40 struct _dispex_prop_t {
41     WCHAR *name;
42     prop_type_t type;
43     DWORD flags;
44
45     union {
46         VARIANT var;
47         const builtin_prop_t *p;
48         DWORD ref;
49     } u;
50 };
51
52 static inline DISPID prop_to_id(DispatchEx *This, dispex_prop_t *prop)
53 {
54     return prop - This->props;
55 }
56
57 static inline dispex_prop_t *get_prop(DispatchEx *This, DISPID id)
58 {
59     if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
60         return NULL;
61
62     return This->props+id;
63 }
64
65 static DWORD get_flags(DispatchEx *This, dispex_prop_t *prop)
66 {
67     if(prop->type == PROP_PROTREF) {
68         dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
69         if(!parent) {
70             prop->type = PROP_DELETED;
71             return 0;
72         }
73
74         return get_flags(This->prototype, parent);
75     }
76
77     return prop->flags;
78 }
79
80 static const builtin_prop_t *find_builtin_prop(DispatchEx *This, const WCHAR *name)
81 {
82     int min = 0, max, i, r;
83
84     max = This->builtin_info->props_cnt-1;
85     while(min <= max) {
86         i = (min+max)/2;
87
88         r = strcmpW(name, This->builtin_info->props[i].name);
89         if(!r)
90             return This->builtin_info->props + i;
91
92         if(r < 0)
93             max = i-1;
94         else
95             min = i+1;
96     }
97
98     return NULL;
99 }
100
101 static dispex_prop_t *alloc_prop(DispatchEx *This, const WCHAR *name, prop_type_t type, DWORD flags)
102 {
103     dispex_prop_t *ret;
104
105     if(This->buf_size == This->prop_cnt) {
106         dispex_prop_t *tmp = heap_realloc(This->props, (This->buf_size<<=1)*sizeof(*This->props));
107         if(!tmp)
108             return NULL;
109         This->props = tmp;
110     }
111
112     ret = This->props + This->prop_cnt++;
113     ret->type = type;
114     ret->flags = flags;
115     ret->name = heap_strdupW(name);
116     if(!ret->name)
117         return NULL;
118
119     return ret;
120 }
121
122 static dispex_prop_t *alloc_protref(DispatchEx *This, const WCHAR *name, DWORD ref)
123 {
124     dispex_prop_t *ret;
125
126     ret = alloc_prop(This, name, PROP_PROTREF, 0);
127     if(!ret)
128         return NULL;
129
130     ret->u.ref = ref;
131     return ret;
132 }
133
134 static HRESULT find_prop_name(DispatchEx *This, const WCHAR *name, dispex_prop_t **ret)
135 {
136     const builtin_prop_t *builtin;
137     dispex_prop_t *prop;
138
139     for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
140         if(prop->name && !strcmpW(prop->name, name)) {
141             *ret = prop;
142             return S_OK;
143         }
144     }
145
146     builtin = find_builtin_prop(This, name);
147     if(builtin) {
148         prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
149         if(!prop)
150             return E_OUTOFMEMORY;
151
152         prop->u.p = builtin;
153         *ret = prop;
154         return S_OK;
155     }
156
157     *ret = NULL;
158     return S_OK;
159 }
160
161 static HRESULT find_prop_name_prot(DispatchEx *This, const WCHAR *name, BOOL alloc, dispex_prop_t **ret)
162 {
163     dispex_prop_t *prop;
164     HRESULT hres;
165
166     hres = find_prop_name(This, name, &prop);
167     if(FAILED(hres))
168         return hres;
169     if(prop) {
170         *ret = prop;
171         return S_OK;
172     }
173
174     if(This->prototype) {
175         hres = find_prop_name_prot(This->prototype, name, FALSE, &prop);
176         if(FAILED(hres))
177             return hres;
178         if(prop) {
179             prop = alloc_protref(This, prop->name, prop - This->prototype->props);
180             if(!prop)
181                 return E_OUTOFMEMORY;
182             *ret = prop;
183             return S_OK;
184         }
185     }
186
187     if(alloc) {
188         TRACE("creating prop %s\n", debugstr_w(name));
189
190         prop = alloc_prop(This, name, PROP_VARIANT, PROPF_ENUM);
191         if(!prop)
192             return E_OUTOFMEMORY;
193         VariantInit(&prop->u.var);
194     }
195
196     *ret = prop;
197     return S_OK;
198 }
199
200 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
201 {
202     VARIANTARG *oldargs;
203     int i;
204
205     static DISPID this_id = DISPID_THIS;
206
207     *dp = *olddp;
208
209     for(i = 0; i < dp->cNamedArgs; i++) {
210         if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
211             return S_OK;
212     }
213
214     oldargs = dp->rgvarg;
215     dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
216     if(!dp->rgvarg)
217         return E_OUTOFMEMORY;
218     memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
219     V_VT(dp->rgvarg) = VT_DISPATCH;
220     V_DISPATCH(dp->rgvarg) = jsthis;
221     dp->cArgs++;
222
223     if(dp->cNamedArgs) {
224         DISPID *old = dp->rgdispidNamedArgs;
225         dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
226         if(!dp->rgdispidNamedArgs) {
227             heap_free(dp->rgvarg);
228             return E_OUTOFMEMORY;
229         }
230
231         memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
232         dp->rgdispidNamedArgs[0] = DISPID_THIS;
233         dp->cNamedArgs++;
234     }else {
235         dp->rgdispidNamedArgs = &this_id;
236         dp->cNamedArgs = 1;
237     }
238
239     return S_OK;
240 }
241
242 static HRESULT invoke_prop_func(DispatchEx *This, DispatchEx *jsthis, dispex_prop_t *prop, LCID lcid, WORD flags,
243         DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
244 {
245     HRESULT hres;
246
247     switch(prop->type) {
248     case PROP_BUILTIN:
249         if(flags == DISPATCH_CONSTRUCT && (prop->flags & DISPATCH_METHOD)) {
250             WARN("%s is not a constructor\n", debugstr_w(prop->name));
251             return E_INVALIDARG;
252         }
253         return prop->u.p->invoke(jsthis, lcid, flags, dp, retv, ei, caller);
254     case PROP_PROTREF:
255         return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, lcid, flags, dp, retv, ei, caller);
256     case PROP_VARIANT: {
257         DISPPARAMS new_dp;
258
259         if(V_VT(&prop->u.var) != VT_DISPATCH) {
260             FIXME("invoke vt %d\n", V_VT(&prop->u.var));
261             return E_FAIL;
262         }
263
264         TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
265
266         hres = set_this(&new_dp, dp, (IDispatch*)_IDispatchEx_(jsthis));
267         if(FAILED(hres))
268             return hres;
269
270         hres = disp_call(V_DISPATCH(&prop->u.var), DISPID_VALUE, lcid, flags, &new_dp, retv, ei, caller);
271
272         if(new_dp.rgvarg != dp->rgvarg) {
273             heap_free(new_dp.rgvarg);
274             if(new_dp.cNamedArgs > 1)
275                 heap_free(new_dp.rgdispidNamedArgs);
276         }
277
278         return hres;
279     }
280     default:
281         ERR("type %d\n", prop->type);
282     }
283
284     return E_FAIL;
285 }
286
287 static HRESULT prop_get(DispatchEx *This, dispex_prop_t *prop, LCID lcid, DISPPARAMS *dp,
288         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
289 {
290     HRESULT hres;
291
292     switch(prop->type) {
293     case PROP_BUILTIN:
294         if(prop->u.p->flags & PROPF_METHOD) {
295             FIXME("function objects not supported\n");
296             return E_NOTIMPL;
297         }else {
298             hres = prop->u.p->invoke(This, lcid, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
299         }
300         break;
301     case PROP_PROTREF:
302         hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, lcid, dp, retv, ei, caller);
303         break;
304     case PROP_VARIANT:
305         hres = VariantCopy(retv, &prop->u.var);
306         break;
307     default:
308         ERR("type %d\n", prop->type);
309         return E_FAIL;
310     }
311
312     if(FAILED(hres)) {
313         TRACE("fail %08x\n", hres);
314         return hres;
315     }
316
317     TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
318     return hres;
319 }
320
321 static HRESULT prop_put(DispatchEx *This, dispex_prop_t *prop, LCID lcid, DISPPARAMS *dp,
322         jsexcept_t *ei, IServiceProvider *caller)
323 {
324     DWORD i;
325     HRESULT hres;
326
327     switch(prop->type) {
328     case PROP_BUILTIN:
329         if(!(prop->flags & PROPF_METHOD))
330             return prop->u.p->invoke(This, lcid, DISPATCH_PROPERTYPUT, dp, NULL, ei, caller);
331     case PROP_PROTREF:
332         prop->type = PROP_VARIANT;
333         prop->flags = PROPF_ENUM;
334         V_VT(&prop->u.var) = VT_EMPTY;
335         break;
336     case PROP_VARIANT:
337         VariantClear(&prop->u.var);
338         break;
339     default:
340         ERR("type %d\n", prop->type);
341         return E_FAIL;
342     }
343
344     for(i=0; i < dp->cNamedArgs; i++) {
345         if(dp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
346             break;
347     }
348
349     if(i == dp->cNamedArgs) {
350         TRACE("no value to set\n");
351         return DISP_E_PARAMNOTOPTIONAL;
352     }
353
354     hres = VariantCopy(&prop->u.var, dp->rgvarg+i);
355     if(FAILED(hres))
356         return hres;
357
358     if(This->builtin_info->on_put)
359         This->builtin_info->on_put(This, prop->name);
360
361     TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(dp->rgvarg+i));
362     return S_OK;
363 }
364
365 static HRESULT fill_protrefs(DispatchEx *This)
366 {
367     dispex_prop_t *iter, *prop;
368     HRESULT hres;
369
370     if(!This->prototype)
371         return S_OK;
372
373     fill_protrefs(This->prototype);
374
375     for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
376         hres = find_prop_name(This, iter->name, &prop);
377         if(FAILED(hres))
378             return hres;
379         if(!prop) {
380             prop = alloc_protref(This, iter->name, iter - This->prototype->props);
381             if(!prop)
382                 return E_OUTOFMEMORY;
383         }
384     }
385
386     return S_OK;
387 }
388
389 #define DISPATCHEX_THIS(iface) DEFINE_THIS(DispatchEx, IDispatchEx, iface)
390
391 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
392 {
393     DispatchEx *This = DISPATCHEX_THIS(iface);
394
395     if(IsEqualGUID(&IID_IUnknown, riid)) {
396         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
397         *ppv = _IDispatchEx_(This);
398     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
399         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
400         *ppv = _IDispatchEx_(This);
401     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
402         TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
403         *ppv = _IDispatchEx_(This);
404     }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
405         TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
406         IUnknown_AddRef(_IDispatchEx_(This));
407         *ppv = This;
408         return S_OK;
409     }else {
410         WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
411         *ppv = NULL;
412         return E_NOINTERFACE;
413     }
414
415     IUnknown_AddRef((IUnknown*)*ppv);
416     return S_OK;
417 }
418
419 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
420 {
421     DispatchEx *This = DISPATCHEX_THIS(iface);
422     LONG ref = InterlockedIncrement(&This->ref);
423
424     TRACE("(%p) ref=%d\n", This, ref);
425
426     return ref;
427 }
428
429 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
430 {
431     DispatchEx *This = DISPATCHEX_THIS(iface);
432     LONG ref = InterlockedDecrement(&This->ref);
433
434     TRACE("(%p) ref=%d\n", This, ref);
435
436     if(!ref) {
437         dispex_prop_t *prop;
438
439         for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
440             if(prop->type == PROP_VARIANT)
441                 VariantClear(&prop->u.var);
442             heap_free(prop->name);
443         }
444         heap_free(This->props);
445         script_release(This->ctx);
446
447         if(This->builtin_info->destructor)
448             This->builtin_info->destructor(This);
449         else
450             heap_free(This);
451     }
452
453     return ref;
454 }
455
456 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
457 {
458     DispatchEx *This = DISPATCHEX_THIS(iface);
459
460     TRACE("(%p)->(%p)\n", This, pctinfo);
461
462     *pctinfo = 1;
463     return S_OK;
464 }
465
466 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
467                                               LCID lcid, ITypeInfo **ppTInfo)
468 {
469     DispatchEx *This = DISPATCHEX_THIS(iface);
470     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
471     return E_NOTIMPL;
472 }
473
474 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
475                                                 LPOLESTR *rgszNames, UINT cNames,
476                                                 LCID lcid, DISPID *rgDispId)
477 {
478     DispatchEx *This = DISPATCHEX_THIS(iface);
479     UINT i;
480     HRESULT hres;
481
482     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
483           lcid, rgDispId);
484
485     for(i=0; i < cNames; i++) {
486         hres = IDispatchEx_GetDispID(_IDispatchEx_(This), rgszNames[i], 0, rgDispId+i);
487         if(FAILED(hres))
488             return hres;
489     }
490
491     return S_OK;
492 }
493
494 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
495                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
496                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
497 {
498     DispatchEx *This = DISPATCHEX_THIS(iface);
499
500     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
501           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
502
503     return IDispatchEx_InvokeEx(_IDispatchEx_(This), dispIdMember, lcid, wFlags,
504             pDispParams, pVarResult, pExcepInfo, NULL);
505 }
506
507 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
508 {
509     DispatchEx *This = DISPATCHEX_THIS(iface);
510     dispex_prop_t *prop;
511     HRESULT hres;
512
513     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
514
515     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit)) {
516         FIXME("Unsupported grfdex %x\n", grfdex);
517         return E_NOTIMPL;
518     }
519
520     hres = find_prop_name_prot(This, bstrName, (grfdex&fdexNameEnsure) != 0, &prop);
521     if(FAILED(hres))
522         return hres;
523     if(prop) {
524         *pid = prop_to_id(This, prop);
525         return S_OK;
526     }
527
528     TRACE("not found %s\n", debugstr_w(bstrName));
529     return DISP_E_UNKNOWNNAME;
530 }
531
532 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
533         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
534 {
535     DispatchEx *This = DISPATCHEX_THIS(iface);
536     dispex_prop_t *prop;
537     jsexcept_t jsexcept;
538     HRESULT hres;
539
540     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
541
542     if(pvarRes)
543         V_VT(pvarRes) = VT_EMPTY;
544
545     prop = get_prop(This, id);
546     if(!prop || prop->type == PROP_DELETED) {
547         TRACE("invalid id\n");
548         return DISP_E_MEMBERNOTFOUND;
549     }
550
551     memset(&jsexcept, 0, sizeof(jsexcept));
552
553     switch(wFlags) {
554     case DISPATCH_METHOD:
555     case DISPATCH_CONSTRUCT:
556         hres = invoke_prop_func(This, This, prop, lcid, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
557         break;
558     case DISPATCH_PROPERTYGET:
559         hres = prop_get(This, prop, lcid, pdp, pvarRes, &jsexcept, pspCaller);
560         break;
561     case DISPATCH_PROPERTYPUT:
562         hres = prop_put(This, prop, lcid, pdp, &jsexcept, pspCaller);
563         break;
564     default:
565         FIXME("Unimplemented flags %x\n", wFlags);
566         return E_INVALIDARG;
567     }
568
569     if(pei)
570         *pei = jsexcept.ei;
571
572     return hres;
573 }
574
575 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
576 {
577     DispatchEx *This = DISPATCHEX_THIS(iface);
578     dispex_prop_t *prop;
579     HRESULT hres;
580
581     TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
582
583     if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit))
584         FIXME("Unsupported grfdex %x\n", grfdex);
585
586     hres = find_prop_name(This, bstrName, &prop);
587     if(FAILED(hres))
588         return hres;
589     if(!prop) {
590         TRACE("not found\n");
591         return S_OK;
592     }
593
594     heap_free(prop->name);
595     prop->name = NULL;
596     prop->type = PROP_DELETED;
597     return S_OK;
598 }
599
600 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
601 {
602     DispatchEx *This = DISPATCHEX_THIS(iface);
603     FIXME("(%p)->(%x)\n", This, id);
604     return E_NOTIMPL;
605 }
606
607 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
608 {
609     DispatchEx *This = DISPATCHEX_THIS(iface);
610     FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
611     return E_NOTIMPL;
612 }
613
614 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
615 {
616     DispatchEx *This = DISPATCHEX_THIS(iface);
617     dispex_prop_t *prop;
618
619     TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
620
621     prop = get_prop(This, id);
622     if(!prop || !prop->name || prop->type == PROP_DELETED)
623         return DISP_E_MEMBERNOTFOUND;
624
625     *pbstrName = SysAllocString(prop->name);
626     if(!*pbstrName)
627         return E_OUTOFMEMORY;
628
629     return S_OK;
630 }
631
632 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
633 {
634     DispatchEx *This = DISPATCHEX_THIS(iface);
635     dispex_prop_t *iter;
636     HRESULT hres;
637
638     TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
639
640     if(id == DISPID_STARTENUM) {
641         hres = fill_protrefs(This);
642         if(FAILED(hres))
643             return hres;
644     }
645
646     iter = get_prop(This, id+1);
647     if(!iter) {
648         *pid = DISPID_STARTENUM;
649         return S_FALSE;
650     }
651
652     while(iter < This->props + This->prop_cnt) {
653         if(iter->name && (get_flags(This, iter) & PROPF_ENUM)) {
654             *pid = prop_to_id(This, iter);
655             return S_OK;
656         }
657         iter++;
658     }
659
660     *pid = DISPID_STARTENUM;
661     return S_FALSE;
662 }
663
664 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
665 {
666     DispatchEx *This = DISPATCHEX_THIS(iface);
667     FIXME("(%p)->(%p)\n", This, ppunk);
668     return E_NOTIMPL;
669 }
670
671 #undef DISPATCHEX_THIS
672
673 static IDispatchExVtbl DispatchExVtbl = {
674     DispatchEx_QueryInterface,
675     DispatchEx_AddRef,
676     DispatchEx_Release,
677     DispatchEx_GetTypeInfoCount,
678     DispatchEx_GetTypeInfo,
679     DispatchEx_GetIDsOfNames,
680     DispatchEx_Invoke,
681     DispatchEx_GetDispID,
682     DispatchEx_InvokeEx,
683     DispatchEx_DeleteMemberByName,
684     DispatchEx_DeleteMemberByDispID,
685     DispatchEx_GetMemberProperties,
686     DispatchEx_GetMemberName,
687     DispatchEx_GetNextDispID,
688     DispatchEx_GetNameSpaceParent
689 };
690
691 static HRESULT jsdisp_set_prot_prop(DispatchEx *dispex, DispatchEx *prototype)
692 {
693     VARIANT *var;
694
695     if(!dispex->props[1].name)
696         return E_OUTOFMEMORY;
697
698     dispex->props[1].type = PROP_VARIANT;
699     dispex->props[1].flags = 0;
700
701     var = &dispex->props[1].u.var;
702     V_VT(var) = VT_DISPATCH;
703     V_DISPATCH(var) = (IDispatch*)_IDispatchEx_(prototype);
704
705     return S_OK;
706 }
707
708 static HRESULT init_dispex(DispatchEx *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype)
709 {
710     static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
711
712     TRACE("%p (%p)\n", dispex, prototype);
713
714     dispex->lpIDispatchExVtbl = &DispatchExVtbl;
715     dispex->ref = 1;
716     dispex->builtin_info = builtin_info;
717
718     dispex->props = heap_alloc((dispex->buf_size=4) * sizeof(dispex_prop_t));
719     if(!dispex->props)
720         return E_OUTOFMEMORY;
721
722     dispex->prototype = prototype;
723     if(prototype)
724         IDispatchEx_AddRef(_IDispatchEx_(prototype));
725
726     dispex->prop_cnt = 2;
727     dispex->props[0].name = NULL;
728     dispex->props[0].flags = 0;
729     if(builtin_info->value_prop.invoke) {
730         dispex->props[0].type = PROP_BUILTIN;
731         dispex->props[0].u.p = &builtin_info->value_prop;
732     }else {
733         dispex->props[0].type = PROP_DELETED;
734     }
735
736     dispex->props[1].type = PROP_DELETED;
737     dispex->props[1].name = SysAllocString(prototypeW);
738     dispex->props[1].flags = 0;
739
740     if(prototype) {
741         HRESULT hres;
742
743         hres = jsdisp_set_prot_prop(dispex, prototype);
744         if(FAILED(hres)) {
745             IDispatchEx_Release(_IDispatchEx_(dispex));
746             return hres;
747         }
748     }
749
750     script_addref(ctx);
751     dispex->ctx = ctx;
752
753     return S_OK;
754 }
755
756 static const builtin_info_t dispex_info = {
757     JSCLASS_NONE,
758     {NULL, NULL, 0},
759     0, NULL,
760     NULL,
761     NULL
762 };
763
764 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, DispatchEx *prototype, DispatchEx **dispex)
765 {
766     DispatchEx *ret;
767     HRESULT hres;
768
769     ret = heap_alloc_zero(sizeof(DispatchEx));
770     if(!ret)
771         return E_OUTOFMEMORY;
772
773     hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
774     if(FAILED(hres))
775         return hres;
776
777     *dispex = ret;
778     return S_OK;
779 }
780
781 DispatchEx *iface_to_jsdisp(IUnknown *iface)
782 {
783     DispatchEx *ret;
784     HRESULT hres;
785
786     hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
787     if(FAILED(hres))
788         return NULL;
789
790     return ret;
791 }
792
793 HRESULT jsdisp_call(DispatchEx *disp, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv,
794         jsexcept_t *ei, IServiceProvider *caller)
795 {
796     dispex_prop_t *prop;
797
798     memset(ei, 0, sizeof(*ei));
799     if(retv)
800         V_VT(retv) = VT_EMPTY;
801
802     prop = get_prop(disp, id);
803     if(!prop)
804         return DISP_E_MEMBERNOTFOUND;
805
806     return invoke_prop_func(disp, disp, prop, lcid, flags, dp, retv, ei, caller);
807 }
808
809 HRESULT disp_call(IDispatch *disp, DISPID id, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv,
810         jsexcept_t *ei, IServiceProvider *caller)
811 {
812     DispatchEx *jsdisp;
813     IDispatchEx *dispex;
814     HRESULT hres;
815
816     jsdisp = iface_to_jsdisp((IUnknown*)disp);
817     if(jsdisp) {
818         hres = jsdisp_call(jsdisp, id, lcid, flags, dp, retv, ei, caller);
819         IDispatchEx_Release(_IDispatchEx_(jsdisp));
820         return hres;
821     }
822
823     memset(ei, 0, sizeof(*ei));
824
825     if(retv)
826         V_VT(retv) = VT_EMPTY;
827     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
828     if(FAILED(hres)) {
829         UINT err = 0;
830
831         TRACE("using IDispatch\n");
832         return IDispatch_Invoke(disp, id, &IID_NULL, lcid, flags, dp, retv, &ei->ei, &err);
833     }
834
835     hres = IDispatchEx_InvokeEx(dispex, id, lcid, flags, dp, retv, &ei->ei, caller);
836     IDispatchEx_Release(dispex);
837
838     return hres;
839 }
840
841 HRESULT disp_propget(IDispatch *disp, DISPID id, LCID lcid, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
842 {
843     DISPPARAMS dp  = {NULL,NULL,0,0};
844     IDispatchEx *dispex;
845     DispatchEx *jsdisp;
846     HRESULT hres;
847
848     jsdisp = iface_to_jsdisp((IUnknown*)disp);
849     if(jsdisp) {
850         dispex_prop_t *prop;
851
852         prop = get_prop(jsdisp, id);
853         if(prop)
854             hres = prop_get(jsdisp, prop, lcid, &dp, val, ei, caller);
855         else
856             hres = DISP_E_MEMBERNOTFOUND;
857
858         IDispatchEx_Release(_IDispatchEx_(jsdisp));
859         return hres;
860     }
861
862     hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
863     if(FAILED(hres)) {
864         ULONG err = 0;
865
866         TRACE("uding IDispatch\n");
867         return IDispatchEx_Invoke(dispex, id, &IID_NULL, lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
868     }
869
870     hres = IDispatchEx_InvokeEx(dispex, id, lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
871     IDispatchEx_Release(dispex);
872
873     return hres;
874 }