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
27 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
28 static const WCHAR concatW
[] = {'c','o','n','c','a','t',0};
29 static const WCHAR joinW
[] = {'j','o','i','n',0};
30 static const WCHAR popW
[] = {'p','o','p',0};
31 static const WCHAR pushW
[] = {'p','u','s','h',0};
32 static const WCHAR reverseW
[] = {'r','e','v','e','r','s','e',0};
33 static const WCHAR shiftW
[] = {'s','h','i','f','t',0};
34 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
35 static const WCHAR sortW
[] = {'s','o','r','t',0};
36 static const WCHAR spliceW
[] = {'s','p','l','i','c','e',0};
37 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
38 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
39 static const WCHAR unshiftW
[] = {'u','n','s','h','i','f','t',0};
41 static const WCHAR default_separatorW
[] = {',',0};
43 static inline ArrayInstance
*array_from_jsdisp(jsdisp_t
*jsdisp
)
45 return CONTAINING_RECORD(jsdisp
, ArrayInstance
, dispex
);
48 static inline ArrayInstance
*array_from_vdisp(vdisp_t
*vdisp
)
50 return array_from_jsdisp(vdisp
->u
.jsdisp
);
53 static inline ArrayInstance
*array_this(vdisp_t
*jsthis
)
55 return is_vclass(jsthis
, JSCLASS_ARRAY
) ? array_from_vdisp(jsthis
) : NULL
;
58 unsigned array_get_length(jsdisp_t
*array
)
60 assert(is_class(array
, JSCLASS_ARRAY
));
61 return array_from_jsdisp(array
)->length
;
64 static HRESULT
get_length(script_ctx_t
*ctx
, vdisp_t
*vdisp
, jsdisp_t
**jsthis
, DWORD
*ret
)
70 array
= array_this(vdisp
);
72 *jsthis
= &array
->dispex
;
78 return throw_type_error(ctx
, JS_E_JSCRIPT_EXPECTED
, NULL
);
80 hres
= jsdisp_propget_name(vdisp
->u
.jsdisp
, lengthW
, &val
);
84 hres
= to_uint32(ctx
, val
, ret
);
89 *jsthis
= vdisp
->u
.jsdisp
;
93 static HRESULT
set_length(jsdisp_t
*obj
, DWORD length
)
95 if(is_class(obj
, JSCLASS_ARRAY
)) {
96 array_from_jsdisp(obj
)->length
= length
;
100 return jsdisp_propput_name(obj
, lengthW
, jsval_number(length
));
103 static WCHAR
*idx_to_str(DWORD idx
, WCHAR
*ptr
)
111 *ptr
-- = '0' + (idx
%10);
118 static HRESULT
Array_get_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
120 TRACE("%p\n", jsthis
);
122 *r
= jsval_number(array_from_jsdisp(jsthis
)->length
);
126 static HRESULT
Array_set_length(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t value
)
128 ArrayInstance
*This
= array_from_jsdisp(jsthis
);
133 TRACE("%p %d\n", This
, This
->length
);
135 hres
= to_number(ctx
, value
, &len
);
141 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
143 for(i
=len
; i
< This
->length
; i
++) {
144 hres
= jsdisp_delete_idx(&This
->dispex
, i
);
153 static HRESULT
concat_array(jsdisp_t
*array
, ArrayInstance
*obj
, DWORD
*len
)
159 for(i
=0; i
< obj
->length
; i
++) {
160 hres
= jsdisp_get_idx(&obj
->dispex
, i
, &val
);
161 if(hres
== DISP_E_UNKNOWNNAME
)
166 hres
= jsdisp_propput_idx(array
, *len
+i
, val
);
176 static HRESULT
concat_obj(jsdisp_t
*array
, IDispatch
*obj
, DWORD
*len
)
181 jsobj
= iface_to_jsdisp(obj
);
183 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
184 hres
= concat_array(array
, array_from_jsdisp(jsobj
), len
);
185 jsdisp_release(jsobj
);
188 jsdisp_release(jsobj
);
191 return jsdisp_propput_idx(array
, (*len
)++, jsval_disp(obj
));
194 static HRESULT
Array_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
203 hres
= create_array(ctx
, 0, &ret
);
207 hres
= concat_obj(ret
, jsthis
->u
.disp
, &len
);
208 if(SUCCEEDED(hres
)) {
211 for(i
=0; i
< argc
; i
++) {
212 if(is_object_instance(argv
[i
]))
213 hres
= concat_obj(ret
, get_object(argv
[i
]), &len
);
215 hres
= jsdisp_propput_idx(ret
, len
++, argv
[i
]);
231 static HRESULT
array_join(script_ctx_t
*ctx
, jsdisp_t
*array
, DWORD length
, const WCHAR
*sep
,
232 unsigned seplen
, jsval_t
*r
)
234 jsstr_t
**str_tab
, *ret
= NULL
;
237 HRESULT hres
= E_FAIL
;
241 *r
= jsval_string(jsstr_empty());
245 str_tab
= heap_alloc_zero(length
* sizeof(*str_tab
));
247 return E_OUTOFMEMORY
;
249 for(i
=0; i
< length
; i
++) {
250 hres
= jsdisp_get_idx(array
, i
, &val
);
251 if(hres
== DISP_E_UNKNOWNNAME
) {
254 } else if(FAILED(hres
))
257 if(!is_undefined(val
) && !is_null(val
)) {
258 hres
= to_string(ctx
, val
, str_tab
+i
);
265 if(SUCCEEDED(hres
)) {
269 len
= jsstr_length(str_tab
[0]);
270 for(i
=1; i
< length
; i
++) {
273 len
+= jsstr_length(str_tab
[i
]);
274 if(len
> JSSTR_MAX_LENGTH
) {
275 hres
= E_OUTOFMEMORY
;
280 if(SUCCEEDED(hres
)) {
283 ret
= jsstr_alloc_buf(len
, &ptr
);
286 ptr
+= jsstr_flush(str_tab
[0], ptr
);
288 for(i
=1; i
< length
; i
++) {
290 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
295 ptr
+= jsstr_flush(str_tab
[i
], ptr
);
298 hres
= E_OUTOFMEMORY
;
303 for(i
=0; i
< length
; i
++) {
305 jsstr_release(str_tab
[i
]);
311 TRACE("= %s\n", debugstr_jsstr(ret
));
314 *r
= jsval_string(ret
);
320 /* ECMA-262 3rd Edition 15.4.4.5 */
321 static HRESULT
Array_join(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
330 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
338 hres
= to_flat_string(ctx
, argv
[0], &sep_str
, &sep
);
342 hres
= array_join(ctx
, jsthis
, length
, sep
, jsstr_length(sep_str
), r
);
344 jsstr_release(sep_str
);
346 hres
= array_join(ctx
, jsthis
, length
, default_separatorW
, strlenW(default_separatorW
), r
);
352 static HRESULT
Array_pop(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
362 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
367 hres
= set_length(jsthis
, 0);
372 *r
= jsval_undefined();
377 hres
= jsdisp_get_idx(jsthis
, length
, &val
);
379 hres
= jsdisp_delete_idx(jsthis
, length
);
380 else if(hres
== DISP_E_UNKNOWNNAME
)
381 val
= jsval_undefined();
386 hres
= set_length(jsthis
, length
);
400 /* ECMA-262 3rd Edition 15.4.4.7 */
401 static HRESULT
Array_push(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
411 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
415 for(i
=0; i
< argc
; i
++) {
416 hres
= jsdisp_propput_idx(jsthis
, length
+i
, argv
[i
]);
421 hres
= set_length(jsthis
, length
+argc
);
426 *r
= jsval_number(length
+argc
);
430 static HRESULT
Array_reverse(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
436 HRESULT hres1
, hres2
;
440 hres1
= get_length(ctx
, vthis
, &jsthis
, &length
);
444 for(k
=0; k
<length
/2; k
++) {
447 hres1
= jsdisp_get_idx(jsthis
, k
, &v1
);
448 if(FAILED(hres1
) && hres1
!=DISP_E_UNKNOWNNAME
)
451 hres2
= jsdisp_get_idx(jsthis
, l
, &v2
);
452 if(FAILED(hres2
) && hres2
!=DISP_E_UNKNOWNNAME
) {
457 if(hres1
== DISP_E_UNKNOWNNAME
)
458 hres1
= jsdisp_delete_idx(jsthis
, l
);
460 hres1
= jsdisp_propput_idx(jsthis
, l
, v1
);
468 if(hres2
== DISP_E_UNKNOWNNAME
)
469 hres2
= jsdisp_delete_idx(jsthis
, k
);
471 hres2
= jsdisp_propput_idx(jsthis
, k
, v2
);
480 *r
= jsval_obj(jsdisp_addref(jsthis
));
484 /* ECMA-262 3rd Edition 15.4.4.9 */
485 static HRESULT
Array_shift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
495 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
500 hres
= set_length(jsthis
, 0);
505 *r
= jsval_undefined();
509 hres
= jsdisp_get_idx(jsthis
, 0, &ret
);
510 if(hres
== DISP_E_UNKNOWNNAME
) {
511 ret
= jsval_undefined();
515 for(i
=1; SUCCEEDED(hres
) && i
<length
; i
++) {
516 hres
= jsdisp_get_idx(jsthis
, i
, &v
);
517 if(hres
== DISP_E_UNKNOWNNAME
)
518 hres
= jsdisp_delete_idx(jsthis
, i
-1);
519 else if(SUCCEEDED(hres
))
520 hres
= jsdisp_propput_idx(jsthis
, i
-1, v
);
523 if(SUCCEEDED(hres
)) {
524 hres
= jsdisp_delete_idx(jsthis
, length
-1);
526 hres
= set_length(jsthis
, length
-1);
539 /* ECMA-262 3rd Edition 15.4.4.10 */
540 static HRESULT
Array_slice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
542 jsdisp_t
*arr
, *jsthis
;
544 DWORD length
, start
, end
, idx
;
549 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
554 hres
= to_number(ctx
, argv
[0], &range
);
558 range
= floor(range
);
559 if(-range
>length
|| isnan(range
)) start
= 0;
560 else if(range
< 0) start
= range
+length
;
561 else if(range
<= length
) start
= range
;
567 hres
= to_number(ctx
, argv
[1], &range
);
571 range
= floor(range
);
572 if(-range
>length
) end
= 0;
573 else if(range
< 0) end
= range
+length
;
574 else if(range
<= length
) end
= range
;
579 hres
= create_array(ctx
, (end
>start
)?end
-start
:0, &arr
);
583 for(idx
=start
; idx
<end
; idx
++) {
586 hres
= jsdisp_get_idx(jsthis
, idx
, &v
);
587 if(hres
== DISP_E_UNKNOWNNAME
)
590 if(SUCCEEDED(hres
)) {
591 hres
= jsdisp_propput_idx(arr
, idx
-start
, v
);
609 static HRESULT
sort_cmp(script_ctx_t
*ctx
, jsdisp_t
*cmp_func
, jsval_t v1
, jsval_t v2
, INT
*cmp
)
621 hres
= jsdisp_call_value(cmp_func
, NULL
, DISPATCH_METHOD
, 2, args
, &res
);
625 hres
= to_number(ctx
, res
, &n
);
632 *cmp
= n
> 0.0 ? 1 : -1;
633 }else if(is_undefined(v1
)) {
634 *cmp
= is_undefined(v2
) ? 0 : 1;
635 }else if(is_undefined(v2
)) {
637 }else if(is_number(v1
) && is_number(v2
)) {
638 double d
= get_number(v1
)-get_number(v2
);
642 *cmp
= d
< -0.0 ? -1 : 0;
646 hres
= to_string(ctx
, v1
, &x
);
650 hres
= to_string(ctx
, v2
, &y
);
651 if(SUCCEEDED(hres
)) {
652 *cmp
= jsstr_cmp(x
, y
);
663 /* ECMA-262 3rd Edition 15.4.4.11 */
664 static HRESULT
Array_sort(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
667 jsdisp_t
*jsthis
, *cmp_func
= NULL
;
668 jsval_t
*vtab
, **sorttab
= NULL
;
675 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
680 WARN("invalid arg_cnt %d\n", argc
);
685 if(!is_object_instance(argv
[0])) {
686 WARN("arg is not dispatch\n");
690 cmp_func
= iface_to_jsdisp(get_object(argv
[0]));
691 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
692 WARN("cmp_func is not a function\n");
694 jsdisp_release(cmp_func
);
701 jsdisp_release(cmp_func
);
703 *r
= jsval_obj(jsdisp_addref(jsthis
));
707 vtab
= heap_alloc_zero(length
* sizeof(*vtab
));
709 for(i
=0; i
<length
; i
++) {
710 hres
= jsdisp_get_idx(jsthis
, i
, vtab
+i
);
711 if(hres
== DISP_E_UNKNOWNNAME
) {
712 vtab
[i
] = jsval_undefined();
714 } else if(FAILED(hres
)) {
715 WARN("Could not get elem %d: %08x\n", i
, hres
);
720 hres
= E_OUTOFMEMORY
;
723 if(SUCCEEDED(hres
)) {
724 sorttab
= heap_alloc(length
*2*sizeof(*sorttab
));
726 hres
= E_OUTOFMEMORY
;
730 if(SUCCEEDED(hres
)) {
731 jsval_t
*tmpv
, **tmpbuf
;
734 tmpbuf
= sorttab
+ length
;
735 for(i
=0; i
< length
; i
++)
738 for(i
=0; i
< length
/2; i
++) {
739 hres
= sort_cmp(ctx
, cmp_func
, *sorttab
[2*i
+1], *sorttab
[2*i
], &cmp
);
745 sorttab
[2*i
] = sorttab
[2*i
+1];
746 sorttab
[2*i
+1] = tmpv
;
750 if(SUCCEEDED(hres
)) {
753 for(k
=2; k
< length
; k
*= 2) {
754 for(i
=0; i
+k
< length
; i
+= 2*k
) {
759 bend
= length
- (i
+k
);
761 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(jsval_t
*));
763 while(a
< k
&& b
< bend
) {
764 hres
= sort_cmp(ctx
, cmp_func
, *tmpbuf
[a
], *sorttab
[i
+k
+b
], &cmp
);
769 sorttab
[i
+a
+b
] = tmpbuf
[a
];
772 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
781 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(jsval_t
*));
789 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
790 hres
= jsdisp_propput_idx(jsthis
, i
, *sorttab
[i
]);
794 for(i
=0; i
< length
; i
++)
795 jsval_release(vtab
[i
]);
800 jsdisp_release(cmp_func
);
806 *r
= jsval_obj(jsdisp_addref(jsthis
));
810 /* ECMA-262 3rd Edition 15.4.4.12 */
811 static HRESULT
Array_splice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
814 DWORD length
, start
=0, delete_cnt
=0, i
, add_args
= 0;
815 jsdisp_t
*ret_array
= NULL
, *jsthis
;
823 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
828 hres
= to_integer(ctx
, argv
[0], &d
);
834 start
= min(n
, length
);
836 start
= -n
> length
? 0 : length
+ n
;
838 start
= d
< 0.0 ? 0 : length
;
843 hres
= to_integer(ctx
, argv
[1], &d
);
849 delete_cnt
= min(n
, length
-start
);
851 delete_cnt
= length
-start
;
858 hres
= create_array(ctx
, 0, &ret_array
);
862 for(i
=0; SUCCEEDED(hres
) && i
< delete_cnt
; i
++) {
863 hres
= jsdisp_get_idx(jsthis
, start
+i
, &val
);
864 if(hres
== DISP_E_UNKNOWNNAME
) {
866 }else if(SUCCEEDED(hres
)) {
867 hres
= jsdisp_propput_idx(ret_array
, i
, val
);
873 hres
= jsdisp_propput_name(ret_array
, lengthW
, jsval_number(delete_cnt
));
876 if(add_args
< delete_cnt
) {
877 for(i
= start
; SUCCEEDED(hres
) && i
< length
-delete_cnt
; i
++) {
878 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
, &val
);
879 if(hres
== DISP_E_UNKNOWNNAME
) {
880 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
);
881 }else if(SUCCEEDED(hres
)) {
882 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
, val
);
887 for(i
=length
; SUCCEEDED(hres
) && i
!= length
-delete_cnt
+add_args
; i
--)
888 hres
= jsdisp_delete_idx(jsthis
, i
-1);
889 }else if(add_args
> delete_cnt
) {
890 for(i
=length
-delete_cnt
; SUCCEEDED(hres
) && i
!= start
; i
--) {
891 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
-1, &val
);
892 if(hres
== DISP_E_UNKNOWNNAME
) {
893 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
-1);
894 }else if(SUCCEEDED(hres
)) {
895 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
-1, val
);
901 for(i
=0; SUCCEEDED(hres
) && i
< add_args
; i
++)
902 hres
= jsdisp_propput_idx(jsthis
, start
+i
, argv
[i
+2]);
905 hres
= jsdisp_propput_name(jsthis
, lengthW
, jsval_number(length
-delete_cnt
+add_args
));
909 jsdisp_release(ret_array
);
914 *r
= jsval_obj(ret_array
);
918 /* ECMA-262 3rd Edition 15.4.4.2 */
919 static HRESULT
Array_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
922 ArrayInstance
*array
;
926 array
= array_this(jsthis
);
928 return throw_type_error(ctx
, JS_E_ARRAY_EXPECTED
, NULL
);
930 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
,
931 strlenW(default_separatorW
), r
);
934 static HRESULT
Array_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
941 /* ECMA-262 3rd Edition 15.4.4.13 */
942 static HRESULT
Array_unshift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
946 WCHAR buf
[14], *buf_end
, *str
;
954 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
959 buf_end
= buf
+ sizeof(buf
)/sizeof(WCHAR
)-1;
964 str
= idx_to_str(i
, buf_end
);
966 hres
= jsdisp_get_id(jsthis
, str
, 0, &id
);
967 if(SUCCEEDED(hres
)) {
968 hres
= jsdisp_propget(jsthis
, id
, &val
);
972 hres
= jsdisp_propput_idx(jsthis
, i
+argc
, val
);
974 }else if(hres
== DISP_E_UNKNOWNNAME
) {
975 hres
= IDispatchEx_DeleteMemberByDispID(vthis
->u
.dispex
, id
);
983 for(i
=0; i
<argc
; i
++) {
984 hres
= jsdisp_propput_idx(jsthis
, i
, argv
[i
]);
991 hres
= set_length(jsthis
, length
);
997 *r
= ctx
->version
< 2 ? jsval_undefined() : jsval_number(length
);
1001 static HRESULT
Array_get_value(script_ctx_t
*ctx
, jsdisp_t
*jsthis
, jsval_t
*r
)
1003 ArrayInstance
*array
= array_from_jsdisp(jsthis
);
1007 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
,
1008 strlenW(default_separatorW
), r
);
1011 static void Array_destructor(jsdisp_t
*dispex
)
1016 static void Array_on_put(jsdisp_t
*dispex
, const WCHAR
*name
)
1018 ArrayInstance
*array
= array_from_jsdisp(dispex
);
1019 const WCHAR
*ptr
= name
;
1025 while(*ptr
&& isdigitW(*ptr
)) {
1026 id
= id
*10 + (*ptr
-'0');
1033 if(id
>= array
->length
)
1034 array
->length
= id
+1;
1037 static const builtin_prop_t Array_props
[] = {
1038 {concatW
, Array_concat
, PROPF_METHOD
|1},
1039 {joinW
, Array_join
, PROPF_METHOD
|1},
1040 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
},
1041 {popW
, Array_pop
, PROPF_METHOD
},
1042 {pushW
, Array_push
, PROPF_METHOD
|1},
1043 {reverseW
, Array_reverse
, PROPF_METHOD
},
1044 {shiftW
, Array_shift
, PROPF_METHOD
},
1045 {sliceW
, Array_slice
, PROPF_METHOD
|2},
1046 {sortW
, Array_sort
, PROPF_METHOD
|1},
1047 {spliceW
, Array_splice
, PROPF_METHOD
|2},
1048 {toLocaleStringW
, Array_toLocaleString
, PROPF_METHOD
},
1049 {toStringW
, Array_toString
, PROPF_METHOD
},
1050 {unshiftW
, Array_unshift
, PROPF_METHOD
|1},
1053 static const builtin_info_t Array_info
= {
1055 {NULL
, NULL
,0, Array_get_value
},
1056 sizeof(Array_props
)/sizeof(*Array_props
),
1062 static const builtin_prop_t ArrayInst_props
[] = {
1063 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
}
1066 static const builtin_info_t ArrayInst_info
= {
1068 {NULL
, NULL
,0, Array_get_value
},
1069 sizeof(ArrayInst_props
)/sizeof(*ArrayInst_props
),
1075 static HRESULT
ArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1085 case DISPATCH_METHOD
:
1086 case DISPATCH_CONSTRUCT
: {
1087 if(argc
== 1 && is_number(argv
[0])) {
1088 double n
= get_number(argv
[0]);
1090 if(n
< 0 || !is_int32(n
))
1091 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
1093 hres
= create_array(ctx
, n
, &obj
);
1097 *r
= jsval_obj(obj
);
1101 hres
= create_array(ctx
, argc
, &obj
);
1105 for(i
=0; i
< argc
; i
++) {
1106 hres
= jsdisp_propput_idx(obj
, i
, argv
[i
]);
1111 jsdisp_release(obj
);
1115 *r
= jsval_obj(obj
);
1119 FIXME("unimplemented flags: %x\n", flags
);
1126 static HRESULT
alloc_array(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, ArrayInstance
**ret
)
1128 ArrayInstance
*array
;
1131 array
= heap_alloc_zero(sizeof(ArrayInstance
));
1133 return E_OUTOFMEMORY
;
1135 if(object_prototype
)
1136 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, object_prototype
);
1138 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &ArrayInst_info
, ctx
->array_constr
);
1149 HRESULT
create_array_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1151 ArrayInstance
*array
;
1154 static const WCHAR ArrayW
[] = {'A','r','r','a','y',0};
1156 hres
= alloc_array(ctx
, object_prototype
, &array
);
1160 hres
= create_builtin_constructor(ctx
, ArrayConstr_value
, ArrayW
, NULL
, PROPF_CONSTR
|1, &array
->dispex
, ret
);
1162 jsdisp_release(&array
->dispex
);
1166 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, jsdisp_t
**ret
)
1168 ArrayInstance
*array
;
1171 hres
= alloc_array(ctx
, NULL
, &array
);
1175 array
->length
= length
;
1177 *ret
= &array
->dispex
;