wineps: Fix a couple of typos in the path painting function.
[wine/testsucceed.git] / dlls / jscript / dispex.c
blobe40f3701d9f7162de17fb4b0b2fa9a54a8125fd2
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
19 #include "jscript.h"
21 #include "wine/unicode.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27 * This IID is used to get jsdisp_t objecto from interface.
28 * We might consider using private insteface instead.
30 static const IID IID_IDispatchJS =
31 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
33 #define FDEX_VERSION_MASK 0xf0000000
34 #define GOLDEN_RATIO 0x9E3779B9U
36 typedef enum {
37 PROP_VARIANT,
38 PROP_BUILTIN,
39 PROP_PROTREF,
40 PROP_DELETED
41 } prop_type_t;
43 struct _dispex_prop_t {
44 WCHAR *name;
45 unsigned hash;
46 prop_type_t type;
47 DWORD flags;
49 union {
50 VARIANT var;
51 const builtin_prop_t *p;
52 DWORD ref;
53 } u;
55 int bucket_head;
56 int bucket_next;
59 static inline DISPID prop_to_id(jsdisp_t *This, dispex_prop_t *prop)
61 return prop - This->props;
64 static inline dispex_prop_t *get_prop(jsdisp_t *This, DISPID id)
66 if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
67 return NULL;
69 return This->props+id;
72 static DWORD get_flags(jsdisp_t *This, dispex_prop_t *prop)
74 if(prop->type == PROP_PROTREF) {
75 dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
76 if(!parent) {
77 prop->type = PROP_DELETED;
78 return 0;
81 return get_flags(This->prototype, parent);
84 return prop->flags;
87 static const builtin_prop_t *find_builtin_prop(jsdisp_t *This, const WCHAR *name)
89 int min = 0, max, i, r;
91 max = This->builtin_info->props_cnt-1;
92 while(min <= max) {
93 i = (min+max)/2;
95 r = strcmpW(name, This->builtin_info->props[i].name);
96 if(!r)
97 return This->builtin_info->props + i;
99 if(r < 0)
100 max = i-1;
101 else
102 min = i+1;
105 return NULL;
108 static inline unsigned string_hash(const WCHAR *name)
110 unsigned h = 0;
111 for(; *name; name++)
112 h = (h>>(sizeof(unsigned)*8-4)) ^ (h<<4) ^ tolowerW(*name);
113 return h;
116 static inline unsigned get_props_idx(jsdisp_t *This, unsigned hash)
118 return (hash*GOLDEN_RATIO) & (This->buf_size-1);
121 static inline HRESULT resize_props(jsdisp_t *This)
123 dispex_prop_t *props;
124 int i, bucket;
126 if(This->buf_size != This->prop_cnt)
127 return S_FALSE;
129 props = heap_realloc(This->props, sizeof(dispex_prop_t)*This->buf_size*2);
130 if(!props)
131 return E_OUTOFMEMORY;
132 This->buf_size *= 2;
133 This->props = props;
135 for(i=0; i<This->buf_size; i++) {
136 This->props[i].bucket_head = 0;
137 This->props[i].bucket_next = 0;
140 for(i=1; i<This->prop_cnt; i++) {
141 props = This->props+i;
143 bucket = get_props_idx(This, props->hash);
144 props->bucket_next = This->props[bucket].bucket_head;
145 This->props[bucket].bucket_head = i;
148 return S_OK;
151 static inline dispex_prop_t* alloc_prop(jsdisp_t *This, const WCHAR *name, prop_type_t type, DWORD flags)
153 dispex_prop_t *prop;
154 unsigned bucket;
156 if(FAILED(resize_props(This)))
157 return NULL;
159 prop = &This->props[This->prop_cnt];
160 prop->name = heap_strdupW(name);
161 if(!prop->name)
162 return NULL;
163 prop->type = type;
164 prop->flags = flags;
165 prop->hash = string_hash(name);
167 bucket = get_props_idx(This, prop->hash);
168 prop->bucket_next = This->props[bucket].bucket_head;
169 This->props[bucket].bucket_head = This->prop_cnt++;
170 return prop;
173 static dispex_prop_t *alloc_protref(jsdisp_t *This, const WCHAR *name, DWORD ref)
175 dispex_prop_t *ret;
177 ret = alloc_prop(This, name, PROP_PROTREF, 0);
178 if(!ret)
179 return NULL;
181 ret->u.ref = ref;
182 return ret;
185 static HRESULT find_prop_name(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
187 const builtin_prop_t *builtin;
188 unsigned bucket, pos, prev = 0;
189 dispex_prop_t *prop;
191 bucket = get_props_idx(This, hash);
192 pos = This->props[bucket].bucket_head;
193 while(pos != 0) {
194 if(!strcmpW(name, This->props[pos].name)) {
195 if(prev != 0) {
196 This->props[prev].bucket_next = This->props[pos].bucket_next;
197 This->props[pos].bucket_next = This->props[bucket].bucket_head;
198 This->props[bucket].bucket_head = pos;
201 *ret = &This->props[pos];
202 return S_OK;
205 prev = pos;
206 pos = This->props[pos].bucket_next;
209 builtin = find_builtin_prop(This, name);
210 if(builtin) {
211 prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
212 if(!prop)
213 return E_OUTOFMEMORY;
215 prop->u.p = builtin;
216 *ret = prop;
217 return S_OK;
220 *ret = NULL;
221 return S_OK;
224 static HRESULT find_prop_name_prot(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
226 dispex_prop_t *prop, *del=NULL;
227 HRESULT hres;
229 hres = find_prop_name(This, hash, name, &prop);
230 if(FAILED(hres))
231 return hres;
232 if(prop && prop->type==PROP_DELETED) {
233 del = prop;
234 } else if(prop) {
235 *ret = prop;
236 return S_OK;
239 if(This->prototype) {
240 hres = find_prop_name_prot(This->prototype, hash, name, &prop);
241 if(FAILED(hres))
242 return hres;
243 if(prop) {
244 if(del) {
245 del->type = PROP_PROTREF;
246 del->flags = 0;
247 del->u.ref = prop - This->prototype->props;
248 prop = del;
249 }else {
250 prop = alloc_protref(This, prop->name, prop - This->prototype->props);
251 if(!prop)
252 return E_OUTOFMEMORY;
255 *ret = prop;
256 return S_OK;
260 *ret = del;
261 return S_OK;
264 static HRESULT ensure_prop_name(jsdisp_t *This, const WCHAR *name, BOOL search_prot, DWORD create_flags, dispex_prop_t **ret)
266 dispex_prop_t *prop;
267 HRESULT hres;
269 if(search_prot)
270 hres = find_prop_name_prot(This, string_hash(name), name, &prop);
271 else
272 hres = find_prop_name(This, string_hash(name), name, &prop);
273 if(SUCCEEDED(hres) && (!prop || prop->type==PROP_DELETED)) {
274 TRACE("creating prop %s\n", debugstr_w(name));
276 if(prop) {
277 prop->type = PROP_VARIANT;
278 prop->flags = create_flags;
279 }else {
280 prop = alloc_prop(This, name, PROP_VARIANT, create_flags);
281 if(!prop)
282 return E_OUTOFMEMORY;
285 VariantInit(&prop->u.var);
288 *ret = prop;
289 return hres;
292 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
294 VARIANTARG *oldargs;
295 int i;
297 static DISPID this_id = DISPID_THIS;
299 *dp = *olddp;
301 for(i = 0; i < dp->cNamedArgs; i++) {
302 if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
303 return S_OK;
306 oldargs = dp->rgvarg;
307 dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
308 if(!dp->rgvarg)
309 return E_OUTOFMEMORY;
310 memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
311 V_VT(dp->rgvarg) = VT_DISPATCH;
312 V_DISPATCH(dp->rgvarg) = jsthis;
313 dp->cArgs++;
315 if(dp->cNamedArgs) {
316 DISPID *old = dp->rgdispidNamedArgs;
317 dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
318 if(!dp->rgdispidNamedArgs) {
319 heap_free(dp->rgvarg);
320 return E_OUTOFMEMORY;
323 memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
324 dp->rgdispidNamedArgs[0] = DISPID_THIS;
325 dp->cNamedArgs++;
326 }else {
327 dp->rgdispidNamedArgs = &this_id;
328 dp->cNamedArgs = 1;
331 return S_OK;
334 static HRESULT invoke_prop_func(jsdisp_t *This, jsdisp_t *jsthis, dispex_prop_t *prop, WORD flags,
335 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
337 HRESULT hres;
339 switch(prop->type) {
340 case PROP_BUILTIN: {
341 vdisp_t vthis;
343 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
344 WARN("%s is not a constructor\n", debugstr_w(prop->name));
345 return E_INVALIDARG;
348 set_jsdisp(&vthis, jsthis);
349 hres = prop->u.p->invoke(This->ctx, &vthis, flags, dp, retv, ei, caller);
350 vdisp_release(&vthis);
351 return hres;
353 case PROP_PROTREF:
354 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
355 case PROP_VARIANT: {
356 DISPPARAMS new_dp;
358 if(V_VT(&prop->u.var) != VT_DISPATCH) {
359 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
360 return E_FAIL;
363 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
365 hres = set_this(&new_dp, dp, to_disp(jsthis));
366 if(FAILED(hres))
367 return hres;
369 hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei, caller);
371 if(new_dp.rgvarg != dp->rgvarg) {
372 heap_free(new_dp.rgvarg);
373 if(new_dp.cNamedArgs > 1)
374 heap_free(new_dp.rgdispidNamedArgs);
377 return hres;
379 default:
380 ERR("type %d\n", prop->type);
383 return E_FAIL;
386 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
387 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
389 HRESULT hres;
391 switch(prop->type) {
392 case PROP_BUILTIN:
393 if(prop->u.p->flags & PROPF_METHOD) {
394 jsdisp_t *obj;
395 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
396 prop->u.p->flags, NULL, &obj);
397 if(FAILED(hres))
398 break;
400 prop->type = PROP_VARIANT;
401 var_set_jsdisp(&prop->u.var, obj);
402 hres = VariantCopy(retv, &prop->u.var);
403 }else {
404 vdisp_t vthis;
406 set_jsdisp(&vthis, This);
407 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei, caller);
408 vdisp_release(&vthis);
410 break;
411 case PROP_PROTREF:
412 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
413 break;
414 case PROP_VARIANT:
415 hres = VariantCopy(retv, &prop->u.var);
416 break;
417 default:
418 ERR("type %d\n", prop->type);
419 return E_FAIL;
422 if(FAILED(hres)) {
423 TRACE("fail %08x\n", hres);
424 return hres;
427 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
428 return hres;
431 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
432 jsexcept_t *ei, IServiceProvider *caller)
434 HRESULT hres;
436 if(prop->flags & PROPF_CONST)
437 return S_OK;
439 switch(prop->type) {
440 case PROP_BUILTIN:
441 if(!(prop->flags & PROPF_METHOD)) {
442 DISPPARAMS dp = {val, NULL, 1, 0};
443 vdisp_t vthis;
445 set_jsdisp(&vthis, This);
446 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei, caller);
447 vdisp_release(&vthis);
448 return hres;
450 case PROP_PROTREF:
451 prop->type = PROP_VARIANT;
452 prop->flags = PROPF_ENUM;
453 V_VT(&prop->u.var) = VT_EMPTY;
454 break;
455 case PROP_VARIANT:
456 VariantClear(&prop->u.var);
457 break;
458 default:
459 ERR("type %d\n", prop->type);
460 return E_FAIL;
463 hres = VariantCopy(&prop->u.var, val);
464 if(FAILED(hres))
465 return hres;
467 if(This->builtin_info->on_put)
468 This->builtin_info->on_put(This, prop->name);
470 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
471 return S_OK;
474 static HRESULT fill_protrefs(jsdisp_t *This)
476 dispex_prop_t *iter, *prop;
477 HRESULT hres;
479 if(!This->prototype)
480 return S_OK;
482 fill_protrefs(This->prototype);
484 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
485 if(!iter->name)
486 continue;
487 hres = find_prop_name(This, iter->hash, iter->name, &prop);
488 if(FAILED(hres))
489 return hres;
490 if(!prop || prop->type==PROP_DELETED) {
491 if(prop) {
492 prop->type = PROP_PROTREF;
493 prop->flags = 0;
494 prop->u.ref = iter - This->prototype->props;
495 }else {
496 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
497 if(!prop)
498 return E_OUTOFMEMORY;
503 return S_OK;
506 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
508 return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
511 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
513 jsdisp_t *This = impl_from_IDispatchEx(iface);
515 if(IsEqualGUID(&IID_IUnknown, riid)) {
516 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
517 *ppv = &This->IDispatchEx_iface;
518 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
519 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
520 *ppv = &This->IDispatchEx_iface;
521 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
522 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
523 *ppv = &This->IDispatchEx_iface;
524 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
525 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
526 jsdisp_addref(This);
527 *ppv = This;
528 return S_OK;
529 }else {
530 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
531 *ppv = NULL;
532 return E_NOINTERFACE;
535 IUnknown_AddRef((IUnknown*)*ppv);
536 return S_OK;
539 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
541 jsdisp_t *This = impl_from_IDispatchEx(iface);
542 LONG ref = InterlockedIncrement(&This->ref);
544 TRACE("(%p) ref=%d\n", This, ref);
546 return ref;
549 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
551 jsdisp_t *This = impl_from_IDispatchEx(iface);
552 LONG ref = InterlockedDecrement(&This->ref);
554 TRACE("(%p) ref=%d\n", This, ref);
556 if(!ref) {
557 dispex_prop_t *prop;
559 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
560 if(prop->type == PROP_VARIANT)
561 VariantClear(&prop->u.var);
562 heap_free(prop->name);
564 heap_free(This->props);
565 script_release(This->ctx);
566 if(This->prototype)
567 jsdisp_release(This->prototype);
569 if(This->builtin_info->destructor)
570 This->builtin_info->destructor(This);
571 else
572 heap_free(This);
575 return ref;
578 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
580 jsdisp_t *This = impl_from_IDispatchEx(iface);
582 TRACE("(%p)->(%p)\n", This, pctinfo);
584 *pctinfo = 1;
585 return S_OK;
588 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
589 ITypeInfo **ppTInfo)
591 jsdisp_t *This = impl_from_IDispatchEx(iface);
592 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
593 return E_NOTIMPL;
596 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
597 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
598 DISPID *rgDispId)
600 jsdisp_t *This = impl_from_IDispatchEx(iface);
601 UINT i;
602 HRESULT hres;
604 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
605 lcid, rgDispId);
607 for(i=0; i < cNames; i++) {
608 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
609 if(FAILED(hres))
610 return hres;
613 return S_OK;
616 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
617 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
618 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
620 jsdisp_t *This = impl_from_IDispatchEx(iface);
622 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
623 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
625 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
626 pDispParams, pVarResult, pExcepInfo, NULL);
629 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
631 jsdisp_t *This = impl_from_IDispatchEx(iface);
633 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
635 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
636 FIXME("Unsupported grfdex %x\n", grfdex);
637 return E_NOTIMPL;
640 return jsdisp_get_id(This, bstrName, grfdex, pid);
643 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
644 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
646 jsdisp_t *This = impl_from_IDispatchEx(iface);
647 dispex_prop_t *prop;
648 jsexcept_t jsexcept;
649 HRESULT hres;
651 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
653 if(pvarRes)
654 V_VT(pvarRes) = VT_EMPTY;
656 prop = get_prop(This, id);
657 if(!prop || prop->type == PROP_DELETED) {
658 TRACE("invalid id\n");
659 return DISP_E_MEMBERNOTFOUND;
662 memset(&jsexcept, 0, sizeof(jsexcept));
664 switch(wFlags) {
665 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
666 wFlags = DISPATCH_METHOD;
667 /* fall through */
668 case DISPATCH_METHOD:
669 case DISPATCH_CONSTRUCT:
670 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
671 break;
672 case DISPATCH_PROPERTYGET:
673 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
674 break;
675 case DISPATCH_PROPERTYPUT: {
676 DWORD i;
678 for(i=0; i < pdp->cNamedArgs; i++) {
679 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
680 break;
683 if(i == pdp->cNamedArgs) {
684 TRACE("no value to set\n");
685 return DISP_E_PARAMNOTOPTIONAL;
688 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
689 break;
691 default:
692 FIXME("Unimplemented flags %x\n", wFlags);
693 return E_INVALIDARG;
696 if(pei)
697 *pei = jsexcept.ei;
699 return hres;
702 static HRESULT delete_prop(dispex_prop_t *prop)
704 if(prop->type == PROP_VARIANT) {
705 VariantClear(&prop->u.var);
706 prop->type = PROP_DELETED;
708 return S_OK;
711 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
713 jsdisp_t *This = impl_from_IDispatchEx(iface);
714 dispex_prop_t *prop;
715 HRESULT hres;
717 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
719 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
720 FIXME("Unsupported grfdex %x\n", grfdex);
722 hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
723 if(FAILED(hres))
724 return hres;
725 if(!prop) {
726 TRACE("not found\n");
727 return S_OK;
730 return delete_prop(prop);
733 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
735 jsdisp_t *This = impl_from_IDispatchEx(iface);
736 dispex_prop_t *prop;
738 TRACE("(%p)->(%x)\n", This, id);
740 prop = get_prop(This, id);
741 if(!prop) {
742 WARN("invalid id\n");
743 return DISP_E_MEMBERNOTFOUND;
746 return delete_prop(prop);
749 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
751 jsdisp_t *This = impl_from_IDispatchEx(iface);
752 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
753 return E_NOTIMPL;
756 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
758 jsdisp_t *This = impl_from_IDispatchEx(iface);
759 dispex_prop_t *prop;
761 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
763 prop = get_prop(This, id);
764 if(!prop || !prop->name || prop->type == PROP_DELETED)
765 return DISP_E_MEMBERNOTFOUND;
767 *pbstrName = SysAllocString(prop->name);
768 if(!*pbstrName)
769 return E_OUTOFMEMORY;
771 return S_OK;
774 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
776 jsdisp_t *This = impl_from_IDispatchEx(iface);
777 dispex_prop_t *iter;
778 HRESULT hres;
780 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
782 if(id == DISPID_STARTENUM) {
783 hres = fill_protrefs(This);
784 if(FAILED(hres))
785 return hres;
788 if(id+1>=0 && id+1<This->prop_cnt) {
789 iter = &This->props[id+1];
790 }else {
791 *pid = DISPID_STARTENUM;
792 return S_FALSE;
795 while(iter < This->props + This->prop_cnt) {
796 if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
797 *pid = prop_to_id(This, iter);
798 return S_OK;
800 iter++;
803 *pid = DISPID_STARTENUM;
804 return S_FALSE;
807 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
809 jsdisp_t *This = impl_from_IDispatchEx(iface);
810 FIXME("(%p)->(%p)\n", This, ppunk);
811 return E_NOTIMPL;
814 static IDispatchExVtbl DispatchExVtbl = {
815 DispatchEx_QueryInterface,
816 DispatchEx_AddRef,
817 DispatchEx_Release,
818 DispatchEx_GetTypeInfoCount,
819 DispatchEx_GetTypeInfo,
820 DispatchEx_GetIDsOfNames,
821 DispatchEx_Invoke,
822 DispatchEx_GetDispID,
823 DispatchEx_InvokeEx,
824 DispatchEx_DeleteMemberByName,
825 DispatchEx_DeleteMemberByDispID,
826 DispatchEx_GetMemberProperties,
827 DispatchEx_GetMemberName,
828 DispatchEx_GetNextDispID,
829 DispatchEx_GetNameSpaceParent
832 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
834 TRACE("%p (%p)\n", dispex, prototype);
836 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
837 dispex->ref = 1;
838 dispex->builtin_info = builtin_info;
840 dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
841 if(!dispex->props)
842 return E_OUTOFMEMORY;
844 dispex->prototype = prototype;
845 if(prototype)
846 jsdisp_addref(prototype);
848 dispex->prop_cnt = 1;
849 if(builtin_info->value_prop.invoke) {
850 dispex->props[0].type = PROP_BUILTIN;
851 dispex->props[0].u.p = &builtin_info->value_prop;
852 }else {
853 dispex->props[0].type = PROP_DELETED;
856 script_addref(ctx);
857 dispex->ctx = ctx;
859 return S_OK;
862 static const builtin_info_t dispex_info = {
863 JSCLASS_NONE,
864 {NULL, NULL, 0},
865 0, NULL,
866 NULL,
867 NULL
870 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
872 jsdisp_t *ret;
873 HRESULT hres;
875 ret = heap_alloc_zero(sizeof(jsdisp_t));
876 if(!ret)
877 return E_OUTOFMEMORY;
879 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
880 if(FAILED(hres))
881 return hres;
883 *dispex = ret;
884 return S_OK;
887 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
889 jsdisp_t *prot = NULL;
890 dispex_prop_t *prop;
891 HRESULT hres;
893 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
894 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
896 hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
897 if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
898 jsexcept_t jsexcept;
899 VARIANT var;
901 V_VT(&var) = VT_EMPTY;
902 memset(&jsexcept, 0, sizeof(jsexcept));
903 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
904 if(FAILED(hres)) {
905 ERR("Could not get prototype\n");
906 return hres;
909 if(V_VT(&var) == VT_DISPATCH)
910 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
911 VariantClear(&var);
914 hres = init_dispex(dispex, ctx, builtin_info, prot);
916 if(prot)
917 jsdisp_release(prot);
918 if(FAILED(hres))
919 return hres;
921 hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
922 if(SUCCEEDED(hres)) {
923 jsexcept_t jsexcept;
924 VARIANT var;
926 var_set_jsdisp(&var, constr);
927 memset(&jsexcept, 0, sizeof(jsexcept));
928 hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
930 if(FAILED(hres))
931 jsdisp_release(dispex);
933 return hres;
936 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
938 jsdisp_t *ret;
939 HRESULT hres;
941 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
942 if(FAILED(hres))
943 return NULL;
945 return ret;
948 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
950 dispex_prop_t *prop;
951 HRESULT hres;
953 if(flags & fdexNameEnsure)
954 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
955 else
956 hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
957 if(FAILED(hres))
958 return hres;
960 if(prop && prop->type!=PROP_DELETED) {
961 *id = prop_to_id(jsdisp, prop);
962 return S_OK;
965 TRACE("not found %s\n", debugstr_w(name));
966 return DISP_E_UNKNOWNNAME;
969 HRESULT jsdisp_call_value(jsdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv,
970 jsexcept_t *ei, IServiceProvider *caller)
972 vdisp_t vdisp;
973 HRESULT hres;
975 set_jsdisp(&vdisp, jsthis);
976 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei, caller);
977 vdisp_release(&vdisp);
978 return hres;
981 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
982 jsexcept_t *ei, IServiceProvider *caller)
984 dispex_prop_t *prop;
986 memset(ei, 0, sizeof(*ei));
987 if(retv)
988 V_VT(retv) = VT_EMPTY;
990 prop = get_prop(disp, id);
991 if(!prop)
992 return DISP_E_MEMBERNOTFOUND;
994 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
997 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
998 jsexcept_t *ei, IServiceProvider *caller)
1000 dispex_prop_t *prop;
1001 HRESULT hres;
1003 hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1004 if(FAILED(hres))
1005 return hres;
1007 memset(ei, 0, sizeof(*ei));
1008 if(retv)
1009 V_VT(retv) = VT_EMPTY;
1011 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, caller);
1014 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv,
1015 jsexcept_t *ei, IServiceProvider *caller)
1017 jsdisp_t *jsdisp;
1018 IDispatchEx *dispex;
1019 HRESULT hres;
1021 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1022 if(jsdisp) {
1023 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei, caller);
1024 jsdisp_release(jsdisp);
1025 return hres;
1028 memset(ei, 0, sizeof(*ei));
1029 if(retv && arg_cnt(dp))
1030 flags |= DISPATCH_PROPERTYGET;
1032 if(retv)
1033 V_VT(retv) = VT_EMPTY;
1034 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1035 if(FAILED(hres)) {
1036 UINT err = 0;
1038 if(flags == DISPATCH_CONSTRUCT) {
1039 WARN("IDispatch cannot be constructor\n");
1040 return DISP_E_MEMBERNOTFOUND;
1043 TRACE("using IDispatch\n");
1044 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
1047 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, caller);
1048 IDispatchEx_Release(dispex);
1050 return hres;
1053 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1055 dispex_prop_t *prop;
1056 HRESULT hres;
1058 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1059 if(FAILED(hres))
1060 return hres;
1062 return prop_put(obj, prop, val, ei, caller);
1065 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1067 dispex_prop_t *prop;
1068 HRESULT hres;
1070 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1071 if(FAILED(hres))
1072 return hres;
1074 return VariantCopy(&prop->u.var, val);
1077 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1079 WCHAR buf[12];
1081 static const WCHAR formatW[] = {'%','d',0};
1083 sprintfW(buf, formatW, idx);
1084 return jsdisp_propput_name(obj, buf, val, ei, caller);
1087 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1089 jsdisp_t *jsdisp;
1090 HRESULT hres;
1092 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1093 if(jsdisp) {
1094 dispex_prop_t *prop;
1096 prop = get_prop(jsdisp, id);
1097 if(prop)
1098 hres = prop_put(jsdisp, prop, val, ei, caller);
1099 else
1100 hres = DISP_E_MEMBERNOTFOUND;
1102 jsdisp_release(jsdisp);
1103 }else {
1104 DISPID dispid = DISPID_PROPERTYPUT;
1105 DISPPARAMS dp = {val, &dispid, 1, 1};
1106 IDispatchEx *dispex;
1108 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1109 if(SUCCEEDED(hres)) {
1110 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, caller);
1111 IDispatchEx_Release(dispex);
1112 }else {
1113 ULONG err = 0;
1115 TRACE("using IDispatch\n");
1116 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1120 return hres;
1123 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1125 DISPPARAMS dp = {NULL, NULL, 0, 0};
1126 dispex_prop_t *prop;
1127 HRESULT hres;
1129 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1130 if(FAILED(hres))
1131 return hres;
1133 V_VT(var) = VT_EMPTY;
1134 if(!prop || prop->type==PROP_DELETED)
1135 return S_OK;
1137 return prop_get(obj, prop, &dp, var, ei, caller);
1140 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei, IServiceProvider *caller)
1142 WCHAR name[12];
1143 DISPPARAMS dp = {NULL, NULL, 0, 0};
1144 dispex_prop_t *prop;
1145 HRESULT hres;
1147 static const WCHAR formatW[] = {'%','d',0};
1149 sprintfW(name, formatW, idx);
1151 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1152 if(FAILED(hres))
1153 return hres;
1155 V_VT(var) = VT_EMPTY;
1156 if(!prop || prop->type==PROP_DELETED)
1157 return DISP_E_UNKNOWNNAME;
1159 return prop_get(obj, prop, &dp, var, ei, caller);
1162 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1164 DISPPARAMS dp = {NULL,NULL,0,0};
1165 dispex_prop_t *prop;
1167 prop = get_prop(jsdisp, id);
1168 if(!prop)
1169 return DISP_E_MEMBERNOTFOUND;
1171 V_VT(val) = VT_EMPTY;
1172 return prop_get(jsdisp, prop, &dp, val, ei, caller);
1175 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei, IServiceProvider *caller)
1177 DISPPARAMS dp = {NULL,NULL,0,0};
1178 IDispatchEx *dispex;
1179 jsdisp_t *jsdisp;
1180 HRESULT hres;
1182 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1183 if(jsdisp) {
1184 hres = jsdisp_propget(jsdisp, id, val, ei, caller);
1185 jsdisp_release(jsdisp);
1186 return hres;
1189 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1190 if(FAILED(hres)) {
1191 ULONG err = 0;
1193 TRACE("using IDispatch\n");
1194 return IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1197 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, caller);
1198 IDispatchEx_Release(dispex);
1200 return hres;
1203 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1205 static const WCHAR formatW[] = {'%','d',0};
1206 WCHAR buf[12];
1207 dispex_prop_t *prop;
1208 HRESULT hres;
1210 sprintfW(buf, formatW, idx);
1212 hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1213 if(FAILED(hres) || !prop)
1214 return hres;
1216 return delete_prop(prop);
1219 VARIANT_BOOL jsdisp_is_own_prop(jsdisp_t *obj, BSTR name)
1221 dispex_prop_t *prop;
1222 HRESULT hres;
1224 hres = find_prop_name(obj, string_hash(name), name, &prop);
1225 if(FAILED(hres))
1226 return VARIANT_FALSE;
1227 else if(!prop)
1228 return VARIANT_FALSE;
1230 return prop->type==PROP_VARIANT ? VARIANT_TRUE : VARIANT_FALSE;