Skip to content

Commit b83e453

Browse files
KevinEadymhdawson
authored andcommitted
src: add Env::GetModuleFileName
PR-URL: #1327 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Michael Dawson <[email protected]
1 parent ac4c87f commit b83e453

File tree

9 files changed

+72
-2
lines changed

9 files changed

+72
-2
lines changed

doc/env.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ addon. The item will be passed to the function `fini` which gets called when an
131131
instance of the addon is unloaded. This overload accepts an additional hint to
132132
be passed to `fini`.
133133

134+
### GetModuleFileName
135+
136+
```cpp
137+
const char* Napi::Env::GetModuleFileName() const;
138+
```
139+
140+
Returns a A URL containing the absolute path of the location from which the
141+
add-on was loaded. For a file on the local file system it will start with
142+
`file://`. The string is null-terminated and owned by env and must thus not be
143+
modified or freed. It is only valid while the add-on is loaded.
144+
134145
### AddCleanupHook
135146

136147
```cpp

napi-inl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,14 @@ void Env::DefaultFiniWithHint(Env, DataType* data, HintType*) {
572572
}
573573
#endif // NAPI_VERSION > 5
574574

575+
#if NAPI_VERSION > 8
576+
inline const char* Env::GetModuleFileName() const {
577+
const char* result;
578+
napi_status status = node_api_get_module_file_name(_env, &result);
579+
NAPI_THROW_IF_FAILED(*this, status, nullptr);
580+
return result;
581+
}
582+
#endif // NAPI_VERSION > 8
575583
////////////////////////////////////////////////////////////////////////////////
576584
// Value class
577585
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ class Env {
367367
} * data;
368368
};
369369
#endif // NAPI_VERSION > 2
370+
371+
#if NAPI_VERSION > 8
372+
const char* GetModuleFileName() const;
373+
#endif // NAPI_VERSION > 8
370374
};
371375

372376
/// A JavaScript value of unknown type.

test/binding.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ Object InitThunkingManual(Env env);
7878
Object InitObjectFreezeSeal(Env env);
7979
Object InitTypeTaggable(Env env);
8080
#endif
81-
81+
#if (NAPI_VERSION > 8)
82+
Object InitEnvMiscellaneous(Env env);
83+
#endif
8284
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
8385
Object InitMaybeCheck(Env env);
8486
#endif
@@ -171,6 +173,9 @@ Object Init(Env env, Object exports) {
171173
exports.Set("object_freeze_seal", InitObjectFreezeSeal(env));
172174
exports.Set("type_taggable", InitTypeTaggable(env));
173175
#endif
176+
#if (NAPI_VERSION > 8)
177+
exports.Set("env_misc", InitEnvMiscellaneous(env));
178+
#endif
174179

175180
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
176181
exports.Set("maybe_check", InitMaybeCheck(env));

test/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'dataview/dataview.cc',
2727
'dataview/dataview_read_write.cc',
2828
'env_cleanup.cc',
29+
'env_misc.cc',
2930
'error.cc',
3031
'error_handling_for_primitives.cc',
3132
'external.cc',

test/common/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ exports.runTest = async function (test, buildType, buildPathRoot = process.env.B
164164
].map(it => require.resolve(it));
165165

166166
for (const item of bindings) {
167-
await Promise.resolve(test(require(item)))
167+
await Promise.resolve(test(require(item), { bindingPath: item }))
168168
.finally(exports.mustCall());
169169
}
170170
};

test/env_misc.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "napi.h"
2+
#include "test_helper.h"
3+
4+
#if (NAPI_VERSION > 8)
5+
6+
using namespace Napi;
7+
8+
namespace {
9+
10+
Value GetModuleFileName(const CallbackInfo& info) {
11+
Env env = info.Env();
12+
return String::New(env, env.GetModuleFileName());
13+
}
14+
15+
} // end anonymous namespace
16+
17+
Object InitEnvMiscellaneous(Env env) {
18+
Object exports = Object::New(env);
19+
20+
exports["get_module_file_name"] = Function::New(env, GetModuleFileName);
21+
22+
return exports;
23+
}
24+
25+
#endif

test/env_misc.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const { pathToFileURL } = require('url');
5+
6+
module.exports = require('./common').runTest(test);
7+
8+
function test (binding, { bindingPath } = {}) {
9+
const path = binding.env_misc.get_module_file_name();
10+
const bindingFileUrl = pathToFileURL(bindingPath).toString();
11+
assert(bindingFileUrl === path);
12+
}

test/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ if (napiVersion < 8 && !filterConditionsProvided) {
137137
testModules.splice(testModules.indexOf('type_taggable'), 1);
138138
}
139139

140+
if (napiVersion < 9 && !filterConditionsProvided) {
141+
testModules.splice(testModules.indexOf('env_misc'), 1);
142+
}
143+
140144
(async function () {
141145
console.log(`Testing with Node-API Version '${napiVersion}'.`);
142146

0 commit comments

Comments
 (0)