winecoreaudio: Fix a leak.
[wine/multimedia.git] / dlls / jscript / array.c
blobf99fe016e8dd4e38fe51a50990ec41e938371870
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, JS_E_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(FAILED(hres))
137 return hres;
139 if(V_VT(&num) == VT_I4)
140 len = V_I4(&num);
141 else
142 len = floor(V_R8(&num));
144 if(len!=(DWORD)len)
145 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
147 for(i=len; i<This->length; i++) {
148 hres = jsdisp_delete_idx(&This->dispex, i);
149 if(FAILED(hres))
150 return hres;
153 This->length = len;
154 break;
156 default:
157 FIXME("unimplemented flags %x\n", flags);
158 return E_NOTIMPL;
161 return S_OK;
164 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len,
165 jsexcept_t *ei, IServiceProvider *caller)
167 VARIANT var;
168 DWORD i;
169 HRESULT hres;
171 for(i=0; i < obj->length; i++) {
172 hres = jsdisp_get_idx(&obj->dispex, i, &var, ei, caller);
173 if(hres == DISP_E_UNKNOWNNAME)
174 continue;
175 if(FAILED(hres))
176 return hres;
178 hres = jsdisp_propput_idx(array, *len+i, &var, ei, caller);
179 VariantClear(&var);
180 if(FAILED(hres))
181 return hres;
184 *len += obj->length;
185 return S_OK;
188 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei, IServiceProvider *caller)
190 jsdisp_t *jsobj;
191 VARIANT var;
192 HRESULT hres;
194 jsobj = iface_to_jsdisp((IUnknown*)obj);
195 if(jsobj) {
196 if(is_class(jsobj, JSCLASS_ARRAY)) {
197 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei, caller);
198 jsdisp_release(jsobj);
199 return hres;
201 jsdisp_release(jsobj);
204 V_VT(&var) = VT_DISPATCH;
205 V_DISPATCH(&var) = obj;
206 return jsdisp_propput_idx(array, (*len)++, &var, ei, caller);
209 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
210 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
212 jsdisp_t *ret;
213 DWORD len = 0;
214 HRESULT hres;
216 TRACE("\n");
218 hres = create_array(ctx, 0, &ret);
219 if(FAILED(hres))
220 return hres;
222 hres = concat_obj(ret, jsthis->u.disp, &len, ei, caller);
223 if(SUCCEEDED(hres)) {
224 VARIANT *arg;
225 DWORD i;
227 for(i=0; i < arg_cnt(dp); i++) {
228 arg = get_arg(dp, i);
229 if(V_VT(arg) == VT_DISPATCH)
230 hres = concat_obj(ret, V_DISPATCH(arg), &len, ei, caller);
231 else
232 hres = jsdisp_propput_idx(ret, len++, arg, ei, caller);
233 if(FAILED(hres))
234 break;
238 if(FAILED(hres))
239 return hres;
241 if(retv)
242 var_set_jsdisp(retv, ret);
243 else
244 jsdisp_release(ret);
245 return S_OK;
248 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, VARIANT *retv,
249 jsexcept_t *ei, IServiceProvider *caller)
251 BSTR *str_tab, ret = NULL;
252 VARIANT var;
253 DWORD i;
254 HRESULT hres = E_FAIL;
256 if(!length) {
257 if(retv) {
258 V_VT(retv) = VT_BSTR;
259 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
260 if(!V_BSTR(retv))
261 return E_OUTOFMEMORY;
263 return S_OK;
266 str_tab = heap_alloc_zero(length * sizeof(BSTR));
267 if(!str_tab)
268 return E_OUTOFMEMORY;
270 for(i=0; i < length; i++) {
271 hres = jsdisp_get_idx(array, i, &var, ei, caller);
272 if(hres == DISP_E_UNKNOWNNAME) {
273 hres = S_OK;
274 continue;
275 } else if(FAILED(hres))
276 break;
278 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
279 hres = to_string(ctx, &var, ei, str_tab+i);
280 VariantClear(&var);
281 if(FAILED(hres))
282 break;
285 if(SUCCEEDED(hres)) {
286 DWORD seplen = 0, len = 0;
287 WCHAR *ptr;
289 seplen = strlenW(sep);
291 if(str_tab[0])
292 len = SysStringLen(str_tab[0]);
293 for(i=1; i < length; i++)
294 len += seplen + SysStringLen(str_tab[i]);
296 ret = SysAllocStringLen(NULL, len);
297 if(ret) {
298 DWORD tmplen = 0;
300 if(str_tab[0]) {
301 tmplen = SysStringLen(str_tab[0]);
302 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
305 ptr = ret + tmplen;
306 for(i=1; i < length; i++) {
307 if(seplen) {
308 memcpy(ptr, sep, seplen*sizeof(WCHAR));
309 ptr += seplen;
312 if(str_tab[i]) {
313 tmplen = SysStringLen(str_tab[i]);
314 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
315 ptr += tmplen;
318 *ptr=0;
319 }else {
320 hres = E_OUTOFMEMORY;
324 for(i=0; i < length; i++)
325 SysFreeString(str_tab[i]);
326 heap_free(str_tab);
327 if(FAILED(hres))
328 return hres;
330 TRACE("= %s\n", debugstr_w(ret));
332 if(retv) {
333 if(!ret) {
334 ret = SysAllocStringLen(NULL, 0);
335 if(!ret)
336 return E_OUTOFMEMORY;
339 V_VT(retv) = VT_BSTR;
340 V_BSTR(retv) = ret;
341 }else {
342 SysFreeString(ret);
345 return S_OK;
348 /* ECMA-262 3rd Edition 15.4.4.5 */
349 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
350 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
352 jsdisp_t *jsthis;
353 DWORD length;
354 HRESULT hres;
356 TRACE("\n");
358 hres = get_length(ctx, vthis, ei, &jsthis, &length);
359 if(FAILED(hres))
360 return hres;
362 if(arg_cnt(dp)) {
363 BSTR sep;
365 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
366 if(FAILED(hres))
367 return hres;
369 hres = array_join(ctx, jsthis, length, sep, retv, ei, caller);
371 SysFreeString(sep);
372 }else {
373 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei, caller);
376 return hres;
379 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
380 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
382 jsdisp_t *jsthis;
383 VARIANT val;
384 DWORD length;
385 HRESULT hres;
387 TRACE("\n");
389 hres = get_length(ctx, vthis, ei, &jsthis, &length);
390 if(FAILED(hres))
391 return hres;
393 if(!length) {
394 hres = set_length(jsthis, ei, 0);
395 if(FAILED(hres))
396 return hres;
398 if(retv)
399 V_VT(retv) = VT_EMPTY;
400 return S_OK;
403 length--;
404 hres = jsdisp_get_idx(jsthis, length, &val, ei, caller);
405 if(SUCCEEDED(hres)) {
406 hres = jsdisp_delete_idx(jsthis, length);
407 } else if(hres == DISP_E_UNKNOWNNAME) {
408 V_VT(&val) = VT_EMPTY;
409 hres = S_OK;
410 } else
411 return hres;
413 if(SUCCEEDED(hres))
414 hres = set_length(jsthis, ei, length);
416 if(FAILED(hres)) {
417 VariantClear(&val);
418 return hres;
421 if(retv)
422 *retv = val;
423 else
424 VariantClear(&val);
426 return S_OK;
429 /* ECMA-262 3rd Edition 15.4.4.7 */
430 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
431 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
433 jsdisp_t *jsthis;
434 DWORD length = 0;
435 int i, n;
436 HRESULT hres;
438 TRACE("\n");
440 hres = get_length(ctx, vthis, ei, &jsthis, &length);
441 if(FAILED(hres))
442 return hres;
444 n = arg_cnt(dp);
445 for(i=0; i < n; i++) {
446 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei, sp);
447 if(FAILED(hres))
448 return hres;
451 hres = set_length(jsthis, ei, length+n);
452 if(FAILED(hres))
453 return hres;
455 if(retv) {
456 V_VT(retv) = VT_I4;
457 V_I4(retv) = length+n;
459 return S_OK;
462 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
463 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
465 jsdisp_t *jsthis;
466 DWORD length, k, l;
467 VARIANT v1, v2;
468 HRESULT hres1, hres2;
470 TRACE("\n");
472 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
473 if(FAILED(hres1))
474 return hres1;
476 for(k=0; k<length/2; k++) {
477 l = length-k-1;
479 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei, sp);
480 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
481 return hres1;
483 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei, sp);
484 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
485 VariantClear(&v1);
486 return hres2;
489 if(hres1 == DISP_E_UNKNOWNNAME)
490 hres1 = jsdisp_delete_idx(jsthis, l);
491 else
492 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei, sp);
494 if(FAILED(hres1)) {
495 VariantClear(&v1);
496 VariantClear(&v2);
497 return hres1;
500 if(hres2 == DISP_E_UNKNOWNNAME)
501 hres2 = jsdisp_delete_idx(jsthis, k);
502 else
503 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei, sp);
505 if(FAILED(hres2)) {
506 VariantClear(&v2);
507 return hres2;
511 if(retv) {
512 jsdisp_addref(jsthis);
513 var_set_jsdisp(retv, jsthis);
516 return S_OK;
519 /* ECMA-262 3rd Edition 15.4.4.9 */
520 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
521 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
523 jsdisp_t *jsthis;
524 DWORD length = 0, i;
525 VARIANT v, ret;
526 HRESULT hres;
528 TRACE("\n");
530 hres = get_length(ctx, vthis, ei, &jsthis, &length);
531 if(FAILED(hres))
532 return hres;
534 if(!length) {
535 hres = set_length(jsthis, ei, 0);
536 if(FAILED(hres))
537 return hres;
540 if(!length) {
541 if(retv)
542 V_VT(retv) = VT_EMPTY;
543 return S_OK;
546 hres = jsdisp_get_idx(jsthis, 0, &ret, ei, caller);
547 if(hres == DISP_E_UNKNOWNNAME) {
548 V_VT(&ret) = VT_EMPTY;
549 hres = S_OK;
552 for(i=1; SUCCEEDED(hres) && i<length; i++) {
553 hres = jsdisp_get_idx(jsthis, i, &v, ei, caller);
554 if(hres == DISP_E_UNKNOWNNAME)
555 hres = jsdisp_delete_idx(jsthis, i-1);
556 else if(SUCCEEDED(hres))
557 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei, caller);
560 if(SUCCEEDED(hres)) {
561 hres = jsdisp_delete_idx(jsthis, length-1);
562 if(SUCCEEDED(hres))
563 hres = set_length(jsthis, ei, length-1);
566 if(SUCCEEDED(hres) && retv)
567 *retv = ret;
568 else
569 VariantClear(&ret);
570 return hres;
573 /* ECMA-262 3rd Edition 15.4.4.10 */
574 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
575 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
577 jsdisp_t *arr, *jsthis;
578 VARIANT v;
579 DOUBLE range;
580 DWORD length, start, end, idx;
581 HRESULT hres;
583 TRACE("\n");
585 hres = get_length(ctx, vthis, ei, &jsthis, &length);
586 if(FAILED(hres))
587 return hres;
589 if(arg_cnt(dp)) {
590 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
591 if(FAILED(hres))
592 return hres;
594 if(V_VT(&v) == VT_I4)
595 range = V_I4(&v);
596 else
597 range = floor(V_R8(&v));
599 if(-range>length || isnan(range)) start = 0;
600 else if(range < 0) start = range+length;
601 else if(range <= length) start = range;
602 else start = length;
604 else start = 0;
606 if(arg_cnt(dp)>1) {
607 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
608 if(FAILED(hres))
609 return hres;
611 if(V_VT(&v) == VT_I4)
612 range = V_I4(&v);
613 else
614 range = floor(V_R8(&v));
616 if(-range>length) end = 0;
617 else if(range < 0) end = range+length;
618 else if(range <= length) end = range;
619 else end = length;
621 else end = length;
623 hres = create_array(ctx, (end>start)?end-start:0, &arr);
624 if(FAILED(hres))
625 return hres;
627 for(idx=start; idx<end; idx++) {
628 hres = jsdisp_get_idx(jsthis, idx, &v, ei, sp);
629 if(hres == DISP_E_UNKNOWNNAME)
630 continue;
632 if(SUCCEEDED(hres)) {
633 hres = jsdisp_propput_idx(arr, idx-start, &v, ei, sp);
634 VariantClear(&v);
637 if(FAILED(hres)) {
638 jsdisp_release(arr);
639 return hres;
643 if(retv)
644 var_set_jsdisp(retv, arr);
645 else
646 jsdisp_release(arr);
648 return S_OK;
651 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
652 IServiceProvider *caller, INT *cmp)
654 HRESULT hres;
656 if(cmp_func) {
657 VARIANTARG args[2];
658 DISPPARAMS dp = {args, NULL, 2, 0};
659 VARIANT tmp;
660 VARIANT res;
662 args[0] = *v2;
663 args[1] = *v1;
665 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
666 if(FAILED(hres))
667 return hres;
669 hres = to_number(ctx, &res, ei, &tmp);
670 VariantClear(&res);
671 if(FAILED(hres))
672 return hres;
674 if(V_VT(&tmp) == VT_I4)
675 *cmp = V_I4(&tmp);
676 else
677 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
678 }else if(V_VT(v1) == VT_EMPTY) {
679 *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
680 }else if(V_VT(v2) == VT_EMPTY) {
681 *cmp = -1;
682 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
683 DOUBLE d = num_val(v1)-num_val(v2);
684 if(d > 0.0)
685 *cmp = 1;
686 else
687 *cmp = d < -0.0 ? -1 : 0;
688 }else {
689 BSTR x, y;
691 hres = to_string(ctx, v1, ei, &x);
692 if(FAILED(hres))
693 return hres;
695 hres = to_string(ctx, v2, ei, &y);
696 if(SUCCEEDED(hres)) {
697 *cmp = strcmpW(x, y);
698 SysFreeString(y);
700 SysFreeString(x);
701 if(FAILED(hres))
702 return hres;
705 return S_OK;
708 /* ECMA-262 3rd Edition 15.4.4.11 */
709 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
710 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
712 jsdisp_t *jsthis, *cmp_func = NULL;
713 VARIANT *vtab, **sorttab = NULL;
714 DWORD length;
715 DWORD i;
716 HRESULT hres = S_OK;
718 TRACE("\n");
720 hres = get_length(ctx, vthis, ei, &jsthis, &length);
721 if(FAILED(hres))
722 return hres;
724 if(arg_cnt(dp) > 1) {
725 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
726 return E_FAIL;
729 if(arg_cnt(dp) == 1) {
730 VARIANT *arg = get_arg(dp, 0);
732 if(V_VT(arg) != VT_DISPATCH) {
733 WARN("arg is not dispatch\n");
734 return E_FAIL;
738 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
739 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
740 WARN("cmp_func is not a function\n");
741 if(cmp_func)
742 jsdisp_release(cmp_func);
743 return E_FAIL;
747 if(!length) {
748 if(cmp_func)
749 jsdisp_release(cmp_func);
750 if(retv) {
751 jsdisp_addref(jsthis);
752 var_set_jsdisp(retv, jsthis);
754 return S_OK;
757 vtab = heap_alloc_zero(length * sizeof(VARIANT));
758 if(vtab) {
759 for(i=0; i<length; i++) {
760 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei, caller);
761 if(hres == DISP_E_UNKNOWNNAME) {
762 V_VT(vtab+i) = VT_EMPTY;
763 hres = S_OK;
764 } else if(FAILED(hres)) {
765 WARN("Could not get elem %d: %08x\n", i, hres);
766 break;
769 }else {
770 hres = E_OUTOFMEMORY;
773 if(SUCCEEDED(hres)) {
774 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
775 if(!sorttab)
776 hres = E_OUTOFMEMORY;
779 /* merge-sort */
780 if(SUCCEEDED(hres)) {
781 VARIANT *tmpv, **tmpbuf;
782 INT cmp;
784 tmpbuf = sorttab + length;
785 for(i=0; i < length; i++)
786 sorttab[i] = vtab+i;
788 for(i=0; i < length/2; i++) {
789 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
790 if(FAILED(hres))
791 break;
793 if(cmp < 0) {
794 tmpv = sorttab[2*i];
795 sorttab[2*i] = sorttab[2*i+1];
796 sorttab[2*i+1] = tmpv;
800 if(SUCCEEDED(hres)) {
801 DWORD k, a, b, bend;
803 for(k=2; k < length; k *= 2) {
804 for(i=0; i+k < length; i += 2*k) {
805 a = b = 0;
806 if(i+2*k <= length)
807 bend = k;
808 else
809 bend = length - (i+k);
811 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
813 while(a < k && b < bend) {
814 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
815 if(FAILED(hres))
816 break;
818 if(cmp < 0) {
819 sorttab[i+a+b] = tmpbuf[a];
820 a++;
821 }else {
822 sorttab[i+a+b] = sorttab[i+k+b];
823 b++;
827 if(FAILED(hres))
828 break;
830 if(a < k)
831 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
834 if(FAILED(hres))
835 break;
839 for(i=0; SUCCEEDED(hres) && i < length; i++)
840 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei, caller);
843 if(vtab) {
844 for(i=0; i < length; i++)
845 VariantClear(vtab+i);
846 heap_free(vtab);
848 heap_free(sorttab);
849 if(cmp_func)
850 jsdisp_release(cmp_func);
852 if(FAILED(hres))
853 return hres;
855 if(retv) {
856 jsdisp_addref(jsthis);
857 var_set_jsdisp(retv, jsthis);
860 return S_OK;
863 /* ECMA-262 3rd Edition 15.4.4.12 */
864 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
865 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
867 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
868 jsdisp_t *ret_array = NULL, *jsthis;
869 VARIANT v;
870 HRESULT hres = S_OK;
872 TRACE("\n");
874 hres = get_length(ctx, vthis, ei, &jsthis, &length);
875 if(FAILED(hres))
876 return hres;
878 argc = arg_cnt(dp);
879 if(argc >= 1) {
880 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
881 if(FAILED(hres))
882 return hres;
884 if(V_VT(&v) == VT_I4) {
885 if(V_I4(&v) >= 0)
886 start = min(V_I4(&v), length);
887 else
888 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
889 }else {
890 start = V_R8(&v) < 0.0 ? 0 : length;
894 if(argc >= 2) {
895 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
896 if(FAILED(hres))
897 return hres;
899 if(V_VT(&v) == VT_I4) {
900 if(V_I4(&v) > 0)
901 delete_cnt = min(V_I4(&v), length-start);
902 }else if(V_R8(&v) > 0.0) {
903 delete_cnt = length-start;
906 add_args = argc-2;
909 if(retv) {
910 hres = create_array(ctx, 0, &ret_array);
911 if(FAILED(hres))
912 return hres;
914 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
915 hres = jsdisp_get_idx(jsthis, start+i, &v, ei, caller);
916 if(hres == DISP_E_UNKNOWNNAME)
917 hres = S_OK;
918 else if(SUCCEEDED(hres))
919 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
922 if(SUCCEEDED(hres)) {
923 V_VT(&v) = VT_I4;
924 V_I4(&v) = delete_cnt;
926 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
930 if(add_args < delete_cnt) {
931 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
932 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei, caller);
933 if(hres == DISP_E_UNKNOWNNAME)
934 hres = jsdisp_delete_idx(jsthis, i+add_args);
935 else if(SUCCEEDED(hres))
936 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
939 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
940 hres = jsdisp_delete_idx(jsthis, i-1);
941 }else if(add_args > delete_cnt) {
942 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
943 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
944 if(hres == DISP_E_UNKNOWNNAME)
945 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
946 else if(SUCCEEDED(hres))
947 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
951 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
952 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
954 if(SUCCEEDED(hres)) {
955 V_VT(&v) = VT_I4;
956 V_I4(&v) = length-delete_cnt+add_args;
957 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
960 if(FAILED(hres)) {
961 if(ret_array)
962 jsdisp_release(ret_array);
963 return hres;
966 if(retv)
967 var_set_jsdisp(retv, ret_array);
968 return S_OK;
971 /* ECMA-262 3rd Edition 15.4.4.2 */
972 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
973 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
975 ArrayInstance *array;
977 TRACE("\n");
979 array = array_this(jsthis);
980 if(!array)
981 return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
983 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
986 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
987 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
989 FIXME("\n");
990 return E_NOTIMPL;
993 /* ECMA-262 3rd Edition 15.4.4.13 */
994 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
995 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
997 jsdisp_t *jsthis;
998 WCHAR buf[14], *buf_end, *str;
999 DWORD argc, i, length;
1000 VARIANT var;
1001 DISPID id;
1002 HRESULT hres;
1004 TRACE("\n");
1006 hres = get_length(ctx, vthis, ei, &jsthis, &length);
1007 if(FAILED(hres))
1008 return hres;
1010 argc = arg_cnt(dp);
1011 if(argc) {
1012 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1013 *buf_end-- = 0;
1014 i = length;
1016 while(i--) {
1017 str = idx_to_str(i, buf_end);
1019 hres = jsdisp_get_id(jsthis, str, 0, &id);
1020 if(SUCCEEDED(hres)) {
1021 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
1022 if(FAILED(hres))
1023 return hres;
1025 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
1026 VariantClear(&var);
1027 }else if(hres == DISP_E_UNKNOWNNAME) {
1028 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1032 if(FAILED(hres))
1033 return hres;
1036 for(i=0; i<argc; i++) {
1037 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
1038 if(FAILED(hres))
1039 return hres;
1042 if(argc) {
1043 length += argc;
1044 hres = set_length(jsthis, ei, length);
1045 if(FAILED(hres))
1046 return hres;
1049 if(retv) {
1050 if(ctx->version < 2) {
1051 V_VT(retv) = VT_EMPTY;
1052 }else {
1053 V_VT(retv) = VT_I4;
1054 V_I4(retv) = length;
1057 return S_OK;
1060 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1061 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1063 TRACE("\n");
1065 switch(flags) {
1066 case INVOKE_FUNC:
1067 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1068 case INVOKE_PROPERTYGET:
1069 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1070 default:
1071 FIXME("unimplemented flags %x\n", flags);
1072 return E_NOTIMPL;
1075 return S_OK;
1078 static void Array_destructor(jsdisp_t *dispex)
1080 heap_free(dispex);
1083 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1085 ArrayInstance *array = (ArrayInstance*)dispex;
1086 const WCHAR *ptr = name;
1087 DWORD id = 0;
1089 if(!isdigitW(*ptr))
1090 return;
1092 while(*ptr && isdigitW(*ptr)) {
1093 id = id*10 + (*ptr-'0');
1094 ptr++;
1097 if(*ptr)
1098 return;
1100 if(id >= array->length)
1101 array->length = id+1;
1104 static const builtin_prop_t Array_props[] = {
1105 {concatW, Array_concat, PROPF_METHOD|1},
1106 {joinW, Array_join, PROPF_METHOD|1},
1107 {lengthW, Array_length, 0},
1108 {popW, Array_pop, PROPF_METHOD},
1109 {pushW, Array_push, PROPF_METHOD|1},
1110 {reverseW, Array_reverse, PROPF_METHOD},
1111 {shiftW, Array_shift, PROPF_METHOD},
1112 {sliceW, Array_slice, PROPF_METHOD|2},
1113 {sortW, Array_sort, PROPF_METHOD|1},
1114 {spliceW, Array_splice, PROPF_METHOD|2},
1115 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1116 {toStringW, Array_toString, PROPF_METHOD},
1117 {unshiftW, Array_unshift, PROPF_METHOD|1},
1120 static const builtin_info_t Array_info = {
1121 JSCLASS_ARRAY,
1122 {NULL, Array_value, 0},
1123 sizeof(Array_props)/sizeof(*Array_props),
1124 Array_props,
1125 Array_destructor,
1126 Array_on_put
1129 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1130 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1132 jsdisp_t *obj;
1133 VARIANT *arg_var;
1134 DWORD i;
1135 HRESULT hres;
1137 TRACE("\n");
1139 switch(flags) {
1140 case DISPATCH_METHOD:
1141 case DISPATCH_CONSTRUCT: {
1142 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1143 if(V_I4(arg_var) < 0)
1144 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1146 hres = create_array(ctx, V_I4(arg_var), &obj);
1147 if(FAILED(hres))
1148 return hres;
1150 var_set_jsdisp(retv, obj);
1151 return S_OK;
1154 hres = create_array(ctx, arg_cnt(dp), &obj);
1155 if(FAILED(hres))
1156 return hres;
1158 for(i=0; i < arg_cnt(dp); i++) {
1159 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei, caller);
1160 if(FAILED(hres))
1161 break;
1163 if(FAILED(hres)) {
1164 jsdisp_release(obj);
1165 return hres;
1168 var_set_jsdisp(retv, obj);
1169 break;
1171 default:
1172 FIXME("unimplemented flags: %x\n", flags);
1173 return E_NOTIMPL;
1176 return S_OK;
1179 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1181 ArrayInstance *array;
1182 HRESULT hres;
1184 array = heap_alloc_zero(sizeof(ArrayInstance));
1185 if(!array)
1186 return E_OUTOFMEMORY;
1188 if(object_prototype)
1189 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1190 else
1191 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1193 if(FAILED(hres)) {
1194 heap_free(array);
1195 return hres;
1198 *ret = array;
1199 return S_OK;
1202 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1204 ArrayInstance *array;
1205 HRESULT hres;
1207 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1209 hres = alloc_array(ctx, object_prototype, &array);
1210 if(FAILED(hres))
1211 return hres;
1213 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1215 jsdisp_release(&array->dispex);
1216 return hres;
1219 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1221 ArrayInstance *array;
1222 HRESULT hres;
1224 hres = alloc_array(ctx, NULL, &array);
1225 if(FAILED(hres))
1226 return hres;
1228 array->length = length;
1230 *ret = &array->dispex;
1231 return S_OK;