mirror of
https://github.com/fluencelabs/wasmer
synced 2025-04-01 15:31:04 +00:00
Added memory to emscripten env
This commit is contained in:
parent
c627fce9f2
commit
c2036c9695
@ -4,15 +4,20 @@ extern crate wasmer_runtime_core;
|
|||||||
use byteorder::{ByteOrder, LittleEndian};
|
use byteorder::{ByteOrder, LittleEndian};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use std::ptr;
|
||||||
use wasmer_runtime_core::{
|
use wasmer_runtime_core::{
|
||||||
export::{Context, Export, FuncPointer, GlobalPointer},
|
export::{Context, Export, FuncPointer, GlobalPointer, MemoryPointer},
|
||||||
import::{ImportObject, Namespace},
|
import::{ImportObject, Namespace},
|
||||||
memory::LinearMemory,
|
memory::LinearMemory,
|
||||||
types::{
|
types::{
|
||||||
FuncSig, GlobalDesc,
|
FuncSig, GlobalDesc,
|
||||||
Type::{self, *},
|
Type::{self, *},
|
||||||
|
Memory,
|
||||||
|
LocalMemoryIndex
|
||||||
},
|
},
|
||||||
|
structures::TypedIndex,
|
||||||
vm::LocalGlobal,
|
vm::LocalGlobal,
|
||||||
|
vm::LocalMemory,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
@ -133,6 +138,9 @@ macro_rules! global {
|
|||||||
|
|
||||||
pub struct EmscriptenGlobals<'a> {
|
pub struct EmscriptenGlobals<'a> {
|
||||||
pub data: HashMap<&'a str, HashMap<&'a str, (u64, Type)>>, // <namespace, <field_name, (global_value, type)>>
|
pub data: HashMap<&'a str, HashMap<&'a str, (u64, Type)>>, // <namespace, <field_name, (global_value, type)>>
|
||||||
|
// The emscripten memory
|
||||||
|
pub memory: LinearMemory,
|
||||||
|
pub vm_memory: LocalMemory,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> EmscriptenGlobals<'a> {
|
impl<'a> EmscriptenGlobals<'a> {
|
||||||
@ -141,6 +149,14 @@ impl<'a> EmscriptenGlobals<'a> {
|
|||||||
let mut env_namepace = HashMap::new();
|
let mut env_namepace = HashMap::new();
|
||||||
let mut global_namepace = HashMap::new();
|
let mut global_namepace = HashMap::new();
|
||||||
|
|
||||||
|
let memory_type = Memory {
|
||||||
|
min: 256,
|
||||||
|
max: Some(256),
|
||||||
|
shared: false,
|
||||||
|
};
|
||||||
|
let mut memory = LinearMemory::new(&memory_type);
|
||||||
|
let mut vm_memory = memory.into_vm_memory(LocalMemoryIndex::new(0));
|
||||||
|
|
||||||
env_namepace.insert("STACKTOP", (stacktop(STATIC_BUMP) as _, I32));
|
env_namepace.insert("STACKTOP", (stacktop(STATIC_BUMP) as _, I32));
|
||||||
env_namepace.insert("STACK_MAX", (stack_max(STATIC_BUMP) as _, I32));
|
env_namepace.insert("STACK_MAX", (stack_max(STATIC_BUMP) as _, I32));
|
||||||
env_namepace.insert("DYNAMICTOP_PTR", (dynamictop_ptr(STATIC_BUMP) as _, I32));
|
env_namepace.insert("DYNAMICTOP_PTR", (dynamictop_ptr(STATIC_BUMP) as _, I32));
|
||||||
@ -151,11 +167,11 @@ impl<'a> EmscriptenGlobals<'a> {
|
|||||||
data.insert("env", env_namepace);
|
data.insert("env", env_namepace);
|
||||||
data.insert("global", global_namepace);
|
data.insert("global", global_namepace);
|
||||||
|
|
||||||
Self { data }
|
Self { data, memory, vm_memory }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> ImportObject {
|
pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject {
|
||||||
let mut imports = ImportObject::new();
|
let mut imports = ImportObject::new();
|
||||||
let mut env_namespace = Namespace::new();
|
let mut env_namespace = Namespace::new();
|
||||||
let mut asm_namespace = Namespace::new();
|
let mut asm_namespace = Namespace::new();
|
||||||
@ -167,6 +183,26 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> ImportObject {
|
|||||||
let env_globals = globals.data.get("env").unwrap();
|
let env_globals = globals.data.get("env").unwrap();
|
||||||
let global_globals = globals.data.get("global").unwrap();
|
let global_globals = globals.data.get("global").unwrap();
|
||||||
|
|
||||||
|
// Memory
|
||||||
|
|
||||||
|
let local_memory = unsafe {
|
||||||
|
MemoryPointer::new(&mut globals.vm_memory)
|
||||||
|
};
|
||||||
|
|
||||||
|
env_namespace.insert(
|
||||||
|
"memory".to_string(),
|
||||||
|
Export::Memory {
|
||||||
|
local: local_memory,
|
||||||
|
// We generate a fake Context that traps on access
|
||||||
|
ctx: Context::External(ptr::null_mut()),
|
||||||
|
memory: Memory {
|
||||||
|
min: 256,
|
||||||
|
max: Some(256),
|
||||||
|
shared: false,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("STACKTOP").unwrap();
|
let (value, ty) = env_globals.get("STACKTOP").unwrap();
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"STACKTOP".to_string(),
|
"STACKTOP".to_string(),
|
||||||
|
@ -16,8 +16,8 @@ macro_rules! assert_emscripten_output {
|
|||||||
// let module = compile(&wasm_bytes[..])
|
// let module = compile(&wasm_bytes[..])
|
||||||
// .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
|
// .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
|
||||||
|
|
||||||
let emscripten_globals = EmscriptenGlobals::new();
|
let mut emscripten_globals = EmscriptenGlobals::new();
|
||||||
let import_object = generate_emscripten_env(&emscripten_globals);
|
let import_object = generate_emscripten_env(&mut emscripten_globals);
|
||||||
|
|
||||||
let mut instance = module.instantiate(import_object)
|
let mut instance = module.instantiate(import_object)
|
||||||
.map_err(|err| format!("Can't instantiate the WebAssembly module: {:?}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
|
.map_err(|err| format!("Can't instantiate the WebAssembly module: {:?}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ??
|
||||||
|
@ -44,7 +44,7 @@ impl LinearMemory {
|
|||||||
/// Create a new linear memory instance with specified initial and maximum number of pages.
|
/// Create a new linear memory instance with specified initial and maximum number of pages.
|
||||||
///
|
///
|
||||||
/// `maximum` cannot be set to more than `65536` pages.
|
/// `maximum` cannot be set to more than `65536` pages.
|
||||||
pub(crate) fn new(mem: &Memory) -> Self {
|
pub fn new(mem: &Memory) -> Self {
|
||||||
assert!(mem.min <= Self::MAX_PAGES);
|
assert!(mem.min <= Self::MAX_PAGES);
|
||||||
assert!(mem.max.is_none() || mem.max.unwrap() <= Self::MAX_PAGES);
|
assert!(mem.max.is_none() || mem.max.unwrap() <= Self::MAX_PAGES);
|
||||||
debug!("Instantiate LinearMemory(mem: {:?})", mem);
|
debug!("Instantiate LinearMemory(mem: {:?})", mem);
|
||||||
@ -108,7 +108,7 @@ impl LinearMemory {
|
|||||||
self.max.unwrap_or(Self::MAX_PAGES)
|
self.max.unwrap_or(Self::MAX_PAGES)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn into_vm_memory(&mut self, index: LocalMemoryIndex) -> vm::LocalMemory {
|
pub fn into_vm_memory(&mut self, index: LocalMemoryIndex) -> vm::LocalMemory {
|
||||||
vm::LocalMemory {
|
vm::LocalMemory {
|
||||||
base: self.base(),
|
base: self.base(),
|
||||||
size: self.size(),
|
size: self.size(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user