array.c 30.5 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, jsexcept_t *ei, jsdisp_t **jsthis, DWORD *ret)
63 64
{
    ArrayInstance *array;
65 66 67
    VARIANT var;
    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, ei, JS_E_JSCRIPT_EXPECTED, NULL);
77

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

82
    hres = to_uint32(ctx, &var, ei, ret);
83
    VariantClear(&var);
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, jsexcept_t *ei, DWORD length)
92 93 94
{
    VARIANT var;

95 96 97 98 99
    if(is_class(obj, JSCLASS_ARRAY)) {
        ((ArrayInstance*)obj)->length = length;
        return S_OK;
    }

100
    num_set_int(&var, length);
101
    return jsdisp_propput_name(obj, lengthW, &var, ei);
102 103
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
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;
}

119
static HRESULT Array_length(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
120
        VARIANT *retv, jsexcept_t *ei)
121
{
122
    ArrayInstance *This = array_from_vdisp(jsthis);
123 124 125 126 127

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

    switch(flags) {
    case DISPATCH_PROPERTYGET:
128
        num_set_int(retv, This->length);
129
        break;
130 131 132 133 134
    case DISPATCH_PROPERTYPUT: {
        DOUBLE len = -1;
        DWORD i;
        HRESULT hres;

135
        hres = to_number(ctx, argv, ei, &len);
136 137 138
        if(FAILED(hres))
            return hres;

139
        len = floor(len);
140
        if(len!=(DWORD)len)
141
            return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
142 143

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

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

    return S_OK;
158 159
}

160
static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len, jsexcept_t *ei)
161 162 163 164 165 166
{
    VARIANT var;
    DWORD i;
    HRESULT hres;

    for(i=0; i < obj->length; i++) {
167
        hres = jsdisp_get_idx(&obj->dispex, i, &var, ei);
168 169 170 171 172
        if(hres == DISP_E_UNKNOWNNAME)
            continue;
        if(FAILED(hres))
            return hres;

173
        hres = jsdisp_propput_idx(array, *len+i, &var, ei);
174 175 176 177 178 179 180 181 182
        VariantClear(&var);
        if(FAILED(hres))
            return hres;
    }

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

183
static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len, jsexcept_t *ei)
184
{
185
    jsdisp_t *jsobj;
186 187 188 189 190 191
    VARIANT var;
    HRESULT hres;

    jsobj = iface_to_jsdisp((IUnknown*)obj);
    if(jsobj) {
        if(is_class(jsobj, JSCLASS_ARRAY)) {
192
            hres = concat_array(array, (ArrayInstance*)jsobj, len, ei);
193 194 195 196 197 198 199 200
            jsdisp_release(jsobj);
            return hres;
        }
        jsdisp_release(jsobj);
    }

    V_VT(&var) = VT_DISPATCH;
    V_DISPATCH(&var) = obj;
201
    return jsdisp_propput_idx(array, (*len)++, &var, ei);
202 203
}

204
static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
205
        VARIANT *retv, jsexcept_t *ei)
206
{
207
    jsdisp_t *ret;
208 209 210 211 212
    DWORD len = 0;
    HRESULT hres;

    TRACE("\n");

213
    hres = create_array(ctx, 0, &ret);
214 215 216
    if(FAILED(hres))
        return hres;

217
    hres = concat_obj(ret, jsthis->u.disp, &len, ei);
218 219 220 221
    if(SUCCEEDED(hres)) {
        VARIANT *arg;
        DWORD i;

222 223
        for(i=0; i < argc; i++) {
            arg = argv+i;
224
            if(V_VT(arg) == VT_DISPATCH)
225
                hres = concat_obj(ret, V_DISPATCH(arg), &len, ei);
226
            else
227
                hres = jsdisp_propput_idx(ret, len++, arg, ei);
228 229 230 231 232 233 234 235
            if(FAILED(hres))
                break;
        }
    }

    if(FAILED(hres))
        return hres;

236 237 238
    if(retv)
        var_set_jsdisp(retv, ret);
    else
239 240
        jsdisp_release(ret);
    return S_OK;
241 242
}

