Skip to content

Commit 812404f

Browse files
committed
move from async_trait to BoxFuture
1 parent 78f66c3 commit 812404f

File tree

11 files changed

+160
-130
lines changed

11 files changed

+160
-130
lines changed

core/src/host_imports/lowering/lo_helper.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ use it_lilo::traits::AllocatableError;
2525
use it_memory_traits::MemoryView;
2626
use it_memory_traits::Memory;
2727

28+
use futures::future::BoxFuture;
29+
use futures::FutureExt;
30+
2831
use std::marker::PhantomData;
2932

3033
pub(crate) struct LoHelper<
@@ -54,23 +57,25 @@ impl<
5457
}
5558
}
5659

57-
#[async_trait::async_trait]
5860
impl<
5961
's,
6062
WB: WasmBackend,
6163
MV: MemoryView<DelayedContextLifetime<WB>>,
6264
M: Memory<MV, DelayedContextLifetime<WB>>,
6365
> Allocatable<MV, DelayedContextLifetime<WB>> for LoHelper<'s, WB, MV, M>
6466
{
65-
async fn allocate<'ctx1, 'ctx2: 'ctx1>(
66-
&mut self,
67+
fn allocate<'this, 'ctx1: 'this, 'ctx2: 'ctx1>(
68+
&'this mut self,
6769
store: &'ctx1 mut <WB as WasmBackend>::ContextMut<'ctx2>,
6870
size: u32,
6971
type_tag: u32,
70-
) -> Result<(u32, MV), AllocatableError> {
71-
let offset = (self.allocate_func)(store, (size as _, type_tag as _))
72-
.await
73-
.unwrap();
74-
Ok((offset as u32, self.memory.view()))
72+
) -> BoxFuture<'this, Result<(u32, MV), AllocatableError>> {
73+
async move {
74+
let offset = (self.allocate_func)(store, (size as _, type_tag as _))
75+
.await
76+
.unwrap();
77+
Ok((offset as u32, self.memory.view()))
78+
}
79+
.boxed()
7580
}
7681
}

core/src/module/exports.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use super::IType;
1919
use super::IFunctionArg;
2020
use wasmer_it::interpreter::wasm;
2121

22+
use futures::future::BoxFuture;
23+
use futures::FutureExt;
24+
2225
// In current implementation export simply does nothing, because there is no more
2326
// explicit instruction call-export in this version of wasmer-interface-types,
2427
// but explicit Exports is still required by wasmer-interface-types::Interpreter.
@@ -42,7 +45,6 @@ impl ITExport {
4245
}
4346
}
4447

45-
#[async_trait::async_trait]
4648
impl wasm::structures::Export for ITExport {
4749
fn name(&self) -> &str {
4850
self.name.as_str()
@@ -64,7 +66,10 @@ impl wasm::structures::Export for ITExport {
6466
&self.outputs
6567
}
6668

67-
async fn call_async(&self, arguments: &[IValue]) -> Result<Vec<IValue>, anyhow::Error> {
68-
(self.function)(arguments)
69+
fn call_async<'args>(
70+
&'args self,
71+
arguments: &'args [IValue],
72+
) -> BoxFuture<'args, Result<Vec<IValue>, anyhow::Error>> {
73+
async move { (self.function)(arguments) }.boxed()
6974
}
7075
}

core/src/module/wit_function.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use marine_wasm_backend_traits::ExportFunction;
2929
use wasmer_it::interpreter::wasm;
3030

3131
use anyhow::anyhow;
32+
use futures::future::BoxFuture;
33+
use futures::FutureExt;
3234

3335
use std::sync::Arc;
3436

@@ -114,7 +116,6 @@ impl<WB: WasmBackend> WITFunction<WB> {
114116
}
115117
}
116118

