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