Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/weak-node-api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ set(GENERATED_SOURCE_DIR "generated")
target_sources(${PROJECT_NAME}
PUBLIC
${GENERATED_SOURCE_DIR}/weak_node_api.cpp
${GENERATED_SOURCE_DIR}/NodeApiMultiHost.cpp
PUBLIC FILE_SET HEADERS
BASE_DIRS ${GENERATED_SOURCE_DIR} ${INCLUDE_DIR} FILES
${GENERATED_SOURCE_DIR}/weak_node_api.hpp
${GENERATED_SOURCE_DIR}/NodeApiHost.hpp
${GENERATED_SOURCE_DIR}/NodeApiMultiHost.hpp
${INCLUDE_DIR}/js_native_api_types.h
${INCLUDE_DIR}/js_native_api.h
${INCLUDE_DIR}/node_api_types.h
Expand Down
21 changes: 21 additions & 0 deletions packages/weak-node-api/scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {

import * as weakNodeApiGenerator from "./generators/weak-node-api.js";
import * as hostGenerator from "./generators/NodeApiHost.js";
import * as multiHostGenerator from "./generators/NodeApiMultiHost.js";

export const OUTPUT_PATH = path.join(import.meta.dirname, "../generated");

Expand Down Expand Up @@ -87,6 +88,26 @@ async function run() {
Provides the implementation for deferring Node-API function calls from addons into a Node-API host.
`,
});
await generateFile({
functions,
fileName: "NodeApiMultiHost.hpp",
generator: multiHostGenerator.generateHeader,
headingComment: `
@brief Weak Node-API multi-host injection interface.
This header provides the struct for deferring Node-API function calls from addons into multiple Node-API host implementations.
`,
});
await generateFile({
functions,
fileName: "NodeApiMultiHost.cpp",
generator: multiHostGenerator.generateSource,
headingComment: `
@brief Weak Node-API multi-host injection implementation.
Provides the implementation for deferring Node-API function calls from addons into multiple Node-API host implementations.
`,
});
}

run().catch((err) => {
Expand Down
263 changes: 263 additions & 0 deletions packages/weak-node-api/scripts/generators/NodeApiMultiHost.ts
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 {

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 WrappedThreadsafeFunction and struct WrappedAsyncCleanupHookHandle with three arguments:

auto ptr = std::make_unique<WrappedAsyncCleanupHookHandle>(original, env, weak_host);
auto ptr = std::make_unique<WrappedThreadsafeFunction>(original, env, weak_host);

When compiling locally, the compiler throws an error. I'm wondering if these structs should explicitly define a constructor that accepts these three parameters?😊

/Applications/Xcode_15.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/include/c++/v1/__memory/unique_ptr.h:686:30: error: no matching constructor for initialization of 'NodeApiMultiHost::WrappedAsyncCleanupHookHandle'
  return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
                             ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
weak-node-api/generated/NodeApiMultiHost.cpp:240:12: note: in instantiation of function template specialization 'std::make_unique<NodeApiMultiHost::WrappedAsyncCleanupHookHandle, napi_async_cleanup_hook_handle__ *&, NodeApiMultiHost::WrappedEnv *&, std::weak_ptr<NodeApiHost> &>' requested here
      std::make_unique<WrappedAsyncCleanupHookHandle>(original, env, weak_host);

Copy link
Collaborator Author

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?

  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;
  };

Copy link
Collaborator Author

@kraenhansen kraenhansen Nov 19, 2025

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?

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?

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!😊

Copy link
Collaborator Author

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?

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")}
`;
}
1 change: 1 addition & 0 deletions packages/weak-node-api/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ FetchContent_MakeAvailable(Catch2)

add_executable(weak-node-api-tests
test_inject.cpp
test_multi_host.cpp
)
target_link_libraries(weak-node-api-tests
PRIVATE
Expand Down
39 changes: 39 additions & 0 deletions packages/weak-node-api/tests/test_inject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,42 @@ TEST_CASE("inject_weak_node_api_host") {
REQUIRE(called);
}
}

TEST_CASE("calling into host from functions") {
auto my_create_function = [](napi_env arg0, const char *arg1, size_t arg2,
napi_callback arg3, void *arg4,
napi_value *arg5) -> napi_status {
// This is a failing noop as we're not actually creating a JS functions
return napi_status::napi_generic_failure;
};
NodeApiHost host{.napi_create_function = my_create_function};
inject_weak_node_api_host(host);
napi_env raw_env{};

SECTION("via global function") {
napi_value result;
napi_callback cb = [](napi_env env, napi_callback_info info) -> napi_value {
napi_value obj;
napi_status status = napi_create_object(env, &obj);
return obj;
};

napi_create_function(raw_env, "foo", 3, cb, nullptr, &result);
}

SECTION("via callback info") {
napi_value result;
napi_callback cb = [](napi_env env, napi_callback_info info) -> napi_value {
// Get host via callback info
void *data;
napi_get_cb_info(env, info, nullptr, nullptr, nullptr, &data);
auto *host_ptr = static_cast<decltype(&host)>(data);

napi_value obj;
napi_status status = host_ptr->napi_create_object(env, &obj);
return obj;
};

napi_create_function(raw_env, "foo", 3, cb, &host, &result);
}
}
Loading
Loading