array.c 35.9 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
#include <math.h>
21
#include <assert.h>
22

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

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(jscript);

typedef struct {
30
    jsdisp_t dispex;
31 32

    DWORD length;
33 34
} ArrayInstance;

35 36 37 38 39
static inline ArrayInstance *array_from_jsdisp(jsdisp_t *jsdisp)
{
    return CONTAINING_RECORD(jsdisp, ArrayInstance, dispex);
}

40
static inline ArrayInstance *array_from_vdisp(vdisp_t *vdisp)
41
{
42
    return array_from_jsdisp(vdisp->u.jsdisp);
43 44 45 46 47 48 49
}

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

50 51 52 53 54 55
unsigned array_get_length(jsdisp_t *array)
{
    assert(is_class(array, JSCLASS_ARRAY));
    return array_from_jsdisp(array)->length;
}

56
static HRESULT get_length(script_ctx_t *ctx, vdisp_t *vdisp, jsdisp_t **jsthis, DWORD *ret)
57 58
{
    ArrayInstance *array;
59
    jsval_t val;
60 61
    HRESULT hres;

62 63 64 65 66 67 68
    array = array_this(vdisp);
    if(array) {
        *jsthis = &array->dispex;
        *ret = array->length;
        return S_OK;
    }

69
    if(!is_jsdisp(vdisp))
70
        return JS_E_JSCRIPT_EXPECTED;
71

72
    hres = jsdisp_propget_name(vdisp->u.jsdisp, L"length", &val);
73 74 75
    if(FAILED(hres))
        return hres;

76
    hres = to_uint32(ctx, val, ret);
77
    jsval_release(val);
78 79 80 81 82
    if(FAILED(hres))
        return hres;

    *jsthis = vdisp->u.jsdisp;
    return S_OK;
83 84
}

85
static HRESULT set_length(jsdisp_t *obj, DWORD length)
86
{
87
    if(is_class(obj, JSCLASS_ARRAY)) {
88
        array_from_jsdisp(obj)->length = length;
89 90 91
        return S_OK;
    }

92
    return jsdisp_propput_name(obj, L"length", jsval_number(length));
93 94
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
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;
}

110
static HRESULT Array_get_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
111
{
112
    TRACE("%p\n", jsthis);
113

114
    *r = jsval_number(array_from_jsdisp(jsthis)->length);
115 116
    return S_OK;
}
117

118
static HRESULT Array_set_length(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t value)
119
{
120
    ArrayInstance *This = array_from_jsdisp(jsthis);
121 122 123
    DOUBLE len = -1;
    DWORD i;
    HRESULT hres;
124

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

127 128 129
    hres = to_number(ctx, value, &len);
    if(FAILED(hres))
        return hres;
130

131 132
    len = floor(len);
    if(len!=(DWORD)len)
133
        return JS_E_INVALID_LENGTH;
134 135 136 137 138

    for(i=len; i < This->length; i++) {
        hres = jsdisp_delete_idx(&This->dispex, i);
        if(FAILED(hres))
            return hres;
139 140
    }

141
    This->length = len;
142
    return S_OK;
143 144
}

145
static HRESULT concat_array(jsdisp_t *array, ArrayInstance *obj, DWORD *len)
146
{
147
    jsval_t val;
148 149 150 151
    DWORD i;
    HRESULT hres;

    for(i=0; i < obj->length; i++) {
152
        hres = jsdisp_get_idx(&obj->dispex, i, &val);
153 154 155 156 157
        if(hres == DISP_E_UNKNOWNNAME)
            continue;
        if(FAILED(hres))
            return hres;

158
        hres = jsdisp_propput_idx(array, *len+i, val);
159
        jsval_release(val);
160 161 162 163 164 165 166 167
        if(FAILED(hres))
            return hres;
    }

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

168
static HRESULT concat_obj(jsdisp_t *array, IDispatch *obj, DWORD *len)
169
{
170
    jsdisp_t *jsobj;
171 172
    HRESULT hres;

173
    jsobj = iface_to_jsdisp(obj);
174 175
    if(jsobj) {
        if(is_class(jsobj, JSCLASS_ARRAY)) {
176
            hres = concat_array(array, array_from_jsdisp(jsobj), len);
177 178 179 180 181 182
            jsdisp_release(jsobj);
            return hres;
        }
        jsdisp_release(jsobj);
    }

183
    return jsdisp_propput_idx(array, (*len)++, jsval_disp(obj));
184 185
}

186
static HRESULT Array_concat(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
187
        jsval_t *r)
188
{
189
    jsdisp_t *ret;
190 191 192 193 194
    DWORD len = 0;
    HRESULT hres;

    TRACE("\n");

195
    hres = create_array(ctx, 0, &ret);
196 197 198
    if(FAILED(hres))
        return hres;

199
    hres = concat_obj(ret, jsthis->u.disp, &len);
200 201 202
    if(SUCCEEDED(hres)) {
        DWORD i;

203
        for(i=0; i < argc; i++) {
204
            if(is_object_instance(argv[i]))
205
                hres = concat_obj(ret, get_object(argv[i]), &len);
206
            else
207
                hres = jsdisp_propput_idx(ret, len++, argv[i]);
208 209 210 211 212 213 214 215
            if(FAILED(hres))
                break;
        }
    }

    if(FAILED(hres))
        return hres;

216 217
    if(r)
        *r = jsval_obj(ret);
218
    else
219 220
        jsdisp_release(ret);
    return S_OK;
221 222
}

223 224
static HRESULT array_join(script_ctx_t *ctx, jsdisp_t *array, DWORD length, const WCHAR *sep,
        unsigned seplen, jsval_t *r)
225
{
226
    jsstr_t **str_tab, *ret = NULL;
227
    jsval_t val;
228 229 230 231
    DWORD i;
    HRESULT hres = E_FAIL;

    if(!length) {
232 233
        if(r)
            *r = jsval_string(jsstr_empty());
234 235 236
        return S_OK;
    }

237
    str_tab = heap_alloc_zero(length * sizeof(*str_tab));
238 239 240 241
    if(!str_tab)
        return E_OUTOFMEMORY;

    for(i=0; i < length; i++) {
242
        hres = jsdisp_get_idx(array, i, &val);
243 244 245 246
        if(hres == DISP_E_UNKNOWNNAME) {
            hres = S_OK;
            continue;
        } else if(FAILED(hres))
247 248
            break;

249
        if(!is_undefined(val) && !is_null(val)) {
250
            hres = to_string(ctx, val, str_tab+i);
251 252 253 254
            jsval_release(val);
            if(FAILED(hres))
                break;
        }
255 256 257
    }

    if(SUCCEEDED(hres)) {
258
        DWORD len = 0;
259 260

        if(str_tab[0])
261 262 263 264 265 266 267 268 269 270
            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;
            }
        }
271

272 273
        if(SUCCEEDED(hres)) {
            WCHAR *ptr = NULL;
274

275 276
            ret = jsstr_alloc_buf(len, &ptr);
            if(ret) {
277 278
                if(str_tab[0])
                    ptr += jsstr_flush(str_tab[0], ptr);
279

280 281 282 283 284
                for(i=1; i < length; i++) {
                    if(seplen) {
                        memcpy(ptr, sep, seplen*sizeof(WCHAR));
                        ptr += seplen;
                    }
285

286 287 288 289 290
                    if(str_tab[i])
                        ptr += jsstr_flush(str_tab[i], ptr);
                }
            }else {
                hres = E_OUTOFMEMORY;
291 292 293 294
            }
        }
    }

