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