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
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
35 static inline ArrayInstance
*array_from_jsdisp(jsdisp_t
*jsdisp
)
37 return CONTAINING_RECORD(jsdisp
, ArrayInstance
, dispex
);
40 static inline ArrayInstance
*array_from_vdisp(vdisp_t
*vdisp
)
42 return array_from_jsdisp(vdisp
->u
.jsdisp
);
45 static inline ArrayInstance
*array_this(vdisp_t
*jsthis
)
47 return is_vclass(jsthis
, JSCLASS_ARRAY
) ? array_from_vdisp(jsthis
) : NULL
;
50 unsigned array_get_length(jsdisp_t
*array
)
52 assert(is_class(array
, JSCLASS_ARRAY
));
53 return array_from_jsdisp(array
)->length
;
56 static HRESULT
get_length(script_ctx_t
*ctx
, vdisp_t
*vdisp
, jsdisp_t
**jsthis
, DWORD
*ret
)
62 array
= array_this(vdisp
);
64 *jsthis
= &array
->dispex
;
70 return JS_E_JSCRIPT_EXPECTED
;
72 hres
= jsdisp_propget_name(vdisp
->u
.jsdisp
, L
"length", &val
);
76 hres
= to_uint32(ctx
, val
, ret
);
81 *jsthis
= vdisp
->u
.jsdisp
;
85 static HRESULT
set_length(jsdisp_t
*obj
, DWORD length
)
87 if(is_class(obj
, JSCLASS_ARRAY
)) {
88 array_from_jsdisp(obj
)->length
= length
;
92 return jsdisp_propput_name(obj
, L
"length", jsval_number(length
));
95 static WCHAR
*idx_to_str(DWORD idx
, WCHAR
*ptr
)
103 *ptr
-- = '0' + (idx
%10);
110 static HRESULT
Array_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
112 TRACE("%p\n", jsthis
);
114 *r
= jsval_number(array_from_jsdisp(jsthis
)->length
);
118 static HRESULT
Array_set_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
120 ArrayInstance
*This
= array_from_jsdisp(jsthis
);
125 TRACE("%p %d\n", This
, This
->length
);
127 hres
= to_number(ctx
, value
, &len
);
133 return JS_E_INVALID_LENGTH
;
135 for(i
=len
; i
< This
->length
; i
++) {
136 hres
= jsdisp_delete_idx(&This
->dispex
, i
);
145 static HRESULT
concat_array(jsdisp_t
*array
, ArrayInstance
*obj
, DWORD
*len
)
151 for(i
=0; i
< obj
->length
; i
++) {
152 hres
= jsdisp_get_idx(&obj
->dispex
, i
, &val
);
153 if(hres
== DISP_E_UNKNOWNNAME
)
158 hres
= jsdisp_propput_idx(array
, *len
+i
, val
);
168 static HRESULT
concat_obj(jsdisp_t
*array
, IDispatch
*obj
, DWORD
*len
)
173 jsobj
= iface_to_jsdisp(obj
);
175 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
176 hres
= concat_array(array
, array_from_jsdisp(jsobj
), len
);
177 jsdisp_release(jsobj
);
180 jsdisp_release(jsobj
);
183 return jsdisp_propput_idx(array
, (*len
)++, jsval_disp(obj
));
186 static HRESULT
Array_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
195 hres
= create_array(ctx
, 0, &ret
);
199 hres
= concat_obj(ret
, jsthis
->u
.disp
, &len
);
200 if(SUCCEEDED(hres
)) {
203 for(i
=0; i
< argc
; i
++) {
204 if(is_object_instance(argv
[i
]))
205 hres
= concat_obj(ret
, get_object(argv
[i
]), &len
);
207 hres
= jsdisp_propput_idx(ret
, len
++, argv
[i
]);
223 static HRESULT
array_join(script_ctx_t
*ctx
, jsdisp_t
*array
, DWORD length
, const WCHAR
*sep
,
224 unsigned seplen
, jsval_t
*r
)
226 jsstr_t
**str_tab
, *ret
= NULL
;
229 HRESULT hres
= E_FAIL
;
233 *r
= jsval_string(jsstr_empty());
237 str_tab
= heap_alloc_zero(length
* sizeof(*str_tab
));
239 return E_OUTOFMEMORY
;
241 for(i
=0; i
< length
; i
++) {
242 hres
= jsdisp_get_idx(array
, i
, &val
);
243 if(hres
== DISP_E_UNKNOWNNAME
) {
246 } else if(FAILED(hres
))
249 if(!is_undefined(val
) && !is_null(val
)) {
250 hres
= to_string(ctx
, val
, str_tab
+i
);
257 if(SUCCEEDED(hres
)) {
261 len
= jsstr_length(str_tab
[0]);
262 for(i
=1; i
< length
; i
++) {
265 len
+= jsstr_length(str_tab
[i
]);
266 if(len
> JSSTR_MAX_LENGTH
) {
267 hres
= E_OUTOFMEMORY
;
272 if(SUCCEEDED(hres
)) {
275 ret
= jsstr_alloc_buf(len
, &ptr
);
278 ptr
+= jsstr_flush(str_tab
[0], ptr
);
280 for(i
=1; i
< length
; i
++) {
282 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
287 ptr
+= jsstr_flush(str_tab
[i
], ptr
);
290 hres
= E_OUTOFMEMORY
;
295 for(i
=0; i
< length
; i
++) {
297 jsstr_release(str_tab
[i
]);
303 TRACE("= %s\n", debugstr_jsstr(ret
));
306 *r
= jsval_string(ret
);
312 /* ECMA-262 3rd Edition 15.4.4.5 */
313 static HRESULT
Array_join(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
322 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
330 hres
= to_flat_string(ctx
, argv
[0], &sep_str
, &sep
);
334 hres
= array_join(ctx
, jsthis
, length
, sep
, jsstr_length(sep_str
), r
);
336 jsstr_release(sep_str
);
338 hres
= array_join(ctx
, jsthis
, length
, L
",", 1, r
);
344 static HRESULT
Array_pop(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
354 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
359 hres
= set_length(jsthis
, 0);
364 *r
= jsval_undefined();
369 hres
= jsdisp_get_idx(jsthis
, length
, &val
);
371 hres
= jsdisp_delete_idx(jsthis
, length
);
372 else if(hres
== DISP_E_UNKNOWNNAME
) {
373 val
= jsval_undefined();
379 hres
= set_length(jsthis
, length
);
393 /* ECMA-262 3rd Edition 15.4.4.7 */
394 static HRESULT
Array_push(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
404 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
408 for(i
=0; i
< argc
; i
++) {
409 hres
= jsdisp_propput_idx(jsthis
, length
+i
, argv
[i
]);
414 hres
= set_length(jsthis
, length
+argc
);
419 *r
= jsval_number(length
+argc
);
423 static HRESULT
Array_reverse(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
429 HRESULT hres1
, hres2
;
433 hres1
= get_length(ctx
, vthis
, &jsthis
, &length
);
437 for(k
=0; k
<length
/2; k
++) {
440 hres1
= jsdisp_get_idx(jsthis
, k
, &v1
);
441 if(FAILED(hres1
) && hres1
!=DISP_E_UNKNOWNNAME
)
444 hres2
= jsdisp_get_idx(jsthis
, l
, &v2
);
445 if(FAILED(hres2
) && hres2
!=DISP_E_UNKNOWNNAME
) {
450 if(hres1
== DISP_E_UNKNOWNNAME
)
451 hres1
= jsdisp_delete_idx(jsthis
, l
);
453 hres1
= jsdisp_propput_idx(jsthis
, l
, v1
);
461 if(hres2
== DISP_E_UNKNOWNNAME
)
462 hres2
= jsdisp_delete_idx(jsthis
, k
);
464 hres2
= jsdisp_propput_idx(jsthis
, k
, v2
);
473 *r
= jsval_obj(jsdisp_addref(jsthis
));
477 /* ECMA-262 3rd Edition 15.4.4.9 */
478 static HRESULT
Array_shift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
488 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
493 hres
= set_length(jsthis
, 0);
498 *r
= jsval_undefined();
502 hres
= jsdisp_get_idx(jsthis
, 0, &ret
);
503 if(hres
== DISP_E_UNKNOWNNAME
) {
504 ret
= jsval_undefined();
508 for(i
=1; SUCCEEDED(hres
) && i
<length
; i
++) {
509 hres
= jsdisp_get_idx(jsthis
, i
, &v
);
510 if(hres
== DISP_E_UNKNOWNNAME
)
511 hres
= jsdisp_delete_idx(jsthis
, i
-1);
512 else if(SUCCEEDED(hres
))
513 hres
= jsdisp_propput_idx(jsthis
, i
-1, v
);
516 if(SUCCEEDED(hres
)) {
517 hres
= jsdisp_delete_idx(jsthis
, length
-1);
519 hres
= set_length(jsthis
, length
-1);
532 /* ECMA-262 3rd Edition 15.4.4.10 */
533 static HRESULT
Array_slice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
535 jsdisp_t
*arr
, *jsthis
;
537 DWORD length
, start
, end
, idx
;
542 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
547 hres
= to_number(ctx
, argv
[0], &range
);
551 range
= floor(range
);
552 if(-range
>length
|| isnan(range
)) start
= 0;
553 else if(range
< 0) start
= range
+length
;
554 else if(range
<= length
) start
= range
;
560 hres
= to_number(ctx
, argv
[1], &range
);
564 range
= floor(range
);
565 if(-range
>length
) end
= 0;
566 else if(range
< 0) end
= range
+length
;
567 else if(range
<= length
) end
= range
;
572 hres
= create_array(ctx
, (end
>start
)?end
-start
:0, &arr
);
576 for(idx
=start
; idx
<end
; idx
++) {
579 hres
= jsdisp_get_idx(jsthis
, idx
, &v
);
580 if(hres
== DISP_E_UNKNOWNNAME
)
583 if(SUCCEEDED(hres
)) {
584 hres
= jsdisp_propput_idx(arr
, idx
-start
, v
);
602 static HRESULT
sort_cmp(script_ctx_t
*ctx
, jsdisp_t
*cmp_func
, jsval_t v1
, jsval_t v2
, INT
*cmp
)
607 jsval_t args
[2] = {v1
, v2
};
611 hres
= jsdisp_call_value(cmp_func
, NULL
, DISPATCH_METHOD
, 2, args
, &res
);
615 hres
= to_number(ctx
, res
, &n
);
622 *cmp
= n
> 0.0 ? 1 : -1;
623 }else if(is_undefined(v1
)) {
624 *cmp
= is_undefined(v2
) ? 0 : 1;
625 }else if(is_undefined(v2
)) {
627 }else if(is_number(v1
) && is_number(v2
)) {
628 double d
= get_number(v1
)-get_number(v2
);
632 *cmp
= d
< -0.0 ? -1 : 0;
636 hres
= to_string(ctx
, v1
, &x
);
640 hres
= to_string(ctx
, v2
, &y
);
641 if(SUCCEEDED(hres
)) {
642 *cmp
= jsstr_cmp(x
, y
);
653 /* ECMA-262 3rd Edition 15.4.4.11 */
654 static HRESULT
Array_sort(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
657 jsdisp_t
*jsthis
, *cmp_func
= NULL
;
658 jsval_t
*vtab
, **sorttab
= NULL
;
665 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
670 if(is_object_instance(argv
[0])) {
671 if(argc
> 1 && ctx
->version
< SCRIPTLANGUAGEVERSION_ES5
) {
672 WARN("invalid arg_cnt %d\n", argc
);
673 return JS_E_JSCRIPT_EXPECTED
;
675 cmp_func
= iface_to_jsdisp(get_object(argv
[0]));
676 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
677 WARN("cmp_func is not a function\n");
679 jsdisp_release(cmp_func
);
680 return JS_E_JSCRIPT_EXPECTED
;
682 }else if(ctx
->version
>= SCRIPTLANGUAGEVERSION_ES5
? !is_undefined(argv
[0]) : !is_null(argv
[0])) {
683 WARN("invalid arg %s\n", debugstr_jsval(argv
[0]));
684 return JS_E_JSCRIPT_EXPECTED
;
690 jsdisp_release(cmp_func
);
692 *r
= jsval_obj(jsdisp_addref(jsthis
));
696 vtab
= heap_alloc_zero(length
* sizeof(*vtab
));
698 for(i
=0; i
<length
; i
++) {
699 hres
= jsdisp_get_idx(jsthis
, i
, vtab
+i
);
700 if(hres
== DISP_E_UNKNOWNNAME
) {
701 vtab
[i
] = jsval_undefined();
703 } else if(FAILED(hres
)) {
704 WARN("Could not get elem %d: %08x\n", i
, hres
);
709 hres
= E_OUTOFMEMORY
;
712 if(SUCCEEDED(hres
)) {
713 sorttab
= heap_alloc(length
*2*sizeof(*sorttab
));
715 hres
= E_OUTOFMEMORY
;
719 if(SUCCEEDED(hres
)) {
720 jsval_t
*tmpv
, **tmpbuf
;
723 tmpbuf
= sorttab
+ length
;
724 for(i
=0; i
< length
; i
++)
727 for(i
=0; i
< length
/2; i
++) {
728 hres
= sort_cmp(ctx
, cmp_func
, *sorttab
[2*i
+1], *sorttab
[2*i
], &cmp
);
734 sorttab
[2*i
] = sorttab
[2*i
+1];
735 sorttab
[2*i
+1] = tmpv
;
739 if(SUCCEEDED(hres
)) {
742 for(k
=2; k
< length
; k
*= 2) {
743 for(i
=0; i
+k
< length
; i
+= 2*k
) {
748 bend
= length
- (i
+k
);
750 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(jsval_t
*));
752 while(a
< k
&& b
< bend
) {
753 hres
= sort_cmp(ctx
, cmp_func
, *tmpbuf
[a
], *sorttab
[i
+k
+b
], &cmp
);
758 sorttab
[i
+a
+b
] = tmpbuf
[a
];
761 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
770 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(jsval_t
*));
778 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
779 hres
= jsdisp_propput_idx(jsthis
, i
, *sorttab
[i
]);
783 for(i
=0; i
< length
; i
++)
784 jsval_release(vtab
[i
]);
789 jsdisp_release(cmp_func
);
795 *r
= jsval_obj(jsdisp_addref(jsthis
));
799 /* ECMA-262 3rd Edition 15.4.4.12 */
800 static HRESULT
Array_splice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
803 DWORD length
, start
=0, delete_cnt
=0, i
, add_args
= 0;
804 jsdisp_t
*ret_array
= NULL
, *jsthis
;
812 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
817 hres
= to_integer(ctx
, argv
[0], &d
);
823 start
= min(n
, length
);
825 start
= -n
> length
? 0 : length
+ n
;
827 start
= d
< 0.0 ? 0 : length
;
832 hres
= to_integer(ctx
, argv
[1], &d
);
838 delete_cnt
= min(n
, length
-start
);
840 delete_cnt
= length
-start
;
847 hres
= create_array(ctx
, 0, &ret_array
);
851 for(i
=0; SUCCEEDED(hres
) && i
< delete_cnt
; i
++) {
852 hres
= jsdisp_get_idx(jsthis
, start
+i
, &val
);
853 if(hres
== DISP_E_UNKNOWNNAME
) {
855 }else if(SUCCEEDED(hres
)) {
856 hres
= jsdisp_propput_idx(ret_array
, i
, val
);
862 hres
= jsdisp_propput_name(ret_array
, L
"length", jsval_number(delete_cnt
));
865 if(add_args
< delete_cnt
) {
866 for(i
= start
; SUCCEEDED(hres
) && i
< length
-delete_cnt
; i
++) {
867 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
, &val
);
868 if(hres
== DISP_E_UNKNOWNNAME
) {
869 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
);
870 }else if(SUCCEEDED(hres
)) {
871 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
, val
);
876 for(i
=length
; SUCCEEDED(hres
) && i
!= length
-delete_cnt
+add_args
; i
--)
877 hres
= jsdisp_delete_idx(jsthis
, i
-1);
878 }else if(add_args
> delete_cnt
) {
879 for(i
=length
-delete_cnt
; SUCCEEDED(hres
) && i
!= start
; i
--) {
880 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
-1, &val
);
881 if(hres
== DISP_E_UNKNOWNNAME
) {
882 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
-1);
883 }else if(SUCCEEDED(hres
)) {
884 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
-1, val
);
890 for(i
=0; SUCCEEDED(hres
) && i
< add_args
; i
++)
891 hres
= jsdisp_propput_idx(jsthis
, start
+i
, argv
[i
+2]);
894 hres
= jsdisp_propput_name(jsthis
, L
"length", jsval_number(length
-delete_cnt
+add_args
));
898 jsdisp_release(ret_array
);
903 *r
= jsval_obj(ret_array
);
907 /* ECMA-262 3rd Edition 15.4.4.2 */
908 static HRESULT
Array_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
911 ArrayInstance
*array
;
915 array
= array_this(jsthis
);
917 return JS_E_ARRAY_EXPECTED
;
919 return array_join(ctx
, &array
->dispex
, array
->length
, L
",", 1, r
);
922 static HRESULT
Array_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
929 static HRESULT
Array_forEach(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
932 IDispatch
*context_obj
= NULL
, *callback
;
933 jsval_t value
, args
[3], res
;
940 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
944 /* Fixme check IsCallable */
945 if(!argc
|| !is_object_instance(argv
[0]) || !get_object(argv
[0])) {
946 FIXME("Invalid arg %s\n", debugstr_jsval(argc
? argv
[0] : jsval_undefined()));
949 callback
= get_object(argv
[0]);
951 if(argc
> 1 && !is_undefined(argv
[1])) {
952 if(!is_object_instance(argv
[1]) || !get_object(argv
[1])) {
953 FIXME("Unsupported context this %s\n", debugstr_jsval(argv
[1]));
956 context_obj
= get_object(argv
[1]);
959 for(i
= 0; i
< length
; i
++) {
960 hres
= jsdisp_get_idx(jsthis
, i
, &value
);
961 if(hres
== DISP_E_UNKNOWNNAME
)
967 args
[1] = jsval_number(i
);
968 args
[2] = jsval_obj(jsthis
);
969 hres
= disp_call_value(ctx
, callback
, context_obj
, DISPATCH_METHOD
, ARRAY_SIZE(args
), args
, &res
);
970 jsval_release(value
);
976 if(r
) *r
= jsval_undefined();
980 static HRESULT
Array_indexOf(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
984 unsigned length
, i
, from
= 0;
985 jsval_t search
, value
;
991 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
995 if(r
) *r
= jsval_number(-1);
999 search
= argc
? argv
[0] : jsval_undefined();
1004 hres
= to_integer(ctx
, argv
[1], &from_arg
);
1009 from
= min(from_arg
, length
);
1011 from
= max(from_arg
+ length
, 0);
1014 for(i
= from
; i
< length
; i
++) {
1015 hres
= jsdisp_get_idx(jsthis
, i
, &value
);
1016 if(hres
== DISP_E_UNKNOWNNAME
)
1021 hres
= jsval_strict_equal(value
, search
, &eq
);
1022 jsval_release(value
);
1026 if(r
) *r
= jsval_number(i
);
1031 if(r
) *r
= jsval_number(-1);
1035 static HRESULT
Array_map(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1037 IDispatch
*context_this
= NULL
, *callback
;
1038 jsval_t callback_args
[3], mapped_value
;
1039 jsdisp_t
*jsthis
, *array
;
1045 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
1047 FIXME("Could not get length\n");
1051 /* FIXME: check IsCallable */
1052 if(!argc
|| !is_object_instance(argv
[0]) || !get_object(argv
[0])) {
1053 FIXME("Invalid arg %s\n", debugstr_jsval(argc
? argv
[0] : jsval_undefined()));
1054 return E_INVALIDARG
;
1056 callback
= get_object(argv
[0]);
1059 if(is_object_instance(argv
[1]) && get_object(argv
[1])) {
1060 context_this
= get_object(argv
[1]);
1061 }else if(!is_undefined(argv
[1])) {
1062 FIXME("Unsupported context this %s\n", debugstr_jsval(argv
[1]));
1067 hres
= create_array(ctx
, length
, &array
);
1071 for(k
= 0; k
< length
; k
++) {
1072 hres
= jsdisp_get_idx(jsthis
, k
, &callback_args
[0]);
1073 if(hres
== DISP_E_UNKNOWNNAME
)
1078 callback_args
[1] = jsval_number(k
);
1079 callback_args
[2] = jsval_obj(jsthis
);
1080 hres
= disp_call_value(ctx
, callback
, context_this
, DISPATCH_METHOD
, 3, callback_args
, &mapped_value
);
1081 jsval_release(callback_args
[0]);
1085 hres
= jsdisp_propput_idx(array
, k
, mapped_value
);
1090 if(SUCCEEDED(hres
) && r
)
1091 *r
= jsval_obj(array
);
1093 jsdisp_release(array
);
1097 static HRESULT
Array_reduce(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1099 IDispatch
*context_this
= NULL
, *callback
;
1100 jsval_t callback_args
[4], acc
, new_acc
;
1101 BOOL have_value
= FALSE
;
1108 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
1110 FIXME("Could not get length\n");
1114 /* Fixme check IsCallable */
1115 if(!argc
|| !is_object_instance(argv
[0]) || !get_object(argv
[0])) {
1116 FIXME("Invalid arg %s\n", debugstr_jsval(argc
? argv
[0] : jsval_undefined()));
1117 return E_INVALIDARG
;
1119 callback
= get_object(argv
[0]);
1123 hres
= jsval_copy(argv
[1], &acc
);
1128 for(k
= 0; k
< length
; k
++) {
1129 hres
= jsdisp_get_idx(jsthis
, k
, &callback_args
[1]);
1130 if(hres
== DISP_E_UNKNOWNNAME
)
1137 acc
= callback_args
[1];
1141 callback_args
[0] = acc
;
1142 callback_args
[2] = jsval_number(k
);
1143 callback_args
[3] = jsval_obj(jsthis
);
1144 hres
= disp_call_value(ctx
, callback
, context_this
, DISPATCH_METHOD
, ARRAY_SIZE(callback_args
), callback_args
, &new_acc
);
1145 jsval_release(callback_args
[1]);
1153 if(SUCCEEDED(hres
) && !have_value
) {
1154 WARN("No array element\n");
1155 hres
= JS_E_INVALID_ACTION
;
1158 if(SUCCEEDED(hres
) && r
)
1165 /* ECMA-262 3rd Edition 15.4.4.13 */
1166 static HRESULT
Array_unshift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1170 WCHAR buf
[14], *buf_end
, *str
;
1178 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
1183 buf_end
= buf
+ ARRAY_SIZE(buf
)-1;
1188 str
= idx_to_str(i
, buf_end
);
1190 hres
= jsdisp_get_id(jsthis
, str
, 0, &id
);
1191 if(SUCCEEDED(hres
)) {
1192 hres
= jsdisp_propget(jsthis
, id
, &val
);
1196 hres
= jsdisp_propput_idx(jsthis
, i
+argc
, val
);
1198 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1199 hres
= IDispatchEx_DeleteMemberByDispID(vthis
->u
.dispex
, id
);
1207 for(i
=0; i
<argc
; i
++) {
1208 hres
= jsdisp_propput_idx(jsthis
, i
, argv
[i
]);
1215 hres
= set_length(jsthis
, length
);
1221 *r
= ctx
->version
< 2 ? jsval_undefined() : jsval_number(length
);
1225 static HRESULT
Array_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1227 ArrayInstance
*array
= array_from_jsdisp(jsthis
);
1231 return array_join(ctx
, &array
->dispex
, array
->length
, L
",", 1, r
);
1234 static void Array_destructor(jsdisp_t
*dispex
)
1239 static void Array_on_put(jsdisp_t
*dispex
, const WCHAR
*name
)
1241 ArrayInstance
*array
= array_from_jsdisp(dispex
);
1242 const WCHAR
*ptr
= name
;
1248 while(*ptr
&& is_digit(*ptr
)) {
1249 id
= id
*10 + (*ptr
-'0');
1256 if(id
>= array
->length
)
1257 array
->length
= id
+1;
1260 static const builtin_prop_t Array_props
[] = {
1261 {L
"concat", Array_concat
, PROPF_METHOD
|1},
1262 {L
"forEach", Array_forEach
, PROPF_METHOD
|PROPF_ES5
|1},
1263 {L
"indexOf", Array_indexOf
, PROPF_METHOD
|PROPF_ES5
|1},
1264 {L
"join", Array_join
, PROPF_METHOD
|1},
1265 {L
"length", NULL
,0, Array_get_length
, Array_set_length
},
1266 {L
"map", Array_map
, PROPF_METHOD
|PROPF_ES5
|1},
1267 {L
"pop", Array_pop
, PROPF_METHOD
},
1268 {L
"push", Array_push
, PROPF_METHOD
|1},
1269 {L
"reduce", Array_reduce
, PROPF_METHOD
|PROPF_ES5
|1},
1270 {L
"reverse", Array_reverse
, PROPF_METHOD
},
1271 {L
"shift", Array_shift
, PROPF_METHOD
},
1272 {L
"slice", Array_slice
, PROPF_METHOD
|2},
1273 {L
"sort", Array_sort
, PROPF_METHOD
|1},
1274 {L
"splice", Array_splice
, PROPF_METHOD
|2},
1275 {L
"toLocaleString", Array_toLocaleString
, PROPF_METHOD
},
1276 {L
"toString", Array_toString
, PROPF_METHOD
},
1277 {L
"unshift", Array_unshift
, PROPF_METHOD
|1},
1280 static const builtin_info_t Array_info
= {
1282 {NULL
, NULL
,0, Array_get_value
},
1283 ARRAY_SIZE(Array_props
),
1289 static const builtin_prop_t ArrayInst_props
[] = {
1290 {L
"length", NULL
,0, Array_get_length
, Array_set_length
}
1293 static const builtin_info_t ArrayInst_info
= {
1295 {NULL
, NULL
,0, Array_get_value
},
1296 ARRAY_SIZE(ArrayInst_props
),
1302 /* ECMA-262 5.1 Edition 15.4.3.2 */
1303 static HRESULT
ArrayConstr_isArray(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
1309 if(!argc
|| !is_object_instance(argv
[0])) {
1310 if(r
) *r
= jsval_bool(FALSE
);
1314 obj
= iface_to_jsdisp(get_object(argv
[0]));
1315 if(r
) *r
= jsval_bool(obj
&& is_class(obj
, JSCLASS_ARRAY
));
1316 if(obj
) jsdisp_release(obj
);
1320 static HRESULT
ArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1330 case DISPATCH_METHOD
:
1331 case DISPATCH_CONSTRUCT
: {
1332 if(argc
== 1 && is_number(argv
[0])) {
1333 double n
= get_number(argv
[0]);
1335 if(n
< 0 || !is_int32(n
))
1336 return JS_E_INVALID_LENGTH
;
1338 hres
= create_array(ctx
, n
, &obj
);
1342 *r
= jsval_obj(obj
);
1346 hres
= create_array(ctx
, argc
, &obj
);
1350 for(i
=0; i
< argc
; i
++) {
1351 hres
= jsdisp_propput_idx(obj
, i
, argv
[i
]);
1356 jsdisp_release(obj
);
1360 *r
= jsval_obj(obj
);
1364 FIXME("unimplemented flags: %x\n", flags
);
1371 static HRESULT
alloc_array(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, ArrayInstance
**ret
)
1373 ArrayInstance
*array
;
1376 array
= heap_alloc_zero(sizeof(ArrayInstance
));
1378 return E_OUTOFMEMORY
;
1380 if(object_prototype
)
1381 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, object_prototype
);
1383 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &ArrayInst_info
, ctx
->array_constr
);
1394 static const builtin_prop_t ArrayConstr_props
[] = {
1395 {L
"isArray", ArrayConstr_isArray
, PROPF_ES5
|PROPF_METHOD
|1}
1398 static const builtin_info_t ArrayConstr_info
= {
1400 DEFAULT_FUNCTION_VALUE
,
1401 ARRAY_SIZE(ArrayConstr_props
),
1407 HRESULT
create_array_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1409 ArrayInstance
*array
;
1412 hres
= alloc_array(ctx
, object_prototype
, &array
);
1416 hres
= create_builtin_constructor(ctx
, ArrayConstr_value
, L
"Array", &ArrayConstr_info
, PROPF_CONSTR
|1, &array
->dispex
, ret
);
1418 jsdisp_release(&array
->dispex
);
1422 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, jsdisp_t
**ret
)
1424 ArrayInstance
*array
;
1427 hres
= alloc_array(ctx
, NULL
, &array
);
1431 array
->length
= length
;
1433 *ret
= &array
->dispex
;