Skip to content

Commit 50af4c7

Browse files
Add ctx.poll
1 parent 3482b36 commit 50af4c7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

core/src/context/ctx.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::{
88
ptr::NonNull,
99
};
1010

11+
#[cfg(feature = "futures")]
12+
use crate::runtime::schedular::SchedularPoll;
1113
#[cfg(feature = "futures")]
1214
use crate::AsyncContext;
1315
use crate::{
@@ -386,6 +388,28 @@ impl<'js> Ctx<'js> {
386388
res != 0
387389
}
388390

391+
#[cfg(feature = "futures")]
392+
pub fn poll(&self, cx: &mut std::task::Context) {
393+
let spawner = unsafe { (*self.get_opaque()).spawner() };
394+
spawner.listen(cx.waker().clone());
395+
396+
let mut ptr = MaybeUninit::<*mut qjs::JSContext>::uninit();
397+
let rt = unsafe { qjs::JS_GetRuntime(self.ctx.as_ptr()) };
398+
399+
loop {
400+
// TODO: Handle error.
401+
if unsafe { qjs::JS_ExecutePendingJob(rt, ptr.as_mut_ptr()) } != 0 {
402+
continue;
403+
}
404+
405+
// TODO: Handle error.
406+
match { spawner.poll(cx) } {
407+
SchedularPoll::ShouldYield | SchedularPoll::Empty | SchedularPoll::Pending => break,
408+
SchedularPoll::PendingProgress => {}
409+
}
410+
}
411+
}
412+
389413
pub(crate) unsafe fn get_opaque(&self) -> *mut Opaque<'js> {
390414
let rt = qjs::JS_GetRuntime(self.ctx.as_ptr());
391415
qjs::JS_GetRuntimeOpaque(rt).cast::<Opaque>()

0 commit comments

Comments
 (0)