-
Notifications
You must be signed in to change notification settings - Fork 7
Enable multiple Node-API hosting JS engines / runtimes to share the global Node-API functions #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kraenhansen
wants to merge
2
commits into
main
Choose a base branch
from
kh/weak-node-api-multi-host
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+499
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
263 changes: 263 additions & 0 deletions
263
packages/weak-node-api/scripts/generators/NodeApiMultiHost.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| import type { FunctionDecl } from "../../src/node-api-functions.js"; | ||
| import { generateFunction } from "./shared.js"; | ||
|
|
||
| const ARGUMENT_NAMES_PR_FUNCTION: Record<string, undefined | string[]> = { | ||
| napi_create_threadsafe_function: [ | ||
| "env", | ||
| "func", | ||
| "async_resource", | ||
| "async_resource_name", | ||
| "max_queue_size", | ||
| "initial_thread_count", | ||
| "thread_finalize_data", | ||
| "thread_finalize_cb", | ||
| "context", | ||
| "call_js_cb", | ||
| "result", | ||
| ], | ||
| napi_add_async_cleanup_hook: ["env", "hook", "arg", "remove_handle"], | ||
| napi_fatal_error: ["location", "location_len", "message", "message_len"], | ||
| }; | ||
|
|
||
| export function generateFunctionDecl(fn: FunctionDecl) { | ||
| return generateFunction({ ...fn, static: true }); | ||
| } | ||
|
|
||
| export function generateHeader(functions: FunctionDecl[]) { | ||
| return ` | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include <node_api.h> | ||
|
|
||
| #include "NodeApiHost.hpp" | ||
|
|
||
| struct NodeApiMultiHost : public NodeApiHost { | ||
|
|
||
| struct WrappedEnv; | ||
|
|
||
| struct WrappedThreadsafeFunction { | ||
| napi_threadsafe_function value; | ||
| WrappedEnv *env; | ||
| std::weak_ptr<NodeApiHost> host; | ||
| }; | ||
|
|
||
| struct WrappedAsyncCleanupHookHandle { | ||
| napi_async_cleanup_hook_handle value; | ||
| WrappedEnv *env; | ||
| std::weak_ptr<NodeApiHost> host; | ||
| }; | ||
|
|
||
| struct WrappedEnv { | ||
| WrappedEnv(napi_env &&value, std::weak_ptr<NodeApiHost> &&host); | ||
|
|
||
| napi_env value; | ||
| std::weak_ptr<NodeApiHost> host; | ||
|
|
||
| private: | ||
| std::vector<std::unique_ptr<WrappedThreadsafeFunction>> | ||
| threadsafe_functions; | ||
| std::vector<std::unique_ptr<WrappedAsyncCleanupHookHandle>> | ||
| async_cleanup_hook_handles; | ||
|
|
||
| public: | ||
| napi_threadsafe_function wrap(napi_threadsafe_function original, | ||
| WrappedEnv *env, | ||
| std::weak_ptr<NodeApiHost>); | ||
| napi_async_cleanup_hook_handle wrap(napi_async_cleanup_hook_handle original, | ||
| WrappedEnv *env, | ||
| std::weak_ptr<NodeApiHost>); | ||
| }; | ||
|
|
||
| napi_env wrap(napi_env original, std::weak_ptr<NodeApiHost>); | ||
|
|
||
| NodeApiMultiHost( | ||
| void napi_module_register(napi_module *), | ||
| void napi_fatal_error(const char *, size_t, const char *, size_t) | ||
| ); | ||
|
|
||
| private: | ||
| std::vector<std::unique_ptr<WrappedEnv>> envs; | ||
|
|
||
| public: | ||
|
|
||
| ${functions.map(generateFunctionDecl).join("\n")} | ||
| }; | ||
| `; | ||
| } | ||
|
|
||
| function generateFunctionImpl(fn: FunctionDecl) { | ||
| const { name, argumentTypes, returnType } = fn; | ||
| const [firstArgument] = argumentTypes; | ||
| const argumentNames = | ||
| ARGUMENT_NAMES_PR_FUNCTION[name] ?? | ||
| argumentTypes.map((_, index) => `arg${index}`); | ||
| if (name === "napi_fatal_error") { | ||
| // Providing a default implementation | ||
| return generateFunction({ | ||
| ...fn, | ||
| namespace: "NodeApiMultiHost", | ||
| argumentNames, | ||
| body: ` | ||
| if (location && location_len) { | ||
| fprintf(stderr, "Fatal Node-API error: %.*s %.*s", | ||
| static_cast<int>(location_len), | ||
| location, | ||
| static_cast<int>(message_len), | ||
| message | ||
| ); | ||
| } else { | ||
| fprintf(stderr, "Fatal Node-API error: %.*s", static_cast<int>(message_len), message); | ||
| } | ||
| abort(); | ||
| `, | ||
| }); | ||
| } else if (name === "napi_module_register") { | ||
| // Providing a default implementation | ||
| return generateFunction({ | ||
| ...fn, | ||
| namespace: "NodeApiMultiHost", | ||
| argumentNames: [""], | ||
| body: ` | ||
| fprintf(stderr, "napi_module_register is not implemented for this NodeApiMultiHost"); | ||
| abort(); | ||
| `, | ||
| }); | ||
| } else if ( | ||
| [ | ||
| "napi_env", | ||
| "node_api_basic_env", | ||
| "napi_threadsafe_function", | ||
| "napi_async_cleanup_hook_handle", | ||
| ].includes(firstArgument) | ||
| ) { | ||
| const joinedArguments = argumentTypes | ||
| .map((_, index) => | ||
| index === 0 ? "wrapped->value" : argumentNames[index], | ||
| ) | ||
| .join(", "); | ||
|
|
||
| function generateCall() { | ||
| if (name === "napi_create_threadsafe_function") { | ||
| return ` | ||
| auto status = host->${name}(${joinedArguments}); | ||
| if (status == napi_status::napi_ok) { | ||
| *${argumentNames[10]} = wrapped->wrap(*${argumentNames[10]}, wrapped, wrapped->host); | ||
| } | ||
| return status; | ||
| `; | ||
| } else if (name === "napi_add_async_cleanup_hook") { | ||
| return ` | ||
| auto status = host->${name}(${joinedArguments}); | ||
| if (status == napi_status::napi_ok) { | ||
| *${argumentNames[3]} = wrapped->wrap(*${argumentNames[3]}, wrapped, wrapped->host); | ||
| } | ||
| return status; | ||
| `; | ||
| } else { | ||
| return ` | ||
| ${returnType === "void" ? "" : "return"} host->${name}(${joinedArguments}); | ||
| `; | ||
| } | ||
| } | ||
|
|
||
| function getWrappedType(nodeApiType: string) { | ||
| if (nodeApiType === "napi_env" || nodeApiType === "node_api_basic_env") { | ||
| return "WrappedEnv"; | ||
| } else if (nodeApiType === "napi_threadsafe_function") { | ||
| return "WrappedThreadsafeFunction"; | ||
| } else if (nodeApiType === "napi_async_cleanup_hook_handle") { | ||
| return "WrappedAsyncCleanupHookHandle"; | ||
| } else { | ||
| throw new Error(`Unexpected Node-API type '${nodeApiType}'`); | ||
| } | ||
| } | ||
|
|
||
| return generateFunction({ | ||
| ...fn, | ||
| namespace: "NodeApiMultiHost", | ||
| argumentNames, | ||
| body: ` | ||
| auto wrapped = reinterpret_cast<${getWrappedType(firstArgument)}*>(${argumentNames[0]}); | ||
| if (auto host = wrapped->host.lock()) { | ||
| if (host->${name} == nullptr) { | ||
| fprintf(stderr, "Node-API function '${name}' called on a host which doesn't provide an implementation\\n"); | ||
| return napi_status::napi_generic_failure; | ||
| } | ||
| ${generateCall()} | ||
| } else { | ||
| fprintf(stderr, "Node-API function '${name}' called after host was destroyed.\\n"); | ||
| return napi_status::napi_generic_failure; | ||
| } | ||
| `, | ||
| }); | ||
| } else { | ||
| throw new Error(`Unexpected signature for '${name}' Node-API function`); | ||
| } | ||
| } | ||
|
|
||
| export function generateSource(functions: FunctionDecl[]) { | ||
| return ` | ||
| #include "NodeApiMultiHost.hpp" | ||
|
|
||
| NodeApiMultiHost::NodeApiMultiHost( | ||
| void napi_module_register(napi_module *), | ||
| void napi_fatal_error(const char *, size_t, const char *, size_t) | ||
| ) | ||
| : NodeApiHost({ | ||
| ${functions | ||
| .map(({ name }) => { | ||
| if ( | ||
| name === "napi_module_register" || | ||
| name === "napi_fatal_error" | ||
| ) { | ||
| // We take functions not taking a wrap-able argument via the constructor and call them directly | ||
| return `.${name} = ${name} == nullptr ? NodeApiMultiHost::${name} : ${name},`; | ||
| } else { | ||
| return `.${name} = NodeApiMultiHost::${name},`; | ||
| } | ||
| }) | ||
| .join("\n")} | ||
| }), envs{} {}; | ||
|
|
||
| // TODO: Ensure the Node-API functions aren't throwing (using NOEXCEPT) | ||
| // TODO: Find a better way to delete these along the way | ||
|
|
||
| NodeApiMultiHost::WrappedEnv::WrappedEnv(napi_env &&value, | ||
| std::weak_ptr<NodeApiHost> &&host) | ||
| : value(value), host(host), threadsafe_functions{}, | ||
| async_cleanup_hook_handles{} {} | ||
|
|
||
| napi_env NodeApiMultiHost::wrap(napi_env value, | ||
| std::weak_ptr<NodeApiHost> host) { | ||
| auto ptr = std::make_unique<WrappedEnv>(std::move(value), std::move(host)); | ||
| auto raw_ptr = ptr.get(); | ||
| envs.push_back(std::move(ptr)); | ||
| return reinterpret_cast<napi_env>(raw_ptr); | ||
| } | ||
|
|
||
| napi_threadsafe_function | ||
| NodeApiMultiHost::WrappedEnv::wrap(napi_threadsafe_function original, | ||
| WrappedEnv *env, | ||
| std::weak_ptr<NodeApiHost> weak_host) { | ||
| auto ptr = std::make_unique<WrappedThreadsafeFunction>(original, env, weak_host); | ||
| auto raw_ptr = ptr.get(); | ||
| env->threadsafe_functions.push_back(std::move(ptr)); | ||
| return reinterpret_cast<napi_threadsafe_function>(raw_ptr); | ||
| } | ||
|
|
||
| napi_async_cleanup_hook_handle | ||
| NodeApiMultiHost::WrappedEnv::wrap(napi_async_cleanup_hook_handle original, | ||
| WrappedEnv *env, | ||
| std::weak_ptr<NodeApiHost> weak_host) { | ||
| auto ptr = std::make_unique<WrappedAsyncCleanupHookHandle>(original, env, weak_host); | ||
| auto raw_ptr = ptr.get(); | ||
| env->async_cleanup_hook_handles.push_back(std::move(ptr)); | ||
| return reinterpret_cast<napi_async_cleanup_hook_handle>(raw_ptr); | ||
| } | ||
|
|
||
| ${functions.map(generateFunctionImpl).join("\n")} | ||
| `; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that we're calling the constructors of
struct WrappedThreadsafeFunctionandstruct WrappedAsyncCleanupHookHandlewith three arguments:When compiling locally, the compiler throws an error. I'm wondering if these structs should explicitly define a constructor that accepts these three parameters?😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's supposed to use the default constructor of those structs 🤔 Do you have this in your
generated/NodeApiMultiHost.hpp?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might need to add explicit constructors? I don't know why it builds for me and on CI though 🤔
I've just rebased on latest main after #330 merged - pulling the latest might change something for you after a clean build?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see now. My local compiler is configured with a lower C++ standard that doesn't support this parenthesis initialization syntax. That makes sense now. Thanks!😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great 👍 Perhaps I could add something to the CMake config to either configure the language version or fail faster?