Skip to content

Commit 9deb2e2

Browse files
committed
Add String/Symbol::New with string view unit tests
1 parent 5b7448e commit 9deb2e2

File tree

4 files changed

+21
-14
lines changed

4 files changed

+21
-14
lines changed

napi-inl.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
#if NAPI_HAS_THREADS
1919
#include <mutex>
2020
#endif // NAPI_HAS_THREADS
21+
#include <string_view>
2122
#include <type_traits>
2223
#include <utility>
23-
#if __cplusplus >= 201703L
24-
#include <string_view>
25-
#endif
2624

2725
#if defined(__clang__) || defined(__GNUC__)
2826
#define NAPI_NO_SANITIZE_VPTR __attribute__((no_sanitize("vptr")))
@@ -1258,11 +1256,9 @@ inline String String::New(napi_env env, const std::u16string& val) {
12581256
return String::New(env, val.c_str(), val.size());
12591257
}
12601258

1261-
#if __cplusplus >= 201703L
12621259
inline String String::New(napi_env env, std::string_view val) {
12631260
return String::New(env, val.data(), val.size());
12641261
}
1265-
#endif
12661262

12671263
inline String String::New(napi_env env, const char* val) {
12681264
// TODO(@gabrielschulhof) Remove if-statement when core's error handling is
@@ -1373,12 +1369,10 @@ inline Symbol Symbol::New(napi_env env, const std::string& description) {
13731369
return Symbol::New(env, descriptionValue);
13741370
}
13751371

1376-
#if __cplusplus >= 201703L
13771372
inline Symbol Symbol::New(napi_env env, std::string_view description) {
13781373
napi_value descriptionValue = String::New(env, description);
13791374
return Symbol::New(env, descriptionValue);
13801375
}
1381-
#endif
13821376

13831377
inline Symbol Symbol::New(napi_env env, String description) {
13841378
napi_value descriptionValue = description;

napi.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
#include <mutex>
1919
#endif // NAPI_HAS_THREADS
2020
#include <string>
21-
#include <vector>
22-
#if __cplusplus >= 201703L
2321
#include <string_view>
24-
#endif
22+
#include <vector>
2523

2624
// VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known
2725
// good version)
@@ -721,12 +719,10 @@ class String : public Name {
721719
const std::u16string& value ///< UTF-16 encoded C++ string
722720
);
723721

724-
#if __cplusplus >= 201703L
725722
/// Creates a new String value from a UTF-8 encoded C++ string view.
726723
static String New(napi_env env, ///< Node-API environment
727724
std::string_view value ///< UTF-8 encoded C++ string view
728725
);
729-
#endif
730726

731727
/// Creates a new String value from a UTF-8 encoded C string.
732728
static String New(
@@ -801,14 +797,12 @@ class Symbol : public Name {
801797
description ///< UTF-8 encoded C++ string describing the symbol
802798
);
803799

804-
#if __cplusplus >= 201703L
805800
/// Creates a new Symbol value with a description.
806801
static Symbol New(
807802
napi_env env, ///< Node-API environment
808803
std::string_view
809804
description ///< UTF-8 encoded C++ string view describing the symbol
810805
);
811-
#endif
812806

813807
/// Creates a new Symbol value with a description.
814808
static Symbol New(napi_env env, ///< Node-API environment

test/name.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "napi.h"
22

3+
#include <string_view>
4+
35
using namespace Napi;
46

57
const char* testValueUtf8 = "123456789";
@@ -43,6 +45,10 @@ Value CreateString(const CallbackInfo& info) {
4345
}
4446
}
4547

48+
Value CreateStringFromStringView(const CallbackInfo& info) {
49+
return String::New(info.Env(), std::string_view("hello1"));
50+
}
51+
4652
Value CheckString(const CallbackInfo& info) {
4753
String value = info[0].As<String>();
4854
String encoding = info[1].As<String>();
@@ -80,6 +86,10 @@ Value CreateSymbol(const CallbackInfo& info) {
8086
}
8187
}
8288

89+
Value CreateSymbolFromStringView(const CallbackInfo& info) {
90+
return Symbol::New(info.Env(), std::string_view("hello2"));
91+
}
92+
8393
Value CheckSymbol(const CallbackInfo& info) {
8494
return Boolean::New(info.Env(), info[0].Type() == napi_symbol);
8595
}
@@ -99,11 +109,15 @@ Object InitName(Env env) {
99109

100110
exports["echoString"] = Function::New(env, EchoString);
101111
exports["createString"] = Function::New(env, CreateString);
112+
exports["createStringFromStringView"] =
113+
Function::New(env, CreateStringFromStringView);
102114
exports["nullStringShouldThrow"] = Function::New(env, NullStringShouldThrow);
103115
exports["nullString16ShouldThrow"] =
104116
Function::New(env, NullString16ShouldThrow);
105117
exports["checkString"] = Function::New(env, CheckString);
106118
exports["createSymbol"] = Function::New(env, CreateSymbol);
119+
exports["createSymbolFromStringView"] =
120+
Function::New(env, CreateSymbolFromStringView);
107121
exports["checkSymbol"] = Function::New(env, CheckSymbol);
108122

109123
return exports;

test/name.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ function test (binding) {
5656
assert.strictEqual(binding.name.echoString(str, 'utf8'), str);
5757
assert.strictEqual(binding.name.echoString(str, 'utf16'), str);
5858
}
59+
60+
assert.strictEqual(binding.name.createStringFromStringView(), 'hello1');
61+
const symFromStringView = binding.name.createSymbolFromStringView();
62+
assert.strictEqual(typeof symFromStringView, 'symbol');
63+
assert.strictEqual(symFromStringView.description, 'hello2');
5964
}

0 commit comments

Comments
 (0)