array.c 29.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2008 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

19 20 21
#include "config.h"
#include "wine/port.h"

22 23
#include <math.h>

24 25 26 27 28 29 30
#include "jscript.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

typedef struct {
31
    jsdisp_t dispex;
32 33

    DWORD length;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
} ArrayInstance;

static const WCHAR lengthW[] = {'l','e','n','g','t','h',0};
static const WCHAR concatW[] = {'c','o','n','c','a','t',0};
static const WCHAR joinW[] = {'j','o','i','n',0};
static const WCHAR popW[] = {'p','o','p',0};
static const WCHAR pushW[] = {'p','u','s','h',0};
static const WCHAR reverseW[] = {'r','e','v','e','r','s','e',0};
static const WCHAR shiftW[] = {'s','h','i','f','t',0};
static const WCHAR sliceW[] = {'s','l','i','c','e',0};
static const WCHAR sortW[] = {'s','o','r','t',0};
static const WCHAR spliceW[] = {'s','p','l','i','c','e',0};
static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
static const WCHAR toLocaleStringW[] = {'t','o','L','o','c','a','l','e','S','t','r','i','n','g',0};
static const WCHAR unshiftW[] = {'u','n','s','h','i','f','t',0};

50
static const WCHAR default_separatorW[] = {',',0};
51

52
static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
53
{
54 55 56 57 58 59 60 61
    return (ArrayInstance*)vdisp->u.jsdisp;
}

static inline ArrayInstance *array_this(vdisp_t *jsthis)
{
    return is_vclass(jsthis, JSCLASS_ARRAY) ? array_from_vdisp(jsthis) : NULL;
}

62
static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsdisp_t **jsthis, DWORD *ret)
63 64
{
    ArrayInstance *array;
65
    jsval_t val;
66 67
    HRESULT hres;

68 69 70 71 72 73 74
    array = array_this(vdisp);
    if(array) {
        *jsthis = &array->dispex;
        *ret = array->length;
        return S_OK;
    }

75
    if(!is_jsdisp(vdisp))
76
        return throw_type_error(ctx, JS_E_JSCRIPT_EXPECTED, NULL);
77

78
    hres = jsdisp_propget_name(vdisp->u.jsdisp, lengthW, &val);
79 80 81
    if(FAILED(hres))
        return hres;

82
    hres = to_uint32(ctx, val, ret);
83
    jsval_release(val);
84 85 86 87 88
    if(FAILED(hres))
        return hres;

    *jsthis = vdisp->u.jsdisp;
    return S_OK;
89 90
}

91
static HRESULT set_length(jsdisp_t *obj, DWORD length)
92
{
93 94 95 96 97
    if(is_class(obj, JSCLASS_ARRAY)) {
        ((ArrayInstance*)obj)->length = length;
        return S_OK;
    }

98
    return jsdisp_propput_name(obj, lengthW, jsval_number(length));
99 100
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static WCHAR *idx_to_str(DWORD idx, WCHAR *ptr)
{
    if(!idx) {
        *ptr = '0';
        return ptr;
    }

    while(idx) {
        *ptr-- = '0' + (idx%10);
        idx /= 10;
    }

    return ptr+1;
}

116
static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
117
        jsval_t *r)
118
{
119
    ArrayInstance *This = array_from_vdisp(jsthis);
120 121 122 123 124

    TRACE("%p %d\n", This, This->length);

    switch(flags) {
    case DISPATCH_PROPERTYGET:
125
        *r = jsval_number(This->length);
126
        break;
127 128 129 130 131
    case DISPATCH_PROPERTYPUT: {
        DOUBLE len = -1;
        DWORD i;
        HRESULT hres;

132
        hres = to_number(ctx, argv[0], &len);
133 134 135
        if(FAILED(hres))
            return hres;

136
        len = floor(len);
137
        if(len!=(DWORD)len)
138
            return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
139 140

        for(i=len; i<This->length; i++) {
141
            hres = jsdisp_delete_idx(&This->dispex, i);
142 143 144 145 146 147 148
            if(FAILED(hres))
                return hres;
        }

        This->length = len;
        break;
    }
149 150 151 152 153 154
    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
155 156
}

157
static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len)
158
{
159
    jsval_t val;
160 161 162 163
    DWORD i;
    HRESULT hres;

    for(i=0; i < obj->length; i++) {
164
        hres = jsdisp_get_idx(&obj->dispex, i, &val);
165 166 167 168 169
        if(hres == DISP_E_UNKNOWNNAME)
            continue;
        if(FAILED(hres))
            return hres;

170
        hres = jsdisp_propput_idx(array, *len+i, val);
171
        jsval_release(val);
172 173 174 175 176 177 178 179
        if(FAILED(hres))
            return hres;
    }

    *len += obj->length;
    return S_OK;
}

