Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
505 changes: 253 additions & 252 deletions crates/oxc_ast_macros/src/generated/structs.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions napi/parser/generated/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

const BUFFER_SIZE = 2147483648,
BUFFER_ALIGN = 4294967296,
DATA_POINTER_POS_32 = 536870908,
IS_TS_FLAG_POS = 2147483636,
DATA_POINTER_POS_32 = 536870910,
IS_TS_FLAG_POS = 2147483644,
PROGRAM_OFFSET = 0;

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion napi/parser/generated/deserialize/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function deserialize(buffer, sourceTextInput, sourceLenInput) {
sourceLen = sourceLenInput;
sourceIsAscii = sourceText.length === sourceLen;

const data = deserializeRawTransferData(uint32[536870908]);
const data = deserializeRawTransferData(uint32[536870910]);

uint8 =
uint32 =
Expand Down
2 changes: 1 addition & 1 deletion napi/parser/generated/deserialize/ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function deserialize(buffer, sourceTextInput, sourceLenInput) {
sourceLen = sourceLenInput;
sourceIsAscii = sourceText.length === sourceLen;

const data = deserializeRawTransferData(uint32[536870908]);
const data = deserializeRawTransferData(uint32[536870910]);

uint8 =
uint32 =
Expand Down
14 changes: 14 additions & 0 deletions napi/parser/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const _: () = {
assert!(offset_of!(RawTransferData, module) == 152);
assert!(offset_of!(RawTransferData, errors) == 256);

// Padding: 3 bytes
assert!(size_of::<RawTransferMetadata>() == 16);
assert!(align_of::<RawTransferMetadata>() == 8);
assert!(offset_of!(RawTransferMetadata, data_offset) == 8);
assert!(offset_of!(RawTransferMetadata, is_ts) == 12);
assert!(offset_of!(RawTransferMetadata, _padding) == 0);

// Padding: 7 bytes
assert!(size_of::<Error>() == 80);
assert!(align_of::<Error>() == 8);
Expand Down Expand Up @@ -68,6 +75,13 @@ const _: () = {
assert!(offset_of!(RawTransferData, module) == 104);
assert!(offset_of!(RawTransferData, errors) == 172);

// Padding: 3 bytes
assert!(size_of::<RawTransferMetadata>() == 16);
assert!(align_of::<RawTransferMetadata>() == 8);
assert!(offset_of!(RawTransferMetadata, data_offset) == 8);
assert!(offset_of!(RawTransferMetadata, is_ts) == 12);
assert!(offset_of!(RawTransferMetadata, _padding) == 0);

// Padding: 3 bytes
assert!(size_of::<Error>() == 44);
assert!(align_of::<Error>() == 4);
Expand Down
17 changes: 9 additions & 8 deletions napi/parser/src/raw_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use oxc_napi::get_source_type;
use crate::{
AstType, ParserOptions, get_ast_type, parse,
raw_transfer_constants::{BUFFER_ALIGN, BUFFER_SIZE},
raw_transfer_types::{EcmaScriptModule, Error, RawTransferData},
raw_transfer_types::{EcmaScriptModule, Error, RawTransferData, RawTransferMetadata},
};

// For raw transfer, use a buffer 2 GiB in size, with 4 GiB alignment.
Expand Down Expand Up @@ -179,10 +179,10 @@ unsafe fn parse_raw_impl(
assert!(is_multiple_of(buffer_ptr as usize, BUFFER_ALIGN));

// Get offsets and size of data region to be managed by arena allocator.
// Leave space for source before it, and 16 bytes for metadata after it.
// Leave space for source before it, and space for metadata after it.
// Metadata actually only takes 5 bytes, but round everything up to multiple of 16,
// as `bumpalo` requires that alignment.
const METADATA_SIZE: usize = 16;
const METADATA_SIZE: usize = size_of::<RawTransferMetadata>();
const {
assert!(METADATA_SIZE >= BUMP_ALIGN);
assert!(is_multiple_of(METADATA_SIZE, BUMP_ALIGN));
Expand Down Expand Up @@ -271,15 +271,16 @@ unsafe fn parse_raw_impl(
ptr::from_ref(data).cast::<u8>()
};

// Write offset of `RawTransferData` and `bool` representing AST type into end of buffer
// Write metadata into end of buffer
#[allow(clippy::cast_possible_truncation)]
let data_offset = data_ptr as u32;
let metadata = RawTransferMetadata::new(data_ptr as u32, ast_type == AstType::TypeScript);
const METADATA_OFFSET: usize = BUFFER_SIZE - METADATA_SIZE;
// SAFETY: `METADATA_OFFSET` is less than length of `buffer`
const _: () = assert!(is_multiple_of(METADATA_OFFSET, BUMP_ALIGN));
// SAFETY: `METADATA_OFFSET` is less than length of `buffer`.
// `METADATA_OFFSET` is aligned on 16.
#[expect(clippy::cast_ptr_alignment)]
unsafe {
buffer_ptr.add(METADATA_OFFSET).cast::<u32>().write(data_offset);
buffer_ptr.add(METADATA_OFFSET + 4).cast::<bool>().write(ast_type == AstType::TypeScript);
buffer_ptr.add(METADATA_OFFSET).cast::<RawTransferMetadata>().write(metadata);
}
}

Expand Down
17 changes: 17 additions & 0 deletions napi/parser/src/raw_transfer_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ pub struct RawTransferData<'a> {
pub errors: Vec<'a, Error<'a>>,
}

/// Metadata written to end of buffer.
#[ast]
pub struct RawTransferMetadata {
/// Offset of `RawTransferData` within buffer.
pub data_offset: u32,
/// `true` if AST is TypeScript.
pub is_ts: bool,
/// Padding to pad struct to size 16.
pub(crate) _padding: u64,
}

impl RawTransferMetadata {
pub fn new(data_offset: u32, is_ts: bool) -> Self {
Self { data_offset, is_ts, _padding: 0 }
}
}

// Errors.
//
// These types and the `From` / `FromIn` impls mirror the implementation in `types.rs`
Expand Down
12 changes: 5 additions & 7 deletions tasks/ast_tools/src/generators/raw_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ const BUFFER_SIZE: u32 = 1 << 31; // 2 GiB
const _: () = assert!(BUFFER_SIZE % 16 == 0);
/// Alignment of raw transfer buffer.
const BUFFER_ALIGN: u64 = 1 << 32; // 4 GiB
/// Size of metadata written to end of buffer.
const METADATA_SIZE: u32 = 16;
/// Offset of flag for whether AST is TypeScript or not, relative to start of metadata.
const IS_TS_FLAG_OFFSET: u32 = 4;

// Offsets of `Vec`'s fields.
// `Vec` is `#[repr(transparent)]` and `RawVec` is `#[repr(C)]`, so these offsets are fixed.
Expand Down Expand Up @@ -1020,9 +1016,11 @@ fn generate_constants(consts: Constants) -> (String, TokenStream) {

/// Calculate constants.
fn get_constants(schema: &Schema) -> Constants {
let metadata_pos = BUFFER_SIZE - METADATA_SIZE;
let data_pointer_pos_32 = metadata_pos / 4;
let is_ts_pos = metadata_pos + IS_TS_FLAG_OFFSET;
let metadata_struct = schema.type_by_name("RawTransferMetadata").as_struct().unwrap();
let metadata_pos = BUFFER_SIZE - metadata_struct.layout_64().size;
let data_pointer_pos = metadata_pos + metadata_struct.field_by_name("data_offset").offset_64();
let data_pointer_pos_32 = data_pointer_pos / 4;
let is_ts_pos = metadata_pos + metadata_struct.field_by_name("is_ts").offset_64();

let program_offset = schema
.type_by_name("RawTransferData")
Expand Down
Loading