243
static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep, VARIANT *retv, jsexcept_t *ei)
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
{
    BSTR *str_tab, ret = NULL;
    VARIANT var;
    DWORD i;
    HRESULT hres = E_FAIL;

    if(!length) {
        if(retv) {
            V_VT(retv) = VT_BSTR;
            V_BSTR(retv) = SysAllocStringLen(NULL, 0);
            if(!V_BSTR(retv))
                return E_OUTOFMEMORY;
        }
        return S_OK;
    }

    str_tab = heap_alloc_zero(length * sizeof(BSTR));
    if(!str_tab)
        return E_OUTOFMEMORY;

    for(i=0; i < length; i++) {
265
        hres = jsdisp_get_idx(array, i, &var, ei);
266 267 268 269
        if(hres == DISP_E_UNKNOWNNAME) {
            hres = S_OK;
            continue;
        } else if(FAILED(hres))
270 271 272
            break;

        if(V_VT(&var) != VT_EMPTY && V_VT(&var) != VT_NULL)
273
            hres = to_string(ctx, &var, ei, str_tab+i);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        VariantClear(&var);
        if(FAILED(hres))
            break;
    }

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

        seplen = strlenW(sep);

        if(str_tab[0])
            len = SysStringLen(str_tab[0]);
        for(i=1; i < length; i++)
            len += seplen + SysStringLen(str_tab[i]);

        ret = SysAllocStringLen(NULL, len);
        if(ret) {
            DWORD tmplen = 0;

            if(str_tab[0]) {
                tmplen = SysStringLen(str_tab[0]);
                memcpy(ret, str_tab[0], tmplen*sizeof(WCHAR));
            }

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

                if(str_tab[i]) {
                    tmplen = SysStringLen(str_tab[i]);
                    memcpy(ptr, str_tab[i], tmplen*sizeof(WCHAR));
                    ptr += tmplen;
                }
            }
            *ptr=0;
        }else {
            hres = E_OUTOFMEMORY;
        }
    }

    for(i=0; i < length; i++)
        SysFreeString(str_tab[i]);
    heap_free(str_tab);
    if(FAILED(hres))
        return hres;

    TRACE("= %s\n", debugstr_w(ret));

    if(retv) {
        if(!ret) {
            ret = SysAllocStringLen(NULL, 0);
            if(!ret)
                return E_OUTOFMEMORY;
        }

        V_VT(retv) = VT_BSTR;
        V_BSTR(retv) = ret;
    }else {
        SysFreeString(ret);
    }

    return S_OK;
}

/* ECMA-262 3rd Edition    15.4.4.5 */
343
static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
344
        VARIANT *retv, jsexcept_t *ei)
345
{
346
    jsdisp_t *jsthis;
347 348 349 350 351
    DWORD length;
    HRESULT hres;

    TRACE("\n");

352 353 354
    hres = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres))
        return hres;
355

356
    if(argc) {
357 358
        BSTR sep;

359
        hres = to_string(ctx, argv, ei, &sep);
360 361 362
        if(FAILED(hres))
            return hres;

363
        hres = array_join(ctx, jsthis, length, sep, retv, ei);
364 365 366

        SysFreeString(sep);
    }else {
367
        hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei);
368 369 370
    }

    return hres;
371 372
}

373
static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
374
        VARIANT *retv, jsexcept_t *ei)
375
{
376
    jsdisp_t *jsthis;
377 378 379 380 381 382
    VARIANT val;
    DWORD length;
    HRESULT hres;

    TRACE("\n");

383 384 385
    hres = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres))
        return hres;
386 387

    if(!length) {
388 389 390 391
        hres = set_length(jsthis, ei, 0);
        if(FAILED(hres))
            return hres;

392 393 394 395 396
        if(retv)
            V_VT(retv) = VT_EMPTY;
        return S_OK;
    }

397
    length--;
398
    hres = jsdisp_get_idx(jsthis, length, &val, ei);
399
    if(SUCCEEDED(hres)) {
400 401
        hres = jsdisp_delete_idx(jsthis, length);
    } else if(hres == DISP_E_UNKNOWNNAME) {
402 403
        V_VT(&val) = VT_EMPTY;
        hres = S_OK;
404
    } else
405 406
        return hres;

407 408
    if(SUCCEEDED(hres))
        hres = set_length(jsthis, ei, length);
409 410 411 412 413 414 415 416 417 418

    if(FAILED(hres)) {
        VariantClear(&val);
        return hres;
    }

    if(retv)
        *retv = val;
    else
        VariantClear(&val);
419

420
    return S_OK;
421 422
}

423
/* ECMA-262 3rd Edition    15.4.4.7 */
424
static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
425
        VARIANT *retv, jsexcept_t *ei)
