Skip to content
This repository was archived by the owner on Oct 3, 2025. It is now read-only.

Commit 9934060

Browse files
committed
Revert "feat: all async/resume functionality is now feature-gated"
This reverts commit 450881e.
1 parent 4cd03a0 commit 9934060

File tree

12 files changed

+227
-311
lines changed

12 files changed

+227
-311
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ rust-version.workspace=true
3030
name="wasm-rust"
3131
test=false
3232

33-
[[example]]
34-
name="host_coro"
35-
required-features=["async"]
36-
3733
[dev-dependencies]
3834
wat={workspace=true}
3935
eyre={workspace=true}

crates/tinywasm/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ logging=["log", "tinywasm-parser?/logging", "tinywasm-types/logging"]
3737
std=["tinywasm-parser?/std", "tinywasm-types/std"]
3838
parser=["dep:tinywasm-parser"]
3939
archive=["tinywasm-types/archive"]
40-
async=[]
41-
test_async=["async"] #feels weird putting it here
40+
test_async=[] #feels weird putting it here
4241

4342
[[test]]
4443
name="test-wasm-1"

crates/tinywasm/src/coro.rs

Lines changed: 200 additions & 220 deletions
Large diffs are not rendered by default.

crates/tinywasm/src/error.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use tinywasm_types::FuncType;
66
#[cfg(feature = "parser")]
77
pub use tinywasm_parser::ParseError;
88

9-
#[cfg(feature = "async")]
10-
use crate::coro::UnexpectedSuspendError;
11-
12-
use crate::interpreter;
9+
use crate::{coro::UnexpectedSuspendError, interpreter};
1310

1411
/// Errors that can occur for `TinyWasm` operations
1512
#[derive(Debug)]
@@ -48,7 +45,6 @@ pub enum Error {
4845

4946
/// Function unexpectedly yielded instead of returning
5047
/// (for backwards compatibility with old api)
51-
#[cfg(feature = "async")]
5248
UnexpectedSuspend(UnexpectedSuspendError),
5349

5450
#[cfg(feature = "std")]
@@ -200,9 +196,6 @@ impl Display for Error {
200196
#[cfg(feature = "std")]
201197
Self::Io(err) => write!(f, "I/O error: {err}"),
202198

203-
#[cfg(feature = "async")]
204-
Self::UnexpectedSuspend(_) => write!(f, "funtion yielded instead of returning"),
205-
206199
Self::Trap(trap) => write!(f, "trap: {trap}"),
207200
Self::Linker(err) => write!(f, "linking error: {err}"),
208201
Self::InvalidLabelType => write!(f, "invalid label type"),
@@ -212,6 +205,8 @@ impl Display for Error {
212205
write!(f, "invalid host function return: expected={expected:?}, actual={actual:?}")
213206
}
214207
Self::InvalidStore => write!(f, "invalid store"),
208+
209+
Self::UnexpectedSuspend(_) => write!(f, "funtion yielded instead of returning"),
215210
Self::InvalidResumeArgument => write!(f, "invalid resume argument supplied to suspended function"),
216211
Self::InvalidResume => write!(f, "attempt to resume coroutine that has already finished"),
217212
}

crates/tinywasm/src/func.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
#[cfg(feature = "async")]
2-
use {crate::coro::CoroState, tinywasm_types::ResumeArgument};
3-
1+
use crate::coro::CoroState;
42
use crate::interpreter;
53
use crate::interpreter::executor::SuspendedHostCoroState;
64
use crate::interpreter::stack::{CallFrame, Stack};
75
use crate::{log, unlikely, Function};
86
use crate::{Error, FuncContext, Result, Store};
97
use alloc::{boxed::Box, format, string::String, string::ToString, vec, vec::Vec};
10-
use tinywasm_types::{ExternRef, FuncRef, FuncType, ModuleInstanceAddr, ValType, WasmValue};
8+
use tinywasm_types::{ExternRef, FuncRef, FuncType, ModuleInstanceAddr, ResumeArgument, ValType, WasmValue};
119

1210
#[derive(Debug)]
1311
/// A function handle
@@ -154,7 +152,7 @@ impl<P: IntoWasmValueTuple, R: FromWasmValueTuple> FuncHandleTyped<P, R> {
154152
// Convert the Vec<WasmValue> back to R
155153
result
156154
.map_result(|vals| R::from_wasm_value_tuple(&vals))
157-
.map_state(|state| SuspendedFuncTyped::<R> { func: state, _marker: Default::default() })
155+
.map_state(|state| SuspendedFuncTyped::<R> { func: state, _marker: core::marker::PhantomData {} })
158156
.propagate_err_result()
159157
}
160158
}
@@ -163,13 +161,11 @@ pub(crate) type FuncHandleCallOutcome = crate::coro::PotentialCoroCallResult<Vec
163161
pub(crate) type TypedFuncHandleCallOutcome<R> = crate::coro::PotentialCoroCallResult<R, SuspendedFuncTyped<R>>;
164162

