jscript: Better handling of to_integer result in Array.splice.
[wine/multimedia.git] / dlls / jscript / array.c
blobe2885255ae2e63e6a2b762a2d37ddf9bbb410095
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 VARIANT var;
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, &var, ei);
79 if(FAILED(hres))
80 return hres;
82 hres = to_uint32(ctx, &var, ei, ret);
83 VariantClear(&var);
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 VARIANT var;
95 if(is_class(obj, JSCLASS_ARRAY)) {
96 ((ArrayInstance*)obj)->length = length;
97 return S_OK;
100 V_VT(&var) = VT_I4;
101 V_I4(&var) = length;
102 return jsdisp_propput_name(obj, lengthW, &var, ei);
105 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
107 if(!idx) {
108 *ptr = '0';
109 return ptr;
112 while(idx) {
113 *ptr-- = '0' + (idx%10);
114 idx /= 10;
117 return ptr+1;
120 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
121 VARIANT *retv, jsexcept_t *ei)
123 ArrayInstance *This = array_from_vdisp(jsthis);
125 TRACE("%p %d\n", This, This->length);
127 switch(flags) {
128 case DISPATCH_PROPERTYGET:
129 V_VT(retv) = VT_I4;
130 V_I4(retv) = This->length;
131 break;
132 case DISPATCH_PROPERTYPUT: {
133 DOUBLE len = -1;
134 DWORD i;
135 HRESULT hres;
137 hres = to_number(ctx, get_arg(dp, 0), ei, &len);
138 if(FAILED(hres))
139 return hres;
141 len = floor(len);
142 if(len!=(DWORD)len)
143 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
145 for(i=len; i<This->length; i++) {
146 hres = jsdisp_delete_idx(&This->dispex, i);
147 if(FAILED(hres))
148 return hres;
151 This->length = len;
152 break;
154 default:
155 FIXME("unimplemented flags %x\n", flags);
156 return E_NOTIMPL;
159 return S_OK;
162 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
164 VARIANT var;
165 DWORD i;
166 HRESULT hres;
168 for(i=0; i < obj->length; i++) {
169 hres = jsdisp_get_idx(&obj->dispex, i, &var, ei);
170 if(hres == DISP_E_UNKNOWNNAME)
171 continue;
172 if(FAILED(hres))
173 return hres;
175 hres = jsdisp_propput_idx(array, *len+i, &var, ei);
176 VariantClear(&var);
177 if(FAILED(hres))
178 return hres;
181 *len += obj->length;
182 return S_OK;
185 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
187 jsdisp_t *jsobj;
188 VARIANT var;
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, ei);
195 jsdisp_release(jsobj);
196 return hres;
198 jsdisp_release(jsobj);
201 V_VT(&var) = VT_DISPATCH;
202 V_DISPATCH(&var) = obj;
203 return jsdisp_propput_idx(array, (*len)++, &var, ei);
206 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei)
209 jsdisp_t *ret;
210 DWORD len = 0;
211 HRESULT hres;
213 TRACE("\n");
215 hres = create_array(ctx, 0, &ret);
216 if(FAILED(hres))
217 return hres;
219 hres = concat_obj(ret, jsthis->u.disp, &len, ei);
220 if(SUCCEEDED(hres)) {
221 VARIANT *arg;
222 DWORD i;
224 for(i=0; i < arg_cnt(dp); i++) {
225 arg = get_arg(dp, i);
226 if(V_VT(arg) == VT_DISPATCH)
227 hres = concat_obj(ret, V_DISPATCH(arg), &len, ei);
228 else
229 hres = jsdisp_propput_idx(ret, len++, arg, ei);
230 if(FAILED(hres))
231 break;
235 if(FAILED(hres))
236 return hres;
238 if(retv)
239 var_set_jsdisp(retv, ret);
240 else
241 jsdisp_release(ret);
242 return S_OK;
245 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, VARIANT *retv, jsexcept_t *ei)
247 BSTR *str_tab, ret = NULL;
248 VARIANT var;
249 DWORD i;
250 HRESULT hres = E_FAIL;
252 if(!length) {
253 if(retv) {
254 V_VT(retv) = VT_BSTR;
255 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
256 if(!V_BSTR(retv))
257 return E_OUTOFMEMORY;
259 return S_OK;
262 str_tab = heap_alloc_zero(length * sizeof(BSTR));
263 if(!str_tab)
264 return E_OUTOFMEMORY;
266 for(i=0; i < length; i++) {
267 hres = jsdisp_get_idx(array, i, &var, ei);
268 if(hres == DISP_E_UNKNOWNNAME) {
269 hres = S_OK;
270 continue;
271 } else if(FAILED(hres))
272 break;
274 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
275 hres = to_string(ctx, &var, ei, str_tab+i);
276 VariantClear(&var);
277 if(FAILED(hres))
278 break;
281 if(SUCCEEDED(hres)) {
282 DWORD seplen = 0, len = 0;
283 WCHAR *ptr;
285 seplen = strlenW(sep);
287 if(str_tab[0])
288 len = SysStringLen(str_tab[0]);
289 for(i=1; i < length; i++)
290 len += seplen + SysStringLen(str_tab[i]);
292 ret = SysAllocStringLen(NULL, len);
293 if(ret) {
294 DWORD tmplen = 0;
296 if(str_tab[0]) {
297 tmplen = SysStringLen(str_tab[0]);
298 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
301 ptr = ret + tmplen;
302 for(i=1; i < length; i++) {
303 if(seplen) {
304 memcpy(ptr, sep, seplen*sizeof(WCHAR));
305 ptr += seplen;
308 if(str_tab[i]) {
309 tmplen = SysStringLen(str_tab[i]);
310 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
311 ptr += tmplen;
314 *ptr=0;
315 }else {
316 hres = E_OUTOFMEMORY;
320 for(i=0; i < length; i++)
321 SysFreeString(str_tab[i]);
322 heap_free(str_tab);
323 if(FAILED(hres))
324 return hres;
326 TRACE("= %s\n", debugstr_w(ret));
328 if(retv) {
329 if(!ret) {
330 ret = SysAllocStringLen(NULL, 0);
331 if(!ret)
332 return E_OUTOFMEMORY;
335 V_VT(retv) = VT_BSTR;
336 V_BSTR(retv) = ret;
337 }else {
338 SysFreeString(ret);
341 return S_OK;
344 /* ECMA-262 3rd Edition 15.4.4.5 */
345 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
346 VARIANT *retv, jsexcept_t *ei)
348 jsdisp_t *jsthis;
349 DWORD length;
350 HRESULT hres;
352 TRACE("\n");
354 hres = get_length(ctx, vthis, ei, &jsthis, &length);
355 if(FAILED(hres))
356 return hres;
358 if(arg_cnt(dp)) {
359 BSTR sep;
361 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
362 if(FAILED(hres))
363 return hres;
365 hres = array_join(ctx, jsthis, length, sep, retv, ei);
367 SysFreeString(sep);
368 }else {
369 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei);
372 return hres;
375 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
376 VARIANT *retv, jsexcept_t *ei)
378 jsdisp_t *jsthis;
379 VARIANT val;
380 DWORD length;
381 HRESULT hres;
383 TRACE("\n");
385 hres = get_length(ctx, vthis, ei, &jsthis, &length);
386 if(FAILED(hres))
387 return hres;
389 if(!length) {
390 hres = set_length(jsthis, ei, 0);
391 if(FAILED(hres))
392 return hres;
394 if(retv)
395 V_VT(retv) = VT_EMPTY;
396 return S_OK;
399 length--;
400 hres = jsdisp_get_idx(jsthis, length, &val, ei);
401 if(SUCCEEDED(hres)) {
402 hres = jsdisp_delete_idx(jsthis, length);
403 } else if(hres == DISP_E_UNKNOWNNAME) {
404 V_VT(&val) = VT_EMPTY;
405 hres = S_OK;
406 } else
407 return hres;
409 if(SUCCEEDED(hres))
410 hres = set_length(jsthis, ei, length);
412 if(FAILED(hres)) {
413 VariantClear(&val);
414 return hres;
417 if(retv)
418 *retv = val;
419 else
420 VariantClear(&val);
422 return S_OK;
425 /* ECMA-262 3rd Edition 15.4.4.7 */
426 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
427 VARIANT *retv, jsexcept_t *ei)
429 jsdisp_t *jsthis;
430 DWORD length = 0;
431 int i, n;
432 HRESULT hres;
434 TRACE("\n");
436 hres = get_length(ctx, vthis, ei, &jsthis, &length);
437 if(FAILED(hres))
438 return hres;
440 n = arg_cnt(dp);
441 for(i=0; i < n; i++) {
442 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei);
443 if(FAILED(hres))
444 return hres;
447 hres = set_length(jsthis, ei, length+n);
448 if(FAILED(hres))
449 return hres;
451 if(retv) {
452 V_VT(retv) = VT_I4;
453 V_I4(retv) = length+n;
455 return S_OK;
458 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
459 VARIANT *retv, jsexcept_t *ei)
461 jsdisp_t *jsthis;
462 DWORD length, k, l;
463 VARIANT v1, v2;
464 HRESULT hres1, hres2;
466 TRACE("\n");
468 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
469 if(FAILED(hres1))
470 return hres1;
472 for(k=0; k<length/2; k++) {
473 l = length-k-1;
475 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei);
476 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
477 return hres1;
479 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
480 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
481 VariantClear(&v1);
482 return hres2;
485 if(hres1 == DISP_E_UNKNOWNNAME)
486 hres1 = jsdisp_delete_idx(jsthis, l);
487 else
488 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei);
490 if(FAILED(hres1)) {
491 VariantClear(&v1);
492 VariantClear(&v2);
493 return hres1;
496 if(hres2 == DISP_E_UNKNOWNNAME)
497 hres2 = jsdisp_delete_idx(jsthis, k);
498 else
499 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei);
501 if(FAILED(hres2)) {
502 VariantClear(&v2);
503 return hres2;
507 if(retv) {
508 jsdisp_addref(jsthis);
509 var_set_jsdisp(retv, jsthis);
512 return S_OK;
515 /* ECMA-262 3rd Edition 15.4.4.9 */
516 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
517 VARIANT *retv, jsexcept_t *ei)
519 jsdisp_t *jsthis;
520 DWORD length = 0, i;
521 VARIANT v, ret;
522 HRESULT hres;
524 TRACE("\n");
526 hres = get_length(ctx, vthis, ei, &jsthis, &length);
527 if(FAILED(hres))
528 return hres;
530 if(!length) {
531 hres = set_length(jsthis, ei, 0);
532 if(FAILED(hres))
533 return hres;
536 if(!length) {
537 if(retv)
538 V_VT(retv) = VT_EMPTY;
539 return S_OK;
542 hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
543 if(hres == DISP_E_UNKNOWNNAME) {
544 V_VT(&ret) = VT_EMPTY;
545 hres = S_OK;
548 for(i=1; SUCCEEDED(hres) && i<length; i++) {
549 hres = jsdisp_get_idx(jsthis, i, &v, ei);
550 if(hres == DISP_E_UNKNOWNNAME)
551 hres = jsdisp_delete_idx(jsthis, i-1);
552 else if(SUCCEEDED(hres))
553 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei);
556 if(SUCCEEDED(hres)) {
557 hres = jsdisp_delete_idx(jsthis, length-1);
558 if(SUCCEEDED(hres))
559 hres = set_length(jsthis, ei, length-1);
562 if(SUCCEEDED(hres) && retv)
563 *retv = ret;
564 else
565 VariantClear(&ret);
566 return hres;
569 /* ECMA-262 3rd Edition 15.4.4.10 */
570 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
571 VARIANT *retv, jsexcept_t *ei)
573 jsdisp_t *arr, *jsthis;
574 DOUBLE range;
575 DWORD length, start, end, idx;
576 HRESULT hres;
578 TRACE("\n");
580 hres = get_length(ctx, vthis, ei, &jsthis, &length);
581 if(FAILED(hres))
582 return hres;
584 if(arg_cnt(dp)) {
585 hres = to_number(ctx, get_arg(dp, 0), ei, &range);
586 if(FAILED(hres))
587 return hres;
589 range = floor(range);
590 if(-range>length || isnan(range)) start = 0;
591 else if(range < 0) start = range+length;
592 else if(range <= length) start = range;
593 else start = length;
595 else start = 0;
597 if(arg_cnt(dp)>1) {
598 hres = to_number(ctx, get_arg(dp, 1), ei, &range);
599 if(FAILED(hres))
600 return hres;
602 range = floor(range);
603 if(-range>length) end = 0;
604 else if(range < 0) end = range+length;
605 else if(range <= length) end = range;
606 else end = length;
608 else end = length;
610 hres = create_array(ctx, (end>start)?end-start:0, &arr);
611 if(FAILED(hres))
612 return hres;
614 for(idx=start; idx<end; idx++) {
615 VARIANT v;
617 hres = jsdisp_get_idx(jsthis, idx, &v, ei);
618 if(hres == DISP_E_UNKNOWNNAME)
619 continue;
621 if(SUCCEEDED(hres)) {
622 hres = jsdisp_propput_idx(arr, idx-start, &v, ei);
623 VariantClear(&v);
626 if(FAILED(hres)) {
627 jsdisp_release(arr);
628 return hres;
632 if(retv)
633 var_set_jsdisp(retv, arr);
634 else
635 jsdisp_release(arr);
637 return S_OK;
640 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei, INT *cmp)
642 HRESULT hres;
644 if(cmp_func) {
645 VARIANTARG args[2];
646 DISPPARAMS dp = {args, NULL, 2, 0};
647 double n;
648 VARIANT res;
650 args[0] = *v2;
651 args[1] = *v1;
653 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei);
654 if(FAILED(hres))
655 return hres;
657 hres = to_number(ctx, &res, ei, &n);
658 VariantClear(&res);
659 if(FAILED(hres))
660 return hres;
662 if(n == 0)
663 *cmp = 0;
664 *cmp = n > 0.0 ? 1 : -1;
665 }else if(V_VT(v1) == VT_EMPTY) {
666 *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
667 }else if(V_VT(v2) == VT_EMPTY) {
668 *cmp = -1;
669 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
670 DOUBLE d = num_val(v1)-num_val(v2);
671 if(d > 0.0)
672 *cmp = 1;
673 else
674 *cmp = d < -0.0 ? -1 : 0;
675 }else {
676 BSTR x, y;
678 hres = to_string(ctx, v1, ei, &x);
679 if(FAILED(hres))
680 return hres;
682 hres = to_string(ctx, v2, ei, &y);
683 if(SUCCEEDED(hres)) {
684 *cmp = strcmpW(x, y);
685 SysFreeString(y);
687 SysFreeString(x);
688 if(FAILED(hres))
689 return hres;
692 return S_OK;
695 /* ECMA-262 3rd Edition 15.4.4.11 */
696 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
697 VARIANT *retv, jsexcept_t *ei)
699 jsdisp_t *jsthis, *cmp_func = NULL;
700 VARIANT *vtab, **sorttab = NULL;
701 DWORD length;
702 DWORD i;
703 HRESULT hres = S_OK;
705 TRACE("\n");
707 hres = get_length(ctx, vthis, ei, &jsthis, &length);
708 if(FAILED(hres))
709 return hres;
711 if(arg_cnt(dp) > 1) {
712 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
713 return E_FAIL;
716 if(arg_cnt(dp) == 1) {
717 VARIANT *arg = get_arg(dp, 0);
719 if(V_VT(arg) != VT_DISPATCH) {
720 WARN("arg is not dispatch\n");
721 return E_FAIL;
725 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
726 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
727 WARN("cmp_func is not a function\n");
728 if(cmp_func)
729 jsdisp_release(cmp_func);
730 return E_FAIL;
734 if(!length) {
735 if(cmp_func)
736 jsdisp_release(cmp_func);
737 if(retv) {
738 jsdisp_addref(jsthis);
739 var_set_jsdisp(retv, jsthis);
741 return S_OK;
744 vtab = heap_alloc_zero(length * sizeof(VARIANT));
745 if(vtab) {
746 for(i=0; i<length; i++) {
747 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei);
748 if(hres == DISP_E_UNKNOWNNAME) {
749 V_VT(vtab+i) = VT_EMPTY;
750 hres = S_OK;
751 } else if(FAILED(hres)) {
752 WARN("Could not get elem %d: %08x\n", i, hres);
753 break;
756 }else {
757 hres = E_OUTOFMEMORY;
760 if(SUCCEEDED(hres)) {
761 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
762 if(!sorttab)
763 hres = E_OUTOFMEMORY;
766 /* merge-sort */
767 if(SUCCEEDED(hres)) {
768 VARIANT *tmpv, **tmpbuf;
769 INT cmp;
771 tmpbuf = sorttab + length;
772 for(i=0; i < length; i++)
773 sorttab[i] = vtab+i;
775 for(i=0; i < length/2; i++) {
776 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, &cmp);
777 if(FAILED(hres))
778 break;
780 if(cmp < 0) {
781 tmpv = sorttab[2*i];
782 sorttab[2*i] = sorttab[2*i+1];
783 sorttab[2*i+1] = tmpv;
787 if(SUCCEEDED(hres)) {
788 DWORD k, a, b, bend;
790 for(k=2; k < length; k *= 2) {
791 for(i=0; i+k < length; i += 2*k) {
792 a = b = 0;
793 if(i+2*k <= length)
794 bend = k;
795 else
796 bend = length - (i+k);
798 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
800 while(a < k && b < bend) {
801 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, &cmp);
802 if(FAILED(hres))
803 break;
805 if(cmp < 0) {
806 sorttab[i+a+b] = tmpbuf[a];
807 a++;
808 }else {
809 sorttab[i+a+b] = sorttab[i+k+b];
810 b++;
814 if(FAILED(hres))
815 break;
817 if(a < k)
818 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
821 if(FAILED(hres))
822 break;
826 for(i=0; SUCCEEDED(hres) && i < length; i++)
827 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei);
830 if(vtab) {
831 for(i=0; i < length; i++)
832 VariantClear(vtab+i);
833 heap_free(vtab);
835 heap_free(sorttab);
836 if(cmp_func)
837 jsdisp_release(cmp_func);
839 if(FAILED(hres))
840 return hres;
842 if(retv) {
843 jsdisp_addref(jsthis);
844 var_set_jsdisp(retv, jsthis);
847 return S_OK;
850 /* ECMA-262 3rd Edition 15.4.4.12 */
851 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
852 VARIANT *retv, jsexcept_t *ei)
854 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
855 jsdisp_t *ret_array = NULL, *jsthis;
856 double d;
857 int n;
858 VARIANT v;
859 HRESULT hres = S_OK;
861 TRACE("\n");
863 hres = get_length(ctx, vthis, ei, &jsthis, &length);
864 if(FAILED(hres))
865 return hres;
867 argc = arg_cnt(dp);
868 if(argc >= 1) {
869 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
870 if(FAILED(hres))
871 return hres;
872 d = num_val(&v);
874 if(is_int32(d)) {
875 if((n = d) >= 0)
876 start = min(n, length);
877 else
878 start = -n > length ? 0 : length + n;
879 }else {
880 start = d < 0.0 ? 0 : length;
884 if(argc >= 2) {
885 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
886 if(FAILED(hres))
887 return hres;
888 d = num_val(&v);
890 if(is_int32(d)) {
891 if((n = d) > 0)
892 delete_cnt = min(n, length-start);
893 }else if(d > 0.0) {
894 delete_cnt = length-start;
897 add_args = argc-2;
900 if(retv) {
901 hres = create_array(ctx, 0, &ret_array);
902 if(FAILED(hres))
903 return hres;
905 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
906 hres = jsdisp_get_idx(jsthis, start+i, &v, ei);
907 if(hres == DISP_E_UNKNOWNNAME)
908 hres = S_OK;
909 else if(SUCCEEDED(hres))
910 hres = jsdisp_propput_idx(ret_array, i, &v, ei);
913 if(SUCCEEDED(hres)) {
914 V_VT(&v) = VT_I4;
915 V_I4(&v) = delete_cnt;
917 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei);
921 if(add_args < delete_cnt) {
922 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
923 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei);
924 if(hres == DISP_E_UNKNOWNNAME)
925 hres = jsdisp_delete_idx(jsthis, i+add_args);
926 else if(SUCCEEDED(hres))
927 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei);
930 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
931 hres = jsdisp_delete_idx(jsthis, i-1);
932 }else if(add_args > delete_cnt) {
933 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
934 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei);
935 if(hres == DISP_E_UNKNOWNNAME)
936 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
937 else if(SUCCEEDED(hres))
938 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei);
942 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
943 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei);
945 if(SUCCEEDED(hres)) {
946 V_VT(&v) = VT_I4;
947 V_I4(&v) = length-delete_cnt+add_args;
948 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei);
951 if(FAILED(hres)) {
952 if(ret_array)
953 jsdisp_release(ret_array);
954 return hres;
957 if(retv)
958 var_set_jsdisp(retv, ret_array);
959 return S_OK;
962 /* ECMA-262 3rd Edition 15.4.4.2 */
963 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
964 VARIANT *retv, jsexcept_t *ei)
966 ArrayInstance *array;
968 TRACE("\n");
970 array = array_this(jsthis);
971 if(!array)
972 return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
974 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei);
977 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
978 VARIANT *retv, jsexcept_t *ei)
980 FIXME("\n");
981 return E_NOTIMPL;
984 /* ECMA-262 3rd Edition 15.4.4.13 */
985 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
986 VARIANT *retv, jsexcept_t *ei)
988 jsdisp_t *jsthis;
989 WCHAR buf[14], *buf_end, *str;
990 DWORD argc, i, length;
991 VARIANT var;
992 DISPID id;
993 HRESULT hres;
995 TRACE("\n");
997 hres = get_length(ctx, vthis, ei, &jsthis, &length);
998 if(FAILED(hres))
999 return hres;
1001 argc = arg_cnt(dp);
1002 if(argc) {
1003 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1004 *buf_end-- = 0;
1005 i = length;
1007 while(i--) {
1008 str = idx_to_str(i, buf_end);
1010 hres = jsdisp_get_id(jsthis, str, 0, &id);
1011 if(SUCCEEDED(hres)) {
1012 hres = jsdisp_propget(jsthis, id, &var, ei);
1013 if(FAILED(hres))
1014 return hres;
1016 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei);
1017 VariantClear(&var);
1018 }else if(hres == DISP_E_UNKNOWNNAME) {
1019 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1023 if(FAILED(hres))
1024 return hres;
1027 for(i=0; i<argc; i++) {
1028 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei);
1029 if(FAILED(hres))
1030 return hres;
1033 if(argc) {
1034 length += argc;
1035 hres = set_length(jsthis, ei, length);
1036 if(FAILED(hres))
1037 return hres;
1040 if(retv) {
1041 if(ctx->version < 2) {
1042 V_VT(retv) = VT_EMPTY;
1043 }else {
1044 V_VT(retv) = VT_I4;
1045 V_I4(retv) = length;
1048 return S_OK;
1051 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1052 VARIANT *retv, jsexcept_t *ei)
1054 TRACE("\n");
1056 switch(flags) {
1057 case INVOKE_FUNC:
1058 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1059 case INVOKE_PROPERTYGET:
1060 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei);
1061 default:
1062 FIXME("unimplemented flags %x\n", flags);
1063 return E_NOTIMPL;
1066 return S_OK;
1069 static void Array_destructor(jsdisp_t *dispex)
1071 heap_free(dispex);
1074 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1076 ArrayInstance *array = (ArrayInstance*)dispex;
1077 const WCHAR *ptr = name;
1078 DWORD id = 0;
1080 if(!isdigitW(*ptr))
1081 return;
1083 while(*ptr && isdigitW(*ptr)) {
1084 id = id*10 + (*ptr-'0');
1085 ptr++;
1088 if(*ptr)
1089 return;
1091 if(id >= array->length)
1092 array->length = id+1;
1095 static const builtin_prop_t Array_props[] = {
1096 {concatW, Array_concat, PROPF_METHOD|1},
1097 {joinW, Array_join, PROPF_METHOD|1},
1098 {lengthW, Array_length, 0},
1099 {popW, Array_pop, PROPF_METHOD},
1100 {pushW, Array_push, PROPF_METHOD|1},
1101 {reverseW, Array_reverse, PROPF_METHOD},
1102 {shiftW, Array_shift, PROPF_METHOD},
1103 {sliceW, Array_slice, PROPF_METHOD|2},
1104 {sortW, Array_sort, PROPF_METHOD|1},
1105 {spliceW, Array_splice, PROPF_METHOD|2},
1106 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1107 {toStringW, Array_toString, PROPF_METHOD},
1108 {unshiftW, Array_unshift, PROPF_METHOD|1},
1111 static const builtin_info_t Array_info = {
1112 JSCLASS_ARRAY,
1113 {NULL, Array_value, 0},
1114 sizeof(Array_props)/sizeof(*Array_props),
1115 Array_props,
1116 Array_destructor,
1117 Array_on_put
1120 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1121 VARIANT *retv, jsexcept_t *ei)
1123 jsdisp_t *obj;
1124 VARIANT *arg_var;
1125 DWORD i;
1126 HRESULT hres;
1128 TRACE("\n");
1130 switch(flags) {
1131 case DISPATCH_METHOD:
1132 case DISPATCH_CONSTRUCT: {
1133 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1134 if(V_I4(arg_var) < 0)
1135 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1137 hres = create_array(ctx, V_I4(arg_var), &obj);
1138 if(FAILED(hres))
1139 return hres;
1141 var_set_jsdisp(retv, obj);
1142 return S_OK;
1145 hres = create_array(ctx, arg_cnt(dp), &obj);
1146 if(FAILED(hres))
1147 return hres;
1149 for(i=0; i < arg_cnt(dp); i++) {
1150 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei);
1151 if(FAILED(hres))
1152 break;
1154 if(FAILED(hres)) {
1155 jsdisp_release(obj);
1156 return hres;
1159 var_set_jsdisp(retv, obj);
1160 break;
1162 default:
1163 FIXME("unimplemented flags: %x\n", flags);
1164 return E_NOTIMPL;
1167 return S_OK;
1170 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1172 ArrayInstance *array;
1173 HRESULT hres;
1175 array = heap_alloc_zero(sizeof(ArrayInstance));
1176 if(!array)
1177 return E_OUTOFMEMORY;
1179 if(object_prototype)
1180 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1181 else
1182 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1184 if(FAILED(hres)) {
1185 heap_free(array);
1186 return hres;
1189 *ret = array;
1190 return S_OK;
1193 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1195 ArrayInstance *array;
1196 HRESULT hres;
1198 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1200 hres = alloc_array(ctx, object_prototype, &array);
1201 if(FAILED(hres))
1202 return hres;
1204 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1206 jsdisp_release(&array->dispex);
1207 return hres;
1210 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1212 ArrayInstance *array;
1213 HRESULT hres;
1215 hres = alloc_array(ctx, NULL, &array);
1216 if(FAILED(hres))
1217 return hres;
1219 array->length = length;
1221 *ret = &array->dispex;
1222 return S_OK;