diff --git a/lib/clif-backend/src/module.rs b/lib/clif-backend/src/module.rs index 2e5f01dca..98ce1dfa6 100644 --- a/lib/clif-backend/src/module.rs +++ b/lib/clif-backend/src/module.rs @@ -32,7 +32,7 @@ impl FuncResolver for PlaceholderFuncResolver { /// This contains all of the items in a `ModuleInner` except the `func_resolver`. pub struct Module { - module: ModuleInner, + pub module: ModuleInner, } impl Module { diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 625d4de04..a7f734624 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -1,15 +1,17 @@ #[macro_use] extern crate wasmer_runtime; +#[macro_use] +use wasmer_runtime::macros; + use wasmer_runtime::{ - instance::{FuncRef}, - import::{Imports}, - export::{Export, Context}, + import::{Imports, NamespaceMap}, + export::{Export, Context, GlobalPointer, FuncPointer}, types::{ - FuncSig, Type::*, Value, + FuncSig, Type::{self, *}, Value, GlobalDesc, }, - vm::{self, LocalGlobal}, + vm::{self, LocalGlobal, Func}, memory::LinearMemory, }; use byteorder::{ByteOrder, LittleEndian}; @@ -91,17 +93,16 @@ pub fn emscripten_set_up_memory(memory: &mut LinearMemory) { } macro_rules! mock_external { - ($imports:ident, $name:ident) => {{ + ($namespace:ident, $name:ident) => {{ extern "C" fn _mocked_fn() -> i32 { debug!("emscripten::{} ", stringify!($name)); -1 } - $imports.register_export( - "env", + $namespace.insert( stringify!($name), Export::Function { - func: unsafe { FuncRef::new(_mocked_fn as _) }, + func: unsafe { FuncPointer::new(_mocked_fn as _) }, ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -112,35 +113,32 @@ macro_rules! mock_external { }}; } -pub struct EmscriptenGlobals { - pub data: Vec<(String, LocalGlobal, GlobalDesc)>, +macro_rules! func { + ($namespace:ident, $function:ident) => {{ + unsafe { FuncPointer::new($namespace::$function as _) } + }}; } -impl EmscriptenGlobals { +pub struct EmscriptenGlobals<'a> { + pub data: HashMap<&'a str, HashMap<&'a str, (u64, Type)>>, // > +} + +impl <'a> EmscriptenGlobals<'a> { pub fn new() -> Self { - let mut data = vec![ - ( - "STACKTOP".into(), - LocalGlobal { data: stacktop(STATIC_BUMP) as _ }, - GlobalDesc { mutable: false, ty: I32 } - ), - ( - "Infinity".into(), - LocalGlobal { data: std::f64::INFINITY.to_bits() }, - GlobalDesc { mutable: false, ty: F64 }, - ), - ( - "NaN".into(), - LocalGlobal { data: std::f64::NAN.to_bits() }, - GlobalDesc { mutable: false, ty: F64 }, - ), - ( - "tableBase".into(), - LocalGlobal { data: 0 }, - GlobalDesc { mutable: false, ty: I32 }, - ), - ]; - + let mut data = HashMap::new(); + let mut env_namepace = HashMap::new(); + let mut global_namepace = HashMap::new(); + + env_namepace.insert("STACKTOP", (stacktop(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("tableBase", (0, I32)); + 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 { data, } @@ -149,36 +147,32 @@ impl EmscriptenGlobals { pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { let mut imports = Imports::new(); + let mut env_namespace = NamespaceMap::new(); + let mut asm_namespace = NamespaceMap::new(); // Add globals. - for (name, global, desc) in &globals.data { - let export = Export::Global { - local: unsafe { std::mem::transmute::<&LocalGlobal, *mut LocalGlobal>(global) }, - global: desc.clone(), - }; +// for () - imports.register_export("env", name.clone(), export); - } + // for (name, global, desc) in &globals.data { + // let global_ptr = unsafe { + // GlobalPointer::new( + // std::mem::transmute::<&LocalGlobal, *mut LocalGlobal>(global) + // ) + // }; - // Print functions - imports.register_export( - "env", + // let export = Export::Global { + // local: global_ptr, + // global: desc.clone(), + // }; + + // imports.register_export("env", name.clone(), export); + // } + + // Print function + env_namespace.insert( "printf", Export::Function { - func: unsafe { FuncRef::new(io::printf as *const _) }, - ctx: Context::Internal, - signature: FuncSig { - params: vec![I32, I32], - returns: vec![I32], - } - }, - ); - - imports.register_export( - "env", - "printf", - Export::Function { - func: unsafe { FuncRef::new(io::printf as _) }, + func: func!(io, printf), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -187,11 +181,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); - imports.register_export( - "env", + + env_namespace.insert( "putchar", Export::Function { - func: unsafe { FuncRef::new(io::putchar as _) }, + func: func!(io, putchar), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -200,11 +194,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Lock - imports.register_export( - "env", + env_namespace.insert( "___lock", Export::Function { - func: unsafe { FuncRef::new(lock::___lock as _) }, + func: func!(lock, ___lock), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -212,11 +205,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___unlock", Export::Function { - func: unsafe { FuncRef::new(lock::___unlock as _) }, + func: func!(lock, ___unlock), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -224,11 +217,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___wait", Export::Function { - func: unsafe { FuncRef::new(lock::___wait as _) }, + func: func!(lock, ___wait), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -237,11 +230,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Env - imports.register_export( - "env", + env_namespace.insert( "_getenv", Export::Function { - func: unsafe { FuncRef::new(env::_getenv as _) }, + func: func!(env, _getenv), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -249,11 +241,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_setenv", Export::Function { - func: unsafe { FuncRef::new(env::_setenv as _) }, + func: func!(env, _setenv), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -261,11 +253,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_putenv", Export::Function { - func: unsafe { FuncRef::new(env::_putenv as _) }, + func: func!(env, _putenv), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -273,11 +265,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_unsetenv", Export::Function { - func: unsafe { FuncRef::new(env::_unsetenv as _) }, + func: func!(env, _unsetenv), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -285,11 +277,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_getpwnam", Export::Function { - func: unsafe { FuncRef::new(env::_getpwnam as _) }, + func: func!(env, _getpwnam), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -297,11 +289,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_getgrnam", Export::Function { - func: unsafe { FuncRef::new(env::_getgrnam as _) }, + func: func!(env, _getgrnam), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -309,11 +301,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___buildEnvironment", Export::Function { - func: unsafe { FuncRef::new(env::___build_environment as _) }, + func: func!(env, ___build_environment), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -322,11 +314,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Errno - imports.register_export( - "env", + env_namespace.insert( "___setErrNo", Export::Function { - func: unsafe { FuncRef::new(errno::___seterrno as _) }, + func: func!(errno, ___seterrno), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -335,11 +326,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Syscalls - imports.register_export( - "env", + env_namespace.insert( "___syscall1", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall1 as _) }, + func: func!(syscalls, ___syscall1), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -347,11 +337,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall3", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall3 as _) }, + func: func!(syscalls, ___syscall3), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -359,11 +349,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall4", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall4 as _) }, + func: func!(syscalls, ___syscall4), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -371,11 +361,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall5", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall5 as _) }, + func: func!(syscalls, ___syscall5), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -383,11 +373,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall6", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall6 as _) }, + func: func!(syscalls, ___syscall6), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -395,11 +385,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall12", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall12 as _) }, + func: func!(syscalls, ___syscall12), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -407,11 +397,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall20", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall20 as _) }, + func: func!(syscalls, ___syscall20), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -419,11 +409,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall39", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall39 as _) }, + func: func!(syscalls, ___syscall39), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -431,11 +421,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall40", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall40 as _) }, + func: func!(syscalls, ___syscall40), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -443,11 +433,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall54", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall54 as _) }, + func: func!(syscalls, ___syscall54), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -455,11 +445,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall57", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall57 as _) }, + func: func!(syscalls, ___syscall57), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -467,11 +457,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall63", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall63 as _) }, + func: func!(syscalls, ___syscall63), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -479,11 +469,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall64", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall64 as _) }, + func: func!(syscalls, ___syscall64), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -491,11 +481,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall102", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall102 as _) }, + func: func!(syscalls, ___syscall102), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -503,11 +493,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall114", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall114 as _) }, + func: func!(syscalls, ___syscall114), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -515,11 +505,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall122", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall122 as _) }, + func: func!(syscalls, ___syscall122), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -527,11 +517,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall140", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall140 as _) }, + func: func!(syscalls, ___syscall140), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -539,11 +529,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall142", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall142 as _) }, + func: func!(syscalls, ___syscall142), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -551,11 +541,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall145", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall145 as _) }, + func: func!(syscalls, ___syscall145), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -563,11 +553,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall146", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall146 as _) }, + func: func!(syscalls, ___syscall146), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -575,11 +565,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall180", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall180 as _) }, + func: func!(syscalls, ___syscall180), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -587,11 +577,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall181", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall181 as _) }, + func: func!(syscalls, ___syscall181), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -599,11 +589,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall192", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall192 as _) }, + func: func!(syscalls, ___syscall192), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -611,11 +601,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall195", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall195 as _) }, + func: func!(syscalls, ___syscall195), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -623,11 +613,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall197", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall197 as _) }, + func: func!(syscalls, ___syscall197), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -635,11 +625,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall201", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall201 as _) }, + func: func!(syscalls, ___syscall201), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -647,11 +637,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall202", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall202 as _) }, + func: func!(syscalls, ___syscall202), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -659,11 +649,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall212", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall212 as _) }, + func: func!(syscalls, ___syscall212), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -671,11 +661,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall221", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall221 as _) }, + func: func!(syscalls, ___syscall221), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -683,11 +673,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall330", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall330 as _) }, + func: func!(syscalls, ___syscall330), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -695,11 +685,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___syscall340", Export::Function { - func: unsafe { FuncRef::new(syscalls::___syscall340 as _) }, + func: func!(syscalls, ___syscall340), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -708,11 +698,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Process - imports.register_export( - "env", + env_namespace.insert( "abort", Export::Function { - func: unsafe { FuncRef::new(process::em_abort as _) }, + func: func!(process, em_abort), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -720,11 +709,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_abort", Export::Function { - func: unsafe { FuncRef::new(process::_abort as _) }, + func: func!(process, _abort), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -732,11 +721,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "abortStackOverflow", Export::Function { - func: unsafe { FuncRef::new(process::abort_stack_overflow as _) }, + func: func!(process, abort_stack_overflow), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -744,11 +733,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_llvm_trap", Export::Function { - func: unsafe { FuncRef::new(process::_llvm_trap as _) }, + func: func!(process, _llvm_trap), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -756,11 +745,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_fork", Export::Function { - func: unsafe { FuncRef::new(process::_fork as _) }, + func: func!(process, _fork), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -768,11 +757,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_exit", Export::Function { - func: unsafe { FuncRef::new(process::_exit as _) }, + func: func!(process, _exit), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -780,11 +769,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_system", Export::Function { - func: unsafe { FuncRef::new(process::_system as _) }, + func: func!(process, _system), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -792,11 +781,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_popen", Export::Function { - func: unsafe { FuncRef::new(process::_popen as _) }, + func: func!(process, _popen), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -805,11 +794,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Signal - imports.register_export( - "env", + env_namespace.insert( "_sigemptyset", Export::Function { - func: unsafe { FuncRef::new(signal::_sigemptyset as _) }, + func: func!(signal, _sigemptyset), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -817,11 +805,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_sigaddset", Export::Function { - func: unsafe { FuncRef::new(signal::_sigaddset as _) }, + func: func!(signal, _sigaddset), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -829,11 +817,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_sigprocmask", Export::Function { - func: unsafe { FuncRef::new(signal::_sigprocmask as _) }, + func: func!(signal, _sigprocmask), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -841,11 +829,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_sigaction", Export::Function { - func: unsafe { FuncRef::new(signal::_sigaction as _) }, + func: func!(signal, _sigaction), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -853,11 +841,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_signal", Export::Function { - func: unsafe { FuncRef::new(signal::_signal as _) }, + func: func!(signal, _signal), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -866,11 +854,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Memory - imports.register_export( - "env", + env_namespace.insert( "abortOnCannotGrowMemory", Export::Function { - func: unsafe { FuncRef::new(memory::abort_on_cannot_grow_memory as _) }, + func: func!(memory, abort_on_cannot_grow_memory), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -878,11 +865,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_emscripten_memcpy_big", Export::Function { - func: unsafe { FuncRef::new(memory::_emscripten_memcpy_big as _) }, + func: func!(memory, _emscripten_memcpy_big), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -890,11 +877,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "enlargeMemory", Export::Function { - func: unsafe { FuncRef::new(memory::enlarge_memory as _) }, + func: func!(memory, enlarge_memory), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -902,11 +889,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "getTotalMemory", Export::Function { - func: unsafe { FuncRef::new(memory::get_total_memory as _) }, + func: func!(memory, get_total_memory), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -914,11 +901,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___map_file", Export::Function { - func: unsafe { FuncRef::new(memory::___map_file as _) }, + func: func!(memory, ___map_file), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -927,11 +914,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Exception - imports.register_export( - "env", + env_namespace.insert( "___cxa_allocate_exception", Export::Function { - func: unsafe { FuncRef::new(exception::___cxa_allocate_exception as _) }, + func: func!(exception, ___cxa_allocate_exception), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -939,11 +925,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___cxa_allocate_exception", Export::Function { - func: unsafe { FuncRef::new(exception::___cxa_throw as _) }, + func: func!(exception, ___cxa_throw), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -951,11 +937,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___cxa_throw", Export::Function { - func: unsafe { FuncRef::new(exception::___cxa_throw as _) }, + func: func!(exception, ___cxa_throw), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32], @@ -964,11 +950,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // NullFuncs - imports.register_export( - "env", + env_namespace.insert( "nullFunc_ii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_ii as _) }, + func: func!(nullfunc, nullfunc_ii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -976,11 +961,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_iii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_iii as _) }, + func: func!(nullfunc, nullfunc_iii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -988,11 +973,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_iiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_iiii as _) }, + func: func!(nullfunc, nullfunc_iiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1000,11 +985,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_iiiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_iiiii as _) }, + func: func!(nullfunc, nullfunc_iiiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1012,11 +997,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_iiiiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_iiiiii as _) }, + func: func!(nullfunc, nullfunc_iiiiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1024,11 +1009,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_v", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_v as _) }, + func: func!(nullfunc, nullfunc_v), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1036,11 +1021,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_vi", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_vi as _) }, + func: func!(nullfunc, nullfunc_vi), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1048,11 +1033,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_vii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_vii as _) }, + func: func!(nullfunc, nullfunc_vii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1060,11 +1045,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_viii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_viii as _) }, + func: func!(nullfunc, nullfunc_viii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1072,11 +1057,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_viiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_viiii as _) }, + func: func!(nullfunc, nullfunc_viiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1084,11 +1069,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_viiiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_viiiii as _) }, + func: func!(nullfunc, nullfunc_viiiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1096,11 +1081,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "nullFunc_viiiiii", Export::Function { - func: unsafe { FuncRef::new(nullfunc::nullfunc_viiiiii as _) }, + func: func!(nullfunc, nullfunc_viiiiii), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1109,11 +1094,10 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); // Time - imports.register_export( - "env", + env_namespace.insert( "_gettimeofday", Export::Function { - func: unsafe { FuncRef::new(time::_gettimeofday as _) }, + func: func!(time, _gettimeofday), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1121,11 +1105,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_clock_gettime", Export::Function { - func: unsafe { FuncRef::new(time::_clock_gettime as _) }, + func: func!(time, _clock_gettime), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1133,11 +1117,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "___clock_gettime", Export::Function { - func: unsafe { FuncRef::new(time::___clock_gettime as _) }, + func: func!(time, ___clock_gettime), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1145,11 +1129,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_clock", Export::Function { - func: unsafe { FuncRef::new(time::_clock as _) }, + func: func!(time, _clock), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -1157,11 +1141,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_difftime", Export::Function { - func: unsafe { FuncRef::new(time::_difftime as _) }, + func: func!(time, _difftime), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1169,11 +1153,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_asctime", Export::Function { - func: unsafe { FuncRef::new(time::_asctime as _) }, + func: func!(time, _asctime), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1181,11 +1165,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_asctime_r", Export::Function { - func: unsafe { FuncRef::new(time::_asctime_r as _) }, + func: func!(time, _asctime_r), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1193,11 +1177,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_localtime", Export::Function { - func: unsafe { FuncRef::new(time::_localtime as _) }, + func: func!(time, _localtime), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1205,11 +1189,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_time", Export::Function { - func: unsafe { FuncRef::new(time::_time as _) }, + func: func!(time, _time), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1217,11 +1201,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_strftime", Export::Function { - func: unsafe { FuncRef::new(time::_strftime as _) }, + func: func!(time, _strftime), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32, I32, I32], @@ -1229,11 +1213,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_localtime_r", Export::Function { - func: unsafe { FuncRef::new(time::_localtime_r as _) }, + func: func!(time, _localtime_r), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1241,11 +1225,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_getpagesize", Export::Function { - func: unsafe { FuncRef::new(env::_getpagesize as _) }, + func: func!(env, _getpagesize), ctx: Context::Internal, signature: FuncSig { params: vec![], @@ -1253,11 +1237,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "_sysconf", Export::Function { - func: unsafe { FuncRef::new(env::_sysconf as _) }, + func: func!(env, _sysconf), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1265,36 +1249,12 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); + // Math - imports.register_export( - "env", - "_llvm_log10_f64", - Export::Function { - func: unsafe { FuncRef::new(math::_llvm_log10_f64 as _) }, - ctx: Context::Internal, - signature: FuncSig { - params: vec![F64], - returns: vec![F64], - }, - }, - ); - imports.register_export( - "env", - "_llvm_log2_f64", - Export::Function { - func: unsafe { FuncRef::new( math::_llvm_log2_f64 as _) }, - ctx: Context::Internal, - signature: FuncSig { - params: vec![F64], - returns: vec![F64], - }, - }, - ); - imports.register_export( - "asm2wasm", + asm_namespace.insert( "f64-rem", Export::Function { - func: unsafe { FuncRef::new(math::f64_rem as _) }, + func: func!(math, f64_rem), ctx: Context::Internal, signature: FuncSig { params: vec![F64, F64], @@ -1302,12 +1262,36 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); + + env_namespace.insert( + "_llvm_log10_f64", + Export::Function { + func: func!(math, _llvm_log10_f64), + ctx: Context::Internal, + signature: FuncSig { + params: vec![F64], + returns: vec![F64], + }, + }, + ); + + env_namespace.insert( + "_llvm_log2_f64", + Export::Function { + func: func!(math, _llvm_log2_f64), + ctx: Context::Internal, + signature: FuncSig { + params: vec![F64], + returns: vec![F64], + }, + }, + ); + // - imports.register_export( - "env", + env_namespace.insert( "__setjmp", Export::Function { - func: unsafe { FuncRef::new(jmp::__setjmp as _) }, + func: func!(jmp, __setjmp), ctx: Context::Internal, signature: FuncSig { params: vec![I32], @@ -1315,11 +1299,11 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, }, ); - imports.register_export( - "env", + + env_namespace.insert( "__longjmp", Export::Function { - func: unsafe { FuncRef::new(jmp::__longjmp as _) }, + func: func!(jmp, __longjmp), ctx: Context::Internal, signature: FuncSig { params: vec![I32, I32], @@ -1328,84 +1312,87 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); - mock_external!(imports, _waitpid); - mock_external!(imports, _utimes); - mock_external!(imports, _usleep); - // mock_external!(imports, _time); - // mock_external!(imports, _sysconf); - // mock_external!(imports, _strftime); - mock_external!(imports, _sigsuspend); - // mock_external!(imports, _sigprocmask); - // mock_external!(imports, _sigemptyset); - // mock_external!(imports, _sigaddset); - // mock_external!(imports, _sigaction); - mock_external!(imports, _setitimer); - mock_external!(imports, _setgroups); - mock_external!(imports, _setgrent); - mock_external!(imports, _sem_wait); - mock_external!(imports, _sem_post); - mock_external!(imports, _sem_init); - mock_external!(imports, _sched_yield); - mock_external!(imports, _raise); - mock_external!(imports, _mktime); - // mock_external!(imports, _localtime_r); - // mock_external!(imports, _localtime); - mock_external!(imports, _llvm_stacksave); - mock_external!(imports, _llvm_stackrestore); - mock_external!(imports, _kill); - mock_external!(imports, _gmtime_r); - // mock_external!(imports, _gettimeofday); - // mock_external!(imports, _getpagesize); - mock_external!(imports, _getgrent); - mock_external!(imports, _getaddrinfo); - // mock_external!(imports, _fork); - // mock_external!(imports, _exit); - mock_external!(imports, _execve); - mock_external!(imports, _endgrent); - // mock_external!(imports, _clock_gettime); - mock_external!(imports, ___syscall97); - mock_external!(imports, ___syscall91); - mock_external!(imports, ___syscall85); - mock_external!(imports, ___syscall75); - mock_external!(imports, ___syscall66); - // mock_external!(imports, ___syscall64); - // mock_external!(imports, ___syscall63); - // mock_external!(imports, ___syscall60); - // mock_external!(imports, ___syscall54); - // mock_external!(imports, ___syscall39); - mock_external!(imports, ___syscall38); - // mock_external!(imports, ___syscall340); - mock_external!(imports, ___syscall334); - mock_external!(imports, ___syscall300); - mock_external!(imports, ___syscall295); - mock_external!(imports, ___syscall272); - mock_external!(imports, ___syscall268); - // mock_external!(imports, ___syscall221); - mock_external!(imports, ___syscall220); - // mock_external!(imports, ___syscall212); - // mock_external!(imports, ___syscall201); - mock_external!(imports, ___syscall199); - // mock_external!(imports, ___syscall197); - mock_external!(imports, ___syscall196); - // mock_external!(imports, ___syscall195); - mock_external!(imports, ___syscall194); - mock_external!(imports, ___syscall191); - // mock_external!(imports, ___syscall181); - // mock_external!(imports, ___syscall180); - mock_external!(imports, ___syscall168); - // mock_external!(imports, ___syscall146); - // mock_external!(imports, ___syscall145); - // mock_external!(imports, ___syscall142); - mock_external!(imports, ___syscall140); - // mock_external!(imports, ___syscall122); - // mock_external!(imports, ___syscall102); - // mock_external!(imports, ___syscall20); - mock_external!(imports, ___syscall15); - mock_external!(imports, ___syscall10); - mock_external!(imports, _dlopen); - mock_external!(imports, _dlclose); - mock_external!(imports, _dlsym); - mock_external!(imports, _dlerror); + mock_external!(env_namespace, _waitpid); + mock_external!(env_namespace, _utimes); + mock_external!(env_namespace, _usleep); + // mock_external!(env_namespace, _time); + // mock_external!(env_namespace, _sysconf); + // mock_external!(env_namespace, _strftime); + mock_external!(env_namespace, _sigsuspend); + // mock_external!(env_namespace, _sigprocmask); + // mock_external!(env_namespace, _sigemptyset); + // mock_external!(env_namespace, _sigaddset); + // mock_external!(env_namespace, _sigaction); + mock_external!(env_namespace, _setitimer); + mock_external!(env_namespace, _setgroups); + mock_external!(env_namespace, _setgrent); + mock_external!(env_namespace, _sem_wait); + mock_external!(env_namespace, _sem_post); + mock_external!(env_namespace, _sem_init); + mock_external!(env_namespace, _sched_yield); + mock_external!(env_namespace, _raise); + mock_external!(env_namespace, _mktime); + // mock_external!(env_namespace, _localtime_r); + // mock_external!(env_namespace, _localtime); + mock_external!(env_namespace, _llvm_stacksave); + mock_external!(env_namespace, _llvm_stackrestore); + mock_external!(env_namespace, _kill); + mock_external!(env_namespace, _gmtime_r); + // mock_external!(env_namespace, _gettimeofday); + // mock_external!(env_namespace, _getpagesize); + mock_external!(env_namespace, _getgrent); + mock_external!(env_namespace, _getaddrinfo); + // mock_external!(env_namespace, _fork); + // mock_external!(env_namespace, _exit); + mock_external!(env_namespace, _execve); + mock_external!(env_namespace, _endgrent); + // mock_external!(env_namespace, _clock_gettime); + mock_external!(env_namespace, ___syscall97); + mock_external!(env_namespace, ___syscall91); + mock_external!(env_namespace, ___syscall85); + mock_external!(env_namespace, ___syscall75); + mock_external!(env_namespace, ___syscall66); + // mock_external!(env_namespace, ___syscall64); + // mock_external!(env_namespace, ___syscall63); + // mock_external!(env_namespace, ___syscall60); + // mock_external!(env_namespace, ___syscall54); + // mock_external!(env_namespace, ___syscall39); + mock_external!(env_namespace, ___syscall38); + // mock_external!(env_namespace, ___syscall340); + mock_external!(env_namespace, ___syscall334); + mock_external!(env_namespace, ___syscall300); + mock_external!(env_namespace, ___syscall295); + mock_external!(env_namespace, ___syscall272); + mock_external!(env_namespace, ___syscall268); + // mock_external!(env_namespace, ___syscall221); + mock_external!(env_namespace, ___syscall220); + // mock_external!(env_namespace, ___syscall212); + // mock_external!(env_namespace, ___syscall201); + mock_external!(env_namespace, ___syscall199); + // mock_external!(env_namespace, ___syscall197); + mock_external!(env_namespace, ___syscall196); + // mock_external!(env_namespace, ___syscall195); + mock_external!(env_namespace, ___syscall194); + mock_external!(env_namespace, ___syscall191); + // mock_external!(env_namespace, ___syscall181); + // mock_external!(env_namespace, ___syscall180); + mock_external!(env_namespace, ___syscall168); + // mock_external!(env_namespace, ___syscall146); + // mock_external!(env_namespace, ___syscall145); + // mock_external!(env_namespace, ___syscall142); + mock_external!(env_namespace, ___syscall140); + // mock_external!(env_namespace, ___syscall122); + // mock_external!(env_namespace, ___syscall102); + // mock_external!(env_namespace, ___syscall20); + mock_external!(env_namespace, ___syscall15); + mock_external!(env_namespace, ___syscall10); + mock_external!(env_namespace, _dlopen); + mock_external!(env_namespace, _dlclose); + mock_external!(env_namespace, _dlsym); + mock_external!(env_namespace, _dlerror); + + imports.register("env", env_namespace); + imports.register("asm2wasm", asm_namespace); imports } diff --git a/lib/emscripten/src/utils.rs b/lib/emscripten/src/utils.rs index dae8f7b65..84e4feb8a 100644 --- a/lib/emscripten/src/utils.rs +++ b/lib/emscripten/src/utils.rs @@ -6,14 +6,16 @@ use std::ffi::CStr; use std::mem::size_of; use std::os::raw::c_char; use std::slice; +use std::sync::Arc; /// We check if a provided module is an Emscripten generated one -pub fn is_emscripten_module(module: &Module) -> bool { - for (_, import_name) in &module.imported_functions { - if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { - return true; - } - } - false +pub fn is_emscripten_module(module: &Arc) -> bool { + // for (_, import_name) in &module.imported_functions { + // if import_name.name == "_emscripten_memcpy_big" && import_name.namespace == "env" { + // return true; + // } + // } + // false + true } pub unsafe fn write_to_buf(string: *const c_char, buf: u32, max: u32, instance: &Instance) -> u32 { diff --git a/lib/runtime/src/instance.rs b/lib/runtime/src/instance.rs index d7dc60071..f4d64712f 100644 --- a/lib/runtime/src/instance.rs +++ b/lib/runtime/src/instance.rs @@ -24,7 +24,7 @@ pub(crate) struct InstanceInner { } pub struct Instance { - pub(crate) module: Rc, + pub module: Rc, inner: Box, #[allow(dead_code)] imports: Box, diff --git a/lib/runtime/src/lib.rs b/lib/runtime/src/lib.rs index 42d812201..37d650262 100644 --- a/lib/runtime/src/lib.rs +++ b/lib/runtime/src/lib.rs @@ -3,7 +3,7 @@ extern crate field_offset; #[macro_use] -mod macros; +pub mod macros; #[doc(hidden)] pub mod backend; mod backing; diff --git a/lib/runtime/src/macros.rs b/lib/runtime/src/macros.rs index fe2bd1a7f..c27b1e8e0 100644 --- a/lib/runtime/src/macros.rs +++ b/lib/runtime/src/macros.rs @@ -1,3 +1,4 @@ +#[macro_export] macro_rules! debug { ($fmt:expr) => (if cfg!(any(debug_assertions, feature="debug")) { println!(concat!("wasmer-runtime(:{})::", $fmt), line!()) }); ($fmt:expr, $($arg:tt)*) => (if cfg!(any(debug_assertions, feature="debug")) { println!(concat!("wasmer-runtime(:{})::", $fmt, "\n"), line!(), $($arg)*) }); diff --git a/lib/runtime/src/module.rs b/lib/runtime/src/module.rs index 7c40c7ef0..e575689b9 100644 --- a/lib/runtime/src/module.rs +++ b/lib/runtime/src/module.rs @@ -39,7 +39,7 @@ pub struct ModuleInner { pub sig_registry: SigRegistry, } -pub struct Module(Rc); +pub struct Module(pub Rc); impl Module { pub(crate) fn new(inner: Rc) -> Self { diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 1028dafc5..220eae9bd 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -83,26 +83,23 @@ fn execute_wasm(options: &Run) -> Result<(), String> { let emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(); - let import_object = if abi == webassembly::InstanceABI::Emscripten { + let mut import_object = if abi == webassembly::InstanceABI::Emscripten { wasmer_emscripten::generate_emscripten_env(&emscripten_globals) } else { wasmer_runtime::import::Imports::new() }; - let import_object = Rc::new(import_object); - let instance_options = webassembly::InstanceOptions { mock_missing_imports: true, mock_missing_globals: true, mock_missing_tables: true, abi: abi, show_progressbar: true, -// isa: isa, }; debug!("webassembly - creating instance"); - let mut instance = module.instantiate(import_object) + let mut instance = module.instantiate(&mut import_object) .map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err))?; webassembly::start_instance( diff --git a/src/webassembly/mod.rs b/src/webassembly/mod.rs index 34173a9d3..05c516f7a 100644 --- a/src/webassembly/mod.rs +++ b/src/webassembly/mod.rs @@ -6,7 +6,7 @@ pub mod utils; use wasmer_clif_backend::CraneliftCompiler; use wasmer_runtime::{ backend::Compiler, - module::Module, + module::{Module, ModuleInner}, import::Imports, instance::Instance, }; @@ -17,6 +17,7 @@ use cranelift_codegen::{ use std::panic; use std::str::FromStr; use std::sync::Arc; +use std::rc::Rc; use target_lexicon; use wasmparser; use wasmparser::WasmDecoder; @@ -122,11 +123,11 @@ pub fn instantiate_streaming( /// webassembly::CompileError. pub fn compile(buffer_source: &[u8]) -> Result, ErrorKind> { let compiler = &CraneliftCompiler {}; - let module = compiler + let module_inner = compiler .compile(buffer_source) .map_err(|e| ErrorKind::CompileError(e))?; - Ok(Arc::new(module)) + Ok(Arc::new(Module(Rc::new(module_inner)))) } /// The webassembly::validate() function validates a given typed @@ -230,7 +231,7 @@ pub fn start_instance( path: &str, args: Vec<&str>, ) -> Result<(), String> { - let main_name = if is_emscripten_module(&instance.module) { + let main_name = if is_emscripten_module(&module) { "_main" } else { "main"