repl_test: wasm tests compile but do not run

This commit is contained in:
Brian Carroll 2022-02-20 10:38:42 +00:00
parent 603c98d1d9
commit 1e010e185c

View file

@ -11,33 +11,39 @@ use wasmer_wasi::WasiState;
const COMPILER_PATH_ENV_VAR: &str = "COMPILER_PATH"; const COMPILER_PATH_ENV_VAR: &str = "COMPILER_PATH";
thread_local! { thread_local! {
static REPL_STATE: RefCell<Option<Env<'static>>> = RefCell::new(None) static REPL_STATE: RefCell<Option<ReplState>> = RefCell::new(None)
} }
struct Env { struct ReplState {
src: &'static str, src: &'static str,
compiler: Instance, compiler: Instance,
app: Option<Instance>, app: Option<Instance>,
result_addr: Option<u32>, result_addr: Option<u32>,
} }
fn wasmer_create_app(app_bytes: &[u8]) { fn wasmer_create_app(app_bytes_ptr: i32, app_bytes_len: i32) {
REPL_STATE.with(|f| {
if let Some(state) = f.borrow_mut().deref_mut() {
let compiler_memory = state.compiler.exports.get_memory("memory").unwrap();
let compiler_memory_bytes: &mut [u8] = unsafe { compiler_memory.data_unchecked_mut() };
// Find the slice of bytes for the compiled Roc app
let ptr = app_bytes_ptr as usize;
let len = app_bytes_len as usize;
let app_module_bytes: &[u8] = &compiler_memory_bytes[ptr..][..len];
// Parse the bytes into a Wasmer module
let store = Store::default(); let store = Store::default();
let wasmer_module = Module::new(&store, &app_bytes).unwrap(); let wasmer_module = Module::new(&store, app_module_bytes).unwrap();
// First, we create the `WasiEnv` // Get the WASI imports for the app
let mut wasi_env = WasiState::new("hello").finalize().unwrap(); let mut wasi_env = WasiState::new("hello").finalize().unwrap();
// Then, we get the import object related to our WASI
// and attach it to the Wasm instance.
let import_object = wasi_env let import_object = wasi_env
.import_object(&wasmer_module) .import_object(&wasmer_module)
.unwrap_or_else(|_| imports!()); .unwrap_or_else(|_| imports!());
// Create an executable instance (give it a stack & heap, etc. For ELF, this would be the OS's job.)
let instance = Instance::new(&wasmer_module, &import_object).unwrap(); let instance = Instance::new(&wasmer_module, &import_object).unwrap();
REPL_STATE.with(|f| {
if let Some(state) = f.borrow_mut().deref_mut() {
state.app = Some(instance) state.app = Some(instance)
} else { } else {
panic!("REPL state not found") panic!("REPL state not found")
@ -108,7 +114,7 @@ fn wasmer_copy_input_string(src_buffer_addr: u32) {
}) })
} }
fn init_compiler() -> Vec<u8> { fn init_compiler() -> Instance {
let path_str = env::var(COMPILER_PATH_ENV_VAR).unwrap(); let path_str = env::var(COMPILER_PATH_ENV_VAR).unwrap();
let path = Path::new(&path_str); let path = Path::new(&path_str);
let wasm_module_bytes = fs::read(&path).unwrap(); let wasm_module_bytes = fs::read(&path).unwrap();
@ -128,7 +134,7 @@ fn init_compiler() -> Vec<u8> {
Instance::new(&wasmer_module, &import_object).unwrap() Instance::new(&wasmer_module, &import_object).unwrap()
} }
fn run(src: &str) -> String { fn run(src: &'static str) -> String {
let compiler = init_compiler(); let compiler = init_compiler();
let entrypoint = compiler let entrypoint = compiler
@ -137,27 +143,26 @@ fn run(src: &str) -> String {
.unwrap(); .unwrap();
REPL_STATE.with(|f| { REPL_STATE.with(|f| {
let new_state = Env { let new_state = ReplState {
src, src,
compiler, compiler,
app: None, app: None,
result_addr: None, result_addr: None,
}; };
let current_state = f.borrow_mut().deref_mut();
assert_eq!(current_state, None); *f.borrow_mut().deref_mut() = Some(new_state);
*current_state = Some(new_state);
}); });
let actual = String::new(); let actual = String::new();
actual actual
} }
pub fn expect_success(input: &str, expected: &str) { pub fn expect_success(input: &'static str, expected: &str) {
let actual = run(input); let actual = run(input);
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }
pub fn expect_failure(input: &str, expected: &str) { pub fn expect_failure(input: &'static str, expected: &str) {
let actual = run(input); let actual = run(input);
assert_eq!(actual, expected); assert_eq!(actual, expected);
} }