jscript: Use separated functions for implementing builtin call, get and set operation.
[wine.git] / dlls / jscript / array.c
blob03804bbdfca1eec651b3bccc8a00b98748a7b0d6
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_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)
64 ArrayInstance *array;
65 jsval_t val;
66 HRESULT hres;
68 array = array_this(vdisp);
69 if(array) {
70 *jsthis = &array->dispex;
71 *ret = array->length;
72 return S_OK;
75 if(!is_jsdisp(vdisp))
76 return throw_type_error(ctx, JS_E_JSCRIPT_EXPECTED, NULL);
78 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &val);
79 if(FAILED(hres))
80 return hres;
82 hres = to_uint32(ctx, val, ret);
83 jsval_release(val);
84 if(FAILED(hres))
85 return hres;
87 *jsthis = vdisp->u.jsdisp;
88 return S_OK;
91 static HRESULT set_length(jsdisp_t *obj, DWORD length)
93 if(is_class(obj, JSCLASS_ARRAY)) {
94 ((ArrayInstance*)obj)->length = length;
95 return S_OK;
98 return jsdisp_propput_name(obj, lengthW, jsval_number(length));
101 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
103 if(!idx) {
104 *ptr = '0';
105 return ptr;
108 while(idx) {
109 *ptr-- = '0' + (idx%10);
110 idx /= 10;
113 return ptr+1;
116 static HRESULT Array_get_length(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t *r)
118 ArrayInstance *This = array_from_vdisp(jsthis);
120 TRACE("%p %d\n", This, This->length);
122 *r = jsval_number(This->length);
123 return S_OK;
126 static HRESULT Array_set_length(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t value)
128 ArrayInstance *This = array_from_vdisp(jsthis);
129 DOUBLE len = -1;
130 DWORD i;
131 HRESULT hres;
133 TRACE("%p %d\n", This, This->length);
135 hres = to_number(ctx, value, &len);
136 if(FAILED(hres))
137 return hres;
139 len = floor(len);
140 if(len!=(DWORD)len)
141 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
143 for(i=len; i < This->length; i++) {
144 hres = jsdisp_delete_idx(&This->dispex, i);
145 if(FAILED(hres))
146 return hres;
149 This->length = len;
150 return S_OK;
153 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len)
155 jsval_t val;
156 DWORD i;
157 HRESULT hres;
159 for(i=0; i < obj->length; i++) {
160 hres = jsdisp_get_idx(&obj->dispex, i, &val);
161 if(hres == DISP_E_UNKNOWNNAME)
162 continue;
163 if(FAILED(hres))
164 return hres;
166 hres = jsdisp_propput_idx(array, *len+i, val);
167 jsval_release(val);
168 if(FAILED(hres))
169 return hres;
172 *len += obj->length;
173 return S_OK;
176 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len)
178 jsdisp_t *jsobj;
179 HRESULT hres;
181 jsobj = iface_to_jsdisp((IUnknown*)obj);
182 if(jsobj) {
183 if(is_class(jsobj, JSCLASS_ARRAY)) {
184 hres = concat_array(array, (ArrayInstance*)jsobj, len);
185 jsdisp_release(jsobj);
186 return hres;
188 jsdisp_release(jsobj);
191 return jsdisp_propput_idx(array, (*len)++, jsval_disp(obj));
194 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
195 jsval_t *r)
197 jsdisp_t *ret;
198 DWORD len = 0;
199 HRESULT hres;
201 TRACE("\n");
203 hres = create_array(ctx, 0, &ret);
204 if(FAILED(hres))
205 return hres;
207 hres = concat_obj(ret, jsthis->u.disp, &len);
208 if(SUCCEEDED(hres)) {
209 DWORD i;
211 for(i=0; i < argc; i++) {
212 if(is_object_instance(argv[i]))
213 hres = concat_obj(ret, get_object(argv[i]), &len);
214 else
215 hres = jsdisp_propput_idx(ret, len++, argv[i]);
216 if(FAILED(hres))
217 break;
221 if(FAILED(hres))
222 return hres;
224 if(r)
225 *r = jsval_obj(ret);
226 else
227 jsdisp_release(ret);
228 return S_OK;
231 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, jsval_t *r)
233 jsstr_t **str_tab, *ret;
234 jsval_t val;
235 DWORD i;
236 HRESULT hres = E_FAIL;
238 if(!length) {
239 if(r)
240 *r = jsval_string(jsstr_empty());
241 return S_OK;
244 str_tab = heap_alloc_zero(length * sizeof(*str_tab));
245 if(!str_tab)
246 return E_OUTOFMEMORY;
248 for(i=0; i < length; i++) {
249 hres = jsdisp_get_idx(array, i, &val);
250 if(hres == DISP_E_UNKNOWNNAME) {
251 hres = S_OK;
252 continue;
253 } else if(FAILED(hres))
254 break;
256 if(!is_undefined(val) && !is_null(val)) {
257 hres = to_string(ctx, val, str_tab+i);
258 jsval_release(val);
259 if(FAILED(hres))
260 break;
264 if(SUCCEEDED(hres)) {
265 DWORD seplen = 0, len = 0;
267 seplen = strlenW(sep);
269 if(str_tab[0])
270 len = jsstr_length(str_tab[0]);
271 for(i=1; i < length; i++) {
272 len += seplen;
273 if(str_tab[i])
274 len += jsstr_length(str_tab[i]);
275 if(len > JSSTR_MAX_LENGTH) {
276 hres = E_OUTOFMEMORY;
277 break;
281 if(SUCCEEDED(hres)) {
282 WCHAR *ptr = NULL;
284 ptr = jsstr_alloc_buf(len, &ret);
285 if(ptr) {
286 if(str_tab[0])
287 ptr += jsstr_flush(str_tab[0], ptr);
289 for(i=1; i < length; i++) {
290 if(seplen) {
291 memcpy(ptr, sep, seplen*sizeof(WCHAR));
292 ptr += seplen;
295 if(str_tab[i])
296 ptr += jsstr_flush(str_tab[i], ptr);
298 }else {
299 hres = E_OUTOFMEMORY;
304 for(i=0; i < length; i++) {
305 if(str_tab[i])
306 jsstr_release(str_tab[i]);
308 heap_free(str_tab);
309 if(FAILED(hres))
310 return hres;
312 TRACE("= %s\n", debugstr_jsstr(ret));
314 if(r)
315 *r = jsval_string(ret);
316 else
317 jsstr_release(ret);
318 return S_OK;
321 /* ECMA-262 3rd Edition 15.4.4.5 */
322 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
323 jsval_t *r)
325 jsdisp_t *jsthis;
326 DWORD length;
327 HRESULT hres;
329 TRACE("\n");
331 hres = get_length(ctx, vthis, &jsthis, &length);
332 if(FAILED(hres))
333 return hres;
335 if(argc) {
336 const WCHAR *sep;
337 jsstr_t *sep_str;
339 hres = to_flat_string(ctx, argv[0], &sep_str, &sep);
340 if(FAILED(hres))
341 return hres;
343 hres = array_join(ctx, jsthis, length, sep, r);
345 jsstr_release(sep_str);
346 }else {
347 hres = array_join(ctx, jsthis, length, default_separatorW, r);
350 return hres;
353 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
354 jsval_t *r)
356 jsdisp_t *jsthis;
357 jsval_t val;
358 DWORD length;
359 HRESULT hres;
361 TRACE("\n");
363 hres = get_length(ctx, vthis, &jsthis, &length);
364 if(FAILED(hres))
365 return hres;
367 if(!length) {
368 hres = set_length(jsthis, 0);
369 if(FAILED(hres))
370 return hres;
372 if(r)
373 *r = jsval_undefined();
374 return S_OK;
377 length--;
378 hres = jsdisp_get_idx(jsthis, length, &val);
379 if(SUCCEEDED(hres))
380 hres = jsdisp_delete_idx(jsthis, length);
381 else if(hres == DISP_E_UNKNOWNNAME)
382 val = jsval_undefined();
383 else
384 return hres;
386 if(SUCCEEDED(hres))
387 hres = set_length(jsthis, length);
389 if(FAILED(hres)) {
390 jsval_release(val);
391 return hres;
394 if(r)
395 *r = val;
396 else
397 jsval_release(val);
398 return hres;
401 /* ECMA-262 3rd Edition 15.4.4.7 */
402 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
403 jsval_t *r)
405 jsdisp_t *jsthis;
406 DWORD length = 0;
407 unsigned i;
408 HRESULT hres;
410 TRACE("\n");
412 hres = get_length(ctx, vthis, &jsthis, &length);
413 if(FAILED(hres))
414 return hres;
416 for(i=0; i < argc; i++) {
417 hres = jsdisp_propput_idx(jsthis, length+i, argv[i]);
418 if(FAILED(hres))
419 return hres;
422 hres = set_length(jsthis, length+argc);
423 if(FAILED(hres))
424 return hres;
426 if(r)
427 *r = jsval_number(length+argc);
428 return S_OK;
431 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
432 jsval_t *r)
434 jsdisp_t *jsthis;
435 DWORD length, k, l;
436 jsval_t v1, v2;
437 HRESULT hres1, hres2;
439 TRACE("\n");
441 hres1 = get_length(ctx, vthis, &jsthis, &length);
442 if(FAILED(hres1))
443 return hres1;
445 for(k=0; k<length/2; k++) {
446 l = length-k-1;
448 hres1 = jsdisp_get_idx(jsthis, k, &v1);
449 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
450 return hres1;
452 hres2 = jsdisp_get_idx(jsthis, l, &v2);
453 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
454 jsval_release(v1);
455 return hres2;
458 if(hres1 == DISP_E_UNKNOWNNAME)
459 hres1 = jsdisp_delete_idx(jsthis, l);
460 else
461 hres1 = jsdisp_propput_idx(jsthis, l, v1);
463 if(FAILED(hres1)) {
464 jsval_release(v1);
465 jsval_release(v2);
466 return hres1;
469 if(hres2 == DISP_E_UNKNOWNNAME)
470 hres2 = jsdisp_delete_idx(jsthis, k);
471 else
472 hres2 = jsdisp_propput_idx(jsthis, k, v2);
474 if(FAILED(hres2)) {
475 jsval_release(v2);
476 return hres2;
480 if(r)
481 *r = jsval_obj(jsdisp_addref(jsthis));
482 return S_OK;
485 /* ECMA-262 3rd Edition 15.4.4.9 */
486 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
487 jsval_t *r)
489 jsdisp_t *jsthis;
490 DWORD length = 0, i;
491 jsval_t v, ret;
492 HRESULT hres;
494 TRACE("\n");
496 hres = get_length(ctx, vthis, &jsthis, &length);
497 if(FAILED(hres))
498 return hres;
500 if(!length) {
501 hres = set_length(jsthis, 0);
502 if(FAILED(hres))
503 return hres;
505 if(r)
506 *r = jsval_undefined();
507 return S_OK;
510 hres = jsdisp_get_idx(jsthis, 0, &ret);
511 if(hres == DISP_E_UNKNOWNNAME) {
512 ret = jsval_undefined();
513 hres = S_OK;
516 for(i=1; SUCCEEDED(hres) && i<length; i++) {
517 hres = jsdisp_get_idx(jsthis, i, &v);
518 if(hres == DISP_E_UNKNOWNNAME)
519 hres = jsdisp_delete_idx(jsthis, i-1);
520 else if(SUCCEEDED(hres))
521 hres = jsdisp_propput_idx(jsthis, i-1, v);
524 if(SUCCEEDED(hres)) {
525 hres = jsdisp_delete_idx(jsthis, length-1);
526 if(SUCCEEDED(hres))
527 hres = set_length(jsthis, length-1);
530 if(FAILED(hres))
531 return hres;
533 if(r)
534 *r = ret;
535 else
536 jsval_release(ret);
537 return hres;
540 /* ECMA-262 3rd Edition 15.4.4.10 */
541 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
543 jsdisp_t *arr, *jsthis;
544 DOUBLE range;
545 DWORD length, start, end, idx;
546 HRESULT hres;
548 TRACE("\n");
550 hres = get_length(ctx, vthis, &jsthis, &length);
551 if(FAILED(hres))
552 return hres;
554 if(argc) {
555 hres = to_number(ctx, argv[0], &range);
556 if(FAILED(hres))
557 return hres;
559 range = floor(range);
560 if(-range>length || isnan(range)) start = 0;
561 else if(range < 0) start = range+length;
562 else if(range <= length) start = range;
563 else start = length;
565 else start = 0;
567 if(argc > 1) {
568 hres = to_number(ctx, argv[1], &range);
569 if(FAILED(hres))
570 return hres;
572 range = floor(range);
573 if(-range>length) end = 0;
574 else if(range < 0) end = range+length;
575 else if(range <= length) end = range;
576 else end = length;
578 else end = length;
580 hres = create_array(ctx, (end>start)?end-start:0, &arr);
581 if(FAILED(hres))
582 return hres;
584 for(idx=start; idx<end; idx++) {
585 jsval_t v;
587 hres = jsdisp_get_idx(jsthis, idx, &v);
588 if(hres == DISP_E_UNKNOWNNAME)
589 continue;
591 if(SUCCEEDED(hres)) {
592 hres = jsdisp_propput_idx(arr, idx-start, v);
593 jsval_release(v);
596 if(FAILED(hres)) {
597 jsdisp_release(arr);
598 return hres;
602 if(r)
603 *r = jsval_obj(arr);
604 else
605 jsdisp_release(arr);
607 return S_OK;
610 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, INT *cmp)
612 HRESULT hres;
614 if(cmp_func) {
615 jsval_t args[2] = {v1, v2};
616 jsval_t res;
617 double n;
619 hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res);
620 if(FAILED(hres))
621 return hres;
623 hres = to_number(ctx, res, &n);
624 jsval_release(res);
625 if(FAILED(hres))
626 return hres;
628 if(n == 0)
629 *cmp = 0;
630 *cmp = n > 0.0 ? 1 : -1;
631 }else if(is_undefined(v1)) {
632 *cmp = is_undefined(v2) ? 0 : 1;
633 }else if(is_undefined(v2)) {
634 *cmp = -1;
635 }else if(is_number(v1) && is_number(v2)) {
636 double d = get_number(v1)-get_number(v2);
637 if(d > 0.0)
638 *cmp = 1;
639 else
640 *cmp = d < -0.0 ? -1 : 0;
641 }else {
642 jsstr_t *x, *y;
644 hres = to_string(ctx, v1, &x);
645 if(FAILED(hres))
646 return hres;
648 hres = to_string(ctx, v2, &y);
649 if(SUCCEEDED(hres)) {
650 *cmp = jsstr_cmp(x, y);
651 jsstr_release(y);
653 jsstr_release(x);
654 if(FAILED(hres))
655 return hres;
658 return S_OK;
661 /* ECMA-262 3rd Edition 15.4.4.11 */
662 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
663 jsval_t *r)
665 jsdisp_t *jsthis, *cmp_func = NULL;
666 jsval_t *vtab, **sorttab = NULL;
667 DWORD length;
668 DWORD i;
669 HRESULT hres = S_OK;
671 TRACE("\n");
673 hres = get_length(ctx, vthis, &jsthis, &length);
674 if(FAILED(hres))
675 return hres;
677 if(argc > 1) {
678 WARN("invalid arg_cnt %d\n", argc);
679 return E_FAIL;
682 if(argc == 1) {
683 if(!is_object_instance(argv[0])) {
684 WARN("arg is not dispatch\n");
685 return E_FAIL;
688 cmp_func = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
689 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
690 WARN("cmp_func is not a function\n");
691 if(cmp_func)
692 jsdisp_release(cmp_func);
693 return E_FAIL;
697 if(!length) {
698 if(cmp_func)
699 jsdisp_release(cmp_func);
700 if(r)
701 *r = jsval_obj(jsdisp_addref(jsthis));
702 return S_OK;
705 vtab = heap_alloc_zero(length * sizeof(*vtab));
706 if(vtab) {
707 for(i=0; i<length; i++) {
708 hres = jsdisp_get_idx(jsthis, i, vtab+i);
709 if(hres == DISP_E_UNKNOWNNAME) {
710 vtab[i] = jsval_undefined();
711 hres = S_OK;
712 } else if(FAILED(hres)) {
713 WARN("Could not get elem %d: %08x\n", i, hres);
714 break;
717 }else {
718 hres = E_OUTOFMEMORY;
721 if(SUCCEEDED(hres)) {
722 sorttab = heap_alloc(length*2*sizeof(*sorttab));
723 if(!sorttab)
724 hres = E_OUTOFMEMORY;
727 /* merge-sort */
728 if(SUCCEEDED(hres)) {
729 jsval_t *tmpv, **tmpbuf;
730 INT cmp;
732 tmpbuf = sorttab + length;
733 for(i=0; i < length; i++)
734 sorttab[i] = vtab+i;
736 for(i=0; i < length/2; i++) {
737 hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], &cmp);
738 if(FAILED(hres))
739 break;
741 if(cmp < 0) {
742 tmpv = sorttab[2*i];
743 sorttab[2*i] = sorttab[2*i+1];
744 sorttab[2*i+1] = tmpv;
748 if(SUCCEEDED(hres)) {
749 DWORD k, a, b, bend;
751 for(k=2; k < length; k *= 2) {
752 for(i=0; i+k < length; i += 2*k) {
753 a = b = 0;
754 if(i+2*k <= length)
755 bend = k;
756 else
757 bend = length - (i+k);
759 memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
761 while(a < k && b < bend) {
762 hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], &cmp);
763 if(FAILED(hres))
764 break;
766 if(cmp < 0) {
767 sorttab[i+a+b] = tmpbuf[a];
768 a++;
769 }else {
770 sorttab[i+a+b] = sorttab[i+k+b];
771 b++;
775 if(FAILED(hres))
776 break;
778 if(a < k)
779 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
782 if(FAILED(hres))
783 break;
787 for(i=0; SUCCEEDED(hres) && i < length; i++)
788 hres = jsdisp_propput_idx(jsthis, i, *sorttab[i]);
791 if(vtab) {
792 for(i=0; i < length; i++)
793 jsval_release(vtab[i]);
794 heap_free(vtab);
796 heap_free(sorttab);
797 if(cmp_func)
798 jsdisp_release(cmp_func);
800 if(FAILED(hres))
801 return hres;
803 if(r)
804 *r = jsval_obj(jsdisp_addref(jsthis));
805 return S_OK;
808 /* ECMA-262 3rd Edition 15.4.4.12 */
809 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
810 jsval_t *r)
812 DWORD length, start=0, delete_cnt=0, i, add_args = 0;
813 jsdisp_t *ret_array = NULL, *jsthis;
814 jsval_t val;
815 double d;
816 int n;
817 HRESULT hres = S_OK;
819 TRACE("\n");
821 hres = get_length(ctx, vthis, &jsthis, &length);
822 if(FAILED(hres))
823 return hres;
825 if(argc) {
826 hres = to_integer(ctx, argv[0], &d);
827 if(FAILED(hres))
828 return hres;
830 if(is_int32(d)) {
831 if((n = d) >= 0)
832 start = min(n, length);
833 else
834 start = -n > length ? 0 : length + n;
835 }else {
836 start = d < 0.0 ? 0 : length;
840 if(argc >= 2) {
841 hres = to_integer(ctx, argv[1], &d);
842 if(FAILED(hres))
843 return hres;
845 if(is_int32(d)) {
846 if((n = d) > 0)
847 delete_cnt = min(n, length-start);
848 }else if(d > 0.0) {
849 delete_cnt = length-start;
852 add_args = argc-2;
855 if(r) {
856 hres = create_array(ctx, 0, &ret_array);
857 if(FAILED(hres))
858 return hres;
860 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
861 hres = jsdisp_get_idx(jsthis, start+i, &val);
862 if(hres == DISP_E_UNKNOWNNAME) {
863 hres = S_OK;
864 }else if(SUCCEEDED(hres)) {
865 hres = jsdisp_propput_idx(ret_array, i, val);
866 jsval_release(val);
870 if(SUCCEEDED(hres))
871 hres = jsdisp_propput_name(ret_array, lengthW, jsval_number(delete_cnt));
874 if(add_args < delete_cnt) {
875 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
876 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val);
877 if(hres == DISP_E_UNKNOWNNAME) {
878 hres = jsdisp_delete_idx(jsthis, i+add_args);
879 }else if(SUCCEEDED(hres)) {
880 hres = jsdisp_propput_idx(jsthis, i+add_args, val);
881 jsval_release(val);
885 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
886 hres = jsdisp_delete_idx(jsthis, i-1);
887 }else if(add_args > delete_cnt) {
888 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
889 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val);
890 if(hres == DISP_E_UNKNOWNNAME) {
891 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
892 }else if(SUCCEEDED(hres)) {
893 hres = jsdisp_propput_idx(jsthis, i+add_args-1, val);
894 jsval_release(val);
899 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
900 hres = jsdisp_propput_idx(jsthis, start+i, argv[i+2]);
902 if(SUCCEEDED(hres))
903 hres = jsdisp_propput_name(jsthis, lengthW, jsval_number(length-delete_cnt+add_args));
905 if(FAILED(hres)) {
906 if(ret_array)
907 jsdisp_release(ret_array);
908 return hres;
911 if(r)
912 *r = jsval_obj(ret_array);
913 return S_OK;
916 /* ECMA-262 3rd Edition 15.4.4.2 */
917 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
918 jsval_t *r)
920 ArrayInstance *array;
922 TRACE("\n");
924 array = array_this(jsthis);
925 if(!array)
926 return throw_type_error(ctx, JS_E_ARRAY_EXPECTED, NULL);
928 return array_join(ctx, &array->dispex, array->length, default_separatorW, r);
931 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
932 jsval_t *r)
934 FIXME("\n");
935 return E_NOTIMPL;
938 /* ECMA-262 3rd Edition 15.4.4.13 */
939 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
940 jsval_t *r)
942 jsdisp_t *jsthis;
943 WCHAR buf[14], *buf_end, *str;
944 DWORD i, length;
945 jsval_t val;
946 DISPID id;
947 HRESULT hres;
949 TRACE("\n");
951 hres = get_length(ctx, vthis, &jsthis, &length);
952 if(FAILED(hres))
953 return hres;
955 if(argc) {
956 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
957 *buf_end-- = 0;
958 i = length;
960 while(i--) {
961 str = idx_to_str(i, buf_end);
963 hres = jsdisp_get_id(jsthis, str, 0, &id);
964 if(SUCCEEDED(hres)) {
965 hres = jsdisp_propget(jsthis, id, &val);
966 if(FAILED(hres))
967 return hres;
969 hres = jsdisp_propput_idx(jsthis, i+argc, val);
970 jsval_release(val);
971 }else if(hres == DISP_E_UNKNOWNNAME) {
972 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
976 if(FAILED(hres))
977 return hres;
980 for(i=0; i<argc; i++) {
981 hres = jsdisp_propput_idx(jsthis, i, argv[i]);
982 if(FAILED(hres))
983 return hres;
986 if(argc) {
987 length += argc;
988 hres = set_length(jsthis, length);
989 if(FAILED(hres))
990 return hres;
993 if(r)
994 *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
995 return S_OK;
998 static HRESULT Array_get_value(script_ctx_t *ctx, vdisp_t *jsthis, jsval_t *r)
1000 TRACE("\n");
1002 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, r);
1005 static void Array_destructor(jsdisp_t *dispex)
1007 heap_free(dispex);
1010 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1012 ArrayInstance *array = (ArrayInstance*)dispex;
1013 const WCHAR *ptr = name;
1014 DWORD id = 0;
1016 if(!isdigitW(*ptr))
1017 return;
1019 while(*ptr && isdigitW(*ptr)) {
1020 id = id*10 + (*ptr-'0');
1021 ptr++;
1024 if(*ptr)
1025 return;
1027 if(id >= array->length)
1028 array->length = id+1;
1031 static const builtin_prop_t Array_props[] = {
1032 {concatW, Array_concat, PROPF_METHOD|1},
1033 {joinW, Array_join, PROPF_METHOD|1},
1034 {lengthW, NULL,0, Array_get_length, Array_set_length},
1035 {popW, Array_pop, PROPF_METHOD},
1036 {pushW, Array_push, PROPF_METHOD|1},
1037 {reverseW, Array_reverse, PROPF_METHOD},
1038 {shiftW, Array_shift, PROPF_METHOD},
1039 {sliceW, Array_slice, PROPF_METHOD|2},
1040 {sortW, Array_sort, PROPF_METHOD|1},
1041 {spliceW, Array_splice, PROPF_METHOD|2},
1042 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1043 {toStringW, Array_toString, PROPF_METHOD},
1044 {unshiftW, Array_unshift, PROPF_METHOD|1},
1047 static const builtin_info_t Array_info = {
1048 JSCLASS_ARRAY,
1049 {NULL, NULL,0, Array_get_value},
1050 sizeof(Array_props)/sizeof(*Array_props),
1051 Array_props,
1052 Array_destructor,
1053 Array_on_put
1056 static const builtin_prop_t ArrayInst_props[] = {
1057 {lengthW, NULL,0, Array_get_length, Array_set_length}
1060 static const builtin_info_t ArrayInst_info = {
1061 JSCLASS_ARRAY,
1062 {NULL, NULL,0, Array_get_value},
1063 sizeof(ArrayInst_props)/sizeof(*ArrayInst_props),
1064 ArrayInst_props,
1065 Array_destructor,
1066 Array_on_put
1069 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1070 jsval_t *r)
1072 jsdisp_t *obj;
1073 DWORD i;
1074 HRESULT hres;
1076 TRACE("\n");
1078 switch(flags) {
1079 case DISPATCH_METHOD:
1080 case DISPATCH_CONSTRUCT: {
1081 if(argc == 1 && is_number(argv[0])) {
1082 double n = get_number(argv[0]);
1084 if(n < 0 || !is_int32(n))
1085 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
1087 hres = create_array(ctx, n, &obj);
1088 if(FAILED(hres))
1089 return hres;
1091 *r = jsval_obj(obj);
1092 return S_OK;
1095 hres = create_array(ctx, argc, &obj);
1096 if(FAILED(hres))
1097 return hres;
1099 for(i=0; i < argc; i++) {
1100 hres = jsdisp_propput_idx(obj, i, argv[i]);
1101 if(FAILED(hres))
1102 break;
1104 if(FAILED(hres)) {
1105 jsdisp_release(obj);
1106 return hres;
1109 *r = jsval_obj(obj);
1110 break;
1112 default:
1113 FIXME("unimplemented flags: %x\n", flags);
1114 return E_NOTIMPL;
1117 return S_OK;
1120 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1122 ArrayInstance *array;
1123 HRESULT hres;
1125 array = heap_alloc_zero(sizeof(ArrayInstance));
1126 if(!array)
1127 return E_OUTOFMEMORY;
1129 if(object_prototype)
1130 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1131 else
1132 hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1134 if(FAILED(hres)) {
1135 heap_free(array);
1136 return hres;
1139 *ret = array;
1140 return S_OK;
1143 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1145 ArrayInstance *array;
1146 HRESULT hres;
1148 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1150 hres = alloc_array(ctx, object_prototype, &array);
1151 if(FAILED(hres))
1152 return hres;
1154 hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1156 jsdisp_release(&array->dispex);
1157 return hres;
1160 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1162 ArrayInstance *array;
1163 HRESULT hres;
1165 hres = alloc_array(ctx, NULL, &array);
1166 if(FAILED(hres))
1167 return hres;
1169 array->length = length;
1171 *ret = &array->dispex;
1172 return S_OK;