180
static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len)
181
{
182
    jsdisp_t *jsobj;
183 184 185 186 187
    HRESULT hres;

    jsobj = iface_to_jsdisp((IUnknown*)obj);
    if(jsobj) {
        if(is_class(jsobj, JSCLASS_ARRAY)) {
188
            hres = concat_array(array, (ArrayInstance*)jsobj, len);
189 190 191 192 193 194
            jsdisp_release(jsobj);
            return hres;
        }
        jsdisp_release(jsobj);
    }

195
    return jsdisp_propput_idx(array, (*len)++, jsval_disp(obj));
196 197
}

198
static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
199
        jsval_t *r)
200
{
201
    jsdisp_t *ret;
202 203 204 205 206
    DWORD len = 0;
    HRESULT hres;

    TRACE("\n");

207
    hres = create_array(ctx, 0, &ret);
208 209 210
    if(FAILED(hres))
        return hres;

211
    hres = concat_obj(ret, jsthis->u.disp, &len);
212 213 214
    if(SUCCEEDED(hres)) {
        DWORD i;

215
        for(i=0; i < argc; i++) {
216
            if(is_object_instance(argv[i]))
217
                hres = concat_obj(ret, get_object(argv[i]), &len);
218
            else
219
                hres = jsdisp_propput_idx(ret, len++, argv[i]);
220 221 222 223 224 225 226 227
            if(FAILED(hres))
                break;
        }
    }

    if(FAILED(hres))
        return hres;

228 229
    if(r)
        *r = jsval_obj(ret);
230
    else
231 232
        jsdisp_release(ret);
    return S_OK;
233 234
}

235
static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, jsval_t *r)
236
{
237
    jsstr_t **str_tab, *ret = NULL;
238
    jsval_t val;
239 240 241 242
    DWORD i;
    HRESULT hres = E_FAIL;

    if(!length) {
243 244
        if(r)
            *r = jsval_string(jsstr_empty());
245 246 247
        return S_OK;
    }

248
    str_tab = heap_alloc_zero(length * sizeof(*str_tab));
249 250 251 252
    if(!str_tab)
        return E_OUTOFMEMORY;

    for(i=0; i < length; i++) {
253
        hres = jsdisp_get_idx(array, i, &val);
254 255 256 257
        if(hres == DISP_E_UNKNOWNNAME) {
            hres = S_OK;
            continue;
        } else if(FAILED(hres))
258 259
            break;

260
        if(!is_undefined(val) && !is_null(val)) {
261
            hres = to_string(ctx, val, str_tab+i);
262 263 264 265
            jsval_release(val);
            if(FAILED(hres))
                break;
        }
266 267 268 269 270 271 272 273 274
    }

    if(SUCCEEDED(hres)) {
        DWORD seplen = 0, len = 0;
        WCHAR *ptr;

        seplen = strlenW(sep);

        if(str_tab[0])
275 276 277 278 279 280 281 282 283 284
            len = jsstr_length(str_tab[0]);
        for(i=1; i < length; i++) {
            len += seplen;
            if(str_tab[i])
                len += jsstr_length(str_tab[i]);
            if(len > JSSTR_MAX_LENGTH) {
                hres = E_OUTOFMEMORY;
                break;
            }
        }
285

286 287
        if(SUCCEEDED(hres))
            ret = jsstr_alloc_buf(len);
288
        if(ret) {
289 290 291
            unsigned tmplen;

            ptr = ret->str;
292 293

            if(str_tab[0]) {
294 295 296
                tmplen = jsstr_length(str_tab[0]);
                memcpy(ptr, str_tab[0]->str, tmplen*sizeof(WCHAR));
                ptr += tmplen;
297 298 299 300 301 302 303 304 305
            }

            for(i=1; i < length; i++) {
                if(seplen) {
                    memcpy(ptr, sep, seplen*sizeof(WCHAR));
                    ptr += seplen;
                }

                if(str_tab[i]) {
306 307
                    tmplen = jsstr_length(str_tab[i]);
                    memcpy(ptr, str_tab[i]->str, tmplen*sizeof(WCHAR));
308 309 310 311 312 313 314 315 316
                    ptr += tmplen;
                }
            }
            *ptr=0;
        }else {
            hres = E_OUTOFMEMORY;
        }
    }

317 318 319 320
    for(i=0; i < length; i++) {
        if(str_tab[i])
            jsstr_release(str_tab[i]);
    }
321 322 323 324
    heap_free(str_tab);
    if(FAILED(hres))
        return hres;

325
    TRACE("= %s\n", debugstr_jsstr(ret));
326

327 328 329 330
    if(r)
        *r = ret ? jsval_string(ret) : jsval_string(jsstr_empty());
    else
        jsstr_release(ret);
331 332 333 334
    return S_OK;
}

