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"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
37 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
38 static const WCHAR concatW
[] = {'c','o','n','c','a','t',0};
39 static const WCHAR joinW
[] = {'j','o','i','n',0};
40 static const WCHAR popW
[] = {'p','o','p',0};
41 static const WCHAR pushW
[] = {'p','u','s','h',0};
42 static const WCHAR reverseW
[] = {'r','e','v','e','r','s','e',0};
43 static const WCHAR shiftW
[] = {'s','h','i','f','t',0};
44 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
45 static const WCHAR sortW
[] = {'s','o','r','t',0};
46 static const WCHAR spliceW
[] = {'s','p','l','i','c','e',0};
47 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
48 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
49 static const WCHAR unshiftW
[] = {'u','n','s','h','i','f','t',0};
50 static const WCHAR indexOfW
[] = {'i','n','d','e','x','O','f',0};
52 static const WCHAR default_separatorW
[] = {',',0};
54 static inline ArrayInstance
*array_from_jsdisp(jsdisp_t
*jsdisp
)
56 return CONTAINING_RECORD(jsdisp
, ArrayInstance
, dispex
);
59 static inline ArrayInstance
*array_from_vdisp(vdisp_t
*vdisp
)
61 return array_from_jsdisp(vdisp
->u
.jsdisp
);
64 static inline ArrayInstance
*array_this(vdisp_t
*jsthis
)
66 return is_vclass(jsthis
, JSCLASS_ARRAY
) ? array_from_vdisp(jsthis
) : NULL
;
69 unsigned array_get_length(jsdisp_t
*array
)
71 assert(is_class(array
, JSCLASS_ARRAY
));
72 return array_from_jsdisp(array
)->length
;
75 static HRESULT
get_length(script_ctx_t
*ctx
, vdisp_t
*vdisp
, jsdisp_t
**jsthis
, DWORD
*ret
)
81 array
= array_this(vdisp
);
83 *jsthis
= &array
->dispex
;
89 return throw_type_error(ctx
, JS_E_JSCRIPT_EXPECTED
, NULL
);
91 hres
= jsdisp_propget_name(vdisp
->u
.jsdisp
, lengthW
, &val
);
95 hres
= to_uint32(ctx
, val
, ret
);
100 *jsthis
= vdisp
->u
.jsdisp
;
104 static HRESULT
set_length(jsdisp_t
*obj
, DWORD length
)
106 if(is_class(obj
, JSCLASS_ARRAY
)) {
107 array_from_jsdisp(obj
)->length
= length
;
111 return jsdisp_propput_name(obj
, lengthW
, jsval_number(length
));
114 static WCHAR
*idx_to_str(DWORD idx
, WCHAR
*ptr
)
122 *ptr
-- = '0' + (idx
%10);
129 static HRESULT
Array_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
131 TRACE("%p\n", jsthis
);
133 *r
= jsval_number(array_from_jsdisp(jsthis
)->length
);
137 static HRESULT
Array_set_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
139 ArrayInstance
*This
= array_from_jsdisp(jsthis
);
144 TRACE("%p %d\n", This
, This
->length
);
146 hres
= to_number(ctx
, value
, &len
);
152 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
154 for(i
=len
; i
< This
->length
; i
++) {
155 hres
= jsdisp_delete_idx(&This
->dispex
, i
);
164 static HRESULT
concat_array(jsdisp_t
*array
, ArrayInstance
*obj
, DWORD
*len
)
170 for(i
=0; i
< obj
->length
; i
++) {
171 hres
= jsdisp_get_idx(&obj
->dispex
, i
, &val
);
172 if(hres
== DISP_E_UNKNOWNNAME
)
177 hres
= jsdisp_propput_idx(array
, *len
+i
, val
);
187 static HRESULT
concat_obj(jsdisp_t
*array
, IDispatch
*obj
, DWORD
*len
)
192 jsobj
= iface_to_jsdisp(obj
);
194 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
195 hres
= concat_array(array
, array_from_jsdisp(jsobj
), len
);
196 jsdisp_release(jsobj
);
199 jsdisp_release(jsobj
);
202 return jsdisp_propput_idx(array
, (*len
)++, jsval_disp(obj
));
205 static HRESULT
Array_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
214 hres
= create_array(ctx
, 0, &ret
);
218 hres
= concat_obj(ret
, jsthis
->u
.disp
, &len
);
219 if(SUCCEEDED(hres
)) {
222 for(i
=0; i
< argc
; i
++) {
223 if(is_object_instance(argv
[i
]))
224 hres
= concat_obj(ret
, get_object(argv
[i
]), &len
);
226 hres
= jsdisp_propput_idx(ret
, len
++, argv
[i
]);
242 static HRESULT
array_join(script_ctx_t
*ctx
, jsdisp_t
*array
, DWORD length
, const WCHAR
*sep
,
243 unsigned seplen
, jsval_t
*r
)
245 jsstr_t
**str_tab
, *ret
= NULL
;
248 HRESULT hres
= E_FAIL
;
252 *r
= jsval_string(jsstr_empty());
256 str_tab
= heap_alloc_zero(length
* sizeof(*str_tab
));
258 return E_OUTOFMEMORY
;
260 for(i
=0; i
< length
; i
++) {
261 hres
= jsdisp_get_idx(array
, i
, &val
);
262 if(hres
== DISP_E_UNKNOWNNAME
) {
265 } else if(FAILED(hres
))
268 if(!is_undefined(val
) && !is_null(val
)) {
269 hres
= to_string(ctx
, val
, str_tab
+i
);
276 if(SUCCEEDED(hres
)) {
280 len
= jsstr_length(str_tab
[0]);
281 for(i
=1; i
< length
; i
++) {
284 len
+= jsstr_length(str_tab
[i
]);
285 if(len
> JSSTR_MAX_LENGTH
) {
286 hres
= E_OUTOFMEMORY
;
291 if(SUCCEEDED(hres
)) {
294 ret
= jsstr_alloc_buf(len
, &ptr
);
297 ptr
+= jsstr_flush(str_tab
[0], ptr
);
299 for(i
=1; i
< length
; i
++) {
301 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
306 ptr
+= jsstr_flush(str_tab
[i
], ptr
);
309 hres
= E_OUTOFMEMORY
;
314 for(i
=0; i
< length
; i
++) {
316 jsstr_release(str_tab
[i
]);
322 TRACE("= %s\n", debugstr_jsstr(ret
));
325 *r
= jsval_string(ret
);
331 /* ECMA-262 3rd Edition 15.4.4.5 */
332 static HRESULT
Array_join(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
341 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
349 hres
= to_flat_string(ctx
, argv
[0], &sep_str
, &sep
);
353 hres
= array_join(ctx
, jsthis
, length
, sep
, jsstr_length(sep_str
), r
);
355 jsstr_release(sep_str
);
357 hres
= array_join(ctx
, jsthis
, length
, default_separatorW
, strlenW(default_separatorW
), r
);
363 static HRESULT
Array_pop(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
373 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
378 hres
= set_length(jsthis
, 0);
383 *r
= jsval_undefined();
388 hres
= jsdisp_get_idx(jsthis
, length
, &val
);
390 hres
= jsdisp_delete_idx(jsthis
, length
);
391 else if(hres
== DISP_E_UNKNOWNNAME
) {
392 val
= jsval_undefined();
398 hres
= set_length(jsthis
, length
);
412 /* ECMA-262 3rd Edition 15.4.4.7 */
413 static HRESULT
Array_push(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
423 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
427 for(i
=0; i
< argc
; i
++) {
428 hres
= jsdisp_propput_idx(jsthis
, length
+i
, argv
[i
]);
433 hres
= set_length(jsthis
, length
+argc
);
438 *r
= jsval_number(length
+argc
);
442 static HRESULT
Array_reverse(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
448 HRESULT hres1
, hres2
;
452 hres1
= get_length(ctx
, vthis
, &jsthis
, &length
);
456 for(k
=0; k
<length
/2; k
++) {
459 hres1
= jsdisp_get_idx(jsthis
, k
, &v1
);
460 if(FAILED(hres1
) && hres1
!=DISP_E_UNKNOWNNAME
)
463 hres2
= jsdisp_get_idx(jsthis
, l
, &v2
);
464 if(FAILED(hres2
) && hres2
!=DISP_E_UNKNOWNNAME
) {
469 if(hres1
== DISP_E_UNKNOWNNAME
)
470 hres1
= jsdisp_delete_idx(jsthis
, l
);
472 hres1
= jsdisp_propput_idx(jsthis
, l
, v1
);
480 if(hres2
== DISP_E_UNKNOWNNAME
)
481 hres2
= jsdisp_delete_idx(jsthis
, k
);
483 hres2
= jsdisp_propput_idx(jsthis
, k
, v2
);
492 *r
= jsval_obj(jsdisp_addref(jsthis
));
496 /* ECMA-262 3rd Edition 15.4.4.9 */
497 static HRESULT
Array_shift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
507 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
512 hres
= set_length(jsthis
, 0);
517 *r
= jsval_undefined();
521 hres
= jsdisp_get_idx(jsthis
, 0, &ret
);
522 if(hres
== DISP_E_UNKNOWNNAME
) {
523 ret
= jsval_undefined();
527 for(i
=1; SUCCEEDED(hres
) && i
<length
; i
++) {
528 hres
= jsdisp_get_idx(jsthis
, i
, &v
);
529 if(hres
== DISP_E_UNKNOWNNAME
)
530 hres
= jsdisp_delete_idx(jsthis
, i
-1);
531 else if(SUCCEEDED(hres
))
532 hres
= jsdisp_propput_idx(jsthis
, i
-1, v
);
535 if(SUCCEEDED(hres
)) {
536 hres
= jsdisp_delete_idx(jsthis
, length
-1);
538 hres
= set_length(jsthis
, length
-1);
551 /* ECMA-262 3rd Edition 15.4.4.10 */
552 static HRESULT
Array_slice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
554 jsdisp_t
*arr
, *jsthis
;
556 DWORD length
, start
, end
, idx
;
561 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
566 hres
= to_number(ctx
, argv
[0], &range
);
570 range
= floor(range
);
571 if(-range
>length
|| isnan(range
)) start
= 0;
572 else if(range
< 0) start
= range
+length
;
573 else if(range
<= length
) start
= range
;
579 hres
= to_number(ctx
, argv
[1], &range
);
583 range
= floor(range
);
584 if(-range
>length
) end
= 0;
585 else if(range
< 0) end
= range
+length
;
586 else if(range
<= length
) end
= range
;
591 hres
= create_array(ctx
, (end
>start
)?end
-start
:0, &arr
);
595 for(idx
=start
; idx
<end
; idx
++) {
598 hres
= jsdisp_get_idx(jsthis
, idx
, &v
);
599 if(hres
== DISP_E_UNKNOWNNAME
)
602 if(SUCCEEDED(hres
)) {
603 hres
= jsdisp_propput_idx(arr
, idx
-start
, v
);
621 static HRESULT
sort_cmp(script_ctx_t
*ctx
, jsdisp_t
*cmp_func
, jsval_t v1
, jsval_t v2
, INT
*cmp
)
626 jsval_t args
[2] = {v1
, v2
};
630 hres
= jsdisp_call_value(cmp_func
, NULL
, DISPATCH_METHOD
, 2, args
, &res
);
634 hres
= to_number(ctx
, res
, &n
);
641 *cmp
= n
> 0.0 ? 1 : -1;
642 }else if(is_undefined(v1
)) {
643 *cmp
= is_undefined(v2
) ? 0 : 1;
644 }else if(is_undefined(v2
)) {
646 }else if(is_number(v1
) && is_number(v2
)) {
647 double d
= get_number(v1
)-get_number(v2
);
651 *cmp
= d
< -0.0 ? -1 : 0;
655 hres
= to_string(ctx
, v1
, &x
);
659 hres
= to_string(ctx
, v2
, &y
);
660 if(SUCCEEDED(hres
)) {
661 *cmp
= jsstr_cmp(x
, y
);
672 /* ECMA-262 3rd Edition 15.4.4.11 */
673 static HRESULT
Array_sort(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
676 jsdisp_t
*jsthis
, *cmp_func
= NULL
;
677 jsval_t
*vtab
, **sorttab
= NULL
;
684 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
689 WARN("invalid arg_cnt %d\n", argc
);
694 if(!is_object_instance(argv
[0])) {
695 WARN("arg is not dispatch\n");
699 cmp_func
= iface_to_jsdisp(get_object(argv
[0]));
700 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
701 WARN("cmp_func is not a function\n");
703 jsdisp_release(cmp_func
);
710 jsdisp_release(cmp_func
);
712 *r
= jsval_obj(jsdisp_addref(jsthis
));
716 vtab
= heap_alloc_zero(length
* sizeof(*vtab
));
718 for(i
=0; i
<length
; i
++) {
719 hres
= jsdisp_get_idx(jsthis
, i
, vtab
+i
);
720 if(hres
== DISP_E_UNKNOWNNAME
) {
721 vtab
[i
] = jsval_undefined();
723 } else if(FAILED(hres
)) {
724 WARN("Could not get elem %d: %08x\n", i
, hres
);
729 hres
= E_OUTOFMEMORY
;
732 if(SUCCEEDED(hres
)) {
733 sorttab
= heap_alloc(length
*2*sizeof(*sorttab
));
735 hres
= E_OUTOFMEMORY
;
739 if(SUCCEEDED(hres
)) {
740 jsval_t
*tmpv
, **tmpbuf
;
743 tmpbuf
= sorttab
+ length
;
744 for(i
=0; i
< length
; i
++)
747 for(i
=0; i
< length
/2; i
++) {
748 hres
= sort_cmp(ctx
, cmp_func
, *sorttab
[2*i
+1], *sorttab
[2*i
], &cmp
);
754 sorttab
[2*i
] = sorttab
[2*i
+1];
755 sorttab
[2*i
+1] = tmpv
;
759 if(SUCCEEDED(hres
)) {
762 for(k
=2; k
< length
; k
*= 2) {
763 for(i
=0; i
+k
< length
; i
+= 2*k
) {
768 bend
= length
- (i
+k
);
770 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(jsval_t
*));
772 while(a
< k
&& b
< bend
) {
773 hres
= sort_cmp(ctx
, cmp_func
, *tmpbuf
[a
], *sorttab
[i
+k
+b
], &cmp
);
778 sorttab
[i
+a
+b
] = tmpbuf
[a
];
781 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
790 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(jsval_t
*));
798 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
799 hres
= jsdisp_propput_idx(jsthis
, i
, *sorttab
[i
]);
803 for(i
=0; i
< length
; i
++)
804 jsval_release(vtab
[i
]);
809 jsdisp_release(cmp_func
);
815 *r
= jsval_obj(jsdisp_addref(jsthis
));
819 /* ECMA-262 3rd Edition 15.4.4.12 */
820 static HRESULT
Array_splice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
823 DWORD length
, start
=0, delete_cnt
=0, i
, add_args
= 0;
824 jsdisp_t
*ret_array
= NULL
, *jsthis
;
832 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
837 hres
= to_integer(ctx
, argv
[0], &d
);
843 start
= min(n
, length
);
845 start
= -n
> length
? 0 : length
+ n
;
847 start
= d
< 0.0 ? 0 : length
;
852 hres
= to_integer(ctx
, argv
[1], &d
);
858 delete_cnt
= min(n
, length
-start
);
860 delete_cnt
= length
-start
;
867 hres
= create_array(ctx
, 0, &ret_array
);
871 for(i
=0; SUCCEEDED(hres
) && i
< delete_cnt
; i
++) {
872 hres
= jsdisp_get_idx(jsthis
, start
+i
, &val
);
873 if(hres
== DISP_E_UNKNOWNNAME
) {
875 }else if(SUCCEEDED(hres
)) {
876 hres
= jsdisp_propput_idx(ret_array
, i
, val
);
882 hres
= jsdisp_propput_name(ret_array
, lengthW
, jsval_number(delete_cnt
));
885 if(add_args
< delete_cnt
) {
886 for(i
= start
; SUCCEEDED(hres
) && i
< length
-delete_cnt
; i
++) {
887 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
, &val
);
888 if(hres
== DISP_E_UNKNOWNNAME
) {
889 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
);
890 }else if(SUCCEEDED(hres
)) {
891 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
, val
);
896 for(i
=length
; SUCCEEDED(hres
) && i
!= length
-delete_cnt
+add_args
; i
--)
897 hres
= jsdisp_delete_idx(jsthis
, i
-1);
898 }else if(add_args
> delete_cnt
) {
899 for(i
=length
-delete_cnt
; SUCCEEDED(hres
) && i
!= start
; i
--) {
900 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
-1, &val
);
901 if(hres
== DISP_E_UNKNOWNNAME
) {
902 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
-1);
903 }else if(SUCCEEDED(hres
)) {
904 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
-1, val
);
910 for(i
=0; SUCCEEDED(hres
) && i
< add_args
; i
++)
911 hres
= jsdisp_propput_idx(jsthis
, start
+i
, argv
[i
+2]);
914 hres
= jsdisp_propput_name(jsthis
, lengthW
, jsval_number(length
-delete_cnt
+add_args
));
918 jsdisp_release(ret_array
);
923 *r
= jsval_obj(ret_array
);
927 /* ECMA-262 3rd Edition 15.4.4.2 */
928 static HRESULT
Array_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
931 ArrayInstance
*array
;
935 array
= array_this(jsthis
);
937 return throw_type_error(ctx
, JS_E_ARRAY_EXPECTED
, NULL
);
939 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
,
940 strlenW(default_separatorW
), r
);
943 static HRESULT
Array_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
950 static HRESULT
Array_indexOf(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
954 unsigned length
, i
, from
= 0;
955 jsval_t search
, value
;
961 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
965 if(r
) *r
= jsval_number(-1);
969 search
= argc
? argv
[0] : jsval_undefined();
974 hres
= to_integer(ctx
, argv
[1], &from_arg
);
979 from
= min(from_arg
, length
);
981 from
= max(from_arg
+ length
, 0);
984 for(i
= from
; i
< length
; i
++) {
985 hres
= jsdisp_get_idx(jsthis
, i
, &value
);
986 if(hres
== DISP_E_UNKNOWNNAME
)
991 hres
= jsval_strict_equal(value
, search
, &eq
);
992 jsval_release(value
);
996 if(r
) *r
= jsval_number(i
);
1001 if(r
) *r
= jsval_number(-1);
1005 /* ECMA-262 3rd Edition 15.4.4.13 */
1006 static HRESULT
Array_unshift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1010 WCHAR buf
[14], *buf_end
, *str
;
1018 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
1023 buf_end
= buf
+ sizeof(buf
)/sizeof(WCHAR
)-1;
1028 str
= idx_to_str(i
, buf_end
);
1030 hres
= jsdisp_get_id(jsthis
, str
, 0, &id
);
1031 if(SUCCEEDED(hres
)) {
1032 hres
= jsdisp_propget(jsthis
, id
, &val
);
1036 hres
= jsdisp_propput_idx(jsthis
, i
+argc
, val
);
1038 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1039 hres
= IDispatchEx_DeleteMemberByDispID(vthis
->u
.dispex
, id
);
1047 for(i
=0; i
<argc
; i
++) {
1048 hres
= jsdisp_propput_idx(jsthis
, i
, argv
[i
]);
1055 hres
= set_length(jsthis
, length
);
1061 *r
= ctx
->version
< 2 ? jsval_undefined() : jsval_number(length
);
1065 static HRESULT
Array_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1067 ArrayInstance
*array
= array_from_jsdisp(jsthis
);
1071 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
,
1072 strlenW(default_separatorW
), r
);
1075 static void Array_destructor(jsdisp_t
*dispex
)
1080 static void Array_on_put(jsdisp_t
*dispex
, const WCHAR
*name
)
1082 ArrayInstance
*array
= array_from_jsdisp(dispex
);
1083 const WCHAR
*ptr
= name
;
1089 while(*ptr
&& isdigitW(*ptr
)) {
1090 id
= id
*10 + (*ptr
-'0');
1097 if(id
>= array
->length
)
1098 array
->length
= id
+1;
1101 static const builtin_prop_t Array_props
[] = {
1102 {concatW
, Array_concat
, PROPF_METHOD
|1},
1103 {indexOfW
, Array_indexOf
, PROPF_ES5
|PROPF_METHOD
|1},
1104 {joinW
, Array_join
, PROPF_METHOD
|1},
1105 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
},
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
, NULL
,0, Array_get_value
},
1121 sizeof(Array_props
)/sizeof(*Array_props
),
1127 static const builtin_prop_t ArrayInst_props
[] = {
1128 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
}
1131 static const builtin_info_t ArrayInst_info
= {
1133 {NULL
, NULL
,0, Array_get_value
},
1134 sizeof(ArrayInst_props
)/sizeof(*ArrayInst_props
),
1140 /* ECMA-262 5.1 Edition 15.4.3.2 */
1141 static HRESULT
ArrayConstr_isArray(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1147 if(!argc
|| !is_object_instance(argv
[0])) {
1148 if(r
) *r
= jsval_bool(FALSE
);
1152 obj
= iface_to_jsdisp(get_object(argv
[0]));
1153 if(r
) *r
= jsval_bool(obj
&& is_class(obj
, JSCLASS_ARRAY
));
1154 if(obj
) jsdisp_release(obj
);
1158 static HRESULT
ArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1168 case DISPATCH_METHOD
:
1169 case DISPATCH_CONSTRUCT
: {
1170 if(argc
== 1 && is_number(argv
[0])) {
1171 double n
= get_number(argv
[0]);
1173 if(n
< 0 || !is_int32(n
))
1174 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
1176 hres
= create_array(ctx
, n
, &obj
);
1180 *r
= jsval_obj(obj
);
1184 hres
= create_array(ctx
, argc
, &obj
);
1188 for(i
=0; i
< argc
; i
++) {
1189 hres
= jsdisp_propput_idx(obj
, i
, argv
[i
]);
1194 jsdisp_release(obj
);
1198 *r
= jsval_obj(obj
);
1202 FIXME("unimplemented flags: %x\n", flags
);
1209 static HRESULT
alloc_array(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, ArrayInstance
**ret
)
1211 ArrayInstance
*array
;
1214 array
= heap_alloc_zero(sizeof(ArrayInstance
));
1216 return E_OUTOFMEMORY
;
1218 if(object_prototype
)
1219 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, object_prototype
);
1221 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &ArrayInst_info
, ctx
->array_constr
);
1232 static const WCHAR isArrayW
[] = {'i','s','A','r','r','a','y',0};
1234 static const builtin_prop_t ArrayConstr_props
[] = {
1235 {isArrayW
, ArrayConstr_isArray
, PROPF_ES5
|PROPF_METHOD
|1}
1238 static const builtin_info_t ArrayConstr_info
= {
1240 DEFAULT_FUNCTION_VALUE
,
1241 sizeof(ArrayConstr_props
)/sizeof(*ArrayConstr_props
),
1247 HRESULT
create_array_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1249 ArrayInstance
*array
;
1252 static const WCHAR ArrayW
[] = {'A','r','r','a','y',0};
1254 hres
= alloc_array(ctx
, object_prototype
, &array
);
1258 hres
= create_builtin_constructor(ctx
, ArrayConstr_value
, ArrayW
, &ArrayConstr_info
, PROPF_CONSTR
|1, &array
->dispex
, ret
);
1260 jsdisp_release(&array
->dispex
);
1264 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, jsdisp_t
**ret
)
1266 ArrayInstance
*array
;
1269 hres
= alloc_array(ctx
, NULL
, &array
);
1273 array
->length
= length
;
1275 *ret
= &array
->dispex
;