Skip to content

Commit a477b84

Browse files
authored
Implementation of napi_fatal_error and napi_get*_version functions (#193)
* feat: Implement logging functions and add Node API version retrieval * chore: Improve error logging format in napi_fatal_error function * refactor: bumped NAPI_VERSION + removed node
1 parent b26c883 commit a477b84

File tree

7 files changed

+122
-11
lines changed

7 files changed

+122
-11
lines changed

.changeset/kind-forks-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-native-node-api": patch
3+
---
4+
5+
Added implementation of napi_fatal_error, napi_get_node_version and napi_get_version. Improved the Logger functionalities

packages/host/cpp/Logger.cpp

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,77 @@
99
#include <TargetConditionals.h>
1010
#endif
1111

12-
namespace callstack::nodeapihost {
13-
void log_debug(const char *format, ...) {
14-
// TODO: Disable logging in release builds
12+
namespace {
13+
constexpr auto LineFormat = "[%s] [NodeApiHost] ";
1514

16-
va_list args;
17-
va_start(args, format);
15+
enum class LogLevel { Debug, Warning, Error };
16+
17+
constexpr std::string_view levelToString(LogLevel level) {
18+
switch (level) {
19+
case LogLevel::Debug:
20+
return "DEBUG";
21+
case LogLevel::Warning:
22+
return "WARNING";
23+
case LogLevel::Error:
24+
return "ERROR";
25+
default:
26+
return "UNKNOWN";
27+
}
28+
}
1829

1930
#if defined(__ANDROID__)
20-
__android_log_vprint(ANDROID_LOG_DEBUG, LOG_TAG, format, args);
31+
constexpr int androidLogLevel(LogLevel level) {
32+
switch (level) {
33+
case LogLevel::Debug:
34+
return ANDROID_LOG_DEBUG;
35+
case LogLevel::Warning:
36+
return ANDROID_LOG_WARN;
37+
case LogLevel::Error:
38+
return ANDROID_LOG_ERROR;
39+
default:
40+
return ANDROID_LOG_UNKNOWN;
41+
}
42+
}
43+
#endif
44+
45+
void log_message_internal(LogLevel level, const char* format, va_list args) {
46+
#if defined(__ANDROID__)
47+
__android_log_vprint(androidLogLevel(level), LOG_TAG, format, args);
2148
#elif defined(__APPLE__)
2249
// iOS or macOS
23-
fprintf(stderr, "[NodeApiHost] ");
50+
const auto level_str = levelToString(level);
51+
fprintf(stderr, LineFormat, level_str.data());
2452
vfprintf(stderr, format, args);
2553
fprintf(stderr, "\n");
2654
#else
2755
// Fallback for other platforms
28-
fprintf(stdout, "[NodeApiHost] ");
56+
const auto level_str = levelToString(level);
57+
fprintf(stdout, LineFormat, level_str.data());
2958
vfprintf(stdout, format, args);
3059
fprintf(stdout, "\n");
3160
#endif
61+
}
62+
} // anonymous namespace
63+
64+
namespace callstack::nodeapihost {
3265

66+
void log_debug(const char* format, ...) {
67+
// TODO: Disable logging in release builds
68+
va_list args;
69+
va_start(args, format);
70+
log_message_internal(LogLevel::Debug, format, args);
71+
va_end(args);
72+
}
73+
void log_warning(const char* format, ...) {
74+
va_list args;
75+
va_start(args, format);
76+
log_message_internal(LogLevel::Warning, format, args);
77+
va_end(args);
78+
}
79+
void log_error(const char* format, ...) {
80+
va_list args;
81+
va_start(args, format);
82+
log_message_internal(LogLevel::Error, format, args);
3383
va_end(args);
3484
}
35-
} // namespace callstack::nodeapihost
85+
} // namespace callstack::nodeapihost

packages/host/cpp/Logger.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
#include <string>
44

55
namespace callstack::nodeapihost {
6-
void log_debug(const char *format, ...);
7-
}
6+
void log_debug(const char* format, ...);
7+
void log_warning(const char* format, ...);
8+
void log_error(const char* format, ...);
9+
} // namespace callstack::nodeapihost

packages/host/cpp/RuntimeNodeApi.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "RuntimeNodeApi.hpp"
22
#include <string>
3+
#include "Logger.hpp"
4+
#include "Versions.hpp"
35

46
auto ArrayType = napi_uint8_array;
57

@@ -121,4 +123,40 @@ napi_status napi_create_external_buffer(napi_env env,
121123
return napi_create_typedarray(env, ArrayType, length, buffer, 0, result);
122124
}
123125

126+
void napi_fatal_error(const char* location,
127+
size_t location_len,
128+
const char* message,
129+
size_t message_len) {
130+
if (location && location_len) {
131+
log_error("Fatal Node-API error: %.*s %.*s",
132+
static_cast<int>(location_len),
133+
location,
134+
static_cast<int>(message_len),
135+
message);
136+
} else {
137+
log_error(
138+
"Fatal Node-API error: %.*s", static_cast<int>(message_len), message);
139+
}
140+
abort();
141+
}
142+
143+
napi_status napi_get_node_version(
144+
node_api_basic_env env, const napi_node_version** result) {
145+
if (!result) {
146+
return napi_invalid_arg;
147+
}
148+
149+
*result = nullptr;
150+
return napi_generic_failure;
151+
}
152+
153+
napi_status napi_get_version(node_api_basic_env env, uint32_t* result) {
154+
if (!result) {
155+
return napi_invalid_arg;
156+
}
157+
158+
*result = NAPI_VERSION;
159+
return napi_ok;
160+
}
161+
124162
} // namespace callstack::nodeapihost

packages/host/cpp/RuntimeNodeApi.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@ napi_status napi_create_external_buffer(napi_env env,
2323
node_api_basic_finalize basic_finalize_cb,
2424
void* finalize_hint,
2525
napi_value* result);
26+
27+
void __attribute__((noreturn)) napi_fatal_error(const char* location,
28+
size_t location_len,
29+
const char* message,
30+
size_t message_len);
31+
32+
napi_status napi_get_node_version(
33+
node_api_basic_env env, const napi_node_version** result);
34+
35+
napi_status napi_get_version(node_api_basic_env env, uint32_t* result);
2636
} // namespace callstack::nodeapihost

packages/host/cpp/Versions.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#define NAPI_VERSION 8

packages/host/scripts/generate-weak-node-api-injector.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const IMPLEMENTED_RUNTIME_FUNCTIONS = [
1717
"napi_queue_async_work",
1818
"napi_delete_async_work",
1919
"napi_cancel_async_work",
20+
"napi_fatal_error",
21+
"napi_get_node_version",
22+
"napi_get_version",
2023
];
2124

2225
/**

0 commit comments

Comments
 (0)