/* ECMA-262 3rd Edition    15.4.4.5 */
335
static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
336
        jsval_t *r)
337
{
338
    jsdisp_t *jsthis;
339 340 341 342 343
    DWORD length;
    HRESULT hres;

    TRACE("\n");

344
    hres = get_length(ctx, vthis, &jsthis, &length);
345 346
    if(FAILED(hres))
        return hres;
347

348
    if(argc) {
349
        jsstr_t *sep;
350

351
        hres = to_string(ctx, argv[0], &sep);
352 353 354
        if(FAILED(hres))
            return hres;

355
        hres = array_join(ctx, jsthis, length, sep->str, r);
356

357
        jsstr_release(sep);
358
    }else {
359
        hres = array_join(ctx, jsthis, length, default_separatorW, r);
360 361 362
    }

    return hres;
363 364
}

365
static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
366
        jsval_t *r)
367
{
368
    jsdisp_t *jsthis;
369
    jsval_t val;
370 371 372 373 374
    DWORD length;
    HRESULT hres;

    TRACE("\n");

375
    hres = get_length(ctx, vthis, &jsthis, &length);
376 377
    if(FAILED(hres))
        return hres;
378 379

    if(!length) {
380
        hres = set_length(jsthis, 0);
381 382 383
        if(FAILED(hres))
            return hres;

384 385
        if(r)
            *r = jsval_undefined();
386 387 388
        return S_OK;
    }

389
    length--;
390
    hres = jsdisp_get_idx(jsthis, length, &val);
391
    if(SUCCEEDED(hres))
392
        hres = jsdisp_delete_idx(jsthis, length);
393 394 395
    else if(hres == DISP_E_UNKNOWNNAME)
        val = jsval_undefined();
    else
396 397
        return hres;

398
    if(SUCCEEDED(hres))
399
        hres = set_length(jsthis, length);
400 401

    if(FAILED(hres)) {
402
        jsval_release(val);
403 404 405
        return hres;
    }

406
    if(r)
407 408 409
        *r = val;
    else
        jsval_release(val);
410
    return hres;
411 412
}

413
/* ECMA-262 3rd Edition    15.4.4.7 */
414
static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
415
        jsval_t *r)
416
{
417
    jsdisp_t *jsthis;
418
    DWORD length = 0;
419
    unsigned i;
420 421 422 423
    HRESULT hres;

    TRACE("\n");

424
    hres = get_length(ctx, vthis, &jsthis, &length);
425 426
    if(FAILED(hres))
        return hres;
427

428
    for(i=0; i < argc; i++) {
429
        hres = jsdisp_propput_idx(jsthis, length+i, argv[i]);
430 431 432 433
        if(FAILED(hres))
            return hres;
    }

434
    hres = set_length(jsthis, length+argc);
435 436
    if(FAILED(hres))
        return hres;
437

438 439
    if(r)
        *r = jsval_number(length+argc);
440
    return S_OK;
441 442
}

443
static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
444
        jsval_t *r)
