diff --git a/compiler/gen_wasm/src/backend.rs b/compiler/gen_wasm/src/backend.rs index 854531f78a..24a3b9c301 100644 --- a/compiler/gen_wasm/src/backend.rs +++ b/compiler/gen_wasm/src/backend.rs @@ -23,7 +23,7 @@ use crate::wasm_module::linking::{ }; use crate::wasm_module::sections::{ CodeSection, DataMode, DataSection, DataSegment, ExportSection, FunctionSection, GlobalSection, - Import, ImportDesc, ImportSection, MemorySection, TypeSection, WasmModule, + Import, ImportDesc, ImportSection, MemorySection, OpaqueSection, TypeSection, WasmModule, }; use crate::wasm_module::{ code_builder, CodeBuilder, ConstExpr, Export, ExportType, Global, GlobalType, LocalId, @@ -109,14 +109,14 @@ impl<'a> WasmBackend<'a> { types: TypeSection::new(arena, num_procs), import: ImportSection::new(arena), function: FunctionSection::new(arena, num_procs), - table: (), + table: OpaqueSection::default(), memory: MemorySection::new(MEMORY_INIT_SIZE), global: GlobalSection { entries: bumpalo::vec![in arena; stack_pointer], }, export: ExportSection { entries: exports }, - start: (), - element: (), + start: OpaqueSection::default(), + element: OpaqueSection::default(), code: CodeSection { preloaded_count: 0, preloaded_bytes: Vec::with_capacity_in(0, arena), diff --git a/compiler/gen_wasm/src/wasm_module/sections.rs b/compiler/gen_wasm/src/wasm_module/sections.rs index 9823f3c144..bc8b5dc363 100644 --- a/compiler/gen_wasm/src/wasm_module/sections.rs +++ b/compiler/gen_wasm/src/wasm_module/sections.rs @@ -627,20 +627,44 @@ impl SectionCounter { } } +/// A Wasm module section that we don't use for Roc code, +/// but may be present in a preloaded binary +#[derive(Debug)] +pub struct OpaqueSection<'a> { + bytes: &'a [u8], +} + +impl<'a> OpaqueSection<'a> { + pub fn new(bytes: &'a [u8]) -> Self { + Self { bytes } + } +} + +impl<'a> Default for OpaqueSection<'a> { + fn default() -> Self { + Self { bytes: &[] } + } +} + +impl Serialize for OpaqueSection<'_> { + fn serialize(&self, buffer: &mut T) { + if !self.bytes.is_empty() { + buffer.append_slice(&self.bytes); + } + } +} + #[derive(Debug)] pub struct WasmModule<'a> { pub types: TypeSection<'a>, pub import: ImportSection<'a>, pub function: FunctionSection<'a>, - /// Dummy placeholder for tables (used for function pointers and host references) - pub table: (), + pub table: OpaqueSection<'a>, pub memory: MemorySection, pub global: GlobalSection<'a>, pub export: ExportSection<'a>, - /// Dummy placeholder for start function. In Roc, this would be part of the platform. - pub start: (), - /// Dummy placeholder for table elements. Roc does not use tables. - pub element: (), + pub start: OpaqueSection<'a>, + pub element: OpaqueSection<'a>, pub code: CodeSection<'a>, pub data: DataSection<'a>, pub linking: LinkingSection<'a>,