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