Skip to content

Commit

Permalink
n-api: fix warning about size_t compare with int
Browse files Browse the repository at this point in the history
PR-URL: nodejs#15508
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Franziska Hinkelmann <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
Sampson Gao authored and Gabriel Schulhof committed Apr 16, 2018
1 parent ccc321d commit 3f8e900
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 50 deletions.
43 changes: 27 additions & 16 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,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 @@ -923,21 +930,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 @@ -3290,7 +3302,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
8 changes: 4 additions & 4 deletions test/addons-napi/test_async/test_async.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ napi_value Test(napi_env env, napi_callback_info info) {
NAPI_CALL(env,
napi_create_reference(env, argv[2], 1, &the_carrier._callback));

NAPI_CALL(env,
napi_create_string_utf8(env, "TestResource", -1, &resource_name));
NAPI_CALL(env, napi_create_string_utf8(
env, "TestResource", NAPI_AUTO_LENGTH, &resource_name));
NAPI_CALL(env, napi_create_async_work(env, argv[1], resource_name,
Execute, Complete, &the_carrier, &the_carrier._request));
NAPI_CALL(env,
Expand Down Expand Up @@ -145,8 +145,8 @@ napi_value TestCancel(napi_env env, napi_callback_info info) {
napi_value resource_name;
void* data;

NAPI_CALL(env,
napi_create_string_utf8(env, "TestResource", -1, &resource_name));
NAPI_CALL(env, napi_create_string_utf8(
env, "TestResource", NAPI_AUTO_LENGTH, &resource_name));

// make sure the work we are going to cancel will not be
// able to start by using all the threads in the pool
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_constructor/test_constructor.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ napi_value Init(napi_env env, napi_value exports) {
};

napi_value cons;
NAPI_CALL(env, napi_define_class(env, "MyObject", -1, New,
NAPI_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New,
NULL, sizeof(properties)/sizeof(*properties), properties, &cons));

NAPI_CALL(env, napi_create_reference(env, cons, 1, &constructor_));
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_conversions/test_conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ napi_value AsString(napi_env env, napi_callback_info info) {
napi_get_value_string_utf8(env, args[0], value, sizeof(value), NULL));

napi_value output;
NAPI_CALL(env, napi_create_string_utf8(env, value, -1, &output));
NAPI_CALL(env, napi_create_string_utf8(
env, value, NAPI_AUTO_LENGTH, &output));

return output;
}
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_env_sharing/compare_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ napi_value compare(napi_env env, napi_callback_info info) {
}

napi_value Init(napi_env env, napi_value exports) {
NAPI_CALL(env, napi_create_function(env, "exports", -1, compare, NULL, &exports));
NAPI_CALL(env, napi_create_function(
env, "exports", NAPI_AUTO_LENGTH, compare, NULL, &exports));
return exports;
}

Expand Down
28 changes: 18 additions & 10 deletions test/addons-napi/test_error/test_error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ napi_value checkError(napi_env env, napi_callback_info info) {
napi_value throwExistingError(napi_env env, napi_callback_info info) {
napi_value message;
napi_value error;
NAPI_CALL(env, napi_create_string_utf8(env, "existing error", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "existing error", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_error(env, nullptr, message, &error));
NAPI_CALL(env, napi_throw(env, error));
return nullptr;
Expand Down Expand Up @@ -62,23 +63,26 @@ napi_value throwTypeErrorCode(napi_env env, napi_callback_info info) {
napi_value createError(napi_env env, napi_callback_info info) {
napi_value result;
napi_value message;
NAPI_CALL(env, napi_create_string_utf8(env, "error", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "error", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_error(env, nullptr, message, &result));
return result;
}

napi_value createRangeError(napi_env env, napi_callback_info info) {
napi_value result;
napi_value message;
NAPI_CALL(env, napi_create_string_utf8(env, "range error", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "range error", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_range_error(env, nullptr, message, &result));
return result;
}

napi_value createTypeError(napi_env env, napi_callback_info info) {
napi_value result;
napi_value message;
NAPI_CALL(env, napi_create_string_utf8(env, "type error", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "type error", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_type_error(env, nullptr, message, &result));
return result;
}
Expand All @@ -87,8 +91,10 @@ napi_value createErrorCode(napi_env env, napi_callback_info info) {
napi_value result;
napi_value message;
napi_value code;
NAPI_CALL(env, napi_create_string_utf8(env, "Error [error]", -1, &message));
NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code));
NAPI_CALL(env, napi_create_string_utf8(
env, "Error [error]", NAPI_AUTO_LENGTH, &message));
NAPI_CALL(env, napi_create_string_utf8(
env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
NAPI_CALL(env, napi_create_error(env, code, message, &result));
return result;
}
Expand All @@ -99,9 +105,10 @@ napi_value createRangeErrorCode(napi_env env, napi_callback_info info) {
napi_value code;
NAPI_CALL(env, napi_create_string_utf8(env,
"RangeError [range error]",
-1,
NAPI_AUTO_LENGTH,
&message));
NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code));
NAPI_CALL(env, napi_create_string_utf8(
env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
NAPI_CALL(env, napi_create_range_error(env, code, message, &result));
return result;
}
Expand All @@ -112,9 +119,10 @@ napi_value createTypeErrorCode(napi_env env, napi_callback_info info) {
napi_value code;
NAPI_CALL(env, napi_create_string_utf8(env,
"TypeError [type error]",
-1,
NAPI_AUTO_LENGTH,
&message));
NAPI_CALL(env, napi_create_string_utf8(env, "ERR_TEST_CODE", -1, &code));
NAPI_CALL(env, napi_create_string_utf8(
env, "ERR_TEST_CODE", NAPI_AUTO_LENGTH, &code));
NAPI_CALL(env, napi_create_type_error(env, code, message, &result));
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_fatal/test_fatal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include "../common.h"

napi_value Test(napi_env env, napi_callback_info info) {
napi_fatal_error("test_fatal::Test", -1, "fatal message", -1);
napi_fatal_error("test_fatal::Test", NAPI_AUTO_LENGTH,
"fatal message", NAPI_AUTO_LENGTH);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions test/addons-napi/test_function/test_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ napi_value TestFunctionName(napi_env env, napi_callback_info info) {
napi_value Init(napi_env env, napi_value exports) {
napi_value fn1;
NAPI_CALL(env, napi_create_function(
env, NULL, -1, TestCallFunction, NULL, &fn1));
env, NULL, NAPI_AUTO_LENGTH, TestCallFunction, NULL, &fn1));

napi_value fn2;
NAPI_CALL(env, napi_create_function(
env, "Name", -1, TestFunctionName, NULL, &fn2));
env, "Name", NAPI_AUTO_LENGTH, TestFunctionName, NULL, &fn2));

napi_value fn3;
NAPI_CALL(env, napi_create_function(
Expand Down
26 changes: 17 additions & 9 deletions test/addons-napi/test_general/test_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ napi_value testGetNodeVersion(napi_env env, napi_callback_info info) {
NAPI_CALL(env, napi_create_uint32(env, node_version->patch, &patch));
NAPI_CALL(env, napi_create_string_utf8(env,
node_version->release,
(size_t)-1,
NAPI_AUTO_LENGTH,
&release));
NAPI_CALL(env, napi_create_array_with_length(env, 4, &result));
NAPI_CALL(env, napi_set_element(env, result, 0, major));
Expand Down Expand Up @@ -120,21 +120,29 @@ napi_value testNapiTypeof(napi_env env, napi_callback_info info) {

napi_value result = NULL;
if (argument_type == napi_number) {
NAPI_CALL(env, napi_create_string_utf8(env, "number", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "number", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_string) {
NAPI_CALL(env, napi_create_string_utf8(env, "string", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "string", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_function) {
NAPI_CALL(env, napi_create_string_utf8(env, "function", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "function", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_object) {
NAPI_CALL(env, napi_create_string_utf8(env, "object", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "object", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_boolean) {
NAPI_CALL(env, napi_create_string_utf8(env, "boolean", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "boolean", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_undefined) {
NAPI_CALL(env, napi_create_string_utf8(env, "undefined", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "undefined", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_symbol) {
NAPI_CALL(env, napi_create_string_utf8(env, "symbol", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "symbol", NAPI_AUTO_LENGTH, &result));
} else if (argument_type == napi_null) {
NAPI_CALL(env, napi_create_string_utf8(env, "null", -1, &result));
NAPI_CALL(env, napi_create_string_utf8(
env, "null", NAPI_AUTO_LENGTH, &result));
}
return result;
}
Expand Down
6 changes: 4 additions & 2 deletions test/addons-napi/test_make_callback/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
NAPI_CALL(env, napi_typeof(env, func, &func_type));

napi_value resource_name;
NAPI_CALL(env, napi_create_string_utf8(env, "test", -1, &resource_name));
NAPI_CALL(env, napi_create_string_utf8(
env, "test", NAPI_AUTO_LENGTH, &resource_name));

napi_async_context context;
NAPI_CALL(env, napi_async_init(env, func, resource_name, &context));
Expand All @@ -45,7 +46,8 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {

napi_value Init(napi_env env, napi_value exports) {
napi_value fn;
NAPI_CALL(env, napi_create_function(env, NULL, -1, MakeCallback, NULL, &fn));
NAPI_CALL(env, napi_create_function(
env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
return exports;
}
Expand Down
3 changes: 2 additions & 1 deletion test/addons-napi/test_make_callback_recurse/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {

napi_value Init(napi_env env, napi_value exports) {
napi_value fn;
NAPI_CALL(env, napi_create_function(env, NULL, -1, MakeCallback, NULL, &fn));
NAPI_CALL(env, napi_create_function(
env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
return exports;
}
Expand Down
4 changes: 2 additions & 2 deletions test/addons-napi/test_properties/test_properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ napi_value Init(napi_env env, napi_value exports) {
napi_value name_value;
NAPI_CALL(env, napi_create_string_utf8(env,
"NameKeyValue",
-1,
NAPI_AUTO_LENGTH,
&name_value));

napi_value symbol_description;
napi_value name_symbol;
NAPI_CALL(env, napi_create_string_utf8(env,
"NameKeySymbol",
-1,
NAPI_AUTO_LENGTH,
&symbol_description));
NAPI_CALL(env, napi_create_symbol(env,
symbol_description,
Expand Down

0 comments on commit 3f8e900

Please sign in to comment.