426
{
427
    jsdisp_t *jsthis;
428
    DWORD length = 0;
429
    unsigned i;
430 431 432 433
    HRESULT hres;

    TRACE("\n");

434 435 436
    hres = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres))
        return hres;
437

438 439
    for(i=0; i < argc; i++) {
        hres = jsdisp_propput_idx(jsthis, length+i, argv+i, ei);
440 441 442 443
        if(FAILED(hres))
            return hres;
    }

444
    hres = set_length(jsthis, ei, length+argc);
445 446
    if(FAILED(hres))
        return hres;
447

448
    if(retv)
449
        num_set_int(retv, length+argc);
450
    return S_OK;
451 452
}

453
static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
454
        VARIANT *retv, jsexcept_t *ei)
455
{
456
    jsdisp_t *jsthis;
457 458 459 460 461 462 463 464 465 466 467 468 469
    DWORD length, k, l;
    VARIANT v1, v2;
    HRESULT hres1, hres2;

    TRACE("\n");

    hres1 = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres1))
        return hres1;

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

470
        hres1 = jsdisp_get_idx(jsthis, k, &v1, ei);
471
        if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
472 473
            return hres1;

474
        hres2 = jsdisp_get_idx(jsthis, l, &v2, ei);
475
        if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
476 477 478
            VariantClear(&v1);
            return hres2;
        }
479 480

        if(hres1 == DISP_E_UNKNOWNNAME)
481
            hres1 = jsdisp_delete_idx(jsthis, l);
482
        else
483
            hres1 = jsdisp_propput_idx(jsthis, l, &v1, ei);
484 485 486 487 488 489

        if(FAILED(hres1)) {
            VariantClear(&v1);
            VariantClear(&v2);
            return hres1;
        }
490 491

        if(hres2 == DISP_E_UNKNOWNNAME)
492
            hres2 = jsdisp_delete_idx(jsthis, k);
493
        else
494
            hres2 = jsdisp_propput_idx(jsthis, k, &v2, ei);
495 496 497 498 499

        if(FAILED(hres2)) {
            VariantClear(&v2);
            return hres2;
        }
500 501 502
    }

    if(retv) {
503
        jsdisp_addref(jsthis);
504
        var_set_jsdisp(retv, jsthis);
505 506 507
    }

    return S_OK;
508 509
}

510
/* ECMA-262 3rd Edition    15.4.4.9 */
511
static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
512
        VARIANT *retv, jsexcept_t *ei)
513
{
514
    jsdisp_t *jsthis;
515 516 517 518 519 520
    DWORD length = 0, i;
    VARIANT v, ret;
    HRESULT hres;

    TRACE("\n");

521 522 523 524 525 526
    hres = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres))
        return hres;

    if(!length) {
        hres = set_length(jsthis, ei, 0);
527 528 529 530 531 532 533 534 535 536
        if(FAILED(hres))
            return hres;
    }

    if(!length) {
        if(retv)
            V_VT(retv) = VT_EMPTY;
        return S_OK;
    }

537
    hres = jsdisp_get_idx(jsthis, 0, &ret, ei);
538 539 540 541 542 543
    if(hres == DISP_E_UNKNOWNNAME) {
        V_VT(&ret) = VT_EMPTY;
        hres = S_OK;
    }

    for(i=1; SUCCEEDED(hres) && i<length; i++) {
544
        hres = jsdisp_get_idx(jsthis, i, &v, ei);
545
        if(hres == DISP_E_UNKNOWNNAME)
546
            hres = jsdisp_delete_idx(jsthis, i-1);
547
        else if(SUCCEEDED(hres))
548
            hres = jsdisp_propput_idx(jsthis, i-1, &v, ei);
549 550 551
    }

    if(SUCCEEDED(hres)) {
552
        hres = jsdisp_delete_idx(jsthis, length-1);
553
        if(SUCCEEDED(hres))
554
            hres = set_length(jsthis, ei, length-1);
555 556 557 558 559 560 561
    }

    if(SUCCEEDED(hres) && retv)
        *retv = ret;
    else
        VariantClear(&ret);
    return hres;
562 563
}

564
/* ECMA-262 3rd Edition    15.4.4.10 */
565
static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
566
        VARIANT *retv, jsexcept_t *ei)
567
{
568
    jsdisp_t *arr, *jsthis;
569 570 571 572 573 574
    DOUBLE range;
    DWORD length, start, end, idx;
    HRESULT hres;

    TRACE("\n");

575 576 577
    hres = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres))
        return hres;
