diff --git a/napi-inl.h b/napi-inl.h index 0f1717ecd..99c110a80 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -18,6 +18,7 @@ #if NAPI_HAS_THREADS #include #endif // NAPI_HAS_THREADS +#include #include #include @@ -1255,6 +1256,10 @@ inline String String::New(napi_env env, const std::u16string& val) { return String::New(env, val.c_str(), val.size()); } +inline String String::New(napi_env env, std::string_view val) { + return String::New(env, val.data(), val.size()); +} + inline String String::New(napi_env env, const char* val) { // TODO(@gabrielschulhof) Remove if-statement when core's error handling is // available in all supported versions. @@ -1364,6 +1369,11 @@ inline Symbol Symbol::New(napi_env env, const std::string& description) { return Symbol::New(env, descriptionValue); } +inline Symbol Symbol::New(napi_env env, std::string_view description) { + napi_value descriptionValue = String::New(env, description); + return Symbol::New(env, descriptionValue); +} + inline Symbol Symbol::New(napi_env env, String description) { napi_value descriptionValue = description; return Symbol::New(env, descriptionValue); diff --git a/napi.h b/napi.h index 013a9114d..7a7363082 100644 --- a/napi.h +++ b/napi.h @@ -18,6 +18,7 @@ #include #endif // NAPI_HAS_THREADS #include +#include #include // VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known @@ -718,6 +719,11 @@ class String : public Name { const std::u16string& value ///< UTF-16 encoded C++ string ); + /// Creates a new String value from a UTF-8 encoded C++ string view. + static String New(napi_env env, ///< Node-API environment + std::string_view value ///< UTF-8 encoded C++ string view + ); + /// Creates a new String value from a UTF-8 encoded C string. static String New( napi_env env, ///< Node-API environment @@ -791,6 +797,13 @@ class Symbol : public Name { description ///< UTF-8 encoded C++ string describing the symbol ); + /// Creates a new Symbol value with a description. + static Symbol New( + napi_env env, ///< Node-API environment + std::string_view + description ///< UTF-8 encoded C++ string view describing the symbol + ); + /// Creates a new Symbol value with a description. static Symbol New(napi_env env, ///< Node-API environment String description ///< String value describing the symbol diff --git a/test/name.cc b/test/name.cc index 27bab9312..d94a3937f 100644 --- a/test/name.cc +++ b/test/name.cc @@ -1,5 +1,7 @@ #include "napi.h" +#include + using namespace Napi; const char* testValueUtf8 = "123456789"; @@ -43,6 +45,10 @@ Value CreateString(const CallbackInfo& info) { } } +Value CreateStringFromStringView(const CallbackInfo& info) { + return String::New(info.Env(), std::string_view("hello1")); +} + Value CheckString(const CallbackInfo& info) { String value = info[0].As(); String encoding = info[1].As(); @@ -80,6 +86,10 @@ Value CreateSymbol(const CallbackInfo& info) { } } +Value CreateSymbolFromStringView(const CallbackInfo& info) { + return Symbol::New(info.Env(), std::string_view("hello2")); +} + Value CheckSymbol(const CallbackInfo& info) { return Boolean::New(info.Env(), info[0].Type() == napi_symbol); } @@ -99,11 +109,15 @@ Object InitName(Env env) { exports["echoString"] = Function::New(env, EchoString); exports["createString"] = Function::New(env, CreateString); + exports["createStringFromStringView"] = + Function::New(env, CreateStringFromStringView); exports["nullStringShouldThrow"] = Function::New(env, NullStringShouldThrow); exports["nullString16ShouldThrow"] = Function::New(env, NullString16ShouldThrow); exports["checkString"] = Function::New(env, CheckString); exports["createSymbol"] = Function::New(env, CreateSymbol); + exports["createSymbolFromStringView"] = + Function::New(env, CreateSymbolFromStringView); exports["checkSymbol"] = Function::New(env, CheckSymbol); return exports; diff --git a/test/name.js b/test/name.js index 406c533e3..8113565c8 100644 --- a/test/name.js +++ b/test/name.js @@ -56,4 +56,9 @@ function test (binding) { assert.strictEqual(binding.name.echoString(str, 'utf8'), str); assert.strictEqual(binding.name.echoString(str, 'utf16'), str); } + + assert.strictEqual(binding.name.createStringFromStringView(), 'hello1'); + const symFromStringView = binding.name.createSymbolFromStringView(); + assert.strictEqual(typeof symFromStringView, 'symbol'); + assert.strictEqual(symFromStringView.description, 'hello2'); }