|
1 | 1 | import type { FunctionDecl } from "../../src/node-api-functions.js"; |
2 | 2 | import { generateFunction } from "./shared.js"; |
3 | 3 |
|
4 | | -export function generateFunctionDecl({ |
5 | | - returnType, |
6 | | - name, |
7 | | - argumentTypes, |
8 | | -}: FunctionDecl) { |
9 | | - return `${returnType} (*${name})(${argumentTypes.join(", ")});`; |
10 | | -} |
11 | | - |
12 | 4 | /** |
13 | 5 | * Generates source code for a version script for the given Node API version. |
14 | 6 | */ |
15 | | -export function generateHeader(functions: FunctionDecl[]) { |
| 7 | +export function generateHeader() { |
16 | 8 | return ` |
17 | 9 | #pragma once |
18 | 10 |
|
19 | | - #include <node_api.h> // Node-API |
| 11 | + #include <node_api.h> |
20 | 12 | #include <stdio.h> // fprintf() |
21 | 13 | #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 |
32 | 14 | |
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); |
39 | 19 | `; |
40 | 20 | } |
41 | 21 |
|
@@ -63,8 +43,15 @@ export function generateSource(functions: FunctionDecl[]) { |
63 | 43 | return ` |
64 | 44 | #include "weak_node_api.hpp" |
65 | 45 |
|
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) { |
68 | 55 | g_host = host; |
69 | 56 | }; |
70 | 57 | |
|
0 commit comments