Emscripten tests compiling again

This commit is contained in:
Brandon Fish
2019-01-17 23:55:44 -06:00
parent 321abe6644
commit 1025a0d730
5 changed files with 58 additions and 69 deletions

View File

@ -0,0 +1,25 @@
use std::io;
use std::io::Error;
use std::io::ErrorKind;
use std::io::Read;
pub struct FileDescriptor(libc::c_int);
impl FileDescriptor {
pub fn new(file_descriptor_number: libc::c_int) -> FileDescriptor {
FileDescriptor(file_descriptor_number)
}
}
impl Read for FileDescriptor {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let file_descriptor: libc::c_int = self.0;
let count =
unsafe { libc::read(file_descriptor, buf.as_mut_ptr() as *mut libc::c_void, 1) };
if count < 0 {
Err(Error::new(ErrorKind::Other, "read error"))
} else {
Ok(count as usize)
}
}
}

View File

@ -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)>>, // <namespace, <field_name, (global_value, 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 {

View File

@ -0,0 +1,75 @@
use super::file_descriptor::FileDescriptor;
use libc;
use std::io::BufReader;
use std::io::Read;
// A struct to hold the references to the base stdout and the captured one
pub struct StdioCapturer {
stdout_backup: libc::c_int,
stderr_backup: libc::c_int,
stdout_reader: libc::c_int,
stderr_reader: libc::c_int,
}
// Implementation inspired in
// https://github.com/rust-lang/rust/blob/7d52cbce6db83e4fc2d8706b4e4b9c7da76cbcf8/src/test/run-pass/issues/issue-30490.rs
// Currently only works in Unix systems (Mac, Linux)
impl StdioCapturer {
fn pipe() -> (libc::c_int, libc::c_int) {
let mut fds = [0; 2];
#[cfg(not(target_os = "windows"))]
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
#[cfg(target_os = "windows")]
assert_eq!(
unsafe { libc::pipe(fds.as_mut_ptr(), 1000, libc::O_TEXT) },
0
);
(fds[0], fds[1])
}
pub fn new() -> Self {
let stdout_backup = unsafe { libc::dup(libc::STDOUT_FILENO) };
let stderr_backup = unsafe { libc::dup(libc::STDERR_FILENO) };
let (stdout_reader, stdout_writer) = Self::pipe();
let (stderr_reader, stderr_writer) = Self::pipe();
assert!(unsafe { libc::dup2(stdout_writer, libc::STDOUT_FILENO) } > -1);
assert!(unsafe { libc::dup2(stderr_writer, libc::STDERR_FILENO) } > -1);
// Make sure we close any duplicates of the writer end of the pipe,
// otherwise we can get stuck reading from the pipe which has open
// writers but no one supplying any input
assert_eq!(unsafe { libc::close(stdout_writer) }, 0);
assert_eq!(unsafe { libc::close(stderr_writer) }, 0);
StdioCapturer {
stdout_backup,
stderr_backup,
stdout_reader,
stderr_reader,
}
}
pub fn end(self) -> Result<(String, String), std::io::Error> {
// The Stdio passed into the Command took over (and closed) std{out, err}
// so we should restore them as they were.
assert!(unsafe { libc::dup2(self.stdout_backup, libc::STDOUT_FILENO) } > -1);
assert!(unsafe { libc::dup2(self.stderr_backup, libc::STDERR_FILENO) } > -1);
let fd = FileDescriptor::new(self.stdout_reader);
let mut reader = BufReader::new(fd);
let mut stdout_read = "".to_string();
let _ = reader.read_to_string(&mut stdout_read)?;
let fd = FileDescriptor::new(self.stderr_reader);
let mut reader = BufReader::new(fd);
let mut stderr_read = "".to_string();
let _ = reader.read_to_string(&mut stderr_read)?;
Ok((stdout_read, stderr_read))
}
}

View File

@ -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
);
}};
}