-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgenerate-weak-node-api.ts
More file actions
88 lines (79 loc) · 3 KB
/
generate-weak-node-api.ts
File metadata and controls
88 lines (79 loc) · 3 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
import fs from "node:fs";
import path from "node:path";
import { spawnSyncCrossPlatform } from "./platform-utils.js";
import { FunctionDecl, getNodeApiFunctions } from "./node-api-functions";
export const WEAK_NODE_API_PATH = path.join(__dirname, "../weak-node-api");
/**
* 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-modules",
"#include <node_api.h>", // Node-API
"#include <stdio.h>", // fprintf()
"#include <stdlib.h>", // abort()
// Generate the struct of function pointers
"struct WeakNodeApiHost {",
...functions.map(
({ returnType, noReturn, name, argumentTypes }) =>
`${returnType} ${
noReturn ? " __attribute__((noreturn))" : ""
}(*${name})(${argumentTypes.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-modules",
`#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, noReturn, name, argumentTypes }) => {
return [
`extern "C" ${returnType} ${
noReturn ? " __attribute__((noreturn))" : ""
}${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(", ") +
");",
"};",
];
}),
].join("\n");
}
async function run() {
await fs.promises.mkdir(WEAK_NODE_API_PATH, { recursive: true });
const nodeApiFunctions = getNodeApiFunctions();
const header = generateHeader(nodeApiFunctions);
const headerPath = path.join(WEAK_NODE_API_PATH, "weak_node_api.hpp");
await fs.promises.writeFile(headerPath, header, "utf-8");
spawnSyncCrossPlatform("clang-format", ["-i", headerPath], { stdio: "inherit" });
const source = generateSource(nodeApiFunctions);
const sourcePath = path.join(WEAK_NODE_API_PATH, "weak_node_api.cpp");
await fs.promises.writeFile(sourcePath, source, "utf-8");
spawnSyncCrossPlatform("clang-format", ["-i", sourcePath], { stdio: "inherit" });
}
run().catch((err) => {
console.error(err);
process.exitCode = 1;
});