Skip to content

Commit

Permalink
minir fix
Browse files Browse the repository at this point in the history
  • Loading branch information
neri committed Mar 3, 2024
1 parent 8384c4a commit ce88be3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ smallvec = {version = "1.13.1", default-features = false}
[workspace]
members = [
"cli",
"temp/test",
]
62 changes: 39 additions & 23 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,31 @@ impl Env {
#[test]
fn instantiate() {
let data = [0, 97, 115, 109, 1, 0, 0, 0];
WebAssembly::instantiate(&data, Env {}).unwrap();
WebAssembly::instantiate(&data, &Env {}).unwrap();

let data = [0, 97, 115, 109, 1, 0, 0];
assert_matches!(
CompileErrorKind::downcast_ref(&WebAssembly::instantiate(&data, Env {}).unwrap_err())
CompileErrorKind::downcast_ref(&WebAssembly::instantiate(&data, &Env {}).unwrap_err())
.unwrap(),
CompileErrorKind::BadExecutable
);

let data = [0, 97, 115, 109, 2, 0, 0, 0];
assert_matches!(
CompileErrorKind::downcast_ref(&WebAssembly::instantiate(&data, Env {}).unwrap_err())
CompileErrorKind::downcast_ref(&WebAssembly::instantiate(&data, &Env {}).unwrap_err())
.unwrap(),
CompileErrorKind::BadExecutable
);

let data = [0, 97, 115, 109, 1, 0, 0, 0, 1];
assert_matches!(
CompileErrorKind::downcast_ref(&WebAssembly::instantiate(&data, Env {}).unwrap_err())
CompileErrorKind::downcast_ref(&WebAssembly::instantiate(&data, &Env {}).unwrap_err())
.unwrap(),
CompileErrorKind::UnexpectedEof
);

let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();
let _ = instance.function("fib").unwrap();
}

Expand All @@ -57,13 +58,13 @@ fn section_order() {
let data = [
0, 97, 115, 109, 1, 0, 0, 0, 1, 1, 0, 2, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 5, 1, 0,
];
WebAssembly::instantiate(&data, Env {}).unwrap();
WebAssembly::instantiate(&data, &Env {}).unwrap();

let data = [
0, 97, 115, 109, 1, 0, 0, 0, 1, 1, 0, 2, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 3, 1, 0,
];
assert_matches!(
CompileErrorKind::downcast_ref(&WebAssembly::instantiate(&data, Env {}).unwrap_err())
CompileErrorKind::downcast_ref(&WebAssembly::instantiate(&data, &Env {}).unwrap_err())
.unwrap(),
CompileErrorKind::InvalidSectionOrder(WasmSectionId::Function)
);
Expand Down Expand Up @@ -791,7 +792,8 @@ fn br_table() {

#[test]
fn app_fact() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();
let runnable = instance.function("fact").unwrap();

let result = runnable
Expand All @@ -813,7 +815,8 @@ fn app_fact() {

#[test]
fn app_fib() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();
let runnable = instance.function("fib").unwrap();

let result = runnable
Expand Down Expand Up @@ -843,7 +846,8 @@ fn app_fib() {

#[test]
fn opr_test_i32() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();

let memory = instance.memory(0).unwrap().try_borrow().unwrap();

Expand Down Expand Up @@ -1016,7 +1020,8 @@ fn opr_test_i32() {

#[test]
fn opr_test_i64() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();

let memory = instance.memory(0).unwrap().try_borrow().unwrap();

Expand Down Expand Up @@ -1193,7 +1198,8 @@ fn opr_test_i64() {

#[test]
fn call_test() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();

let memory = instance.memory(0).unwrap().try_borrow().unwrap();

Expand Down Expand Up @@ -1294,7 +1300,8 @@ fn call_test() {

#[test]
fn call_indirect_test() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();

// let memory = instance.memory(0).unwrap().try_borrow().unwrap();

Expand Down Expand Up @@ -1334,7 +1341,8 @@ fn call_indirect_test() {

#[test]
fn mem_load_store() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();

let mut src = Vec::new();
for i in 0..65536 {
Expand Down Expand Up @@ -1622,7 +1630,8 @@ fn mem_load_store() {

#[test]
fn memory() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();

let mut src = Vec::new();
for i in 0..256 {
Expand Down Expand Up @@ -1790,7 +1799,8 @@ fn memory() {

#[test]
fn global() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();
let runnable = instance.function("global_add").unwrap();

assert_eq!(
Expand Down Expand Up @@ -1847,7 +1857,7 @@ fn name() {
0x61, 0x6D, 0x65, 0x00, 0x06, 0x05, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x01, 0x0E, 0x02, 0x01,
0x04, 0x77, 0x61, 0x73, 0x6D, 0xB4, 0x24, 0x04, 0x74, 0x65, 0x73, 0x74, 0x7F, 0x00,
];
let instance = WebAssembly::instantiate(&slice, Env {}).unwrap();
let instance = WebAssembly::instantiate(&slice, &Env {}).unwrap();
let names = instance.module().names().unwrap();

assert_eq!(names.module().unwrap(), "Hello");
Expand Down Expand Up @@ -1949,7 +1959,8 @@ fn float32() {

#[test]
fn float32_opr() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();

let memory = instance.memory(0).unwrap().try_borrow().unwrap();

Expand Down Expand Up @@ -2646,7 +2657,8 @@ fn float64() {

#[test]
fn float64_opr() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();

let memory = instance.memory(0).unwrap().try_borrow().unwrap();

Expand Down Expand Up @@ -3419,7 +3431,8 @@ fn block_nest() {

#[test]
fn block_test() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();
let runnable = instance.function("block_test").unwrap();

let result = runnable
Expand Down Expand Up @@ -3482,7 +3495,8 @@ fn loop_nest() {

#[test]
fn loop_test() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();
let runnable = instance.function("loop_test").unwrap();

let result = runnable
Expand Down Expand Up @@ -3544,7 +3558,8 @@ fn if_nest() {

#[test]
fn if_test() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();
let runnable = instance.function("if_test1").unwrap();

let result = runnable
Expand Down Expand Up @@ -3600,7 +3615,8 @@ fn if_test() {

#[test]
fn import_test() {
let instance = WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), Env {}).unwrap();
let instance =
WebAssembly::instantiate(include_bytes!("../test/tester.wasm"), &Env {}).unwrap();
let runnable = instance.function("import_test1").unwrap();

let result = runnable
Expand Down
4 changes: 2 additions & 2 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl WebAssembly {
/// Instantiate wasm module
pub fn instantiate<Env: WasmEnv>(
bytes: &[u8],
env: Env,
env: &Env,
) -> Result<WasmInstance, Box<dyn Error>> {
Self::compile(bytes)?.instantiate(env)
}
Expand Down Expand Up @@ -239,7 +239,7 @@ impl WasmModule {
Ok(module)
}

pub fn instantiate<Env: WasmEnv>(mut self, env: Env) -> Result<WasmInstance, Box<dyn Error>> {
pub fn instantiate<Env: WasmEnv>(mut self, env: &Env) -> Result<WasmInstance, Box<dyn Error>> {
let mut func_idx = 0;
for import in &self.imports {
match import.desc {
Expand Down

0 comments on commit ce88be3

Please sign in to comment.