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;
102 return jsdisp_propput_name(obj, lengthW, &var, ei);
105 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
113 *ptr-- = '0' + (idx%10);
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);
128 case DISPATCH_PROPERTYGET:
130 V_I4(retv) = This->length;
132 case DISPATCH_PROPERTYPUT: {
137 hres = to_number(ctx, get_arg(dp, 0), ei, &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);
155 FIXME("unimplemented flags %x\n", flags);
162 static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
168 for(i=0; i < obj->length; i++) {
169 hres = jsdisp_get_idx(&obj->dispex, i, &var, ei);
170 if(hres == DISP_E_UNKNOWNNAME)
175 hres = jsdisp_propput_idx(array, *len+i, &var, ei);
185 static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
191 jsobj = iface_to_jsdisp((IUnknown*)obj);
193 if(is_class(jsobj, JSCLASS_ARRAY)) {
194 hres = concat_array(array, (ArrayInstance*)jsobj, len, ei);
195 jsdisp_release(jsobj);
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)
215 hres = create_array(ctx, 0, &ret);
219 hres = concat_obj(ret, jsthis->u.disp, &len, ei);
220 if(SUCCEEDED(hres)) {
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);
229 hres = jsdisp_propput_idx(ret, len++, arg, ei);
239 var_set_jsdisp(retv, ret);
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;
250 HRESULT hres = E_FAIL;
254 V_VT(retv) = VT_BSTR;
255 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
257 return E_OUTOFMEMORY;
262 str_tab = heap_alloc_zero(length * sizeof(BSTR));
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) {
271 } else if(FAILED(hres))
274 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
275 hres = to_string(ctx, &var, ei, str_tab+i);
281 if(SUCCEEDED(hres)) {
282 DWORD seplen = 0, len = 0;
285 seplen = strlenW(sep);
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);
297 tmplen = SysStringLen(str_tab[0]);
298 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
302 for(i=1; i < length; i++) {
304 memcpy(ptr, sep, seplen*sizeof(WCHAR));
309 tmplen = SysStringLen(str_tab[i]);
310 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
316 hres = E_OUTOFMEMORY;
320 for(i=0; i < length; i++)
321 SysFreeString(str_tab[i]);
326 TRACE("= %s\n", debugstr_w(ret));
330 ret = SysAllocStringLen(NULL, 0);
332 return E_OUTOFMEMORY;
335 V_VT(retv) = VT_BSTR;
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)
354 hres = get_length(ctx, vthis, ei, &jsthis, &length);
361 hres = to_string(ctx, get_arg(dp,0), ei, &sep);
365 hres = array_join(ctx, jsthis, length, sep, retv, ei);
369 hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei);
375 static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
376 VARIANT *retv, jsexcept_t *ei)
385 hres = get_length(ctx, vthis, ei, &jsthis, &length);
390 hres = set_length(jsthis, ei, 0);
395 V_VT(retv) = VT_EMPTY;
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;
410 hres = set_length(jsthis, ei, length);
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)
436 hres = get_length(ctx, vthis, ei, &jsthis, &length);
441 for(i=0; i < n; i++) {
442 hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei);
447 hres = set_length(jsthis, ei, length+n);
453 V_I4(retv) = length+n;
458 static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
459 VARIANT *retv, jsexcept_t *ei)
464 HRESULT hres1, hres2;
468 hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
472 for(k=0; k<length/2; k++) {
475 hres1 = jsdisp_get_idx(jsthis, k, &v1, ei);
476 if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
479 hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
480 if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
485 if(hres1 == DISP_E_UNKNOWNNAME)
486 hres1 = jsdisp_delete_idx(jsthis, l);
488 hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei);
496 if(hres2 == DISP_E_UNKNOWNNAME)
497 hres2 = jsdisp_delete_idx(jsthis, k);
499 hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei);
508 jsdisp_addref(jsthis);
509 var_set_jsdisp(retv, jsthis);
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)
526 hres = get_length(ctx, vthis, ei, &jsthis, &length);
531 hres = set_length(jsthis, ei, 0);
538 V_VT(retv) = VT_EMPTY;
542 hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
543 if(hres == DISP_E_UNKNOWNNAME) {
544 V_VT(&ret) = VT_EMPTY;
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);
559 hres = set_length(jsthis, ei, length-1);
562 if(SUCCEEDED(hres) && retv)
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;
575 DWORD length, start, end, idx;
580 hres = get_length(ctx, vthis, ei, &jsthis, &length);
585 hres = to_number(ctx, get_arg(dp, 0), ei, &range);
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;
598 hres = to_number(ctx, get_arg(dp, 1), ei, &range);
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;
610 hres = create_array(ctx, (end>start)?end-start:0, &arr);
614 for(idx=start; idx<end; idx++) {
617 hres = jsdisp_get_idx(jsthis, idx, &v, ei);
618 if(hres == DISP_E_UNKNOWNNAME)
621 if(SUCCEEDED(hres)) {
622 hres = jsdisp_propput_idx(arr, idx-start, &v, ei);
633 var_set_jsdisp(retv, arr);
640 static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei, INT *cmp)
646 DISPPARAMS dp = {args, NULL, 2, 0};
653 hres = jsdisp_call_value(cmp_func, DISPATCH_METHOD, &dp, &res, ei);
657 hres = to_number(ctx, &res, ei, &n);
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) {
669 }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
670 DOUBLE d = num_val(v1)-num_val(v2);
674 *cmp = d < -0.0 ? -1 : 0;
678 hres = to_string(ctx, v1, ei, &x);
682 hres = to_string(ctx, v2, ei, &y);
683 if(SUCCEEDED(hres)) {
684 *cmp = strcmpW(x, y);
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;
707 hres = get_length(ctx, vthis, ei, &jsthis, &length);
711 if(arg_cnt(dp) > 1) {
712 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
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");
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");
729 jsdisp_release(cmp_func);
736 jsdisp_release(cmp_func);
738 jsdisp_addref(jsthis);
739 var_set_jsdisp(retv, jsthis);
744 vtab = heap_alloc_zero(length * sizeof(VARIANT));
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;
751 } else if(FAILED(hres)) {
752 WARN("Could not get elem %d: %08x\n", i, hres);
757 hres = E_OUTOFMEMORY;
760 if(SUCCEEDED(hres)) {
761 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
763 hres = E_OUTOFMEMORY;
767 if(SUCCEEDED(hres)) {
768 VARIANT *tmpv, **tmpbuf;
771 tmpbuf = sorttab + length;
772 for(i=0; i < length; 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);
782 sorttab[2*i] = sorttab[2*i+1];
783 sorttab[2*i+1] = tmpv;
787 if(SUCCEEDED(hres)) {
790 for(k=2; k < length; k *= 2) {
791 for(i=0; i+k < length; i += 2*k) {
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);
806 sorttab[i+a+b] = tmpbuf[a];
809 sorttab[i+a+b] = sorttab[i+k+b];
818 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
826 for(i=0; SUCCEEDED(hres) && i < length; i++)
827 hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei);
831 for(i=0; i < length; i++)
832 VariantClear(vtab+i);
837 jsdisp_release(cmp_func);
843 jsdisp_addref(jsthis);
844 var_set_jsdisp(retv, jsthis);
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;
863 hres = get_length(ctx, vthis, ei, &jsthis, &length);
869 hres = to_integer(ctx, get_arg(dp,0), ei, &d);
875 start = min(n, length);
877 start = -n > length ? 0 : length + n;
879 start = d < 0.0 ? 0 : length;
884 hres = to_integer(ctx, get_arg(dp,1), ei, &d);
890 delete_cnt = min(n, length-start);
892 delete_cnt = length-start;
899 hres = create_array(ctx, 0, &ret_array);
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)
907 else if(SUCCEEDED(hres))
908 hres = jsdisp_propput_idx(ret_array, i, &v, ei);
911 if(SUCCEEDED(hres)) {
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)) {
945 V_I4(&v) = length-delete_cnt+add_args;
946 hres = jsdisp_propput_name(jsthis, lengthW, &v, ei);
951 jsdisp_release(ret_array);
956 var_set_jsdisp(retv, ret_array);
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;
968 array = array_this(jsthis);
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)
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)
987 WCHAR buf[14], *buf_end, *str;
988 DWORD argc, i, length;
995 hres = get_length(ctx, vthis, ei, &jsthis, &length);
1001 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
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);
1014 hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei);
1016 }else if(hres == DISP_E_UNKNOWNNAME) {
1017 hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
1025 for(i=0; i<argc; i++) {
1026 hres = jsdisp_propput_idx(jsthis, i, get_arg(dp,i), ei);
1033 hres = set_length(jsthis, ei, length);
1039 if(ctx->version < 2) {
1040 V_VT(retv) = VT_EMPTY;
1043 V_I4(retv) = length;
1049 static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
1050 VARIANT *retv, jsexcept_t *ei)
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);
1060 FIXME("unimplemented flags %x\n", flags);
1067 static void Array_destructor(jsdisp_t *dispex)
1072 static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1074 ArrayInstance *array = (ArrayInstance*)dispex;
1075 const WCHAR *ptr = name;
1081 while(*ptr && isdigitW(*ptr)) {
1082 id = id*10 + (*ptr-'0');
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 = {
1111 {NULL, Array_value, 0},
1112 sizeof(Array_props)/sizeof(*Array_props),
1118 static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp,
1119 VARIANT *retv, jsexcept_t *ei)
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);
1139 var_set_jsdisp(retv, obj);
1143 hres = create_array(ctx, arg_cnt(dp), &obj);
1147 for(i=0; i < arg_cnt(dp); i++) {
1148 hres = jsdisp_propput_idx(obj, i, get_arg(dp, i), ei);
1153 jsdisp_release(obj);
1157 var_set_jsdisp(retv, obj);
1161 FIXME("unimplemented flags: %x\n", flags);
1168 static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1170 ArrayInstance *array;
1173 array = heap_alloc_zero(sizeof(ArrayInstance));
1175 return E_OUTOFMEMORY;
1177 if(object_prototype)
1178 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1180 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1191 HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1193 ArrayInstance *array;
1196 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1198 hres = alloc_array(ctx, object_prototype, &array);
1202 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1204 jsdisp_release(&array->dispex);
1208 HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1210 ArrayInstance *array;
1213 hres = alloc_array(ctx, NULL, &array);
1217 array->length = length;
1219 *ret = &array->dispex;