mirror of
https://github.com/denoland/deno.git
synced 2025-01-21 21:50:00 -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:
parent
c391ad315e
commit
a844d96ee9
3 changed files with 354 additions and 217 deletions
|
@ -121,6 +121,10 @@ impl StringTable {
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct NodeRef(pub usize);
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct BoolPos(pub usize);
|
pub struct BoolPos(pub usize);
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -152,13 +156,8 @@ where
|
||||||
K: Into<u8> + Display,
|
K: Into<u8> + Display,
|
||||||
P: Into<u8> + Display,
|
P: Into<u8> + Display,
|
||||||
{
|
{
|
||||||
fn header(
|
fn header(&mut self, kind: K, parent: NodeRef, span: &Span)
|
||||||
&mut self,
|
-> PendingNodeRef;
|
||||||
kind: K,
|
|
||||||
parent: NodeRef,
|
|
||||||
span: &Span,
|
|
||||||
prop_count: usize,
|
|
||||||
) -> NodeRef;
|
|
||||||
fn ref_field(&mut self, prop: P) -> FieldPos;
|
fn ref_field(&mut self, prop: P) -> FieldPos;
|
||||||
fn ref_vec_field(&mut self, prop: P, len: usize) -> FieldArrPos;
|
fn ref_vec_field(&mut self, prop: P, len: usize) -> FieldArrPos;
|
||||||
fn str_field(&mut self, prop: P) -> StrPos;
|
fn str_field(&mut self, prop: P) -> StrPos;
|
||||||
|
@ -166,6 +165,7 @@ where
|
||||||
fn undefined_field(&mut self, prop: P) -> UndefPos;
|
fn undefined_field(&mut self, prop: P) -> UndefPos;
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn null_field(&mut self, prop: P) -> NullPos;
|
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_ref(&mut self, pos: FieldPos, value: NodeRef);
|
||||||
fn write_maybe_ref(&mut self, pos: FieldPos, value: Option<NodeRef>);
|
fn write_maybe_ref(&mut self, pos: FieldPos, value: Option<NodeRef>);
|
||||||
|
@ -183,6 +183,7 @@ pub struct SerializeCtx {
|
||||||
str_table: StringTable,
|
str_table: StringTable,
|
||||||
kind_map: Vec<usize>,
|
kind_map: Vec<usize>,
|
||||||
prop_map: Vec<usize>,
|
prop_map: Vec<usize>,
|
||||||
|
field_count: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This is the internal context used to allocate and fill the buffer. The point
|
/// This is the internal context used to allocate and fill the buffer. The point
|
||||||
|
@ -200,8 +201,9 @@ impl SerializeCtx {
|
||||||
start_buf: NodeRef(0),
|
start_buf: NodeRef(0),
|
||||||
buf: vec![],
|
buf: vec![],
|
||||||
str_table: StringTable::new(),
|
str_table: StringTable::new(),
|
||||||
kind_map: vec![0; kind_size + 1],
|
kind_map: vec![0; kind_size],
|
||||||
prop_map: vec![0; prop_size + 1],
|
prop_map: vec![0; prop_size],
|
||||||
|
field_count: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
let empty_str = ctx.str_table.insert("");
|
let empty_str = ctx.str_table.insert("");
|
||||||
|
@ -232,6 +234,8 @@ impl SerializeCtx {
|
||||||
where
|
where
|
||||||
P: Into<u8> + Display + Clone,
|
P: Into<u8> + Display + Clone,
|
||||||
{
|
{
|
||||||
|
self.field_count += 1;
|
||||||
|
|
||||||
let offset = self.buf.len();
|
let offset = self.buf.len();
|
||||||
|
|
||||||
let n: u8 = prop.clone().into();
|
let n: u8 = prop.clone().into();
|
||||||
|
@ -268,7 +272,7 @@ impl SerializeCtx {
|
||||||
parent: NodeRef,
|
parent: NodeRef,
|
||||||
span: &Span,
|
span: &Span,
|
||||||
prop_count: usize,
|
prop_count: usize,
|
||||||
) -> NodeRef {
|
) -> PendingNodeRef {
|
||||||
let offset = self.buf.len();
|
let offset = self.buf.len();
|
||||||
|
|
||||||
// Node type fits in a u8
|
// Node type fits in a u8
|
||||||
|
@ -285,7 +289,19 @@ impl SerializeCtx {
|
||||||
debug_assert!(prop_count < 10);
|
debug_assert!(prop_count < 10);
|
||||||
self.buf.push(prop_count as u8);
|
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.
|
/// Allocate the node header. It's always the same for every node.
|
||||||
|
@ -299,8 +315,7 @@ impl SerializeCtx {
|
||||||
kind: N,
|
kind: N,
|
||||||
parent: NodeRef,
|
parent: NodeRef,
|
||||||
span: &Span,
|
span: &Span,
|
||||||
prop_count: usize,
|
) -> PendingNodeRef
|
||||||
) -> NodeRef
|
|
||||||
where
|
where
|
||||||
N: Into<u8> + Display + Clone,
|
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
|
/// Allocate a reference property that will hold the offset of
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -12,6 +12,7 @@ use super::buffer::FieldArrPos;
|
||||||
use super::buffer::FieldPos;
|
use super::buffer::FieldPos;
|
||||||
use super::buffer::NodeRef;
|
use super::buffer::NodeRef;
|
||||||
use super::buffer::NullPos;
|
use super::buffer::NullPos;
|
||||||
|
use super::buffer::PendingNodeRef;
|
||||||
use super::buffer::SerializeCtx;
|
use super::buffer::SerializeCtx;
|
||||||
use super::buffer::StrPos;
|
use super::buffer::StrPos;
|
||||||
use super::buffer::UndefPos;
|
use super::buffer::UndefPos;
|
||||||
|
@ -447,10 +448,10 @@ impl TsEsTreeBuilder {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
// Max values
|
// Max values
|
||||||
// TODO: Maybe there is a rust macro to grab the last enum value?
|
// TODO: Maybe there is a rust macro to grab the last enum value?
|
||||||
let kind_count: u8 = AstNode::TSEnumBody.into();
|
let kind_max_count: u8 = u8::from(AstNode::TSEnumBody) + 1;
|
||||||
let prop_count: u8 = AstProp::Value.into();
|
let prop_max_count: u8 = u8::from(AstProp::Value) + 1;
|
||||||
Self {
|
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,
|
kind: AstNode,
|
||||||
parent: NodeRef,
|
parent: NodeRef,
|
||||||
span: &Span,
|
span: &Span,
|
||||||
prop_count: usize,
|
) -> PendingNodeRef {
|
||||||
) -> NodeRef {
|
self.ctx.header(kind, parent, span)
|
||||||
self.ctx.header(kind, parent, span, prop_count)
|
}
|
||||||
|
|
||||||
|
fn commit_schema(&mut self, offset: PendingNodeRef) -> NodeRef {
|
||||||
|
self.ctx.commit_schema(offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ref_field(&mut self, prop: AstProp) -> FieldPos {
|
fn ref_field(&mut self, prop: AstProp) -> FieldPos {
|
||||||
|
|
Loading…
Add table
Reference in a new issue