Skip to content

Commit

Permalink
Update engine.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
StaticallyTypedAnxiety committed Jun 16, 2024
1 parent a116527 commit 718773e
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@ use std::{

use crate::bindings::wasi::io::poll::{poll, Pollable};

pub struct WasmRuntimeEngine {
/// The async engine instance
pub struct WasmRuntimeAsyncEngine {
waker: Arc<FutureWaker>,
recv: Receiver<()>,
}

/// the reactor that processes poll submissions.
/// TODO: should be given future support
#[derive(Clone)]
pub struct Reactor;

impl Reactor {
///calls poll::poll in wasi:io. Useful for finidng out state of subscribed resources
pub fn poll(polls: &[&Pollable]) -> Vec<u32> {
poll(polls)
}
}

impl WasmRuntimeEngine {
impl WasmRuntimeAsyncEngine {
/// function to execute futures
pub fn block_on<K, F: Future<Output = K>, Fun: FnOnce(Reactor) -> F>(async_closure: Fun) -> K {
let reactor = Reactor;
let future = async_closure(reactor);
pin_mut!(future);
let (sender, recv) = crossbeam::channel::unbounded();
let runtime_engine = WasmRuntimeEngine {
let runtime_engine = WasmRuntimeAsyncEngine {
waker: Arc::new(FutureWaker(sender.clone())),
recv,
};
Expand Down Expand Up @@ -89,7 +95,7 @@ mod test {
fn test_enqueue() {
let (sender, recv) = crossbeam::channel::unbounded();
let count_future = CountFuture { max: 3, min: 0 };
let runtime_engine = WasmRuntimeEngine {
let runtime_engine = WasmRuntimeAsyncEngine {
waker: FutureWaker(sender).into(),
recv,
};
Expand All @@ -106,7 +112,7 @@ mod test {
let count_future = CountFuture { max: 3, min: 0 };

assert_eq!(
WasmRuntimeEngine::block_on(|_reactor| async move { count_future.await }),
WasmRuntimeAsyncEngine::block_on(|_reactor| async move { count_future.await }),
3
);
}
Expand Down

0 comments on commit 718773e

Please sign in to comment.