feat: add experimental Env::get_module_file_name method (#1783)

This commit is contained in:
LongYinan 2023-11-06 11:01:27 +08:00 committed by GitHub
parent 2b0ddf4a21
commit 4e4d891ad8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 9 deletions

View file

@ -20,15 +20,15 @@ all-features = true
independent = true independent = true
[features] [features]
noop = []
async = ["tokio_rt"] async = ["tokio_rt"]
chrono_date = ["chrono", "napi5"]
compat-mode = [] compat-mode = []
default = ["napi3", "compat-mode"] # for most Node.js users default = ["napi3", "compat-mode"] # for most Node.js users
deferred_trace = ["napi4"]
error_anyhow = ["anyhow"]
experimental = ["napi-sys/experimental"] experimental = ["napi-sys/experimental"]
chrono_date = ["chrono", "napi5"]
full = ["latin1", "napi9", "async", "serde-json", "experimental", "chrono_date"] full = ["latin1", "napi9", "async", "serde-json", "experimental", "chrono_date"]
latin1 = ["encoding_rs"] latin1 = ["encoding_rs"]
error_anyhow = ["anyhow"]
napi1 = [] napi1 = []
napi2 = ["napi1", "napi-sys/napi2"] napi2 = ["napi1", "napi-sys/napi2"]
napi3 = ["napi2", "napi-sys/napi3"] napi3 = ["napi2", "napi-sys/napi3"]
@ -37,7 +37,8 @@ napi5 = ["napi4", "napi-sys/napi5"]
napi6 = ["napi5", "napi-sys/napi6"] napi6 = ["napi5", "napi-sys/napi6"]
napi7 = ["napi6", "napi-sys/napi7"] napi7 = ["napi6", "napi-sys/napi7"]
napi8 = ["napi7", "napi-sys/napi8"] napi8 = ["napi7", "napi-sys/napi8"]
napi9 = ["napi8", "napi-sys/napi9"] napi9 = ["napi8", "napi-sys/napi9", "byteorder"]
noop = []
serde-json = ["serde", "serde_json"] serde-json = ["serde", "serde_json"]
tokio_fs = ["tokio/fs"] tokio_fs = ["tokio/fs"]
tokio_full = ["tokio/full"] tokio_full = ["tokio/full"]
@ -52,20 +53,19 @@ tokio_stats = ["tokio/stats"]
tokio_sync = ["tokio/sync"] tokio_sync = ["tokio/sync"]
tokio_test_util = ["tokio/test-util"] tokio_test_util = ["tokio/test-util"]
tokio_time = ["tokio/time"] tokio_time = ["tokio/time"]
deferred_trace = ["napi4"]
[dependencies] [dependencies]
bitflags = "2"
ctor = "0.2" ctor = "0.2"
once_cell = "1.16" once_cell = "1.16"
bitflags = "2"
[dependencies.anyhow] [dependencies.anyhow]
version = "1"
optional = true optional = true
version = "1"
[dependencies.napi-sys] [dependencies.napi-sys]
version = "2.2.3"
path = "../sys" path = "../sys"
version = "2.2.3"
[dependencies.encoding_rs] [dependencies.encoding_rs]
optional = true optional = true
@ -75,9 +75,13 @@ version = "0.8"
optional = true optional = true
version = "0.4" version = "0.4"
[dependencies.byteorder]
optional = true
version = "1.5"
[target.'cfg(target_os = "wasi")'.dependencies] [target.'cfg(target_os = "wasi")'.dependencies]
tokio = { version = "1", optional = true, features = ["rt", "sync"] }
napi-derive = { path = "../macro", version = "2.10.1", default-features = false } napi-derive = { path = "../macro", version = "2.10.1", default-features = false }
tokio = { version = "1", optional = true, features = ["rt", "sync"] }
[target.'cfg(not(target_os = "wasi"))'.dependencies] [target.'cfg(not(target_os = "wasi"))'.dependencies]
tokio = { version = "1", optional = true, features = [ tokio = { version = "1", optional = true, features = [

View file

@ -1269,6 +1269,36 @@ impl Env {
Ok(unsafe { JsSymbol::from_raw_unchecked(self.0, result) }) Ok(unsafe { JsSymbol::from_raw_unchecked(self.0, result) })
} }
#[cfg(feature = "napi9")]
/// This API retrieves the file path of the currently running JS module as a URL. For a file on
/// the local file system it will start with `file://`.
///
/// # Errors
///
/// The retrieved string may be empty if the add-on loading process fails to establish the
/// add-on's file name.
pub fn get_module_file_name(&self) -> Result<String> {
let mut char_ptr = ptr::null();
check_status!(
unsafe { sys::node_api_get_module_file_name(self.0, &mut char_ptr) },
"call node_api_get_module_file_name failed"
)?;
// SAFETY: This is safe because `char_ptr` is guaranteed to not be `null`, and point to
// null-terminated string data.
let module_filename = unsafe { std::ffi::CStr::from_ptr(char_ptr) };
#[cfg(windows)]
{
let byte_len = module_filename.to_bytes().len();
let mut utf16_buf = vec![0u16; byte_len / 2];
byteorder::LittleEndian::read_u16_into(module_filename.to_bytes(), &mut utf16_buf);
Ok(String::from_utf16_lossy(utf16_buf))
}
#[cfg(not(windows))]
{
Ok(module_filename.to_string_lossy().into_owned())
}
}
/// ### Serialize `Rust Struct` into `JavaScript Value` /// ### Serialize `Rust Struct` into `JavaScript Value`
/// ///
/// ``` /// ```