diff --git a/src/binding.cc b/src/binding.cc index 14234023..1f643a69 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -312,6 +312,10 @@ void v8__Isolate__SetWasmStreamingCallback(v8::Isolate* isolate, isolate->SetWasmStreamingCallback(callback); } +bool v8__Isolate__HasPendingBackgroundTasks(v8::Isolate* isolate) { + return isolate->HasPendingBackgroundTasks(); +} + void v8__Isolate__CreateParams__CONSTRUCT( uninit_t* buf) { construct_in_place(buf); diff --git a/src/isolate.rs b/src/isolate.rs index 728d75dd..f15c1183 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -232,11 +232,11 @@ extern "C" { function: *const Function, ); fn v8__Isolate__SetAllowAtomicsWait(isolate: *mut Isolate, allow: bool); - fn v8__Isolate__SetWasmStreamingCallback( isolate: *mut Isolate, callback: extern "C" fn(*const FunctionCallbackInfo), ); + fn v8__Isolate__HasPendingBackgroundTasks(isolate: *const Isolate) -> bool; fn v8__HeapProfiler__TakeHeapSnapshot( isolate: *mut Isolate, @@ -655,6 +655,13 @@ impl Isolate { unsafe { v8__Isolate__SetWasmStreamingCallback(self, trampoline::()) } } + /// Returns true if there is ongoing background work within V8 that will + /// eventually post a foreground task, like asynchronous WebAssembly + /// compilation. + pub fn has_pending_background_tasks(&self) -> bool { + unsafe { v8__Isolate__HasPendingBackgroundTasks(self) } + } + /// Disposes the isolate. The isolate must not be entered by any /// thread to be disposable. unsafe fn dispose(&mut self) { diff --git a/tests/test_api.rs b/tests/test_api.rs index 5a16dbbc..4e5a4211 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -4726,6 +4726,7 @@ fn wasm_streaming_callback() { .then(result => globalThis.result = result); "#; eval(scope, script).unwrap(); + assert!(scope.has_pending_background_tasks()); let global = context.global(scope); let name = v8::String::new(scope, "result").unwrap().into(); @@ -4739,6 +4740,7 @@ fn wasm_streaming_callback() { assert!(global.get(scope, name).unwrap().is_null()); ws.finish(); + assert!(!scope.has_pending_background_tasks()); assert!(global.get(scope, name).unwrap().is_null()); scope.perform_microtask_checkpoint();