Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/actions/install-wasi-sdk/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@ runs:
- name: Setup `wasmtime`
uses: bytecodealliance/actions/wasmtime/setup@v1
with:
version: "44.0.0"
# Pinned to the rolling `dev` build (main, which will become wasmtime
# 47) because the component model `implements` clause used by the
# `implements` runtime test has landed on `main` but is not in a
# released wasmtime yet.
version: "dev"
114 changes: 38 additions & 76 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,11 @@ csharp = ['dep:wit-bindgen-csharp']
csharp-mono = ['csharp']
moonbit = ['dep:wit-bindgen-moonbit']
async = []

# The component model `implements` clause requires composition support that is
# not yet in a released `wac`. Until that ships on crates.io, point the `wac`
# crates at the `implements` branch.
[patch.crates-io]
wac-parser = { git = "https://github.com/ricochet/wac", branch = "implements" }
wac-types = { git = "https://github.com/ricochet/wac", branch = "implements" }
wac-graph = { git = "https://github.com/ricochet/wac", branch = "implements" }
16 changes: 16 additions & 0 deletions tests/runtime/implements/compose.wac
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package example:composition;

// Instantiate the `test` component twice to back the two labeled imports of the
// runner. The component model `implements` clause lets the runner import the
// same `store` interface under two different plain names (`primary` and
// `backup`), each wired here to its own independent instance.
let primary = new test:test { ... };
let backup = new test:test { ... };

let runner = new test:runner {
primary: primary.store,
backup: backup.store,
...
};

export runner...;
35 changes: 35 additions & 0 deletions tests/runtime/implements/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//@ wasmtime-flags = '-Wcomponent-model-implements'

package export_wit_world

import (
"wit_component/backup"
"wit_component/primary"

. "go.bytecodealliance.org/pkg/wit/types"
)

func Run() {
// Each labeled import is its own instance of `store`, so values set
// through `primary` are independent from those set through `backup`.
primary.Set("key", "from-primary")
backup.Set("key", "from-backup")

assertSome(primary.Get("key"), "from-primary")
assertSome(backup.Get("key"), "from-backup")

assertNone(primary.Get("missing"))
assertNone(backup.Get("missing"))
}

func assertSome(opt Option[string], expected string) {
if opt.Tag() != OptionSome || opt.Some() != expected {
panic("unexpected value")
}
}

func assertNone(opt Option[string]) {
if opt.Tag() != OptionNone {
panic("expected none")
}
}
22 changes: 22 additions & 0 deletions tests/runtime/implements/runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@ wasmtime-flags = '-Wcomponent-model-implements'

include!(env!("BINDINGS"));

struct Component;

export!(Component);

impl Guest for Component {
fn run() {
// Each labeled import is its own instance of `store`, so values set
// through `primary` are independent from those set through `backup`.
primary::set("key", "from-primary");
backup::set("key", "from-backup");

assert_eq!(primary::get("key").as_deref(), Some("from-primary"));
assert_eq!(backup::get("key").as_deref(), Some("from-backup"));

assert_eq!(primary::get("missing"), None);
assert_eq!(backup::get("missing"), None);
}
}
22 changes: 22 additions & 0 deletions tests/runtime/implements/runner.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;;@ wasmtime-flags = '-Wcomponent-model-implements'

;; A `runner` written directly in WebAssembly text. It imports the same `store`
;; interface twice under two different plain names (`primary` and `backup`) via
;; the component model `implements` clause, so the two imports appear as distinct
;; core wasm import modules.
(module
(import "primary" "set" (func $primary-set (param i32 i32 i32 i32)))
(import "backup" "set" (func $backup-set (param i32 i32 i32 i32)))

(memory (export "memory") 1)

;; "key" @ 0 (len 3), "primary" @ 3 (len 7), "backup" @ 10 (len 6)
(data (i32.const 0) "keyprimarybackup")

(func (export "run")
;; primary::set("key", "primary")
(call $primary-set (i32.const 0) (i32.const 3) (i32.const 3) (i32.const 7))
;; backup::set("key", "backup")
(call $backup-set (i32.const 0) (i32.const 3) (i32.const 10) (i32.const 6))
)
)
18 changes: 18 additions & 0 deletions tests/runtime/implements/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package export_test_implements_store

import (
. "go.bytecodealliance.org/pkg/wit/types"
)

var store = map[string]string{}

func Get(key string) Option[string] {
if value, ok := store[key]; ok {
return Some[string](value)
}
return None[string]()
}

func Set(key string, value string) {
store[key] = value
}
Loading
Loading