summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/test-integrity-level-detached.js
blob: 68f772de6129a5ada7cfcedb9b517664713848a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const EMPTY = 0;
const INLINE_STORAGE = 10;
const NON_INLINE_STORAGE = 1024;

class DetachedInt32Array extends Int32Array {
    constructor(...args) {
        super(...args);
        detachArrayBuffer(this.buffer);
    }
}

function throwsTypeError(fn) {
    try {
        fn();
    } catch (e) {
        assertEq(e instanceof TypeError, true);
        return true;
    }
    return false;
}

// Non-standard: Accessing elements of detached array buffers should throw, but
// this is currently not implemented.
const ACCESS_ON_DETACHED_ARRAY_BUFFER_THROWS = (() => {
    let ta = new DetachedInt32Array(10);
    let throws = throwsTypeError(() => ta[0]);
    // Ensure [[Get]] and [[GetOwnProperty]] return consistent results.
    assertEq(throwsTypeError(() => Object.getOwnPropertyDescriptor(ta, 0)), throws);
    return throws;
})();

function maybeThrowOnDetached(fn, returnValue) {
    if (ACCESS_ON_DETACHED_ARRAY_BUFFER_THROWS) {
        assertThrowsInstanceOf(fn, TypeError);
        return returnValue;
    }
    return fn();
}

// Empty typed arrays can be sealed.
{
    let ta = new DetachedInt32Array(EMPTY);
    Object.seal(ta);

    assertEq(Object.isExtensible(ta), false);
    assertEq(Object.isSealed(ta), true);
    assertEq(Object.isFrozen(ta), true);
}

// Non-empty typed arrays can be sealed, but calling TestIntegrityLevel will
// throw on detached typed arrays.
for (let length of [INLINE_STORAGE, NON_INLINE_STORAGE]) {
    let ta = new DetachedInt32Array(length);
    Object.seal(ta);

    assertEq(Object.isExtensible(ta), false);
    assertEq(maybeThrowOnDetached(() => Object.isSealed(ta), true), true);
    assertEq(maybeThrowOnDetached(() => Object.isFrozen(ta), true), true);
}

// Empty typed arrays can be frozen.
{
    let ta = new DetachedInt32Array(EMPTY);
    Object.freeze(ta);

    assertEq(Object.isExtensible(ta), false);
    assertEq(Object.isSealed(ta), true);
    assertEq(Object.isFrozen(ta), true);
}

// Non-empty typed arrays cannot be frozen.
for (let length of [INLINE_STORAGE, NON_INLINE_STORAGE]) {
    let ta = new DetachedInt32Array(length);
    maybeThrowOnDetached(() => Object.freeze(ta));

    assertEq(Object.isExtensible(ta), false);
    assertEq(maybeThrowOnDetached(() => Object.isSealed(ta), true), true);
    assertEq(maybeThrowOnDetached(() => Object.isFrozen(ta), true), true);
}

// Non-extensible empty typed arrays are sealed and frozen.
{
    let ta = new DetachedInt32Array(EMPTY);
    Object.preventExtensions(ta);

    assertEq(Object.isExtensible(ta), false);
    assertEq(Object.isSealed(ta), true);
    assertEq(Object.isFrozen(ta), true);
}

// Calling TestIntegrityLevel will throw on detached typed arrays with non-zero
// length.
for (let length of [INLINE_STORAGE, NON_INLINE_STORAGE]) {
    let ta = new DetachedInt32Array(length);
    Object.preventExtensions(ta);

    assertEq(Object.isExtensible(ta), false);
    assertEq(maybeThrowOnDetached(() => Object.isSealed(ta), true), true);
    assertEq(maybeThrowOnDetached(() => Object.isFrozen(ta), true), true);
}


if (typeof reportCompare === "function")
    reportCompare(true, true);