shell32: Implement GetSelectedCount() using IFolderView2::GetSelection().
[wine.git] / dlls / jscript / array.c
bloba005309022151999c69579f2bbf7999a954e5b93
1 /*
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <math.h>
24 #include "jscript.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 typedef struct {
31 jsdisp_t dispex;
33 DWORD length;
34 } ArrayInstance;
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_jsdisp(jsdisp_t *jsdisp)
54 return CONTAINING_RECORD(jsdisp, ArrayInstance, dispex);
57 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
59 return array_from_jsdisp(vdisp->u.jsdisp);
62 static inline ArrayInstance *array_this(vdisp_t *jsthis)
64 return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
67 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsdisp_t **jsthis, DWORD *ret)
69 ArrayInstance *array;
70 jsval_t val;
71 HRESULT hres;
73 array = array_this(vdisp);
74 if(array) {
75 *jsthis = &array->dispex;
76 *ret = array->length;
77 return S_OK;
80 if(!is_jsdisp(vdisp))
81 return throw_type_error(ctx, JS_E_JSCRIPT_EXPECTED, NULL);
83 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &val);
84 if(FAILED(hres))
85 return hres;
87 hres = to_uint32(ctx, val, ret);
88 jsval_release(val);
89 if(FAILED(hres))
90 return hres;
92 *jsthis = vdisp->u.jsdisp;
93 return S_OK;
96 static HRESULT set_length(jsdisp_t *obj, DWORD length)
98 if(is_class(obj, JSCLASS_ARRAY)) {
99 ((ArrayInstance*)obj)->length = length;
100 return S_OK;
103 return jsdisp_propput_name(obj, lengthW, jsval_number(length));
106 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
108 if(!idx) {
109 *ptr = '0';
110 return ptr;
113 while(idx) {
114 *ptr-- = '0' + (idx%10);
115 idx /= 10;
118 return ptr+1;
121 static HRESULT Array_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
123 TRACE("%p\n", jsthis);
125 *r = jsval_number(array_from_jsdisp(jsthis)->length);
126 return S_OK;
129 static HRESULT Array_set_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
131 ArrayInstance *This = array_from_jsdisp(jsthis);
132 DOUBLE len = -1;
133 DWORD i;
134 HRESULT hres;
136 TRACE("%p %d\n", This, This->length);
138 hres = to_number(ctx, value, &len);
139 if(FAILED(hres))
140 return hres;
142 len = floor(len);
143 if(len!=(DWORD)len)
144 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
146 for(i=len; i < This->length; i++) {
147 hres = jsdisp_delete_idx(&This->dispex, i);
148 if(FAILED(hres))
149 return hres;
152 This->length = len;
153 return S_OK;
156 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len)
158 jsval_t val;
159 DWORD i;
160 HRESULT hres;
162 for(i=0; i < obj->length; i++) {
163 hres = jsdisp_get_idx(&obj->dispex, i, &val);
164 if(hres == DISP_E_UNKNOWNNAME)
165 continue;
166 if(FAILED(hres))
167 return hres;
169 hres = jsdisp_propput_idx(array, *len+i, val);
170 jsval_release(val);
171 if(FAILED(hres))
172 return hres;
175 *len += obj->length;
176 return S_OK;
179 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len)
181 jsdisp_t *jsobj;
182 HRESULT hres;
184 jsobj = iface_to_jsdisp((IUnknown*)obj);
185 if(jsobj) {
186 if(is_class(jsobj, JSCLASS_ARRAY)) {
187 hres = concat_array(array, (ArrayInstance*)jsobj, len);
188 jsdisp_release(jsobj);
189 return hres;
191 jsdisp_release(jsobj);
194 return jsdisp_propput_idx(array, (*len)++, jsval_disp(obj));
197 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
198 jsval_t *r)
200 jsdisp_t *ret;
201 DWORD len = 0;
202 HRESULT hres;
204 TRACE("\n");
206 hres = create_array(ctx, 0, &ret);
207 if(FAILED(hres))
208 return hres;
210 hres = concat_obj(ret, jsthis->u.disp, &len);
211 if(SUCCEEDED(hres)) {
212 DWORD i;
214 for(i=0; i < argc; i++) {
215 if(is_object_instance(argv[i]))
216 hres = concat_obj(ret, get_object(argv[i]), &len);
217 else
218 hres = jsdisp_propput_idx(ret, len++, argv[i]);
219 if(FAILED(hres))
220 break;
224 if(FAILED(hres))
225 return hres;
227 if(r)
228 *r = jsval_obj(ret);
229 else
230 jsdisp_release(ret);
231 return S_OK;
234 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, jsval_t *r)
236 jsstr_t **str_tab, *ret;
237 jsval_t val;
238 DWORD i;
239 HRESULT hres = E_FAIL;
241 if(!length) {
242 if(r)
243 *r = jsval_string(jsstr_empty());
244 return S_OK;
247 str_tab = heap_alloc_zero(length * sizeof(*str_tab));
248 if(!str_tab)
249 return E_OUTOFMEMORY;
251 for(i=0; i < length; i++) {
252 hres = jsdisp_get_idx(array, i, &val);
253 if(hres == DISP_E_UNKNOWNNAME) {
254 hres = S_OK;
255 continue;
256 } else if(FAILED(hres))
257 break;
259 if(!is_undefined(val) && !is_null(val)) {
260 hres = to_string(ctx, val, str_tab+i);
261 jsval_release(val);
262 if(FAILED(hres))
263 break;
267 if(SUCCEEDED(hres)) {
268 DWORD seplen = 0, len = 0;
270 seplen = strlenW(sep);
272 if(str_tab[0])
273 len = jsstr_length(str_tab[0]);
274 for(i=1; i < length; i++) {
275 len += seplen;
276 if(str_tab[i])
277 len += jsstr_length(str_tab[i]);
278 if(len > JSSTR_MAX_LENGTH) {
279 hres = E_OUTOFMEMORY;
280 break;
284 if(SUCCEEDED(hres)) {
285 WCHAR *ptr = NULL;
287 ptr = jsstr_alloc_buf(len, &ret);
288 if(ptr) {
289 if(str_tab[0])
290 ptr += jsstr_flush(str_tab[0], ptr);
292 for(i=1; i < length; i++) {
293 if(seplen) {
294 memcpy(ptr, sep, seplen*sizeof(WCHAR));
295 ptr += seplen;
298 if(str_tab[i])
299 ptr += jsstr_flush(str_tab[i], ptr);
301 }else {
302 hres = E_OUTOFMEMORY;
307 for(i=0; i < length; i++) {
308 if(str_tab[i])
309 jsstr_release(str_tab[i]);
311 heap_free(str_tab);
312 if(FAILED(hres))
313 return hres;
315 TRACE("= %s\n", debugstr_jsstr(ret));
317 if(r)
318 *r = jsval_string(ret);
319 else
320 jsstr_release(ret);
321 return S_OK;
324 /* ECMA-262 3rd Edition 15.4.4.5 */
325 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
326 jsval_t *r)
328 jsdisp_t *jsthis;
329 DWORD length;
330 HRESULT hres;
332 TRACE("\n");
334 hres = get_length(ctx, vthis, &jsthis, &length);
335 if(FAILED(hres))
336 return hres;
338 if(argc) {
339 const WCHAR *sep;
340 jsstr_t *sep_str;
342 hres = to_flat_string(ctx, argv[0], &sep_str, &sep);
343 if(FAILED(hres))
344 return hres;
346 hres = array_join(ctx, jsthis, length, sep, r);
348 jsstr_release(sep_str);
349 }else {
350 hres = array_join(ctx, jsthis, length, default_separatorW, r);
353 return hres;
356 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
357 jsval_t *r)
359 jsdisp_t *jsthis;
360 jsval_t val;
361 DWORD length;
362 HRESULT hres;
364 TRACE("\n");
366 hres = get_length(ctx, vthis, &jsthis, &length);
367 if(FAILED(hres))
368 return hres;
370 if(!length) {
371 hres = set_length(jsthis, 0);
372 if(FAILED(hres))
373 return hres;
375 if(r)
376 *r = jsval_undefined();
377 return S_OK;
380 length--;
381 hres = jsdisp_get_idx(jsthis, length, &val);
382 if(SUCCEEDED(hres))
383 hres = jsdisp_delete_idx(jsthis, length);
384 else if(hres == DISP_E_UNKNOWNNAME)
385 val = jsval_undefined();
386 else
387 return hres;
389 if(SUCCEEDED(hres))
390 hres = set_length(jsthis, length);
392 if(FAILED(hres)) {
393 jsval_release(val);
394 return hres;
397 if(r)
398 *r = val;
399 else
400 jsval_release(val);
401 return hres;
404 /* ECMA-262 3rd Edition 15.4.4.7 */
405 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
406 jsval_t *r)
408 jsdisp_t *jsthis;
409 DWORD length = 0;
410 unsigned i;
411 HRESULT hres;
413 TRACE("\n");
415 hres = get_length(ctx, vthis, &jsthis, &length);
416 if(FAILED(hres))
417 return hres;
419 for(i=0; i < argc; i++) {
420 hres = jsdisp_propput_idx(jsthis, length+i, argv[i]);
421 if(FAILED(hres))
422 return hres;
425 hres = set_length(jsthis, length+argc);
426 if(FAILED(hres))
427 return hres;
429 if(r)
430 *r = jsval_number(length+argc);
431 return S_OK;
434 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
435 jsval_t *r)
437 jsdisp_t *jsthis;
438 DWORD length, k, l;
439 jsval_t v1, v2;
440 HRESULT hres1, hres2;
442 TRACE("\n");
444 hres1 = get_length(ctx, vthis, &jsthis, &length);
445 if(FAILED(hres1))
446 return hres1;
448 for(k=0; k<length/2; k++) {
449 l = length-k-1;
451 hres1 = jsdisp_get_idx(jsthis, k, &v1);
452 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
453 return hres1;
455 hres2 = jsdisp_get_idx(jsthis, l, &v2);
456 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
457 jsval_release(v1);
458 return hres2;
461 if(hres1 == DISP_E_UNKNOWNNAME)
462 hres1 = jsdisp_delete_idx(jsthis, l);
463 else
464 hres1 = jsdisp_propput_idx(jsthis, l, v1);
466 if(FAILED(hres1)) {
467 jsval_release(v1);
468 jsval_release(v2);
469 return hres1;
472 if(hres2 == DISP_E_UNKNOWNNAME)
473 hres2 = jsdisp_delete_idx(jsthis, k);
474 else
475 hres2 = jsdisp_propput_idx(jsthis, k, v2);
477 if(FAILED(hres2)) {
478 jsval_release(v2);
479 return hres2;
483 if(r)
484 *r = jsval_obj(jsdisp_addref(jsthis));
485 return S_OK;
488 /* ECMA-262 3rd Edition 15.4.4.9 */
489 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
490 jsval_t *r)
492 jsdisp_t *jsthis;
493 DWORD length = 0, i;
494 jsval_t v, ret;
495 HRESULT hres;
497 TRACE("\n");
499 hres = get_length(ctx, vthis, &jsthis, &length);
500 if(FAILED(hres))
501 return hres;
503 if(!length) {
504 hres = set_length(jsthis, 0);
505 if(FAILED(hres))
506 return hres;
508 if(r)
509 *r = jsval_undefined();
510 return S_OK;
513 hres = jsdisp_get_idx(jsthis, 0, &ret);
514 if(hres == DISP_E_UNKNOWNNAME) {
515 ret = jsval_undefined();
516 hres = S_OK;
519 for(i=1; SUCCEEDED(hres) && i<length; i++) {
520 hres = jsdisp_get_idx(jsthis, i, &v);
521 if(hres == DISP_E_UNKNOWNNAME)
522 hres = jsdisp_delete_idx(jsthis, i-1);
523 else if(SUCCEEDED(hres))
524 hres = jsdisp_propput_idx(jsthis, i-1, v);
527 if(SUCCEEDED(hres)) {
528 hres = jsdisp_delete_idx(jsthis, length-1);
529 if(SUCCEEDED(hres))
530 hres = set_length(jsthis, length-1);
533 if(FAILED(hres))
534 return hres;
536 if(r)
537 *r = ret;
538 else
539 jsval_release(ret);
540 return hres;
543 /* ECMA-262 3rd Edition 15.4.4.10 */
544 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
546 jsdisp_t *arr, *jsthis;
547 DOUBLE range;
548 DWORD length, start, end, idx;
549 HRESULT hres;
551 TRACE("\n");
553 hres = get_length(ctx, vthis, &jsthis, &length);
554 if(FAILED(hres))
555 return hres;
557 if(argc) {
558 hres = to_number(ctx, argv[0], &range);
559 if(FAILED(hres))
560 return hres;
562 range = floor(range);
563 if(-range>length || isnan(range)) start = 0;
564 else if(range < 0) start = range+length;
565 else if(range <= length) start = range;
566 else start = length;
568 else start = 0;
570 if(argc > 1) {
571 hres = to_number(ctx, argv[1], &range);
572 if(FAILED(hres))
573 return hres;
575 range = floor(range);
576 if(-range>length) end = 0;
577 else if(range < 0) end = range+length;
578 else if(range <= length) end = range;
579 else end = length;
581 else end = length;
583 hres = create_array(ctx, (end>start)?end-start:0, &arr);
584 if(FAILED(hres))
585 return hres;
587 for(idx=start; idx<end; idx++) {
588 jsval_t v;
590 hres = jsdisp_get_idx(jsthis, idx, &v);
591 if(hres == DISP_E_UNKNOWNNAME)
592 continue;
594 if(SUCCEEDED(hres)) {
595 hres = jsdisp_propput_idx(arr, idx-start, v);
596 jsval_release(v);
599 if(FAILED(hres)) {
600 jsdisp_release(arr);
601 return hres;
605 if(r)
606 *r = jsval_obj(arr);
607 else
608 jsdisp_release(arr);
610 return S_OK;
613 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, INT *cmp)
615 HRESULT hres;
617 if(cmp_func) {
618 jsval_t args[2] = {v1, v2};
619 jsval_t res;
620 double n;
622 hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res);
623 if(FAILED(hres))
624 return hres;
626 hres = to_number(ctx, res, &n);
627 jsval_release(res);
628 if(FAILED(hres))
629 return hres;
631 if(n == 0)
632 *cmp = 0;
633 *cmp = n > 0.0 ? 1 : -1;
634 }else if(is_undefined(v1)) {
635 *cmp = is_undefined(v2) ? 0 : 1;
636 }else if(is_undefined(v2)) {
637 *cmp = -1;
638 }else if(is_number(v1) && is_number(v2)) {
639 double d = get_number(v1)-get_number(v2);
640 if(d > 0.0)
641 *cmp = 1;
642 else
643 *cmp = d < -0.0 ? -1 : 0;
644 }else {
645 jsstr_t *x, *y;
647 hres = to_string(ctx, v1, &x);
648 if(FAILED(hres))
649 return hres;
651 hres = to_string(ctx, v2, &y);
652 if(SUCCEEDED(hres)) {
653 *cmp = jsstr_cmp(x, y);
654 jsstr_release(y);
656 jsstr_release(x);
657 if(FAILED(hres))
658 return hres;
661 return S_OK;
664 /* ECMA-262 3rd Edition 15.4.4.11 */
665 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
666 jsval_t *r)
668 jsdisp_t *jsthis, *cmp_func = NULL;
669 jsval_t *vtab, **sorttab = NULL;
670 DWORD length;
671 DWORD i;
672 HRESULT hres = S_OK;
674 TRACE("\n");
676 hres = get_length(ctx, vthis, &jsthis, &length);
677 if(FAILED(hres))
678 return hres;
680 if(argc > 1) {
681 WARN("invalid arg_cnt %d\n", argc);
682 return E_FAIL;
685 if(argc == 1) {
686 if(!is_object_instance(argv[0])) {
687 WARN("arg is not dispatch\n");
688 return E_FAIL;
691 cmp_func = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
692 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
693 WARN("cmp_func is not a function\n");
694 if(cmp_func)
695 jsdisp_release(cmp_func);
696 return E_FAIL;
700 if(!length) {
701 if(cmp_func)
702 jsdisp_release(cmp_func);
703 if(r)
704 *r = jsval_obj(jsdisp_addref(jsthis));
705 return S_OK;
708 vtab = heap_alloc_zero(length * sizeof(*vtab));
709 if(vtab) {
710 for(i=0; i<length; i++) {
711 hres = jsdisp_get_idx(jsthis, i, vtab+i);
712 if(hres == DISP_E_UNKNOWNNAME) {
713 vtab[i] = jsval_undefined();
714 hres = S_OK;
715 } else if(FAILED(hres)) {
716 WARN("Could not get elem %d: %08x\n", i, hres);
717 break;
720 }else {
721 hres = E_OUTOFMEMORY;
724 if(SUCCEEDED(hres)) {
725 sorttab = heap_alloc(length*2*sizeof(*sorttab));
726 if(!sorttab)
727 hres = E_OUTOFMEMORY;
730 /* merge-sort */
731 if(SUCCEEDED(hres)) {
732 jsval_t *tmpv, **tmpbuf;
733 INT cmp;
735 tmpbuf = sorttab + length;
736 for(i=0; i < length; i++)
737 sorttab[i] = vtab+i;
739 for(i=0; i < length/2; i++) {
740 hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], &cmp);
741 if(FAILED(hres))
742 break;
744 if(cmp < 0) {
745 tmpv = sorttab[2*i];
746 sorttab[2*i] = sorttab[2*i+1];
747 sorttab[2*i+1] = tmpv;
751 if(SUCCEEDED(hres)) {
752 DWORD k, a, b, bend;
754 for(k=2; k < length; k *= 2) {
755 for(i=0; i+k < length; i += 2*k) {
756 a = b = 0;
757 if(i+2*k <= length)
758 bend = k;
759 else
760 bend = length - (i+k);
762 memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
764 while(a < k && b < bend) {
765 hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], &cmp);
766 if(FAILED(hres))
767 break;
769 if(cmp < 0) {
770 sorttab[i+a+b] = tmpbuf[a];
771 a++;
772 }else {
773 sorttab[i+a+b] = sorttab[i+k+b];
774 b++;
778 if(FAILED(hres))
779 break;
781 if(a < k)
782 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
785 if(FAILED(hres))
786 break;
790 for(i=0; SUCCEEDED(hres) && i < length; i++)
791 hres = jsdisp_propput_idx(jsthis, i, *sorttab[i]);
794 if(vtab) {
795 for(i=0; i < length; i++)
796 jsval_release(vtab[i]);
797 heap_free(vtab);
799 heap_free(sorttab);
800 if(cmp_func)
801 jsdisp_release(cmp_func);
803 if(FAILED(hres))
804 return hres;
806 if(r)
807 *r = jsval_obj(jsdisp_addref(jsthis));
808 return S_OK;
811 /* ECMA-262 3rd Edition 15.4.4.12 */
812 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
813 jsval_t *r)
815 DWORD length, start=0, delete_cnt=0, i, add_args = 0;
816 jsdisp_t *ret_array = NULL, *jsthis;
817 jsval_t val;
818 double d;
819 int n;
820 HRESULT hres = S_OK;
822 TRACE("\n");
824 hres = get_length(ctx, vthis, &jsthis, &length);
825 if(FAILED(hres))
826 return hres;
828 if(argc) {
829 hres = to_integer(ctx, argv[0], &d);
830 if(FAILED(hres))
831 return hres;
833 if(is_int32(d)) {
834 if((n = d) >= 0)
835 start = min(n, length);
836 else
837 start = -n > length ? 0 : length + n;
838 }else {
839 start = d < 0.0 ? 0 : length;
843 if(argc >= 2) {
844 hres = to_integer(ctx, argv[1], &d);
845 if(FAILED(hres))
846 return hres;
848 if(is_int32(d)) {
849 if((n = d) > 0)
850 delete_cnt = min(n, length-start);
851 }else if(d > 0.0) {
852 delete_cnt = length-start;
855 add_args = argc-2;
858 if(r) {
859 hres = create_array(ctx, 0, &ret_array);
860 if(FAILED(hres))
861 return hres;
863 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
864 hres = jsdisp_get_idx(jsthis, start+i, &val);
865 if(hres == DISP_E_UNKNOWNNAME) {
866 hres = S_OK;
867 }else if(SUCCEEDED(hres)) {
868 hres = jsdisp_propput_idx(ret_array, i, val);
869 jsval_release(val);
873 if(SUCCEEDED(hres))
874 hres = jsdisp_propput_name(ret_array, lengthW, jsval_number(delete_cnt));
877 if(add_args < delete_cnt) {
878 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
879 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val);
880 if(hres == DISP_E_UNKNOWNNAME) {
881 hres = jsdisp_delete_idx(jsthis, i+add_args);
882 }else if(SUCCEEDED(hres)) {
883 hres = jsdisp_propput_idx(jsthis, i+add_args, val);
884 jsval_release(val);
888 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
889 hres = jsdisp_delete_idx(jsthis, i-1);
890 }else if(add_args > delete_cnt) {
891 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
892 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val);
893 if(hres == DISP_E_UNKNOWNNAME) {
894 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
895 }else if(SUCCEEDED(hres)) {
896 hres = jsdisp_propput_idx(jsthis, i+add_args-1, val);
897 jsval_release(val);
902 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
903 hres = jsdisp_propput_idx(jsthis, start+i, argv[i+2]);
905 if(SUCCEEDED(hres))
906 hres = jsdisp_propput_name(jsthis, lengthW, jsval_number(length-delete_cnt+add_args));
908 if(FAILED(hres)) {
909 if(ret_array)
910 jsdisp_release(ret_array);
911 return hres;
914 if(r)
915 *r = jsval_obj(ret_array);
916 return S_OK;
919 /* ECMA-262 3rd Edition 15.4.4.2 */
920 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
921 jsval_t *r)
923 ArrayInstance *array;
925 TRACE("\n");
927 array = array_this(jsthis);
928 if(!array)
929 return throw_type_error(ctx, JS_E_ARRAY_EXPECTED, NULL);
931 return array_join(ctx, &array->dispex, array->length, default_separatorW, r);
934 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
935 jsval_t *r)
937 FIXME("\n");
938 return E_NOTIMPL;
941 /* ECMA-262 3rd Edition 15.4.4.13 */
942 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
943 jsval_t *r)
945 jsdisp_t *jsthis;
946 WCHAR buf[14], *buf_end, *str;
947 DWORD i, length;
948 jsval_t val;
949 DISPID id;
950 HRESULT hres;
952 TRACE("\n");
954 hres = get_length(ctx, vthis, &jsthis, &length);
955 if(FAILED(hres))
956 return hres;
958 if(argc) {
959 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
960 *buf_end-- = 0;
961 i = length;
963 while(i--) {
964 str = idx_to_str(i, buf_end);
966 hres = jsdisp_get_id(jsthis, str, 0, &id);
967 if(SUCCEEDED(hres)) {
968 hres = jsdisp_propget(jsthis, id, &val);
969 if(FAILED(hres))
970 return hres;
972 hres = jsdisp_propput_idx(jsthis, i+argc, val);
973 jsval_release(val);
974 }else if(hres == DISP_E_UNKNOWNNAME) {
975 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
979 if(FAILED(hres))
980 return hres;
983 for(i=0; i<argc; i++) {
984 hres = jsdisp_propput_idx(jsthis, i, argv[i]);
985 if(FAILED(hres))
986 return hres;
989 if(argc) {
990 length += argc;
991 hres = set_length(jsthis, length);
992 if(FAILED(hres))
993 return hres;
996 if(r)
997 *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
998 return S_OK;
1001 static HRESULT Array_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
1003 ArrayInstance *array = array_from_jsdisp(jsthis);
1005 TRACE("\n");
1007 return array_join(ctx, &array->dispex, array->length, default_separatorW, r);
1010 static void Array_destructor(jsdisp_t *dispex)
1012 heap_free(dispex);
1015 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1017 ArrayInstance *array = (ArrayInstance*)dispex;
1018 const WCHAR *ptr = name;
1019 DWORD id = 0;
1021 if(!isdigitW(*ptr))
1022 return;
1024 while(*ptr && isdigitW(*ptr)) {
1025 id = id*10 + (*ptr-'0');
1026 ptr++;
1029 if(*ptr)
1030 return;
1032 if(id >= array->length)
1033 array->length = id+1;
1036 static const builtin_prop_t Array_props[] = {
1037 {concatW, Array_concat, PROPF_METHOD|1},
1038 {joinW, Array_join, PROPF_METHOD|1},
1039 {lengthW, NULL,0, Array_get_length, Array_set_length},
1040 {popW, Array_pop, PROPF_METHOD},
1041 {pushW, Array_push, PROPF_METHOD|1},
1042 {reverseW, Array_reverse, PROPF_METHOD},
1043 {shiftW, Array_shift, PROPF_METHOD},
1044 {sliceW, Array_slice, PROPF_METHOD|2},
1045 {sortW, Array_sort, PROPF_METHOD|1},
1046 {spliceW, Array_splice, PROPF_METHOD|2},
1047 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1048 {toStringW, Array_toString, PROPF_METHOD},
1049 {unshiftW, Array_unshift, PROPF_METHOD|1},
1052 static const builtin_info_t Array_info = {
1053 JSCLASS_ARRAY,
1054 {NULL, NULL,0, Array_get_value},
1055 sizeof(Array_props)/sizeof(*Array_props),
1056 Array_props,
1057 Array_destructor,
1058 Array_on_put
1061 static const builtin_prop_t ArrayInst_props[] = {
1062 {lengthW, NULL,0, Array_get_length, Array_set_length}
1065 static const builtin_info_t ArrayInst_info = {
1066 JSCLASS_ARRAY,
1067 {NULL, NULL,0, Array_get_value},
1068 sizeof(ArrayInst_props)/sizeof(*ArrayInst_props),
1069 ArrayInst_props,
1070 Array_destructor,
1071 Array_on_put
1074 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1075 jsval_t *r)
1077 jsdisp_t *obj;
1078 DWORD i;
1079 HRESULT hres;
1081 TRACE("\n");
1083 switch(flags) {
1084 case DISPATCH_METHOD:
1085 case DISPATCH_CONSTRUCT: {
1086 if(argc == 1 && is_number(argv[0])) {
1087 double n = get_number(argv[0]);
1089 if(n < 0 || !is_int32(n))
1090 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
1092 hres = create_array(ctx, n, &obj);
1093 if(FAILED(hres))
1094 return hres;
1096 *r = jsval_obj(obj);
1097 return S_OK;
1100 hres = create_array(ctx, argc, &obj);
1101 if(FAILED(hres))
1102 return hres;
1104 for(i=0; i < argc; i++) {
1105 hres = jsdisp_propput_idx(obj, i, argv[i]);
1106 if(FAILED(hres))
1107 break;
1109 if(FAILED(hres)) {
1110 jsdisp_release(obj);
1111 return hres;
1114 *r = jsval_obj(obj);
1115 break;
1117 default:
1118 FIXME("unimplemented flags: %x\n", flags);
1119 return E_NOTIMPL;
1122 return S_OK;
1125 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1127 ArrayInstance *array;
1128 HRESULT hres;
1130 array = heap_alloc_zero(sizeof(ArrayInstance));
1131 if(!array)
1132 return E_OUTOFMEMORY;
1134 if(object_prototype)
1135 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1136 else
1137 hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1139 if(FAILED(hres)) {
1140 heap_free(array);
1141 return hres;
1144 *ret = array;
1145 return S_OK;
1148 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1150 ArrayInstance *array;
1151 HRESULT hres;
1153 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1155 hres = alloc_array(ctx, object_prototype, &array);
1156 if(FAILED(hres))
1157 return hres;
1159 hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1161 jsdisp_release(&array->dispex);
1162 return hres;
1165 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1167 ArrayInstance *array;
1168 HRESULT hres;
1170 hres = alloc_array(ctx, NULL, &array);
1171 if(FAILED(hres))
1172 return hres;
1174 array->length = length;
1176 *ret = &array->dispex;
1177 return S_OK;