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 forEachW
[] = {'f','o','r','E','a','c','h',0};
40 static const WCHAR joinW
[] = {'j','o','i','n',0};
41 static const WCHAR popW
[] = {'p','o','p',0};
42 static const WCHAR pushW
[] = {'p','u','s','h',0};
43 static const WCHAR reverseW
[] = {'r','e','v','e','r','s','e',0};
44 static const WCHAR shiftW
[] = {'s','h','i','f','t',0};
45 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
46 static const WCHAR sortW
[] = {'s','o','r','t',0};
47 static const WCHAR spliceW
[] = {'s','p','l','i','c','e',0};
48 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
49 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
50 static const WCHAR unshiftW
[] = {'u','n','s','h','i','f','t',0};
51 static const WCHAR indexOfW
[] = {'i','n','d','e','x','O','f',0};
53 static const WCHAR default_separatorW
[] = {',',0};
55 static inline ArrayInstance
*array_from_jsdisp(jsdisp_t
*jsdisp
)
57 return CONTAINING_RECORD(jsdisp
, ArrayInstance
, dispex
);
60 static inline ArrayInstance
*array_from_vdisp(vdisp_t
*vdisp
)
62 return array_from_jsdisp(vdisp
->u
.jsdisp
);
65 static inline ArrayInstance
*array_this(vdisp_t
*jsthis
)
67 return is_vclass(jsthis
, JSCLASS_ARRAY
) ? array_from_vdisp(jsthis
) : NULL
;
70 unsigned array_get_length(jsdisp_t
*array
)
72 assert(is_class(array
, JSCLASS_ARRAY
));
73 return array_from_jsdisp(array
)->length
;
76 static HRESULT
get_length(script_ctx_t
*ctx
, vdisp_t
*vdisp
, jsdisp_t
**jsthis
, DWORD
*ret
)
82 array
= array_this(vdisp
);
84 *jsthis
= &array
->dispex
;
90 return throw_type_error(ctx
, JS_E_JSCRIPT_EXPECTED
, NULL
);
92 hres
= jsdisp_propget_name(vdisp
->u
.jsdisp
, lengthW
, &val
);
96 hres
= to_uint32(ctx
, val
, ret
);
101 *jsthis
= vdisp
->u
.jsdisp
;
105 static HRESULT
set_length(jsdisp_t
*obj
, DWORD length
)
107 if(is_class(obj
, JSCLASS_ARRAY
)) {
108 array_from_jsdisp(obj
)->length
= length
;
112 return jsdisp_propput_name(obj
, lengthW
, jsval_number(length
));
115 static WCHAR
*idx_to_str(DWORD idx
, WCHAR
*ptr
)
123 *ptr
-- = '0' + (idx
%10);
130 static HRESULT
Array_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
132 TRACE("%p\n", jsthis
);
134 *r
= jsval_number(array_from_jsdisp(jsthis
)->length
);
138 static HRESULT
Array_set_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
140 ArrayInstance
*This
= array_from_jsdisp(jsthis
);
145 TRACE("%p %d\n", This
, This
->length
);
147 hres
= to_number(ctx
, value
, &len
);
153 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
155 for(i
=len
; i
< This
->length
; i
++) {
156 hres
= jsdisp_delete_idx(&This
->dispex
, i
);
165 static HRESULT
concat_array(jsdisp_t
*array
, ArrayInstance
*obj
, DWORD
*len
)
171 for(i
=0; i
< obj
->length
; i
++) {
172 hres
= jsdisp_get_idx(&obj
->dispex
, i
, &val
);
173 if(hres
== DISP_E_UNKNOWNNAME
)
178 hres
= jsdisp_propput_idx(array
, *len
+i
, val
);
188 static HRESULT
concat_obj(jsdisp_t
*array
, IDispatch
*obj
, DWORD
*len
)
193 jsobj
= iface_to_jsdisp(obj
);
195 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
196 hres
= concat_array(array
, array_from_jsdisp(jsobj
), len
);
197 jsdisp_release(jsobj
);
200 jsdisp_release(jsobj
);
203 return jsdisp_propput_idx(array
, (*len
)++, jsval_disp(obj
));
206 static HRESULT
Array_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
215 hres
= create_array(ctx
, 0, &ret
);
219 hres
= concat_obj(ret
, jsthis
->u
.disp
, &len
);
220 if(SUCCEEDED(hres
)) {
223 for(i
=0; i
< argc
; i
++) {
224 if(is_object_instance(argv
[i
]))
225 hres
= concat_obj(ret
, get_object(argv
[i
]), &len
);
227 hres
= jsdisp_propput_idx(ret
, len
++, argv
[i
]);
243 static HRESULT
array_join(script_ctx_t
*ctx
, jsdisp_t
*array
, DWORD length
, const WCHAR
*sep
,
244 unsigned seplen
, jsval_t
*r
)
246 jsstr_t
**str_tab
, *ret
= NULL
;
249 HRESULT hres
= E_FAIL
;
253 *r
= jsval_string(jsstr_empty());
257 str_tab
= heap_alloc_zero(length
* sizeof(*str_tab
));
259 return E_OUTOFMEMORY
;
261 for(i
=0; i
< length
; i
++) {
262 hres
= jsdisp_get_idx(array
, i
, &val
);
263 if(hres
== DISP_E_UNKNOWNNAME
) {
266 } else if(FAILED(hres
))
269 if(!is_undefined(val
) && !is_null(val
)) {
270 hres
= to_string(ctx
, val
, str_tab
+i
);
277 if(SUCCEEDED(hres
)) {
281 len
= jsstr_length(str_tab
[0]);
282 for(i
=1; i
< length
; i
++) {
285 len
+= jsstr_length(str_tab
[i
]);
286 if(len
> JSSTR_MAX_LENGTH
) {
287 hres
= E_OUTOFMEMORY
;
292 if(SUCCEEDED(hres
)) {
295 ret
= jsstr_alloc_buf(len
, &ptr
);
298 ptr
+= jsstr_flush(str_tab
[0], ptr
);
300 for(i
=1; i
< length
; i
++) {
302 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
307 ptr
+= jsstr_flush(str_tab
[i
], ptr
);
310 hres
= E_OUTOFMEMORY
;
315 for(i
=0; i
< length
; i
++) {
317 jsstr_release(str_tab
[i
]);
323 TRACE("= %s\n", debugstr_jsstr(ret
));
326 *r
= jsval_string(ret
);
332 /* ECMA-262 3rd Edition 15.4.4.5 */
333 static HRESULT
Array_join(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
342 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
350 hres
= to_flat_string(ctx
, argv
[0], &sep_str
, &sep
);
354 hres
= array_join(ctx
, jsthis
, length
, sep
, jsstr_length(sep_str
), r
);
356 jsstr_release(sep_str
);
358 hres
= array_join(ctx
, jsthis
, length
, default_separatorW
, strlenW(default_separatorW
), r
);
364 static HRESULT
Array_pop(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
374 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
379 hres
= set_length(jsthis
, 0);
384 *r
= jsval_undefined();
389 hres
= jsdisp_get_idx(jsthis
, length
, &val
);
391 hres
= jsdisp_delete_idx(jsthis
, length
);
392 else if(hres
== DISP_E_UNKNOWNNAME
) {
393 val
= jsval_undefined();
399 hres
= set_length(jsthis
, length
);
413 /* ECMA-262 3rd Edition 15.4.4.7 */
414 static HRESULT
Array_push(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
424 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
428 for(i
=0; i
< argc
; i
++) {
429 hres
= jsdisp_propput_idx(jsthis
, length
+i
, argv
[i
]);
434 hres
= set_length(jsthis
, length
+argc
);
439 *r
= jsval_number(length
+argc
);
443 static HRESULT
Array_reverse(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
449 HRESULT hres1
, hres2
;
453 hres1
= get_length(ctx
, vthis
, &jsthis
, &length
);
457 for(k
=0; k
<length
/2; k
++) {
460 hres1
= jsdisp_get_idx(jsthis
, k
, &v1
);
461 if(FAILED(hres1
) && hres1
!=DISP_E_UNKNOWNNAME
)
464 hres2
= jsdisp_get_idx(jsthis
, l
, &v2
);
465 if(FAILED(hres2
) && hres2
!=DISP_E_UNKNOWNNAME
) {
470 if(hres1
== DISP_E_UNKNOWNNAME
)
471 hres1
= jsdisp_delete_idx(jsthis
, l
);
473 hres1
= jsdisp_propput_idx(jsthis
, l
, v1
);
481 if(hres2
== DISP_E_UNKNOWNNAME
)
482 hres2
= jsdisp_delete_idx(jsthis
, k
);
484 hres2
= jsdisp_propput_idx(jsthis
, k
, v2
);
493 *r
= jsval_obj(jsdisp_addref(jsthis
));
497 /* ECMA-262 3rd Edition 15.4.4.9 */
498 static HRESULT
Array_shift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
508 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
513 hres
= set_length(jsthis
, 0);
518 *r
= jsval_undefined();
522 hres
= jsdisp_get_idx(jsthis
, 0, &ret
);
523 if(hres
== DISP_E_UNKNOWNNAME
) {
524 ret
= jsval_undefined();
528 for(i
=1; SUCCEEDED(hres
) && i
<length
; i
++) {
529 hres
= jsdisp_get_idx(jsthis
, i
, &v
);
530 if(hres
== DISP_E_UNKNOWNNAME
)
531 hres
= jsdisp_delete_idx(jsthis
, i
-1);
532 else if(SUCCEEDED(hres
))
533 hres
= jsdisp_propput_idx(jsthis
, i
-1, v
);
536 if(SUCCEEDED(hres
)) {
537 hres
= jsdisp_delete_idx(jsthis
, length
-1);
539 hres
= set_length(jsthis
, length
-1);
552 /* ECMA-262 3rd Edition 15.4.4.10 */
553 static HRESULT
Array_slice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
555 jsdisp_t
*arr
, *jsthis
;
557 DWORD length
, start
, end
, idx
;
562 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
567 hres
= to_number(ctx
, argv
[0], &range
);
571 range
= floor(range
);
572 if(-range
>length
|| isnan(range
)) start
= 0;
573 else if(range
< 0) start
= range
+length
;
574 else if(range
<= length
) start
= range
;
580 hres
= to_number(ctx
, argv
[1], &range
);
584 range
= floor(range
);
585 if(-range
>length
) end
= 0;
586 else if(range
< 0) end
= range
+length
;
587 else if(range
<= length
) end
= range
;
592 hres
= create_array(ctx
, (end
>start
)?end
-start
:0, &arr
);
596 for(idx
=start
; idx
<end
; idx
++) {
599 hres
= jsdisp_get_idx(jsthis
, idx
, &v
);
600 if(hres
== DISP_E_UNKNOWNNAME
)
603 if(SUCCEEDED(hres
)) {
604 hres
= jsdisp_propput_idx(arr
, idx
-start
, v
);
622 static HRESULT
sort_cmp(script_ctx_t
*ctx
, jsdisp_t
*cmp_func
, jsval_t v1
, jsval_t v2
, INT
*cmp
)
627 jsval_t args
[2] = {v1
, v2
};
631 hres
= jsdisp_call_value(cmp_func
, NULL
, DISPATCH_METHOD
, 2, args
, &res
);
635 hres
= to_number(ctx
, res
, &n
);
642 *cmp
= n
> 0.0 ? 1 : -1;
643 }else if(is_undefined(v1
)) {
644 *cmp
= is_undefined(v2
) ? 0 : 1;
645 }else if(is_undefined(v2
)) {
647 }else if(is_number(v1
) && is_number(v2
)) {
648 double d
= get_number(v1
)-get_number(v2
);
652 *cmp
= d
< -0.0 ? -1 : 0;
656 hres
= to_string(ctx
, v1
, &x
);
660 hres
= to_string(ctx
, v2
, &y
);
661 if(SUCCEEDED(hres
)) {
662 *cmp
= jsstr_cmp(x
, y
);
673 /* ECMA-262 3rd Edition 15.4.4.11 */
674 static HRESULT
Array_sort(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
677 jsdisp_t
*jsthis
, *cmp_func
= NULL
;
678 jsval_t
*vtab
, **sorttab
= NULL
;
685 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
690 WARN("invalid arg_cnt %d\n", argc
);
695 if(!is_object_instance(argv
[0])) {
696 WARN("arg is not dispatch\n");
700 cmp_func
= iface_to_jsdisp(get_object(argv
[0]));
701 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
702 WARN("cmp_func is not a function\n");
704 jsdisp_release(cmp_func
);
711 jsdisp_release(cmp_func
);
713 *r
= jsval_obj(jsdisp_addref(jsthis
));
717 vtab
= heap_alloc_zero(length
* sizeof(*vtab
));
719 for(i
=0; i
<length
; i
++) {
720 hres
= jsdisp_get_idx(jsthis
, i
, vtab
+i
);
721 if(hres
== DISP_E_UNKNOWNNAME
) {
722 vtab
[i
] = jsval_undefined();
724 } else if(FAILED(hres
)) {
725 WARN("Could not get elem %d: %08x\n", i
, hres
);
730 hres
= E_OUTOFMEMORY
;
733 if(SUCCEEDED(hres
)) {
734 sorttab
= heap_alloc(length
*2*sizeof(*sorttab
));
736 hres
= E_OUTOFMEMORY
;
740 if(SUCCEEDED(hres
)) {
741 jsval_t
*tmpv
, **tmpbuf
;
744 tmpbuf
= sorttab
+ length
;
745 for(i
=0; i
< length
; i
++)
748 for(i
=0; i
< length
/2; i
++) {
749 hres
= sort_cmp(ctx
, cmp_func
, *sorttab
[2*i
+1], *sorttab
[2*i
], &cmp
);
755 sorttab
[2*i
] = sorttab
[2*i
+1];
756 sorttab
[2*i
+1] = tmpv
;
760 if(SUCCEEDED(hres
)) {
763 for(k
=2; k
< length
; k
*= 2) {
764 for(i
=0; i
+k
< length
; i
+= 2*k
) {
769 bend
= length
- (i
+k
);
771 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(jsval_t
*));
773 while(a
< k
&& b
< bend
) {
774 hres
= sort_cmp(ctx
, cmp_func
, *tmpbuf
[a
], *sorttab
[i
+k
+b
], &cmp
);
779 sorttab
[i
+a
+b
] = tmpbuf
[a
];
782 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
791 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(jsval_t
*));
799 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
800 hres
= jsdisp_propput_idx(jsthis
, i
, *sorttab
[i
]);
804 for(i
=0; i
< length
; i
++)
805 jsval_release(vtab
[i
]);
810 jsdisp_release(cmp_func
);
816 *r
= jsval_obj(jsdisp_addref(jsthis
));
820 /* ECMA-262 3rd Edition 15.4.4.12 */
821 static HRESULT
Array_splice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
824 DWORD length
, start
=0, delete_cnt
=0, i
, add_args
= 0;
825 jsdisp_t
*ret_array
= NULL
, *jsthis
;
833 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
838 hres
= to_integer(ctx
, argv
[0], &d
);
844 start
= min(n
, length
);
846 start
= -n
> length
? 0 : length
+ n
;
848 start
= d
< 0.0 ? 0 : length
;
853 hres
= to_integer(ctx
, argv
[1], &d
);
859 delete_cnt
= min(n
, length
-start
);
861 delete_cnt
= length
-start
;
868 hres
= create_array(ctx
, 0, &ret_array
);
872 for(i
=0; SUCCEEDED(hres
) && i
< delete_cnt
; i
++) {
873 hres
= jsdisp_get_idx(jsthis
, start
+i
, &val
);
874 if(hres
== DISP_E_UNKNOWNNAME
) {
876 }else if(SUCCEEDED(hres
)) {
877 hres
= jsdisp_propput_idx(ret_array
, i
, val
);
883 hres
= jsdisp_propput_name(ret_array
, lengthW
, jsval_number(delete_cnt
));
886 if(add_args
< delete_cnt
) {
887 for(i
= start
; SUCCEEDED(hres
) && i
< length
-delete_cnt
; i
++) {
888 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
, &val
);
889 if(hres
== DISP_E_UNKNOWNNAME
) {
890 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
);
891 }else if(SUCCEEDED(hres
)) {
892 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
, val
);
897 for(i
=length
; SUCCEEDED(hres
) && i
!= length
-delete_cnt
+add_args
; i
--)
898 hres
= jsdisp_delete_idx(jsthis
, i
-1);
899 }else if(add_args
> delete_cnt
) {
900 for(i
=length
-delete_cnt
; SUCCEEDED(hres
) && i
!= start
; i
--) {
901 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
-1, &val
);
902 if(hres
== DISP_E_UNKNOWNNAME
) {
903 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
-1);
904 }else if(SUCCEEDED(hres
)) {
905 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
-1, val
);
911 for(i
=0; SUCCEEDED(hres
) && i
< add_args
; i
++)
912 hres
= jsdisp_propput_idx(jsthis
, start
+i
, argv
[i
+2]);
915 hres
= jsdisp_propput_name(jsthis
, lengthW
, jsval_number(length
-delete_cnt
+add_args
));
919 jsdisp_release(ret_array
);
924 *r
= jsval_obj(ret_array
);
928 /* ECMA-262 3rd Edition 15.4.4.2 */
929 static HRESULT
Array_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
932 ArrayInstance
*array
;
936 array
= array_this(jsthis
);
938 return throw_type_error(ctx
, JS_E_ARRAY_EXPECTED
, NULL
);
940 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
,
941 strlenW(default_separatorW
), r
);
944 static HRESULT
Array_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
951 static HRESULT
Array_forEach(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
954 jsval_t value
, args
[3], res
;
961 /* FIXME: Check IsCallable */
962 if(argc
!= 1 || !is_object_instance(argv
[0])) {
963 FIXME("Unsupported arguments\n");
967 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
971 for(i
= 0; i
< length
; i
++) {
972 hres
= jsdisp_get_idx(jsthis
, i
, &value
);
973 if(hres
== DISP_E_UNKNOWNNAME
)
979 args
[1] = jsval_number(i
);
980 args
[2] = jsval_obj(jsthis
);
981 hres
= disp_call_value(ctx
, get_object(argv
[0]), NULL
, DISPATCH_METHOD
, ARRAY_SIZE(args
), args
, &res
);
982 jsval_release(value
);
988 if(r
) *r
= jsval_undefined();
992 static HRESULT
Array_indexOf(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
996 unsigned length
, i
, from
= 0;
997 jsval_t search
, value
;
1003 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
1007 if(r
) *r
= jsval_number(-1);
1011 search
= argc
? argv
[0] : jsval_undefined();
1016 hres
= to_integer(ctx
, argv
[1], &from_arg
);
1021 from
= min(from_arg
, length
);
1023 from
= max(from_arg
+ length
, 0);
1026 for(i
= from
; i
< length
; i
++) {
1027 hres
= jsdisp_get_idx(jsthis
, i
, &value
);
1028 if(hres
== DISP_E_UNKNOWNNAME
)
1033 hres
= jsval_strict_equal(value
, search
, &eq
);
1034 jsval_release(value
);
1038 if(r
) *r
= jsval_number(i
);
1043 if(r
) *r
= jsval_number(-1);
1047 /* ECMA-262 3rd Edition 15.4.4.13 */
1048 static HRESULT
Array_unshift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1052 WCHAR buf
[14], *buf_end
, *str
;
1060 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
1065 buf_end
= buf
+ ARRAY_SIZE(buf
)-1;
1070 str
= idx_to_str(i
, buf_end
);
1072 hres
= jsdisp_get_id(jsthis
, str
, 0, &id
);
1073 if(SUCCEEDED(hres
)) {
1074 hres
= jsdisp_propget(jsthis
, id
, &val
);
1078 hres
= jsdisp_propput_idx(jsthis
, i
+argc
, val
);
1080 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1081 hres
= IDispatchEx_DeleteMemberByDispID(vthis
->u
.dispex
, id
);
1089 for(i
=0; i
<argc
; i
++) {
1090 hres
= jsdisp_propput_idx(jsthis
, i
, argv
[i
]);
1097 hres
= set_length(jsthis
, length
);
1103 *r
= ctx
->version
< 2 ? jsval_undefined() : jsval_number(length
);
1107 static HRESULT
Array_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1109 ArrayInstance
*array
= array_from_jsdisp(jsthis
);
1113 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
,
1114 strlenW(default_separatorW
), r
);
1117 static void Array_destructor(jsdisp_t
*dispex
)
1122 static void Array_on_put(jsdisp_t
*dispex
, const WCHAR
*name
)
1124 ArrayInstance
*array
= array_from_jsdisp(dispex
);
1125 const WCHAR
*ptr
= name
;
1131 while(*ptr
&& isdigitW(*ptr
)) {
1132 id
= id
*10 + (*ptr
-'0');
1139 if(id
>= array
->length
)
1140 array
->length
= id
+1;
1143 static const builtin_prop_t Array_props
[] = {
1144 {concatW
, Array_concat
, PROPF_METHOD
|1},
1145 {forEachW
, Array_forEach
, PROPF_METHOD
|PROPF_ES5
|1},
1146 {indexOfW
, Array_indexOf
, PROPF_METHOD
|PROPF_ES5
|1},
1147 {joinW
, Array_join
, PROPF_METHOD
|1},
1148 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
},
1149 {popW
, Array_pop
, PROPF_METHOD
},
1150 {pushW
, Array_push
, PROPF_METHOD
|1},
1151 {reverseW
, Array_reverse
, PROPF_METHOD
},
1152 {shiftW
, Array_shift
, PROPF_METHOD
},
1153 {sliceW
, Array_slice
, PROPF_METHOD
|2},
1154 {sortW
, Array_sort
, PROPF_METHOD
|1},
1155 {spliceW
, Array_splice
, PROPF_METHOD
|2},
1156 {toLocaleStringW
, Array_toLocaleString
, PROPF_METHOD
},
1157 {toStringW
, Array_toString
, PROPF_METHOD
},
1158 {unshiftW
, Array_unshift
, PROPF_METHOD
|1},
1161 static const builtin_info_t Array_info
= {
1163 {NULL
, NULL
,0, Array_get_value
},
1164 ARRAY_SIZE(Array_props
),
1170 static const builtin_prop_t ArrayInst_props
[] = {
1171 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
}
1174 static const builtin_info_t ArrayInst_info
= {
1176 {NULL
, NULL
,0, Array_get_value
},
1177 ARRAY_SIZE(ArrayInst_props
),
1183 /* ECMA-262 5.1 Edition 15.4.3.2 */
1184 static HRESULT
ArrayConstr_isArray(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1190 if(!argc
|| !is_object_instance(argv
[0])) {
1191 if(r
) *r
= jsval_bool(FALSE
);
1195 obj
= iface_to_jsdisp(get_object(argv
[0]));
1196 if(r
) *r
= jsval_bool(obj
&& is_class(obj
, JSCLASS_ARRAY
));
1197 if(obj
) jsdisp_release(obj
);
1201 static HRESULT
ArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1211 case DISPATCH_METHOD
:
1212 case DISPATCH_CONSTRUCT
: {
1213 if(argc
== 1 && is_number(argv
[0])) {
1214 double n
= get_number(argv
[0]);
1216 if(n
< 0 || !is_int32(n
))
1217 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
1219 hres
= create_array(ctx
, n
, &obj
);
1223 *r
= jsval_obj(obj
);
1227 hres
= create_array(ctx
, argc
, &obj
);
1231 for(i
=0; i
< argc
; i
++) {
1232 hres
= jsdisp_propput_idx(obj
, i
, argv
[i
]);
1237 jsdisp_release(obj
);
1241 *r
= jsval_obj(obj
);
1245 FIXME("unimplemented flags: %x\n", flags
);
1252 static HRESULT
alloc_array(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, ArrayInstance
**ret
)
1254 ArrayInstance
*array
;
1257 array
= heap_alloc_zero(sizeof(ArrayInstance
));
1259 return E_OUTOFMEMORY
;
1261 if(object_prototype
)
1262 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, object_prototype
);
1264 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &ArrayInst_info
, ctx
->array_constr
);
1275 static const WCHAR isArrayW
[] = {'i','s','A','r','r','a','y',0};
1277 static const builtin_prop_t ArrayConstr_props
[] = {
1278 {isArrayW
, ArrayConstr_isArray
, PROPF_ES5
|PROPF_METHOD
|1}
1281 static const builtin_info_t ArrayConstr_info
= {
1283 DEFAULT_FUNCTION_VALUE
,
1284 ARRAY_SIZE(ArrayConstr_props
),
1290 HRESULT
create_array_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1292 ArrayInstance
*array
;
1295 static const WCHAR ArrayW
[] = {'A','r','r','a','y',0};
1297 hres
= alloc_array(ctx
, object_prototype
, &array
);
1301 hres
= create_builtin_constructor(ctx
, ArrayConstr_value
, ArrayW
, &ArrayConstr_info
, PROPF_CONSTR
|1, &array
->dispex
, ret
);
1303 jsdisp_release(&array
->dispex
);
1307 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, jsdisp_t
**ret
)
1309 ArrayInstance
*array
;
1312 hres
= alloc_array(ctx
, NULL
, &array
);
1316 array
->length
= length
;
1318 *ret
= &array
->dispex
;