mshtml.idl: Added IHTMLTableCell declaration.
[wine/multimedia.git] / dlls / jscript / array.c
blob54dc34704331d188d7c9db4e5f8443ff5f44ecc0
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <math.h>
21 #include "jscript.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
27 typedef struct {
28 jsdisp_t dispex;
30 DWORD length;
31 } ArrayInstance;
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};
47 static const WCHAR default_separatorW[] = {',',0};
49 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
51 return (ArrayInstance*)vdisp->u.jsdisp;
54 static inline ArrayInstance *array_this(vdisp_t *jsthis)
56 return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
59 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsexcept_t *ei, jsdisp_t **jsthis, DWORD *ret)
61 ArrayInstance *array;
62 VARIANT var;
63 HRESULT hres;
65 array = array_this(vdisp);
66 if(array) {
67 *jsthis = &array->dispex;
68 *ret = array->length;
69 return S_OK;
72 if(!is_jsdisp(vdisp))
73 return throw_type_error(ctx, ei, JS_E_JSCRIPT_EXPECTED, NULL);
75 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei);
76 if(FAILED(hres))
77 return hres;
79 hres = to_uint32(ctx, &var, ei, ret);
80 VariantClear(&var);
81 if(FAILED(hres))
82 return hres;
84 *jsthis = vdisp->u.jsdisp;
85 return S_OK;
88 static HRESULT set_length(jsdisp_t *obj, jsexcept_t *ei, DWORD length)
90 VARIANT var;
92 if(is_class(obj, JSCLASS_ARRAY)) {
93 ((ArrayInstance*)obj)->length = length;
94 return S_OK;
97 V_VT(&var) = VT_I4;
98 V_I4(&var) = length;
99 return jsdisp_propput_name(obj, lengthW, &var, ei);
102 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
104 if(!idx) {
105 *ptr = '0';
106 return ptr;
109 while(idx) {
110 *ptr-- = '0' + (idx%10);
111 idx /= 10;
114 return ptr+1;
117 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
118 VARIANT *retv, jsexcept_t *ei)
120 ArrayInstance *This = array_from_vdisp(jsthis);
122 TRACE("%p %d\n", This, This->length);
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 DOUBLE len = -1;
131 DWORD i;
132 HRESULT hres;
134 hres = to_number(ctx, get_arg(dp, 0), ei, &len);
135 if(FAILED(hres))
136 return hres;
138 len = floor(len);
139 if(len!=(DWORD)len)
140 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
142 for(i=len; i<This->length; i++) {
143 hres = jsdisp_delete_idx(&This->dispex, i);
144 if(FAILED(hres))
145 return hres;
148 This->length = len;
149 break;
151 default:
152 FIXME("unimplemented flags %x\n", flags);
153 return E_NOTIMPL;
156 return S_OK;
159 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
161 VARIANT var;
162 DWORD i;
163 HRESULT hres;
165 for(i=0; i < obj->length; i++) {
166 hres = jsdisp_get_idx(&obj->dispex, i, &var, ei);
167 if(hres == DISP_E_UNKNOWNNAME)
168 continue;
169 if(FAILED(hres))
170 return hres;
172 hres = jsdisp_propput_idx(array, *len+i, &var, ei);
173 VariantClear(&var);
174 if(FAILED(hres))
175 return hres;
178 *len += obj->length;
179 return S_OK;
182 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
184 jsdisp_t *jsobj;
185 VARIANT var;
186 HRESULT hres;
188 jsobj = iface_to_jsdisp((IUnknown*)obj);
189 if(jsobj) {
190 if(is_class(jsobj, JSCLASS_ARRAY)) {
191 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei);
192 jsdisp_release(jsobj);
193 return hres;
195 jsdisp_release(jsobj);
198 V_VT(&var) = VT_DISPATCH;
199 V_DISPATCH(&var) = obj;
200 return jsdisp_propput_idx(array, (*len)++, &var, ei);
203 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
204 VARIANT *retv, jsexcept_t *ei)
206 jsdisp_t *ret;
207 DWORD len = 0;
208 HRESULT hres;
210 TRACE("\n");
212 hres = create_array(ctx, 0, &ret);
213 if(FAILED(hres))
214 return hres;
216 hres = concat_obj(ret, jsthis->u.disp, &len, ei);
217 if(SUCCEEDED(hres)) {
218 VARIANT *arg;
219 DWORD i;
221 for(i=0; i < arg_cnt(dp); i++) {
222 arg = get_arg(dp, i);
223 if(V_VT(arg) == VT_DISPATCH)
224 hres = concat_obj(ret, V_DISPATCH(arg), &len, ei);
225 else
226 hres = jsdisp_propput_idx(ret, len++, arg, ei);
227 if(FAILED(hres))
228 break;
232 if(FAILED(hres))
233 return hres;
235 if(retv)
236 var_set_jsdisp(retv, ret);
237 else
238 jsdisp_release(ret);
239 return S_OK;
242 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, VARIANT *retv, jsexcept_t *ei)
244 BSTR *str_tab, ret = NULL;
245 VARIANT var;
246 DWORD i;
247 HRESULT hres = E_FAIL;
249 if(!length) {
250 if(retv) {
251 V_VT(retv) = VT_BSTR;
252 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
253 if(!V_BSTR(retv))
254 return E_OUTOFMEMORY;
256 return S_OK;
259 str_tab = heap_alloc_zero(length * sizeof(BSTR));
260 if(!str_tab)
261 return E_OUTOFMEMORY;
263 for(i=0; i < length; i++) {
264 hres = jsdisp_get_idx(array, i, &var, ei);
265 if(hres == DISP_E_UNKNOWNNAME) {
266 hres = S_OK;
267 continue;
268 } else if(FAILED(hres))
269 break;
271 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
272 hres = to_string(ctx, &var, ei, str_tab+i);
273 VariantClear(&var);
274 if(FAILED(hres))
275 break;
278 if(SUCCEEDED(hres)) {
279 DWORD seplen = 0, len = 0;
280 WCHAR *ptr;
282 seplen = strlenW(sep);
284 if(str_tab[0])
285 len = SysStringLen(str_tab[0]);
286 for(i=1; i < length; i++)
287 len += seplen + SysStringLen(str_tab[i]);
289 ret = SysAllocStringLen(NULL, len);
290 if(ret) {
291 DWORD tmplen = 0;
293 if(str_tab[0]) {
294 tmplen = SysStringLen(str_tab[0]);
295 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
298 ptr = ret + tmplen;
299 for(i=1; i < length; i++) {
300 if(seplen) {
301 memcpy(ptr, sep, seplen*sizeof(WCHAR));
302 ptr += seplen;
305 if(str_tab[i]) {
306 tmplen = SysStringLen(str_tab[i]);
307 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
308 ptr += tmplen;
311 *ptr=0;
312 }else {
313 hres = E_OUTOFMEMORY;
317 for(i=0; i < length; i++)
318 SysFreeString(str_tab[i]);
319 heap_free(str_tab);
320 if(FAILED(hres))
321 return hres;
323 TRACE("= %s\n", debugstr_w(ret));
325 if(retv) {
326 if(!ret) {
327 ret = SysAllocStringLen(NULL, 0);
328 if(!ret)
329 return E_OUTOFMEMORY;
332 V_VT(retv) = VT_BSTR;
333 V_BSTR(retv) = ret;
334 }else {
335 SysFreeString(ret);
338 return S_OK;
341 /* ECMA-262 3rd Edition 15.4.4.5 */
342 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
343 VARIANT *retv, jsexcept_t *ei)
345 jsdisp_t *jsthis;
346 DWORD length;
347 HRESULT hres;
349 TRACE("\n");
351 hres = get_length(ctx, vthis, ei, &jsthis, &length);
352 if(FAILED(hres))
353 return hres;
355 if(arg_cnt(dp)) {
356 BSTR sep;
358 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
359 if(FAILED(hres))
360 return hres;
362 hres = array_join(ctx, jsthis, length, sep, retv, ei);
364 SysFreeString(sep);
365 }else {
366 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei);
369 return hres;
372 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
373 VARIANT *retv, jsexcept_t *ei)
375 jsdisp_t *jsthis;
376 VARIANT val;
377 DWORD length;
378 HRESULT hres;
380 TRACE("\n");
382 hres = get_length(ctx, vthis, ei, &jsthis, &length);
383 if(FAILED(hres))
384 return hres;
386 if(!length) {
387 hres = set_length(jsthis, ei, 0);
388 if(FAILED(hres))
389 return hres;
391 if(retv)
392 V_VT(retv) = VT_EMPTY;
393 return S_OK;
396 length--;
397 hres = jsdisp_get_idx(jsthis, length, &val, ei);
398 if(SUCCEEDED(hres)) {
399 hres = jsdisp_delete_idx(jsthis, length);
400 } else if(hres == DISP_E_UNKNOWNNAME) {
401 V_VT(&val) = VT_EMPTY;
402 hres = S_OK;
403 } else
404 return hres;
406 if(SUCCEEDED(hres))
407 hres = set_length(jsthis, ei, length);
409 if(FAILED(hres)) {
410 VariantClear(&val);
411 return hres;
414 if(retv)
415 *retv = val;
416 else
417 VariantClear(&val);
419 return S_OK;
422 /* ECMA-262 3rd Edition 15.4.4.7 */
423 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
424 VARIANT *retv, jsexcept_t *ei)
426 jsdisp_t *jsthis;
427 DWORD length = 0;
428 int i, n;
429 HRESULT hres;
431 TRACE("\n");
433 hres = get_length(ctx, vthis, ei, &jsthis, &length);
434 if(FAILED(hres))
435 return hres;
437 n = arg_cnt(dp);
438 for(i=0; i < n; i++) {
439 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei);
440 if(FAILED(hres))
441 return hres;
444 hres = set_length(jsthis, ei, length+n);
445 if(FAILED(hres))
446 return hres;
448 if(retv) {
449 V_VT(retv) = VT_I4;
450 V_I4(retv) = length+n;
452 return S_OK;
455 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
456 VARIANT *retv, jsexcept_t *ei)
458 jsdisp_t *jsthis;
459 DWORD length, k, l;
460 VARIANT v1, v2;
461 HRESULT hres1, hres2;
463 TRACE("\n");
465 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
466 if(FAILED(hres1))
467 return hres1;
469 for(k=0; k<length/2; k++) {
470 l = length-k-1;
472 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei);
473 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
474 return hres1;
476 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
477 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
478 VariantClear(&v1);
479 return hres2;
482 if(hres1 == DISP_E_UNKNOWNNAME)
483 hres1 = jsdisp_delete_idx(jsthis, l);
484 else
485 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei);
487 if(FAILED(hres1)) {
488 VariantClear(&v1);
489 VariantClear(&v2);
490 return hres1;
493 if(hres2 == DISP_E_UNKNOWNNAME)
494 hres2 = jsdisp_delete_idx(jsthis, k);
495 else
496 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei);
498 if(FAILED(hres2)) {
499 VariantClear(&v2);
500 return hres2;
504 if(retv) {
505 jsdisp_addref(jsthis);
506 var_set_jsdisp(retv, jsthis);
509 return S_OK;
512 /* ECMA-262 3rd Edition 15.4.4.9 */
513 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
514 VARIANT *retv, jsexcept_t *ei)
516 jsdisp_t *jsthis;
517 DWORD length = 0, i;
518 VARIANT v, ret;
519 HRESULT hres;
521 TRACE("\n");
523 hres = get_length(ctx, vthis, ei, &jsthis, &length);
524 if(FAILED(hres))
525 return hres;
527 if(!length) {
528 hres = set_length(jsthis, ei, 0);
529 if(FAILED(hres))
530 return hres;
533 if(!length) {
534 if(retv)
535 V_VT(retv) = VT_EMPTY;
536 return S_OK;
539 hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
540 if(hres == DISP_E_UNKNOWNNAME) {
541 V_VT(&ret) = VT_EMPTY;
542 hres = S_OK;
545 for(i=1; SUCCEEDED(hres) && i<length; i++) {
546 hres = jsdisp_get_idx(jsthis, i, &v, ei);
547 if(hres == DISP_E_UNKNOWNNAME)
548 hres = jsdisp_delete_idx(jsthis, i-1);
549 else if(SUCCEEDED(hres))
550 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei);
553 if(SUCCEEDED(hres)) {
554 hres = jsdisp_delete_idx(jsthis, length-1);
555 if(SUCCEEDED(hres))
556 hres = set_length(jsthis, ei, length-1);
559 if(SUCCEEDED(hres) && retv)
560 *retv = ret;
561 else
562 VariantClear(&ret);
563 return hres;
566 /* ECMA-262 3rd Edition 15.4.4.10 */
567 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
568 VARIANT *retv, jsexcept_t *ei)
570 jsdisp_t *arr, *jsthis;
571 DOUBLE range;
572 DWORD length, start, end, idx;
573 HRESULT hres;
575 TRACE("\n");
577 hres = get_length(ctx, vthis, ei, &jsthis, &length);
578 if(FAILED(hres))
579 return hres;
581 if(arg_cnt(dp)) {
582 hres = to_number(ctx, get_arg(dp, 0), ei, &range);
583 if(FAILED(hres))
584 return hres;
586 range = floor(range);
587 if(-range>length || isnan(range)) start = 0;
588 else if(range < 0) start = range+length;
589 else if(range <= length) start = range;
590 else start = length;
592 else start = 0;
594 if(arg_cnt(dp)>1) {
595 hres = to_number(ctx, get_arg(dp, 1), ei, &range);
596 if(FAILED(hres))
597 return hres;
599 range = floor(range);
600 if(-range>length) end = 0;
601 else if(range < 0) end = range+length;
602 else if(range <= length) end = range;
603 else end = length;
605 else end = length;
607 hres = create_array(ctx, (end>start)?end-start:0, &arr);
608 if(FAILED(hres))
609 return hres;
611 for(idx=start; idx<end; idx++) {
612 VARIANT v;
614 hres = jsdisp_get_idx(jsthis, idx, &v, ei);
615 if(hres == DISP_E_UNKNOWNNAME)
616 continue;
618 if(SUCCEEDED(hres)) {
619 hres = jsdisp_propput_idx(arr, idx-start, &v, ei);
620 VariantClear(&v);
623 if(FAILED(hres)) {
624 jsdisp_release(arr);
625 return hres;
629 if(retv)
630 var_set_jsdisp(retv, arr);
631 else
632 jsdisp_release(arr);
634 return S_OK;
637 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei, INT *cmp)
639 HRESULT hres;
641 if(cmp_func) {
642 VARIANTARG args[2];
643 DISPPARAMS dp = {args, NULL, 2, 0};
644 double n;
645 VARIANT res;
647 args[0] = *v2;
648 args[1] = *v1;
650 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei);
651 if(FAILED(hres))
652 return hres;
654 hres = to_number(ctx, &res, ei, &n);
655 VariantClear(&res);
656 if(FAILED(hres))
657 return hres;
659 if(n == 0)
660 *cmp = 0;
661 *cmp = n > 0.0 ? 1 : -1;
662 }else if(V_VT(v1) == VT_EMPTY) {
663 *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
664 }else if(V_VT(v2) == VT_EMPTY) {
665 *cmp = -1;
666 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
667 DOUBLE d = num_val(v1)-num_val(v2);
668 if(d > 0.0)
669 *cmp = 1;
670 else
671 *cmp = d < -0.0 ? -1 : 0;
672 }else {
673 BSTR x, y;
675 hres = to_string(ctx, v1, ei, &x);
676 if(FAILED(hres))
677 return hres;
679 hres = to_string(ctx, v2, ei, &y);
680 if(SUCCEEDED(hres)) {
681 *cmp = strcmpW(x, y);
682 SysFreeString(y);
684 SysFreeString(x);
685 if(FAILED(hres))
686 return hres;
689 return S_OK;
692 /* ECMA-262 3rd Edition 15.4.4.11 */
693 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
694 VARIANT *retv, jsexcept_t *ei)
696 jsdisp_t *jsthis, *cmp_func = NULL;
697 VARIANT *vtab, **sorttab = NULL;
698 DWORD length;
699 DWORD i;
700 HRESULT hres = S_OK;
702 TRACE("\n");
704 hres = get_length(ctx, vthis, ei, &jsthis, &length);
705 if(FAILED(hres))
706 return hres;
708 if(arg_cnt(dp) > 1) {
709 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
710 return E_FAIL;
713 if(arg_cnt(dp) == 1) {
714 VARIANT *arg = get_arg(dp, 0);
716 if(V_VT(arg) != VT_DISPATCH) {
717 WARN("arg is not dispatch\n");
718 return E_FAIL;
722 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
723 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
724 WARN("cmp_func is not a function\n");
725 if(cmp_func)
726 jsdisp_release(cmp_func);
727 return E_FAIL;
731 if(!length) {
732 if(cmp_func)
733 jsdisp_release(cmp_func);
734 if(retv) {
735 jsdisp_addref(jsthis);
736 var_set_jsdisp(retv, jsthis);
738 return S_OK;
741 vtab = heap_alloc_zero(length * sizeof(VARIANT));
742 if(vtab) {
743 for(i=0; i<length; i++) {
744 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei);
745 if(hres == DISP_E_UNKNOWNNAME) {
746 V_VT(vtab+i) = VT_EMPTY;
747 hres = S_OK;
748 } else if(FAILED(hres)) {
749 WARN("Could not get elem %d: %08x\n", i, hres);
750 break;
753 }else {
754 hres = E_OUTOFMEMORY;
757 if(SUCCEEDED(hres)) {
758 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
759 if(!sorttab)
760 hres = E_OUTOFMEMORY;
763 /* merge-sort */
764 if(SUCCEEDED(hres)) {
765 VARIANT *tmpv, **tmpbuf;
766 INT cmp;
768 tmpbuf = sorttab + length;
769 for(i=0; i < length; i++)
770 sorttab[i] = vtab+i;
772 for(i=0; i < length/2; i++) {
773 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, &cmp);
774 if(FAILED(hres))
775 break;
777 if(cmp < 0) {
778 tmpv = sorttab[2*i];
779 sorttab[2*i] = sorttab[2*i+1];
780 sorttab[2*i+1] = tmpv;
784 if(SUCCEEDED(hres)) {
785 DWORD k, a, b, bend;
787 for(k=2; k < length; k *= 2) {
788 for(i=0; i+k < length; i += 2*k) {
789 a = b = 0;
790 if(i+2*k <= length)
791 bend = k;
792 else
793 bend = length - (i+k);
795 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
797 while(a < k && b < bend) {
798 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, &cmp);
799 if(FAILED(hres))
800 break;
802 if(cmp < 0) {
803 sorttab[i+a+b] = tmpbuf[a];
804 a++;
805 }else {
806 sorttab[i+a+b] = sorttab[i+k+b];
807 b++;
811 if(FAILED(hres))
812 break;
814 if(a < k)
815 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
818 if(FAILED(hres))
819 break;
823 for(i=0; SUCCEEDED(hres) && i < length; i++)
824 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei);
827 if(vtab) {
828 for(i=0; i < length; i++)
829 VariantClear(vtab+i);
830 heap_free(vtab);
832 heap_free(sorttab);
833 if(cmp_func)
834 jsdisp_release(cmp_func);
836 if(FAILED(hres))
837 return hres;
839 if(retv) {
840 jsdisp_addref(jsthis);
841 var_set_jsdisp(retv, jsthis);
844 return S_OK;
847 /* ECMA-262 3rd Edition 15.4.4.12 */
848 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
849 VARIANT *retv, jsexcept_t *ei)
851 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
852 jsdisp_t *ret_array = NULL, *jsthis;
853 VARIANT v;
854 HRESULT hres = S_OK;
856 TRACE("\n");
858 hres = get_length(ctx, vthis, ei, &jsthis, &length);
859 if(FAILED(hres))
860 return hres;
862 argc = arg_cnt(dp);
863 if(argc >= 1) {
864 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
865 if(FAILED(hres))
866 return hres;
868 if(V_VT(&v) == VT_I4) {
869 if(V_I4(&v) >= 0)
870 start = min(V_I4(&v), length);
871 else
872 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
873 }else {
874 start = V_R8(&v) < 0.0 ? 0 : length;
878 if(argc >= 2) {
879 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
880 if(FAILED(hres))
881 return hres;
883 if(V_VT(&v) == VT_I4) {
884 if(V_I4(&v) > 0)
885 delete_cnt = min(V_I4(&v), length-start);
886 }else if(V_R8(&v) > 0.0) {
887 delete_cnt = length-start;
890 add_args = argc-2;
893 if(retv) {
894 hres = create_array(ctx, 0, &ret_array);
895 if(FAILED(hres))
896 return hres;
898 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
899 hres = jsdisp_get_idx(jsthis, start+i, &v, ei);
900 if(hres == DISP_E_UNKNOWNNAME)
901 hres = S_OK;
902 else if(SUCCEEDED(hres))
903 hres = jsdisp_propput_idx(ret_array, i, &v, ei);
906 if(SUCCEEDED(hres)) {
907 V_VT(&v) = VT_I4;
908 V_I4(&v) = delete_cnt;
910 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei);
914 if(add_args < delete_cnt) {
915 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
916 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei);
917 if(hres == DISP_E_UNKNOWNNAME)
918 hres = jsdisp_delete_idx(jsthis, i+add_args);
919 else if(SUCCEEDED(hres))
920 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei);
923 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
924 hres = jsdisp_delete_idx(jsthis, i-1);
925 }else if(add_args > delete_cnt) {
926 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
927 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei);
928 if(hres == DISP_E_UNKNOWNNAME)
929 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
930 else if(SUCCEEDED(hres))
931 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei);
935 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
936 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei);
938 if(SUCCEEDED(hres)) {
939 V_VT(&v) = VT_I4;
940 V_I4(&v) = length-delete_cnt+add_args;
941 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei);
944 if(FAILED(hres)) {
945 if(ret_array)
946 jsdisp_release(ret_array);
947 return hres;
950 if(retv)
951 var_set_jsdisp(retv, ret_array);
952 return S_OK;
955 /* ECMA-262 3rd Edition 15.4.4.2 */
956 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
957 VARIANT *retv, jsexcept_t *ei)
959 ArrayInstance *array;
961 TRACE("\n");
963 array = array_this(jsthis);
964 if(!array)
965 return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
967 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei);
970 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
971 VARIANT *retv, jsexcept_t *ei)
973 FIXME("\n");
974 return E_NOTIMPL;
977 /* ECMA-262 3rd Edition 15.4.4.13 */
978 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
979 VARIANT *retv, jsexcept_t *ei)
981 jsdisp_t *jsthis;
982 WCHAR buf[14], *buf_end, *str;
983 DWORD argc, i, length;
984 VARIANT var;
985 DISPID id;
986 HRESULT hres;
988 TRACE("\n");
990 hres = get_length(ctx, vthis, ei, &jsthis, &length);
991 if(FAILED(hres))
992 return hres;
994 argc = arg_cnt(dp);
995 if(argc) {
996 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
997 *buf_end-- = 0;
998 i = length;
1000 while(i--) {
1001 str = idx_to_str(i, buf_end);
1003 hres = jsdisp_get_id(jsthis, str, 0, &id);
1004 if(SUCCEEDED(hres)) {
1005 hres = jsdisp_propget(jsthis, id, &var, ei);
1006 if(FAILED(hres))
1007 return hres;
1009 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei);
1010 VariantClear(&var);
1011 }else if(hres == DISP_E_UNKNOWNNAME) {
1012 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1016 if(FAILED(hres))
1017 return hres;
1020 for(i=0; i<argc; i++) {
1021 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei);
1022 if(FAILED(hres))
1023 return hres;
1026 if(argc) {
1027 length += argc;
1028 hres = set_length(jsthis, ei, length);
1029 if(FAILED(hres))
1030 return hres;
1033 if(retv) {
1034 if(ctx->version < 2) {
1035 V_VT(retv) = VT_EMPTY;
1036 }else {
1037 V_VT(retv) = VT_I4;
1038 V_I4(retv) = length;
1041 return S_OK;
1044 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1045 VARIANT *retv, jsexcept_t *ei)
1047 TRACE("\n");
1049 switch(flags) {
1050 case INVOKE_FUNC:
1051 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1052 case INVOKE_PROPERTYGET:
1053 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei);
1054 default:
1055 FIXME("unimplemented flags %x\n", flags);
1056 return E_NOTIMPL;
1059 return S_OK;
1062 static void Array_destructor(jsdisp_t *dispex)
1064 heap_free(dispex);
1067 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1069 ArrayInstance *array = (ArrayInstance*)dispex;
1070 const WCHAR *ptr = name;
1071 DWORD id = 0;
1073 if(!isdigitW(*ptr))
1074 return;
1076 while(*ptr && isdigitW(*ptr)) {
1077 id = id*10 + (*ptr-'0');
1078 ptr++;
1081 if(*ptr)
1082 return;
1084 if(id >= array->length)
1085 array->length = id+1;
1088 static const builtin_prop_t Array_props[] = {
1089 {concatW, Array_concat, PROPF_METHOD|1},
1090 {joinW, Array_join, PROPF_METHOD|1},
1091 {lengthW, Array_length, 0},
1092 {popW, Array_pop, PROPF_METHOD},
1093 {pushW, Array_push, PROPF_METHOD|1},
1094 {reverseW, Array_reverse, PROPF_METHOD},
1095 {shiftW, Array_shift, PROPF_METHOD},
1096 {sliceW, Array_slice, PROPF_METHOD|2},
1097 {sortW, Array_sort, PROPF_METHOD|1},
1098 {spliceW, Array_splice, PROPF_METHOD|2},
1099 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1100 {toStringW, Array_toString, PROPF_METHOD},
1101 {unshiftW, Array_unshift, PROPF_METHOD|1},
1104 static const builtin_info_t Array_info = {
1105 JSCLASS_ARRAY,
1106 {NULL, Array_value, 0},
1107 sizeof(Array_props)/sizeof(*Array_props),
1108 Array_props,
1109 Array_destructor,
1110 Array_on_put
1113 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1114 VARIANT *retv, jsexcept_t *ei)
1116 jsdisp_t *obj;
1117 VARIANT *arg_var;
1118 DWORD i;
1119 HRESULT hres;
1121 TRACE("\n");
1123 switch(flags) {
1124 case DISPATCH_METHOD:
1125 case DISPATCH_CONSTRUCT: {
1126 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1127 if(V_I4(arg_var) < 0)
1128 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1130 hres = create_array(ctx, V_I4(arg_var), &obj);
1131 if(FAILED(hres))
1132 return hres;
1134 var_set_jsdisp(retv, obj);
1135 return S_OK;
1138 hres = create_array(ctx, arg_cnt(dp), &obj);
1139 if(FAILED(hres))
1140 return hres;
1142 for(i=0; i < arg_cnt(dp); i++) {
1143 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei);
1144 if(FAILED(hres))
1145 break;
1147 if(FAILED(hres)) {
1148 jsdisp_release(obj);
1149 return hres;
1152 var_set_jsdisp(retv, obj);
1153 break;
1155 default:
1156 FIXME("unimplemented flags: %x\n", flags);
1157 return E_NOTIMPL;
1160 return S_OK;
1163 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1165 ArrayInstance *array;
1166 HRESULT hres;
1168 array = heap_alloc_zero(sizeof(ArrayInstance));
1169 if(!array)
1170 return E_OUTOFMEMORY;
1172 if(object_prototype)
1173 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1174 else
1175 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1177 if(FAILED(hres)) {
1178 heap_free(array);
1179 return hres;
1182 *ret = array;
1183 return S_OK;
1186 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1188 ArrayInstance *array;
1189 HRESULT hres;
1191 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1193 hres = alloc_array(ctx, object_prototype, &array);
1194 if(FAILED(hres))
1195 return hres;
1197 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1199 jsdisp_release(&array->dispex);
1200 return hres;
1203 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1205 ArrayInstance *array;
1206 HRESULT hres;
1208 hres = alloc_array(ctx, NULL, &array);
1209 if(FAILED(hres))
1210 return hres;
1212 array->length = length;
1214 *ret = &array->dispex;
1215 return S_OK;