445
{
446
    jsdisp_t *jsthis;
447
    DWORD length, k, l;
448
    jsval_t v1, v2;
449 450 451 452
    HRESULT hres1, hres2;

    TRACE("\n");

453
    hres1 = get_length(ctx, vthis, &jsthis, &length);
454 455 456 457 458 459
    if(FAILED(hres1))
        return hres1;

    for(k=0; k<length/2; k++) {
        l = length-k-1;

460
        hres1 = jsdisp_get_idx(jsthis, k, &v1);
461
        if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
462 463
            return hres1;

464
        hres2 = jsdisp_get_idx(jsthis, l, &v2);
465
        if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
466
            jsval_release(v1);
467 468
            return hres2;
        }
469 470

        if(hres1 == DISP_E_UNKNOWNNAME)
471
            hres1 = jsdisp_delete_idx(jsthis, l);
472
        else
473
            hres1 = jsdisp_propput_idx(jsthis, l, v1);
474 475

        if(FAILED(hres1)) {
476 477
            jsval_release(v1);
            jsval_release(v2);
478 479
            return hres1;
        }
480 481

        if(hres2 == DISP_E_UNKNOWNNAME)
482
            hres2 = jsdisp_delete_idx(jsthis, k);
483
        else
484
            hres2 = jsdisp_propput_idx(jsthis, k, v2);
485 486

        if(FAILED(hres2)) {
487
            jsval_release(v2);
488 489
            return hres2;
        }
490 491
    }

492 493
    if(r)
        *r = jsval_obj(jsdisp_addref(jsthis));
494
    return S_OK;
495 496
}

497
/* ECMA-262 3rd Edition    15.4.4.9 */
498
static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
499
        jsval_t *r)
500
{
501
    jsdisp_t *jsthis;
502
    DWORD length = 0, i;
503
    jsval_t v, ret;
504 505 506 507
    HRESULT hres;

    TRACE("\n");

508
    hres = get_length(ctx, vthis, &jsthis, &length);
509 510 511 512
    if(FAILED(hres))
        return hres;

    if(!length) {
513
        hres = set_length(jsthis, 0);
514 515 516 517 518
        if(FAILED(hres))
            return hres;
    }

    if(!length) {
519 520
        if(r)
            *r = jsval_undefined();
521 522 523
        return S_OK;
    }

524
    hres = jsdisp_get_idx(jsthis, 0, &ret);
525
    if(hres == DISP_E_UNKNOWNNAME) {
526
        ret = jsval_undefined();
527 528 529 530
        hres = S_OK;
    }

    for(i=1; SUCCEEDED(hres) && i<length; i++) {
531
        hres = jsdisp_get_idx(jsthis, i, &v);
532
        if(hres == DISP_E_UNKNOWNNAME)
533
            hres = jsdisp_delete_idx(jsthis, i-1);
534
        else if(SUCCEEDED(hres))
535
            hres = jsdisp_propput_idx(jsthis, i-1, v);
536 537 538
    }

    if(SUCCEEDED(hres)) {
539
        hres = jsdisp_delete_idx(jsthis, length-1);
540
        if(SUCCEEDED(hres))
541
            hres = set_length(jsthis, length-1);
542 543
    }

544 545 546 547 548 549 550
    if(FAILED(hres))
        return hres;

    if(r)
        *r = ret;
    else
        jsval_release(ret);
551
    return hres;
552 553
}

554
/* ECMA-262 3rd Edition    15.4.4.10 */
555
static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
556
{
557
    jsdisp_t *arr, *jsthis;
558 559 560 561 562 563
    DOUBLE range;
    DWORD length, start, end, idx;
    HRESULT hres;

    TRACE("\n");

564
    hres = get_length(ctx, vthis, &jsthis, &length);
565 566
    if(FAILED(hres))
        return hres;
567

568
    if(argc) {
569
        hres = to_number(ctx, argv[0], &range);
570 571 572
        if(FAILED(hres))
            return hres;

573
        range = floor(range);
574 575 576 577 578 579 580
        if(-range>length || isnan(range)) start = 0;
        else if(range < 0) start = range+length;
        else if(range <= length) start = range;
        else start = length;
    }
    else start = 0;

581
    if(argc > 1) {
582
        hres = to_number(ctx, argv[1], &range);
583 584 585
        if(FAILED(hres))
            return hres;

586
        range = floor(range);
587 588 589 590 591 592 593
        if(-range>length) end = 0;
        else if(range < 0) end = range+length;
        else if(range <= length) end = range;
        else end = length;
    }
    else end = length;

594
    hres = create_array(ctx, (end>start)?end-start:0, &arr);
595 596 597 598
    if(FAILED(hres))
        return hres;

    for(idx=start; idx<end; idx++) {
599
        jsval_t v;
600

601
        hres = jsdisp_get_idx(jsthis, idx, &v);
602 603 604
        if(hres == DISP_E_UNKNOWNNAME)
            continue;

605
        if(SUCCEEDED(hres)) {
606
            hres = jsdisp_propput_idx(arr, idx-start, v);
607
            jsval_release(v);
608
        }
609 610 611 612 613 614 615

        if(FAILED(hres)) {
            jsdisp_release(arr);
            return hres;
        }
    }

616 617
    if(r)
        *r = jsval_obj(arr);
618 619 620 621
    else
        jsdisp_release(arr);

    return S_OK;
622 623
}

