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 Array_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
50 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
52 ArrayInstance *This = (ArrayInstance*)dispex;
54 TRACE("%p %d\n", This, This->length);
57 case DISPATCH_PROPERTYGET:
59 V_I4(retv) = This->length;
61 case DISPATCH_PROPERTYPUT: {
67 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
68 if(V_VT(&num) == VT_I4)
71 len = floor(V_R8(&num));
74 return throw_range_error(dispex->ctx, ei, IDS_INVALID_LENGTH, NULL);
76 for(i=len; i<This->length; i++) {
77 hres = jsdisp_delete_idx(dispex, i);
86 FIXME("unimplemented flags %x\n", flags);
93 static HRESULT concat_array(DispatchEx *array, ArrayInstance *obj, DWORD *len, LCID lcid,
94 jsexcept_t *ei, IServiceProvider *caller)
100 for(i=0; i < obj->length; i++) {
101 hres = jsdisp_propget_idx(&obj->dispex, i, lcid, &var, ei, caller);
102 if(hres == DISP_E_UNKNOWNNAME)
107 hres = jsdisp_propput_idx(array, *len+i, lcid, &var, ei, caller);
117 static HRESULT concat_obj(DispatchEx *array, IDispatch *obj, DWORD *len, LCID lcid, jsexcept_t *ei, IServiceProvider *caller)
123 jsobj = iface_to_jsdisp((IUnknown*)obj);
125 if(is_class(jsobj, JSCLASS_ARRAY)) {
126 hres = concat_array(array, (ArrayInstance*)jsobj, len, lcid, ei, caller);
127 jsdisp_release(jsobj);
130 jsdisp_release(jsobj);
133 V_VT(&var) = VT_DISPATCH;
134 V_DISPATCH(&var) = obj;
135 return jsdisp_propput_idx(array, (*len)++, lcid, &var, ei, caller);
138 static HRESULT Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
139 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
147 hres = create_array(dispex->ctx, 0, &ret);
151 hres = concat_obj(ret, (IDispatch*)_IDispatchEx_(dispex), &len, lcid, ei, caller);
152 if(SUCCEEDED(hres)) {
156 for(i=0; i < arg_cnt(dp); i++) {
157 arg = get_arg(dp, i);
158 if(V_VT(arg) == VT_DISPATCH)
159 hres = concat_obj(ret, V_DISPATCH(arg), &len, lcid, ei, caller);
161 hres = jsdisp_propput_idx(ret, len++, lcid, arg, ei, caller);
171 V_VT(retv) = VT_DISPATCH;
172 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
179 static HRESULT array_join(DispatchEx *array, LCID lcid, DWORD length, const WCHAR *sep, VARIANT *retv,
180 jsexcept_t *ei, IServiceProvider *caller)
182 BSTR *str_tab, ret = NULL;
185 HRESULT hres = E_FAIL;
189 V_VT(retv) = VT_BSTR;
190 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
192 return E_OUTOFMEMORY;
197 str_tab = heap_alloc_zero(length * sizeof(BSTR));
199 return E_OUTOFMEMORY;
201 for(i=0; i < length; i++) {
202 hres = jsdisp_propget_idx(array, i, lcid, &var, ei, caller);
206 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
207 hres = to_string(array->ctx, &var, ei, str_tab+i);
213 if(SUCCEEDED(hres)) {
214 DWORD seplen = 0, len = 0;
217 seplen = strlenW(sep);
220 len = SysStringLen(str_tab[0]);
221 for(i=1; i < length; i++)
222 len += seplen + SysStringLen(str_tab[i]);
224 ret = SysAllocStringLen(NULL, len);
229 tmplen = SysStringLen(str_tab[0]);
230 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
234 for(i=1; i < length; i++) {
236 memcpy(ptr, sep, seplen*sizeof(WCHAR));
241 tmplen = SysStringLen(str_tab[i]);
242 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
248 hres = E_OUTOFMEMORY;
252 for(i=0; i < length; i++)
253 SysFreeString(str_tab[i]);
258 TRACE("= %s\n", debugstr_w(ret));
262 ret = SysAllocStringLen(NULL, 0);
264 return E_OUTOFMEMORY;
267 V_VT(retv) = VT_BSTR;
276 /* ECMA-262 3rd Edition 15.4.4.5 */
277 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
278 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
285 if(is_class(dispex, JSCLASS_ARRAY)) {
286 length = ((ArrayInstance*)dispex)->length;
288 FIXME("dispid is not Array\n");
295 hres = to_string(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &sep);
299 hres = array_join(dispex, lcid, length, sep, retv, ei, caller);
303 hres = array_join(dispex, lcid, length, default_separatorW, retv, ei, caller);
309 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
310 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
318 static const WCHAR formatW[] = {'%','d',0};
322 if(is_class(dispex, JSCLASS_ARRAY)) {
323 ArrayInstance *array = (ArrayInstance*)dispex;
324 length = array->length;
326 FIXME("not Array this\n");
332 V_VT(retv) = VT_EMPTY;
336 sprintfW(buf, formatW, --length);
337 hres = jsdisp_get_id(dispex, buf, 0, &id);
338 if(SUCCEEDED(hres)) {
339 hres = jsdisp_propget(dispex, id, lcid, &val, ei, caller);
343 hres = IDispatchEx_DeleteMemberByDispID(_IDispatchEx_(dispex), id);
344 }else if(hres == DISP_E_UNKNOWNNAME) {
345 V_VT(&val) = VT_EMPTY;
351 if(SUCCEEDED(hres)) {
352 if(is_class(dispex, JSCLASS_ARRAY)) {
353 ArrayInstance *array = (ArrayInstance*)dispex;
354 array->length = length;
370 /* ECMA-262 3rd Edition 15.4.4.7 */
371 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
372 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
380 if(dispex->builtin_info->class == JSCLASS_ARRAY) {
381 length = ((ArrayInstance*)dispex)->length;
383 FIXME("not Array this\n");
387 n = dp->cArgs - dp->cNamedArgs;
388 for(i=0; i < n; i++) {
389 hres = jsdisp_propput_idx(dispex, length+i, lcid, get_arg(dp, i), ei, sp);
396 V_I4(retv) = length+n;
401 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
402 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
408 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
409 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
415 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
416 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
421 DWORD length, start, end, idx;
426 if(is_class(dispex, JSCLASS_ARRAY)) {
427 length = ((ArrayInstance*)dispex)->length;
429 FIXME("not Array this\n");
434 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
438 if(V_VT(&v) == VT_I4)
441 range = floor(V_R8(&v));
443 if(-range>length || isnan(range)) start = 0;
444 else if(range < 0) start = range+length;
445 else if(range <= length) start = range;
451 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &v);
455 if(V_VT(&v) == VT_I4)
458 range = floor(V_R8(&v));
460 if(-range>length) end = 0;
461 else if(range < 0) end = range+length;
462 else if(range <= length) end = range;
467 hres = create_array(dispex->ctx, (end>start)?end-start:0, &arr);
471 for(idx=start; idx<end; idx++) {
472 hres = jsdisp_propget_idx(dispex, idx, lcid, &v, ei, sp);
473 if(hres == DISP_E_UNKNOWNNAME)
477 hres = jsdisp_propput_idx(arr, idx-start, lcid, &v, ei, sp);
486 V_VT(retv) = VT_DISPATCH;
487 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
495 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
496 IServiceProvider *caller, INT *cmp)
502 DISPPARAMS dp = {args, NULL, 2, 0};
509 hres = jsdisp_call_value(cmp_func, ctx->lcid, DISPATCH_METHOD, &dp, &res, ei, caller);
513 hres = to_number(ctx, &res, ei, &tmp);
518 if(V_VT(&tmp) == VT_I4)
521 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
522 }else if(is_num_vt(V_VT(v1))) {
523 if(is_num_vt(V_VT(v2))) {
524 DOUBLE d = num_val(v1)-num_val(v2);
534 }else if(is_num_vt(V_VT(v2))) {
536 }else if(V_VT(v1) == VT_BSTR) {
537 if(V_VT(v2) == VT_BSTR)
538 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
541 }else if(V_VT(v2) == VT_BSTR) {
550 /* ECMA-262 3rd Edition 15.4.4.11 */
551 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
552 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
554 DispatchEx *cmp_func = NULL;
555 VARIANT *vtab, **sorttab = NULL;
562 if(is_class(dispex, JSCLASS_ARRAY)) {
563 length = ((ArrayInstance*)dispex)->length;
565 FIXME("unsupported this not array\n");
569 if(arg_cnt(dp) > 1) {
570 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
574 if(arg_cnt(dp) == 1) {
575 VARIANT *arg = get_arg(dp, 0);
577 if(V_VT(arg) != VT_DISPATCH) {
578 WARN("arg is not dispatch\n");
583 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
584 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
585 WARN("cmp_func is not a function\n");
587 jsdisp_release(cmp_func);
594 jsdisp_release(cmp_func);
596 V_VT(retv) = VT_DISPATCH;
597 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
598 IDispatchEx_AddRef(_IDispatchEx_(dispex));
603 vtab = heap_alloc_zero(length * sizeof(VARIANT));
605 for(i=0; i<length; i++) {
606 hres = jsdisp_propget_idx(dispex, i, lcid, vtab+i, ei, caller);
607 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
608 WARN("Could not get elem %d: %08x\n", i, hres);
613 hres = E_OUTOFMEMORY;
616 if(SUCCEEDED(hres)) {
617 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
619 hres = E_OUTOFMEMORY;
623 if(SUCCEEDED(hres)) {
624 VARIANT *tmpv, **tmpbuf;
627 tmpbuf = sorttab + length;
628 for(i=0; i < length; i++)
631 for(i=0; i < length/2; i++) {
632 hres = sort_cmp(dispex->ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
638 sorttab[2*i] = sorttab[2*i+1];
639 sorttab[2*i+1] = tmpv;
643 if(SUCCEEDED(hres)) {
646 for(k=2; k < length; k *= 2) {
647 for(i=0; i+k < length; i += 2*k) {
652 bend = length - (i+k);
654 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
656 while(a < k && b < bend) {
657 hres = sort_cmp(dispex->ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
662 sorttab[i+a+b] = tmpbuf[a];
665 sorttab[i+a+b] = sorttab[i+k+b];
674 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
682 for(i=0; SUCCEEDED(hres) && i < length; i++)
683 hres = jsdisp_propput_idx(dispex, i, lcid, sorttab[i], ei, caller);
687 for(i=0; i < length; i++)
688 VariantClear(vtab+i);
693 jsdisp_release(cmp_func);
699 V_VT(retv) = VT_DISPATCH;
700 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
701 IDispatch_AddRef(_IDispatchEx_(dispex));
707 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
708 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
714 /* ECMA-262 3rd Edition 15.4.4.2 */
715 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
716 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
720 if(!is_class(dispex, JSCLASS_ARRAY)) {
721 WARN("not Array object\n");
725 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
728 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
729 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
735 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
736 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
742 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
743 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
749 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
750 case INVOKE_PROPERTYGET:
751 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
753 FIXME("unimplemented flags %x\n", flags);
760 static void Array_destructor(DispatchEx *dispex)
765 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
767 ArrayInstance *array = (ArrayInstance*)dispex;
768 const WCHAR *ptr = name;
774 while(*ptr && isdigitW(*ptr)) {
775 id = id*10 + (*ptr-'0');
782 if(id >= array->length)
783 array->length = id+1;
786 static const builtin_prop_t Array_props[] = {
787 {concatW, Array_concat, PROPF_METHOD|1},
788 {joinW, Array_join, PROPF_METHOD|1},
789 {lengthW, Array_length, 0},
790 {popW, Array_pop, PROPF_METHOD},
791 {pushW, Array_push, PROPF_METHOD|1},
792 {reverseW, Array_reverse, PROPF_METHOD},
793 {shiftW, Array_shift, PROPF_METHOD},
794 {sliceW, Array_slice, PROPF_METHOD|2},
795 {sortW, Array_sort, PROPF_METHOD|1},
796 {spliceW, Array_splice, PROPF_METHOD|2},
797 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
798 {toStringW, Array_toString, PROPF_METHOD},
799 {unshiftW, Array_unshift, PROPF_METHOD|1},
802 static const builtin_info_t Array_info = {
804 {NULL, Array_value, 0},
805 sizeof(Array_props)/sizeof(*Array_props),
811 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
812 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
822 case DISPATCH_METHOD:
823 case DISPATCH_CONSTRUCT: {
824 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
825 if(V_I4(arg_var) < 0)
826 return throw_range_error(dispex->ctx, ei, IDS_INVALID_LENGTH, NULL);
828 hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
832 V_VT(retv) = VT_DISPATCH;
833 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
837 hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
841 for(i=0; i < arg_cnt(dp); i++) {
842 hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
851 V_VT(retv) = VT_DISPATCH;
852 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
856 FIXME("unimplemented flags: %x\n", flags);
863 static HRESULT alloc_array(script_ctx_t *ctx, DispatchEx *object_prototype, ArrayInstance **ret)
865 ArrayInstance *array;
868 array = heap_alloc_zero(sizeof(ArrayInstance));
870 return E_OUTOFMEMORY;
873 hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
875 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->object_constr);
886 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
888 ArrayInstance *array;
891 hres = alloc_array(ctx, object_prototype, &array);
895 hres = create_builtin_function(ctx, ArrayConstr_value, NULL, PROPF_CONSTR, &array->dispex, ret);
897 IDispatchEx_Release(_IDispatchEx_(&array->dispex));
901 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
903 ArrayInstance *array;
906 hres = alloc_array(ctx, NULL, &array);
910 array->length = length;
912 *ret = &array->dispex;