-
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.
Merge pull request #9 from wetware/feat/wasm_load
Merge WASM PoC
- Loading branch information
Showing
19 changed files
with
232 additions
and
56 deletions.
There are no files selected for viewing
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,3 +1,7 @@ | ||
# Web Assembly files | ||
*.wasm | ||
|
||
|
||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
|
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
File renamed without changes.
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,22 @@ | ||
use bytes::Bytes; | ||
use ipfs_api_backend_hyper::{Error, IpfsApi, IpfsClient, TryFromUri}; | ||
use ipfs_api_prelude::BoxStream; | ||
use libp2p::Multiaddr; | ||
|
||
// TODO rename and move to ipfs file | ||
pub struct Client { | ||
client: IpfsClient, | ||
} | ||
|
||
impl Client { | ||
pub fn new(addr: Multiaddr) -> Self { | ||
Self { | ||
client: IpfsClient::from_multiaddr_str(addr.to_string().as_str()) | ||
.expect("error initializing IPFS client"), | ||
} | ||
} | ||
|
||
pub fn get_file(&self, path: String) -> BoxStream<Bytes, Error> { | ||
self.client.cat(path.as_str()) | ||
} | ||
} |
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,26 @@ | ||
[package] | ||
name = "proc" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
uuid = { version = "1.10.0", features = [ | ||
"v4", # Lets you generate random UUIDs | ||
"fast-rng", # Use a faster (but still sufficiently random) RNG | ||
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs | ||
] } | ||
wasmer = { version = "4.x" } | ||
|
||
wasmer-wasix = { version = "0.24", default-features = false, features = [ | ||
"chrono", | ||
"compiler", | ||
"journal", | ||
"reqwest", | ||
"sys", | ||
"sys-poll", | ||
"sys-thread", | ||
"time", | ||
] } | ||
tracing = "0.1.37" |
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,63 @@ | ||
use uuid::Uuid; | ||
use wasmer::{self}; | ||
use wasmer_wasix::{WasiEnv, WasiFunctionEnv}; | ||
|
||
pub struct WasmProcess { | ||
function: wasmer::Function, | ||
env: WasiFunctionEnv, | ||
} | ||
|
||
impl WasmProcess { | ||
pub fn new(wasi_env: WasiFunctionEnv, function: wasmer::Function) -> Self { | ||
Self { | ||
function: function, | ||
env: wasi_env, | ||
} | ||
} | ||
|
||
pub fn run( | ||
&mut self, | ||
store: &mut wasmer::Store, | ||
) -> Result<Box<[wasmer::Value]>, wasmer::RuntimeError> { | ||
let exit_code = self.function.call(store, &[])?; | ||
self.env.on_exit(store, None); | ||
Ok(exit_code) | ||
} | ||
} | ||
|
||
pub struct WasmRuntime { | ||
store: wasmer::Store, | ||
} | ||
|
||
impl WasmRuntime { | ||
pub fn new() -> Self { | ||
Self { | ||
store: wasmer::Store::default(), | ||
} | ||
} | ||
|
||
pub fn store(&mut self) -> &mut wasmer::Store { | ||
&mut self.store | ||
} | ||
|
||
pub fn build(&mut self, bytecode: Vec<u8>) -> Result<WasmProcess, Box<dyn std::error::Error>> { | ||
let module = wasmer::Module::new(&self.store, bytecode).expect("couldn't load WASM module"); | ||
let uuid = Uuid::new_v4(); | ||
let mut wasi_env = WasiEnv::builder(uuid) | ||
// .args(&["arg1", "arg2"]) | ||
// .env("KEY", "VALUE") | ||
.finalize(&mut self.store)?; | ||
let import_object = wasi_env.import_object(&mut self.store, &module)?; | ||
let instance = wasmer::Instance::new(&mut self.store, &module, &import_object)?; | ||
|
||
// // Attach the memory export | ||
// let memory = instance.exports.get_memory("memory")?; | ||
// wasi_env.data_mut(&mut store).set_memory(memory.clone()); | ||
|
||
wasi_env.initialize(&mut self.store, instance.clone())?; | ||
|
||
let function = instance.exports.get_function("_start")?; | ||
|
||
Ok(WasmProcess::new(wasi_env, function.to_owned())) | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.