295 296 297 298
    for(i=0; i < length; i++) {
        if(str_tab[i])
            jsstr_release(str_tab[i]);
    }
299 300 301 302
    heap_free(str_tab);
    if(FAILED(hres))
        return hres;

303
    TRACE("= %s\n", debugstr_jsstr(ret));
304

305
    if(r)
306
        *r = jsval_string(ret);
307 308
    else
        jsstr_release(ret);
309 310 311 312
    return S_OK;
}

/* ECMA-262 3rd Edition    15.4.4.5 */
313
static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
314
        jsval_t *r)
315
{
316
    jsdisp_t *jsthis;
317 318 319 320 321
    DWORD length;
    HRESULT hres;

    TRACE("\n");

322
    hres = get_length(ctx, vthis, &jsthis, &length);
323 324
    if(FAILED(hres))
        return hres;
325

326
    if(argc) {
327 328
        const WCHAR *sep;
        jsstr_t *sep_str;
329

330
        hres = to_flat_string(ctx, argv[0], &sep_str, &sep);
331 332 333
        if(FAILED(hres))
            return hres;

334
        hres = array_join(ctx, jsthis, length, sep, jsstr_length(sep_str), r);
335

336
        jsstr_release(sep_str);
337
    }else {
338
        hres = array_join(ctx, jsthis, length, L",", 1, r);
339 340 341
    }

    return hres;
342 343
}

344
static HRESULT Array_pop(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
345
        jsval_t *r)
346
{
347
    jsdisp_t *jsthis;
348
    jsval_t val;
349 350 351 352 353
    DWORD length;
    HRESULT hres;

    TRACE("\n");

354
    hres = get_length(ctx, vthis, &jsthis, &length);
355 356
    if(FAILED(hres))
        return hres;
357 358

    if(!length) {
359
        hres = set_length(jsthis, 0);
360 361 362
        if(FAILED(hres))
            return hres;

363 364
        if(r)
            *r = jsval_undefined();
365 366 367
        return S_OK;
    }

368
    length--;
369
    hres = jsdisp_get_idx(jsthis, length, &val);
370
    if(SUCCEEDED(hres))
371
        hres = jsdisp_delete_idx(jsthis, length);
372
    else if(hres == DISP_E_UNKNOWNNAME) {
373
        val = jsval_undefined();
374 375
        hres = S_OK;
    }else
376 377
        return hres;

378
    if(SUCCEEDED(hres))
379
        hres = set_length(jsthis, length);
380 381

    if(FAILED(hres)) {
382
        jsval_release(val);
383 384 385
        return hres;
    }

386
    if(r)
387 388 389
        *r = val;
    else
        jsval_release(val);
390
    return hres;
391 392
}

393
/* ECMA-262 3rd Edition    15.4.4.7 */
394
static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
395
        jsval_t *r)
396
{
397
    jsdisp_t *jsthis;
398
    DWORD length = 0;
399
    unsigned i;
400 401 402 403
    HRESULT hres;

    TRACE("\n");

404
    hres = get_length(ctx, vthis, &jsthis, &length);
405 406
    if(FAILED(hres))
        return hres;
407

408
    for(i=0; i < argc; i++) {
409
        hres = jsdisp_propput_idx(jsthis, length+i, argv[i]);
410 411 412 413
        if(FAILED(hres))
            return hres;
    }

414
    hres = set_length(jsthis, length+argc);
415 416
    if(FAILED(hres))
        return hres;
417

418 419
    if(r)
        *r = jsval_number(length+argc);
420
    return S_OK;
421 422
}

