jscript: Make Array.sort generic.
[wine] / dlls / jscript / array.c
1 /*
2  * Copyright 2008 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <math.h>
20
21 #include "jscript.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
26
27 typedef struct {
28     DispatchEx dispex;
29
30     DWORD length;
31 } ArrayInstance;
32
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};
46
47 static const WCHAR default_separatorW[] = {',',0};
48
49 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
50 {
51     return (ArrayInstance*)vdisp->u.jsdisp;
52 }
53
54 static inline ArrayInstance *array_this(vdisp_t *jsthis)
55 {
56     return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
57 }
58
59 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsexcept_t *ei, DispatchEx **jsthis, DWORD *ret)
60 {
61     ArrayInstance *array;
62     VARIANT var;
63     HRESULT hres;
64
65     array = array_this(vdisp);
66     if(array) {
67         *jsthis = &array->dispex;
68         *ret = array->length;
69         return S_OK;
70     }
71
72     if(!is_jsdisp(vdisp))
73         return throw_type_error(ctx, ei, IDS_JSCRIPT_EXPECTED, NULL);
74
75     hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei, NULL/*FIXME*/);
76     if(FAILED(hres))
77         return hres;
78
79     hres = to_uint32(ctx, &var, ei, ret);
80     VariantClear(&var);
81     if(FAILED(hres))
82         return hres;
83
84     *jsthis = vdisp->u.jsdisp;
85     return S_OK;
86 }
87
88 static HRESULT set_length(DispatchEx *obj, jsexcept_t *ei, DWORD length)
89 {
90     VARIANT var;
91
92     if(is_class(obj, JSCLASS_ARRAY)) {
93         ((ArrayInstance*)obj)->length = length;
94         return S_OK;
95     }
96
97     V_VT(&var) = VT_I4;
98     V_I4(&var) = length;
99     return jsdisp_propput_name(obj, lengthW, &var, ei, NULL/*FIXME*/);
100 }
101
102 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
103 {
104     if(!idx) {
105         *ptr = '0';
106         return ptr;
107     }
108
109     while(idx) {
110         *ptr-- = '0' + (idx%10);
111         idx /= 10;
112     }
113
114     return ptr+1;
115 }
116
117 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
118         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
119 {
120     ArrayInstance *This = array_from_vdisp(jsthis);
121
122     TRACE("%p %d\n", This, This->length);
123
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;
134
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));
140
141         if(len!=(DWORD)len)
142             return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
143
144         for(i=len; i<This->length; i++) {
145             hres = jsdisp_delete_idx(&This->dispex, i);
146             if(FAILED(hres))
147                 return hres;
148         }
149
150         This->length = len;
151         break;
152     }
153     default:
154         FIXME("unimplemented flags %x\n", flags);
155         return E_NOTIMPL;
156     }
157
158     return S_OK;
159 }
160
161 static HRESULT concat_array(DispatchEx *array, ArrayInstance *obj, DWORD *len,
162         jsexcept_t *ei, IServiceProvider *caller)
163 {
164     VARIANT var;
165     DWORD i;
166     HRESULT hres;
167
168     for(i=0; i < obj->length; i++) {
169         hres = jsdisp_propget_idx(&obj->dispex, i, &var, ei, caller);
170         if(hres == DISP_E_UNKNOWNNAME)
171             continue;
172         if(FAILED(hres))
173             return hres;
174
175         hres = jsdisp_propput_idx(array, *len+i, &var, ei, caller);
176         VariantClear(&var);
177         if(FAILED(hres))
178             return hres;
179     }
180
181     *len += obj->length;
182     return S_OK;
183 }
184
185 static HRESULT concat_obj(DispatchEx *array, IDispatch *obj, DWORD *len, jsexcept_t *ei, IServiceProvider *caller)
186 {
187     DispatchEx *jsobj;
188     VARIANT var;
189     HRESULT hres;
190
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;
197         }
198         jsdisp_release(jsobj);
199     }
200
201     V_VT(&var) = VT_DISPATCH;
202     V_DISPATCH(&var) = obj;
203     return jsdisp_propput_idx(array, (*len)++, &var, ei, caller);
204 }
205
206 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
207         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
208 {
209     DispatchEx *ret;
210     DWORD len = 0;
211     HRESULT hres;
212
213     TRACE("\n");
214
215     hres = create_array(ctx, 0, &ret);
216     if(FAILED(hres))
217         return hres;
218
219     hres = concat_obj(ret, jsthis->u.disp, &len, ei, caller);
220     if(SUCCEEDED(hres)) {
221         VARIANT *arg;
222         DWORD i;
223
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;
232         }
233     }
234
235     if(FAILED(hres))
236         return hres;
237
238     if(retv) {
239         V_VT(retv) = VT_DISPATCH;
240         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
241     }else {
242         jsdisp_release(ret);
243     }
244     return S_OK;
245 }
246
247 static HRESULT array_join(script_ctx_t *ctx, DispatchEx *array, DWORD length, const WCHAR *sep, VARIANT *retv,
248         jsexcept_t *ei, IServiceProvider *caller)
249 {
250     BSTR *str_tab, ret = NULL;
251     VARIANT var;
252     DWORD i;
253     HRESULT hres = E_FAIL;
254
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;
261         }
262         return S_OK;
263     }
264
265     str_tab = heap_alloc_zero(length * sizeof(BSTR));
266     if(!str_tab)
267         return E_OUTOFMEMORY;
268
269     for(i=0; i < length; i++) {
270         hres = jsdisp_propget_idx(array, i, &var, ei, caller);
271         if(FAILED(hres))
272             break;
273
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;
279     }
280
281     if(SUCCEEDED(hres)) {
282         DWORD seplen = 0, len = 0;
283         WCHAR *ptr;
284
285         seplen = strlenW(sep);
286
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]);
291
292         ret = SysAllocStringLen(NULL, len);
293         if(ret) {
294             DWORD tmplen = 0;
295
296             if(str_tab[0]) {
297                 tmplen = SysStringLen(str_tab[0]);
298                 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
299             }
300
301             ptr = ret + tmplen;
302             for(i=1; i < length; i++) {
303                 if(seplen) {
304                     memcpy(ptr, sep, seplen*sizeof(WCHAR));
305                     ptr += seplen;
306                 }
307
308                 if(str_tab[i]) {
309                     tmplen = SysStringLen(str_tab[i]);
310                     memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
311                     ptr += tmplen;
312                 }
313             }
314             *ptr=0;
315         }else {
316             hres = E_OUTOFMEMORY;
317         }
318     }
319
320     for(i=0; i < length; i++)
321         SysFreeString(str_tab[i]);
322     heap_free(str_tab);
323     if(FAILED(hres))
324         return hres;
325
326     TRACE("= %s\n", debugstr_w(ret));
327
328     if(retv) {
329         if(!ret) {
330             ret = SysAllocStringLen(NULL, 0);
331             if(!ret)
332                 return E_OUTOFMEMORY;
333         }
334
335         V_VT(retv) = VT_BSTR;
336         V_BSTR(retv) = ret;
337     }else {
338         SysFreeString(ret);
339     }
340
341     return S_OK;
342 }
343
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, IServiceProvider *caller)
347 {
348     DispatchEx *jsthis;
349     DWORD length;
350     HRESULT hres;
351
352     TRACE("\n");
353
354     hres = get_length(ctx, vthis, ei, &jsthis, &length);
355     if(FAILED(hres))
356         return hres;
357
358     if(arg_cnt(dp)) {
359         BSTR sep;
360
361         hres = to_string(ctx, get_arg(dp,0), ei, &sep);
362         if(FAILED(hres))
363             return hres;
364
365         hres = array_join(ctx, jsthis, length, sep, retv, ei, caller);
366
367         SysFreeString(sep);
368     }else {
369         hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei, caller);
370     }
371
372     return hres;
373 }
374
375 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
376         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
377 {
378     DispatchEx *jsthis;
379     VARIANT val;
380     DWORD length;
381     HRESULT hres;
382
383     TRACE("\n");
384
385     hres = get_length(ctx, vthis, ei, &jsthis, &length);
386     if(FAILED(hres))
387         return hres;
388
389     if(!length) {
390         hres = set_length(jsthis, ei, 0);
391         if(FAILED(hres))
392             return hres;
393
394         if(retv)
395             V_VT(retv) = VT_EMPTY;
396         return S_OK;
397     }
398
399     length--;
400     hres = jsdisp_propget_idx(jsthis, length, &val, ei, caller);
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;
408
409     if(SUCCEEDED(hres))
410         hres = set_length(jsthis, ei, length);
411
412     if(FAILED(hres)) {
413         VariantClear(&val);
414         return hres;
415     }
416
417     if(retv)
418         *retv = val;
419     else
420         VariantClear(&val);
421
422     return S_OK;
423 }
424
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, IServiceProvider *sp)
428 {
429     DispatchEx *jsthis;
430     DWORD length = 0;
431     int i, n;
432     HRESULT hres;
433
434     TRACE("\n");
435
436     hres = get_length(ctx, vthis, ei, &jsthis, &length);
437     if(FAILED(hres))
438         return hres;
439
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, sp);
443         if(FAILED(hres))
444             return hres;
445     }
446
447     hres = set_length(jsthis, ei, length+n);
448     if(FAILED(hres))
449         return hres;
450
451     if(retv) {
452         V_VT(retv) = VT_I4;
453         V_I4(retv) = length+n;
454     }
455     return S_OK;
456 }
457
458 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
459         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
460 {
461     DispatchEx *jsthis;
462     DWORD length, k, l;
463     VARIANT v1, v2;
464     HRESULT hres1, hres2;
465
466     TRACE("\n");
467
468     hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
469     if(FAILED(hres1))
470         return hres1;
471
472     for(k=0; k<length/2; k++) {
473         l = length-k-1;
474
475         hres1 = jsdisp_propget_idx(jsthis, k, &v1, ei, sp);
476         hres2 = jsdisp_propget_idx(jsthis, l, &v2, ei, sp);
477
478         if(hres1 == DISP_E_UNKNOWNNAME)
479             jsdisp_delete_idx(jsthis, l);
480         else
481             jsdisp_propput_idx(jsthis, l, &v1, ei, sp);
482
483         if(hres2 == DISP_E_UNKNOWNNAME)
484             jsdisp_delete_idx(jsthis, k);
485         else
486             jsdisp_propput_idx(jsthis, k, &v2, ei, sp);
487     }
488
489     if(retv) {
490         V_VT(retv) = VT_DISPATCH;
491         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
492         IDispatch_AddRef(V_DISPATCH(retv));
493     }
494
495     return S_OK;
496 }
497
498 /* ECMA-262 3rd Edition    15.4.4.9 */
499 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
500         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
501 {
502     DispatchEx *jsthis;
503     DWORD length = 0, i;
504     VARIANT v, ret;
505     HRESULT hres;
506
507     TRACE("\n");
508
509     hres = get_length(ctx, vthis, ei, &jsthis, &length);
510     if(FAILED(hres))
511         return hres;
512
513     if(!length) {
514         hres = set_length(jsthis, ei, 0);
515         if(FAILED(hres))
516             return hres;
517     }
518
519     if(!length) {
520         if(retv)
521             V_VT(retv) = VT_EMPTY;
522         return S_OK;
523     }
524
525     hres = jsdisp_propget_idx(jsthis, 0, &ret, ei, caller);
526     if(hres == DISP_E_UNKNOWNNAME) {
527         V_VT(&ret) = VT_EMPTY;
528         hres = S_OK;
529     }
530
531     for(i=1; SUCCEEDED(hres) && i<length; i++) {
532         hres = jsdisp_propget_idx(jsthis, i, &v, ei, caller);
533         if(hres == DISP_E_UNKNOWNNAME)
534             hres = jsdisp_delete_idx(jsthis, i-1);
535         else if(SUCCEEDED(hres))
536             hres = jsdisp_propput_idx(jsthis, i-1, &v, ei, caller);
537     }
538
539     if(SUCCEEDED(hres)) {
540         hres = jsdisp_delete_idx(jsthis, length-1);
541         if(SUCCEEDED(hres))
542             hres = set_length(jsthis, ei, length-1);
543     }
544
545     if(SUCCEEDED(hres) && retv)
546         *retv = ret;
547     else
548         VariantClear(&ret);
549     return hres;
550 }
551
552 /* ECMA-262 3rd Edition    15.4.4.10 */
553 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
554         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
555 {
556     DispatchEx *arr, *jsthis;
557     VARIANT v;
558     DOUBLE range;
559     DWORD length, start, end, idx;
560     HRESULT hres;
561
562     TRACE("\n");
563
564     hres = get_length(ctx, vthis, ei, &jsthis, &length);
565     if(FAILED(hres))
566         return hres;
567
568     if(arg_cnt(dp)) {
569         hres = to_number(ctx, get_arg(dp, 0), ei, &v);
570         if(FAILED(hres))
571             return hres;
572
573         if(V_VT(&v) == VT_I4)
574             range = V_I4(&v);
575         else
576             range = floor(V_R8(&v));
577
578         if(-range>length || isnan(range)) start = 0;
579         else if(range < 0) start = range+length;
580         else if(range <= length) start = range;
581         else start = length;
582     }
583     else start = 0;
584
585     if(arg_cnt(dp)>1) {
586         hres = to_number(ctx, get_arg(dp, 1), ei, &v);
587         if(FAILED(hres))
588             return hres;
589
590         if(V_VT(&v) == VT_I4)
591             range = V_I4(&v);
592         else
593             range = floor(V_R8(&v));
594
595         if(-range>length) end = 0;
596         else if(range < 0) end = range+length;
597         else if(range <= length) end = range;
598         else end = length;
599     }
600     else end = length;
601
602     hres = create_array(ctx, (end>start)?end-start:0, &arr);
603     if(FAILED(hres))
604         return hres;
605
606     for(idx=start; idx<end; idx++) {
607         hres = jsdisp_propget_idx(jsthis, idx, &v, ei, sp);
608         if(hres == DISP_E_UNKNOWNNAME)
609             continue;
610
611         if(SUCCEEDED(hres)) {
612             hres = jsdisp_propput_idx(arr, idx-start, &v, ei, sp);
613             VariantClear(&v);
614         }
615
616         if(FAILED(hres)) {
617             jsdisp_release(arr);
618             return hres;
619         }
620     }
621
622     if(retv) {
623         V_VT(retv) = VT_DISPATCH;
624         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
625     }
626     else
627         jsdisp_release(arr);
628
629     return S_OK;
630 }
631
632 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
633         IServiceProvider *caller, INT *cmp)
634 {
635     HRESULT hres;
636
637     if(cmp_func) {
638         VARIANTARG args[2];
639         DISPPARAMS dp = {args, NULL, 2, 0};
640         VARIANT tmp;
641         VARIANT res;
642
643         args[0] = *v2;
644         args[1] = *v1;
645
646         hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
647         if(FAILED(hres))
648             return hres;
649
650         hres = to_number(ctx, &res, ei, &tmp);
651         VariantClear(&res);
652         if(FAILED(hres))
653             return hres;
654
655         if(V_VT(&tmp) == VT_I4)
656             *cmp = V_I4(&tmp);
657         else
658             *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
659     }else if(is_num_vt(V_VT(v1))) {
660         if(is_num_vt(V_VT(v2))) {
661             DOUBLE d = num_val(v1)-num_val(v2);
662             if(d > 0.0)
663                 *cmp = 1;
664             else if(d < -0.0)
665                 *cmp = -1;
666             else
667                 *cmp = 0;
668         }else {
669             *cmp = -1;
670         }
671     }else if(is_num_vt(V_VT(v2))) {
672         *cmp = 1;
673     }else if(V_VT(v1) == VT_BSTR) {
674         if(V_VT(v2) == VT_BSTR)
675             *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
676         else
677             *cmp = -1;
678     }else if(V_VT(v2) == VT_BSTR) {
679         *cmp = 1;
680     }else {
681         *cmp = 0;
682     }
683
684     return S_OK;
685 }
686
687 /* ECMA-262 3rd Edition    15.4.4.11 */
688 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
689         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
690 {
691     DispatchEx *jsthis, *cmp_func = NULL;
692     VARIANT *vtab, **sorttab = NULL;
693     DWORD length;
694     DWORD i;
695     HRESULT hres = S_OK;
696
697     TRACE("\n");
698
699     hres = get_length(ctx, vthis, ei, &jsthis, &length);
700     if(FAILED(hres))
701         return hres;
702
703     if(arg_cnt(dp) > 1) {
704         WARN("invalid arg_cnt %d\n", arg_cnt(dp));
705         return E_FAIL;
706     }
707
708     if(arg_cnt(dp) == 1) {
709         VARIANT *arg = get_arg(dp, 0);
710
711         if(V_VT(arg) != VT_DISPATCH) {
712             WARN("arg is not dispatch\n");
713             return E_FAIL;
714         }
715
716
717         cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
718         if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
719             WARN("cmp_func is not a function\n");
720             if(cmp_func)
721                 jsdisp_release(cmp_func);
722             return E_FAIL;
723         }
724     }
725
726     if(!length) {
727         if(cmp_func)
728             jsdisp_release(cmp_func);
729         if(retv) {
730             V_VT(retv) = VT_DISPATCH;
731             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
732             IDispatch_AddRef(V_DISPATCH(retv));
733         }
734         return S_OK;
735     }
736
737     vtab = heap_alloc_zero(length * sizeof(VARIANT));
738     if(vtab) {
739         for(i=0; i<length; i++) {
740             hres = jsdisp_propget_idx(jsthis, i, vtab+i, ei, caller);
741             if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
742                 WARN("Could not get elem %d: %08x\n", i, hres);
743                 break;
744             }
745         }
746     }else {
747         hres = E_OUTOFMEMORY;
748     }
749
750     if(SUCCEEDED(hres)) {
751         sorttab = heap_alloc(length*2*sizeof(VARIANT*));
752         if(!sorttab)
753             hres = E_OUTOFMEMORY;
754     }
755
756     /* merge-sort */
757     if(SUCCEEDED(hres)) {
758         VARIANT *tmpv, **tmpbuf;
759         INT cmp;
760
761         tmpbuf = sorttab + length;
762         for(i=0; i < length; i++)
763             sorttab[i] = vtab+i;
764
765         for(i=0; i < length/2; i++) {
766             hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
767             if(FAILED(hres))
768                 break;
769
770             if(cmp < 0) {
771                 tmpv = sorttab[2*i];
772                 sorttab[2*i] = sorttab[2*i+1];
773                 sorttab[2*i+1] = tmpv;
774             }
775         }
776
777         if(SUCCEEDED(hres)) {
778             DWORD k, a, b, bend;
779
780             for(k=2; k < length; k *= 2) {
781                 for(i=0; i+k < length; i += 2*k) {
782                     a = b = 0;
783                     if(i+2*k <= length)
784                         bend = k;
785                     else
786                         bend = length - (i+k);
787
788                     memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
789
790                     while(a < k && b < bend) {
791                         hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
792                         if(FAILED(hres))
793                             break;
794
795                         if(cmp < 0) {
796                             sorttab[i+a+b] = tmpbuf[a];
797                             a++;
798                         }else {
799                             sorttab[i+a+b] = sorttab[i+k+b];
800                             b++;
801                         }
802                     }
803
804                     if(FAILED(hres))
805                         break;
806
807                     if(a < k)
808                         memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
809                 }
810
811                 if(FAILED(hres))
812                     break;
813             }
814         }
815
816         for(i=0; SUCCEEDED(hres) && i < length; i++)
817             hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei, caller);
818     }
819
820     if(vtab) {
821         for(i=0; i < length; i++)
822             VariantClear(vtab+i);
823         heap_free(vtab);
824     }
825     heap_free(sorttab);
826     if(cmp_func)
827         jsdisp_release(cmp_func);
828
829     if(FAILED(hres))
830         return hres;
831
832     if(retv) {
833         V_VT(retv) = VT_DISPATCH;
834         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
835         IDispatch_AddRef(V_DISPATCH(retv));
836     }
837
838     return S_OK;
839 }
840
841 /* ECMA-262 3rd Edition    15.4.4.12 */
842 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
843         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
844 {
845     DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
846     DispatchEx *ret_array = NULL, *jsthis;
847     VARIANT v;
848     HRESULT hres = S_OK;
849
850     TRACE("\n");
851
852     hres = get_length(ctx, vthis, ei, &jsthis, &length);
853     if(FAILED(hres))
854         return hres;
855
856     argc = arg_cnt(dp);
857     if(argc >= 1) {
858         hres = to_integer(ctx, get_arg(dp,0), ei, &v);
859         if(FAILED(hres))
860             return hres;
861
862         if(V_VT(&v) == VT_I4) {
863             if(V_I4(&v) >= 0)
864                 start = min(V_I4(&v), length);
865             else
866                 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
867         }else {
868             start = V_R8(&v) < 0.0 ? 0 : length;
869         }
870     }
871
872     if(argc >= 2) {
873         hres = to_integer(ctx, get_arg(dp,1), ei, &v);
874         if(FAILED(hres))
875             return hres;
876
877         if(V_VT(&v) == VT_I4) {
878             if(V_I4(&v) > 0)
879                 delete_cnt = min(V_I4(&v), length-start);
880         }else if(V_R8(&v) > 0.0) {
881             delete_cnt = length-start;
882         }
883
884         add_args = argc-2;
885     }
886
887     if(retv) {
888         hres = create_array(ctx, 0, &ret_array);
889         if(FAILED(hres))
890             return hres;
891
892         for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
893             hres = jsdisp_propget_idx(jsthis, start+i, &v, ei, caller);
894             if(hres == DISP_E_UNKNOWNNAME)
895                 hres = S_OK;
896             else if(SUCCEEDED(hres))
897                 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
898         }
899
900         if(SUCCEEDED(hres)) {
901             V_VT(&v) = VT_I4;
902             V_I4(&v) = delete_cnt;
903
904             hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
905         }
906     }
907
908     if(add_args < delete_cnt) {
909         for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
910             hres = jsdisp_propget_idx(jsthis, i+delete_cnt, &v, ei, caller);
911             if(hres == DISP_E_UNKNOWNNAME)
912                 hres = jsdisp_delete_idx(jsthis, i+add_args);
913             else if(SUCCEEDED(hres))
914                 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
915         }
916
917         for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
918             hres = jsdisp_delete_idx(jsthis, i-1);
919     }else if(add_args > delete_cnt) {
920         for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
921             hres = jsdisp_propget_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
922             if(hres == DISP_E_UNKNOWNNAME)
923                 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
924             else if(SUCCEEDED(hres))
925                 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
926         }
927     }
928
929     for(i=0; SUCCEEDED(hres) && i < add_args; i++)
930         hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
931
932     if(SUCCEEDED(hres)) {
933         V_VT(&v) = VT_I4;
934         V_I4(&v) = length-delete_cnt+add_args;
935         hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
936     }
937
938     if(FAILED(hres)) {
939         if(ret_array)
940             jsdisp_release(ret_array);
941         return hres;
942     }
943
944     if(retv) {
945         V_VT(retv) = VT_DISPATCH;
946         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret_array);
947     }
948     return S_OK;
949 }
950
951 /* ECMA-262 3rd Edition    15.4.4.2 */
952 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
953         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
954 {
955     ArrayInstance *array;
956
957     TRACE("\n");
958
959     array = array_this(jsthis);
960     if(!array)
961         return throw_type_error(ctx, ei, IDS_ARRAY_EXPECTED, NULL);
962
963     return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
964 }
965
966 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
967         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
968 {
969     FIXME("\n");
970     return E_NOTIMPL;
971 }
972
973 /* ECMA-262 3rd Edition    15.4.4.13 */
974 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
975         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
976 {
977     DispatchEx *jsthis;
978     WCHAR buf[14], *buf_end, *str;
979     DWORD argc, i, length;
980     VARIANT var;
981     DISPID id;
982     HRESULT hres;
983
984     TRACE("\n");
985
986     hres = get_length(ctx, vthis, ei, &jsthis, &length);
987     if(FAILED(hres))
988         return hres;
989
990     argc = arg_cnt(dp);
991     if(argc) {
992         buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
993         *buf_end-- = 0;
994         i = length;
995
996         while(i--) {
997             str = idx_to_str(i, buf_end);
998
999             hres = jsdisp_get_id(jsthis, str, 0, &id);
1000             if(SUCCEEDED(hres)) {
1001                 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
1002                 if(FAILED(hres))
1003                     return hres;
1004
1005                 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
1006                 VariantClear(&var);
1007             }else if(hres == DISP_E_UNKNOWNNAME) {
1008                 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1009             }
1010         }
1011
1012         if(FAILED(hres))
1013             return hres;
1014     }
1015
1016     for(i=0; i<argc; i++) {
1017         hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
1018         if(FAILED(hres))
1019             return hres;
1020     }
1021
1022     if(argc) {
1023         length += argc;
1024         hres = set_length(jsthis, ei, length);
1025         if(FAILED(hres))
1026             return hres;
1027     }
1028
1029     if(retv) {
1030         if(ctx->version < 2) {
1031             V_VT(retv) = VT_EMPTY;
1032         }else {
1033             V_VT(retv) = VT_I4;
1034             V_I4(retv) = length;
1035         }
1036     }
1037     return S_OK;
1038 }
1039
1040 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1041         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1042 {
1043     TRACE("\n");
1044
1045     switch(flags) {
1046     case INVOKE_FUNC:
1047         return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1048     case INVOKE_PROPERTYGET:
1049         return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1050     default:
1051         FIXME("unimplemented flags %x\n", flags);
1052         return E_NOTIMPL;
1053     }
1054
1055     return S_OK;
1056 }
1057
1058 static void Array_destructor(DispatchEx *dispex)
1059 {
1060     heap_free(dispex);
1061 }
1062
1063 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
1064 {
1065     ArrayInstance *array = (ArrayInstance*)dispex;
1066     const WCHAR *ptr = name;
1067     DWORD id = 0;
1068
1069     if(!isdigitW(*ptr))
1070         return;
1071
1072     while(*ptr && isdigitW(*ptr)) {
1073         id = id*10 + (*ptr-'0');
1074         ptr++;
1075     }
1076
1077     if(*ptr)
1078         return;
1079
1080     if(id >= array->length)
1081         array->length = id+1;
1082 }
1083
1084 static const builtin_prop_t Array_props[] = {
1085     {concatW,                Array_concat,               PROPF_METHOD|1},
1086     {joinW,                  Array_join,                 PROPF_METHOD|1},
1087     {lengthW,                Array_length,               0},
1088     {popW,                   Array_pop,                  PROPF_METHOD},
1089     {pushW,                  Array_push,                 PROPF_METHOD|1},
1090     {reverseW,               Array_reverse,              PROPF_METHOD},
1091     {shiftW,                 Array_shift,                PROPF_METHOD},
1092     {sliceW,                 Array_slice,                PROPF_METHOD|2},
1093     {sortW,                  Array_sort,                 PROPF_METHOD|1},
1094     {spliceW,                Array_splice,               PROPF_METHOD|2},
1095     {toLocaleStringW,        Array_toLocaleString,       PROPF_METHOD},
1096     {toStringW,              Array_toString,             PROPF_METHOD},
1097     {unshiftW,               Array_unshift,              PROPF_METHOD|1},
1098 };
1099
1100 static const builtin_info_t Array_info = {
1101     JSCLASS_ARRAY,
1102     {NULL, Array_value, 0},
1103     sizeof(Array_props)/sizeof(*Array_props),
1104     Array_props,
1105     Array_destructor,
1106     Array_on_put
1107 };
1108
1109 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1110         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1111 {
1112     DispatchEx *obj;
1113     VARIANT *arg_var;
1114     DWORD i;
1115     HRESULT hres;
1116
1117     TRACE("\n");
1118
1119     switch(flags) {
1120     case DISPATCH_METHOD:
1121     case DISPATCH_CONSTRUCT: {
1122         if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1123             if(V_I4(arg_var) < 0)
1124                 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
1125
1126             hres = create_array(ctx, V_I4(arg_var), &obj);
1127             if(FAILED(hres))
1128                 return hres;
1129
1130             V_VT(retv) = VT_DISPATCH;
1131             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1132             return S_OK;
1133         }
1134
1135         hres = create_array(ctx, arg_cnt(dp), &obj);
1136         if(FAILED(hres))
1137             return hres;
1138
1139         for(i=0; i < arg_cnt(dp); i++) {
1140             hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei, caller);
1141             if(FAILED(hres))
1142                 break;
1143         }
1144         if(FAILED(hres)) {
1145             jsdisp_release(obj);
1146             return hres;
1147         }
1148
1149         V_VT(retv) = VT_DISPATCH;
1150         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1151         break;
1152     }
1153     default:
1154         FIXME("unimplemented flags: %x\n", flags);
1155         return E_NOTIMPL;
1156     }
1157
1158     return S_OK;
1159 }
1160
1161 static HRESULT alloc_array(script_ctx_t *ctx, DispatchEx *object_prototype, ArrayInstance **ret)
1162 {
1163     ArrayInstance *array;
1164     HRESULT hres;
1165
1166     array = heap_alloc_zero(sizeof(ArrayInstance));
1167     if(!array)
1168         return E_OUTOFMEMORY;
1169
1170     if(object_prototype)
1171         hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1172     else
1173         hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1174
1175     if(FAILED(hres)) {
1176         heap_free(array);
1177         return hres;
1178     }
1179
1180     *ret = array;
1181     return S_OK;
1182 }
1183
1184 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1185 {
1186     ArrayInstance *array;
1187     HRESULT hres;
1188
1189     static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1190
1191     hres = alloc_array(ctx, object_prototype, &array);
1192     if(FAILED(hres))
1193         return hres;
1194
1195     hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1196
1197     jsdisp_release(&array->dispex);
1198     return hres;
1199 }
1200
1201 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
1202 {
1203     ArrayInstance *array;
1204     HRESULT hres;
1205
1206     hres = alloc_array(ctx, NULL, &array);
1207     if(FAILED(hres))
1208         return hres;
1209
1210     array->length = length;
1211
1212     *ret = &array->dispex;
1213     return S_OK;
1214 }