From 8551c62511678c973c2aea8258de3357522b752d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 15 Feb 2021 12:08:39 +0100 Subject: [PATCH] Add Module::source_offset_to_location, deprecate old APIs (#624) --- src/binding.cc | 5 +++++ src/module.rs | 32 ++++++++++++++++++++++++++++++-- tests/test_api.rs | 26 ++++++++++---------------- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 2c10b612..322b16ef 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -2057,6 +2057,11 @@ void v8__Module__GetModuleRequestLocation(const v8::Module& self, int 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) { return local_to_ptr(ptr_to_local(&self)->GetModuleNamespace()); } diff --git a/src/module.rs b/src/module.rs index d1430c91..cbe5fac2 100644 --- a/src/module.rs +++ b/src/module.rs @@ -145,6 +145,11 @@ extern "C" { i: int, out: *mut MaybeUninit, ) -> Location; + fn v8__Module__SourceOffsetToLocation( + this: *const Module, + offset: int, + out: *mut MaybeUninit, + ) -> 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; @@ -228,6 +233,10 @@ impl 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 { unsafe { v8__Module__GetModuleRequestsLength(self) } .try_into() @@ -236,6 +245,10 @@ impl Module { /// Returns the ith module specifier in this module. /// 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 { // Note: the returned value is not actually stored in a HandleScope, // 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 /// 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 { let mut out = MaybeUninit::::uninit(); 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::::uninit(); + unsafe { + v8__Module__SourceOffsetToLocation(self, offset, &mut out); + out.assume_init() + } + } + /// The `Module` specific equivalent of `Data::get_hash()`. /// This function is kept around for testing purposes only. #[doc(hidden)] @@ -409,7 +437,7 @@ impl ModuleRequest { } /// 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 { unsafe { v8__ModuleRequest__GetSourceOffset(self) } } @@ -417,7 +445,7 @@ impl ModuleRequest { /// Contains the import assertions for this request in the form: /// [key1, value1, source_offset1, key2, value2, source_offset2, ...]. /// 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. /// /// All assertions present in the module request will be supplied in this diff --git a/tests/test_api.rs b/tests/test_api.rs index 97b91e4c..2c2a3934 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -1954,33 +1954,27 @@ fn module_instantiation_failures1() { assert_eq!(v8::ModuleStatus::Uninstantiated, module.get_status()); let module_requests = module.get_module_requests(); assert_eq!(2, module_requests.length()); + assert!(module.script_id().is_some()); + let mr1 = v8::Local::::try_from( module_requests.get(scope, 0).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()); + let mr2 = v8::Local::::try_from( module_requests.get(scope, 1).unwrap(), ) .unwrap(); - assert_eq!(0, mr2.get_import_assertions().length()); - assert!(module.script_id().is_some()); - - 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!("./bar.js", mr2.get_specifier().to_rust_string_lossy(scope)); + let loc = module.source_offset_to_location(mr2.get_source_offset()); assert_eq!(1, loc.get_line_number()); assert_eq!(15, loc.get_column_number()); + assert_eq!(0, mr2.get_import_assertions().length()); // Instantiation should fail. {