423
static HRESULT Array_reverse(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
424
        jsval_t *r)
425
{
426
    jsdisp_t *jsthis;
427
    DWORD length, k, l;
428
    jsval_t v1, v2;
429 430 431 432
    HRESULT hres1, hres2;

    TRACE("\n");

433
    hres1 = get_length(ctx, vthis, &jsthis, &length);
434 435 436 437 438 439
    if(FAILED(hres1))
        return hres1;

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

440
        hres1 = jsdisp_get_idx(jsthis, k, &v1);
441
        if(FAILED(hres1) && hres1!=DISP_E_UNKNOWNNAME)
442 443
            return hres1;

444
        hres2 = jsdisp_get_idx(jsthis, l, &v2);
445
        if(FAILED(hres2) && hres2!=DISP_E_UNKNOWNNAME) {
446
            jsval_release(v1);
447 448
            return hres2;
        }
449 450

        if(hres1 == DISP_E_UNKNOWNNAME)
451
            hres1 = jsdisp_delete_idx(jsthis, l);
452
        else
453
            hres1 = jsdisp_propput_idx(jsthis, l, v1);
454 455

        if(FAILED(hres1)) {
456 457
            jsval_release(v1);
            jsval_release(v2);
458 459
            return hres1;
        }
460 461

        if(hres2 == DISP_E_UNKNOWNNAME)
462
            hres2 = jsdisp_delete_idx(jsthis, k);
463
        else
464
            hres2 = jsdisp_propput_idx(jsthis, k, v2);
465 466

        if(FAILED(hres2)) {
467
            jsval_release(v2);
468 469
            return hres2;
        }
470 471
    }

472 473
    if(r)
        *r = jsval_obj(jsdisp_addref(jsthis));
474
    return S_OK;
475 476
}

477
/* ECMA-262 3rd Edition    15.4.4.9 */
478
static HRESULT Array_shift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
479
        jsval_t *r)
480
{
481
    jsdisp_t *jsthis;
482
    DWORD length = 0, i;
483
    jsval_t v, ret;
484 485 486 487
    HRESULT hres;

    TRACE("\n");

488
    hres = get_length(ctx, vthis, &jsthis, &length);
489 490 491 492
    if(FAILED(hres))
        return hres;

    if(!length) {
493
        hres = set_length(jsthis, 0);
494 495 496
        if(FAILED(hres))
            return hres;

497 498
        if(r)
            *r = jsval_undefined();
499 500 501
        return S_OK;
    }

502
    hres = jsdisp_get_idx(jsthis, 0, &ret);
503
    if(hres == DISP_E_UNKNOWNNAME) {
504
        ret = jsval_undefined();
505 506 507 508
        hres = S_OK;
    }

    for(i=1; SUCCEEDED(hres) && i<length; i++) {
509
        hres = jsdisp_get_idx(jsthis, i, &v);
510
        if(hres == DISP_E_UNKNOWNNAME)
511
            hres = jsdisp_delete_idx(jsthis, i-1);
512
        else if(SUCCEEDED(hres))
513
            hres = jsdisp_propput_idx(jsthis, i-1, v);
514 515 516
    }

    if(SUCCEEDED(hres)) {
517
        hres = jsdisp_delete_idx(jsthis, length-1);
518
        if(SUCCEEDED(hres))
519
            hres = set_length(jsthis, length-1);
520 521
    }

522 523 524 525 526 527 528
    if(FAILED(hres))
        return hres;

    if(r)
        *r = ret;
    else
        jsval_release(ret);
529
    return hres;
530 531
}

