|
9 | 9 | ////////////////////////////////////////////////////////////////////////////////
|
10 | 10 |
|
11 | 11 | // Note: Do not include this file directly! Include "napi.h" instead.
|
| 12 | +// This should be a no-op and is intended for better IDE integration. |
| 13 | +#include "napi.h" |
12 | 14 |
|
13 | 15 | #include <algorithm>
|
| 16 | +#include <cstdarg> |
14 | 17 | #include <cstring>
|
15 | 18 | #if NAPI_HAS_THREADS
|
16 | 19 | #include <mutex>
|
@@ -358,6 +361,18 @@ struct AccessorCallbackData {
|
358 | 361 | void* data;
|
359 | 362 | };
|
360 | 363 |
|
| 364 | +// Debugging-purpose C++-style variant of sprintf(). |
| 365 | +inline std::string StringFormat(const char* format, ...) { |
| 366 | + std::string result; |
| 367 | + va_list args; |
| 368 | + va_start(args, format); |
| 369 | + int len = vsnprintf(nullptr, 0, format, args); |
| 370 | + result.resize(len); |
| 371 | + vsnprintf(&result[0], len + 1, format, args); |
| 372 | + va_end(args); |
| 373 | + return result; |
| 374 | +} |
| 375 | + |
361 | 376 | } // namespace details
|
362 | 377 |
|
363 | 378 | #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
|
@@ -826,8 +841,7 @@ inline void Boolean::CheckCast(napi_env env, napi_value value) {
|
826 | 841 | napi_valuetype type;
|
827 | 842 | napi_status status = napi_typeof(env, value, &type);
|
828 | 843 | NAPI_CHECK(status == napi_ok, "Boolean::CheckCast", "napi_typeof failed");
|
829 |
| - NAPI_CHECK( |
830 |
| - type == napi_boolean, "Boolean::CheckCast", "value is not napi_boolean"); |
| 844 | + NAPI_INTERNAL_CHECK_EQ(type, napi_boolean, "%d", "Boolean::CheckCast"); |
831 | 845 | }
|
832 | 846 |
|
833 | 847 | inline Boolean::Boolean() : Napi::Value() {}
|
@@ -863,8 +877,7 @@ inline void Number::CheckCast(napi_env env, napi_value value) {
|
863 | 877 | napi_valuetype type;
|
864 | 878 | napi_status status = napi_typeof(env, value, &type);
|
865 | 879 | NAPI_CHECK(status == napi_ok, "Number::CheckCast", "napi_typeof failed");
|
866 |
| - NAPI_CHECK( |
867 |
| - type == napi_number, "Number::CheckCast", "value is not napi_number"); |
| 880 | + NAPI_INTERNAL_CHECK_EQ(type, napi_number, "%d", "Number::CheckCast"); |
868 | 881 | }
|
869 | 882 |
|
870 | 883 | inline Number::Number() : Value() {}
|
@@ -959,8 +972,7 @@ inline void BigInt::CheckCast(napi_env env, napi_value value) {
|
959 | 972 | napi_valuetype type;
|
960 | 973 | napi_status status = napi_typeof(env, value, &type);
|
961 | 974 | NAPI_CHECK(status == napi_ok, "BigInt::CheckCast", "napi_typeof failed");
|
962 |
| - NAPI_CHECK( |
963 |
| - type == napi_bigint, "BigInt::CheckCast", "value is not napi_bigint"); |
| 975 | + NAPI_INTERNAL_CHECK_EQ(type, napi_bigint, "%d", "BigInt::CheckCast"); |
964 | 976 | }
|
965 | 977 |
|
966 | 978 | inline BigInt::BigInt() : Value() {}
|
@@ -1046,9 +1058,10 @@ inline void Name::CheckCast(napi_env env, napi_value value) {
|
1046 | 1058 | napi_valuetype type;
|
1047 | 1059 | napi_status status = napi_typeof(env, value, &type);
|
1048 | 1060 | NAPI_CHECK(status == napi_ok, "Name::CheckCast", "napi_typeof failed");
|
1049 |
| - NAPI_CHECK(type == napi_string || type == napi_symbol, |
1050 |
| - "Name::CheckCast", |
1051 |
| - "value is not napi_string or napi_symbol"); |
| 1061 | + NAPI_INTERNAL_CHECK(type == napi_string || type == napi_symbol, |
| 1062 | + "Name::CheckCast", |
| 1063 | + "value is not napi_string or napi_symbol, got %d.", |
| 1064 | + type); |
1052 | 1065 | }
|
1053 | 1066 |
|
1054 | 1067 | inline Name::Name() : Value() {}
|
@@ -1115,8 +1128,7 @@ inline void String::CheckCast(napi_env env, napi_value value) {
|
1115 | 1128 | napi_valuetype type;
|
1116 | 1129 | napi_status status = napi_typeof(env, value, &type);
|
1117 | 1130 | NAPI_CHECK(status == napi_ok, "String::CheckCast", "napi_typeof failed");
|
1118 |
| - NAPI_CHECK( |
1119 |
| - type == napi_string, "String::CheckCast", "value is not napi_string"); |
| 1131 | + NAPI_INTERNAL_CHECK_EQ(type, napi_string, "%d", "String::CheckCast"); |
1120 | 1132 | }
|
1121 | 1133 |
|
1122 | 1134 | inline String::String() : Name() {}
|
@@ -1252,8 +1264,7 @@ inline void Symbol::CheckCast(napi_env env, napi_value value) {
|
1252 | 1264 | napi_valuetype type;
|
1253 | 1265 | napi_status status = napi_typeof(env, value, &type);
|
1254 | 1266 | NAPI_CHECK(status == napi_ok, "Symbol::CheckCast", "napi_typeof failed");
|
1255 |
| - NAPI_CHECK( |
1256 |
| - type == napi_symbol, "Symbol::CheckCast", "value is not napi_symbol"); |
| 1267 | + NAPI_INTERNAL_CHECK_EQ(type, napi_symbol, "%d", "Symbol::CheckCast"); |
1257 | 1268 | }
|
1258 | 1269 |
|
1259 | 1270 | inline Symbol::Symbol() : Name() {}
|
@@ -1424,8 +1435,7 @@ inline void Object::CheckCast(napi_env env, napi_value value) {
|
1424 | 1435 | napi_valuetype type;
|
1425 | 1436 | napi_status status = napi_typeof(env, value, &type);
|
1426 | 1437 | NAPI_CHECK(status == napi_ok, "Object::CheckCast", "napi_typeof failed");
|
1427 |
| - NAPI_CHECK( |
1428 |
| - type == napi_object, "Object::CheckCast", "value is not napi_object"); |
| 1438 | + NAPI_INTERNAL_CHECK_EQ(type, napi_object, "%d", "Object::CheckCast"); |
1429 | 1439 | }
|
1430 | 1440 |
|
1431 | 1441 | inline Object::Object() : TypeTaggable() {}
|
@@ -1837,9 +1847,7 @@ inline void External<T>::CheckCast(napi_env env, napi_value value) {
|
1837 | 1847 | napi_valuetype type;
|
1838 | 1848 | napi_status status = napi_typeof(env, value, &type);
|
1839 | 1849 | NAPI_CHECK(status == napi_ok, "External::CheckCast", "napi_typeof failed");
|
1840 |
| - NAPI_CHECK(type == napi_external, |
1841 |
| - "External::CheckCast", |
1842 |
| - "value is not napi_external"); |
| 1850 | + NAPI_INTERNAL_CHECK_EQ(type, napi_external, "%d", "External::CheckCast"); |
1843 | 1851 | }
|
1844 | 1852 |
|
1845 | 1853 | template <typename T>
|
@@ -2295,12 +2303,13 @@ inline void TypedArrayOf<T>::CheckCast(napi_env env, napi_value value) {
|
2295 | 2303 | "TypedArrayOf::CheckCast",
|
2296 | 2304 | "napi_is_typedarray failed");
|
2297 | 2305 |
|
2298 |
| - NAPI_CHECK( |
| 2306 | + NAPI_INTERNAL_CHECK( |
2299 | 2307 | (type == TypedArrayTypeForPrimitiveType<T>() ||
|
2300 | 2308 | (type == napi_uint8_clamped_array && std::is_same<T, uint8_t>::value)),
|
2301 | 2309 | "TypedArrayOf::CheckCast",
|
2302 |
| - "Array type must match the template parameter. (Uint8 arrays may " |
2303 |
| - "optionally have the \"clamped\" array type.)"); |
| 2310 | + "Array type must match the template parameter, (Uint8 arrays may " |
| 2311 | + "optionally have the \"clamped\" array type.), got %d.", |
| 2312 | + type); |
2304 | 2313 | }
|
2305 | 2314 |
|
2306 | 2315 | template <typename T>
|
@@ -2481,9 +2490,7 @@ inline void Function::CheckCast(napi_env env, napi_value value) {
|
2481 | 2490 | napi_valuetype type;
|
2482 | 2491 | napi_status status = napi_typeof(env, value, &type);
|
2483 | 2492 | NAPI_CHECK(status == napi_ok, "Function::CheckCast", "napi_typeof failed");
|
2484 |
| - NAPI_CHECK(type == napi_function, |
2485 |
| - "Function::CheckCast", |
2486 |
| - "value is not napi_function"); |
| 2493 | + NAPI_INTERNAL_CHECK_EQ(type, napi_function, "%d", "Function::CheckCast"); |
2487 | 2494 | }
|
2488 | 2495 |
|
2489 | 2496 | inline Function::Function() : Object() {}
|
|
0 commit comments