dxgiformat.h: Add missing DXGI_FORMAT enums.
[wine.git] / dlls / jscript / array.c
blobd17b6051f70662dfa3a6f55debcf83def8208977
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>
23 #include <assert.h>
25 #include "jscript.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 typedef struct {
32 jsdisp_t dispex;
34 DWORD length;
35 } ArrayInstance;
37 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
38 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
39 static const WCHAR joinW[] = {'j','o','i','n',0};
40 static const WCHAR popW[] = {'p','o','p',0};
41 static const WCHAR pushW[] = {'p','u','s','h',0};
42 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
43 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
44 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
45 static const WCHAR sortW[] = {'s','o','r','t',0};
46 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
47 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
48 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
49 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
51 static const WCHAR default_separatorW[] = {',',0};
53 static inline ArrayInstance *array_from_jsdisp(jsdisp_t *jsdisp)
55 return CONTAINING_RECORD(jsdisp, ArrayInstance, dispex);
58 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
60 return array_from_jsdisp(vdisp->u.jsdisp);
63 static inline ArrayInstance *array_this(vdisp_t *jsthis)
65 return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
68 unsigned array_get_length(jsdisp_t *array)
70 assert(is_class(array, JSCLASS_ARRAY));
71 return array_from_jsdisp(array)->length;
74 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsdisp_t **jsthis, DWORD *ret)
76 ArrayInstance *array;
77 jsval_t val;
78 HRESULT hres;
80 array = array_this(vdisp);
81 if(array) {
82 *jsthis = &array->dispex;
83 *ret = array->length;
84 return S_OK;
87 if(!is_jsdisp(vdisp))
88 return throw_type_error(ctx, JS_E_JSCRIPT_EXPECTED, NULL);
90 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &val);
91 if(FAILED(hres))
92 return hres;
94 hres = to_uint32(ctx, val, ret);
95 jsval_release(val);
96 if(FAILED(hres))
97 return hres;
99 *jsthis = vdisp->u.jsdisp;
100 return S_OK;
103 static HRESULT set_length(jsdisp_t *obj, DWORD length)
105 if(is_class(obj, JSCLASS_ARRAY)) {
106 array_from_jsdisp(obj)->length = length;
107 return S_OK;
110 return jsdisp_propput_name(obj, lengthW, jsval_number(length));
113 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
115 if(!idx) {
116 *ptr = '0';
117 return ptr;
120 while(idx) {
121 *ptr-- = '0' + (idx%10);
122 idx /= 10;
125 return ptr+1;
128 static HRESULT Array_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
130 TRACE("%p\n", jsthis);
132 *r = jsval_number(array_from_jsdisp(jsthis)->length);
133 return S_OK;
136 static HRESULT Array_set_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
138 ArrayInstance *This = array_from_jsdisp(jsthis);
139 DOUBLE len = -1;
140 DWORD i;
141 HRESULT hres;
143 TRACE("%p %d\n", This, This->length);
145 hres = to_number(ctx, value, &len);
146 if(FAILED(hres))
147 return hres;
149 len = floor(len);
150 if(len!=(DWORD)len)
151 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
153 for(i=len; i < This->length; i++) {
154 hres = jsdisp_delete_idx(&This->dispex, i);
155 if(FAILED(hres))
156 return hres;
159 This->length = len;
160 return S_OK;
163 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len)
165 jsval_t val;
166 DWORD i;
167 HRESULT hres;
169 for(i=0; i < obj->length; i++) {
170 hres = jsdisp_get_idx(&obj->dispex, i, &val);
171 if(hres == DISP_E_UNKNOWNNAME)
172 continue;
173 if(FAILED(hres))
174 return hres;
176 hres = jsdisp_propput_idx(array, *len+i, val);
177 jsval_release(val);
178 if(FAILED(hres))
179 return hres;
182 *len += obj->length;
183 return S_OK;
186 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len)
188 jsdisp_t *jsobj;
189 HRESULT hres;
191 jsobj = iface_to_jsdisp(obj);
192 if(jsobj) {
193 if(is_class(jsobj, JSCLASS_ARRAY)) {
194 hres = concat_array(array, array_from_jsdisp(jsobj), len);
195 jsdisp_release(jsobj);
196 return hres;
198 jsdisp_release(jsobj);
201 return jsdisp_propput_idx(array, (*len)++, jsval_disp(obj));
204 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
205 jsval_t *r)
207 jsdisp_t *ret;
208 DWORD len = 0;
209 HRESULT hres;
211 TRACE("\n");
213 hres = create_array(ctx, 0, &ret);
214 if(FAILED(hres))
215 return hres;
217 hres = concat_obj(ret, jsthis->u.disp, &len);
218 if(SUCCEEDED(hres)) {
219 DWORD i;
221 for(i=0; i < argc; i++) {
222 if(is_object_instance(argv[i]))
223 hres = concat_obj(ret, get_object(argv[i]), &len);
224 else
225 hres = jsdisp_propput_idx(ret, len++, argv[i]);
226 if(FAILED(hres))
227 break;
231 if(FAILED(hres))
232 return hres;
234 if(r)
235 *r = jsval_obj(ret);
236 else
237 jsdisp_release(ret);
238 return S_OK;
241 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep,
242 unsigned seplen, jsval_t *r)
244 jsstr_t **str_tab, *ret = NULL;
245 jsval_t val;
246 DWORD i;
247 HRESULT hres = E_FAIL;
249 if(!length) {
250 if(r)
251 *r = jsval_string(jsstr_empty());
252 return S_OK;
255 str_tab = heap_alloc_zero(length * sizeof(*str_tab));
256 if(!str_tab)
257 return E_OUTOFMEMORY;
259 for(i=0; i < length; i++) {
260 hres = jsdisp_get_idx(array, i, &val);
261 if(hres == DISP_E_UNKNOWNNAME) {
262 hres = S_OK;
263 continue;
264 } else if(FAILED(hres))
265 break;
267 if(!is_undefined(val) && !is_null(val)) {
268 hres = to_string(ctx, val, str_tab+i);
269 jsval_release(val);
270 if(FAILED(hres))
271 break;
275 if(SUCCEEDED(hres)) {
276 DWORD len = 0;
278 if(str_tab[0])
279 len = jsstr_length(str_tab[0]);
280 for(i=1; i < length; i++) {
281 len += seplen;
282 if(str_tab[i])
283 len += jsstr_length(str_tab[i]);
284 if(len > JSSTR_MAX_LENGTH) {
285 hres = E_OUTOFMEMORY;
286 break;
290 if(SUCCEEDED(hres)) {
291 WCHAR *ptr = NULL;
293 ret = jsstr_alloc_buf(len, &ptr);
294 if(ret) {
295 if(str_tab[0])
296 ptr += jsstr_flush(str_tab[0], ptr);
298 for(i=1; i < length; i++) {
299 if(seplen) {
300 memcpy(ptr, sep, seplen*sizeof(WCHAR));
301 ptr += seplen;
304 if(str_tab[i])
305 ptr += jsstr_flush(str_tab[i], ptr);
307 }else {
308 hres = E_OUTOFMEMORY;
313 for(i=0; i < length; i++) {
314 if(str_tab[i])
315 jsstr_release(str_tab[i]);
317 heap_free(str_tab);
318 if(FAILED(hres))
319 return hres;
321 TRACE("= %s\n", debugstr_jsstr(ret));
323 if(r)
324 *r = jsval_string(ret);
325 else
326 jsstr_release(ret);
327 return S_OK;
330 /* ECMA-262 3rd Edition 15.4.4.5 */
331 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
332 jsval_t *r)
334 jsdisp_t *jsthis;
335 DWORD length;
336 HRESULT hres;
338 TRACE("\n");
340 hres = get_length(ctx, vthis, &jsthis, &length);
341 if(FAILED(hres))
342 return hres;
344 if(argc) {
345 const WCHAR *sep;
346 jsstr_t *sep_str;
348 hres = to_flat_string(ctx, argv[0], &sep_str, &sep);
349 if(FAILED(hres))
350 return hres;
352 hres = array_join(ctx, jsthis, length, sep, jsstr_length(sep_str), r);
354 jsstr_release(sep_str);
355 }else {
356 hres = array_join(ctx, jsthis, length, default_separatorW, strlenW(default_separatorW), r);
359 return hres;
362 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
363 jsval_t *r)
365 jsdisp_t *jsthis;
366 jsval_t val;
367 DWORD length;
368 HRESULT hres;
370 TRACE("\n");
372 hres = get_length(ctx, vthis, &jsthis, &length);
373 if(FAILED(hres))
374 return hres;
376 if(!length) {
377 hres = set_length(jsthis, 0);
378 if(FAILED(hres))
379 return hres;
381 if(r)
382 *r = jsval_undefined();
383 return S_OK;
386 length--;
387 hres = jsdisp_get_idx(jsthis, length, &val);
388 if(SUCCEEDED(hres))
389 hres = jsdisp_delete_idx(jsthis, length);
390 else if(hres == DISP_E_UNKNOWNNAME)
391 val = jsval_undefined();
392 else
393 return hres;
395 if(SUCCEEDED(hres))
396 hres = set_length(jsthis, length);
398 if(FAILED(hres)) {
399 jsval_release(val);
400 return hres;
403 if(r)
404 *r = val;
405 else
406 jsval_release(val);
407 return hres;
410 /* ECMA-262 3rd Edition 15.4.4.7 */
411 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
412 jsval_t *r)
414 jsdisp_t *jsthis;
415 DWORD length = 0;
416 unsigned i;
417 HRESULT hres;
419 TRACE("\n");
421 hres = get_length(ctx, vthis, &jsthis, &length);
422 if(FAILED(hres))
423 return hres;
425 for(i=0; i < argc; i++) {
426 hres = jsdisp_propput_idx(jsthis, length+i, argv[i]);
427 if(FAILED(hres))
428 return hres;
431 hres = set_length(jsthis, length+argc);
432 if(FAILED(hres))
433 return hres;
435 if(r)
436 *r = jsval_number(length+argc);
437 return S_OK;
440 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
441 jsval_t *r)
443 jsdisp_t *jsthis;
444 DWORD length, k, l;
445 jsval_t v1, v2;
446 HRESULT hres1, hres2;
448 TRACE("\n");
450 hres1 = get_length(ctx, vthis, &jsthis, &length);
451 if(FAILED(hres1))
452 return hres1;
454 for(k=0; k<length/2; k++) {
455 l = length-k-1;
457 hres1 = jsdisp_get_idx(jsthis, k, &v1);
458 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
459 return hres1;
461 hres2 = jsdisp_get_idx(jsthis, l, &v2);
462 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
463 jsval_release(v1);
464 return hres2;
467 if(hres1 == DISP_E_UNKNOWNNAME)
468 hres1 = jsdisp_delete_idx(jsthis, l);
469 else
470 hres1 = jsdisp_propput_idx(jsthis, l, v1);
472 if(FAILED(hres1)) {
473 jsval_release(v1);
474 jsval_release(v2);
475 return hres1;
478 if(hres2 == DISP_E_UNKNOWNNAME)
479 hres2 = jsdisp_delete_idx(jsthis, k);
480 else
481 hres2 = jsdisp_propput_idx(jsthis, k, v2);
483 if(FAILED(hres2)) {
484 jsval_release(v2);
485 return hres2;
489 if(r)
490 *r = jsval_obj(jsdisp_addref(jsthis));
491 return S_OK;
494 /* ECMA-262 3rd Edition 15.4.4.9 */
495 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
496 jsval_t *r)
498 jsdisp_t *jsthis;
499 DWORD length = 0, i;
500 jsval_t v, ret;
501 HRESULT hres;
503 TRACE("\n");
505 hres = get_length(ctx, vthis, &jsthis, &length);
506 if(FAILED(hres))
507 return hres;
509 if(!length) {
510 hres = set_length(jsthis, 0);
511 if(FAILED(hres))
512 return hres;
514 if(r)
515 *r = jsval_undefined();
516 return S_OK;
519 hres = jsdisp_get_idx(jsthis, 0, &ret);
520 if(hres == DISP_E_UNKNOWNNAME) {
521 ret = jsval_undefined();
522 hres = S_OK;
525 for(i=1; SUCCEEDED(hres) && i<length; i++) {
526 hres = jsdisp_get_idx(jsthis, i, &v);
527 if(hres == DISP_E_UNKNOWNNAME)
528 hres = jsdisp_delete_idx(jsthis, i-1);
529 else if(SUCCEEDED(hres))
530 hres = jsdisp_propput_idx(jsthis, i-1, v);
533 if(SUCCEEDED(hres)) {
534 hres = jsdisp_delete_idx(jsthis, length-1);
535 if(SUCCEEDED(hres))
536 hres = set_length(jsthis, length-1);
539 if(FAILED(hres))
540 return hres;
542 if(r)
543 *r = ret;
544 else
545 jsval_release(ret);
546 return hres;
549 /* ECMA-262 3rd Edition 15.4.4.10 */
550 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
552 jsdisp_t *arr, *jsthis;
553 DOUBLE range;
554 DWORD length, start, end, idx;
555 HRESULT hres;
557 TRACE("\n");
559 hres = get_length(ctx, vthis, &jsthis, &length);
560 if(FAILED(hres))
561 return hres;
563 if(argc) {
564 hres = to_number(ctx, argv[0], &range);
565 if(FAILED(hres))
566 return hres;
568 range = floor(range);
569 if(-range>length || isnan(range)) start = 0;
570 else if(range < 0) start = range+length;
571 else if(range <= length) start = range;
572 else start = length;
574 else start = 0;
576 if(argc > 1) {
577 hres = to_number(ctx, argv[1], &range);
578 if(FAILED(hres))
579 return hres;
581 range = floor(range);
582 if(-range>length) end = 0;
583 else if(range < 0) end = range+length;
584 else if(range <= length) end = range;
585 else end = length;
587 else end = length;
589 hres = create_array(ctx, (end>start)?end-start:0, &arr);
590 if(FAILED(hres))
591 return hres;
593 for(idx=start; idx<end; idx++) {
594 jsval_t v;
596 hres = jsdisp_get_idx(jsthis, idx, &v);
597 if(hres == DISP_E_UNKNOWNNAME)
598 continue;
600 if(SUCCEEDED(hres)) {
601 hres = jsdisp_propput_idx(arr, idx-start, v);
602 jsval_release(v);
605 if(FAILED(hres)) {
606 jsdisp_release(arr);
607 return hres;
611 if(r)
612 *r = jsval_obj(arr);
613 else
614 jsdisp_release(arr);
616 return S_OK;
619 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, INT *cmp)
621 HRESULT hres;
623 if(cmp_func) {
624 jsval_t args[2] = {v1, v2};
625 jsval_t res;
626 double n;
628 hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res);
629 if(FAILED(hres))
630 return hres;
632 hres = to_number(ctx, res, &n);
633 jsval_release(res);
634 if(FAILED(hres))
635 return hres;
637 if(n == 0)
638 *cmp = 0;
639 *cmp = n > 0.0 ? 1 : -1;
640 }else if(is_undefined(v1)) {
641 *cmp = is_undefined(v2) ? 0 : 1;
642 }else if(is_undefined(v2)) {
643 *cmp = -1;
644 }else if(is_number(v1) && is_number(v2)) {
645 double d = get_number(v1)-get_number(v2);
646 if(d > 0.0)
647 *cmp = 1;
648 else
649 *cmp = d < -0.0 ? -1 : 0;
650 }else {
651 jsstr_t *x, *y;
653 hres = to_string(ctx, v1, &x);
654 if(FAILED(hres))
655 return hres;
657 hres = to_string(ctx, v2, &y);
658 if(SUCCEEDED(hres)) {
659 *cmp = jsstr_cmp(x, y);
660 jsstr_release(y);
662 jsstr_release(x);
663 if(FAILED(hres))
664 return hres;
667 return S_OK;
670 /* ECMA-262 3rd Edition 15.4.4.11 */
671 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
672 jsval_t *r)
674 jsdisp_t *jsthis, *cmp_func = NULL;
675 jsval_t *vtab, **sorttab = NULL;
676 DWORD length;
677 DWORD i;
678 HRESULT hres = S_OK;
680 TRACE("\n");
682 hres = get_length(ctx, vthis, &jsthis, &length);
683 if(FAILED(hres))
684 return hres;
686 if(argc > 1) {
687 WARN("invalid arg_cnt %d\n", argc);
688 return E_FAIL;
691 if(argc == 1) {
692 if(!is_object_instance(argv[0])) {
693 WARN("arg is not dispatch\n");
694 return E_FAIL;
697 cmp_func = iface_to_jsdisp(get_object(argv[0]));
698 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
699 WARN("cmp_func is not a function\n");
700 if(cmp_func)
701 jsdisp_release(cmp_func);
702 return E_FAIL;
706 if(!length) {
707 if(cmp_func)
708 jsdisp_release(cmp_func);
709 if(r)
710 *r = jsval_obj(jsdisp_addref(jsthis));
711 return S_OK;
714 vtab = heap_alloc_zero(length * sizeof(*vtab));
715 if(vtab) {
716 for(i=0; i<length; i++) {
717 hres = jsdisp_get_idx(jsthis, i, vtab+i);
718 if(hres == DISP_E_UNKNOWNNAME) {
719 vtab[i] = jsval_undefined();
720 hres = S_OK;
721 } else if(FAILED(hres)) {
722 WARN("Could not get elem %d: %08x\n", i, hres);
723 break;
726 }else {
727 hres = E_OUTOFMEMORY;
730 if(SUCCEEDED(hres)) {
731 sorttab = heap_alloc(length*2*sizeof(*sorttab));
732 if(!sorttab)
733 hres = E_OUTOFMEMORY;
736 /* merge-sort */
737 if(SUCCEEDED(hres)) {
738 jsval_t *tmpv, **tmpbuf;
739 INT cmp;
741 tmpbuf = sorttab + length;
742 for(i=0; i < length; i++)
743 sorttab[i] = vtab+i;
745 for(i=0; i < length/2; i++) {
746 hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], &cmp);
747 if(FAILED(hres))
748 break;
750 if(cmp < 0) {
751 tmpv = sorttab[2*i];
752 sorttab[2*i] = sorttab[2*i+1];
753 sorttab[2*i+1] = tmpv;
757 if(SUCCEEDED(hres)) {
758 DWORD k, a, b, bend;
760 for(k=2; k < length; k *= 2) {
761 for(i=0; i+k < length; i += 2*k) {
762 a = b = 0;
763 if(i+2*k <= length)
764 bend = k;
765 else
766 bend = length - (i+k);
768 memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
770 while(a < k && b < bend) {
771 hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], &cmp);
772 if(FAILED(hres))
773 break;
775 if(cmp < 0) {
776 sorttab[i+a+b] = tmpbuf[a];
777 a++;
778 }else {
779 sorttab[i+a+b] = sorttab[i+k+b];
780 b++;
784 if(FAILED(hres))
785 break;
787 if(a < k)
788 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
791 if(FAILED(hres))
792 break;
796 for(i=0; SUCCEEDED(hres) && i < length; i++)
797 hres = jsdisp_propput_idx(jsthis, i, *sorttab[i]);
800 if(vtab) {
801 for(i=0; i < length; i++)
802 jsval_release(vtab[i]);
803 heap_free(vtab);
805 heap_free(sorttab);
806 if(cmp_func)
807 jsdisp_release(cmp_func);
809 if(FAILED(hres))
810 return hres;
812 if(r)
813 *r = jsval_obj(jsdisp_addref(jsthis));
814 return S_OK;
817 /* ECMA-262 3rd Edition 15.4.4.12 */
818 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
819 jsval_t *r)
821 DWORD length, start=0, delete_cnt=0, i, add_args = 0;
822 jsdisp_t *ret_array = NULL, *jsthis;
823 jsval_t val;
824 double d;
825 int n;
826 HRESULT hres = S_OK;
828 TRACE("\n");
830 hres = get_length(ctx, vthis, &jsthis, &length);
831 if(FAILED(hres))
832 return hres;
834 if(argc) {
835 hres = to_integer(ctx, argv[0], &d);
836 if(FAILED(hres))
837 return hres;
839 if(is_int32(d)) {
840 if((n = d) >= 0)
841 start = min(n, length);
842 else
843 start = -n > length ? 0 : length + n;
844 }else {
845 start = d < 0.0 ? 0 : length;
849 if(argc >= 2) {
850 hres = to_integer(ctx, argv[1], &d);
851 if(FAILED(hres))
852 return hres;
854 if(is_int32(d)) {
855 if((n = d) > 0)
856 delete_cnt = min(n, length-start);
857 }else if(d > 0.0) {
858 delete_cnt = length-start;
861 add_args = argc-2;
864 if(r) {
865 hres = create_array(ctx, 0, &ret_array);
866 if(FAILED(hres))
867 return hres;
869 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
870 hres = jsdisp_get_idx(jsthis, start+i, &val);
871 if(hres == DISP_E_UNKNOWNNAME) {
872 hres = S_OK;
873 }else if(SUCCEEDED(hres)) {
874 hres = jsdisp_propput_idx(ret_array, i, val);
875 jsval_release(val);
879 if(SUCCEEDED(hres))
880 hres = jsdisp_propput_name(ret_array, lengthW, jsval_number(delete_cnt));
883 if(add_args < delete_cnt) {
884 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
885 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val);
886 if(hres == DISP_E_UNKNOWNNAME) {
887 hres = jsdisp_delete_idx(jsthis, i+add_args);
888 }else if(SUCCEEDED(hres)) {
889 hres = jsdisp_propput_idx(jsthis, i+add_args, val);
890 jsval_release(val);
894 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
895 hres = jsdisp_delete_idx(jsthis, i-1);
896 }else if(add_args > delete_cnt) {
897 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
898 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val);
899 if(hres == DISP_E_UNKNOWNNAME) {
900 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
901 }else if(SUCCEEDED(hres)) {
902 hres = jsdisp_propput_idx(jsthis, i+add_args-1, val);
903 jsval_release(val);
908 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
909 hres = jsdisp_propput_idx(jsthis, start+i, argv[i+2]);
911 if(SUCCEEDED(hres))
912 hres = jsdisp_propput_name(jsthis, lengthW, jsval_number(length-delete_cnt+add_args));
914 if(FAILED(hres)) {
915 if(ret_array)
916 jsdisp_release(ret_array);
917 return hres;
920 if(r)
921 *r = jsval_obj(ret_array);
922 return S_OK;
925 /* ECMA-262 3rd Edition 15.4.4.2 */
926 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
927 jsval_t *r)
929 ArrayInstance *array;
931 TRACE("\n");
933 array = array_this(jsthis);
934 if(!array)
935 return throw_type_error(ctx, JS_E_ARRAY_EXPECTED, NULL);
937 return array_join(ctx, &array->dispex, array->length, default_separatorW,
938 strlenW(default_separatorW), r);
941 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
942 jsval_t *r)
944 FIXME("\n");
945 return E_NOTIMPL;
948 /* ECMA-262 3rd Edition 15.4.4.13 */
949 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
950 jsval_t *r)
952 jsdisp_t *jsthis;
953 WCHAR buf[14], *buf_end, *str;
954 DWORD i, length;
955 jsval_t val;
956 DISPID id;
957 HRESULT hres;
959 TRACE("\n");
961 hres = get_length(ctx, vthis, &jsthis, &length);
962 if(FAILED(hres))
963 return hres;
965 if(argc) {
966 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
967 *buf_end-- = 0;
968 i = length;
970 while(i--) {
971 str = idx_to_str(i, buf_end);
973 hres = jsdisp_get_id(jsthis, str, 0, &id);
974 if(SUCCEEDED(hres)) {
975 hres = jsdisp_propget(jsthis, id, &val);
976 if(FAILED(hres))
977 return hres;
979 hres = jsdisp_propput_idx(jsthis, i+argc, val);
980 jsval_release(val);
981 }else if(hres == DISP_E_UNKNOWNNAME) {
982 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
986 if(FAILED(hres))
987 return hres;
990 for(i=0; i<argc; i++) {
991 hres = jsdisp_propput_idx(jsthis, i, argv[i]);
992 if(FAILED(hres))
993 return hres;
996 if(argc) {
997 length += argc;
998 hres = set_length(jsthis, length);
999 if(FAILED(hres))
1000 return hres;
1003 if(r)
1004 *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
1005 return S_OK;
1008 static HRESULT Array_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
1010 ArrayInstance *array = array_from_jsdisp(jsthis);
1012 TRACE("\n");
1014 return array_join(ctx, &array->dispex, array->length, default_separatorW,
1015 strlenW(default_separatorW), r);
1018 static void Array_destructor(jsdisp_t *dispex)
1020 heap_free(dispex);
1023 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1025 ArrayInstance *array = array_from_jsdisp(dispex);
1026 const WCHAR *ptr = name;
1027 DWORD id = 0;
1029 if(!isdigitW(*ptr))
1030 return;
1032 while(*ptr && isdigitW(*ptr)) {
1033 id = id*10 + (*ptr-'0');
1034 ptr++;
1037 if(*ptr)
1038 return;
1040 if(id >= array->length)
1041 array->length = id+1;
1044 static const builtin_prop_t Array_props[] = {
1045 {concatW, Array_concat, PROPF_METHOD|1},
1046 {joinW, Array_join, PROPF_METHOD|1},
1047 {lengthW, NULL,0, Array_get_length, Array_set_length},
1048 {popW, Array_pop, PROPF_METHOD},
1049 {pushW, Array_push, PROPF_METHOD|1},
1050 {reverseW, Array_reverse, PROPF_METHOD},
1051 {shiftW, Array_shift, PROPF_METHOD},
1052 {sliceW, Array_slice, PROPF_METHOD|2},
1053 {sortW, Array_sort, PROPF_METHOD|1},
1054 {spliceW, Array_splice, PROPF_METHOD|2},
1055 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1056 {toStringW, Array_toString, PROPF_METHOD},
1057 {unshiftW, Array_unshift, PROPF_METHOD|1},
1060 static const builtin_info_t Array_info = {
1061 JSCLASS_ARRAY,
1062 {NULL, NULL,0, Array_get_value},
1063 sizeof(Array_props)/sizeof(*Array_props),
1064 Array_props,
1065 Array_destructor,
1066 Array_on_put
1069 static const builtin_prop_t ArrayInst_props[] = {
1070 {lengthW, NULL,0, Array_get_length, Array_set_length}
1073 static const builtin_info_t ArrayInst_info = {
1074 JSCLASS_ARRAY,
1075 {NULL, NULL,0, Array_get_value},
1076 sizeof(ArrayInst_props)/sizeof(*ArrayInst_props),
1077 ArrayInst_props,
1078 Array_destructor,
1079 Array_on_put
1082 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1083 jsval_t *r)
1085 jsdisp_t *obj;
1086 DWORD i;
1087 HRESULT hres;
1089 TRACE("\n");
1091 switch(flags) {
1092 case DISPATCH_METHOD:
1093 case DISPATCH_CONSTRUCT: {
1094 if(argc == 1 && is_number(argv[0])) {
1095 double n = get_number(argv[0]);
1097 if(n < 0 || !is_int32(n))
1098 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
1100 hres = create_array(ctx, n, &obj);
1101 if(FAILED(hres))
1102 return hres;
1104 *r = jsval_obj(obj);
1105 return S_OK;
1108 hres = create_array(ctx, argc, &obj);
1109 if(FAILED(hres))
1110 return hres;
1112 for(i=0; i < argc; i++) {
1113 hres = jsdisp_propput_idx(obj, i, argv[i]);
1114 if(FAILED(hres))
1115 break;
1117 if(FAILED(hres)) {
1118 jsdisp_release(obj);
1119 return hres;
1122 *r = jsval_obj(obj);
1123 break;
1125 default:
1126 FIXME("unimplemented flags: %x\n", flags);
1127 return E_NOTIMPL;
1130 return S_OK;
1133 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1135 ArrayInstance *array;
1136 HRESULT hres;
1138 array = heap_alloc_zero(sizeof(ArrayInstance));
1139 if(!array)
1140 return E_OUTOFMEMORY;
1142 if(object_prototype)
1143 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1144 else
1145 hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1147 if(FAILED(hres)) {
1148 heap_free(array);
1149 return hres;
1152 *ret = array;
1153 return S_OK;
1156 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1158 ArrayInstance *array;
1159 HRESULT hres;
1161 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1163 hres = alloc_array(ctx, object_prototype, &array);
1164 if(FAILED(hres))
1165 return hres;
1167 hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1169 jsdisp_release(&array->dispex);
1170 return hres;
1173 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1175 ArrayInstance *array;
1176 HRESULT hres;
1178 hres = alloc_array(ctx, NULL, &array);
1179 if(FAILED(hres))
1180 return hres;
1182 array->length = length;
1184 *ret = &array->dispex;
1185 return S_OK;