Skip to content

Commit 03eae86

Browse files
committed
Splitting WeakNodeApiHost out of weak_node_api and renaming to NodeApiHost
1 parent 7108fd8 commit 03eae86

File tree

7 files changed

+72
-34
lines changed

7 files changed

+72
-34
lines changed

packages/host/scripts/generate-injector.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export function generateSource(functions: FunctionDecl[]) {
5959
abort();
6060
}
6161
62-
log_debug("Injecting WeakNodeApiHost");
63-
inject_weak_node_api_host(WeakNodeApiHost {
62+
log_debug("Injecting NodeApiHost");
63+
inject_weak_node_api_host(NodeApiHost {
6464
${functions
6565
.filter(
6666
({ kind, name }) =>

packages/weak-node-api/.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
/build-tests/
66
/*.xcframework
77
/*.android.node
8-
/generated/weak_node_api.cpp
9-
/generated/weak_node_api.hpp
8+
/generated/
109

1110
# Copied from node-api-headers by scripts/copy-node-api-headers.ts
1211
/include/

packages/weak-node-api/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ target_sources(${PROJECT_NAME}
1616
PUBLIC FILE_SET HEADERS
1717
BASE_DIRS ${GENERATED_SOURCE_DIR} ${INCLUDE_DIR} FILES
1818
${GENERATED_SOURCE_DIR}/weak_node_api.hpp
19+
${GENERATED_SOURCE_DIR}/NodeApiHost.hpp
1920
${INCLUDE_DIR}/js_native_api_types.h
2021
${INCLUDE_DIR}/js_native_api.h
2122
${INCLUDE_DIR}/node_api_types.h

packages/weak-node-api/scripts/generate.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "../src/node-api-functions.js";
1010

1111
import * as weakNodeApiGenerator from "./generators/weak-node-api.js";
12+
import * as hostGenerator from "./generators/NodeApiHost.js";
1213

1314
export const OUTPUT_PATH = path.join(import.meta.dirname, "../generated");
1415

@@ -41,6 +42,16 @@ async function run() {
4142
await fs.promises.mkdir(OUTPUT_PATH, { recursive: true });
4243

4344
const functions = getNodeApiFunctions();
45+
await generateFile({
46+
functions,
47+
fileName: "NodeApiHost.hpp",
48+
generator: hostGenerator.generateHeader,
49+
headingComment: `
50+
@brief NodeApiHost struct.
51+
52+
This header provides a struct of Node-API functions implemented by a host to inject its implementations.
53+
`,
54+
});
4455
await generateFile({
4556
functions,
4657
fileName: "weak_node_api.hpp",
@@ -50,6 +61,11 @@ async function run() {
5061
functions,
5162
fileName: "weak_node_api.cpp",
5263
generator: weakNodeApiGenerator.generateSource,
64+
headingComment: `
65+
@brief Weak Node-API host injection implementation.
66+
67+
Provides the implementation for deferring Node-API function calls from addons into a Node-API host.
68+
`,
5369
});
5470
}
5571

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { FunctionDecl } from "../../src/node-api-functions.js";
2+
3+
export function generateFunctionDecl({
4+
returnType,
5+
name,
6+
argumentTypes,
7+
}: FunctionDecl) {
8+
return `${returnType} (*${name})(${argumentTypes.join(", ")});`;
9+
}
10+
11+
/**
12+
* Generates source code for a version script for the given Node API version.
13+
*/
14+
export function generateHeader(functions: FunctionDecl[]) {
15+
return `
16+
#pragma once
17+
18+
#include <node_api.h>
19+
20+
// Ideally we would have just used NAPI_NO_RETURN, but
21+
// __declspec(noreturn) (when building with Microsoft Visual C++) cannot be used on members of a struct
22+
// TODO: If we targeted C++23 we could use std::unreachable()
23+
24+
#if defined(__GNUC__)
25+
#define WEAK_NODE_API_UNREACHABLE __builtin_unreachable()
26+
#else
27+
#define WEAK_NODE_API_UNREACHABLE __assume(0)
28+
#endif
29+
30+
// Generate the struct of function pointers
31+
struct NodeApiHost {
32+
${functions.map(generateFunctionDecl).join("\n")}
33+
};
34+
`;
35+
}

packages/weak-node-api/scripts/generators/weak-node-api.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,21 @@
11
import type { FunctionDecl } from "../../src/node-api-functions.js";
22
import { generateFunction } from "./shared.js";
33

4-
export function generateFunctionDecl({
5-
returnType,
6-
name,
7-
argumentTypes,
8-
}: FunctionDecl) {
9-
return `${returnType} (*${name})(${argumentTypes.join(", ")});`;
10-
}
11-
124
/**
135
* Generates source code for a version script for the given Node API version.
146
*/
15-
export function generateHeader(functions: FunctionDecl[]) {
7+
export function generateHeader() {
168
return `
179
#pragma once
1810
19-
#include <node_api.h> // Node-API
11+
#include <node_api.h>
2012
#include <stdio.h> // fprintf()
2113
#include <stdlib.h> // abort()
22-
23-
// Ideally we would have just used NAPI_NO_RETURN, but
24-
// __declspec(noreturn) (when building with Microsoft Visual C++) cannot be used on members of a struct
25-
// TODO: If we targeted C++23 we could use std::unreachable()
26-
27-
#if defined(__GNUC__)
28-
#define WEAK_NODE_API_UNREACHABLE __builtin_unreachable()
29-
#else
30-
#define WEAK_NODE_API_UNREACHABLE __assume(0)
31-
#endif
3214
33-
// Generate the struct of function pointers
34-
struct WeakNodeApiHost {
35-
${functions.map(generateFunctionDecl).join("\n")}
36-
};
37-
typedef void(*InjectHostFunction)(const WeakNodeApiHost&);
38-
extern "C" void inject_weak_node_api_host(const WeakNodeApiHost& host);
15+
#include "NodeApiHost.hpp"
16+
17+
typedef void(*InjectHostFunction)(const NodeApiHost&);
18+
extern "C" void inject_weak_node_api_host(const NodeApiHost& host);
3919
`;
4020
}
4121

@@ -63,8 +43,15 @@ export function generateSource(functions: FunctionDecl[]) {
6343
return `
6444
#include "weak_node_api.hpp"
6545
66-
WeakNodeApiHost g_host;
67-
void inject_weak_node_api_host(const WeakNodeApiHost& host) {
46+
/**
47+
* @brief Global instance of the injected Node-API host.
48+
*
49+
* This variable holds the function table for Node-API calls.
50+
* It is set via inject_weak_node_api_host() before any Node-API function is dispatched.
51+
* All Node-API calls are routed through this host.
52+
*/
53+
NodeApiHost g_host;
54+
void inject_weak_node_api_host(const NodeApiHost& host) {
6855
g_host = host;
6956
};
7057

packages/weak-node-api/tests/test_inject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
TEST_CASE("inject_weak_node_api_host") {
55
SECTION("is callable") {
6-
WeakNodeApiHost host{};
6+
NodeApiHost host{};
77
inject_weak_node_api_host(host);
88
}
99

@@ -14,7 +14,7 @@ TEST_CASE("inject_weak_node_api_host") {
1414
called = true;
1515
return napi_status::napi_ok;
1616
};
17-
WeakNodeApiHost host{.napi_create_object = my_create_object};
17+
NodeApiHost host{.napi_create_object = my_create_object};
1818
inject_weak_node_api_host(host);
1919

2020
napi_value result;

0 commit comments

Comments
 (0)