Skip to content

Commit

Permalink
fixup!
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Jun 14, 2024
1 parent 0a5713d commit 4cfeb29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
16 changes: 10 additions & 6 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "napi.h"

#include <algorithm>
#include <cstdarg>
#include <cstring>
#if NAPI_HAS_THREADS
#include <mutex>
Expand Down Expand Up @@ -340,12 +341,15 @@ struct AccessorCallbackData {
};

// Debugging-purpose C++-style variant of sprintf().
template <typename... Args>
inline std::string SPrintF(const char* format, Args&&... args) {
char buf[256];
int ret = snprintf(buf, 256, format, args...);
NAPI_CHECK(ret >= 0, "SPrintF", "Malformed format");
return buf;
inline std::string StringFormat(const char* format, ...) {
std::string result;
va_list args;
va_start(args, format);
int len = vsnprintf(nullptr, 0, format, args);
result.resize(len);
vsnprintf(&result[0], len + 1, format, args);
va_end(args);
return result;
}

} // namespace details
Expand Down
17 changes: 10 additions & 7 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,20 @@ static_assert(sizeof(char16_t) == sizeof(wchar_t),
#define NAPI_INTERNAL_CHECK(expr, location, ...) \
do { \
if (!(expr)) { \
std::string msg = Napi::details::SPrintF(__VA_ARGS__); \
Napi::Error::Fatal(location " " #expr, msg.c_str()); \
std::string msg = Napi::details::StringFormat(__VA_ARGS__); \
Napi::Error::Fatal(location, msg.c_str()); \
} \
} while (0)

#define NAPI_INTERNAL_CHECK_EQ(actual, expected, value_format, location) \
NAPI_INTERNAL_CHECK((actual) == (expected), \
location, \
"Expected " #actual " to be equal to " #expected \
", but got " value_format ".", \
actual)
do { \
auto actual_value = (actual); \
NAPI_INTERNAL_CHECK(actual_value == (expected), \
location, \
"Expected " #actual " to be equal to " #expected \
", but got " value_format ".", \
actual_value); \
} while (0)

#define NAPI_FATAL_IF_FAILED(status, location, message) \
NAPI_CHECK((status) == napi_ok, location, message)
Expand Down

0 comments on commit 4cfeb29

Please sign in to comment.