vbscript: Added support for do..loop statement without an expression.
[wine.git] / dlls / jscript / array.c
blobe66b0ddb166876fc84f3e0c142527f36fabe12f7
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, jsexcept_t *ei, 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, ei, JS_E_JSCRIPT_EXPECTED, NULL);
78 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &val, ei);
79 if(FAILED(hres))
80 return hres;
82 hres = to_uint32(ctx, val, ei, 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, jsexcept_t *ei, 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), ei);
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_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
117 jsval_t *r, jsexcept_t *ei)
119 ArrayInstance *This = array_from_vdisp(jsthis);
121 TRACE("%p %d\n", This, This->length);
123 switch(flags) {
124 case DISPATCH_PROPERTYGET:
125 *r = jsval_number(This->length);
126 break;
127 case DISPATCH_PROPERTYPUT: {
128 DOUBLE len = -1;
129 DWORD i;
130 HRESULT hres;
132 hres = to_number(ctx, argv[0], ei, &len);
133 if(FAILED(hres))
134 return hres;
136 len = floor(len);
137 if(len!=(DWORD)len)
138 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
140 for(i=len; i<This->length; i++) {
141 hres = jsdisp_delete_idx(&This->dispex, i);
142 if(FAILED(hres))
143 return hres;
146 This->length = len;
147 break;
149 default:
150 FIXME("unimplemented flags %x\n", flags);
151 return E_NOTIMPL;
154 return S_OK;
157 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
159 jsval_t val;
160 DWORD i;
161 HRESULT hres;
163 for(i=0; i < obj->length; i++) {
164 hres = jsdisp_get_idx(&obj->dispex, i, &val, ei);
165 if(hres == DISP_E_UNKNOWNNAME)
166 continue;
167 if(FAILED(hres))
168 return hres;
170 hres = jsdisp_propput_idx(array, *len+i, val, ei);
171 jsval_release(val);
172 if(FAILED(hres))
173 return hres;
176 *len += obj->length;
177 return S_OK;
180 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
182 jsdisp_t *jsobj;
183 HRESULT hres;
185 jsobj = iface_to_jsdisp((IUnknown*)obj);
186 if(jsobj) {
187 if(is_class(jsobj, JSCLASS_ARRAY)) {
188 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei);
189 jsdisp_release(jsobj);
190 return hres;
192 jsdisp_release(jsobj);
195 return jsdisp_propput_idx(array, (*len)++, jsval_disp(obj), ei);
198 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
199 jsval_t *r, jsexcept_t *ei)
201 jsdisp_t *ret;
202 DWORD len = 0;
203 HRESULT hres;
205 TRACE("\n");
207 hres = create_array(ctx, 0, &ret);
208 if(FAILED(hres))
209 return hres;
211 hres = concat_obj(ret, jsthis->u.disp, &len, ei);
212 if(SUCCEEDED(hres)) {
213 DWORD i;
215 for(i=0; i < argc; i++) {
216 if(is_object_instance(argv[i]))
217 hres = concat_obj(ret, get_object(argv[i]), &len, ei);
218 else
219 hres = jsdisp_propput_idx(ret, len++, argv[i], ei);
220 if(FAILED(hres))
221 break;
225 if(FAILED(hres))
226 return hres;
228 if(r)
229 *r = jsval_obj(ret);
230 else
231 jsdisp_release(ret);
232 return S_OK;
235 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, jsval_t *r, jsexcept_t *ei)
237 BSTR *str_tab, ret = NULL;
238 jsval_t val;
239 DWORD i;
240 HRESULT hres = E_FAIL;
242 if(!length) {
243 if(r) {
244 BSTR ret = SysAllocStringLen(NULL, 0);
245 if(!ret)
246 return E_OUTOFMEMORY;
247 *r = jsval_string(ret);
249 return S_OK;
252 str_tab = heap_alloc_zero(length * sizeof(BSTR));
253 if(!str_tab)
254 return E_OUTOFMEMORY;
256 for(i=0; i < length; i++) {
257 hres = jsdisp_get_idx(array, i, &val, ei);
258 if(hres == DISP_E_UNKNOWNNAME) {
259 hres = S_OK;
260 continue;
261 } else if(FAILED(hres))
262 break;
264 if(!is_undefined(val) && !is_null(val)) {
265 hres = to_string(ctx, val, ei, str_tab+i);
266 jsval_release(val);
267 if(FAILED(hres))
268 break;
272 if(SUCCEEDED(hres)) {
273 DWORD seplen = 0, len = 0;
274 WCHAR *ptr;
276 seplen = strlenW(sep);
278 if(str_tab[0])
279 len = SysStringLen(str_tab[0]);
280 for(i=1; i < length; i++)
281 len += seplen + SysStringLen(str_tab[i]);
283 ret = SysAllocStringLen(NULL, len);
284 if(ret) {
285 DWORD tmplen = 0;
287 if(str_tab[0]) {
288 tmplen = SysStringLen(str_tab[0]);
289 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
292 ptr = ret + tmplen;
293 for(i=1; i < length; i++) {
294 if(seplen) {
295 memcpy(ptr, sep, seplen*sizeof(WCHAR));
296 ptr += seplen;
299 if(str_tab[i]) {
300 tmplen = SysStringLen(str_tab[i]);
301 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
302 ptr += tmplen;
305 *ptr=0;
306 }else {
307 hres = E_OUTOFMEMORY;
311 for(i=0; i < length; i++)
312 SysFreeString(str_tab[i]);
313 heap_free(str_tab);
314 if(FAILED(hres))
315 return hres;
317 TRACE("= %s\n", debugstr_w(ret));
319 if(r) {
320 if(!ret) {
321 ret = SysAllocStringLen(NULL, 0);
322 if(!ret)
323 return E_OUTOFMEMORY;
326 *r = jsval_string(ret);
327 }else {
328 SysFreeString(ret);
331 return S_OK;
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,
336 jsval_t *r, jsexcept_t *ei)
338 jsdisp_t *jsthis;
339 DWORD length;
340 HRESULT hres;
342 TRACE("\n");
344 hres = get_length(ctx, vthis, ei, &jsthis, &length);
345 if(FAILED(hres))
346 return hres;
348 if(argc) {
349 BSTR sep;
351 hres = to_string(ctx, argv[0], ei, &sep);
352 if(FAILED(hres))
353 return hres;
355 hres = array_join(ctx, jsthis, length, sep, r, ei);
357 SysFreeString(sep);
358 }else {
359 hres = array_join(ctx, jsthis, length, default_separatorW, r, ei);
362 return hres;
365 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
366 jsval_t *r, jsexcept_t *ei)
368 jsdisp_t *jsthis;
369 jsval_t val;
370 DWORD length;
371 HRESULT hres;
373 TRACE("\n");
375 hres = get_length(ctx, vthis, ei, &jsthis, &length);
376 if(FAILED(hres))
377 return hres;
379 if(!length) {
380 hres = set_length(jsthis, ei, 0);
381 if(FAILED(hres))
382 return hres;
384 if(r)
385 *r = jsval_undefined();
386 return S_OK;
389 length--;
390 hres = jsdisp_get_idx(jsthis, length, &val, ei);
391 if(SUCCEEDED(hres))
392 hres = jsdisp_delete_idx(jsthis, length);
393 else if(hres == DISP_E_UNKNOWNNAME)
394 val = jsval_undefined();
395 else
396 return hres;
398 if(SUCCEEDED(hres))
399 hres = set_length(jsthis, ei, length);
401 if(FAILED(hres)) {
402 jsval_release(val);
403 return hres;
406 if(r)
407 *r = val;
408 else
409 jsval_release(val);
410 return hres;
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,
415 jsval_t *r, jsexcept_t *ei)
417 jsdisp_t *jsthis;
418 DWORD length = 0;
419 unsigned i;
420 HRESULT hres;
422 TRACE("\n");
424 hres = get_length(ctx, vthis, ei, &jsthis, &length);
425 if(FAILED(hres))
426 return hres;
428 for(i=0; i < argc; i++) {
429 hres = jsdisp_propput_idx(jsthis, length+i, argv[i], ei);
430 if(FAILED(hres))
431 return hres;
434 hres = set_length(jsthis, ei, length+argc);
435 if(FAILED(hres))
436 return hres;
438 if(r)
439 *r = jsval_number(length+argc);
440 return S_OK;
443 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
444 jsval_t *r, jsexcept_t *ei)
446 jsdisp_t *jsthis;
447 DWORD length, k, l;
448 jsval_t v1, v2;
449 HRESULT hres1, hres2;
451 TRACE("\n");
453 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
454 if(FAILED(hres1))
455 return hres1;
457 for(k=0; k<length/2; k++) {
458 l = length-k-1;
460 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei);
461 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
462 return hres1;
464 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
465 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
466 jsval_release(v1);
467 return hres2;
470 if(hres1 == DISP_E_UNKNOWNNAME)
471 hres1 = jsdisp_delete_idx(jsthis, l);
472 else
473 hres1 = jsdisp_propput_idx(jsthis, l, v1, ei);
475 if(FAILED(hres1)) {
476 jsval_release(v1);
477 jsval_release(v2);
478 return hres1;
481 if(hres2 == DISP_E_UNKNOWNNAME)
482 hres2 = jsdisp_delete_idx(jsthis, k);
483 else
484 hres2 = jsdisp_propput_idx(jsthis, k, v2, ei);
486 if(FAILED(hres2)) {
487 jsval_release(v2);
488 return hres2;
492 if(r)
493 *r = jsval_obj(jsdisp_addref(jsthis));
494 return S_OK;
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,
499 jsval_t *r, jsexcept_t *ei)
501 jsdisp_t *jsthis;
502 DWORD length = 0, i;
503 jsval_t v, ret;
504 HRESULT hres;
506 TRACE("\n");
508 hres = get_length(ctx, vthis, ei, &jsthis, &length);
509 if(FAILED(hres))
510 return hres;
512 if(!length) {
513 hres = set_length(jsthis, ei, 0);
514 if(FAILED(hres))
515 return hres;
518 if(!length) {
519 if(r)
520 *r = jsval_undefined();
521 return S_OK;
524 hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
525 if(hres == DISP_E_UNKNOWNNAME) {
526 ret = jsval_undefined();
527 hres = S_OK;
530 for(i=1; SUCCEEDED(hres) && i<length; i++) {
531 hres = jsdisp_get_idx(jsthis, i, &v, ei);
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, ei);
538 if(SUCCEEDED(hres)) {
539 hres = jsdisp_delete_idx(jsthis, length-1);
540 if(SUCCEEDED(hres))
541 hres = set_length(jsthis, ei, length-1);
544 if(FAILED(hres))
545 return hres;
547 if(r)
548 *r = ret;
549 else
550 jsval_release(ret);
551 return hres;
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,
556 jsval_t *r, jsexcept_t *ei)
558 jsdisp_t *arr, *jsthis;
559 DOUBLE range;
560 DWORD length, start, end, idx;
561 HRESULT hres;
563 TRACE("\n");
565 hres = get_length(ctx, vthis, ei, &jsthis, &length);
566 if(FAILED(hres))
567 return hres;
569 if(argc) {
570 hres = to_number(ctx, argv[0], ei, &range);
571 if(FAILED(hres))
572 return hres;
574 range = floor(range);
575 if(-range>length || isnan(range)) start = 0;
576 else if(range < 0) start = range+length;
577 else if(range <= length) start = range;
578 else start = length;
580 else start = 0;
582 if(argc > 1) {
583 hres = to_number(ctx, argv[1], ei, &range);
584 if(FAILED(hres))
585 return hres;
587 range = floor(range);
588 if(-range>length) end = 0;
589 else if(range < 0) end = range+length;
590 else if(range <= length) end = range;
591 else end = length;
593 else end = length;
595 hres = create_array(ctx, (end>start)?end-start:0, &arr);
596 if(FAILED(hres))
597 return hres;
599 for(idx=start; idx<end; idx++) {
600 jsval_t v;
602 hres = jsdisp_get_idx(jsthis, idx, &v, ei);
603 if(hres == DISP_E_UNKNOWNNAME)
604 continue;
606 if(SUCCEEDED(hres)) {
607 hres = jsdisp_propput_idx(arr, idx-start, v, ei);
608 jsval_release(v);
611 if(FAILED(hres)) {
612 jsdisp_release(arr);
613 return hres;
617 if(r)
618 *r = jsval_obj(arr);
619 else
620 jsdisp_release(arr);
622 return S_OK;
625 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, jsexcept_t *ei, INT *cmp)
627 HRESULT hres;
629 if(cmp_func) {
630 jsval_t args[2] = {v1, v2};
631 jsval_t res;
632 double n;
634 hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res, ei);
635 if(FAILED(hres))
636 return hres;
638 hres = to_number(ctx, res, ei, &n);
639 jsval_release(res);
640 if(FAILED(hres))
641 return hres;
643 if(n == 0)
644 *cmp = 0;
645 *cmp = n > 0.0 ? 1 : -1;
646 }else if(is_undefined(v1)) {
647 *cmp = is_undefined(v2) ? 0 : 1;
648 }else if(is_undefined(v2)) {
649 *cmp = -1;
650 }else if(is_number(v1) && is_number(v2)) {
651 double d = get_number(v1)-get_number(v2);
652 if(d > 0.0)
653 *cmp = 1;
654 else
655 *cmp = d < -0.0 ? -1 : 0;
656 }else {
657 BSTR x, y;
659 hres = to_string(ctx, v1, ei, &x);
660 if(FAILED(hres))
661 return hres;
663 hres = to_string(ctx, v2, ei, &y);
664 if(SUCCEEDED(hres)) {
665 *cmp = strcmpW(x, y);
666 SysFreeString(y);
668 SysFreeString(x);
669 if(FAILED(hres))
670 return hres;
673 return S_OK;
676 /* ECMA-262 3rd Edition 15.4.4.11 */
677 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
678 jsval_t *r, jsexcept_t *ei)
680 jsdisp_t *jsthis, *cmp_func = NULL;
681 jsval_t *vtab, **sorttab = NULL;
682 DWORD length;
683 DWORD i;
684 HRESULT hres = S_OK;
686 TRACE("\n");
688 hres = get_length(ctx, vthis, ei, &jsthis, &length);
689 if(FAILED(hres))
690 return hres;
692 if(argc > 1) {
693 WARN("invalid arg_cnt %d\n", argc);
694 return E_FAIL;
697 if(argc == 1) {
698 if(!is_object_instance(argv[0])) {
699 WARN("arg is not dispatch\n");
700 return E_FAIL;
703 cmp_func = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
704 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
705 WARN("cmp_func is not a function\n");
706 if(cmp_func)
707 jsdisp_release(cmp_func);
708 return E_FAIL;
712 if(!length) {
713 if(cmp_func)
714 jsdisp_release(cmp_func);
715 if(r)
716 *r = jsval_obj(jsdisp_addref(jsthis));
717 return S_OK;
720 vtab = heap_alloc_zero(length * sizeof(*vtab));
721 if(vtab) {
722 for(i=0; i<length; i++) {
723 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei);
724 if(hres == DISP_E_UNKNOWNNAME) {
725 vtab[i] = jsval_undefined();
726 hres = S_OK;
727 } else if(FAILED(hres)) {
728 WARN("Could not get elem %d: %08x\n", i, hres);
729 break;
732 }else {
733 hres = E_OUTOFMEMORY;
736 if(SUCCEEDED(hres)) {
737 sorttab = heap_alloc(length*2*sizeof(*sorttab));
738 if(!sorttab)
739 hres = E_OUTOFMEMORY;
742 /* merge-sort */
743 if(SUCCEEDED(hres)) {
744 jsval_t *tmpv, **tmpbuf;
745 INT cmp;
747 tmpbuf = sorttab + length;
748 for(i=0; i < length; i++)
749 sorttab[i] = vtab+i;
751 for(i=0; i < length/2; i++) {
752 hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], ei, &cmp);
753 if(FAILED(hres))
754 break;
756 if(cmp < 0) {
757 tmpv = sorttab[2*i];
758 sorttab[2*i] = sorttab[2*i+1];
759 sorttab[2*i+1] = tmpv;
763 if(SUCCEEDED(hres)) {
764 DWORD k, a, b, bend;
766 for(k=2; k < length; k *= 2) {
767 for(i=0; i+k < length; i += 2*k) {
768 a = b = 0;
769 if(i+2*k <= length)
770 bend = k;
771 else
772 bend = length - (i+k);
774 memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
776 while(a < k && b < bend) {
777 hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], ei, &cmp);
778 if(FAILED(hres))
779 break;
781 if(cmp < 0) {
782 sorttab[i+a+b] = tmpbuf[a];
783 a++;
784 }else {
785 sorttab[i+a+b] = sorttab[i+k+b];
786 b++;
790 if(FAILED(hres))
791 break;
793 if(a < k)
794 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
797 if(FAILED(hres))
798 break;
802 for(i=0; SUCCEEDED(hres) && i < length; i++)
803 hres = jsdisp_propput_idx(jsthis, i, *sorttab[i], ei);
806 if(vtab) {
807 for(i=0; i < length; i++)
808 jsval_release(vtab[i]);
809 heap_free(vtab);
811 heap_free(sorttab);
812 if(cmp_func)
813 jsdisp_release(cmp_func);
815 if(FAILED(hres))
816 return hres;
818 if(r)
819 *r = jsval_obj(jsdisp_addref(jsthis));
820 return S_OK;
823 /* ECMA-262 3rd Edition 15.4.4.12 */
824 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
825 jsval_t *r, jsexcept_t *ei)
827 DWORD length, start=0, delete_cnt=0, i, add_args = 0;
828 jsdisp_t *ret_array = NULL, *jsthis;
829 jsval_t val;
830 double d;
831 int n;
832 HRESULT hres = S_OK;
834 TRACE("\n");
836 hres = get_length(ctx, vthis, ei, &jsthis, &length);
837 if(FAILED(hres))
838 return hres;
840 if(argc) {
841 hres = to_integer(ctx, argv[0], ei, &d);
842 if(FAILED(hres))
843 return hres;
845 if(is_int32(d)) {
846 if((n = d) >= 0)
847 start = min(n, length);
848 else
849 start = -n > length ? 0 : length + n;
850 }else {
851 start = d < 0.0 ? 0 : length;
855 if(argc >= 2) {
856 hres = to_integer(ctx, argv[1], ei, &d);
857 if(FAILED(hres))
858 return hres;
860 if(is_int32(d)) {
861 if((n = d) > 0)
862 delete_cnt = min(n, length-start);
863 }else if(d > 0.0) {
864 delete_cnt = length-start;
867 add_args = argc-2;
870 if(r) {
871 hres = create_array(ctx, 0, &ret_array);
872 if(FAILED(hres))
873 return hres;
875 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
876 hres = jsdisp_get_idx(jsthis, start+i, &val, ei);
877 if(hres == DISP_E_UNKNOWNNAME) {
878 hres = S_OK;
879 }else if(SUCCEEDED(hres)) {
880 hres = jsdisp_propput_idx(ret_array, i, val, ei);
881 jsval_release(val);
885 if(SUCCEEDED(hres))
886 hres = jsdisp_propput_name(ret_array, lengthW, jsval_number(delete_cnt), ei);
889 if(add_args < delete_cnt) {
890 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
891 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val, ei);
892 if(hres == DISP_E_UNKNOWNNAME) {
893 hres = jsdisp_delete_idx(jsthis, i+add_args);
894 }else if(SUCCEEDED(hres)) {
895 hres = jsdisp_propput_idx(jsthis, i+add_args, val, ei);
896 jsval_release(val);
900 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
901 hres = jsdisp_delete_idx(jsthis, i-1);
902 }else if(add_args > delete_cnt) {
903 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
904 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val, ei);
905 if(hres == DISP_E_UNKNOWNNAME) {
906 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
907 }else if(SUCCEEDED(hres)) {
908 hres = jsdisp_propput_idx(jsthis, i+add_args-1, val, ei);
909 jsval_release(val);
914 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
915 hres = jsdisp_propput_idx(jsthis, start+i, argv[i+2], ei);
917 if(SUCCEEDED(hres))
918 hres = jsdisp_propput_name(jsthis, lengthW, jsval_number(length-delete_cnt+add_args), ei);
920 if(FAILED(hres)) {
921 if(ret_array)
922 jsdisp_release(ret_array);
923 return hres;
926 if(r)
927 *r = jsval_obj(ret_array);
928 return S_OK;
931 /* ECMA-262 3rd Edition 15.4.4.2 */
932 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
933 jsval_t *r, jsexcept_t *ei)
935 ArrayInstance *array;
937 TRACE("\n");
939 array = array_this(jsthis);
940 if(!array)
941 return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
943 return array_join(ctx, &array->dispex, array->length, default_separatorW, r, ei);
946 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
947 jsval_t *r, jsexcept_t *ei)
949 FIXME("\n");
950 return E_NOTIMPL;
953 /* ECMA-262 3rd Edition 15.4.4.13 */
954 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
955 jsval_t *r, jsexcept_t *ei)
957 jsdisp_t *jsthis;
958 WCHAR buf[14], *buf_end, *str;
959 DWORD i, length;
960 jsval_t val;
961 DISPID id;
962 HRESULT hres;
964 TRACE("\n");
966 hres = get_length(ctx, vthis, ei, &jsthis, &length);
967 if(FAILED(hres))
968 return hres;
970 if(argc) {
971 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
972 *buf_end-- = 0;
973 i = length;
975 while(i--) {
976 str = idx_to_str(i, buf_end);
978 hres = jsdisp_get_id(jsthis, str, 0, &id);
979 if(SUCCEEDED(hres)) {
980 hres = jsdisp_propget(jsthis, id, &val, ei);
981 if(FAILED(hres))
982 return hres;
984 hres = jsdisp_propput_idx(jsthis, i+argc, val, ei);
985 jsval_release(val);
986 }else if(hres == DISP_E_UNKNOWNNAME) {
987 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
991 if(FAILED(hres))
992 return hres;
995 for(i=0; i<argc; i++) {
996 hres = jsdisp_propput_idx(jsthis, i, argv[i], ei);
997 if(FAILED(hres))
998 return hres;
1001 if(argc) {
1002 length += argc;
1003 hres = set_length(jsthis, ei, length);
1004 if(FAILED(hres))
1005 return hres;
1008 if(r)
1009 *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
1010 return S_OK;
1013 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1014 jsval_t *r, jsexcept_t *ei)
1016 TRACE("\n");
1018 switch(flags) {
1019 case INVOKE_FUNC:
1020 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1021 case INVOKE_PROPERTYGET:
1022 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, r, ei);
1023 default:
1024 FIXME("unimplemented flags %x\n", flags);
1025 return E_NOTIMPL;
1028 return S_OK;
1031 static void Array_destructor(jsdisp_t *dispex)
1033 heap_free(dispex);
1036 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1038 ArrayInstance *array = (ArrayInstance*)dispex;
1039 const WCHAR *ptr = name;
1040 DWORD id = 0;
1042 if(!isdigitW(*ptr))
1043 return;
1045 while(*ptr && isdigitW(*ptr)) {
1046 id = id*10 + (*ptr-'0');
1047 ptr++;
1050 if(*ptr)
1051 return;
1053 if(id >= array->length)
1054 array->length = id+1;
1057 static const builtin_prop_t Array_props[] = {
1058 {concatW, Array_concat, PROPF_METHOD|1},
1059 {joinW, Array_join, PROPF_METHOD|1},
1060 {lengthW, Array_length, 0},
1061 {popW, Array_pop, PROPF_METHOD},
1062 {pushW, Array_push, PROPF_METHOD|1},
1063 {reverseW, Array_reverse, PROPF_METHOD},
1064 {shiftW, Array_shift, PROPF_METHOD},
1065 {sliceW, Array_slice, PROPF_METHOD|2},
1066 {sortW, Array_sort, PROPF_METHOD|1},
1067 {spliceW, Array_splice, PROPF_METHOD|2},
1068 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1069 {toStringW, Array_toString, PROPF_METHOD},
1070 {unshiftW, Array_unshift, PROPF_METHOD|1},
1073 static const builtin_info_t Array_info = {
1074 JSCLASS_ARRAY,
1075 {NULL, Array_value, 0},
1076 sizeof(Array_props)/sizeof(*Array_props),
1077 Array_props,
1078 Array_destructor,
1079 Array_on_put
1082 static const builtin_prop_t ArrayInst_props[] = {
1083 {lengthW, Array_length, 0}
1086 static const builtin_info_t ArrayInst_info = {
1087 JSCLASS_ARRAY,
1088 {NULL, Array_value, 0},
1089 sizeof(ArrayInst_props)/sizeof(*ArrayInst_props),
1090 ArrayInst_props,
1091 Array_destructor,
1092 Array_on_put
1095 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1096 jsval_t *r, jsexcept_t *ei)
1098 jsdisp_t *obj;
1099 DWORD i;
1100 HRESULT hres;
1102 TRACE("\n");
1104 switch(flags) {
1105 case DISPATCH_METHOD:
1106 case DISPATCH_CONSTRUCT: {
1107 if(argc == 1 && is_number(argv[0])) {
1108 double n = get_number(argv[0]);
1110 if(n < 0 || !is_int32(n))
1111 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1113 hres = create_array(ctx, n, &obj);
1114 if(FAILED(hres))
1115 return hres;
1117 *r = jsval_obj(obj);
1118 return S_OK;
1121 hres = create_array(ctx, argc, &obj);
1122 if(FAILED(hres))
1123 return hres;
1125 for(i=0; i < argc; i++) {
1126 hres = jsdisp_propput_idx(obj, i, argv[i], ei);
1127 if(FAILED(hres))
1128 break;
1130 if(FAILED(hres)) {
1131 jsdisp_release(obj);
1132 return hres;
1135 *r = jsval_obj(obj);
1136 break;
1138 default:
1139 FIXME("unimplemented flags: %x\n", flags);
1140 return E_NOTIMPL;
1143 return S_OK;
1146 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1148 ArrayInstance *array;
1149 HRESULT hres;
1151 array = heap_alloc_zero(sizeof(ArrayInstance));
1152 if(!array)
1153 return E_OUTOFMEMORY;
1155 if(object_prototype)
1156 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1157 else
1158 hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1160 if(FAILED(hres)) {
1161 heap_free(array);
1162 return hres;
1165 *ret = array;
1166 return S_OK;
1169 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1171 ArrayInstance *array;
1172 HRESULT hres;
1174 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1176 hres = alloc_array(ctx, object_prototype, &array);
1177 if(FAILED(hres))
1178 return hres;
1180 hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1182 jsdisp_release(&array->dispex);
1183 return hres;
1186 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1188 ArrayInstance *array;
1189 HRESULT hres;
1191 hres = alloc_array(ctx, NULL, &array);
1192 if(FAILED(hres))
1193 return hres;
1195 array->length = length;
1197 *ret = &array->dispex;
1198 return S_OK;