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 valueOfW
[] = {'v','a','l','u','e','O','f',0};
44 static const WCHAR unshiftW
[] = {'u','n','s','h','i','f','t',0};
45 static const WCHAR hasOwnPropertyW
[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
46 static const WCHAR propertyIsEnumerableW
[] =
47 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
48 static const WCHAR isPrototypeOfW
[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
50 const WCHAR default_separatorW
[] = {',',0};
52 static HRESULT
Array_length(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
53 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
55 ArrayInstance
*This
= (ArrayInstance
*)dispex
;
57 TRACE("%p %d\n", This
, This
->length
);
60 case DISPATCH_PROPERTYGET
:
62 V_I4(retv
) = This
->length
;
65 FIXME("unimplemented flags %x\n", flags
);
72 static HRESULT
concat_array(DispatchEx
*array
, ArrayInstance
*obj
, DWORD
*len
, LCID lcid
,
73 jsexcept_t
*ei
, IServiceProvider
*caller
)
79 for(i
=0; i
< obj
->length
; i
++) {
80 hres
= jsdisp_propget_idx(&obj
->dispex
, i
, lcid
, &var
, ei
, caller
);
81 if(hres
== DISP_E_UNKNOWNNAME
)
86 hres
= jsdisp_propput_idx(array
, *len
+i
, lcid
, &var
, ei
, caller
);
96 static HRESULT
concat_obj(DispatchEx
*array
, IDispatch
*obj
, DWORD
*len
, LCID lcid
, jsexcept_t
*ei
, IServiceProvider
*caller
)
102 jsobj
= iface_to_jsdisp((IUnknown
*)obj
);
104 if(is_class(jsobj
, JSCLASS_ARRAY
)) {
105 hres
= concat_array(array
, (ArrayInstance
*)jsobj
, len
, lcid
, ei
, caller
);
106 jsdisp_release(jsobj
);
109 jsdisp_release(jsobj
);
112 V_VT(&var
) = VT_DISPATCH
;
113 V_DISPATCH(&var
) = obj
;
114 return jsdisp_propput_idx(array
, (*len
)++, lcid
, &var
, ei
, caller
);
117 static HRESULT
Array_concat(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
118 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
126 hres
= create_array(dispex
->ctx
, 0, &ret
);
130 hres
= concat_obj(ret
, (IDispatch
*)_IDispatchEx_(dispex
), &len
, lcid
, ei
, caller
);
131 if(SUCCEEDED(hres
)) {
135 for(i
=0; i
< arg_cnt(dp
); i
++) {
136 arg
= get_arg(dp
, i
);
137 if(V_VT(arg
) == VT_DISPATCH
)
138 hres
= concat_obj(ret
, V_DISPATCH(arg
), &len
, lcid
, ei
, caller
);
140 hres
= jsdisp_propput_idx(ret
, len
++, lcid
, arg
, ei
, caller
);
150 V_VT(retv
) = VT_DISPATCH
;
151 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(ret
);
158 static HRESULT
array_join(DispatchEx
*array
, LCID lcid
, DWORD length
, const WCHAR
*sep
, VARIANT
*retv
,
159 jsexcept_t
*ei
, IServiceProvider
*caller
)
161 BSTR
*str_tab
, ret
= NULL
;
164 HRESULT hres
= E_FAIL
;
168 V_VT(retv
) = VT_BSTR
;
169 V_BSTR(retv
) = SysAllocStringLen(NULL
, 0);
171 return E_OUTOFMEMORY
;
176 str_tab
= heap_alloc_zero(length
* sizeof(BSTR
));
178 return E_OUTOFMEMORY
;
180 for(i
=0; i
< length
; i
++) {
181 hres
= jsdisp_propget_idx(array
, i
, lcid
, &var
, ei
, caller
);
185 if(V_VT(&var
) != VT_EMPTY
&& V_VT(&var
) != VT_NULL
)
186 hres
= to_string(array
->ctx
, &var
, ei
, str_tab
+i
);
192 if(SUCCEEDED(hres
)) {
193 DWORD seplen
= 0, len
= 0;
196 seplen
= strlenW(sep
);
199 len
= SysStringLen(str_tab
[0]);
200 for(i
=1; i
< length
; i
++)
201 len
+= seplen
+ SysStringLen(str_tab
[i
]);
203 ret
= SysAllocStringLen(NULL
, len
);
208 tmplen
= SysStringLen(str_tab
[0]);
209 memcpy(ret
, str_tab
[0], tmplen
*sizeof(WCHAR
));
213 for(i
=1; i
< length
; i
++) {
215 memcpy(ptr
, sep
, seplen
*sizeof(WCHAR
));
220 tmplen
= SysStringLen(str_tab
[i
]);
221 memcpy(ptr
, str_tab
[i
], tmplen
*sizeof(WCHAR
));
227 hres
= E_OUTOFMEMORY
;
231 for(i
=0; i
< length
; i
++)
232 SysFreeString(str_tab
[i
]);
237 TRACE("= %s\n", debugstr_w(ret
));
241 ret
= SysAllocStringLen(NULL
, 0);
243 return E_OUTOFMEMORY
;
246 V_VT(retv
) = VT_BSTR
;
255 /* ECMA-262 3rd Edition 15.4.4.5 */
256 static HRESULT
Array_join(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
257 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
264 if(is_class(dispex
, JSCLASS_ARRAY
)) {
265 length
= ((ArrayInstance
*)dispex
)->length
;
267 FIXME("dispid is not Array\n");
274 hres
= to_string(dispex
->ctx
, dp
->rgvarg
+ dp
->cArgs
-1, ei
, &sep
);
278 hres
= array_join(dispex
, lcid
, length
, sep
, retv
, ei
, caller
);
282 hres
= array_join(dispex
, lcid
, length
, default_separatorW
, retv
, ei
, caller
);
288 static HRESULT
Array_pop(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
289 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
297 static const WCHAR formatW
[] = {'%','d',0};
301 if(is_class(dispex
, JSCLASS_ARRAY
)) {
302 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
303 length
= array
->length
;
305 FIXME("not Array this\n");
311 V_VT(retv
) = VT_EMPTY
;
315 sprintfW(buf
, formatW
, --length
);
316 hres
= jsdisp_get_id(dispex
, buf
, 0, &id
);
317 if(SUCCEEDED(hres
)) {
318 hres
= jsdisp_propget(dispex
, id
, lcid
, &val
, ei
, caller
);
322 hres
= IDispatchEx_DeleteMemberByDispID(_IDispatchEx_(dispex
), id
);
323 }else if(hres
== DISP_E_UNKNOWNNAME
) {
324 V_VT(&val
) = VT_EMPTY
;
330 if(SUCCEEDED(hres
)) {
331 if(is_class(dispex
, JSCLASS_ARRAY
)) {
332 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
333 array
->length
= length
;
349 /* ECMA-262 3rd Edition 15.4.4.7 */
350 static HRESULT
Array_push(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
351 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
359 if(dispex
->builtin_info
->class == JSCLASS_ARRAY
) {
360 length
= ((ArrayInstance
*)dispex
)->length
;
362 FIXME("not Array this\n");
366 n
= dp
->cArgs
- dp
->cNamedArgs
;
367 for(i
=0; i
< n
; i
++) {
368 hres
= jsdisp_propput_idx(dispex
, length
+i
, lcid
, get_arg(dp
, i
), ei
, sp
);
375 V_I4(retv
) = length
+n
;
380 static HRESULT
Array_reverse(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
381 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
387 static HRESULT
Array_shift(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
388 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
394 static HRESULT
Array_slice(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
395 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
401 static HRESULT
sort_cmp(script_ctx_t
*ctx
, DispatchEx
*cmp_func
, VARIANT
*v1
, VARIANT
*v2
, jsexcept_t
*ei
,
402 IServiceProvider
*caller
, INT
*cmp
)
408 DISPPARAMS dp
= {args
, NULL
, 2, 0};
415 hres
= jsdisp_call_value(cmp_func
, ctx
->lcid
, DISPATCH_METHOD
, &dp
, &res
, ei
, caller
);
419 hres
= to_number(ctx
, &res
, ei
, &tmp
);
424 if(V_VT(&tmp
) == VT_I4
)
427 *cmp
= V_R8(&tmp
) > 0.0 ? 1 : -1;
428 }else if(is_num_vt(V_VT(v1
))) {
429 if(is_num_vt(V_VT(v2
))) {
430 DOUBLE d
= num_val(v1
)-num_val(v2
);
440 }else if(is_num_vt(V_VT(v2
))) {
442 }else if(V_VT(v1
) == VT_BSTR
) {
443 if(V_VT(v2
) == VT_BSTR
)
444 *cmp
= strcmpW(V_BSTR(v1
), V_BSTR(v2
));
447 }else if(V_VT(v2
) == VT_BSTR
) {
456 /* ECMA-262 3rd Edition 15.4.4.11 */
457 static HRESULT
Array_sort(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
458 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
460 DispatchEx
*cmp_func
= NULL
;
461 VARIANT
*vtab
, **sorttab
= NULL
;
468 if(is_class(dispex
, JSCLASS_ARRAY
)) {
469 length
= ((ArrayInstance
*)dispex
)->length
;
471 FIXME("unsupported this not array\n");
475 if(arg_cnt(dp
) > 1) {
476 WARN("invalid arg_cnt %d\n", arg_cnt(dp
));
480 if(arg_cnt(dp
) == 1) {
481 VARIANT
*arg
= get_arg(dp
, 0);
483 if(V_VT(arg
) != VT_DISPATCH
) {
484 WARN("arg is not dispatch\n");
489 cmp_func
= iface_to_jsdisp((IUnknown
*)V_DISPATCH(arg
));
490 if(!cmp_func
|| !is_class(cmp_func
, JSCLASS_FUNCTION
)) {
491 WARN("cmp_func is not a function\n");
493 jsdisp_release(cmp_func
);
500 jsdisp_release(cmp_func
);
502 V_VT(retv
) = VT_DISPATCH
;
503 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(dispex
);
504 IDispatchEx_AddRef(_IDispatchEx_(dispex
));
509 vtab
= heap_alloc_zero(length
* sizeof(VARIANT
));
511 for(i
=0; i
<length
; i
++) {
512 hres
= jsdisp_propget_idx(dispex
, i
, lcid
, vtab
+i
, ei
, caller
);
513 if(FAILED(hres
) && hres
!= DISP_E_UNKNOWNNAME
) {
514 WARN("Could not get elem %d: %08x\n", i
, hres
);
519 hres
= E_OUTOFMEMORY
;
522 if(SUCCEEDED(hres
)) {
523 sorttab
= heap_alloc(length
*2*sizeof(VARIANT
*));
525 hres
= E_OUTOFMEMORY
;
529 if(SUCCEEDED(hres
)) {
530 VARIANT
*tmpv
, **tmpbuf
;
533 tmpbuf
= sorttab
+ length
;
534 for(i
=0; i
< length
; i
++)
537 for(i
=0; i
< length
/2; i
++) {
538 hres
= sort_cmp(dispex
->ctx
, cmp_func
, sorttab
[2*i
+1], sorttab
[2*i
], ei
, caller
, &cmp
);
544 sorttab
[2*i
] = sorttab
[2*i
+1];
545 sorttab
[2*i
+1] = tmpv
;
549 if(SUCCEEDED(hres
)) {
552 for(k
=2; k
< length
; k
*= 2) {
553 for(i
=0; i
+k
< length
; i
+= 2*k
) {
558 bend
= length
- (i
+k
);
560 memcpy(tmpbuf
, sorttab
+i
, k
*sizeof(VARIANT
*));
562 while(a
< k
&& b
< bend
) {
563 hres
= sort_cmp(dispex
->ctx
, cmp_func
, tmpbuf
[a
], sorttab
[i
+k
+b
], ei
, caller
, &cmp
);
568 sorttab
[i
+a
+b
] = tmpbuf
[a
];
571 sorttab
[i
+a
+b
] = sorttab
[i
+k
+b
];
580 memcpy(sorttab
+i
+a
+b
, tmpbuf
+a
, (k
-a
)*sizeof(VARIANT
*));
588 for(i
=0; SUCCEEDED(hres
) && i
< length
; i
++)
589 hres
= jsdisp_propput_idx(dispex
, i
, lcid
, sorttab
[i
], ei
, caller
);
593 for(i
=0; i
< length
; i
++)
594 VariantClear(vtab
+i
);
599 jsdisp_release(cmp_func
);
605 V_VT(retv
) = VT_DISPATCH
;
606 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(dispex
);
607 IDispatch_AddRef(_IDispatchEx_(dispex
));
613 static HRESULT
Array_splice(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
614 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
620 /* ECMA-262 3rd Edition 15.4.4.2 */
621 static HRESULT
Array_toString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
622 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
626 if(!is_class(dispex
, JSCLASS_ARRAY
)) {
627 WARN("not Array object\n");
631 return array_join(dispex
, lcid
, ((ArrayInstance
*)dispex
)->length
, default_separatorW
, retv
, ei
, sp
);
634 static HRESULT
Array_toLocaleString(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
635 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
641 static HRESULT
Array_valueOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
642 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
648 static HRESULT
Array_unshift(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
649 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
655 static HRESULT
Array_hasOwnProperty(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
656 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
662 static HRESULT
Array_propertyIsEnumerable(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
663 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
669 static HRESULT
Array_isPrototypeOf(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
670 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
676 static HRESULT
Array_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
677 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*sp
)
682 case INVOKE_PROPERTYGET
:
683 return array_join(dispex
, lcid
, ((ArrayInstance
*)dispex
)->length
, default_separatorW
, retv
, ei
, sp
);
685 FIXME("unimplemented flags %x\n", flags
);
692 static void Array_destructor(DispatchEx
*dispex
)
697 static void Array_on_put(DispatchEx
*dispex
, const WCHAR
*name
)
699 ArrayInstance
*array
= (ArrayInstance
*)dispex
;
700 const WCHAR
*ptr
= name
;
706 while(*ptr
&& isdigitW(*ptr
)) {
707 id
= id
*10 + (*ptr
-'0');
714 if(id
>= array
->length
)
715 array
->length
= id
+1;
718 static const builtin_prop_t Array_props
[] = {
719 {concatW
, Array_concat
, PROPF_METHOD
},
720 {hasOwnPropertyW
, Array_hasOwnProperty
, PROPF_METHOD
},
721 {isPrototypeOfW
, Array_isPrototypeOf
, PROPF_METHOD
},
722 {joinW
, Array_join
, PROPF_METHOD
},
723 {lengthW
, Array_length
, 0},
724 {popW
, Array_pop
, PROPF_METHOD
},
725 {propertyIsEnumerableW
, Array_propertyIsEnumerable
, PROPF_METHOD
},
726 {pushW
, Array_push
, PROPF_METHOD
},
727 {reverseW
, Array_reverse
, PROPF_METHOD
},
728 {shiftW
, Array_shift
, PROPF_METHOD
},
729 {sliceW
, Array_slice
, PROPF_METHOD
},
730 {sortW
, Array_sort
, PROPF_METHOD
},
731 {spliceW
, Array_splice
, PROPF_METHOD
},
732 {toLocaleStringW
, Array_toLocaleString
, PROPF_METHOD
},
733 {toStringW
, Array_toString
, PROPF_METHOD
},
734 {unshiftW
, Array_unshift
, PROPF_METHOD
},
735 {valueOfW
, Array_valueOf
, PROPF_METHOD
}
738 static const builtin_info_t Array_info
= {
740 {NULL
, Array_value
, 0},
741 sizeof(Array_props
)/sizeof(*Array_props
),
747 static HRESULT
ArrayConstr_value(DispatchEx
*dispex
, LCID lcid
, WORD flags
, DISPPARAMS
*dp
,
748 VARIANT
*retv
, jsexcept_t
*ei
, IServiceProvider
*caller
)
758 case DISPATCH_CONSTRUCT
: {
759 if(arg_cnt(dp
) == 1 && V_VT((arg_var
= get_arg(dp
, 0))) == VT_I4
) {
760 if(V_I4(arg_var
) < 0) {
761 FIXME("throw RangeError\n");
765 hres
= create_array(dispex
->ctx
, V_I4(arg_var
), &obj
);
769 V_VT(retv
) = VT_DISPATCH
;
770 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(obj
);
774 hres
= create_array(dispex
->ctx
, arg_cnt(dp
), &obj
);
778 for(i
=0; i
< arg_cnt(dp
); i
++) {
779 hres
= jsdisp_propput_idx(obj
, i
, lcid
, get_arg(dp
, i
), ei
, caller
);
788 V_VT(retv
) = VT_DISPATCH
;
789 V_DISPATCH(retv
) = (IDispatch
*)_IDispatchEx_(obj
);
793 FIXME("unimplemented flags: %x\n", flags
);
800 static HRESULT
alloc_array(script_ctx_t
*ctx
, BOOL use_constr
, ArrayInstance
**ret
)
802 ArrayInstance
*array
;
805 array
= heap_alloc_zero(sizeof(ArrayInstance
));
807 return E_OUTOFMEMORY
;
810 hres
= init_dispex_from_constr(&array
->dispex
, ctx
, &Array_info
, ctx
->array_constr
);
812 hres
= init_dispex(&array
->dispex
, ctx
, &Array_info
, NULL
);
823 HRESULT
create_array_constr(script_ctx_t
*ctx
, DispatchEx
**ret
)
825 ArrayInstance
*array
;
828 hres
= alloc_array(ctx
, FALSE
, &array
);
832 hres
= create_builtin_function(ctx
, ArrayConstr_value
, PROPF_CONSTR
, &array
->dispex
, ret
);
834 IDispatchEx_Release(_IDispatchEx_(&array
->dispex
));
838 HRESULT
create_array(script_ctx_t
*ctx
, DWORD length
, DispatchEx
**ret
)
840 ArrayInstance
*array
;
843 hres
= alloc_array(ctx
, TRUE
, &array
);
847 array
->length
= length
;
849 *ret
= &array
->dispex
;