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
20 #include "wine/port.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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)
68 array = array_this(vdisp);
70 *jsthis = &array->dispex;
76 return throw_type_error(ctx, ei, JS_E_JSCRIPT_EXPECTED, NULL);
78 hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &var, ei);
82 hres = to_uint32(ctx, &var, ei, ret);
87 *jsthis = vdisp->u.jsdisp;
91 static HRESULT set_length(jsdisp_t *obj, jsexcept_t *ei, DWORD length)
95 if(is_class(obj, JSCLASS_ARRAY)) {
96 ((ArrayInstance*)obj)->length = length;
100 num_set_int(&var, length);
101 return jsdisp_propput_name(obj, lengthW, &var, ei);
104 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
112 *ptr-- = '0' + (idx%10);
119 static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
120 jsval_t *r, jsexcept_t *ei)
122 ArrayInstance *This = array_from_vdisp(jsthis);
124 TRACE("%p %d\n", This, This->length);
127 case DISPATCH_PROPERTYGET:
128 *r = jsval_number(This->length);
130 case DISPATCH_PROPERTYPUT: {
135 hres = to_number(ctx, argv, ei, &len);
141 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
143 for(i=len; i<This->length; i++) {
144 hres = jsdisp_delete_idx(&This->dispex, i);
153 FIXME("unimplemented flags %x\n", flags);
160 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
166 for(i=0; i < obj->length; i++) {
167 hres = jsdisp_get_idx(&obj->dispex, i, &var, ei);
168 if(hres == DISP_E_UNKNOWNNAME)
173 hres = jsdisp_propput_idx(array, *len+i, &var, ei);
183 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
189 jsobj = iface_to_jsdisp((IUnknown*)obj);
191 if(is_class(jsobj, JSCLASS_ARRAY)) {
192 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei);
193 jsdisp_release(jsobj);
196 jsdisp_release(jsobj);
199 V_VT(&var) = VT_DISPATCH;
200 V_DISPATCH(&var) = obj;
201 return jsdisp_propput_idx(array, (*len)++, &var, ei);
204 static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
205 jsval_t *r, jsexcept_t *ei)
213 hres = create_array(ctx, 0, &ret);
217 hres = concat_obj(ret, jsthis->u.disp, &len, ei);
218 if(SUCCEEDED(hres)) {
222 for(i=0; i < argc; i++) {
224 if(V_VT(arg) == VT_DISPATCH)
225 hres = concat_obj(ret, V_DISPATCH(arg), &len, ei);
227 hres = jsdisp_propput_idx(ret, len++, arg, ei);
243 static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, jsval_t *r, jsexcept_t *ei)
245 BSTR *str_tab, ret = NULL;
248 HRESULT hres = E_FAIL;
252 BSTR ret = SysAllocStringLen(NULL, 0);
254 return E_OUTOFMEMORY;
255 *r = jsval_string(ret);
260 str_tab = heap_alloc_zero(length * sizeof(BSTR));
262 return E_OUTOFMEMORY;
264 for(i=0; i < length; i++) {
265 hres = jsdisp_get_idx(array, i, &var, ei);
266 if(hres == DISP_E_UNKNOWNNAME) {
269 } else if(FAILED(hres))
272 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
273 hres = to_string(ctx, &var, ei, str_tab+i);
279 if(SUCCEEDED(hres)) {
280 DWORD seplen = 0, len = 0;
283 seplen = strlenW(sep);
286 len = SysStringLen(str_tab[0]);
287 for(i=1; i < length; i++)
288 len += seplen + SysStringLen(str_tab[i]);
290 ret = SysAllocStringLen(NULL, len);
295 tmplen = SysStringLen(str_tab[0]);
296 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
300 for(i=1; i < length; i++) {
302 memcpy(ptr, sep, seplen*sizeof(WCHAR));
307 tmplen = SysStringLen(str_tab[i]);
308 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
314 hres = E_OUTOFMEMORY;
318 for(i=0; i < length; i++)
319 SysFreeString(str_tab[i]);
324 TRACE("= %s\n", debugstr_w(ret));
328 ret = SysAllocStringLen(NULL, 0);
330 return E_OUTOFMEMORY;
333 *r = jsval_string(ret);
341 /* ECMA-262 3rd Edition 15.4.4.5 */
342 static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
343 jsval_t *r, jsexcept_t *ei)
351 hres = get_length(ctx, vthis, ei, &jsthis, &length);
358 hres = to_string(ctx, argv, ei, &sep);
362 hres = array_join(ctx, jsthis, length, sep, r, ei);
366 hres = array_join(ctx, jsthis, length, default_separatorW, r, ei);
372 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
373 jsval_t *r, jsexcept_t *ei)
382 hres = get_length(ctx, vthis, ei, &jsthis, &length);
387 hres = set_length(jsthis, ei, 0);
392 *r = jsval_undefined();
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;
407 hres = set_length(jsthis, ei, length);
415 hres = variant_to_jsval(&val, r);
420 /* ECMA-262 3rd Edition 15.4.4.7 */
421 static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
422 jsval_t *r, jsexcept_t *ei)
431 hres = get_length(ctx, vthis, ei, &jsthis, &length);
435 for(i=0; i < argc; i++) {
436 hres = jsdisp_propput_idx(jsthis, length+i, argv+i, ei);
441 hres = set_length(jsthis, ei, length+argc);
446 *r = jsval_number(length+argc);
450 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
451 jsval_t *r, jsexcept_t *ei)
456 HRESULT hres1, hres2;
460 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
464 for(k=0; k<length/2; k++) {
467 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei);
468 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
471 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
472 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
477 if(hres1 == DISP_E_UNKNOWNNAME)
478 hres1 = jsdisp_delete_idx(jsthis, l);
480 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei);
488 if(hres2 == DISP_E_UNKNOWNNAME)
489 hres2 = jsdisp_delete_idx(jsthis, k);
491 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei);
500 *r = jsval_obj(jsdisp_addref(jsthis));
504 /* ECMA-262 3rd Edition 15.4.4.9 */
505 static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
506 jsval_t *r, jsexcept_t *ei)
515 hres = get_length(ctx, vthis, ei, &jsthis, &length);
520 hres = set_length(jsthis, ei, 0);
527 *r = jsval_undefined();
531 hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
532 if(hres == DISP_E_UNKNOWNNAME) {
533 V_VT(&ret) = VT_EMPTY;
537 for(i=1; SUCCEEDED(hres) && i<length; i++) {
538 hres = jsdisp_get_idx(jsthis, i, &v, ei);
539 if(hres == DISP_E_UNKNOWNNAME)
540 hres = jsdisp_delete_idx(jsthis, i-1);
541 else if(SUCCEEDED(hres))
542 hres = jsdisp_propput_idx(jsthis, i-1, &v, ei);
545 if(SUCCEEDED(hres)) {
546 hres = jsdisp_delete_idx(jsthis, length-1);
548 hres = set_length(jsthis, ei, length-1);
551 if(SUCCEEDED(hres) && r)
552 hres = variant_to_jsval(&ret, r);
557 /* ECMA-262 3rd Edition 15.4.4.10 */
558 static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
559 jsval_t *r, jsexcept_t *ei)
561 jsdisp_t *arr, *jsthis;
563 DWORD length, start, end, idx;
568 hres = get_length(ctx, vthis, ei, &jsthis, &length);
573 hres = to_number(ctx, argv, ei, &range);
577 range = floor(range);
578 if(-range>length || isnan(range)) start = 0;
579 else if(range < 0) start = range+length;
580 else if(range <= length) start = range;
586 hres = to_number(ctx, argv+1, ei, &range);
590 range = floor(range);
591 if(-range>length) end = 0;
592 else if(range < 0) end = range+length;
593 else if(range <= length) end = range;
598 hres = create_array(ctx, (end>start)?end-start:0, &arr);
602 for(idx=start; idx<end; idx++) {
605 hres = jsdisp_get_idx(jsthis, idx, &v, ei);
606 if(hres == DISP_E_UNKNOWNNAME)
609 if(SUCCEEDED(hres)) {
610 hres = jsdisp_propput_idx(arr, idx-start, &v, ei);
628 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei, INT *cmp)
640 hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res, ei);
644 hres = to_number_jsval(ctx, res, ei, &n);
651 *cmp = n > 0.0 ? 1 : -1;
652 }else if(V_VT(v1) == VT_EMPTY) {
653 *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
654 }else if(V_VT(v2) == VT_EMPTY) {
656 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
657 DOUBLE d = num_val(v1)-num_val(v2);
661 *cmp = d < -0.0 ? -1 : 0;
665 hres = to_string(ctx, v1, ei, &x);
669 hres = to_string(ctx, v2, ei, &y);
670 if(SUCCEEDED(hres)) {
671 *cmp = strcmpW(x, y);
682 /* ECMA-262 3rd Edition 15.4.4.11 */
683 static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
684 jsval_t *r, jsexcept_t *ei)
686 jsdisp_t *jsthis, *cmp_func = NULL;
687 VARIANT *vtab, **sorttab = NULL;
694 hres = get_length(ctx, vthis, ei, &jsthis, &length);
699 WARN("invalid arg_cnt %d\n", argc);
704 if(V_VT(argv) != VT_DISPATCH) {
705 WARN("arg is not dispatch\n");
710 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(argv));
711 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
712 WARN("cmp_func is not a function\n");
714 jsdisp_release(cmp_func);
721 jsdisp_release(cmp_func);
723 *r = jsval_obj(jsdisp_addref(jsthis));
727 vtab = heap_alloc_zero(length * sizeof(VARIANT));
729 for(i=0; i<length; i++) {
730 hres = jsdisp_get_idx(jsthis, i, vtab+i, ei);
731 if(hres == DISP_E_UNKNOWNNAME) {
732 V_VT(vtab+i) = VT_EMPTY;
734 } else if(FAILED(hres)) {
735 WARN("Could not get elem %d: %08x\n", i, hres);
740 hres = E_OUTOFMEMORY;
743 if(SUCCEEDED(hres)) {
744 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
746 hres = E_OUTOFMEMORY;
750 if(SUCCEEDED(hres)) {
751 VARIANT *tmpv, **tmpbuf;
754 tmpbuf = sorttab + length;
755 for(i=0; i < length; i++)
758 for(i=0; i < length/2; i++) {
759 hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, &cmp);
765 sorttab[2*i] = sorttab[2*i+1];
766 sorttab[2*i+1] = tmpv;
770 if(SUCCEEDED(hres)) {
773 for(k=2; k < length; k *= 2) {
774 for(i=0; i+k < length; i += 2*k) {
779 bend = length - (i+k);
781 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
783 while(a < k && b < bend) {
784 hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, &cmp);
789 sorttab[i+a+b] = tmpbuf[a];
792 sorttab[i+a+b] = sorttab[i+k+b];
801 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
809 for(i=0; SUCCEEDED(hres) && i < length; i++)
810 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei);
814 for(i=0; i < length; i++)
815 VariantClear(vtab+i);
820 jsdisp_release(cmp_func);
826 *r = jsval_obj(jsdisp_addref(jsthis));
830 /* ECMA-262 3rd Edition 15.4.4.12 */
831 static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
832 jsval_t *r, jsexcept_t *ei)
834 DWORD length, start=0, delete_cnt=0, i, add_args = 0;
835 jsdisp_t *ret_array = NULL, *jsthis;
843 hres = get_length(ctx, vthis, ei, &jsthis, &length);
848 hres = to_integer(ctx, argv, ei, &d);
854 start = min(n, length);
856 start = -n > length ? 0 : length + n;
858 start = d < 0.0 ? 0 : length;
863 hres = to_integer(ctx, argv+1, ei, &d);
869 delete_cnt = min(n, length-start);
871 delete_cnt = length-start;
878 hres = create_array(ctx, 0, &ret_array);
882 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
883 hres = jsdisp_get_idx(jsthis, start+i, &v, ei);
884 if(hres == DISP_E_UNKNOWNNAME)
886 else if(SUCCEEDED(hres))
887 hres = jsdisp_propput_idx(ret_array, i, &v, ei);
890 if(SUCCEEDED(hres)) {
891 num_set_int(&v, delete_cnt);
892 hres = jsdisp_propput_name(ret_array, lengthW, &v, ei);
896 if(add_args < delete_cnt) {
897 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
898 hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei);
899 if(hres == DISP_E_UNKNOWNNAME)
900 hres = jsdisp_delete_idx(jsthis, i+add_args);
901 else if(SUCCEEDED(hres))
902 hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei);
905 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
906 hres = jsdisp_delete_idx(jsthis, i-1);
907 }else if(add_args > delete_cnt) {
908 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
909 hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei);
910 if(hres == DISP_E_UNKNOWNNAME)
911 hres = jsdisp_delete_idx(jsthis, i+add_args-1);
912 else if(SUCCEEDED(hres))
913 hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei);
917 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
918 hres = jsdisp_propput_idx(jsthis, start+i, argv+i+2, ei);
920 if(SUCCEEDED(hres)) {
921 num_set_int(&v, length-delete_cnt+add_args);
922 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei);
927 jsdisp_release(ret_array);
932 *r = jsval_obj(ret_array);
936 /* ECMA-262 3rd Edition 15.4.4.2 */
937 static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
938 jsval_t *r, jsexcept_t *ei)
940 ArrayInstance *array;
944 array = array_this(jsthis);
946 return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
948 return array_join(ctx, &array->dispex, array->length, default_separatorW, r, ei);
951 static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
952 jsval_t *r, jsexcept_t *ei)
958 /* ECMA-262 3rd Edition 15.4.4.13 */
959 static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
960 jsval_t *r, jsexcept_t *ei)
963 WCHAR buf[14], *buf_end, *str;
971 hres = get_length(ctx, vthis, ei, &jsthis, &length);
976 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
981 str = idx_to_str(i, buf_end);
983 hres = jsdisp_get_id(jsthis, str, 0, &id);
984 if(SUCCEEDED(hres)) {
985 hres = jsdisp_propget(jsthis, id, &var, ei);
989 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei);
991 }else if(hres == DISP_E_UNKNOWNNAME) {
992 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1000 for(i=0; i<argc; i++) {
1001 hres = jsdisp_propput_idx(jsthis, i, argv+i, ei);
1008 hres = set_length(jsthis, ei, length);
1014 *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
1018 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
1019 jsval_t *r, jsexcept_t *ei)
1025 return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1026 case INVOKE_PROPERTYGET:
1027 return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, r, ei);
1029 FIXME("unimplemented flags %x\n", flags);
1036 static void Array_destructor(jsdisp_t *dispex)
1041 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1043 ArrayInstance *array = (ArrayInstance*)dispex;
1044 const WCHAR *ptr = name;
1050 while(*ptr && isdigitW(*ptr)) {
1051 id = id*10 + (*ptr-'0');
1058 if(id >= array->length)
1059 array->length = id+1;
1062 static const builtin_prop_t Array_props[] = {
1063 {concatW, Array_concat, PROPF_METHOD|1},
1064 {joinW, Array_join, PROPF_METHOD|1},
1065 {lengthW, Array_length, 0},
1066 {popW, Array_pop, PROPF_METHOD},
1067 {pushW, Array_push, PROPF_METHOD|1},
1068 {reverseW, Array_reverse, PROPF_METHOD},
1069 {shiftW, Array_shift, PROPF_METHOD},
1070 {sliceW, Array_slice, PROPF_METHOD|2},
1071 {sortW, Array_sort, PROPF_METHOD|1},
1072 {spliceW, Array_splice, PROPF_METHOD|2},
1073 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1074 {toStringW, Array_toString, PROPF_METHOD},
1075 {unshiftW, Array_unshift, PROPF_METHOD|1},
1078 static const builtin_info_t Array_info = {
1080 {NULL, Array_value, 0},
1081 sizeof(Array_props)/sizeof(*Array_props),
1087 static const builtin_prop_t ArrayInst_props[] = {
1088 {lengthW, Array_length, 0}
1091 static const builtin_info_t ArrayInst_info = {
1093 {NULL, Array_value, 0},
1094 sizeof(ArrayInst_props)/sizeof(*ArrayInst_props),
1100 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
1101 jsval_t *r, jsexcept_t *ei)
1110 case DISPATCH_METHOD:
1111 case DISPATCH_CONSTRUCT: {
1112 if(argc == 1 && V_VT(argv) == VT_I4) {
1114 return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1116 hres = create_array(ctx, V_I4(argv), &obj);
1120 *r = jsval_obj(obj);
1124 hres = create_array(ctx, argc, &obj);
1128 for(i=0; i < argc; i++) {
1129 hres = jsdisp_propput_idx(obj, i, argv+i, ei);
1134 jsdisp_release(obj);
1138 *r = jsval_obj(obj);
1142 FIXME("unimplemented flags: %x\n", flags);
1149 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1151 ArrayInstance *array;
1154 array = heap_alloc_zero(sizeof(ArrayInstance));
1156 return E_OUTOFMEMORY;
1158 if(object_prototype)
1159 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1161 hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1172 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1174 ArrayInstance *array;
1177 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1179 hres = alloc_array(ctx, object_prototype, &array);
1183 hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1185 jsdisp_release(&array->dispex);
1189 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1191 ArrayInstance *array;
1194 hres = alloc_array(ctx, NULL, &array);
1198 array->length = length;
1200 *ret = &array->dispex;