Skip to content

Commit

Permalink
Add a runtime test for multiple versions (bytecodealliance#787)
Browse files Browse the repository at this point in the history
* 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
jsturtevant authored Jan 3, 2024
1 parent e77eaec commit 8825a3b
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 149 deletions.
306 changes: 168 additions & 138 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ indexmap = "2.0.0"

wasm-encoder = "0.38.1"
wasm-metadata = "0.10.14"
wasmtime-wasi = "16.0.0"
wit-parser = "0.13.0"
wit-component = "0.19.0"

Expand Down Expand Up @@ -79,7 +78,7 @@ csharp-mono = ['csharp']

[dev-dependencies]
heck = { workspace = true }
wasmtime = { version = "16", features = ['component-model'] }
wasmtime-wasi = { workspace = true }
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", features = ['component-model'] }
wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime"}
test-artifacts = { path = 'crates/test-rust-wasm/artifacts' }
wit-parser = { workspace = true }
3 changes: 3 additions & 0 deletions crates/test-rust-wasm/src/bin/versions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include!("../../../../tests/runtime/versions/wasm.rs");

fn main() {}
16 changes: 8 additions & 8 deletions tests/runtime/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use anyhow::{Context, Result};
use heck::ToUpperCamelCase;

use std::borrow::Cow;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};
use wasm_encoder::{Encode, Section};
use wasmtime::component::{Component, Instance, Linker};
use wasmtime::{Config, Engine, Store};
use wasmtime_wasi::preview2::{Table, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime::component::{Component, Instance, Linker, ResourceTable};
use wasmtime::{Config, Engine, Store, Table};
use wasmtime_wasi::preview2::{WasiCtx, WasiCtxBuilder, WasiView};
use wit_component::{ComponentEncoder, StringEncoding};
use wit_parser::{Resolve, WorldId, WorldItem};

Expand All @@ -35,17 +34,18 @@ mod resources;
mod smoke;
mod strings;
mod variants;
mod versions;

struct MyCtx {}

struct Wasi<T: Send>(T, MyCtx, Table, WasiCtx);
struct Wasi<T: Send>(T, MyCtx, ResourceTable, WasiCtx);

// wasi trait
impl<T: Send> WasiView for Wasi<T> {
fn table(&self) -> &Table {
fn table(&self) -> &ResourceTable {
&self.2
}
fn table_mut(&mut self) -> &mut Table {
fn table_mut(&mut self) -> &mut ResourceTable {
&mut self.2
}
fn ctx(&self) -> &WasiCtx {
Expand Down Expand Up @@ -95,7 +95,7 @@ where
add_to_linker(&mut linker)?;
let state = MyCtx {};

let table = Table::new();
let table = ResourceTable::new();
let wasi: WasiCtx = WasiCtxBuilder::new().inherit_stdout().args(&[""]).build();

let data = Wasi(T::default(), state, table, wasi);
Expand Down
56 changes: 56 additions & 0 deletions tests/runtime/versions.rs
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(())
}
6 changes: 6 additions & 0 deletions tests/runtime/versions/deps/v1/v1.wit
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;
}
6 changes: 6 additions & 0 deletions tests/runtime/versions/deps/v2/v2.wit
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;
}
46 changes: 46 additions & 0 deletions tests/runtime/versions/wasm.rs
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
}
}
10 changes: 10 additions & 0 deletions tests/runtime/versions/world.wit
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();
}

0 comments on commit 8825a3b

Please sign in to comment.