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};
51 static const WCHAR default_separatorW
[] = {',',0};
53 static inline ArrayInstance
*array_from_jsdisp(jsdisp_t
*jsdisp
)
55 return CONTAINING_RECORD(jsdisp
, ArrayInstance
, dispex
);
58 static inline ArrayInstance
*array_from_vdisp(vdisp_t
*vdisp
)
60 return array_from_jsdisp(vdisp
->u
.jsdisp
);
63 static inline ArrayInstance
*array_this(vdisp_t
*jsthis
)
65 return is_vclass(jsthis
, JSCLASS_ARRAY
) ? array_from_vdisp(jsthis
) : NULL
;
68 unsigned array_get_length(jsdisp_t
*array
)
70 assert(is_class(array
, JSCLASS_ARRAY
));
71 return array_from_jsdisp(array
)->length
;
74 static HRESULT
get_length(script_ctx_t
*ctx
, vdisp_t
*vdisp
, jsdisp_t
**jsthis
, DWORD
*ret
)
80 array
= array_this(vdisp
);
82 *jsthis
= &array
->dispex
;
88 return throw_type_error(ctx
, JS_E_JSCRIPT_EXPECTED
, NULL
);
90 hres
= jsdisp_propget_name(vdisp
->u
.jsdisp
, lengthW
, &val
);
94 hres
= to_uint32(ctx
, val
, ret
);
99 *jsthis
= vdisp
->u
.jsdisp
;
103 static HRESULT
set_length(jsdisp_t
*obj
, DWORD length
)
105 if(is_class(obj
, JSCLASS_ARRAY
)) {
106 array_from_jsdisp(obj
)->length
= length
;
110 return jsdisp_propput_name(obj
, lengthW
, jsval_number(length
));
113 static WCHAR
*idx_to_str(DWORD idx
, WCHAR
*ptr
)
121 *ptr
-- = '0' + (idx
%10);
128 static HRESULT
Array_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
130 TRACE("%p\n", jsthis
);
132 *r
= jsval_number(array_from_jsdisp(jsthis
)->length
);
136 static HRESULT
Array_set_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
138 ArrayInstance
*This
= array_from_jsdisp(jsthis
);
143 TRACE("%p %d\n", This
, This
->length
);
145 hres
= to_number(ctx
, value
, &len
);
151 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
153 for(i
=len
; i
< This
->length
; i
++) {
154 hres
= jsdisp_delete_idx(&This
->dispex
, i
);
163 static HRESULT
concat_array(jsdisp_t
*array
, ArrayInstance
*obj
, DWORD
*len
)
169 for(i
=0; i
< obj
->length
; i
++) {
170 hres
= jsdisp_get_idx(&obj
->dispex
, i
, &val
);
171 if(hres
== DISP_E_UNKNOWNNAME
)
176 hres
= jsdisp_propput_idx(array
, *len
+i
, val
);
186 static HRESULT
concat_obj(jsdisp_t
*array
, IDispatch
*obj
, DWORD
*len
)
191 jsobj
= iface_to_jsdisp(obj
);
193 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
194 hres
= concat_array(array
, array_from_jsdisp(jsobj
), len
);
195 jsdisp_release(jsobj
);
198 jsdisp_release(jsobj
);
201 return jsdisp_propput_idx(array
, (*len
)++, jsval_disp(obj
));
204 static HRESULT
Array_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
213 hres
= create_array(ctx
, 0, &ret
);
217 hres
= concat_obj(ret
, jsthis
->u
.disp
, &len
);
218 if(SUCCEEDED(hres
)) {
221 for(i
=0; i
< argc
; i
++) {
222 if(is_object_instance(argv
[i
]))
223 hres
= concat_obj(ret
, get_object(argv
[i
]), &len
);
225 hres
= jsdisp_propput_idx(ret
, len
++, argv
[i
]);
241 static HRESULT
array_join(script_ctx_t
*ctx
, jsdisp_t
*array
, DWORD length
, const WCHAR
*sep
,
242 unsigned seplen
, jsval_t
*r
)
244 jsstr_t
**str_tab
, *ret
= NULL
;
247 HRESULT hres
= E_FAIL
;
251 *r
= jsval_string(jsstr_empty());
255 str_tab
= heap_alloc_zero(length
* sizeof(*str_tab
));
257 return E_OUTOFMEMORY
;
259 for(i
=0; i
< length
; i
++) {
260 hres
= jsdisp_get_idx(array
, i
, &val
);
261 if(hres
== DISP_E_UNKNOWNNAME
) {
264 } else if(FAILED(hres
))
267 if(!is_undefined(val
) && !is_null(val
)) {
268 hres
= to_string(ctx
, val
, str_tab
+i
);
275 if(SUCCEEDED(hres
)) {
279 len
= jsstr_length(str_tab
[0]);
280 for(i
=1; i
< length
; i
++) {
283 len
+= jsstr_length(str_tab
[i
]);
284 if(len
> JSSTR_MAX_LENGTH
) {
285 hres
= E_OUTOFMEMORY
;
290 if(SUCCEEDED(hres
)) {
293 ret
= jsstr_alloc_buf(len
, &ptr
);
296 ptr
+= jsstr_flush(str_tab
[0], ptr
);
298 for(i
=1; i
< length
; i
++) {
300 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
305 ptr
+= jsstr_flush(str_tab
[i
], ptr
);
308 hres
= E_OUTOFMEMORY
;
313 for(i
=0; i
< length
; i
++) {
315 jsstr_release(str_tab
[i
]);
321 TRACE("= %s\n", debugstr_jsstr(ret
));
324 *r
= jsval_string(ret
);
330 /* ECMA-262 3rd Edition 15.4.4.5 */
331 static HRESULT
Array_join(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
340 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
348 hres
= to_flat_string(ctx
, argv
[0], &sep_str
, &sep
);
352 hres
= array_join(ctx
, jsthis
, length
, sep
, jsstr_length(sep_str
), r
);
354 jsstr_release(sep_str
);
356 hres
= array_join(ctx
, jsthis
, length
, default_separatorW
, strlenW(default_separatorW
), r
);
362 static HRESULT
Array_pop(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
372 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
377 hres
= set_length(jsthis
, 0);
382 *r
= jsval_undefined();
387 hres
= jsdisp_get_idx(jsthis
, length
, &val
);
389 hres
= jsdisp_delete_idx(jsthis
, length
);
390 else if(hres
== DISP_E_UNKNOWNNAME
)
391 val
= jsval_undefined();
396 hres
= set_length(jsthis
, length
);
410 /* ECMA-262 3rd Edition 15.4.4.7 */
411 static HRESULT
Array_push(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
421 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
425 for(i
=0; i
< argc
; i
++) {
426 hres
= jsdisp_propput_idx(jsthis
, length
+i
, argv
[i
]);
431 hres
= set_length(jsthis
, length
+argc
);
436 *r
= jsval_number(length
+argc
);
440 static HRESULT
Array_reverse(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
446 HRESULT hres1
, hres2
;
450 hres1
= get_length(ctx
, vthis
, &jsthis
, &length
);
454 for(k
=0; k
<length
/2; k
++) {
457 hres1
= jsdisp_get_idx(jsthis
, k
, &v1
);
458 if(FAILED(hres1
) && hres1
!=DISP_E_UNKNOWNNAME
)
461 hres2
= jsdisp_get_idx(jsthis
, l
, &v2
);
462 if(FAILED(hres2
) && hres2
!=DISP_E_UNKNOWNNAME
) {
467 if(hres1
== DISP_E_UNKNOWNNAME
)
468 hres1
= jsdisp_delete_idx(jsthis
, l
);
470 hres1
= jsdisp_propput_idx(jsthis
, l
, v1
);
478 if(hres2
== DISP_E_UNKNOWNNAME
)
479 hres2
= jsdisp_delete_idx(jsthis
, k
);
481 hres2
= jsdisp_propput_idx(jsthis
, k
, v2
);
490 *r
= jsval_obj(jsdisp_addref(jsthis
));
494 /* ECMA-262 3rd Edition 15.4.4.9 */
495 static HRESULT
Array_shift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
505 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
510 hres
= set_length(jsthis
, 0);
515 *r
= jsval_undefined();
519 hres
= jsdisp_get_idx(jsthis
, 0, &ret
);
520 if(hres
== DISP_E_UNKNOWNNAME
) {
521 ret
= jsval_undefined();
525 for(i
=1; SUCCEEDED(hres
) && i
<length
; i
++) {
526 hres
= jsdisp_get_idx(jsthis
, i
, &v
);
527 if(hres
== DISP_E_UNKNOWNNAME
)
528 hres
= jsdisp_delete_idx(jsthis
, i
-1);
529 else if(SUCCEEDED(hres
))
530 hres
= jsdisp_propput_idx(jsthis
, i
-1, v
);
533 if(SUCCEEDED(hres
)) {
534 hres
= jsdisp_delete_idx(jsthis
, length
-1);
536 hres
= set_length(jsthis
, length
-1);
549 /* ECMA-262 3rd Edition 15.4.4.10 */
550 static HRESULT
Array_slice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
552 jsdisp_t
*arr
, *jsthis
;
554 DWORD length
, start
, end
, idx
;
559 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
564 hres
= to_number(ctx
, argv
[0], &range
);
568 range
= floor(range
);
569 if(-range
>length
|| isnan(range
)) start
= 0;
570 else if(range
< 0) start
= range
+length
;
571 else if(range
<= length
) start
= range
;
577 hres
= to_number(ctx
, argv
[1], &range
);
581 range
= floor(range
);
582 if(-range
>length
) end
= 0;
583 else if(range
< 0) end
= range
+length
;
584 else if(range
<= length
) end
= range
;
589 hres
= create_array(ctx
, (end
>start
)?end
-start
:0, &arr
);
593 for(idx
=start
; idx
<end
; idx
++) {
596 hres
= jsdisp_get_idx(jsthis
, idx
, &v
);
597 if(hres
== DISP_E_UNKNOWNNAME
)
600 if(SUCCEEDED(hres
)) {
601 hres
= jsdisp_propput_idx(arr
, idx
-start
, v
);
619 static HRESULT
sort_cmp(script_ctx_t
*ctx
, jsdisp_t
*cmp_func
, jsval_t v1
, jsval_t v2
, INT
*cmp
)
624 jsval_t args
[2] = {v1
, v2
};
628 hres
= jsdisp_call_value(cmp_func
, NULL
, DISPATCH_METHOD
, 2, args
, &res
);
632 hres
= to_number(ctx
, res
, &n
);
639 *cmp
= n
> 0.0 ? 1 : -1;
640 }else if(is_undefined(v1
)) {
641 *cmp
= is_undefined(v2
) ? 0 : 1;
642 }else if(is_undefined(v2
)) {
644 }else if(is_number(v1
) && is_number(v2
)) {
645 double d
= get_number(v1
)-get_number(v2
);
649 *cmp
= d
< -0.0 ? -1 : 0;
653 hres
= to_string(ctx
, v1
, &x
);
657 hres
= to_string(ctx
, v2
, &y
);
658 if(SUCCEEDED(hres
)) {
659 *cmp
= jsstr_cmp(x
, y
);
670 /* ECMA-262 3rd Edition 15.4.4.11 */
671 static HRESULT
Array_sort(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
674 jsdisp_t
*jsthis
, *cmp_func
= NULL
;
675 jsval_t
*vtab
, **sorttab
= NULL
;
682 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
687 WARN("invalid arg_cnt %d\n", argc
);
692 if(!is_object_instance(argv
[0])) {
693 WARN("arg is not dispatch\n");
697 cmp_func
= iface_to_jsdisp(get_object(argv
[0]));
698 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
699 WARN("cmp_func is not a function\n");
701 jsdisp_release(cmp_func
);
708 jsdisp_release(cmp_func
);
710 *r
= jsval_obj(jsdisp_addref(jsthis
));
714 vtab
= heap_alloc_zero(length
* sizeof(*vtab
));
716 for(i
=0; i
<length
; i
++) {
717 hres
= jsdisp_get_idx(jsthis
, i
, vtab
+i
);
718 if(hres
== DISP_E_UNKNOWNNAME
) {
719 vtab
[i
] = jsval_undefined();
721 } else if(FAILED(hres
)) {
722 WARN("Could not get elem %d: %08x\n", i
, hres
);
727 hres
= E_OUTOFMEMORY
;
730 if(SUCCEEDED(hres
)) {
731 sorttab
= heap_alloc(length
*2*sizeof(*sorttab
));
733 hres
= E_OUTOFMEMORY
;
737 if(SUCCEEDED(hres
)) {
738 jsval_t
*tmpv
, **tmpbuf
;
741 tmpbuf
= sorttab
+ length
;
742 for(i
=0; i
< length
; i
++)
745 for(i
=0; i
< length
/2; i
++) {
746 hres
= sort_cmp(ctx
, cmp_func
, *sorttab
[2*i
+1], *sorttab
[2*i
], &cmp
);
752 sorttab
[2*i
] = sorttab
[2*i
+1];
753 sorttab
[2*i
+1] = tmpv
;
757 if(SUCCEEDED(hres
)) {
760 for(k
=2; k
< length
; k
*= 2) {
761 for(i
=0; i
+k
< length
; i
+= 2*k
) {
766 bend
= length
- (i
+k
);
768 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(jsval_t
*));
770 while(a
< k
&& b
< bend
) {
771 hres
= sort_cmp(ctx
, cmp_func
, *tmpbuf
[a
], *sorttab
[i
+k
+b
], &cmp
);
776 sorttab
[i
+a
+b
] = tmpbuf
[a
];
779 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
788 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(jsval_t
*));
796 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
797 hres
= jsdisp_propput_idx(jsthis
, i
, *sorttab
[i
]);
801 for(i
=0; i
< length
; i
++)
802 jsval_release(vtab
[i
]);
807 jsdisp_release(cmp_func
);
813 *r
= jsval_obj(jsdisp_addref(jsthis
));
817 /* ECMA-262 3rd Edition 15.4.4.12 */
818 static HRESULT
Array_splice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
821 DWORD length
, start
=0, delete_cnt
=0, i
, add_args
= 0;
822 jsdisp_t
*ret_array
= NULL
, *jsthis
;
830 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
835 hres
= to_integer(ctx
, argv
[0], &d
);
841 start
= min(n
, length
);
843 start
= -n
> length
? 0 : length
+ n
;
845 start
= d
< 0.0 ? 0 : length
;
850 hres
= to_integer(ctx
, argv
[1], &d
);
856 delete_cnt
= min(n
, length
-start
);
858 delete_cnt
= length
-start
;
865 hres
= create_array(ctx
, 0, &ret_array
);
869 for(i
=0; SUCCEEDED(hres
) && i
< delete_cnt
; i
++) {
870 hres
= jsdisp_get_idx(jsthis
, start
+i
, &val
);
871 if(hres
== DISP_E_UNKNOWNNAME
) {
873 }else if(SUCCEEDED(hres
)) {
874 hres
= jsdisp_propput_idx(ret_array
, i
, val
);
880 hres
= jsdisp_propput_name(ret_array
, lengthW
, jsval_number(delete_cnt
));
883 if(add_args
< delete_cnt
) {
884 for(i
= start
; SUCCEEDED(hres
) && i
< length
-delete_cnt
; i
++) {
885 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
, &val
);
886 if(hres
== DISP_E_UNKNOWNNAME
) {
887 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
);
888 }else if(SUCCEEDED(hres
)) {
889 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
, val
);
894 for(i
=length
; SUCCEEDED(hres
) && i
!= length
-delete_cnt
+add_args
; i
--)
895 hres
= jsdisp_delete_idx(jsthis
, i
-1);
896 }else if(add_args
> delete_cnt
) {
897 for(i
=length
-delete_cnt
; SUCCEEDED(hres
) && i
!= start
; i
--) {
898 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
-1, &val
);
899 if(hres
== DISP_E_UNKNOWNNAME
) {
900 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
-1);
901 }else if(SUCCEEDED(hres
)) {
902 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
-1, val
);
908 for(i
=0; SUCCEEDED(hres
) && i
< add_args
; i
++)
909 hres
= jsdisp_propput_idx(jsthis
, start
+i
, argv
[i
+2]);
912 hres
= jsdisp_propput_name(jsthis
, lengthW
, jsval_number(length
-delete_cnt
+add_args
));
916 jsdisp_release(ret_array
);
921 *r
= jsval_obj(ret_array
);
925 /* ECMA-262 3rd Edition 15.4.4.2 */
926 static HRESULT
Array_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
929 ArrayInstance
*array
;
933 array
= array_this(jsthis
);
935 return throw_type_error(ctx
, JS_E_ARRAY_EXPECTED
, NULL
);
937 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
,
938 strlenW(default_separatorW
), r
);
941 static HRESULT
Array_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
948 /* ECMA-262 3rd Edition 15.4.4.13 */
949 static HRESULT
Array_unshift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
953 WCHAR buf
[14], *buf_end
, *str
;
961 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
966 buf_end
= buf
+ sizeof(buf
)/sizeof(WCHAR
)-1;
971 str
= idx_to_str(i
, buf_end
);
973 hres
= jsdisp_get_id(jsthis
, str
, 0, &id
);
974 if(SUCCEEDED(hres
)) {
975 hres
= jsdisp_propget(jsthis
, id
, &val
);
979 hres
= jsdisp_propput_idx(jsthis
, i
+argc
, val
);
981 }else if(hres
== DISP_E_UNKNOWNNAME
) {
982 hres
= IDispatchEx_DeleteMemberByDispID(vthis
->u
.dispex
, id
);
990 for(i
=0; i
<argc
; i
++) {
991 hres
= jsdisp_propput_idx(jsthis
, i
, argv
[i
]);
998 hres
= set_length(jsthis
, length
);
1004 *r
= ctx
->version
< 2 ? jsval_undefined() : jsval_number(length
);
1008 static HRESULT
Array_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1010 ArrayInstance
*array
= array_from_jsdisp(jsthis
);
1014 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
,
1015 strlenW(default_separatorW
), r
);
1018 static void Array_destructor(jsdisp_t
*dispex
)
1023 static void Array_on_put(jsdisp_t
*dispex
, const WCHAR
*name
)
1025 ArrayInstance
*array
= array_from_jsdisp(dispex
);
1026 const WCHAR
*ptr
= name
;
1032 while(*ptr
&& isdigitW(*ptr
)) {
1033 id
= id
*10 + (*ptr
-'0');
1040 if(id
>= array
->length
)
1041 array
->length
= id
+1;
1044 static const builtin_prop_t Array_props
[] = {
1045 {concatW
, Array_concat
, PROPF_METHOD
|1},
1046 {joinW
, Array_join
, PROPF_METHOD
|1},
1047 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
},
1048 {popW
, Array_pop
, PROPF_METHOD
},
1049 {pushW
, Array_push
, PROPF_METHOD
|1},
1050 {reverseW
, Array_reverse
, PROPF_METHOD
},
1051 {shiftW
, Array_shift
, PROPF_METHOD
},
1052 {sliceW
, Array_slice
, PROPF_METHOD
|2},
1053 {sortW
, Array_sort
, PROPF_METHOD
|1},
1054 {spliceW
, Array_splice
, PROPF_METHOD
|2},
1055 {toLocaleStringW
, Array_toLocaleString
, PROPF_METHOD
},
1056 {toStringW
, Array_toString
, PROPF_METHOD
},
1057 {unshiftW
, Array_unshift
, PROPF_METHOD
|1},
1060 static const builtin_info_t Array_info
= {
1062 {NULL
, NULL
,0, Array_get_value
},
1063 sizeof(Array_props
)/sizeof(*Array_props
),
1069 static const builtin_prop_t ArrayInst_props
[] = {
1070 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
}
1073 static const builtin_info_t ArrayInst_info
= {
1075 {NULL
, NULL
,0, Array_get_value
},
1076 sizeof(ArrayInst_props
)/sizeof(*ArrayInst_props
),
1082 static HRESULT
ArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1092 case DISPATCH_METHOD
:
1093 case DISPATCH_CONSTRUCT
: {
1094 if(argc
== 1 && is_number(argv
[0])) {
1095 double n
= get_number(argv
[0]);
1097 if(n
< 0 || !is_int32(n
))
1098 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
1100 hres
= create_array(ctx
, n
, &obj
);
1104 *r
= jsval_obj(obj
);
1108 hres
= create_array(ctx
, argc
, &obj
);
1112 for(i
=0; i
< argc
; i
++) {
1113 hres
= jsdisp_propput_idx(obj
, i
, argv
[i
]);
1118 jsdisp_release(obj
);
1122 *r
= jsval_obj(obj
);
1126 FIXME("unimplemented flags: %x\n", flags
);
1133 static HRESULT
alloc_array(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, ArrayInstance
**ret
)
1135 ArrayInstance
*array
;
1138 array
= heap_alloc_zero(sizeof(ArrayInstance
));
1140 return E_OUTOFMEMORY
;
1142 if(object_prototype
)
1143 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, object_prototype
);
1145 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &ArrayInst_info
, ctx
->array_constr
);
1156 HRESULT
create_array_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1158 ArrayInstance
*array
;
1161 static const WCHAR ArrayW
[] = {'A','r','r','a','y',0};
1163 hres
= alloc_array(ctx
, object_prototype
, &array
);
1167 hres
= create_builtin_constructor(ctx
, ArrayConstr_value
, ArrayW
, NULL
, PROPF_CONSTR
|1, &array
->dispex
, ret
);
1169 jsdisp_release(&array
->dispex
);
1173 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, jsdisp_t
**ret
)
1175 ArrayInstance
*array
;
1178 hres
= alloc_array(ctx
, NULL
, &array
);
1182 array
->length
= length
;
1184 *ret
= &array
->dispex
;