Skip to content

Commit 506d8f6

Browse files
committed
fix: Hermes breaks language spec again
1 parent e6b4966 commit 506d8f6

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

fallback/_utils.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
const { Buffer, TextEncoder, TextDecoder } = globalThis
22
const haveNativeBuffer = Buffer && !Buffer.TYPED_ARRAY_SUPPORT
3-
let isNative = (x) => x && (haveNativeBuffer || `${x}`.includes('[native code]')) // we consider Node.js TextDecoder/TextEncoder native
3+
export const nativeBuffer = haveNativeBuffer ? Buffer : null
4+
export const isHermes = Boolean(globalThis.HermesInternal)
5+
export const isDeno = Boolean(globalThis.Deno)
6+
export const isLE = new Uint8Array(Uint16Array.of(258).buffer)[0] === 2
7+
8+
let isNative = (x) => {
9+
if (!x) return false
10+
if (haveNativeBuffer) return true // we consider Node.js TextDecoder/TextEncoder native
11+
const s = `${x}`
12+
// See https://github.com/facebook/hermes/pull/1855#issuecomment-3659386410
13+
return s.includes('[native code]') || s.includes(`[bytecode]`) // Static Hermes has [bytecode] for contrib, which includes TextEncoder/TextDecoder
14+
}
15+
416
if (!haveNativeBuffer && isNative(() => {})) isNative = () => false // e.g. XS, we don't want false positives
517

618
export const nativeEncoder = isNative(TextEncoder) ? new TextEncoder() : null
719
export const nativeDecoder = isNative(TextDecoder)
820
? new TextDecoder('utf-8', { ignoreBOM: true })
921
: null
10-
export const nativeBuffer = haveNativeBuffer ? Buffer : null
11-
export const isHermes = Boolean(globalThis.HermesInternal)
12-
export const isDeno = Boolean(globalThis.Deno)
13-
export const isLE = new Uint8Array(Uint16Array.of(258).buffer)[0] === 2
1422

1523
// Actually windows-1252, compatible with ascii and latin1 decoding
1624
// Beware that on non-latin1, i.e. on windows-1252, this is broken in ~all Node.js versions released
1725
// in 2025 due to a regression, so we call it Latin1 as it's usable only for that
1826
let nativeDecoderLatin1impl = null
19-
if (isNative(TextDecoder)) {
27+
if (nativeDecoder) {
2028
// Not all barebone engines with TextDecoder support something except utf-8, detect
2129
try {
2230
nativeDecoderLatin1impl = new TextDecoder('latin1', { ignoreBOM: true })

0 commit comments

Comments
 (0)