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