| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 42cc33c commit 061939d
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5954,6 +5954,31 @@ idempotent. | |||
| 5954 | 5954 | ||
| 5955 | 5955 | This API may only be called from the main thread. | |
| 5956 | 5956 | ||
| 5957 | + ## Miscellaneous utilities | ||
| 5958 | + | ||
| 5959 | + ## node_api_get_module_file_name | ||
| 5960 | + | ||
| 5961 | + <!-- YAML | ||
| 5962 | + added: REPLACEME | ||
| 5963 | + --> | ||
| 5964 | + | ||
| 5965 | + > Stability: 1 - Experimental | ||
| 5966 | + | ||
| 5967 | + ```c | ||
| 5968 | + NAPI_EXTERN napi_status | ||
| 5969 | + node_api_get_module_file_name(napi_env env, const char** result); | ||
| 5970 | + | ||
| 5971 | + ``` | ||
| 5972 | + | ||
| 5973 | + * `[in] env`: The environment that the API is invoked under. | ||
| 5974 | + * `[out] result`: A URL containing the absolute path of the | ||
| 5975 | + location from which the add-on was loaded. For a file on the local | ||
| 5976 | + file system it will start with `file://`. The string is null-terminated and | ||
| 5977 | + owned by `env` and must thus not be modified or freed. | ||
| 5978 | + | ||
| 5979 | + `result` may be an empty string if the add-on loading process fails to establish | ||
| 5980 | + the add-on's file name during loading. | ||
| 5981 | + | ||
| 5957 | 5982 | [ABI Stability]: https://nodejs.org/en/docs/guides/abi-stability/ | |
| 5958 | 5983 | [AppVeyor]: https://www.appveyor.com | |
| 5959 | 5984 | [C++ Addons]: addons.md | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -253,6 +253,7 @@ constexpr size_t kFsStatsBufferLength = | |||
| 253 | 253 | V(fd_string, "fd") \ | |
| 254 | 254 | V(fields_string, "fields") \ | |
| 255 | 255 | V(file_string, "file") \ | |
| 256 | + V(filename_string, "filename") \ | ||
| 256 | 257 | V(fingerprint256_string, "fingerprint256") \ | |
| 257 | 258 | V(fingerprint_string, "fingerprint") \ | |
| 258 | 259 | V(flags_string, "flags") \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,8 +15,9 @@ | |||
| 15 | 15 | #include <memory> | |
| 16 | 16 | ||
| 17 | 17 | struct node_napi_env__ : public napi_env__ { | |
| 18 | - explicit node_napi_env__(v8::Local<v8::Context> context): | ||
| 19 | - napi_env__(context) { | ||
| 18 | + explicit node_napi_env__(v8::Local<v8::Context> context, | ||
| 19 | + const std::string& module_filename): | ||
| 20 | + napi_env__(context), filename(module_filename) { | ||
| 20 | 21 | CHECK_NOT_NULL(node_env()); | |
| 21 | 22 | } | |
| 22 | 23 | ||
@@ -46,6 +47,10 @@ struct node_napi_env__ : public napi_env__ { | |||
| 46 | 47 | }); | |
| 47 | 48 | }); | |
| 48 | 49 | } | |
| 50 | + | ||
| 51 | + const char* GetFilename() const { return filename.c_str(); } | ||
| 52 | + | ||
| 53 | + std::string filename; | ||
| 49 | 54 | }; | |
| 50 | 55 | ||
| 51 | 56 | typedef node_napi_env__* node_napi_env; | |
@@ -87,10 +92,11 @@ class BufferFinalizer : private Finalizer { | |||
| 87 | 92 | }; | |
| 88 | 93 | }; | |
| 89 | 94 | ||
| 90 | - static inline napi_env NewEnv(v8::Local<v8::Context> context) { | ||
| 95 | + static inline napi_env | ||
| 96 | + NewEnv(v8::Local<v8::Context> context, const std::string& module_filename) { | ||
| 91 | 97 | node_napi_env result; | |
| 92 | 98 | ||
| 93 | - result = new node_napi_env__(context); | ||
| 99 | + result = new node_napi_env__(context, module_filename); | ||
| 94 | 100 | // TODO(addaleax): There was previously code that tried to delete the | |
| 95 | 101 | // napi_env when its v8::Context was garbage collected; | |
| 96 | 102 | // However, as long as N-API addons using this napi_env are in place, | |
@@ -552,16 +558,35 @@ void napi_module_register_by_symbol(v8::Local<v8::Object> exports, | |||
| 552 | 558 | v8::Local<v8::Value> module, | |
| 553 | 559 | v8::Local<v8::Context> context, | |
| 554 | 560 | napi_addon_register_func init) { | |
| 561 | + node::Environment* node_env = node::Environment::GetCurrent(context); | ||
| 562 | + std::string module_filename = ""; | ||
| 555 | 563 | if (init == nullptr) { | |
| 556 | - node::Environment* node_env = node::Environment::GetCurrent(context); | ||
| 557 | 564 | CHECK_NOT_NULL(node_env); | |
| 558 | 565 | node_env->ThrowError( | |
| 559 | 566 | "Module has no declared entry point."); | |
| 560 | 567 | return; | |
| 561 | 568 | } | |
| 562 | 569 | ||
| 570 | + // We set `env->filename` from `module.filename` here, but we could just as | ||
| 571 | + // easily add a private property to `exports` in `process.dlopen`, which | ||
| 572 | + // receives the file name from JS, and retrieve *that* here. Thus, we are not | ||
| 573 | + // endorsing commonjs here by making use of `module.filename`. | ||
| 574 | + v8::Local<v8::Value> filename_js; | ||
| 575 | + v8::Local<v8::Object> modobj; | ||
| 576 | + if (module->ToObject(context).ToLocal(&modobj) && | ||
| 577 | + modobj->Get(context, node_env->filename_string()).ToLocal(&filename_js) && | ||
| 578 | + filename_js->IsString()) { | ||
| 579 | + node::Utf8Value filename(node_env->isolate(), filename_js); // Cast | ||
| 580 | + | ||
| 581 | + // Turn the absolute path into a URL. Currently the absolute path is always | ||
| 582 | + // a file system path. | ||
| 583 | + // TODO(gabrielschulhof): Pass the `filename` through unchanged if/when we | ||
| 584 | + // receive it as a URL already. | ||
| 585 | + module_filename = std::string("file://") + (*filename); | ||
| 586 | + } | ||
| 587 | + | ||
| 563 | 588 | // Create a new napi_env for this specific module. | |
| 564 | - napi_env env = v8impl::NewEnv(context); | ||
| 589 | + napi_env env = v8impl::NewEnv(context, module_filename); | ||
| 565 | 590 | ||
| 566 | 591 | napi_value _exports; | |
| 567 | 592 | env->CallIntoModule([&](napi_env env) { | |
@@ -1257,3 +1282,11 @@ napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func) { | |||
| 1257 | 1282 | CHECK_NOT_NULL(func); | |
| 1258 | 1283 | return reinterpret_cast<v8impl::ThreadSafeFunction*>(func)->Ref(); | |
| 1259 | 1284 | } | |
| 1285 | + | ||
| 1286 | + napi_status node_api_get_module_file_name(napi_env env, const char** result) { | ||
| 1287 | + CHECK_ENV(env); | ||
| 1288 | + CHECK_ARG(env, result); | ||
| 1289 | + | ||
| 1290 | + *result = static_cast<node_napi_env>(env)->GetFilename(); | ||
| 1291 | + return napi_clear_last_error(env); | ||
| 1292 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -261,6 +261,9 @@ NAPI_EXTERN napi_status napi_add_async_cleanup_hook( | |||
| 261 | 261 | NAPI_EXTERN napi_status napi_remove_async_cleanup_hook( | |
| 262 | 262 | napi_async_cleanup_hook_handle remove_handle); | |
| 263 | 263 | ||
| 264 | + NAPI_EXTERN napi_status | ||
| 265 | + node_api_get_module_file_name(napi_env env, const char** result); | ||
| 266 | + | ||
| 264 | 267 | #endif // NAPI_EXPERIMENTAL | |
| 265 | 268 | ||
| 266 | 269 | EXTERN_C_END | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,14 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const common = require('../../common'); | |
| 4 | - const test_general = require(`./build/${common.buildType}/test_general`); | ||
| 4 | + const filename = require.resolve(`./build/${common.buildType}/test_general`); | ||
| 5 | + const test_general = require(filename); | ||
| 5 | 6 | const assert = require('assert'); | |
| 6 | 7 | ||
| 8 | + // TODO(gabrielschulhof): This test may need updating if/when the filename | ||
| 9 | + // becomes a full-fledged URL. | ||
| 10 | + assert.strictEqual(test_general.filename, `file://${filename}`); | ||
| 11 | + | ||
| 7 | 12 | const [ major, minor, patch, release ] = test_general.testGetNodeVersion(); | |
| 8 | 13 | assert.strictEqual(process.version.split('-')[0], | |
| 9 | 14 | `v${major}.${minor}.${patch}`); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | + #define NAPI_EXPERIMENTAL | ||
| 1 | 2 | #include <node_api.h> | |
| 2 | 3 | #include <stdlib.h> | |
| 3 | 4 | #include "../../js-native-api/common.h" | |
@@ -20,9 +21,21 @@ static napi_value testGetNodeVersion(napi_env env, napi_callback_info info) { | |||
| 20 | 21 | return result; | |
| 21 | 22 | } | |
| 22 | 23 | ||
| 24 | + static napi_value GetFilename(napi_env env, napi_callback_info info) { | ||
| 25 | + const char* filename; | ||
| 26 | + napi_value result; | ||
| 27 | + | ||
| 28 | + NODE_API_CALL(env, node_api_get_module_file_name(env, &filename)); | ||
| 29 | + NODE_API_CALL(env, | ||
| 30 | + napi_create_string_utf8(env, filename, NAPI_AUTO_LENGTH, &result)); | ||
| 31 | + | ||
| 32 | + return result; | ||
| 33 | + } | ||
| 34 | + | ||
| 23 | 35 | static napi_value Init(napi_env env, napi_value exports) { | |
| 24 | 36 | napi_property_descriptor descriptors[] = { | |
| 25 | 37 | DECLARE_NODE_API_PROPERTY("testGetNodeVersion", testGetNodeVersion), | |
| 38 | + DECLARE_NODE_API_GETTER("filename", GetFilename), | ||
| 26 | 39 | }; | |
| 27 | 40 | ||
| 28 | 41 | NODE_API_CALL(env, napi_define_properties( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments