-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgenerate-weak-node-api.ts
More file actions
103 lines (94 loc) · 3.31 KB
/
generate-weak-node-api.ts
File metadata and controls
103 lines (94 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import fs from "node:fs";
import path from "node:path";
import cp from "node:child_process";
import {
FunctionDecl,
getNodeApiFunctions,
} from "../src/node-api-functions.js";
export const OUTPUT_PATH = path.join(import.meta.dirname, "../generated");
/**
* Generates source code for a version script for the given Node API version.
*/
export function generateHeader(functions: FunctionDecl[]) {
return [
"// This file is generated by react-native-node-api",
"#include <node_api.h>", // Node-API
"#include <stdio.h>", // fprintf()
"#include <stdlib.h>", // abort()
"",
// Ideally we would have just used NAPI_NO_RETURN, but
// __declspec(noreturn) (when building with Microsoft Visual C++) cannot be used on members of a struct
// TODO: If we targeted C++23 we could use std::unreachable()
"#if defined(__GNUC__)",
"#define WEAK_NODE_API_UNREACHABLE __builtin_unreachable();",
"#else",
"#define WEAK_NODE_API_UNREACHABLE __assume(0);",
"#endif",
"",
// Generate the struct of function pointers
"struct WeakNodeApiHost {",
...functions.map(({ returnType, name, argumentTypes }) =>
[
returnType,
// Signature
`(*${name})(${argumentTypes.join(", ")});`,
].join(" "),
),
"};",
"typedef void(*InjectHostFunction)(const WeakNodeApiHost&);",
`extern "C" void inject_weak_node_api_host(const WeakNodeApiHost& host);`,
].join("\n");
}
/**
* Generates source code for a version script for the given Node API version.
*/
export function generateSource(functions: FunctionDecl[]) {
return [
"// This file is generated by react-native-node-api",
`#include "weak_node_api.hpp"`, // Generated header
// Generate the struct of function pointers
"WeakNodeApiHost g_host;",
"void inject_weak_node_api_host(const WeakNodeApiHost& host) {",
" g_host = host;",
"};",
``,
// Generate function calling into the host
...functions.flatMap(({ returnType, name, argumentTypes, noReturn }) => {
return [
'extern "C"',
returnType,
name,
"(",
argumentTypes.map((type, index) => `${type} arg${index}`).join(", "),
") {",
`if (g_host.${name} == nullptr) {`,
` fprintf(stderr, "Node-API function '${name}' called before it was injected!\\n");`,
" abort();",
"}",
returnType === "void" ? "" : "return ",
`g_host.${name}`,
"(",
argumentTypes.map((_, index) => `arg${index}`).join(", "),
");",
noReturn ? "WEAK_NODE_API_UNREACHABLE" : "",
"};",
].join(" ");
}),
].join("\n");
}
async function run() {
await fs.promises.mkdir(OUTPUT_PATH, { recursive: true });
const nodeApiFunctions = getNodeApiFunctions();
const header = generateHeader(nodeApiFunctions);
const headerPath = path.join(OUTPUT_PATH, "weak_node_api.hpp");
await fs.promises.writeFile(headerPath, header, "utf-8");
cp.spawnSync("clang-format", ["-i", headerPath], { stdio: "inherit" });
const source = generateSource(nodeApiFunctions);
const sourcePath = path.join(OUTPUT_PATH, "weak_node_api.cpp");
await fs.promises.writeFile(sourcePath, source, "utf-8");
cp.spawnSync("clang-format", ["-i", sourcePath], { stdio: "inherit" });
}
run().catch((err) => {
console.error(err);
process.exitCode = 1;
});