Merge pull request #71 from NikVolf/import_external_global

global import breaks with Function("missing exports with name STACKTOP")
This commit is contained in:
Nikolay Volf 2017-07-25 15:41:12 +03:00 committed by GitHub
commit b831ff783a
3 changed files with 38 additions and 28 deletions

View File

@ -81,7 +81,8 @@ pub struct EnvModuleInstance {
impl EnvModuleInstance { impl EnvModuleInstance {
pub fn new(params: EnvParams, module: Module) -> Result<Self, Error> { pub fn new(params: EnvParams, module: Module) -> Result<Self, Error> {
let instance = ModuleInstance::new(Weak::default(), "env".into(), module)?; let mut instance = ModuleInstance::new(Weak::default(), "env".into(), module)?;
instance.instantiate(false, None)?;
Ok(EnvModuleInstance { Ok(EnvModuleInstance {
_params: params, _params: params,
@ -186,7 +187,7 @@ pub fn env_module(params: EnvParams) -> Result<EnvModuleInstance, Error> {
.with_export(ExportEntry::new("DYNAMIC_BASE".into(), Internal::Global(INDEX_GLOBAL_DYNAMIC_BASE))) .with_export(ExportEntry::new("DYNAMIC_BASE".into(), Internal::Global(INDEX_GLOBAL_DYNAMIC_BASE)))
.with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, true), InitExpr::new(vec![Opcode::I32Const((DEFAULT_STACK_BASE + params.total_stack) as i32)]))) .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, true), InitExpr::new(vec![Opcode::I32Const((DEFAULT_STACK_BASE + params.total_stack) as i32)])))
.with_export(ExportEntry::new("DYNAMICTOP_PTR".into(), Internal::Global(INDEX_GLOBAL_DYNAMICTOP_PTR))) .with_export(ExportEntry::new("DYNAMICTOP_PTR".into(), Internal::Global(INDEX_GLOBAL_DYNAMICTOP_PTR)))
.with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, params.allow_memory_growth), InitExpr::new(vec![Opcode::I32Const(params.total_memory as i32)]))) .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, params.allow_memory_growth), InitExpr::new(vec![Opcode::I32Const(params.total_memory as i32)])))
.with_export(ExportEntry::new("TOTAL_MEMORY".into(), Internal::Global(INDEX_GLOBAL_TOTAL_MEMORY))) .with_export(ExportEntry::new("TOTAL_MEMORY".into(), Internal::Global(INDEX_GLOBAL_TOTAL_MEMORY)))
.with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, true), InitExpr::new(vec![Opcode::I32Const(0)]))) .with_global(GlobalEntry::new(GlobalType::new(ValueType::I32, true), InitExpr::new(vec![Opcode::I32Const(0)])))
.with_export(ExportEntry::new("ABORT".into(), Internal::Global(INDEX_GLOBAL_ABORT))) .with_export(ExportEntry::new("ABORT".into(), Internal::Global(INDEX_GLOBAL_ABORT)))

View File

@ -235,32 +235,30 @@ impl ModuleInstance {
} }
// validate export section // validate export section
if is_user_module { // TODO: env module exports STACKTOP global, which is mutable => check is failed if let Some(export_section) = self.module.export_section() {
if let Some(export_section) = self.module.export_section() { for export in export_section.entries() {
for export in export_section.entries() { match export.internal() {
match export.internal() { &Internal::Function(function_index) => {
&Internal::Function(function_index) => { self.require_function(ItemIndex::IndexSpace(function_index)).map(|_| ())?;
self.require_function(ItemIndex::IndexSpace(function_index)).map(|_| ())?; self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Function(function_index));
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Function(function_index)); },
}, &Internal::Global(global_index) => {
&Internal::Global(global_index) => { self.global(ItemIndex::IndexSpace(global_index), None)
self.global(ItemIndex::IndexSpace(global_index), None) .and_then(|g| if g.is_mutable() && is_user_module {
.and_then(|g| if g.is_mutable() { Err(Error::Validation(format!("trying to export mutable global {}", export.field())))
Err(Error::Validation(format!("trying to export mutable global {}", export.field()))) } else {
} else { Ok(())
Ok(()) })?;
})?; self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Global(global_index));
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Global(global_index)); },
}, &Internal::Memory(memory_index) => {
&Internal::Memory(memory_index) => { self.memory(ItemIndex::IndexSpace(memory_index)).map(|_| ())?;
self.memory(ItemIndex::IndexSpace(memory_index)).map(|_| ())?; self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Memory(memory_index));
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Memory(memory_index)); },
}, &Internal::Table(table_index) => {
&Internal::Table(table_index) => { self.table(ItemIndex::IndexSpace(table_index)).map(|_| ())?;
self.table(ItemIndex::IndexSpace(table_index)).map(|_| ())?; self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Table(table_index));
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Table(table_index)); },
},
}
} }
} }
} }

View File

@ -210,6 +210,17 @@ fn single_program_different_modules() {
assert_eq!(executor.values, vec![7, 57, 42]); assert_eq!(executor.values, vec![7, 57, 42]);
} }
#[test]
fn import_env_mutable_global() {
let program = ProgramInstance::new().unwrap();
let module = module()
.with_import(ImportEntry::new("env".into(), "STACKTOP".into(), External::Global(GlobalType::new(ValueType::I32, false))))
.build();
program.add_module("main", module, None).unwrap();
}
#[test] #[test]
fn env_native_export_entry_type_check() { fn env_native_export_entry_type_check() {
let program = ProgramInstance::new().unwrap(); let program = ProgramInstance::new().unwrap();