diff --git a/src/common/file_descriptor.rs b/lib/emscripten/src/file_descriptor.rs similarity index 100% rename from src/common/file_descriptor.rs rename to lib/emscripten/src/file_descriptor.rs diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 886d602ca..c94425af5 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -4,21 +4,26 @@ extern crate wasmer_runtime; #[macro_use] use wasmer_runtime::macros; -use wasmer_runtime::{ - import::{Imports, NamespaceMap}, - export::{Export, Context, GlobalPointer, FuncPointer}, - types::{ - FuncSig, Type::{self, *}, Value, - GlobalDesc, - }, - vm::{self, LocalGlobal, Func}, - memory::LinearMemory, -}; use byteorder::{ByteOrder, LittleEndian}; +use hashbrown::{hash_map::Entry, HashMap}; use libc::c_int; use std::cell::UnsafeCell; use std::mem; -use hashbrown::{hash_map::Entry, HashMap}; +use wasmer_runtime::{ + export::{Context, Export, FuncPointer, GlobalPointer}, + import::{Imports, NamespaceMap}, + memory::LinearMemory, + types::{ + FuncSig, GlobalDesc, + Type::{self, *}, + Value, + }, + vm::{self, Func, LocalGlobal}, +}; + +//#[cfg(test)] +mod file_descriptor; +pub mod stdio; // EMSCRIPTEN APIS mod env; @@ -124,7 +129,7 @@ macro_rules! global { unsafe { GlobalPointer::new( // 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), ) } }}; @@ -134,7 +139,7 @@ pub struct EmscriptenGlobals<'a> { pub data: HashMap<&'a str, HashMap<&'a str, (u64, Type)>>, // > } -impl <'a> EmscriptenGlobals<'a> { +impl<'a> EmscriptenGlobals<'a> { pub fn new() -> Self { let mut data = HashMap::new(); let mut env_namepace = HashMap::new(); @@ -150,9 +155,7 @@ impl <'a> EmscriptenGlobals<'a> { data.insert("env", env_namepace); data.insert("global", global_namepace); - Self { - data, - } + Self { data } } } @@ -175,7 +178,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -187,7 +190,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -199,7 +202,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -211,7 +214,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -223,7 +226,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -235,7 +238,7 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { global: GlobalDesc { mutable: false, ty: ty.clone(), - } + }, }, ); @@ -252,7 +255,6 @@ pub fn generate_emscripten_env(globals: &EmscriptenGlobals) -> Imports { }, ); - env_namespace.insert( "putchar", Export::Function { diff --git a/src/common/stdio.rs b/lib/emscripten/src/stdio.rs similarity index 100% rename from src/common/stdio.rs rename to lib/emscripten/src/stdio.rs diff --git a/lib/emscripten/tests/emtests/_common.rs b/lib/emscripten/tests/emtests/_common.rs index 6ecc52ce9..526ae891c 100644 --- a/lib/emscripten/tests/emtests/_common.rs +++ b/lib/emscripten/tests/emtests/_common.rs @@ -1,64 +1,55 @@ macro_rules! assert_emscripten_output { ($file:expr, $name:expr, $args:expr, $expected:expr) => {{ - // TODO: Cyclic Dep! - use wasmer::{ - webassembly::{ - compile, - start_instance, - }, - common::stdio::StdioCapturer, - }; - - use wasmer_runtime::{ - instance::Instance, - module::Module, - table::TableBacking - }; use wasmer_clif_backend::CraneliftCompiler; use wasmer_emscripten::{ EmscriptenGlobals, generate_emscripten_env, + stdio::StdioCapturer }; - use std::sync::Arc; - let wasm_bytes = include_bytes!($file); - let module = compile(&wasm_bytes[..]) - .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? + let module = wasmer_runtime::compile(&wasm_bytes[..], &CraneliftCompiler::new()) + .expect("WASM can't be compiled"); + +// let module = compile(&wasm_bytes[..]) +// .map_err(|err| format!("Can't create the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? let emscripten_globals = EmscriptenGlobals::new(); - let mut import_object = generate_emscripten_env(&emscripten_globals); + let import_object = generate_emscripten_env(&emscripten_globals); let mut instance = module.instantiate(import_object) .map_err(|err| format!("Can't instantiate the WebAssembly module: {}", err)).unwrap(); // NOTE: Need to figure what the unwrap is for ?? - start_instance( - Arc::clone(&module), - &mut instance, - $name, - $args, - ); +// start_instance( +// Arc::clone(&module), +// &mut instance, +// $name, +// $args, +// ); assert!(false, "Emscripten tests are mocked"); - // let capturer = StdioCapturer::new(); - // start_instance( - // Arc::clone(&result_object.module), - // &mut result_object.instance, - // $name, - // $args, - // ) - // .unwrap(); - // let output = capturer.end().unwrap().0; - // let expected_output = include_str!($expected); - // assert!(false, "Emscripten tests are mocked"); - // assert!( - // output.contains(expected_output), - // "Output: `{}` does not contain expected output: `{}`", - // output, - // expected_output - // ); + let capturer = StdioCapturer::new(); + + instance.call("_main", &[]).map(|_o| ()).unwrap(); + // TODO handle start instance logic +// start_instance( +// Arc::clone(&result_object.module), +// &mut result_object.instance, +// $name, +// $args, +// ) +// .unwrap(); + let output = capturer.end().unwrap().0; + let expected_output = include_str!($expected); + assert!(false, "Emscripten tests are mocked"); + assert!( + output.contains(expected_output), + "Output: `{}` does not contain expected output: `{}`", + output, + expected_output + ); }}; } diff --git a/src/common/mod.rs b/src/common/mod.rs index 34d8a0b66..2e4a760a8 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,6 +1,2 @@ pub mod mmap; pub mod slice; - -mod file_descriptor; -//#[cfg(test)] -pub mod stdio; \ No newline at end of file