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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
33 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
34 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
35 static const WCHAR joinW[] = {'j','o','i','n',0};
36 static const WCHAR popW[] = {'p','o','p',0};
37 static const WCHAR pushW[] = {'p','u','s','h',0};
38 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
39 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
40 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
41 static const WCHAR sortW[] = {'s','o','r','t',0};
42 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
43 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
44 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
45 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
47 static const WCHAR default_separatorW[] = {',',0};
49 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
51 return (ArrayInstance*)vdisp->u.jsdisp;
54 static inline ArrayInstance *array_this(vdisp_t *jsthis)
56 return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
59 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsexcept_t *ei, jsdisp_t **jsthis, DWORD *ret)
65 array = array_this(vdisp);
67 *jsthis = &array->dispex;
73 return throw_type_error(ctx, ei, JS_E_JSCRIPT_EXPECTED, NULL);
75 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei);
79 hres = to_uint32(ctx, &var, ei, ret);
84 *jsthis = vdisp->u.jsdisp;
88 static HRESULT set_length(jsdisp_t *obj, jsexcept_t *ei, DWORD length)
92 if(is_class(obj, JSCLASS_ARRAY)) {
93 ((ArrayInstance*)obj)->length = length;
99 return jsdisp_propput_name(obj, lengthW, &var, ei);
102 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
110 *ptr-- = '0' + (idx%10);
117 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
118 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
120 ArrayInstance *This = array_from_vdisp(jsthis);
122 TRACE("%p %d\n", This, This->length);
125 case DISPATCH_PROPERTYGET:
127 V_I4(retv) = This->length;
129 case DISPATCH_PROPERTYPUT: {
135 hres = to_number(ctx, get_arg(dp, 0), ei, &num);
139 if(V_VT(&num) == VT_I4)
142 len = floor(V_R8(&num));
145 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
147 for(i=len; i<This->length; i++) {
148 hres = jsdisp_delete_idx(&This->dispex, i);
157 FIXME("unimplemented flags %x\n", flags);
164 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
170 for(i=0; i < obj->length; i++) {
171 hres = jsdisp_get_idx(&obj->dispex, i, &var, ei);
172 if(hres == DISP_E_UNKNOWNNAME)
177 hres = jsdisp_propput_idx(array, *len+i, &var, ei);
187 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
193 jsobj = iface_to_jsdisp((IUnknown*)obj);
195 if(is_class(jsobj, JSCLASS_ARRAY)) {
196 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei);
197 jsdisp_release(jsobj);
200 jsdisp_release(jsobj);
203 V_VT(&var) = VT_DISPATCH;
204 V_DISPATCH(&var) = obj;
205 return jsdisp_propput_idx(array, (*len)++, &var, ei);
208 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
209 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
217 hres = create_array(ctx, 0, &ret);
221 hres = concat_obj(ret, jsthis->u.disp, &len, ei);
222 if(SUCCEEDED(hres)) {
226 for(i=0; i < arg_cnt(dp); i++) {
227 arg = get_arg(dp, i);
228 if(V_VT(arg) == VT_DISPATCH)
229 hres = concat_obj(ret, V_DISPATCH(arg), &len, ei);
231 hres = jsdisp_propput_idx(ret, len++, arg, ei);
241 var_set_jsdisp(retv, ret);
247 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, VARIANT *retv,
248 jsexcept_t *ei, IServiceProvider *caller)
250 BSTR *str_tab, ret = NULL;
253 HRESULT hres = E_FAIL;
257 V_VT(retv) = VT_BSTR;
258 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
260 return E_OUTOFMEMORY;
265 str_tab = heap_alloc_zero(length * sizeof(BSTR));
267 return E_OUTOFMEMORY;
269 for(i=0; i < length; i++) {
270 hres = jsdisp_get_idx(array, i, &var, ei);
271 if(hres == DISP_E_UNKNOWNNAME) {
274 } else if(FAILED(hres))
277 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
278 hres = to_string(ctx, &var, ei, str_tab+i);
284 if(SUCCEEDED(hres)) {
285 DWORD seplen = 0, len = 0;
288 seplen = strlenW(sep);
291 len = SysStringLen(str_tab[0]);
292 for(i=1; i < length; i++)
293 len += seplen + SysStringLen(str_tab[i]);
295 ret = SysAllocStringLen(NULL, len);
300 tmplen = SysStringLen(str_tab[0]);
301 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
305 for(i=1; i < length; i++) {
307 memcpy(ptr, sep, seplen*sizeof(WCHAR));
312 tmplen = SysStringLen(str_tab[i]);
313 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
319 hres = E_OUTOFMEMORY;
323 for(i=0; i < length; i++)
324 SysFreeString(str_tab[i]);
329 TRACE("= %s\n", debugstr_w(ret));
333 ret = SysAllocStringLen(NULL, 0);
335 return E_OUTOFMEMORY;
338 V_VT(retv) = VT_BSTR;
347 /* ECMA-262 3rd Edition 15.4.4.5 */
348 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
349 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
357 hres = get_length(ctx, vthis, ei, &jsthis, &length);
364 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
368 hres = array_join(ctx, jsthis, length, sep, retv, ei, caller);
372 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei, caller);
378 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
379 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
388 hres = get_length(ctx, vthis, ei, &jsthis, &length);
393 hres = set_length(jsthis, ei, 0);
398 V_VT(retv) = VT_EMPTY;
403 hres = jsdisp_get_idx(jsthis, length, &val, ei);
404 if(SUCCEEDED(hres)) {
405 hres = jsdisp_delete_idx(jsthis, length);
406 } else if(hres == DISP_E_UNKNOWNNAME) {
407 V_VT(&val) = VT_EMPTY;
413 hres = set_length(jsthis, ei, length);
428 /* ECMA-262 3rd Edition 15.4.4.7 */
429 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
430 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
439 hres = get_length(ctx, vthis, ei, &jsthis, &length);
444 for(i=0; i < n; i++) {
445 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei);
450 hres = set_length(jsthis, ei, length+n);
456 V_I4(retv) = length+n;
461 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
462 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
467 HRESULT hres1, hres2;
471 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
475 for(k=0; k<length/2; k++) {
478 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei);
479 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
482 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
483 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
488 if(hres1 == DISP_E_UNKNOWNNAME)
489 hres1 = jsdisp_delete_idx(jsthis, l);
491 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei);
499 if(hres2 == DISP_E_UNKNOWNNAME)
500 hres2 = jsdisp_delete_idx(jsthis, k);
502 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei);
511 jsdisp_addref(jsthis);
512 var_set_jsdisp(retv, jsthis);
518 /* ECMA-262 3rd Edition 15.4.4.9 */
519 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
520 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
529 hres = get_length(ctx, vthis, ei, &jsthis, &length);
534 hres = set_length(jsthis, ei, 0);
541 V_VT(retv) = VT_EMPTY;
545 hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
546 if(hres == DISP_E_UNKNOWNNAME) {
547 V_VT(&ret) = VT_EMPTY;
551 for(i=1; SUCCEEDED(hres) && i<length; i++) {
552 hres = jsdisp_get_idx(jsthis, i, &v, ei);
553 if(hres == DISP_E_UNKNOWNNAME)
554 hres = jsdisp_delete_idx(jsthis, i-1);
555 else if(SUCCEEDED(hres))
556 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei);
559 if(SUCCEEDED(hres)) {
560 hres = jsdisp_delete_idx(jsthis, length-1);
562 hres = set_length(jsthis, ei, length-1);
565 if(SUCCEEDED(hres) && retv)
572 /* ECMA-262 3rd Edition 15.4.4.10 */
573 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
574 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
576 jsdisp_t *arr, *jsthis;
579 DWORD length, start, end, idx;
584 hres = get_length(ctx, vthis, ei, &jsthis, &length);
589 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
593 if(V_VT(&v) == VT_I4)
596 range = floor(V_R8(&v));
598 if(-range>length || isnan(range)) start = 0;
599 else if(range < 0) start = range+length;
600 else if(range <= length) start = range;
606 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
610 if(V_VT(&v) == VT_I4)
613 range = floor(V_R8(&v));
615 if(-range>length) end = 0;
616 else if(range < 0) end = range+length;
617 else if(range <= length) end = range;
622 hres = create_array(ctx, (end>start)?end-start:0, &arr);
626 for(idx=start; idx<end; idx++) {
627 hres = jsdisp_get_idx(jsthis, idx, &v, ei);
628 if(hres == DISP_E_UNKNOWNNAME)
631 if(SUCCEEDED(hres)) {
632 hres = jsdisp_propput_idx(arr, idx-start, &v, ei);
643 var_set_jsdisp(retv, arr);
650 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei, INT *cmp)
656 DISPPARAMS dp = {args, NULL, 2, 0};
663 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei);
667 hres = to_number(ctx, &res, ei, &tmp);
672 if(V_VT(&tmp) == VT_I4)
675 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
676 }else if(V_VT(v1) == VT_EMPTY) {
677 *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
678 }else if(V_VT(v2) == VT_EMPTY) {
680 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
681 DOUBLE d = num_val(v1)-num_val(v2);
685 *cmp = d < -0.0 ? -1 : 0;
689 hres = to_string(ctx, v1, ei, &x);
693 hres = to_string(ctx, v2, ei, &y);
694 if(SUCCEEDED(hres)) {
695 *cmp = strcmpW(x, y);
706 /* ECMA-262 3rd Edition 15.4.4.11 */
707 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
708 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
710 jsdisp_t *jsthis, *cmp_func = NULL;
711 VARIANT *vtab, **sorttab = NULL;
718 hres = get_length(ctx, vthis, ei, &jsthis, &length);
722 if(arg_cnt(dp) > 1) {
723 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
727 if(arg_cnt(dp) == 1) {
728 VARIANT *arg = get_arg(dp, 0);
730 if(V_VT(arg) != VT_DISPATCH) {
731 WARN("arg is not dispatch\n");
736 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
737 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
738 WARN("cmp_func is not a function\n");
740 jsdisp_release(cmp_func);
747 jsdisp_release(cmp_func);
749 jsdisp_addref(jsthis);
750 var_set_jsdisp(retv, jsthis);
755 vtab = heap_alloc_zero(length * sizeof(VARIANT));
757 for(i=0; i<length; i++) {
758 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei);
759 if(hres == DISP_E_UNKNOWNNAME) {
760 V_VT(vtab+i) = VT_EMPTY;
762 } else if(FAILED(hres)) {
763 WARN("Could not get elem %d: %08x\n", i, hres);
768 hres = E_OUTOFMEMORY;
771 if(SUCCEEDED(hres)) {
772 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
774 hres = E_OUTOFMEMORY;
778 if(SUCCEEDED(hres)) {
779 VARIANT *tmpv, **tmpbuf;
782 tmpbuf = sorttab + length;
783 for(i=0; i < length; i++)
786 for(i=0; i < length/2; i++) {
787 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, &cmp);
793 sorttab[2*i] = sorttab[2*i+1];
794 sorttab[2*i+1] = tmpv;
798 if(SUCCEEDED(hres)) {
801 for(k=2; k < length; k *= 2) {
802 for(i=0; i+k < length; i += 2*k) {
807 bend = length - (i+k);
809 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
811 while(a < k && b < bend) {
812 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, &cmp);
817 sorttab[i+a+b] = tmpbuf[a];
820 sorttab[i+a+b] = sorttab[i+k+b];
829 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
837 for(i=0; SUCCEEDED(hres) && i < length; i++)
838 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei);
842 for(i=0; i < length; i++)
843 VariantClear(vtab+i);
848 jsdisp_release(cmp_func);
854 jsdisp_addref(jsthis);
855 var_set_jsdisp(retv, jsthis);
861 /* ECMA-262 3rd Edition 15.4.4.12 */
862 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
863 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
865 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
866 jsdisp_t *ret_array = NULL, *jsthis;
872 hres = get_length(ctx, vthis, ei, &jsthis, &length);
878 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
882 if(V_VT(&v) == VT_I4) {
884 start = min(V_I4(&v), length);
886 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
888 start = V_R8(&v) < 0.0 ? 0 : length;
893 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
897 if(V_VT(&v) == VT_I4) {
899 delete_cnt = min(V_I4(&v), length-start);
900 }else if(V_R8(&v) > 0.0) {
901 delete_cnt = length-start;
908 hres = create_array(ctx, 0, &ret_array);
912 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
913 hres = jsdisp_get_idx(jsthis, start+i, &v, ei);
914 if(hres == DISP_E_UNKNOWNNAME)
916 else if(SUCCEEDED(hres))
917 hres = jsdisp_propput_idx(ret_array, i, &v, ei);
920 if(SUCCEEDED(hres)) {
922 V_I4(&v) = delete_cnt;
924 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei);
928 if(add_args < delete_cnt) {
929 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
930 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei);
931 if(hres == DISP_E_UNKNOWNNAME)
932 hres = jsdisp_delete_idx(jsthis, i+add_args);
933 else if(SUCCEEDED(hres))
934 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei);
937 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
938 hres = jsdisp_delete_idx(jsthis, i-1);
939 }else if(add_args > delete_cnt) {
940 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
941 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei);
942 if(hres == DISP_E_UNKNOWNNAME)
943 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
944 else if(SUCCEEDED(hres))
945 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei);
949 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
950 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei);
952 if(SUCCEEDED(hres)) {
954 V_I4(&v) = length-delete_cnt+add_args;
955 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei);
960 jsdisp_release(ret_array);
965 var_set_jsdisp(retv, ret_array);
969 /* ECMA-262 3rd Edition 15.4.4.2 */
970 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
971 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
973 ArrayInstance *array;
977 array = array_this(jsthis);
979 return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
981 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
984 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
985 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
991 /* ECMA-262 3rd Edition 15.4.4.13 */
992 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
993 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
996 WCHAR buf[14], *buf_end, *str;
997 DWORD argc, i, length;
1004 hres = get_length(ctx, vthis, ei, &jsthis, &length);
1010 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1015 str = idx_to_str(i, buf_end);
1017 hres = jsdisp_get_id(jsthis, str, 0, &id);
1018 if(SUCCEEDED(hres)) {
1019 hres = jsdisp_propget(jsthis, id, &var, ei);
1023 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei);
1025 }else if(hres == DISP_E_UNKNOWNNAME) {
1026 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1034 for(i=0; i<argc; i++) {
1035 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei);
1042 hres = set_length(jsthis, ei, length);
1048 if(ctx->version < 2) {
1049 V_VT(retv) = VT_EMPTY;
1052 V_I4(retv) = length;
1058 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1059 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1065 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1066 case INVOKE_PROPERTYGET:
1067 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1069 FIXME("unimplemented flags %x\n", flags);
1076 static void Array_destructor(jsdisp_t *dispex)
1081 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1083 ArrayInstance *array = (ArrayInstance*)dispex;
1084 const WCHAR *ptr = name;
1090 while(*ptr && isdigitW(*ptr)) {
1091 id = id*10 + (*ptr-'0');
1098 if(id >= array->length)
1099 array->length = id+1;
1102 static const builtin_prop_t Array_props[] = {
1103 {concatW, Array_concat, PROPF_METHOD|1},
1104 {joinW, Array_join, PROPF_METHOD|1},
1105 {lengthW, Array_length, 0},
1106 {popW, Array_pop, PROPF_METHOD},
1107 {pushW, Array_push, PROPF_METHOD|1},
1108 {reverseW, Array_reverse, PROPF_METHOD},
1109 {shiftW, Array_shift, PROPF_METHOD},
1110 {sliceW, Array_slice, PROPF_METHOD|2},
1111 {sortW, Array_sort, PROPF_METHOD|1},
1112 {spliceW, Array_splice, PROPF_METHOD|2},
1113 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1114 {toStringW, Array_toString, PROPF_METHOD},
1115 {unshiftW, Array_unshift, PROPF_METHOD|1},
1118 static const builtin_info_t Array_info = {
1120 {NULL, Array_value, 0},
1121 sizeof(Array_props)/sizeof(*Array_props),
1127 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1128 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1138 case DISPATCH_METHOD:
1139 case DISPATCH_CONSTRUCT: {
1140 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1141 if(V_I4(arg_var) < 0)
1142 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1144 hres = create_array(ctx, V_I4(arg_var), &obj);
1148 var_set_jsdisp(retv, obj);
1152 hres = create_array(ctx, arg_cnt(dp), &obj);
1156 for(i=0; i < arg_cnt(dp); i++) {
1157 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei);
1162 jsdisp_release(obj);
1166 var_set_jsdisp(retv, obj);
1170 FIXME("unimplemented flags: %x\n", flags);
1177 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1179 ArrayInstance *array;
1182 array = heap_alloc_zero(sizeof(ArrayInstance));
1184 return E_OUTOFMEMORY;
1186 if(object_prototype)
1187 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1189 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1200 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1202 ArrayInstance *array;
1205 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1207 hres = alloc_array(ctx, object_prototype, &array);
1211 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1213 jsdisp_release(&array->dispex);
1217 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1219 ArrayInstance *array;
1222 hres = alloc_array(ctx, NULL, &array);
1226 array->length = length;
1228 *ret = &array->dispex;