|
8 | 8 | * |
9 | 9 | ******************************************************************************/ |
10 | 10 |
|
11 | | -#include "node_internals.h" |
12 | 11 | #include <node_buffer.h> |
13 | 12 | #include <node_object_wrap.h> |
14 | 13 | #include <string.h> |
15 | 14 | #include <algorithm> |
16 | 15 | #include <cmath> |
17 | 16 | #include <vector> |
18 | 17 | #include "node_api.h" |
| 18 | +#include "node_internals.h" |
19 | 19 |
|
20 | 20 | #define NAPI_VERSION 1 |
21 | 21 |
|
@@ -117,16 +117,23 @@ struct napi_env__ { |
117 | 117 | CHECK_TO_TYPE((env), Boolean, (context), (result), (src), \ |
118 | 118 | napi_boolean_expected) |
119 | 119 |
|
| 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. |
120 | 124 | #define CHECK_NEW_FROM_UTF8_LEN(env, result, str, len) \ |
121 | 125 | do { \ |
| 126 | + static_assert(static_cast<int>(NAPI_AUTO_LENGTH) == -1, \ |
| 127 | + "Casting NAPI_AUTO_LENGTH to int must result in -1"); \ |
122 | 128 | 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)); \ |
124 | 131 | CHECK_MAYBE_EMPTY((env), str_maybe, napi_generic_failure); \ |
125 | 132 | (result) = str_maybe.ToLocalChecked(); \ |
126 | 133 | } while (0) |
127 | 134 |
|
128 | 135 | #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) |
130 | 137 |
|
131 | 138 | #define GET_RETURN_STATUS(env) \ |
132 | 139 | (!try_catch.HasCaught() ? napi_ok \ |
@@ -918,21 +925,26 @@ NAPI_NO_RETURN void napi_fatal_error(const char* location, |
918 | 925 | size_t location_len, |
919 | 926 | const char* message, |
920 | 927 | 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)); |
928 | 937 | } |
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)); |
934 | 945 | } |
935 | | - node::FatalError(location_string, message_string); |
| 946 | + |
| 947 | + node::FatalError(location_string.c_str(), message_string.c_str()); |
936 | 948 | } |
937 | 949 |
|
938 | 950 | napi_status napi_create_function(napi_env env, |
@@ -3285,7 +3297,6 @@ napi_status napi_adjust_external_memory(napi_env env, |
3285 | 3297 | int64_t change_in_bytes, |
3286 | 3298 | int64_t* adjusted_value) { |
3287 | 3299 | CHECK_ENV(env); |
3288 | | - CHECK_ARG(env, &change_in_bytes); |
3289 | 3300 | CHECK_ARG(env, adjusted_value); |
3290 | 3301 |
|
3291 | 3302 | *adjusted_value = env->isolate->AdjustAmountOfExternalAllocatedMemory( |
|
0 commit comments