Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#endif // NAPI_HAS_THREADS
#include <type_traits>
#include <utility>
#if __cplusplus >= 201703L
#include <string_view>
#endif

#if defined(__clang__) || defined(__GNUC__)
#define NAPI_NO_SANITIZE_VPTR __attribute__((no_sanitize("vptr")))
Expand Down Expand Up @@ -1255,6 +1258,12 @@ inline String String::New(napi_env env, const std::u16string& val) {
return String::New(env, val.c_str(), val.size());
}

#if __cplusplus >= 201703L
inline String String::New(napi_env env, std::string_view val) {
return String::New(env, val.data(), val.size());
}
#endif

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.
Expand Down Expand Up @@ -1364,6 +1373,13 @@ inline Symbol Symbol::New(napi_env env, const std::string& description) {
return Symbol::New(env, descriptionValue);
}

#if __cplusplus >= 201703L
inline Symbol Symbol::New(napi_env env, std::string_view description) {
napi_value descriptionValue = String::New(env, description);
return Symbol::New(env, descriptionValue);
}
#endif

inline Symbol Symbol::New(napi_env env, String description) {
napi_value descriptionValue = description;
return Symbol::New(env, descriptionValue);
Expand Down
19 changes: 19 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#endif // NAPI_HAS_THREADS
#include <string>
#include <vector>
#if __cplusplus >= 201703L
#include <string_view>
#endif

// VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known
// good version)
Expand Down Expand Up @@ -718,6 +721,13 @@ class String : public Name {
const std::u16string& value ///< UTF-16 encoded C++ string
);

#if __cplusplus >= 201703L
/// 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
);
#endif

/// Creates a new String value from a UTF-8 encoded C string.
static String New(
napi_env env, ///< Node-API environment
Expand Down Expand Up @@ -791,6 +801,15 @@ class Symbol : public Name {
description ///< UTF-8 encoded C++ string describing the symbol
);

#if __cplusplus >= 201703L
/// 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
);
#endif

/// Creates a new Symbol value with a description.
static Symbol New(napi_env env, ///< Node-API environment
String description ///< String value describing the symbol
Expand Down
18 changes: 18 additions & 0 deletions test/name.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "napi.h"

#if __cplusplus >= 201703L
#include <string_view>
#endif

using namespace Napi;

const char* testValueUtf8 = "123456789";
Expand Down Expand Up @@ -43,6 +47,12 @@ Value CreateString(const CallbackInfo& info) {
}
}

#if __cplusplus >= 201703L
Value CreateStringFromStringView(const CallbackInfo& info) {
return String::New(info.Env(), std::string_view("hello1"));
}
#endif

Value CheckString(const CallbackInfo& info) {
String value = info[0].As<String>();
String encoding = info[1].As<String>();
Expand Down Expand Up @@ -80,6 +90,12 @@ Value CreateSymbol(const CallbackInfo& info) {
}
}

#if __cplusplus >= 201703L
Value CreateSymbolFromStringView(const CallbackInfo& info) {
return Symbol::New(info.Env(), std::string_view("hello2"));
}
#endif

Value CheckSymbol(const CallbackInfo& info) {
return Boolean::New(info.Env(), info[0].Type() == napi_symbol);
}
Expand All @@ -99,11 +115,13 @@ 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;
Expand Down
3 changes: 3 additions & 0 deletions test/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ 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")
assert.strictEqual(binding.name.createSymbolFromStringView(), "hello2")
}