0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-25 15:29:43 -05:00

Add Module::source_offset_to_location, deprecate old APIs (#624)

This commit is contained in:
Bartek Iwańczuk 2021-02-15 12:08:39 +01:00 committed by GitHub
parent ec54f28bfd
commit 8551c62511
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 18 deletions

View file

@ -2057,6 +2057,11 @@ void v8__Module__GetModuleRequestLocation(const v8::Module& self, int i,
*out = self.GetModuleRequestLocation(i); *out = self.GetModuleRequestLocation(i);
} }
void v8__Module__SourceOffsetToLocation(const v8::Module& self, int offset,
v8::Location* out) {
*out = self.SourceOffsetToLocation(offset);
}
const v8::Value* v8__Module__GetModuleNamespace(const v8::Module& self) { const v8::Value* v8__Module__GetModuleNamespace(const v8::Module& self) {
return local_to_ptr(ptr_to_local(&self)->GetModuleNamespace()); return local_to_ptr(ptr_to_local(&self)->GetModuleNamespace());
} }

View file

@ -145,6 +145,11 @@ extern "C" {
i: int, i: int,
out: *mut MaybeUninit<Location>, out: *mut MaybeUninit<Location>,
) -> Location; ) -> Location;
fn v8__Module__SourceOffsetToLocation(
this: *const Module,
offset: int,
out: *mut MaybeUninit<Location>,
) -> Location;
fn v8__Module__GetModuleNamespace(this: *const Module) -> *const Value; fn v8__Module__GetModuleNamespace(this: *const Module) -> *const Value;
fn v8__Module__GetIdentityHash(this: *const Module) -> int; fn v8__Module__GetIdentityHash(this: *const Module) -> int;
fn v8__Module__ScriptId(this: *const Module) -> int; fn v8__Module__ScriptId(this: *const Module) -> int;
@ -228,6 +233,10 @@ impl Module {
} }
/// Returns the number of modules requested by this module. /// Returns the number of modules requested by this module.
#[deprecated(
since = "0.18.2",
note = "Use Module::get_module_requests() and FixedArray::length()."
)]
pub fn get_module_requests_length(&self) -> usize { pub fn get_module_requests_length(&self) -> usize {
unsafe { v8__Module__GetModuleRequestsLength(self) } unsafe { v8__Module__GetModuleRequestsLength(self) }
.try_into() .try_into()
@ -236,6 +245,10 @@ impl Module {
/// Returns the ith module specifier in this module. /// Returns the ith module specifier in this module.
/// i must be < self.get_module_requests_length() and >= 0. /// i must be < self.get_module_requests_length() and >= 0.
#[deprecated(
since = "0.18.2",
note = "Use Module::get_module_requests() and ModuleRequest::get_specifier()."
)]
pub fn get_module_request(&self, i: usize) -> Local<String> { pub fn get_module_request(&self, i: usize) -> Local<String> {
// Note: the returned value is not actually stored in a HandleScope, // Note: the returned value is not actually stored in a HandleScope,
// therefore we don't need a scope object here. // therefore we don't need a scope object here.
@ -252,6 +265,11 @@ impl Module {
/// Returns the source location (line number and column number) of the ith /// Returns the source location (line number and column number) of the ith
/// module specifier's first occurrence in this module. /// module specifier's first occurrence in this module.
#[deprecated(
since = "0.18.2",
note = "Use Module::get_module_requests(), ModuleRequest::get_source_offset() and
Module::source_offset_to_location()."
)]
pub fn get_module_request_location(&self, i: usize) -> Location { pub fn get_module_request_location(&self, i: usize) -> Location {
let mut out = MaybeUninit::<Location>::uninit(); let mut out = MaybeUninit::<Location>::uninit();
unsafe { unsafe {
@ -264,6 +282,16 @@ impl Module {
} }
} }
/// For the given source text offset in this module, returns the corresponding
/// Location with line and column numbers.
pub fn source_offset_to_location(&self, offset: int) -> Location {
let mut out = MaybeUninit::<Location>::uninit();
unsafe {
v8__Module__SourceOffsetToLocation(self, offset, &mut out);
out.assume_init()
}
}
/// The `Module` specific equivalent of `Data::get_hash()`. /// The `Module` specific equivalent of `Data::get_hash()`.
/// This function is kept around for testing purposes only. /// This function is kept around for testing purposes only.
#[doc(hidden)] #[doc(hidden)]
@ -409,7 +437,7 @@ impl ModuleRequest {
} }
/// Returns the source code offset of this module request. /// Returns the source code offset of this module request.
/// Use Module::SourceOffsetToLocation to convert this to line/column numbers. /// Use Module::source_offset_to_location to convert this to line/column numbers.
pub fn get_source_offset(&self) -> int { pub fn get_source_offset(&self) -> int {
unsafe { v8__ModuleRequest__GetSourceOffset(self) } unsafe { v8__ModuleRequest__GetSourceOffset(self) }
} }
@ -417,7 +445,7 @@ impl ModuleRequest {
/// Contains the import assertions for this request in the form: /// Contains the import assertions for this request in the form:
/// [key1, value1, source_offset1, key2, value2, source_offset2, ...]. /// [key1, value1, source_offset1, key2, value2, source_offset2, ...].
/// The keys and values are of type v8::String, and the source offsets are of /// The keys and values are of type v8::String, and the source offsets are of
/// type Int32. Use Module::SourceOffsetToLocation to convert the source /// type Int32. Use Module::source_offset_to_location to convert the source
/// offsets to Locations with line/column numbers. /// offsets to Locations with line/column numbers.
/// ///
/// All assertions present in the module request will be supplied in this /// All assertions present in the module request will be supplied in this

View file

@ -1954,33 +1954,27 @@ fn module_instantiation_failures1() {
assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status()); assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status());
let module_requests = module.get_module_requests(); let module_requests = module.get_module_requests();
assert_eq!(2, module_requests.length()); assert_eq!(2, module_requests.length());
assert!(module.script_id().is_some());
let mr1 = v8::Local::<v8::ModuleRequest>::try_from( let mr1 = v8::Local::<v8::ModuleRequest>::try_from(
module_requests.get(scope, 0).unwrap(), module_requests.get(scope, 0).unwrap(),
) )
.unwrap(); .unwrap();
assert_eq!("./foo.js", mr1.get_specifier().to_rust_string_lossy(scope));
let loc = module.source_offset_to_location(mr1.get_source_offset());
assert_eq!(0, loc.get_line_number());
assert_eq!(7, loc.get_column_number());
assert_eq!(0, mr1.get_import_assertions().length()); assert_eq!(0, mr1.get_import_assertions().length());
let mr2 = v8::Local::<v8::ModuleRequest>::try_from( let mr2 = v8::Local::<v8::ModuleRequest>::try_from(
module_requests.get(scope, 1).unwrap(), module_requests.get(scope, 1).unwrap(),
) )
.unwrap(); .unwrap();
assert_eq!(0, mr2.get_import_assertions().length()); assert_eq!("./bar.js", mr2.get_specifier().to_rust_string_lossy(scope));
assert!(module.script_id().is_some()); let loc = module.source_offset_to_location(mr2.get_source_offset());
assert_eq!(
"./foo.js",
module.get_module_request(0).to_rust_string_lossy(scope)
);
let loc = module.get_module_request_location(0);
assert_eq!(0, loc.get_line_number());
assert_eq!(7, loc.get_column_number());
assert_eq!(
"./bar.js",
module.get_module_request(1).to_rust_string_lossy(scope)
);
let loc = module.get_module_request_location(1);
assert_eq!(1, loc.get_line_number()); assert_eq!(1, loc.get_line_number());
assert_eq!(15, loc.get_column_number()); assert_eq!(15, loc.get_column_number());
assert_eq!(0, mr2.get_import_assertions().length());
// Instantiation should fail. // Instantiation should fail.
{ {