Skip to content

Commit

Permalink
feat: adapt nodejs@20 and node-addon-api@8
Browse files Browse the repository at this point in the history
  • Loading branch information
ajihyf committed Mar 26, 2024
1 parent 91ed674 commit b9e884b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
timeout-minutes: 30
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
os:
- ubuntu-18.04
- macos-latest
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ node_modules/

.vscode
!.vscode/launch.json

package-lock.json
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

8 changes: 8 additions & 0 deletions naah.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

namespace naah {

namespace {
#ifdef NAPI_CPP_CUSTOM_NAMESPACE
namespace Napi = ::Napi::NAPI_CPP_CUSTOM_NAMESPACE;
#else
namespace Napi = ::Napi;
#endif
} // namespace

template <typename T, typename Enabled = void>
struct ValueTransformer {
static std::optional<T> FromJS(Napi::Value);
Expand Down
40 changes: 39 additions & 1 deletion naah_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,44 @@ inline ObjectRegistration<T> Registration::Object() {
using NAAH_REGISTRATION_ADDON = naah::Registration; \
NODE_API_ADDON(NAAH_REGISTRATION_ADDON)

#define NAAH_REGISTRATION NAPI_C_CTOR(napi_helper_auto_register_function_)
#ifndef NAAH_CDECL
#ifdef _WIN32
#define NAAH_CDECL __cdecl
#else
#define NAAH_CDECL
#endif
#endif

#if defined(_MSC_VER)
#if defined(__cplusplus)
#define NAAH_C_CTOR(fn) \
static void NAAH_CDECL fn(void); \
namespace { \
struct fn##_ { \
fn##_() { fn(); } \
} fn##_v_; \
} \
static void NAAH_CDECL fn(void)
#else // !defined(__cplusplus)
#pragma section(".CRT$XCU", read)
// The NAAH_C_CTOR macro defines a function fn that is called during CRT
// initialization.
// C does not support dynamic initialization of static variables and this code
// simulates C++ behavior. Exporting the function pointer prevents it from being
// optimized. See for details:
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170
#define NAAH_C_CTOR(fn) \
static void NAAH_CDECL fn(void); \
__declspec(dllexport, allocate(".CRT$XCU")) void(NAAH_CDECL * fn##_)(void) = \
fn; \
static void NAAH_CDECL fn(void)
#endif // defined(__cplusplus)
#else
#define NAAH_C_CTOR(fn) \
static void fn(void) __attribute__((constructor)); \
static void fn(void)
#endif

#define NAAH_REGISTRATION NAAH_C_CTOR(napi_helper_auto_register_function_)

#endif // SRC_NAAH_INL_H_
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
"eslint": "^7.32.0",
"eslint-config-standard": "^16.0.3",
"mocha": "^9.2.2",
"node-gyp": "^9.0.0",
"node-gyp": "^10.0.0",
"husky": "^7.0.0"
},
"dependencies": {
"node-addon-api": "^4.0.0"
"node-addon-api": ">=4"
},
"files": [
"*.{h,gyp,gypi}"
Expand Down
6 changes: 6 additions & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
'includes': ['./common.gypi', './except.gypi'],
'sources': ['>@(binding_sources)']
},
{
'target_name': 'binding_namespace',
'includes': ['./common.gypi', './except.gypi'],
'sources': ['>@(binding_sources)'],
'defines': [ 'NAPI_CPP_CUSTOM_NAMESPACE=MyCustomNapi' ]
},
{
'target_name': 'binding_noexcept',
'includes': ['./common.gypi', './noexcept.gypi'],
Expand Down
4 changes: 4 additions & 0 deletions test/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ exports.forEachBinding = cb => {
cb(bindings('binding.node'))
})

describe('Exception with namespace', () => {
cb(bindings('binding_namespace.node'))
})

describe('No Exception', () => {
cb(bindings('binding_noexcept.node'))
})
Expand Down

0 comments on commit b9e884b

Please sign in to comment.