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 Array_concat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
73 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
79 static HRESULT array_join(DispatchEx *array, LCID lcid, DWORD length, const WCHAR *sep, VARIANT *retv,
80 jsexcept_t *ei, IServiceProvider *caller)
82 BSTR *str_tab, ret = NULL;
85 HRESULT hres = E_FAIL;
90 V_BSTR(retv) = SysAllocStringLen(NULL, 0);
97 str_tab = heap_alloc_zero(length * sizeof(BSTR));
101 for(i=0; i < length; i++) {
102 hres = jsdisp_propget_idx(array, i, lcid, &var, ei, caller);
106 if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
107 hres = to_string(array->ctx, &var, ei, str_tab+i);
113 if(SUCCEEDED(hres)) {
114 DWORD seplen = 0, len = 0;
117 seplen = strlenW(sep);
120 len = SysStringLen(str_tab[0]);
121 for(i=1; i < length; i++)
122 len += seplen + SysStringLen(str_tab[i]);
124 ret = SysAllocStringLen(NULL, len);
129 tmplen = SysStringLen(str_tab[0]);
130 memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
134 for(i=1; i < length; i++) {
136 memcpy(ptr, sep, seplen*sizeof(WCHAR));
141 tmplen = SysStringLen(str_tab[i]);
142 memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
148 hres = E_OUTOFMEMORY;
152 for(i=0; i < length; i++)
153 SysFreeString(str_tab[i]);
158 TRACE("= %s\n", debugstr_w(ret));
162 ret = SysAllocStringLen(NULL, 0);
164 return E_OUTOFMEMORY;
167 V_VT(retv) = VT_BSTR;
176 /* ECMA-262 3rd Edition 15.4.4.5 */
177 static HRESULT Array_join(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
178 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
185 if(is_class(dispex, JSCLASS_ARRAY)) {
186 length = ((ArrayInstance*)dispex)->length;
188 FIXME("dispid is not Array\n");
195 hres = to_string(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &sep);
199 hres = array_join(dispex, lcid, length, sep, retv, ei, caller);
203 hres = array_join(dispex, lcid, length, default_separatorW, retv, ei, caller);
209 static HRESULT Array_pop(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
210 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
216 /* ECMA-262 3rd Edition 15.4.4.7 */
217 static HRESULT Array_push(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
218 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
226 if(dispex->builtin_info->class == JSCLASS_ARRAY) {
227 length = ((ArrayInstance*)dispex)->length;
229 FIXME("not Array this\n");
233 n = dp->cArgs - dp->cNamedArgs;
234 for(i=0; i < n; i++) {
235 hres = jsdisp_propput_idx(dispex, length+i, lcid, get_arg(dp, i), ei, sp);
242 V_I4(retv) = length+n;
247 static HRESULT Array_reverse(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
248 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
254 static HRESULT Array_shift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
255 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
261 static HRESULT Array_slice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
262 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
268 static HRESULT sort_cmp(script_ctx_t *ctx, DispatchEx *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei,
269 IServiceProvider *caller, INT *cmp)
275 DISPPARAMS dp = {args, NULL, 2, 0};
282 hres = jsdisp_call_value(cmp_func, ctx->lcid, DISPATCH_METHOD, &dp, &res, ei, caller);
286 hres = to_number(ctx, &res, ei, &tmp);
291 if(V_VT(&tmp) == VT_I4)
294 *cmp = V_R8(&tmp) > 0.0 ? 1 : -1;
295 }else if(is_num_vt(V_VT(v1))) {
296 if(is_num_vt(V_VT(v2))) {
297 DOUBLE d = num_val(v1)-num_val(v2);
307 }else if(is_num_vt(V_VT(v2))) {
309 }else if(V_VT(v1) == VT_BSTR) {
310 if(V_VT(v2) == VT_BSTR)
311 *cmp = strcmpW(V_BSTR(v1), V_BSTR(v2));
314 }else if(V_VT(v2) == VT_BSTR) {
323 /* ECMA-262 3rd Edition 15.4.4.11 */
324 static HRESULT Array_sort(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
325 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
327 DispatchEx *cmp_func = NULL;
328 VARIANT *vtab, **sorttab = NULL;
335 if(is_class(dispex, JSCLASS_ARRAY)) {
336 length = ((ArrayInstance*)dispex)->length;
338 FIXME("unsupported this not array\n");
342 if(arg_cnt(dp) > 1) {
343 WARN("invalid arg_cnt %d\n", arg_cnt(dp));
347 if(arg_cnt(dp) == 1) {
348 VARIANT *arg = get_arg(dp, 0);
350 if(V_VT(arg) != VT_DISPATCH) {
351 WARN("arg is not dispatch\n");
356 cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
357 if(!is_class(cmp_func, JSCLASS_FUNCTION)) {
358 WARN("cmp_func is not a function\n");
359 jsdisp_release(cmp_func);
366 jsdisp_release(cmp_func);
368 V_VT(retv) = VT_DISPATCH;
369 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
370 IDispatchEx_AddRef(_IDispatchEx_(dispex));
375 vtab = heap_alloc_zero(length * sizeof(VARIANT));
377 for(i=0; i<length; i++) {
378 hres = jsdisp_propget_idx(dispex, i, lcid, vtab+i, ei, caller);
379 if(FAILED(hres) && hres != DISP_E_UNKNOWNNAME) {
380 WARN("Could not get elem %d: %08x\n", i, hres);
385 hres = E_OUTOFMEMORY;
388 if(SUCCEEDED(hres)) {
389 sorttab = heap_alloc(length*2*sizeof(VARIANT*));
391 hres = E_OUTOFMEMORY;
395 if(SUCCEEDED(hres)) {
396 VARIANT *tmpv, **tmpbuf;
399 tmpbuf = sorttab + length;
400 for(i=0; i < length; i++)
403 for(i=0; i < length/2; i++) {
404 hres = sort_cmp(dispex->ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, caller, &cmp);
410 sorttab[2*i] = sorttab[2*i+1];
411 sorttab[2*i+1] = tmpv;
415 if(SUCCEEDED(hres)) {
418 for(k=2; k < length; k *= 2) {
419 for(i=0; i+k < length; i += 2*k) {
424 bend = length - (i+k);
426 memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));
428 while(a < k && b < bend) {
429 hres = sort_cmp(dispex->ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, caller, &cmp);
434 sorttab[i+a+b] = tmpbuf[a];
437 sorttab[i+a+b] = sorttab[i+k+b];
446 memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
454 for(i=0; SUCCEEDED(hres) && i < length; i++)
455 hres = jsdisp_propput_idx(dispex, i, lcid, sorttab[i], ei, caller);
459 for(i=0; i < length; i++)
460 VariantClear(vtab+i);
465 jsdisp_release(cmp_func);
471 V_VT(retv) = VT_DISPATCH;
472 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(dispex);
473 IDispatch_AddRef(_IDispatchEx_(dispex));
479 static HRESULT Array_splice(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
480 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
486 /* ECMA-262 3rd Edition 15.4.4.2 */
487 static HRESULT Array_toString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
488 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
492 if(!is_class(dispex, JSCLASS_ARRAY)) {
493 WARN("not Array object\n");
497 return array_join(dispex, lcid, ((ArrayInstance*)dispex)->length, default_separatorW, retv, ei, sp);
500 static HRESULT Array_toLocaleString(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
501 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
507 static HRESULT Array_valueOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
508 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
514 static HRESULT Array_unshift(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
515 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
521 static HRESULT Array_hasOwnProperty(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
522 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
528 static HRESULT Array_propertyIsEnumerable(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
529 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
535 static HRESULT Array_isPrototypeOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
536 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
542 static HRESULT Array_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
543 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
549 static void Array_destructor(DispatchEx *dispex)
554 static void Array_on_put(DispatchEx *dispex, const WCHAR *name)
556 ArrayInstance *array = (ArrayInstance*)dispex;
557 const WCHAR *ptr = name;
563 while(*ptr && isdigitW(*ptr)) {
564 id = id*10 + (*ptr-'0');
571 if(id >= array->length)
572 array->length = id+1;
575 static const builtin_prop_t Array_props[] = {
576 {concatW, Array_concat, PROPF_METHOD},
577 {hasOwnPropertyW, Array_hasOwnProperty, PROPF_METHOD},
578 {isPrototypeOfW, Array_isPrototypeOf, PROPF_METHOD},
579 {joinW, Array_join, PROPF_METHOD},
580 {lengthW, Array_length, 0},
581 {popW, Array_pop, PROPF_METHOD},
582 {propertyIsEnumerableW, Array_propertyIsEnumerable, PROPF_METHOD},
583 {pushW, Array_push, PROPF_METHOD},
584 {reverseW, Array_reverse, PROPF_METHOD},
585 {shiftW, Array_shift, PROPF_METHOD},
586 {sliceW, Array_slice, PROPF_METHOD},
587 {sortW, Array_sort, PROPF_METHOD},
588 {spliceW, Array_splice, PROPF_METHOD},
589 {toLocaleStringW, Array_toLocaleString, PROPF_METHOD},
590 {toStringW, Array_toString, PROPF_METHOD},
591 {unshiftW, Array_unshift, PROPF_METHOD},
592 {valueOfW, Array_valueOf, PROPF_METHOD}
595 static const builtin_info_t Array_info = {
597 {NULL, Array_value, 0},
598 sizeof(Array_props)/sizeof(*Array_props),
604 static HRESULT ArrayConstr_value(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
605 VARIANT *retv, jsexcept_t *ei, IServiceProvider *caller)
615 case DISPATCH_CONSTRUCT: {
616 if(arg_cnt(dp) == 1 && V_VT((arg_var = get_arg(dp, 0))) == VT_I4) {
617 if(V_I4(arg_var) < 0) {
618 FIXME("throw RangeError\n");
622 hres = create_array(dispex->ctx, V_I4(arg_var), &obj);
626 V_VT(retv) = VT_DISPATCH;
627 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
631 hres = create_array(dispex->ctx, arg_cnt(dp), &obj);
635 for(i=0; i < arg_cnt(dp); i++) {
636 hres = jsdisp_propput_idx(obj, i, lcid, get_arg(dp, i), ei, caller);
645 V_VT(retv) = VT_DISPATCH;
646 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(obj);
650 FIXME("unimplemented flags: %x\n", flags);
657 static HRESULT alloc_array(script_ctx_t *ctx, BOOL use_constr, ArrayInstance **ret)
659 ArrayInstance *array = heap_alloc_zero(sizeof(ArrayInstance));
663 hres = init_dispex_from_constr(&array->dispex, ctx, &Array_info, ctx->array_constr);
665 hres = init_dispex(&array->dispex, ctx, &Array_info, NULL);
676 HRESULT create_array_constr(script_ctx_t *ctx, DispatchEx **ret)
678 ArrayInstance *array;
681 hres = alloc_array(ctx, FALSE, &array);
685 hres = create_builtin_function(ctx, ArrayConstr_value, PROPF_CONSTR, &array->dispex, ret);
687 IDispatchEx_Release(_IDispatchEx_(&array->dispex));
691 HRESULT create_array(script_ctx_t *ctx, DWORD length, DispatchEx **ret)
693 ArrayInstance *array;
696 hres = alloc_array(ctx, TRUE, &array);
700 array->length = length;
702 *ret = &array->dispex;