winealsa.drv: Rewrite channel counting for additional readability and circumvention...
[wine.git] / dlls / jscript / array.c
blob99127d49430e4898174f432bafc020f07d2378a6
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 <math.h>
21 #include "jscript.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27 typedef struct {
28 jsdisp_t dispex;
30 DWORD length;
31 } ArrayInstance;
33 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
34 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
35 static const WCHAR joinW[] = {'j','o','i','n',0};
36 static const WCHAR popW[] = {'p','o','p',0};
37 static const WCHAR pushW[] = {'p','u','s','h',0};
38 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
39 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
40 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
41 static const WCHAR sortW[] = {'s','o','r','t',0};
42 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
43 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
44 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
45 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
47 static const WCHAR default_separatorW[] = {',',0};
49 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
51 return (ArrayInstance*)vdisp->u.jsdisp;
54 static inline ArrayInstance *array_this(vdisp_t *jsthis)
56 return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
59 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsexcept_t *ei, jsdisp_t **jsthis, DWORD *ret)
61 ArrayInstance *array;
62 VARIANT var;
63 HRESULT hres;
65 array = array_this(vdisp);
66 if(array) {
67 *jsthis = &array->dispex;
68 *ret = array->length;
69 return S_OK;
72 if(!is_jsdisp(vdisp))
73 return throw_type_error(ctx, ei, IDS_JSCRIPT_EXPECTED, NULL);
75 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei, NULL/*FIXME*/);
76 if(FAILED(hres))
77 return hres;
79 hres = to_uint32(ctx, &var, ei, ret);
80 VariantClear(&var);
81 if(FAILED(hres))
82 return hres;
84 *jsthis = vdisp->u.jsdisp;
85 return S_OK;
88 static HRESULT set_length(jsdisp_t *obj, jsexcept_t *ei, DWORD length)
90 VARIANT var;
92 if(is_class(obj, JSCLASS_ARRAY)) {
93 ((ArrayInstance*)obj)->length = length;
94 return S_OK;
97 V_VT(&var) = VT_I4;
98 V_I4(&var) = length;
99 return jsdisp_propput_name(obj, lengthW, &var, ei, NULL/*FIXME*/);
102 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
104 if(!idx) {
105 *ptr = '0';
106 return ptr;
109 while(idx) {
110 *ptr-- = '0' + (idx%10);
111 idx /= 10;
114 return ptr+1;
117 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
118 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
120 ArrayInstance *This = array_from_vdisp(jsthis);
122 TRACE("%p %d\n", This, This->length);
124 switch(flags) {
125 case DISPATCH_PROPERTYGET:
126 V_VT(retv) = VT_I4;
127 V_I4(retv) = This->length;
128 break;
129 case DISPATCH_PROPERTYPUT: {
130 VARIANT num;
131 DOUBLE len = -1;
132 DWORD i;
133 HRESULT hres;
135 hres = to_number(ctx, get_arg(dp, 0), ei, &num);
136 if(V_VT(&num) == VT_I4)
137 len = V_I4(&num);
138 else
139 len = floor(V_R8(&num));
141 if(len!=(DWORD)len)
142 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
144 for(i=len; i<This->length; i++) {
145 hres = jsdisp_delete_idx(&This->dispex, i);
146 if(FAILED(hres))
147 return hres;
150 This->length = len;
151 break;
153 default:
154 FIXME("unimplemented flags %x\n", flags);
155 return E_NOTIMPL;
158 return S_OK;
161 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len,
162 jsexcept_t *ei, IServiceProvider *caller)
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, caller);
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, caller);
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, IServiceProvider *caller)
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, caller);
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, caller);
206 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
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, caller);
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, caller);
228 else
229 hres = jsdisp_propput_idx(ret, len++, arg, ei, caller);
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,
246 jsexcept_t *ei, IServiceProvider *caller)
248 BSTR *str_tab, ret = NULL;
249 VARIANT var;
250 DWORD i;
251 HRESULT hres = E_FAIL;
253 if(!length) {
254 if(retv) {
255 V_VT(retv) = VT_BSTR;
256 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
257 if(!V_BSTR(retv))
258 return E_OUTOFMEMORY;
260 return S_OK;
263 str_tab = heap_alloc_zero(length * sizeof(BSTR));
264 if(!str_tab)
265 return E_OUTOFMEMORY;
267 for(i=0; i < length; i++) {
268 hres = jsdisp_get_idx(array, i, &var, ei, caller);
269 if(hres == DISP_E_UNKNOWNNAME) {
270 hres = S_OK;
271 continue;
272 } else if(FAILED(hres))
273 break;
275 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
276 hres = to_string(ctx, &var, ei, str_tab+i);
277 VariantClear(&var);
278 if(FAILED(hres))
279 break;
282 if(SUCCEEDED(hres)) {
283 DWORD seplen = 0, len = 0;
284 WCHAR *ptr;
286 seplen = strlenW(sep);
288 if(str_tab[0])
289 len = SysStringLen(str_tab[0]);
290 for(i=1; i < length; i++)
291 len += seplen + SysStringLen(str_tab[i]);
293 ret = SysAllocStringLen(NULL, len);
294 if(ret) {
295 DWORD tmplen = 0;
297 if(str_tab[0]) {
298 tmplen = SysStringLen(str_tab[0]);
299 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
302 ptr = ret + tmplen;
303 for(i=1; i < length; i++) {
304 if(seplen) {
305 memcpy(ptr, sep, seplen*sizeof(WCHAR));
306 ptr += seplen;
309 if(str_tab[i]) {
310 tmplen = SysStringLen(str_tab[i]);
311 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
312 ptr += tmplen;
315 *ptr=0;
316 }else {
317 hres = E_OUTOFMEMORY;
321 for(i=0; i < length; i++)
322 SysFreeString(str_tab[i]);
323 heap_free(str_tab);
324 if(FAILED(hres))
325 return hres;
327 TRACE("= %s\n", debugstr_w(ret));
329 if(retv) {
330 if(!ret) {
331 ret = SysAllocStringLen(NULL, 0);
332 if(!ret)
333 return E_OUTOFMEMORY;
336 V_VT(retv) = VT_BSTR;
337 V_BSTR(retv) = ret;
338 }else {
339 SysFreeString(ret);
342 return S_OK;
345 /* ECMA-262 3rd Edition 15.4.4.5 */
346 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
347 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
349 jsdisp_t *jsthis;
350 DWORD length;
351 HRESULT hres;
353 TRACE("\n");
355 hres = get_length(ctx, vthis, ei, &jsthis, &length);
356 if(FAILED(hres))
357 return hres;
359 if(arg_cnt(dp)) {
360 BSTR sep;
362 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
363 if(FAILED(hres))
364 return hres;
366 hres = array_join(ctx, jsthis, length, sep, retv, ei, caller);
368 SysFreeString(sep);
369 }else {
370 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei, caller);
373 return hres;
376 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
377 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
379 jsdisp_t *jsthis;
380 VARIANT val;
381 DWORD length;
382 HRESULT hres;
384 TRACE("\n");
386 hres = get_length(ctx, vthis, ei, &jsthis, &length);
387 if(FAILED(hres))
388 return hres;
390 if(!length) {
391 hres = set_length(jsthis, ei, 0);
392 if(FAILED(hres))
393 return hres;
395 if(retv)
396 V_VT(retv) = VT_EMPTY;
397 return S_OK;
400 length--;
401 hres = jsdisp_get_idx(jsthis, length, &val, ei, caller);
402 if(SUCCEEDED(hres)) {
403 hres = jsdisp_delete_idx(jsthis, length);
404 } else if(hres == DISP_E_UNKNOWNNAME) {
405 V_VT(&val) = VT_EMPTY;
406 hres = S_OK;
407 } else
408 return hres;
410 if(SUCCEEDED(hres))
411 hres = set_length(jsthis, ei, length);
413 if(FAILED(hres)) {
414 VariantClear(&val);
415 return hres;
418 if(retv)
419 *retv = val;
420 else
421 VariantClear(&val);
423 return S_OK;
426 /* ECMA-262 3rd Edition 15.4.4.7 */
427 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
428 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
430 jsdisp_t *jsthis;
431 DWORD length = 0;
432 int i, n;
433 HRESULT hres;
435 TRACE("\n");
437 hres = get_length(ctx, vthis, ei, &jsthis, &length);
438 if(FAILED(hres))
439 return hres;
441 n = arg_cnt(dp);
442 for(i=0; i < n; i++) {
443 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei, sp);
444 if(FAILED(hres))
445 return hres;
448 hres = set_length(jsthis, ei, length+n);
449 if(FAILED(hres))
450 return hres;
452 if(retv) {
453 V_VT(retv) = VT_I4;
454 V_I4(retv) = length+n;
456 return S_OK;
459 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
460 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
462 jsdisp_t *jsthis;
463 DWORD length, k, l;
464 VARIANT v1, v2;
465 HRESULT hres1, hres2;
467 TRACE("\n");
469 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
470 if(FAILED(hres1))
471 return hres1;
473 for(k=0; k<length/2; k++) {
474 l = length-k-1;
476 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei, sp);
477 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
478 return hres1;
480 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei, sp);
481 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
482 VariantClear(&v1);
483 return hres2;
486 if(hres1 == DISP_E_UNKNOWNNAME)
487 hres1 = jsdisp_delete_idx(jsthis, l);
488 else
489 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei, sp);
491 if(FAILED(hres1)) {
492 VariantClear(&v1);
493 VariantClear(&v2);
494 return hres1;
497 if(hres2 == DISP_E_UNKNOWNNAME)
498 hres2 = jsdisp_delete_idx(jsthis, k);
499 else
500 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei, sp);
502 if(FAILED(hres2)) {
503 VariantClear(&v2);
504 return hres2;
508 if(retv) {
509 jsdisp_addref(jsthis);
510 var_set_jsdisp(retv, jsthis);
513 return S_OK;
516 /* ECMA-262 3rd Edition 15.4.4.9 */
517 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
518 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
520 jsdisp_t *jsthis;
521 DWORD length = 0, i;
522 VARIANT v, ret;
523 HRESULT hres;
525 TRACE("\n");
527 hres = get_length(ctx, vthis, ei, &jsthis, &length);
528 if(FAILED(hres))
529 return hres;
531 if(!length) {
532 hres = set_length(jsthis, ei, 0);
533 if(FAILED(hres))
534 return hres;
537 if(!length) {
538 if(retv)
539 V_VT(retv) = VT_EMPTY;
540 return S_OK;
543 hres = jsdisp_get_idx(jsthis, 0, &ret, ei, caller);
544 if(hres == DISP_E_UNKNOWNNAME) {
545 V_VT(&ret) = VT_EMPTY;
546 hres = S_OK;
549 for(i=1; SUCCEEDED(hres) && i<length; i++) {
550 hres = jsdisp_get_idx(jsthis, i, &v, ei, caller);
551 if(hres == DISP_E_UNKNOWNNAME)
552 hres = jsdisp_delete_idx(jsthis, i-1);
553 else if(SUCCEEDED(hres))
554 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei, caller);
557 if(SUCCEEDED(hres)) {
558 hres = jsdisp_delete_idx(jsthis, length-1);
559 if(SUCCEEDED(hres))
560 hres = set_length(jsthis, ei, length-1);
563 if(SUCCEEDED(hres) && retv)
564 *retv = ret;
565 else
566 VariantClear(&ret);
567 return hres;
570 /* ECMA-262 3rd Edition 15.4.4.10 */
571 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
572 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
574 jsdisp_t *arr, *jsthis;
575 VARIANT v;
576 DOUBLE range;
577 DWORD length, start, end, idx;
578 HRESULT hres;
580 TRACE("\n");
582 hres = get_length(ctx, vthis, ei, &jsthis, &length);
583 if(FAILED(hres))
584 return hres;
586 if(arg_cnt(dp)) {
587 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
588 if(FAILED(hres))
589 return hres;
591 if(V_VT(&v) == VT_I4)
592 range = V_I4(&v);
593 else
594 range = floor(V_R8(&v));
596 if(-range>length || isnan(range)) start = 0;
597 else if(range < 0) start = range+length;
598 else if(range <= length) start = range;
599 else start = length;
601 else start = 0;
603 if(arg_cnt(dp)>1) {
604 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
605 if(FAILED(hres))
606 return hres;
608 if(V_VT(&v) == VT_I4)
609 range = V_I4(&v);
610 else
611 range = floor(V_R8(&v));
613 if(-range>length) end = 0;
614 else if(range < 0) end = range+length;
615 else if(range <= length) end = range;
616 else end = length;
618 else end = length;
620 hres = create_array(ctx, (end>start)?end-start:0, &arr);
621 if(FAILED(hres))
622 return hres;
624 for(idx=start; idx<end; idx++) {
625 hres = jsdisp_get_idx(jsthis, idx, &v, ei, sp);
626 if(hres == DISP_E_UNKNOWNNAME)
627 continue;
629 if(SUCCEEDED(hres)) {
630 hres = jsdisp_propput_idx(arr, idx-start, &v, ei, sp);
631 VariantClear(&v);
634 if(FAILED(hres)) {
635 jsdisp_release(arr);
636 return hres;
640 if(retv)
641 var_set_jsdisp(retv, arr);
642 else
643 jsdisp_release(arr);
645 return S_OK;
648 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
649 IServiceProvider *caller, INT *cmp)
651 HRESULT hres;
653 if(cmp_func) {
654 VARIANTARG args[2];
655 DISPPARAMS dp = {args, NULL, 2, 0};
656 VARIANT tmp;
657 VARIANT res;
659 args[0] = *v2;
660 args[1] = *v1;
662 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
663 if(FAILED(hres))
664 return hres;
666 hres = to_number(ctx, &res, ei, &tmp);
667 VariantClear(&res);
668 if(FAILED(hres))
669 return hres;
671 if(V_VT(&tmp) == VT_I4)
672 *cmp = V_I4(&tmp);
673 else
674 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
675 }else if(V_VT(v1) == VT_EMPTY) {
676 *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
677 }else if(V_VT(v2) == VT_EMPTY) {
678 *cmp = -1;
679 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
680 DOUBLE d = num_val(v1)-num_val(v2);
681 if(d > 0.0)
682 *cmp = 1;
683 else
684 *cmp = d < -0.0 ? -1 : 0;
685 }else {
686 BSTR x, y;
688 hres = to_string(ctx, v1, ei, &x);
689 if(FAILED(hres))
690 return hres;
692 hres = to_string(ctx, v2, ei, &y);
693 if(SUCCEEDED(hres)) {
694 *cmp = strcmpW(x, y);
695 SysFreeString(y);
697 SysFreeString(x);
698 if(FAILED(hres))
699 return hres;
702 return S_OK;
705 /* ECMA-262 3rd Edition 15.4.4.11 */
706 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
707 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
709 jsdisp_t *jsthis, *cmp_func = NULL;
710 VARIANT *vtab, **sorttab = NULL;
711 DWORD length;
712 DWORD i;
713 HRESULT hres = S_OK;
715 TRACE("\n");
717 hres = get_length(ctx, vthis, ei, &jsthis, &length);
718 if(FAILED(hres))
719 return hres;
721 if(arg_cnt(dp) > 1) {
722 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
723 return E_FAIL;
726 if(arg_cnt(dp) == 1) {
727 VARIANT *arg = get_arg(dp, 0);
729 if(V_VT(arg) != VT_DISPATCH) {
730 WARN("arg is not dispatch\n");
731 return E_FAIL;
735 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
736 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
737 WARN("cmp_func is not a function\n");
738 if(cmp_func)
739 jsdisp_release(cmp_func);
740 return E_FAIL;
744 if(!length) {
745 if(cmp_func)
746 jsdisp_release(cmp_func);
747 if(retv) {
748 jsdisp_addref(jsthis);
749 var_set_jsdisp(retv, jsthis);
751 return S_OK;
754 vtab = heap_alloc_zero(length * sizeof(VARIANT));
755 if(vtab) {
756 for(i=0; i<length; i++) {
757 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei, caller);
758 if(hres == DISP_E_UNKNOWNNAME) {
759 V_VT(vtab+i) = VT_EMPTY;
760 hres = S_OK;
761 } else if(FAILED(hres)) {
762 WARN("Could not get elem %d: %08x\n", i, hres);
763 break;
766 }else {
767 hres = E_OUTOFMEMORY;
770 if(SUCCEEDED(hres)) {
771 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
772 if(!sorttab)
773 hres = E_OUTOFMEMORY;
776 /* merge-sort */
777 if(SUCCEEDED(hres)) {
778 VARIANT *tmpv, **tmpbuf;
779 INT cmp;
781 tmpbuf = sorttab + length;
782 for(i=0; i < length; i++)
783 sorttab[i] = vtab+i;
785 for(i=0; i < length/2; i++) {
786 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
787 if(FAILED(hres))
788 break;
790 if(cmp < 0) {
791 tmpv = sorttab[2*i];
792 sorttab[2*i] = sorttab[2*i+1];
793 sorttab[2*i+1] = tmpv;
797 if(SUCCEEDED(hres)) {
798 DWORD k, a, b, bend;
800 for(k=2; k < length; k *= 2) {
801 for(i=0; i+k < length; i += 2*k) {
802 a = b = 0;
803 if(i+2*k <= length)
804 bend = k;
805 else
806 bend = length - (i+k);
808 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
810 while(a < k && b < bend) {
811 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
812 if(FAILED(hres))
813 break;
815 if(cmp < 0) {
816 sorttab[i+a+b] = tmpbuf[a];
817 a++;
818 }else {
819 sorttab[i+a+b] = sorttab[i+k+b];
820 b++;
824 if(FAILED(hres))
825 break;
827 if(a < k)
828 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
831 if(FAILED(hres))
832 break;
836 for(i=0; SUCCEEDED(hres) && i < length; i++)
837 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei, caller);
840 if(vtab) {
841 for(i=0; i < length; i++)
842 VariantClear(vtab+i);
843 heap_free(vtab);
845 heap_free(sorttab);
846 if(cmp_func)
847 jsdisp_release(cmp_func);
849 if(FAILED(hres))
850 return hres;
852 if(retv) {
853 jsdisp_addref(jsthis);
854 var_set_jsdisp(retv, jsthis);
857 return S_OK;
860 /* ECMA-262 3rd Edition 15.4.4.12 */
861 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
862 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
864 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
865 jsdisp_t *ret_array = NULL, *jsthis;
866 VARIANT v;
867 HRESULT hres = S_OK;
869 TRACE("\n");
871 hres = get_length(ctx, vthis, ei, &jsthis, &length);
872 if(FAILED(hres))
873 return hres;
875 argc = arg_cnt(dp);
876 if(argc >= 1) {
877 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
878 if(FAILED(hres))
879 return hres;
881 if(V_VT(&v) == VT_I4) {
882 if(V_I4(&v) >= 0)
883 start = min(V_I4(&v), length);
884 else
885 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
886 }else {
887 start = V_R8(&v) < 0.0 ? 0 : length;
891 if(argc >= 2) {
892 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
893 if(FAILED(hres))
894 return hres;
896 if(V_VT(&v) == VT_I4) {
897 if(V_I4(&v) > 0)
898 delete_cnt = min(V_I4(&v), length-start);
899 }else if(V_R8(&v) > 0.0) {
900 delete_cnt = length-start;
903 add_args = argc-2;
906 if(retv) {
907 hres = create_array(ctx, 0, &ret_array);
908 if(FAILED(hres))
909 return hres;
911 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
912 hres = jsdisp_get_idx(jsthis, start+i, &v, ei, caller);
913 if(hres == DISP_E_UNKNOWNNAME)
914 hres = S_OK;
915 else if(SUCCEEDED(hres))
916 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
919 if(SUCCEEDED(hres)) {
920 V_VT(&v) = VT_I4;
921 V_I4(&v) = delete_cnt;
923 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
927 if(add_args < delete_cnt) {
928 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
929 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei, caller);
930 if(hres == DISP_E_UNKNOWNNAME)
931 hres = jsdisp_delete_idx(jsthis, i+add_args);
932 else if(SUCCEEDED(hres))
933 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
936 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
937 hres = jsdisp_delete_idx(jsthis, i-1);
938 }else if(add_args > delete_cnt) {
939 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
940 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
941 if(hres == DISP_E_UNKNOWNNAME)
942 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
943 else if(SUCCEEDED(hres))
944 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
948 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
949 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
951 if(SUCCEEDED(hres)) {
952 V_VT(&v) = VT_I4;
953 V_I4(&v) = length-delete_cnt+add_args;
954 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
957 if(FAILED(hres)) {
958 if(ret_array)
959 jsdisp_release(ret_array);
960 return hres;
963 if(retv)
964 var_set_jsdisp(retv, ret_array);
965 return S_OK;
968 /* ECMA-262 3rd Edition 15.4.4.2 */
969 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
970 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
972 ArrayInstance *array;
974 TRACE("\n");
976 array = array_this(jsthis);
977 if(!array)
978 return throw_type_error(ctx, ei, IDS_ARRAY_EXPECTED, NULL);
980 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
983 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
984 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
986 FIXME("\n");
987 return E_NOTIMPL;
990 /* ECMA-262 3rd Edition 15.4.4.13 */
991 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
992 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
994 jsdisp_t *jsthis;
995 WCHAR buf[14], *buf_end, *str;
996 DWORD argc, i, length;
997 VARIANT var;
998 DISPID id;
999 HRESULT hres;
1001 TRACE("\n");
1003 hres = get_length(ctx, vthis, ei, &jsthis, &length);
1004 if(FAILED(hres))
1005 return hres;
1007 argc = arg_cnt(dp);
1008 if(argc) {
1009 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1010 *buf_end-- = 0;
1011 i = length;
1013 while(i--) {
1014 str = idx_to_str(i, buf_end);
1016 hres = jsdisp_get_id(jsthis, str, 0, &id);
1017 if(SUCCEEDED(hres)) {
1018 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
1019 if(FAILED(hres))
1020 return hres;
1022 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
1023 VariantClear(&var);
1024 }else if(hres == DISP_E_UNKNOWNNAME) {
1025 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1029 if(FAILED(hres))
1030 return hres;
1033 for(i=0; i<argc; i++) {
1034 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
1035 if(FAILED(hres))
1036 return hres;
1039 if(argc) {
1040 length += argc;
1041 hres = set_length(jsthis, ei, length);
1042 if(FAILED(hres))
1043 return hres;
1046 if(retv) {
1047 if(ctx->version < 2) {
1048 V_VT(retv) = VT_EMPTY;
1049 }else {
1050 V_VT(retv) = VT_I4;
1051 V_I4(retv) = length;
1054 return S_OK;
1057 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1058 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1060 TRACE("\n");
1062 switch(flags) {
1063 case INVOKE_FUNC:
1064 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1065 case INVOKE_PROPERTYGET:
1066 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1067 default:
1068 FIXME("unimplemented flags %x\n", flags);
1069 return E_NOTIMPL;
1072 return S_OK;
1075 static void Array_destructor(jsdisp_t *dispex)
1077 heap_free(dispex);
1080 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1082 ArrayInstance *array = (ArrayInstance*)dispex;
1083 const WCHAR *ptr = name;
1084 DWORD id = 0;
1086 if(!isdigitW(*ptr))
1087 return;
1089 while(*ptr && isdigitW(*ptr)) {
1090 id = id*10 + (*ptr-'0');
1091 ptr++;
1094 if(*ptr)
1095 return;
1097 if(id >= array->length)
1098 array->length = id+1;
1101 static const builtin_prop_t Array_props[] = {
1102 {concatW, Array_concat, PROPF_METHOD|1},
1103 {joinW, Array_join, PROPF_METHOD|1},
1104 {lengthW, Array_length, 0},
1105 {popW, Array_pop, PROPF_METHOD},
1106 {pushW, Array_push, PROPF_METHOD|1},
1107 {reverseW, Array_reverse, PROPF_METHOD},
1108 {shiftW, Array_shift, PROPF_METHOD},
1109 {sliceW, Array_slice, PROPF_METHOD|2},
1110 {sortW, Array_sort, PROPF_METHOD|1},
1111 {spliceW, Array_splice, PROPF_METHOD|2},
1112 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1113 {toStringW, Array_toString, PROPF_METHOD},
1114 {unshiftW, Array_unshift, PROPF_METHOD|1},
1117 static const builtin_info_t Array_info = {
1118 JSCLASS_ARRAY,
1119 {NULL, Array_value, 0},
1120 sizeof(Array_props)/sizeof(*Array_props),
1121 Array_props,
1122 Array_destructor,
1123 Array_on_put
1126 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1127 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1129 jsdisp_t *obj;
1130 VARIANT *arg_var;
1131 DWORD i;
1132 HRESULT hres;
1134 TRACE("\n");
1136 switch(flags) {
1137 case DISPATCH_METHOD:
1138 case DISPATCH_CONSTRUCT: {
1139 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1140 if(V_I4(arg_var) < 0)
1141 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
1143 hres = create_array(ctx, V_I4(arg_var), &obj);
1144 if(FAILED(hres))
1145 return hres;
1147 var_set_jsdisp(retv, obj);
1148 return S_OK;
1151 hres = create_array(ctx, arg_cnt(dp), &obj);
1152 if(FAILED(hres))
1153 return hres;
1155 for(i=0; i < arg_cnt(dp); i++) {
1156 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei, caller);
1157 if(FAILED(hres))
1158 break;
1160 if(FAILED(hres)) {
1161 jsdisp_release(obj);
1162 return hres;
1165 var_set_jsdisp(retv, obj);
1166 break;
1168 default:
1169 FIXME("unimplemented flags: %x\n", flags);
1170 return E_NOTIMPL;
1173 return S_OK;
1176 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1178 ArrayInstance *array;
1179 HRESULT hres;
1181 array = heap_alloc_zero(sizeof(ArrayInstance));
1182 if(!array)
1183 return E_OUTOFMEMORY;
1185 if(object_prototype)
1186 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1187 else
1188 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1190 if(FAILED(hres)) {
1191 heap_free(array);
1192 return hres;
1195 *ret = array;
1196 return S_OK;
1199 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1201 ArrayInstance *array;
1202 HRESULT hres;
1204 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1206 hres = alloc_array(ctx, object_prototype, &array);
1207 if(FAILED(hres))
1208 return hres;
1210 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1212 jsdisp_release(&array->dispex);
1213 return hres;
1216 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1218 ArrayInstance *array;
1219 HRESULT hres;
1221 hres = alloc_array(ctx, NULL, &array);
1222 if(FAILED(hres))
1223 return hres;
1225 array->length = length;
1227 *ret = &array->dispex;
1228 return S_OK;