Skip to content

Commit

Permalink
src: add Env::GetModuleFileName
Browse files Browse the repository at this point in the history
PR-URL: #1327
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]
  • Loading branch information
KevinEady authored and mhdawson committed Jun 28, 2023
1 parent ac4c87f commit b83e453
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 2 deletions.
11 changes: 11 additions & 0 deletions doc/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ addon. The item will be passed to the function `fini` which gets called when an
instance of the addon is unloaded. This overload accepts an additional hint to
be passed to `fini`.

### GetModuleFileName

```cpp
const char* Napi::Env::GetModuleFileName() const;
```

Returns a A URL containing the absolute path of the location from which the
add-on was loaded. For a file on the local file system it will start with
`file://`. The string is null-terminated and owned by env and must thus not be
modified or freed. It is only valid while the add-on is loaded.

### AddCleanupHook

```cpp
Expand Down
8 changes: 8 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,14 @@ void Env::DefaultFiniWithHint(Env, DataType* data, HintType*) {
}
#endif // NAPI_VERSION > 5

#if NAPI_VERSION > 8
inline const char* Env::GetModuleFileName() const {
const char* result;
napi_status status = node_api_get_module_file_name(_env, &result);
NAPI_THROW_IF_FAILED(*this, status, nullptr);
return result;
}
#endif // NAPI_VERSION > 8
////////////////////////////////////////////////////////////////////////////////
// Value class
////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ class Env {
} * data;
};
#endif // NAPI_VERSION > 2

#if NAPI_VERSION > 8
const char* GetModuleFileName() const;
#endif // NAPI_VERSION > 8
};

/// A JavaScript value of unknown type.
Expand Down
7 changes: 6 additions & 1 deletion test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ Object InitThunkingManual(Env env);
Object InitObjectFreezeSeal(Env env);
Object InitTypeTaggable(Env env);
#endif

#if (NAPI_VERSION > 8)
Object InitEnvMiscellaneous(Env env);
#endif
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
Object InitMaybeCheck(Env env);
#endif
Expand Down Expand Up @@ -171,6 +173,9 @@ Object Init(Env env, Object exports) {
exports.Set("object_freeze_seal", InitObjectFreezeSeal(env));
exports.Set("type_taggable", InitTypeTaggable(env));
#endif
#if (NAPI_VERSION > 8)
exports.Set("env_misc", InitEnvMiscellaneous(env));
#endif

#if defined(NODE_ADDON_API_ENABLE_MAYBE)
exports.Set("maybe_check", InitMaybeCheck(env));
Expand Down
1 change: 1 addition & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'dataview/dataview.cc',
'dataview/dataview_read_write.cc',
'env_cleanup.cc',
'env_misc.cc',
'error.cc',
'error_handling_for_primitives.cc',
'external.cc',
Expand Down
2 changes: 1 addition & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ exports.runTest = async function (test, buildType, buildPathRoot = process.env.B
].map(it => require.resolve(it));

for (const item of bindings) {
await Promise.resolve(test(require(item)))
await Promise.resolve(test(require(item), { bindingPath: item }))
.finally(exports.mustCall());
}
};
Expand Down
25 changes: 25 additions & 0 deletions test/env_misc.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "napi.h"
#include "test_helper.h"

#if (NAPI_VERSION > 8)

using namespace Napi;

namespace {

Value GetModuleFileName(const CallbackInfo& info) {
Env env = info.Env();
return String::New(env, env.GetModuleFileName());
}

} // end anonymous namespace

Object InitEnvMiscellaneous(Env env) {
Object exports = Object::New(env);

exports["get_module_file_name"] = Function::New(env, GetModuleFileName);

return exports;
}

#endif
12 changes: 12 additions & 0 deletions test/env_misc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const assert = require('assert');
const { pathToFileURL } = require('url');

module.exports = require('./common').runTest(test);

function test (binding, { bindingPath } = {}) {
const path = binding.env_misc.get_module_file_name();
const bindingFileUrl = pathToFileURL(bindingPath).toString();
assert(bindingFileUrl === path);
}
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ if (napiVersion < 8 && !filterConditionsProvided) {
testModules.splice(testModules.indexOf('type_taggable'), 1);
}

if (napiVersion < 9 && !filterConditionsProvided) {
testModules.splice(testModules.indexOf('env_misc'), 1);
}

(async function () {
console.log(`Testing with Node-API Version '${napiVersion}'.`);

Expand Down

0 comments on commit b83e453

Please sign in to comment.