-
Notifications
You must be signed in to change notification settings - Fork 7
Add test to weak-node-api
#321
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
Changes from all commits
58a351c
9c5f8c1
ebb7b52
93478d3
6c08e57
fd171e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,6 @@ | |
|
|
||
| # Copied from node-api-headers by scripts/copy-node-api-headers.ts | ||
| /include/ | ||
|
|
||
| # Clang cache | ||
| /.cache/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,13 +18,24 @@ export function generateHeader(functions: FunctionDecl[]) { | |
| "#include <node_api.h>", // Node-API | ||
| "#include <stdio.h>", // fprintf() | ||
| "#include <stdlib.h>", // abort() | ||
| "", | ||
| // Ideally we would have just used NAPI_NO_RETURN, but | ||
| // __declspec(noreturn) (when building with Microsoft Visual C++) cannot be used on members of a struct | ||
| // TODO: If we targeted C++23 we could use std::unreachable() | ||
| "#if defined(__GNUC__)", | ||
| "#define WEAK_NODE_API_UNREACHABLE __builtin_unreachable();", | ||
| "#else", | ||
| "#define WEAK_NODE_API_UNREACHABLE __assume(0);", | ||
| "#endif", | ||
| "", | ||
| // Generate the struct of function pointers | ||
| "struct WeakNodeApiHost {", | ||
| ...functions.map( | ||
| ({ returnType, noReturn, name, argumentTypes }) => | ||
| `${returnType} ${ | ||
| noReturn ? " __attribute__((noreturn))" : "" | ||
| }(*${name})(${argumentTypes.join(", ")});`, | ||
| ...functions.map(({ returnType, name, argumentTypes }) => | ||
| [ | ||
| returnType, | ||
| // Signature | ||
| `(*${name})(${argumentTypes.join(", ")});`, | ||
| ].join(" "), | ||
| ), | ||
| "};", | ||
| "typedef void(*InjectHostFunction)(const WeakNodeApiHost&);", | ||
|
|
@@ -46,25 +57,26 @@ export function generateSource(functions: FunctionDecl[]) { | |
| "};", | ||
| ``, | ||
| // Generate function calling into the host | ||
| ...functions.flatMap(({ returnType, noReturn, name, argumentTypes }) => { | ||
| ...functions.flatMap(({ returnType, name, argumentTypes, noReturn }) => { | ||
| return [ | ||
| `extern "C" ${returnType} ${ | ||
| noReturn ? " __attribute__((noreturn))" : "" | ||
| }${name}(${argumentTypes | ||
| .map((type, index) => `${type} arg${index}`) | ||
| .join(", ")}) {`, | ||
| 'extern "C"', | ||
| returnType, | ||
| 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(", ") + | ||
| ");", | ||
| returnType === "void" ? "" : "return ", | ||
| `g_host.${name}`, | ||
| "(", | ||
| argumentTypes.map((_, index) => `arg${index}`).join(", "), | ||
| ");", | ||
| noReturn ? "WEAK_NODE_API_UNREACHABLE" : "", | ||
| "};", | ||
|
Comment on lines
+72
to
78
|
||
| ]; | ||
| ].join(" "); | ||
| }), | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| Include(FetchContent) | ||
|
|
||
| FetchContent_Declare( | ||
| Catch2 | ||
| GIT_REPOSITORY https://github.com/catchorg/Catch2.git | ||
| GIT_TAG v3.11.0 | ||
| ) | ||
|
|
||
| FetchContent_MakeAvailable(Catch2) | ||
|
|
||
| add_executable(weak-node-api-tests | ||
| test_inject.cpp | ||
| ) | ||
| target_link_libraries(weak-node-api-tests | ||
| PRIVATE | ||
| weak-node-api | ||
| Catch2::Catch2WithMain | ||
| ) | ||
|
|
||
| target_compile_features(weak-node-api-tests PRIVATE cxx_std_20) | ||
| target_compile_definitions(weak-node-api-tests PRIVATE NAPI_VERSION=8) | ||
|
|
||
| # As per https://github.com/catchorg/Catch2/blob/devel/docs/cmake-integration.md#catchcmake-and-catchaddtestscmake | ||
| list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) | ||
| include(CTest) | ||
| include(Catch) | ||
| catch_discover_tests(weak-node-api-tests) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #include <catch2/catch_test_macros.hpp> | ||
| #include <weak_node_api.hpp> | ||
|
|
||
| TEST_CASE("inject_weak_node_api_host") { | ||
| SECTION("is callable") { | ||
| WeakNodeApiHost host{}; | ||
| inject_weak_node_api_host(host); | ||
| } | ||
|
|
||
| SECTION("propagates calls to napi_create_object") { | ||
| static bool called = false; | ||
| auto my_create_object = [](napi_env env, | ||
| napi_value *result) -> napi_status { | ||
| called = true; | ||
| return napi_status::napi_ok; | ||
| }; | ||
| WeakNodeApiHost host{.napi_create_object = my_create_object}; | ||
| inject_weak_node_api_host(host); | ||
|
|
||
| napi_value result; | ||
| napi_create_object({}, &result); | ||
|
|
||
| REQUIRE(called); | ||
| } | ||
| } |
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.
Corrected spelling of 'jod' to 'jodium'.