Skip to content

Commit

Permalink
Add cache store, load, and execute test for cranelift and llvm
Browse files Browse the repository at this point in the history
  • Loading branch information
bjfish committed Jun 1, 2019
1 parent ecadbe1 commit fd373db
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/runtime/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,54 @@ impl Cache for FileSystemCache {
Ok(())
}
}

#[cfg(all(test, not(feature = "singlepass")))]
mod tests {

use super::*;
use std::env;

#[test]
fn test_file_system_cache_run() {
use crate::{compile, imports, Func};
use wabt::wat2wasm;

static WAT: &'static str = r#"
(module
(type $t0 (func (param i32) (result i32)))
(func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32)
get_local $p0
i32.const 1
i32.add))
"#;

let wasm = wat2wasm(WAT).unwrap();

let module = compile(&wasm).unwrap();

let cache_dir = env::temp_dir();
println!("test temp_dir {:?}", cache_dir);

let mut fs_cache = unsafe {
FileSystemCache::new(cache_dir)
.map_err(|e| format!("Cache error: {:?}", e))
.unwrap()
};
// store module
let key = WasmHash::generate(&wasm);
fs_cache.store(key, module.clone()).unwrap();

// load module
let cached_module = fs_cache.load(key).unwrap();

let import_object = imports! {};
let instance = cached_module.instantiate(&import_object).unwrap();
let add_one: Func<i32, i32> = instance.func("add_one").unwrap();

let value = add_one.call(42).unwrap();

// verify it works
assert_eq!(value, 43);
}

}

0 comments on commit fd373db

Please sign in to comment.