Skip to content

Commit f233545

Browse files
committed
Use the injected NodeApiHostGetter to retrieve the host.
1 parent 3d2e03e commit f233545

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@ export function generateHeader() {
1111
1212
#include "NodeApiHost.hpp"
1313
14+
typedef NodeApiHost* (*NodeApiHostGetter)(node_api_basic_env env);
15+
extern "C" void inject_weak_node_api_host_getter(NodeApiHostGetter host_getter);
16+
1417
typedef void(*InjectHostFunction)(const NodeApiHost&);
1518
extern "C" void inject_weak_node_api_host(const NodeApiHost& host);
1619
`;
1720
}
1821

1922
function generateFunctionImpl(fn: FunctionDecl) {
20-
const { name, returnType, argumentTypes } = fn;
23+
const { name, returnType, argumentTypes, needEnv } = fn;
2124
return generateFunction({
2225
...fn,
2326
extern: true,
2427
body: `
25-
if (g_host.${name} == nullptr) {
28+
NodeApiHost* host = g_host_getter(${needEnv ? "arg0" : "nullptr"});
29+
if (host->${name} == nullptr) {
2630
fprintf(stderr, "Node-API function '${name}' called before it was injected!\\n");
2731
abort();
2832
}
29-
${returnType === "void" ? "" : "return "} g_host.${name}(
33+
${returnType === "void" ? "" : "return "} host->${name}(
3034
${argumentTypes.map((_, index) => `arg${index}`).join(", ")}
3135
);
3236
`,
@@ -44,6 +48,11 @@ export function generateSource(functions: FunctionDecl[]) {
4448
* It is set via inject_weak_node_api_host() before any Node-API function is dispatched.
4549
* All Node-API calls are routed through this host.
4650
*/
51+
52+
NodeApiHostGetter g_host_getter;
53+
54+
void inject_weak_node_api_host_getter(NodeApiHostGetter host_getter) { g_host_getter = host_getter; };
55+
4756
NodeApiHost g_host;
4857
void inject_weak_node_api_host(const NodeApiHost& host) {
4958
g_host = host;

packages/weak-node-api/src/node-api-functions.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export type FunctionDecl = {
8282
returnType: string;
8383
noReturn: boolean;
8484
argumentTypes: string[];
85+
needEnv: boolean;
8586
libraryPath: string;
8687
fallbackReturnStatement: string;
8788
};
@@ -127,14 +128,22 @@ export function getNodeApiFunctions(version: NodeApiVersion = "v8") {
127128
`Expected return type to be napi_status, got ${returnType}`,
128129
);
129130

131+
const argTypes = argumentTypes
132+
.split(",")
133+
.map((arg) => arg.trim().replace("_Bool", "bool"));
134+
135+
// Check if the first parameter is napi_env or node_api_basic_env
136+
const needEnv = argTypes.length > 0 &&
137+
(argTypes[0].includes("napi_env") ||
138+
argTypes[0].includes("node_api_basic_env"));
139+
130140
nodeApiFunctions.push({
131141
name,
132142
returnType,
133143
noReturn: node.type.qualType.includes("__attribute__((noreturn))"),
134144
kind: engineSymbols.has(name) ? "engine" : "runtime",
135-
argumentTypes: argumentTypes
136-
.split(",")
137-
.map((arg) => arg.trim().replace("_Bool", "bool")),
145+
argumentTypes: argTypes,
146+
needEnv,
138147
// Defer to the right library
139148
libraryPath: engineSymbols.has(name)
140149
? "libhermes.so"

0 commit comments

Comments
 (0)