gen_wasm: correctly detect empty ElementSection and don't emit TableSection

This commit is contained in:
Brian Carroll 2022-07-13 21:22:49 +01:00
parent 2a963ca3ba
commit 06f8af6b1e
No known key found for this signature in database
GPG key ID: 9CF4E3BF9C4722C7
2 changed files with 12 additions and 4 deletions

View file

@ -64,7 +64,9 @@ impl<'a> WasmModule<'a> {
self.types.serialize(buffer);
self.import.serialize(buffer);
self.function.serialize(buffer);
self.table.serialize(buffer);
if !self.element.is_empty() {
self.table.serialize(buffer);
}
self.memory.serialize(buffer);
self.global.serialize(buffer);
self.export.serialize(buffer);

View file

@ -1118,6 +1118,10 @@ impl<'a> ElementSection<'a> {
pub fn size(&self) -> usize {
self.segments.iter().map(|seg| seg.size()).sum()
}
pub fn is_empty(&self) -> bool {
self.segments.iter().all(|seg| seg.fn_indices.is_empty())
}
}
impl<'a> Parse<&'a Bump> for ElementSection<'a> {
@ -1148,9 +1152,11 @@ impl<'a> Parse<&'a Bump> for ElementSection<'a> {
impl<'a> Serialize for ElementSection<'a> {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
let header_indices = write_section_header(buffer, Self::ID);
self.segments.serialize(buffer);
update_section_size(buffer, header_indices);
if !self.is_empty() {
let header_indices = write_section_header(buffer, Self::ID);
self.segments.serialize(buffer);
update_section_size(buffer, header_indices);
}
}
}