532
/* ECMA-262 3rd Edition    15.4.4.10 */
533
static HRESULT Array_slice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
534
{
535
    jsdisp_t *arr, *jsthis;
536 537 538 539 540 541
    DOUBLE range;
    DWORD length, start, end, idx;
    HRESULT hres;

    TRACE("\n");

542
    hres = get_length(ctx, vthis, &jsthis, &length);
543 544
    if(FAILED(hres))
        return hres;
545

546
    if(argc) {
547
        hres = to_number(ctx, argv[0], &range);
548 549 550
        if(FAILED(hres))
            return hres;

551
        range = floor(range);
552 553 554 555 556 557 558
        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;

559
    if(argc > 1) {
560
        hres = to_number(ctx, argv[1], &range);
561 562 563
        if(FAILED(hres))
            return hres;

564
        range = floor(range);
565 566 567 568 569 570 571
        if(-range>length) end = 0;
        else if(range < 0) end = range+length;
        else if(range <= length) end = range;
        else end = length;
    }
    else end = length;

572
    hres = create_array(ctx, (end>start)?end-start:0, &arr);
573 574 575 576
    if(FAILED(hres))
        return hres;

    for(idx=start; idx<end; idx++) {
577
        jsval_t v;
578

579
        hres = jsdisp_get_idx(jsthis, idx, &v);
580 581 582
        if(hres == DISP_E_UNKNOWNNAME)
            continue;

583
        if(SUCCEEDED(hres)) {
584
            hres = jsdisp_propput_idx(arr, idx-start, v);
585
            jsval_release(v);
586
        }
587 588 589 590 591 592 593

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

594 595
    if(r)
        *r = jsval_obj(arr);
596 597 598 599
    else
        jsdisp_release(arr);

    return S_OK;
600 601
}

602
static HRESULT sort_cmp(script_ctx_t *ctx, jsdisp_t *cmp_func, jsval_t v1, jsval_t v2, INT *cmp)
603 604 605 606
{
    HRESULT hres;

    if(cmp_func) {
607
        jsval_t args[2] = {v1, v2};
608
        jsval_t res;
609
        double n;
610

611
        hres = jsdisp_call_value(cmp_func, NULL, DISPATCH_METHOD, 2, args, &res);
612 613 614
        if(FAILED(hres))
            return hres;

615
        hres = to_number(ctx, res, &n);
616
        jsval_release(res);
617 618 619
        if(FAILED(hres))
            return hres;

620 621 622
        if(n == 0)
            *cmp = 0;
        *cmp = n > 0.0 ? 1 : -1;
623 624 625
    }else if(is_undefined(v1)) {
        *cmp = is_undefined(v2) ? 0 : 1;
    }else if(is_undefined(v2)) {
626
        *cmp = -1;
627 628
    }else if(is_number(v1) && is_number(v2)) {
        double d = get_number(v1)-get_number(v2);
629 630
        if(d > 0.0)
            *cmp = 1;
631
        else
632
            *cmp = d < -0.0 ? -1 : 0;
633
    }else {
634
        jsstr_t *x, *y;
635

636
        hres = to_string(ctx, v1, &x);
637 638 639
        if(FAILED(hres))
            return hres;

640
        hres = to_string(ctx, v2, &y);
641
        if(SUCCEEDED(hres)) {
642 643
            *cmp = jsstr_cmp(x, y);
            jsstr_release(y);
644
        }
645
        jsstr_release(x);
646 647
        if(FAILED(hres))
            return hres;
648 649 650 651 652 653
    }

    return S_OK;
}

/* ECMA-262 3rd Edition    15.4.4.11 */
654
static HRESULT Array_sort(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
655
        jsval_t *r)
656
{
657
    jsdisp_t *jsthis, *cmp_func = NULL;
658
    jsval_t *vtab, **sorttab = NULL;
659 660 661 662 663 664
    DWORD length;
    DWORD i;
    HRESULT hres = S_OK;

    TRACE("\n");

665
    hres = get_length(ctx, vthis, &jsthis, &length);
666 667
    if(FAILED(hres))
        return hres;
668

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    if(argc >= 1) {
        if(is_object_instance(argv[0])) {
            if(argc > 1 && ctx->version < SCRIPTLANGUAGEVERSION_ES5) {
                WARN("invalid arg_cnt %d\n", argc);
                return JS_E_JSCRIPT_EXPECTED;
            }
            cmp_func = iface_to_jsdisp(get_object(argv[0]));
            if(!cmp_func || !is_class(cmp_func, JSCLASS_FUNCTION)) {
                WARN("cmp_func is not a function\n");
                if(cmp_func)
                    jsdisp_release(cmp_func);
                return JS_E_JSCRIPT_EXPECTED;
            }
        }else if(ctx->version >= SCRIPTLANGUAGEVERSION_ES5 ? !is_undefined(argv[0]) : !is_null(argv[0])) {
            WARN("invalid arg %s\n", debugstr_jsval(argv[0]));
            return JS_E_JSCRIPT_EXPECTED;
685 686 687 688 689 690
        }
    }

    if(!length) {
        if(cmp_func)
            jsdisp_release(cmp_func);
691 692
        if(r)
            *r = jsval_obj(jsdisp_addref(jsthis));
693 694 695
        return S_OK;
    }

696
    vtab = heap_alloc_zero(length * sizeof(*vtab));
697 698
    if(vtab) {
        for(i=0; i<length; i++) {
699
            hres = jsdisp_get_idx(jsthis, i, vtab+i);
700
            if(hres == DISP_E_UNKNOWNNAME) {
701
                vtab[i] = jsval_undefined();
702 703
                hres = S_OK;
            } else if(FAILED(hres)) {
704 705 706 707 708 709 710 711 712
                WARN("Could not get elem %d: %08x\n", i, hres);
                break;
            }
        }
    }else {
        hres = E_OUTOFMEMORY;
    }

    if(SUCCEEDED(hres)) {
713
        sorttab = heap_alloc(length*2*sizeof(*sorttab));
714 715 716 717 718 719
        if(!sorttab)
            hres = E_OUTOFMEMORY;
    }

    /* merge-sort */
    if(SUCCEEDED(hres)) {
720
        jsval_t *tmpv, **tmpbuf;
721 722 723 724 725 726 727
        INT cmp;

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

        for(i=0; i < length/2; i++) {
728
            hres = sort_cmp(ctx, cmp_func, *sorttab[2*i+1], *sorttab[2*i], &cmp);
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
            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);

750
                    memcpy(tmpbuf, sorttab+i, k*sizeof(jsval_t*));
751 752

                    while(a < k && b < bend) {
753
                        hres = sort_cmp(ctx, cmp_func, *tmpbuf[a], *sorttab[i+k+b], &cmp);
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
                        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)
770
                        memcpy(sorttab+i+a+b, tmpbuf+a, (k-a)*sizeof(jsval_t*));
771 772 773 774 775 776 777 778
                }

                if(FAILED(hres))
                    break;
            }
        }

        for(i=0; SUCCEEDED(hres) && i < length; i++)
779
            hres = jsdisp_propput_idx(jsthis, i, *sorttab[i]);
780 781 782 783
    }

    if(vtab) {
        for(i=0; i < length; i++)
784
            jsval_release(vtab[i]);
785 786 787 788 789 790 791 792 793
        heap_free(vtab);
    }
    heap_free(sorttab);
    if(cmp_func)
        jsdisp_release(cmp_func);

    if(FAILED(hres))
        return hres;

