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

const scopes = [
  "SameProcess",
  "DifferentProcess",
  "DifferentProcessForIndexedDB",
];

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

  var ab = new ArrayBuffer(byteLength, {maxByteLength});
  assertEq(ab.resizable, 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 = serialize(ta1, undefined, {scope});
  var ta2 = deserialize(clonebuf);
  assertEq(ta2 instanceof Int32Array, true);
  assertEq(ta2.byteLength, byteLength);
  assertEq(ta2.toString(), "1,87654321,-123,0");
  assertEq(ta2.buffer.resizable, true);
  assertEq(ta2.buffer.byteLength, byteLength);
  assertEq(ta2.buffer.maxByteLength, maxByteLength);

  ta2.buffer.resize(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 ArrayBuffer(byteLength, {maxByteLength});
  assertEq(ab.resizable, 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 = serialize(ta1, undefined, {scope});
  var ta2 = deserialize(clonebuf);
  assertEq(ta2 instanceof Float64Array, true);
  assertEq(ta2.byteLength, byteLength);
  assertEq(ta2.toString(), "NaN,3.14,0,0");
  assertEq(ta2.buffer.resizable, true);
  assertEq(ta2.buffer.byteLength, byteLength);
  assertEq(ta2.buffer.maxByteLength, maxByteLength);

  ta2.buffer.resize(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 ArrayBuffer(byteLength, {maxByteLength});
  assertEq(ab.resizable, 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 = serialize(dv1, undefined, {scope});
  var dv2 = deserialize(clonebuf);
  assertEq(dv2 instanceof DataView, true);
  assertEq(dv2.byteLength, byteLength);
  assertEq(new Uint8Array(dv2.buffer).toString(), "5,0,255,0");
  assertEq(dv2.buffer.resizable, true);
  assertEq(dv2.buffer.byteLength, byteLength);
  assertEq(dv2.buffer.maxByteLength, maxByteLength);

  dv2.buffer.resize(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 ArrayBuffer(byteLength, {maxByteLength});
  assertEq(ab.resizable, 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 = serialize(ab, undefined, {scope});
  var ab2 = deserialize(clonebuf);
  assertEq(ab2 instanceof ArrayBuffer, true);
  assertEq(new Uint8Array(ab2).toString(), "33,44,55,66");
  assertEq(ab2.resizable, true);
  assertEq(ab2.byteLength, byteLength);
  assertEq(ab2.maxByteLength, maxByteLength);

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