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
20 #include "wine/port.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
36 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
37 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
38 static const WCHAR joinW[] = {'j','o','i','n',0};
39 static const WCHAR popW[] = {'p','o','p',0};
40 static const WCHAR pushW[] = {'p','u','s','h',0};
41 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
42 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
43 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
44 static const WCHAR sortW[] = {'s','o','r','t',0};
45 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
46 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
47 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
48 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
50 static const WCHAR default_separatorW[] = {',',0};
52 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
54 return (ArrayInstance*)vdisp->u.jsdisp;
57 static inline ArrayInstance *array_this(vdisp_t *jsthis)
59 return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
62 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsdisp_t **jsthis, DWORD *ret)
68 array = array_this(vdisp);
70 *jsthis = &array->dispex;
76 return throw_type_error(ctx, JS_E_JSCRIPT_EXPECTED, NULL);
78 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &val);
82 hres = to_uint32(ctx, val, ret);
87 *jsthis = vdisp->u.jsdisp;
91 static HRESULT set_length(jsdisp_t *obj, DWORD length)
93 if(is_class(obj, JSCLASS_ARRAY)) {
94 ((ArrayInstance*)obj)->length = length;
98 return jsdisp_propput_name(obj, lengthW, jsval_number(length));
101 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
109 *ptr-- = '0' + (idx%10);
116 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
119 ArrayInstance *This = array_from_vdisp(jsthis);
121 TRACE("%p %d\n", This, This->length);
124 case DISPATCH_PROPERTYGET:
125 *r = jsval_number(This->length);
127 case DISPATCH_PROPERTYPUT: {
132 hres = to_number(ctx, argv[0], &len);
138 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
140 for(i=len; i<This->length; i++) {
141 hres = jsdisp_delete_idx(&This->dispex, i);
150 FIXME("unimplemented flags %x\n", flags);
157 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len)
163 for(i=0; i < obj->length; i++) {
164 hres = jsdisp_get_idx(&obj->dispex, i, &val);
165 if(hres == DISP_E_UNKNOWNNAME)
170 hres = jsdisp_propput_idx(array, *len+i, val);
180 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len)
185 jsobj = iface_to_jsdisp((IUnknown*)obj);
187 if(is_class(jsobj, JSCLASS_ARRAY)) {
188 hres = concat_array(array, (ArrayInstance*)jsobj, len);
189 jsdisp_release(jsobj);
192 jsdisp_release(jsobj);
195 return jsdisp_propput_idx(array, (*len)++, jsval_disp(obj));
198 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
207 hres = create_array(ctx, 0, &ret);
211 hres = concat_obj(ret, jsthis->u.disp, &len);
212 if(SUCCEEDED(hres)) {
215 for(i=0; i < argc; i++) {
216 if(is_object_instance(argv[i]))
217 hres = concat_obj(ret, get_object(argv[i]), &len);
219 hres = jsdisp_propput_idx(ret, len++, argv[i]);
235 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, jsval_t *r)
237 jsstr_t **str_tab, *ret = NULL;
240 HRESULT hres = E_FAIL;
244 *r = jsval_string(jsstr_empty());
248 str_tab = heap_alloc_zero(length * sizeof(*str_tab));
250 return E_OUTOFMEMORY;
252 for(i=0; i < length; i++) {
253 hres = jsdisp_get_idx(array, i, &val);
254 if(hres == DISP_E_UNKNOWNNAME) {
257 } else if(FAILED(hres))
260 if(!is_undefined(val) && !is_null(val)) {
261 hres = to_string(ctx, val, str_tab+i);
268 if(SUCCEEDED(hres)) {
269 DWORD seplen = 0, len = 0;
272 seplen = strlenW(sep);
275 len = jsstr_length(str_tab[0]);
276 for(i=1; i < length; i++) {
279 len += jsstr_length(str_tab[i]);
280 if(len > JSSTR_MAX_LENGTH) {
281 hres = E_OUTOFMEMORY;
287 ret = jsstr_alloc_buf(len);
294 tmplen = jsstr_length(str_tab[0]);
295 memcpy(ptr, str_tab[0]->str, tmplen*sizeof(WCHAR));
299 for(i=1; i < length; i++) {
301 memcpy(ptr, sep, seplen*sizeof(WCHAR));
306 tmplen = jsstr_length(str_tab[i]);
307 memcpy(ptr, str_tab[i]->str, tmplen*sizeof(WCHAR));
313 hres = E_OUTOFMEMORY;
317 for(i=0; i < length; i++) {
319 jsstr_release(str_tab[i]);
325 TRACE("= %s\n", debugstr_jsstr(ret));
328 *r = jsval_string(ret);
334 /* ECMA-262 3rd Edition 15.4.4.5 */
335 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
344 hres = get_length(ctx, vthis, &jsthis, &length);
351 hres = to_string(ctx, argv[0], &sep);
355 hres = array_join(ctx, jsthis, length, sep->str, r);
359 hres = array_join(ctx, jsthis, length, default_separatorW, r);
365 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
375 hres = get_length(ctx, vthis, &jsthis, &length);
380 hres = set_length(jsthis, 0);
385 *r = jsval_undefined();
390 hres = jsdisp_get_idx(jsthis, length, &val);
392 hres = jsdisp_delete_idx(jsthis, length);
393 else if(hres == DISP_E_UNKNOWNNAME)
394 val = jsval_undefined();
399 hres = set_length(jsthis, length);
413 /* ECMA-262 3rd Edition 15.4.4.7 */
414 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
424 hres = get_length(ctx, vthis, &jsthis, &length);
428 for(i=0; i < argc; i++) {
429 hres = jsdisp_propput_idx(jsthis, length+i, argv[i]);
434 hres = set_length(jsthis, length+argc);
439 *r = jsval_number(length+argc);
443 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
449 HRESULT hres1, hres2;
453 hres1 = get_length(ctx, vthis, &jsthis, &length);
457 for(k=0; k<length/2; k++) {
460 hres1 = jsdisp_get_idx(jsthis, k, &v1);
461 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
464 hres2 = jsdisp_get_idx(jsthis, l, &v2);
465 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
470 if(hres1 == DISP_E_UNKNOWNNAME)
471 hres1 = jsdisp_delete_idx(jsthis, l);
473 hres1 = jsdisp_propput_idx(jsthis, l, v1);
481 if(hres2 == DISP_E_UNKNOWNNAME)
482 hres2 = jsdisp_delete_idx(jsthis, k);
484 hres2 = jsdisp_propput_idx(jsthis, k, v2);
493 *r = jsval_obj(jsdisp_addref(jsthis));
497 /* ECMA-262 3rd Edition 15.4.4.9 */
498 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
508 hres = get_length(ctx, vthis, &jsthis, &length);
513 hres = set_length(jsthis, 0);
520 *r = jsval_undefined();
524 hres = jsdisp_get_idx(jsthis, 0, &ret);
525 if(hres == DISP_E_UNKNOWNNAME) {
526 ret = jsval_undefined();
530 for(i=1; SUCCEEDED(hres) && i<length; i++) {
531 hres = jsdisp_get_idx(jsthis, i, &v);
532 if(hres == DISP_E_UNKNOWNNAME)
533 hres = jsdisp_delete_idx(jsthis, i-1);
534 else if(SUCCEEDED(hres))
535 hres = jsdisp_propput_idx(jsthis, i-1, v);
538 if(SUCCEEDED(hres)) {
539 hres = jsdisp_delete_idx(jsthis, length-1);
541 hres = set_length(jsthis, length-1);
554 /* ECMA-262 3rd Edition 15.4.4.10 */
555 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
557 jsdisp_t *arr, *jsthis;
559 DWORD length, start, end, idx;
564 hres = get_length(ctx, vthis, &jsthis, &length);
569 hres = to_number(ctx, argv[0], &range);
573 range = floor(range);
574 if(-range>length || isnan(range)) start = 0;
575 else if(range < 0) start = range+length;
576 else if(range <= length) start = range;
582 hres = to_number(ctx, argv[1], &range);
586 range = floor(range);
587 if(-range>length) end = 0;
588 else if(range < 0) end = range+length;
589 else if(range <= length) end = range;
594 hres = create_array(ctx, (end>start)?end-start:0, &arr);
598 for(idx=start; idx<end; idx++) {
601 hres = jsdisp_get_idx(jsthis, idx, &v);
602 if(hres == DISP_E_UNKNOWNNAME)
605 if(SUCCEEDED(hres)) {
606 hres = jsdisp_propput_idx(arr, idx-start, v);
624 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, INT *cmp)
629 jsval_t args[2] = {v1, v2};
633 hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res);
637 hres = to_number(ctx, res, &n);
644 *cmp = n > 0.0 ? 1 : -1;
645 }else if(is_undefined(v1)) {
646 *cmp = is_undefined(v2) ? 0 : 1;
647 }else if(is_undefined(v2)) {
649 }else if(is_number(v1) && is_number(v2)) {
650 double d = get_number(v1)-get_number(v2);
654 *cmp = d < -0.0 ? -1 : 0;
658 hres = to_string(ctx, v1, &x);
662 hres = to_string(ctx, v2, &y);
663 if(SUCCEEDED(hres)) {
664 *cmp = jsstr_cmp(x, y);
675 /* ECMA-262 3rd Edition 15.4.4.11 */
676 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
679 jsdisp_t *jsthis, *cmp_func = NULL;
680 jsval_t *vtab, **sorttab = NULL;
687 hres = get_length(ctx, vthis, &jsthis, &length);
692 WARN("invalid arg_cnt %d\n", argc);
697 if(!is_object_instance(argv[0])) {
698 WARN("arg is not dispatch\n");
702 cmp_func = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
703 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
704 WARN("cmp_func is not a function\n");
706 jsdisp_release(cmp_func);
713 jsdisp_release(cmp_func);
715 *r = jsval_obj(jsdisp_addref(jsthis));
719 vtab = heap_alloc_zero(length * sizeof(*vtab));
721 for(i=0; i<length; i++) {
722 hres = jsdisp_get_idx(jsthis, i, vtab+i);
723 if(hres == DISP_E_UNKNOWNNAME) {
724 vtab[i] = jsval_undefined();
726 } else if(FAILED(hres)) {
727 WARN("Could not get elem %d: %08x\n", i, hres);
732 hres = E_OUTOFMEMORY;
735 if(SUCCEEDED(hres)) {
736 sorttab = heap_alloc(length*2*sizeof(*sorttab));
738 hres = E_OUTOFMEMORY;
742 if(SUCCEEDED(hres)) {
743 jsval_t *tmpv, **tmpbuf;
746 tmpbuf = sorttab + length;
747 for(i=0; i < length; i++)
750 for(i=0; i < length/2; i++) {
751 hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], &cmp);
757 sorttab[2*i] = sorttab[2*i+1];
758 sorttab[2*i+1] = tmpv;
762 if(SUCCEEDED(hres)) {
765 for(k=2; k < length; k *= 2) {
766 for(i=0; i+k < length; i += 2*k) {
771 bend = length - (i+k);
773 memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
775 while(a < k && b < bend) {
776 hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], &cmp);
781 sorttab[i+a+b] = tmpbuf[a];
784 sorttab[i+a+b] = sorttab[i+k+b];
793 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
801 for(i=0; SUCCEEDED(hres) && i < length; i++)
802 hres = jsdisp_propput_idx(jsthis, i, *sorttab[i]);
806 for(i=0; i < length; i++)
807 jsval_release(vtab[i]);
812 jsdisp_release(cmp_func);
818 *r = jsval_obj(jsdisp_addref(jsthis));
822 /* ECMA-262 3rd Edition 15.4.4.12 */
823 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
826 DWORD length, start=0, delete_cnt=0, i, add_args = 0;
827 jsdisp_t *ret_array = NULL, *jsthis;
835 hres = get_length(ctx, vthis, &jsthis, &length);
840 hres = to_integer(ctx, argv[0], &d);
846 start = min(n, length);
848 start = -n > length ? 0 : length + n;
850 start = d < 0.0 ? 0 : length;
855 hres = to_integer(ctx, argv[1], &d);
861 delete_cnt = min(n, length-start);
863 delete_cnt = length-start;
870 hres = create_array(ctx, 0, &ret_array);
874 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
875 hres = jsdisp_get_idx(jsthis, start+i, &val);
876 if(hres == DISP_E_UNKNOWNNAME) {
878 }else if(SUCCEEDED(hres)) {
879 hres = jsdisp_propput_idx(ret_array, i, val);
885 hres = jsdisp_propput_name(ret_array, lengthW, jsval_number(delete_cnt));
888 if(add_args < delete_cnt) {
889 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
890 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val);
891 if(hres == DISP_E_UNKNOWNNAME) {
892 hres = jsdisp_delete_idx(jsthis, i+add_args);
893 }else if(SUCCEEDED(hres)) {
894 hres = jsdisp_propput_idx(jsthis, i+add_args, val);
899 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
900 hres = jsdisp_delete_idx(jsthis, i-1);
901 }else if(add_args > delete_cnt) {
902 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
903 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val);
904 if(hres == DISP_E_UNKNOWNNAME) {
905 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
906 }else if(SUCCEEDED(hres)) {
907 hres = jsdisp_propput_idx(jsthis, i+add_args-1, val);
913 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
914 hres = jsdisp_propput_idx(jsthis, start+i, argv[i+2]);
917 hres = jsdisp_propput_name(jsthis, lengthW, jsval_number(length-delete_cnt+add_args));
921 jsdisp_release(ret_array);
926 *r = jsval_obj(ret_array);
930 /* ECMA-262 3rd Edition 15.4.4.2 */
931 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
934 ArrayInstance *array;
938 array = array_this(jsthis);
940 return throw_type_error(ctx, JS_E_ARRAY_EXPECTED, NULL);
942 return array_join(ctx, &array->dispex, array->length, default_separatorW, r);
945 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
952 /* ECMA-262 3rd Edition 15.4.4.13 */
953 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
957 WCHAR buf[14], *buf_end, *str;
965 hres = get_length(ctx, vthis, &jsthis, &length);
970 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
975 str = idx_to_str(i, buf_end);
977 hres = jsdisp_get_id(jsthis, str, 0, &id);
978 if(SUCCEEDED(hres)) {
979 hres = jsdisp_propget(jsthis, id, &val);
983 hres = jsdisp_propput_idx(jsthis, i+argc, val);
985 }else if(hres == DISP_E_UNKNOWNNAME) {
986 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
994 for(i=0; i<argc; i++) {
995 hres = jsdisp_propput_idx(jsthis, i, argv[i]);
1002 hres = set_length(jsthis, length);
1008 *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
1012 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1019 return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1020 case INVOKE_PROPERTYGET:
1021 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, r);
1023 FIXME("unimplemented flags %x\n", flags);
1030 static void Array_destructor(jsdisp_t *dispex)
1035 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1037 ArrayInstance *array = (ArrayInstance*)dispex;
1038 const WCHAR *ptr = name;
1044 while(*ptr && isdigitW(*ptr)) {
1045 id = id*10 + (*ptr-'0');
1052 if(id >= array->length)
1053 array->length = id+1;
1056 static const builtin_prop_t Array_props[] = {
1057 {concatW, Array_concat, PROPF_METHOD|1},
1058 {joinW, Array_join, PROPF_METHOD|1},
1059 {lengthW, Array_length, 0},
1060 {popW, Array_pop, PROPF_METHOD},
1061 {pushW, Array_push, PROPF_METHOD|1},
1062 {reverseW, Array_reverse, PROPF_METHOD},
1063 {shiftW, Array_shift, PROPF_METHOD},
1064 {sliceW, Array_slice, PROPF_METHOD|2},
1065 {sortW, Array_sort, PROPF_METHOD|1},
1066 {spliceW, Array_splice, PROPF_METHOD|2},
1067 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1068 {toStringW, Array_toString, PROPF_METHOD},
1069 {unshiftW, Array_unshift, PROPF_METHOD|1},
1072 static const builtin_info_t Array_info = {
1074 {NULL, Array_value, 0},
1075 sizeof(Array_props)/sizeof(*Array_props),
1081 static const builtin_prop_t ArrayInst_props[] = {
1082 {lengthW, Array_length, 0}
1085 static const builtin_info_t ArrayInst_info = {
1087 {NULL, Array_value, 0},
1088 sizeof(ArrayInst_props)/sizeof(*ArrayInst_props),
1094 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1104 case DISPATCH_METHOD:
1105 case DISPATCH_CONSTRUCT: {
1106 if(argc == 1 && is_number(argv[0])) {
1107 double n = get_number(argv[0]);
1109 if(n < 0 || !is_int32(n))
1110 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
1112 hres = create_array(ctx, n, &obj);
1116 *r = jsval_obj(obj);
1120 hres = create_array(ctx, argc, &obj);
1124 for(i=0; i < argc; i++) {
1125 hres = jsdisp_propput_idx(obj, i, argv[i]);
1130 jsdisp_release(obj);
1134 *r = jsval_obj(obj);
1138 FIXME("unimplemented flags: %x\n", flags);
1145 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1147 ArrayInstance *array;
1150 array = heap_alloc_zero(sizeof(ArrayInstance));
1152 return E_OUTOFMEMORY;
1154 if(object_prototype)
1155 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1157 hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1168 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1170 ArrayInstance *array;
1173 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1175 hres = alloc_array(ctx, object_prototype, &array);
1179 hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1181 jsdisp_release(&array->dispex);
1185 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1187 ArrayInstance *array;
1190 hres = alloc_array(ctx, NULL, &array);
1194 array->length = length;
1196 *ret = &array->dispex;