From 6c43e71a7ae3eb5223ef733c5cf2440e6cb4f67a Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Fri, 27 Mar 2020 20:43:01 +0000 Subject: [PATCH] Bug 1623954 [wpt PR 22361] - Encoding: use Wasm to get a SharedArrayBuffer instance, a=testonly Automatic update from web-platform-tests Encoding: use Wasm to get a SharedArrayBuffer instance For https://github.com/web-platform-tests/wpt/issues/22358. -- wpt-commits: 4e83bff9e071561dd10538dda073cd2f43b68e4a wpt-pr: 22361 --- testing/web-platform/tests/common/sab.js | 13 +++++++++ .../tests/encoding/encodeInto.any.js | 27 ++++++++++--------- .../tests/encoding/streams/decode-utf8.any.js | 11 ++++---- .../tests/encoding/textdecoder-copy.any.js | 13 +++++---- .../encoding/textdecoder-streaming.any.js | 4 ++- 5 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 testing/web-platform/tests/common/sab.js diff --git a/testing/web-platform/tests/common/sab.js b/testing/web-platform/tests/common/sab.js new file mode 100644 index 0000000000000..c7fd1a742e64f --- /dev/null +++ b/testing/web-platform/tests/common/sab.js @@ -0,0 +1,13 @@ +const createBuffer = (() => { + // See https://github.com/whatwg/html/issues/5380 for why not `new SharedArrayBuffer()` + const sabConstructor = new WebAssembly.Memory({ shared:true, initial:0, maximum:0 }).buffer.constructor; + return (type, length) => { + if (type === "ArrayBuffer") { + return new ArrayBuffer(length); + } else if (type === "SharedArrayBuffer") { + return new sabConstructor(length); + } else { + throw new Error("type has to be ArrayBuffer or SharedArrayBuffer"); + } + } +})(); diff --git a/testing/web-platform/tests/encoding/encodeInto.any.js b/testing/web-platform/tests/encoding/encodeInto.any.js index 7e18c812499a5..eca0e1bca158b 100644 --- a/testing/web-platform/tests/encoding/encodeInto.any.js +++ b/testing/web-platform/tests/encoding/encodeInto.any.js @@ -1,3 +1,6 @@ +// META: global=window,worker +// META: script=/common/sab.js + [ { "input": "Hi", @@ -77,15 +80,15 @@ ["ArrayBuffer", "SharedArrayBuffer"].forEach(arrayBufferOrSharedArrayBuffer => { test(() => { // Setup - const bufferLength = testData.destinationLength + destinationData.bufferIncrease, - destinationOffset = destinationData.destinationOffset, - destinationLength = testData.destinationLength, - destinationFiller = destinationData.filler, - encoder = new TextEncoder(), - buffer = new self[arrayBufferOrSharedArrayBuffer](bufferLength), - view = new Uint8Array(buffer, destinationOffset, destinationLength), - fullView = new Uint8Array(buffer), - control = new Array(bufferLength); + const bufferLength = testData.destinationLength + destinationData.bufferIncrease; + const destinationOffset = destinationData.destinationOffset; + const destinationLength = testData.destinationLength; + const destinationFiller = destinationData.filler; + const encoder = new TextEncoder(); + const buffer = createBuffer(arrayBufferOrSharedArrayBuffer, bufferLength); + const view = new Uint8Array(buffer, destinationOffset, destinationLength); + const fullView = new Uint8Array(buffer); + const control = new Array(bufferLength); let byte = destinationFiller; for (let i = 0; i < bufferLength; i++) { if (destinationFiller === "random") { @@ -128,19 +131,17 @@ Float64Array].forEach(view => { ["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => { test(() => { - assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", new view(new self[arrayBufferOrSharedArrayBuffer](0)))); + assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", new view(createBuffer(arrayBufferOrSharedArrayBuffer, 0)))); }, "Invalid encodeInto() destination: " + view.name + ", backed by: " + arrayBufferOrSharedArrayBuffer); }); }); ["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => { test(() => { - assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", new self[arrayBufferOrSharedArrayBuffer](10))); + assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", createBuffer(arrayBufferOrSharedArrayBuffer, 10))); }, "Invalid encodeInto() destination: " + arrayBufferOrSharedArrayBuffer); }); - - test(() => { const buffer = new ArrayBuffer(10), view = new Uint8Array(buffer); diff --git a/testing/web-platform/tests/encoding/streams/decode-utf8.any.js b/testing/web-platform/tests/encoding/streams/decode-utf8.any.js index 421b98c2eea53..5abd8dcb8f9b8 100644 --- a/testing/web-platform/tests/encoding/streams/decode-utf8.any.js +++ b/testing/web-platform/tests/encoding/streams/decode-utf8.any.js @@ -1,15 +1,14 @@ -// META: global=worker +// META: global=window,worker // META: script=resources/readable-stream-from-array.js // META: script=resources/readable-stream-to-array.js - +// META: script=/common/sab.js 'use strict'; ["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => { - const inputChunkData = [73, 32, 240, 159, 146, 153, 32, 115, 116, - 114, 101, 97, 109, 115] + const inputChunkData = [73, 32, 240, 159, 146, 153, 32, 115, 116, 114, 101, 97, 109, 115]; - const emptyChunk = new Uint8Array(new self[arrayBufferOrSharedArrayBuffer](0)); - const inputChunk = new Uint8Array(new self[arrayBufferOrSharedArrayBuffer](inputChunkData.length)); + const emptyChunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, 0)); + const inputChunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, inputChunkData.length)); inputChunk.set(inputChunkData); diff --git a/testing/web-platform/tests/encoding/textdecoder-copy.any.js b/testing/web-platform/tests/encoding/textdecoder-copy.any.js index 6ae65119db687..61de4142bf27a 100644 --- a/testing/web-platform/tests/encoding/textdecoder-copy.any.js +++ b/testing/web-platform/tests/encoding/textdecoder-copy.any.js @@ -1,10 +1,13 @@ +// META: global=window,worker +// META: script=/common/sab.js + ["ArrayBuffer", "SharedArrayBuffer"].forEach(arrayBufferOrSharedArrayBuffer => { test(() => { - const buf = new self[arrayBufferOrSharedArrayBuffer](2), - view = new Uint8Array(buf), - buf2 = new self[arrayBufferOrSharedArrayBuffer](2), - view2 = new Uint8Array(buf2), - decoder = new TextDecoder("utf-8"); + const buf = createBuffer(arrayBufferOrSharedArrayBuffer, 2); + const view = new Uint8Array(buf); + const buf2 = createBuffer(arrayBufferOrSharedArrayBuffer, 2); + const view2 = new Uint8Array(buf2); + const decoder = new TextDecoder("utf-8"); view[0] = 0xEF; view[1] = 0xBB; view2[0] = 0xBF; diff --git a/testing/web-platform/tests/encoding/textdecoder-streaming.any.js b/testing/web-platform/tests/encoding/textdecoder-streaming.any.js index e0c59472598fb..5717c2dbe00a2 100644 --- a/testing/web-platform/tests/encoding/textdecoder-streaming.any.js +++ b/testing/web-platform/tests/encoding/textdecoder-streaming.any.js @@ -1,5 +1,7 @@ // META: title=Encoding API: Streaming decode +// META: global=window,worker // META: script=resources/encodings.js +// META: script=/common/sab.js var string = '\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF'; var octets = { @@ -28,7 +30,7 @@ var octets = { var sub = []; for (var j = i; j < encoded.length && j < i + len; ++j) sub.push(encoded[j]); - var uintArray = new Uint8Array(new self[arrayBufferOrSharedArrayBuffer](sub.length)); + var uintArray = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, sub.length)); uintArray.set(sub); out += decoder.decode(uintArray, {stream: true}); }