d3d10_1: Validate parameters in D3D10CreateDevice1().
[wine.git] / dlls / jscript / array.c
blobfb4982fb05f9bae6fd81ad86bff1fef1ef9f3e6d
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 ((ArrayInstance*)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((IUnknown*)obj);
192 if(jsobj) {
193 if(is_class(jsobj, JSCLASS_ARRAY)) {
194 hres = concat_array(array, (ArrayInstance*)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, jsval_t *r)
243 jsstr_t **str_tab, *ret;
244 jsval_t val;
245 DWORD i;
246 HRESULT hres = E_FAIL;
248 if(!length) {
249 if(r)
250 *r = jsval_string(jsstr_empty());
251 return S_OK;
254 str_tab = heap_alloc_zero(length * sizeof(*str_tab));
255 if(!str_tab)
256 return E_OUTOFMEMORY;
258 for(i=0; i < length; i++) {
259 hres = jsdisp_get_idx(array, i, &val);
260 if(hres == DISP_E_UNKNOWNNAME) {
261 hres = S_OK;
262 continue;
263 } else if(FAILED(hres))
264 break;
266 if(!is_undefined(val) && !is_null(val)) {
267 hres = to_string(ctx, val, str_tab+i);
268 jsval_release(val);
269 if(FAILED(hres))
270 break;
274 if(SUCCEEDED(hres)) {
275 DWORD seplen = 0, len = 0;
277 seplen = strlenW(sep);
279 if(str_tab[0])
280 len = jsstr_length(str_tab[0]);
281 for(i=1; i < length; i++) {
282 len += seplen;
283 if(str_tab[i])
284 len += jsstr_length(str_tab[i]);
285 if(len > JSSTR_MAX_LENGTH) {
286 hres = E_OUTOFMEMORY;
287 break;
291 if(SUCCEEDED(hres)) {
292 WCHAR *ptr = NULL;
294 ptr = jsstr_alloc_buf(len, &ret);
295 if(ptr) {
296 if(str_tab[0])
297 ptr += jsstr_flush(str_tab[0], ptr);
299 for(i=1; i < length; i++) {
300 if(seplen) {
301 memcpy(ptr, sep, seplen*sizeof(WCHAR));
302 ptr += seplen;
305 if(str_tab[i])
306 ptr += jsstr_flush(str_tab[i], ptr);
308 }else {
309 hres = E_OUTOFMEMORY;
314 for(i=0; i < length; i++) {
315 if(str_tab[i])
316 jsstr_release(str_tab[i]);
318 heap_free(str_tab);
319 if(FAILED(hres))
320 return hres;
322 TRACE("= %s\n", debugstr_jsstr(ret));
324 if(r)
325 *r = jsval_string(ret);
326 else
327 jsstr_release(ret);
328 return S_OK;
331 /* ECMA-262 3rd Edition 15.4.4.5 */
332 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
333 jsval_t *r)
335 jsdisp_t *jsthis;
336 DWORD length;
337 HRESULT hres;
339 TRACE("\n");
341 hres = get_length(ctx, vthis, &jsthis, &length);
342 if(FAILED(hres))
343 return hres;
345 if(argc) {
346 const WCHAR *sep;
347 jsstr_t *sep_str;
349 hres = to_flat_string(ctx, argv[0], &sep_str, &sep);
350 if(FAILED(hres))
351 return hres;
353 hres = array_join(ctx, jsthis, length, sep, r);
355 jsstr_release(sep_str);
356 }else {
357 hres = array_join(ctx, jsthis, length, default_separatorW, r);
360 return hres;
363 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
364 jsval_t *r)
366 jsdisp_t *jsthis;
367 jsval_t val;
368 DWORD length;
369 HRESULT hres;
371 TRACE("\n");
373 hres = get_length(ctx, vthis, &jsthis, &length);
374 if(FAILED(hres))
375 return hres;
377 if(!length) {
378 hres = set_length(jsthis, 0);
379 if(FAILED(hres))
380 return hres;
382 if(r)
383 *r = jsval_undefined();
384 return S_OK;
387 length--;
388 hres = jsdisp_get_idx(jsthis, length, &val);
389 if(SUCCEEDED(hres))
390 hres = jsdisp_delete_idx(jsthis, length);
391 else if(hres == DISP_E_UNKNOWNNAME)
392 val = jsval_undefined();
393 else
394 return hres;
396 if(SUCCEEDED(hres))
397 hres = set_length(jsthis, length);
399 if(FAILED(hres)) {
400 jsval_release(val);
401 return hres;
404 if(r)
405 *r = val;
406 else
407 jsval_release(val);
408 return hres;
411 /* ECMA-262 3rd Edition 15.4.4.7 */
412 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
413 jsval_t *r)
415 jsdisp_t *jsthis;
416 DWORD length = 0;
417 unsigned i;
418 HRESULT hres;
420 TRACE("\n");
422 hres = get_length(ctx, vthis, &jsthis, &length);
423 if(FAILED(hres))
424 return hres;
426 for(i=0; i < argc; i++) {
427 hres = jsdisp_propput_idx(jsthis, length+i, argv[i]);
428 if(FAILED(hres))
429 return hres;
432 hres = set_length(jsthis, length+argc);
433 if(FAILED(hres))
434 return hres;
436 if(r)
437 *r = jsval_number(length+argc);
438 return S_OK;
441 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
442 jsval_t *r)
444 jsdisp_t *jsthis;
445 DWORD length, k, l;
446 jsval_t v1, v2;
447 HRESULT hres1, hres2;
449 TRACE("\n");
451 hres1 = get_length(ctx, vthis, &jsthis, &length);
452 if(FAILED(hres1))
453 return hres1;
455 for(k=0; k<length/2; k++) {
456 l = length-k-1;
458 hres1 = jsdisp_get_idx(jsthis, k, &v1);
459 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
460 return hres1;
462 hres2 = jsdisp_get_idx(jsthis, l, &v2);
463 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
464 jsval_release(v1);
465 return hres2;
468 if(hres1 == DISP_E_UNKNOWNNAME)
469 hres1 = jsdisp_delete_idx(jsthis, l);
470 else
471 hres1 = jsdisp_propput_idx(jsthis, l, v1);
473 if(FAILED(hres1)) {
474 jsval_release(v1);
475 jsval_release(v2);
476 return hres1;
479 if(hres2 == DISP_E_UNKNOWNNAME)
480 hres2 = jsdisp_delete_idx(jsthis, k);
481 else
482 hres2 = jsdisp_propput_idx(jsthis, k, v2);
484 if(FAILED(hres2)) {
485 jsval_release(v2);
486 return hres2;
490 if(r)
491 *r = jsval_obj(jsdisp_addref(jsthis));
492 return S_OK;
495 /* ECMA-262 3rd Edition 15.4.4.9 */
496 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
497 jsval_t *r)
499 jsdisp_t *jsthis;
500 DWORD length = 0, i;
501 jsval_t v, ret;
502 HRESULT hres;
504 TRACE("\n");
506 hres = get_length(ctx, vthis, &jsthis, &length);
507 if(FAILED(hres))
508 return hres;
510 if(!length) {
511 hres = set_length(jsthis, 0);
512 if(FAILED(hres))
513 return hres;
515 if(r)
516 *r = jsval_undefined();
517 return S_OK;
520 hres = jsdisp_get_idx(jsthis, 0, &ret);
521 if(hres == DISP_E_UNKNOWNNAME) {
522 ret = jsval_undefined();
523 hres = S_OK;
526 for(i=1; SUCCEEDED(hres) && i<length; i++) {
527 hres = jsdisp_get_idx(jsthis, i, &v);
528 if(hres == DISP_E_UNKNOWNNAME)
529 hres = jsdisp_delete_idx(jsthis, i-1);
530 else if(SUCCEEDED(hres))
531 hres = jsdisp_propput_idx(jsthis, i-1, v);
534 if(SUCCEEDED(hres)) {
535 hres = jsdisp_delete_idx(jsthis, length-1);
536 if(SUCCEEDED(hres))
537 hres = set_length(jsthis, length-1);
540 if(FAILED(hres))
541 return hres;
543 if(r)
544 *r = ret;
545 else
546 jsval_release(ret);
547 return hres;
550 /* ECMA-262 3rd Edition 15.4.4.10 */
551 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
553 jsdisp_t *arr, *jsthis;
554 DOUBLE range;
555 DWORD length, start, end, idx;
556 HRESULT hres;
558 TRACE("\n");
560 hres = get_length(ctx, vthis, &jsthis, &length);
561 if(FAILED(hres))
562 return hres;
564 if(argc) {
565 hres = to_number(ctx, argv[0], &range);
566 if(FAILED(hres))
567 return hres;
569 range = floor(range);
570 if(-range>length || isnan(range)) start = 0;
571 else if(range < 0) start = range+length;
572 else if(range <= length) start = range;
573 else start = length;
575 else start = 0;
577 if(argc > 1) {
578 hres = to_number(ctx, argv[1], &range);
579 if(FAILED(hres))
580 return hres;
582 range = floor(range);
583 if(-range>length) end = 0;
584 else if(range < 0) end = range+length;
585 else if(range <= length) end = range;
586 else end = length;
588 else end = length;
590 hres = create_array(ctx, (end>start)?end-start:0, &arr);
591 if(FAILED(hres))
592 return hres;
594 for(idx=start; idx<end; idx++) {
595 jsval_t v;
597 hres = jsdisp_get_idx(jsthis, idx, &v);
598 if(hres == DISP_E_UNKNOWNNAME)
599 continue;
601 if(SUCCEEDED(hres)) {
602 hres = jsdisp_propput_idx(arr, idx-start, v);
603 jsval_release(v);
606 if(FAILED(hres)) {
607 jsdisp_release(arr);
608 return hres;
612 if(r)
613 *r = jsval_obj(arr);
614 else
615 jsdisp_release(arr);
617 return S_OK;
620 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, INT *cmp)
622 HRESULT hres;
624 if(cmp_func) {
625 jsval_t args[2] = {v1, v2};
626 jsval_t res;
627 double n;
629 hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res);
630 if(FAILED(hres))
631 return hres;
633 hres = to_number(ctx, res, &n);
634 jsval_release(res);
635 if(FAILED(hres))
636 return hres;
638 if(n == 0)
639 *cmp = 0;
640 *cmp = n > 0.0 ? 1 : -1;
641 }else if(is_undefined(v1)) {
642 *cmp = is_undefined(v2) ? 0 : 1;
643 }else if(is_undefined(v2)) {
644 *cmp = -1;
645 }else if(is_number(v1) && is_number(v2)) {
646 double d = get_number(v1)-get_number(v2);
647 if(d > 0.0)
648 *cmp = 1;
649 else
650 *cmp = d < -0.0 ? -1 : 0;
651 }else {
652 jsstr_t *x, *y;
654 hres = to_string(ctx, v1, &x);
655 if(FAILED(hres))
656 return hres;
658 hres = to_string(ctx, v2, &y);
659 if(SUCCEEDED(hres)) {
660 *cmp = jsstr_cmp(x, y);
661 jsstr_release(y);
663 jsstr_release(x);
664 if(FAILED(hres))
665 return hres;
668 return S_OK;
671 /* ECMA-262 3rd Edition 15.4.4.11 */
672 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
673 jsval_t *r)
675 jsdisp_t *jsthis, *cmp_func = NULL;
676 jsval_t *vtab, **sorttab = NULL;
677 DWORD length;
678 DWORD i;
679 HRESULT hres = S_OK;
681 TRACE("\n");
683 hres = get_length(ctx, vthis, &jsthis, &length);
684 if(FAILED(hres))
685 return hres;
687 if(argc > 1) {
688 WARN("invalid arg_cnt %d\n", argc);
689 return E_FAIL;
692 if(argc == 1) {
693 if(!is_object_instance(argv[0])) {
694 WARN("arg is not dispatch\n");
695 return E_FAIL;
698 cmp_func = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
699 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
700 WARN("cmp_func is not a function\n");
701 if(cmp_func)
702 jsdisp_release(cmp_func);
703 return E_FAIL;
707 if(!length) {
708 if(cmp_func)
709 jsdisp_release(cmp_func);
710 if(r)
711 *r = jsval_obj(jsdisp_addref(jsthis));
712 return S_OK;
715 vtab = heap_alloc_zero(length * sizeof(*vtab));
716 if(vtab) {
717 for(i=0; i<length; i++) {
718 hres = jsdisp_get_idx(jsthis, i, vtab+i);
719 if(hres == DISP_E_UNKNOWNNAME) {
720 vtab[i] = jsval_undefined();
721 hres = S_OK;
722 } else if(FAILED(hres)) {
723 WARN("Could not get elem %d: %08x\n", i, hres);
724 break;
727 }else {
728 hres = E_OUTOFMEMORY;
731 if(SUCCEEDED(hres)) {
732 sorttab = heap_alloc(length*2*sizeof(*sorttab));
733 if(!sorttab)
734 hres = E_OUTOFMEMORY;
737 /* merge-sort */
738 if(SUCCEEDED(hres)) {
739 jsval_t *tmpv, **tmpbuf;
740 INT cmp;
742 tmpbuf = sorttab + length;
743 for(i=0; i < length; i++)
744 sorttab[i] = vtab+i;
746 for(i=0; i < length/2; i++) {
747 hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], &cmp);
748 if(FAILED(hres))
749 break;
751 if(cmp < 0) {
752 tmpv = sorttab[2*i];
753 sorttab[2*i] = sorttab[2*i+1];
754 sorttab[2*i+1] = tmpv;
758 if(SUCCEEDED(hres)) {
759 DWORD k, a, b, bend;
761 for(k=2; k < length; k *= 2) {
762 for(i=0; i+k < length; i += 2*k) {
763 a = b = 0;
764 if(i+2*k <= length)
765 bend = k;
766 else
767 bend = length - (i+k);
769 memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
771 while(a < k && b < bend) {
772 hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], &cmp);
773 if(FAILED(hres))
774 break;
776 if(cmp < 0) {
777 sorttab[i+a+b] = tmpbuf[a];
778 a++;
779 }else {
780 sorttab[i+a+b] = sorttab[i+k+b];
781 b++;
785 if(FAILED(hres))
786 break;
788 if(a < k)
789 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
792 if(FAILED(hres))
793 break;
797 for(i=0; SUCCEEDED(hres) && i < length; i++)
798 hres = jsdisp_propput_idx(jsthis, i, *sorttab[i]);
801 if(vtab) {
802 for(i=0; i < length; i++)
803 jsval_release(vtab[i]);
804 heap_free(vtab);
806 heap_free(sorttab);
807 if(cmp_func)
808 jsdisp_release(cmp_func);
810 if(FAILED(hres))
811 return hres;
813 if(r)
814 *r = jsval_obj(jsdisp_addref(jsthis));
815 return S_OK;
818 /* ECMA-262 3rd Edition 15.4.4.12 */
819 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
820 jsval_t *r)
822 DWORD length, start=0, delete_cnt=0, i, add_args = 0;
823 jsdisp_t *ret_array = NULL, *jsthis;
824 jsval_t val;
825 double d;
826 int n;
827 HRESULT hres = S_OK;
829 TRACE("\n");
831 hres = get_length(ctx, vthis, &jsthis, &length);
832 if(FAILED(hres))
833 return hres;
835 if(argc) {
836 hres = to_integer(ctx, argv[0], &d);
837 if(FAILED(hres))
838 return hres;
840 if(is_int32(d)) {
841 if((n = d) >= 0)
842 start = min(n, length);
843 else
844 start = -n > length ? 0 : length + n;
845 }else {
846 start = d < 0.0 ? 0 : length;
850 if(argc >= 2) {
851 hres = to_integer(ctx, argv[1], &d);
852 if(FAILED(hres))
853 return hres;
855 if(is_int32(d)) {
856 if((n = d) > 0)
857 delete_cnt = min(n, length-start);
858 }else if(d > 0.0) {
859 delete_cnt = length-start;
862 add_args = argc-2;
865 if(r) {
866 hres = create_array(ctx, 0, &ret_array);
867 if(FAILED(hres))
868 return hres;
870 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
871 hres = jsdisp_get_idx(jsthis, start+i, &val);
872 if(hres == DISP_E_UNKNOWNNAME) {
873 hres = S_OK;
874 }else if(SUCCEEDED(hres)) {
875 hres = jsdisp_propput_idx(ret_array, i, val);
876 jsval_release(val);
880 if(SUCCEEDED(hres))
881 hres = jsdisp_propput_name(ret_array, lengthW, jsval_number(delete_cnt));
884 if(add_args < delete_cnt) {
885 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
886 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val);
887 if(hres == DISP_E_UNKNOWNNAME) {
888 hres = jsdisp_delete_idx(jsthis, i+add_args);
889 }else if(SUCCEEDED(hres)) {
890 hres = jsdisp_propput_idx(jsthis, i+add_args, val);
891 jsval_release(val);
895 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
896 hres = jsdisp_delete_idx(jsthis, i-1);
897 }else if(add_args > delete_cnt) {
898 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
899 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val);
900 if(hres == DISP_E_UNKNOWNNAME) {
901 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
902 }else if(SUCCEEDED(hres)) {
903 hres = jsdisp_propput_idx(jsthis, i+add_args-1, val);
904 jsval_release(val);
909 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
910 hres = jsdisp_propput_idx(jsthis, start+i, argv[i+2]);
912 if(SUCCEEDED(hres))
913 hres = jsdisp_propput_name(jsthis, lengthW, jsval_number(length-delete_cnt+add_args));
915 if(FAILED(hres)) {
916 if(ret_array)
917 jsdisp_release(ret_array);
918 return hres;
921 if(r)
922 *r = jsval_obj(ret_array);
923 return S_OK;
926 /* ECMA-262 3rd Edition 15.4.4.2 */
927 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
928 jsval_t *r)
930 ArrayInstance *array;
932 TRACE("\n");
934 array = array_this(jsthis);
935 if(!array)
936 return throw_type_error(ctx, JS_E_ARRAY_EXPECTED, NULL);
938 return array_join(ctx, &array->dispex, array->length, 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, r);
1017 static void Array_destructor(jsdisp_t *dispex)
1019 heap_free(dispex);
1022 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1024 ArrayInstance *array = (ArrayInstance*)dispex;
1025 const WCHAR *ptr = name;
1026 DWORD id = 0;
1028 if(!isdigitW(*ptr))
1029 return;
1031 while(*ptr && isdigitW(*ptr)) {
1032 id = id*10 + (*ptr-'0');
1033 ptr++;
1036 if(*ptr)
1037 return;
1039 if(id >= array->length)
1040 array->length = id+1;
1043 static const builtin_prop_t Array_props[] = {
1044 {concatW, Array_concat, PROPF_METHOD|1},
1045 {joinW, Array_join, PROPF_METHOD|1},
1046 {lengthW, NULL,0, Array_get_length, Array_set_length},
1047 {popW, Array_pop, PROPF_METHOD},
1048 {pushW, Array_push, PROPF_METHOD|1},
1049 {reverseW, Array_reverse, PROPF_METHOD},
1050 {shiftW, Array_shift, PROPF_METHOD},
1051 {sliceW, Array_slice, PROPF_METHOD|2},
1052 {sortW, Array_sort, PROPF_METHOD|1},
1053 {spliceW, Array_splice, PROPF_METHOD|2},
1054 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1055 {toStringW, Array_toString, PROPF_METHOD},
1056 {unshiftW, Array_unshift, PROPF_METHOD|1},
1059 static const builtin_info_t Array_info = {
1060 JSCLASS_ARRAY,
1061 {NULL, NULL,0, Array_get_value},
1062 sizeof(Array_props)/sizeof(*Array_props),
1063 Array_props,
1064 Array_destructor,
1065 Array_on_put
1068 static const builtin_prop_t ArrayInst_props[] = {
1069 {lengthW, NULL,0, Array_get_length, Array_set_length}
1072 static const builtin_info_t ArrayInst_info = {
1073 JSCLASS_ARRAY,
1074 {NULL, NULL,0, Array_get_value},
1075 sizeof(ArrayInst_props)/sizeof(*ArrayInst_props),
1076 ArrayInst_props,
1077 Array_destructor,
1078 Array_on_put
1081 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1082 jsval_t *r)
1084 jsdisp_t *obj;
1085 DWORD i;
1086 HRESULT hres;
1088 TRACE("\n");
1090 switch(flags) {
1091 case DISPATCH_METHOD:
1092 case DISPATCH_CONSTRUCT: {
1093 if(argc == 1 && is_number(argv[0])) {
1094 double n = get_number(argv[0]);
1096 if(n < 0 || !is_int32(n))
1097 return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
1099 hres = create_array(ctx, n, &obj);
1100 if(FAILED(hres))
1101 return hres;
1103 *r = jsval_obj(obj);
1104 return S_OK;
1107 hres = create_array(ctx, argc, &obj);
1108 if(FAILED(hres))
1109 return hres;
1111 for(i=0; i < argc; i++) {
1112 hres = jsdisp_propput_idx(obj, i, argv[i]);
1113 if(FAILED(hres))
1114 break;
1116 if(FAILED(hres)) {
1117 jsdisp_release(obj);
1118 return hres;
1121 *r = jsval_obj(obj);
1122 break;
1124 default:
1125 FIXME("unimplemented flags: %x\n", flags);
1126 return E_NOTIMPL;
1129 return S_OK;
1132 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1134 ArrayInstance *array;
1135 HRESULT hres;
1137 array = heap_alloc_zero(sizeof(ArrayInstance));
1138 if(!array)
1139 return E_OUTOFMEMORY;
1141 if(object_prototype)
1142 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1143 else
1144 hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1146 if(FAILED(hres)) {
1147 heap_free(array);
1148 return hres;
1151 *ret = array;
1152 return S_OK;
1155 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1157 ArrayInstance *array;
1158 HRESULT hres;
1160 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1162 hres = alloc_array(ctx, object_prototype, &array);
1163 if(FAILED(hres))
1164 return hres;
1166 hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1168 jsdisp_release(&array->dispex);
1169 return hres;
1172 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1174 ArrayInstance *array;
1175 HRESULT hres;
1177 hres = alloc_array(ctx, NULL, &array);
1178 if(FAILED(hres))
1179 return hres;
1181 array->length = length;
1183 *ret = &array->dispex;
1184 return S_OK;