mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-23 13:41:32 +00:00
Simplified storage of emscripten globals data
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -517,7 +517,6 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"hashbrown 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"libc 0.2.44 (git+https://github.com/rust-lang/libc)",
|
"libc 0.2.44 (git+https://github.com/rust-lang/libc)",
|
||||||
"time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
|
"time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"wabt 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"wabt 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -9,7 +9,6 @@ edition = "2018"
|
|||||||
build = "build/mod.rs"
|
build = "build/mod.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
hashbrown = "0.1"
|
|
||||||
wasmer-runtime-core = { path = "../runtime-core", version = "0.1.0" }
|
wasmer-runtime-core = { path = "../runtime-core", version = "0.1.0" }
|
||||||
libc = { git = "https://github.com/rust-lang/libc" }
|
libc = { git = "https://github.com/rust-lang/libc" }
|
||||||
byteorder = "1"
|
byteorder = "1"
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
extern crate wasmer_runtime_core;
|
extern crate wasmer_runtime_core;
|
||||||
|
|
||||||
use byteorder::{ByteOrder, LittleEndian};
|
use byteorder::{ByteOrder, LittleEndian};
|
||||||
use hashbrown::HashMap;
|
|
||||||
use libc::c_int;
|
use libc::c_int;
|
||||||
use std::cell::UnsafeCell;
|
use std::cell::UnsafeCell;
|
||||||
use std::{ffi::c_void, mem, ptr};
|
use std::{ffi::c_void, mem, ptr};
|
||||||
@ -332,14 +331,31 @@ macro_rules! global {
|
|||||||
unsafe {
|
unsafe {
|
||||||
GlobalPointer::new(
|
GlobalPointer::new(
|
||||||
// NOTE: Taking a shortcut here. LocalGlobal is a struct containing just u64.
|
// NOTE: Taking a shortcut here. LocalGlobal is a struct containing just u64.
|
||||||
std::mem::transmute::<&u64, *mut LocalGlobal>($value),
|
std::mem::transmute::<&u64, *mut LocalGlobal>(&$value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EmscriptenGlobals<'a> {
|
type EmscriptenDataType = (u64, Type);
|
||||||
pub data: HashMap<&'a str, HashMap<&'a str, (u64, Type)>>, // <namespace, <field_name, (global_value, type)>>
|
|
||||||
|
pub struct EmscriptenGlobalsData {
|
||||||
|
// Env namespace
|
||||||
|
stacktop: EmscriptenDataType,
|
||||||
|
stack_max: EmscriptenDataType,
|
||||||
|
dynamictop_ptr: EmscriptenDataType,
|
||||||
|
memory_base: EmscriptenDataType,
|
||||||
|
table_base: EmscriptenDataType,
|
||||||
|
temp_double_ptr: EmscriptenDataType,
|
||||||
|
|
||||||
|
// Global namespace
|
||||||
|
infinity: EmscriptenDataType,
|
||||||
|
nan: EmscriptenDataType,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EmscriptenGlobals {
|
||||||
|
// The emscripten data
|
||||||
|
pub data: EmscriptenGlobalsData,
|
||||||
// The emscripten memory
|
// The emscripten memory
|
||||||
pub memory: LinearMemory,
|
pub memory: LinearMemory,
|
||||||
pub vm_memory: LocalMemory,
|
pub vm_memory: LocalMemory,
|
||||||
@ -348,12 +364,8 @@ pub struct EmscriptenGlobals<'a> {
|
|||||||
pub vm_table: LocalTable,
|
pub vm_table: LocalTable,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> EmscriptenGlobals<'a> {
|
impl<'a> EmscriptenGlobals {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut data = HashMap::new();
|
|
||||||
let mut env_namepace = HashMap::new();
|
|
||||||
let mut global_namepace = HashMap::new();
|
|
||||||
|
|
||||||
// Memory initialization
|
// Memory initialization
|
||||||
let memory_type = Memory {
|
let memory_type = Memory {
|
||||||
min: 256,
|
min: 256,
|
||||||
@ -374,18 +386,19 @@ impl<'a> EmscriptenGlobals<'a> {
|
|||||||
let memory_base = (STATIC_BASE as u64, I32);
|
let memory_base = (STATIC_BASE as u64, I32);
|
||||||
let table_base = (0 as u64, I32);
|
let table_base = (0 as u64, I32);
|
||||||
let temp_double_ptr = (0 as u64, I32);
|
let temp_double_ptr = (0 as u64, I32);
|
||||||
|
let data = EmscriptenGlobalsData {
|
||||||
|
// env
|
||||||
|
stacktop: (stacktop(STATIC_BUMP) as _, I32),
|
||||||
|
stack_max: (stack_max(STATIC_BUMP) as _, I32),
|
||||||
|
dynamictop_ptr: (dynamictop_ptr(STATIC_BUMP) as _, I32),
|
||||||
|
memory_base: memory_base,
|
||||||
|
table_base: table_base,
|
||||||
|
temp_double_ptr: temp_double_ptr,
|
||||||
|
|
||||||
env_namepace.insert("STACKTOP", (stacktop(STATIC_BUMP) as _, I32));
|
// global
|
||||||
env_namepace.insert("STACK_MAX", (stack_max(STATIC_BUMP) as _, I32));
|
infinity: (std::f64::INFINITY.to_bits() as _, F64),
|
||||||
env_namepace.insert("DYNAMICTOP_PTR", (dynamictop_ptr(STATIC_BUMP) as _, I32));
|
nan: (std::f64::NAN.to_bits() as _, F64),
|
||||||
env_namepace.insert("memoryBase", memory_base);
|
};
|
||||||
env_namepace.insert("tableBase", table_base);
|
|
||||||
env_namepace.insert("tempDoublePtr", temp_double_ptr);
|
|
||||||
global_namepace.insert("Infinity", (std::f64::INFINITY.to_bits() as _, F64));
|
|
||||||
global_namepace.insert("NaN", (std::f64::NAN.to_bits() as _, F64));
|
|
||||||
|
|
||||||
data.insert("env", env_namepace);
|
|
||||||
data.insert("global", global_namepace);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
data,
|
data,
|
||||||
@ -406,8 +419,6 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
|
|
||||||
// Add globals.
|
// Add globals.
|
||||||
// NOTE: There is really no need for checks, these globals should always be available.
|
// NOTE: There is really no need for checks, these globals should always be available.
|
||||||
let env_globals = globals.data.get("env").unwrap();
|
|
||||||
let global_globals = globals.data.get("global").unwrap();
|
|
||||||
|
|
||||||
// We generate a fake Context that traps on access
|
// We generate a fake Context that traps on access
|
||||||
let null_ctx = Context::External(ptr::null_mut());
|
let null_ctx = Context::External(ptr::null_mut());
|
||||||
@ -445,7 +456,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("STACKTOP").unwrap();
|
let (value, ty) = globals.data.stacktop;
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"STACKTOP".to_string(),
|
"STACKTOP".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -457,7 +468,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("STACK_MAX").unwrap();
|
let (value, ty) = globals.data.stack_max;
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"STACK_MAX".to_string(),
|
"STACK_MAX".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -469,7 +480,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("DYNAMICTOP_PTR").unwrap();
|
let (value, ty) = globals.data.dynamictop_ptr;
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"DYNAMICTOP_PTR".to_string(),
|
"DYNAMICTOP_PTR".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -481,7 +492,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("tableBase").unwrap();
|
let (value, ty) = globals.data.table_base;
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"tableBase".to_string(),
|
"tableBase".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -493,7 +504,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("tableBase").unwrap();
|
let (value, ty) = globals.data.table_base;
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"__table_base".to_string(),
|
"__table_base".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -505,7 +516,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("memoryBase").unwrap();
|
let (value, ty) = globals.data.memory_base;
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"memoryBase".to_string(),
|
"memoryBase".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -517,7 +528,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("memoryBase").unwrap();
|
let (value, ty) = globals.data.memory_base;
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"__memory_base".to_string(),
|
"__memory_base".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -529,7 +540,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = env_globals.get("tempDoublePtr").unwrap();
|
let (value, ty) = globals.data.temp_double_ptr;
|
||||||
env_namespace.insert(
|
env_namespace.insert(
|
||||||
"tempDoublePtr".to_string(),
|
"tempDoublePtr".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -541,7 +552,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = global_globals.get("Infinity").unwrap();
|
let (value, ty) = globals.data.infinity;
|
||||||
global_namespace.insert(
|
global_namespace.insert(
|
||||||
"Infinity".to_string(),
|
"Infinity".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
@ -553,7 +564,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let (value, ty) = global_globals.get("NaN").unwrap();
|
let (value, ty) = globals.data.nan;
|
||||||
global_namespace.insert(
|
global_namespace.insert(
|
||||||
"NaN".to_string(),
|
"NaN".to_string(),
|
||||||
Export::Global {
|
Export::Global {
|
||||||
|
Reference in New Issue
Block a user