117-
#[async_trait::async_trait]
118119
impl<WB: WasmBackend> wasm::structures::LocalImport<DelayedContextLifetime<WB>>
119120
for WITFunction<WB>
120121
{
@@ -138,31 +139,34 @@ impl<WB: WasmBackend> wasm::structures::LocalImport<DelayedContextLifetime<WB>>
138139
&self.outputs
139140
}
140141

141-
async fn call_async(
142-
&self,
143-
store: &mut <WB as WasmBackend>::ContextMut<'_>,
144-
arguments: &[IValue],
145-
) -> std::result::Result<Vec<IValue>, anyhow::Error> {
146-
use super::type_converters::wval_to_ival;
147-
use super::type_converters::ival_to_wval;
148-
match &self.inner {
149-
WITFunctionInner::Export { func, .. } => func
150-
.as_ref()
151-
.call(
152-
store,
153-
arguments
154-
.iter()
155-
.map(ival_to_wval)
156-
.collect::<Vec<WValue>>()
157-
.as_slice(),
158-
)
159-
.await
160-
.map_err(|e| anyhow!(e))
161-
.map(|results| results.iter().map(wval_to_ival).collect()),
162-
WITFunctionInner::Import { callable, .. } => Arc::make_mut(&mut callable.clone())
163-
.call(store, arguments)
164-
.await
165-
.map_err(|e| anyhow!(e)),
142+
fn call_async<'args>(
143+
&'args self,
144+
store: &'args mut <WB as WasmBackend>::ContextMut<'_>,
145+
arguments: &'args [IValue],
146+
) -> BoxFuture<'args, anyhow::Result<Vec<IValue>>> {
147+
async move {
148+
use super::type_converters::wval_to_ival;
149+
use super::type_converters::ival_to_wval;
150+
match &self.inner {
151+
WITFunctionInner::Export { func, .. } => func
152+
.as_ref()
153+
.call(
154+
store,
155+
arguments
156+
.iter()
157+
.map(ival_to_wval)
158+
.collect::<Vec<WValue>>()
159+
.as_slice(),
160+
)
161+
.await
162+
.map_err(|e| anyhow!(e))
163+
.map(|results| results.iter().map(wval_to_ival).collect()),
164+
WITFunctionInner::Import { callable, .. } => Arc::make_mut(&mut callable.clone())
165+
.call(store, arguments)
166+
.await
167+
.map_err(|e| anyhow!(e)),
168+
}
166169
}
170+
.boxed()
167171
}
168172
}

crates/js-backend/src/function.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
use std::future::Future;
1817
use crate::JsInstance;
1918
use crate::JsWasmBackend;
2019
use crate::JsImportCallContext;
@@ -33,9 +32,12 @@ use marine_wasm_backend_traits::replace_with;
3332
use marine_wasm_backend_traits::prelude::*;
3433

3534
use anyhow::anyhow;
35+
use futures::future::BoxFuture;
36+
use futures::FutureExt;
3637
use js_sys::Array;
3738
use wasm_bindgen::prelude::*;
38-
use futures::future::BoxFuture;
39+
40+
use std::future::Future;
3941

4042
// Safety: this is safe because its intended to run in single thread
4143
unsafe impl Send for HostImportFunction {}
@@ -239,7 +241,11 @@ impl HostFunction<JsWasmBackend> for HostImportFunction {
239241
}
240242

