Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/node-addon-examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export const suites: Record<
require("../tests/buffers/addon.js");
},
async: () => require("../tests/async/addon.js") as () => Promise<void>,
"module-register": () =>
require("../tests/module-register/addon.js") as () => void,
"threadsafe-function": () =>
require("../tests/threadsafe-function/addon.js") as () => Promise<void>,
},
Expand Down
28 changes: 28 additions & 0 deletions packages/node-addon-examples/tests/module-register/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.15...3.31)
project(module-register-test)

find_package(weak-node-api REQUIRED CONFIG)

add_library(module-register-test-addon SHARED addon.c)

option(BUILD_APPLE_FRAMEWORK "Wrap addon in an Apple framework" ON)

if(APPLE AND BUILD_APPLE_FRAMEWORK)
set_target_properties(module-register-test-addon PROPERTIES
FRAMEWORK TRUE
MACOSX_FRAMEWORK_IDENTIFIER module-register-test.addon
MACOSX_FRAMEWORK_SHORT_VERSION_STRING 1.0
MACOSX_FRAMEWORK_BUNDLE_VERSION 1.0
XCODE_ATTRIBUTE_SKIP_INSTALL NO
OUTPUT_NAME addon
)
else()
set_target_properties(module-register-test-addon PROPERTIES
PREFIX ""
SUFFIX .node
OUTPUT_NAME addon
)
endif()

target_link_libraries(module-register-test-addon PRIVATE weak-node-api)
target_compile_features(module-register-test-addon PRIVATE cxx_std_17)
42 changes: 42 additions & 0 deletions packages/node-addon-examples/tests/module-register/addon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <node_api.h>

// This addon registers itself the deprecated way — a napi_module_register call
// made while the library loads — and deliberately exports no
// napi_register_module_v1 symbol, so a host that only looks for that symbol
// cannot load it.
//
// The constructor is hand-rolled because node_api.h no longer offers a macro
// that emits one: NAPI_MODULE_X is now an alias of the symbol-based
// NAPI_MODULE.

static napi_value Registration(napi_env env, napi_callback_info info) {
(void)info;
napi_value result;
if (napi_create_string_utf8(env, "napi_module_register", NAPI_AUTO_LENGTH,
&result) != napi_ok) {
return NULL;
}
return result;
}

static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
{"registration", NULL, Registration, NULL, NULL, NULL, napi_default,
NULL},
};
if (napi_define_properties(env, exports,
sizeof(properties) / sizeof(properties[0]),
properties) != napi_ok) {
return NULL;
}
return exports;
}

static napi_module addon_module = {
NAPI_MODULE_VERSION, 0, __FILE__, Init, "module-register-test",
NULL, {0},
};

__attribute__((constructor)) static void RegisterAddon(void) {
napi_module_register(&addon_module);
}
8 changes: 8 additions & 0 deletions packages/node-addon-examples/tests/module-register/addon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const assert = require("assert");
// cmake-rn emits to {targetSourceDir}/build/{configuration}, and this package's
// build script pins the configuration.
const addon = require("./build/RelWithDebInfo/addon.node");

module.exports = () => {
assert.strictEqual(addon.registration(), "napi_module_register");
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "module-register-test",
"version": "0.0.0",
"description": "Tests of the deprecated napi_module_register registration",
"main": "addon.js",
"private": true
}
Loading