794 795
    if(r)
        *r = jsval_obj(jsdisp_addref(jsthis));
796
    return S_OK;
797 798
}

799
/* ECMA-262 3rd Edition    15.4.4.12 */
800
static HRESULT Array_splice(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
801
        jsval_t *r)
802
{
803
    DWORD length, start=0, delete_cnt=0, i, add_args = 0;
804
    jsdisp_t *ret_array = NULL, *jsthis;
805
    jsval_t val;
806 807
    double d;
    int n;
808 809 810 811
    HRESULT hres = S_OK;

    TRACE("\n");

812
    hres = get_length(ctx, vthis, &jsthis, &length);
813 814
    if(FAILED(hres))
        return hres;
815

816
    if(argc) {
817
        hres = to_integer(ctx, argv[0], &d);
818 819 820
        if(FAILED(hres))
            return hres;

821 822 823
        if(is_int32(d)) {
            if((n = d) >= 0)
                start = min(n, length);
824
            else
825
                start = -n > length ? 0 : length + n;
826
        }else {
827
            start = d < 0.0 ? 0 : length;
828 829 830 831
        }
    }

    if(argc >= 2) {
832
        hres = to_integer(ctx, argv[1], &d);
833 834 835
        if(FAILED(hres))
            return hres;

836 837 838 839
        if(is_int32(d)) {
            if((n = d) > 0)
                delete_cnt = min(n, length-start);
        }else if(d > 0.0) {
840 841 842 843 844 845
            delete_cnt = length-start;
        }

        add_args = argc-2;
    }

846
    if(r) {
847
        hres = create_array(ctx, 0, &ret_array);
848 849 850 851
        if(FAILED(hres))
            return hres;

        for(i=0; SUCCEEDED(hres) && i < delete_cnt; i++) {
852
            hres = jsdisp_get_idx(jsthis, start+i, &val);
853
            if(hres == DISP_E_UNKNOWNNAME) {
854
                hres = S_OK;
855
            }else if(SUCCEEDED(hres)) {
856
                hres = jsdisp_propput_idx(ret_array, i, val);
857 858
                jsval_release(val);
            }
859 860
        }

861
        if(SUCCEEDED(hres))
862
            hres = jsdisp_propput_name(ret_array, L"length", jsval_number(delete_cnt));
863 864 865 866
    }

    if(add_args < delete_cnt) {
        for(i = start; SUCCEEDED(hres) && i < length-delete_cnt; i++) {
867
            hres = jsdisp_get_idx(jsthis, i+delete_cnt, &val);
868
            if(hres == DISP_E_UNKNOWNNAME) {
869
                hres = jsdisp_delete_idx(jsthis, i+add_args);
870
            }else if(SUCCEEDED(hres)) {
871
                hres = jsdisp_propput_idx(jsthis, i+add_args, val);
872 873
                jsval_release(val);
            }
874 875 876
        }

        for(i=length; SUCCEEDED(hres) && i != length-delete_cnt+add_args; i--)
877
            hres = jsdisp_delete_idx(jsthis, i-1);
878 879
    }else if(add_args > delete_cnt) {
        for(i=length-delete_cnt; SUCCEEDED(hres) && i != start; i--) {
880
            hres = jsdisp_get_idx(jsthis, i+delete_cnt-1, &val);
881
            if(hres == DISP_E_UNKNOWNNAME) {
882
                hres = jsdisp_delete_idx(jsthis, i+add_args-1);
883
            }else if(SUCCEEDED(hres)) {
884
                hres = jsdisp_propput_idx(jsthis, i+add_args-1, val);
885 886
                jsval_release(val);
            }
887 888 889 890
        }
    }

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

893
    if(SUCCEEDED(hres))
894
        hres = jsdisp_propput_name(jsthis, L"length", jsval_number(length-delete_cnt+add_args));
895 896 897 898 899 900 901

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

902 903
    if(r)
        *r = jsval_obj(ret_array);
904
    return S_OK;
905 906
}

907
/* ECMA-262 3rd Edition    15.4.4.2 */
908
static HRESULT Array_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv,
909
        jsval_t *r)
910
{
911 912
    ArrayInstance *array;

913 914
    TRACE("\n");

915
    array = array_this(jsthis);
916
    if(!array)
917
        return JS_E_ARRAY_EXPECTED;
918

919
    return array_join(ctx, &array->dispex, array->length, L",", 1, r);
920 921
}

922
static HRESULT Array_toLocaleString(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
923
        jsval_t *r)
924 925 926 927 928
{
    FIXME("\n");
    return E_NOTIMPL;
}

