user32: Handle all kinds of values returned by WIN_GetPtr.
[wine/multimedia.git] / dlls / jscript / array.c
blob5e94f95bf8c3323c1e1e93cf4a0f11b237c523f7
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/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
25 typedef struct {
26 DispatchEx dispex;
28 DWORD length;
29 } ArrayInstance;
31 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
32 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
33 static const WCHAR joinW[] = {'j','o','i','n',0};
34 static const WCHAR popW[] = {'p','o','p',0};
35 static const WCHAR pushW[] = {'p','u','s','h',0};
36 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
37 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
38 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
39 static const WCHAR sortW[] = {'s','o','r','t',0};
40 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
41 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
42 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
43 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
44 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
45 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
46 static const WCHAR propertyIsEnumerableW[] =
47 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
48 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
50 const WCHAR default_separatorW[] = {',',0};
52 static HRESULT Array_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
53 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
55 ArrayInstance *This = (ArrayInstance*)dispex;
57 TRACE("%p %d\n", This, This->length);
59 switch(flags) {
60 case DISPATCH_PROPERTYGET:
61 V_VT(retv) = VT_I4;
62 V_I4(retv) = This->length;
63 break;
64 default:
65 FIXME("unimplemented flags %x\n", flags);
66 return E_NOTIMPL;
69 return S_OK;
72 static HRESULT concat_array(DispatchEx *array, ArrayInstance *obj, DWORD *len, LCID lcid,
73 jsexcept_t *ei, IServiceProvider *caller)
75 VARIANT var;
76 DWORD i;
77 HRESULT hres;
79 for(i=0; i < obj->length; i++) {
80 hres = jsdisp_propget_idx(&obj->dispex, i, lcid, &var, ei, caller);
81 if(hres == DISP_E_UNKNOWNNAME)
82 continue;
83 if(FAILED(hres))
84 return hres;
86 hres = jsdisp_propput_idx(array, *len+i, lcid, &var, ei, caller);
87 VariantClear(&var);
88 if(FAILED(hres))
89 return hres;
92 *len += obj->length;
93 return S_OK;
96 static HRESULT concat_obj(DispatchEx *array, IDispatch *obj, DWORD *len, LCID lcid, jsexcept_t *ei, IServiceProvider *caller)
98 DispatchEx *jsobj;
99 VARIANT var;
100 HRESULT hres;
102 jsobj = iface_to_jsdisp((IUnknown*)obj);
103 if(jsobj) {
104 if(is_class(jsobj, JSCLASS_ARRAY)) {
105 hres = concat_array(array, (ArrayInstance*)jsobj, len, lcid, ei, caller);
106 jsdisp_release(jsobj);
107 return hres;
109 jsdisp_release(jsobj);
112 V_VT(&var) = VT_DISPATCH;
113 V_DISPATCH(&var) = obj;
114 return jsdisp_propput_idx(array, (*len)++, lcid, &var, ei, caller);
117 static HRESULT Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
118 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
120 DispatchEx *ret;
121 DWORD len = 0;
122 HRESULT hres;
124 TRACE("\n");
126 hres = create_array(dispex->ctx, 0, &ret);
127 if(FAILED(hres))
128 return hres;
130 hres = concat_obj(ret, (IDispatch*)_IDispatchEx_(dispex), &len, lcid, ei, caller);
131 if(SUCCEEDED(hres)) {
132 VARIANT *arg;
133 DWORD i;
135 for(i=0; i < arg_cnt(dp); i++) {
136 arg = get_arg(dp, i);
137 if(V_VT(arg) == VT_DISPATCH)
138 hres = concat_obj(ret, V_DISPATCH(arg), &len, lcid, ei, caller);
139 else
140 hres = jsdisp_propput_idx(ret, len++, lcid, arg, ei, caller);
141 if(FAILED(hres))
142 break;
146 if(FAILED(hres))
147 return hres;
149 if(retv) {
150 V_VT(retv) = VT_DISPATCH;
151 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
152 }else {
153 jsdisp_release(ret);
155 return S_OK;
158 static HRESULT array_join(DispatchEx *array, LCID lcid, DWORD length, const WCHAR *sep, VARIANT *retv,
159 jsexcept_t *ei, IServiceProvider *caller)
161 BSTR *str_tab, ret = NULL;
162 VARIANT var;
163 DWORD i;
164 HRESULT hres = E_FAIL;
166 if(!length) {
167 if(retv) {
168 V_VT(retv) = VT_BSTR;
169 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
170 if(!V_BSTR(retv))
171 return E_OUTOFMEMORY;
173 return S_OK;
176 str_tab = heap_alloc_zero(length * sizeof(BSTR));
177 if(!str_tab)
178 return E_OUTOFMEMORY;
180 for(i=0; i < length; i++) {
181 hres = jsdisp_propget_idx(array, i, lcid, &var, ei, caller);
182 if(FAILED(hres))
183 break;
185 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
186 hres = to_string(array->ctx, &var, ei, str_tab+i);
187 VariantClear(&var);
188 if(FAILED(hres))
189 break;
192 if(SUCCEEDED(hres)) {
193 DWORD seplen = 0, len = 0;
194 WCHAR *ptr;
196 seplen = strlenW(sep);
198 if(str_tab[0])
199 len = SysStringLen(str_tab[0]);
200 for(i=1; i < length; i++)
201 len += seplen + SysStringLen(str_tab[i]);
203 ret = SysAllocStringLen(NULL, len);
204 if(ret) {
205 DWORD tmplen = 0;
207 if(str_tab[0]) {
208 tmplen = SysStringLen(str_tab[0]);
209 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
212 ptr = ret + tmplen;
213 for(i=1; i < length; i++) {
214 if(seplen) {
215 memcpy(ptr, sep, seplen*sizeof(WCHAR));
216 ptr += seplen;
219 if(str_tab[i]) {
220 tmplen = SysStringLen(str_tab[i]);
221 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
222 ptr += tmplen;
225 *ptr=0;
226 }else {
227 hres = E_OUTOFMEMORY;
231 for(i=0; i < length; i++)
232 SysFreeString(str_tab[i]);
233 heap_free(str_tab);
234 if(FAILED(hres))
235 return hres;
237 TRACE("= %s\n", debugstr_w(ret));
239 if(retv) {
240 if(!ret) {
241 ret = SysAllocStringLen(NULL, 0);
242 if(!ret)
243 return E_OUTOFMEMORY;
246 V_VT(retv) = VT_BSTR;
247 V_BSTR(retv) = ret;
248 }else {
249 SysFreeString(ret);
252 return S_OK;
255 /* ECMA-262 3rd Edition 15.4.4.5 */
256 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
257 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
259 DWORD length;
260 HRESULT hres;
262 TRACE("\n");
264 if(is_class(dispex, JSCLASS_ARRAY)) {
265 length = ((ArrayInstance*)dispex)->length;
266 }else {
267 FIXME("dispid is not Array\n");
268 return E_NOTIMPL;
271 if(arg_cnt(dp)) {
272 BSTR sep;
274 hres = to_string(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &sep);
275 if(FAILED(hres))
276 return hres;
278 hres = array_join(dispex, lcid, length, sep, retv, ei, caller);
280 SysFreeString(sep);
281 }else {
282 hres = array_join(dispex, lcid, length, default_separatorW, retv, ei, caller);
285 return hres;
288 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
289 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
291 VARIANT val;
292 DWORD length;
293 WCHAR buf[14];
294 DISPID id;
295 HRESULT hres;
297 static const WCHAR formatW[] = {'%','d',0};
299 TRACE("\n");
301 if(is_class(dispex, JSCLASS_ARRAY)) {
302 ArrayInstance *array = (ArrayInstance*)dispex;
303 length = array->length;
304 }else {
305 FIXME("not Array this\n");
306 return E_NOTIMPL;
309 if(!length) {
310 if(retv)
311 V_VT(retv) = VT_EMPTY;
312 return S_OK;
315 sprintfW(buf, formatW, --length);
316 hres = jsdisp_get_id(dispex, buf, 0, &id);
317 if(SUCCEEDED(hres)) {
318 hres = jsdisp_propget(dispex, id, lcid, &val, ei, caller);
319 if(FAILED(hres))
320 return hres;
322 hres = IDispatchEx_DeleteMemberByDispID(_IDispatchEx_(dispex), id);
323 }else if(hres == DISP_E_UNKNOWNNAME) {
324 V_VT(&val) = VT_EMPTY;
325 hres = S_OK;
326 }else {
327 return hres;
330 if(SUCCEEDED(hres)) {
331 if(is_class(dispex, JSCLASS_ARRAY)) {
332 ArrayInstance *array = (ArrayInstance*)dispex;
333 array->length = length;
337 if(FAILED(hres)) {
338 VariantClear(&val);
339 return hres;
342 if(retv)
343 *retv = val;
344 else
345 VariantClear(&val);
346 return S_OK;
349 /* ECMA-262 3rd Edition 15.4.4.7 */
350 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
351 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
353 DWORD length = 0;
354 int i, n;
355 HRESULT hres;
357 TRACE("\n");
359 if(dispex->builtin_info->class == JSCLASS_ARRAY) {
360 length = ((ArrayInstance*)dispex)->length;
361 }else {
362 FIXME("not Array this\n");
363 return E_NOTIMPL;
366 n = dp->cArgs - dp->cNamedArgs;
367 for(i=0; i < n; i++) {
368 hres = jsdisp_propput_idx(dispex, length+i, lcid, get_arg(dp, i), ei, sp);
369 if(FAILED(hres))
370 return hres;
373 if(retv) {
374 V_VT(retv) = VT_I4;
375 V_I4(retv) = length+n;
377 return S_OK;
380 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
381 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
383 FIXME("\n");
384 return E_NOTIMPL;
387 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
388 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
390 FIXME("\n");
391 return E_NOTIMPL;
394 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
395 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
397 FIXME("\n");
398 return E_NOTIMPL;
401 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
402 IServiceProvider *caller, INT *cmp)
404 HRESULT hres;
406 if(cmp_func) {
407 VARIANTARG args[2];
408 DISPPARAMS dp = {args, NULL, 2, 0};
409 VARIANT tmp;
410 VARIANT res;
412 args[0] = *v2;
413 args[1] = *v1;
415 hres = jsdisp_call_value(cmp_func, ctx->lcid, DISPATCH_METHOD, &dp, &res, ei, caller);
416 if(FAILED(hres))
417 return hres;
419 hres = to_number(ctx, &res, ei, &tmp);
420 VariantClear(&res);
421 if(FAILED(hres))
422 return hres;
424 if(V_VT(&tmp) == VT_I4)
425 *cmp = V_I4(&tmp);
426 else
427 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
428 }else if(is_num_vt(V_VT(v1))) {
429 if(is_num_vt(V_VT(v2))) {
430 DOUBLE d = num_val(v1)-num_val(v2);
431 if(d > 0.0)
432 *cmp = 1;
433 else if(d < -0.0)
434 *cmp = -1;
435 else
436 *cmp = 0;
437 }else {
438 *cmp = -1;
440 }else if(is_num_vt(V_VT(v2))) {
441 *cmp = 1;
442 }else if(V_VT(v1) == VT_BSTR) {
443 if(V_VT(v2) == VT_BSTR)
444 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
445 else
446 *cmp = -1;
447 }else if(V_VT(v2) == VT_BSTR) {
448 *cmp = 1;
449 }else {
450 *cmp = 0;
453 return S_OK;
456 /* ECMA-262 3rd Edition 15.4.4.11 */
457 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
458 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
460 DispatchEx *cmp_func = NULL;
461 VARIANT *vtab, **sorttab = NULL;
462 DWORD length;
463 DWORD i;
464 HRESULT hres = S_OK;
466 TRACE("\n");
468 if(is_class(dispex, JSCLASS_ARRAY)) {
469 length = ((ArrayInstance*)dispex)->length;
470 }else {
471 FIXME("unsupported this not array\n");
472 return E_NOTIMPL;
475 if(arg_cnt(dp) > 1) {
476 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
477 return E_FAIL;
480 if(arg_cnt(dp) == 1) {
481 VARIANT *arg = get_arg(dp, 0);
483 if(V_VT(arg) != VT_DISPATCH) {
484 WARN("arg is not dispatch\n");
485 return E_FAIL;
489 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
490 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
491 WARN("cmp_func is not a function\n");
492 if(cmp_func)
493 jsdisp_release(cmp_func);
494 return E_FAIL;
498 if(!length) {
499 if(cmp_func)
500 jsdisp_release(cmp_func);
501 if(retv) {
502 V_VT(retv) = VT_DISPATCH;
503 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
504 IDispatchEx_AddRef(_IDispatchEx_(dispex));
506 return S_OK;
509 vtab = heap_alloc_zero(length * sizeof(VARIANT));
510 if(vtab) {
511 for(i=0; i<length; i++) {
512 hres = jsdisp_propget_idx(dispex, i, lcid, vtab+i, ei, caller);
513 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
514 WARN("Could not get elem %d: %08x\n", i, hres);
515 break;
518 }else {
519 hres = E_OUTOFMEMORY;
522 if(SUCCEEDED(hres)) {
523 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
524 if(!sorttab)
525 hres = E_OUTOFMEMORY;
528 /* merge-sort */
529 if(SUCCEEDED(hres)) {
530 VARIANT *tmpv, **tmpbuf;
531 INT cmp;
533 tmpbuf = sorttab + length;
534 for(i=0; i < length; i++)
535 sorttab[i] = vtab+i;
537 for(i=0; i < length/2; i++) {
538 hres = sort_cmp(dispex->ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
539 if(FAILED(hres))
540 break;
542 if(cmp < 0) {
543 tmpv = sorttab[2*i];
544 sorttab[2*i] = sorttab[2*i+1];
545 sorttab[2*i+1] = tmpv;
549 if(SUCCEEDED(hres)) {
550 DWORD k, a, b, bend;
552 for(k=2; k < length; k *= 2) {
553 for(i=0; i+k < length; i += 2*k) {
554 a = b = 0;
555 if(i+2*k <= length)
556 bend = k;
557 else
558 bend = length - (i+k);
560 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
562 while(a < k && b < bend) {
563 hres = sort_cmp(dispex->ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
564 if(FAILED(hres))
565 break;
567 if(cmp < 0) {
568 sorttab[i+a+b] = tmpbuf[a];
569 a++;
570 }else {
571 sorttab[i+a+b] = sorttab[i+k+b];
572 b++;
576 if(FAILED(hres))
577 break;
579 if(a < k)
580 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
583 if(FAILED(hres))
584 break;
588 for(i=0; SUCCEEDED(hres) && i < length; i++)
589 hres = jsdisp_propput_idx(dispex, i, lcid, sorttab[i], ei, caller);
592 if(vtab) {
593 for(i=0; i < length; i++)
594 VariantClear(vtab+i);
595 heap_free(vtab);
597 heap_free(sorttab);
598 if(cmp_func)
599 jsdisp_release(cmp_func);
601 if(FAILED(hres))
602 return hres;
604 if(retv) {
605 V_VT(retv) = VT_DISPATCH;
606 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
607 IDispatch_AddRef(_IDispatchEx_(dispex));
610 return S_OK;
613 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
614 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
616 FIXME("\n");
617 return E_NOTIMPL;
620 /* ECMA-262 3rd Edition 15.4.4.2 */
621 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
622 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
624 TRACE("\n");
626 if(!is_class(dispex, JSCLASS_ARRAY)) {
627 WARN("not Array object\n");
628 return E_FAIL;
631 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
634 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
635 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
637 FIXME("\n");
638 return E_NOTIMPL;
641 static HRESULT Array_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
642 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
644 FIXME("\n");
645 return E_NOTIMPL;
648 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
649 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
651 FIXME("\n");
652 return E_NOTIMPL;
655 static HRESULT Array_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
656 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
658 FIXME("\n");
659 return E_NOTIMPL;
662 static HRESULT Array_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
663 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
665 FIXME("\n");
666 return E_NOTIMPL;
669 static HRESULT Array_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
670 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
672 FIXME("\n");
673 return E_NOTIMPL;
676 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
677 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
679 TRACE("\n");
681 switch(flags) {
682 case INVOKE_PROPERTYGET:
683 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
684 default:
685 FIXME("unimplemented flags %x\n", flags);
686 return E_NOTIMPL;
689 return S_OK;
692 static void Array_destructor(DispatchEx *dispex)
694 heap_free(dispex);
697 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
699 ArrayInstance *array = (ArrayInstance*)dispex;
700 const WCHAR *ptr = name;
701 DWORD id = 0;
703 if(!isdigitW(*ptr))
704 return;
706 while(*ptr && isdigitW(*ptr)) {
707 id = id*10 + (*ptr-'0');
708 ptr++;
711 if(*ptr)
712 return;
714 if(id >= array->length)
715 array->length = id+1;
718 static const builtin_prop_t Array_props[] = {
719 {concatW, Array_concat, PROPF_METHOD},
720 {hasOwnPropertyW, Array_hasOwnProperty, PROPF_METHOD},
721 {isPrototypeOfW, Array_isPrototypeOf, PROPF_METHOD},
722 {joinW, Array_join, PROPF_METHOD},
723 {lengthW, Array_length, 0},
724 {popW, Array_pop, PROPF_METHOD},
725 {propertyIsEnumerableW, Array_propertyIsEnumerable, PROPF_METHOD},
726 {pushW, Array_push, PROPF_METHOD},
727 {reverseW, Array_reverse, PROPF_METHOD},
728 {shiftW, Array_shift, PROPF_METHOD},
729 {sliceW, Array_slice, PROPF_METHOD},
730 {sortW, Array_sort, PROPF_METHOD},
731 {spliceW, Array_splice, PROPF_METHOD},
732 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
733 {toStringW, Array_toString, PROPF_METHOD},
734 {unshiftW, Array_unshift, PROPF_METHOD},
735 {valueOfW, Array_valueOf, PROPF_METHOD}
738 static const builtin_info_t Array_info = {
739 JSCLASS_ARRAY,
740 {NULL, Array_value, 0},
741 sizeof(Array_props)/sizeof(*Array_props),
742 Array_props,
743 Array_destructor,
744 Array_on_put
747 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
748 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
750 DispatchEx *obj;
751 VARIANT *arg_var;
752 DWORD i;
753 HRESULT hres;
755 TRACE("\n");
757 switch(flags) {
758 case DISPATCH_CONSTRUCT: {
759 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
760 if(V_I4(arg_var) < 0) {
761 FIXME("throw RangeError\n");
762 return E_FAIL;
765 hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
766 if(FAILED(hres))
767 return hres;
769 V_VT(retv) = VT_DISPATCH;
770 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
771 return S_OK;
774 hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
775 if(FAILED(hres))
776 return hres;
778 for(i=0; i < arg_cnt(dp); i++) {
779 hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
780 if(FAILED(hres))
781 break;
783 if(FAILED(hres)) {
784 jsdisp_release(obj);
785 return hres;
788 V_VT(retv) = VT_DISPATCH;
789 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
790 break;
792 default:
793 FIXME("unimplemented flags: %x\n", flags);
794 return E_NOTIMPL;
797 return S_OK;
800 static HRESULT alloc_array(script_ctx_t *ctx, BOOL use_constr, ArrayInstance **ret)
802 ArrayInstance *array;
803 HRESULT hres;
805 array = heap_alloc_zero(sizeof(ArrayInstance));
806 if(!array)
807 return E_OUTOFMEMORY;
809 if(use_constr)
810 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
811 else
812 hres = init_dispex(&array->dispex, ctx, &Array_info, NULL);
814 if(FAILED(hres)) {
815 heap_free(array);
816 return hres;
819 *ret = array;
820 return S_OK;
823 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx **ret)
825 ArrayInstance *array;
826 HRESULT hres;
828 hres = alloc_array(ctx, FALSE, &array);
829 if(FAILED(hres))
830 return hres;
832 hres = create_builtin_function(ctx, ArrayConstr_value, PROPF_CONSTR, &array->dispex, ret);
834 IDispatchEx_Release(_IDispatchEx_(&array->dispex));
835 return hres;
838 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
840 ArrayInstance *array;
841 HRESULT hres;
843 hres = alloc_array(ctx, TRUE, &array);
844 if(FAILED(hres))
845 return hres;
847 array->length = length;
849 *ret = &array->dispex;
850 return S_OK;