From a77ca2a9743df380f3c06d7640b24520b3dffc91 Mon Sep 17 00:00:00 2001 From: fro Date: Thu, 5 Apr 2018 21:32:20 +0300 Subject: [PATCH] ensure here that module has the DataSection --- src/pack.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/pack.rs b/src/pack.rs index 1906dbd..a623375 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -1,6 +1,6 @@ use std::fmt; use parity_wasm::elements::{ - self, Section, Opcode, DataSegment, InitExpr, Internal, External, + self, Section, DataSection, Opcode, DataSegment, InitExpr, Internal, External, ImportCountType, }; use parity_wasm::builder; @@ -146,12 +146,21 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> // If new function is put in ctor module, it will have this callable index let last_function_index = ctor_module.functions_space(); + // We ensure here that module has the DataSection + if ctor_module + .sections() + .iter() + .find(|section| match **section { Section::Data(ref _d) => true, _ => false }) + .is_none() { + // DataSection has to be the last non-custom section according the to the spec + ctor_module.sections_mut().push(Section::Data(DataSection::with_entries(vec![]))); + } + // Code data address is an address where we put the contract's code (raw_module) let mut code_data_address = 0i32; for section in ctor_module.sections_mut() { match section { - // TODO: add data section is there no one &mut Section::Data(ref mut data_section) => { let (index, offset) = if let Some(ref entry) = data_section.entries().iter().last() { if let Opcode::I32Const(offst) = entry.offset().code()[0] {