feat: copied from github.com/pmalmgren/wasi-data-sharing
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -32,10 +32,6 @@ Temporary Items
|
||||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
1550
Cargo.lock
generated
Normal file
1550
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
Cargo.toml
Normal file
19
Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "wasi-demo"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[[example]]
|
||||
name = "wasi"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
anyhow = "1.0.56"
|
||||
wasmtime = "0.35.2"
|
||||
wasmtime-wasi = "0.35.2"
|
||||
wasi-common = "0.35.2"
|
||||
7
LICENSE
Normal file
7
LICENSE
Normal file
@@ -0,0 +1,7 @@
|
||||
Copyright 2022 Peter Malmgren
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
18
README.md
18
README.md
@@ -1,3 +1,17 @@
|
||||
# pmalmgren-wasi-data-sharing
|
||||
# Sharing data between hosts with stdio
|
||||
|
||||
From: https://github.com/pmalmgren/wasi-data-sharing
|
||||
This repository has an accompanying [blog post](https://petermalmgren.com/serverside-wasm-data/).
|
||||
|
||||
## Running
|
||||
|
||||
### 1. Build the WASM
|
||||
|
||||
```bash
|
||||
$ cargo build --target wasm32-wasi
|
||||
```
|
||||
|
||||
### 2. Run the example
|
||||
|
||||
```bash
|
||||
$ cargo run --example wasi
|
||||
```
|
||||
57
examples/wasi/main.rs
Normal file
57
examples/wasi/main.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use anyhow::Result;
|
||||
use wasi_common::pipe::{ReadPipe, WritePipe};
|
||||
use wasmtime::*;
|
||||
use wasmtime_wasi::sync::WasiCtxBuilder;
|
||||
use wire::Input;
|
||||
|
||||
use crate::wire::Output;
|
||||
|
||||
mod wire;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// Define the WASI functions globally on the `Config`.
|
||||
let engine = Engine::default();
|
||||
let mut linker = Linker::new(&engine);
|
||||
wasmtime_wasi::add_to_linker(&mut linker, |s| s)?;
|
||||
|
||||
let input = Input {
|
||||
name: "Rust".into(),
|
||||
num: 10,
|
||||
};
|
||||
let serialized_input = serde_json::to_string(&input)?;
|
||||
|
||||
let stdin = ReadPipe::from(serialized_input);
|
||||
let stdout = WritePipe::new_in_memory();
|
||||
|
||||
let wasi = WasiCtxBuilder::new()
|
||||
.stdin(Box::new(stdin.clone()))
|
||||
.stdout(Box::new(stdout.clone()))
|
||||
.build();
|
||||
|
||||
let module = Module::from_file(&engine, "target/wasm32-wasi/debug/wasi-demo.wasm")?;
|
||||
|
||||
let mut store = Store::new(&engine, wasi);
|
||||
|
||||
linker
|
||||
.module(&mut store, "", &module)
|
||||
.expect("linking the function");
|
||||
linker
|
||||
.get_default(&mut store, "")
|
||||
.expect("should get the wasi runtime")
|
||||
.typed::<(), (), _>(&store)
|
||||
.expect("should type the function")
|
||||
.call(&mut store, ())
|
||||
.expect("should call the function");
|
||||
|
||||
drop(store);
|
||||
|
||||
let contents: Vec<u8> = stdout
|
||||
.try_into_inner()
|
||||
.map_err(|_err| anyhow::Error::msg("sole remaining reference"))?
|
||||
.into_inner();
|
||||
let output: Output = serde_json::from_slice(&contents)?;
|
||||
|
||||
println!("output: {:?}", output);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
12
examples/wasi/wire.rs
Normal file
12
examples/wasi/wire.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Input {
|
||||
pub name: String,
|
||||
pub num: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Output {
|
||||
pub names: Vec<String>,
|
||||
}
|
||||
24
src/main.rs
Normal file
24
src/main.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use std::{error::Error, io::stdin};
|
||||
|
||||
use wire::{Input, Output};
|
||||
|
||||
mod wire;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let input: Input = serde_json::from_reader(stdin()).map_err(|e| {
|
||||
eprintln!("ser: {e}");
|
||||
e
|
||||
})?;
|
||||
|
||||
let names: Vec<String> = (0..input.num).map(|_idx| input.name.clone()).collect();
|
||||
|
||||
let output = Output { names };
|
||||
let serialized = serde_json::to_string(&output).map_err(|e| {
|
||||
eprintln!("de: {e}");
|
||||
e
|
||||
})?;
|
||||
|
||||
println!("{serialized}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
12
src/wire.rs
Normal file
12
src/wire.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Input {
|
||||
pub name: String,
|
||||
pub num: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Output {
|
||||
pub names: Vec<String>,
|
||||
}
|
||||
Reference in New Issue
Block a user