929 930 931
static HRESULT Array_forEach(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
        jsval_t *r)
{
932
    IDispatch *context_obj = NULL, *callback;
933 934 935 936 937 938 939 940 941 942 943
    jsval_t value, args[3], res;
    jsdisp_t *jsthis;
    unsigned length, i;
    HRESULT hres;

    TRACE("\n");

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

944 945 946 947 948
    /* Fixme check IsCallable */
    if(!argc || !is_object_instance(argv[0]) || !get_object(argv[0])) {
        FIXME("Invalid arg %s\n", debugstr_jsval(argc ? argv[0] : jsval_undefined()));
        return E_INVALIDARG;
    }
949
    callback = get_object(argv[0]);
950 951

    if(argc > 1 && !is_undefined(argv[1])) {
952 953 954 955 956
        if(!is_object_instance(argv[1]) || !get_object(argv[1])) {
            FIXME("Unsupported context this %s\n", debugstr_jsval(argv[1]));
            return E_NOTIMPL;
        }
        context_obj = get_object(argv[1]);
957 958
    }

959 960 961 962 963 964 965 966 967 968
    for(i = 0; i < length; i++) {
        hres = jsdisp_get_idx(jsthis, i, &value);
        if(hres == DISP_E_UNKNOWNNAME)
            continue;
        if(FAILED(hres))
            return hres;

        args[0] = value;
        args[1] = jsval_number(i);
        args[2] = jsval_obj(jsthis);
969
        hres = disp_call_value(ctx, callback, context_obj, DISPATCH_METHOD, ARRAY_SIZE(args), args, &res);
970 971 972 973 974 975 976 977 978 979
        jsval_release(value);
        if(FAILED(hres))
            return hres;
        jsval_release(res);
    }

    if(r) *r = jsval_undefined();
    return S_OK;
}

