• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: global=window,worker
2// META: script=/common/sab.js
3
4[
5  {
6    "input": "Hi",
7    "read": 0,
8    "destinationLength": 0,
9    "written": []
10  },
11  {
12    "input": "A",
13    "read": 1,
14    "destinationLength": 10,
15    "written": [0x41]
16  },
17  {
18    "input": "\u{1D306}", // "\uD834\uDF06"
19    "read": 2,
20    "destinationLength": 4,
21    "written": [0xF0, 0x9D, 0x8C, 0x86]
22  },
23  {
24    "input": "\u{1D306}A",
25    "read": 0,
26    "destinationLength": 3,
27    "written": []
28  },
29  {
30    "input": "\uD834A\uDF06A¥Hi",
31    "read": 5,
32    "destinationLength": 10,
33    "written": [0xEF, 0xBF, 0xBD, 0x41, 0xEF, 0xBF, 0xBD, 0x41, 0xC2, 0xA5]
34  },
35  {
36    "input": "A\uDF06",
37    "read": 2,
38    "destinationLength": 4,
39    "written": [0x41, 0xEF, 0xBF, 0xBD]
40  },
41  {
42    "input": "¥¥",
43    "read": 2,
44    "destinationLength": 4,
45    "written": [0xC2, 0xA5, 0xC2, 0xA5]
46  }
47].forEach(testData => {
48  [
49    {
50      "bufferIncrease": 0,
51      "destinationOffset": 0,
52      "filler": 0
53    },
54    {
55      "bufferIncrease": 10,
56      "destinationOffset": 4,
57      "filler": 0
58    },
59    {
60      "bufferIncrease": 0,
61      "destinationOffset": 0,
62      "filler": 0x80
63    },
64    {
65      "bufferIncrease": 10,
66      "destinationOffset": 4,
67      "filler": 0x80
68    },
69    {
70      "bufferIncrease": 0,
71      "destinationOffset": 0,
72      "filler": "random"
73    },
74    {
75      "bufferIncrease": 10,
76      "destinationOffset": 4,
77      "filler": "random"
78    }
79  ].forEach(destinationData => {
80    ["ArrayBuffer", "SharedArrayBuffer"].forEach(arrayBufferOrSharedArrayBuffer => {
81      test(() => {
82        // Setup
83        const bufferLength = testData.destinationLength + destinationData.bufferIncrease;
84        const destinationOffset = destinationData.destinationOffset;
85        const destinationLength = testData.destinationLength;
86        const destinationFiller = destinationData.filler;
87        const encoder = new TextEncoder();
88        const buffer = createBuffer(arrayBufferOrSharedArrayBuffer, bufferLength);
89        const view = new Uint8Array(buffer, destinationOffset, destinationLength);
90        const fullView = new Uint8Array(buffer);
91        const control = new Array(bufferLength);
92        let byte = destinationFiller;
93        for (let i = 0; i < bufferLength; i++) {
94          if (destinationFiller === "random") {
95            byte = Math.floor(Math.random() * 256);
96          }
97          control[i] = byte;
98          fullView[i] = byte;
99        }
100
101        // It's happening
102        const result = encoder.encodeInto(testData.input, view);
103
104        // Basics
105        assert_equals(view.byteLength, destinationLength);
106        assert_equals(view.length, destinationLength);
107
108        // Remainder
109        assert_equals(result.read, testData.read);
110        assert_equals(result.written, testData.written.length);
111        for (let i = 0; i < bufferLength; i++) {
112          if (i < destinationOffset || i >= (destinationOffset + testData.written.length)) {
113            assert_equals(fullView[i], control[i]);
114          } else {
115            assert_equals(fullView[i], testData.written[i - destinationOffset]);
116          }
117        }
118      }, "encodeInto() into "  + arrayBufferOrSharedArrayBuffer + " with " + testData.input + " and destination length " + testData.destinationLength + ", offset " + destinationData.destinationOffset + ", filler " + destinationData.filler);
119    })
120  });
121});
122
123["DataView",
124 "Int8Array",
125 "Int16Array",
126 "Int32Array",
127 "Uint16Array",
128 "Uint32Array",
129 "Uint8ClampedArray",
130 "BigInt64Array",
131 "BigUint64Array",
132 "Float32Array",
133 "Float64Array"].forEach(type => {
134  ["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
135    test(() => {
136      const viewInstance = new self[type](createBuffer(arrayBufferOrSharedArrayBuffer, 0));
137      assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", viewInstance));
138    }, "Invalid encodeInto() destination: " + type + ", backed by: " + arrayBufferOrSharedArrayBuffer);
139  });
140});
141
142["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
143  test(() => {
144    assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", createBuffer(arrayBufferOrSharedArrayBuffer, 10)));
145  }, "Invalid encodeInto() destination: " + arrayBufferOrSharedArrayBuffer);
146});
147
148test(() => {
149  const buffer = new ArrayBuffer(10),
150        view = new Uint8Array(buffer);
151  let { read, written } = new TextEncoder().encodeInto("", view);
152  assert_equals(read, 0);
153  assert_equals(written, 0);
154  new MessageChannel().port1.postMessage(buffer, [buffer]);
155  ({ read, written } = new TextEncoder().encodeInto("", view));
156  assert_equals(read, 0);
157  assert_equals(written, 0);
158  ({ read, written } = new TextEncoder().encodeInto("test", view));
159  assert_equals(read, 0);
160  assert_equals(written, 0);
161}, "encodeInto() and a detached output buffer");
162