Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #71 from NikVolf/import_external_global
Browse files Browse the repository at this point in the history
global import breaks with Function("missing exports with name STACKTOP")
  • Loading branch information
NikVolf authored Jul 25, 2017
2 parents d78b8c6 + 2f73f02 commit b831ff7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/interpreter/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ pub struct EnvModuleInstance {

impl EnvModuleInstance {
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 {
_params: params,
Expand Down Expand Up @@ -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_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_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_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)))
Expand Down
50 changes: 24 additions & 26 deletions src/interpreter/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,32 +235,30 @@ impl ModuleInstance {
}

// 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() {
for export in export_section.entries() {
match export.internal() {
&Internal::Function(function_index) => {
self.require_function(ItemIndex::IndexSpace(function_index)).map(|_| ())?;
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Function(function_index));
},
&Internal::Global(global_index) => {
self.global(ItemIndex::IndexSpace(global_index), None)
.and_then(|g| if g.is_mutable() {
Err(Error::Validation(format!("trying to export mutable global {}", export.field())))
} else {
Ok(())
})?;
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Global(global_index));
},
&Internal::Memory(memory_index) => {
self.memory(ItemIndex::IndexSpace(memory_index)).map(|_| ())?;
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Memory(memory_index));
},
&Internal::Table(table_index) => {
self.table(ItemIndex::IndexSpace(table_index)).map(|_| ())?;
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Table(table_index));
},
}
if let Some(export_section) = self.module.export_section() {
for export in export_section.entries() {
match export.internal() {
&Internal::Function(function_index) => {
self.require_function(ItemIndex::IndexSpace(function_index)).map(|_| ())?;
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Function(function_index));
},
&Internal::Global(global_index) => {
self.global(ItemIndex::IndexSpace(global_index), None)
.and_then(|g| if g.is_mutable() && is_user_module {
Err(Error::Validation(format!("trying to export mutable global {}", export.field())))
} else {
Ok(())
})?;
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Global(global_index));
},
&Internal::Memory(memory_index) => {
self.memory(ItemIndex::IndexSpace(memory_index)).map(|_| ())?;
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Memory(memory_index));
},
&Internal::Table(table_index) => {
self.table(ItemIndex::IndexSpace(table_index)).map(|_| ())?;
self.exports.entry(export.field().into()).or_insert_with(Default::default).push(Internal::Table(table_index));
},
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/interpreter/tests/basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ fn single_program_different_modules() {
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]
fn env_native_export_entry_type_check() {
let program = ProgramInstance::new().unwrap();
Expand Down

0 comments on commit b831ff7

Please sign in to comment.