jscript: Add the Romanian translation.
[wine/testsucceed.git] / dlls / jscript / array.c
blob1268d59638bda8f1c601ea886fec99554f39f46b
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 DispatchEx 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, DispatchEx **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(DispatchEx *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(DispatchEx *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(DispatchEx *array, IDispatch *obj, DWORD *len, jsexcept_t *ei, IServiceProvider *caller)
187 DispatchEx *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 DispatchEx *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 V_VT(retv) = VT_DISPATCH;
240 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
241 }else {
242 jsdisp_release(ret);
244 return S_OK;
247 static HRESULT array_join(script_ctx_t *ctx, DispatchEx *array, DWORD length, const WCHAR *sep, VARIANT *retv,
248 jsexcept_t *ei, IServiceProvider *caller)
250 BSTR *str_tab, ret = NULL;
251 VARIANT var;
252 DWORD i;
253 HRESULT hres = E_FAIL;
255 if(!length) {
256 if(retv) {
257 V_VT(retv) = VT_BSTR;
258 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
259 if(!V_BSTR(retv))
260 return E_OUTOFMEMORY;
262 return S_OK;
265 str_tab = heap_alloc_zero(length * sizeof(BSTR));
266 if(!str_tab)
267 return E_OUTOFMEMORY;
269 for(i=0; i < length; i++) {
270 hres = jsdisp_get_idx(array, i, &var, ei, caller);
271 if(hres == DISP_E_UNKNOWNNAME) {
272 hres = S_OK;
273 continue;
274 } else if(FAILED(hres))
275 break;
277 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
278 hres = to_string(ctx, &var, ei, str_tab+i);
279 VariantClear(&var);
280 if(FAILED(hres))
281 break;
284 if(SUCCEEDED(hres)) {
285 DWORD seplen = 0, len = 0;
286 WCHAR *ptr;
288 seplen = strlenW(sep);
290 if(str_tab[0])
291 len = SysStringLen(str_tab[0]);
292 for(i=1; i < length; i++)
293 len += seplen + SysStringLen(str_tab[i]);
295 ret = SysAllocStringLen(NULL, len);
296 if(ret) {
297 DWORD tmplen = 0;
299 if(str_tab[0]) {
300 tmplen = SysStringLen(str_tab[0]);
301 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
304 ptr = ret + tmplen;
305 for(i=1; i < length; i++) {
306 if(seplen) {
307 memcpy(ptr, sep, seplen*sizeof(WCHAR));
308 ptr += seplen;
311 if(str_tab[i]) {
312 tmplen = SysStringLen(str_tab[i]);
313 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
314 ptr += tmplen;
317 *ptr=0;
318 }else {
319 hres = E_OUTOFMEMORY;
323 for(i=0; i < length; i++)
324 SysFreeString(str_tab[i]);
325 heap_free(str_tab);
326 if(FAILED(hres))
327 return hres;
329 TRACE("= %s\n", debugstr_w(ret));
331 if(retv) {
332 if(!ret) {
333 ret = SysAllocStringLen(NULL, 0);
334 if(!ret)
335 return E_OUTOFMEMORY;
338 V_VT(retv) = VT_BSTR;
339 V_BSTR(retv) = ret;
340 }else {
341 SysFreeString(ret);
344 return S_OK;
347 /* ECMA-262 3rd Edition 15.4.4.5 */
348 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
349 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
351 DispatchEx *jsthis;
352 DWORD length;
353 HRESULT hres;
355 TRACE("\n");
357 hres = get_length(ctx, vthis, ei, &jsthis, &length);
358 if(FAILED(hres))
359 return hres;
361 if(arg_cnt(dp)) {
362 BSTR sep;
364 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
365 if(FAILED(hres))
366 return hres;
368 hres = array_join(ctx, jsthis, length, sep, retv, ei, caller);
370 SysFreeString(sep);
371 }else {
372 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei, caller);
375 return hres;
378 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
379 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
381 DispatchEx *jsthis;
382 VARIANT val;
383 DWORD length;
384 HRESULT hres;
386 TRACE("\n");
388 hres = get_length(ctx, vthis, ei, &jsthis, &length);
389 if(FAILED(hres))
390 return hres;
392 if(!length) {
393 hres = set_length(jsthis, ei, 0);
394 if(FAILED(hres))
395 return hres;
397 if(retv)
398 V_VT(retv) = VT_EMPTY;
399 return S_OK;
402 length--;
403 hres = jsdisp_get_idx(jsthis, length, &val, ei, caller);
404 if(SUCCEEDED(hres)) {
405 hres = jsdisp_delete_idx(jsthis, length);
406 } else if(hres == DISP_E_UNKNOWNNAME) {
407 V_VT(&val) = VT_EMPTY;
408 hres = S_OK;
409 } else
410 return hres;
412 if(SUCCEEDED(hres))
413 hres = set_length(jsthis, ei, length);
415 if(FAILED(hres)) {
416 VariantClear(&val);
417 return hres;
420 if(retv)
421 *retv = val;
422 else
423 VariantClear(&val);
425 return S_OK;
428 /* ECMA-262 3rd Edition 15.4.4.7 */
429 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
430 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
432 DispatchEx *jsthis;
433 DWORD length = 0;
434 int i, n;
435 HRESULT hres;
437 TRACE("\n");
439 hres = get_length(ctx, vthis, ei, &jsthis, &length);
440 if(FAILED(hres))
441 return hres;
443 n = arg_cnt(dp);
444 for(i=0; i < n; i++) {
445 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei, sp);
446 if(FAILED(hres))
447 return hres;
450 hres = set_length(jsthis, ei, length+n);
451 if(FAILED(hres))
452 return hres;
454 if(retv) {
455 V_VT(retv) = VT_I4;
456 V_I4(retv) = length+n;
458 return S_OK;
461 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
462 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
464 DispatchEx *jsthis;
465 DWORD length, k, l;
466 VARIANT v1, v2;
467 HRESULT hres1, hres2;
469 TRACE("\n");
471 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
472 if(FAILED(hres1))
473 return hres1;
475 for(k=0; k<length/2; k++) {
476 l = length-k-1;
478 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei, sp);
479 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
480 return hres1;
482 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei, sp);
483 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
484 VariantClear(&v1);
485 return hres2;
488 if(hres1 == DISP_E_UNKNOWNNAME)
489 hres1 = jsdisp_delete_idx(jsthis, l);
490 else
491 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei, sp);
493 if(FAILED(hres1)) {
494 VariantClear(&v1);
495 VariantClear(&v2);
496 return hres1;
499 if(hres2 == DISP_E_UNKNOWNNAME)
500 hres2 = jsdisp_delete_idx(jsthis, k);
501 else
502 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei, sp);
504 if(FAILED(hres2)) {
505 VariantClear(&v2);
506 return hres2;
510 if(retv) {
511 V_VT(retv) = VT_DISPATCH;
512 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
513 IDispatch_AddRef(V_DISPATCH(retv));
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 DispatchEx *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 DispatchEx *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 V_VT(retv) = VT_DISPATCH;
645 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
647 else
648 jsdisp_release(arr);
650 return S_OK;
653 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
654 IServiceProvider *caller, INT *cmp)
656 HRESULT hres;
658 if(cmp_func) {
659 VARIANTARG args[2];
660 DISPPARAMS dp = {args, NULL, 2, 0};
661 VARIANT tmp;
662 VARIANT res;
664 args[0] = *v2;
665 args[1] = *v1;
667 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
668 if(FAILED(hres))
669 return hres;
671 hres = to_number(ctx, &res, ei, &tmp);
672 VariantClear(&res);
673 if(FAILED(hres))
674 return hres;
676 if(V_VT(&tmp) == VT_I4)
677 *cmp = V_I4(&tmp);
678 else
679 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
680 }else if(is_num_vt(V_VT(v1))) {
681 if(is_num_vt(V_VT(v2))) {
682 DOUBLE d = num_val(v1)-num_val(v2);
683 if(d > 0.0)
684 *cmp = 1;
685 else if(d < -0.0)
686 *cmp = -1;
687 else
688 *cmp = 0;
689 }else {
690 *cmp = -1;
692 }else if(is_num_vt(V_VT(v2))) {
693 *cmp = 1;
694 }else if(V_VT(v1) == VT_BSTR) {
695 if(V_VT(v2) == VT_BSTR)
696 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
697 else
698 *cmp = -1;
699 }else if(V_VT(v2) == VT_BSTR) {
700 *cmp = 1;
701 }else {
702 *cmp = 0;
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 DispatchEx *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 V_VT(retv) = VT_DISPATCH;
752 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
753 IDispatch_AddRef(V_DISPATCH(retv));
755 return S_OK;
758 vtab = heap_alloc_zero(length * sizeof(VARIANT));
759 if(vtab) {
760 for(i=0; i<length; i++) {
761 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei, caller);
762 if(hres == DISP_E_UNKNOWNNAME) {
763 V_VT(vtab+i) = VT_EMPTY;
764 hres = S_OK;
765 } else if(FAILED(hres)) {
766 WARN("Could not get elem %d: %08x\n", i, hres);
767 break;
770 }else {
771 hres = E_OUTOFMEMORY;
774 if(SUCCEEDED(hres)) {
775 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
776 if(!sorttab)
777 hres = E_OUTOFMEMORY;
780 /* merge-sort */
781 if(SUCCEEDED(hres)) {
782 VARIANT *tmpv, **tmpbuf;
783 INT cmp;
785 tmpbuf = sorttab + length;
786 for(i=0; i < length; i++)
787 sorttab[i] = vtab+i;
789 for(i=0; i < length/2; i++) {
790 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
791 if(FAILED(hres))
792 break;
794 if(cmp < 0) {
795 tmpv = sorttab[2*i];
796 sorttab[2*i] = sorttab[2*i+1];
797 sorttab[2*i+1] = tmpv;
801 if(SUCCEEDED(hres)) {
802 DWORD k, a, b, bend;
804 for(k=2; k < length; k *= 2) {
805 for(i=0; i+k < length; i += 2*k) {
806 a = b = 0;
807 if(i+2*k <= length)
808 bend = k;
809 else
810 bend = length - (i+k);
812 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
814 while(a < k && b < bend) {
815 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
816 if(FAILED(hres))
817 break;
819 if(cmp < 0) {
820 sorttab[i+a+b] = tmpbuf[a];
821 a++;
822 }else {
823 sorttab[i+a+b] = sorttab[i+k+b];
824 b++;
828 if(FAILED(hres))
829 break;
831 if(a < k)
832 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
835 if(FAILED(hres))
836 break;
840 for(i=0; SUCCEEDED(hres) && i < length; i++)
841 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei, caller);
844 if(vtab) {
845 for(i=0; i < length; i++)
846 VariantClear(vtab+i);
847 heap_free(vtab);
849 heap_free(sorttab);
850 if(cmp_func)
851 jsdisp_release(cmp_func);
853 if(FAILED(hres))
854 return hres;
856 if(retv) {
857 V_VT(retv) = VT_DISPATCH;
858 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
859 IDispatch_AddRef(V_DISPATCH(retv));
862 return S_OK;
865 /* ECMA-262 3rd Edition 15.4.4.12 */
866 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
867 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
869 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
870 DispatchEx *ret_array = NULL, *jsthis;
871 VARIANT v;
872 HRESULT hres = S_OK;
874 TRACE("\n");
876 hres = get_length(ctx, vthis, ei, &jsthis, &length);
877 if(FAILED(hres))
878 return hres;
880 argc = arg_cnt(dp);
881 if(argc >= 1) {
882 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
883 if(FAILED(hres))
884 return hres;
886 if(V_VT(&v) == VT_I4) {
887 if(V_I4(&v) >= 0)
888 start = min(V_I4(&v), length);
889 else
890 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
891 }else {
892 start = V_R8(&v) < 0.0 ? 0 : length;
896 if(argc >= 2) {
897 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
898 if(FAILED(hres))
899 return hres;
901 if(V_VT(&v) == VT_I4) {
902 if(V_I4(&v) > 0)
903 delete_cnt = min(V_I4(&v), length-start);
904 }else if(V_R8(&v) > 0.0) {
905 delete_cnt = length-start;
908 add_args = argc-2;
911 if(retv) {
912 hres = create_array(ctx, 0, &ret_array);
913 if(FAILED(hres))
914 return hres;
916 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
917 hres = jsdisp_get_idx(jsthis, start+i, &v, ei, caller);
918 if(hres == DISP_E_UNKNOWNNAME)
919 hres = S_OK;
920 else if(SUCCEEDED(hres))
921 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
924 if(SUCCEEDED(hres)) {
925 V_VT(&v) = VT_I4;
926 V_I4(&v) = delete_cnt;
928 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
932 if(add_args < delete_cnt) {
933 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
934 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei, caller);
935 if(hres == DISP_E_UNKNOWNNAME)
936 hres = jsdisp_delete_idx(jsthis, i+add_args);
937 else if(SUCCEEDED(hres))
938 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
941 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
942 hres = jsdisp_delete_idx(jsthis, i-1);
943 }else if(add_args > delete_cnt) {
944 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
945 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
946 if(hres == DISP_E_UNKNOWNNAME)
947 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
948 else if(SUCCEEDED(hres))
949 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
953 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
954 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
956 if(SUCCEEDED(hres)) {
957 V_VT(&v) = VT_I4;
958 V_I4(&v) = length-delete_cnt+add_args;
959 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
962 if(FAILED(hres)) {
963 if(ret_array)
964 jsdisp_release(ret_array);
965 return hres;
968 if(retv) {
969 V_VT(retv) = VT_DISPATCH;
970 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret_array);
972 return S_OK;
975 /* ECMA-262 3rd Edition 15.4.4.2 */
976 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
977 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
979 ArrayInstance *array;
981 TRACE("\n");
983 array = array_this(jsthis);
984 if(!array)
985 return throw_type_error(ctx, ei, IDS_ARRAY_EXPECTED, NULL);
987 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
990 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
991 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
993 FIXME("\n");
994 return E_NOTIMPL;
997 /* ECMA-262 3rd Edition 15.4.4.13 */
998 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
999 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1001 DispatchEx *jsthis;
1002 WCHAR buf[14], *buf_end, *str;
1003 DWORD argc, i, length;
1004 VARIANT var;
1005 DISPID id;
1006 HRESULT hres;
1008 TRACE("\n");
1010 hres = get_length(ctx, vthis, ei, &jsthis, &length);
1011 if(FAILED(hres))
1012 return hres;
1014 argc = arg_cnt(dp);
1015 if(argc) {
1016 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1017 *buf_end-- = 0;
1018 i = length;
1020 while(i--) {
1021 str = idx_to_str(i, buf_end);
1023 hres = jsdisp_get_id(jsthis, str, 0, &id);
1024 if(SUCCEEDED(hres)) {
1025 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
1026 if(FAILED(hres))
1027 return hres;
1029 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
1030 VariantClear(&var);
1031 }else if(hres == DISP_E_UNKNOWNNAME) {
1032 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1036 if(FAILED(hres))
1037 return hres;
1040 for(i=0; i<argc; i++) {
1041 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
1042 if(FAILED(hres))
1043 return hres;
1046 if(argc) {
1047 length += argc;
1048 hres = set_length(jsthis, ei, length);
1049 if(FAILED(hres))
1050 return hres;
1053 if(retv) {
1054 if(ctx->version < 2) {
1055 V_VT(retv) = VT_EMPTY;
1056 }else {
1057 V_VT(retv) = VT_I4;
1058 V_I4(retv) = length;
1061 return S_OK;
1064 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1065 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1067 TRACE("\n");
1069 switch(flags) {
1070 case INVOKE_FUNC:
1071 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1072 case INVOKE_PROPERTYGET:
1073 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1074 default:
1075 FIXME("unimplemented flags %x\n", flags);
1076 return E_NOTIMPL;
1079 return S_OK;
1082 static void Array_destructor(DispatchEx *dispex)
1084 heap_free(dispex);
1087 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
1089 ArrayInstance *array = (ArrayInstance*)dispex;
1090 const WCHAR *ptr = name;
1091 DWORD id = 0;
1093 if(!isdigitW(*ptr))
1094 return;
1096 while(*ptr && isdigitW(*ptr)) {
1097 id = id*10 + (*ptr-'0');
1098 ptr++;
1101 if(*ptr)
1102 return;
1104 if(id >= array->length)
1105 array->length = id+1;
1108 static const builtin_prop_t Array_props[] = {
1109 {concatW, Array_concat, PROPF_METHOD|1},
1110 {joinW, Array_join, PROPF_METHOD|1},
1111 {lengthW, Array_length, 0},
1112 {popW, Array_pop, PROPF_METHOD},
1113 {pushW, Array_push, PROPF_METHOD|1},
1114 {reverseW, Array_reverse, PROPF_METHOD},
1115 {shiftW, Array_shift, PROPF_METHOD},
1116 {sliceW, Array_slice, PROPF_METHOD|2},
1117 {sortW, Array_sort, PROPF_METHOD|1},
1118 {spliceW, Array_splice, PROPF_METHOD|2},
1119 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1120 {toStringW, Array_toString, PROPF_METHOD},
1121 {unshiftW, Array_unshift, PROPF_METHOD|1},
1124 static const builtin_info_t Array_info = {
1125 JSCLASS_ARRAY,
1126 {NULL, Array_value, 0},
1127 sizeof(Array_props)/sizeof(*Array_props),
1128 Array_props,
1129 Array_destructor,
1130 Array_on_put
1133 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1134 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1136 DispatchEx *obj;
1137 VARIANT *arg_var;
1138 DWORD i;
1139 HRESULT hres;
1141 TRACE("\n");
1143 switch(flags) {
1144 case DISPATCH_METHOD:
1145 case DISPATCH_CONSTRUCT: {
1146 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1147 if(V_I4(arg_var) < 0)
1148 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
1150 hres = create_array(ctx, V_I4(arg_var), &obj);
1151 if(FAILED(hres))
1152 return hres;
1154 V_VT(retv) = VT_DISPATCH;
1155 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1156 return S_OK;
1159 hres = create_array(ctx, arg_cnt(dp), &obj);
1160 if(FAILED(hres))
1161 return hres;
1163 for(i=0; i < arg_cnt(dp); i++) {
1164 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei, caller);
1165 if(FAILED(hres))
1166 break;
1168 if(FAILED(hres)) {
1169 jsdisp_release(obj);
1170 return hres;
1173 V_VT(retv) = VT_DISPATCH;
1174 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1175 break;
1177 default:
1178 FIXME("unimplemented flags: %x\n", flags);
1179 return E_NOTIMPL;
1182 return S_OK;
1185 static HRESULT alloc_array(script_ctx_t *ctx, DispatchEx *object_prototype, ArrayInstance **ret)
1187 ArrayInstance *array;
1188 HRESULT hres;
1190 array = heap_alloc_zero(sizeof(ArrayInstance));
1191 if(!array)
1192 return E_OUTOFMEMORY;
1194 if(object_prototype)
1195 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1196 else
1197 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1199 if(FAILED(hres)) {
1200 heap_free(array);
1201 return hres;
1204 *ret = array;
1205 return S_OK;
1208 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1210 ArrayInstance *array;
1211 HRESULT hres;
1213 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1215 hres = alloc_array(ctx, object_prototype, &array);
1216 if(FAILED(hres))
1217 return hres;
1219 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1221 jsdisp_release(&array->dispex);
1222 return hres;
1225 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
1227 ArrayInstance *array;
1228 HRESULT hres;
1230 hres = alloc_array(ctx, NULL, &array);
1231 if(FAILED(hres))
1232 return hres;
1234 array->length = length;
1236 *ret = &array->dispex;
1237 return S_OK;