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
20 #include "wine/port.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
36 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
37 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
38 static const WCHAR joinW[] = {'j','o','i','n',0};
39 static const WCHAR popW[] = {'p','o','p',0};
40 static const WCHAR pushW[] = {'p','u','s','h',0};
41 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
42 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
43 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
44 static const WCHAR sortW[] = {'s','o','r','t',0};
45 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
46 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
47 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
48 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
50 static const WCHAR default_separatorW[] = {',',0};
52 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
54 return (ArrayInstance*)vdisp->u.jsdisp;
57 static inline ArrayInstance *array_this(vdisp_t *jsthis)
59 return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
62 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsexcept_t *ei, jsdisp_t **jsthis, DWORD *ret)
68 array = array_this(vdisp);
70 *jsthis = &array->dispex;
76 return throw_type_error(ctx, ei, JS_E_JSCRIPT_EXPECTED, NULL);
78 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei, NULL/*FIXME*/);
82 hres = to_uint32(ctx, &var, ei, ret);
87 *jsthis = vdisp->u.jsdisp;
91 static HRESULT set_length(jsdisp_t *obj, jsexcept_t *ei, DWORD length)
95 if(is_class(obj, JSCLASS_ARRAY)) {
96 ((ArrayInstance*)obj)->length = length;
102 return jsdisp_propput_name(obj, lengthW, &var, ei, NULL/*FIXME*/);
105 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
113 *ptr-- = '0' + (idx%10);
120 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
121 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
123 ArrayInstance *This = array_from_vdisp(jsthis);
125 TRACE("%p %d\n", This, This->length);
128 case DISPATCH_PROPERTYGET:
130 V_I4(retv) = This->length;
132 case DISPATCH_PROPERTYPUT: {
138 hres = to_number(ctx, get_arg(dp, 0), ei, &num);
142 if(V_VT(&num) == VT_I4)
145 len = floor(V_R8(&num));
148 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
150 for(i=len; i<This->length; i++) {
151 hres = jsdisp_delete_idx(&This->dispex, i);
160 FIXME("unimplemented flags %x\n", flags);
167 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len,
168 jsexcept_t *ei, IServiceProvider *caller)
174 for(i=0; i < obj->length; i++) {
175 hres = jsdisp_get_idx(&obj->dispex, i, &var, ei, caller);
176 if(hres == DISP_E_UNKNOWNNAME)
181 hres = jsdisp_propput_idx(array, *len+i, &var, ei, caller);
191 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei, IServiceProvider *caller)
197 jsobj = iface_to_jsdisp((IUnknown*)obj);
199 if(is_class(jsobj, JSCLASS_ARRAY)) {
200 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei, caller);
201 jsdisp_release(jsobj);
204 jsdisp_release(jsobj);
207 V_VT(&var) = VT_DISPATCH;
208 V_DISPATCH(&var) = obj;
209 return jsdisp_propput_idx(array, (*len)++, &var, ei, caller);
212 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
213 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
221 hres = create_array(ctx, 0, &ret);
225 hres = concat_obj(ret, jsthis->u.disp, &len, ei, caller);
226 if(SUCCEEDED(hres)) {
230 for(i=0; i < arg_cnt(dp); i++) {
231 arg = get_arg(dp, i);
232 if(V_VT(arg) == VT_DISPATCH)
233 hres = concat_obj(ret, V_DISPATCH(arg), &len, ei, caller);
235 hres = jsdisp_propput_idx(ret, len++, arg, ei, caller);
245 var_set_jsdisp(retv, ret);
251 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, VARIANT *retv,
252 jsexcept_t *ei, IServiceProvider *caller)
254 BSTR *str_tab, ret = NULL;
257 HRESULT hres = E_FAIL;
261 V_VT(retv) = VT_BSTR;
262 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
264 return E_OUTOFMEMORY;
269 str_tab = heap_alloc_zero(length * sizeof(BSTR));
271 return E_OUTOFMEMORY;
273 for(i=0; i < length; i++) {
274 hres = jsdisp_get_idx(array, i, &var, ei, caller);
275 if(hres == DISP_E_UNKNOWNNAME) {
278 } else if(FAILED(hres))
281 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
282 hres = to_string(ctx, &var, ei, str_tab+i);
288 if(SUCCEEDED(hres)) {
289 DWORD seplen = 0, len = 0;
292 seplen = strlenW(sep);
295 len = SysStringLen(str_tab[0]);
296 for(i=1; i < length; i++)
297 len += seplen + SysStringLen(str_tab[i]);
299 ret = SysAllocStringLen(NULL, len);
304 tmplen = SysStringLen(str_tab[0]);
305 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
309 for(i=1; i < length; i++) {
311 memcpy(ptr, sep, seplen*sizeof(WCHAR));
316 tmplen = SysStringLen(str_tab[i]);
317 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
323 hres = E_OUTOFMEMORY;
327 for(i=0; i < length; i++)
328 SysFreeString(str_tab[i]);
333 TRACE("= %s\n", debugstr_w(ret));
337 ret = SysAllocStringLen(NULL, 0);
339 return E_OUTOFMEMORY;
342 V_VT(retv) = VT_BSTR;
351 /* ECMA-262 3rd Edition 15.4.4.5 */
352 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
353 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
361 hres = get_length(ctx, vthis, ei, &jsthis, &length);
368 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
372 hres = array_join(ctx, jsthis, length, sep, retv, ei, caller);
376 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei, caller);
382 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
383 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
392 hres = get_length(ctx, vthis, ei, &jsthis, &length);
397 hres = set_length(jsthis, ei, 0);
402 V_VT(retv) = VT_EMPTY;
407 hres = jsdisp_get_idx(jsthis, length, &val, ei, caller);
408 if(SUCCEEDED(hres)) {
409 hres = jsdisp_delete_idx(jsthis, length);
410 } else if(hres == DISP_E_UNKNOWNNAME) {
411 V_VT(&val) = VT_EMPTY;
417 hres = set_length(jsthis, ei, length);
432 /* ECMA-262 3rd Edition 15.4.4.7 */
433 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
434 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
443 hres = get_length(ctx, vthis, ei, &jsthis, &length);
448 for(i=0; i < n; i++) {
449 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei, sp);
454 hres = set_length(jsthis, ei, length+n);
460 V_I4(retv) = length+n;
465 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
466 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
471 HRESULT hres1, hres2;
475 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
479 for(k=0; k<length/2; k++) {
482 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei, sp);
483 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
486 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei, sp);
487 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
492 if(hres1 == DISP_E_UNKNOWNNAME)
493 hres1 = jsdisp_delete_idx(jsthis, l);
495 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei, sp);
503 if(hres2 == DISP_E_UNKNOWNNAME)
504 hres2 = jsdisp_delete_idx(jsthis, k);
506 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei, sp);
515 jsdisp_addref(jsthis);
516 var_set_jsdisp(retv, jsthis);
522 /* ECMA-262 3rd Edition 15.4.4.9 */
523 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
524 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
533 hres = get_length(ctx, vthis, ei, &jsthis, &length);
538 hres = set_length(jsthis, ei, 0);
545 V_VT(retv) = VT_EMPTY;
549 hres = jsdisp_get_idx(jsthis, 0, &ret, ei, caller);
550 if(hres == DISP_E_UNKNOWNNAME) {
551 V_VT(&ret) = VT_EMPTY;
555 for(i=1; SUCCEEDED(hres) && i<length; i++) {
556 hres = jsdisp_get_idx(jsthis, i, &v, ei, caller);
557 if(hres == DISP_E_UNKNOWNNAME)
558 hres = jsdisp_delete_idx(jsthis, i-1);
559 else if(SUCCEEDED(hres))
560 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei, caller);
563 if(SUCCEEDED(hres)) {
564 hres = jsdisp_delete_idx(jsthis, length-1);
566 hres = set_length(jsthis, ei, length-1);
569 if(SUCCEEDED(hres) && retv)
576 /* ECMA-262 3rd Edition 15.4.4.10 */
577 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
578 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
580 jsdisp_t *arr, *jsthis;
583 DWORD length, start, end, idx;
588 hres = get_length(ctx, vthis, ei, &jsthis, &length);
593 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
597 if(V_VT(&v) == VT_I4)
600 range = floor(V_R8(&v));
602 if(-range>length || isnan(range)) start = 0;
603 else if(range < 0) start = range+length;
604 else if(range <= length) start = range;
610 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
614 if(V_VT(&v) == VT_I4)
617 range = floor(V_R8(&v));
619 if(-range>length) end = 0;
620 else if(range < 0) end = range+length;
621 else if(range <= length) end = range;
626 hres = create_array(ctx, (end>start)?end-start:0, &arr);
630 for(idx=start; idx<end; idx++) {
631 hres = jsdisp_get_idx(jsthis, idx, &v, ei, sp);
632 if(hres == DISP_E_UNKNOWNNAME)
635 if(SUCCEEDED(hres)) {
636 hres = jsdisp_propput_idx(arr, idx-start, &v, ei, sp);
647 var_set_jsdisp(retv, arr);
654 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
655 IServiceProvider *caller, INT *cmp)
661 DISPPARAMS dp = {args, NULL, 2, 0};
668 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
672 hres = to_number(ctx, &res, ei, &tmp);
677 if(V_VT(&tmp) == VT_I4)
680 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
681 }else if(V_VT(v1) == VT_EMPTY) {
682 *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
683 }else if(V_VT(v2) == VT_EMPTY) {
685 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
686 DOUBLE d = num_val(v1)-num_val(v2);
690 *cmp = d < -0.0 ? -1 : 0;
694 hres = to_string(ctx, v1, ei, &x);
698 hres = to_string(ctx, v2, ei, &y);
699 if(SUCCEEDED(hres)) {
700 *cmp = strcmpW(x, y);
711 /* ECMA-262 3rd Edition 15.4.4.11 */
712 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
713 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
715 jsdisp_t *jsthis, *cmp_func = NULL;
716 VARIANT *vtab, **sorttab = NULL;
723 hres = get_length(ctx, vthis, ei, &jsthis, &length);
727 if(arg_cnt(dp) > 1) {
728 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
732 if(arg_cnt(dp) == 1) {
733 VARIANT *arg = get_arg(dp, 0);
735 if(V_VT(arg) != VT_DISPATCH) {
736 WARN("arg is not dispatch\n");
741 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
742 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
743 WARN("cmp_func is not a function\n");
745 jsdisp_release(cmp_func);
752 jsdisp_release(cmp_func);
754 jsdisp_addref(jsthis);
755 var_set_jsdisp(retv, jsthis);
760 vtab = heap_alloc_zero(length * sizeof(VARIANT));
762 for(i=0; i<length; i++) {
763 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei, caller);
764 if(hres == DISP_E_UNKNOWNNAME) {
765 V_VT(vtab+i) = VT_EMPTY;
767 } else if(FAILED(hres)) {
768 WARN("Could not get elem %d: %08x\n", i, hres);
773 hres = E_OUTOFMEMORY;
776 if(SUCCEEDED(hres)) {
777 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
779 hres = E_OUTOFMEMORY;
783 if(SUCCEEDED(hres)) {
784 VARIANT *tmpv, **tmpbuf;
787 tmpbuf = sorttab + length;
788 for(i=0; i < length; i++)
791 for(i=0; i < length/2; i++) {
792 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
798 sorttab[2*i] = sorttab[2*i+1];
799 sorttab[2*i+1] = tmpv;
803 if(SUCCEEDED(hres)) {
806 for(k=2; k < length; k *= 2) {
807 for(i=0; i+k < length; i += 2*k) {
812 bend = length - (i+k);
814 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
816 while(a < k && b < bend) {
817 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
822 sorttab[i+a+b] = tmpbuf[a];
825 sorttab[i+a+b] = sorttab[i+k+b];
834 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
842 for(i=0; SUCCEEDED(hres) && i < length; i++)
843 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei, caller);
847 for(i=0; i < length; i++)
848 VariantClear(vtab+i);
853 jsdisp_release(cmp_func);
859 jsdisp_addref(jsthis);
860 var_set_jsdisp(retv, jsthis);
866 /* ECMA-262 3rd Edition 15.4.4.12 */
867 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
868 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
870 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
871 jsdisp_t *ret_array = NULL, *jsthis;
877 hres = get_length(ctx, vthis, ei, &jsthis, &length);
883 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
887 if(V_VT(&v) == VT_I4) {
889 start = min(V_I4(&v), length);
891 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
893 start = V_R8(&v) < 0.0 ? 0 : length;
898 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
902 if(V_VT(&v) == VT_I4) {
904 delete_cnt = min(V_I4(&v), length-start);
905 }else if(V_R8(&v) > 0.0) {
906 delete_cnt = length-start;
913 hres = create_array(ctx, 0, &ret_array);
917 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
918 hres = jsdisp_get_idx(jsthis, start+i, &v, ei, caller);
919 if(hres == DISP_E_UNKNOWNNAME)
921 else if(SUCCEEDED(hres))
922 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
925 if(SUCCEEDED(hres)) {
927 V_I4(&v) = delete_cnt;
929 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
933 if(add_args < delete_cnt) {
934 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
935 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei, caller);
936 if(hres == DISP_E_UNKNOWNNAME)
937 hres = jsdisp_delete_idx(jsthis, i+add_args);
938 else if(SUCCEEDED(hres))
939 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
942 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
943 hres = jsdisp_delete_idx(jsthis, i-1);
944 }else if(add_args > delete_cnt) {
945 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
946 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
947 if(hres == DISP_E_UNKNOWNNAME)
948 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
949 else if(SUCCEEDED(hres))
950 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
954 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
955 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
957 if(SUCCEEDED(hres)) {
959 V_I4(&v) = length-delete_cnt+add_args;
960 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
965 jsdisp_release(ret_array);
970 var_set_jsdisp(retv, ret_array);
974 /* ECMA-262 3rd Edition 15.4.4.2 */
975 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
976 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
978 ArrayInstance *array;
982 array = array_this(jsthis);
984 return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
986 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
989 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
990 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
996 /* ECMA-262 3rd Edition 15.4.4.13 */
997 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
998 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1001 WCHAR buf[14], *buf_end, *str;
1002 DWORD argc, i, length;
1009 hres = get_length(ctx, vthis, ei, &jsthis, &length);
1015 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1020 str = idx_to_str(i, buf_end);
1022 hres = jsdisp_get_id(jsthis, str, 0, &id);
1023 if(SUCCEEDED(hres)) {
1024 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
1028 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
1030 }else if(hres == DISP_E_UNKNOWNNAME) {
1031 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1039 for(i=0; i<argc; i++) {
1040 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
1047 hres = set_length(jsthis, ei, length);
1053 if(ctx->version < 2) {
1054 V_VT(retv) = VT_EMPTY;
1057 V_I4(retv) = length;
1063 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1064 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1070 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1071 case INVOKE_PROPERTYGET:
1072 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1074 FIXME("unimplemented flags %x\n", flags);
1081 static void Array_destructor(jsdisp_t *dispex)
1086 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1088 ArrayInstance *array = (ArrayInstance*)dispex;
1089 const WCHAR *ptr = name;
1095 while(*ptr && isdigitW(*ptr)) {
1096 id = id*10 + (*ptr-'0');
1103 if(id >= array->length)
1104 array->length = id+1;
1107 static const builtin_prop_t Array_props[] = {
1108 {concatW, Array_concat, PROPF_METHOD|1},
1109 {joinW, Array_join, PROPF_METHOD|1},
1110 {lengthW, Array_length, 0},
1111 {popW, Array_pop, PROPF_METHOD},
1112 {pushW, Array_push, PROPF_METHOD|1},
1113 {reverseW, Array_reverse, PROPF_METHOD},
1114 {shiftW, Array_shift, PROPF_METHOD},
1115 {sliceW, Array_slice, PROPF_METHOD|2},
1116 {sortW, Array_sort, PROPF_METHOD|1},
1117 {spliceW, Array_splice, PROPF_METHOD|2},
1118 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1119 {toStringW, Array_toString, PROPF_METHOD},
1120 {unshiftW, Array_unshift, PROPF_METHOD|1},
1123 static const builtin_info_t Array_info = {
1125 {NULL, Array_value, 0},
1126 sizeof(Array_props)/sizeof(*Array_props),
1132 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1133 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1143 case DISPATCH_METHOD:
1144 case DISPATCH_CONSTRUCT: {
1145 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1146 if(V_I4(arg_var) < 0)
1147 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1149 hres = create_array(ctx, V_I4(arg_var), &obj);
1153 var_set_jsdisp(retv, obj);
1157 hres = create_array(ctx, arg_cnt(dp), &obj);
1161 for(i=0; i < arg_cnt(dp); i++) {
1162 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei, caller);
1167 jsdisp_release(obj);
1171 var_set_jsdisp(retv, obj);
1175 FIXME("unimplemented flags: %x\n", flags);
1182 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1184 ArrayInstance *array;
1187 array = heap_alloc_zero(sizeof(ArrayInstance));
1189 return E_OUTOFMEMORY;
1191 if(object_prototype)
1192 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1194 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1205 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1207 ArrayInstance *array;
1210 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1212 hres = alloc_array(ctx, object_prototype, &array);
1216 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1218 jsdisp_release(&array->dispex);
1222 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1224 ArrayInstance *array;
1227 hres = alloc_array(ctx, NULL, &array);
1231 array->length = length;
1233 *ret = &array->dispex;