578

579 580
    if(argc) {
        hres = to_number(ctx, argv, ei, &range);
581 582 583
        if(FAILED(hres))
            return hres;

584
        range = floor(range);
585 586 587 588 589 590 591
        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;

592 593
    if(argc > 1) {
        hres = to_number(ctx, argv+1, ei, &range);
594 595 596
        if(FAILED(hres))
            return hres;

597
        range = floor(range);
598 599 600 601 602 603 604
        if(-range>length) end = 0;
        else if(range < 0) end = range+length;
        else if(range <= length) end = range;
        else end = length;
    }
    else end = length;

605
    hres = create_array(ctx, (end>start)?end-start:0, &arr);
606 607 608 609
    if(FAILED(hres))
        return hres;

    for(idx=start; idx<end; idx++) {
610 611
        VARIANT v;

612
        hres = jsdisp_get_idx(jsthis, idx, &v, ei);
613 614 615
        if(hres == DISP_E_UNKNOWNNAME)
            continue;

616
        if(SUCCEEDED(hres)) {
617
            hres = jsdisp_propput_idx(arr, idx-start, &v, ei);
618 619
            VariantClear(&v);
        }
620 621 622 623 624 625 626

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

627 628
    if(retv)
        var_set_jsdisp(retv, arr);
629 630 631 632
    else
        jsdisp_release(arr);

    return S_OK;
633 634
}

635
static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, VARIANT *v1, VARIANT *v2, jsexcept_t *ei, INT *cmp)
636 637 638 639 640
{
    HRESULT hres;

    if(cmp_func) {
        VARIANTARG args[2];
641
        double n;
642 643
        VARIANT res;

644 645
        args[0] = *v1;
        args[1] = *v2;
646

647
        hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res, ei);
648 649 650
        if(FAILED(hres))
            return hres;

651
        hres = to_number(ctx, &res, ei, &n);
652 653 654 655
        VariantClear(&res);
        if(FAILED(hres))
            return hres;

656 657 658
        if(n == 0)
            *cmp = 0;
        *cmp = n > 0.0 ? 1 : -1;
659 660 661 662 663 664 665 666
    }else if(V_VT(v1) == VT_EMPTY) {
        *cmp = V_VT(v2) == VT_EMPTY ? 0 : 1;
    }else if(V_VT(v2) == VT_EMPTY) {
        *cmp = -1;
    }else if(is_num_vt(V_VT(v1)) && is_num_vt(V_VT(v2))) {
        DOUBLE d = num_val(v1)-num_val(v2);
        if(d > 0.0)
            *cmp = 1;
667
        else
668
            *cmp = d < -0.0 ? -1 : 0;
669
    }else {
670 671 672 673 674 675 676 677 678 679 680 681 682 683
        BSTR x, y;

        hres = to_string(ctx, v1, ei, &x);
        if(FAILED(hres))
            return hres;

        hres = to_string(ctx, v2, ei, &y);
        if(SUCCEEDED(hres)) {
            *cmp = strcmpW(x, y);
            SysFreeString(y);
        }
        SysFreeString(x);
        if(FAILED(hres))
            return hres;
684 685 686 687 688 689
    }

    return S_OK;
}

/* ECMA-262 3rd Edition    15.4.4.11 */
690
static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
691
        VARIANT *retv, jsexcept_t *ei)
692
{
693
    jsdisp_t *jsthis, *cmp_func = NULL;
694 695 696 697 698 699 700
    VARIANT *vtab, **sorttab = NULL;
    DWORD length;
    DWORD i;
    HRESULT hres = S_OK;

    TRACE("\n");

701 702 703
    hres = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres))
        return hres;
704

705 706
    if(argc > 1) {
        WARN("invalid arg_cnt %d\n", argc);
707 708 709
        return E_FAIL;
    }

