Skip to content

Commit 6ba3891

Browse files
authored
fix: missing node_api_nogc_env definition (#1585)
1 parent f3eaae6 commit 6ba3891

File tree

4 files changed

+108
-20
lines changed

4 files changed

+108
-20
lines changed
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Node.js CI with node-api-headers
2+
3+
on: [push, pull_request]
4+
5+
env:
6+
PYTHON_VERSION: '3.11'
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
test:
13+
timeout-minutes: 30
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
api_version:
18+
- '9'
19+
node-version:
20+
- 22.x
21+
node-api-headers-version:
22+
- '1.1.0'
23+
- '1.2.0'
24+
- '1.3.0'
25+
os:
26+
- ubuntu-latest
27+
compiler:
28+
- gcc
29+
- clang
30+
runs-on: ${{ matrix.os }}
31+
steps:
32+
- name: Harden Runner
33+
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
34+
with:
35+
egress-policy: audit
36+
37+
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0
38+
- name: Set up Python ${{ env.PYTHON_VERSION }}
39+
uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3 # v5.2.0
40+
with:
41+
python-version: ${{ env.PYTHON_VERSION }}
42+
- name: Use Node.js ${{ matrix.node-version }}
43+
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
44+
with:
45+
node-version: ${{ matrix.node-version }}
46+
- name: Check Node.js installation
47+
run: |
48+
node --version
49+
npm --version
50+
- name: Install dependencies
51+
run: |
52+
npm install
53+
npm install "node-api-headers@${{ matrix.node-api-headers-version }}"
54+
- name: npm test
55+
run: |
56+
export NAPI_VERSION=${{ matrix.api_version }}
57+
if [ "${{ matrix.compiler }}" = "gcc" ]; then
58+
export CC="gcc" CXX="g++"
59+
fi
60+
if [ "${{ matrix.compiler }}" = "clang" ]; then
61+
export CC="clang" CXX="clang++"
62+
fi
63+
echo "CC=\"$CC\" CXX=\"$CXX\""
64+
echo "$CC --version"
65+
$CC --version
66+
echo "$CXX --version"
67+
$CXX --version
68+
export CFLAGS="$CFLAGS -O3 --coverage" LDFLAGS="$LDFLAGS --coverage"
69+
export use_node_api_headers=true
70+
echo "CFLAGS=\"$CFLAGS\" LDFLAGS=\"$LDFLAGS\""
71+
npm run pretest -- --verbose

napi-inl.h

+16-15
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace details {
3535
constexpr int napi_no_external_buffers_allowed = 22;
3636

3737
template <typename FreeType>
38-
inline void default_basic_finalizer(node_api_nogc_env /*env*/,
38+
inline void default_basic_finalizer(node_addon_api_basic_env /*env*/,
3939
void* data,
4040
void* /*hint*/) {
4141
delete static_cast<FreeType*>(data);
@@ -45,8 +45,9 @@ inline void default_basic_finalizer(node_api_nogc_env /*env*/,
4545
// garbage-collected.
4646
// TODO: Replace this code with `napi_add_finalizer()` whenever it becomes
4747
// available on all supported versions of Node.js.
48-
template <typename FreeType,
49-
node_api_nogc_finalize finalizer = default_basic_finalizer<FreeType>>
48+
template <
49+
typename FreeType,
50+
node_addon_api_basic_finalize finalizer = default_basic_finalizer<FreeType>>
5051
inline napi_status AttachData(napi_env env,
5152
napi_value obj,
5253
FreeType* data,
@@ -192,10 +193,10 @@ template <typename T, typename Finalizer, typename Hint = void>
192193
struct FinalizeData {
193194
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
194195
template <typename F = Finalizer,
195-
typename =
196-
std::enable_if_t<std::is_invocable_v<F, node_api_nogc_env, T*>>>
196+
typename = std::enable_if_t<
197+
std::is_invocable_v<F, node_addon_api_basic_env, T*>>>
197198
#endif
198-
static inline void Wrapper(node_api_nogc_env env,
199+
static inline void Wrapper(node_addon_api_basic_env env,
199200
void* data,
200201
void* finalizeHint) NAPI_NOEXCEPT {
201202
WrapVoidCallback([&] {
@@ -208,9 +209,9 @@ struct FinalizeData {
208209
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
209210
template <typename F = Finalizer,
210211
typename = std::enable_if_t<
211-
!std::is_invocable_v<F, node_api_nogc_env, T*>>,
212+
!std::is_invocable_v<F, node_addon_api_basic_env, T*>>,
212213
typename = void>
213-
static inline void Wrapper(node_api_nogc_env env,
214+
static inline void Wrapper(node_addon_api_basic_env env,
214215
void* data,
215216
void* finalizeHint) NAPI_NOEXCEPT {
216217
#ifdef NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS
@@ -228,9 +229,9 @@ struct FinalizeData {
228229
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
229230
template <typename F = Finalizer,
230231
typename = std::enable_if_t<
231-
std::is_invocable_v<F, node_api_nogc_env, T*, Hint*>>>
232+
std::is_invocable_v<F, node_addon_api_basic_env, T*, Hint*>>>
232233
#endif
233-
static inline void WrapperWithHint(node_api_nogc_env env,
234+
static inline void WrapperWithHint(node_addon_api_basic_env env,
234235
void* data,
235236
void* finalizeHint) NAPI_NOEXCEPT {
236237
WrapVoidCallback([&] {
@@ -243,9 +244,9 @@ struct FinalizeData {
243244
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
244245
template <typename F = Finalizer,
245246
typename = std::enable_if_t<
246-
!std::is_invocable_v<F, node_api_nogc_env, T*, Hint*>>,
247+
!std::is_invocable_v<F, node_addon_api_basic_env, T*, Hint*>>,
247248
typename = void>
248-
static inline void WrapperWithHint(node_api_nogc_env env,
249+
static inline void WrapperWithHint(node_addon_api_basic_env env,
249250
void* data,
250251
void* finalizeHint) NAPI_NOEXCEPT {
251252
#ifdef NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS
@@ -576,9 +577,9 @@ inline Maybe<T> Just(const T& t) {
576577
// BasicEnv / Env class
577578
////////////////////////////////////////////////////////////////////////////////
578579

579-
inline BasicEnv::BasicEnv(node_api_nogc_env env) : _env(env) {}
580+
inline BasicEnv::BasicEnv(node_addon_api_basic_env env) : _env(env) {}
580581

581-
inline BasicEnv::operator node_api_nogc_env() const {
582+
inline BasicEnv::operator node_addon_api_basic_env() const {
582583
return _env;
583584
}
584585

@@ -5034,7 +5035,7 @@ inline napi_value ObjectWrap<T>::StaticSetterCallbackWrapper(
50345035
}
50355036

50365037
template <typename T>
5037-
inline void ObjectWrap<T>::FinalizeCallback(node_api_nogc_env env,
5038+
inline void ObjectWrap<T>::FinalizeCallback(node_addon_api_basic_env env,
50385039
void* data,
50395040
void* /*hint*/) {
50405041
// If the child class does not override _any_ Finalize() method, `env` will be

napi.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ template <typename T>
299299
using MaybeOrValue = T;
300300
#endif
301301

302+
#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
303+
using node_addon_api_basic_env = node_api_nogc_env;
304+
using node_addon_api_basic_finalize = node_api_nogc_finalize;
305+
#else
306+
using node_addon_api_basic_env = napi_env;
307+
using node_addon_api_basic_finalize = napi_finalize;
308+
#endif
309+
302310
/// Environment for Node-API values and operations.
303311
///
304312
/// All Node-API values and operations must be associated with an environment.
@@ -313,16 +321,17 @@ using MaybeOrValue = T;
313321
/// corresponds to an Isolate.
314322
class BasicEnv {
315323
private:
316-
node_api_nogc_env _env;
324+
node_addon_api_basic_env _env;
317325
#if NAPI_VERSION > 5
318326
template <typename T>
319327
static void DefaultFini(Env, T* data);
320328
template <typename DataType, typename HintType>
321329
static void DefaultFiniWithHint(Env, DataType* data, HintType* hint);
322330
#endif // NAPI_VERSION > 5
323331
public:
324-
BasicEnv(node_api_nogc_env env);
325-
operator node_api_nogc_env() const;
332+
BasicEnv(node_addon_api_basic_env env);
333+
334+
operator node_addon_api_basic_env() const;
326335

327336
// Without these operator overloads, the error:
328337
//
@@ -2469,7 +2478,9 @@ class ObjectWrap : public InstanceWrap<T>, public Reference<Object> {
24692478
napi_callback_info info);
24702479
static napi_value StaticSetterCallbackWrapper(napi_env env,
24712480
napi_callback_info info);
2472-
static void FinalizeCallback(node_api_nogc_env env, void* data, void* hint);
2481+
static void FinalizeCallback(node_addon_api_basic_env env,
2482+
void* data,
2483+
void* hint);
24732484

24742485
static void PostFinalizeCallback(napi_env env, void* data, void* hint);
24752486

test/binding.gyp

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
'value_type_cast.cc'
9090
],
9191
'want_coverage': '<!(node -p process.env.npm_config_coverage)',
92+
'use_node_api_headers': '<!(node -p process.env.use_node_api_headers)',
9293
'conditions': [
9394
['disable_deprecated!="true"', {
9495
'build_sources': ['object/object_deprecated.cc']
@@ -99,7 +100,11 @@
99100
['want_coverage=="true" and OS=="linux"', {
100101
'cflags_cc': ['--coverage'],
101102
'ldflags': ['--coverage'],
102-
}]
103+
}],
104+
['use_node_api_headers=="true"', {
105+
# prepend to the include_dirs list
106+
'include_dirs+': ["<!(node -p \"require('node-api-headers').include_dir\")"],
107+
}],
103108
],
104109
},
105110
'targets': [

0 commit comments

Comments
 (0)