fix(op_crates/web) let TextEncoder#encodeInto accept detached ArrayBuffers (#9143)

This commit is contained in:
Anonymous 2021-01-18 07:04:46 -08:00 committed by GitHub
parent 6a50615e7c
commit db3a1d6633
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View file

@ -1134,14 +1134,16 @@
return new Uint8Array(output); return new Uint8Array(output);
} }
encodeInto(input, dest) { encodeInto(input, dest) {
const encoder = new UTF8Encoder();
const inputStream = new Stream(stringToCodePoints(input));
if (!(dest instanceof Uint8Array)) { if (!(dest instanceof Uint8Array)) {
throw new TypeError( throw new TypeError(
"2nd argument to TextEncoder.encodeInto must be Uint8Array", "2nd argument to TextEncoder.encodeInto must be Uint8Array",
); );
} }
if (dest.byteLength === 0) {
return { read: 0, written: 0 };
}
const encoder = new UTF8Encoder();
const inputStream = new Stream(stringToCodePoints(input));
let written = 0; let written = 0;
let read = 0; let read = 0;

View file

@ -243,6 +243,22 @@ function textEncodeInto3() {
assertArrayEquals(Array.from(bytes), [0xf0, 0x9d, 0x93, 0xbd, 0x00]); assertArrayEquals(Array.from(bytes), [0xf0, 0x9d, 0x93, 0xbd, 0x00]);
} }
function textEncodeIntoDetachedBuffer() {
const fixture = "𝓽𝓮𝔁𝓽";
const encoder = new TextEncoder();
const memory = new WebAssembly.Memory({
initial: 1,
maximum: 1,
shared: false,
});
const bytes = new Uint8Array(memory.buffer, 0, 100);
memory.grow(0); // detaches memory.buffer
const result = encoder.encodeInto(fixture, bytes);
assert(bytes.byteLength === 0);
assert(result.read === 0);
assert(result.written === 0);
}
function textDecoderSharedUint8Array() { function textDecoderSharedUint8Array() {
const ab = new SharedArrayBuffer(6); const ab = new SharedArrayBuffer(6);
const dataView = new DataView(ab); const dataView = new DataView(ab);
@ -1209,6 +1225,7 @@ function main() {
textEncodeInto(); textEncodeInto();
textEncodeInto2(); textEncodeInto2();
textEncodeInto3(); textEncodeInto3();
textEncodeIntoDetachedBuffer();
textDecoderSharedUint8Array(); textDecoderSharedUint8Array();
textDecoderSharedInt32Array(); textDecoderSharedInt32Array();
toStringShouldBeWebCompatibility(); toStringShouldBeWebCompatibility();