624
static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, INT *cmp)
625 626 627 628
{
    HRESULT hres;

    if(cmp_func) {
629
        jsval_t args[2] = {v1, v2};
630
        jsval_t res;
631
        double n;
632

633
        hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res);
634 635 636
        if(FAILED(hres))
            return hres;

637
        hres = to_number(ctx, res, &n);
638
        jsval_release(res);
639 640 641
        if(FAILED(hres))
            return hres;

642 643 644
        if(n == 0)
            *cmp = 0;
        *cmp = n > 0.0 ? 1 : -1;
645 646 647
    }else if(is_undefined(v1)) {
        *cmp = is_undefined(v2) ? 0 : 1;
    }else if(is_undefined(v2)) {
648
        *cmp = -1;
649 650
    }else if(is_number(v1) && is_number(v2)) {
        double d = get_number(v1)-get_number(v2);
651 652
        if(d > 0.0)
            *cmp = 1;
653
        else
654
            *cmp = d < -0.0 ? -1 : 0;
655
    }else {
656
        jsstr_t *x, *y;
657

658
        hres = to_string(ctx, v1, &x);
659 660 661
        if(FAILED(hres))
            return hres;

662
        hres = to_string(ctx, v2, &y);
663
        if(SUCCEEDED(hres)) {
664 665
            *cmp = jsstr_cmp(x, y);
            jsstr_release(y);
666
        }
667
        jsstr_release(x);
668 669
        if(FAILED(hres))
            return hres;
670 671 672 673 674 675
    }

    return S_OK;
}

/* ECMA-262 3rd Edition    15.4.4.11 */
676
static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
677
        jsval_t *r)
678
{
679
    jsdisp_t *jsthis, *cmp_func = NULL;
680
    jsval_t *vtab, **sorttab = NULL;
681 682 683 684 685 686
    DWORD length;
    DWORD i;
    HRESULT hres = S_OK;

    TRACE("\n");

687
    hres = get_length(ctx, vthis, &jsthis, &length);
688 689
    if(FAILED(hres))
        return hres;
690

691 692
    if(argc > 1) {
        WARN("invalid arg_cnt %d\n", argc);
693 694 695
        return E_FAIL;
    }

696
    if(argc == 1) {
697
        if(!is_object_instance(argv[0])) {
698 699 700 701
            WARN("arg is not dispatch\n");
            return E_FAIL;
        }

702
        cmp_func = iface_to_jsdisp((IUnknown*)get_object(argv[0]));
703
        if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
704
            WARN("cmp_func is not a function\n");
705 706
            if(cmp_func)
                jsdisp_release(cmp_func);
707 708 709 710 711 712 713
            return E_FAIL;
        }
    }

    if(!length) {
        if(cmp_func)
            jsdisp_release(cmp_func);
714 715
        if(r)
            *r = jsval_obj(jsdisp_addref(jsthis));
716 717 718
        return S_OK;
    }

719
    vtab = heap_alloc_zero(length * sizeof(*vtab));
720 721
    if(vtab) {
        for(i=0; i<length; i++) {
722
            hres = jsdisp_get_idx(jsthis, i, vtab+i);
723
            if(hres == DISP_E_UNKNOWNNAME) {
724
                vtab[i] = jsval_undefined();
725 726
                hres = S_OK;
            } else if(FAILED(hres)) {
727 728 729 730 731 732 733 734 735
                WARN("Could not get elem %d: %08x\n", i, hres);
                break;
            }
        }
    }else {
        hres = E_OUTOFMEMORY;
    }

    if(SUCCEEDED(hres)) {
736
        sorttab = heap_alloc(length*2*sizeof(*sorttab));
737 738 739 740 741 742
        if(!sorttab)
            hres = E_OUTOFMEMORY;
    }

    /* merge-sort */
    if(SUCCEEDED(hres)) {
743
        jsval_t *tmpv, **tmpbuf;
744 745 746 747 748 749 750
        INT cmp;

        tmpbuf = sorttab + length;
        for(i=0; i < length; i++)
            sorttab[i] = vtab+i;

        for(i=0; i < length/2; i++) {
751
            hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], &cmp);
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
            if(FAILED(hres))
                break;

            if(cmp < 0) {
                tmpv = sorttab[2*i];
                sorttab[2*i] = sorttab[2*i+1];
                sorttab[2*i+1] = tmpv;
            }
        }

        if(SUCCEEDED(hres)) {
            DWORD k, a, b, bend;

            for(k=2; k < length; k *= 2) {
                for(i=0; i+k < length; i += 2*k) {
                    a = b = 0;
                    if(i+2*k <= length)
                        bend = k;
                    else
                        bend = length - (i+k);

773
                    memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
774 775

                    while(a < k && b < bend) {
776
                        hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], &cmp);
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
                        if(FAILED(hres))
                            break;

                        if(cmp < 0) {
                            sorttab[i+a+b] = tmpbuf[a];
                            a++;
                        }else {
                            sorttab[i+a+b] = sorttab[i+k+b];
                            b++;
                        }
                    }

                    if(FAILED(hres))
                        break;

                    if(a < k)
793
                        memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
794 795 796 797 798 799 800 801
                }

                if(FAILED(hres))
                    break;
            }
        }

        for(i=0; SUCCEEDED(hres) && i < length; i++)
