From 76a6ea57753be420398d3eba8f313a6c98eab8c3 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Wed, 13 Dec 2023 08:07:26 -0700 Subject: [PATCH] refactor(cli): update to new deno_core promise/call methods (#21519) --- Cargo.lock | 19 +++++++++++----- Cargo.toml | 2 +- bench_util/js_runtime.rs | 6 ++++- cli/tests/integration/inspector_tests.rs | 6 ++++- cli/tools/bench/mod.rs | 7 +++++- cli/tools/repl/session.rs | 11 ++++------ cli/tools/test/mod.rs | 18 +++++++++------ cli/worker.rs | 28 +++++++----------------- runtime/web_worker.rs | 9 +++----- runtime/worker.rs | 25 ++++----------------- 10 files changed, 60 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e5c811f920..51f1abc389 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -681,6 +681,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" +[[package]] +name = "cooked-waker" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147be55d677052dabc6b22252d5dd0fd4c29c8c27aa4f2fbef0f94aa003b406f" + [[package]] name = "core-foundation" version = "0.9.4" @@ -1128,12 +1134,13 @@ dependencies = [ [[package]] name = "deno_core" -version = "0.237.0" +version = "0.238.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2ea708c221abdb5734e3c4b72075379c3046eb0ac54afa0ecb5e58509cce72c" +checksum = "0ded8b759e4435aa0929913909dd6c482ed6042dae19c53260e1caf9d55b37a9" dependencies = [ "anyhow", "bytes", + "cooked-waker", "deno_ops", "deno_unsync 0.3.0", "futures", @@ -1559,9 +1566,9 @@ dependencies = [ [[package]] name = "deno_ops" -version = "0.113.0" +version = "0.114.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5b9c0f6360795fb625774a8b5955c87c470c43159670cf5d2052df5ce9d84bc" +checksum = "168a929496191fdd8e91f898c8454429df4d5489597777d89f47897f6a37da6b" dependencies = [ "proc-macro-rules", "proc-macro2", @@ -5084,9 +5091,9 @@ dependencies = [ [[package]] name = "serde_v8" -version = "0.146.0" +version = "0.147.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78309bd1ec4d14d165f271e203bdc45ad5bf45525da57bb70901f57942f6c0f7" +checksum = "2af950d83e1c70b762d48fa7a869d6db9a4f191548dfd666fa4e62f2229e1dce" dependencies = [ "bytes", "derive_more", diff --git a/Cargo.toml b/Cargo.toml index bc0bcf5b64..ac8206061e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ repository = "https://github.com/denoland/deno" [workspace.dependencies] deno_ast = { version = "0.31.6", features = ["transpiling"] } -deno_core = { version = "0.237.0" } +deno_core = { version = "0.238.0" } deno_runtime = { version = "0.135.0", path = "./runtime" } napi_sym = { version = "0.57.0", path = "./cli/napi/sym" } diff --git a/bench_util/js_runtime.rs b/bench_util/js_runtime.rs index a12e6ca62a..3de4bf7872 100644 --- a/bench_util/js_runtime.rs +++ b/bench_util/js_runtime.rs @@ -3,6 +3,7 @@ use bencher::Bencher; use deno_core::v8; use deno_core::Extension; use deno_core::JsRuntime; +use deno_core::PollEventLoopOptions; use deno_core::RuntimeOptions; use crate::profiling::is_profiling; @@ -116,5 +117,8 @@ pub fn bench_js_async_with( async fn inner_async(src: &'static str, runtime: &mut JsRuntime) { runtime.execute_script_static("inner_loop", src).unwrap(); - runtime.run_event_loop(false).await.unwrap(); + runtime + .run_event_loop(PollEventLoopOptions::default()) + .await + .unwrap(); } diff --git a/cli/tests/integration/inspector_tests.rs b/cli/tests/integration/inspector_tests.rs index 36d469196a..41c126cdbc 100644 --- a/cli/tests/integration/inspector_tests.rs +++ b/cli/tests/integration/inspector_tests.rs @@ -869,7 +869,11 @@ async fn inspector_break_on_first_line_in_test() { .await; assert_starts_with!(&tester.stdout_line(), "running 1 test from"); - assert!(&tester.stdout_line().contains("basic test ... ok")); + let line = tester.stdout_line(); + assert!( + &line.contains("basic test ... ok"), + "Missing content: {line}" + ); tester.child.kill().unwrap(); tester.child.wait().unwrap(); diff --git a/cli/tools/bench/mod.rs b/cli/tools/bench/mod.rs index 57d1484639..ed6f106897 100644 --- a/cli/tools/bench/mod.rs +++ b/cli/tools/bench/mod.rs @@ -31,6 +31,7 @@ use deno_core::unsync::spawn; use deno_core::unsync::spawn_blocking; use deno_core::v8; use deno_core::ModuleSpecifier; +use deno_core::PollEventLoopOptions; use deno_runtime::permissions::Permissions; use deno_runtime::permissions::PermissionsContainer; use deno_runtime::tokio_util::create_and_run_current_thread; @@ -254,7 +255,11 @@ async fn bench_specifier_inner( }))?; for (desc, function) in benchmarks { sender.send(BenchEvent::Wait(desc.id))?; - let result = worker.js_runtime.call_and_await(&function).await?; + let call = worker.js_runtime.call(&function); + let result = worker + .js_runtime + .with_event_loop_promise(call, PollEventLoopOptions::default()) + .await?; let scope = &mut worker.js_runtime.handle_scope(); let result = v8::Local::new(scope, result); let result = serde_v8::from_v8::(scope, result)?; diff --git a/cli/tools/repl/session.rs b/cli/tools/repl/session.rs index 22dd30caf5..624d7dafe5 100644 --- a/cli/tools/repl/session.rs +++ b/cli/tools/repl/session.rs @@ -202,14 +202,11 @@ impl ReplSession { worker .js_runtime - .with_event_loop( + .with_event_loop_future( session .post_message::<()>("Runtime.enable", None) .boxed_local(), - PollEventLoopOptions { - wait_for_inspector: false, - ..Default::default() - }, + PollEventLoopOptions::default(), ) .await?; @@ -298,14 +295,14 @@ impl ReplSession { self .worker .js_runtime - .with_event_loop( + .with_event_loop_future( self.session.post_message(method, params).boxed_local(), PollEventLoopOptions { - wait_for_inspector: false, // NOTE(bartlomieju): this is an important bit; we don't want to pump V8 // message loop here, so that GC won't run. Otherwise, the resulting // object might be GC'ed before we have a chance to inspect it. pump_v8_message_loop: false, + ..Default::default() }, ) .await diff --git a/cli/tools/test/mod.rs b/cli/tools/test/mod.rs index 840c5ac875..fe1d6cc9c4 100644 --- a/cli/tools/test/mod.rs +++ b/cli/tools/test/mod.rs @@ -498,12 +498,9 @@ async fn test_specifier_inner( if let Some(coverage_collector) = coverage_collector.as_mut() { worker .js_runtime - .with_event_loop( + .with_event_loop_future( coverage_collector.stop_collecting().boxed_local(), - PollEventLoopOptions { - wait_for_inspector: false, - ..Default::default() - }, + PollEventLoopOptions::default(), ) .await?; } @@ -574,11 +571,18 @@ pub async fn run_tests_for_worker( // but haven't responded to settle. let waker = noop_waker(); let mut cx = Context::from_waker(&waker); - let _ = worker.js_runtime.poll_event_loop(&mut cx, false); + let _ = worker + .js_runtime + .poll_event_loop(&mut cx, PollEventLoopOptions::default()); } let earlier = SystemTime::now(); - let result = match worker.js_runtime.call_and_await(&function).await { + let call = worker.js_runtime.call(&function); + let result = match worker + .js_runtime + .with_event_loop_promise(call, PollEventLoopOptions::default()) + .await + { Ok(r) => r, Err(error) => { if error.is::() { diff --git a/cli/worker.rs b/cli/worker.rs index 22e534e1d9..2f00165812 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -211,12 +211,9 @@ impl CliMainWorker { self .worker .js_runtime - .with_event_loop( + .with_event_loop_future( coverage_collector.stop_collecting().boxed_local(), - PollEventLoopOptions { - wait_for_inspector: false, - ..Default::default() - }, + PollEventLoopOptions::default(), ) .await?; } @@ -224,12 +221,9 @@ impl CliMainWorker { self .worker .js_runtime - .with_event_loop( + .with_event_loop_future( hmr_runner.stop().boxed_local(), - PollEventLoopOptions { - wait_for_inspector: false, - ..Default::default() - }, + PollEventLoopOptions::default(), ) .await?; } @@ -340,12 +334,9 @@ impl CliMainWorker { self .worker .js_runtime - .with_event_loop( + .with_event_loop_future( coverage_collector.start_collecting().boxed_local(), - PollEventLoopOptions { - wait_for_inspector: false, - ..Default::default() - }, + PollEventLoopOptions::default(), ) .await?; Ok(Some(coverage_collector)) @@ -371,12 +362,9 @@ impl CliMainWorker { self .worker .js_runtime - .with_event_loop( + .with_event_loop_future( hmr_runner.start().boxed_local(), - PollEventLoopOptions { - wait_for_inspector: false, - ..Default::default() - }, + PollEventLoopOptions::default(), ) .await?; diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index b669b5c2a1..41e6ca4fe4 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -691,7 +691,7 @@ impl WebWorker { maybe_result } - event_loop_result = self.js_runtime.run_event_loop(false) => { + event_loop_result = self.js_runtime.run_event_loop(PollEventLoopOptions::default()) => { event_loop_result?; receiver.await } @@ -706,10 +706,7 @@ impl WebWorker { id: ModuleId, ) -> Result<(), AnyError> { let mut receiver = self.js_runtime.mod_evaluate(id); - let poll_options = PollEventLoopOptions { - wait_for_inspector: false, - ..Default::default() - }; + let poll_options = PollEventLoopOptions::default(); tokio::select! { biased; @@ -741,7 +738,7 @@ impl WebWorker { self.internal_handle.terminate_waker.register(cx.waker()); - match self.js_runtime.poll_event_loop2(cx, poll_options) { + match self.js_runtime.poll_event_loop(cx, poll_options) { Poll::Ready(r) => { // If js ended because we are terminating, just return Ok if self.internal_handle.terminate_if_needed() { diff --git a/runtime/worker.rs b/runtime/worker.rs index 0bfb9305ca..94b0d9606a 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -3,8 +3,6 @@ use std::rc::Rc; use std::sync::atomic::AtomicI32; use std::sync::atomic::Ordering::Relaxed; use std::sync::Arc; -use std::task::Context; -use std::task::Poll; use std::time::Duration; use std::time::Instant; @@ -561,10 +559,9 @@ impl MainWorker { ) -> Result<(), AnyError> { match tokio::time::timeout( duration, - self.js_runtime.run_event_loop2(PollEventLoopOptions { - wait_for_inspector: false, - pump_v8_message_loop: true, - }), + self + .js_runtime + .run_event_loop(PollEventLoopOptions::default()), ) .await { @@ -613,27 +610,13 @@ impl MainWorker { self.js_runtime.inspector().borrow().create_local_session() } - pub fn poll_event_loop( - &mut self, - cx: &mut Context, - wait_for_inspector: bool, - ) -> Poll> { - self.js_runtime.poll_event_loop2( - cx, - deno_core::PollEventLoopOptions { - wait_for_inspector, - ..Default::default() - }, - ) - } - pub async fn run_event_loop( &mut self, wait_for_inspector: bool, ) -> Result<(), AnyError> { self .js_runtime - .run_event_loop2(deno_core::PollEventLoopOptions { + .run_event_loop(deno_core::PollEventLoopOptions { wait_for_inspector, ..Default::default() })