Skip to content

Commit

Permalink
self-review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeryAntopol committed Dec 5, 2023
1 parent bf11145 commit 6aa8da5
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 78 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion core/src/host_imports/lowering/lo_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<
M: Memory<MV, DelayedContextLifetime<WB>>,
> Allocatable<MV, DelayedContextLifetime<WB>> for LoHelper<WB, MV, M>
{
fn allocate<'this, 'ctx1: 'this, 'ctx2: 'ctx1>(
fn allocate<'this, 'ctx1: 'this, 'ctx2: 'this>(
&'this mut self,
store: &'ctx1 mut <WB as WasmBackend>::ContextMut<'ctx2>,
size: u32,
Expand Down
19 changes: 2 additions & 17 deletions crates/js-backend/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ use futures::FutureExt;
use js_sys::Array;
use wasm_bindgen::prelude::*;

use std::future::Future;

// Safety: this is safe because its intended to run in single thread
unsafe impl Send for HostImportFunction {}
unsafe impl Sync for HostImportFunction {}
Expand Down Expand Up @@ -220,12 +218,12 @@ impl HostFunction<JsWasmBackend> for HostImportFunction {

fn new_async<F>(store: &mut impl AsContextMut<JsWasmBackend>, sig: FuncSig, func: F) -> Self
where
F: for<'c> Fn(&'c [WValue]) -> Box<dyn Future<Output = Vec<WValue>> + Send + 'c>
F: for<'c> Fn(&'c [WValue]) -> BoxFuture<'c, anyhow::Result<Vec<WValue>>>
+ Sync
+ Send
+ 'static,
{
todo!()
Self::new_with_caller_async(store, sig, move |_caller, args| func(args))
}

fn new_typed<Params, Results, Env>(
Expand All @@ -240,19 +238,6 @@ impl HostFunction<JsWasmBackend> for HostImportFunction {
}
}

impl AsyncFunction<JsWasmBackend> for HostImportFunction {
fn call_async<CTX>(
&self,
store: &mut CTX,
args: &[WValue],
) -> BoxFuture<RuntimeResult<Vec<WValue>>>
where
CTX: AsContextMut<JsWasmBackend> + Send,
{
todo!()
}
}

