Skip to content
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

src: return Maybe<> on pending exception when cpp exception disabled #927

Merged
merged 12 commits into from
Aug 3, 2021

Conversation

legendecas
Copy link
Member

@legendecas legendecas commented Mar 9, 2021

Fixes: #690

This PR introduced a Maybe type to enforce value existence check when CPP exceptions disabled and there is a pending JavaScript exception so that the operation can not be performed.

The Maybe returning behavior requires a predefined macro NODE_ADDON_API_ENABLE_MAYBE to enable.

  • make exiting tests compatible with two flavors (with or without Maybes)

@legendecas legendecas force-pushed the maybe branch 2 times, most recently from 019adad to 2b40b49 Compare March 26, 2021 08:44
@legendecas

This comment has been minimized.

napi-inl.h Outdated Show resolved Hide resolved
@legendecas legendecas force-pushed the maybe branch 5 times, most recently from 96baadc to 7d933d0 Compare April 6, 2021 15:57
@legendecas legendecas marked this pull request as ready for review April 6, 2021 16:02
@legendecas

This comment has been minimized.

@legendecas legendecas force-pushed the maybe branch 2 times, most recently from d75115a to e42a1b9 Compare April 15, 2021 18:29
@legendecas legendecas force-pushed the maybe branch 4 times, most recently from eb8980e to 9684123 Compare May 23, 2021 16:01
@legendecas
Copy link
Member Author

@nodejs/node-api hi, team, I believe this PR is ready and may take some reviews. Thanks a lot!

@legendecas legendecas requested a review from a team June 9, 2021 15:18
@mhdawson
Copy link
Member

A few questions that we might discuss more in the nest team meeting

  • This seems to change whether some functions are const or not ?
  • Changes Number::FloatValue - to add a round trip instead of cast ?
  • There are still a number of TODOs
    • //TODO: find a way to define NAPI_HAS_ATTRIBUTE_WARN_UNUSED_RESULT
    • Many of -> ///TODO(legendecas): find a way to boxing with MaybeOrValue
  • We might need more explanation of what additional we need to do when writing tests to make sure the
    Maybe case is properly covered.

test/README.md Outdated

Value fn(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Value value = MaybeUnwrap(obj->Get("foobar")); // <- `obj->Get` may throws
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in a return or is it that we need to use the helpers for more than returns as well?

test/README.md Outdated
are defined as `Napi::MaybeOrValue<>` to prevent from duplicating most of the
code base.

To properly test these build flavors, we should take care of the return value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be something like.

To properly test these build flavors, all values returned by a function defined/used within the
node-addon-api test suite, should use one of helpers to ....

napi-inl.h Outdated
napi_value result;
napi_status status = napi_coerce_to_number(_env, _value, &result);
NAPI_THROW_IF_FAILED(_env, status, Number());
return Number(_env, result);
NAPI_MAYBE_RETURN_OR_THROW_IF_FAILED(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe this should just be called:

NAPI_RETURN_OR_THROW_IF_FAILED

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds much simpler. Renamed.

doc/error_handling.md Outdated Show resolved Hide resolved
doc/maybe.md Outdated Show resolved Hide resolved
doc/maybe.md Outdated Show resolved Hide resolved
doc/maybe.md Outdated Show resolved Hide resolved
doc/maybe.md Outdated Show resolved Hide resolved
doc/maybe.md Outdated Show resolved Hide resolved
napi-inl.h Outdated
}

inline bool Object::InstanceOf(const Function& constructor) const {
inline MaybeOrValue<bool> Object::InstanceOf(const Function& constructor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should remain const based on our discussion about #992.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gabrielschulhof thanks for catching that, not sure why I did not find it on my second look.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching it, fixed.

napi-inl.h Show resolved Hide resolved
napi-inl.h Show resolved Hide resolved
napi-inl.h Show resolved Hide resolved
@mhdawson
Copy link
Member

@legendecas looks like this needs to be rebased.

# Conflicts:
#	napi-inl.h
#	test/binding.gyp
test/README.md Outdated
Comment on lines 21 to 35
```cpp
#include "napi.h"
#include "test_helper.h"

using namespace Napi;

void fn(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Value value = MaybeUnwrap(obj->Get("foobar")); // <- `obj->Get` is calling
// into JavaScript and may throw JavaScript Exceptions. Here we just assert
// getting the parameter must not fail for convenience.

// ... do works with the value.
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might be better after the three examples or maybe it could just be removed if we had examples in each of the sections for the helpers?

test/functionreference.cc Outdated Show resolved Hide resolved
Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@legendecas
Copy link
Member Author

@gabrielschulhof 👋 may I have your review on this again? thanks a lot!

doc/error_handling.md Outdated Show resolved Hide resolved
doc/error_handling.md Outdated Show resolved Hide resolved
doc/error_handling.md Outdated Show resolved Hide resolved
doc/maybe.md Outdated Show resolved Hide resolved
doc/maybe.md Outdated Show resolved Hide resolved
test/README.md Outdated Show resolved Hide resolved
test/README.md Outdated Show resolved Hide resolved
test/README.md Outdated Show resolved Hide resolved
test/README.md Outdated Show resolved Hide resolved
@@ -115,64 +120,120 @@ class Test : public Napi::ObjectWrap<Test> {
Napi::Symbol kTestMethodTInternal = Napi::Symbol::New(env, "kTestMethodTInternal");
Napi::Symbol kTestVoidMethodTInternal = Napi::Symbol::New(env, "kTestVoidMethodTInternal");

exports.Set("Test", DefineClass(env, "Test", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one and many lines below look like pure whitespace changes. Are they necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two new elements at the bottom of the block. git-clang-format will check with blocks as minimum units.

@legendecas
Copy link
Member Author

@gabrielschulhof hi, PR updated with suggestion applied. PTAL, thanks a lot!

@mhdawson
Copy link
Member

@gabrielschulhof I'll plan to land end of this week unless you have objections/additional comments before then.

@legendecas
Copy link
Member Author

legendecas commented Aug 3, 2021

@mhdawson @gabrielschulhof 👋 should we land this PR now?

@mhdawson
Copy link
Member

mhdawson commented Aug 3, 2021

@legendecas could you squash into 1 commit? I tried landing and I ended up with conflicts.

@legendecas legendecas merged commit 3615041 into nodejs:main Aug 3, 2021
@legendecas
Copy link
Member Author

@mhdawson used the GitHub interface to squash merging. It seems no conflicts at all..

@legendecas legendecas deleted the maybe branch August 3, 2021 15:04
deepakrkris pushed a commit to deepakrkris/node-addon-api that referenced this pull request Sep 23, 2021
deepakrkris pushed a commit to deepakrkris/node-addon-api that referenced this pull request Oct 15, 2021
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional Value Boxes
3 participants