Add table to the emscripten env

This commit is contained in:
Syrus
2019-01-23 12:34:56 -08:00
parent c2036c9695
commit 70c512ecb3

View File

@ -6,18 +6,22 @@ use hashbrown::HashMap;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use wasmer_runtime_core::{ use wasmer_runtime_core::{
export::{Context, Export, FuncPointer, GlobalPointer, MemoryPointer}, export::{Context, Export, FuncPointer, GlobalPointer, MemoryPointer, TablePointer},
import::{ImportObject, Namespace}, import::{ImportObject, Namespace},
memory::LinearMemory, memory::LinearMemory,
table::TableBacking,
types::{ types::{
FuncSig, GlobalDesc, FuncSig, GlobalDesc,
Type::{self, *}, Type::{self, *},
Memory, Memory,
LocalMemoryIndex LocalMemoryIndex,
ElementType,
Table,
}, },
structures::TypedIndex, structures::TypedIndex,
vm::LocalGlobal, vm::LocalGlobal,
vm::LocalMemory, vm::LocalMemory,
vm::LocalTable,
}; };
#[macro_use] #[macro_use]
@ -141,6 +145,9 @@ pub struct EmscriptenGlobals<'a> {
// The emscripten memory // The emscripten memory
pub memory: LinearMemory, pub memory: LinearMemory,
pub vm_memory: LocalMemory, pub vm_memory: LocalMemory,
// The emscripten table
pub table: TableBacking,
pub vm_table: LocalTable,
} }
impl<'a> EmscriptenGlobals<'a> { impl<'a> EmscriptenGlobals<'a> {
@ -149,6 +156,7 @@ 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();
// Memory initialization
let memory_type = Memory { let memory_type = Memory {
min: 256, min: 256,
max: Some(256), max: Some(256),
@ -157,6 +165,14 @@ impl<'a> EmscriptenGlobals<'a> {
let mut memory = LinearMemory::new(&memory_type); let mut memory = LinearMemory::new(&memory_type);
let mut vm_memory = memory.into_vm_memory(LocalMemoryIndex::new(0)); let mut vm_memory = memory.into_vm_memory(LocalMemoryIndex::new(0));
let table_type = Table {
ty: ElementType::Anyfunc,
min: 10,
max: Some(10),
};
let mut table = TableBacking::new(&table_type);
let mut vm_table = table.into_vm_table();
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));
@ -167,7 +183,7 @@ 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, memory, vm_memory } Self { data, memory, vm_memory, table, vm_table }
} }
} }
@ -183,8 +199,10 @@ pub fn generate_emscripten_env(globals: &mut 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 // We generate a fake Context that traps on access
let null_ctx = Context::External(ptr::null_mut());
// Memory
let local_memory = unsafe { let local_memory = unsafe {
MemoryPointer::new(&mut globals.vm_memory) MemoryPointer::new(&mut globals.vm_memory)
}; };
@ -193,8 +211,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
"memory".to_string(), "memory".to_string(),
Export::Memory { Export::Memory {
local: local_memory, local: local_memory,
// We generate a fake Context that traps on access ctx: null_ctx,
ctx: Context::External(ptr::null_mut()),
memory: Memory { memory: Memory {
min: 256, min: 256,
max: Some(256), max: Some(256),
@ -203,6 +220,25 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
}, },
); );
// Table
let local_table = unsafe {
TablePointer::new(&mut globals.vm_table)
};
env_namespace.insert(
"table".to_string(),
Export::Table {
local: local_table,
// We generate a fake Context that traps on access
ctx: null_ctx,
table: Table {
ty: ElementType::Anyfunc,
min: 10,
max: Some(10),
}
},
);
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(),