241243
impl AsyncFunction<JsWasmBackend> for HostImportFunction {
242-
async fn call_async<CTX>(&self, store: &mut CTX, args: &[WValue]) -> RuntimeResult<Vec<WValue>>
244+
fn call_async<CTX>(
245+
&self,
246+
store: &mut CTX,
247+
args: &[WValue],
248+
) -> BoxFuture<RuntimeResult<Vec<WValue>>>
243249
where
244250
CTX: AsContextMut<JsWasmBackend> + Send,
245251
{
@@ -331,28 +337,31 @@ fn prepare_js_closure(func: Box<dyn FnMut(&Array) -> Array>) -> js_sys::Function
331337

332338
wrapper.bind1(&JsValue::UNDEFINED, &closure)
333339
}
334-
#[async_trait::async_trait]
340+
335341
impl ExportFunction<JsWasmBackend> for WasmExportFunction {
336342
fn signature(&self, store: &mut impl AsContextMut<JsWasmBackend>) -> FuncSig {
337343
self.stored_mut(store.as_context_mut()).signature.clone()
338344
}
339345

340-
async fn call(
341-
&self,
342-
store: &mut impl AsContextMut<JsWasmBackend>,
343-
args: &[WValue],
344-
) -> RuntimeResult<Vec<WValue>> {
345-
store
346-
.as_context_mut()
347-
.inner
348-
.wasm_call_stack
349-
.push(self.bound_instance.clone());
346+
fn call<'args>(
347+
&'args self,
348+
store: &'args mut impl AsContextMut<JsWasmBackend>,
349+
args: &'args [WValue],
350+
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>> {
351+
async move {
352+
store
353+
.as_context_mut()
354+
.inner
355+
.wasm_call_stack
356+
.push(self.bound_instance.clone());
350357

351-
let result = self.call_inner(store, args);
358+
let result = self.call_inner(store, args);
352359

353-
store.as_context_mut().inner.wasm_call_stack.pop();
360+
store.as_context_mut().inner.wasm_call_stack.pop();
354361

355-
result
362+
result
363+
}
364+
.boxed()
356365
}
357366
}
358367

crates/js-backend/src/module.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use crate::module_info::ModuleInfo;
2323
use marine_wasm_backend_traits::prelude::*;
2424

2525
use anyhow::anyhow;
26+
use futures::future::BoxFuture;
27+
use futures::FutureExt;
2628
use js_sys::WebAssembly;
2729
use js_sys::Uint8Array;
2830
use wasm_bindgen::JsValue;
@@ -35,7 +37,6 @@ pub struct JsModule {
3537
unsafe impl Send for JsModule {}
3638
unsafe impl Sync for JsModule {}
3739

38-
//#[async_trait::async_trait]
3940
impl Module<JsWasmBackend> for JsModule {
4041
fn new(_store: &mut JsStore, wasm: &[u8]) -> ModuleCreationResult<Self> {
4142
let data = Uint8Array::new_with_length(wasm.len() as u32);
@@ -68,23 +69,28 @@ impl Module<JsWasmBackend> for JsModule {
6869
}
6970
}
7071

71-
async fn instantiate(
72-
&self,
73-
store: &mut JsStore,
74-
imports: &JsImports,
75-
) -> InstantiationResult<<JsWasmBackend as WasmBackend>::Instance> {
76-
let imports_object = imports.build_import_object(store.as_context(), &self.inner);
77-
let instance = WebAssembly::Instance::new(&self.inner, &imports_object)
78-
.map_err(|e| InstantiationError::Other(anyhow!("failed to instantiate: {:?}", e)))?;
72+
fn instantiate<'args>(
73+
&'args self,
74+
store: &'args mut JsStore,
75+
imports: &'args JsImports,
76+
) -> BoxFuture<'args, InstantiationResult<<JsWasmBackend as WasmBackend>::Instance>> {
77+
async move {
78+
let imports_object = imports.build_import_object(store.as_context(), &self.inner);
79+
let instance =
80+
WebAssembly::Instance::new(&self.inner, &imports_object).map_err(|e| {
81+
InstantiationError::Other(anyhow!("failed to instantiate: {:?}", e))
82+
})?;
7983

80-
// adds memory to @wasmer/wasi object
81-
imports.bind_to_instance(store.as_context(), &instance);
84+
// adds memory to @wasmer/wasi object
85+
imports.bind_to_instance(store.as_context(), &instance);
8286

83-
let stored_instance = JsInstance::new(
84-
&mut store.as_context_mut(),
85-
instance,
86-
self.module_info.clone(),
87-
);
88-
Ok(stored_instance)
87+
let stored_instance = JsInstance::new(
88+
&mut store.as_context_mut(),
89+
instance,
90+
self.module_info.clone(),
91+
);
92+
Ok(stored_instance)
93+
}
94+
.boxed()
8995
}
9096
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ use crate::WValue;
2424

2525
use futures::future::BoxFuture;
2626

27-
//use async_trait::async_trait;
28-
2927
/// A host function ready to be used as an import for instantiating a module.
3028
/// As it is only a handle to an object in `Store`, cloning is cheap.
3129
pub trait HostFunction<WB: WasmBackend>: AsyncFunction<WB> + Send + Sync + Clone {
@@ -80,7 +78,6 @@ pub trait HostFunction<WB: WasmBackend>: AsyncFunction<WB> + Send + Sync + Clone
8078

8179
/// A Wasm function handle, it can be either a function from a host or an export from an `Instance`.
8280
/// As it is only a handle to an object in `Store`, cloning is cheap
83-
//#[async_trait]
8481
pub trait ExportFunction<WB: WasmBackend>: Send + Sync + Clone {
8582
/// Returns the signature of the function.
8683
/// The signature is constructed each time this function is called, so
@@ -92,16 +89,19 @@ pub trait ExportFunction<WB: WasmBackend>: Send + Sync + Clone {
9289
/// If given a store different from the one that stores the function.
9390
/// # Errors:
9491
/// See `RuntimeError` documentation.
95-
fn call<'store>(
96-
&self,
97-
store: &'store mut impl AsContextMut<WB>,
98-
args: &[WValue],
99-
) -> impl Future<Output = RuntimeResult<Vec<WValue>>> + Send + 'store;
92+
fn call<'args>(
93+
&'args self,
94+
store: &'args mut impl AsContextMut<WB>,
95+
args: &'args [WValue],
96+
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>>;
10097
}
10198

102-
//#[async_trait]
10399
pub trait AsyncFunction<WB: WasmBackend> {
104-
async fn call_async<CTX>(&self, store: &mut CTX, args: &[WValue]) -> RuntimeResult<Vec<WValue>>
100+
fn call_async<'args, CTX>(
101+
&'args self,
102+
store: &'args mut CTX,
103+
args: &'args [WValue],
104+
) -> BoxFuture<'args, RuntimeResult<Vec<WValue>>>
105105
where
106106
CTX: AsContextMut<WB> + Send;
107107
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
#![feature(async_fn_in_trait)]
18-
#![feature(return_position_impl_trait_in_trait)]
19-
2017
pub mod errors;
2118
pub mod exports;
2219
pub mod imports;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ use crate::ModuleCreationResult;
1818
use crate::InstantiationResult;
1919
use crate::WasmBackend;
2020

21-
use async_trait::async_trait;
21+
use futures::future::BoxFuture;
2222

2323
/// A handle to compiled wasm module.
24-
//#[async_trait]
2524
pub trait Module<WB: WasmBackend>: Sized {
2625
/// Compiles a wasm bytes into a module and extracts custom sections.
2726
fn new(store: &mut <WB as WasmBackend>::Store, wasm: &[u8]) -> ModuleCreationResult<Self>;
@@ -35,9 +34,9 @@ pub trait Module<WB: WasmBackend>: Sized {
3534
/// # Panics:
3635
///
3736
/// If the `Store` given is not the same with `Store` used to create `Imports` and this object.
38-
async fn instantiate(
39-
&self,
40-
store: &mut <WB as WasmBackend>::Store,
41-
imports: &<WB as WasmBackend>::Imports,
42-
) -> InstantiationResult<<WB as WasmBackend>::Instance>;
37+
fn instantiate<'args>(
38+
&'args self,
39+
store: &'args mut <WB as WasmBackend>::Store,
40+
imports: &'args <WB as WasmBackend>::Imports,
41+
) -> BoxFuture<'args, InstantiationResult<<WB as WasmBackend>::Instance>>;
4342
}

0 commit comments

Comments
 (0)