Get rid of some overcomplicated Data section stuff

Currently a Wasm module can only have one memory
The Data Segment spec allows for more in future
But the format is confusing enough already without that!
Let's just get rid of that complexity, we don't need it.
This commit is contained in:
Brian Carroll 2021-11-04 19:44:10 +00:00
parent c57d99c1ac
commit 1b91fd9533

View file

@ -476,18 +476,11 @@ impl<'a> CodeSection<'a> {
pub enum DataMode { pub enum DataMode {
/// A data segment that auto-initialises on instantiation /// A data segment that auto-initialises on instantiation
Active { memidx: u32, offset: ConstExpr }, Active { offset: ConstExpr },
/// A data segment that can be initialised with the `memory.init` instruction /// A data segment that can be initialised with the `memory.init` instruction
Passive, Passive,
} }
#[repr(u8)]
enum DataModeEncoding {
DefaultMemoryActive = 0,
Passive = 1,
ExplicitMemoryActive = 2,
}
pub struct DataSegment<'a> { pub struct DataSegment<'a> {
mode: DataMode, mode: DataMode,
init: Vec<'a, u8>, init: Vec<'a, u8>,
@ -496,17 +489,12 @@ pub struct DataSegment<'a> {
impl Serialize for DataSegment<'_> { impl Serialize for DataSegment<'_> {
fn serialize<T: SerialBuffer>(&self, buffer: &mut T) { fn serialize<T: SerialBuffer>(&self, buffer: &mut T) {
match &self.mode { match &self.mode {
DataMode::Active { memidx, offset } => { DataMode::Active { offset } => {
if *memidx == 0 { buffer.append_u8(0);
buffer.append_u8(DataModeEncoding::DefaultMemoryActive as u8);
} else {
buffer.append_u8(DataModeEncoding::ExplicitMemoryActive as u8);
buffer.encode_u32(*memidx);
}
offset.serialize(buffer); offset.serialize(buffer);
} }
DataMode::Passive => { DataMode::Passive => {
buffer.append_u8(DataModeEncoding::Passive as u8); buffer.append_u8(1);
} }
} }