summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/text_encoding.ts13
-rw-r--r--js/text_encoding_test.ts26
2 files changed, 37 insertions, 2 deletions
diff --git a/js/text_encoding.ts b/js/text_encoding.ts
index d0e08f73b..364a0f020 100644
--- a/js/text_encoding.ts
+++ b/js/text_encoding.ts
@@ -356,6 +356,13 @@ export interface TextDecoderOptions {
ignoreBOM?: false;
}
+type EitherArrayBuffer = SharedArrayBuffer | ArrayBuffer;
+
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+function isEitherArrayBuffer(x: any): x is EitherArrayBuffer {
+ return x instanceof SharedArrayBuffer || x instanceof ArrayBuffer;
+}
+
export class TextDecoder {
private _encoding: string;
@@ -400,12 +407,14 @@ export class TextDecoder {
}
let bytes: Uint8Array;
- if (typeof input === "object" && input instanceof ArrayBuffer) {
+ if (input instanceof Uint8Array) {
+ bytes = input;
+ } else if (isEitherArrayBuffer(input)) {
bytes = new Uint8Array(input);
} else if (
typeof input === "object" &&
"buffer" in input &&
- input.buffer instanceof ArrayBuffer
+ isEitherArrayBuffer(input.buffer)
) {
bytes = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
} else {
diff --git a/js/text_encoding_test.ts b/js/text_encoding_test.ts
index 4eab24808..0799b1c3e 100644
--- a/js/text_encoding_test.ts
+++ b/js/text_encoding_test.ts
@@ -65,3 +65,29 @@ test(function textEncoder2() {
0xf0, 0x9d, 0x93, 0xbd
]);
});
+
+test(function textDecoderSharedUint8Array() {
+ const ab = new SharedArrayBuffer(6);
+ const dataView = new DataView(ab);
+ const charCodeA = "A".charCodeAt(0);
+ for (let i = 0; i < ab.byteLength; i++) {
+ dataView.setUint8(i, charCodeA + i);
+ }
+ const ui8 = new Uint8Array(ab);
+ const decoder = new TextDecoder();
+ const actual = decoder.decode(ui8);
+ assertEquals(actual, "ABCDEF");
+});
+
+test(function textDecoderSharedInt32Array() {
+ const ab = new SharedArrayBuffer(8);
+ const dataView = new DataView(ab);
+ const charCodeA = "A".charCodeAt(0);
+ for (let i = 0; i < ab.byteLength; i++) {
+ dataView.setUint8(i, charCodeA + i);
+ }
+ const i32 = new Int32Array(ab);
+ const decoder = new TextDecoder();
+ const actual = decoder.decode(i32);
+ assertEquals(actual, "ABCDEFGH");
+});