diff --git a/doc/symbol.md b/doc/symbol.md index d94a618ec..cde46130d 100644 --- a/doc/symbol.md +++ b/doc/symbol.md @@ -26,7 +26,7 @@ Napi::Symbol::New(napi_env env, napi_value description); - `[in] env`: The `napi_env` environment in which to construct the `Napi::Symbol` object. - `[in] value`: The C++ primitive which represents the description hint for the `Napi::Symbol`. `description` may be any of: - - `std::string&` - UTF8 string description. + - `const std::string&` - UTF8 string description. - `const char*` - represents a UTF8 string description. - `std::string_view` - represents a UTF8 string view. - `String` - Node addon API String description. @@ -50,6 +50,7 @@ Returns a `Napi::Symbol` representing a well-known `Symbol` from the ### For ```cpp static Napi::Symbol Napi::Symbol::For(napi_env env, const std::string& description); +static Napi::Symbol Napi::Symbol::For(napi_env env, std::string_view description); static Napi::Symbol Napi::Symbol::For(napi_env env, const char* description); static Napi::Symbol Napi::Symbol::For(napi_env env, String description); static Napi::Symbol Napi::Symbol::For(napi_env env, napi_value description); @@ -57,6 +58,12 @@ static Napi::Symbol Napi::Symbol::For(napi_env env, napi_value description); - `[in] env`: The `napi_env` environment in which to construct the `Napi::Symbol` object. - `[in] description`: The C++ string representing the `Napi::Symbol` in the global registry to retrieve. + `description` may be any of: + - `std::string&` - UTF8 string description. + - `std::string_view` - represents a UTF8 string view. + - `const char*` - represents a UTF8 string description. + - `String` - Node addon API String description. + - `napi_value` - Node-API `napi_value` description. Searches in the global registry for existing symbol with the given name. If the symbol already exist it will be returned, otherwise a new symbol will be created in the registry. It's equivalent to Symbol.for() called from JavaScript. diff --git a/napi-inl.h b/napi-inl.h index 7824562a3..cc2fad6f7 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -1422,6 +1422,12 @@ inline MaybeOrValue Symbol::For(napi_env env, return Symbol::For(env, descriptionValue); } +inline MaybeOrValue Symbol::For(napi_env env, + std::string_view description) { + napi_value descriptionValue = String::New(env, description); + return Symbol::For(env, descriptionValue); +} + inline MaybeOrValue Symbol::For(napi_env env, const char* description) { napi_value descriptionValue = String::New(env, description); return Symbol::For(env, descriptionValue); diff --git a/napi.h b/napi.h index ba5c5576e..eaae1e711 100644 --- a/napi.h +++ b/napi.h @@ -828,6 +828,9 @@ class Symbol : public Name { // Create a symbol in the global registry, UTF-8 Encoded cpp string static MaybeOrValue For(napi_env env, const std::string& description); + // Create a symbol in the global registry, UTF-8 encoded cpp string view + static MaybeOrValue For(napi_env env, std::string_view description); + // Create a symbol in the global registry, C style string (null terminated) static MaybeOrValue For(napi_env env, const char* description); diff --git a/test/symbol.cc b/test/symbol.cc index 08ea80393..d978739ff 100644 --- a/test/symbol.cc +++ b/test/symbol.cc @@ -1,4 +1,7 @@ #include + +#include + #include "test_helper.h" using namespace Napi; @@ -37,6 +40,13 @@ Symbol FetchSymbolFromGlobalRegistryWithCppKey(const Napi::CallbackInfo& info) { return MaybeUnwrap(Napi::Symbol::For(info.Env(), cppStringKey.Utf8Value())); } +Symbol FetchSymbolFromGlobalRegistryWithStringViewKey( + const Napi::CallbackInfo& info) { + String cppStringKey = info[0].As(); + std::string key = cppStringKey.Utf8Value(); + return MaybeUnwrap(Napi::Symbol::For(info.Env(), std::string_view(key))); +} + Symbol FetchSymbolFromGlobalRegistryWithCKey(const Napi::CallbackInfo& info) { String cppStringKey = info[0].As(); return MaybeUnwrap( @@ -71,6 +81,8 @@ Object InitSymbol(Env env) { Function::New(env, FetchSymbolFromGlobalRegistryWithCKey); exports["getSymbolFromGlobalRegistryWithCppKey"] = Function::New(env, FetchSymbolFromGlobalRegistryWithCppKey); + exports["getSymbolFromGlobalRegistryWithStringViewKey"] = + Function::New(env, FetchSymbolFromGlobalRegistryWithStringViewKey); exports["testUndefinedSymbolCanBeCreated"] = Function::New(env, TestUndefinedSymbolsCanBeCreated); exports["testNullSymbolCanBeCreated"] = diff --git a/test/symbol.js b/test/symbol.js index bd2e3c83e..baf39c81b 100644 --- a/test/symbol.js +++ b/test/symbol.js @@ -54,6 +54,7 @@ function test (binding) { assertCanCreateOrFetchGlobalSymbols('data', binding.symbol.getSymbolFromGlobalRegistry); assertCanCreateOrFetchGlobalSymbols('CppKey', binding.symbol.getSymbolFromGlobalRegistryWithCppKey); + assertCanCreateOrFetchGlobalSymbols('StringViewKey', binding.symbol.getSymbolFromGlobalRegistryWithStringViewKey); assertCanCreateOrFetchGlobalSymbols('CKey', binding.symbol.getSymbolFromGlobalRegistryWithCKey); assert(binding.symbol.createNewSymbolWithNoArgs() === undefined);