Skip to content

Commit

Permalink
update for latest n-api changes
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node-addon-api#144
Reviewed-By: Sampson Gao <sampsong@ca.ibm.com>
  • Loading branch information
Marlyfleitas committed Sep 26, 2017
1 parent f839e2f commit 0113458
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
12 changes: 7 additions & 5 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ inline Function Function::New(napi_env env,

napi_value value;
napi_status status = napi_create_function(
env, utf8name, -1, CbData::Wrapper, callbackData, &value);
env, utf8name, NAPI_AUTO_LENGTH, CbData::Wrapper, callbackData, &value);
NAPI_THROW_IF_FAILED(env, status, Function());
return Function(env, value);
}
Expand Down Expand Up @@ -1476,7 +1476,7 @@ inline Error Error::New(napi_env env, const std::string& message) {
}

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

inline Error::Error() : ObjectReference(), _message(nullptr) {
Expand Down Expand Up @@ -2312,7 +2312,8 @@ inline Function ObjectWrap<T>::DefineClass(
void* data) {
napi_value value;
napi_status status = napi_define_class(
env, utf8name, -1, T::ConstructorCallbackWrapper, data, properties.size(),
env, utf8name, NAPI_AUTO_LENGTH,
T::ConstructorCallbackWrapper, data, properties.size(),
reinterpret_cast<const napi_property_descriptor*>(properties.begin()), &value);
NAPI_THROW_IF_FAILED(env, status, Function());

Expand All @@ -2327,7 +2328,8 @@ inline Function ObjectWrap<T>::DefineClass(
void* data) {
napi_value value;
napi_status status = napi_define_class(
env, utf8name, -1, T::ConstructorCallbackWrapper, data, properties.size(),
env, utf8name, NAPI_AUTO_LENGTH,
T::ConstructorCallbackWrapper, data, properties.size(),
reinterpret_cast<const napi_property_descriptor*>(properties.data()), &value);
NAPI_THROW_IF_FAILED(env, status, Function());

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

napi_value resource_id;
napi_status status = napi_create_string_latin1(
_env, "generic", -1, &resource_id);
_env, "generic", NAPI_AUTO_LENGTH, &resource_id);
NAPI_THROW_IF_FAILED(_env, status);

status = napi_create_async_work(
Expand Down
45 changes: 28 additions & 17 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
*
******************************************************************************/

#include "node_internals.h"
#include <node_buffer.h>
#include <node_object_wrap.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include "node_api.h"
#include "node_internals.h"

#define NAPI_VERSION 1

Expand Down Expand Up @@ -117,16 +117,23 @@ struct napi_env__ {
CHECK_TO_TYPE((env), Boolean, (context), (result), (src), \
napi_boolean_expected)

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

#define CHECK_NEW_FROM_UTF8(env, result, str) \
CHECK_NEW_FROM_UTF8_LEN((env), (result), (str), -1)
CHECK_NEW_FROM_UTF8_LEN((env), (result), (str), NAPI_AUTO_LENGTH)

#define GET_RETURN_STATUS(env) \
(!try_catch.HasCaught() ? napi_ok \
Expand Down Expand Up @@ -918,21 +925,26 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
size_t message_len) {
char* location_string = const_cast<char*>(location);
char* message_string = const_cast<char*>(message);
if (location_len != -1) {
location_string = reinterpret_cast<char*>(
malloc(location_len * sizeof(char) + 1));
strncpy(location_string, location, location_len);
location_string[location_len] = '\0';
std::string location_string;
std::string message_string;

if (location_len != NAPI_AUTO_LENGTH) {
location_string.assign(
const_cast<char*>(location), location_len);
} else {
location_string.assign(
const_cast<char*>(location), strlen(location));
}
if (message_len != -1) {
message_string = reinterpret_cast<char*>(
malloc(message_len * sizeof(char) + 1));
strncpy(message_string, message, message_len);
message_string[message_len] = '\0';

if (message_len != NAPI_AUTO_LENGTH) {
message_string.assign(
const_cast<char*>(message), message_len);
} else {
message_string.assign(
const_cast<char*>(message), strlen(message));
}
node::FatalError(location_string, message_string);

node::FatalError(location_string.c_str(), message_string.c_str());
}

napi_status napi_create_function(napi_env env,
Expand Down Expand Up @@ -3285,7 +3297,6 @@ napi_status napi_adjust_external_memory(napi_env env,
int64_t change_in_bytes,
int64_t* adjusted_value) {
CHECK_ENV(env);
CHECK_ARG(env, &change_in_bytes);
CHECK_ARG(env, adjusted_value);

*adjusted_value = env->isolate->AdjustAmountOfExternalAllocatedMemory(
Expand Down
2 changes: 2 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ typedef struct {
#define NAPI_MODULE(modname, regfunc) \
NAPI_MODULE_X(modname, regfunc, NULL, 0)

#define NAPI_AUTO_LENGTH SIZE_MAX

EXTERN_C_START

NAPI_EXTERN void napi_module_register(napi_module* mod);
Expand Down

0 comments on commit 0113458

Please sign in to comment.