Skip to content

Commit

Permalink
more better github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwample committed Oct 18, 2023
1 parent b5e8f66 commit 4518312
Show file tree
Hide file tree
Showing 37 changed files with 1,028 additions and 639 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ env:
jobs:
build:
strategy:
fail-fast: false
fail-fast: true
matrix:
os: [ "ubuntu-latest", "macos-latest" ]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- name: Format
run: cargo fmt --all -- --check
- name: Lint
run: cargo clippy --workspace --all-targets --verbose --all-features
- name: Test
run: cargo test --verbose --workspace --all-features
- name: Build
run: cargo build --verbose
- name: Test
run: cargo test --verbose
10 changes: 8 additions & 2 deletions crates/water/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ pub struct WATERConfig {
}

impl WATERConfig {
pub fn init(wasm_path: String, entry_fn: String, config_wasm: String, client_type: u32, debug: bool) -> Result<Self, anyhow::Error> {
pub fn init(
wasm_path: String,
entry_fn: String,
config_wasm: String,
client_type: u32,
debug: bool,
) -> Result<Self, anyhow::Error> {
Ok(WATERConfig {
filepath: wasm_path,
entry_fn: entry_fn,
Expand All @@ -18,4 +24,4 @@ impl WATERConfig {
debug: debug,
})
}
}
}
11 changes: 5 additions & 6 deletions crates/water/src/config/wasm_shared_config.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use serde::{Deserialize, Serialize};
use std::mem;
use serde::{Serialize, Deserialize};

#[repr(C)]
pub struct WASMSharedConfig {

// pub key: u64, // a pointer to a key string's byte-view
// pub size: u64, // size for the key
// pub key: u64, // a pointer to a key string's byte-view
// pub size: u64, // size for the key
}

impl WASMSharedConfig {
pub fn to_bytes(&self) -> Vec<u8> {
let size = mem::size_of::<WASMSharedConfig>();
let ptr = self as *const Self;

let bytes_slice = unsafe { std::slice::from_raw_parts(ptr as *const u8, size) };
let bytes = bytes_slice.to_vec();
bytes
Expand Down Expand Up @@ -42,7 +41,7 @@ impl StreamConfig {
pub fn to_bytes(&self) -> Vec<u8> {
let size = mem::size_of::<StreamConfig>();
let ptr = self as *const Self;

let bytes_slice = unsafe { std::slice::from_raw_parts(ptr as *const u8, size) };
let bytes = bytes_slice.to_vec();
bytes
Expand Down
1 change: 1 addition & 0 deletions crates/water/src/errors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 2 additions & 2 deletions crates/water/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate wasmtime_wasi;
extern crate wasmtime_wasi_threads;

pub mod config;
pub mod runtime;
pub mod errors;
pub mod utils;
pub mod globals;
pub mod runtime;
pub mod utils;
63 changes: 37 additions & 26 deletions crates/water/src/runtime/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,72 @@ pub struct H2O<Host> {
impl H2O<Host> {
pub fn init(conf: &WATERConfig) -> Result<Self, anyhow::Error> {
info!("[HOST] WATERCore H2O initing...");

let mut wasm_config = wasmtime::Config::new();
wasm_config.wasm_threads(true);

let engine = Engine::new(&wasm_config)?;
let mut linker: Linker<Host> = Linker::new(&engine);

let module = Module::from_file(&engine, &conf.filepath)?;

let host = Host::default();
let mut store = Store::new(&engine, host);

let version = module.exports().find_map(|global| {
info!("[HOST] WATERCore finding exported symbols from WASM bin: {:?}", global.name());
info!(
"[HOST] WATERCore finding exported symbols from WASM bin: {:?}",
global.name()
);
match Version::from_str(global.name()) {
Some(v) => {
info!("[HOST] WATERCore found version: {:?}", v.as_str());
Some(v)
},
}
None => None,
}
});

if version.is_none() {
return Err(anyhow::Error::msg("WASM module version not found"));
}

// let path = unsafe { Dir::open_ambient_dir(".", ambient_authority())? };

// store.data_mut().preview1_ctx = Some(WasiCtxBuilder::new().inherit_stdio().preopened_dir(path, ".")?.build());
store.data_mut().preview1_ctx = Some(WasiCtxBuilder::new().inherit_stdio().build());

wasmtime_wasi::add_to_linker(&mut linker, |h: &mut Host| {
h.preview1_ctx.as_mut().unwrap()
})?;


wasmtime_wasi::add_to_linker(&mut linker, |h: &mut Host| h.preview1_ctx.as_mut().unwrap())?;

// initializing stuff for multithreading
#[cfg(feature = "multithread")]
{

store.data_mut().wasi_threads = Some(Arc::new(WasiThreadsCtx::new(
module.clone(),
Arc::new(linker.clone()),
)?));

wasmtime_wasi_threads::add_to_linker(&mut linker, &store, &module, |h: &mut Host| {
h.wasi_threads.as_ref().unwrap()
})?;
}


// export functions -- version dependent -- has to be done before instantiate
match &version {
Some(Version::V0) => {
v0::funcs::export_tcp_connect(&mut linker);
v0::funcs::export_tcplistener_create(&mut linker);
},
}
Some(Version::V1) => {
v1::funcs::export_tcp_connect(&mut linker);
v1::funcs::export_tcplistener_create(&mut linker);
},
}
_ => {} // add export funcs for other versions here
}

// export functions -- version independent
version_common::funcs::export_config(&mut linker, conf.config_wasm.clone());

let instance = linker.instantiate(&mut store, &module)?;

Ok(H2O {
Expand All @@ -96,7 +95,7 @@ impl H2O<Host> {
instance: instance,
store: store,
module: module,
})
})
}

pub fn _prepare(&mut self, conf: &WATERConfig) -> Result<(), anyhow::Error> {
Expand All @@ -117,7 +116,7 @@ impl H2O<Host> {
// TODO: check if we need to pass in any arguments / configs later
let params = vec![Val::I32(debug as i32); init_fn.ty(&self.store).params().len()];
match init_fn.call(&mut self.store, &params, &mut []) {
Ok(_) => {},
Ok(_) => {}
Err(e) => return Err(anyhow::Error::msg(format!("init function failed: {}", e))),
}

Expand All @@ -130,23 +129,35 @@ impl H2O<Host> {
// _required to implement _process_config(i32) in WASM, which will be parsing all the configurations
let config_fn = match self.instance.get_func(&mut self.store, CONFIG_FN) {
Some(func) => func,
None => return Err(anyhow::Error::msg("_process_config function not found in WASM")),
None => {
return Err(anyhow::Error::msg(
"_process_config function not found in WASM",
))
}
};

// open the config file and insert to WASM
let dir = Dir::open_ambient_dir(".", ambient_authority())?; // Open the root directory
let wasi_file = dir.open_with(&config.config_wasm, OpenOptions::new().read(true).write(true))?;
let wasi_file = dir.open_with(
&config.config_wasm,
OpenOptions::new().read(true).write(true),
)?;
let wasi_file = wasmtime_wasi::sync::file::File::from_cap_std(wasi_file);

let ctx = self.store.data_mut().preview1_ctx.as_mut().unwrap();
let config_fd = ctx.push_file(Box::new(wasi_file), FileAccessMode::all())? as i32;

let params = vec![Val::I32(config_fd); config_fn.ty(&self.store).params().len()];
match config_fn.call(&mut self.store, &params, &mut []) {
Ok(_) => {},
Err(e) => return Err(anyhow::Error::msg(format!("_process_config function in WASM failed: {}", e))),
Ok(_) => {}
Err(e) => {
return Err(anyhow::Error::msg(format!(
"_process_config function in WASM failed: {}",
e
)))
}
}

Ok(())
}
}
}
Loading

0 comments on commit 4518312

Please sign in to comment.