forked from bytecodealliance/wit-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a runtime test for multiple versions (bytecodealliance#787)
* Add a runtime test for multiple versions Signed-off-by: James Sturtevant <jsturtevant@gmail.com> * Update wasmtime component for test Signed-off-by: James Sturtevant <jsturtevant@gmail.com> --------- Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
- Loading branch information
1 parent
e77eaec
commit 8825a3b
Showing
9 changed files
with
305 additions
and
149 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include!("../../../../tests/runtime/versions/wasm.rs"); | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use anyhow::{Ok, Result}; | ||
use wasmtime::Store; | ||
|
||
wasmtime::component::bindgen!(in "tests/runtime/versions"); | ||
use crate::versions::test::dep0_1_0::test::Host as v1; | ||
use crate::versions::test::dep0_2_0::test::Host as v2; | ||
|
||
#[derive(Default)] | ||
pub struct MyFoo; | ||
|
||
impl v1 for MyFoo { | ||
fn x(&mut self) -> wasmtime::Result<f32> { | ||
Ok(1.0) | ||
} | ||
|
||
fn y(&mut self, a: f32) -> wasmtime::Result<f32> { | ||
Ok(1.0 + a) | ||
} | ||
} | ||
|
||
impl v2 for MyFoo { | ||
fn x(&mut self) -> wasmtime::Result<f32> { | ||
Ok(2.0) | ||
} | ||
|
||
fn z(&mut self, a: f32, b: f32) -> wasmtime::Result<f32> { | ||
Ok(2.0 + a + b) | ||
} | ||
} | ||
|
||
#[test] | ||
fn run() -> Result<()> { | ||
crate::run_test( | ||
"versions", | ||
|linker| Foo::add_to_linker(linker, |x| &mut x.0), | ||
|store, component, linker| Foo::instantiate(store, component, linker), | ||
run_test, | ||
) | ||
} | ||
|
||
fn run_test(exports: Foo, store: &mut Store<crate::Wasi<MyFoo>>) -> Result<()> { | ||
// test version 1 | ||
assert_eq!(exports.test_dep0_1_0_test().call_x(&mut *store)?, 1.0); | ||
assert_eq!(exports.test_dep0_1_0_test().call_y(&mut *store, 1.0)?, 2.0); | ||
|
||
// test version 2 | ||
assert_eq!(exports.test_dep0_2_0_test().call_x(&mut *store)?, 2.0); | ||
assert_eq!( | ||
exports.test_dep0_2_0_test().call_z(&mut *store, 1.0, 1.0)?, | ||
4.0 | ||
); | ||
|
||
// test imports | ||
exports.call_test_imports(&mut *store)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package test:dep@0.1.0; | ||
|
||
interface test { | ||
x: func() -> float32; | ||
y: func(a: float32) -> float32; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package test:dep@0.2.0; | ||
|
||
interface test { | ||
x: func() -> float32; | ||
z: func(a: float32, b: float32) -> float32; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
wit_bindgen::generate!({ | ||
path: "../../tests/runtime/versions", | ||
exports: { | ||
world: Component, | ||
"test:dep/test@0.1.0": Component1, | ||
"test:dep/test@0.2.0": Component2, | ||
} | ||
}); | ||
|
||
use exports::test::dep0_1_0::test::Guest as v1; | ||
use exports::test::dep0_2_0::test::Guest as v2; | ||
|
||
struct Component; | ||
impl Guest for Component { | ||
fn test_imports() { | ||
use test::dep0_1_0::test as v1; | ||
assert_eq!(v1::x(), 1.0); | ||
assert_eq!(v1::y(1.0), 2.0); | ||
|
||
use test::dep0_2_0::test as v2; | ||
assert_eq!(v2::x(), 2.0); | ||
assert_eq!(v2::z(1.0, 1.0), 4.0); | ||
} | ||
} | ||
|
||
struct Component1; | ||
impl v1 for Component1{ | ||
fn x() -> f32 { | ||
1.0 | ||
} | ||
|
||
fn y(a: f32) -> f32 { | ||
1.0+a | ||
} | ||
} | ||
|
||
struct Component2; | ||
impl v2 for Component2{ | ||
fn x() -> f32 { | ||
2.0 | ||
} | ||
|
||
fn z(a: f32, b: f32) -> f32 { | ||
2.0+a+b | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package test:versions; | ||
|
||
world foo { | ||
import test:dep/test@0.1.0; | ||
import test:dep/test@0.2.0; | ||
|
||
export test:dep/test@0.1.0; | ||
export test:dep/test@0.2.0; | ||
export test-imports: func(); | ||
} |