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};
46 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
47 static const WCHAR propertyIsEnumerableW[] =
48 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
49 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
51 static const WCHAR default_separatorW[] = {',',0};
53 static HRESULT Array_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
54 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
56 ArrayInstance *This = (ArrayInstance*)dispex;
58 TRACE("%p %d\n", This, This->length);
61 case DISPATCH_PROPERTYGET:
63 V_I4(retv) = This->length;
65 case DISPATCH_PROPERTYPUT: {
71 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &num);
72 if(V_VT(&num) == VT_I4)
75 len = floor(V_R8(&num));
78 return throw_range_error(dispex->ctx, ei, IDS_INVALID_LENGTH, NULL);
80 for(i=len; i<This->length; i++) {
81 hres = jsdisp_delete_idx(dispex, i);
90 FIXME("unimplemented flags %x\n", flags);
97 static HRESULT concat_array(DispatchEx *array, ArrayInstance *obj, DWORD *len, LCID lcid,
98 jsexcept_t *ei, IServiceProvider *caller)
104 for(i=0; i < obj->length; i++) {
105 hres = jsdisp_propget_idx(&obj->dispex, i, lcid, &var, ei, caller);
106 if(hres == DISP_E_UNKNOWNNAME)
111 hres = jsdisp_propput_idx(array, *len+i, lcid, &var, ei, caller);
121 static HRESULT concat_obj(DispatchEx *array, IDispatch *obj, DWORD *len, LCID lcid, jsexcept_t *ei, IServiceProvider *caller)
127 jsobj = iface_to_jsdisp((IUnknown*)obj);
129 if(is_class(jsobj, JSCLASS_ARRAY)) {
130 hres = concat_array(array, (ArrayInstance*)jsobj, len, lcid, ei, caller);
131 jsdisp_release(jsobj);
134 jsdisp_release(jsobj);
137 V_VT(&var) = VT_DISPATCH;
138 V_DISPATCH(&var) = obj;
139 return jsdisp_propput_idx(array, (*len)++, lcid, &var, ei, caller);
142 static HRESULT Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
143 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
151 hres = create_array(dispex->ctx, 0, &ret);
155 hres = concat_obj(ret, (IDispatch*)_IDispatchEx_(dispex), &len, lcid, ei, caller);
156 if(SUCCEEDED(hres)) {
160 for(i=0; i < arg_cnt(dp); i++) {
161 arg = get_arg(dp, i);
162 if(V_VT(arg) == VT_DISPATCH)
163 hres = concat_obj(ret, V_DISPATCH(arg), &len, lcid, ei, caller);
165 hres = jsdisp_propput_idx(ret, len++, lcid, arg, ei, caller);
175 V_VT(retv) = VT_DISPATCH;
176 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
183 static HRESULT array_join(DispatchEx *array, LCID lcid, DWORD length, const WCHAR *sep, VARIANT *retv,
184 jsexcept_t *ei, IServiceProvider *caller)
186 BSTR *str_tab, ret = NULL;
189 HRESULT hres = E_FAIL;
193 V_VT(retv) = VT_BSTR;
194 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
196 return E_OUTOFMEMORY;
201 str_tab = heap_alloc_zero(length * sizeof(BSTR));
203 return E_OUTOFMEMORY;
205 for(i=0; i < length; i++) {
206 hres = jsdisp_propget_idx(array, i, lcid, &var, ei, caller);
210 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
211 hres = to_string(array->ctx, &var, ei, str_tab+i);
217 if(SUCCEEDED(hres)) {
218 DWORD seplen = 0, len = 0;
221 seplen = strlenW(sep);
224 len = SysStringLen(str_tab[0]);
225 for(i=1; i < length; i++)
226 len += seplen + SysStringLen(str_tab[i]);
228 ret = SysAllocStringLen(NULL, len);
233 tmplen = SysStringLen(str_tab[0]);
234 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
238 for(i=1; i < length; i++) {
240 memcpy(ptr, sep, seplen*sizeof(WCHAR));
245 tmplen = SysStringLen(str_tab[i]);
246 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
252 hres = E_OUTOFMEMORY;
256 for(i=0; i < length; i++)
257 SysFreeString(str_tab[i]);
262 TRACE("= %s\n", debugstr_w(ret));
266 ret = SysAllocStringLen(NULL, 0);
268 return E_OUTOFMEMORY;
271 V_VT(retv) = VT_BSTR;
280 /* ECMA-262 3rd Edition 15.4.4.5 */
281 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
282 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
289 if(is_class(dispex, JSCLASS_ARRAY)) {
290 length = ((ArrayInstance*)dispex)->length;
292 FIXME("dispid is not Array\n");
299 hres = to_string(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &sep);
303 hres = array_join(dispex, lcid, length, sep, retv, ei, caller);
307 hres = array_join(dispex, lcid, length, default_separatorW, retv, ei, caller);
313 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
314 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
322 static const WCHAR formatW[] = {'%','d',0};
326 if(is_class(dispex, JSCLASS_ARRAY)) {
327 ArrayInstance *array = (ArrayInstance*)dispex;
328 length = array->length;
330 FIXME("not Array this\n");
336 V_VT(retv) = VT_EMPTY;
340 sprintfW(buf, formatW, --length);
341 hres = jsdisp_get_id(dispex, buf, 0, &id);
342 if(SUCCEEDED(hres)) {
343 hres = jsdisp_propget(dispex, id, lcid, &val, ei, caller);
347 hres = IDispatchEx_DeleteMemberByDispID(_IDispatchEx_(dispex), id);
348 }else if(hres == DISP_E_UNKNOWNNAME) {
349 V_VT(&val) = VT_EMPTY;
355 if(SUCCEEDED(hres)) {
356 if(is_class(dispex, JSCLASS_ARRAY)) {
357 ArrayInstance *array = (ArrayInstance*)dispex;
358 array->length = length;
374 /* ECMA-262 3rd Edition 15.4.4.7 */
375 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
376 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
384 if(dispex->builtin_info->class == JSCLASS_ARRAY) {
385 length = ((ArrayInstance*)dispex)->length;
387 FIXME("not Array this\n");
391 n = dp->cArgs - dp->cNamedArgs;
392 for(i=0; i < n; i++) {
393 hres = jsdisp_propput_idx(dispex, length+i, lcid, get_arg(dp, i), ei, sp);
400 V_I4(retv) = length+n;
405 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
406 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
412 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
413 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
419 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
420 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
425 DWORD length, start, end, idx;
430 if(is_class(dispex, JSCLASS_ARRAY)) {
431 length = ((ArrayInstance*)dispex)->length;
433 FIXME("not Array this\n");
438 hres = to_number(dispex->ctx, get_arg(dp, 0), ei, &v);
442 if(V_VT(&v) == VT_I4)
445 range = floor(V_R8(&v));
447 if(-range>length || isnan(range)) start = 0;
448 else if(range < 0) start = range+length;
449 else if(range <= length) start = range;
455 hres = to_number(dispex->ctx, get_arg(dp, 1), ei, &v);
459 if(V_VT(&v) == VT_I4)
462 range = floor(V_R8(&v));
464 if(-range>length) end = 0;
465 else if(range < 0) end = range+length;
466 else if(range <= length) end = range;
471 hres = create_array(dispex->ctx, (end>start)?end-start:0, &arr);
475 for(idx=start; idx<end; idx++) {
476 hres = jsdisp_propget_idx(dispex, idx, lcid, &v, ei, sp);
477 if(hres == DISP_E_UNKNOWNNAME)
481 hres = jsdisp_propput_idx(arr, idx-start, lcid, &v, ei, sp);
490 V_VT(retv) = VT_DISPATCH;
491 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(arr);
499 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
500 IServiceProvider *caller, INT *cmp)
506 DISPPARAMS dp = {args, NULL, 2, 0};
513 hres = jsdisp_call_value(cmp_func, ctx->lcid, DISPATCH_METHOD, &dp, &res, ei, caller);
517 hres = to_number(ctx, &res, ei, &tmp);
522 if(V_VT(&tmp) == VT_I4)
525 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
526 }else if(is_num_vt(V_VT(v1))) {
527 if(is_num_vt(V_VT(v2))) {
528 DOUBLE d = num_val(v1)-num_val(v2);
538 }else if(is_num_vt(V_VT(v2))) {
540 }else if(V_VT(v1) == VT_BSTR) {
541 if(V_VT(v2) == VT_BSTR)
542 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
545 }else if(V_VT(v2) == VT_BSTR) {
554 /* ECMA-262 3rd Edition 15.4.4.11 */
555 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
556 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
558 DispatchEx *cmp_func = NULL;
559 VARIANT *vtab, **sorttab = NULL;
566 if(is_class(dispex, JSCLASS_ARRAY)) {
567 length = ((ArrayInstance*)dispex)->length;
569 FIXME("unsupported this not array\n");
573 if(arg_cnt(dp) > 1) {
574 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
578 if(arg_cnt(dp) == 1) {
579 VARIANT *arg = get_arg(dp, 0);
581 if(V_VT(arg) != VT_DISPATCH) {
582 WARN("arg is not dispatch\n");
587 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
588 if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
589 WARN("cmp_func is not a function\n");
591 jsdisp_release(cmp_func);
598 jsdisp_release(cmp_func);
600 V_VT(retv) = VT_DISPATCH;
601 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
602 IDispatchEx_AddRef(_IDispatchEx_(dispex));
607 vtab = heap_alloc_zero(length * sizeof(VARIANT));
609 for(i=0; i<length; i++) {
610 hres = jsdisp_propget_idx(dispex, i, lcid, vtab+i, ei, caller);
611 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
612 WARN("Could not get elem %d: %08x\n", i, hres);
617 hres = E_OUTOFMEMORY;
620 if(SUCCEEDED(hres)) {
621 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
623 hres = E_OUTOFMEMORY;
627 if(SUCCEEDED(hres)) {
628 VARIANT *tmpv, **tmpbuf;
631 tmpbuf = sorttab + length;
632 for(i=0; i < length; i++)
635 for(i=0; i < length/2; i++) {
636 hres = sort_cmp(dispex->ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
642 sorttab[2*i] = sorttab[2*i+1];
643 sorttab[2*i+1] = tmpv;
647 if(SUCCEEDED(hres)) {
650 for(k=2; k < length; k *= 2) {
651 for(i=0; i+k < length; i += 2*k) {
656 bend = length - (i+k);
658 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
660 while(a < k && b < bend) {
661 hres = sort_cmp(dispex->ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
666 sorttab[i+a+b] = tmpbuf[a];
669 sorttab[i+a+b] = sorttab[i+k+b];
678 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
686 for(i=0; SUCCEEDED(hres) && i < length; i++)
687 hres = jsdisp_propput_idx(dispex, i, lcid, sorttab[i], ei, caller);
691 for(i=0; i < length; i++)
692 VariantClear(vtab+i);
697 jsdisp_release(cmp_func);
703 V_VT(retv) = VT_DISPATCH;
704 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
705 IDispatch_AddRef(_IDispatchEx_(dispex));
711 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
712 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
718 /* ECMA-262 3rd Edition 15.4.4.2 */
719 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
720 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
724 if(!is_class(dispex, JSCLASS_ARRAY)) {
725 WARN("not Array object\n");
729 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
732 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
733 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
739 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
740 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
746 static HRESULT Array_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
747 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
753 static HRESULT Array_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
754 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
760 static HRESULT Array_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
761 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
767 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
768 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
774 return throw_type_error(dispex->ctx, ei, IDS_NOT_FUNC, NULL);
775 case INVOKE_PROPERTYGET:
776 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
778 FIXME("unimplemented flags %x\n", flags);
785 static void Array_destructor(DispatchEx *dispex)
790 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
792 ArrayInstance *array = (ArrayInstance*)dispex;
793 const WCHAR *ptr = name;
799 while(*ptr && isdigitW(*ptr)) {
800 id = id*10 + (*ptr-'0');
807 if(id >= array->length)
808 array->length = id+1;
811 static const builtin_prop_t Array_props[] = {
812 {concatW, Array_concat, PROPF_METHOD},
813 {hasOwnPropertyW, Array_hasOwnProperty, PROPF_METHOD},
814 {isPrototypeOfW, Array_isPrototypeOf, PROPF_METHOD},
815 {joinW, Array_join, PROPF_METHOD},
816 {lengthW, Array_length, 0},
817 {popW, Array_pop, PROPF_METHOD},
818 {propertyIsEnumerableW, Array_propertyIsEnumerable, PROPF_METHOD},
819 {pushW, Array_push, PROPF_METHOD},
820 {reverseW, Array_reverse, PROPF_METHOD},
821 {shiftW, Array_shift, PROPF_METHOD},
822 {sliceW, Array_slice, PROPF_METHOD},
823 {sortW, Array_sort, PROPF_METHOD},
824 {spliceW, Array_splice, PROPF_METHOD},
825 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
826 {toStringW, Array_toString, PROPF_METHOD},
827 {unshiftW, Array_unshift, PROPF_METHOD},
830 static const builtin_info_t Array_info = {
832 {NULL, Array_value, 0},
833 sizeof(Array_props)/sizeof(*Array_props),
839 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
840 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
850 case DISPATCH_METHOD:
851 case DISPATCH_CONSTRUCT: {
852 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
853 if(V_I4(arg_var) < 0)
854 return throw_range_error(dispex->ctx, ei, IDS_INVALID_LENGTH, NULL);
856 hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
860 V_VT(retv) = VT_DISPATCH;
861 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
865 hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
869 for(i=0; i < arg_cnt(dp); i++) {
870 hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
879 V_VT(retv) = VT_DISPATCH;
880 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
884 FIXME("unimplemented flags: %x\n", flags);
891 static HRESULT alloc_array(script_ctx_t *ctx, BOOL use_constr, ArrayInstance **ret)
893 ArrayInstance *array;
896 array = heap_alloc_zero(sizeof(ArrayInstance));
898 return E_OUTOFMEMORY;
901 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
903 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->object_constr);
914 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx **ret)
916 ArrayInstance *array;
919 hres = alloc_array(ctx, FALSE, &array);
923 hres = create_builtin_function(ctx, ArrayConstr_value, NULL, PROPF_CONSTR, &array->dispex, ret);
925 IDispatchEx_Release(_IDispatchEx_(&array->dispex));
929 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
931 ArrayInstance *array;
934 hres = alloc_array(ctx, TRUE, &array);
938 array->length = length;
940 *ret = &array->dispex;