1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-08 07:08:27 -05:00

chore(lint): remove manual AST field counter (#27449)

Addresses the review feedback in
https://github.com/denoland/deno/pull/27416 .

- Hoist the buffer max size variable to make it less confusing
- Remove manual AST field counter in favour of an explicit "commit
schema" step which writes the actual field count.
This commit is contained in:
Marvin Hagemeister 2024-12-31 08:59:41 +01:00 committed by GitHub
parent c391ad315e
commit a844d96ee9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 354 additions and 217 deletions

View file

@ -121,6 +121,10 @@ impl StringTable {
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NodeRef(pub usize);
/// Represents an offset to a node whose schema hasn't been committed yet
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PendingNodeRef(pub NodeRef);
#[derive(Debug)]
pub struct BoolPos(pub usize);
#[derive(Debug)]
@ -152,13 +156,8 @@ where
K: Into<u8> + Display,
P: Into<u8> + Display,
{
fn header(
&mut self,
kind: K,
parent: NodeRef,
span: &Span,
prop_count: usize,
) -> NodeRef;
fn header(&mut self, kind: K, parent: NodeRef, span: &Span)
-> PendingNodeRef;
fn ref_field(&mut self, prop: P) -> FieldPos;
fn ref_vec_field(&mut self, prop: P, len: usize) -> FieldArrPos;
fn str_field(&mut self, prop: P) -> StrPos;
@ -166,6 +165,7 @@ where
fn undefined_field(&mut self, prop: P) -> UndefPos;
#[allow(dead_code)]
fn null_field(&mut self, prop: P) -> NullPos;
fn commit_schema(&mut self, offset: PendingNodeRef) -> NodeRef;
fn write_ref(&mut self, pos: FieldPos, value: NodeRef);
fn write_maybe_ref(&mut self, pos: FieldPos, value: Option<NodeRef>);
@ -183,6 +183,7 @@ pub struct SerializeCtx {
str_table: StringTable,
kind_map: Vec<usize>,
prop_map: Vec<usize>,
field_count: u8,
}
/// This is the internal context used to allocate and fill the buffer. The point
@ -200,8 +201,9 @@ impl SerializeCtx {
start_buf: NodeRef(0),
buf: vec![],
str_table: StringTable::new(),
kind_map: vec![0; kind_size + 1],
prop_map: vec![0; prop_size + 1],
kind_map: vec![0; kind_size],
prop_map: vec![0; prop_size],
field_count: 0,
};
let empty_str = ctx.str_table.insert("");
@ -232,6 +234,8 @@ impl SerializeCtx {
where
P: Into<u8> + Display + Clone,
{
self.field_count += 1;
let offset = self.buf.len();
let n: u8 = prop.clone().into();
@ -268,7 +272,7 @@ impl SerializeCtx {
parent: NodeRef,
span: &Span,
prop_count: usize,
) -> NodeRef {
) -> PendingNodeRef {
let offset = self.buf.len();
// Node type fits in a u8
@ -285,7 +289,19 @@ impl SerializeCtx {
debug_assert!(prop_count < 10);
self.buf.push(prop_count as u8);
NodeRef(offset)
PendingNodeRef(NodeRef(offset))
}
pub fn commit_schema(&mut self, node_ref: PendingNodeRef) -> NodeRef {
let mut offset = node_ref.0 .0;
// type + parentId + span lo + span hi
offset += 1 + 4 + 4 + 4;
self.buf[offset] = self.field_count;
self.field_count = 0;
node_ref.0
}
/// Allocate the node header. It's always the same for every node.
@ -299,8 +315,7 @@ impl SerializeCtx {
kind: N,
parent: NodeRef,
span: &Span,
prop_count: usize,
) -> NodeRef
) -> PendingNodeRef
where
N: Into<u8> + Display + Clone,
{
@ -313,7 +328,9 @@ impl SerializeCtx {
}
}
self.append_node(n, parent, span, prop_count)
// Prop count will be filled with the actual value when the
// schema is committed.
self.append_node(n, parent, span, 0)
}
/// Allocate a reference property that will hold the offset of

File diff suppressed because it is too large Load diff

View file

@ -12,6 +12,7 @@ use super::buffer::FieldArrPos;
use super::buffer::FieldPos;
use super::buffer::NodeRef;
use super::buffer::NullPos;
use super::buffer::PendingNodeRef;
use super::buffer::SerializeCtx;
use super::buffer::StrPos;
use super::buffer::UndefPos;
@ -447,10 +448,10 @@ impl TsEsTreeBuilder {
pub fn new() -> Self {
// Max values
// TODO: Maybe there is a rust macro to grab the last enum value?
let kind_count: u8 = AstNode::TSEnumBody.into();
let prop_count: u8 = AstProp::Value.into();
let kind_max_count: u8 = u8::from(AstNode::TSEnumBody) + 1;
let prop_max_count: u8 = u8::from(AstProp::Value) + 1;
Self {
ctx: SerializeCtx::new(kind_count, prop_count),
ctx: SerializeCtx::new(kind_max_count, prop_max_count),
}
}
}
@ -461,9 +462,12 @@ impl AstBufSerializer<AstNode, AstProp> for TsEsTreeBuilder {
kind: AstNode,
parent: NodeRef,
span: &Span,
prop_count: usize,
) -> NodeRef {
self.ctx.header(kind, parent, span, prop_count)
) -> PendingNodeRef {
self.ctx.header(kind, parent, span)
}
fn commit_schema(&mut self, offset: PendingNodeRef) -> NodeRef {
self.ctx.commit_schema(offset)
}
fn ref_field(&mut self, prop: AstProp) -> FieldPos {