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, DispatchEx **jsthis, DWORD *ret)
65 array = array_this(vdisp);
67 *jsthis = &array->dispex;
72 if(!is_jsdisp(vdisp)) {
73 FIXME("Not JScript object\n");
77 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei, NULL/*FIXME*/);
81 hres = to_uint32(ctx, &var, ei, ret);
86 *jsthis = vdisp->u.jsdisp;
90 static HRESULT set_length(DispatchEx *obj, jsexcept_t *ei, DWORD length)
94 if(is_class(obj, JSCLASS_ARRAY)) {
95 ((ArrayInstance*)obj)->length = length;
101 return jsdisp_propput_name(obj, lengthW, &var, ei, NULL/*FIXME*/);
104 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
112 *ptr-- = '0' + (idx%10);
119 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
120 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
122 ArrayInstance *This = array_from_vdisp(jsthis);
124 TRACE("%p %d\n", This, This->length);
127 case DISPATCH_PROPERTYGET:
129 V_I4(retv) = This->length;
131 case DISPATCH_PROPERTYPUT: {
137 hres = to_number(ctx, get_arg(dp, 0), ei, &num);
138 if(V_VT(&num) == VT_I4)
141 len = floor(V_R8(&num));
144 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
146 for(i=len; i<This->length; i++) {
147 hres = jsdisp_delete_idx(&This->dispex, i);
156 FIXME("unimplemented flags %x\n", flags);
163 static HRESULT concat_array(DispatchEx *array, ArrayInstance *obj, DWORD *len,
164 jsexcept_t *ei, IServiceProvider *caller)
170 for(i=0; i < obj->length; i++) {
171 hres = jsdisp_propget_idx(&obj->dispex, i, &var, ei, caller);
172 if(hres == DISP_E_UNKNOWNNAME)
177 hres = jsdisp_propput_idx(array, *len+i, &var, ei, caller);
187 static HRESULT concat_obj(DispatchEx *array, IDispatch *obj, DWORD *len, jsexcept_t *ei, IServiceProvider *caller)
193 jsobj = iface_to_jsdisp((IUnknown*)obj);
195 if(is_class(jsobj, JSCLASS_ARRAY)) {
196 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei, caller);
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, caller);
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, caller);
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, caller);
231 hres = jsdisp_propput_idx(ret, len++, arg, ei, caller);
241 V_VT(retv) = VT_DISPATCH;
242 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
249 static HRESULT array_join(script_ctx_t *ctx, DispatchEx *array, DWORD length, const WCHAR *sep, VARIANT *retv,
250 jsexcept_t *ei, IServiceProvider *caller)
252 BSTR *str_tab, ret = NULL;
255 HRESULT hres = E_FAIL;
259 V_VT(retv) = VT_BSTR;
260 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
262 return E_OUTOFMEMORY;
267 str_tab = heap_alloc_zero(length * sizeof(BSTR));
269 return E_OUTOFMEMORY;
271 for(i=0; i < length; i++) {
272 hres = jsdisp_propget_idx(array, i, &var, ei, caller);
276 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
277 hres = to_string(ctx, &var, ei, str_tab+i);
283 if(SUCCEEDED(hres)) {
284 DWORD seplen = 0, len = 0;
287 seplen = strlenW(sep);
290 len = SysStringLen(str_tab[0]);
291 for(i=1; i < length; i++)
292 len += seplen + SysStringLen(str_tab[i]);
294 ret = SysAllocStringLen(NULL, len);
299 tmplen = SysStringLen(str_tab[0]);
300 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
304 for(i=1; i < length; i++) {
306 memcpy(ptr, sep, seplen*sizeof(WCHAR));
311 tmplen = SysStringLen(str_tab[i]);
312 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
318 hres = E_OUTOFMEMORY;
322 for(i=0; i < length; i++)
323 SysFreeString(str_tab[i]);
328 TRACE("= %s\n", debugstr_w(ret));
332 ret = SysAllocStringLen(NULL, 0);
334 return E_OUTOFMEMORY;
337 V_VT(retv) = VT_BSTR;
346 /* ECMA-262 3rd Edition 15.4.4.5 */
347 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
348 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
355 if(is_vclass(jsthis, JSCLASS_ARRAY)) {
356 length = array_from_vdisp(jsthis)->length;
358 FIXME("dispid is not Array\n");
365 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
369 hres = array_join(ctx, jsthis->u.jsdisp, length, sep, retv, ei, caller);
373 hres = array_join(ctx, jsthis->u.jsdisp, length, default_separatorW, retv, ei, caller);
379 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
380 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
388 static const WCHAR formatW[] = {'%','d',0};
392 if(is_vclass(jsthis, JSCLASS_ARRAY)) {
393 ArrayInstance *array = array_from_vdisp(jsthis);
394 length = array->length;
396 FIXME("not Array this\n");
402 V_VT(retv) = VT_EMPTY;
406 sprintfW(buf, formatW, --length);
407 hres = jsdisp_get_id(jsthis->u.jsdisp, buf, 0, &id);
408 if(SUCCEEDED(hres)) {
409 hres = jsdisp_propget(jsthis->u.jsdisp, id, &val, ei, caller);
413 hres = IDispatchEx_DeleteMemberByDispID(jsthis->u.dispex, id);
414 }else if(hres == DISP_E_UNKNOWNNAME) {
415 V_VT(&val) = VT_EMPTY;
421 if(SUCCEEDED(hres)) {
422 ArrayInstance *array = array_from_vdisp(jsthis);
423 array->length = length;
438 /* ECMA-262 3rd Edition 15.4.4.7 */
439 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
440 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
449 hres = get_length(ctx, vthis, ei, &jsthis, &length);
454 for(i=0; i < n; i++) {
455 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei, sp);
460 hres = set_length(jsthis, ei, length+n);
466 V_I4(retv) = length+n;
471 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
472 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
478 /* ECMA-262 3rd Edition 15.4.4.9 */
479 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
480 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
489 hres = get_length(ctx, vthis, ei, &jsthis, &length);
494 hres = set_length(jsthis, ei, 0);
501 V_VT(retv) = VT_EMPTY;
505 hres = jsdisp_propget_idx(jsthis, 0, &ret, ei, caller);
506 if(hres == DISP_E_UNKNOWNNAME) {
507 V_VT(&ret) = VT_EMPTY;
511 for(i=1; SUCCEEDED(hres) && i<length; i++) {
512 hres = jsdisp_propget_idx(jsthis, i, &v, ei, caller);
513 if(hres == DISP_E_UNKNOWNNAME)
514 hres = jsdisp_delete_idx(jsthis, i-1);
515 else if(SUCCEEDED(hres))
516 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei, caller);
519 if(SUCCEEDED(hres)) {
520 hres = jsdisp_delete_idx(jsthis, length-1);
522 hres = set_length(jsthis, ei, length-1);
525 if(SUCCEEDED(hres) && retv)
532 /* ECMA-262 3rd Edition 15.4.4.10 */
533 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
534 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
536 DispatchEx *arr, *jsthis;
539 DWORD length, start, end, idx;
544 hres = get_length(ctx, vthis, ei, &jsthis, &length);
549 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
553 if(V_VT(&v) == VT_I4)
556 range = floor(V_R8(&v));
558 if(-range>length || isnan(range)) start = 0;
559 else if(range < 0) start = range+length;
560 else if(range <= length) start = range;
566 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
570 if(V_VT(&v) == VT_I4)
573 range = floor(V_R8(&v));
575 if(-range>length) end = 0;
576 else if(range < 0) end = range+length;
577 else if(range <= length) end = range;
582 hres = create_array(ctx, (end>start)?end-start:0, &arr);
586 for(idx=start; idx<end; idx++) {
587 hres = jsdisp_propget_idx(jsthis, idx, &v, ei, sp);
588 if(hres == DISP_E_UNKNOWNNAME)
592 hres = jsdisp_propput_idx(arr, idx-start, &v, ei, sp);
601 V_VT(retv) = VT_DISPATCH;
602 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
610 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
611 IServiceProvider *caller, INT *cmp)
617 DISPPARAMS dp = {args, NULL, 2, 0};
624 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
628 hres = to_number(ctx, &res, ei, &tmp);
633 if(V_VT(&tmp) == VT_I4)
636 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
637 }else if(is_num_vt(V_VT(v1))) {
638 if(is_num_vt(V_VT(v2))) {
639 DOUBLE d = num_val(v1)-num_val(v2);
649 }else if(is_num_vt(V_VT(v2))) {
651 }else if(V_VT(v1) == VT_BSTR) {
652 if(V_VT(v2) == VT_BSTR)
653 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
656 }else if(V_VT(v2) == VT_BSTR) {
665 /* ECMA-262 3rd Edition 15.4.4.11 */
666 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
667 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
669 DispatchEx *cmp_func = NULL;
670 VARIANT *vtab, **sorttab = NULL;
677 if(is_vclass(jsthis, JSCLASS_ARRAY)) {
678 length = array_from_vdisp(jsthis)->length;
680 FIXME("unsupported this not array\n");
684 if(arg_cnt(dp) > 1) {
685 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
689 if(arg_cnt(dp) == 1) {
690 VARIANT *arg = get_arg(dp, 0);
692 if(V_VT(arg) != VT_DISPATCH) {
693 WARN("arg is not dispatch\n");
698 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
699 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
700 WARN("cmp_func is not a function\n");
702 jsdisp_release(cmp_func);
709 jsdisp_release(cmp_func);
711 V_VT(retv) = VT_DISPATCH;
712 V_DISPATCH(retv) = jsthis->u.disp;
713 IDispatch_AddRef(jsthis->u.disp);
718 vtab = heap_alloc_zero(length * sizeof(VARIANT));
720 for(i=0; i<length; i++) {
721 hres = jsdisp_propget_idx(jsthis->u.jsdisp, i, vtab+i, ei, caller);
722 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
723 WARN("Could not get elem %d: %08x\n", i, hres);
728 hres = E_OUTOFMEMORY;
731 if(SUCCEEDED(hres)) {
732 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
734 hres = E_OUTOFMEMORY;
738 if(SUCCEEDED(hres)) {
739 VARIANT *tmpv, **tmpbuf;
742 tmpbuf = sorttab + length;
743 for(i=0; i < length; i++)
746 for(i=0; i < length/2; i++) {
747 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
753 sorttab[2*i] = sorttab[2*i+1];
754 sorttab[2*i+1] = tmpv;
758 if(SUCCEEDED(hres)) {
761 for(k=2; k < length; k *= 2) {
762 for(i=0; i+k < length; i += 2*k) {
767 bend = length - (i+k);
769 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
771 while(a < k && b < bend) {
772 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
777 sorttab[i+a+b] = tmpbuf[a];
780 sorttab[i+a+b] = sorttab[i+k+b];
789 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
797 for(i=0; SUCCEEDED(hres) && i < length; i++)
798 hres = jsdisp_propput_idx(jsthis->u.jsdisp, i, sorttab[i], ei, caller);
802 for(i=0; i < length; i++)
803 VariantClear(vtab+i);
808 jsdisp_release(cmp_func);
814 V_VT(retv) = VT_DISPATCH;
815 V_DISPATCH(retv) = jsthis->u.disp;
816 IDispatch_AddRef(jsthis->u.disp);
822 /* ECMA-262 3rd Edition 15.4.4.12 */
823 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
824 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
826 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
827 DispatchEx *ret_array = NULL, *jsthis;
833 hres = get_length(ctx, vthis, ei, &jsthis, &length);
839 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
843 if(V_VT(&v) == VT_I4) {
845 start = min(V_I4(&v), length);
847 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
849 start = V_R8(&v) < 0.0 ? 0 : length;
854 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
858 if(V_VT(&v) == VT_I4) {
860 delete_cnt = min(V_I4(&v), length-start);
861 }else if(V_R8(&v) > 0.0) {
862 delete_cnt = length-start;
869 hres = create_array(ctx, 0, &ret_array);
873 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
874 hres = jsdisp_propget_idx(jsthis, start+i, &v, ei, caller);
875 if(hres == DISP_E_UNKNOWNNAME)
877 else if(SUCCEEDED(hres))
878 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
881 if(SUCCEEDED(hres)) {
883 V_I4(&v) = delete_cnt;
885 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
889 if(add_args < delete_cnt) {
890 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
891 hres = jsdisp_propget_idx(jsthis, i+delete_cnt, &v, ei, caller);
892 if(hres == DISP_E_UNKNOWNNAME)
893 hres = jsdisp_delete_idx(jsthis, i+add_args);
894 else if(SUCCEEDED(hres))
895 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
898 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
899 hres = jsdisp_delete_idx(jsthis, i-1);
900 }else if(add_args > delete_cnt) {
901 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
902 hres = jsdisp_propget_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
903 if(hres == DISP_E_UNKNOWNNAME)
904 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
905 else if(SUCCEEDED(hres))
906 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
910 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
911 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
913 if(SUCCEEDED(hres)) {
915 V_I4(&v) = length-delete_cnt+add_args;
916 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
921 jsdisp_release(ret_array);
926 V_VT(retv) = VT_DISPATCH;
927 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret_array);
932 /* ECMA-262 3rd Edition 15.4.4.2 */
933 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
934 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
936 ArrayInstance *array;
940 array = array_this(jsthis);
942 FIXME("not Array object\n");
946 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
949 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
950 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
956 /* ECMA-262 3rd Edition 15.4.4.13 */
957 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
958 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
961 WCHAR buf[14], *buf_end, *str;
962 DWORD argc, i, length;
969 hres = get_length(ctx, vthis, ei, &jsthis, &length);
976 V_VT(retv) = VT_EMPTY;
980 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
985 str = idx_to_str(i, buf_end);
987 hres = jsdisp_get_id(jsthis, str, 0, &id);
988 if(SUCCEEDED(hres)) {
989 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
993 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
995 }else if(hres == DISP_E_UNKNOWNNAME) {
996 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1003 for(i=0; i<argc; i++) {
1004 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
1009 hres = set_length(jsthis, ei, length+argc);
1014 V_VT(retv) = VT_EMPTY;
1018 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1019 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1025 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1026 case INVOKE_PROPERTYGET:
1027 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1029 FIXME("unimplemented flags %x\n", flags);
1036 static void Array_destructor(DispatchEx *dispex)
1041 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
1043 ArrayInstance *array = (ArrayInstance*)dispex;
1044 const WCHAR *ptr = name;
1050 while(*ptr && isdigitW(*ptr)) {
1051 id = id*10 + (*ptr-'0');
1058 if(id >= array->length)
1059 array->length = id+1;
1062 static const builtin_prop_t Array_props[] = {
1063 {concatW, Array_concat, PROPF_METHOD|1},
1064 {joinW, Array_join, PROPF_METHOD|1},
1065 {lengthW, Array_length, 0},
1066 {popW, Array_pop, PROPF_METHOD},
1067 {pushW, Array_push, PROPF_METHOD|1},
1068 {reverseW, Array_reverse, PROPF_METHOD},
1069 {shiftW, Array_shift, PROPF_METHOD},
1070 {sliceW, Array_slice, PROPF_METHOD|2},
1071 {sortW, Array_sort, PROPF_METHOD|1},
1072 {spliceW, Array_splice, PROPF_METHOD|2},
1073 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1074 {toStringW, Array_toString, PROPF_METHOD},
1075 {unshiftW, Array_unshift, PROPF_METHOD|1},
1078 static const builtin_info_t Array_info = {
1080 {NULL, Array_value, 0},
1081 sizeof(Array_props)/sizeof(*Array_props),
1087 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1088 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1098 case DISPATCH_METHOD:
1099 case DISPATCH_CONSTRUCT: {
1100 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1101 if(V_I4(arg_var) < 0)
1102 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
1104 hres = create_array(ctx, V_I4(arg_var), &obj);
1108 V_VT(retv) = VT_DISPATCH;
1109 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1113 hres = create_array(ctx, arg_cnt(dp), &obj);
1117 for(i=0; i < arg_cnt(dp); i++) {
1118 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei, caller);
1123 jsdisp_release(obj);
1127 V_VT(retv) = VT_DISPATCH;
1128 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1132 FIXME("unimplemented flags: %x\n", flags);
1139 static HRESULT alloc_array(script_ctx_t *ctx, DispatchEx *object_prototype, ArrayInstance **ret)
1141 ArrayInstance *array;
1144 array = heap_alloc_zero(sizeof(ArrayInstance));
1146 return E_OUTOFMEMORY;
1148 if(object_prototype)
1149 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1151 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1162 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1164 ArrayInstance *array;
1167 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1169 hres = alloc_array(ctx, object_prototype, &array);
1173 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR, &array->dispex, ret);
1175 jsdisp_release(&array->dispex);
1179 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
1181 ArrayInstance *array;
1184 hres = alloc_array(ctx, NULL, &array);
1188 array->length = length;
1190 *ret = &array->dispex;