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