Skip to content

Commit 6aa8da5

Browse files
committed
self-review fixes
1 parent bf11145 commit 6aa8da5

File tree

9 files changed

+28
-78
lines changed

9 files changed

+28
-78
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/host_imports/lowering/lo_helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<
6060
M: Memory<MV, DelayedContextLifetime<WB>>,
6161
> Allocatable<MV, DelayedContextLifetime<WB>> for LoHelper<WB, MV, M>
6262
{
63-
fn allocate<'this, 'ctx1: 'this, 'ctx2: 'ctx1>(
63+
fn allocate<'this, 'ctx1: 'this, 'ctx2: 'this>(
6464
&'this mut self,
6565
store: &'ctx1 mut <WB as WasmBackend>::ContextMut<'ctx2>,
6666
size: u32,

crates/js-backend/src/function.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ use futures::FutureExt;
3737
use js_sys::Array;
3838
use wasm_bindgen::prelude::*;
3939

40-
use std::future::Future;
41-
4240
// Safety: this is safe because its intended to run in single thread
4341
unsafe impl Send for HostImportFunction {}
4442
unsafe impl Sync for HostImportFunction {}
@@ -220,12 +218,12 @@ impl HostFunction<JsWasmBackend> for HostImportFunction {
220218

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

231229
fn new_typed<Params, Results, Env>(
@@ -240,19 +238,6 @@ impl HostFunction<JsWasmBackend> for HostImportFunction {
240238
}
241239
}
242240

243-
impl AsyncFunction<JsWasmBackend> for HostImportFunction {
244-
fn call_async<CTX>(
245-
&self,
246-
store: &mut CTX,
247-
args: &[WValue],
248-
) -> BoxFuture<RuntimeResult<Vec<WValue>>>
249-
where
250-
CTX: AsContextMut<JsWasmBackend> + Send,
251-
{
252-
todo!()
253-
}
254-
}
255-
256241
fn wrap_raw_host_fn<F>(
257242
signature: FuncSig,
258243
store_inner_ptr: *mut JsStoreInner,

crates/js-backend/src/single_shot_async_executor.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use futures::{
2-
future::{BoxFuture, FutureExt},
3-
task::{waker_ref, ArcWake},
4-
};
5-
use std::{
6-
future::Future,
7-
sync::{Arc},
8-
task::Context,
9-
};
1+
use marine_wasm_backend_traits::WValue;
2+
3+
use futures::future::BoxFuture;
4+
use futures::task::waker_ref;
5+
use futures::task::ArcWake;
106

7+
use std::future::Future;
118
use std::pin::Pin;
9+
use std::sync::Arc;
10+
use std::task::Context;
1211
use std::task::Poll;
13-
use marine_wasm_backend_traits::WValue;
1412

1513
struct DummyTask();
1614
impl ArcWake for DummyTask {
@@ -23,6 +21,8 @@ pub(crate) fn execute_future_blocking(
2321
let task = Arc::new(DummyTask());
2422
let waker = waker_ref(&task);
2523
let context = &mut Context::from_waker(&waker);
24+
25+
#[allow(unused_assignments)]
2626
let mut result: anyhow::Result<Vec<WValue>> = Ok(Vec::default());
2727
loop {
2828
match Pin::new(&mut future).poll(context) {

crates/wasm-backend-traits/src/function.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ use crate::WValue;
2323

2424
use futures::future::BoxFuture;
2525

26-
use std::future::Future;
27-
2826
/// A host function ready to be used as an import for instantiating a module.
2927
/// As it is only a handle to an object in `Store`, cloning is cheap.
30-
pub trait HostFunction<WB: WasmBackend>: AsyncFunction<WB> + Send + Sync + Clone {
28+
pub trait HostFunction<WB: WasmBackend>: /*AsyncFunction<WB> + */Send + Sync + Clone {
3129
/// Creates a new function with dynamic signature.
3230
/// The signature check is performed at runtime.
3331
fn new<F>(store: &mut impl AsContextMut<WB>, sig: FuncSig, func: F) -> Self
@@ -59,7 +57,7 @@ pub trait HostFunction<WB: WasmBackend>: AsyncFunction<WB> + Send + Sync + Clone
5957
/// Creates a new function with dynamic signature that needs a context.
6058
fn new_async<F>(store: &mut impl AsContextMut<WB>, sig: FuncSig, func: F) -> Self
6159
where
62-
F: for<'c> Fn(&'c [WValue]) -> Box<dyn Future<Output = Vec<WValue>> + Send + 'c>
60+
F: for<'c> Fn(&'c [WValue]) -> BoxFuture<'c, anyhow::Result<Vec<WValue>>>
6361
+ Sync
6462
+ Send
6563
+ 'static;
@@ -97,16 +95,6 @@ pub trait ExportFunction<WB: WasmBackend>: Send + Sync + Clone {
9795
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>>;
9896
}
9997

100-
pub trait AsyncFunction<WB: WasmBackend> {
101-
fn call_async<'args, CTX>(
102-
&'args self,
103-
store: &'args mut CTX,
104-
args: &'args [WValue],
105-
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>>
106-
where
107-
CTX: AsContextMut<WB> + Send;
108-
}
109-
11098
/// A helper trait for creating a function with a static signature.
11199
/// Should not be implemented by users.
112100
/// Implemented for all functions that meet the following criteria:

crates/wasmtime-backend/src/caller.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ use marine_wasm_backend_traits::prelude::*;
2424

2525
use wasmtime::AsContext as WasmtimeAsContext;
2626
use wasmtime::AsContextMut as WasmtimeAsContextMut;
27+
28+
// these imports are used in the macro `impl_func_getter`, but compiler does not detect it
29+
#[allow(unused)]
2730
use futures::future::BoxFuture;
31+
#[allow(unused)]
2832
use futures::FutureExt;
2933

34+
#[allow(unused)]
3035
use std::sync::Arc;
3136

3237
pub struct WasmtimeImportCallContext<'c> {

crates/wasmtime-backend/src/function.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ impl HostFunction<WasmtimeWasmBackend> for WasmtimeFunction {
129129
func: F,
130130
) -> Self
131131
where
132-
F: for<'c> Fn(&'c [WValue]) -> Box<dyn Future<Output = Vec<WValue>> + Send + 'c>
132+
F: for<'c> Fn(&'c [WValue]) -> BoxFuture<'c, anyhow::Result<Vec<WValue>>>
133133
+ Sync
134134
+ Send
135135
+ 'static,
136136
{
137-
todo!()
137+
Self::new_with_caller_async(store, sig, move |_caller, args| func(args))
138138
}
139139

140140
fn new_typed<Params, Results, Env>(
@@ -180,36 +180,6 @@ impl ExportFunction<WasmtimeWasmBackend> for WasmtimeFunction {
180180
}
181181
}
182182

183-
impl AsyncFunction<WasmtimeWasmBackend> for WasmtimeFunction {
184-
fn call_async<'args, CTX>(
185-
&'args self,
186-
store: &'args mut CTX,
187-
args: &'args [WValue],
188-
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>>
189-
where
190-
CTX: AsContextMut<WasmtimeWasmBackend> + Send,
191-
{
192-
async move {
193-
let mut context = store.as_context_mut().inner;
194-
195-
let args = args.iter().map(wvalue_to_val).collect::<Vec<_>>();
196-
let results_count = self.inner.ty(&mut context).results().len();
197-
let mut results = vec![wasmtime::Val::null(); results_count];
198-
199-
self.inner
200-
.call_async(&mut context, &args, &mut results)
201-
.await
202-
.map_err(inspect_call_error)?;
203-
204-
results
205-
.iter()
206-
.map(val_to_wvalue)
207-
.collect::<Result<Vec<_>, _>>()
208-
}
209-
.boxed()
210-
}
211-
}
212-
213183
/// Generates a function that accepts a Fn with $num template parameters and turns it into WasmtimeFunction.
214184
/// Needed to allow users to pass almost any function to `Function::new_typed` without worrying about signature.
215185
macro_rules! impl_func_construction {

marine-js/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ crate-type = ["cdylib"]
1212
[dependencies]
1313
marine-js-backend = {path = "../crates/js-backend", version = "0.2.1"}
1414
marine-runtime = {path = "../marine", default-features = false}
15+
marine-wasm-backend-traits = {path = "../crates/wasm-backend-traits", version = "0.4.0"}
1516

1617
wasm-bindgen = "0.2.86"
1718
once_cell = "1.18.0"

marine-js/src/api.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use marine::generic::MarineModuleConfig;
2323
use marine::generic::ModuleDescriptor;
2424
use marine::MarineWASIConfig;
2525
use marine_js_backend::JsWasmBackend;
26+
use marine_wasm_backend_traits::WasmBackend;
2627

2728
use serde::Serialize;
2829
use serde::Deserialize;
@@ -31,7 +32,6 @@ use wasm_bindgen::prelude::*;
3132

3233
use std::collections::HashMap;
3334
use std::collections::HashSet;
34-
use std::ops::DerefMut;
3535
use std::path::PathBuf;
3636

3737
#[derive(Serialize, Deserialize)]
@@ -161,7 +161,9 @@ pub async fn register_module(
161161

162162
marine_logger().enable_service_logging(log_fn, module_names);
163163

164-
let new_marine = Marine::<JsWasmBackend>::with_modules(modules, marine_config).await?;
164+
let backend = JsWasmBackend::new()?;
165+
166+
let new_marine = Marine::<JsWasmBackend>::with_modules(backend, modules, marine_config).await?;
165167

166168
marine.replace(new_marine);
167169

0 commit comments

Comments
 (0)