802
            hres = jsdisp_propput_idx(jsthis, i, *sorttab[i]);
803 804 805 806
    }

    if(vtab) {
        for(i=0; i < length; i++)
807
            jsval_release(vtab[i]);
808 809 810 811 812 813 814 815 816
        heap_free(vtab);
    }
    heap_free(sorttab);
    if(cmp_func)
        jsdisp_release(cmp_func);

    if(FAILED(hres))
        return hres;

817 818
    if(r)
        *r = jsval_obj(jsdisp_addref(jsthis));
819
    return S_OK;
820 821
}

822
/* ECMA-262 3rd Edition    15.4.4.12 */
823
static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
824
        jsval_t *r)
825
{
826
    DWORD length, start=0, delete_cnt=0, i, add_args = 0;
827
    jsdisp_t *ret_array = NULL, *jsthis;
828
    jsval_t val;
829 830
    double d;
    int n;
831 832 833 834
    HRESULT hres = S_OK;

    TRACE("\n");

835
    hres = get_length(ctx, vthis, &jsthis, &length);
836 837
    if(FAILED(hres))
        return hres;
838

839
    if(argc) {
840
        hres = to_integer(ctx, argv[0], &d);
841 842 843
        if(FAILED(hres))
            return hres;

844 845 846
        if(is_int32(d)) {
            if((n = d) >= 0)
                start = min(n, length);
847
            else
848
                start = -n > length ? 0 : length + n;
849
        }else {
850
            start = d < 0.0 ? 0 : length;
851 852 853 854
        }
    }

    if(argc >= 2) {
855
        hres = to_integer(ctx, argv[1], &d);
856 857 858
        if(FAILED(hres))
            return hres;

859 860 861 862
        if(is_int32(d)) {
            if((n = d) > 0)
                delete_cnt = min(n, length-start);
        }else if(d > 0.0) {
863 864 865 866 867 868
            delete_cnt = length-start;
        }

        add_args = argc-2;
    }

869
    if(r) {
870
        hres = create_array(ctx, 0, &ret_array);
871 872 873 874
        if(FAILED(hres))
            return hres;

        for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
875
            hres = jsdisp_get_idx(jsthis, start+i, &val);
876
            if(hres == DISP_E_UNKNOWNNAME) {
877
                hres = S_OK;
878
            }else if(SUCCEEDED(hres)) {
879
                hres = jsdisp_propput_idx(ret_array, i, val);
880 881
                jsval_release(val);
            }
882 883
        }

884
        if(SUCCEEDED(hres))
885
            hres = jsdisp_propput_name(ret_array, lengthW, jsval_number(delete_cnt));
886 887 888 889
    }

    if(add_args < delete_cnt) {
        for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
890
            hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val);
891
            if(hres == DISP_E_UNKNOWNNAME) {
892
                hres = jsdisp_delete_idx(jsthis, i+add_args);
893
            }else if(SUCCEEDED(hres)) {
894
                hres = jsdisp_propput_idx(jsthis, i+add_args, val);
895 896
                jsval_release(val);
            }
897 898 899
        }

        for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
900
            hres = jsdisp_delete_idx(jsthis, i-1);
901 902
    }else if(add_args > delete_cnt) {
        for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
903
            hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val);