165163
#[derive(Debug)]
166-
#[cfg_attr(not(feature = "async"), allow(unused))]
167164
struct SuspendedWasmFunc {
168165
runtime: interpreter::SuspendedRuntime,
169166
result_types: Box<[ValType]>,
170167
}
171168
impl SuspendedWasmFunc {
172-
#[cfg(feature = "async")]
173169
fn resume(
174170
&mut self,
175171
ctx: FuncContext<'_>,
@@ -180,7 +176,6 @@ impl SuspendedWasmFunc {
180176
}
181177

182178
#[derive(Debug)]
183-
#[cfg_attr(not(feature = "async"), allow(unused))]
184179
#[allow(clippy::large_enum_variant)] // Wasm is bigger, but also much more common variant
185180
enum SuspendedFuncInner {
186181
Wasm(SuspendedWasmFunc),
@@ -189,15 +184,13 @@ enum SuspendedFuncInner {
189184

190185
/// handle to function that was suspended and can be resumed
191186
#[derive(Debug)]
192-
#[cfg_attr(not(feature = "async"), allow(unused))]
193187
pub struct SuspendedFunc {
194188
func: SuspendedFuncInner,
195189
module_addr: ModuleInstanceAddr,
196190
store_id: usize,
197191
}
198192

199193
impl crate::coro::CoroState<Vec<WasmValue>, &mut Store> for SuspendedFunc {
200-
#[cfg(feature = "async")]
201194
fn resume(
202195
&mut self,
203196
store: &mut Store,
@@ -215,7 +208,6 @@ impl crate::coro::CoroState<Vec<WasmValue>, &mut Store> for SuspendedFunc {
215208
}
216209
}
217210

218-
#[cfg_attr(not(feature = "async"), allow(unused))]
219211
pub struct SuspendedFuncTyped<R> {
220212
pub func: SuspendedFunc,
221213
pub(crate) _marker: core::marker::PhantomData<R>,
@@ -231,7 +223,6 @@ impl<R> crate::coro::CoroState<R, &mut Store> for SuspendedFuncTyped<R>
231223
where
232224
R: FromWasmValueTuple,
233225
{
234-
#[cfg(feature = "async")]
235226
fn resume(&mut self, ctx: &mut Store, arg: ResumeArgument) -> Result<crate::CoroStateResumeResult<R>> {
236227
self.func.resume(ctx, arg)?.map_result(|vals| R::from_wasm_value_tuple(&vals)).propagate_err()
237228
}

crates/tinywasm/src/instance.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use alloc::{boxed::Box, format, rc::Rc, string::ToString};
22
use tinywasm_types::*;
33

44
use crate::func::{FromWasmValueTuple, IntoWasmValueTuple};
5-
use crate::{Error, FuncHandle, FuncHandleTyped, Imports, MemoryRef, MemoryRefMut, Module, Result, Store};
6-
#[cfg(feature = "async")]
7-
use crate::{PotentialCoroCallResult, SuspendedFunc};
5+
use crate::{
6+
Error, FuncHandle, FuncHandleTyped, Imports, MemoryRef, MemoryRefMut, Module, PotentialCoroCallResult, Result,
7+
Store, SuspendedFunc,
8+
};
89

910
/// An instanciated WebAssembly module
1011
///
@@ -269,9 +270,8 @@ impl ModuleInstance {
269270
/// Invoke the start function of the module
270271
///
271272
/// Returns None if the module has no start function
272-
/// If start function suspends, returns SuspendedFunc.
273+
/// If start function suspends, returns SuspededFunc.
273274
/// Only when it finishes can this module instance be considered instantiated
274-
#[cfg(feature = "async")]
275275
pub fn start_coro(&self, store: &mut Store) -> Result<Option<PotentialCoroCallResult<(), SuspendedFunc>>> {
276276
let Some(func) = self.start_func(store)? else {
277277
return Ok(None);

crates/tinywasm/src/interpreter/executor.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,18 @@ use super::no_std_floats::NoStdFloatExt;
55
use alloc::boxed::Box;
66
use alloc::{format, rc::Rc, string::ToString};
77
use core::ops::ControlFlow;
8+
use coro::SuspendReason;
89
use interpreter::simd::exec_next_simd;
910
use interpreter::stack::CallFrame;
1011
use tinywasm_types::*;
1112

12-
#[cfg(feature = "async")]
13-
use coro::SuspendReason;
14-
1513
use super::num_helpers::*;
1614
use super::stack::{BlockFrame, BlockType, Stack};
1715
use super::values::*;
1816
use crate::*;
1917

2018
pub(crate) enum ReasonToBreak {
2119
Errored(Error),
22-
#[cfg_attr(not(feature = "async"), allow(unused))]
2320
Suspended(SuspendReason),
2421
Finished,
2522
}
@@ -31,20 +28,18 @@ impl From<ReasonToBreak> for ControlFlow<ReasonToBreak> {
3128
}
3229

3330
#[derive(Debug)]
34-
#[cfg_attr(not(feature = "async"), allow(unused))]
3531
pub(crate) struct SuspendedHostCoroState {
3632
pub(crate) coro_state: Box<dyn HostCoroState>,
3733
// plug into used in store.get_func to get original function
3834
// can be used for checking returned types
39-
#[allow(dead_code)] // knowing context is useful for debug and other possible future uses
35+
#[allow(dead_code)] // not implemented yet, but knowing context is useful
4036
pub(crate) coro_orig_function: u32,
4137
}
4238

4339
#[derive(Debug)]
4440
pub(crate) struct Executor<'store, 'stack> {
4541
pub(crate) cf: CallFrame,
4642
pub(crate) module: ModuleInstance,
47-
#[cfg(feature = "async")]
4843
pub(crate) suspended_host_coro: Option<SuspendedHostCoroState>,
4944
pub(crate) store: &'store mut Store,
5045
pub(crate) stack: &'stack mut Stack,
@@ -56,14 +51,7 @@ impl<'store, 'stack> Executor<'store, 'stack> {
5651
pub(crate) fn new(store: &'store mut Store, stack: &'stack mut Stack) -> Result<Self> {
5752
let current_frame = stack.call_stack.pop().expect("no call frame, this is a bug");
5853
let current_module = store.get_module_instance_raw(current_frame.module_addr());
59-
Ok(Self {
60-
cf: current_frame,
61-
module: current_module,
62-
#[cfg(feature = "async")]
63-
suspended_host_coro: None,
64-
stack,
65-
store,
66-
})
54+
Ok(Self { cf: current_frame, module: current_module, suspended_host_coro: None, stack, store })
6755
}
6856

6957
#[inline(always)]
@@ -79,7 +67,6 @@ impl<'store, 'stack> Executor<'store, 'stack> {
7967
}
8068
}
8169

82-
#[cfg(feature = "async")]
8370
#[inline(always)]
8471
pub(crate) fn resume(&mut self, res_arg: ResumeArgument) -> Result<ExecOutcome> {
8572
if let Some(coro_state) = self.suspended_host_coro.as_mut() {
@@ -118,7 +105,6 @@ impl<'store, 'stack> Executor<'store, 'stack> {
118105
/// execution may not be suspended in the middle of execution the funcion:
119106
/// so only do it as the last thing or first thing in the intsruction execution
120107
#[must_use = "If this returns ControlFlow::Break, the caller should propagate it"]
121-
#[cfg(feature = "async")]
122108
fn check_should_suspend(&mut self) -> ControlFlow<ReasonToBreak> {
123109
if let Some(flag) = &self.store.suspend_cond.suspend_flag {
124110
if flag.load(core::sync::atomic::Ordering::Acquire) {
@@ -144,11 +130,6 @@ impl<'store, 'stack> Executor<'store, 'stack> {
144130
ControlFlow::Continue(())
145131
}
146132

147-
#[cfg(not(feature = "async"))]
148-
fn check_should_suspend(&mut self) -> ControlFlow<ReasonToBreak> {
149-
ControlFlow::Continue(())
150-
}
151-
152133
#[inline(always)]
153134
fn exec_next(&mut self) -> ControlFlow<ReasonToBreak> {
154135
use tinywasm_types::Instruction::*;
@@ -433,7 +414,7 @@ impl<'store, 'stack> Executor<'store, 'stack> {
433414
self.module.swap_with(self.cf.module_addr(), self.store);
434415
ControlFlow::Continue(())
435416
}
436-
fn exec_call_host(&mut self, host_func: Rc<HostFunction>, _func_ref: u32) -> ControlFlow<ReasonToBreak> {
417+
fn exec_call_host(&mut self, host_func: Rc<HostFunction>, func_ref: u32) -> ControlFlow<ReasonToBreak> {
437418
let params = self.stack.values.pop_params(&host_func.ty.params);
438419
let res = host_func.call(FuncContext { store: self.store, module_addr: self.module.id() }, &params).to_cf()?;
439420
match res {
@@ -443,10 +424,9 @@ impl<'store, 'stack> Executor<'store, 'stack> {
443424
self.check_should_suspend()?; // who knows how long we've spent in host function
444425
ControlFlow::Continue(())
445426
}
446-
#[cfg(feature = "async")]
447427
PotentialCoroCallResult::Suspended(suspend_reason, state) => {
448428
self.suspended_host_coro =
449-
Some(SuspendedHostCoroState { coro_state: state, coro_orig_function: _func_ref });
429+
Some(SuspendedHostCoroState { coro_state: state, coro_orig_function: func_ref });
450430
self.cf.incr_instr_ptr();
451431
ReasonToBreak::Suspended(suspend_reason).into()
452432
}

crates/tinywasm/src/interpreter/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ mod values;
66

77
#[cfg(not(feature = "std"))]
88
mod no_std_floats;
9-
#[cfg(feature = "async")]
10-
use {executor::Executor, tinywasm_types::ResumeArgument};
119

1210
use crate::coro;
1311
use crate::{FuncContext, ModuleInstance, Result, Store};
14-
use executor::SuspendedHostCoroState;
12+
use executor::{Executor, SuspendedHostCoroState};
1513
use stack::{CallFrame, Stack};
14+
use tinywasm_types::ResumeArgument;
1615
pub use values::*;
1716

1817
/// The main `TinyWasm` runtime.
@@ -22,7 +21,6 @@ pub use values::*;
2221
pub struct InterpreterRuntime {}
2322

2423
#[derive(Debug)]
25-
#[cfg_attr(not(feature = "async"), allow(unused))]
2624
pub(crate) struct SuspendedRuntimeBody {
2725
pub(crate) suspended_host_coro: Option<SuspendedHostCoroState>,
2826
pub(crate) module: ModuleInstance,
@@ -31,10 +29,8 @@ pub(crate) struct SuspendedRuntimeBody {
3129

3230
#[derive(Debug)]
3331
pub(crate) struct SuspendedRuntime {
34-
#[cfg_attr(not(feature = "async"), allow(unused))]
3532
pub(crate) body: Option<(SuspendedRuntimeBody, Stack)>,
3633
}
37-
#[cfg(feature = "async")]
3834
impl SuspendedRuntime {
3935
fn make_exec<'store, 'stack>(
4036
body: SuspendedRuntimeBody,
@@ -48,11 +44,10 @@ impl SuspendedRuntime {
4844
}
4945
}
5046

51-
impl coro::CoroState<stack::Stack, FuncContext<'_>> for SuspendedRuntime {
52-
#[cfg(feature = "async")]
47+
impl<'a> coro::CoroState<stack::Stack, FuncContext<'a>> for SuspendedRuntime {
5348
fn resume(
5449
&mut self,
55-
ctx: FuncContext<'_>,
50+
ctx: FuncContext<'a>,
5651
arg: ResumeArgument,
5752
) -> Result<coro::CoroStateResumeResult<stack::Stack>> {
5853
// should be put back into self.body unless we're finished
@@ -88,7 +83,6 @@ impl InterpreterRuntime {
8883
let mut executor = executor::Executor::new(store, &mut stack)?;
8984
match executor.run_to_suspension()? {
9085
coro::CoroStateResumeResult::Return(()) => Ok(RuntimeExecOutcome::Return(stack)),
91-
#[cfg(feature = "async")]
9286
coro::CoroStateResumeResult::Suspended(suspend) => Ok(RuntimeExecOutcome::Suspended(
9387
suspend,
9488
SuspendedRuntime { body: Some((SuspendedRuntime::unmake_exec(executor), stack)) },

crates/tinywasm/src/lib.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,12 @@ pub(crate) mod log {
9191
}
9292

9393
mod error;
94-
#[cfg(not(feature = "async"))]
95-
#[allow(unused)]
96-
use coro::{CoroState, CoroStateResumeResult, PotentialCoroCallResult, SuspendReason};
97-
#[cfg(feature = "async")]
98-
pub use {
99-
coro::{CoroState, CoroStateResumeResult, PotentialCoroCallResult, SuspendReason},
100-
module::IncompleteModule,
101-
};
102-
94+
pub use coro::{CoroState, CoroStateResumeResult, PotentialCoroCallResult, SuspendReason};
10395
pub use error::*;
10496
pub use func::{FuncHandle, FuncHandleTyped, SuspendedFunc};
10597
pub use imports::*;
10698
pub use instance::ModuleInstance;
107-
pub use module::Module;
99+
pub use module::{IncompleteModule, Module};
108100
pub use reference::*;
109101
pub use store::*;
110102

crates/tinywasm/src/module.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
#[cfg(feature = "async")]
2-
use crate::{CoroState, PotentialCoroCallResult, SuspendedFunc};
3-
#[cfg(feature = "async")]
4-
use tinywasm_types::ResumeArgument;
5-
6-
use crate::{Imports, ModuleInstance, Result, Store};
7-
use tinywasm_types::TinyWasmModule;
1+
use crate::{CoroState, Imports, ModuleInstance, PotentialCoroCallResult, Result, Store, SuspendedFunc};
2+
use tinywasm_types::{ResumeArgument, TinyWasmModule};
83

94
/// A WebAssembly Module
105
///
@@ -64,7 +59,6 @@ impl Module {
6459

6560
/// same as [Self::instantiate] but accounts for possibility of start function suspending, in which case it returns
6661
/// [PotentialCoroCallResult::Suspended]. You can call [CoroState::resume] on it at any time to resume instantiation
67-
#[cfg(feature = "async")]
6862
pub fn instantiate_coro(
6963
self,
7064
store: &mut Store,
@@ -86,14 +80,11 @@ impl Module {
8680

8781
/// a corostate that results in [ModuleInstance] when finished
8882
#[derive(Debug)]
89-
#[cfg(feature = "async")]
9083
pub struct IncompleteModule(Option<HitTheFloor>);
9184

9285
#[derive(Debug)]
93-
#[cfg(feature = "async")]
9486
struct HitTheFloor(ModuleInstance, SuspendedFunc);
9587

96-
#[cfg(feature = "async")]
9788
impl CoroState<ModuleInstance, &mut Store> for IncompleteModule {
9889
fn resume(&mut self, ctx: &mut Store, arg: ResumeArgument) -> Result<crate::CoroStateResumeResult<ModuleInstance>> {
9990
let mut body: HitTheFloor = match self.0.take() {

0 commit comments

Comments
 (0)