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