904
            if(hres == DISP_E_UNKNOWNNAME) {
905
                hres = jsdisp_delete_idx(jsthis, i+add_args-1);
906
            }else if(SUCCEEDED(hres)) {
907
                hres = jsdisp_propput_idx(jsthis, i+add_args-1, val);
908 909
                jsval_release(val);
            }
910 911 912 913
        }
    }

    for(i=0; SUCCEEDED(hres) && i < add_args; i++)
914
        hres = jsdisp_propput_idx(jsthis, start+i, argv[i+2]);
915

916
    if(SUCCEEDED(hres))
917
        hres = jsdisp_propput_name(jsthis, lengthW, jsval_number(length-delete_cnt+add_args));
918 919 920 921 922 923 924

    if(FAILED(hres)) {
        if(ret_array)
            jsdisp_release(ret_array);
        return hres;
    }

925 926
    if(r)
        *r = jsval_obj(ret_array);
927
    return S_OK;
928 929
}

930
/* ECMA-262 3rd Edition    15.4.4.2 */
931
static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
932
        jsval_t *r)
933
{
934 935
    ArrayInstance *array;

936 937
    TRACE("\n");

938
    array = array_this(jsthis);
939
    if(!array)
940
        return throw_type_error(ctx, JS_E_ARRAY_EXPECTED, NULL);
941

942
    return array_join(ctx, &array->dispex, array->length, default_separatorW, r);
943 944
}

945
static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
946
        jsval_t *r)
947 948 949 950 951
{
    FIXME("\n");
    return E_NOTIMPL;
}

952
/* ECMA-262 3rd Edition    15.4.4.13 */
953
static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
954
        jsval_t *r)
955
{
956
    jsdisp_t *jsthis;
957
    WCHAR buf[14], *buf_end, *str;
958
    DWORD i, length;
959
    jsval_t val;
960 961 962 963 964
    DISPID id;
    HRESULT hres;

    TRACE("\n");

965
    hres = get_length(ctx, vthis, &jsthis, &length);
966 967
    if(FAILED(hres))
        return hres;
968

969 970 971 972
    if(argc) {
        buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
        *buf_end-- = 0;
        i = length;
973

974 975
        while(i--) {
            str = idx_to_str(i, buf_end);
976

977 978
            hres = jsdisp_get_id(jsthis, str, 0, &id);
            if(SUCCEEDED(hres)) {
979
                hres = jsdisp_propget(jsthis, id, &val);
980 981
                if(FAILED(hres))
                    return hres;
982

983
                hres = jsdisp_propput_idx(jsthis, i+argc, val);
984
                jsval_release(val);
985 986 987
            }else if(hres == DISP_E_UNKNOWNNAME) {
                hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
            }
988 989 990 991 992 993 994
        }

        if(FAILED(hres))
            return hres;
    }

    for(i=0; i<argc; i++) {
995
        hres = jsdisp_propput_idx(jsthis, i, argv[i]);
996 997 998 999
        if(FAILED(hres))
            return hres;
    }

1000 1001
    if(argc) {
        length += argc;
1002
        hres = set_length(jsthis, length);
1003 1004 1005
        if(FAILED(hres))
            return hres;
    }
1006

1007 1008
    if(r)
        *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
1009
    return S_OK;
1010 1011
}

1012
static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
1013
        jsval_t *r)
1014
{
1015 1016 1017
    TRACE("\n");

    switch(flags) {
1018
    case INVOKE_FUNC:
1019
        return throw_type_error(ctx, JS_E_FUNCTION_EXPECTED, NULL);
1020
    case INVOKE_PROPERTYGET:
1021
        return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, r);
1022 1023 1024 1025 1026 1027
    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
1028 1029
}

1030
static void Array_destructor(jsdisp_t *dispex)
1031 1032 1033 1034
{
    heap_free(dispex);
}

1035
static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1036
{
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    ArrayInstance *array = (ArrayInstance*)dispex;
    const WCHAR *ptr = name;
    DWORD id = 0;

    if(!isdigitW(*ptr))
        return;

    while(*ptr && isdigitW(*ptr)) {
        id = id*10 + (*ptr-'0');
        ptr++;
    }

    if(*ptr)
        return;

    if(id >= array->length)
        array->length = id+1;
1054 1055 1056
}

