summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/extensions/clone-transferables.js
blob: 6b3ff99079d1df549a1d7dcae47e34e81a6699ca (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// |reftest| skip-if(!xulRuntime.shell)
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

function* buffer_options() {
    for (var scope of ["SameProcess",
                       "DifferentProcess",
                       "DifferentProcessForIndexedDB"])
    {
        for (var size of [0, 8, 16, 200, 1000, 4096, 8192, 65536]) {
            yield { scope, size };
        }
    }
}


function test() {
    for (var {scope, size} of buffer_options()) {
        var old = new ArrayBuffer(size);
        var copy = deserialize(serialize([old, old], [old], { scope }), { scope });
        assertEq(old.byteLength, 0);
        assertEq(copy[0] === copy[1], true);
        copy = copy[0];
        assertEq(copy.byteLength, size);

        var constructors = [ Int8Array,
                             Uint8Array,
                             Int16Array,
                             Uint16Array,
                             Int32Array,
                             Uint32Array,
                             Float32Array,
                             Float64Array,
                             Uint8ClampedArray,
                             DataView ];

        for (var ctor of constructors) {
            var dataview = (ctor === DataView);

            var buf = new ArrayBuffer(size);
            var old_arr = new ctor(buf);
            assertEq(buf.byteLength, size);
            assertEq(buf, old_arr.buffer);
            if (!dataview)
                assertEq(old_arr.length, size / old_arr.BYTES_PER_ELEMENT);

            var copy_arr = deserialize(serialize(old_arr, [ buf ], { scope }), { scope });
            assertEq(buf.byteLength, 0,
                     "donor array buffer should be detached");
            if (!dataview) {
                assertEq(old_arr.length, 0,
                         "donor typed array should be detached");
            }
            assertEq(copy_arr.buffer.byteLength == size, true);
            if (!dataview)
                assertEq(copy_arr.length, size / old_arr.BYTES_PER_ELEMENT);

            buf = null;
            old_arr = null;
            gc(); // Tickle the ArrayBuffer -> view management
        }

        for (var ctor of constructors) {
            var dataview = (ctor === DataView);

            var buf = new ArrayBuffer(size);
            var old_arr = new ctor(buf);
            var dv = new DataView(buf); // Second view
            var copy_arr = deserialize(serialize(old_arr, [ buf ], { scope }), { scope });
            assertEq(buf.byteLength, 0,
                     "donor array buffer should be detached");
            if (!dataview) {
                assertEq(old_arr.byteLength, 0,
                         "donor typed array should be detached");
                assertEq(old_arr.length, 0,
                         "donor typed array should be detached");
            }

            buf = null;
            old_arr = null;
            gc(); // Tickle the ArrayBuffer -> view management
        }

        // Mutate the buffer during the clone operation. The modifications should be visible.
        if (size >= 4) {
            old = new ArrayBuffer(size);
            var view = new Int32Array(old);
            view[0] = 1;
            var mutator = { get foo() { view[0] = 2; } };
            var copy = deserialize(serialize([ old, mutator ], [ old ], { scope }), { scope });
            var viewCopy = new Int32Array(copy[0]);
            assertEq(view.length, 0); // Underlying buffer now detached.
            assertEq(viewCopy[0], 2);
        }

        // Detach the buffer during the clone operation. Should throw an
        // exception.
        if (size >= 4) {
            const b1 = new ArrayBuffer(size);
            let mutator = {
                get foo() {
                    serialize(b1, [b1], { scope });
                }
            };

            assertThrowsInstanceOf(
                () => serialize([ b1, mutator ], [b1]),
                TypeError,
                "detaching (due to Transferring) while serializing should throw"
            );

            const b2 = new ArrayBuffer(size);
            mutator = {
                get foo() {
                    detachArrayBuffer(b2);
                }
            };

            assertThrowsInstanceOf(
                () => serialize([ b2, mutator ], [b2]),
                TypeError,
                "detaching (due to detachArrayBuffer) while serializing should throw"
            );
        }
    }
}

test();
reportCompare(0, 0, 'ok');