Skip to content

Commit

Permalink
refactor(core): clean up timer and promise code (denoland#384)
Browse files Browse the repository at this point in the history
Deno PR: denoland#21519

Note: `PollEventLoopOptions` was changed so the defaults are now the
ones used in the majority of cases.

Started working on cleaning up the call_and_await and resolve_value code
in JsRuntime. I managed to turn these into pure futures that will
resolve independently of the event loop (though still requiring someone
to poll it), which means that you can now do something like this:

```
#[op2]
fn call(scope: &mut v8::HandleScope, #[global] f: v8::Global<v8::Function>) -> impl Future<...> {
  JsRuntime::scoped_call(f).await
}
```

This should allow for some simplification of any code that's currently
using `call_and_await`.

`with_event_loop` is split into a "promise" and "future" version. The
promise version will throw if the event loop terminates without the
promise being resolved. The future version will continue polling until
the future resolves (even if the event loop completes). This matches the
current behaviour that we are relying on in deno.

---------

Co-authored-by: Bartek Iwańczuk <[email protected]>
  • Loading branch information
mmastrac and bartlomieju authored Dec 12, 2023
1 parent d567c8b commit 61972e0
Show file tree
Hide file tree
Showing 17 changed files with 357 additions and 252 deletions.
7 changes: 6 additions & 1 deletion core/benches/ops/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ fn bench_op(
.unwrap();
let guard = tokio.enter();
let run = runtime.execute_script("", ascii_str!("run()")).unwrap();
#[allow(deprecated)]
let bench = tokio.block_on(runtime.resolve_value(run)).unwrap();
let mut scope = runtime.handle_scope();
let bench: v8::Local<v8::Function> =
Expand All @@ -152,7 +153,11 @@ fn bench_op(
b.iter(move || {
tokio.block_on(async {
let guard = tokio.enter();
runtime.call_and_await(&bench).await.unwrap();
let call = runtime.call(&bench);
runtime
.with_event_loop_promise(call, PollEventLoopOptions::default())
.await
.unwrap();
drop(guard);
});
});
Expand Down
10 changes: 5 additions & 5 deletions core/runtime/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,10 @@ pub(crate) fn watch_promise<'s, F>(
) -> Option<v8::Local<'s, v8::Promise>>
where
F: FnOnce(
&mut v8::HandleScope,
v8::ReturnValue,
Result<v8::Local<v8::Value>, v8::Local<v8::Value>>,
),
&mut v8::HandleScope,
v8::ReturnValue,
Result<v8::Local<v8::Value>, v8::Local<v8::Value>>,
) + 'static,
{
let external =
v8::External::new(scope, Box::into_raw(Box::new(Some(f))) as _);
Expand All @@ -564,7 +564,7 @@ where
.data(external.into())
.build(scope);

let on_rejected = v8::Function::builder(
let on_rejected: Option<v8::Local<'_, v8::Function>> = v8::Function::builder(
|scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
rv: v8::ReturnValue| {
Expand Down
Loading

0 comments on commit 61972e0

Please sign in to comment.