Skip to content

Commit 28ed99b

Browse files
committed
refactor(napi/parser): do not compile raw transfer code on WASM (#12271)
Reduce size of `oxc-parser` WASM binary by omitting code for raw transfer. Raw transfer is not supported on WASM. This also allows removing the workaround to prevent compilation failure on 32-bit systems due to `1 << 32` overflowing `usize`. This code is no longer compiled on 32-bit systems.
1 parent 91b9905 commit 28ed99b

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

napi/parser/src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,28 @@ use oxc::{
2222
use oxc_napi::{Comment, OxcError, convert_utf8_to_utf16, get_source_type};
2323

2424
mod convert;
25+
mod types;
26+
pub use types::{EcmaScriptModule, ParseResult, ParserOptions};
27+
28+
// Raw transfer is only supported on 64-bit little-endian systems.
29+
// Don't include raw transfer code on other platforms (notably WASM32).
30+
// `raw_transfer_types` still needs to be compiled, as `assert_layouts` refers to those types,
31+
// but it's all dead code on unsupported platforms, and will be excluded from binary.
32+
#[cfg(all(target_pointer_width = "64", target_endian = "little"))]
2533
mod raw_transfer;
2634
mod raw_transfer_types;
27-
mod types;
35+
#[cfg(all(target_pointer_width = "64", target_endian = "little"))]
2836
pub use raw_transfer::{
2937
get_buffer_offset, parse_async_raw, parse_sync_raw, raw_transfer_supported,
3038
};
31-
pub use types::{EcmaScriptModule, ParseResult, ParserOptions};
39+
40+
// Fallback for 32-bit or big-endian platforms.
41+
/// Returns `true` if raw transfer is supported on this platform.
42+
#[cfg(not(all(target_pointer_width = "64", target_endian = "little")))]
43+
#[napi]
44+
pub fn raw_transfer_supported() -> bool {
45+
false
46+
}
3247

3348
mod generated {
3449
// Note: We intentionally don't import `generated/derive_estree.rs`. It's not needed.

napi/parser/src/raw_transfer.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ use crate::{
2222
raw_transfer_types::{EcmaScriptModule, Error, RawTransferData},
2323
};
2424

25-
// Only 64-bit little-endian platforms are supported at present.
26-
const IS_SUPPORTED_PLATFORM: bool =
27-
cfg!(all(target_pointer_width = "64", target_endian = "little"));
28-
2925
// For raw transfer, use a buffer 4 GiB in size, with 4 GiB alignment.
3026
// This ensures that all 64-bit pointers have the same value in upper 32 bits,
3127
// so JS only needs to read the lower 32 bits to get an offset into the buffer.
@@ -34,9 +30,7 @@ const IS_SUPPORTED_PLATFORM: bool =
3430
// so this enables using `>>` bitshift operator in JS, rather than the more expensive `>>>`,
3531
// without offsets being interpreted as negative.
3632
const TWO_GIB: usize = 1 << 31;
37-
// `1 << 32`.
38-
// We use `IS_SUPPORTED_PLATFORM as usize * 32` to avoid compilation failure on 32-bit platforms.
39-
const FOUR_GIB: usize = 1 << (IS_SUPPORTED_PLATFORM as usize * 32);
33+
const FOUR_GIB: usize = 1 << 32;
4034

4135
const BUFFER_SIZE: usize = TWO_GIB;
4236
const BUFFER_ALIGN: usize = FOUR_GIB;
@@ -175,11 +169,6 @@ unsafe fn parse_raw_impl(
175169
source_len: u32,
176170
options: Option<ParserOptions>,
177171
) {
178-
assert!(
179-
IS_SUPPORTED_PLATFORM,
180-
"Raw transfer is only supported on 64-bit little-endian platforms"
181-
);
182-
183172
// Check buffer has expected size and alignment
184173
assert_eq!(buffer.len(), BUFFER_SIZE);
185174
let buffer_ptr = ptr::from_mut(buffer).cast::<u8>();
@@ -291,9 +280,12 @@ unsafe fn parse_raw_impl(
291280
}
292281

293282
/// Returns `true` if raw transfer is supported on this platform.
283+
//
284+
// This module is only compiled on 64-bit little-endian platforms.
285+
// Fallback version for unsupported platforms in `lib.rs`.
294286
#[napi]
295287
pub fn raw_transfer_supported() -> bool {
296-
IS_SUPPORTED_PLATFORM
288+
true
297289
}
298290

299291
/// Returns `true` if `n` is a multiple of `divisor`.

napi/parser/src/raw_transfer_types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(not(all(target_pointer_width = "64", target_endian = "little")), expect(dead_code))]
2+
13
use std::sync::Arc;
24

35
use rustc_hash::FxHashMap;

0 commit comments

Comments
 (0)