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
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
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 HRESULT get_jsdisp_length(DispatchEx *obj, LCID lcid, jsexcept_t *ei, DWORD *ret)
54 hres = jsdisp_propget_name(obj, lengthW, lcid, &var, ei, NULL/*FIXME*/);
58 hres = to_uint32(obj->ctx, &var, ei, ret);
63 static HRESULT set_jsdisp_length(DispatchEx *obj, LCID lcid, jsexcept_t *ei, DWORD length)
69 return jsdisp_propput_name(obj, lengthW, lcid, &var, ei, NULL/*FIXME*/);
72 static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
80 *ptr-- = '0' + (idx%10);
87 static HRESULT Array_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
88 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
90 ArrayInstance *This = (ArrayInstance*)dispex;
92 TRACE("%p %d\n", This, This->length);
95 case DISPATCH_PROPERTYGET:
97 V_I4(retv) = This->length;
99 case DISPATCH_PROPERTYPUT: {
105 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
106 if(V_VT(&num) == VT_I4)
109 len = floor(V_R8(&num));
112 return throw_range_error(dispex->ctx, ei, IDS_INVALID_LENGTH, NULL);
114 for(i=len; i<This->length; i++) {
115 hres = jsdisp_delete_idx(dispex, i);
124 FIXME("unimplemented flags %x\n", flags);
131 static HRESULT concat_array(DispatchEx *array, ArrayInstance *obj, DWORD *len, LCID lcid,
132 jsexcept_t *ei, IServiceProvider *caller)
138 for(i=0; i < obj->length; i++) {
139 hres = jsdisp_propget_idx(&obj->dispex, i, lcid, &var, ei, caller);
140 if(hres == DISP_E_UNKNOWNNAME)
145 hres = jsdisp_propput_idx(array, *len+i, lcid, &var, ei, caller);
155 static HRESULT concat_obj(DispatchEx *array, IDispatch *obj, DWORD *len, LCID lcid, jsexcept_t *ei, IServiceProvider *caller)
161 jsobj = iface_to_jsdisp((IUnknown*)obj);
163 if(is_class(jsobj, JSCLASS_ARRAY)) {
164 hres = concat_array(array, (ArrayInstance*)jsobj, len, lcid, ei, caller);
165 jsdisp_release(jsobj);
168 jsdisp_release(jsobj);
171 V_VT(&var) = VT_DISPATCH;
172 V_DISPATCH(&var) = obj;
173 return jsdisp_propput_idx(array, (*len)++, lcid, &var, ei, caller);
176 static HRESULT Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
177 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
185 hres = create_array(dispex->ctx, 0, &ret);
189 hres = concat_obj(ret, (IDispatch*)_IDispatchEx_(dispex), &len, lcid, ei, caller);
190 if(SUCCEEDED(hres)) {
194 for(i=0; i < arg_cnt(dp); i++) {
195 arg = get_arg(dp, i);
196 if(V_VT(arg) == VT_DISPATCH)
197 hres = concat_obj(ret, V_DISPATCH(arg), &len, lcid, ei, caller);
199 hres = jsdisp_propput_idx(ret, len++, lcid, arg, ei, caller);
209 V_VT(retv) = VT_DISPATCH;
210 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
217 static HRESULT array_join(DispatchEx *array, LCID lcid, DWORD length, const WCHAR *sep, VARIANT *retv,
218 jsexcept_t *ei, IServiceProvider *caller)
220 BSTR *str_tab, ret = NULL;
223 HRESULT hres = E_FAIL;
227 V_VT(retv) = VT_BSTR;
228 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
230 return E_OUTOFMEMORY;
235 str_tab = heap_alloc_zero(length * sizeof(BSTR));
237 return E_OUTOFMEMORY;
239 for(i=0; i < length; i++) {
240 hres = jsdisp_propget_idx(array, i, lcid, &var, ei, caller);
244 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
245 hres = to_string(array->ctx, &var, ei, str_tab+i);
251 if(SUCCEEDED(hres)) {
252 DWORD seplen = 0, len = 0;
255 seplen = strlenW(sep);
258 len = SysStringLen(str_tab[0]);
259 for(i=1; i < length; i++)
260 len += seplen + SysStringLen(str_tab[i]);
262 ret = SysAllocStringLen(NULL, len);
267 tmplen = SysStringLen(str_tab[0]);
268 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
272 for(i=1; i < length; i++) {
274 memcpy(ptr, sep, seplen*sizeof(WCHAR));
279 tmplen = SysStringLen(str_tab[i]);
280 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
286 hres = E_OUTOFMEMORY;
290 for(i=0; i < length; i++)
291 SysFreeString(str_tab[i]);
296 TRACE("= %s\n", debugstr_w(ret));
300 ret = SysAllocStringLen(NULL, 0);
302 return E_OUTOFMEMORY;
305 V_VT(retv) = VT_BSTR;
314 /* ECMA-262 3rd Edition 15.4.4.5 */
315 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
316 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
323 if(is_class(dispex, JSCLASS_ARRAY)) {
324 length = ((ArrayInstance*)dispex)->length;
326 FIXME("dispid is not Array\n");
333 hres = to_string(dispex->ctx, get_arg(dp,0), ei, &sep);
337 hres = array_join(dispex, lcid, length, sep, retv, ei, caller);
341 hres = array_join(dispex, lcid, length, default_separatorW, retv, ei, caller);
347 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
348 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
356 static const WCHAR formatW[] = {'%','d',0};
360 if(is_class(dispex, JSCLASS_ARRAY)) {
361 ArrayInstance *array = (ArrayInstance*)dispex;
362 length = array->length;
364 FIXME("not Array this\n");
370 V_VT(retv) = VT_EMPTY;
374 sprintfW(buf, formatW, --length);
375 hres = jsdisp_get_id(dispex, buf, 0, &id);
376 if(SUCCEEDED(hres)) {
377 hres = jsdisp_propget(dispex, id, lcid, &val, ei, caller);
381 hres = IDispatchEx_DeleteMemberByDispID(_IDispatchEx_(dispex), id);
382 }else if(hres == DISP_E_UNKNOWNNAME) {
383 V_VT(&val) = VT_EMPTY;
389 if(SUCCEEDED(hres)) {
390 if(is_class(dispex, JSCLASS_ARRAY)) {
391 ArrayInstance *array = (ArrayInstance*)dispex;
392 array->length = length;
408 /* ECMA-262 3rd Edition 15.4.4.7 */
409 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
410 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
418 if(dispex->builtin_info->class == JSCLASS_ARRAY) {
419 length = ((ArrayInstance*)dispex)->length;
421 hres = get_jsdisp_length(dispex, lcid, ei, &length);
427 for(i=0; i < n; i++) {
428 hres = jsdisp_propput_idx(dispex, length+i, lcid, get_arg(dp, i), ei, sp);
433 if(!is_class(dispex, JSCLASS_ARRAY)) {
434 hres = set_jsdisp_length(dispex, lcid, ei, length+n);
441 V_I4(retv) = length+n;
446 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
447 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
453 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
454 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
460 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
461 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
466 DWORD length, start, end, idx;
471 if(is_class(dispex, JSCLASS_ARRAY)) {
472 length = ((ArrayInstance*)dispex)->length;
474 hres = get_jsdisp_length(dispex, lcid, ei, &length);
480 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
484 if(V_VT(&v) == VT_I4)
487 range = floor(V_R8(&v));
489 if(-range>length || isnan(range)) start = 0;
490 else if(range < 0) start = range+length;
491 else if(range <= length) start = range;
497 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &v);
501 if(V_VT(&v) == VT_I4)
504 range = floor(V_R8(&v));
506 if(-range>length) end = 0;
507 else if(range < 0) end = range+length;
508 else if(range <= length) end = range;
513 hres = create_array(dispex->ctx, (end>start)?end-start:0, &arr);
517 for(idx=start; idx<end; idx++) {
518 hres = jsdisp_propget_idx(dispex, idx, lcid, &v, ei, sp);
519 if(hres == DISP_E_UNKNOWNNAME)
523 hres = jsdisp_propput_idx(arr, idx-start, lcid, &v, ei, sp);
532 V_VT(retv) = VT_DISPATCH;
533 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
541 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
542 IServiceProvider *caller, INT *cmp)
548 DISPPARAMS dp = {args, NULL, 2, 0};
555 hres = jsdisp_call_value(cmp_func, ctx->lcid, DISPATCH_METHOD, &dp, &res, ei, caller);
559 hres = to_number(ctx, &res, ei, &tmp);
564 if(V_VT(&tmp) == VT_I4)
567 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
568 }else if(is_num_vt(V_VT(v1))) {
569 if(is_num_vt(V_VT(v2))) {
570 DOUBLE d = num_val(v1)-num_val(v2);
580 }else if(is_num_vt(V_VT(v2))) {
582 }else if(V_VT(v1) == VT_BSTR) {
583 if(V_VT(v2) == VT_BSTR)
584 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
587 }else if(V_VT(v2) == VT_BSTR) {
596 /* ECMA-262 3rd Edition 15.4.4.11 */
597 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
598 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
600 DispatchEx *cmp_func = NULL;
601 VARIANT *vtab, **sorttab = NULL;
608 if(is_class(dispex, JSCLASS_ARRAY)) {
609 length = ((ArrayInstance*)dispex)->length;
611 FIXME("unsupported this not array\n");
615 if(arg_cnt(dp) > 1) {
616 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
620 if(arg_cnt(dp) == 1) {
621 VARIANT *arg = get_arg(dp, 0);
623 if(V_VT(arg) != VT_DISPATCH) {
624 WARN("arg is not dispatch\n");
629 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
630 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
631 WARN("cmp_func is not a function\n");
633 jsdisp_release(cmp_func);
640 jsdisp_release(cmp_func);
642 V_VT(retv) = VT_DISPATCH;
643 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
644 IDispatchEx_AddRef(_IDispatchEx_(dispex));
649 vtab = heap_alloc_zero(length * sizeof(VARIANT));
651 for(i=0; i<length; i++) {
652 hres = jsdisp_propget_idx(dispex, i, lcid, vtab+i, ei, caller);
653 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
654 WARN("Could not get elem %d: %08x\n", i, hres);
659 hres = E_OUTOFMEMORY;
662 if(SUCCEEDED(hres)) {
663 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
665 hres = E_OUTOFMEMORY;
669 if(SUCCEEDED(hres)) {
670 VARIANT *tmpv, **tmpbuf;
673 tmpbuf = sorttab + length;
674 for(i=0; i < length; i++)
677 for(i=0; i < length/2; i++) {
678 hres = sort_cmp(dispex->ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
684 sorttab[2*i] = sorttab[2*i+1];
685 sorttab[2*i+1] = tmpv;
689 if(SUCCEEDED(hres)) {
692 for(k=2; k < length; k *= 2) {
693 for(i=0; i+k < length; i += 2*k) {
698 bend = length - (i+k);
700 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
702 while(a < k && b < bend) {
703 hres = sort_cmp(dispex->ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
708 sorttab[i+a+b] = tmpbuf[a];
711 sorttab[i+a+b] = sorttab[i+k+b];
720 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
728 for(i=0; SUCCEEDED(hres) && i < length; i++)
729 hres = jsdisp_propput_idx(dispex, i, lcid, sorttab[i], ei, caller);
733 for(i=0; i < length; i++)
734 VariantClear(vtab+i);
739 jsdisp_release(cmp_func);
745 V_VT(retv) = VT_DISPATCH;
746 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
747 IDispatch_AddRef(_IDispatchEx_(dispex));
753 /* ECMA-262 3rd Edition 15.4.4.12 */
754 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
755 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
757 DWORD length, start=0, delete_cnt=0, argc, i, add_args = 0;
758 DispatchEx *ret_array = NULL;
764 if(is_class(dispex, JSCLASS_ARRAY)) {
765 length = ((ArrayInstance*)dispex)->length;
767 hres = get_jsdisp_length(dispex, lcid, ei, &length);
774 hres = to_integer(dispex->ctx, get_arg(dp,0), ei, &v);
778 if(V_VT(&v) == VT_I4) {
780 start = min(V_I4(&v), length);
782 start = -V_I4(&v) > length ? 0 : length + V_I4(&v);
784 start = V_R8(&v) < 0.0 ? 0 : length;
789 hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &v);
793 if(V_VT(&v) == VT_I4) {
795 delete_cnt = min(V_I4(&v), length-start);
796 }else if(V_R8(&v) > 0.0) {
797 delete_cnt = length-start;
804 hres = create_array(dispex->ctx, 0, &ret_array);
808 for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
809 hres = jsdisp_propget_idx(dispex, start+i, lcid, &v, ei, caller);
810 if(hres == DISP_E_UNKNOWNNAME)
812 else if(SUCCEEDED(hres))
813 hres = jsdisp_propput_idx(ret_array, i, lcid, &v, ei, caller);
816 if(SUCCEEDED(hres)) {
818 V_I4(&v) = delete_cnt;
820 hres = jsdisp_propput_name(ret_array, lengthW, lcid, &v, ei, caller);
824 if(add_args < delete_cnt) {
825 for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
826 hres = jsdisp_propget_idx(dispex, i+delete_cnt, lcid, &v, ei, caller);
827 if(hres == DISP_E_UNKNOWNNAME)
828 hres = jsdisp_delete_idx(dispex, i+add_args);
829 else if(SUCCEEDED(hres))
830 hres = jsdisp_propput_idx(dispex, i+add_args, lcid, &v, ei, caller);
833 for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
834 hres = jsdisp_delete_idx(dispex, i-1);
835 }else if(add_args > delete_cnt) {
836 for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
837 hres = jsdisp_propget_idx(dispex, i+delete_cnt-1, lcid, &v, ei, caller);
838 if(hres == DISP_E_UNKNOWNNAME)
839 hres = jsdisp_delete_idx(dispex, i+add_args-1);
840 else if(SUCCEEDED(hres))
841 hres = jsdisp_propput_idx(dispex, i+add_args-1, lcid, &v, ei, caller);
845 for(i=0; SUCCEEDED(hres) && i < add_args; i++)
846 hres = jsdisp_propput_idx(dispex, start+i, lcid, get_arg(dp,i+2), ei, caller);
848 if(SUCCEEDED(hres)) {
850 V_I4(&v) = length-delete_cnt+add_args;
851 hres = jsdisp_propput_name(dispex, lengthW, lcid, &v, ei, caller);
856 jsdisp_release(ret_array);
861 V_VT(retv) = VT_DISPATCH;
862 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret_array);
867 /* ECMA-262 3rd Edition 15.4.4.2 */
868 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
869 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
873 if(!is_class(dispex, JSCLASS_ARRAY)) {
874 WARN("not Array object\n");
878 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
881 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
882 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
888 /* ECMA-262 3rd Edition 15.4.4.13 */
889 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
890 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
892 WCHAR buf[14], *buf_end, *str;
893 DWORD argc, i, length;
900 if(is_class(dispex, JSCLASS_ARRAY)) {
901 length = ((ArrayInstance*)dispex)->length;
903 hres = get_jsdisp_length(dispex, lcid, ei, &length);
911 V_VT(retv) = VT_EMPTY;
915 buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
920 str = idx_to_str(i, buf_end);
922 hres = jsdisp_get_id(dispex, str, 0, &id);
923 if(SUCCEEDED(hres)) {
924 hres = jsdisp_propget(dispex, id, lcid, &var, ei, caller);
928 hres = jsdisp_propput_idx(dispex, i+argc, lcid, &var, ei, caller);
930 }else if(hres == DISP_E_UNKNOWNNAME) {
931 hres = IDispatchEx_DeleteMemberByDispID(_IDispatchEx_(dispex), id);
938 for(i=0; i<argc; i++) {
939 hres = jsdisp_propput_idx(dispex, i, lcid, get_arg(dp,i), ei, caller);
944 if(!is_class(dispex, JSCLASS_ARRAY)) {
945 hres = set_jsdisp_length(dispex, lcid, ei, length+argc);
951 V_VT(retv) = VT_EMPTY;
955 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
956 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
962 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
963 case INVOKE_PROPERTYGET:
964 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
966 FIXME("unimplemented flags %x\n", flags);
973 static void Array_destructor(DispatchEx *dispex)
978 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
980 ArrayInstance *array = (ArrayInstance*)dispex;
981 const WCHAR *ptr = name;
987 while(*ptr && isdigitW(*ptr)) {
988 id = id*10 + (*ptr-'0');
995 if(id >= array->length)
996 array->length = id+1;
999 static const builtin_prop_t Array_props[] = {
1000 {concatW, Array_concat, PROPF_METHOD|1},
1001 {joinW, Array_join, PROPF_METHOD|1},
1002 {lengthW, Array_length, 0},
1003 {popW, Array_pop, PROPF_METHOD},
1004 {pushW, Array_push, PROPF_METHOD|1},
1005 {reverseW, Array_reverse, PROPF_METHOD},
1006 {shiftW, Array_shift, PROPF_METHOD},
1007 {sliceW, Array_slice, PROPF_METHOD|2},
1008 {sortW, Array_sort, PROPF_METHOD|1},
1009 {spliceW, Array_splice, PROPF_METHOD|2},
1010 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
1011 {toStringW, Array_toString, PROPF_METHOD},
1012 {unshiftW, Array_unshift, PROPF_METHOD|1},
1015 static const builtin_info_t Array_info = {
1017 {NULL, Array_value, 0},
1018 sizeof(Array_props)/sizeof(*Array_props),
1024 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
1025 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
1035 case DISPATCH_METHOD:
1036 case DISPATCH_CONSTRUCT: {
1037 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
1038 if(V_I4(arg_var) < 0)
1039 return throw_range_error(dispex->ctx, ei, IDS_INVALID_LENGTH, NULL);
1041 hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
1045 V_VT(retv) = VT_DISPATCH;
1046 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1050 hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
1054 for(i=0; i < arg_cnt(dp); i++) {
1055 hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
1060 jsdisp_release(obj);
1064 V_VT(retv) = VT_DISPATCH;
1065 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
1069 FIXME("unimplemented flags: %x\n", flags);
1076 static HRESULT alloc_array(script_ctx_t *ctx, DispatchEx *object_prototype, ArrayInstance **ret)
1078 ArrayInstance *array;
1081 array = heap_alloc_zero(sizeof(ArrayInstance));
1083 return E_OUTOFMEMORY;
1085 if(object_prototype)
1086 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1088 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
1099 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
1101 ArrayInstance *array;
1104 static const WCHAR ArrayW[] = {'A','r','r','a','y',0};
1106 hres = alloc_array(ctx, object_prototype, &array);
1110 hres = create_builtin_function(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR, &array->dispex, ret);
1112 jsdisp_release(&array->dispex);
1116 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
1118 ArrayInstance *array;
1121 hres = alloc_array(ctx, NULL, &array);
1125 array->length = length;
1127 *ret = &array->dispex;