980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
static HRESULT Array_indexOf(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
        jsval_t *r)
{
    jsdisp_t *jsthis;
    unsigned length, i, from = 0;
    jsval_t search, value;
    BOOL eq;
    HRESULT hres;

    TRACE("\n");

    hres = get_length(ctx, vthis, &jsthis, &length);
    if(FAILED(hres))
        return hres;
    if(!length) {
        if(r) *r = jsval_number(-1);
        return S_OK;
    }

    search = argc ? argv[0] : jsval_undefined();

    if(argc > 1) {
        double from_arg;

        hres = to_integer(ctx, argv[1], &from_arg);
        if(FAILED(hres))
            return hres;

        if(from_arg >= 0)
            from = min(from_arg, length);
        else
            from = max(from_arg + length, 0);
    }

    for(i = from; i < length; i++) {
        hres = jsdisp_get_idx(jsthis, i, &value);
        if(hres == DISP_E_UNKNOWNNAME)
            continue;
        if(FAILED(hres))
            return hres;

        hres = jsval_strict_equal(value, search, &eq);
        jsval_release(value);
        if(FAILED(hres))
            return hres;
        if(eq) {
            if(r) *r = jsval_number(i);
            return S_OK;
        }
    }

    if(r) *r = jsval_number(-1);
    return S_OK;
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
static HRESULT Array_map(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
{
    IDispatch *context_this = NULL, *callback;
    jsval_t callback_args[3], mapped_value;
    jsdisp_t *jsthis, *array;
    DWORD length, k;
    HRESULT hres;

    TRACE("\n");

    hres = get_length(ctx, vthis, &jsthis, &length);
    if(FAILED(hres)) {
        FIXME("Could not get length\n");
        return hres;
    }

1051
    /* FIXME: check IsCallable */
1052 1053 1054 1055 1056 1057 1058
    if(!argc || !is_object_instance(argv[0]) || !get_object(argv[0])) {
        FIXME("Invalid arg %s\n", debugstr_jsval(argc ? argv[0] : jsval_undefined()));
        return E_INVALIDARG;
    }
    callback = get_object(argv[0]);

    if(argc > 1) {
1059 1060 1061
        if(is_object_instance(argv[1]) && get_object(argv[1])) {
            context_this = get_object(argv[1]);
        }else if(!is_undefined(argv[1])) {
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
            FIXME("Unsupported context this %s\n", debugstr_jsval(argv[1]));
            return E_NOTIMPL;
        }
    }

    hres = create_array(ctx, length, &array);
    if(FAILED(hres))
        return hres;

    for(k = 0; k < length; k++) {
        hres = jsdisp_get_idx(jsthis, k, &callback_args[0]);
        if(hres == DISP_E_UNKNOWNNAME)
            continue;
        if(FAILED(hres))
            break;

        callback_args[1] = jsval_number(k);
        callback_args[2] = jsval_obj(jsthis);
        hres = disp_call_value(ctx, callback, context_this, DISPATCH_METHOD, 3, callback_args, &mapped_value);
        jsval_release(callback_args[0]);
        if(FAILED(hres))
            break;

        hres = jsdisp_propput_idx(array, k, mapped_value);
        if(FAILED(hres))
            break;
    }

    if(SUCCEEDED(hres) && r)
        *r = jsval_obj(array);
    else
        jsdisp_release(array);
    return hres;
}

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
static HRESULT Array_reduce(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
{
    IDispatch *context_this = NULL, *callback;
    jsval_t callback_args[4], acc, new_acc;
    BOOL have_value = FALSE;
    jsdisp_t *jsthis;
    DWORD length, k;
    HRESULT hres;

    TRACE("\n");

    hres = get_length(ctx, vthis, &jsthis, &length);
    if(FAILED(hres)) {
        FIXME("Could not get length\n");
        return hres;
    }

    /* Fixme check IsCallable */
    if(!argc || !is_object_instance(argv[0]) || !get_object(argv[0])) {
        FIXME("Invalid arg %s\n", debugstr_jsval(argc ? argv[0] : jsval_undefined()));
        return E_INVALIDARG;
    }
    callback = get_object(argv[0]);

    if(argc > 1) {
        have_value = TRUE;
        hres = jsval_copy(argv[1], &acc);
        if(FAILED(hres))
            return hres;
    }

    for(k = 0; k < length; k++) {
        hres = jsdisp_get_idx(jsthis, k, &callback_args[1]);
        if(hres == DISP_E_UNKNOWNNAME)
            continue;
        if(FAILED(hres))
            break;

        if(!have_value) {
            have_value = TRUE;
            acc = callback_args[1];
            continue;
        }

        callback_args[0] = acc;
        callback_args[2] = jsval_number(k);
        callback_args[3] = jsval_obj(jsthis);
        hres = disp_call_value(ctx, callback, context_this, DISPATCH_METHOD, ARRAY_SIZE(callback_args), callback_args, &new_acc);
        jsval_release(callback_args[1]);
        if(FAILED(hres))
            break;

        jsval_release(acc);
        acc = new_acc;
    }

    if(SUCCEEDED(hres) && !have_value) {
        WARN("No array element\n");
        hres = JS_E_INVALID_ACTION;
    }

    if(SUCCEEDED(hres) && r)
        *r = acc;
    else if(have_value)
        jsval_release(acc);
    return hres;
}

1165
/* ECMA-262 3rd Edition    15.4.4.13 */
1166
static HRESULT Array_unshift(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1167
        jsval_t *r)
1168
{
1169
    jsdisp_t *jsthis;
1170
    WCHAR buf[14], *buf_end, *str;
1171
    DWORD i, length;
1172
    jsval_t val;
1173 1174 1175 1176 1177
    DISPID id;
    HRESULT hres;

    TRACE("\n");

1178
    hres = get_length(ctx, vthis, &jsthis, &length);
1179 1180
    if(FAILED(hres))
        return hres;
1181

1182
    if(argc) {
1183
        buf_end = buf + ARRAY_SIZE(buf)-1;
1184 1185
        *buf_end-- = 0;
        i = length;
1186

1187 1188
        while(i--) {
            str = idx_to_str(i, buf_end);
1189

1190 1191
            hres = jsdisp_get_id(jsthis, str, 0, &id);
            if(SUCCEEDED(hres)) {
1192
                hres = jsdisp_propget(jsthis, id, &val);
1193 1194
                if(FAILED(hres))
                    return hres;
1195

1196
                hres = jsdisp_propput_idx(jsthis, i+argc, val);
1197
                jsval_release(val);
1198 1199 1200
            }else if(hres == DISP_E_UNKNOWNNAME) {
                hres = IDispatchEx_DeleteMemberByDispID(vthis->u.dispex, id);
            }
1201 1202 1203 1204 1205 1206 1207
        }

        if(FAILED(hres))
            return hres;
    }

    for(i=0; i<argc; i++) {
1208
        hres = jsdisp_propput_idx(jsthis, i, argv[i]);
1209 1210 1211 1212
        if(FAILED(hres))
            return hres;
    }

1213 1214
    if(argc) {
        length += argc;
1215
        hres = set_length(jsthis, length);
1216 1217 1218
        if(FAILED(hres))
            return hres;
    }
1219

1220 1221
    if(r)
        *r = ctx->version < 2 ? jsval_undefined() : jsval_number(length);
1222
    return S_OK;
1223 1224
}

1225
static HRESULT Array_get_value(script_ctx_t *ctx, jsdisp_t *jsthis, jsval_t *r)
1226
{
1227 1228
    ArrayInstance *array = array_from_jsdisp(jsthis);

1229 1230
    TRACE("\n");

1231
    return array_join(ctx, &array->dispex, array->length, L",", 1, r);
1232 1233
}

1234
static void Array_destructor(jsdisp_t *dispex)
1235 1236 1237 1238
{
    heap_free(dispex);
}

1239
static void Array_on_put(jsdisp_t *dispex, const WCHAR *name)
1240
{
1241
    ArrayInstance *array = array_from_jsdisp(dispex);
1242 1243 1244
    const WCHAR *ptr = name;
    DWORD id = 0;

1245
    if(!is_digit(*ptr))
1246 1247
        return;

1248
    while(*ptr && is_digit(*ptr)) {
1249 1250 1251 1252 1253 1254 1255 1256 1257
        id = id*10 + (*ptr-'0');
        ptr++;
    }

    if(*ptr)
        return;

    if(id >= array->length)
        array->length = id+1;
1258 1259 1260
}

static const builtin_prop_t Array_props[] = {
1261 1262 1263 1264 1265 1266 1267 1268
    {L"concat",                Array_concat,               PROPF_METHOD|1},
    {L"forEach",               Array_forEach,              PROPF_METHOD|PROPF_ES5|1},
    {L"indexOf",               Array_indexOf,              PROPF_METHOD|PROPF_ES5|1},
    {L"join",                  Array_join,                 PROPF_METHOD|1},
    {L"length",                NULL,0,                     Array_get_length, Array_set_length},
    {L"map",                   Array_map,                  PROPF_METHOD|PROPF_ES5|1},
    {L"pop",                   Array_pop,                  PROPF_METHOD},
    {L"push",                  Array_push,                 PROPF_METHOD|1},
1269
    {L"reduce",                Array_reduce,               PROPF_METHOD|PROPF_ES5|1},
1270 1271 1272 1273 1274 1275 1276 1277
    {L"reverse",               Array_reverse,              PROPF_METHOD},
    {L"shift",                 Array_shift,                PROPF_METHOD},
    {L"slice",                 Array_slice,                PROPF_METHOD|2},
    {L"sort",                  Array_sort,                 PROPF_METHOD|1},
    {L"splice",                Array_splice,               PROPF_METHOD|2},
    {L"toLocaleString",        Array_toLocaleString,       PROPF_METHOD},
    {L"toString",              Array_toString,             PROPF_METHOD},
    {L"unshift",               Array_unshift,              PROPF_METHOD|1},
1278 1279 1280 1281
};

static const builtin_info_t Array_info = {
    JSCLASS_ARRAY,
1282
    {NULL, NULL,0, Array_get_value},
1283
    ARRAY_SIZE(Array_props),
1284 1285 1286 1287 1288
    Array_props,
    Array_destructor,
    Array_on_put
};

1289
static const builtin_prop_t ArrayInst_props[] = {
1290
    {L"length",                NULL,0,                     Array_get_length, Array_set_length}
1291 1292 1293 1294
};

static const builtin_info_t ArrayInst_info = {
    JSCLASS_ARRAY,
1295
    {NULL, NULL,0, Array_get_value},
1296
    ARRAY_SIZE(ArrayInst_props),
1297 1298 1299 1300 1301
    ArrayInst_props,
    Array_destructor,
    Array_on_put
};

1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
/* ECMA-262 5.1 Edition    15.4.3.2 */
static HRESULT ArrayConstr_isArray(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r)
{
    jsdisp_t *obj;

    TRACE("\n");

    if(!argc || !is_object_instance(argv[0])) {
        if(r) *r = jsval_bool(FALSE);
        return S_OK;
    }

    obj = iface_to_jsdisp(get_object(argv[0]));
    if(r) *r = jsval_bool(obj && is_class(obj, JSCLASS_ARRAY));
    if(obj) jsdisp_release(obj);
    return S_OK;
}

1320
static HRESULT ArrayConstr_value(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv,
1321
        jsval_t *r)
1322
{
1323
    jsdisp_t *obj;
1324 1325 1326 1327 1328 1329
    DWORD i;
    HRESULT hres;

    TRACE("\n");

    switch(flags) {
1330
    case DISPATCH_METHOD:
1331
    case DISPATCH_CONSTRUCT: {
1332 1333 1334 1335
        if(argc == 1 && is_number(argv[0])) {
            double n = get_number(argv[0]);

            if(n < 0 || !is_int32(n))
1336
                return JS_E_INVALID_LENGTH;
1337

1338
            hres = create_array(ctx, n, &obj);
1339 1340 1341
            if(FAILED(hres))
                return hres;

1342
            *r = jsval_obj(obj);
1343 1344 1345
            return S_OK;
        }

1346
        hres = create_array(ctx, argc, &obj);
1347 1348 1349
        if(FAILED(hres))
            return hres;

1350
        for(i=0; i < argc; i++) {
1351
            hres = jsdisp_propput_idx(obj, i, argv[i]);
1352 1353 1354 1355 1356 1357 1358 1359
            if(FAILED(hres))
                break;
        }
        if(FAILED(hres)) {
            jsdisp_release(obj);
            return hres;
        }

1360
        *r = jsval_obj(obj);
1361 1362 1363 1364 1365 1366 1367 1368
        break;
    }
    default:
        FIXME("unimplemented flags: %x\n", flags);
        return E_NOTIMPL;
    }

    return S_OK;
1369 1370
}

1371
static HRESULT alloc_array(script_ctx_t *ctx, jsdisp_t *object_prototype, ArrayInstance **ret)
1372
{
1373
    ArrayInstance *array;
1374 1375
    HRESULT hres;

1376 1377 1378 1379
    array = heap_alloc_zero(sizeof(ArrayInstance));
    if(!array)
        return E_OUTOFMEMORY;

1380 1381
    if(object_prototype)
        hres = init_dispex(&array->dispex, ctx, &Array_info, object_prototype);
1382
    else
1383
        hres = init_dispex_from_constr(&array->dispex, ctx, &ArrayInst_info, ctx->array_constr);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393

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

    *ret = array;
    return S_OK;
}

1394
static const builtin_prop_t ArrayConstr_props[] = {
1395
    {L"isArray",    ArrayConstr_isArray,    PROPF_ES5|PROPF_METHOD|1}
1396 1397 1398 1399 1400
};

static const builtin_info_t ArrayConstr_info = {
    JSCLASS_FUNCTION,
    DEFAULT_FUNCTION_VALUE,
1401
    ARRAY_SIZE(ArrayConstr_props),
1402 1403 1404 1405 1406
    ArrayConstr_props,
    NULL,
    NULL
};

1407
HRESULT create_array_constr(script_ctx_t *ctx, jsdisp_t *object_prototype, jsdisp_t **ret)
1408 1409 1410 1411
{
    ArrayInstance *array;
    HRESULT hres;

1412
    hres = alloc_array(ctx, object_prototype, &array);
1413 1414 1415
    if(FAILED(hres))
        return hres;

1416
    hres = create_builtin_constructor(ctx, ArrayConstr_value, L"Array", &ArrayConstr_info, PROPF_CONSTR|1, &array->dispex, ret);
1417

Jacek Caban's avatar
Jacek Caban committed
1418
    jsdisp_release(&array->dispex);
1419 1420
    return hres;
}
1421

1422
HRESULT create_array(script_ctx_t *ctx, DWORD length, jsdisp_t **ret)
1423 1424 1425 1426
{
    ArrayInstance *array;
    HRESULT hres;

1427
    hres = alloc_array(ctx, NULL, &array);
1428 1429 1430 1431 1432 1433 1434 1435
    if(FAILED(hres))
        return hres;

    array->length = length;

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