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
21 #include "wine/debug.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
31 static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
32 static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
33 static const WCHAR joinW[] = {'j','o','i','n',0};
34 static const WCHAR popW[] = {'p','o','p',0};
35 static const WCHAR pushW[] = {'p','u','s','h',0};
36 static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
37 static const WCHAR shiftW[] = {'s','h','i','f','t',0};
38 static const WCHAR sliceW[] = {'s','l','i','c','e',0};
39 static const WCHAR sortW[] = {'s','o','r','t',0};
40 static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
41 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
42 static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
43 static const WCHAR valueOfW[] = {'v','a','l','u','e','O','f',0};
44 static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};
45 static const WCHAR hasOwnPropertyW[] = {'h','a','s','O','w','n','P','r','o','p','e','r','t','y',0};
46 static const WCHAR propertyIsEnumerableW[] =
47 {'p','r','o','p','e','r','t','y','I','s','E','n','u','m','e','r','a','b','l','e',0};
48 static const WCHAR isPrototypeOfW[] = {'i','s','P','r','o','t','o','t','y','p','e','O','f',0};
50 const WCHAR default_separatorW[] = {',',0};
52 static HRESULT Array_length(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
53 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
55 ArrayInstance *This = (ArrayInstance*)dispex;
57 TRACE("%p %d\n", This, This->length);
60 case DISPATCH_PROPERTYGET:
62 V_I4(retv) = This->length;
65 FIXME("unimplemented flags %x\n", flags);
72 static HRESULT concat_array(DispatchEx *array, ArrayInstance *obj, DWORD *len, LCID lcid,
73 jsexcept_t *ei, IServiceProvider *caller)
79 for(i=0; i < obj->length; i++) {
80 hres = jsdisp_propget_idx(&obj->dispex, i, lcid, &var, ei, caller);
81 if(hres == DISP_E_UNKNOWNNAME)
86 hres = jsdisp_propput_idx(array, *len+i, lcid, &var, ei, caller);
96 static HRESULT concat_obj(DispatchEx *array, IDispatch *obj, DWORD *len, LCID lcid, jsexcept_t *ei, IServiceProvider *caller)
102 jsobj = iface_to_jsdisp((IUnknown*)obj);
104 if(is_class(jsobj, JSCLASS_ARRAY)) {
105 hres = concat_array(array, (ArrayInstance*)jsobj, len, lcid, ei, caller);
106 jsdisp_release(jsobj);
109 jsdisp_release(jsobj);
112 V_VT(&var) = VT_DISPATCH;
113 V_DISPATCH(&var) = obj;
114 return jsdisp_propput_idx(array, (*len)++, lcid, &var, ei, caller);
117 static HRESULT Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
118 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
126 hres = create_array(dispex->ctx, 0, &ret);
130 hres = concat_obj(ret, (IDispatch*)_IDispatchEx_(dispex), &len, lcid, ei, caller);
131 if(SUCCEEDED(hres)) {
135 for(i=0; i < arg_cnt(dp); i++) {
136 arg = get_arg(dp, i);
137 if(V_VT(arg) == VT_DISPATCH)
138 hres = concat_obj(ret, V_DISPATCH(arg), &len, lcid, ei, caller);
140 hres = jsdisp_propput_idx(ret, len++, lcid, arg, ei, caller);
150 V_VT(retv) = VT_DISPATCH;
151 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
158 static HRESULT array_join(DispatchEx *array, LCID lcid, DWORD length, const WCHAR *sep, VARIANT *retv,
159 jsexcept_t *ei, IServiceProvider *caller)
161 BSTR *str_tab, ret = NULL;
164 HRESULT hres = E_FAIL;
168 V_VT(retv) = VT_BSTR;
169 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
171 return E_OUTOFMEMORY;
176 str_tab = heap_alloc_zero(length * sizeof(BSTR));
178 return E_OUTOFMEMORY;
180 for(i=0; i < length; i++) {
181 hres = jsdisp_propget_idx(array, i, lcid, &var, ei, caller);
185 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
186 hres = to_string(array->ctx, &var, ei, str_tab+i);
192 if(SUCCEEDED(hres)) {
193 DWORD seplen = 0, len = 0;
196 seplen = strlenW(sep);
199 len = SysStringLen(str_tab[0]);
200 for(i=1; i < length; i++)
201 len += seplen + SysStringLen(str_tab[i]);
203 ret = SysAllocStringLen(NULL, len);
208 tmplen = SysStringLen(str_tab[0]);
209 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
213 for(i=1; i < length; i++) {
215 memcpy(ptr, sep, seplen*sizeof(WCHAR));
220 tmplen = SysStringLen(str_tab[i]);
221 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
227 hres = E_OUTOFMEMORY;
231 for(i=0; i < length; i++)
232 SysFreeString(str_tab[i]);
237 TRACE("= %s\n", debugstr_w(ret));
241 ret = SysAllocStringLen(NULL, 0);
243 return E_OUTOFMEMORY;
246 V_VT(retv) = VT_BSTR;
255 /* ECMA-262 3rd Edition 15.4.4.5 */
256 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
257 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
264 if(is_class(dispex, JSCLASS_ARRAY)) {
265 length = ((ArrayInstance*)dispex)->length;
267 FIXME("dispid is not Array\n");
274 hres = to_string(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &sep);
278 hres = array_join(dispex, lcid, length, sep, retv, ei, caller);
282 hres = array_join(dispex, lcid, length, default_separatorW, retv, ei, caller);
288 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
289 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
295 /* ECMA-262 3rd Edition 15.4.4.7 */
296 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
297 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
305 if(dispex->builtin_info->class == JSCLASS_ARRAY) {
306 length = ((ArrayInstance*)dispex)->length;
308 FIXME("not Array this\n");
312 n = dp->cArgs - dp->cNamedArgs;
313 for(i=0; i < n; i++) {
314 hres = jsdisp_propput_idx(dispex, length+i, lcid, get_arg(dp, i), ei, sp);
321 V_I4(retv) = length+n;
326 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
327 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
333 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
334 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
340 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
341 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
347 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
348 IServiceProvider *caller, INT *cmp)
354 DISPPARAMS dp = {args, NULL, 2, 0};
361 hres = jsdisp_call_value(cmp_func, ctx->lcid, DISPATCH_METHOD, &dp, &res, ei, caller);
365 hres = to_number(ctx, &res, ei, &tmp);
370 if(V_VT(&tmp) == VT_I4)
373 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
374 }else if(is_num_vt(V_VT(v1))) {
375 if(is_num_vt(V_VT(v2))) {
376 DOUBLE d = num_val(v1)-num_val(v2);
386 }else if(is_num_vt(V_VT(v2))) {
388 }else if(V_VT(v1) == VT_BSTR) {
389 if(V_VT(v2) == VT_BSTR)
390 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
393 }else if(V_VT(v2) == VT_BSTR) {
402 /* ECMA-262 3rd Edition 15.4.4.11 */
403 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
404 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
406 DispatchEx *cmp_func = NULL;
407 VARIANT *vtab, **sorttab = NULL;
414 if(is_class(dispex, JSCLASS_ARRAY)) {
415 length = ((ArrayInstance*)dispex)->length;
417 FIXME("unsupported this not array\n");
421 if(arg_cnt(dp) > 1) {
422 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
426 if(arg_cnt(dp) == 1) {
427 VARIANT *arg = get_arg(dp, 0);
429 if(V_VT(arg) != VT_DISPATCH) {
430 WARN("arg is not dispatch\n");
435 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
436 if(!is_class(cmp_func, JSCLASS_FUNCTION)) {
437 WARN("cmp_func is not a function\n");
438 jsdisp_release(cmp_func);
445 jsdisp_release(cmp_func);
447 V_VT(retv) = VT_DISPATCH;
448 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
449 IDispatchEx_AddRef(_IDispatchEx_(dispex));
454 vtab = heap_alloc_zero(length * sizeof(VARIANT));
456 for(i=0; i<length; i++) {
457 hres = jsdisp_propget_idx(dispex, i, lcid, vtab+i, ei, caller);
458 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
459 WARN("Could not get elem %d: %08x\n", i, hres);
464 hres = E_OUTOFMEMORY;
467 if(SUCCEEDED(hres)) {
468 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
470 hres = E_OUTOFMEMORY;
474 if(SUCCEEDED(hres)) {
475 VARIANT *tmpv, **tmpbuf;
478 tmpbuf = sorttab + length;
479 for(i=0; i < length; i++)
482 for(i=0; i < length/2; i++) {
483 hres = sort_cmp(dispex->ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
489 sorttab[2*i] = sorttab[2*i+1];
490 sorttab[2*i+1] = tmpv;
494 if(SUCCEEDED(hres)) {
497 for(k=2; k < length; k *= 2) {
498 for(i=0; i+k < length; i += 2*k) {
503 bend = length - (i+k);
505 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
507 while(a < k && b < bend) {
508 hres = sort_cmp(dispex->ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
513 sorttab[i+a+b] = tmpbuf[a];
516 sorttab[i+a+b] = sorttab[i+k+b];
525 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
533 for(i=0; SUCCEEDED(hres) && i < length; i++)
534 hres = jsdisp_propput_idx(dispex, i, lcid, sorttab[i], ei, caller);
538 for(i=0; i < length; i++)
539 VariantClear(vtab+i);
544 jsdisp_release(cmp_func);
550 V_VT(retv) = VT_DISPATCH;
551 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
552 IDispatch_AddRef(_IDispatchEx_(dispex));
558 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
559 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
565 /* ECMA-262 3rd Edition 15.4.4.2 */
566 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
567 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
571 if(!is_class(dispex, JSCLASS_ARRAY)) {
572 WARN("not Array object\n");
576 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
579 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
580 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
586 static HRESULT Array_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
587 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
593 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
594 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
600 static HRESULT Array_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
601 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
607 static HRESULT Array_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
608 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
614 static HRESULT Array_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
615 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
621 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
622 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
627 case INVOKE_PROPERTYGET:
628 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
630 FIXME("unimplemented flags %x\n", flags);
637 static void Array_destructor(DispatchEx *dispex)
642 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
644 ArrayInstance *array = (ArrayInstance*)dispex;
645 const WCHAR *ptr = name;
651 while(*ptr && isdigitW(*ptr)) {
652 id = id*10 + (*ptr-'0');
659 if(id >= array->length)
660 array->length = id+1;
663 static const builtin_prop_t Array_props[] = {
664 {concatW, Array_concat, PROPF_METHOD},
665 {hasOwnPropertyW, Array_hasOwnProperty, PROPF_METHOD},
666 {isPrototypeOfW, Array_isPrototypeOf, PROPF_METHOD},
667 {joinW, Array_join, PROPF_METHOD},
668 {lengthW, Array_length, 0},
669 {popW, Array_pop, PROPF_METHOD},
670 {propertyIsEnumerableW, Array_propertyIsEnumerable, PROPF_METHOD},
671 {pushW, Array_push, PROPF_METHOD},
672 {reverseW, Array_reverse, PROPF_METHOD},
673 {shiftW, Array_shift, PROPF_METHOD},
674 {sliceW, Array_slice, PROPF_METHOD},
675 {sortW, Array_sort, PROPF_METHOD},
676 {spliceW, Array_splice, PROPF_METHOD},
677 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
678 {toStringW, Array_toString, PROPF_METHOD},
679 {unshiftW, Array_unshift, PROPF_METHOD},
680 {valueOfW, Array_valueOf, PROPF_METHOD}
683 static const builtin_info_t Array_info = {
685 {NULL, Array_value, 0},
686 sizeof(Array_props)/sizeof(*Array_props),
692 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
693 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
703 case DISPATCH_CONSTRUCT: {
704 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
705 if(V_I4(arg_var) < 0) {
706 FIXME("throw RangeError\n");
710 hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
714 V_VT(retv) = VT_DISPATCH;
715 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
719 hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
723 for(i=0; i < arg_cnt(dp); i++) {
724 hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
733 V_VT(retv) = VT_DISPATCH;
734 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
738 FIXME("unimplemented flags: %x\n", flags);
745 static HRESULT alloc_array(script_ctx_t *ctx, BOOL use_constr, ArrayInstance **ret)
747 ArrayInstance *array = heap_alloc_zero(sizeof(ArrayInstance));
751 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
753 hres = init_dispex(&array->dispex, ctx, &Array_info, NULL);
764 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx **ret)
766 ArrayInstance *array;
769 hres = alloc_array(ctx, FALSE, &array);
773 hres = create_builtin_function(ctx, ArrayConstr_value, PROPF_CONSTR, &array->dispex, ret);
775 IDispatchEx_Release(_IDispatchEx_(&array->dispex));
779 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
781 ArrayInstance *array;
784 hres = alloc_array(ctx, TRUE, &array);
788 array->length = length;
790 *ret = &array->dispex;