d3dx9_36: Implementation of D3DXQuaternionSquadSetup.
[wine.git] / dlls / jscript / array.c
blob5ff6344f0e973750ac4452755e74932c5d541dd4
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 "config.h"
20 #include "wine/port.h"
22 #include <math.h>
24 #include "jscript.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
30 typedef struct {
31 jsdisp_t dispex;
33 DWORD length;
34 } ArrayInstance;
36 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
37 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
38 static const WCHAR joinW[] = {'j','o','i','n',0};
39 static const WCHAR popW[] = {'p','o','p',0};
40 static const WCHAR pushW[] = {'p','u','s','h',0};
41 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
42 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
43 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
44 static const WCHAR sortW[] = {'s','o','r','t',0};
45 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
46 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
47 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
48 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
50 static const WCHAR default_separatorW[] = {',',0};
52 static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
54 return (ArrayInstance*)vdisp->u.jsdisp;
57 static inline ArrayInstance *array_this(vdisp_t *jsthis)
59 return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
62 static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsexcept_t *ei, jsdisp_t **jsthis, DWORD *ret)
64 ArrayInstance *array;
65 VARIANT var;
66 HRESULT hres;
68 array = array_this(vdisp);
69 if(array) {
70 *jsthis = &array->dispex;
71 *ret = array->length;
72 return S_OK;
75 if(!is_jsdisp(vdisp))
76 return throw_type_error(ctx, ei, JS_E_JSCRIPT_EXPECTED, NULL);
78 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei);
79 if(FAILED(hres))
80 return hres;
82 hres = to_uint32(ctx, &var, ei, ret);
83 VariantClear(&var);
84 if(FAILED(hres))
85 return hres;
87 *jsthis = vdisp->u.jsdisp;
88 return S_OK;
91 static HRESULT set_length(jsdisp_t *obj, jsexcept_t *ei, DWORD length)
93 VARIANT var;
95 if(is_class(obj, JSCLASS_ARRAY)) {
96 ((ArrayInstance*)obj)->length = length;
97 return S_OK;
100 V_VT(&var) = VT_I4;
101 V_I4(&var) = length;
102 return jsdisp_propput_name(obj, lengthW, &var, ei);
105 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
107 if(!idx) {
108 *ptr = '0';
109 return ptr;
112 while(idx) {
113 *ptr-- = '0' + (idx%10);
114 idx /= 10;
117 return ptr+1;
120 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
121 VARIANT *retv, jsexcept_t *ei)
123 ArrayInstance *This = array_from_vdisp(jsthis);
125 TRACE("%p %d\n", This, This->length);
127 switch(flags) {
128 case DISPATCH_PROPERTYGET:
129 V_VT(retv) = VT_I4;
130 V_I4(retv) = This->length;
131 break;
132 case DISPATCH_PROPERTYPUT: {
133 DOUBLE len = -1;
134 DWORD i;
135 HRESULT hres;
137 hres = to_number(ctx, get_arg(dp, 0), ei, &len);
138 if(FAILED(hres))
139 return hres;
141 len = floor(len);
142 if(len!=(DWORD)len)
143 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
145 for(i=len; i<This->length; i++) {
146 hres = jsdisp_delete_idx(&This->dispex, i);
147 if(FAILED(hres))
148 return hres;
151 This->length = len;
152 break;
154 default:
155 FIXME("unimplemented flags %x\n", flags);
156 return E_NOTIMPL;
159 return S_OK;
162 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
164 VARIANT var;
165 DWORD i;
166 HRESULT hres;
168 for(i=0; i < obj->length; i++) {
169 hres = jsdisp_get_idx(&obj->dispex, i, &var, ei);
170 if(hres == DISP_E_UNKNOWNNAME)
171 continue;
172 if(FAILED(hres))
173 return hres;
175 hres = jsdisp_propput_idx(array, *len+i, &var, ei);
176 VariantClear(&var);
177 if(FAILED(hres))
178 return hres;
181 *len += obj->length;
182 return S_OK;
185 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
187 jsdisp_t *jsobj;
188 VARIANT var;
189 HRESULT hres;
191 jsobj = iface_to_jsdisp((IUnknown*)obj);
192 if(jsobj) {
193 if(is_class(jsobj, JSCLASS_ARRAY)) {
194 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei);
195 jsdisp_release(jsobj);
196 return hres;
198 jsdisp_release(jsobj);
201 V_VT(&var) = VT_DISPATCH;
202 V_DISPATCH(&var) = obj;
203 return jsdisp_propput_idx(array, (*len)++, &var, ei);
206 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei)
209 jsdisp_t *ret;
210 DWORD len = 0;
211 HRESULT hres;
213 TRACE("\n");
215 hres = create_array(ctx, 0, &ret);
216 if(FAILED(hres))
217 return hres;
219 hres = concat_obj(ret, jsthis->u.disp, &len, ei);
220 if(SUCCEEDED(hres)) {
221 VARIANT *arg;
222 DWORD i;
224 for(i=0; i < arg_cnt(dp); i++) {
225 arg = get_arg(dp, i);
226 if(V_VT(arg) == VT_DISPATCH)
227 hres = concat_obj(ret, V_DISPATCH(arg), &len, ei);
228 else
229 hres = jsdisp_propput_idx(ret, len++, arg, ei);
230 if(FAILED(hres))
231 break;
235 if(FAILED(hres))
236 return hres;
238 if(retv)
239 var_set_jsdisp(retv, ret);
240 else
241 jsdisp_release(ret);
242 return S_OK;
245 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, VARIANT *retv, jsexcept_t *ei)
247 BSTR *str_tab, ret = NULL;
248 VARIANT var;
249 DWORD i;
250 HRESULT hres = E_FAIL;
252 if(!length) {
253 if(retv) {
254 V_VT(retv) = VT_BSTR;
255 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
256 if(!V_BSTR(retv))
257 return E_OUTOFMEMORY;
259 return S_OK;
262 str_tab = heap_alloc_zero(length * sizeof(BSTR));
263 if(!str_tab)
264 return E_OUTOFMEMORY;
266 for(i=0; i < length; i++) {
267 hres = jsdisp_get_idx(array, i, &var, ei);
268 if(hres == DISP_E_UNKNOWNNAME) {
269 hres = S_OK;
270 continue;
271 } else if(FAILED(hres))
272 break;
274 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
275 hres = to_string(ctx, &var, ei, str_tab+i);
276 VariantClear(&var);
277 if(FAILED(hres))
278 break;
281 if(SUCCEEDED(hres)) {
282 DWORD seplen = 0, len = 0;
283 WCHAR *ptr;
285 seplen = strlenW(sep);
287 if(str_tab[0])
288 len = SysStringLen(str_tab[0]);
289 for(i=1; i < length; i++)
290 len += seplen + SysStringLen(str_tab[i]);
292 ret = SysAllocStringLen(NULL, len);
293 if(ret) {
294 DWORD tmplen = 0;
296 if(str_tab[0]) {
297 tmplen = SysStringLen(str_tab[0]);
298 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
301 ptr = ret + tmplen;
302 for(i=1; i < length; i++) {
303 if(seplen) {
304 memcpy(ptr, sep, seplen*sizeof(WCHAR));
305 ptr += seplen;
308 if(str_tab[i]) {
309 tmplen = SysStringLen(str_tab[i]);
310 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
311 ptr += tmplen;
314 *ptr=0;
315 }else {
316 hres = E_OUTOFMEMORY;
320 for(i=0; i < length; i++)
321 SysFreeString(str_tab[i]);
322 heap_free(str_tab);
323 if(FAILED(hres))
324 return hres;
326 TRACE("= %s\n", debugstr_w(ret));
328 if(retv) {
329 if(!ret) {
330 ret = SysAllocStringLen(NULL, 0);
331 if(!ret)
332 return E_OUTOFMEMORY;
335 V_VT(retv) = VT_BSTR;
336 V_BSTR(retv) = ret;
337 }else {
338 SysFreeString(ret);
341 return S_OK;
344 /* ECMA-262 3rd Edition 15.4.4.5 */
345 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
346 VARIANT *retv, jsexcept_t *ei)
348 jsdisp_t *jsthis;
349 DWORD length;
350 HRESULT hres;
352 TRACE("\n");
354 hres = get_length(ctx, vthis, ei, &jsthis, &length);
355 if(FAILED(hres))
356 return hres;
358 if(arg_cnt(dp)) {
359 BSTR sep;
361 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
362 if(FAILED(hres))
363 return hres;
365 hres = array_join(ctx, jsthis, length, sep, retv, ei);
367 SysFreeString(sep);
368 }else {
369 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei);
372 return hres;
375 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
376 VARIANT *retv, jsexcept_t *ei)
378 jsdisp_t *jsthis;
379 VARIANT val;
380 DWORD length;
381 HRESULT hres;
383 TRACE("\n");
385 hres = get_length(ctx, vthis, ei, &jsthis, &length);
386 if(FAILED(hres))
387 return hres;
389 if(!length) {
390 hres = set_length(jsthis, ei, 0);
391 if(FAILED(hres))
392 return hres;
394 if(retv)
395 V_VT(retv) = VT_EMPTY;
396 return S_OK;
399 length--;
400 hres = jsdisp_get_idx(jsthis, length, &val, ei);
401 if(SUCCEEDED(hres)) {
402 hres = jsdisp_delete_idx(jsthis, length);
403 } else if(hres == DISP_E_UNKNOWNNAME) {
404 V_VT(&val) = VT_EMPTY;
405 hres = S_OK;
406 } else
407 return hres;
409 if(SUCCEEDED(hres))
410 hres = set_length(jsthis, ei, length);
412 if(FAILED(hres)) {
413 VariantClear(&val);
414 return hres;
417 if(retv)
418 *retv = val;
419 else
420 VariantClear(&val);
422 return S_OK;
425 /* ECMA-262 3rd Edition 15.4.4.7 */
426 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
427 VARIANT *retv, jsexcept_t *ei)
429 jsdisp_t *jsthis;
430 DWORD length = 0;
431 int i, n;
432 HRESULT hres;
434 TRACE("\n");
436 hres = get_length(ctx, vthis, ei, &jsthis, &length);
437 if(FAILED(hres))
438 return hres;
440 n = arg_cnt(dp);
441 for(i=0; i < n; i++) {
442 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei);
443 if(FAILED(hres))
444 return hres;
447 hres = set_length(jsthis, ei, length+n);
448 if(FAILED(hres))
449 return hres;
451 if(retv) {
452 V_VT(retv) = VT_I4;
453 V_I4(retv) = length+n;
455 return S_OK;
458 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
459 VARIANT *retv, jsexcept_t *ei)
461 jsdisp_t *jsthis;
462 DWORD length, k, l;
463 VARIANT v1, v2;
464 HRESULT hres1, hres2;
466 TRACE("\n");
468 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
469 if(FAILED(hres1))
470 return hres1;
472 for(k=0; k<length/2; k++) {
473 l = length-k-1;
475 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei);
476 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
477 return hres1;
479 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
480 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
481 VariantClear(&v1);
482 return hres2;
485 if(hres1 == DISP_E_UNKNOWNNAME)
486 hres1 = jsdisp_delete_idx(jsthis, l);
487 else
488 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei);
490 if(FAILED(hres1)) {
491 VariantClear(&v1);
492 VariantClear(&v2);
493 return hres1;
496 if(hres2 == DISP_E_UNKNOWNNAME)
497 hres2 = jsdisp_delete_idx(jsthis, k);
498 else
499 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei);
501 if(FAILED(hres2)) {
502 VariantClear(&v2);
503 return hres2;
507 if(retv) {
508 jsdisp_addref(jsthis);
509 var_set_jsdisp(retv, jsthis);
512 return S_OK;
515 /* ECMA-262 3rd Edition 15.4.4.9 */
516 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
517 VARIANT *retv, jsexcept_t *ei)
519 jsdisp_t *jsthis;
520 DWORD length = 0, i;
521 VARIANT v, ret;
522 HRESULT hres;
524 TRACE("\n");
526 hres = get_length(ctx, vthis, ei, &jsthis, &length);
527 if(FAILED(hres))
528 return hres;
530 if(!length) {
531 hres = set_length(jsthis, ei, 0);
532 if(FAILED(hres))
533 return hres;
536 if(!length) {
537 if(retv)
538 V_VT(retv) = VT_EMPTY;
539 return S_OK;
542 hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
543 if(hres == DISP_E_UNKNOWNNAME) {
544 V_VT(&ret) = VT_EMPTY;
545 hres = S_OK;
548 for(i=1; SUCCEEDED(hres) && i<length; i++) {
549 hres = jsdisp_get_idx(jsthis, i, &v, ei);
550 if(hres == DISP_E_UNKNOWNNAME)
551 hres = jsdisp_delete_idx(jsthis, i-1);
552 else if(SUCCEEDED(hres))
553 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei);
556 if(SUCCEEDED(hres)) {
557 hres = jsdisp_delete_idx(jsthis, length-1);
558 if(SUCCEEDED(hres))
559 hres = set_length(jsthis, ei, length-1);
562 if(SUCCEEDED(hres) && retv)
563 *retv = ret;
564 else
565 VariantClear(&ret);
566 return hres;
569 /* ECMA-262 3rd Edition 15.4.4.10 */
570 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
571 VARIANT *retv, jsexcept_t *ei)
573 jsdisp_t *arr, *jsthis;
574 DOUBLE range;
575 DWORD length, start, end, idx;
576 HRESULT hres;
578 TRACE("\n");
580 hres = get_length(ctx, vthis, ei, &jsthis, &length);
581 if(FAILED(hres))
582 return hres;
584 if(arg_cnt(dp)) {
585 hres = to_number(ctx, get_arg(dp, 0), ei, &range);
586 if(FAILED(hres))
587 return hres;
589 range = floor(range);
590 if(-range>length || isnan(range)) start = 0;
591 else if(range < 0) start = range+length;
592 else if(range <= length) start = range;
593 else start = length;
595 else start = 0;
597 if(arg_cnt(dp)>1) {
598 hres = to_number(ctx, get_arg(dp, 1), ei, &range);
599 if(FAILED(hres))
600 return hres;
602 range = floor(range);
603 if(-range>length) end = 0;
604 else if(range < 0) end = range+length;
605 else if(range <= length) end = range;
606 else end = length;
608 else end = length;
610 hres = create_array(ctx, (end>start)?end-start:0, &arr);
611 if(FAILED(hres))
612 return hres;
614 for(idx=start; idx<end; idx++) {
615 VARIANT v;
617 hres = jsdisp_get_idx(jsthis, idx, &v, ei);
618 if(hres == DISP_E_UNKNOWNNAME)
619 continue;
621 if(SUCCEEDED(hres)) {
622 hres = jsdisp_propput_idx(arr, idx-start, &v, ei);
623 VariantClear(&v);
626 if(FAILED(hres)) {
627 jsdisp_release(arr);
628 return hres;
632 if(retv)
633 var_set_jsdisp(retv, arr);
634 else
635 jsdisp_release(arr);
637 return S_OK;
640 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei, INT *cmp)
642 HRESULT hres;
644 if(cmp_func) {
645 VARIANTARG args[2];
646 DISPPARAMS dp = {args, NULL, 2, 0};
647 double n;
648 VARIANT res;
650 args[0] = *v2;
651 args[1] = *v1;
653 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei);
654 if(FAILED(hres))
655 return hres;
657 hres = to_number(ctx, &res, ei, &n);
658 VariantClear(&res);
659 if(FAILED(hres))
660 return hres;
662 if(n == 0)
663 *cmp = 0;
664 *cmp = n > 0.0 ? 1 : -1;
665 }else if(V_VT(v1) == VT_EMPTY) {
666 *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
667 }else if(V_VT(v2) == VT_EMPTY) {
668 *cmp = -1;
669 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
670 DOUBLE d = num_val(v1)-num_val(v2);
671 if(d > 0.0)
672 *cmp = 1;
673 else
674 *cmp = d < -0.0 ? -1 : 0;
675 }else {
676 BSTR x, y;
678 hres = to_string(ctx, v1, ei, &x);
679 if(FAILED(hres))
680 return hres;
682 hres = to_string(ctx, v2, ei, &y);
683 if(SUCCEEDED(hres)) {
684 *cmp = strcmpW(x, y);
685 SysFreeString(y);
687 SysFreeString(x);
688 if(FAILED(hres))
689 return hres;
692 return S_OK;
695 /* ECMA-262 3rd Edition 15.4.4.11 */
696 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
697 VARIANT *retv, jsexcept_t *ei)
699 jsdisp_t *jsthis, *cmp_func = NULL;
700 VARIANT *vtab, **sorttab = NULL;
701 DWORD length;
702 DWORD i;
703 HRESULT hres = S_OK;
705 TRACE("\n");
707 hres = get_length(ctx, vthis, ei, &jsthis, &length);
708 if(FAILED(hres))
709 return hres;
711 if(arg_cnt(dp) > 1) {
712 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
713 return E_FAIL;
716 if(arg_cnt(dp) == 1) {
717 VARIANT *arg = get_arg(dp, 0);
719 if(V_VT(arg) != VT_DISPATCH) {
720 WARN("arg is not dispatch\n");
721 return E_FAIL;
725 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
726 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
727 WARN("cmp_func is not a function\n");
728 if(cmp_func)
729 jsdisp_release(cmp_func);
730 return E_FAIL;
734 if(!length) {
735 if(cmp_func)
736 jsdisp_release(cmp_func);
737 if(retv) {
738 jsdisp_addref(jsthis);
739 var_set_jsdisp(retv, jsthis);
741 return S_OK;
744 vtab = heap_alloc_zero(length * sizeof(VARIANT));
745 if(vtab) {
746 for(i=0; i<length; i++) {
747 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei);
748 if(hres == DISP_E_UNKNOWNNAME) {
749 V_VT(vtab+i) = VT_EMPTY;
750 hres = S_OK;
751 } else if(FAILED(hres)) {
752 WARN("Could not get elem %d: %08x\n", i, hres);
753 break;
756 }else {
757 hres = E_OUTOFMEMORY;
760 if(SUCCEEDED(hres)) {
761 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
762 if(!sorttab)
763 hres = E_OUTOFMEMORY;
766 /* merge-sort */
767 if(SUCCEEDED(hres)) {
768 VARIANT *tmpv, **tmpbuf;
769 INT cmp;
771 tmpbuf = sorttab + length;
772 for(i=0; i < length; i++)
773 sorttab[i] = vtab+i;
775 for(i=0; i < length/2; i++) {
776 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, &cmp);
777 if(FAILED(hres))
778 break;
780 if(cmp < 0) {
781 tmpv = sorttab[2*i];
782 sorttab[2*i] = sorttab[2*i+1];
783 sorttab[2*i+1] = tmpv;
787 if(SUCCEEDED(hres)) {
788 DWORD k, a, b, bend;
790 for(k=2; k < length; k *= 2) {
791 for(i=0; i+k < length; i += 2*k) {
792 a = b = 0;
793 if(i+2*k <= length)
794 bend = k;
795 else
796 bend = length - (i+k);
798 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
800 while(a < k && b < bend) {
801 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, &cmp);
802 if(FAILED(hres))
803 break;
805 if(cmp < 0) {
806 sorttab[i+a+b] = tmpbuf[a];
807 a++;
808 }else {
809 sorttab[i+a+b] = sorttab[i+k+b];
810 b++;
814 if(FAILED(hres))
815 break;
817 if(a < k)
818 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
821 if(FAILED(hres))
822 break;
826 for(i=0; SUCCEEDED(hres) && i < length; i++)
827 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei);
830 if(vtab) {
831 for(i=0; i < length; i++)
832 VariantClear(vtab+i);
833 heap_free(vtab);
835 heap_free(sorttab);
836 if(cmp_func)
837 jsdisp_release(cmp_func);
839 if(FAILED(hres))
840 return hres;
842 if(retv) {
843 jsdisp_addref(jsthis);
844 var_set_jsdisp(retv, jsthis);
847 return S_OK;
850 /* ECMA-262 3rd Edition 15.4.4.12 */
851 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
852 VARIANT *retv, jsexcept_t *ei)
854 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
855 jsdisp_t *ret_array = NULL, *jsthis;
856 VARIANT v;
857 double d;
858 int n;
859 HRESULT hres = S_OK;
861 TRACE("\n");
863 hres = get_length(ctx, vthis, ei, &jsthis, &length);
864 if(FAILED(hres))
865 return hres;
867 argc = arg_cnt(dp);
868 if(argc >= 1) {
869 hres = to_integer(ctx, get_arg(dp,0), ei, &d);
870 if(FAILED(hres))
871 return hres;
873 if(is_int32(d)) {
874 if((n = d) >= 0)
875 start = min(n, length);
876 else
877 start = -n > length ? 0 : length + n;
878 }else {
879 start = d < 0.0 ? 0 : length;
883 if(argc >= 2) {
884 hres = to_integer(ctx, get_arg(dp,1), ei, &d);
885 if(FAILED(hres))
886 return hres;
888 if(is_int32(d)) {
889 if((n = d) > 0)
890 delete_cnt = min(n, length-start);
891 }else if(d > 0.0) {
892 delete_cnt = length-start;
895 add_args = argc-2;
898 if(retv) {
899 hres = create_array(ctx, 0, &ret_array);
900 if(FAILED(hres))
901 return hres;
903 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
904 hres = jsdisp_get_idx(jsthis, start+i, &v, ei);
905 if(hres == DISP_E_UNKNOWNNAME)
906 hres = S_OK;
907 else if(SUCCEEDED(hres))
908 hres = jsdisp_propput_idx(ret_array, i, &v, ei);
911 if(SUCCEEDED(hres)) {
912 V_VT(&v) = VT_I4;
913 V_I4(&v) = delete_cnt;
915 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei);
919 if(add_args < delete_cnt) {
920 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
921 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei);
922 if(hres == DISP_E_UNKNOWNNAME)
923 hres = jsdisp_delete_idx(jsthis, i+add_args);
924 else if(SUCCEEDED(hres))
925 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei);
928 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
929 hres = jsdisp_delete_idx(jsthis, i-1);
930 }else if(add_args > delete_cnt) {
931 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
932 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei);
933 if(hres == DISP_E_UNKNOWNNAME)
934 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
935 else if(SUCCEEDED(hres))
936 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei);
940 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
941 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei);
943 if(SUCCEEDED(hres)) {
944 V_VT(&v) = VT_I4;
945 V_I4(&v) = length-delete_cnt+add_args;
946 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei);
949 if(FAILED(hres)) {
950 if(ret_array)
951 jsdisp_release(ret_array);
952 return hres;
955 if(retv)
956 var_set_jsdisp(retv, ret_array);
957 return S_OK;
960 /* ECMA-262 3rd Edition 15.4.4.2 */
961 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
962 VARIANT *retv, jsexcept_t *ei)
964 ArrayInstance *array;
966 TRACE("\n");
968 array = array_this(jsthis);
969 if(!array)
970 return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
972 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei);
975 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
976 VARIANT *retv, jsexcept_t *ei)
978 FIXME("\n");
979 return E_NOTIMPL;
982 /* ECMA-262 3rd Edition 15.4.4.13 */
983 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
984 VARIANT *retv, jsexcept_t *ei)
986 jsdisp_t *jsthis;
987 WCHAR buf[14], *buf_end, *str;
988 DWORD argc, i, length;
989 VARIANT var;
990 DISPID id;
991 HRESULT hres;
993 TRACE("\n");
995 hres = get_length(ctx, vthis, ei, &jsthis, &length);
996 if(FAILED(hres))
997 return hres;
999 argc = arg_cnt(dp);
1000 if(argc) {
1001 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
1002 *buf_end-- = 0;
1003 i = length;
1005 while(i--) {
1006 str = idx_to_str(i, buf_end);
1008 hres = jsdisp_get_id(jsthis, str, 0, &id);
1009 if(SUCCEEDED(hres)) {
1010 hres = jsdisp_propget(jsthis, id, &var, ei);
1011 if(FAILED(hres))
1012 return hres;
1014 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei);
1015 VariantClear(&var);
1016 }else if(hres == DISP_E_UNKNOWNNAME) {
1017 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1021 if(FAILED(hres))
1022 return hres;
1025 for(i=0; i<argc; i++) {
1026 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei);
1027 if(FAILED(hres))
1028 return hres;
1031 if(argc) {
1032 length += argc;
1033 hres = set_length(jsthis, ei, length);
1034 if(FAILED(hres))
1035 return hres;
1038 if(retv) {
1039 if(ctx->version < 2) {
1040 V_VT(retv) = VT_EMPTY;
1041 }else {
1042 V_VT(retv) = VT_I4;
1043 V_I4(retv) = length;
1046 return S_OK;
1049 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1050 VARIANT *retv, jsexcept_t *ei)
1052 TRACE("\n");
1054 switch(flags) {
1055 case INVOKE_FUNC:
1056 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1057 case INVOKE_PROPERTYGET:
1058 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei);
1059 default:
1060 FIXME("unimplemented flags %x\n", flags);
1061 return E_NOTIMPL;
1064 return S_OK;
1067 static void Array_destructor(jsdisp_t *dispex)
1069 heap_free(dispex);
1072 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1074 ArrayInstance *array = (ArrayInstance*)dispex;
1075 const WCHAR *ptr = name;
1076 DWORD id = 0;
1078 if(!isdigitW(*ptr))
1079 return;
1081 while(*ptr && isdigitW(*ptr)) {
1082 id = id*10 + (*ptr-'0');
1083 ptr++;
1086 if(*ptr)
1087 return;
1089 if(id >= array->length)
1090 array->length = id+1;
1093 static const builtin_prop_t Array_props[] = {
1094 {concatW, Array_concat, PROPF_METHOD|1},
1095 {joinW, Array_join, PROPF_METHOD|1},
1096 {lengthW, Array_length, 0},
1097 {popW, Array_pop, PROPF_METHOD},
1098 {pushW, Array_push, PROPF_METHOD|1},
1099 {reverseW, Array_reverse, PROPF_METHOD},
1100 {shiftW, Array_shift, PROPF_METHOD},
1101 {sliceW, Array_slice, PROPF_METHOD|2},
1102 {sortW, Array_sort, PROPF_METHOD|1},
1103 {spliceW, Array_splice, PROPF_METHOD|2},
1104 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1105 {toStringW, Array_toString, PROPF_METHOD},
1106 {unshiftW, Array_unshift, PROPF_METHOD|1},
1109 static const builtin_info_t Array_info = {
1110 JSCLASS_ARRAY,
1111 {NULL, Array_value, 0},
1112 sizeof(Array_props)/sizeof(*Array_props),
1113 Array_props,
1114 Array_destructor,
1115 Array_on_put
1118 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1119 VARIANT *retv, jsexcept_t *ei)
1121 jsdisp_t *obj;
1122 VARIANT *arg_var;
1123 DWORD i;
1124 HRESULT hres;
1126 TRACE("\n");
1128 switch(flags) {
1129 case DISPATCH_METHOD:
1130 case DISPATCH_CONSTRUCT: {
1131 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1132 if(V_I4(arg_var) < 0)
1133 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1135 hres = create_array(ctx, V_I4(arg_var), &obj);
1136 if(FAILED(hres))
1137 return hres;
1139 var_set_jsdisp(retv, obj);
1140 return S_OK;
1143 hres = create_array(ctx, arg_cnt(dp), &obj);
1144 if(FAILED(hres))
1145 return hres;
1147 for(i=0; i < arg_cnt(dp); i++) {
1148 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei);
1149 if(FAILED(hres))
1150 break;
1152 if(FAILED(hres)) {
1153 jsdisp_release(obj);
1154 return hres;
1157 var_set_jsdisp(retv, obj);
1158 break;
1160 default:
1161 FIXME("unimplemented flags: %x\n", flags);
1162 return E_NOTIMPL;
1165 return S_OK;
1168 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1170 ArrayInstance *array;
1171 HRESULT hres;
1173 array = heap_alloc_zero(sizeof(ArrayInstance));
1174 if(!array)
1175 return E_OUTOFMEMORY;
1177 if(object_prototype)
1178 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1179 else
1180 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1182 if(FAILED(hres)) {
1183 heap_free(array);
1184 return hres;
1187 *ret = array;
1188 return S_OK;
1191 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1193 ArrayInstance *array;
1194 HRESULT hres;
1196 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1198 hres = alloc_array(ctx, object_prototype, &array);
1199 if(FAILED(hres))
1200 return hres;
1202 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1204 jsdisp_release(&array->dispex);
1205 return hres;
1208 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1210 ArrayInstance *array;
1211 HRESULT hres;
1213 hres = alloc_array(ctx, NULL, &array);
1214 if(FAILED(hres))
1215 return hres;
1217 array->length = length;
1219 *ret = &array->dispex;
1220 return S_OK;