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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
33 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
34 static const WCHAR concatW
[] = {'c','o','n','c','a','t',0};
35 static const WCHAR joinW
[] = {'j','o','i','n',0};
36 static const WCHAR popW
[] = {'p','o','p',0};
37 static const WCHAR pushW
[] = {'p','u','s','h',0};
38 static const WCHAR reverseW
[] = {'r','e','v','e','r','s','e',0};
39 static const WCHAR shiftW
[] = {'s','h','i','f','t',0};
40 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
41 static const WCHAR sortW
[] = {'s','o','r','t',0};
42 static const WCHAR spliceW
[] = {'s','p','l','i','c','e',0};
43 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
44 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
45 static const WCHAR unshiftW
[] = {'u','n','s','h','i','f','t',0};
47 static const WCHAR default_separatorW
[] = {',',0};
49 static inline ArrayInstance
*array_from_vdisp(vdisp_t
*vdisp
)
51 return (ArrayInstance
*)vdisp
->u
.jsdisp
;
54 static inline ArrayInstance
*array_this(vdisp_t
*jsthis
)
56 return is_vclass(jsthis
, JSCLASS_ARRAY
) ? array_from_vdisp(jsthis
) : NULL
;
59 static HRESULT
get_length(script_ctx_t
*ctx
, vdisp_t
*vdisp
, jsexcept_t
*ei
, jsdisp_t
**jsthis
, DWORD
*ret
)
65 array
= array_this(vdisp
);
67 *jsthis
= &array
->dispex
;
73 return throw_type_error(ctx
, ei
, JS_E_JSCRIPT_EXPECTED
, NULL
);
75 hres
= jsdisp_propget_name(vdisp
->u
.jsdisp
, lengthW
, &var
, ei
);
79 hres
= to_uint32(ctx
, &var
, ei
, ret
);
84 *jsthis
= vdisp
->u
.jsdisp
;
88 static HRESULT
set_length(jsdisp_t
*obj
, jsexcept_t
*ei
, DWORD length
)
92 if(is_class(obj
, JSCLASS_ARRAY
)) {
93 ((ArrayInstance
*)obj
)->length
= length
;
99 return jsdisp_propput_name(obj
, lengthW
, &var
, ei
);
102 static WCHAR
*idx_to_str(DWORD idx
, WCHAR
*ptr
)
110 *ptr
-- = '0' + (idx
%10);
117 static HRESULT
Array_length(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
118 VARIANT
*retv
, jsexcept_t
*ei
)
120 ArrayInstance
*This
= array_from_vdisp(jsthis
);
122 TRACE("%p %d\n", This
, This
->length
);
125 case DISPATCH_PROPERTYGET
:
127 V_I4(retv
) = This
->length
;
129 case DISPATCH_PROPERTYPUT
: {
134 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &len
);
140 return throw_range_error(ctx
, ei
, JS_E_INVALID_LENGTH
, NULL
);
142 for(i
=len
; i
<This
->length
; i
++) {
143 hres
= jsdisp_delete_idx(&This
->dispex
, i
);
152 FIXME("unimplemented flags %x\n", flags
);
159 static HRESULT
concat_array(jsdisp_t
*array
, ArrayInstance
*obj
, DWORD
*len
, jsexcept_t
*ei
)
165 for(i
=0; i
< obj
->length
; i
++) {
166 hres
= jsdisp_get_idx(&obj
->dispex
, i
, &var
, ei
);
167 if(hres
== DISP_E_UNKNOWNNAME
)
172 hres
= jsdisp_propput_idx(array
, *len
+i
, &var
, ei
);
182 static HRESULT
concat_obj(jsdisp_t
*array
, IDispatch
*obj
, DWORD
*len
, jsexcept_t
*ei
)
188 jsobj
= iface_to_jsdisp((IUnknown
*)obj
);
190 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
191 hres
= concat_array(array
, (ArrayInstance
*)jsobj
, len
, ei
);
192 jsdisp_release(jsobj
);
195 jsdisp_release(jsobj
);
198 V_VT(&var
) = VT_DISPATCH
;
199 V_DISPATCH(&var
) = obj
;
200 return jsdisp_propput_idx(array
, (*len
)++, &var
, ei
);
203 static HRESULT
Array_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
204 VARIANT
*retv
, jsexcept_t
*ei
)
212 hres
= create_array(ctx
, 0, &ret
);
216 hres
= concat_obj(ret
, jsthis
->u
.disp
, &len
, ei
);
217 if(SUCCEEDED(hres
)) {
221 for(i
=0; i
< arg_cnt(dp
); i
++) {
222 arg
= get_arg(dp
, i
);
223 if(V_VT(arg
) == VT_DISPATCH
)
224 hres
= concat_obj(ret
, V_DISPATCH(arg
), &len
, ei
);
226 hres
= jsdisp_propput_idx(ret
, len
++, arg
, ei
);
236 var_set_jsdisp(retv
, ret
);
242 static HRESULT
array_join(script_ctx_t
*ctx
, jsdisp_t
*array
, DWORD length
, const WCHAR
*sep
, VARIANT
*retv
, jsexcept_t
*ei
)
244 BSTR
*str_tab
, ret
= NULL
;
247 HRESULT hres
= E_FAIL
;
251 V_VT(retv
) = VT_BSTR
;
252 V_BSTR(retv
) = SysAllocStringLen(NULL
, 0);
254 return E_OUTOFMEMORY
;
259 str_tab
= heap_alloc_zero(length
* sizeof(BSTR
));
261 return E_OUTOFMEMORY
;
263 for(i
=0; i
< length
; i
++) {
264 hres
= jsdisp_get_idx(array
, i
, &var
, ei
);
265 if(hres
== DISP_E_UNKNOWNNAME
) {
268 } else if(FAILED(hres
))
271 if(V_VT(&var
) != VT_EMPTY
&& V_VT(&var
) != VT_NULL
)
272 hres
= to_string(ctx
, &var
, ei
, str_tab
+i
);
278 if(SUCCEEDED(hres
)) {
279 DWORD seplen
= 0, len
= 0;
282 seplen
= strlenW(sep
);
285 len
= SysStringLen(str_tab
[0]);
286 for(i
=1; i
< length
; i
++)
287 len
+= seplen
+ SysStringLen(str_tab
[i
]);
289 ret
= SysAllocStringLen(NULL
, len
);
294 tmplen
= SysStringLen(str_tab
[0]);
295 memcpy(ret
, str_tab
[0], tmplen
*sizeof(WCHAR
));
299 for(i
=1; i
< length
; i
++) {
301 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
306 tmplen
= SysStringLen(str_tab
[i
]);
307 memcpy(ptr
, str_tab
[i
], tmplen
*sizeof(WCHAR
));
313 hres
= E_OUTOFMEMORY
;
317 for(i
=0; i
< length
; i
++)
318 SysFreeString(str_tab
[i
]);
323 TRACE("= %s\n", debugstr_w(ret
));
327 ret
= SysAllocStringLen(NULL
, 0);
329 return E_OUTOFMEMORY
;
332 V_VT(retv
) = VT_BSTR
;
341 /* ECMA-262 3rd Edition 15.4.4.5 */
342 static HRESULT
Array_join(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
343 VARIANT
*retv
, jsexcept_t
*ei
)
351 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
358 hres
= to_string(ctx
, get_arg(dp
,0), ei
, &sep
);
362 hres
= array_join(ctx
, jsthis
, length
, sep
, retv
, ei
);
366 hres
= array_join(ctx
, jsthis
, length
, default_separatorW
, retv
, ei
);
372 static HRESULT
Array_pop(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
373 VARIANT
*retv
, jsexcept_t
*ei
)
382 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
387 hres
= set_length(jsthis
, ei
, 0);
392 V_VT(retv
) = VT_EMPTY
;
397 hres
= jsdisp_get_idx(jsthis
, length
, &val
, ei
);
398 if(SUCCEEDED(hres
)) {
399 hres
= jsdisp_delete_idx(jsthis
, length
);
400 } else if(hres
== DISP_E_UNKNOWNNAME
) {
401 V_VT(&val
) = VT_EMPTY
;
407 hres
= set_length(jsthis
, ei
, length
);
422 /* ECMA-262 3rd Edition 15.4.4.7 */
423 static HRESULT
Array_push(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
424 VARIANT
*retv
, jsexcept_t
*ei
)
433 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
438 for(i
=0; i
< n
; i
++) {
439 hres
= jsdisp_propput_idx(jsthis
, length
+i
, get_arg(dp
, i
), ei
);
444 hres
= set_length(jsthis
, ei
, length
+n
);
450 V_I4(retv
) = length
+n
;
455 static HRESULT
Array_reverse(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
456 VARIANT
*retv
, jsexcept_t
*ei
)
461 HRESULT hres1
, hres2
;
465 hres1
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
469 for(k
=0; k
<length
/2; k
++) {
472 hres1
= jsdisp_get_idx(jsthis
, k
, &v1
, ei
);
473 if(FAILED(hres1
) && hres1
!=DISP_E_UNKNOWNNAME
)
476 hres2
= jsdisp_get_idx(jsthis
, l
, &v2
, ei
);
477 if(FAILED(hres2
) && hres2
!=DISP_E_UNKNOWNNAME
) {
482 if(hres1
== DISP_E_UNKNOWNNAME
)
483 hres1
= jsdisp_delete_idx(jsthis
, l
);
485 hres1
= jsdisp_propput_idx(jsthis
, l
, &v1
, ei
);
493 if(hres2
== DISP_E_UNKNOWNNAME
)
494 hres2
= jsdisp_delete_idx(jsthis
, k
);
496 hres2
= jsdisp_propput_idx(jsthis
, k
, &v2
, ei
);
505 jsdisp_addref(jsthis
);
506 var_set_jsdisp(retv
, jsthis
);
512 /* ECMA-262 3rd Edition 15.4.4.9 */
513 static HRESULT
Array_shift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
514 VARIANT
*retv
, jsexcept_t
*ei
)
523 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
528 hres
= set_length(jsthis
, ei
, 0);
535 V_VT(retv
) = VT_EMPTY
;
539 hres
= jsdisp_get_idx(jsthis
, 0, &ret
, ei
);
540 if(hres
== DISP_E_UNKNOWNNAME
) {
541 V_VT(&ret
) = VT_EMPTY
;
545 for(i
=1; SUCCEEDED(hres
) && i
<length
; i
++) {
546 hres
= jsdisp_get_idx(jsthis
, i
, &v
, ei
);
547 if(hres
== DISP_E_UNKNOWNNAME
)
548 hres
= jsdisp_delete_idx(jsthis
, i
-1);
549 else if(SUCCEEDED(hres
))
550 hres
= jsdisp_propput_idx(jsthis
, i
-1, &v
, ei
);
553 if(SUCCEEDED(hres
)) {
554 hres
= jsdisp_delete_idx(jsthis
, length
-1);
556 hres
= set_length(jsthis
, ei
, length
-1);
559 if(SUCCEEDED(hres
) && retv
)
566 /* ECMA-262 3rd Edition 15.4.4.10 */
567 static HRESULT
Array_slice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
568 VARIANT
*retv
, jsexcept_t
*ei
)
570 jsdisp_t
*arr
, *jsthis
;
572 DWORD length
, start
, end
, idx
;
577 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
582 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &range
);
586 range
= floor(range
);
587 if(-range
>length
|| isnan(range
)) start
= 0;
588 else if(range
< 0) start
= range
+length
;
589 else if(range
<= length
) start
= range
;
595 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &range
);
599 range
= floor(range
);
600 if(-range
>length
) end
= 0;
601 else if(range
< 0) end
= range
+length
;
602 else if(range
<= length
) end
= range
;
607 hres
= create_array(ctx
, (end
>start
)?end
-start
:0, &arr
);
611 for(idx
=start
; idx
<end
; idx
++) {
614 hres
= jsdisp_get_idx(jsthis
, idx
, &v
, ei
);
615 if(hres
== DISP_E_UNKNOWNNAME
)
618 if(SUCCEEDED(hres
)) {
619 hres
= jsdisp_propput_idx(arr
, idx
-start
, &v
, ei
);
630 var_set_jsdisp(retv
, arr
);
637 static HRESULT
sort_cmp(script_ctx_t
*ctx
, jsdisp_t
*cmp_func
, VARIANT
*v1
, VARIANT
*v2
, jsexcept_t
*ei
, INT
*cmp
)
643 DISPPARAMS dp
= {args
, NULL
, 2, 0};
650 hres
= jsdisp_call_value(cmp_func
, DISPATCH_METHOD
, &dp
, &res
, ei
);
654 hres
= to_number(ctx
, &res
, ei
, &n
);
661 *cmp
= n
> 0.0 ? 1 : -1;
662 }else if(V_VT(v1
) == VT_EMPTY
) {
663 *cmp
= V_VT(v2
) == VT_EMPTY
? 0 : 1;
664 }else if(V_VT(v2
) == VT_EMPTY
) {
666 }else if(is_num_vt(V_VT(v1
)) && is_num_vt(V_VT(v2
))) {
667 DOUBLE d
= num_val(v1
)-num_val(v2
);
671 *cmp
= d
< -0.0 ? -1 : 0;
675 hres
= to_string(ctx
, v1
, ei
, &x
);
679 hres
= to_string(ctx
, v2
, ei
, &y
);
680 if(SUCCEEDED(hres
)) {
681 *cmp
= strcmpW(x
, y
);
692 /* ECMA-262 3rd Edition 15.4.4.11 */
693 static HRESULT
Array_sort(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
694 VARIANT
*retv
, jsexcept_t
*ei
)
696 jsdisp_t
*jsthis
, *cmp_func
= NULL
;
697 VARIANT
*vtab
, **sorttab
= NULL
;
704 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
708 if(arg_cnt(dp
) > 1) {
709 WARN("invalid arg_cnt %d\n", arg_cnt(dp
));
713 if(arg_cnt(dp
) == 1) {
714 VARIANT
*arg
= get_arg(dp
, 0);
716 if(V_VT(arg
) != VT_DISPATCH
) {
717 WARN("arg is not dispatch\n");
722 cmp_func
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
723 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
724 WARN("cmp_func is not a function\n");
726 jsdisp_release(cmp_func
);
733 jsdisp_release(cmp_func
);
735 jsdisp_addref(jsthis
);
736 var_set_jsdisp(retv
, jsthis
);
741 vtab
= heap_alloc_zero(length
* sizeof(VARIANT
));
743 for(i
=0; i
<length
; i
++) {
744 hres
= jsdisp_get_idx(jsthis
, i
, vtab
+i
, ei
);
745 if(hres
== DISP_E_UNKNOWNNAME
) {
746 V_VT(vtab
+i
) = VT_EMPTY
;
748 } else if(FAILED(hres
)) {
749 WARN("Could not get elem %d: %08x\n", i
, hres
);
754 hres
= E_OUTOFMEMORY
;
757 if(SUCCEEDED(hres
)) {
758 sorttab
= heap_alloc(length
*2*sizeof(VARIANT
*));
760 hres
= E_OUTOFMEMORY
;
764 if(SUCCEEDED(hres
)) {
765 VARIANT
*tmpv
, **tmpbuf
;
768 tmpbuf
= sorttab
+ length
;
769 for(i
=0; i
< length
; i
++)
772 for(i
=0; i
< length
/2; i
++) {
773 hres
= sort_cmp(ctx
, cmp_func
, sorttab
[2*i
+1], sorttab
[2*i
], ei
, &cmp
);
779 sorttab
[2*i
] = sorttab
[2*i
+1];
780 sorttab
[2*i
+1] = tmpv
;
784 if(SUCCEEDED(hres
)) {
787 for(k
=2; k
< length
; k
*= 2) {
788 for(i
=0; i
+k
< length
; i
+= 2*k
) {
793 bend
= length
- (i
+k
);
795 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(VARIANT
*));
797 while(a
< k
&& b
< bend
) {
798 hres
= sort_cmp(ctx
, cmp_func
, tmpbuf
[a
], sorttab
[i
+k
+b
], ei
, &cmp
);
803 sorttab
[i
+a
+b
] = tmpbuf
[a
];
806 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
815 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(VARIANT
*));
823 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
824 hres
= jsdisp_propput_idx(jsthis
, i
, sorttab
[i
], ei
);
828 for(i
=0; i
< length
; i
++)
829 VariantClear(vtab
+i
);
834 jsdisp_release(cmp_func
);
840 jsdisp_addref(jsthis
);
841 var_set_jsdisp(retv
, jsthis
);
847 /* ECMA-262 3rd Edition 15.4.4.12 */
848 static HRESULT
Array_splice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
849 VARIANT
*retv
, jsexcept_t
*ei
)
851 DWORD length
, start
=0, delete_cnt
=0, argc
, i
, add_args
= 0;
852 jsdisp_t
*ret_array
= NULL
, *jsthis
;
858 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
864 hres
= to_integer(ctx
, get_arg(dp
,0), ei
, &v
);
868 if(V_VT(&v
) == VT_I4
) {
870 start
= min(V_I4(&v
), length
);
872 start
= -V_I4(&v
) > length
? 0 : length
+ V_I4(&v
);
874 start
= V_R8(&v
) < 0.0 ? 0 : length
;
879 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &v
);
883 if(V_VT(&v
) == VT_I4
) {
885 delete_cnt
= min(V_I4(&v
), length
-start
);
886 }else if(V_R8(&v
) > 0.0) {
887 delete_cnt
= length
-start
;
894 hres
= create_array(ctx
, 0, &ret_array
);
898 for(i
=0; SUCCEEDED(hres
) && i
< delete_cnt
; i
++) {
899 hres
= jsdisp_get_idx(jsthis
, start
+i
, &v
, ei
);
900 if(hres
== DISP_E_UNKNOWNNAME
)
902 else if(SUCCEEDED(hres
))
903 hres
= jsdisp_propput_idx(ret_array
, i
, &v
, ei
);
906 if(SUCCEEDED(hres
)) {
908 V_I4(&v
) = delete_cnt
;
910 hres
= jsdisp_propput_name(ret_array
, lengthW
, &v
, ei
);
914 if(add_args
< delete_cnt
) {
915 for(i
= start
; SUCCEEDED(hres
) && i
< length
-delete_cnt
; i
++) {
916 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
, &v
, ei
);
917 if(hres
== DISP_E_UNKNOWNNAME
)
918 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
);
919 else if(SUCCEEDED(hres
))
920 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
, &v
, ei
);
923 for(i
=length
; SUCCEEDED(hres
) && i
!= length
-delete_cnt
+add_args
; i
--)
924 hres
= jsdisp_delete_idx(jsthis
, i
-1);
925 }else if(add_args
> delete_cnt
) {
926 for(i
=length
-delete_cnt
; SUCCEEDED(hres
) && i
!= start
; i
--) {
927 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
-1, &v
, ei
);
928 if(hres
== DISP_E_UNKNOWNNAME
)
929 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
-1);
930 else if(SUCCEEDED(hres
))
931 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
-1, &v
, ei
);
935 for(i
=0; SUCCEEDED(hres
) && i
< add_args
; i
++)
936 hres
= jsdisp_propput_idx(jsthis
, start
+i
, get_arg(dp
,i
+2), ei
);
938 if(SUCCEEDED(hres
)) {
940 V_I4(&v
) = length
-delete_cnt
+add_args
;
941 hres
= jsdisp_propput_name(jsthis
, lengthW
, &v
, ei
);
946 jsdisp_release(ret_array
);
951 var_set_jsdisp(retv
, ret_array
);
955 /* ECMA-262 3rd Edition 15.4.4.2 */
956 static HRESULT
Array_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
957 VARIANT
*retv
, jsexcept_t
*ei
)
959 ArrayInstance
*array
;
963 array
= array_this(jsthis
);
965 return throw_type_error(ctx
, ei
, JS_E_ARRAY_EXPECTED
, NULL
);
967 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
, retv
, ei
);
970 static HRESULT
Array_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
971 VARIANT
*retv
, jsexcept_t
*ei
)
977 /* ECMA-262 3rd Edition 15.4.4.13 */
978 static HRESULT
Array_unshift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
979 VARIANT
*retv
, jsexcept_t
*ei
)
982 WCHAR buf
[14], *buf_end
, *str
;
983 DWORD argc
, i
, length
;
990 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
996 buf_end
= buf
+ sizeof(buf
)/sizeof(WCHAR
)-1;
1001 str
= idx_to_str(i
, buf_end
);
1003 hres
= jsdisp_get_id(jsthis
, str
, 0, &id
);
1004 if(SUCCEEDED(hres
)) {
1005 hres
= jsdisp_propget(jsthis
, id
, &var
, ei
);
1009 hres
= jsdisp_propput_idx(jsthis
, i
+argc
, &var
, ei
);
1011 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1012 hres
= IDispatchEx_DeleteMemberByDispID(vthis
->u
.dispex
, id
);
1020 for(i
=0; i
<argc
; i
++) {
1021 hres
= jsdisp_propput_idx(jsthis
, i
, get_arg(dp
,i
), ei
);
1028 hres
= set_length(jsthis
, ei
, length
);
1034 if(ctx
->version
< 2) {
1035 V_VT(retv
) = VT_EMPTY
;
1038 V_I4(retv
) = length
;
1044 static HRESULT
Array_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1045 VARIANT
*retv
, jsexcept_t
*ei
)
1051 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
1052 case INVOKE_PROPERTYGET
:
1053 return array_join(ctx
, jsthis
->u
.jsdisp
, array_from_vdisp(jsthis
)->length
, default_separatorW
, retv
, ei
);
1055 FIXME("unimplemented flags %x\n", flags
);
1062 static void Array_destructor(jsdisp_t
*dispex
)
1067 static void Array_on_put(jsdisp_t
*dispex
, const WCHAR
*name
)
1069 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
1070 const WCHAR
*ptr
= name
;
1076 while(*ptr
&& isdigitW(*ptr
)) {
1077 id
= id
*10 + (*ptr
-'0');
1084 if(id
>= array
->length
)
1085 array
->length
= id
+1;
1088 static const builtin_prop_t Array_props
[] = {
1089 {concatW
, Array_concat
, PROPF_METHOD
|1},
1090 {joinW
, Array_join
, PROPF_METHOD
|1},
1091 {lengthW
, Array_length
, 0},
1092 {popW
, Array_pop
, PROPF_METHOD
},
1093 {pushW
, Array_push
, PROPF_METHOD
|1},
1094 {reverseW
, Array_reverse
, PROPF_METHOD
},
1095 {shiftW
, Array_shift
, PROPF_METHOD
},
1096 {sliceW
, Array_slice
, PROPF_METHOD
|2},
1097 {sortW
, Array_sort
, PROPF_METHOD
|1},
1098 {spliceW
, Array_splice
, PROPF_METHOD
|2},
1099 {toLocaleStringW
, Array_toLocaleString
, PROPF_METHOD
},
1100 {toStringW
, Array_toString
, PROPF_METHOD
},
1101 {unshiftW
, Array_unshift
, PROPF_METHOD
|1},
1104 static const builtin_info_t Array_info
= {
1106 {NULL
, Array_value
, 0},
1107 sizeof(Array_props
)/sizeof(*Array_props
),
1113 static HRESULT
ArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
1114 VARIANT
*retv
, jsexcept_t
*ei
)
1124 case DISPATCH_METHOD
:
1125 case DISPATCH_CONSTRUCT
: {
1126 if(arg_cnt(dp
) == 1 && V_VT((arg_var
= get_arg(dp
, 0))) == VT_I4
) {
1127 if(V_I4(arg_var
) < 0)
1128 return throw_range_error(ctx
, ei
, JS_E_INVALID_LENGTH
, NULL
);
1130 hres
= create_array(ctx
, V_I4(arg_var
), &obj
);
1134 var_set_jsdisp(retv
, obj
);
1138 hres
= create_array(ctx
, arg_cnt(dp
), &obj
);
1142 for(i
=0; i
< arg_cnt(dp
); i
++) {
1143 hres
= jsdisp_propput_idx(obj
, i
, get_arg(dp
, i
), ei
);
1148 jsdisp_release(obj
);
1152 var_set_jsdisp(retv
, obj
);
1156 FIXME("unimplemented flags: %x\n", flags
);
1163 static HRESULT
alloc_array(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, ArrayInstance
**ret
)
1165 ArrayInstance
*array
;
1168 array
= heap_alloc_zero(sizeof(ArrayInstance
));
1170 return E_OUTOFMEMORY
;
1172 if(object_prototype
)
1173 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, object_prototype
);
1175 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &Array_info
, ctx
->array_constr
);
1186 HRESULT
create_array_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1188 ArrayInstance
*array
;
1191 static const WCHAR ArrayW
[] = {'A','r','r','a','y',0};
1193 hres
= alloc_array(ctx
, object_prototype
, &array
);
1197 hres
= create_builtin_function(ctx
, ArrayConstr_value
, ArrayW
, NULL
, PROPF_CONSTR
|1, &array
->dispex
, ret
);
1199 jsdisp_release(&array
->dispex
);
1203 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, jsdisp_t
**ret
)
1205 ArrayInstance
*array
;
1208 hres
= alloc_array(ctx
, NULL
, &array
);
1212 array
->length
= length
;
1214 *ret
= &array
->dispex
;