From a97d89614e8249a86c223a2bc049b1c574bb40f1 Mon Sep 17 00:00:00 2001 From: snek Date: Wed, 4 Sep 2024 18:00:29 -0700 Subject: [PATCH] feat: add Module::is_graph_async (#1607) --- src/binding.cc | 4 ++++ src/module.rs | 10 ++++++++++ tests/test_api.rs | 2 ++ 3 files changed, 16 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 4f198984ae..713724b311 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -3223,6 +3223,10 @@ const v8::Value* v8__Module__Evaluate(const v8::Module& self, ptr_to_local(&self)->Evaluate(ptr_to_local(&context))); } +bool v8__Module__IsGraphAsync(const v8::Module& self) { + return ptr_to_local(&self)->IsGraphAsync(); +} + bool v8__Module__IsSourceTextModule(const v8::Module& self) { return ptr_to_local(&self)->IsSourceTextModule(); } diff --git a/src/module.rs b/src/module.rs index ab2b6cf3dc..f9424d588a 100644 --- a/src/module.rs +++ b/src/module.rs @@ -169,6 +169,7 @@ extern "C" { this: *const Module, context: *const Context, ) -> *const Value; + fn v8__Module__IsGraphAsync(this: *const Module) -> bool; fn v8__Module__IsSourceTextModule(this: *const Module) -> bool; fn v8__Module__IsSyntheticModule(this: *const Module) -> bool; fn v8__Module__CreateSyntheticModule( @@ -349,6 +350,15 @@ impl Module { } } + /// Returns whether this module or any of its requested modules is async, + /// i.e. contains top-level await. + /// + /// The module's status must be at least kInstantiated. + #[inline(always)] + pub fn is_graph_async(&self) -> bool { + unsafe { v8__Module__IsGraphAsync(self) } + } + /// Returns whether the module is a SourceTextModule. #[inline(always)] pub fn is_source_text_module(&self) -> bool { diff --git a/tests/test_api.rs b/tests/test_api.rs index e7168fe0d9..e1256a0d5d 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -4989,6 +4989,7 @@ fn module_stalled_top_level_await() { .instantiate_module(scope, compile_specifier_as_module_resolve_callback); assert!(result.unwrap()); assert_eq!(v8::ModuleStatus::Instantiated, module.get_status()); + assert!(module.is_graph_async()); let result = module.evaluate(scope); assert!(result.is_some()); @@ -7756,6 +7757,7 @@ fn synthetic_module() { .instantiate_module(scope, unexpected_module_resolve_callback) .unwrap(); assert_eq!(module.get_status(), v8::ModuleStatus::Instantiated); + assert!(!module.is_graph_async()); module.evaluate(scope).unwrap(); assert_eq!(module.get_status(), v8::ModuleStatus::Evaluated);