diff --git a/doc/symbol.md b/doc/symbol.md index 4fa1ae0da..1269cd7a1 100644 --- a/doc/symbol.md +++ b/doc/symbol.md @@ -49,7 +49,6 @@ 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); @@ -59,8 +58,8 @@ 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: - - `const std::string&` - UTF8 string description. - - `std::string_view` - represents a UTF8 string view. + - `std::string_view` - represents a UTF-8 string view. `std::string` values + are implicitly convertible to `std::string_view`. - `const char*` - represents a UTF8 string description. - `String` - Node addon API String description. - `napi_value` - Node-API `napi_value` description. diff --git a/napi-inl.h b/napi-inl.h index ec63ffeee..04dde9959 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -1416,12 +1416,6 @@ inline MaybeOrValue Symbol::WellKnown(napi_env env, #endif } -inline MaybeOrValue Symbol::For(napi_env env, - const std::string& description) { - napi_value descriptionValue = String::New(env, description); - return Symbol::For(env, descriptionValue); -} - inline MaybeOrValue Symbol::For(napi_env env, std::string_view description) { napi_value descriptionValue = String::New(env, description); diff --git a/napi.h b/napi.h index 870a5c290..75d11fb12 100644 --- a/napi.h +++ b/napi.h @@ -825,9 +825,6 @@ class Symbol : public Name { /// Get a public Symbol (e.g. Symbol.iterator). static MaybeOrValue WellKnown(napi_env, const std::string& 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); diff --git a/test/symbol.cc b/test/symbol.cc index d978739ff..856da4611 100644 --- a/test/symbol.cc +++ b/test/symbol.cc @@ -5,6 +5,21 @@ #include "test_helper.h" using namespace Napi; +namespace { + +class StringLike { + public: + explicit StringLike(const std::string& value) : _value(value) {} + + operator std::string() const { return _value; } + operator std::string_view() const { return _value; } + + private: + std::string _value; +}; + +} // namespace + Symbol CreateNewSymbolWithNoArgs(const Napi::CallbackInfo&) { return Napi::Symbol(); } @@ -47,6 +62,12 @@ Symbol FetchSymbolFromGlobalRegistryWithStringViewKey( return MaybeUnwrap(Napi::Symbol::For(info.Env(), std::string_view(key))); } +Symbol FetchSymbolFromGlobalRegistryWithStringLikeKey( + const Napi::CallbackInfo& info) { + StringLike key(info[0].As().Utf8Value()); + return MaybeUnwrap(Napi::Symbol::For(info.Env(), key)); +} + Symbol FetchSymbolFromGlobalRegistryWithCKey(const Napi::CallbackInfo& info) { String cppStringKey = info[0].As(); return MaybeUnwrap( @@ -83,6 +104,8 @@ Object InitSymbol(Env env) { Function::New(env, FetchSymbolFromGlobalRegistryWithCppKey); exports["getSymbolFromGlobalRegistryWithStringViewKey"] = Function::New(env, FetchSymbolFromGlobalRegistryWithStringViewKey); + exports["getSymbolFromGlobalRegistryWithStringLikeKey"] = + Function::New(env, FetchSymbolFromGlobalRegistryWithStringLikeKey); exports["testUndefinedSymbolCanBeCreated"] = Function::New(env, TestUndefinedSymbolsCanBeCreated); exports["testNullSymbolCanBeCreated"] = diff --git a/test/symbol.js b/test/symbol.js index baf39c81b..4994698fd 100644 --- a/test/symbol.js +++ b/test/symbol.js @@ -55,6 +55,7 @@ function test (binding) { assertCanCreateOrFetchGlobalSymbols('data', binding.symbol.getSymbolFromGlobalRegistry); assertCanCreateOrFetchGlobalSymbols('CppKey', binding.symbol.getSymbolFromGlobalRegistryWithCppKey); assertCanCreateOrFetchGlobalSymbols('StringViewKey', binding.symbol.getSymbolFromGlobalRegistryWithStringViewKey); + assertCanCreateOrFetchGlobalSymbols('StringLikeKey', binding.symbol.getSymbolFromGlobalRegistryWithStringLikeKey); assertCanCreateOrFetchGlobalSymbols('CKey', binding.symbol.getSymbolFromGlobalRegistryWithCKey); assert(binding.symbol.createNewSymbolWithNoArgs() === undefined);