Updated compatibility with latest emscripten

This commit is contained in:
Syrus 2019-07-06 17:15:35 -07:00
parent 336b5459bd
commit 24e7c1b263
3 changed files with 39 additions and 16 deletions

View File

@ -22,11 +22,11 @@ use wasmer_runtime_core::{
}; };
pub fn call_malloc(ctx: &mut Ctx, size: u32) -> u32 { pub fn call_malloc(ctx: &mut Ctx, size: u32) -> u32 {
get_emscripten_data(ctx).malloc.call(size).unwrap() get_emscripten_data(ctx).malloc.as_ref().unwrap().call(size).unwrap()
} }
pub fn call_malloc_with_cast<T: Copy, Ty>(ctx: &mut Ctx, size: u32) -> WasmPtr<T, Ty> { pub fn call_malloc_with_cast<T: Copy, Ty>(ctx: &mut Ctx, size: u32) -> WasmPtr<T, Ty> {
WasmPtr::new(get_emscripten_data(ctx).malloc.call(size).unwrap()) WasmPtr::new(get_emscripten_data(ctx).malloc.as_ref().unwrap().call(size).unwrap())
} }
pub fn call_memalign(ctx: &mut Ctx, alignment: u32, size: u32) -> u32 { pub fn call_memalign(ctx: &mut Ctx, alignment: u32, size: u32) -> u32 {
@ -40,6 +40,8 @@ pub fn call_memalign(ctx: &mut Ctx, alignment: u32, size: u32) -> u32 {
pub fn call_memset(ctx: &mut Ctx, pointer: u32, value: u32, size: u32) -> u32 { pub fn call_memset(ctx: &mut Ctx, pointer: u32, value: u32, size: u32) -> u32 {
get_emscripten_data(ctx) get_emscripten_data(ctx)
.memset .memset
.as_ref()
.unwrap()
.call(pointer, value, size) .call(pointer, value, size)
.unwrap() .unwrap()
} }

View File

@ -84,11 +84,11 @@ const GLOBAL_BASE: u32 = 1024;
const STATIC_BASE: u32 = GLOBAL_BASE; const STATIC_BASE: u32 = GLOBAL_BASE;
pub struct EmscriptenData<'a> { pub struct EmscriptenData<'a> {
pub malloc: Func<'a, u32, u32>, pub malloc: Option<Func<'a, u32, u32>>,
pub free: Func<'a, u32>, pub free: Option<Func<'a, u32>>,
pub memalign: Option<Func<'a, (u32, u32), u32>>, pub memalign: Option<Func<'a, (u32, u32), u32>>,
pub memset: Func<'a, (u32, u32, u32), u32>, pub memset: Option<Func<'a, (u32, u32, u32), u32>>,
pub stack_alloc: Func<'a, u32, u32>, pub stack_alloc: Option<Func<'a, u32, u32>>,
pub jumps: Vec<UnsafeCell<[u32; 27]>>, pub jumps: Vec<UnsafeCell<[u32; 27]>>,
pub opened_dirs: HashMap<i32, Box<*mut libcDIR>>, pub opened_dirs: HashMap<i32, Box<*mut libcDIR>>,
@ -164,11 +164,11 @@ impl<'a> EmscriptenData<'a> {
instance: &'a mut Instance, instance: &'a mut Instance,
mapped_dirs: HashMap<String, PathBuf>, mapped_dirs: HashMap<String, PathBuf>,
) -> EmscriptenData<'a> { ) -> EmscriptenData<'a> {
let malloc = instance.func("_malloc").unwrap(); let malloc = instance.func("_malloc").or(instance.func("malloc")).ok();
let free = instance.func("_free").unwrap(); let free = instance.func("_free").or(instance.func("free")).ok();
let memalign = instance.func("_memalign").ok(); let memalign = instance.func("_memalign").or(instance.func("memalign")).ok();
let memset = instance.func("_memset").unwrap(); let memset = instance.func("_memset").or(instance.func("memset")).ok();
let stack_alloc = instance.func("stackAlloc").unwrap(); let stack_alloc = instance.func("stackAlloc").ok();
let dyn_call_i = instance.func("dynCall_i").ok(); let dyn_call_i = instance.func("dynCall_i").ok();
let dyn_call_ii = instance.func("dynCall_ii").ok(); let dyn_call_ii = instance.func("dynCall_ii").ok();
@ -227,7 +227,7 @@ impl<'a> EmscriptenData<'a> {
let stack_save = instance.func("stackSave").ok(); let stack_save = instance.func("stackSave").ok();
let stack_restore = instance.func("stackRestore").ok(); let stack_restore = instance.func("stackRestore").ok();
let set_threw = instance.func("_setThrew").ok(); let set_threw = instance.func("_setThrew").or(instance.func("setThrew")).ok();
EmscriptenData { EmscriptenData {
malloc, malloc,
@ -332,17 +332,27 @@ pub fn run_emscripten_instance(
//let (argc, argv) = store_module_arguments(instance.context_mut(), args); //let (argc, argv) = store_module_arguments(instance.context_mut(), args);
instance.call(&ep, &[Value::I32(arg as i32)])?; instance.call(&ep, &[Value::I32(arg as i32)])?;
} else { } else {
let main_func = instance.dyn_func("_main")?; let (func_name, main_func) = match instance.dyn_func("_main") {
Ok(func) => {
Ok(("_main", func))
},
Err(_e) => {
match instance.dyn_func("main") {
Ok(func) => Ok(("main", func)),
Err(e) => Err(e)
}
}
}?;
let num_params = main_func.signature().params().len(); let num_params = main_func.signature().params().len();
let _result = match num_params { let _result = match num_params {
2 => { 2 => {
let mut new_args = vec![path]; let mut new_args = vec![path];
new_args.extend(args); new_args.extend(args);
let (argc, argv) = store_module_arguments(instance.context_mut(), new_args); let (argc, argv) = store_module_arguments(instance.context_mut(), new_args);
instance.call("_main", &[Value::I32(argc as i32), Value::I32(argv as i32)])?; instance.call(func_name, &[Value::I32(argc as i32), Value::I32(argv as i32)])?;
} }
0 => { 0 => {
instance.call("_main", &[])?; instance.call(func_name, &[])?;
} }
_ => panic!( _ => panic!(
"The emscripten main function has received an incorrect number of params {}", "The emscripten main function has received an incorrect number of params {}",
@ -952,6 +962,15 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
"_confstr" => func!(crate::unistd::confstr), "_confstr" => func!(crate::unistd::confstr),
}; };
// Compatibility with newer versions of Emscripten
use crate::wasmer_runtime_core::import::LikeNamespace;
for (k, v) in env_ns.get_exports() {
if k.starts_with("_") {
let k = &k[1..];
env_ns.insert(k, v.to_export());
}
}
for null_func_name in globals.null_func_names.iter() { for null_func_name in globals.null_func_names.iter() {
env_ns.insert(null_func_name.as_str(), Func::new(nullfunc).to_export()); env_ns.insert(null_func_name.as_str(), Func::new(nullfunc).to_export());
} }

View File

@ -24,7 +24,7 @@ pub fn is_emscripten_module(module: &Module) -> bool {
.namespace_table .namespace_table
.get(import_name.namespace_index); .get(import_name.namespace_index);
let field = module.info().name_table.get(import_name.name_index); let field = module.info().name_table.get(import_name.name_index);
if field == "_emscripten_memcpy_big" && namespace == "env" { if (field == "_emscripten_memcpy_big" || field=="emscripten_memcpy_big") && namespace == "env" {
return true; return true;
} }
} }
@ -110,6 +110,8 @@ pub unsafe fn copy_cstr_into_wasm(ctx: &mut Ctx, cstr: *const c_char) -> u32 {
pub unsafe fn allocate_on_stack<'a, T: Copy>(ctx: &'a mut Ctx, count: u32) -> (u32, &'a mut [T]) { pub unsafe fn allocate_on_stack<'a, T: Copy>(ctx: &'a mut Ctx, count: u32) -> (u32, &'a mut [T]) {
let offset = get_emscripten_data(ctx) let offset = get_emscripten_data(ctx)
.stack_alloc .stack_alloc
.as_ref()
.unwrap()
.call(count * (size_of::<T>() as u32)) .call(count * (size_of::<T>() as u32))
.unwrap(); .unwrap();
let addr = emscripten_memory_pointer!(ctx.memory(0), offset) as *mut T; let addr = emscripten_memory_pointer!(ctx.memory(0), offset) as *mut T;