Skip to content

Commit e645c8d

Browse files
committed
update for latest n-api changes
PR-URL: nodejs/node-addon-api#144 Reviewed-By: Sampson Gao <[email protected]>
1 parent 42e8373 commit e645c8d

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

napi-inl.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ inline Function Function::New(napi_env env,
12041204

12051205
napi_value value;
12061206
napi_status status = napi_create_function(
1207-
env, utf8name, -1, CbData::Wrapper, callbackData, &value);
1207+
env, utf8name, NAPI_AUTO_LENGTH, CbData::Wrapper, callbackData, &value);
12081208
NAPI_THROW_IF_FAILED(env, status, Function());
12091209
return Function(env, value);
12101210
}
@@ -1476,7 +1476,7 @@ inline Error Error::New(napi_env env, const std::string& message) {
14761476
}
14771477

14781478
inline NAPI_NO_RETURN void Error::Fatal(const char* location, const char* message) {
1479-
napi_fatal_error(location, -1, message, -1);
1479+
napi_fatal_error(location, NAPI_AUTO_LENGTH, message, NAPI_AUTO_LENGTH);
14801480
}
14811481

14821482
inline Error::Error() : ObjectReference(), _message(nullptr) {
@@ -2312,7 +2312,8 @@ inline Function ObjectWrap<T>::DefineClass(
23122312
void* data) {
23132313
napi_value value;
23142314
napi_status status = napi_define_class(
2315-
env, utf8name, -1, T::ConstructorCallbackWrapper, data, properties.size(),
2315+
env, utf8name, NAPI_AUTO_LENGTH,
2316+
T::ConstructorCallbackWrapper, data, properties.size(),
23162317
reinterpret_cast<const napi_property_descriptor*>(properties.begin()), &value);
23172318
NAPI_THROW_IF_FAILED(env, status, Function());
23182319

@@ -2327,7 +2328,8 @@ inline Function ObjectWrap<T>::DefineClass(
23272328
void* data) {
23282329
napi_value value;
23292330
napi_status status = napi_define_class(
2330-
env, utf8name, -1, T::ConstructorCallbackWrapper, data, properties.size(),
2331+
env, utf8name, NAPI_AUTO_LENGTH,
2332+
T::ConstructorCallbackWrapper, data, properties.size(),
23312333
reinterpret_cast<const napi_property_descriptor*>(properties.data()), &value);
23322334
NAPI_THROW_IF_FAILED(env, status, Function());
23332335

@@ -2683,7 +2685,7 @@ inline AsyncWorker::AsyncWorker(const Object& receiver, const Function& callback
26832685

26842686
napi_value resource_id;
26852687
napi_status status = napi_create_string_latin1(
2686-
_env, "generic", -1, &resource_id);
2688+
_env, "generic", NAPI_AUTO_LENGTH, &resource_id);
26872689
NAPI_THROW_IF_FAILED(_env, status);
26882690

26892691
status = napi_create_async_work(

src/node_api.cc

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
*
99
******************************************************************************/
1010

11-
#include "node_internals.h"
1211
#include <node_buffer.h>
1312
#include <node_object_wrap.h>
1413
#include <string.h>
1514
#include <algorithm>
1615
#include <cmath>
1716
#include <vector>
1817
#include "node_api.h"
18+
#include "node_internals.h"
1919

2020
#define NAPI_VERSION 1
2121

@@ -117,16 +117,23 @@ struct napi_env__ {
117117
CHECK_TO_TYPE((env), Boolean, (context), (result), (src), \
118118
napi_boolean_expected)
119119

120+
// n-api defines NAPI_AUTO_LENGHTH as the indicator that a string
121+
// is null terminated. For V8 the equivalent is -1. The assert
122+
// validates that our cast of NAPI_AUTO_LENGTH results in -1 as
123+
// needed by V8.
120124
#define CHECK_NEW_FROM_UTF8_LEN(env, result, str, len) \
121125
do { \
126+
static_assert(static_cast<int>(NAPI_AUTO_LENGTH) == -1, \
127+
"Casting NAPI_AUTO_LENGTH to int must result in -1"); \
122128
auto str_maybe = v8::String::NewFromUtf8( \
123-
(env)->isolate, (str), v8::NewStringType::kInternalized, (len)); \
129+
(env)->isolate, (str), v8::NewStringType::kInternalized, \
130+
static_cast<int>(len)); \
124131
CHECK_MAYBE_EMPTY((env), str_maybe, napi_generic_failure); \
125132
(result) = str_maybe.ToLocalChecked(); \
126133
} while (0)
127134

128135
#define CHECK_NEW_FROM_UTF8(env, result, str) \
129-
CHECK_NEW_FROM_UTF8_LEN((env), (result), (str), -1)
136+
CHECK_NEW_FROM_UTF8_LEN((env), (result), (str), NAPI_AUTO_LENGTH)
130137

131138
#define GET_RETURN_STATUS(env) \
132139
(!try_catch.HasCaught() ? napi_ok \
@@ -918,21 +925,26 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location,
918925
size_t location_len,
919926
const char* message,
920927
size_t message_len) {
921-
char* location_string = const_cast<char*>(location);
922-
char* message_string = const_cast<char*>(message);
923-
if (location_len != -1) {
924-
location_string = reinterpret_cast<char*>(
925-
malloc(location_len * sizeof(char) + 1));
926-
strncpy(location_string, location, location_len);
927-
location_string[location_len] = '\0';
928+
std::string location_string;
929+
std::string message_string;
930+
931+
if (location_len != NAPI_AUTO_LENGTH) {
932+
location_string.assign(
933+
const_cast<char*>(location), location_len);
934+
} else {
935+
location_string.assign(
936+
const_cast<char*>(location), strlen(location));
928937
}
929-
if (message_len != -1) {
930-
message_string = reinterpret_cast<char*>(
931-
malloc(message_len * sizeof(char) + 1));
932-
strncpy(message_string, message, message_len);
933-
message_string[message_len] = '\0';
938+
939+
if (message_len != NAPI_AUTO_LENGTH) {
940+
message_string.assign(
941+
const_cast<char*>(message), message_len);
942+
} else {
943+
message_string.assign(
944+
const_cast<char*>(message), strlen(message));
934945
}
935-
node::FatalError(location_string, message_string);
946+
947+
node::FatalError(location_string.c_str(), message_string.c_str());
936948
}
937949

938950
napi_status napi_create_function(napi_env env,
@@ -3285,7 +3297,6 @@ napi_status napi_adjust_external_memory(napi_env env,
32853297
int64_t change_in_bytes,
32863298
int64_t* adjusted_value) {
32873299
CHECK_ENV(env);
3288-
CHECK_ARG(env, &change_in_bytes);
32893300
CHECK_ARG(env, adjusted_value);
32903301

32913302
*adjusted_value = env->isolate->AdjustAmountOfExternalAllocatedMemory(

src/node_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ typedef struct {
100100
#define NAPI_MODULE(modname, regfunc) \
101101
NAPI_MODULE_X(modname, regfunc, NULL, 0)
102102

103+
#define NAPI_AUTO_LENGTH SIZE_MAX
104+
103105
EXTERN_C_START
104106

105107
NAPI_EXTERN void napi_module_register(napi_module* mod);

0 commit comments

Comments
 (0)