Skip to content

Add ctx.poll #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions core/src/context/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::{
ptr::NonNull,
};

#[cfg(feature = "futures")]
use crate::runtime::schedular::SchedularPoll;
#[cfg(feature = "futures")]
use crate::AsyncContext;
use crate::{
Expand Down Expand Up @@ -386,6 +388,28 @@ impl<'js> Ctx<'js> {
res != 0
}

#[cfg(feature = "futures")]
pub fn poll(&self, cx: &mut std::task::Context) {
let spawner = unsafe { (*self.get_opaque()).spawner() };
spawner.listen(cx.waker().clone());

let mut ptr = MaybeUninit::<*mut qjs::JSContext>::uninit();
let rt = unsafe { qjs::JS_GetRuntime(self.ctx.as_ptr()) };

loop {
// TODO: Handle error.
if unsafe { qjs::JS_ExecutePendingJob(rt, ptr.as_mut_ptr()) } != 0 {
continue;
}

// TODO: Handle error.
match { spawner.poll(cx) } {
SchedularPoll::ShouldYield | SchedularPoll::Empty | SchedularPoll::Pending => break,
SchedularPoll::PendingProgress => {}
}
}
}

pub(crate) unsafe fn get_opaque(&self) -> *mut Opaque<'js> {
let rt = qjs::JS_GetRuntime(self.ctx.as_ptr());
qjs::JS_GetRuntimeOpaque(rt).cast::<Opaque>()
Expand Down
120 changes: 120 additions & 0 deletions core/src/runtime/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,126 @@ mod test {
assert_eq!(COUNT.load(Ordering::Relaxed),2);
});

async_test_case!(ctx_poll => (rt,ctx){

use std::{

future::{poll_fn, Future},
pin::pin,
sync::Arc,
task::Poll,
};

use tokio::{runtime::Handle, task};

use crate::{function::Func, *};

fn spawn_timeout<'js>(
ctx: Ctx<'js>,
callback: Function<'js>,
timeout: usize,
) -> Result<()> {
ctx.spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(timeout as u64)).await;
callback.call::<_, ()>(()).unwrap();
});

Ok(())
}

fn blocking_async<'js>(ctx:Ctx<'js>, promise: Promise<'js>) -> Result<Value<'js>>{
let mut fut = pin!(promise.into_future::<Value>());
task::block_in_place(move || {
Handle::current().block_on(async move {
poll_fn(move |cx| {
if let Poll::Ready(x) = fut.as_mut().poll(cx) {
return Poll::Ready(x);
}
ctx.poll(cx);
cx.waker().wake_by_ref();
Poll::Pending
})
.await
})
})
}


let order_vec = Arc::new(std::sync::Mutex::new(Vec::<u8>::new()));
let order_vec2 = order_vec.clone();

ctx.with(|ctx|{

let res = || {
let globals = ctx.globals();
globals.set("setTimeout", Func::from(spawn_timeout))?;
globals.set("blockingAsync", Func::from(blocking_async))?;
globals.set("done", Func::from(|| {

}))?;
globals.set("storeOrder", Func::from(move |value:u8| {
order_vec2.clone().lock().unwrap().push(value);
}))?;


let mut options = EvalOptions::default();
options.global = false;

ctx.eval_with_options(r#"

await new Promise((res) => setTimeout(res, 1));

storeOrder(1);

blockingAsync(
new Promise((res) =>
setTimeout(() => {
storeOrder(2);
res();
}, 1),
),
);

storeOrder(3);

setTimeout(() => {
setTimeout(async () => {
await new Promise((res) => setTimeout(res, 1));
storeOrder(4);
blockingAsync(
new Promise((res) =>
setTimeout(() => {
storeOrder(5);
res();
}, 1),
),
);
storeOrder(6);
}, 1);
}, 1);


"#,options)?;

Ok::<_,Error>(())
};
res().catch(&ctx).unwrap();
}).await;


rt.idle().await;

//assert that order is correct using a loop
let mut order = order_vec.lock().unwrap();
let mut i = order.len();
while let Some(value) = order.pop(){
assert_eq!(value as usize, i);
i -= 1;
}


});

#[cfg(feature = "parallel")]
fn assert_is_send<T: Send>(t: T) -> T {
t
Expand Down