jscript: Bettter handling of to_number result in String.indexOf.
[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 "config.h"
20 #include "wine/port.h"
21
22 #include <math.h>
23
24 #include "jscript.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
29
30 typedef struct {
31     jsdisp_t dispex;
32
33     DWORD length;
34 } ArrayInstance;
35
36 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
37 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
38 static const WCHAR joinW[] = {'j','o','i','n',0};
39 static const WCHAR popW[] = {'p','o','p',0};
40 static const WCHAR pushW[] = {'p','u','s','h',0};
41 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
42 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
43 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
44 static const WCHAR sortW[] = {'s','o','r','t',0};
45 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
46 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
47 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
48 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
49
50 static const WCHAR default_separatorW[] = {',',0};
51
52 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
53 {
54     return (ArrayInstance*)vdisp->u.jsdisp;
55 }
56
57 static inline ArrayInstance *array_this(vdisp_t *jsthis)
58 {
59     return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
60 }
61
62 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsexcept_t *ei, jsdisp_t **jsthis, DWORD *ret)
63 {
64     ArrayInstance *array;
65     VARIANT var;
66     HRESULT hres;
67
68     array = array_this(vdisp);
69     if(array) {
70         *jsthis = &array->dispex;
71         *ret = array->length;
72         return S_OK;
73     }
74
75     if(!is_jsdisp(vdisp))
76         return throw_type_error(ctx, ei, JS_E_JSCRIPT_EXPECTED, NULL);
77
78     hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei);
79     if(FAILED(hres))
80         return hres;
81
82     hres = to_uint32(ctx, &var, ei, ret);
83     VariantClear(&var);
84     if(FAILED(hres))
85         return hres;
86
87     *jsthis = vdisp->u.jsdisp;
88     return S_OK;
89 }
90
91 static HRESULT set_length(jsdisp_t *obj, jsexcept_t *ei, DWORD length)
92 {
93     VARIANT var;
94
95     if(is_class(obj, JSCLASS_ARRAY)) {
96         ((ArrayInstance*)obj)->length = length;
97         return S_OK;
98     }
99
100     V_VT(&var) = VT_I4;
101     V_I4(&var) = length;
102     return jsdisp_propput_name(obj, lengthW, &var, ei);
103 }
104
105 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
106 {
107     if(!idx) {
108         *ptr = '0';
109         return ptr;
110     }
111
112     while(idx) {
113         *ptr-- = '0' + (idx%10);
114         idx /= 10;
115     }
116
117     return ptr+1;
118 }
119
120 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
121         VARIANT *retv, jsexcept_t *ei)
122 {
123     ArrayInstance *This = array_from_vdisp(jsthis);
124
125     TRACE("%p %d\n", This, This->length);
126
127     switch(flags) {
128     case DISPATCH_PROPERTYGET:
129         V_VT(retv) = VT_I4;
130         V_I4(retv) = This->length;
131         break;
132     case DISPATCH_PROPERTYPUT: {
133         DOUBLE len = -1;
134         DWORD i;
135         HRESULT hres;
136
137         hres = to_number(ctx, get_arg(dp, 0), ei, &len);
138         if(FAILED(hres))
139             return hres;
140
141         len = floor(len);
142         if(len!=(DWORD)len)
143             return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
144
145         for(i=len; i<This->length; i++) {
146             hres = jsdisp_delete_idx(&This->dispex, i);
147             if(FAILED(hres))
148                 return hres;
149         }
150
151         This->length = len;
152         break;
153     }
154     default:
155         FIXME("unimplemented flags %x\n", flags);
156         return E_NOTIMPL;
157     }
158
159     return S_OK;
160 }
161
162 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
163 {
164     VARIANT var;
165     DWORD i;
166     HRESULT hres;
167
168     for(i=0; i < obj->length; i++) {
169         hres = jsdisp_get_idx(&obj->dispex, i, &var, ei);
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);
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(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
186 {
187     jsdisp_t *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);
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);
204 }
205
206 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
207         VARIANT *retv, jsexcept_t *ei)
208 {
209     jsdisp_t *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);
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);
228             else
229                 hres = jsdisp_propput_idx(ret, len++, arg, ei);
230             if(FAILED(hres))
231                 break;
232         }
233     }
234
235     if(FAILED(hres))
236         return hres;
237
238     if(retv)
239         var_set_jsdisp(retv, ret);
240     else
241         jsdisp_release(ret);
242     return S_OK;
243 }
244
245 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, VARIANT *retv, jsexcept_t *ei)
246 {
247     BSTR *str_tab, ret = NULL;
248     VARIANT var;
249     DWORD i;
250     HRESULT hres = E_FAIL;
251
252     if(!length) {
253         if(retv) {
254             V_VT(retv) = VT_BSTR;
255             V_BSTR(retv) = SysAllocStringLen(NULL, 0);
256             if(!V_BSTR(retv))
257                 return E_OUTOFMEMORY;
258         }
259         return S_OK;
260     }
261
262     str_tab = heap_alloc_zero(length * sizeof(BSTR));
263     if(!str_tab)
264         return E_OUTOFMEMORY;
265
266     for(i=0; i < length; i++) {
267         hres = jsdisp_get_idx(array, i, &var, ei);
268         if(hres == DISP_E_UNKNOWNNAME) {
269             hres = S_OK;
270             continue;
271         } else 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)
347 {
348     jsdisp_t *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);
366
367         SysFreeString(sep);
368     }else {
369         hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei);
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)
377 {
378     jsdisp_t *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_get_idx(jsthis, length, &val, ei);
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)
428 {
429     jsdisp_t *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);
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)
460 {
461     jsdisp_t *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_get_idx(jsthis, k, &v1, ei);
476         if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
477             return hres1;
478
479         hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
480         if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
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);
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);
500
501         if(FAILED(hres2)) {
502             VariantClear(&v2);
503             return hres2;
504         }
505     }
506
507     if(retv) {
508         jsdisp_addref(jsthis);
509         var_set_jsdisp(retv, jsthis);
510     }
511
512     return S_OK;
513 }
514
515 /* ECMA-262 3rd Edition    15.4.4.9 */
516 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
517         VARIANT *retv, jsexcept_t *ei)
518 {
519     jsdisp_t *jsthis;
520     DWORD length = 0, i;
521     VARIANT v, ret;
522     HRESULT hres;
523
524     TRACE("\n");
525
526     hres = get_length(ctx, vthis, ei, &jsthis, &length);
527     if(FAILED(hres))
528         return hres;
529
530     if(!length) {
531         hres = set_length(jsthis, ei, 0);
532         if(FAILED(hres))
533             return hres;
534     }
535
536     if(!length) {
537         if(retv)
538             V_VT(retv) = VT_EMPTY;
539         return S_OK;
540     }
541
542     hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
543     if(hres == DISP_E_UNKNOWNNAME) {
544         V_VT(&ret) = VT_EMPTY;
545         hres = S_OK;
546     }
547
548     for(i=1; SUCCEEDED(hres) && i<length; i++) {
549         hres = jsdisp_get_idx(jsthis, i, &v, ei);
550         if(hres == DISP_E_UNKNOWNNAME)
551             hres = jsdisp_delete_idx(jsthis, i-1);
552         else if(SUCCEEDED(hres))
553             hres = jsdisp_propput_idx(jsthis, i-1, &v, ei);
554     }
555
556     if(SUCCEEDED(hres)) {
557         hres = jsdisp_delete_idx(jsthis, length-1);
558         if(SUCCEEDED(hres))
559             hres = set_length(jsthis, ei, length-1);
560     }
561
562     if(SUCCEEDED(hres) && retv)
563         *retv = ret;
564     else
565         VariantClear(&ret);
566     return hres;
567 }
568
569 /* ECMA-262 3rd Edition    15.4.4.10 */
570 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
571         VARIANT *retv, jsexcept_t *ei)
572 {
573     jsdisp_t *arr, *jsthis;
574     DOUBLE range;
575     DWORD length, start, end, idx;
576     HRESULT hres;
577
578     TRACE("\n");
579
580     hres = get_length(ctx, vthis, ei, &jsthis, &length);
581     if(FAILED(hres))
582         return hres;
583
584     if(arg_cnt(dp)) {
585         hres = to_number(ctx, get_arg(dp, 0), ei, &range);
586         if(FAILED(hres))
587             return hres;
588
589         range = floor(range);
590         if(-range>length || isnan(range)) start = 0;
591         else if(range < 0) start = range+length;
592         else if(range <= length) start = range;
593         else start = length;
594     }
595     else start = 0;
596
597     if(arg_cnt(dp)>1) {
598         hres = to_number(ctx, get_arg(dp, 1), ei, &range);
599         if(FAILED(hres))
600             return hres;
601
602         range = floor(range);
603         if(-range>length) end = 0;
604         else if(range < 0) end = range+length;
605         else if(range <= length) end = range;
606         else end = length;
607     }
608     else end = length;
609
610     hres = create_array(ctx, (end>start)?end-start:0, &arr);
611     if(FAILED(hres))
612         return hres;
613
614     for(idx=start; idx<end; idx++) {
615         VARIANT v;
616
617         hres = jsdisp_get_idx(jsthis, idx, &v, ei);
618         if(hres == DISP_E_UNKNOWNNAME)
619             continue;
620
621         if(SUCCEEDED(hres)) {
622             hres = jsdisp_propput_idx(arr, idx-start, &v, ei);
623             VariantClear(&v);
624         }
625
626         if(FAILED(hres)) {
627             jsdisp_release(arr);
628             return hres;
629         }
630     }
631
632     if(retv)
633         var_set_jsdisp(retv, arr);
634     else
635         jsdisp_release(arr);
636
637     return S_OK;
638 }
639
640 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei, INT *cmp)
641 {
642     HRESULT hres;
643
644     if(cmp_func) {
645         VARIANTARG args[2];
646         DISPPARAMS dp = {args, NULL, 2, 0};
647         double n;
648         VARIANT res;
649
650         args[0] = *v2;
651         args[1] = *v1;
652
653         hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei);
654         if(FAILED(hres))
655             return hres;
656
657         hres = to_number(ctx, &res, ei, &n);
658         VariantClear(&res);
659         if(FAILED(hres))
660             return hres;
661
662         if(n == 0)
663             *cmp = 0;
664         *cmp = n > 0.0 ? 1 : -1;
665     }else if(V_VT(v1) == VT_EMPTY) {
666         *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
667     }else if(V_VT(v2) == VT_EMPTY) {
668         *cmp = -1;
669     }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
670         DOUBLE d = num_val(v1)-num_val(v2);
671         if(d > 0.0)
672             *cmp = 1;
673         else
674             *cmp = d < -0.0 ? -1 : 0;
675     }else {
676         BSTR x, y;
677
678         hres = to_string(ctx, v1, ei, &x);
679         if(FAILED(hres))
680             return hres;
681
682         hres = to_string(ctx, v2, ei, &y);
683         if(SUCCEEDED(hres)) {
684             *cmp = strcmpW(x, y);
685             SysFreeString(y);
686         }
687         SysFreeString(x);
688         if(FAILED(hres))
689             return hres;
690     }
691
692     return S_OK;
693 }
694
695 /* ECMA-262 3rd Edition    15.4.4.11 */
696 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
697         VARIANT *retv, jsexcept_t *ei)
698 {
699     jsdisp_t *jsthis, *cmp_func = NULL;
700     VARIANT *vtab, **sorttab = NULL;
701     DWORD length;
702     DWORD i;
703     HRESULT hres = S_OK;
704
705     TRACE("\n");
706
707     hres = get_length(ctx, vthis, ei, &jsthis, &length);
708     if(FAILED(hres))
709         return hres;
710
711     if(arg_cnt(dp) > 1) {
712         WARN("invalid arg_cnt %d\n", arg_cnt(dp));
713         return E_FAIL;
714     }
715
716     if(arg_cnt(dp) == 1) {
717         VARIANT *arg = get_arg(dp, 0);
718
719         if(V_VT(arg) != VT_DISPATCH) {
720             WARN("arg is not dispatch\n");
721             return E_FAIL;
722         }
723
724
725         cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
726         if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
727             WARN("cmp_func is not a function\n");
728             if(cmp_func)
729                 jsdisp_release(cmp_func);
730             return E_FAIL;
731         }
732     }
733
734     if(!length) {
735         if(cmp_func)
736             jsdisp_release(cmp_func);
737         if(retv) {
738             jsdisp_addref(jsthis);
739             var_set_jsdisp(retv, jsthis);
740         }
741         return S_OK;
742     }
743
744     vtab = heap_alloc_zero(length * sizeof(VARIANT));
745     if(vtab) {
746         for(i=0; i<length; i++) {
747             hres = jsdisp_get_idx(jsthis, i, vtab+i, ei);
748             if(hres == DISP_E_UNKNOWNNAME) {
749                 V_VT(vtab+i) = VT_EMPTY;
750                 hres = S_OK;
751             } else if(FAILED(hres)) {
752                 WARN("Could not get elem %d: %08x\n", i, hres);
753                 break;
754             }
755         }
756     }else {
757         hres = E_OUTOFMEMORY;
758     }
759
760     if(SUCCEEDED(hres)) {
761         sorttab = heap_alloc(length*2*sizeof(VARIANT*));
762         if(!sorttab)
763             hres = E_OUTOFMEMORY;
764     }
765
766     /* merge-sort */
767     if(SUCCEEDED(hres)) {
768         VARIANT *tmpv, **tmpbuf;
769         INT cmp;
770
771         tmpbuf = sorttab + length;
772         for(i=0; i < length; i++)
773             sorttab[i] = vtab+i;
774
775         for(i=0; i < length/2; i++) {
776             hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, &cmp);
777             if(FAILED(hres))
778                 break;
779
780             if(cmp < 0) {
781                 tmpv = sorttab[2*i];
782                 sorttab[2*i] = sorttab[2*i+1];
783                 sorttab[2*i+1] = tmpv;
784             }
785         }
786
787         if(SUCCEEDED(hres)) {
788             DWORD k, a, b, bend;
789
790             for(k=2; k < length; k *= 2) {
791                 for(i=0; i+k < length; i += 2*k) {
792                     a = b = 0;
793                     if(i+2*k <= length)
794                         bend = k;
795                     else
796                         bend = length - (i+k);
797
798                     memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
799
800                     while(a < k && b < bend) {
801                         hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, &cmp);
802                         if(FAILED(hres))
803                             break;
804
805                         if(cmp < 0) {
806                             sorttab[i+a+b] = tmpbuf[a];
807                             a++;
808                         }else {
809                             sorttab[i+a+b] = sorttab[i+k+b];
810                             b++;
811                         }
812                     }
813
814                     if(FAILED(hres))
815                         break;
816
817                     if(a < k)
818                         memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
819                 }
820
821                 if(FAILED(hres))
822                     break;
823             }
824         }
825
826         for(i=0; SUCCEEDED(hres) && i < length; i++)
827             hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei);
828     }
829
830     if(vtab) {
831         for(i=0; i < length; i++)
832             VariantClear(vtab+i);
833         heap_free(vtab);
834     }
835     heap_free(sorttab);
836     if(cmp_func)
837         jsdisp_release(cmp_func);
838
839     if(FAILED(hres))
840         return hres;
841
842     if(retv) {
843         jsdisp_addref(jsthis);
844         var_set_jsdisp(retv, jsthis);
845     }
846
847     return S_OK;
848 }
849
850 /* ECMA-262 3rd Edition    15.4.4.12 */
851 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
852         VARIANT *retv, jsexcept_t *ei)
853 {
854     DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
855     jsdisp_t *ret_array = NULL, *jsthis;
856     double d;
857     int n;
858     VARIANT v;
859     HRESULT hres = S_OK;
860
861     TRACE("\n");
862
863     hres = get_length(ctx, vthis, ei, &jsthis, &length);
864     if(FAILED(hres))
865         return hres;
866
867     argc = arg_cnt(dp);
868     if(argc >= 1) {
869         hres = to_integer(ctx, get_arg(dp,0), ei, &v);
870         if(FAILED(hres))
871             return hres;
872         d = num_val(&v);
873
874         if(is_int32(d)) {
875             if((n = d) >= 0)
876                 start = min(n, length);
877             else
878                 start = -n > length ? 0 : length + n;
879         }else {
880             start = d < 0.0 ? 0 : length;
881         }
882     }
883
884     if(argc >= 2) {
885         hres = to_integer(ctx, get_arg(dp,1), ei, &v);
886         if(FAILED(hres))
887             return hres;
888         d = num_val(&v);
889
890         if(is_int32(d)) {
891             if((n = d) > 0)
892                 delete_cnt = min(n, length-start);
893         }else if(d > 0.0) {
894             delete_cnt = length-start;
895         }
896
897         add_args = argc-2;
898     }
899
900     if(retv) {
901         hres = create_array(ctx, 0, &ret_array);
902         if(FAILED(hres))
903             return hres;
904
905         for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
906             hres = jsdisp_get_idx(jsthis, start+i, &v, ei);
907             if(hres == DISP_E_UNKNOWNNAME)
908                 hres = S_OK;
909             else if(SUCCEEDED(hres))
910                 hres = jsdisp_propput_idx(ret_array, i, &v, ei);
911         }
912
913         if(SUCCEEDED(hres)) {
914             V_VT(&v) = VT_I4;
915             V_I4(&v) = delete_cnt;
916
917             hres = jsdisp_propput_name(ret_array, lengthW, &v, ei);
918         }
919     }
920
921     if(add_args < delete_cnt) {
922         for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
923             hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei);
924             if(hres == DISP_E_UNKNOWNNAME)
925                 hres = jsdisp_delete_idx(jsthis, i+add_args);
926             else if(SUCCEEDED(hres))
927                 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei);
928         }
929
930         for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
931             hres = jsdisp_delete_idx(jsthis, i-1);
932     }else if(add_args > delete_cnt) {
933         for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
934             hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei);
935             if(hres == DISP_E_UNKNOWNNAME)
936                 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
937             else if(SUCCEEDED(hres))
938                 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei);
939         }
940     }
941
942     for(i=0; SUCCEEDED(hres) && i < add_args; i++)
943         hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei);
944
945     if(SUCCEEDED(hres)) {
946         V_VT(&v) = VT_I4;
947         V_I4(&v) = length-delete_cnt+add_args;
948         hres = jsdisp_propput_name(jsthis, lengthW, &v, ei);
949     }
950
951     if(FAILED(hres)) {
952         if(ret_array)
953             jsdisp_release(ret_array);
954         return hres;
955     }
956
957     if(retv)
958         var_set_jsdisp(retv, ret_array);
959     return S_OK;
960 }
961
962 /* ECMA-262 3rd Edition    15.4.4.2 */
963 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
964         VARIANT *retv, jsexcept_t *ei)
965 {
966     ArrayInstance *array;
967
968     TRACE("\n");
969
970     array = array_this(jsthis);
971     if(!array)
972         return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
973
974     return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei);
975 }
976
977 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
978         VARIANT *retv, jsexcept_t *ei)
979 {
980     FIXME("\n");
981     return E_NOTIMPL;
982 }
983
984 /* ECMA-262 3rd Edition    15.4.4.13 */
985 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
986         VARIANT *retv, jsexcept_t *ei)
987 {
988     jsdisp_t *jsthis;
989     WCHAR buf[14], *buf_end, *str;
990     DWORD argc, i, length;
991     VARIANT var;
992     DISPID id;
993     HRESULT hres;
994
995     TRACE("\n");
996
997     hres = get_length(ctx, vthis, ei, &jsthis, &length);
998     if(FAILED(hres))
999         return hres;
1000
1001     argc = arg_cnt(dp);
1002     if(argc) {
1003         buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1004         *buf_end-- = 0;
1005         i = length;
1006
1007         while(i--) {
1008             str = idx_to_str(i, buf_end);
1009
1010             hres = jsdisp_get_id(jsthis, str, 0, &id);
1011             if(SUCCEEDED(hres)) {
1012                 hres = jsdisp_propget(jsthis, id, &var, ei);
1013                 if(FAILED(hres))
1014                     return hres;
1015
1016                 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei);
1017                 VariantClear(&var);
1018             }else if(hres == DISP_E_UNKNOWNNAME) {
1019                 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1020             }
1021         }
1022
1023         if(FAILED(hres))
1024             return hres;
1025     }
1026
1027     for(i=0; i<argc; i++) {
1028         hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei);
1029         if(FAILED(hres))
1030             return hres;
1031     }
1032
1033     if(argc) {
1034         length += argc;
1035         hres = set_length(jsthis, ei, length);
1036         if(FAILED(hres))
1037             return hres;
1038     }
1039
1040     if(retv) {
1041         if(ctx->version < 2) {
1042             V_VT(retv) = VT_EMPTY;
1043         }else {
1044             V_VT(retv) = VT_I4;
1045             V_I4(retv) = length;
1046         }
1047     }
1048     return S_OK;
1049 }
1050
1051 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1052         VARIANT *retv, jsexcept_t *ei)
1053 {
1054     TRACE("\n");
1055
1056     switch(flags) {
1057     case INVOKE_FUNC:
1058         return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1059     case INVOKE_PROPERTYGET:
1060         return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei);
1061     default:
1062         FIXME("unimplemented flags %x\n", flags);
1063         return E_NOTIMPL;
1064     }
1065
1066     return S_OK;
1067 }
1068
1069 static void Array_destructor(jsdisp_t *dispex)
1070 {
1071     heap_free(dispex);
1072 }
1073
1074 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1075 {
1076     ArrayInstance *array = (ArrayInstance*)dispex;
1077     const WCHAR *ptr = name;
1078     DWORD id = 0;
1079
1080     if(!isdigitW(*ptr))
1081         return;
1082
1083     while(*ptr && isdigitW(*ptr)) {
1084         id = id*10 + (*ptr-'0');
1085         ptr++;
1086     }
1087
1088     if(*ptr)
1089         return;
1090
1091     if(id >= array->length)
1092         array->length = id+1;
1093 }
1094
1095 static const builtin_prop_t Array_props[] = {
1096     {concatW,                Array_concat,               PROPF_METHOD|1},
1097     {joinW,                  Array_join,                 PROPF_METHOD|1},
1098     {lengthW,                Array_length,               0},
1099     {popW,                   Array_pop,                  PROPF_METHOD},
1100     {pushW,                  Array_push,                 PROPF_METHOD|1},
1101     {reverseW,               Array_reverse,              PROPF_METHOD},
1102     {shiftW,                 Array_shift,                PROPF_METHOD},
1103     {sliceW,                 Array_slice,                PROPF_METHOD|2},
1104     {sortW,                  Array_sort,                 PROPF_METHOD|1},
1105     {spliceW,                Array_splice,               PROPF_METHOD|2},
1106     {toLocaleStringW,        Array_toLocaleString,       PROPF_METHOD},
1107     {toStringW,              Array_toString,             PROPF_METHOD},
1108     {unshiftW,               Array_unshift,              PROPF_METHOD|1},
1109 };
1110
1111 static const builtin_info_t Array_info = {
1112     JSCLASS_ARRAY,
1113     {NULL, Array_value, 0},
1114     sizeof(Array_props)/sizeof(*Array_props),
1115     Array_props,
1116     Array_destructor,
1117     Array_on_put
1118 };
1119
1120 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1121         VARIANT *retv, jsexcept_t *ei)
1122 {
1123     jsdisp_t *obj;
1124     VARIANT *arg_var;
1125     DWORD i;
1126     HRESULT hres;
1127
1128     TRACE("\n");
1129
1130     switch(flags) {
1131     case DISPATCH_METHOD:
1132     case DISPATCH_CONSTRUCT: {
1133         if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1134             if(V_I4(arg_var) < 0)
1135                 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1136
1137             hres = create_array(ctx, V_I4(arg_var), &obj);
1138             if(FAILED(hres))
1139                 return hres;
1140
1141             var_set_jsdisp(retv, obj);
1142             return S_OK;
1143         }
1144
1145         hres = create_array(ctx, arg_cnt(dp), &obj);
1146         if(FAILED(hres))
1147             return hres;
1148
1149         for(i=0; i < arg_cnt(dp); i++) {
1150             hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei);
1151             if(FAILED(hres))
1152                 break;
1153         }
1154         if(FAILED(hres)) {
1155             jsdisp_release(obj);
1156             return hres;
1157         }
1158
1159         var_set_jsdisp(retv, obj);
1160         break;
1161     }
1162     default:
1163         FIXME("unimplemented flags: %x\n", flags);
1164         return E_NOTIMPL;
1165     }
1166
1167     return S_OK;
1168 }
1169
1170 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1171 {
1172     ArrayInstance *array;
1173     HRESULT hres;
1174
1175     array = heap_alloc_zero(sizeof(ArrayInstance));
1176     if(!array)
1177         return E_OUTOFMEMORY;
1178
1179     if(object_prototype)
1180         hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1181     else
1182         hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1183
1184     if(FAILED(hres)) {
1185         heap_free(array);
1186         return hres;
1187     }
1188
1189     *ret = array;
1190     return S_OK;
1191 }
1192
1193 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1194 {
1195     ArrayInstance *array;
1196     HRESULT hres;
1197
1198     static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1199
1200     hres = alloc_array(ctx, object_prototype, &array);
1201     if(FAILED(hres))
1202         return hres;
1203
1204     hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1205
1206     jsdisp_release(&array->dispex);
1207     return hres;
1208 }
1209
1210 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1211 {
1212     ArrayInstance *array;
1213     HRESULT hres;
1214
1215     hres = alloc_array(ctx, NULL, &array);
1216     if(FAILED(hres))
1217         return hres;
1218
1219     array->length = length;
1220
1221     *ret = &array->dispex;
1222     return S_OK;
1223 }