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