mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-30 07:14:46 +00:00
Wasm: Store Function section as bytes and a count
This commit is contained in:
parent
7a6c6b675c
commit
743e14148c
1 changed files with 32 additions and 10 deletions
|
@ -84,6 +84,21 @@ fn serialize_vector_section<B: SerialBuffer, T: Serialize>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serialize a section that is stored as bytes and a count
|
||||||
|
fn serialize_bytes_section<B: SerialBuffer>(
|
||||||
|
buffer: &mut B,
|
||||||
|
section_id: SectionId,
|
||||||
|
count: u32,
|
||||||
|
bytes: &[u8],
|
||||||
|
) {
|
||||||
|
if !bytes.is_empty() {
|
||||||
|
let header_indices = write_section_header(buffer, section_id);
|
||||||
|
buffer.encode_u32(count);
|
||||||
|
buffer.append_slice(bytes);
|
||||||
|
update_section_size(buffer, header_indices);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*******************************************************************
|
/*******************************************************************
|
||||||
*
|
*
|
||||||
* Type section
|
* Type section
|
||||||
|
@ -150,12 +165,12 @@ impl<'a> TypeSection<'a> {
|
||||||
|
|
||||||
impl<'a> Serialize for TypeSection<'a> {
|
impl<'a> Serialize for TypeSection<'a> {
|
||||||
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
|
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
|
||||||
if !self.bytes.is_empty() {
|
serialize_bytes_section(
|
||||||
let header_indices = write_section_header(buffer, SectionId::Type);
|
buffer,
|
||||||
buffer.encode_u32(self.offsets.len() as u32);
|
SectionId::Type,
|
||||||
buffer.append_slice(&self.bytes);
|
self.offsets.len() as u32,
|
||||||
update_section_size(buffer, header_indices);
|
&self.bytes,
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -260,19 +275,26 @@ impl<'a> Serialize for ImportSection<'a> {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FunctionSection<'a> {
|
pub struct FunctionSection<'a> {
|
||||||
pub signature_indices: Vec<'a, u32>,
|
pub count: u32,
|
||||||
|
pub bytes: Vec<'a, u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FunctionSection<'a> {
|
impl<'a> FunctionSection<'a> {
|
||||||
pub fn new(arena: &'a Bump, capacity: usize) -> Self {
|
pub fn new(arena: &'a Bump, capacity: usize) -> Self {
|
||||||
FunctionSection {
|
FunctionSection {
|
||||||
signature_indices: Vec::with_capacity_in(capacity, arena),
|
count: 0,
|
||||||
|
bytes: Vec::with_capacity_in(capacity, arena),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_sig(&mut self, sig_id: u32) {
|
||||||
|
self.bytes.encode_u32(sig_id);
|
||||||
|
self.count += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
impl<'a> Serialize for FunctionSection<'a> {
|
impl<'a> Serialize for FunctionSection<'a> {
|
||||||
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
|
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
|
||||||
serialize_vector_section(buffer, SectionId::Function, &self.signature_indices);
|
serialize_bytes_section(buffer, SectionId::Function, self.count, &self.bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -608,7 +630,7 @@ impl<'a> WasmModule<'a> {
|
||||||
/// Create entries in the Type and Function sections for a function signature
|
/// Create entries in the Type and Function sections for a function signature
|
||||||
pub fn add_function_signature(&mut self, signature: Signature<'a>) {
|
pub fn add_function_signature(&mut self, signature: Signature<'a>) {
|
||||||
let index = self.types.insert(signature);
|
let index = self.types.insert(signature);
|
||||||
self.function.signature_indices.push(index);
|
self.function.add_sig(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serialize the module to bytes
|
/// Serialize the module to bytes
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue