forked from bytecodealliance/wasmtime
-
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.
- Loading branch information
Showing
13 changed files
with
269 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -1,36 +1,64 @@ | ||
#!/bin/sh | ||
#!/bin/bash | ||
|
||
# Script to re-vendor the WIT files that Wasmtime uses as defined by a | ||
# particular tag in upstream repositories. | ||
# | ||
# This script is executed on CI to ensure that everything is up-to-date. | ||
set -ex | ||
|
||
# Space-separated list of wasi proposals that are vendored here along with the | ||
# tag that they're all vendored at. | ||
# | ||
# This assumes that the repositories all have the pattern: | ||
# https://github.com/WebAssembly/wasi-$repo | ||
# and every repository has a tag `v$tag` here. That is currently done as part | ||
# of the WASI release process. | ||
repos="cli clocks filesystem http io random sockets" | ||
tag=0.2.0 | ||
|
||
# First, replace the existing vendored WIT files in the `wasi` crate. | ||
dst=crates/wasi/wit/deps | ||
rm -rf $dst | ||
mkdir -p $dst | ||
for repo in $repos; do | ||
mkdir $dst/$repo | ||
curl -L https://github.com/WebAssembly/wasi-$repo/archive/refs/tags/v$tag.tar.gz | \ | ||
tar xzf - --strip-components=2 -C $dst/$repo wasi-$repo-$tag/wit | ||
rm -rf $dst/$repo/deps* | ||
done | ||
|
||
# Also replace the `wasi-http` WIT files since they match those in the `wasi` | ||
# crate. | ||
rm -rf crates/wasi-http/wit/deps | ||
cp -r $dst crates/wasi-http/wit | ||
# The make_vendor function takes a base path (e.g., "wasi") and a list | ||
# of packages in the format "name@tag". It constructs the full destination | ||
# path, downloads the tarballs from GitHub, extracts the relevant files, and | ||
# removes any unwanted directories. | ||
make_vendor() { | ||
local name=$1 | ||
local packages=$2 | ||
local path="crates/$name/wit/deps" | ||
|
||
rm -rf $path | ||
mkdir -p $path | ||
|
||
for package in $packages; do | ||
IFS='@' read -r repo tag <<< "$package" | ||
mkdir -p $path/$repo | ||
cached_extracted_dir="$cache_dir/$repo-$tag" | ||
|
||
if [[ ! -d $cached_extracted_dir ]]; then | ||
mkdir -p $cached_extracted_dir | ||
curl -sL https://github.com/WebAssembly/wasi-$repo/archive/$tag.tar.gz | \ | ||
tar xzf - --strip-components=1 -C $cached_extracted_dir | ||
rm -rf $cached_extracted_dir/wit/deps* | ||
fi | ||
|
||
cp -r $cached_extracted_dir/wit/* $path/$repo | ||
done | ||
} | ||
|
||
cache_dir=$(mktemp -d) | ||
|
||
make_vendor "wasi" " | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
" | ||
|
||
make_vendor "wasi-http" " | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] | ||
" | ||
|
||
make_vendor "wasi-runtime-config" "runtime-config@c667fe6" | ||
|
||
rm -rf $cache_dir | ||
|
||
# Separately (for now), vendor the `wasi-nn` WIT files since their retrieval is | ||
# slightly different than above. | ||
|
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,8 @@ | ||
use test_programs::config::wasi::config::runtime; | ||
|
||
fn main() { | ||
let v = runtime::get("hello").unwrap().unwrap(); | ||
assert_eq!(v, "world"); | ||
let config = runtime::get_all().unwrap(); | ||
assert_eq!(config, [("hello".to_string(), "world".to_string())]) | ||
} |
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,20 @@ | ||
[package] | ||
name = "wasmtime-wasi-runtime-config" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
repository = "https://github.com/bytecodealliance/wasmtime" | ||
license = "Apache-2.0 WITH LLVM-exception" | ||
description = "Wasmtime implementation of the wasi-runtime-config API" | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
wasmtime = { workspace = true, features = ["runtime", "component-model"] } | ||
|
||
[dev-dependencies] | ||
test-programs-artifacts = { workspace = true } | ||
wasmtime-wasi = { workspace = true } | ||
tokio = { workspace = true, features = ["macros"] } |
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,68 @@ | ||
use anyhow::Result; | ||
use std::collections::HashMap; | ||
|
||
mod gen_ { | ||
wasmtime::component::bindgen!({ | ||
path: "wit", | ||
world: "wasi:config/imports", | ||
trappable_imports: true, | ||
}); | ||
} | ||
use self::gen_::wasi::config::runtime as generated; | ||
|
||
/// Capture the state necessary for use in the `wasi-runtime-config` API implementation. | ||
pub struct WasiRuntimeConfigCtx { | ||
runtime_config: HashMap<String, String>, | ||
} | ||
|
||
impl WasiRuntimeConfigCtx { | ||
/// Create a new context. | ||
pub fn new<S, I>(config: I) -> Self | ||
where | ||
S: Into<String>, | ||
I: IntoIterator<Item = (S, S)>, | ||
{ | ||
Self { | ||
runtime_config: config | ||
.into_iter() | ||
.map(|(k, v)| (k.into(), v.into())) | ||
.collect(), | ||
} | ||
} | ||
} | ||
|
||
/// A wrapper capturing the needed internal `wasi-runtime-config` state. | ||
pub struct WasiRuntimeConfigView<'a> { | ||
ctx: &'a WasiRuntimeConfigCtx, | ||
} | ||
|
||
impl<'a> WasiRuntimeConfigView<'a> { | ||
/// Create a new view into the `wasi-runtime-config` state. | ||
pub fn new(ctx: &'a WasiRuntimeConfigCtx) -> Self { | ||
Self { ctx } | ||
} | ||
} | ||
|
||
impl generated::Host for WasiRuntimeConfigView<'_> { | ||
fn get(&mut self, key: String) -> Result<Result<Option<String>, generated::ConfigError>> { | ||
Ok(Ok(self.ctx.runtime_config.get(&key).map(|s| s.to_owned()))) | ||
} | ||
|
||
fn get_all(&mut self) -> Result<Result<Vec<(String, String)>, generated::ConfigError>> { | ||
Ok(Ok(self | ||
.ctx | ||
.runtime_config | ||
.iter() | ||
.map(|(k, v)| (k.to_string(), v.to_string())) | ||
.collect())) | ||
} | ||
} | ||
|
||
/// Add all the `wasi-runtime-config` world's interfaces to a [`wasmtime::component::Linker`]. | ||
pub fn add_to_linker<T>( | ||
l: &mut wasmtime::component::Linker<T>, | ||
f: impl Fn(&mut T) -> WasiRuntimeConfigView + Send + Sync + Copy + 'static, | ||
) -> Result<()> { | ||
generated::add_to_linker_get_host(l, f)?; | ||
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,58 @@ | ||
use anyhow::{anyhow, Result}; | ||
use wasmtime::{ | ||
component::{Component, Linker, ResourceTable}, | ||
Store, | ||
}; | ||
use wasmtime_wasi::{add_to_linker_async, bindings::Command, WasiCtx, WasiCtxBuilder, WasiView}; | ||
use wasmtime_wasi_runtime_config::{WasiRuntimeConfigCtx, WasiRuntimeConfigView}; | ||
|
||
struct Ctx { | ||
table: ResourceTable, | ||
wasi_runtime_config_ctx: WasiRuntimeConfigCtx, | ||
wasi_ctx: WasiCtx, | ||
} | ||
|
||
impl WasiView for Ctx { | ||
fn table(&mut self) -> &mut ResourceTable { | ||
&mut self.table | ||
} | ||
|
||
fn ctx(&mut self) -> &mut WasiCtx { | ||
&mut self.wasi_ctx | ||
} | ||
} | ||
|
||
async fn run_wasi(path: &str, ctx: Ctx) -> Result<()> { | ||
let engine = test_programs_artifacts::engine(|config| { | ||
config.async_support(true); | ||
}); | ||
let component = Component::from_file(&engine, path)?; | ||
|
||
let mut linker = Linker::new(&engine); | ||
add_to_linker_async(&mut linker)?; | ||
wasmtime_wasi_runtime_config::add_to_linker(&mut linker, |h: &mut Ctx| { | ||
WasiRuntimeConfigView::new(&h.wasi_runtime_config_ctx) | ||
})?; | ||
|
||
let mut store = Store::new(&engine, ctx); | ||
|
||
let command = Command::instantiate_async(&mut store, &component, &linker).await?; | ||
command | ||
.wasi_cli_run() | ||
.call_run(&mut store) | ||
.await? | ||
.map_err(|()| anyhow!("command returned with failing exit status")) | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn runtime_config_get() -> Result<()> { | ||
run_wasi( | ||
test_programs_artifacts::RUNTIME_CONFIG_GET_COMPONENT, | ||
Ctx { | ||
table: ResourceTable::new(), | ||
wasi_ctx: WasiCtxBuilder::new().build(), | ||
wasi_runtime_config_ctx: WasiRuntimeConfigCtx::new([("hello", "world")]), | ||
}, | ||
) | ||
.await | ||
} |
25 changes: 25 additions & 0 deletions
25
crates/wasi-runtime-config/wit/deps/runtime-config/runtime_config.wit
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,25 @@ | ||
interface runtime { | ||
/// An error type that encapsulates the different errors that can occur fetching config | ||
variant config-error { | ||
/// This indicates an error from an "upstream" config source. | ||
/// As this could be almost _anything_ (such as Vault, Kubernetes ConfigMaps, KeyValue buckets, etc), | ||
/// the error message is a string. | ||
upstream(string), | ||
/// This indicates an error from an I/O operation. | ||
/// As this could be almost _anything_ (such as a file read, network connection, etc), | ||
/// the error message is a string. | ||
/// Depending on how this ends up being consumed, | ||
/// we may consider moving this to use the `wasi:io/error` type instead. | ||
/// For simplicity right now in supporting multiple implementations, it is being left as a string. | ||
io(string), | ||
} | ||
|
||
/// Gets a single opaque config value set at the given key if it exists | ||
get: func( | ||
/// A string key to fetch | ||
key: string | ||
) -> result<option<string>, config-error>; | ||
|
||
/// Gets a list of all set config data | ||
get-all: func() -> result<list<tuple<string, string>>, config-error>; | ||
} |
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 wasi:config@0.2.0-draft; | ||
|
||
world imports { | ||
/// The runtime interface for config | ||
import runtime; | ||
} |
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 @@ | ||
// We actually don't use this; it's just to let bindgen! find the corresponding world in wit/deps. | ||
package wasmtime:wasi; | ||
|
||
world bindings { | ||
include wasi:config/imports@0.2.0-draft; | ||
} |
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