Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 493
Added test coverage for Symbol class#972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Conversation
JckXia commented Apr 20, 2021
I can't seem to recreate this test failure. It seems intermittent as the CI running against this commit is successful in my forked repo. https://github.com/JckXia/node-addon-api/actions/runs/765661855. |
NickNaso left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JckXia well done. Just some comments.
napi.h Outdated
| static Symbol WellKnown(napi_env, const std::string& name); | ||
| // Create a symbol in the global registry; | ||
| static Symbol For(napi_env env, const std::string& name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need some other overloads for this methods?
static Symbol For(napi_env env, constchar* description = nullptr); static Symbol For(napi_env env, String description); static Symbol For(napi_env env, napi_value description);What do you think about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm yeah I think that will be a good idea. Will add them in soon.
napi.h Outdated
| static Symbol WellKnown(napi_env, const std::string& name); | ||
| // Create a symbol in the global registry; | ||
| static Symbol For(napi_env env, const std::string& name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remember to add documentation for this new method.
test/symbol.cc Outdated
| usingnamespaceNapi; | ||
| Symbol CreateNewSymbolWithNoArgs(const Napi::CallbackInfo& info){ | ||
| (void)info; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does it necessary do this?
(void)info;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was to suppress compiler warnings for unused parameter, since the default Symbol constructor doesn't take in any values. But i can remove this function since it's not being called from js.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid compiler complains maybe you can try the following solution (comment the formal parameter of the function):
Symbol CreateNewSymbolWithNoArgs(const Napi::CallbackInfo& /*info*/){Let me know if it worked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep it works. Thanks!
test/symbol.js Outdated
| } | ||
No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove one empty new line.
| exports["getSymbolFromGlobalRegistry"] = | ||
| Function::New(env, FetchSymbolFromGlobalRegistry); | ||
| return exports; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PLease add one empty new line.
NickNaso commented Apr 20, 2021
Don't worry I restarted the jobs and now it's all green. |
NickNaso left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @JckXia,
I added som e comments about you last work.
doc/symbol.md Outdated
| attempting to use the returned value. | ||
| ### Utf8Value | ||
| ### Wellkown |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the typo it should be WellKnown
| ```cpp | ||
| static Napi::Symbol Napi::Symbol::WellKnown(napi_env env, const std::string& name); | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the documentation for all the overloads of Napi::Symbol Napi::Symbol::WellKnown() methos.
doc/symbol.md Outdated
| - `[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. | ||
| Register `Napi::Symbol` in the global registry. If symbol already exist retrieve said symbol. Equivalent to `Symbol.for("symb")` called from JS. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the following description is better:
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-inl.h Outdated
| } | ||
| inline Symbol Symbol::For(napi_env env, String description){ | ||
| napi_value descriptionValue = description; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can directory pass description to Symbol::For() because Napi::String is a Napi::Value that has the cast operator napi_value.
doc/symbol.md Outdated
| ### For | ||
| ```cpp | ||
| static Napi::Symbol Napi::Symbol::WellKnown(napi_env env, const std::string& name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are adding the For method... should this be For...?
NickNaso left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @JckXia,
just something to fix.
doc/symbol.md Outdated
| attempting to use the returned value. | ||
| ### Utf8Value | ||
| ### Wellknown |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It shlould be WellKnown insteand of Wellknown. The k should be in uppercase.
doc/symbol.md Outdated
| static Napi::Symbol For(napi_env env, const char* description = nullptr); | ||
| static Napi::Symbol For(napi_env env, String description); | ||
| static Napi::Symbol For(napi_env env, napi_value description); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the complete namespace there:
static Napi::Symbol Napi::Symbol::For(napi_env env, constchar* 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);
NickNaso left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @JckXia,
could you propose the last change on the package.json on a different PR? I'm asking for this because the change it's not correlated with the test and in case we will need to revert the change it will be more simpler to do. For the rest it's all ok.
JckXia commented Apr 28, 2021
Hey @NickNaso |
NickNaso left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
gabrielschulhof left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking until we figure out what happens when you pass nullptr to napi_create_string_utf8() as its const char* parameter.
| 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, constchar* description = nullptr); | ||
| // Create a symbol in the global registry, String value describing the symbol | ||
| static Symbol For(napi_env env, String description); | ||
| // Create a symbol in the global registry, napi_value describing the symbol | ||
| static Symbol For(napi_env env, napi_value description); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| staticSymbolFor(napi_envenv, conststd::string&description); | |
| // Create a symbol in the global registry, C style string (null terminated) | |
| staticSymbolFor(napi_envenv, constchar*description=nullptr); | |
| // Create a symbol in the global registry, String value describing the symbol | |
| staticSymbolFor(napi_envenv, Stringdescription); | |
| // Create a symbol in the global registry, napi_value describing the symbol | |
| staticSymbolFor(napi_envenv, napi_valuedescription); | |
| staticSymbolFor(Envenv, conststd::string&description); | |
| // Create a symbol in the global registry, C style string (null terminated) | |
| staticSymbolFor(Envenv, constchar*description=nullptr); | |
| // Create a symbol in the global registry, String value describing the symbol | |
| staticSymbolFor(Envenv, Stringdescription); | |
| // Create a symbol in the global registry, napi_value describing the symbol | |
| staticSymbolFor(Envenv, napi_valuedescription); | |
| returnNapi::Env(env).Global().Get("Symbol").As<Object>().Get(name).As<Symbol>(); | ||
| } | ||
| inline Symbol Symbol::For(napi_env env, const std::string& description){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| inlineSymbolSymbol::For(napi_envenv, conststd::string&description){ | |
| inlineSymbolSymbol::For(Napi::Envenv, conststd::string&description){ |
... and a similar change for all APIs below.
gabrielschulhof commented May 28, 2021
@JckXia thanks for catching this! |
gabrielschulhof commented May 29, 2021
We must never pass I'll make a PR upstream to fix this in core. |
JckXia commented Jun 2, 2021 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Thanks for the review @gabrielschulhof . Just making sure, are we converting the env parameter in these overloads from type |
gabrielschulhof commented Jun 4, 2021
I checked napi.h by looking for |
gabrielschulhof commented Jun 4, 2021
Refs: nodejs/node#38923 |
mhdawson commented Jun 14, 2021
@JckXia just wondering what the next steps are to get this landed? |
JckXia commented Jun 14, 2021
@mhdawson I believe that it is ready to be merged. |
doc/symbol.md Outdated
| ### For | ||
| ```cpp | ||
| 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @JckXia ,
Do we have a test for this nullptr value...? I am not sure what happens when using a nullptr napi_value for the description here:
auto forSymb = symbObject.Get("for").As<Function>().Call(symbObject,{description});and I think a test would be beneficial
JckXia commented Jun 16, 2021
@KevinEady Thanks for catching that! Apparently if we simply pass the C++ nullptr into that function it will cause a SegFault. I updated the Symbol::For method impl to use the napi_value we get from calling the napi_get_null function instead and it seems to work. |
KevinEady commented Jun 16, 2021
@JckXia Aahahh... I am inclined then to remove the {[null]: "3"}is a valid JSON object, but we do not check in Lines 1227 to 1232 in ad76ad0
The application would just crash (I assume). I would think, for consistency purposes, we should remove the nullptr default as well as the runtime check (which is additional overhead anyway). If the user wanted to explicitly use Thoughts? |
JckXia commented Jun 16, 2021
@KevinEady Oh okay I see. So I think we can simply pass the description argument into String::New(), and pass the newly constructed Napi string the Symbol::For function? Also, I think this |
KevinEady commented Jul 2, 2021
This depends on landing #1015 first. |
gabrielschulhof commented Jul 2, 2021
Please feel free to dismiss my change request after #1015 has landed and the |
KevinEady commented Jul 2, 2021
As discussed in today's meeting, please remove the |
mhdawson commented Jul 9, 2021
1015 landed, dismissing @gabrielschulhof review as suggested above. |
He said it could be dismissed after 1015 landed which has landed.
mhdawson left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
mhdawson commented Jul 9, 2021
@JckXia could you squash down to 1 commit? I'm having trouble landing because it crashes in an git am and I'm thinking it being in 1 commit would resolve that. |
Add new static method to symbol and updated tests Updated Symbol.md 's docs, added function overload for Symbol class and new tests Remove extra space and comments Remove un-necssary string initialization Update PR based on comments received Add test checking if it's possible to pass nullptr to Symbol::For method Update Symbol::For implementations and added new tests Remove default parameter Update documentation for Symbol Fixing merge conflicts
JckXia commented Jul 9, 2021
@mhdawson Thanks, it's been fixed. |
PR-URL: #972 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
mhdawson commented Jul 9, 2021
Landed in e02e8a4 |
PR-URL: nodejs#972 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
PR-URL: nodejs#972 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
PR-URL: nodejs/node-addon-api#972 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
PR-URL: nodejs/node-addon-api#972 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
PR-URL: nodejs/node-addon-api#972 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
PR-URL: nodejs/node-addon-api#972 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
Added new tests for the Symbol class. Also added a static method to the Symbol class to perform "Symbol.For()" from the C++ side, since the exisiting method (Symbol::Wellknown) , which has a different functionality caused some confusion.