static const builtin_prop_t Array_props[] = {
1057 1058
    {concatW,                Array_concat,               PROPF_METHOD|1},
    {joinW,                  Array_join,                 PROPF_METHOD|1},
1059 1060
    {lengthW,                Array_length,               0},
    {popW,                   Array_pop,                  PROPF_METHOD},
1061
    {pushW,                  Array_push,                 PROPF_METHOD|1},
1062 1063
    {reverseW,               Array_reverse,              PROPF_METHOD},
    {shiftW,                 Array_shift,                PROPF_METHOD},
1064 1065 1066
    {sliceW,                 Array_slice,                PROPF_METHOD|2},
    {sortW,                  Array_sort,                 PROPF_METHOD|1},
    {spliceW,                Array_splice,               PROPF_METHOD|2},
1067 1068
    {toLocaleStringW,        Array_toLocaleString,       PROPF_METHOD},
    {toStringW,              Array_toString,             PROPF_METHOD},
1069
    {unshiftW,               Array_unshift,              PROPF_METHOD|1},
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
};

static const builtin_info_t Array_info = {
    JSCLASS_ARRAY,
    {NULL, Array_value, 0},
    sizeof(Array_props)/sizeof(*Array_props),
    Array_props,
    Array_destructor,
    Array_on_put
};

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
static const builtin_prop_t ArrayInst_props[] = {
    {lengthW,                Array_length,               0}
};

static const builtin_info_t ArrayInst_info = {
    JSCLASS_ARRAY,
    {NULL, Array_value, 0},
    sizeof(ArrayInst_props)/sizeof(*ArrayInst_props),
    ArrayInst_props,
    Array_destructor,
    Array_on_put
};

1094
static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1095
        jsval_t *r)
1096
{
1097
    jsdisp_t *obj;
1098 1099 1100 1101 1102 1103
    DWORD i;
    HRESULT hres;

    TRACE("\n");

    switch(flags) {
1104
    case DISPATCH_METHOD:
1105
    case DISPATCH_CONSTRUCT: {
1106 1107 1108 1109
        if(argc == 1 && is_number(argv[0])) {
            double n = get_number(argv[0]);

            if(n < 0 || !is_int32(n))
1110
                return throw_range_error(ctx, JS_E_INVALID_LENGTH, NULL);
1111

1112
            hres = create_array(ctx, n, &obj);
1113 1114 1115
            if(FAILED(hres))
                return hres;

1116
            *r = jsval_obj(obj);
1117 1118 1119
            return S_OK;
        }

1120
        hres = create_array(ctx, argc, &obj);
1121 1122 1123
        if(FAILED(hres))
            return hres;

1124
        for(i=0; i < argc; i++) {
1125
            hres = jsdisp_propput_idx(obj, i, argv[i]);
1126 1127 1128 1129 1130 1131 1132 1133
            if(FAILED(hres))
                break;
        }
        if(FAILED(hres)) {
            jsdisp_release(obj);
            return hres;
        }

1134
        *r = jsval_obj(obj);
1135 1136 1137 1138 1139 1140 1141 1142
        break;
    }
    default:
        FIXME("unimplemented flags: %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
1143 1144
}

1145
static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1146
{
1147
    ArrayInstance *array;
1148 1149
    HRESULT hres;

1150 1151 1152 1153
    array = heap_alloc_zero(sizeof(ArrayInstance));
    if(!array)
        return E_OUTOFMEMORY;

1154 1155
    if(object_prototype)
        hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1156
    else
1157
        hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

    if(FAILED(hres)) {
        heap_free(array);
        return hres;
    }

    *ret = array;
    return S_OK;
}

1168
HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1169 1170 1171 1172
{
    ArrayInstance *array;
    HRESULT hres;

1173 1174
    static const WCHAR ArrayW[] = {'A','r','r','a','y',0};

1175
    hres = alloc_array(ctx, object_prototype, &array);
1176 1177 1178
    if(FAILED(hres))
        return hres;

1179
    hres = create_builtin_constructor(ctx, ArrayConstr_value, ArrayW, NULL, PROPF_CONSTR|1, &array->dispex, ret);
1180

Jacek Caban's avatar
Jacek Caban committed
1181
    jsdisp_release(&array->dispex);
1182 1183
    return hres;
}
1184

1185
HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1186 1187 1188 1189
{
    ArrayInstance *array;
    HRESULT hres;

1190
    hres = alloc_array(ctx, NULL, &array);
1191 1192 1193 1194 1195 1196 1197 1198
    if(FAILED(hres))
        return hres;

    array->length = length;

    *ret = &array->dispex;
    return S_OK;
}