diff --git a/lib/emscripten/src/env/mod.rs b/lib/emscripten/src/env/mod.rs index 1b958db9e..99888c5bb 100644 --- a/lib/emscripten/src/env/mod.rs +++ b/lib/emscripten/src/env/mod.rs @@ -22,11 +22,11 @@ use wasmer_runtime_core::{ }; 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(ctx: &mut Ctx, size: u32) -> WasmPtr { - 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 { @@ -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 { get_emscripten_data(ctx) .memset + .as_ref() + .unwrap() .call(pointer, value, size) .unwrap() } diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 8bbc47adc..ace9b6856 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -84,11 +84,11 @@ const GLOBAL_BASE: u32 = 1024; const STATIC_BASE: u32 = GLOBAL_BASE; pub struct EmscriptenData<'a> { - pub malloc: Func<'a, u32, u32>, - pub free: Func<'a, u32>, + pub malloc: Option>, + pub free: Option>, pub memalign: Option>, - pub memset: Func<'a, (u32, u32, u32), u32>, - pub stack_alloc: Func<'a, u32, u32>, + pub memset: Option>, + pub stack_alloc: Option>, pub jumps: Vec>, pub opened_dirs: HashMap>, @@ -164,11 +164,11 @@ impl<'a> EmscriptenData<'a> { instance: &'a mut Instance, mapped_dirs: HashMap, ) -> EmscriptenData<'a> { - let malloc = instance.func("_malloc").unwrap(); - let free = instance.func("_free").unwrap(); - let memalign = instance.func("_memalign").ok(); - let memset = instance.func("_memset").unwrap(); - let stack_alloc = instance.func("stackAlloc").unwrap(); + let malloc = instance.func("_malloc").or(instance.func("malloc")).ok(); + let free = instance.func("_free").or(instance.func("free")).ok(); + let memalign = instance.func("_memalign").or(instance.func("memalign")).ok(); + let memset = instance.func("_memset").or(instance.func("memset")).ok(); + let stack_alloc = instance.func("stackAlloc").ok(); let dyn_call_i = instance.func("dynCall_i").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_restore = instance.func("stackRestore").ok(); - let set_threw = instance.func("_setThrew").ok(); + let set_threw = instance.func("_setThrew").or(instance.func("setThrew")).ok(); EmscriptenData { malloc, @@ -332,17 +332,27 @@ pub fn run_emscripten_instance( //let (argc, argv) = store_module_arguments(instance.context_mut(), args); instance.call(&ep, &[Value::I32(arg as i32)])?; } 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 _result = match num_params { 2 => { let mut new_args = vec![path]; new_args.extend(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 => { - instance.call("_main", &[])?; + instance.call(func_name, &[])?; } _ => panic!( "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), }; + // 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() { env_ns.insert(null_func_name.as_str(), Func::new(nullfunc).to_export()); } diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index 112728130..7e3bc5318 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -24,7 +24,7 @@ pub fn is_emscripten_module(module: &Module) -> bool { .namespace_table .get(import_name.namespace_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; } } @@ -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]) { let offset = get_emscripten_data(ctx) .stack_alloc + .as_ref() + .unwrap() .call(count * (size_of::() as u32)) .unwrap(); let addr = emscripten_memory_pointer!(ctx.memory(0), offset) as *mut T;