cfgmgr32.h: Define MAX_DEVICE_ID_LEN.
[wine/multimedia.git] / dlls / jscript / array.c
blob8ce35cf1b6ea00b102cfab07b812cecd535b1d31
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 DispatchEx 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, DispatchEx **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, IDS_JSCRIPT_EXPECTED, NULL);
75 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei, NULL/*FIXME*/);
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(DispatchEx *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, NULL/*FIXME*/);
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, IServiceProvider *sp)
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 VARIANT num;
131 DOUBLE len = -1;
132 DWORD i;
133 HRESULT hres;
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));
141 if(len!=(DWORD)len)
142 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
144 for(i=len; i<This->length; i++) {
145 hres = jsdisp_delete_idx(&This->dispex, i);
146 if(FAILED(hres))
147 return hres;
150 This->length = len;
151 break;
153 default:
154 FIXME("unimplemented flags %x\n", flags);
155 return E_NOTIMPL;
158 return S_OK;
161 static HRESULT concat_array(DispatchEx *array, ArrayInstance *obj, DWORD *len,
162 jsexcept_t *ei, IServiceProvider *caller)
164 VARIANT var;
165 DWORD i;
166 HRESULT hres;
168 for(i=0; i < obj->length; i++) {
169 hres = jsdisp_propget_idx(&obj->dispex, i, &var, ei, caller);
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, caller);
176 VariantClear(&var);
177 if(FAILED(hres))
178 return hres;
181 *len += obj->length;
182 return S_OK;
185 static HRESULT concat_obj(DispatchEx *array, IDispatch *obj, DWORD *len, jsexcept_t *ei, IServiceProvider *caller)
187 DispatchEx *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, caller);
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, caller);
206 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
207 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
209 DispatchEx *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, caller);
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, caller);
228 else
229 hres = jsdisp_propput_idx(ret, len++, arg, ei, caller);
230 if(FAILED(hres))
231 break;
235 if(FAILED(hres))
236 return hres;
238 if(retv) {
239 V_VT(retv) = VT_DISPATCH;
240 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
241 }else {
242 jsdisp_release(ret);
244 return S_OK;
247 static HRESULT array_join(script_ctx_t *ctx, DispatchEx *array, DWORD length, const WCHAR *sep, VARIANT *retv,
248 jsexcept_t *ei, IServiceProvider *caller)
250 BSTR *str_tab, ret = NULL;
251 VARIANT var;
252 DWORD i;
253 HRESULT hres = E_FAIL;
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;
262 return S_OK;
265 str_tab = heap_alloc_zero(length * sizeof(BSTR));
266 if(!str_tab)
267 return E_OUTOFMEMORY;
269 for(i=0; i < length; i++) {
270 hres = jsdisp_propget_idx(array, i, &var, ei, caller);
271 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 *jsthis, WORD flags, DISPPARAMS *dp,
346 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
348 DWORD length;
349 HRESULT hres;
351 TRACE("\n");
353 if(is_vclass(jsthis, JSCLASS_ARRAY)) {
354 length = array_from_vdisp(jsthis)->length;
355 }else {
356 FIXME("dispid is not Array\n");
357 return E_NOTIMPL;
360 if(arg_cnt(dp)) {
361 BSTR sep;
363 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
364 if(FAILED(hres))
365 return hres;
367 hres = array_join(ctx, jsthis->u.jsdisp, length, sep, retv, ei, caller);
369 SysFreeString(sep);
370 }else {
371 hres = array_join(ctx, jsthis->u.jsdisp, length, default_separatorW, retv, ei, caller);
374 return hres;
377 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
378 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
380 VARIANT val;
381 DWORD length;
382 WCHAR buf[14];
383 DISPID id;
384 HRESULT hres;
386 static const WCHAR formatW[] = {'%','d',0};
388 TRACE("\n");
390 if(is_vclass(jsthis, JSCLASS_ARRAY)) {
391 ArrayInstance *array = array_from_vdisp(jsthis);
392 length = array->length;
393 }else {
394 FIXME("not Array this\n");
395 return E_NOTIMPL;
398 if(!length) {
399 if(retv)
400 V_VT(retv) = VT_EMPTY;
401 return S_OK;
404 sprintfW(buf, formatW, --length);
405 hres = jsdisp_get_id(jsthis->u.jsdisp, buf, 0, &id);
406 if(SUCCEEDED(hres)) {
407 hres = jsdisp_propget(jsthis->u.jsdisp, id, &val, ei, caller);
408 if(FAILED(hres))
409 return hres;
411 hres = IDispatchEx_DeleteMemberByDispID(jsthis->u.dispex, id);
412 }else if(hres == DISP_E_UNKNOWNNAME) {
413 V_VT(&val) = VT_EMPTY;
414 hres = S_OK;
415 }else {
416 return hres;
419 if(SUCCEEDED(hres)) {
420 ArrayInstance *array = array_from_vdisp(jsthis);
421 array->length = length;
424 if(FAILED(hres)) {
425 VariantClear(&val);
426 return hres;
429 if(retv)
430 *retv = val;
431 else
432 VariantClear(&val);
433 return S_OK;
436 /* ECMA-262 3rd Edition 15.4.4.7 */
437 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
438 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
440 DispatchEx *jsthis;
441 DWORD length = 0;
442 int i, n;
443 HRESULT hres;
445 TRACE("\n");
447 hres = get_length(ctx, vthis, ei, &jsthis, &length);
448 if(FAILED(hres))
449 return hres;
451 n = arg_cnt(dp);
452 for(i=0; i < n; i++) {
453 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei, sp);
454 if(FAILED(hres))
455 return hres;
458 hres = set_length(jsthis, ei, length+n);
459 if(FAILED(hres))
460 return hres;
462 if(retv) {
463 V_VT(retv) = VT_I4;
464 V_I4(retv) = length+n;
466 return S_OK;
469 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
470 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
472 FIXME("\n");
473 return E_NOTIMPL;
476 /* ECMA-262 3rd Edition 15.4.4.9 */
477 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
478 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
480 DispatchEx *jsthis;
481 DWORD length = 0, i;
482 VARIANT v, ret;
483 HRESULT hres;
485 TRACE("\n");
487 hres = get_length(ctx, vthis, ei, &jsthis, &length);
488 if(FAILED(hres))
489 return hres;
491 if(!length) {
492 hres = set_length(jsthis, ei, 0);
493 if(FAILED(hres))
494 return hres;
497 if(!length) {
498 if(retv)
499 V_VT(retv) = VT_EMPTY;
500 return S_OK;
503 hres = jsdisp_propget_idx(jsthis, 0, &ret, ei, caller);
504 if(hres == DISP_E_UNKNOWNNAME) {
505 V_VT(&ret) = VT_EMPTY;
506 hres = S_OK;
509 for(i=1; SUCCEEDED(hres) && i<length; i++) {
510 hres = jsdisp_propget_idx(jsthis, i, &v, ei, caller);
511 if(hres == DISP_E_UNKNOWNNAME)
512 hres = jsdisp_delete_idx(jsthis, i-1);
513 else if(SUCCEEDED(hres))
514 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei, caller);
517 if(SUCCEEDED(hres)) {
518 hres = jsdisp_delete_idx(jsthis, length-1);
519 if(SUCCEEDED(hres))
520 hres = set_length(jsthis, ei, length-1);
523 if(SUCCEEDED(hres) && retv)
524 *retv = ret;
525 else
526 VariantClear(&ret);
527 return hres;
530 /* ECMA-262 3rd Edition 15.4.4.10 */
531 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
532 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
534 DispatchEx *arr, *jsthis;
535 VARIANT v;
536 DOUBLE range;
537 DWORD length, start, end, idx;
538 HRESULT hres;
540 TRACE("\n");
542 hres = get_length(ctx, vthis, ei, &jsthis, &length);
543 if(FAILED(hres))
544 return hres;
546 if(arg_cnt(dp)) {
547 hres = to_number(ctx, get_arg(dp, 0), ei, &v);
548 if(FAILED(hres))
549 return hres;
551 if(V_VT(&v) == VT_I4)
552 range = V_I4(&v);
553 else
554 range = floor(V_R8(&v));
556 if(-range>length || isnan(range)) start = 0;
557 else if(range < 0) start = range+length;
558 else if(range <= length) start = range;
559 else start = length;
561 else start = 0;
563 if(arg_cnt(dp)>1) {
564 hres = to_number(ctx, get_arg(dp, 1), ei, &v);
565 if(FAILED(hres))
566 return hres;
568 if(V_VT(&v) == VT_I4)
569 range = V_I4(&v);
570 else
571 range = floor(V_R8(&v));
573 if(-range>length) end = 0;
574 else if(range < 0) end = range+length;
575 else if(range <= length) end = range;
576 else end = length;
578 else end = length;
580 hres = create_array(ctx, (end>start)?end-start:0, &arr);
581 if(FAILED(hres))
582 return hres;
584 for(idx=start; idx<end; idx++) {
585 hres = jsdisp_propget_idx(jsthis, idx, &v, ei, sp);
586 if(hres == DISP_E_UNKNOWNNAME)
587 continue;
589 if(SUCCEEDED(hres))
590 hres = jsdisp_propput_idx(arr, idx-start, &v, ei, sp);
592 if(FAILED(hres)) {
593 jsdisp_release(arr);
594 return hres;
598 if(retv) {
599 V_VT(retv) = VT_DISPATCH;
600 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
602 else
603 jsdisp_release(arr);
605 return S_OK;
608 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
609 IServiceProvider *caller, INT *cmp)
611 HRESULT hres;
613 if(cmp_func) {
614 VARIANTARG args[2];
615 DISPPARAMS dp = {args, NULL, 2, 0};
616 VARIANT tmp;
617 VARIANT res;
619 args[0] = *v2;
620 args[1] = *v1;
622 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei, caller);
623 if(FAILED(hres))
624 return hres;
626 hres = to_number(ctx, &res, ei, &tmp);
627 VariantClear(&res);
628 if(FAILED(hres))
629 return hres;
631 if(V_VT(&tmp) == VT_I4)
632 *cmp = V_I4(&tmp);
633 else
634 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
635 }else if(is_num_vt(V_VT(v1))) {
636 if(is_num_vt(V_VT(v2))) {
637 DOUBLE d = num_val(v1)-num_val(v2);
638 if(d > 0.0)
639 *cmp = 1;
640 else if(d < -0.0)
641 *cmp = -1;
642 else
643 *cmp = 0;
644 }else {
645 *cmp = -1;
647 }else if(is_num_vt(V_VT(v2))) {
648 *cmp = 1;
649 }else if(V_VT(v1) == VT_BSTR) {
650 if(V_VT(v2) == VT_BSTR)
651 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
652 else
653 *cmp = -1;
654 }else if(V_VT(v2) == VT_BSTR) {
655 *cmp = 1;
656 }else {
657 *cmp = 0;
660 return S_OK;
663 /* ECMA-262 3rd Edition 15.4.4.11 */
664 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
665 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
667 DispatchEx *cmp_func = NULL;
668 VARIANT *vtab, **sorttab = NULL;
669 DWORD length;
670 DWORD i;
671 HRESULT hres = S_OK;
673 TRACE("\n");
675 if(is_vclass(jsthis, JSCLASS_ARRAY)) {
676 length = array_from_vdisp(jsthis)->length;
677 }else {
678 FIXME("unsupported this not array\n");
679 return E_NOTIMPL;
682 if(arg_cnt(dp) > 1) {
683 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
684 return E_FAIL;
687 if(arg_cnt(dp) == 1) {
688 VARIANT *arg = get_arg(dp, 0);
690 if(V_VT(arg) != VT_DISPATCH) {
691 WARN("arg is not dispatch\n");
692 return E_FAIL;
696 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
697 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
698 WARN("cmp_func is not a function\n");
699 if(cmp_func)
700 jsdisp_release(cmp_func);
701 return E_FAIL;
705 if(!length) {
706 if(cmp_func)
707 jsdisp_release(cmp_func);
708 if(retv) {
709 V_VT(retv) = VT_DISPATCH;
710 V_DISPATCH(retv) = jsthis->u.disp;
711 IDispatch_AddRef(jsthis->u.disp);
713 return S_OK;
716 vtab = heap_alloc_zero(length * sizeof(VARIANT));
717 if(vtab) {
718 for(i=0; i<length; i++) {
719 hres = jsdisp_propget_idx(jsthis->u.jsdisp, i, vtab+i, ei, caller);
720 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
721 WARN("Could not get elem %d: %08x\n", i, hres);
722 break;
725 }else {
726 hres = E_OUTOFMEMORY;
729 if(SUCCEEDED(hres)) {
730 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
731 if(!sorttab)
732 hres = E_OUTOFMEMORY;
735 /* merge-sort */
736 if(SUCCEEDED(hres)) {
737 VARIANT *tmpv, **tmpbuf;
738 INT cmp;
740 tmpbuf = sorttab + length;
741 for(i=0; i < length; i++)
742 sorttab[i] = vtab+i;
744 for(i=0; i < length/2; i++) {
745 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
746 if(FAILED(hres))
747 break;
749 if(cmp < 0) {
750 tmpv = sorttab[2*i];
751 sorttab[2*i] = sorttab[2*i+1];
752 sorttab[2*i+1] = tmpv;
756 if(SUCCEEDED(hres)) {
757 DWORD k, a, b, bend;
759 for(k=2; k < length; k *= 2) {
760 for(i=0; i+k < length; i += 2*k) {
761 a = b = 0;
762 if(i+2*k <= length)
763 bend = k;
764 else
765 bend = length - (i+k);
767 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
769 while(a < k && b < bend) {
770 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
771 if(FAILED(hres))
772 break;
774 if(cmp < 0) {
775 sorttab[i+a+b] = tmpbuf[a];
776 a++;
777 }else {
778 sorttab[i+a+b] = sorttab[i+k+b];
779 b++;
783 if(FAILED(hres))
784 break;
786 if(a < k)
787 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
790 if(FAILED(hres))
791 break;
795 for(i=0; SUCCEEDED(hres) && i < length; i++)
796 hres = jsdisp_propput_idx(jsthis->u.jsdisp, i, sorttab[i], ei, caller);
799 if(vtab) {
800 for(i=0; i < length; i++)
801 VariantClear(vtab+i);
802 heap_free(vtab);
804 heap_free(sorttab);
805 if(cmp_func)
806 jsdisp_release(cmp_func);
808 if(FAILED(hres))
809 return hres;
811 if(retv) {
812 V_VT(retv) = VT_DISPATCH;
813 V_DISPATCH(retv) = jsthis->u.disp;
814 IDispatch_AddRef(jsthis->u.disp);
817 return S_OK;
820 /* ECMA-262 3rd Edition 15.4.4.12 */
821 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
822 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
824 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
825 DispatchEx *ret_array = NULL, *jsthis;
826 VARIANT v;
827 HRESULT hres = S_OK;
829 TRACE("\n");
831 hres = get_length(ctx, vthis, ei, &jsthis, &length);
832 if(FAILED(hres))
833 return hres;
835 argc = arg_cnt(dp);
836 if(argc >= 1) {
837 hres = to_integer(ctx, get_arg(dp,0), ei, &v);
838 if(FAILED(hres))
839 return hres;
841 if(V_VT(&v) == VT_I4) {
842 if(V_I4(&v) >= 0)
843 start = min(V_I4(&v), length);
844 else
845 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
846 }else {
847 start = V_R8(&v) < 0.0 ? 0 : length;
851 if(argc >= 2) {
852 hres = to_integer(ctx, get_arg(dp,1), ei, &v);
853 if(FAILED(hres))
854 return hres;
856 if(V_VT(&v) == VT_I4) {
857 if(V_I4(&v) > 0)
858 delete_cnt = min(V_I4(&v), length-start);
859 }else if(V_R8(&v) > 0.0) {
860 delete_cnt = length-start;
863 add_args = argc-2;
866 if(retv) {
867 hres = create_array(ctx, 0, &ret_array);
868 if(FAILED(hres))
869 return hres;
871 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
872 hres = jsdisp_propget_idx(jsthis, start+i, &v, ei, caller);
873 if(hres == DISP_E_UNKNOWNNAME)
874 hres = S_OK;
875 else if(SUCCEEDED(hres))
876 hres = jsdisp_propput_idx(ret_array, i, &v, ei, caller);
879 if(SUCCEEDED(hres)) {
880 V_VT(&v) = VT_I4;
881 V_I4(&v) = delete_cnt;
883 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei, caller);
887 if(add_args < delete_cnt) {
888 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
889 hres = jsdisp_propget_idx(jsthis, i+delete_cnt, &v, ei, caller);
890 if(hres == DISP_E_UNKNOWNNAME)
891 hres = jsdisp_delete_idx(jsthis, i+add_args);
892 else if(SUCCEEDED(hres))
893 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei, caller);
896 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
897 hres = jsdisp_delete_idx(jsthis, i-1);
898 }else if(add_args > delete_cnt) {
899 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
900 hres = jsdisp_propget_idx(jsthis, i+delete_cnt-1, &v, ei, caller);
901 if(hres == DISP_E_UNKNOWNNAME)
902 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
903 else if(SUCCEEDED(hres))
904 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei, caller);
908 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
909 hres = jsdisp_propput_idx(jsthis, start+i, get_arg(dp,i+2), ei, caller);
911 if(SUCCEEDED(hres)) {
912 V_VT(&v) = VT_I4;
913 V_I4(&v) = length-delete_cnt+add_args;
914 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei, caller);
917 if(FAILED(hres)) {
918 if(ret_array)
919 jsdisp_release(ret_array);
920 return hres;
923 if(retv) {
924 V_VT(retv) = VT_DISPATCH;
925 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret_array);
927 return S_OK;
930 /* ECMA-262 3rd Edition 15.4.4.2 */
931 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
932 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
934 ArrayInstance *array;
936 TRACE("\n");
938 array = array_this(jsthis);
939 if(!array)
940 return throw_type_error(ctx, ei, IDS_ARRAY_EXPECTED, NULL);
942 return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei, sp);
945 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
946 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
948 FIXME("\n");
949 return E_NOTIMPL;
952 /* ECMA-262 3rd Edition 15.4.4.13 */
953 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
954 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
956 DispatchEx *jsthis;
957 WCHAR buf[14], *buf_end, *str;
958 DWORD argc, i, length;
959 VARIANT var;
960 DISPID id;
961 HRESULT hres;
963 TRACE("\n");
965 hres = get_length(ctx, vthis, ei, &jsthis, &length);
966 if(FAILED(hres))
967 return hres;
969 argc = arg_cnt(dp);
970 if(argc) {
971 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
972 *buf_end-- = 0;
973 i = length;
975 while(i--) {
976 str = idx_to_str(i, buf_end);
978 hres = jsdisp_get_id(jsthis, str, 0, &id);
979 if(SUCCEEDED(hres)) {
980 hres = jsdisp_propget(jsthis, id, &var, ei, caller);
981 if(FAILED(hres))
982 return hres;
984 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei, caller);
985 VariantClear(&var);
986 }else if(hres == DISP_E_UNKNOWNNAME) {
987 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
991 if(FAILED(hres))
992 return hres;
995 for(i=0; i<argc; i++) {
996 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei, caller);
997 if(FAILED(hres))
998 return hres;
1001 if(argc) {
1002 length += argc;
1003 hres = set_length(jsthis, ei, length);
1004 if(FAILED(hres))
1005 return hres;
1008 if(retv) {
1009 if(ctx->version < 2) {
1010 V_VT(retv) = VT_EMPTY;
1011 }else {
1012 V_VT(retv) = VT_I4;
1013 V_I4(retv) = length;
1016 return S_OK;
1019 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1020 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
1022 TRACE("\n");
1024 switch(flags) {
1025 case INVOKE_FUNC:
1026 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
1027 case INVOKE_PROPERTYGET:
1028 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei, sp);
1029 default:
1030 FIXME("unimplemented flags %x\n", flags);
1031 return E_NOTIMPL;
1034 return S_OK;
1037 static void Array_destructor(DispatchEx *dispex)
1039 heap_free(dispex);
1042 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
1044 ArrayInstance *array = (ArrayInstance*)dispex;
1045 const WCHAR *ptr = name;
1046 DWORD id = 0;
1048 if(!isdigitW(*ptr))
1049 return;
1051 while(*ptr && isdigitW(*ptr)) {
1052 id = id*10 + (*ptr-'0');
1053 ptr++;
1056 if(*ptr)
1057 return;
1059 if(id >= array->length)
1060 array->length = id+1;
1063 static const builtin_prop_t Array_props[] = {
1064 {concatW, Array_concat, PROPF_METHOD|1},
1065 {joinW, Array_join, PROPF_METHOD|1},
1066 {lengthW, Array_length, 0},
1067 {popW, Array_pop, PROPF_METHOD},
1068 {pushW, Array_push, PROPF_METHOD|1},
1069 {reverseW, Array_reverse, PROPF_METHOD},
1070 {shiftW, Array_shift, PROPF_METHOD},
1071 {sliceW, Array_slice, PROPF_METHOD|2},
1072 {sortW, Array_sort, PROPF_METHOD|1},
1073 {spliceW, Array_splice, PROPF_METHOD|2},
1074 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1075 {toStringW, Array_toString, PROPF_METHOD},
1076 {unshiftW, Array_unshift, PROPF_METHOD|1},
1079 static const builtin_info_t Array_info = {
1080 JSCLASS_ARRAY,
1081 {NULL, Array_value, 0},
1082 sizeof(Array_props)/sizeof(*Array_props),
1083 Array_props,
1084 Array_destructor,
1085 Array_on_put
1088 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1089 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1091 DispatchEx *obj;
1092 VARIANT *arg_var;
1093 DWORD i;
1094 HRESULT hres;
1096 TRACE("\n");
1098 switch(flags) {
1099 case DISPATCH_METHOD:
1100 case DISPATCH_CONSTRUCT: {
1101 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1102 if(V_I4(arg_var) < 0)
1103 return throw_range_error(ctx, ei, IDS_INVALID_LENGTH, NULL);
1105 hres = create_array(ctx, V_I4(arg_var), &obj);
1106 if(FAILED(hres))
1107 return hres;
1109 V_VT(retv) = VT_DISPATCH;
1110 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1111 return S_OK;
1114 hres = create_array(ctx, arg_cnt(dp), &obj);
1115 if(FAILED(hres))
1116 return hres;
1118 for(i=0; i < arg_cnt(dp); i++) {
1119 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei, caller);
1120 if(FAILED(hres))
1121 break;
1123 if(FAILED(hres)) {
1124 jsdisp_release(obj);
1125 return hres;
1128 V_VT(retv) = VT_DISPATCH;
1129 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1130 break;
1132 default:
1133 FIXME("unimplemented flags: %x\n", flags);
1134 return E_NOTIMPL;
1137 return S_OK;
1140 static HRESULT alloc_array(script_ctx_t *ctx, DispatchEx *object_prototype, ArrayInstance **ret)
1142 ArrayInstance *array;
1143 HRESULT hres;
1145 array = heap_alloc_zero(sizeof(ArrayInstance));
1146 if(!array)
1147 return E_OUTOFMEMORY;
1149 if(object_prototype)
1150 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1151 else
1152 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1154 if(FAILED(hres)) {
1155 heap_free(array);
1156 return hres;
1159 *ret = array;
1160 return S_OK;
1163 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1165 ArrayInstance *array;
1166 HRESULT hres;
1168 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1170 hres = alloc_array(ctx, object_prototype, &array);
1171 if(FAILED(hres))
1172 return hres;
1174 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1176 jsdisp_release(&array->dispex);
1177 return hres;
1180 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
1182 ArrayInstance *array;
1183 HRESULT hres;
1185 hres = alloc_array(ctx, NULL, &array);
1186 if(FAILED(hres))
1187 return hres;
1189 array->length = length;
1191 *ret = &array->dispex;
1192 return S_OK;