710 711
    if(argc == 1) {
        if(V_VT(argv) != VT_DISPATCH) {
712 713 714 715 716
            WARN("arg is not dispatch\n");
            return E_FAIL;
        }


717
        cmp_func = iface_to_jsdisp((IUnknown*)V_DISPATCH(argv));
718
        if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
719
            WARN("cmp_func is not a function\n");
720 721
            if(cmp_func)
                jsdisp_release(cmp_func);
722 723 724 725 726 727 728 729
            return E_FAIL;
        }
    }

    if(!length) {
        if(cmp_func)
            jsdisp_release(cmp_func);
        if(retv) {
730
            jsdisp_addref(jsthis);
731
            var_set_jsdisp(retv, jsthis);
732 733 734 735 736 737 738
        }
        return S_OK;
    }

    vtab = heap_alloc_zero(length * sizeof(VARIANT));
    if(vtab) {
        for(i=0; i<length; i++) {
739
            hres = jsdisp_get_idx(jsthis, i, vtab+i, ei);
740 741 742 743
            if(hres == DISP_E_UNKNOWNNAME) {
                V_VT(vtab+i) = VT_EMPTY;
                hres = S_OK;
            } else if(FAILED(hres)) {
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
                WARN("Could not get elem %d: %08x\n", i, hres);
                break;
            }
        }
    }else {
        hres = E_OUTOFMEMORY;
    }

    if(SUCCEEDED(hres)) {
        sorttab = heap_alloc(length*2*sizeof(VARIANT*));
        if(!sorttab)
            hres = E_OUTOFMEMORY;
    }

    /* merge-sort */
    if(SUCCEEDED(hres)) {
        VARIANT *tmpv, **tmpbuf;
        INT cmp;

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

        for(i=0; i < length/2; i++) {
768
            hres = sort_cmp(ctx, cmp_func, sorttab[2*i+1], sorttab[2*i], ei, &cmp);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
            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);

                    memcpy(tmpbuf, sorttab+i, k*sizeof(VARIANT*));

                    while(a < k && b < bend) {
793
                        hres = sort_cmp(ctx, cmp_func, tmpbuf[a], sorttab[i+k+b], ei, &cmp);
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
                        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)
                        memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(VARIANT*));
                }

                if(FAILED(hres))
                    break;
            }
        }

        for(i=0; SUCCEEDED(hres) && i < length; i++)
819
            hres = jsdisp_propput_idx(jsthis, i, sorttab[i], ei);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    }

    if(vtab) {
        for(i=0; i < length; i++)
            VariantClear(vtab+i);
        heap_free(vtab);
    }
    heap_free(sorttab);
    if(cmp_func)
        jsdisp_release(cmp_func);

    if(FAILED(hres))
        return hres;

    if(retv) {
835
        jsdisp_addref(jsthis);
836
        var_set_jsdisp(retv, jsthis);
837 838 839
    }

    return S_OK;
840 841
}

842
/* ECMA-262 3rd Edition    15.4.4.12 */
843
static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
844
        VARIANT *retv, jsexcept_t *ei)
845
{
846
    DWORD length, start=0, delete_cnt=0, i, add_args = 0;
847
    jsdisp_t *ret_array = NULL, *jsthis;
848
    VARIANT v;
849 850
    double d;
    int n;
851 852 853 854
    HRESULT hres = S_OK;

    TRACE("\n");

855 856 857
    hres = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres))
        return hres;
858

859 860
    if(argc) {
        hres = to_integer(ctx, argv, ei, &d);
861 862 863
        if(FAILED(hres))
            return hres;

864 865 866
        if(is_int32(d)) {
            if((n = d) >= 0)
                start = min(n, length);
867
            else
868
                start = -n > length ? 0 : length + n;
869
        }else {
870
            start = d < 0.0 ? 0 : length;
871 872 873 874
        }
    }

    if(argc >= 2) {
875
        hres = to_integer(ctx, argv+1, ei, &d);
876 877 878
        if(FAILED(hres))
            return hres;

879 880 881 882
        if(is_int32(d)) {
            if((n = d) > 0)
                delete_cnt = min(n, length-start);
        }else if(d > 0.0) {
883 884 885 886 887 888 889
            delete_cnt = length-start;
        }

        add_args = argc-2;
    }

    if(retv) {
890
        hres = create_array(ctx, 0, &ret_array);
891 892 893 894
        if(FAILED(hres))
            return hres;

        for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
895
            hres = jsdisp_get_idx(jsthis, start+i, &v, ei);
896 897 898
            if(hres == DISP_E_UNKNOWNNAME)
                hres = S_OK;
            else if(SUCCEEDED(hres))
899
                hres = jsdisp_propput_idx(ret_array, i, &v, ei);
900 901 902
        }

        if(SUCCEEDED(hres)) {
903
            num_set_int(&v, delete_cnt);
904
            hres = jsdisp_propput_name(ret_array, lengthW, &v, ei);
905 906 907 908 909
        }
    }

    if(add_args < delete_cnt) {
        for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
910
            hres = jsdisp_get_idx(jsthis, i+delete_cnt, &v, ei);
911
            if(hres == DISP_E_UNKNOWNNAME)
912
                hres = jsdisp_delete_idx(jsthis, i+add_args);
913
            else if(SUCCEEDED(hres))
914
                hres = jsdisp_propput_idx(jsthis, i+add_args, &v, ei);
915 916 917
        }

        for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
