Skip to content

Commit

Permalink
Update PR based on comments received
Browse files Browse the repository at this point in the history
  • Loading branch information
JckXia committed Apr 28, 2021
1 parent f49c894 commit 0366264
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
13 changes: 8 additions & 5 deletions doc/symbol.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ If an error occurs, a `Napi::Error` will get thrown. If C++ exceptions are not
being used, callers should check the result of `Napi::Env::IsExceptionPending` before
attempting to use the returned value.
### Wellkown
### WellKnown
```cpp
static Napi::Symbol Napi::Symbol::WellKnown(napi_env env, const std::string& name);
```
Expand All @@ -47,12 +47,15 @@ Returns a `Napi::Symbol` representing a well-known `Symbol` from the

### For
```cpp
static Napi::Symbol Napi::Symbol::WellKnown(napi_env env, const std::string& name);
static Napi::Symbol Napi::Symbol::For(napi_env env, const std::string& description);
static Napi::Symbol Napi::Symbol::For(napi_env env, const char* description = nullptr);
static Napi::Symbol Napi::Symbol::For(napi_env env, String description);
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] name`: The C++ string representing the `Napi::Symbol` to retrieve.
- `[in] description`: The C++ string representing the `Napi::Symbol` in the global registry to retrieve.
Register `Napi::Symbol` in the global registry. If symbol already exist retrieve said symbol. Equivalent to `Symbol.for("symb")` called from JS.
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.
[`Napi::Name`]: ./name.md
[`Napi::Name`]: ./name.md
3 changes: 1 addition & 2 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,7 @@ inline Symbol Symbol::For(napi_env env, const char* description) {
}

inline Symbol Symbol::For(napi_env env, String description) {
napi_value descriptionValue = description;
return Symbol::For(env, descriptionValue);
return Symbol::For(env, static_cast<napi_value>(description));
}

inline Symbol Symbol::For(napi_env env, napi_value description) {
Expand Down
2 changes: 1 addition & 1 deletion napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ namespace Napi {
static Symbol WellKnown(napi_env, const std::string& name);

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

// Create a symbol in the global registry, C style string (null terminated)
static Symbol For(napi_env env, const char* description = nullptr);
Expand Down
16 changes: 8 additions & 8 deletions test/symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ async function test(binding)
assertSymbolAreUnique("symbol");
assertSymbolIsNotWellknown("testing");

for(const wellknownProperty of wellKnownSymbolFunctions)
{
assertSymbolIsWellknown(wellknownProperty);
}
for(const wellknownProperty of wellKnownSymbolFunctions)
{
assertSymbolIsWellknown(wellknownProperty);
}

assertCanCreateOrFetchGlobalSymbols("data", binding.symbol.getSymbolFromGlobalRegistry);
assertCanCreateOrFetchGlobalSymbols("CppKey", binding.symbol.getSymbolFromGlobalRegistryWithCppKey);
assertCanCreateOrFetchGlobalSymbols("CKey", binding.symbol.getSymbolFromGlobalRegistryWithCKey);
assertCanCreateOrFetchGlobalSymbols("data", binding.symbol.getSymbolFromGlobalRegistry);
assertCanCreateOrFetchGlobalSymbols("CppKey", binding.symbol.getSymbolFromGlobalRegistryWithCppKey);
assertCanCreateOrFetchGlobalSymbols("CKey", binding.symbol.getSymbolFromGlobalRegistryWithCKey);

assert(binding.symbol.createNewSymbolWithNoArgs() === undefined);
}
}

0 comments on commit 0366264

Please sign in to comment.