From 836557e84feb257a11e91d783c602a8a62e6361a Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 15 Oct 2020 11:05:38 +0200 Subject: [PATCH] Add Module::script_id() (#497) --- src/binding.cc | 8 ++++++++ src/module.rs | 14 ++++++++++++++ tests/test_api.rs | 8 ++++++++ 3 files changed, 30 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 0755510e..d114cc63 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -1909,6 +1909,14 @@ int v8__Module__GetIdentityHash(const v8::Module& self) { return self.GetIdentityHash(); } +int v8__Module__ScriptId(const v8::Module& self) { + // Module::ScriptId() isn't marked const but its implementation is + // so this const_cast is sound. + // TODO(bnoordhuis) Open V8 CL to mark Module::ScriptId() and + // UnboundScript::GetId() const. + return const_cast(self).ScriptId(); +} + MaybeBool v8__Module__InstantiateModule(const v8::Module& self, const v8::Context& context, v8::Module::ResolveCallback cb) { diff --git a/src/module.rs b/src/module.rs index 11d7507d..1adf1e07 100644 --- a/src/module.rs +++ b/src/module.rs @@ -138,6 +138,7 @@ extern "C" { ) -> Location; fn v8__Module__GetModuleNamespace(this: *const Module) -> *const Value; fn v8__Module__GetIdentityHash(this: *const Module) -> int; + fn v8__Module__ScriptId(this: *const Module) -> int; fn v8__Module__InstantiateModule( this: *const Module, context: *const Context, @@ -248,6 +249,19 @@ impl Module { unsafe { v8__Module__GetIdentityHash(self) } } + /// Returns the underlying script's id. + /// + /// The module must be a SourceTextModule and must not have an Errored status. + pub fn script_id(&self) -> Option { + if !self.is_source_text_module() { + return None; + } + if self.get_status() == ModuleStatus::Errored { + return None; + } + Some(unsafe { v8__Module__ScriptId(self) }) + } + /// Returns the namespace object of this module. /// /// The module's status must be at least kInstantiated. diff --git a/tests/test_api.rs b/tests/test_api.rs index 2f895ad9..2211ac36 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1856,6 +1856,7 @@ fn module_instantiation_failures1() { let module = v8::script_compiler::compile_module(scope, source).unwrap(); assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status()); assert_eq!(2, module.get_module_requests_length()); + assert!(module.script_id().is_some()); assert_eq!( "./foo.js", @@ -1930,6 +1931,7 @@ fn module_evaluation() { let source = v8::script_compiler::Source::new(source_text, &origin); let module = v8::script_compiler::compile_module(scope, source).unwrap(); + assert!(module.script_id().is_some()); assert!(module.is_source_text_module()); assert!(!module.is_synthetic_module()); assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status()); @@ -3530,16 +3532,21 @@ fn module_snapshot() { let module = v8::script_compiler::compile_module(scope, source).unwrap(); assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status()); + let script_id = module.script_id(); + assert!(script_id.is_some()); + let result = module.instantiate_module( scope, compile_specifier_as_module_resolve_callback, ); assert!(result.unwrap()); assert_eq!(v8::ModuleStatus::Instantiated, module.get_status()); + assert_eq!(script_id, module.script_id()); let result = module.evaluate(scope); assert!(result.is_some()); assert_eq!(v8::ModuleStatus::Evaluated, module.get_status()); + assert_eq!(script_id, module.script_id()); snapshot_creator.set_default_context(context); } @@ -3722,6 +3729,7 @@ fn synthetic_module() { ); assert!(!module.is_source_text_module()); assert!(module.is_synthetic_module()); + assert!(module.script_id().is_none()); assert_eq!(module.get_status(), v8::ModuleStatus::Uninstantiated); module