Skip to content

Commit 6e5d7a1

Browse files
committed
Squashed commit of the following:
commit 27217585d7d4412eb7e30c8a3b8df843494f6bd7 Author: Kræn Hansen <[email protected]> Date: Tue May 6 17:04:37 2025 +0200 Fix compilation warnings
1 parent f473108 commit 6e5d7a1

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

packages/react-native-node-api-modules/cpp/CxxNodeApiHostModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ bool CxxNodeApiHostModule::loadNodeAddon(NodeAddon &addon,
6969
// the pointer address of the loaded module
7070
addon.generatedName.resize(32, '\0');
7171
snprintf(addon.generatedName.data(), addon.generatedName.size(),
72-
"RN$NodeAddon_%lX", (uintptr_t)addon.moduleHandle);
72+
"RN$NodeAddon_%p", addon.moduleHandle);
7373

7474
initFn = LoaderPolicy::getSymbol(library, "napi_register_module_v1");
7575
if (NULL != initFn) {

packages/react-native-node-api-modules/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"scripts": {
1818
"build": "tsc --build",
1919
"copy-node-api-headers": "tsx scripts/copy-node-api-headers.ts",
20-
"build-weak-node-api": "tsx scripts/build-weak-node-api.ts",
20+
"generate-weak-node-api": "tsx scripts/generate-weak-node-api.ts",
21+
"build-weak-node-api": "npm run generate-weak-node-api && react-native-node-api-cmake --android --apple --no-auto-link --no-weak-node-api-linkage --source ./weak-node-api",
2122
"test": "tsx --test src/node/**/*.test.ts src/node/*.test.ts"
2223
},
2324
"keywords": [

packages/react-native-node-api-modules/scripts/build-weak-node-api.ts renamed to packages/react-native-node-api-modules/scripts/generate-weak-node-api.ts

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,46 +76,56 @@ type FunctionDecl = {
7676
returnType: string;
7777
argumentTypes: string[];
7878
libraryPath: string;
79+
fallbackReturnStatement: string;
7980
};
8081

81-
export function generateNodeApiFunction({
82+
export function generateNodeApiFunctionStubBody({
8283
name,
8384
returnType,
8485
argumentTypes,
8586
libraryPath,
87+
fallbackReturnStatement,
8688
}: FunctionDecl) {
87-
const stubbedReturnStatement =
88-
returnType === "void"
89+
const returnStatement =
90+
name === "napi_fatal_error"
8991
? "abort();"
90-
: "return napi_status::napi_generic_failure;";
92+
: returnType === "void"
93+
? ""
94+
: `return real_func(${argumentTypes
95+
.map((t, index) => `arg${index}`)
96+
.join(", ")}); // Call the real function`;
9197
return `
92-
typedef ${returnType} (*${name}_t)(${argumentTypes.join(", ")});
93-
94-
${returnType} ${name}(${argumentTypes
95-
.map((type, index) => `${type} arg${index}`)
96-
.join(", ")}) {
97-
#ifdef NODE_API_REEXPORT
9898
static ${name}_t real_func = NULL;
9999
100100
if (!real_func) {
101101
void* handle = dlopen("${libraryPath}", RTLD_LAZY | RTLD_GLOBAL);
102102
if (!handle) {
103103
fprintf(stderr, "Failed to load ${libraryPath}: %s\\n", dlerror());
104-
${stubbedReturnStatement}
104+
${fallbackReturnStatement}
105105
}
106106
107107
real_func = (${name}_t)dlsym(handle, "${name}");
108108
if (!real_func) {
109109
fprintf(stderr, "Failed to find symbol: %s\\n", dlerror());
110-
${stubbedReturnStatement}
110+
${fallbackReturnStatement}
111111
}
112112
}
113113
114-
${returnType === "void" ? "" : "return "}real_func(${argumentTypes
115-
.map((t, index) => `arg${index}`)
116-
.join(", ")}); // Call the real function
114+
${returnStatement}
115+
`;
116+
}
117+
118+
export function generateNodeApiFunction(decl: FunctionDecl) {
119+
const { name, returnType, argumentTypes, fallbackReturnStatement } = decl;
120+
return `
121+
typedef ${returnType} (*${name}_t)(${argumentTypes.join(", ")});
122+
${returnType} ${name}(${argumentTypes
123+
.map((type, index) => `${type} arg${index}`)
124+
.join(", ")}) {
125+
#ifdef NODE_API_REEXPORT
126+
${generateNodeApiFunctionStubBody(decl)}
117127
#else
118-
${stubbedReturnStatement}
128+
${fallbackReturnStatement}
119129
#endif
120130
}`;
121131
}
@@ -179,6 +189,10 @@ export function generateFakeNodeApiSource(version: NodeApiVersion) {
179189
libraryPath: engineSymbols.has(name)
180190
? "libhermes.so"
181191
: "libnode-api-host.so",
192+
fallbackReturnStatement:
193+
returnType === "void"
194+
? "abort();"
195+
: "return napi_status::napi_generic_failure;",
182196
})
183197
);
184198
}
@@ -201,21 +215,6 @@ async function run() {
201215
sourceCode,
202216
"utf-8"
203217
);
204-
// Build for all supported platforms
205-
cp.execFileSync(
206-
"react-native-node-api-cmake",
207-
[
208-
"--android",
209-
"--apple",
210-
"--no-auto-link",
211-
"--no-weak-node-api-linkage",
212-
// TODO: Add support for passing variables through to CMake
213-
// "-D NODE_API_REEXPORT=1",
214-
"--source",
215-
WEAK_NODE_API_PATH,
216-
],
217-
{ stdio: "inherit" }
218-
);
219218
}
220219

221220
run().catch((err) => {

0 commit comments

Comments
 (0)