jscript: Skip tests on old jscript.dll.
[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         if(FAILED(hres1))
477             return hres1;
478
479         hres2 = jsdisp_propget_idx(jsthis, l, &v2, ei, sp);
480         if(FAILED(hres2)) {
481             VariantClear(&v1);
482             return hres2;
483         }
484
485         if(hres1 == DISP_E_UNKNOWNNAME)
486             hres1 = jsdisp_delete_idx(jsthis, l);
487         else
488             hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei, sp);
489
490         if(FAILED(hres1)) {
491             VariantClear(&v1);
492             VariantClear(&v2);
493             return hres1;
494         }
495
496         if(hres2 == DISP_E_UNKNOWNNAME)
497             hres2 = jsdisp_delete_idx(jsthis, k);
498         else
499             hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei, sp);
500
501         if(FAILED(hres2)) {
502             VariantClear(&v2);
503             return hres2;
504         }
505     }
506
507     if(retv) {
508         V_VT(retv) = VT_DISPATCH;
509         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
510         IDispatch_AddRef(V_DISPATCH(retv));
511     }
512
513     return S_OK;
514 }
515
516 /* ECMA-262 3rd Edition    15.4.4.9 */
517 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
518         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
519 {
520     DispatchEx *jsthis;
521     DWORD length = 0, i;
522     VARIANT v, ret;
523     HRESULT hres;
524
525     TRACE("\n");
526
527     hres = get_length(ctx, vthis, ei, &jsthis, &length);
528     if(FAILED(hres))
529         return hres;
530
531     if(!length) {
532         hres = set_length(jsthis, ei, 0);
533         if(FAILED(hres))
534             return hres;
535     }
536
537     if(!length) {
538         if(retv)
539             V_VT(retv) = VT_EMPTY;
540         return S_OK;
541     }
542
543     hres = jsdisp_propget_idx(jsthis, 0, &ret, ei, caller);
544     if(hres == DISP_E_UNKNOWNNAME) {
545         V_VT(&ret) = VT_EMPTY;
546         hres = S_OK;
547     }
548
549     for(i=1; SUCCEEDED(hres) && i<length; i++) {
550         hres = jsdisp_propget_idx(jsthis, i, &v, ei, caller);
551         if(hres == DISP_E_UNKNOWNNAME)
552             hres = jsdisp_delete_idx(jsthis, i-1);
553         else if(SUCCEEDED(hres))
554             hres = jsdisp_propput_idx(jsthis, i-1, &v, ei, caller);
555     }
556
557     if(SUCCEEDED(hres)) {
558         hres = jsdisp_delete_idx(jsthis, length-1);
559         if(SUCCEEDED(hres))
560             hres = set_length(jsthis, ei, length-1);
561     }
562
563     if(SUCCEEDED(hres) && retv)
564         *retv = ret;
565     else
566         VariantClear(&ret);
567     return hres;
568 }
569
570 /* ECMA-262 3rd Edition    15.4.4.10 */
571 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
572         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
573 {
574     DispatchEx *arr, *jsthis;
575     VARIANT v;
576     DOUBLE range;
577     DWORD length, start, end, idx;
578     HRESULT hres;
579
580     TRACE("\n");
581
582     hres = get_length(ctx, vthis, ei, &jsthis, &length);
583     if(FAILED(hres))
584         return hres;
585
586     if(arg_cnt(dp)) {
587         hres = to_number(ctx, get_arg(dp, 0), ei, &v);
588         if(FAILED(hres))
589             return hres;
590
591         if(V_VT(&v) == VT_I4)
592             range = V_I4(&v);
593         else
594             range = floor(V_R8(&v));
595
596         if(-range>length || isnan(range)) start = 0;
597         else if(range < 0) start = range+length;
598         else if(range <= length) start = range;
599         else start = length;
600     }
601     else start = 0;
602
603     if(arg_cnt(dp)>1) {
604         hres = to_number(ctx, get_arg(dp, 1), ei, &v);
605         if(FAILED(hres))
606             return hres;
607
608         if(V_VT(&v) == VT_I4)
609             range = V_I4(&v);
610         else
611             range = floor(V_R8(&v));
612
613         if(-range>length) end = 0;
614         else if(range < 0) end = range+length;
615         else if(range <= length) end = range;
616         else end = length;
617     }
618     else end = length;
619
620     hres = create_array(ctx, (end>start)?end-start:0, &arr);
621     if(FAILED(hres))
622         return hres;
623
624     for(idx=start; idx<end; idx++) {
625         hres = jsdisp_propget_idx(jsthis, idx, &v, ei, sp);
626         if(hres == DISP_E_UNKNOWNNAME)
627             continue;
628
629         if(SUCCEEDED(hres)) {
630             hres = jsdisp_propput_idx(arr, idx-start, &v, ei, sp);
631             VariantClear(&v);
632         }
633
634         if(FAILED(hres)) {
635             jsdisp_release(arr);
636             return hres;
637         }
638     }
639
640     if(retv) {
641         V_VT(retv) = VT_DISPATCH;
642         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
643     }
644     else
645         jsdisp_release(arr);
646
647     return S_OK;
648 }
649
650 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
651         IServiceProvider *caller, INT *cmp)
652 {
653     HRESULT hres;
654
655     if(cmp_func) {
656         VARIANTARG args[2];
657         DISPPARAMS dp = {args, NULL, 2, 0};
658         VARIANT tmp;
659         VARIANT res;
660
661         args[0] = *v2;
662         args[1] = *v1;
663
664         hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
665         if(FAILED(hres))
666             return hres;
667
668         hres = to_number(ctx, &res, ei, &tmp);
669         VariantClear(&res);
670         if(FAILED(hres))
671             return hres;
672
673         if(V_VT(&tmp) == VT_I4)
674             *cmp = V_I4(&tmp);
675         else
676             *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
677     }else if(is_num_vt(V_VT(v1))) {
678         if(is_num_vt(V_VT(v2))) {
679             DOUBLE d = num_val(v1)-num_val(v2);
680             if(d > 0.0)
681                 *cmp = 1;
682             else if(d < -0.0)
683                 *cmp = -1;
684             else
685                 *cmp = 0;
686         }else {
687             *cmp = -1;
688         }
689     }else if(is_num_vt(V_VT(v2))) {
690         *cmp = 1;
691     }else if(V_VT(v1) == VT_BSTR) {
692         if(V_VT(v2) == VT_BSTR)
693             *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
694         else
695             *cmp = -1;
696     }else if(V_VT(v2) == VT_BSTR) {
697         *cmp = 1;
698     }else {
699         *cmp = 0;
700     }
701
702     return S_OK;
703 }
704
705 /* ECMA-262 3rd Edition    15.4.4.11 */
706 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
707         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
708 {
709     DispatchEx *jsthis, *cmp_func = NULL;
710     VARIANT *vtab, **sorttab = NULL;
711     DWORD length;
712     DWORD i;
713     HRESULT hres = S_OK;
714
715     TRACE("\n");
716
717     hres = get_length(ctx, vthis, ei, &jsthis, &length);
718     if(FAILED(hres))
719         return hres;
720
721     if(arg_cnt(dp) > 1) {
722         WARN("invalid arg_cnt %d\n", arg_cnt(dp));
723         return E_FAIL;
724     }
725
726     if(arg_cnt(dp) == 1) {
727         VARIANT *arg = get_arg(dp, 0);
728
729         if(V_VT(arg) != VT_DISPATCH) {
730             WARN("arg is not dispatch\n");
731             return E_FAIL;
732         }
733
734
735         cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
736         if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
737             WARN("cmp_func is not a function\n");
738             if(cmp_func)
739                 jsdisp_release(cmp_func);
740             return E_FAIL;
741         }
742     }
743
744     if(!length) {
745         if(cmp_func)
746             jsdisp_release(cmp_func);
747         if(retv) {
748             V_VT(retv) = VT_DISPATCH;
749             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
750             IDispatch_AddRef(V_DISPATCH(retv));
751         }
752         return S_OK;
753     }
754
755     vtab = heap_alloc_zero(length * sizeof(VARIANT));
756     if(vtab) {
757         for(i=0; i<length; i++) {
758             hres = jsdisp_propget_idx(jsthis, i, vtab+i, ei, caller);
759             if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
760                 WARN("Could not get elem %d: %08x\n", i, hres);
761                 break;
762             }
763         }
764     }else {
765         hres = E_OUTOFMEMORY;
766     }
767
768     if(SUCCEEDED(hres)) {
769         sorttab = heap_alloc(length*2*sizeof(VARIANT*));
770         if(!sorttab)
771             hres = E_OUTOFMEMORY;
772     }
773
774     /* merge-sort */
775     if(SUCCEEDED(hres)) {
776         VARIANT *tmpv, **tmpbuf;
777         INT cmp;
778
779         tmpbuf = sorttab + length;
780         for(i=0; i < length; i++)
781             sorttab[i] = vtab+i;
782
783         for(i=0; i < length/2; i++) {
784             hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
785             if(FAILED(hres))
786                 break;
787
788             if(cmp < 0) {
789                 tmpv = sorttab[2*i];
790                 sorttab[2*i] = sorttab[2*i+1];
791                 sorttab[2*i+1] = tmpv;
792             }
793         }
794
795         if(SUCCEEDED(hres)) {
796             DWORD k, a, b, bend;
797
798             for(k=2; k < length; k *= 2) {
799                 for(i=0; i+k < length; i += 2*k) {
800                     a = b = 0;
801                     if(i+2*k <= length)
802                         bend = k;
803                     else
804                         bend = length - (i+k);
805
806                     memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
807
808                     while(a < k && b < bend) {
809                         hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
810                         if(FAILED(hres))
811                             break;
812
813                         if(cmp < 0) {
814                             sorttab[i+a+b] = tmpbuf[a];
815                             a++;
816                         }else {
817                             sorttab[i+a+b] = sorttab[i+k+b];
818                             b++;
819                         }
820                     }
821
822                     if(FAILED(hres))
823                         break;
824
825                     if(a < k)
826                         memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
827                 }
828
829                 if(FAILED(hres))
830                     break;
831             }
832         }
833
834         for(i=0; SUCCEEDED(hres) && i < length; i++)
835             hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei, caller);
836     }
837
838     if(vtab) {
839         for(i=0; i < length; i++)
840             VariantClear(vtab+i);
841         heap_free(vtab);
842     }
843     heap_free(sorttab);
844     if(cmp_func)
845         jsdisp_release(cmp_func);
846
847     if(FAILED(hres))
848         return hres;
849
850     if(retv) {
851         V_VT(retv) = VT_DISPATCH;
852         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsthis);
853         IDispatch_AddRef(V_DISPATCH(retv));
854     }
855
856     return S_OK;
857 }
858
859 /* ECMA-262 3rd Edition    15.4.4.12 */
860 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
861         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
862 {
863     DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
864     DispatchEx *ret_array = NULL, *jsthis;
865     VARIANT v;
866     HRESULT hres = S_OK;
867
868     TRACE("\n");
869
870     hres = get_length(ctx, vthis, ei, &jsthis, &length);
871     if(FAILED(hres))
872         return hres;
873
874     argc = arg_cnt(dp);
875     if(argc >= 1) {
876         hres = to_integer(ctx, get_arg(dp,0), ei, &v);
877         if(FAILED(hres))
878             return hres;
879
880         if(V_VT(&v) == VT_I4) {
881             if(V_I4(&v) >= 0)
882                 start = min(V_I4(&v), length);
883             else
884                 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
885         }else {
886             start = V_R8(&v) < 0.0 ? 0 : length;
887         }
888     }
889
890     if(argc >= 2) {
891         hres = to_integer(ctx, get_arg(dp,1), ei, &v);
892         if(FAILED(hres))
893             return hres;
894
895         if(V_VT(&v) == VT_I4) {
896             if(V_I4(&v) > 0)
897                 delete_cnt = min(V_I4(&v), length-start);
898         }else if(V_R8(&v) > 0.0) {
899             delete_cnt = length-start;
900         }
901
902         add_args = argc-2;
903     }
904
905     if(retv) {
906         hres = create_array(ctx, 0, &ret_array);
907         if(FAILED(hres))
908             return hres;
909
910         for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
911             hres = jsdisp_propget_idx(jsthis, start+i, &v, ei, caller);
912             if(hres == DISP_E_UNKNOWNNAME)
913                 hres = S_OK;
914             else if(SUCCEEDED(hres))
915                 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
916         }
917
918         if(SUCCEEDED(hres)) {
919             V_VT(&v) = VT_I4;
920             V_I4(&v) = delete_cnt;
921
922             hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
923         }
924     }
925
926     if(add_args < delete_cnt) {
927         for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
928             hres = jsdisp_propget_idx(jsthis, i+delete_cnt, &v, ei, caller);
929             if(hres == DISP_E_UNKNOWNNAME)
930                 hres = jsdisp_delete_idx(jsthis, i+add_args);
931             else if(SUCCEEDED(hres))
932                 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
933         }
934
935         for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
936             hres = jsdisp_delete_idx(jsthis, i-1);
937     }else if(add_args > delete_cnt) {
938         for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
939             hres = jsdisp_propget_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
940             if(hres == DISP_E_UNKNOWNNAME)
941                 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
942             else if(SUCCEEDED(hres))
943                 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
944         }
945     }
946
947     for(i=0; SUCCEEDED(hres) && i < add_args; i++)
948         hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
949
950     if(SUCCEEDED(hres)) {
951         V_VT(&v) = VT_I4;
952         V_I4(&v) = length-delete_cnt+add_args;
953         hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
954     }
955
956     if(FAILED(hres)) {
957         if(ret_array)
958             jsdisp_release(ret_array);
959         return hres;
960     }
961
962     if(retv) {
963         V_VT(retv) = VT_DISPATCH;
964         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret_array);
965     }
966     return S_OK;
967 }
968
969 /* ECMA-262 3rd Edition    15.4.4.2 */
970 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
971         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
972 {
973     ArrayInstance *array;
974
975     TRACE("\n");
976
977     array = array_this(jsthis);
978     if(!array)
979         return throw_type_error(ctx, ei, IDS_ARRAY_EXPECTED, NULL);
980
981     return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
982 }
983
984 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
985         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
986 {
987     FIXME("\n");
988     return E_NOTIMPL;
989 }
990
991 /* ECMA-262 3rd Edition    15.4.4.13 */
992 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
993         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
994 {
995     DispatchEx *jsthis;
996     WCHAR buf[14], *buf_end, *str;
997     DWORD argc, i, length;
998     VARIANT var;
999     DISPID id;
1000     HRESULT hres;
1001
1002     TRACE("\n");
1003
1004     hres = get_length(ctx, vthis, ei, &jsthis, &length);
1005     if(FAILED(hres))
1006         return hres;
1007
1008     argc = arg_cnt(dp);
1009     if(argc) {
1010         buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1011         *buf_end-- = 0;
1012         i = length;
1013
1014         while(i--) {
1015             str = idx_to_str(i, buf_end);
1016
1017             hres = jsdisp_get_id(jsthis, str, 0, &id);
1018             if(SUCCEEDED(hres)) {
1019                 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
1020                 if(FAILED(hres))
1021                     return hres;
1022
1023                 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
1024                 VariantClear(&var);
1025             }else if(hres == DISP_E_UNKNOWNNAME) {
1026                 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1027             }
1028         }
1029
1030         if(FAILED(hres))
1031             return hres;
1032     }
1033
1034     for(i=0; i<argc; i++) {
1035         hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
1036         if(FAILED(hres))
1037             return hres;
1038     }
1039
1040     if(argc) {
1041         length += argc;
1042         hres = set_length(jsthis, ei, length);
1043         if(FAILED(hres))
1044             return hres;
1045     }
1046
1047     if(retv) {
1048         if(ctx->version < 2) {
1049             V_VT(retv) = VT_EMPTY;
1050         }else {
1051             V_VT(retv) = VT_I4;
1052             V_I4(retv) = length;
1053         }
1054     }
1055     return S_OK;
1056 }
1057
1058 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1059         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1060 {
1061     TRACE("\n");
1062
1063     switch(flags) {
1064     case INVOKE_FUNC:
1065         return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1066     case INVOKE_PROPERTYGET:
1067         return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1068     default:
1069         FIXME("unimplemented flags %x\n", flags);
1070         return E_NOTIMPL;
1071     }
1072
1073     return S_OK;
1074 }
1075
1076 static void Array_destructor(DispatchEx *dispex)
1077 {
1078     heap_free(dispex);
1079 }
1080
1081 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
1082 {
1083     ArrayInstance *array = (ArrayInstance*)dispex;
1084     const WCHAR *ptr = name;
1085     DWORD id = 0;
1086
1087     if(!isdigitW(*ptr))
1088         return;
1089
1090     while(*ptr && isdigitW(*ptr)) {
1091         id = id*10 + (*ptr-'0');
1092         ptr++;
1093     }
1094
1095     if(*ptr)
1096         return;
1097
1098     if(id >= array->length)
1099         array->length = id+1;
1100 }
1101
1102 static const builtin_prop_t Array_props[] = {
1103     {concatW,                Array_concat,               PROPF_METHOD|1},
1104     {joinW,                  Array_join,                 PROPF_METHOD|1},
1105     {lengthW,                Array_length,               0},
1106     {popW,                   Array_pop,                  PROPF_METHOD},
1107     {pushW,                  Array_push,                 PROPF_METHOD|1},
1108     {reverseW,               Array_reverse,              PROPF_METHOD},
1109     {shiftW,                 Array_shift,                PROPF_METHOD},
1110     {sliceW,                 Array_slice,                PROPF_METHOD|2},
1111     {sortW,                  Array_sort,                 PROPF_METHOD|1},
1112     {spliceW,                Array_splice,               PROPF_METHOD|2},
1113     {toLocaleStringW,        Array_toLocaleString,       PROPF_METHOD},
1114     {toStringW,              Array_toString,             PROPF_METHOD},
1115     {unshiftW,               Array_unshift,              PROPF_METHOD|1},
1116 };
1117
1118 static const builtin_info_t Array_info = {
1119     JSCLASS_ARRAY,
1120     {NULL, Array_value, 0},
1121     sizeof(Array_props)/sizeof(*Array_props),
1122     Array_props,
1123     Array_destructor,
1124     Array_on_put
1125 };
1126
1127 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1128         VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1129 {
1130     DispatchEx *obj;
1131     VARIANT *arg_var;
1132     DWORD i;
1133     HRESULT hres;
1134
1135     TRACE("\n");
1136
1137     switch(flags) {
1138     case DISPATCH_METHOD:
1139     case DISPATCH_CONSTRUCT: {
1140         if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1141             if(V_I4(arg_var) < 0)
1142                 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
1143
1144             hres = create_array(ctx, V_I4(arg_var), &obj);
1145             if(FAILED(hres))
1146                 return hres;
1147
1148             V_VT(retv) = VT_DISPATCH;
1149             V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1150             return S_OK;
1151         }
1152
1153         hres = create_array(ctx, arg_cnt(dp), &obj);
1154         if(FAILED(hres))
1155             return hres;
1156
1157         for(i=0; i < arg_cnt(dp); i++) {
1158             hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei, caller);
1159             if(FAILED(hres))
1160                 break;
1161         }
1162         if(FAILED(hres)) {
1163             jsdisp_release(obj);
1164             return hres;
1165         }
1166
1167         V_VT(retv) = VT_DISPATCH;
1168         V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1169         break;
1170     }
1171     default:
1172         FIXME("unimplemented flags: %x\n", flags);
1173         return E_NOTIMPL;
1174     }
1175
1176     return S_OK;
1177 }
1178
1179 static HRESULT alloc_array(script_ctx_t *ctx, DispatchEx *object_prototype, ArrayInstance **ret)
1180 {
1181     ArrayInstance *array;
1182     HRESULT hres;
1183
1184     array = heap_alloc_zero(sizeof(ArrayInstance));
1185     if(!array)
1186         return E_OUTOFMEMORY;
1187
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);
1192
1193     if(FAILED(hres)) {
1194         heap_free(array);
1195         return hres;
1196     }
1197
1198     *ret = array;
1199     return S_OK;
1200 }
1201
1202 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1203 {
1204     ArrayInstance *array;
1205     HRESULT hres;
1206
1207     static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1208
1209     hres = alloc_array(ctx, object_prototype, &array);
1210     if(FAILED(hres))
1211         return hres;
1212
1213     hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1214
1215     jsdisp_release(&array->dispex);
1216     return hres;
1217 }
1218
1219 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
1220 {
1221     ArrayInstance *array;
1222     HRESULT hres;
1223
1224     hres = alloc_array(ctx, NULL, &array);
1225     if(FAILED(hres))
1226         return hres;
1227
1228     array->length = length;
1229
1230     *ret = &array->dispex;
1231     return S_OK;
1232 }