From ba6828c3d4d947a4ee01981fd013d55995444126 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Fri, 11 Jan 2019 17:10:59 +0100 Subject: [PATCH] Add memory offset addr impl --- lib/runtime/src/instance.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index e9b23a7bd..b102832ab 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -344,7 +344,24 @@ impl Namespace for Instance { // TODO Remove this later, only needed for compilation till emscripten is updated impl Instance { - pub fn memory_offset_addr(&self, _index: usize, _offset: usize) -> *const usize { - unimplemented!("TODO replace this emscripten stub") + pub fn memory_offset_addr(&self, index: usize, offset: usize) -> *const u8 { + // Get vm context. + let vmctx = &self.vmctx; + + // Check if the index specified refers to an imported memory. + if index < self.module.0.imported_memories.len() { + // Get imported memory at specified index. + let memory = unsafe { vmctx.imported_memories.add(index) }; + + // Get the actual address of the specified offset. + return unsafe { (*(*memory).memory).base.add(offset) } + } + + // Index points to a locally-defined memory + // Get local memory at specified index. + let memory = unsafe { vmctx.memories.add(index) }; + + // Get the actual address of the specified offset. + unsafe { (*memory).base.add(offset) } } }