Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 2 additions & 3 deletions doc/symbol.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.
Expand Down
6 changes: 0 additions & 6 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1416,12 +1416,6 @@ inline MaybeOrValue<Symbol> Symbol::WellKnown(napi_env env,
#endif
}

inline MaybeOrValue<Symbol> Symbol::For(napi_env env,
const std::string& description) {
napi_value descriptionValue = String::New(env, description);
return Symbol::For(env, descriptionValue);
}

inline MaybeOrValue<Symbol> Symbol::For(napi_env env,
std::string_view description) {
napi_value descriptionValue = String::New(env, description);
Expand Down
3 changes: 0 additions & 3 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -825,9 +825,6 @@ class Symbol : public Name {
/// Get a public Symbol (e.g. Symbol.iterator).
static MaybeOrValue<Symbol> WellKnown(napi_env, const std::string& name);

// Create a symbol in the global registry, UTF-8 Encoded cpp string
static MaybeOrValue<Symbol> For(napi_env env, const std::string& description);

// Create a symbol in the global registry, UTF-8 encoded cpp string view
static MaybeOrValue<Symbol> For(napi_env env, std::string_view description);

Expand Down
23 changes: 23 additions & 0 deletions test/symbol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Comment thread
umuoy1 marked this conversation as resolved.

} // namespace

Symbol CreateNewSymbolWithNoArgs(const Napi::CallbackInfo&) {
return Napi::Symbol();
}
Expand Down Expand Up @@ -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<String>().Utf8Value());
return MaybeUnwrap(Napi::Symbol::For(info.Env(), key));
}

Symbol FetchSymbolFromGlobalRegistryWithCKey(const Napi::CallbackInfo& info) {
String cppStringKey = info[0].As<String>();
return MaybeUnwrap(
Expand Down Expand Up @@ -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"] =
Expand Down
1 change: 1 addition & 0 deletions test/symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading