Fix globals

This commit is contained in:
Steve Akinyemi
2019-01-17 23:10:29 +01:00
parent 0bf6ce49f6
commit 57fbfd58d3
2 changed files with 91 additions and 20 deletions

View File

@ -119,6 +119,17 @@ macro_rules! func {
}}; }};
} }
macro_rules! global {
($value:ident) => {{
unsafe {
GlobalPointer::new(
// NOTE: Taking a shortcut here. LocalGlobal is a struct containing just u64.
std::mem::transmute::<&u64, *mut LocalGlobal>($value)
)
}
}};
}
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)>>
} }
@ -149,24 +160,84 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports {
let mut imports = Imports::new(); let mut imports = Imports::new();
let mut env_namespace = NamespaceMap::new(); let mut env_namespace = NamespaceMap::new();
let mut asm_namespace = NamespaceMap::new(); let mut asm_namespace = NamespaceMap::new();
let mut global_namespace = NamespaceMap::new();
// Add globals. // Add globals.
// for () // 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();
// for (name, global, desc) in &globals.data { let (value, ty) = env_globals.get("STACKTOP").unwrap();
// let global_ptr = unsafe { env_namespace.insert(
// GlobalPointer::new( "STACKTOP".to_string(),
// std::mem::transmute::<&LocalGlobal, *mut LocalGlobal>(global) Export::Global {
// ) local: global!(value),
// }; global: GlobalDesc {
mutable: false,
ty: ty.clone(),
}
},
);
// let export = Export::Global { let (value, ty) = env_globals.get("STACK_MAX").unwrap();
// local: global_ptr, env_namespace.insert(
// global: desc.clone(), "STACK_MAX".to_string(),
// }; Export::Global {
local: global!(value),
global: GlobalDesc {
mutable: false,
ty: ty.clone(),
}
},
);
// imports.register_export("env", name.clone(), export); let (value, ty) = env_globals.get("DYNAMICTOP_PTR").unwrap();
// } env_namespace.insert(
"DYNAMICTOP_PTR".to_string(),
Export::Global {
local: global!(value),
global: GlobalDesc {
mutable: false,
ty: ty.clone(),
}
},
);
let (value, ty) = env_globals.get("tableBase").unwrap();
env_namespace.insert(
"tableBase".to_string(),
Export::Global {
local: global!(value),
global: GlobalDesc {
mutable: false,
ty: ty.clone(),
}
},
);
let (value, ty) = global_globals.get("Infinity").unwrap();
global_namespace.insert(
"Infinity".to_string(),
Export::Global {
local: global!(value),
global: GlobalDesc {
mutable: false,
ty: ty.clone(),
}
},
);
let (value, ty) = global_globals.get("NaN").unwrap();
global_namespace.insert(
"NaN".to_string(),
Export::Global {
local: global!(value),
global: GlobalDesc {
mutable: false,
ty: ty.clone(),
}
},
);
// Print function // Print function
env_namespace.insert( env_namespace.insert(
@ -193,6 +264,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports {
}, },
}, },
); );
// Lock // Lock
env_namespace.insert( env_namespace.insert(
"___lock", "___lock",

View File

@ -9,13 +9,12 @@ use std::slice;
use std::sync::Arc; use std::sync::Arc;
/// We check if a provided module is an Emscripten generated one /// We check if a provided module is an Emscripten generated one
pub fn is_emscripten_module(module: &Arc<Module>) -> bool { pub fn is_emscripten_module(module: &Arc<Module>) -> bool {
// for (_, import_name) in &module.imported_functions { for (_, import_name) in &module.0.imported_functions {
// if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" {
// return true; return true;
// } }
// } }
// false false
true
} }
pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, instance: &Instance) -> u32 { pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, instance: &Instance) -> u32 {