fn wrap_raw_host_fn<F>(
signature: FuncSig,
store_inner_ptr: *mut JsStoreInner,
Expand Down
20 changes: 10 additions & 10 deletions crates/js-backend/src/single_shot_async_executor.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use futures::{
future::{BoxFuture, FutureExt},
task::{waker_ref, ArcWake},
};
use std::{
future::Future,
sync::{Arc},
task::Context,
};
use marine_wasm_backend_traits::WValue;

use futures::future::BoxFuture;
use futures::task::waker_ref;
use futures::task::ArcWake;

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
use marine_wasm_backend_traits::WValue;

struct DummyTask();
impl ArcWake for DummyTask {
Expand All @@ -23,6 +21,8 @@ pub(crate) fn execute_future_blocking(
let task = Arc::new(DummyTask());
let waker = waker_ref(&task);
let context = &mut Context::from_waker(&waker);

#[allow(unused_assignments)]
let mut result: anyhow::Result<Vec<WValue>> = Ok(Vec::default());
loop {
match Pin::new(&mut future).poll(context) {
Expand Down
16 changes: 2 additions & 14 deletions crates/wasm-backend-traits/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ use crate::WValue;

use futures::future::BoxFuture;

use std::future::Future;

/// A host function ready to be used as an import for instantiating a module.
/// As it is only a handle to an object in `Store`, cloning is cheap.
pub trait HostFunction<WB: WasmBackend>: AsyncFunction<WB> + Send + Sync + Clone {
pub trait HostFunction<WB: WasmBackend>: /*AsyncFunction<WB> + */Send + Sync + Clone {
/// Creates a new function with dynamic signature.
/// The signature check is performed at runtime.
fn new<F>(store: &mut impl AsContextMut<WB>, sig: FuncSig, func: F) -> Self
Expand Down Expand Up @@ -59,7 +57,7 @@ pub trait HostFunction<WB: WasmBackend>: AsyncFunction<WB> + Send + Sync + Clone
/// Creates a new function with dynamic signature that needs a context.
fn new_async<F>(store: &mut impl AsContextMut<WB>, sig: FuncSig, func: F) -> Self
where
F: for<'c> Fn(&'c [WValue]) -> Box<dyn Future<Output = Vec<WValue>> + Send + 'c>
F: for<'c> Fn(&'c [WValue]) -> BoxFuture<'c, anyhow::Result<Vec<WValue>>>
+ Sync
+ Send
+ 'static;
Expand Down Expand Up @@ -97,16 +95,6 @@ pub trait ExportFunction<WB: WasmBackend>: Send + Sync + Clone {
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>>;
}

pub trait AsyncFunction<WB: WasmBackend> {
fn call_async<'args, CTX>(
&'args self,
store: &'args mut CTX,
args: &'args [WValue],
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>>
where
CTX: AsContextMut<WB> + Send;
}

/// A helper trait for creating a function with a static signature.
/// Should not be implemented by users.
/// Implemented for all functions that meet the following criteria:
Expand Down
5 changes: 5 additions & 0 deletions crates/wasmtime-backend/src/caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ use marine_wasm_backend_traits::prelude::*;

use wasmtime::AsContext as WasmtimeAsContext;
use wasmtime::AsContextMut as WasmtimeAsContextMut;

// these imports are used in the macro `impl_func_getter`, but compiler does not detect it
#[allow(unused)]
use futures::future::BoxFuture;
#[allow(unused)]
use futures::FutureExt;

#[allow(unused)]
use std::sync::Arc;

pub struct WasmtimeImportCallContext<'c> {
Expand Down
34 changes: 2 additions & 32 deletions crates/wasmtime-backend/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ impl HostFunction<WasmtimeWasmBackend> for WasmtimeFunction {
func: F,
) -> Self
where
F: for<'c> Fn(&'c [WValue]) -> Box<dyn Future<Output = Vec<WValue>> + Send + 'c>
F: for<'c> Fn(&'c [WValue]) -> BoxFuture<'c, anyhow::Result<Vec<WValue>>>
+ Sync
+ Send
+ 'static,
{
todo!()
Self::new_with_caller_async(store, sig, move |_caller, args| func(args))
}

fn new_typed<Params, Results, Env>(
Expand Down Expand Up @@ -180,36 +180,6 @@ impl ExportFunction<WasmtimeWasmBackend> for WasmtimeFunction {
}
}

impl AsyncFunction<WasmtimeWasmBackend> for WasmtimeFunction {
fn call_async<'args, CTX>(
&'args self,
store: &'args mut CTX,
args: &'args [WValue],
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>>
where
CTX: AsContextMut<WasmtimeWasmBackend> + Send,
{
async move {
let mut context = store.as_context_mut().inner;

let args = args.iter().map(wvalue_to_val).collect::<Vec<_>>();
let results_count = self.inner.ty(&mut context).results().len();
let mut results = vec![wasmtime::Val::null(); results_count];

self.inner
.call_async(&mut context, &args, &mut results)
.await
.map_err(inspect_call_error)?;

results
.iter()
.map(val_to_wvalue)
.collect::<Result<Vec<_>, _>>()
}
.boxed()
}
}

/// Generates a function that accepts a Fn with $num template parameters and turns it into WasmtimeFunction.
/// Needed to allow users to pass almost any function to `Function::new_typed` without worrying about signature.
macro_rules! impl_func_construction {
Expand Down
1 change: 1 addition & 0 deletions marine-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ crate-type = ["cdylib"]
[dependencies]
marine-js-backend = {path = "../crates/js-backend", version = "0.2.1"}
marine-runtime = {path = "../marine", default-features = false}
marine-wasm-backend-traits = {path = "../crates/wasm-backend-traits", version = "0.4.0"}

wasm-bindgen = "0.2.86"
once_cell = "1.18.0"
Expand Down
6 changes: 4 additions & 2 deletions marine-js/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use marine::generic::MarineModuleConfig;
use marine::generic::ModuleDescriptor;
use marine::MarineWASIConfig;
use marine_js_backend::JsWasmBackend;
use marine_wasm_backend_traits::WasmBackend;

use serde::Serialize;
use serde::Deserialize;
Expand All @@ -31,7 +32,6 @@ use wasm_bindgen::prelude::*;

use std::collections::HashMap;
use std::collections::HashSet;
use std::ops::DerefMut;
use std::path::PathBuf;

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -161,7 +161,9 @@ pub async fn register_module(

marine_logger().enable_service_logging(log_fn, module_names);

let new_marine = Marine::<JsWasmBackend>::with_modules(modules, marine_config).await?;
let backend = JsWasmBackend::new()?;

let new_marine = Marine::<JsWasmBackend>::with_modules(backend, modules, marine_config).await?;

marine.replace(new_marine);

Expand Down

0 comments on commit 6aa8da5

Please sign in to comment.