wined3d: Figure out the index size inside drawPrimitive().
[wine/multimedia.git] / dlls / jscript / dispex.c
blob96c17c85f3bab46551fde56383216d66e7e70b09
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 <assert.h>
21 #include "jscript.h"
23 #include "wine/unicode.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29 * This IID is used to get jsdisp_t objecto from interface.
30 * We might consider using private interface instead.
32 static const IID IID_IDispatchJS =
33 {0x719c3050,0xf9d3,0x11cf,{0xa4,0x93,0x00,0x40,0x05,0x23,0xa8,0xa6}};
35 #define FDEX_VERSION_MASK 0xf0000000
36 #define GOLDEN_RATIO 0x9E3779B9U
38 typedef enum {
39 PROP_VARIANT,
40 PROP_BUILTIN,
41 PROP_PROTREF,
42 PROP_DELETED
43 } prop_type_t;
45 struct _dispex_prop_t {
46 WCHAR *name;
47 unsigned hash;
48 prop_type_t type;
49 DWORD flags;
51 union {
52 VARIANT var;
53 const builtin_prop_t *p;
54 DWORD ref;
55 } u;
57 int bucket_head;
58 int bucket_next;
61 static inline DISPID prop_to_id(jsdisp_t *This, dispex_prop_t *prop)
63 return prop - This->props;
66 static inline dispex_prop_t *get_prop(jsdisp_t *This, DISPID id)
68 if(id < 0 || id >= This->prop_cnt || This->props[id].type == PROP_DELETED)
69 return NULL;
71 return This->props+id;
74 static DWORD get_flags(jsdisp_t *This, dispex_prop_t *prop)
76 if(prop->type == PROP_PROTREF) {
77 dispex_prop_t *parent = get_prop(This->prototype, prop->u.ref);
78 if(!parent) {
79 prop->type = PROP_DELETED;
80 return 0;
83 return get_flags(This->prototype, parent);
86 return prop->flags;
89 static const builtin_prop_t *find_builtin_prop(jsdisp_t *This, const WCHAR *name)
91 int min = 0, max, i, r;
93 max = This->builtin_info->props_cnt-1;
94 while(min <= max) {
95 i = (min+max)/2;
97 r = strcmpW(name, This->builtin_info->props[i].name);
98 if(!r)
99 return This->builtin_info->props + i;
101 if(r < 0)
102 max = i-1;
103 else
104 min = i+1;
107 return NULL;
110 static inline unsigned string_hash(const WCHAR *name)
112 unsigned h = 0;
113 for(; *name; name++)
114 h = (h>>(sizeof(unsigned)*8-4)) ^ (h<<4) ^ tolowerW(*name);
115 return h;
118 static inline unsigned get_props_idx(jsdisp_t *This, unsigned hash)
120 return (hash*GOLDEN_RATIO) & (This->buf_size-1);
123 static inline HRESULT resize_props(jsdisp_t *This)
125 dispex_prop_t *props;
126 int i, bucket;
128 if(This->buf_size != This->prop_cnt)
129 return S_FALSE;
131 props = heap_realloc(This->props, sizeof(dispex_prop_t)*This->buf_size*2);
132 if(!props)
133 return E_OUTOFMEMORY;
134 This->buf_size *= 2;
135 This->props = props;
137 for(i=0; i<This->buf_size; i++) {
138 This->props[i].bucket_head = 0;
139 This->props[i].bucket_next = 0;
142 for(i=1; i<This->prop_cnt; i++) {
143 props = This->props+i;
145 bucket = get_props_idx(This, props->hash);
146 props->bucket_next = This->props[bucket].bucket_head;
147 This->props[bucket].bucket_head = i;
150 return S_OK;
153 static inline dispex_prop_t* alloc_prop(jsdisp_t *This, const WCHAR *name, prop_type_t type, DWORD flags)
155 dispex_prop_t *prop;
156 unsigned bucket;
158 if(FAILED(resize_props(This)))
159 return NULL;
161 prop = &This->props[This->prop_cnt];
162 prop->name = heap_strdupW(name);
163 if(!prop->name)
164 return NULL;
165 prop->type = type;
166 prop->flags = flags;
167 prop->hash = string_hash(name);
169 bucket = get_props_idx(This, prop->hash);
170 prop->bucket_next = This->props[bucket].bucket_head;
171 This->props[bucket].bucket_head = This->prop_cnt++;
172 return prop;
175 static dispex_prop_t *alloc_protref(jsdisp_t *This, const WCHAR *name, DWORD ref)
177 dispex_prop_t *ret;
179 ret = alloc_prop(This, name, PROP_PROTREF, 0);
180 if(!ret)
181 return NULL;
183 ret->u.ref = ref;
184 return ret;
187 static HRESULT find_prop_name(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
189 const builtin_prop_t *builtin;
190 unsigned bucket, pos, prev = 0;
191 dispex_prop_t *prop;
193 bucket = get_props_idx(This, hash);
194 pos = This->props[bucket].bucket_head;
195 while(pos != 0) {
196 if(!strcmpW(name, This->props[pos].name)) {
197 if(prev != 0) {
198 This->props[prev].bucket_next = This->props[pos].bucket_next;
199 This->props[pos].bucket_next = This->props[bucket].bucket_head;
200 This->props[bucket].bucket_head = pos;
203 *ret = &This->props[pos];
204 return S_OK;
207 prev = pos;
208 pos = This->props[pos].bucket_next;
211 builtin = find_builtin_prop(This, name);
212 if(builtin) {
213 prop = alloc_prop(This, name, PROP_BUILTIN, builtin->flags);
214 if(!prop)
215 return E_OUTOFMEMORY;
217 prop->u.p = builtin;
218 *ret = prop;
219 return S_OK;
222 *ret = NULL;
223 return S_OK;
226 static HRESULT find_prop_name_prot(jsdisp_t *This, unsigned hash, const WCHAR *name, dispex_prop_t **ret)
228 dispex_prop_t *prop, *del=NULL;
229 HRESULT hres;
231 hres = find_prop_name(This, hash, name, &prop);
232 if(FAILED(hres))
233 return hres;
234 if(prop && prop->type==PROP_DELETED) {
235 del = prop;
236 } else if(prop) {
237 *ret = prop;
238 return S_OK;
241 if(This->prototype) {
242 hres = find_prop_name_prot(This->prototype, hash, name, &prop);
243 if(FAILED(hres))
244 return hres;
245 if(prop) {
246 if(del) {
247 del->type = PROP_PROTREF;
248 del->flags = 0;
249 del->u.ref = prop - This->prototype->props;
250 prop = del;
251 }else {
252 prop = alloc_protref(This, prop->name, prop - This->prototype->props);
253 if(!prop)
254 return E_OUTOFMEMORY;
257 *ret = prop;
258 return S_OK;
262 *ret = del;
263 return S_OK;
266 static HRESULT ensure_prop_name(jsdisp_t *This, const WCHAR *name, BOOL search_prot, DWORD create_flags, dispex_prop_t **ret)
268 dispex_prop_t *prop;
269 HRESULT hres;
271 if(search_prot)
272 hres = find_prop_name_prot(This, string_hash(name), name, &prop);
273 else
274 hres = find_prop_name(This, string_hash(name), name, &prop);
275 if(SUCCEEDED(hres) && (!prop || prop->type==PROP_DELETED)) {
276 TRACE("creating prop %s\n", debugstr_w(name));
278 if(prop) {
279 prop->type = PROP_VARIANT;
280 prop->flags = create_flags;
281 }else {
282 prop = alloc_prop(This, name, PROP_VARIANT, create_flags);
283 if(!prop)
284 return E_OUTOFMEMORY;
287 VariantInit(&prop->u.var);
290 *ret = prop;
291 return hres;
294 static HRESULT set_this(DISPPARAMS *dp, DISPPARAMS *olddp, IDispatch *jsthis)
296 VARIANTARG *oldargs;
297 int i;
299 static DISPID this_id = DISPID_THIS;
301 *dp = *olddp;
303 for(i = 0; i < dp->cNamedArgs; i++) {
304 if(dp->rgdispidNamedArgs[i] == DISPID_THIS)
305 return S_OK;
308 oldargs = dp->rgvarg;
309 dp->rgvarg = heap_alloc((dp->cArgs+1) * sizeof(VARIANTARG));
310 if(!dp->rgvarg)
311 return E_OUTOFMEMORY;
312 memcpy(dp->rgvarg+1, oldargs, dp->cArgs*sizeof(VARIANTARG));
313 V_VT(dp->rgvarg) = VT_DISPATCH;
314 V_DISPATCH(dp->rgvarg) = jsthis;
315 dp->cArgs++;
317 if(dp->cNamedArgs) {
318 DISPID *old = dp->rgdispidNamedArgs;
319 dp->rgdispidNamedArgs = heap_alloc((dp->cNamedArgs+1)*sizeof(DISPID));
320 if(!dp->rgdispidNamedArgs) {
321 heap_free(dp->rgvarg);
322 return E_OUTOFMEMORY;
325 memcpy(dp->rgdispidNamedArgs+1, old, dp->cNamedArgs*sizeof(DISPID));
326 dp->rgdispidNamedArgs[0] = DISPID_THIS;
327 dp->cNamedArgs++;
328 }else {
329 dp->rgdispidNamedArgs = &this_id;
330 dp->cNamedArgs = 1;
333 return S_OK;
336 static HRESULT convert_params(const DISPPARAMS *dp, VARIANT *buf, DISPPARAMS *ret)
338 BOOL need_conversion = FALSE;
339 const VARIANT *s;
340 VARIANT *d;
341 unsigned i;
343 *ret = *dp;
345 for(i = 0; i < ret->cArgs; i++) {
346 if(V_VT(get_arg(dp, i)) == VT_I2) {
347 need_conversion = TRUE;
348 break;
352 if(!need_conversion)
353 return S_OK;
355 if(ret->cArgs > 6) {
356 ret->rgvarg = heap_alloc(ret->cArgs * sizeof(VARIANT));
357 if(!ret->rgvarg)
358 return E_OUTOFMEMORY;
359 }else {
360 ret->rgvarg = buf;
363 for(i = 0; i < ret->cArgs; i++) {
364 s = get_arg(dp, i);
365 d = get_arg(ret, i);
366 switch(V_VT(s)) {
367 case VT_I2:
368 V_VT(d) = VT_I4;
369 V_I4(d) = V_I2(s);
370 break;
371 default:
372 *d = *s;
376 return S_OK;
379 static HRESULT invoke_prop_func(jsdisp_t *This, jsdisp_t *jsthis, dispex_prop_t *prop, WORD flags,
380 DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
382 HRESULT hres;
384 switch(prop->type) {
385 case PROP_BUILTIN: {
386 DISPPARAMS params;
387 VARIANT buf[6];
388 vdisp_t vthis;
390 if(flags == DISPATCH_CONSTRUCT && (prop->flags & PROPF_METHOD)) {
391 WARN("%s is not a constructor\n", debugstr_w(prop->name));
392 return E_INVALIDARG;
395 hres = convert_params(dp, buf, &params);
396 if(FAILED(hres))
397 return hres;
399 set_jsdisp(&vthis, jsthis);
400 hres = prop->u.p->invoke(This->ctx, &vthis, flags, &params, retv, ei);
401 vdisp_release(&vthis);
402 if(params.rgvarg != buf && params.rgvarg != dp->rgvarg)
403 heap_free(params.rgvarg);
404 return hres;
406 case PROP_PROTREF:
407 return invoke_prop_func(This->prototype, jsthis, This->prototype->props+prop->u.ref, flags, dp, retv, ei, caller);
408 case PROP_VARIANT: {
409 DISPPARAMS new_dp;
411 if(V_VT(&prop->u.var) != VT_DISPATCH) {
412 FIXME("invoke vt %d\n", V_VT(&prop->u.var));
413 return E_FAIL;
416 TRACE("call %s %p\n", debugstr_w(prop->name), V_DISPATCH(&prop->u.var));
418 hres = set_this(&new_dp, dp, to_disp(jsthis));
419 if(FAILED(hres))
420 return hres;
422 hres = disp_call(This->ctx, V_DISPATCH(&prop->u.var), DISPID_VALUE, flags, &new_dp, retv, ei);
424 if(new_dp.rgvarg != dp->rgvarg) {
425 heap_free(new_dp.rgvarg);
426 if(new_dp.cNamedArgs > 1)
427 heap_free(new_dp.rgdispidNamedArgs);
430 return hres;
432 default:
433 ERR("type %d\n", prop->type);
436 return E_FAIL;
439 static HRESULT prop_get(jsdisp_t *This, dispex_prop_t *prop, DISPPARAMS *dp,
440 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
442 HRESULT hres;
444 switch(prop->type) {
445 case PROP_BUILTIN:
446 if(prop->u.p->flags & PROPF_METHOD) {
447 jsdisp_t *obj;
448 hres = create_builtin_function(This->ctx, prop->u.p->invoke, prop->u.p->name, NULL,
449 prop->u.p->flags, NULL, &obj);
450 if(FAILED(hres))
451 break;
453 prop->type = PROP_VARIANT;
454 var_set_jsdisp(&prop->u.var, obj);
455 hres = VariantCopy(retv, &prop->u.var);
456 }else {
457 vdisp_t vthis;
459 set_jsdisp(&vthis, This);
460 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYGET, dp, retv, ei);
461 vdisp_release(&vthis);
463 break;
464 case PROP_PROTREF:
465 hres = prop_get(This->prototype, This->prototype->props+prop->u.ref, dp, retv, ei, caller);
466 break;
467 case PROP_VARIANT:
468 hres = VariantCopy(retv, &prop->u.var);
469 break;
470 default:
471 ERR("type %d\n", prop->type);
472 return E_FAIL;
475 if(FAILED(hres)) {
476 TRACE("fail %08x\n", hres);
477 return hres;
480 TRACE("%s ret %s\n", debugstr_w(prop->name), debugstr_variant(retv));
481 return hres;
484 static HRESULT prop_put(jsdisp_t *This, dispex_prop_t *prop, VARIANT *val,
485 jsexcept_t *ei, IServiceProvider *caller)
487 HRESULT hres;
489 if(prop->flags & PROPF_CONST)
490 return S_OK;
492 switch(prop->type) {
493 case PROP_BUILTIN:
494 if(!(prop->flags & PROPF_METHOD)) {
495 DISPPARAMS dp = {val, NULL, 1, 0};
496 vdisp_t vthis;
498 set_jsdisp(&vthis, This);
499 hres = prop->u.p->invoke(This->ctx, &vthis, DISPATCH_PROPERTYPUT, &dp, NULL, ei);
500 vdisp_release(&vthis);
501 return hres;
503 case PROP_PROTREF:
504 prop->type = PROP_VARIANT;
505 prop->flags = PROPF_ENUM;
506 V_VT(&prop->u.var) = VT_EMPTY;
507 break;
508 case PROP_VARIANT:
509 VariantClear(&prop->u.var);
510 break;
511 default:
512 ERR("type %d\n", prop->type);
513 return E_FAIL;
516 hres = VariantCopy(&prop->u.var, val);
517 if(FAILED(hres))
518 return hres;
520 if(This->builtin_info->on_put)
521 This->builtin_info->on_put(This, prop->name);
523 TRACE("%s = %s\n", debugstr_w(prop->name), debugstr_variant(val));
524 return S_OK;
527 static HRESULT fill_protrefs(jsdisp_t *This)
529 dispex_prop_t *iter, *prop;
530 HRESULT hres;
532 if(!This->prototype)
533 return S_OK;
535 fill_protrefs(This->prototype);
537 for(iter = This->prototype->props; iter < This->prototype->props+This->prototype->prop_cnt; iter++) {
538 if(!iter->name)
539 continue;
540 hres = find_prop_name(This, iter->hash, iter->name, &prop);
541 if(FAILED(hres))
542 return hres;
543 if(!prop || prop->type==PROP_DELETED) {
544 if(prop) {
545 prop->type = PROP_PROTREF;
546 prop->flags = 0;
547 prop->u.ref = iter - This->prototype->props;
548 }else {
549 prop = alloc_protref(This, iter->name, iter - This->prototype->props);
550 if(!prop)
551 return E_OUTOFMEMORY;
556 return S_OK;
559 static inline jsdisp_t *impl_from_IDispatchEx(IDispatchEx *iface)
561 return CONTAINING_RECORD(iface, jsdisp_t, IDispatchEx_iface);
564 static HRESULT WINAPI DispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
566 jsdisp_t *This = impl_from_IDispatchEx(iface);
568 if(IsEqualGUID(&IID_IUnknown, riid)) {
569 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
570 *ppv = &This->IDispatchEx_iface;
571 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
572 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
573 *ppv = &This->IDispatchEx_iface;
574 }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
575 TRACE("(%p)->(IID_IDispatchEx %p)\n", This, ppv);
576 *ppv = &This->IDispatchEx_iface;
577 }else if(IsEqualGUID(&IID_IDispatchJS, riid)) {
578 TRACE("(%p)->(IID_IDispatchJS %p)\n", This, ppv);
579 jsdisp_addref(This);
580 *ppv = This;
581 return S_OK;
582 }else {
583 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
584 *ppv = NULL;
585 return E_NOINTERFACE;
588 IUnknown_AddRef((IUnknown*)*ppv);
589 return S_OK;
592 static ULONG WINAPI DispatchEx_AddRef(IDispatchEx *iface)
594 jsdisp_t *This = impl_from_IDispatchEx(iface);
595 LONG ref = InterlockedIncrement(&This->ref);
597 TRACE("(%p) ref=%d\n", This, ref);
599 return ref;
602 static ULONG WINAPI DispatchEx_Release(IDispatchEx *iface)
604 jsdisp_t *This = impl_from_IDispatchEx(iface);
605 LONG ref = InterlockedDecrement(&This->ref);
607 TRACE("(%p) ref=%d\n", This, ref);
609 if(!ref) {
610 dispex_prop_t *prop;
612 for(prop = This->props; prop < This->props+This->prop_cnt; prop++) {
613 if(prop->type == PROP_VARIANT)
614 VariantClear(&prop->u.var);
615 heap_free(prop->name);
617 heap_free(This->props);
618 script_release(This->ctx);
619 if(This->prototype)
620 jsdisp_release(This->prototype);
622 if(This->builtin_info->destructor)
623 This->builtin_info->destructor(This);
624 else
625 heap_free(This);
628 return ref;
631 static HRESULT WINAPI DispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
633 jsdisp_t *This = impl_from_IDispatchEx(iface);
635 TRACE("(%p)->(%p)\n", This, pctinfo);
637 *pctinfo = 1;
638 return S_OK;
641 static HRESULT WINAPI DispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo, LCID lcid,
642 ITypeInfo **ppTInfo)
644 jsdisp_t *This = impl_from_IDispatchEx(iface);
645 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
646 return E_NOTIMPL;
649 static HRESULT WINAPI DispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
650 LPOLESTR *rgszNames, UINT cNames, LCID lcid,
651 DISPID *rgDispId)
653 jsdisp_t *This = impl_from_IDispatchEx(iface);
654 UINT i;
655 HRESULT hres;
657 TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
658 lcid, rgDispId);
660 for(i=0; i < cNames; i++) {
661 hres = IDispatchEx_GetDispID(&This->IDispatchEx_iface, rgszNames[i], 0, rgDispId+i);
662 if(FAILED(hres))
663 return hres;
666 return S_OK;
669 static HRESULT WINAPI DispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
670 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
671 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
673 jsdisp_t *This = impl_from_IDispatchEx(iface);
675 TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
676 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
678 return IDispatchEx_InvokeEx(&This->IDispatchEx_iface, dispIdMember, lcid, wFlags,
679 pDispParams, pVarResult, pExcepInfo, NULL);
682 static HRESULT WINAPI DispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
684 jsdisp_t *This = impl_from_IDispatchEx(iface);
686 TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(bstrName), grfdex, pid);
688 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK)) {
689 FIXME("Unsupported grfdex %x\n", grfdex);
690 return E_NOTIMPL;
693 return jsdisp_get_id(This, bstrName, grfdex, pid);
696 static HRESULT WINAPI DispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
697 VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
699 jsdisp_t *This = impl_from_IDispatchEx(iface);
700 dispex_prop_t *prop;
701 jsexcept_t jsexcept;
702 HRESULT hres;
704 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
706 if(pvarRes)
707 V_VT(pvarRes) = VT_EMPTY;
709 prop = get_prop(This, id);
710 if(!prop || prop->type == PROP_DELETED) {
711 TRACE("invalid id\n");
712 return DISP_E_MEMBERNOTFOUND;
715 memset(&jsexcept, 0, sizeof(jsexcept));
717 switch(wFlags) {
718 case DISPATCH_METHOD|DISPATCH_PROPERTYGET:
719 wFlags = DISPATCH_METHOD;
720 /* fall through */
721 case DISPATCH_METHOD:
722 case DISPATCH_CONSTRUCT:
723 hres = invoke_prop_func(This, This, prop, wFlags, pdp, pvarRes, &jsexcept, pspCaller);
724 break;
725 case DISPATCH_PROPERTYGET:
726 hres = prop_get(This, prop, pdp, pvarRes, &jsexcept, pspCaller);
727 break;
728 case DISPATCH_PROPERTYPUT: {
729 DWORD i;
731 for(i=0; i < pdp->cNamedArgs; i++) {
732 if(pdp->rgdispidNamedArgs[i] == DISPID_PROPERTYPUT)
733 break;
736 if(i == pdp->cNamedArgs) {
737 TRACE("no value to set\n");
738 return DISP_E_PARAMNOTOPTIONAL;
741 hres = prop_put(This, prop, pdp->rgvarg+i, &jsexcept, pspCaller);
742 break;
744 default:
745 FIXME("Unimplemented flags %x\n", wFlags);
746 return E_INVALIDARG;
749 if(pei)
750 *pei = jsexcept.ei;
752 return hres;
755 static HRESULT delete_prop(dispex_prop_t *prop)
757 if(prop->type == PROP_VARIANT) {
758 VariantClear(&prop->u.var);
759 prop->type = PROP_DELETED;
761 return S_OK;
764 static HRESULT WINAPI DispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
766 jsdisp_t *This = impl_from_IDispatchEx(iface);
767 dispex_prop_t *prop;
768 HRESULT hres;
770 TRACE("(%p)->(%s %x)\n", This, debugstr_w(bstrName), grfdex);
772 if(grfdex & ~(fdexNameCaseSensitive|fdexNameEnsure|fdexNameImplicit|FDEX_VERSION_MASK))
773 FIXME("Unsupported grfdex %x\n", grfdex);
775 hres = find_prop_name(This, string_hash(bstrName), bstrName, &prop);
776 if(FAILED(hres))
777 return hres;
778 if(!prop) {
779 TRACE("not found\n");
780 return S_OK;
783 return delete_prop(prop);
786 static HRESULT WINAPI DispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
788 jsdisp_t *This = impl_from_IDispatchEx(iface);
789 dispex_prop_t *prop;
791 TRACE("(%p)->(%x)\n", This, id);
793 prop = get_prop(This, id);
794 if(!prop) {
795 WARN("invalid id\n");
796 return DISP_E_MEMBERNOTFOUND;
799 return delete_prop(prop);
802 static HRESULT WINAPI DispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
804 jsdisp_t *This = impl_from_IDispatchEx(iface);
805 FIXME("(%p)->(%x %x %p)\n", This, id, grfdexFetch, pgrfdex);
806 return E_NOTIMPL;
809 static HRESULT WINAPI DispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
811 jsdisp_t *This = impl_from_IDispatchEx(iface);
812 dispex_prop_t *prop;
814 TRACE("(%p)->(%x %p)\n", This, id, pbstrName);
816 prop = get_prop(This, id);
817 if(!prop || !prop->name || prop->type == PROP_DELETED)
818 return DISP_E_MEMBERNOTFOUND;
820 *pbstrName = SysAllocString(prop->name);
821 if(!*pbstrName)
822 return E_OUTOFMEMORY;
824 return S_OK;
827 static HRESULT WINAPI DispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
829 jsdisp_t *This = impl_from_IDispatchEx(iface);
830 dispex_prop_t *iter;
831 HRESULT hres;
833 TRACE("(%p)->(%x %x %p)\n", This, grfdex, id, pid);
835 if(id == DISPID_STARTENUM) {
836 hres = fill_protrefs(This);
837 if(FAILED(hres))
838 return hres;
841 if(id+1>=0 && id+1<This->prop_cnt) {
842 iter = &This->props[id+1];
843 }else {
844 *pid = DISPID_STARTENUM;
845 return S_FALSE;
848 while(iter < This->props + This->prop_cnt) {
849 if(iter->name && (get_flags(This, iter) & PROPF_ENUM) && iter->type!=PROP_DELETED) {
850 *pid = prop_to_id(This, iter);
851 return S_OK;
853 iter++;
856 *pid = DISPID_STARTENUM;
857 return S_FALSE;
860 static HRESULT WINAPI DispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
862 jsdisp_t *This = impl_from_IDispatchEx(iface);
863 FIXME("(%p)->(%p)\n", This, ppunk);
864 return E_NOTIMPL;
867 static IDispatchExVtbl DispatchExVtbl = {
868 DispatchEx_QueryInterface,
869 DispatchEx_AddRef,
870 DispatchEx_Release,
871 DispatchEx_GetTypeInfoCount,
872 DispatchEx_GetTypeInfo,
873 DispatchEx_GetIDsOfNames,
874 DispatchEx_Invoke,
875 DispatchEx_GetDispID,
876 DispatchEx_InvokeEx,
877 DispatchEx_DeleteMemberByName,
878 DispatchEx_DeleteMemberByDispID,
879 DispatchEx_GetMemberProperties,
880 DispatchEx_GetMemberName,
881 DispatchEx_GetNextDispID,
882 DispatchEx_GetNameSpaceParent
885 jsdisp_t *as_jsdisp(IDispatch *disp)
887 assert(disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl);
888 return impl_from_IDispatchEx((IDispatchEx*)disp);
891 jsdisp_t *to_jsdisp(IDispatch *disp)
893 return disp->lpVtbl == (IDispatchVtbl*)&DispatchExVtbl ? impl_from_IDispatchEx((IDispatchEx*)disp) : NULL;
896 HRESULT init_dispex(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype)
898 TRACE("%p (%p)\n", dispex, prototype);
900 dispex->IDispatchEx_iface.lpVtbl = &DispatchExVtbl;
901 dispex->ref = 1;
902 dispex->builtin_info = builtin_info;
904 dispex->props = heap_alloc_zero(sizeof(dispex_prop_t)*(dispex->buf_size=4));
905 if(!dispex->props)
906 return E_OUTOFMEMORY;
908 dispex->prototype = prototype;
909 if(prototype)
910 jsdisp_addref(prototype);
912 dispex->prop_cnt = 1;
913 if(builtin_info->value_prop.invoke) {
914 dispex->props[0].type = PROP_BUILTIN;
915 dispex->props[0].u.p = &builtin_info->value_prop;
916 }else {
917 dispex->props[0].type = PROP_DELETED;
920 script_addref(ctx);
921 dispex->ctx = ctx;
923 return S_OK;
926 static const builtin_info_t dispex_info = {
927 JSCLASS_NONE,
928 {NULL, NULL, 0},
929 0, NULL,
930 NULL,
931 NULL
934 HRESULT create_dispex(script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *prototype, jsdisp_t **dispex)
936 jsdisp_t *ret;
937 HRESULT hres;
939 ret = heap_alloc_zero(sizeof(jsdisp_t));
940 if(!ret)
941 return E_OUTOFMEMORY;
943 hres = init_dispex(ret, ctx, builtin_info ? builtin_info : &dispex_info, prototype);
944 if(FAILED(hres))
945 return hres;
947 *dispex = ret;
948 return S_OK;
951 HRESULT init_dispex_from_constr(jsdisp_t *dispex, script_ctx_t *ctx, const builtin_info_t *builtin_info, jsdisp_t *constr)
953 jsdisp_t *prot = NULL;
954 dispex_prop_t *prop;
955 HRESULT hres;
957 static const WCHAR constructorW[] = {'c','o','n','s','t','r','u','c','t','o','r',0};
958 static const WCHAR prototypeW[] = {'p','r','o','t','o','t','y','p','e',0};
960 hres = find_prop_name_prot(constr, string_hash(prototypeW), prototypeW, &prop);
961 if(SUCCEEDED(hres) && prop && prop->type!=PROP_DELETED) {
962 jsexcept_t jsexcept;
963 VARIANT var;
965 V_VT(&var) = VT_EMPTY;
966 memset(&jsexcept, 0, sizeof(jsexcept));
967 hres = prop_get(constr, prop, NULL, &var, &jsexcept, NULL/*FIXME*/);
968 if(FAILED(hres)) {
969 ERR("Could not get prototype\n");
970 return hres;
973 if(V_VT(&var) == VT_DISPATCH)
974 prot = iface_to_jsdisp((IUnknown*)V_DISPATCH(&var));
975 VariantClear(&var);
978 hres = init_dispex(dispex, ctx, builtin_info, prot);
980 if(prot)
981 jsdisp_release(prot);
982 if(FAILED(hres))
983 return hres;
985 hres = ensure_prop_name(dispex, constructorW, FALSE, 0, &prop);
986 if(SUCCEEDED(hres)) {
987 jsexcept_t jsexcept;
988 VARIANT var;
990 var_set_jsdisp(&var, constr);
991 memset(&jsexcept, 0, sizeof(jsexcept));
992 hres = prop_put(dispex, prop, &var, &jsexcept, NULL/*FIXME*/);
994 if(FAILED(hres))
995 jsdisp_release(dispex);
997 return hres;
1000 jsdisp_t *iface_to_jsdisp(IUnknown *iface)
1002 jsdisp_t *ret;
1003 HRESULT hres;
1005 hres = IUnknown_QueryInterface(iface, &IID_IDispatchJS, (void**)&ret);
1006 if(FAILED(hres))
1007 return NULL;
1009 return ret;
1012 static void ensure_retval_type(VARIANT *v)
1014 switch(V_VT(v)) {
1015 case VT_I2:
1016 V_VT(v) = VT_I4;
1017 V_I4(v) = V_I2(v);
1021 HRESULT jsdisp_get_id(jsdisp_t *jsdisp, const WCHAR *name, DWORD flags, DISPID *id)
1023 dispex_prop_t *prop;
1024 HRESULT hres;
1026 if(flags & fdexNameEnsure)
1027 hres = ensure_prop_name(jsdisp, name, TRUE, PROPF_ENUM, &prop);
1028 else
1029 hres = find_prop_name_prot(jsdisp, string_hash(name), name, &prop);
1030 if(FAILED(hres))
1031 return hres;
1033 if(prop && prop->type!=PROP_DELETED) {
1034 *id = prop_to_id(jsdisp, prop);
1035 return S_OK;
1038 TRACE("not found %s\n", debugstr_w(name));
1039 return DISP_E_UNKNOWNNAME;
1042 HRESULT jsdisp_call_value(jsdisp_t *jsthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1044 vdisp_t vdisp;
1045 HRESULT hres;
1047 set_jsdisp(&vdisp, jsthis);
1048 hres = jsthis->builtin_info->value_prop.invoke(jsthis->ctx, &vdisp, flags, dp, retv, ei);
1049 vdisp_release(&vdisp);
1050 return hres;
1053 HRESULT jsdisp_call(jsdisp_t *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1055 dispex_prop_t *prop;
1057 memset(ei, 0, sizeof(*ei));
1058 if(retv)
1059 V_VT(retv) = VT_EMPTY;
1061 prop = get_prop(disp, id);
1062 if(!prop)
1063 return DISP_E_MEMBERNOTFOUND;
1065 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, NULL);
1068 HRESULT jsdisp_call_name(jsdisp_t *disp, const WCHAR *name, WORD flags, DISPPARAMS *dp, VARIANT *retv,
1069 jsexcept_t *ei)
1071 dispex_prop_t *prop;
1072 HRESULT hres;
1074 hres = find_prop_name_prot(disp, string_hash(name), name, &prop);
1075 if(FAILED(hres))
1076 return hres;
1078 memset(ei, 0, sizeof(*ei));
1079 if(retv)
1080 V_VT(retv) = VT_EMPTY;
1082 return invoke_prop_func(disp, disp, prop, flags, dp, retv, ei, NULL);
1085 HRESULT disp_call(script_ctx_t *ctx, IDispatch *disp, DISPID id, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei)
1087 jsdisp_t *jsdisp;
1088 IDispatchEx *dispex;
1089 HRESULT hres;
1091 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1092 if(jsdisp) {
1093 if(flags & DISPATCH_PROPERTYPUT) {
1094 FIXME("disp_call(propput) on builtin object\n");
1095 return E_FAIL;
1098 hres = jsdisp_call(jsdisp, id, flags, dp, retv, ei);
1099 jsdisp_release(jsdisp);
1100 return hres;
1103 memset(ei, 0, sizeof(*ei));
1104 if(retv && arg_cnt(dp))
1105 flags |= DISPATCH_PROPERTYGET;
1107 if(retv)
1108 V_VT(retv) = VT_EMPTY;
1109 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1110 if(SUCCEEDED(hres)) {
1111 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, flags, dp, retv, &ei->ei, &ctx->jscaller->IServiceProvider_iface);
1112 IDispatchEx_Release(dispex);
1113 }else {
1114 UINT err = 0;
1116 if(flags == DISPATCH_CONSTRUCT) {
1117 WARN("IDispatch cannot be constructor\n");
1118 return DISP_E_MEMBERNOTFOUND;
1121 TRACE("using IDispatch\n");
1122 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, flags, dp, retv, &ei->ei, &err);
1125 if(FAILED(hres))
1126 return hres;
1128 if(retv)
1129 ensure_retval_type(retv);
1130 return S_OK;
1133 HRESULT jsdisp_propput_name(jsdisp_t *obj, const WCHAR *name, VARIANT *val, jsexcept_t *ei)
1135 dispex_prop_t *prop;
1136 HRESULT hres;
1138 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM, &prop);
1139 if(FAILED(hres))
1140 return hres;
1142 return prop_put(obj, prop, val, ei, NULL);
1145 HRESULT jsdisp_propput_const(jsdisp_t *obj, const WCHAR *name, VARIANT *val)
1147 dispex_prop_t *prop;
1148 HRESULT hres;
1150 hres = ensure_prop_name(obj, name, FALSE, PROPF_ENUM|PROPF_CONST, &prop);
1151 if(FAILED(hres))
1152 return hres;
1154 return VariantCopy(&prop->u.var, val);
1157 HRESULT jsdisp_propput_idx(jsdisp_t *obj, DWORD idx, VARIANT *val, jsexcept_t *ei)
1159 WCHAR buf[12];
1161 static const WCHAR formatW[] = {'%','d',0};
1163 sprintfW(buf, formatW, idx);
1164 return jsdisp_propput_name(obj, buf, val, ei);
1167 HRESULT disp_propput(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1169 jsdisp_t *jsdisp;
1170 HRESULT hres;
1172 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1173 if(jsdisp) {
1174 dispex_prop_t *prop;
1176 prop = get_prop(jsdisp, id);
1177 if(prop)
1178 hres = prop_put(jsdisp, prop, val, ei, NULL);
1179 else
1180 hres = DISP_E_MEMBERNOTFOUND;
1182 jsdisp_release(jsdisp);
1183 }else {
1184 DISPID dispid = DISPID_PROPERTYPUT;
1185 DISPPARAMS dp = {val, &dispid, 1, 1};
1186 IDispatchEx *dispex;
1188 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1189 if(SUCCEEDED(hres)) {
1190 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei,
1191 &ctx->jscaller->IServiceProvider_iface);
1192 IDispatchEx_Release(dispex);
1193 }else {
1194 ULONG err = 0;
1196 TRACE("using IDispatch\n");
1197 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, DISPATCH_PROPERTYPUT, &dp, NULL, &ei->ei, &err);
1201 return hres;
1204 HRESULT jsdisp_propget_name(jsdisp_t *obj, const WCHAR *name, VARIANT *var, jsexcept_t *ei)
1206 DISPPARAMS dp = {NULL, NULL, 0, 0};
1207 dispex_prop_t *prop;
1208 HRESULT hres;
1210 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1211 if(FAILED(hres))
1212 return hres;
1214 V_VT(var) = VT_EMPTY;
1215 if(!prop || prop->type==PROP_DELETED)
1216 return S_OK;
1218 return prop_get(obj, prop, &dp, var, ei, NULL);
1221 HRESULT jsdisp_get_idx(jsdisp_t *obj, DWORD idx, VARIANT *var, jsexcept_t *ei)
1223 WCHAR name[12];
1224 DISPPARAMS dp = {NULL, NULL, 0, 0};
1225 dispex_prop_t *prop;
1226 HRESULT hres;
1228 static const WCHAR formatW[] = {'%','d',0};
1230 sprintfW(name, formatW, idx);
1232 hres = find_prop_name_prot(obj, string_hash(name), name, &prop);
1233 if(FAILED(hres))
1234 return hres;
1236 V_VT(var) = VT_EMPTY;
1237 if(!prop || prop->type==PROP_DELETED)
1238 return DISP_E_UNKNOWNNAME;
1240 return prop_get(obj, prop, &dp, var, ei, NULL);
1243 HRESULT jsdisp_propget(jsdisp_t *jsdisp, DISPID id, VARIANT *val, jsexcept_t *ei)
1245 DISPPARAMS dp = {NULL,NULL,0,0};
1246 dispex_prop_t *prop;
1248 prop = get_prop(jsdisp, id);
1249 if(!prop)
1250 return DISP_E_MEMBERNOTFOUND;
1252 V_VT(val) = VT_EMPTY;
1253 return prop_get(jsdisp, prop, &dp, val, ei, NULL);
1256 HRESULT disp_propget(script_ctx_t *ctx, IDispatch *disp, DISPID id, VARIANT *val, jsexcept_t *ei)
1258 DISPPARAMS dp = {NULL,NULL,0,0};
1259 IDispatchEx *dispex;
1260 jsdisp_t *jsdisp;
1261 HRESULT hres;
1263 jsdisp = iface_to_jsdisp((IUnknown*)disp);
1264 if(jsdisp) {
1265 hres = jsdisp_propget(jsdisp, id, val, ei);
1266 jsdisp_release(jsdisp);
1267 return hres;
1270 hres = IDispatch_QueryInterface(disp, &IID_IDispatchEx, (void**)&dispex);
1271 if(SUCCEEDED(hres)) {
1272 hres = IDispatchEx_InvokeEx(dispex, id, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei,
1273 &ctx->jscaller->IServiceProvider_iface);
1274 IDispatchEx_Release(dispex);
1275 }else {
1276 ULONG err = 0;
1278 TRACE("using IDispatch\n");
1279 hres = IDispatch_Invoke(disp, id, &IID_NULL, ctx->lcid, INVOKE_PROPERTYGET, &dp, val, &ei->ei, &err);
1281 if(FAILED(hres))
1282 return hres;
1284 ensure_retval_type(val);
1285 return S_OK;
1288 HRESULT jsdisp_delete_idx(jsdisp_t *obj, DWORD idx)
1290 static const WCHAR formatW[] = {'%','d',0};
1291 WCHAR buf[12];
1292 dispex_prop_t *prop;
1293 HRESULT hres;
1295 sprintfW(buf, formatW, idx);
1297 hres = find_prop_name(obj, string_hash(buf), buf, &prop);
1298 if(FAILED(hres) || !prop)
1299 return hres;
1301 return delete_prop(prop);
1304 VARIANT_BOOL jsdisp_is_own_prop(jsdisp_t *obj, BSTR name)
1306 dispex_prop_t *prop;
1307 HRESULT hres;
1309 hres = find_prop_name(obj, string_hash(name), name, &prop);
1310 if(FAILED(hres))
1311 return VARIANT_FALSE;
1312 else if(!prop)
1313 return VARIANT_FALSE;
1315 return prop->type==PROP_VARIANT ? VARIANT_TRUE : VARIANT_FALSE;