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 ((ArrayInstance
*)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((IUnknown
*)obj
);
193 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
194 hres
= concat_array(array
, (ArrayInstance
*)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
, jsval_t
*r
)
243 jsstr_t
**str_tab
, *ret
;
246 HRESULT hres
= E_FAIL
;
250 *r
= jsval_string(jsstr_empty());
254 str_tab
= heap_alloc_zero(length
* sizeof(*str_tab
));
256 return E_OUTOFMEMORY
;
258 for(i
=0; i
< length
; i
++) {
259 hres
= jsdisp_get_idx(array
, i
, &val
);
260 if(hres
== DISP_E_UNKNOWNNAME
) {
263 } else if(FAILED(hres
))
266 if(!is_undefined(val
) && !is_null(val
)) {
267 hres
= to_string(ctx
, val
, str_tab
+i
);
274 if(SUCCEEDED(hres
)) {
275 DWORD seplen
= 0, len
= 0;
277 seplen
= strlenW(sep
);
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 ptr
= jsstr_alloc_buf(len
, &ret
);
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
, r
);
355 jsstr_release(sep_str
);
357 hres
= array_join(ctx
, jsthis
, length
, 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();
397 hres
= set_length(jsthis
, length
);
411 /* ECMA-262 3rd Edition 15.4.4.7 */
412 static HRESULT
Array_push(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
422 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
426 for(i
=0; i
< argc
; i
++) {
427 hres
= jsdisp_propput_idx(jsthis
, length
+i
, argv
[i
]);
432 hres
= set_length(jsthis
, length
+argc
);
437 *r
= jsval_number(length
+argc
);
441 static HRESULT
Array_reverse(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
447 HRESULT hres1
, hres2
;
451 hres1
= get_length(ctx
, vthis
, &jsthis
, &length
);
455 for(k
=0; k
<length
/2; k
++) {
458 hres1
= jsdisp_get_idx(jsthis
, k
, &v1
);
459 if(FAILED(hres1
) && hres1
!=DISP_E_UNKNOWNNAME
)
462 hres2
= jsdisp_get_idx(jsthis
, l
, &v2
);
463 if(FAILED(hres2
) && hres2
!=DISP_E_UNKNOWNNAME
) {
468 if(hres1
== DISP_E_UNKNOWNNAME
)
469 hres1
= jsdisp_delete_idx(jsthis
, l
);
471 hres1
= jsdisp_propput_idx(jsthis
, l
, v1
);
479 if(hres2
== DISP_E_UNKNOWNNAME
)
480 hres2
= jsdisp_delete_idx(jsthis
, k
);
482 hres2
= jsdisp_propput_idx(jsthis
, k
, v2
);
491 *r
= jsval_obj(jsdisp_addref(jsthis
));
495 /* ECMA-262 3rd Edition 15.4.4.9 */
496 static HRESULT
Array_shift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
506 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
511 hres
= set_length(jsthis
, 0);
516 *r
= jsval_undefined();
520 hres
= jsdisp_get_idx(jsthis
, 0, &ret
);
521 if(hres
== DISP_E_UNKNOWNNAME
) {
522 ret
= jsval_undefined();
526 for(i
=1; SUCCEEDED(hres
) && i
<length
; i
++) {
527 hres
= jsdisp_get_idx(jsthis
, i
, &v
);
528 if(hres
== DISP_E_UNKNOWNNAME
)
529 hres
= jsdisp_delete_idx(jsthis
, i
-1);
530 else if(SUCCEEDED(hres
))
531 hres
= jsdisp_propput_idx(jsthis
, i
-1, v
);
534 if(SUCCEEDED(hres
)) {
535 hres
= jsdisp_delete_idx(jsthis
, length
-1);
537 hres
= set_length(jsthis
, length
-1);
550 /* ECMA-262 3rd Edition 15.4.4.10 */
551 static HRESULT
Array_slice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
, jsval_t
*r
)
553 jsdisp_t
*arr
, *jsthis
;
555 DWORD length
, start
, end
, idx
;
560 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
565 hres
= to_number(ctx
, argv
[0], &range
);
569 range
= floor(range
);
570 if(-range
>length
|| isnan(range
)) start
= 0;
571 else if(range
< 0) start
= range
+length
;
572 else if(range
<= length
) start
= range
;
578 hres
= to_number(ctx
, argv
[1], &range
);
582 range
= floor(range
);
583 if(-range
>length
) end
= 0;
584 else if(range
< 0) end
= range
+length
;
585 else if(range
<= length
) end
= range
;
590 hres
= create_array(ctx
, (end
>start
)?end
-start
:0, &arr
);
594 for(idx
=start
; idx
<end
; idx
++) {
597 hres
= jsdisp_get_idx(jsthis
, idx
, &v
);
598 if(hres
== DISP_E_UNKNOWNNAME
)
601 if(SUCCEEDED(hres
)) {
602 hres
= jsdisp_propput_idx(arr
, idx
-start
, v
);
620 static HRESULT
sort_cmp(script_ctx_t
*ctx
, jsdisp_t
*cmp_func
, jsval_t v1
, jsval_t v2
, INT
*cmp
)
625 jsval_t args
[2] = {v1
, v2
};
629 hres
= jsdisp_call_value(cmp_func
, NULL
, DISPATCH_METHOD
, 2, args
, &res
);
633 hres
= to_number(ctx
, res
, &n
);
640 *cmp
= n
> 0.0 ? 1 : -1;
641 }else if(is_undefined(v1
)) {
642 *cmp
= is_undefined(v2
) ? 0 : 1;
643 }else if(is_undefined(v2
)) {
645 }else if(is_number(v1
) && is_number(v2
)) {
646 double d
= get_number(v1
)-get_number(v2
);
650 *cmp
= d
< -0.0 ? -1 : 0;
654 hres
= to_string(ctx
, v1
, &x
);
658 hres
= to_string(ctx
, v2
, &y
);
659 if(SUCCEEDED(hres
)) {
660 *cmp
= jsstr_cmp(x
, y
);
671 /* ECMA-262 3rd Edition 15.4.4.11 */
672 static HRESULT
Array_sort(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
675 jsdisp_t
*jsthis
, *cmp_func
= NULL
;
676 jsval_t
*vtab
, **sorttab
= NULL
;
683 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
688 WARN("invalid arg_cnt %d\n", argc
);
693 if(!is_object_instance(argv
[0])) {
694 WARN("arg is not dispatch\n");
698 cmp_func
= iface_to_jsdisp((IUnknown
*)get_object(argv
[0]));
699 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
700 WARN("cmp_func is not a function\n");
702 jsdisp_release(cmp_func
);
709 jsdisp_release(cmp_func
);
711 *r
= jsval_obj(jsdisp_addref(jsthis
));
715 vtab
= heap_alloc_zero(length
* sizeof(*vtab
));
717 for(i
=0; i
<length
; i
++) {
718 hres
= jsdisp_get_idx(jsthis
, i
, vtab
+i
);
719 if(hres
== DISP_E_UNKNOWNNAME
) {
720 vtab
[i
] = jsval_undefined();
722 } else if(FAILED(hres
)) {
723 WARN("Could not get elem %d: %08x\n", i
, hres
);
728 hres
= E_OUTOFMEMORY
;
731 if(SUCCEEDED(hres
)) {
732 sorttab
= heap_alloc(length
*2*sizeof(*sorttab
));
734 hres
= E_OUTOFMEMORY
;
738 if(SUCCEEDED(hres
)) {
739 jsval_t
*tmpv
, **tmpbuf
;
742 tmpbuf
= sorttab
+ length
;
743 for(i
=0; i
< length
; i
++)
746 for(i
=0; i
< length
/2; i
++) {
747 hres
= sort_cmp(ctx
, cmp_func
, *sorttab
[2*i
+1], *sorttab
[2*i
], &cmp
);
753 sorttab
[2*i
] = sorttab
[2*i
+1];
754 sorttab
[2*i
+1] = tmpv
;
758 if(SUCCEEDED(hres
)) {
761 for(k
=2; k
< length
; k
*= 2) {
762 for(i
=0; i
+k
< length
; i
+= 2*k
) {
767 bend
= length
- (i
+k
);
769 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(jsval_t
*));
771 while(a
< k
&& b
< bend
) {
772 hres
= sort_cmp(ctx
, cmp_func
, *tmpbuf
[a
], *sorttab
[i
+k
+b
], &cmp
);
777 sorttab
[i
+a
+b
] = tmpbuf
[a
];
780 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
789 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(jsval_t
*));
797 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
798 hres
= jsdisp_propput_idx(jsthis
, i
, *sorttab
[i
]);
802 for(i
=0; i
< length
; i
++)
803 jsval_release(vtab
[i
]);
808 jsdisp_release(cmp_func
);
814 *r
= jsval_obj(jsdisp_addref(jsthis
));
818 /* ECMA-262 3rd Edition 15.4.4.12 */
819 static HRESULT
Array_splice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
822 DWORD length
, start
=0, delete_cnt
=0, i
, add_args
= 0;
823 jsdisp_t
*ret_array
= NULL
, *jsthis
;
831 hres
= get_length(ctx
, vthis
, &jsthis
, &length
);
836 hres
= to_integer(ctx
, argv
[0], &d
);
842 start
= min(n
, length
);
844 start
= -n
> length
? 0 : length
+ n
;
846 start
= d
< 0.0 ? 0 : length
;
851 hres
= to_integer(ctx
, argv
[1], &d
);
857 delete_cnt
= min(n
, length
-start
);
859 delete_cnt
= length
-start
;
866 hres
= create_array(ctx
, 0, &ret_array
);
870 for(i
=0; SUCCEEDED(hres
) && i
< delete_cnt
; i
++) {
871 hres
= jsdisp_get_idx(jsthis
, start
+i
, &val
);
872 if(hres
== DISP_E_UNKNOWNNAME
) {
874 }else if(SUCCEEDED(hres
)) {
875 hres
= jsdisp_propput_idx(ret_array
, i
, val
);
881 hres
= jsdisp_propput_name(ret_array
, lengthW
, jsval_number(delete_cnt
));
884 if(add_args
< delete_cnt
) {
885 for(i
= start
; SUCCEEDED(hres
) && i
< length
-delete_cnt
; i
++) {
886 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
, &val
);
887 if(hres
== DISP_E_UNKNOWNNAME
) {
888 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
);
889 }else if(SUCCEEDED(hres
)) {
890 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
, val
);
895 for(i
=length
; SUCCEEDED(hres
) && i
!= length
-delete_cnt
+add_args
; i
--)
896 hres
= jsdisp_delete_idx(jsthis
, i
-1);
897 }else if(add_args
> delete_cnt
) {
898 for(i
=length
-delete_cnt
; SUCCEEDED(hres
) && i
!= start
; i
--) {
899 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
-1, &val
);
900 if(hres
== DISP_E_UNKNOWNNAME
) {
901 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
-1);
902 }else if(SUCCEEDED(hres
)) {
903 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
-1, val
);
909 for(i
=0; SUCCEEDED(hres
) && i
< add_args
; i
++)
910 hres
= jsdisp_propput_idx(jsthis
, start
+i
, argv
[i
+2]);
913 hres
= jsdisp_propput_name(jsthis
, lengthW
, jsval_number(length
-delete_cnt
+add_args
));
917 jsdisp_release(ret_array
);
922 *r
= jsval_obj(ret_array
);
926 /* ECMA-262 3rd Edition 15.4.4.2 */
927 static HRESULT
Array_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
930 ArrayInstance
*array
;
934 array
= array_this(jsthis
);
936 return throw_type_error(ctx
, JS_E_ARRAY_EXPECTED
, NULL
);
938 return array_join(ctx
, &array
->dispex
, array
->length
, 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
, r
);
1017 static void Array_destructor(jsdisp_t
*dispex
)
1022 static void Array_on_put(jsdisp_t
*dispex
, const WCHAR
*name
)
1024 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
1025 const WCHAR
*ptr
= name
;
1031 while(*ptr
&& isdigitW(*ptr
)) {
1032 id
= id
*10 + (*ptr
-'0');
1039 if(id
>= array
->length
)
1040 array
->length
= id
+1;
1043 static const builtin_prop_t Array_props
[] = {
1044 {concatW
, Array_concat
, PROPF_METHOD
|1},
1045 {joinW
, Array_join
, PROPF_METHOD
|1},
1046 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
},
1047 {popW
, Array_pop
, PROPF_METHOD
},
1048 {pushW
, Array_push
, PROPF_METHOD
|1},
1049 {reverseW
, Array_reverse
, PROPF_METHOD
},
1050 {shiftW
, Array_shift
, PROPF_METHOD
},
1051 {sliceW
, Array_slice
, PROPF_METHOD
|2},
1052 {sortW
, Array_sort
, PROPF_METHOD
|1},
1053 {spliceW
, Array_splice
, PROPF_METHOD
|2},
1054 {toLocaleStringW
, Array_toLocaleString
, PROPF_METHOD
},
1055 {toStringW
, Array_toString
, PROPF_METHOD
},
1056 {unshiftW
, Array_unshift
, PROPF_METHOD
|1},
1059 static const builtin_info_t Array_info
= {
1061 {NULL
, NULL
,0, Array_get_value
},
1062 sizeof(Array_props
)/sizeof(*Array_props
),
1068 static const builtin_prop_t ArrayInst_props
[] = {
1069 {lengthW
, NULL
,0, Array_get_length
, Array_set_length
}
1072 static const builtin_info_t ArrayInst_info
= {
1074 {NULL
, NULL
,0, Array_get_value
},
1075 sizeof(ArrayInst_props
)/sizeof(*ArrayInst_props
),
1081 static HRESULT
ArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, unsigned argc
, jsval_t
*argv
,
1091 case DISPATCH_METHOD
:
1092 case DISPATCH_CONSTRUCT
: {
1093 if(argc
== 1 && is_number(argv
[0])) {
1094 double n
= get_number(argv
[0]);
1096 if(n
< 0 || !is_int32(n
))
1097 return throw_range_error(ctx
, JS_E_INVALID_LENGTH
, NULL
);
1099 hres
= create_array(ctx
, n
, &obj
);
1103 *r
= jsval_obj(obj
);
1107 hres
= create_array(ctx
, argc
, &obj
);
1111 for(i
=0; i
< argc
; i
++) {
1112 hres
= jsdisp_propput_idx(obj
, i
, argv
[i
]);
1117 jsdisp_release(obj
);
1121 *r
= jsval_obj(obj
);
1125 FIXME("unimplemented flags: %x\n", flags
);
1132 static HRESULT
alloc_array(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, ArrayInstance
**ret
)
1134 ArrayInstance
*array
;
1137 array
= heap_alloc_zero(sizeof(ArrayInstance
));
1139 return E_OUTOFMEMORY
;
1141 if(object_prototype
)
1142 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, object_prototype
);
1144 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &ArrayInst_info
, ctx
->array_constr
);
1155 HRESULT
create_array_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1157 ArrayInstance
*array
;
1160 static const WCHAR ArrayW
[] = {'A','r','r','a','y',0};
1162 hres
= alloc_array(ctx
, object_prototype
, &array
);
1166 hres
= create_builtin_constructor(ctx
, ArrayConstr_value
, ArrayW
, NULL
, PROPF_CONSTR
|1, &array
->dispex
, ret
);
1168 jsdisp_release(&array
->dispex
);
1172 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, jsdisp_t
**ret
)
1174 ArrayInstance
*array
;
1177 hres
= alloc_array(ctx
, NULL
, &array
);
1181 array
->length
= length
;
1183 *ret
= &array
->dispex
;