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