summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/structured-clone/growable-shared-array-buffers.js
blob: f4eba68231391dbe56102dcef1aaccafabf3a172 (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
// |jit-test| --enable-arraybuffer-resizable; skip-if: !SharedArrayBuffer.prototype.grow

const scopes = [
  "SameProcess",
];

const g = newGlobal({enableCoopAndCoep: true});

const options = {
  SharedArrayBuffer: "allow",
};

function testInt32Array(scope) {
  var length = 4;
  var byteLength = length * Int32Array.BYTES_PER_ELEMENT;
  var maxByteLength = 2 * byteLength;

  var ab = new SharedArrayBuffer(byteLength, {maxByteLength});
  assertEq(ab.growable, true);
  assertEq(ab.byteLength, byteLength);
  assertEq(ab.maxByteLength, maxByteLength);

  var ta1 = new Int32Array(ab);
  assertEq(ta1.byteLength, byteLength);
  ta1.set([1, 87654321, -123]);
  assertEq(ta1.toString(), "1,87654321,-123,0");

  var clonebuf = g.serialize(ta1, undefined, {scope, ...options});
  var ta2 = g.deserialize(clonebuf, {...options});
  assertEq(ta2 instanceof g.Int32Array, true);
  assertEq(ta2.byteLength, byteLength);
  assertEq(ta2.toString(), "1,87654321,-123,0");
  assertEq(ta2.buffer.growable, true);
  assertEq(ta2.buffer.byteLength, byteLength);
  assertEq(ta2.buffer.maxByteLength, maxByteLength);

  ta2.buffer.grow(maxByteLength);
  assertEq(ta2.byteLength, maxByteLength);
}
scopes.forEach(testInt32Array);

function testFloat64Array(scope) {
  var length = 4;
  var byteLength = length * Float64Array.BYTES_PER_ELEMENT;
  var maxByteLength = 2 * byteLength;

  var ab = new SharedArrayBuffer(byteLength, {maxByteLength});
  assertEq(ab.growable, true);
  assertEq(ab.byteLength, byteLength);
  assertEq(ab.maxByteLength, maxByteLength);

  var ta1 = new Float64Array(ab);
  assertEq(ta1.byteLength, byteLength);
  ta1.set([NaN, 3.14, 0, 0]);
  assertEq(ta1.toString(), "NaN,3.14,0,0");

  var clonebuf = g.serialize(ta1, undefined, {scope, ...options});
  var ta2 = g.deserialize(clonebuf, {...options});
  assertEq(ta2 instanceof g.Float64Array, true);
  assertEq(ta2.byteLength, byteLength);
  assertEq(ta2.toString(), "NaN,3.14,0,0");
  assertEq(ta2.buffer.growable, true);
  assertEq(ta2.buffer.byteLength, byteLength);
  assertEq(ta2.buffer.maxByteLength, maxByteLength);

  ta2.buffer.grow(maxByteLength);
  assertEq(ta2.byteLength, maxByteLength);
}
scopes.forEach(testFloat64Array);

function testDataView(scope) {
  var length = 4;
  var byteLength = length * Uint8Array.BYTES_PER_ELEMENT;
  var maxByteLength = 2 * byteLength;

  var ab = new SharedArrayBuffer(byteLength, {maxByteLength});
  assertEq(ab.growable, true);
  assertEq(ab.byteLength, byteLength);
  assertEq(ab.maxByteLength, maxByteLength);

  var ta = new Uint8Array(ab);
  ta.set([5, 0, 255]);
  assertEq(ta.toString(), "5,0,255,0");
  var dv1 = new DataView(ab);
  assertEq(dv1.byteLength, byteLength);

  var clonebuf = g.serialize(dv1, undefined, {scope, ...options});
  var dv2 = g.deserialize(clonebuf, {...options});
  assertEq(dv2 instanceof g.DataView, true);
  assertEq(dv2.byteLength, byteLength);
  assertEq(new Uint8Array(dv2.buffer).toString(), "5,0,255,0");
  assertEq(dv2.buffer.growable, true);
  assertEq(dv2.buffer.byteLength, byteLength);
  assertEq(dv2.buffer.maxByteLength, maxByteLength);

  dv2.buffer.grow(maxByteLength);
  assertEq(dv2.byteLength, maxByteLength);
}
scopes.forEach(testDataView);

function testArrayBuffer(scope) {
  var length = 4;
  var byteLength = length * Uint8Array.BYTES_PER_ELEMENT;
  var maxByteLength = 2 * byteLength;

  var ab = new SharedArrayBuffer(byteLength, {maxByteLength});
  assertEq(ab.growable, true);
  assertEq(ab.byteLength, byteLength);
  assertEq(ab.maxByteLength, maxByteLength);

  var ta = new Uint8Array(ab);
  ta.set([33, 44, 55, 66]);
  assertEq(ta.toString(), "33,44,55,66");

  var clonebuf = g.serialize(ab, undefined, {scope, ...options});
  var ab2 = g.deserialize(clonebuf, {...options});
  assertEq(ab2 instanceof g.SharedArrayBuffer, true);
  assertEq(new Uint8Array(ab2).toString(), "33,44,55,66");
  assertEq(ab2.growable, true);
  assertEq(ab2.byteLength, byteLength);
  assertEq(ab2.maxByteLength, maxByteLength);

  ab2.grow(maxByteLength);
  assertEq(ab2.byteLength, maxByteLength);
}
scopes.forEach(testArrayBuffer);