Skip to content

Commit

Permalink
refactor: validate() returns [MetadataEntry]
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwfang committed Jan 14, 2025
1 parent ccc2819 commit d1545df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/interpreter/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Module<'m> {
types: Vec<FuncType<'m>>,
// TODO(dev/fast-interp): Flatten it to 1D array when making it persistent in
// flash.
side_table: &'m [SideTableEntry],
side_table: &'m [MetadataEntry],
}

impl<'m> Import<'m> {
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'m> Module<'m> {
let size = parser.parse_u32().into_ok() as usize;
let parser = parser.split_at(size).into_ok();
if i == x as usize {
return (parser, &self.side_table[i].metadata_entry.branch_table);
return (parser, &self.side_table[i].branch_table);
}
}
unreachable!()
Expand Down
23 changes: 9 additions & 14 deletions crates/interpreter/src/side_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ impl<'m> Metadata<'m> {
}
}

#[derive(Default, Debug)]
pub struct MetadataEntry {
#[allow(dead_code)]
pub type_idx: usize,
#[allow(dead_code)]
pub parser_range: Range<usize>,
pub branch_table: Vec<BranchTableEntry>,
}

#[derive(Copy, Clone, Debug, bytemuck::AnyBitPattern)]
#[repr(transparent)]
pub struct BranchTableEntry([u16; 3]);
Expand Down Expand Up @@ -101,17 +110,3 @@ impl BranchTableEntry {
BranchTableEntry([0; 3])
}
}

#[derive(Default, Debug)]
pub struct SideTableEntry {
#[allow(dead_code)]
pub type_idx: usize,
pub metadata_entry: MetadataEntry,
}

#[derive(Default, Debug)]
pub struct MetadataEntry {
#[allow(dead_code)]
pub parser_range: Range<usize>,
pub branch_table: Vec<BranchTableEntry>,
}
12 changes: 5 additions & 7 deletions crates/interpreter/src/valid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::util::*;
use crate::*;

/// Checks whether a WASM module in binary format is valid.
pub fn validate(binary: &[u8]) -> Result<Vec<SideTableEntry>, Error> {
pub fn validate(binary: &[u8]) -> Result<Vec<MetadataEntry>, Error> {
Context::default().check_module(&mut Parser::new(binary))
}

Expand All @@ -45,7 +45,7 @@ struct Context<'m> {
}

impl<'m> Context<'m> {
fn check_module(&mut self, parser: &mut Parser<'m>) -> MResult<Vec<SideTableEntry>, Check> {
fn check_module(&mut self, parser: &mut Parser<'m>) -> MResult<Vec<MetadataEntry>, Check> {
check(parser.parse_bytes(8)? == b"\0asm\x01\0\0\0")?;
if let Some(mut parser) = self.check_section(parser, SectionId::Type)? {
let n = parser.parse_vec()?;
Expand Down Expand Up @@ -137,12 +137,10 @@ impl<'m> Context<'m> {
let mut locals = t.params.to_vec();
parser.parse_locals(&mut locals)?;
let branch_table = Expr::check_body(self, &mut parser, &refs, locals, t.results)?;
side_tables.push(SideTableEntry {
side_tables.push(MetadataEntry {
type_idx: self.funcs[x] as usize,
metadata_entry: MetadataEntry {
parser_range: Range { start: offset, end: offset + size },
branch_table,
},
parser_range: Range { start: offset, end: offset + size },
branch_table,
});
offset += size;
check(parser.is_empty())?;
Expand Down

0 comments on commit d1545df

Please sign in to comment.