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
, NULL
/*FIXME*/);
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
, NULL
/*FIXME*/);
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
, IServiceProvider
*sp
)
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
: {
135 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &num
);
136 if(V_VT(&num
) == VT_I4
)
139 len
= floor(V_R8(&num
));
142 return throw_range_error(ctx
, ei
, JS_E_INVALID_LENGTH
, NULL
);
144 for(i
=len
; i
<This
->length
; i
++) {
145 hres
= jsdisp_delete_idx(&This
->dispex
, i
);
154 FIXME("unimplemented flags %x\n", flags
);
161 static HRESULT
concat_array(jsdisp_t
*array
, ArrayInstance
*obj
, DWORD
*len
,
162 jsexcept_t
*ei
, IServiceProvider
*caller
)
168 for(i
=0; i
< obj
->length
; i
++) {
169 hres
= jsdisp_get_idx(&obj
->dispex
, i
, &var
, ei
, caller
);
170 if(hres
== DISP_E_UNKNOWNNAME
)
175 hres
= jsdisp_propput_idx(array
, *len
+i
, &var
, ei
, caller
);
185 static HRESULT
concat_obj(jsdisp_t
*array
, IDispatch
*obj
, DWORD
*len
, jsexcept_t
*ei
, IServiceProvider
*caller
)
191 jsobj
= iface_to_jsdisp((IUnknown
*)obj
);
193 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
194 hres
= concat_array(array
, (ArrayInstance
*)jsobj
, len
, ei
, caller
);
195 jsdisp_release(jsobj
);
198 jsdisp_release(jsobj
);
201 V_VT(&var
) = VT_DISPATCH
;
202 V_DISPATCH(&var
) = obj
;
203 return jsdisp_propput_idx(array
, (*len
)++, &var
, ei
, caller
);
206 static HRESULT
Array_concat(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
207 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
215 hres
= create_array(ctx
, 0, &ret
);
219 hres
= concat_obj(ret
, jsthis
->u
.disp
, &len
, ei
, caller
);
220 if(SUCCEEDED(hres
)) {
224 for(i
=0; i
< arg_cnt(dp
); i
++) {
225 arg
= get_arg(dp
, i
);
226 if(V_VT(arg
) == VT_DISPATCH
)
227 hres
= concat_obj(ret
, V_DISPATCH(arg
), &len
, ei
, caller
);
229 hres
= jsdisp_propput_idx(ret
, len
++, arg
, ei
, caller
);
239 var_set_jsdisp(retv
, ret
);
245 static HRESULT
array_join(script_ctx_t
*ctx
, jsdisp_t
*array
, DWORD length
, const WCHAR
*sep
, VARIANT
*retv
,
246 jsexcept_t
*ei
, IServiceProvider
*caller
)
248 BSTR
*str_tab
, ret
= NULL
;
251 HRESULT hres
= E_FAIL
;
255 V_VT(retv
) = VT_BSTR
;
256 V_BSTR(retv
) = SysAllocStringLen(NULL
, 0);
258 return E_OUTOFMEMORY
;
263 str_tab
= heap_alloc_zero(length
* sizeof(BSTR
));
265 return E_OUTOFMEMORY
;
267 for(i
=0; i
< length
; i
++) {
268 hres
= jsdisp_get_idx(array
, i
, &var
, ei
, caller
);
269 if(hres
== DISP_E_UNKNOWNNAME
) {
272 } else if(FAILED(hres
))
275 if(V_VT(&var
) != VT_EMPTY
&& V_VT(&var
) != VT_NULL
)
276 hres
= to_string(ctx
, &var
, ei
, str_tab
+i
);
282 if(SUCCEEDED(hres
)) {
283 DWORD seplen
= 0, len
= 0;
286 seplen
= strlenW(sep
);
289 len
= SysStringLen(str_tab
[0]);
290 for(i
=1; i
< length
; i
++)
291 len
+= seplen
+ SysStringLen(str_tab
[i
]);
293 ret
= SysAllocStringLen(NULL
, len
);
298 tmplen
= SysStringLen(str_tab
[0]);
299 memcpy(ret
, str_tab
[0], tmplen
*sizeof(WCHAR
));
303 for(i
=1; i
< length
; i
++) {
305 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
310 tmplen
= SysStringLen(str_tab
[i
]);
311 memcpy(ptr
, str_tab
[i
], tmplen
*sizeof(WCHAR
));
317 hres
= E_OUTOFMEMORY
;
321 for(i
=0; i
< length
; i
++)
322 SysFreeString(str_tab
[i
]);
327 TRACE("= %s\n", debugstr_w(ret
));
331 ret
= SysAllocStringLen(NULL
, 0);
333 return E_OUTOFMEMORY
;
336 V_VT(retv
) = VT_BSTR
;
345 /* ECMA-262 3rd Edition 15.4.4.5 */
346 static HRESULT
Array_join(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
347 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
355 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
362 hres
= to_string(ctx
, get_arg(dp
,0), ei
, &sep
);
366 hres
= array_join(ctx
, jsthis
, length
, sep
, retv
, ei
, caller
);
370 hres
= array_join(ctx
, jsthis
, length
, default_separatorW
, retv
, ei
, caller
);
376 static HRESULT
Array_pop(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
377 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
386 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
391 hres
= set_length(jsthis
, ei
, 0);
396 V_VT(retv
) = VT_EMPTY
;
401 hres
= jsdisp_get_idx(jsthis
, length
, &val
, ei
, caller
);
402 if(SUCCEEDED(hres
)) {
403 hres
= jsdisp_delete_idx(jsthis
, length
);
404 } else if(hres
== DISP_E_UNKNOWNNAME
) {
405 V_VT(&val
) = VT_EMPTY
;
411 hres
= set_length(jsthis
, ei
, length
);
426 /* ECMA-262 3rd Edition 15.4.4.7 */
427 static HRESULT
Array_push(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
428 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
437 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
442 for(i
=0; i
< n
; i
++) {
443 hres
= jsdisp_propput_idx(jsthis
, length
+i
, get_arg(dp
, i
), ei
, sp
);
448 hres
= set_length(jsthis
, ei
, length
+n
);
454 V_I4(retv
) = length
+n
;
459 static HRESULT
Array_reverse(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
460 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
465 HRESULT hres1
, hres2
;
469 hres1
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
473 for(k
=0; k
<length
/2; k
++) {
476 hres1
= jsdisp_get_idx(jsthis
, k
, &v1
, ei
, sp
);
477 if(FAILED(hres1
) && hres1
!=DISP_E_UNKNOWNNAME
)
480 hres2
= jsdisp_get_idx(jsthis
, l
, &v2
, ei
, sp
);
481 if(FAILED(hres2
) && hres2
!=DISP_E_UNKNOWNNAME
) {
486 if(hres1
== DISP_E_UNKNOWNNAME
)
487 hres1
= jsdisp_delete_idx(jsthis
, l
);
489 hres1
= jsdisp_propput_idx(jsthis
, l
, &v1
, ei
, sp
);
497 if(hres2
== DISP_E_UNKNOWNNAME
)
498 hres2
= jsdisp_delete_idx(jsthis
, k
);
500 hres2
= jsdisp_propput_idx(jsthis
, k
, &v2
, ei
, sp
);
509 jsdisp_addref(jsthis
);
510 var_set_jsdisp(retv
, jsthis
);
516 /* ECMA-262 3rd Edition 15.4.4.9 */
517 static HRESULT
Array_shift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
518 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
527 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
532 hres
= set_length(jsthis
, ei
, 0);
539 V_VT(retv
) = VT_EMPTY
;
543 hres
= jsdisp_get_idx(jsthis
, 0, &ret
, ei
, caller
);
544 if(hres
== DISP_E_UNKNOWNNAME
) {
545 V_VT(&ret
) = VT_EMPTY
;
549 for(i
=1; SUCCEEDED(hres
) && i
<length
; i
++) {
550 hres
= jsdisp_get_idx(jsthis
, i
, &v
, ei
, caller
);
551 if(hres
== DISP_E_UNKNOWNNAME
)
552 hres
= jsdisp_delete_idx(jsthis
, i
-1);
553 else if(SUCCEEDED(hres
))
554 hres
= jsdisp_propput_idx(jsthis
, i
-1, &v
, ei
, caller
);
557 if(SUCCEEDED(hres
)) {
558 hres
= jsdisp_delete_idx(jsthis
, length
-1);
560 hres
= set_length(jsthis
, ei
, length
-1);
563 if(SUCCEEDED(hres
) && retv
)
570 /* ECMA-262 3rd Edition 15.4.4.10 */
571 static HRESULT
Array_slice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
572 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
574 jsdisp_t
*arr
, *jsthis
;
577 DWORD length
, start
, end
, idx
;
582 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
587 hres
= to_number(ctx
, get_arg(dp
, 0), ei
, &v
);
591 if(V_VT(&v
) == VT_I4
)
594 range
= floor(V_R8(&v
));
596 if(-range
>length
|| isnan(range
)) start
= 0;
597 else if(range
< 0) start
= range
+length
;
598 else if(range
<= length
) start
= range
;
604 hres
= to_number(ctx
, get_arg(dp
, 1), ei
, &v
);
608 if(V_VT(&v
) == VT_I4
)
611 range
= floor(V_R8(&v
));
613 if(-range
>length
) end
= 0;
614 else if(range
< 0) end
= range
+length
;
615 else if(range
<= length
) end
= range
;
620 hres
= create_array(ctx
, (end
>start
)?end
-start
:0, &arr
);
624 for(idx
=start
; idx
<end
; idx
++) {
625 hres
= jsdisp_get_idx(jsthis
, idx
, &v
, ei
, sp
);
626 if(hres
== DISP_E_UNKNOWNNAME
)
629 if(SUCCEEDED(hres
)) {
630 hres
= jsdisp_propput_idx(arr
, idx
-start
, &v
, ei
, sp
);
641 var_set_jsdisp(retv
, arr
);
648 static HRESULT
sort_cmp(script_ctx_t
*ctx
, jsdisp_t
*cmp_func
, VARIANT
*v1
, VARIANT
*v2
, jsexcept_t
*ei
,
649 IServiceProvider
*caller
, INT
*cmp
)
655 DISPPARAMS dp
= {args
, NULL
, 2, 0};
662 hres
= jsdisp_call_value(cmp_func
, DISPATCH_METHOD
, &dp
, &res
, ei
, caller
);
666 hres
= to_number(ctx
, &res
, ei
, &tmp
);
671 if(V_VT(&tmp
) == VT_I4
)
674 *cmp
= V_R8(&tmp
) > 0.0 ? 1 : -1;
675 }else if(V_VT(v1
) == VT_EMPTY
) {
676 *cmp
= V_VT(v2
) == VT_EMPTY
? 0 : 1;
677 }else if(V_VT(v2
) == VT_EMPTY
) {
679 }else if(is_num_vt(V_VT(v1
)) && is_num_vt(V_VT(v2
))) {
680 DOUBLE d
= num_val(v1
)-num_val(v2
);
684 *cmp
= d
< -0.0 ? -1 : 0;
688 hres
= to_string(ctx
, v1
, ei
, &x
);
692 hres
= to_string(ctx
, v2
, ei
, &y
);
693 if(SUCCEEDED(hres
)) {
694 *cmp
= strcmpW(x
, y
);
705 /* ECMA-262 3rd Edition 15.4.4.11 */
706 static HRESULT
Array_sort(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
707 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
709 jsdisp_t
*jsthis
, *cmp_func
= NULL
;
710 VARIANT
*vtab
, **sorttab
= NULL
;
717 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
721 if(arg_cnt(dp
) > 1) {
722 WARN("invalid arg_cnt %d\n", arg_cnt(dp
));
726 if(arg_cnt(dp
) == 1) {
727 VARIANT
*arg
= get_arg(dp
, 0);
729 if(V_VT(arg
) != VT_DISPATCH
) {
730 WARN("arg is not dispatch\n");
735 cmp_func
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
736 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
737 WARN("cmp_func is not a function\n");
739 jsdisp_release(cmp_func
);
746 jsdisp_release(cmp_func
);
748 jsdisp_addref(jsthis
);
749 var_set_jsdisp(retv
, jsthis
);
754 vtab
= heap_alloc_zero(length
* sizeof(VARIANT
));
756 for(i
=0; i
<length
; i
++) {
757 hres
= jsdisp_get_idx(jsthis
, i
, vtab
+i
, ei
, caller
);
758 if(hres
== DISP_E_UNKNOWNNAME
) {
759 V_VT(vtab
+i
) = VT_EMPTY
;
761 } else if(FAILED(hres
)) {
762 WARN("Could not get elem %d: %08x\n", i
, hres
);
767 hres
= E_OUTOFMEMORY
;
770 if(SUCCEEDED(hres
)) {
771 sorttab
= heap_alloc(length
*2*sizeof(VARIANT
*));
773 hres
= E_OUTOFMEMORY
;
777 if(SUCCEEDED(hres
)) {
778 VARIANT
*tmpv
, **tmpbuf
;
781 tmpbuf
= sorttab
+ length
;
782 for(i
=0; i
< length
; i
++)
785 for(i
=0; i
< length
/2; i
++) {
786 hres
= sort_cmp(ctx
, cmp_func
, sorttab
[2*i
+1], sorttab
[2*i
], ei
, caller
, &cmp
);
792 sorttab
[2*i
] = sorttab
[2*i
+1];
793 sorttab
[2*i
+1] = tmpv
;
797 if(SUCCEEDED(hres
)) {
800 for(k
=2; k
< length
; k
*= 2) {
801 for(i
=0; i
+k
< length
; i
+= 2*k
) {
806 bend
= length
- (i
+k
);
808 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(VARIANT
*));
810 while(a
< k
&& b
< bend
) {
811 hres
= sort_cmp(ctx
, cmp_func
, tmpbuf
[a
], sorttab
[i
+k
+b
], ei
, caller
, &cmp
);
816 sorttab
[i
+a
+b
] = tmpbuf
[a
];
819 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
828 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(VARIANT
*));
836 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
837 hres
= jsdisp_propput_idx(jsthis
, i
, sorttab
[i
], ei
, caller
);
841 for(i
=0; i
< length
; i
++)
842 VariantClear(vtab
+i
);
847 jsdisp_release(cmp_func
);
853 jsdisp_addref(jsthis
);
854 var_set_jsdisp(retv
, jsthis
);
860 /* ECMA-262 3rd Edition 15.4.4.12 */
861 static HRESULT
Array_splice(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
862 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
864 DWORD length
, start
=0, delete_cnt
=0, argc
, i
, add_args
= 0;
865 jsdisp_t
*ret_array
= NULL
, *jsthis
;
871 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
877 hres
= to_integer(ctx
, get_arg(dp
,0), ei
, &v
);
881 if(V_VT(&v
) == VT_I4
) {
883 start
= min(V_I4(&v
), length
);
885 start
= -V_I4(&v
) > length
? 0 : length
+ V_I4(&v
);
887 start
= V_R8(&v
) < 0.0 ? 0 : length
;
892 hres
= to_integer(ctx
, get_arg(dp
,1), ei
, &v
);
896 if(V_VT(&v
) == VT_I4
) {
898 delete_cnt
= min(V_I4(&v
), length
-start
);
899 }else if(V_R8(&v
) > 0.0) {
900 delete_cnt
= length
-start
;
907 hres
= create_array(ctx
, 0, &ret_array
);
911 for(i
=0; SUCCEEDED(hres
) && i
< delete_cnt
; i
++) {
912 hres
= jsdisp_get_idx(jsthis
, start
+i
, &v
, ei
, caller
);
913 if(hres
== DISP_E_UNKNOWNNAME
)
915 else if(SUCCEEDED(hres
))
916 hres
= jsdisp_propput_idx(ret_array
, i
, &v
, ei
, caller
);
919 if(SUCCEEDED(hres
)) {
921 V_I4(&v
) = delete_cnt
;
923 hres
= jsdisp_propput_name(ret_array
, lengthW
, &v
, ei
, caller
);
927 if(add_args
< delete_cnt
) {
928 for(i
= start
; SUCCEEDED(hres
) && i
< length
-delete_cnt
; i
++) {
929 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
, &v
, ei
, caller
);
930 if(hres
== DISP_E_UNKNOWNNAME
)
931 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
);
932 else if(SUCCEEDED(hres
))
933 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
, &v
, ei
, caller
);
936 for(i
=length
; SUCCEEDED(hres
) && i
!= length
-delete_cnt
+add_args
; i
--)
937 hres
= jsdisp_delete_idx(jsthis
, i
-1);
938 }else if(add_args
> delete_cnt
) {
939 for(i
=length
-delete_cnt
; SUCCEEDED(hres
) && i
!= start
; i
--) {
940 hres
= jsdisp_get_idx(jsthis
, i
+delete_cnt
-1, &v
, ei
, caller
);
941 if(hres
== DISP_E_UNKNOWNNAME
)
942 hres
= jsdisp_delete_idx(jsthis
, i
+add_args
-1);
943 else if(SUCCEEDED(hres
))
944 hres
= jsdisp_propput_idx(jsthis
, i
+add_args
-1, &v
, ei
, caller
);
948 for(i
=0; SUCCEEDED(hres
) && i
< add_args
; i
++)
949 hres
= jsdisp_propput_idx(jsthis
, start
+i
, get_arg(dp
,i
+2), ei
, caller
);
951 if(SUCCEEDED(hres
)) {
953 V_I4(&v
) = length
-delete_cnt
+add_args
;
954 hres
= jsdisp_propput_name(jsthis
, lengthW
, &v
, ei
, caller
);
959 jsdisp_release(ret_array
);
964 var_set_jsdisp(retv
, ret_array
);
968 /* ECMA-262 3rd Edition 15.4.4.2 */
969 static HRESULT
Array_toString(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
970 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
972 ArrayInstance
*array
;
976 array
= array_this(jsthis
);
978 return throw_type_error(ctx
, ei
, JS_E_ARRAY_EXPECTED
, NULL
);
980 return array_join(ctx
, &array
->dispex
, array
->length
, default_separatorW
, retv
, ei
, sp
);
983 static HRESULT
Array_toLocaleString(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
984 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
990 /* ECMA-262 3rd Edition 15.4.4.13 */
991 static HRESULT
Array_unshift(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
992 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
995 WCHAR buf
[14], *buf_end
, *str
;
996 DWORD argc
, i
, length
;
1003 hres
= get_length(ctx
, vthis
, ei
, &jsthis
, &length
);
1009 buf_end
= buf
+ sizeof(buf
)/sizeof(WCHAR
)-1;
1014 str
= idx_to_str(i
, buf_end
);
1016 hres
= jsdisp_get_id(jsthis
, str
, 0, &id
);
1017 if(SUCCEEDED(hres
)) {
1018 hres
= jsdisp_propget(jsthis
, id
, &var
, ei
, caller
);
1022 hres
= jsdisp_propput_idx(jsthis
, i
+argc
, &var
, ei
, caller
);
1024 }else if(hres
== DISP_E_UNKNOWNNAME
) {
1025 hres
= IDispatchEx_DeleteMemberByDispID(vthis
->u
.dispex
, id
);
1033 for(i
=0; i
<argc
; i
++) {
1034 hres
= jsdisp_propput_idx(jsthis
, i
, get_arg(dp
,i
), ei
, caller
);
1041 hres
= set_length(jsthis
, ei
, length
);
1047 if(ctx
->version
< 2) {
1048 V_VT(retv
) = VT_EMPTY
;
1051 V_I4(retv
) = length
;
1057 static HRESULT
Array_value(script_ctx_t
*ctx
, vdisp_t
*jsthis
, WORD flags
, DISPPARAMS
*dp
,
1058 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
1064 return throw_type_error(ctx
, ei
, JS_E_FUNCTION_EXPECTED
, NULL
);
1065 case INVOKE_PROPERTYGET
:
1066 return array_join(ctx
, jsthis
->u
.jsdisp
, array_from_vdisp(jsthis
)->length
, default_separatorW
, retv
, ei
, sp
);
1068 FIXME("unimplemented flags %x\n", flags
);
1075 static void Array_destructor(jsdisp_t
*dispex
)
1080 static void Array_on_put(jsdisp_t
*dispex
, const WCHAR
*name
)
1082 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
1083 const WCHAR
*ptr
= name
;
1089 while(*ptr
&& isdigitW(*ptr
)) {
1090 id
= id
*10 + (*ptr
-'0');
1097 if(id
>= array
->length
)
1098 array
->length
= id
+1;
1101 static const builtin_prop_t Array_props
[] = {
1102 {concatW
, Array_concat
, PROPF_METHOD
|1},
1103 {joinW
, Array_join
, PROPF_METHOD
|1},
1104 {lengthW
, Array_length
, 0},
1105 {popW
, Array_pop
, PROPF_METHOD
},
1106 {pushW
, Array_push
, PROPF_METHOD
|1},
1107 {reverseW
, Array_reverse
, PROPF_METHOD
},
1108 {shiftW
, Array_shift
, PROPF_METHOD
},
1109 {sliceW
, Array_slice
, PROPF_METHOD
|2},
1110 {sortW
, Array_sort
, PROPF_METHOD
|1},
1111 {spliceW
, Array_splice
, PROPF_METHOD
|2},
1112 {toLocaleStringW
, Array_toLocaleString
, PROPF_METHOD
},
1113 {toStringW
, Array_toString
, PROPF_METHOD
},
1114 {unshiftW
, Array_unshift
, PROPF_METHOD
|1},
1117 static const builtin_info_t Array_info
= {
1119 {NULL
, Array_value
, 0},
1120 sizeof(Array_props
)/sizeof(*Array_props
),
1126 static HRESULT
ArrayConstr_value(script_ctx_t
*ctx
, vdisp_t
*vthis
, WORD flags
, DISPPARAMS
*dp
,
1127 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
1137 case DISPATCH_METHOD
:
1138 case DISPATCH_CONSTRUCT
: {
1139 if(arg_cnt(dp
) == 1 && V_VT((arg_var
= get_arg(dp
, 0))) == VT_I4
) {
1140 if(V_I4(arg_var
) < 0)
1141 return throw_range_error(ctx
, ei
, JS_E_INVALID_LENGTH
, NULL
);
1143 hres
= create_array(ctx
, V_I4(arg_var
), &obj
);
1147 var_set_jsdisp(retv
, obj
);
1151 hres
= create_array(ctx
, arg_cnt(dp
), &obj
);
1155 for(i
=0; i
< arg_cnt(dp
); i
++) {
1156 hres
= jsdisp_propput_idx(obj
, i
, get_arg(dp
, i
), ei
, caller
);
1161 jsdisp_release(obj
);
1165 var_set_jsdisp(retv
, obj
);
1169 FIXME("unimplemented flags: %x\n", flags
);
1176 static HRESULT
alloc_array(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, ArrayInstance
**ret
)
1178 ArrayInstance
*array
;
1181 array
= heap_alloc_zero(sizeof(ArrayInstance
));
1183 return E_OUTOFMEMORY
;
1185 if(object_prototype
)
1186 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, object_prototype
);
1188 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &Array_info
, ctx
->array_constr
);
1199 HRESULT
create_array_constr(script_ctx_t
*ctx
, jsdisp_t
*object_prototype
, jsdisp_t
**ret
)
1201 ArrayInstance
*array
;
1204 static const WCHAR ArrayW
[] = {'A','r','r','a','y',0};
1206 hres
= alloc_array(ctx
, object_prototype
, &array
);
1210 hres
= create_builtin_function(ctx
, ArrayConstr_value
, ArrayW
, NULL
, PROPF_CONSTR
|1, &array
->dispex
, ret
);
1212 jsdisp_release(&array
->dispex
);
1216 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, jsdisp_t
**ret
)
1218 ArrayInstance
*array
;
1221 hres
= alloc_array(ctx
, NULL
, &array
);
1225 array
->length
= length
;
1227 *ret
= &array
->dispex
;