Wasm: store function_count on the ImportSection

This commit is contained in:
Brian Carroll 2022-01-12 08:45:24 +00:00
parent 7a4593170c
commit ca2597973e
3 changed files with 25 additions and 8 deletions

View file

@ -96,8 +96,7 @@ pub fn build_module_help<'a>(
let initial_module = WasmModule::preload(env.arena, preload_bytes); let initial_module = WasmModule::preload(env.arena, preload_bytes);
// Adjust Wasm function indices to account for functions from the object file // Adjust Wasm function indices to account for functions from the object file
let runtime_import_fn_count: u32 = initial_module.import.function_count(); // to be imported at runtime (e.g. WASI) let fn_index_offset: u32 = initial_module.import.function_count + initial_module.code.preloaded_count;
let fn_index_offset: u32 = runtime_import_fn_count + initial_module.code.preloaded_count;
// Get a map of name to index for the preloaded functions // Get a map of name to index for the preloaded functions
// Assumes the preloaded object file has all symbols exported, as per `zig build-lib -dymamic` // Assumes the preloaded object file has all symbols exported, as per `zig build-lib -dymamic`

View file

@ -56,7 +56,7 @@ pub trait Section<'a>: Sized {
} }
macro_rules! section_impl { macro_rules! section_impl {
($structname: ident, $id: expr) => { ($structname: ident, $id: expr, $from_count_and_bytes: expr) => {
impl<'a> Section<'a> for $structname<'a> { impl<'a> Section<'a> for $structname<'a> {
const ID: SectionId = $id; const ID: SectionId = $id;
@ -72,7 +72,7 @@ macro_rules! section_impl {
let (count, initial_bytes) = parse_section(Self::ID, module_bytes, cursor); let (count, initial_bytes) = parse_section(Self::ID, module_bytes, cursor);
let mut bytes = Vec::with_capacity_in(initial_bytes.len() * 2, arena); let mut bytes = Vec::with_capacity_in(initial_bytes.len() * 2, arena);
bytes.extend_from_slice(initial_bytes); bytes.extend_from_slice(initial_bytes);
$structname { bytes, count } $from_count_and_bytes(count, bytes)
} }
fn size(&self) -> usize { fn size(&self) -> usize {
@ -84,6 +84,13 @@ macro_rules! section_impl {
} }
} }
}; };
($structname: ident, $id: expr) => {
section_impl!($structname, $id, |count, bytes| $structname {
bytes,
count
});
};
} }
impl<'a, Sec> Serialize for Sec impl<'a, Sec> Serialize for Sec
@ -367,6 +374,7 @@ impl Serialize for Import {
#[derive(Debug)] #[derive(Debug)]
pub struct ImportSection<'a> { pub struct ImportSection<'a> {
pub count: u32, pub count: u32,
pub function_count: u32,
pub bytes: Vec<'a, u8>, pub bytes: Vec<'a, u8>,
} }
@ -376,7 +384,7 @@ impl<'a> ImportSection<'a> {
self.count += 1; self.count += 1;
} }
pub fn function_count(&self) -> u32 { fn update_function_count(&mut self) {
let mut f_count = 0; let mut f_count = 0;
let mut cursor = 0; let mut cursor = 0;
while cursor < self.bytes.len() { while cursor < self.bytes.len() {
@ -403,11 +411,21 @@ impl<'a> ImportSection<'a> {
} }
} }
f_count self.function_count = f_count;
}
pub fn from_count_and_bytes(count: u32, bytes: Vec<'a, u8>) -> Self {
let mut created = ImportSection {
bytes,
count,
function_count: 0,
};
created.update_function_count();
created
} }
} }
section_impl!(ImportSection, SectionId::Import); section_impl!(ImportSection, SectionId::Import, ImportSection::from_count_and_bytes);
/******************************************************************* /*******************************************************************
* *

View file

@ -14,7 +14,7 @@ pub trait Wasm32TestResult {
wrapper_name: &str, wrapper_name: &str,
main_function_index: u32, main_function_index: u32,
) { ) {
let index = module.import.function_count() let index = module.import.function_count
+ module.code.preloaded_count + module.code.preloaded_count
+ module.code.code_builders.len() as u32; + module.code.code_builders.len() as u32;