918
            hres = jsdisp_delete_idx(jsthis, i-1);
919 920
    }else if(add_args > delete_cnt) {
        for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
921
            hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &v, ei);
922
            if(hres == DISP_E_UNKNOWNNAME)
923
                hres = jsdisp_delete_idx(jsthis, i+add_args-1);
924
            else if(SUCCEEDED(hres))
925
                hres = jsdisp_propput_idx(jsthis, i+add_args-1, &v, ei);
926 927 928 929
        }
    }

    for(i=0; SUCCEEDED(hres) && i < add_args; i++)
930
        hres = jsdisp_propput_idx(jsthis, start+i, argv+i+2, ei);
931 932

    if(SUCCEEDED(hres)) {
933
        num_set_int(&v, length-delete_cnt+add_args);
934
        hres = jsdisp_propput_name(jsthis, lengthW, &v, ei);
935 936 937 938 939 940 941 942
    }

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

943 944
    if(retv)
        var_set_jsdisp(retv, ret_array);
945
    return S_OK;
946 947
}

948
/* ECMA-262 3rd Edition    15.4.4.2 */
949
static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
950
        VARIANT *retv, jsexcept_t *ei)
951
{
952 953
    ArrayInstance *array;

954 955
    TRACE("\n");

956
    array = array_this(jsthis);
957
    if(!array)
958
        return throw_type_error(ctx, ei, JS_E_ARRAY_EXPECTED, NULL);
959

960
    return array_join(ctx, &array->dispex, array->length, default_separatorW, retv, ei);
961 962
}

963
static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
964
        VARIANT *retv, jsexcept_t *ei)
965 966 967 968 969
{
    FIXME("\n");
    return E_NOTIMPL;
}

970
/* ECMA-262 3rd Edition    15.4.4.13 */
971
static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
972
        VARIANT *retv, jsexcept_t *ei)
973
{
974
    jsdisp_t *jsthis;
975
    WCHAR buf[14], *buf_end, *str;
976
    DWORD i, length;
977 978 979 980 981 982
    VARIANT var;
    DISPID id;
    HRESULT hres;

    TRACE("\n");

983 984 985
    hres = get_length(ctx, vthis, ei, &jsthis, &length);
    if(FAILED(hres))
        return hres;
986

987 988 989 990
    if(argc) {
        buf_end = buf + sizeof(buf)/sizeof(WCHAR)-1;
        *buf_end-- = 0;
        i = length;
991

992 993
        while(i--) {
            str = idx_to_str(i, buf_end);
994

995 996
            hres = jsdisp_get_id(jsthis, str, 0, &id);
            if(SUCCEEDED(hres)) {
997
                hres = jsdisp_propget(jsthis, id, &var, ei);
998 999
                if(FAILED(hres))
                    return hres;
1000

1001
                hres = jsdisp_propput_idx(jsthis, i+argc, &var, ei);
1002 1003 1004 1005
                VariantClear(&var);
            }else if(hres == DISP_E_UNKNOWNNAME) {
                hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
            }
1006 1007 1008 1009 1010 1011 1012
        }

        if(FAILED(hres))
            return hres;
    }

    for(i=0; i<argc; i++) {
1013
        hres = jsdisp_propput_idx(jsthis, i, argv+i, ei);
1014 1015 1016 1017
        if(FAILED(hres))
            return hres;
    }

1018 1019 1020 1021 1022 1023
    if(argc) {
        length += argc;
        hres = set_length(jsthis, ei, length);
        if(FAILED(hres))
            return hres;
    }
1024

1025
    if(retv) {
1026
        if(ctx->version < 2)
1027
            V_VT(retv) = VT_EMPTY;
1028 1029
        else
            num_set_int(retv, length);
1030
    }
1031
    return S_OK;
1032 1033
}

1034
static HRESULT Array_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, VARIANT *argv,
1035
        VARIANT *retv, jsexcept_t *ei)
1036
{
1037 1038 1039
    TRACE("\n");

    switch(flags) {
1040
    case INVOKE_FUNC:
1041
        return throw_type_error(ctx, ei, JS_E_FUNCTION_EXPECTED, NULL);
1042
    case INVOKE_PROPERTYGET:
1043
        return array_join(ctx, jsthis->u.jsdisp, array_from_vdisp(jsthis)->length, default_separatorW, retv, ei);
1044 1045 1046 1047 1048 1049
    default:
        FIXME("unimplemented flags %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
1050 1051
}

1052
static void Array_destructor(jsdisp_t *dispex)
1053 1054 1055 1056
{
    heap_free(dispex);
}

1057
static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1058
{
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    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;
1076 1077 1078
}

static const builtin_prop_t Array_props[] = {
1079 1080
    {concatW,                Array_concat,               PROPF_METHOD|1},
    {joinW,                  Array_join,                 PROPF_METHOD|1},
1081 1082
    {lengthW,                Array_length,               0},
    {popW,                   Array_pop,                  PROPF_METHOD},
1083
    {pushW,                  Array_push,                 PROPF_METHOD|1},
1084 1085
    {reverseW,               Array_reverse,              PROPF_METHOD},
    {shiftW,                 Array_shift,                PROPF_METHOD},
1086 1087 1088
    {sliceW,                 Array_slice,                PROPF_METHOD|2},
    {sortW,                  Array_sort,                 PROPF_METHOD|1},
    {spliceW,                Array_splice,               PROPF_METHOD|2},
1089 1090
    {toLocaleStringW,        Array_toLocaleString,       PROPF_METHOD},
    {toStringW,              Array_toString,             PROPF_METHOD},
1091
    {unshiftW,               Array_unshift,              PROPF_METHOD|1},
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
};

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
};

1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
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
};

1116
static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, VARIANT *argv,
1117
        VARIANT *retv, jsexcept_t *ei)
1118
{
1119
    jsdisp_t *obj;
1120 1121 1122 1123 1124 1125
    DWORD i;
    HRESULT hres;

    TRACE("\n");

    switch(flags) {
1126
    case DISPATCH_METHOD:
1127
    case DISPATCH_CONSTRUCT: {
1128 1129
        if(argc == 1 && V_VT(argv) == VT_I4) {
            if(V_I4(argv) < 0)
1130
                return throw_range_error(ctx, ei, JS_E_INVALID_LENGTH, NULL);
1131

1132
            hres = create_array(ctx, V_I4(argv), &obj);
1133 1134 1135
            if(FAILED(hres))
                return hres;

1136
            var_set_jsdisp(retv, obj);
1137 1138 1139
            return S_OK;
        }

1140
        hres = create_array(ctx, argc, &obj);
1141 1142 1143
        if(FAILED(hres))
            return hres;

1144 1145
        for(i=0; i < argc; i++) {
            hres = jsdisp_propput_idx(obj, i, argv+i, ei);
1146 1147 1148 1149 1150 1151 1152 1153
            if(FAILED(hres))
                break;
        }
        if(FAILED(hres)) {
            jsdisp_release(obj);
            return hres;
        }

1154
        var_set_jsdisp(retv, obj);
1155 1156 1157 1158 1159 1160 1161 1162
        break;
    }
    default:
        FIXME("unimplemented flags: %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
1163 1164
}

1165
static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1166
{
1167
    ArrayInstance *array;
1168 1169
    HRESULT hres;

1170 1171 1172 1173
    array = heap_alloc_zero(sizeof(ArrayInstance));
    if(!array)
        return E_OUTOFMEMORY;

1174 1175
    if(object_prototype)
        hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1176
    else
1177
        hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187

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

    *ret = array;
    return S_OK;
}

1188
HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1189 1190 1191 1192
{
    ArrayInstance *array;
    HRESULT hres;

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

1195
    hres = alloc_array(ctx, object_prototype, &array);
1196 1197 1198
    if(FAILED(hres))
        return hres;

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

Jacek Caban's avatar
Jacek Caban committed
1201
    jsdisp_release(&array->dispex);
1202 1203
    return hres;
}
1204

1205
HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1206 1207 1208 1209
{
    ArrayInstance *array;
    HRESULT hres;

1210
    hres = alloc_array(ctx, NULL, &array);
1211 1212 1213 1214 1215 1216 1217 1218
    if(FAILED(hres))
        return hres;

    array->length = length;

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