Skip to content

Implementation of napi_fatal_error and napi_get*_version functions #193

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

Merged
merged 3 commits into from
Jul 24, 2025

Conversation

paradowstack
Copy link
Contributor

This pull request introduces enhancements to the logging system and adds new Node-API functions.

Logging Enhancements:

  • Refactored the logging system to support multiple log levels (Debug, Warning, Error)

Node-API Functionality:

  • Added napi_fatal_error to log fatal errors and terminate the process
  • Introduced napi_get_node_version and napi_get_version functions to provide versioning information for Node.js and Node-API

Copy link

changeset-bot bot commented Jul 23, 2025

🦋 Changeset detected

Latest commit: f523909

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
react-native-node-api Patch
cmake-rn Patch
ferric-cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@paradowstack paradowstack changed the title Implementation of napi_fatal_error and napi_get*_version(s) functions Implementation of napi_fatal_error and napi_get*_version functions Jul 23, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements new Node-API functions and enhances the logging system to support multiple log levels. The changes add fatal error handling and version querying capabilities to the Node-API implementation, while refactoring the logging infrastructure to differentiate between debug, warning, and error messages.

  • Added napi_fatal_error, napi_get_node_version, and napi_get_version Node-API functions
  • Refactored logging system to support Debug, Warning, and Error log levels with unified internal implementation
  • Created version constants header to centralize Node.js and Node-API version definitions

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
generate-weak-node-api-injector.ts Added new Node-API functions to the implemented runtime functions list
Versions.hpp New header defining Node.js version constants and NAPI_VERSION
RuntimeNodeApi.hpp Added function declarations for fatal error and version query functions
RuntimeNodeApi.cpp Implemented the new Node-API functions with proper error handling
Logger.hpp Extended logger interface with warning and error logging functions
Logger.cpp Refactored logging implementation to support multiple log levels with unified internal logic

@paradowstack paradowstack force-pushed the kp/general-runtime-functions branch from e6cd4ae to eb36b61 Compare July 23, 2025 10:17
@paradowstack paradowstack added Apple 🍎 Anything related to the Apple platform (iOS, macOS, Cocoapods, Xcode, XCFrameworks, etc.) Android 🤖 Anything related to the Android platform (Gradle, NDK, Android SDK) labels Jul 23, 2025
@paradowstack paradowstack force-pushed the kp/general-runtime-functions branch from eb36b61 to 966b23a Compare July 23, 2025 10:23
@paradowstack paradowstack marked this pull request as ready for review July 23, 2025 10:23
Copy link
Collaborator

@kraenhansen kraenhansen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comments that could come as follow-ups.

size_t location_len,
const char* message,
size_t message_len) {
log_error("Fatal error in Node-API: %.*s: %.*s",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps put the message first and the location in a parenthesis afterwards? Just to get to the meat of the issue first.

Suggested change
log_error("Fatal error in Node-API: %.*s: %.*s",
log_error("Fatal error in Node-API: %.*s (in %.*s)",

@paradowstack paradowstack force-pushed the kp/general-runtime-functions branch from 0d644c7 to fc3a35b Compare July 24, 2025 10:50
@paradowstack paradowstack merged commit a477b84 into main Jul 24, 2025
6 checks passed
@paradowstack paradowstack deleted the kp/general-runtime-functions branch July 24, 2025 14:50
Comment on lines +45 to +61
void log_message_internal(LogLevel level, const char* format, va_list args) {
#if defined(__ANDROID__)
__android_log_vprint(androidLogLevel(level), LOG_TAG, format, args);
#elif defined(__APPLE__)
// iOS or macOS
fprintf(stderr, "[NodeApiHost] ");
const auto level_str = levelToString(level);
fprintf(stderr, LineFormat, level_str.data());
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
#else
// Fallback for other platforms
fprintf(stdout, "[NodeApiHost] ");
const auto level_str = levelToString(level);
fprintf(stdout, LineFormat, level_str.data());
vfprintf(stdout, format, args);
fprintf(stdout, "\n");
#endif
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logs to the native side - totally a good starting point, but would it make sense in future to log to the JS console so that we see it in the Metro/Chrome logs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, it makes sense, but is the appearance of native logs in the JS console something common? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to say because Node.js is the main precedent here and I imagine these logs show up in its logs.

Mainly, if the Node-API module logs a fatal error, it feels like it might be good to be able to see it without having to attach Xcode/LLDB/Android Studio to the process.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shirakaba I agree it would be great to make these more discoverable, although I expect them to be rare. I'm a bit concerned though, that it might not be possible because of the semantics of the fatal error / exception functions, which cannot return and must abort the process. There won't be an app to display the error nor a DevTools connection to transport it once the program leaves the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we put a line to the JS console just before we abort the process, so it is the last visible line and gives the developer information what went wrong?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit unsure on the semantics which need to be upheld by this function. If we call into other Node-API functions, to propagate the log message to JS, we might potentially (an edge-case for sure) get into an endless recursion. And hinted above, even if we called into console.log as an example, it won't make it into the DevTools client because the network transporting it from the app is likely not blocking and the app will die before it gets to send the message.

That's my hunch - we could definitely experiment with it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android 🤖 Anything related to the Android platform (Gradle, NDK, Android SDK) Apple 🍎 Anything related to the Apple platform (iOS, macOS, Cocoapods, Xcode, XCFrameworks, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants