Skip to content

Commit fa1a81f

Browse files
committed
fix: multi-byte encoder on ASCII does not return pooled Uint8Arrays on Hermes
1 parent e57bf55 commit fa1a81f

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

fallback/multi-byte.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { E_STRING } from './_utils.js'
2-
import { asciiPrefix, decodeAscii, decodeLatin1, decodeUCS2, encodeAscii } from './latin1.js'
2+
import { nativeEncoder } from './platform.js'
3+
import { asciiPrefix, decodeAscii, decodeLatin1, decodeUCS2 } from './latin1.js'
34
import { getTable } from './multi-byte.table.js'
45

56
export const E_STRICT = 'Input is not well-formed for this encoding'
@@ -776,10 +777,9 @@ export function multibyteEncoder(enc, onError) {
776777
if (iso2022jp && !katakana) katakana = getTable('iso-2022-jp-katakana')
777778
return (str) => {
778779
if (typeof str !== 'string') throw new TypeError(E_STRING)
779-
if (ascii && !NON_LATIN.test(str)) {
780-
try {
781-
return encodeAscii(str, E_STRICT)
782-
} catch {}
780+
if (ascii && nativeEncoder && !NON_LATIN.test(str)) {
781+
const u8 = nativeEncoder.encode(str)
782+
if (u8.length === str.length) return u8
783783
}
784784

785785
const length = str.length

tests/multi-byte.encode.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ describe('multi-byte encodings are supersets of ascii', () => {
2727
t.assert.strictEqual(str.length, 1, i)
2828
t.assert.strictEqual(str.codePointAt(0), i, i)
2929
t.assert.strictEqual(decoder(toShared(Uint8Array.of(i))), str)
30-
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(i))
30+
const u8 = encoder(str)
31+
t.assert.deepStrictEqual(u8, Uint8Array.of(i))
32+
t.assert.strictEqual(u8.byteLength, u8.buffer.byteLength)
3133
}
3234
})
3335
}

0 commit comments

Comments
 (0)