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
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript
);
31 static const WCHAR lengthW
[] = {'l','e','n','g','t','h',0};
32 static const WCHAR concatW
[] = {'c','o','n','c','a','t',0};
33 static const WCHAR joinW
[] = {'j','o','i','n',0};
34 static const WCHAR popW
[] = {'p','o','p',0};
35 static const WCHAR pushW
[] = {'p','u','s','h',0};
36 static const WCHAR reverseW
[] = {'r','e','v','e','r','s','e',0};
37 static const WCHAR shiftW
[] = {'s','h','i','f','t',0};
38 static const WCHAR sliceW
[] = {'s','l','i','c','e',0};
39 static const WCHAR sortW
[] = {'s','o','r','t',0};
40 static const WCHAR spliceW
[] = {'s','p','l','i','c','e',0};
41 static const WCHAR toStringW
[] = {'t','o','S','t','r','i','n','g',0};
42 static const WCHAR toLocaleStringW
[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
43 static const WCHAR unshiftW
[] = {'u','n','s','h','i','f','t',0};
44 static const WCHAR hasOwnPropertyW
[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
45 static const WCHAR propertyIsEnumerableW
[] =
46 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
47 static const WCHAR isPrototypeOfW
[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
49 static const WCHAR default_separatorW
[] = {',',0};
51 static HRESULT
Array_length(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
52 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
54 ArrayInstance
*This
= (ArrayInstance
*)dispex
;
56 TRACE("%p %d\n", This
, This
->length
);
59 case DISPATCH_PROPERTYGET
:
61 V_I4(retv
) = This
->length
;
64 FIXME("unimplemented flags %x\n", flags
);
71 static HRESULT
concat_array(DispatchEx
*array
, ArrayInstance
*obj
, DWORD
*len
, LCID lcid
,
72 jsexcept_t
*ei
, IServiceProvider
*caller
)
78 for(i
=0; i
< obj
->length
; i
++) {
79 hres
= jsdisp_propget_idx(&obj
->dispex
, i
, lcid
, &var
, ei
, caller
);
80 if(hres
== DISP_E_UNKNOWNNAME
)
85 hres
= jsdisp_propput_idx(array
, *len
+i
, lcid
, &var
, ei
, caller
);
95 static HRESULT
concat_obj(DispatchEx
*array
, IDispatch
*obj
, DWORD
*len
, LCID lcid
, jsexcept_t
*ei
, IServiceProvider
*caller
)
101 jsobj
= iface_to_jsdisp((IUnknown
*)obj
);
103 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
104 hres
= concat_array(array
, (ArrayInstance
*)jsobj
, len
, lcid
, ei
, caller
);
105 jsdisp_release(jsobj
);
108 jsdisp_release(jsobj
);
111 V_VT(&var
) = VT_DISPATCH
;
112 V_DISPATCH(&var
) = obj
;
113 return jsdisp_propput_idx(array
, (*len
)++, lcid
, &var
, ei
, caller
);
116 static HRESULT
Array_concat(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
117 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
125 hres
= create_array(dispex
->ctx
, 0, &ret
);
129 hres
= concat_obj(ret
, (IDispatch
*)_IDispatchEx_(dispex
), &len
, lcid
, ei
, caller
);
130 if(SUCCEEDED(hres
)) {
134 for(i
=0; i
< arg_cnt(dp
); i
++) {
135 arg
= get_arg(dp
, i
);
136 if(V_VT(arg
) == VT_DISPATCH
)
137 hres
= concat_obj(ret
, V_DISPATCH(arg
), &len
, lcid
, ei
, caller
);
139 hres
= jsdisp_propput_idx(ret
, len
++, lcid
, arg
, ei
, caller
);
149 V_VT(retv
) = VT_DISPATCH
;
150 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(ret
);
157 static HRESULT
array_join(DispatchEx
*array
, LCID lcid
, DWORD length
, const WCHAR
*sep
, VARIANT
*retv
,
158 jsexcept_t
*ei
, IServiceProvider
*caller
)
160 BSTR
*str_tab
, ret
= NULL
;
163 HRESULT hres
= E_FAIL
;
167 V_VT(retv
) = VT_BSTR
;
168 V_BSTR(retv
) = SysAllocStringLen(NULL
, 0);
170 return E_OUTOFMEMORY
;
175 str_tab
= heap_alloc_zero(length
* sizeof(BSTR
));
177 return E_OUTOFMEMORY
;
179 for(i
=0; i
< length
; i
++) {
180 hres
= jsdisp_propget_idx(array
, i
, lcid
, &var
, ei
, caller
);
184 if(V_VT(&var
) != VT_EMPTY
&& V_VT(&var
) != VT_NULL
)
185 hres
= to_string(array
->ctx
, &var
, ei
, str_tab
+i
);
191 if(SUCCEEDED(hres
)) {
192 DWORD seplen
= 0, len
= 0;
195 seplen
= strlenW(sep
);
198 len
= SysStringLen(str_tab
[0]);
199 for(i
=1; i
< length
; i
++)
200 len
+= seplen
+ SysStringLen(str_tab
[i
]);
202 ret
= SysAllocStringLen(NULL
, len
);
207 tmplen
= SysStringLen(str_tab
[0]);
208 memcpy(ret
, str_tab
[0], tmplen
*sizeof(WCHAR
));
212 for(i
=1; i
< length
; i
++) {
214 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
219 tmplen
= SysStringLen(str_tab
[i
]);
220 memcpy(ptr
, str_tab
[i
], tmplen
*sizeof(WCHAR
));
226 hres
= E_OUTOFMEMORY
;
230 for(i
=0; i
< length
; i
++)
231 SysFreeString(str_tab
[i
]);
236 TRACE("= %s\n", debugstr_w(ret
));
240 ret
= SysAllocStringLen(NULL
, 0);
242 return E_OUTOFMEMORY
;
245 V_VT(retv
) = VT_BSTR
;
254 /* ECMA-262 3rd Edition 15.4.4.5 */
255 static HRESULT
Array_join(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
256 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
263 if(is_class(dispex
, JSCLASS_ARRAY
)) {
264 length
= ((ArrayInstance
*)dispex
)->length
;
266 FIXME("dispid is not Array\n");
273 hres
= to_string(dispex
->ctx
, dp
->rgvarg
+ dp
->cArgs
-1, ei
, &sep
);
277 hres
= array_join(dispex
, lcid
, length
, sep
, retv
, ei
, caller
);
281 hres
= array_join(dispex
, lcid
, length
, default_separatorW
, retv
, ei
, caller
);
287 static HRESULT
Array_pop(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
288 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
296 static const WCHAR formatW
[] = {'%','d',0};
300 if(is_class(dispex
, JSCLASS_ARRAY
)) {
301 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
302 length
= array
->length
;
304 FIXME("not Array this\n");
310 V_VT(retv
) = VT_EMPTY
;
314 sprintfW(buf
, formatW
, --length
);
315 hres
= jsdisp_get_id(dispex
, buf
, 0, &id
);
316 if(SUCCEEDED(hres
)) {
317 hres
= jsdisp_propget(dispex
, id
, lcid
, &val
, ei
, caller
);
321 hres
= IDispatchEx_DeleteMemberByDispID(_IDispatchEx_(dispex
), id
);
322 }else if(hres
== DISP_E_UNKNOWNNAME
) {
323 V_VT(&val
) = VT_EMPTY
;
329 if(SUCCEEDED(hres
)) {
330 if(is_class(dispex
, JSCLASS_ARRAY
)) {
331 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
332 array
->length
= length
;
348 /* ECMA-262 3rd Edition 15.4.4.7 */
349 static HRESULT
Array_push(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
350 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
358 if(dispex
->builtin_info
->class == JSCLASS_ARRAY
) {
359 length
= ((ArrayInstance
*)dispex
)->length
;
361 FIXME("not Array this\n");
365 n
= dp
->cArgs
- dp
->cNamedArgs
;
366 for(i
=0; i
< n
; i
++) {
367 hres
= jsdisp_propput_idx(dispex
, length
+i
, lcid
, get_arg(dp
, i
), ei
, sp
);
374 V_I4(retv
) = length
+n
;
379 static HRESULT
Array_reverse(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
380 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
386 static HRESULT
Array_shift(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
387 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
393 static HRESULT
Array_slice(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
394 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
400 static HRESULT
sort_cmp(script_ctx_t
*ctx
, DispatchEx
*cmp_func
, VARIANT
*v1
, VARIANT
*v2
, jsexcept_t
*ei
,
401 IServiceProvider
*caller
, INT
*cmp
)
407 DISPPARAMS dp
= {args
, NULL
, 2, 0};
414 hres
= jsdisp_call_value(cmp_func
, ctx
->lcid
, DISPATCH_METHOD
, &dp
, &res
, ei
, caller
);
418 hres
= to_number(ctx
, &res
, ei
, &tmp
);
423 if(V_VT(&tmp
) == VT_I4
)
426 *cmp
= V_R8(&tmp
) > 0.0 ? 1 : -1;
427 }else if(is_num_vt(V_VT(v1
))) {
428 if(is_num_vt(V_VT(v2
))) {
429 DOUBLE d
= num_val(v1
)-num_val(v2
);
439 }else if(is_num_vt(V_VT(v2
))) {
441 }else if(V_VT(v1
) == VT_BSTR
) {
442 if(V_VT(v2
) == VT_BSTR
)
443 *cmp
= strcmpW(V_BSTR(v1
), V_BSTR(v2
));
446 }else if(V_VT(v2
) == VT_BSTR
) {
455 /* ECMA-262 3rd Edition 15.4.4.11 */
456 static HRESULT
Array_sort(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
457 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
459 DispatchEx
*cmp_func
= NULL
;
460 VARIANT
*vtab
, **sorttab
= NULL
;
467 if(is_class(dispex
, JSCLASS_ARRAY
)) {
468 length
= ((ArrayInstance
*)dispex
)->length
;
470 FIXME("unsupported this not array\n");
474 if(arg_cnt(dp
) > 1) {
475 WARN("invalid arg_cnt %d\n", arg_cnt(dp
));
479 if(arg_cnt(dp
) == 1) {
480 VARIANT
*arg
= get_arg(dp
, 0);
482 if(V_VT(arg
) != VT_DISPATCH
) {
483 WARN("arg is not dispatch\n");
488 cmp_func
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
489 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
490 WARN("cmp_func is not a function\n");
492 jsdisp_release(cmp_func
);
499 jsdisp_release(cmp_func
);
501 V_VT(retv
) = VT_DISPATCH
;
502 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(dispex
);
503 IDispatchEx_AddRef(_IDispatchEx_(dispex
));
508 vtab
= heap_alloc_zero(length
* sizeof(VARIANT
));
510 for(i
=0; i
<length
; i
++) {
511 hres
= jsdisp_propget_idx(dispex
, i
, lcid
, vtab
+i
, ei
, caller
);
512 if(FAILED(hres
) && hres
!= DISP_E_UNKNOWNNAME
) {
513 WARN("Could not get elem %d: %08x\n", i
, hres
);
518 hres
= E_OUTOFMEMORY
;
521 if(SUCCEEDED(hres
)) {
522 sorttab
= heap_alloc(length
*2*sizeof(VARIANT
*));
524 hres
= E_OUTOFMEMORY
;
528 if(SUCCEEDED(hres
)) {
529 VARIANT
*tmpv
, **tmpbuf
;
532 tmpbuf
= sorttab
+ length
;
533 for(i
=0; i
< length
; i
++)
536 for(i
=0; i
< length
/2; i
++) {
537 hres
= sort_cmp(dispex
->ctx
, cmp_func
, sorttab
[2*i
+1], sorttab
[2*i
], ei
, caller
, &cmp
);
543 sorttab
[2*i
] = sorttab
[2*i
+1];
544 sorttab
[2*i
+1] = tmpv
;
548 if(SUCCEEDED(hres
)) {
551 for(k
=2; k
< length
; k
*= 2) {
552 for(i
=0; i
+k
< length
; i
+= 2*k
) {
557 bend
= length
- (i
+k
);
559 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(VARIANT
*));
561 while(a
< k
&& b
< bend
) {
562 hres
= sort_cmp(dispex
->ctx
, cmp_func
, tmpbuf
[a
], sorttab
[i
+k
+b
], ei
, caller
, &cmp
);
567 sorttab
[i
+a
+b
] = tmpbuf
[a
];
570 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
579 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(VARIANT
*));
587 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
588 hres
= jsdisp_propput_idx(dispex
, i
, lcid
, sorttab
[i
], ei
, caller
);
592 for(i
=0; i
< length
; i
++)
593 VariantClear(vtab
+i
);
598 jsdisp_release(cmp_func
);
604 V_VT(retv
) = VT_DISPATCH
;
605 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(dispex
);
606 IDispatch_AddRef(_IDispatchEx_(dispex
));
612 static HRESULT
Array_splice(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
613 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
619 /* ECMA-262 3rd Edition 15.4.4.2 */
620 static HRESULT
Array_toString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
621 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
625 if(!is_class(dispex
, JSCLASS_ARRAY
)) {
626 WARN("not Array object\n");
630 return array_join(dispex
, lcid
, ((ArrayInstance
*)dispex
)->length
, default_separatorW
, retv
, ei
, sp
);
633 static HRESULT
Array_toLocaleString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
634 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
640 static HRESULT
Array_unshift(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
641 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
647 static HRESULT
Array_hasOwnProperty(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
648 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
654 static HRESULT
Array_propertyIsEnumerable(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
655 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
661 static HRESULT
Array_isPrototypeOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
662 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
668 static HRESULT
Array_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
669 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
674 case INVOKE_PROPERTYGET
:
675 return array_join(dispex
, lcid
, ((ArrayInstance
*)dispex
)->length
, default_separatorW
, retv
, ei
, sp
);
677 FIXME("unimplemented flags %x\n", flags
);
684 static void Array_destructor(DispatchEx
*dispex
)
689 static void Array_on_put(DispatchEx
*dispex
, const WCHAR
*name
)
691 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
692 const WCHAR
*ptr
= name
;
698 while(*ptr
&& isdigitW(*ptr
)) {
699 id
= id
*10 + (*ptr
-'0');
706 if(id
>= array
->length
)
707 array
->length
= id
+1;
710 static const builtin_prop_t Array_props
[] = {
711 {concatW
, Array_concat
, PROPF_METHOD
},
712 {hasOwnPropertyW
, Array_hasOwnProperty
, PROPF_METHOD
},
713 {isPrototypeOfW
, Array_isPrototypeOf
, PROPF_METHOD
},
714 {joinW
, Array_join
, PROPF_METHOD
},
715 {lengthW
, Array_length
, 0},
716 {popW
, Array_pop
, PROPF_METHOD
},
717 {propertyIsEnumerableW
, Array_propertyIsEnumerable
, PROPF_METHOD
},
718 {pushW
, Array_push
, PROPF_METHOD
},
719 {reverseW
, Array_reverse
, PROPF_METHOD
},
720 {shiftW
, Array_shift
, PROPF_METHOD
},
721 {sliceW
, Array_slice
, PROPF_METHOD
},
722 {sortW
, Array_sort
, PROPF_METHOD
},
723 {spliceW
, Array_splice
, PROPF_METHOD
},
724 {toLocaleStringW
, Array_toLocaleString
, PROPF_METHOD
},
725 {toStringW
, Array_toString
, PROPF_METHOD
},
726 {unshiftW
, Array_unshift
, PROPF_METHOD
},
729 static const builtin_info_t Array_info
= {
731 {NULL
, Array_value
, 0},
732 sizeof(Array_props
)/sizeof(*Array_props
),
738 static HRESULT
ArrayConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
739 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
749 case DISPATCH_CONSTRUCT
: {
750 if(arg_cnt(dp
) == 1 && V_VT((arg_var
= get_arg(dp
, 0))) == VT_I4
) {
751 if(V_I4(arg_var
) < 0) {
752 FIXME("throw RangeError\n");
756 hres
= create_array(dispex
->ctx
, V_I4(arg_var
), &obj
);
760 V_VT(retv
) = VT_DISPATCH
;
761 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(obj
);
765 hres
= create_array(dispex
->ctx
, arg_cnt(dp
), &obj
);
769 for(i
=0; i
< arg_cnt(dp
); i
++) {
770 hres
= jsdisp_propput_idx(obj
, i
, lcid
, get_arg(dp
, i
), ei
, caller
);
779 V_VT(retv
) = VT_DISPATCH
;
780 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(obj
);
784 FIXME("unimplemented flags: %x\n", flags
);
791 static HRESULT
alloc_array(script_ctx_t
*ctx
, BOOL use_constr
, ArrayInstance
**ret
)
793 ArrayInstance
*array
;
796 array
= heap_alloc_zero(sizeof(ArrayInstance
));
798 return E_OUTOFMEMORY
;
801 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &Array_info
, ctx
->array_constr
);
803 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &Array_info
, ctx
->object_constr
);
814 HRESULT
create_array_constr(script_ctx_t
*ctx
, DispatchEx
**ret
)
816 ArrayInstance
*array
;
819 hres
= alloc_array(ctx
, FALSE
, &array
);
823 hres
= create_builtin_function(ctx
, ArrayConstr_value
, NULL
, PROPF_CONSTR
, &array
->dispex
, ret
);
825 IDispatchEx_Release(_IDispatchEx_(&array
->dispex
));
829 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, DispatchEx
**ret
)
831 ArrayInstance
*array
;
834 hres
= alloc_array(ctx
, TRUE
, &array
);
838 array
->length
= length
;
840 *ret
= &array
->dispex
;