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, DispatchEx **jsthis, DWORD *ret)
65 array = array_this(vdisp);
67 *jsthis = &array->dispex;
73 return throw_type_error(ctx, ei, IDS_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(DispatchEx *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, IDS_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(DispatchEx *array, ArrayInstance *obj, DWORD *len,
162 jsexcept_t *ei, IServiceProvider *caller)
168 for(i=0; i < obj->length; i++) {
169 hres = jsdisp_propget_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(DispatchEx *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 V_VT(retv) = VT_DISPATCH;
240 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
247 static HRESULT array_join(script_ctx_t *ctx, DispatchEx *array, DWORD length, const WCHAR *sep, VARIANT *retv,
248 jsexcept_t *ei, IServiceProvider *caller)
250 BSTR *str_tab, ret = NULL;
253 HRESULT hres = E_FAIL;
257 V_VT(retv) = VT_BSTR;
258 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
260 return E_OUTOFMEMORY;
265 str_tab = heap_alloc_zero(length * sizeof(BSTR));
267 return E_OUTOFMEMORY;
269 for(i=0; i < length; i++) {
270 hres = jsdisp_propget_idx(array, i, &var, ei, caller);
274 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
275 hres = to_string(ctx, &var, ei, str_tab+i);
281 if(SUCCEEDED(hres)) {
282 DWORD seplen = 0, len = 0;
285 seplen = strlenW(sep);
288 len = SysStringLen(str_tab[0]);
289 for(i=1; i < length; i++)
290 len += seplen + SysStringLen(str_tab[i]);
292 ret = SysAllocStringLen(NULL, len);
297 tmplen = SysStringLen(str_tab[0]);
298 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
302 for(i=1; i < length; i++) {
304 memcpy(ptr, sep, seplen*sizeof(WCHAR));
309 tmplen = SysStringLen(str_tab[i]);
310 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
316 hres = E_OUTOFMEMORY;
320 for(i=0; i < length; i++)
321 SysFreeString(str_tab[i]);
326 TRACE("= %s\n", debugstr_w(ret));
330 ret = SysAllocStringLen(NULL, 0);
332 return E_OUTOFMEMORY;
335 V_VT(retv) = VT_BSTR;
344 /* ECMA-262 3rd Edition 15.4.4.5 */
345 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
346 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
354 hres = get_length(ctx, vthis, ei, &jsthis, &length);
361 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
365 hres = array_join(ctx, jsthis, length, sep, retv, ei, caller);
369 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei, caller);
375 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
376 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
385 hres = get_length(ctx, vthis, ei, &jsthis, &length);
390 hres = set_length(jsthis, ei, 0);
395 V_VT(retv) = VT_EMPTY;
400 hres = jsdisp_propget_idx(jsthis, length, &val, ei, caller);
401 if(SUCCEEDED(hres)) {
402 hres = jsdisp_delete_idx(jsthis, length);
403 } else if(hres == DISP_E_UNKNOWNNAME) {
404 V_VT(&val) = VT_EMPTY;
410 hres = set_length(jsthis, ei, length);
425 /* ECMA-262 3rd Edition 15.4.4.7 */
426 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
427 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
436 hres = get_length(ctx, vthis, ei, &jsthis, &length);
441 for(i=0; i < n; i++) {
442 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei, sp);
447 hres = set_length(jsthis, ei, length+n);
453 V_I4(retv) = length+n;
458 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
459 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
464 HRESULT hres1, hres2;
468 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
472 for(k=0; k<length/2; k++) {
475 hres1 = jsdisp_propget_idx(jsthis, k, &v1, ei, sp);
476 hres2 = jsdisp_propget_idx(jsthis, l, &v2, ei, sp);
478 if(hres1 == DISP_E_UNKNOWNNAME)
479 jsdisp_delete_idx(jsthis, l);
481 jsdisp_propput_idx(jsthis, l, &v1, ei, sp);
483 if(hres2 == DISP_E_UNKNOWNNAME)
484 jsdisp_delete_idx(jsthis, k);
486 jsdisp_propput_idx(jsthis, k, &v2, ei, sp);
490 V_VT(retv) = VT_DISPATCH;
491 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
492 IDispatch_AddRef(V_DISPATCH(retv));
498 /* ECMA-262 3rd Edition 15.4.4.9 */
499 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
500 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
509 hres = get_length(ctx, vthis, ei, &jsthis, &length);
514 hres = set_length(jsthis, ei, 0);
521 V_VT(retv) = VT_EMPTY;
525 hres = jsdisp_propget_idx(jsthis, 0, &ret, ei, caller);
526 if(hres == DISP_E_UNKNOWNNAME) {
527 V_VT(&ret) = VT_EMPTY;
531 for(i=1; SUCCEEDED(hres) && i<length; i++) {
532 hres = jsdisp_propget_idx(jsthis, i, &v, ei, caller);
533 if(hres == DISP_E_UNKNOWNNAME)
534 hres = jsdisp_delete_idx(jsthis, i-1);
535 else if(SUCCEEDED(hres))
536 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei, caller);
539 if(SUCCEEDED(hres)) {
540 hres = jsdisp_delete_idx(jsthis, length-1);
542 hres = set_length(jsthis, ei, length-1);
545 if(SUCCEEDED(hres) && retv)
552 /* ECMA-262 3rd Edition 15.4.4.10 */
553 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
554 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
556 DispatchEx *arr, *jsthis;
559 DWORD length, start, end, idx;
564 hres = get_length(ctx, vthis, ei, &jsthis, &length);
569 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
573 if(V_VT(&v) == VT_I4)
576 range = floor(V_R8(&v));
578 if(-range>length || isnan(range)) start = 0;
579 else if(range < 0) start = range+length;
580 else if(range <= length) start = range;
586 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
590 if(V_VT(&v) == VT_I4)
593 range = floor(V_R8(&v));
595 if(-range>length) end = 0;
596 else if(range < 0) end = range+length;
597 else if(range <= length) end = range;
602 hres = create_array(ctx, (end>start)?end-start:0, &arr);
606 for(idx=start; idx<end; idx++) {
607 hres = jsdisp_propget_idx(jsthis, idx, &v, ei, sp);
608 if(hres == DISP_E_UNKNOWNNAME)
611 if(SUCCEEDED(hres)) {
612 hres = jsdisp_propput_idx(arr, idx-start, &v, ei, sp);
623 V_VT(retv) = VT_DISPATCH;
624 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
632 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
633 IServiceProvider *caller, INT *cmp)
639 DISPPARAMS dp = {args, NULL, 2, 0};
646 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
650 hres = to_number(ctx, &res, ei, &tmp);
655 if(V_VT(&tmp) == VT_I4)
658 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
659 }else if(is_num_vt(V_VT(v1))) {
660 if(is_num_vt(V_VT(v2))) {
661 DOUBLE d = num_val(v1)-num_val(v2);
671 }else if(is_num_vt(V_VT(v2))) {
673 }else if(V_VT(v1) == VT_BSTR) {
674 if(V_VT(v2) == VT_BSTR)
675 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
678 }else if(V_VT(v2) == VT_BSTR) {
687 /* ECMA-262 3rd Edition 15.4.4.11 */
688 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
689 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
691 DispatchEx *cmp_func = NULL;
692 VARIANT *vtab, **sorttab = NULL;
699 if(is_vclass(jsthis, JSCLASS_ARRAY)) {
700 length = array_from_vdisp(jsthis)->length;
702 FIXME("unsupported this not array\n");
706 if(arg_cnt(dp) > 1) {
707 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
711 if(arg_cnt(dp) == 1) {
712 VARIANT *arg = get_arg(dp, 0);
714 if(V_VT(arg) != VT_DISPATCH) {
715 WARN("arg is not dispatch\n");
720 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
721 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
722 WARN("cmp_func is not a function\n");
724 jsdisp_release(cmp_func);
731 jsdisp_release(cmp_func);
733 V_VT(retv) = VT_DISPATCH;
734 V_DISPATCH(retv) = jsthis->u.disp;
735 IDispatch_AddRef(jsthis->u.disp);
740 vtab = heap_alloc_zero(length * sizeof(VARIANT));
742 for(i=0; i<length; i++) {
743 hres = jsdisp_propget_idx(jsthis->u.jsdisp, i, vtab+i, ei, caller);
744 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
745 WARN("Could not get elem %d: %08x\n", i, hres);
750 hres = E_OUTOFMEMORY;
753 if(SUCCEEDED(hres)) {
754 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
756 hres = E_OUTOFMEMORY;
760 if(SUCCEEDED(hres)) {
761 VARIANT *tmpv, **tmpbuf;
764 tmpbuf = sorttab + length;
765 for(i=0; i < length; i++)
768 for(i=0; i < length/2; i++) {
769 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
775 sorttab[2*i] = sorttab[2*i+1];
776 sorttab[2*i+1] = tmpv;
780 if(SUCCEEDED(hres)) {
783 for(k=2; k < length; k *= 2) {
784 for(i=0; i+k < length; i += 2*k) {
789 bend = length - (i+k);
791 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
793 while(a < k && b < bend) {
794 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
799 sorttab[i+a+b] = tmpbuf[a];
802 sorttab[i+a+b] = sorttab[i+k+b];
811 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
819 for(i=0; SUCCEEDED(hres) && i < length; i++)
820 hres = jsdisp_propput_idx(jsthis->u.jsdisp, i, sorttab[i], ei, caller);
824 for(i=0; i < length; i++)
825 VariantClear(vtab+i);
830 jsdisp_release(cmp_func);
836 V_VT(retv) = VT_DISPATCH;
837 V_DISPATCH(retv) = jsthis->u.disp;
838 IDispatch_AddRef(jsthis->u.disp);
844 /* ECMA-262 3rd Edition 15.4.4.12 */
845 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
846 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
848 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
849 DispatchEx *ret_array = NULL, *jsthis;
855 hres = get_length(ctx, vthis, ei, &jsthis, &length);
861 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
865 if(V_VT(&v) == VT_I4) {
867 start = min(V_I4(&v), length);
869 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
871 start = V_R8(&v) < 0.0 ? 0 : length;
876 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
880 if(V_VT(&v) == VT_I4) {
882 delete_cnt = min(V_I4(&v), length-start);
883 }else if(V_R8(&v) > 0.0) {
884 delete_cnt = length-start;
891 hres = create_array(ctx, 0, &ret_array);
895 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
896 hres = jsdisp_propget_idx(jsthis, start+i, &v, ei, caller);
897 if(hres == DISP_E_UNKNOWNNAME)
899 else if(SUCCEEDED(hres))
900 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
903 if(SUCCEEDED(hres)) {
905 V_I4(&v) = delete_cnt;
907 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
911 if(add_args < delete_cnt) {
912 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
913 hres = jsdisp_propget_idx(jsthis, i+delete_cnt, &v, ei, caller);
914 if(hres == DISP_E_UNKNOWNNAME)
915 hres = jsdisp_delete_idx(jsthis, i+add_args);
916 else if(SUCCEEDED(hres))
917 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
920 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
921 hres = jsdisp_delete_idx(jsthis, i-1);
922 }else if(add_args > delete_cnt) {
923 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
924 hres = jsdisp_propget_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
925 if(hres == DISP_E_UNKNOWNNAME)
926 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
927 else if(SUCCEEDED(hres))
928 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
932 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
933 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
935 if(SUCCEEDED(hres)) {
937 V_I4(&v) = length-delete_cnt+add_args;
938 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
943 jsdisp_release(ret_array);
948 V_VT(retv) = VT_DISPATCH;
949 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret_array);
954 /* ECMA-262 3rd Edition 15.4.4.2 */
955 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
956 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
958 ArrayInstance *array;
962 array = array_this(jsthis);
964 return throw_type_error(ctx, ei, IDS_ARRAY_EXPECTED, NULL);
966 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
969 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
970 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
976 /* ECMA-262 3rd Edition 15.4.4.13 */
977 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
978 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
981 WCHAR buf[14], *buf_end, *str;
982 DWORD argc, i, length;
989 hres = get_length(ctx, vthis, ei, &jsthis, &length);
995 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1000 str = idx_to_str(i, buf_end);
1002 hres = jsdisp_get_id(jsthis, str, 0, &id);
1003 if(SUCCEEDED(hres)) {
1004 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
1008 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
1010 }else if(hres == DISP_E_UNKNOWNNAME) {
1011 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1019 for(i=0; i<argc; i++) {
1020 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
1027 hres = set_length(jsthis, ei, length);
1033 if(ctx->version < 2) {
1034 V_VT(retv) = VT_EMPTY;
1037 V_I4(retv) = length;
1043 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1044 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1050 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1051 case INVOKE_PROPERTYGET:
1052 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1054 FIXME("unimplemented flags %x\n", flags);
1061 static void Array_destructor(DispatchEx *dispex)
1066 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
1068 ArrayInstance *array = (ArrayInstance*)dispex;
1069 const WCHAR *ptr = name;
1075 while(*ptr && isdigitW(*ptr)) {
1076 id = id*10 + (*ptr-'0');
1083 if(id >= array->length)
1084 array->length = id+1;
1087 static const builtin_prop_t Array_props[] = {
1088 {concatW, Array_concat, PROPF_METHOD|1},
1089 {joinW, Array_join, PROPF_METHOD|1},
1090 {lengthW, Array_length, 0},
1091 {popW, Array_pop, PROPF_METHOD},
1092 {pushW, Array_push, PROPF_METHOD|1},
1093 {reverseW, Array_reverse, PROPF_METHOD},
1094 {shiftW, Array_shift, PROPF_METHOD},
1095 {sliceW, Array_slice, PROPF_METHOD|2},
1096 {sortW, Array_sort, PROPF_METHOD|1},
1097 {spliceW, Array_splice, PROPF_METHOD|2},
1098 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1099 {toStringW, Array_toString, PROPF_METHOD},
1100 {unshiftW, Array_unshift, PROPF_METHOD|1},
1103 static const builtin_info_t Array_info = {
1105 {NULL, Array_value, 0},
1106 sizeof(Array_props)/sizeof(*Array_props),
1112 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1113 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1123 case DISPATCH_METHOD:
1124 case DISPATCH_CONSTRUCT: {
1125 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1126 if(V_I4(arg_var) < 0)
1127 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
1129 hres = create_array(ctx, V_I4(arg_var), &obj);
1133 V_VT(retv) = VT_DISPATCH;
1134 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(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, caller);
1148 jsdisp_release(obj);
1152 V_VT(retv) = VT_DISPATCH;
1153 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1157 FIXME("unimplemented flags: %x\n", flags);
1164 static HRESULT alloc_array(script_ctx_t *ctx, DispatchEx *object_prototype, ArrayInstance **ret)
1166 ArrayInstance *array;
1169 array = heap_alloc_zero(sizeof(ArrayInstance));
1171 return E_OUTOFMEMORY;
1173 if(object_prototype)
1174 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1176 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1187 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1189 ArrayInstance *array;
1192 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1194 hres = alloc_array(ctx, object_prototype, &array);
1198 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1200 jsdisp_release(&array->dispex);
1204 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
1206 ArrayInstance *array;
1209 hres = alloc_array(ctx, NULL, &array);
1213 array->length = length;
1215 *ret = &array->dispex;