0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-23 15:50:11 -05:00

Add Module::script_id() (#497)

This commit is contained in:
Ben Noordhuis 2020-10-15 11:05:38 +02:00 committed by GitHub
parent 57390ec4ee
commit 836557e84f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View file

@ -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<v8::Module&>(self).ScriptId();
}
MaybeBool v8__Module__InstantiateModule(const v8::Module& self,
const v8::Context& context,
v8::Module::ResolveCallback cb) {

View file

@ -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<int> {
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.

View file

@ -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