|
| 1 | +import type { FunctionDecl } from "../../src/node-api-functions.js"; |
| 2 | +import { generateFunction } from "./shared.js"; |
| 3 | + |
| 4 | +const ARGUMENT_NAMES_PR_FUNCTION: Record<string, undefined | string[]> = { |
| 5 | + napi_create_threadsafe_function: [ |
| 6 | + "env", |
| 7 | + "func", |
| 8 | + "async_resource", |
| 9 | + "async_resource_name", |
| 10 | + "max_queue_size", |
| 11 | + "initial_thread_count", |
| 12 | + "thread_finalize_data", |
| 13 | + "thread_finalize_cb", |
| 14 | + "context", |
| 15 | + "call_js_cb", |
| 16 | + "result", |
| 17 | + ], |
| 18 | + napi_add_async_cleanup_hook: ["env", "hook", "arg", "remove_handle"], |
| 19 | + napi_fatal_error: ["location", "location_len", "message", "message_len"], |
| 20 | +}; |
| 21 | + |
| 22 | +export function generateFunctionDecl(fn: FunctionDecl) { |
| 23 | + return generateFunction({ ...fn, static: true }); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Generates source code for a version script for the given Node API version. |
| 28 | + */ |
| 29 | +export function generateHeader(functions: FunctionDecl[]) { |
| 30 | + return ` |
| 31 | + #pragma once |
| 32 | +
|
| 33 | + #include <memory> |
| 34 | + #include <type_traits> |
| 35 | + #include <vector> |
| 36 | + #include <variant> |
| 37 | + |
| 38 | + #include <node_api.h> |
| 39 | + #include "weak_node_api.hpp" |
| 40 | +
|
| 41 | + struct WeakNodeApiMultiHost : WeakNodeApiHost { |
| 42 | + template <typename T> struct Wrapped { |
| 43 | + static_assert(std::is_same<T, napi_env>::value || |
| 44 | + std::is_same<T, napi_threadsafe_function>::value || |
| 45 | + std::is_same<T, napi_async_cleanup_hook_handle>::value, |
| 46 | + "T must be either napi_env, node_api_basic_env, napi_threadsafe_function or napi_async_cleanup_hook_handle"); |
| 47 | + T value; |
| 48 | + std::weak_ptr<WeakNodeApiHost> host; |
| 49 | + WeakNodeApiMultiHost *multi_host; |
| 50 | + }; |
| 51 | +
|
| 52 | + napi_env wrap(napi_env value, std::weak_ptr<WeakNodeApiHost>); |
| 53 | + napi_threadsafe_function wrap(napi_threadsafe_function value, std::weak_ptr<WeakNodeApiHost>); |
| 54 | + napi_async_cleanup_hook_handle wrap(napi_async_cleanup_hook_handle value, std::weak_ptr<WeakNodeApiHost>); |
| 55 | +
|
| 56 | + WeakNodeApiMultiHost( |
| 57 | + void napi_module_register(napi_module *), |
| 58 | + void napi_fatal_error(const char *, size_t, const char *, size_t) |
| 59 | + ); |
| 60 | +
|
| 61 | + private: |
| 62 | + std::vector<std::variant<std::unique_ptr<Wrapped<napi_env>>, std::unique_ptr<Wrapped<napi_threadsafe_function>>, std::unique_ptr<Wrapped<napi_async_cleanup_hook_handle>>>> wrapped_values; |
| 63 | +
|
| 64 | + public: |
| 65 | +
|
| 66 | + ${functions.map(generateFunctionDecl).join("\n")} |
| 67 | + }; |
| 68 | + `; |
| 69 | +} |
| 70 | + |
| 71 | +function generateFunctionImpl(fn: FunctionDecl) { |
| 72 | + const { name, argumentTypes, returnType } = fn; |
| 73 | + const [firstArgument] = argumentTypes; |
| 74 | + const argumentNames = |
| 75 | + ARGUMENT_NAMES_PR_FUNCTION[name] ?? |
| 76 | + argumentTypes.map((_, index) => `arg${index}`); |
| 77 | + if (name === "napi_fatal_error") { |
| 78 | + // Providing a default implementation |
| 79 | + return generateFunction({ |
| 80 | + ...fn, |
| 81 | + namespace: "WeakNodeApiMultiHost", |
| 82 | + argumentNames, |
| 83 | + body: ` |
| 84 | + if (location && location_len) { |
| 85 | + fprintf(stderr, "Fatal Node-API error: %.*s %.*s", |
| 86 | + static_cast<int>(location_len), |
| 87 | + location, |
| 88 | + static_cast<int>(message_len), |
| 89 | + message |
| 90 | + ); |
| 91 | + } else { |
| 92 | + fprintf(stderr, "Fatal Node-API error: %.*s", static_cast<int>(message_len), message); |
| 93 | + } |
| 94 | + abort(); |
| 95 | + `, |
| 96 | + }); |
| 97 | + } else if (name === "napi_module_register") { |
| 98 | + // Providing a default implementation |
| 99 | + return generateFunction({ |
| 100 | + ...fn, |
| 101 | + namespace: "WeakNodeApiMultiHost", |
| 102 | + argumentNames: [""], |
| 103 | + body: ` |
| 104 | + fprintf(stderr, "napi_module_register is not implemented for this WeakNodeApiMultiHost"); |
| 105 | + abort(); |
| 106 | + `, |
| 107 | + }); |
| 108 | + } else if ( |
| 109 | + [ |
| 110 | + "napi_env", |
| 111 | + "node_api_basic_env", |
| 112 | + // TODO: Wrap these on creation |
| 113 | + "napi_threadsafe_function", |
| 114 | + "napi_async_cleanup_hook_handle", |
| 115 | + ].includes(firstArgument) |
| 116 | + ) { |
| 117 | + const joinedArguments = argumentTypes |
| 118 | + .map((_, index) => |
| 119 | + index === 0 ? "wrapped->value" : argumentNames[index], |
| 120 | + ) |
| 121 | + .join(", "); |
| 122 | + |
| 123 | + function generateCall() { |
| 124 | + if (name === "napi_create_threadsafe_function") { |
| 125 | + return ` |
| 126 | + auto status = host->${name}(${joinedArguments}); |
| 127 | + if (status == napi_status::napi_ok) { |
| 128 | + *${argumentNames[10]} = wrapped->multi_host->wrap(*${argumentNames[10]}, wrapped->host); |
| 129 | + } |
| 130 | + return status; |
| 131 | + `; |
| 132 | + } else if (name === "napi_add_async_cleanup_hook") { |
| 133 | + return ` |
| 134 | + auto status = host->${name}(${joinedArguments}); |
| 135 | + if (status == napi_status::napi_ok) { |
| 136 | + *${argumentNames[3]} = wrapped->multi_host->wrap(*${argumentNames[3]}, wrapped->host); |
| 137 | + } |
| 138 | + return status; |
| 139 | + `; |
| 140 | + } else { |
| 141 | + return ` |
| 142 | + ${returnType === "void" ? "" : "return"} host->${name}(${joinedArguments}); |
| 143 | + `; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return generateFunction({ |
| 148 | + ...fn, |
| 149 | + namespace: "WeakNodeApiMultiHost", |
| 150 | + argumentNames, |
| 151 | + body: ` |
| 152 | + auto wrapped = reinterpret_cast<Wrapped<${firstArgument}>*>(${argumentNames[0]}); |
| 153 | + if (auto host = wrapped->host.lock()) { |
| 154 | + if (host->${name} == nullptr) { |
| 155 | + fprintf(stderr, "Node-API function '${name}' called on a host which doesn't provide an implementation\\n"); |
| 156 | + return napi_status::napi_generic_failure; |
| 157 | + } |
| 158 | + ${generateCall()} |
| 159 | + } else { |
| 160 | + fprintf(stderr, "Node-API function '${name}' called after host was destroyed.\\n"); |
| 161 | + return napi_status::napi_generic_failure; |
| 162 | + } |
| 163 | + `, |
| 164 | + }); |
| 165 | + } else { |
| 166 | + throw new Error(`Unexpected signature for '${name}' Node-API function`); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/** |
| 171 | + * Generates source code for a version script for the given Node API version. |
| 172 | + */ |
| 173 | +export function generateSource(functions: FunctionDecl[]) { |
| 174 | + return ` |
| 175 | + #include "weak_node_api_multi_host.hpp" |
| 176 | +
|
| 177 | + WeakNodeApiMultiHost::WeakNodeApiMultiHost( |
| 178 | + void napi_module_register(napi_module *), |
| 179 | + void napi_fatal_error(const char *, size_t, const char *, size_t) |
| 180 | + ) |
| 181 | + : WeakNodeApiHost({ |
| 182 | + ${functions |
| 183 | + .map(({ name }) => { |
| 184 | + if ( |
| 185 | + name === "napi_module_register" || |
| 186 | + name === "napi_fatal_error" |
| 187 | + ) { |
| 188 | + // We take functions not taking a wrap-able argument via the constructor and call them directly |
| 189 | + return `.${name} = ${name} == nullptr ? WeakNodeApiMultiHost::${name} : ${name},`; |
| 190 | + } else { |
| 191 | + return `.${name} = WeakNodeApiMultiHost::${name},`; |
| 192 | + } |
| 193 | + }) |
| 194 | + .join("\n")} |
| 195 | + }), wrapped_values{} {}; |
| 196 | + |
| 197 | + // TODO: Find a better way to delete these along the way |
| 198 | +
|
| 199 | + napi_env WeakNodeApiMultiHost::wrap(napi_env value, |
| 200 | + std::weak_ptr<WeakNodeApiHost> host) { |
| 201 | + auto ptr = std::make_unique<Wrapped<napi_env>>(value, host, this); |
| 202 | + auto raw_ptr = ptr.get(); |
| 203 | + wrapped_values.push_back(std::move(ptr)); |
| 204 | + return reinterpret_cast<napi_env>(raw_ptr); |
| 205 | + } |
| 206 | +
|
| 207 | + napi_threadsafe_function |
| 208 | + WeakNodeApiMultiHost::wrap(napi_threadsafe_function value, |
| 209 | + std::weak_ptr<WeakNodeApiHost> host) { |
| 210 | + auto ptr = std::make_unique<Wrapped<napi_threadsafe_function>>(value, host, this); |
| 211 | + auto raw_ptr = ptr.get(); |
| 212 | + wrapped_values.push_back(std::move(ptr)); |
| 213 | + return reinterpret_cast<napi_threadsafe_function>(raw_ptr); |
| 214 | + } |
| 215 | +
|
| 216 | + napi_async_cleanup_hook_handle |
| 217 | + WeakNodeApiMultiHost::wrap(napi_async_cleanup_hook_handle value, |
| 218 | + std::weak_ptr<WeakNodeApiHost> host) { |
| 219 | + auto ptr = std::make_unique<Wrapped<napi_async_cleanup_hook_handle>>(value, host, this); |
| 220 | + auto raw_ptr = ptr.get(); |
| 221 | + wrapped_values.push_back(std::move(ptr)); |
| 222 | + return reinterpret_cast<napi_async_cleanup_hook_handle>(raw_ptr); |
| 223 | + } |
| 224 | +
|
| 225 | + ${functions.map(generateFunctionImpl).join("\n")} |
| 226 | + `; |
| 227 | +} |
0 commit comments