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