-
Notifications
You must be signed in to change notification settings - Fork 465
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
Add NonConstructor
to allow customization of ObjectWrap behavior
#1125
Conversation
test/objectwrap_nonconstructor.cc
Outdated
} | ||
|
||
static std::unordered_map<napi_env, Napi::FunctionReference*> constructors; | ||
|
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 know this is leaky without an env-shutdown-hook, but it's good enough to run the tests without clobbering Napi::Addon<T>
via env.SetInstanceData()
as would be the typical idiom.
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.
Should probably add a comment to the source so nobody thinks it’s a good idea to copy paste this into a real project…
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've added an env cleanup-hook so this test-code shouldn't leak anymore and should be adaptable to real-world projects.
5d75653
to
1d1ad35
Compare
We discussed in the node-api team meeting today. We agreed giving developers the option makes sense. We would like the PR to include updating the documentation as well. We would prefer that instead of using NonConstructor that we use CreateViaFunctionCall |
Sounds reasonable to me - I'll make the requested adjustments. |
I'm happy to name it |
+@mhdawson and @gabrielschulhof @p120ph37, it is a great point! There were a few points for the alternative name:
Since |
I like |
I like it too! Though, the final decision is from @mhdawson and @gabrielschulhof. |
adding my +1 for OnCalledAsFunction |
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
Add new method to allow customization of ObjectWrap behavior PR-URL: #1125 Reviewed-By: Michael Dawson <midawson@redhat.com
Landed as cee899a |
Add new method to allow customization of ObjectWrap behavior PR-URL: nodejs/node-addon-api#1125 Reviewed-By: Michael Dawson <midawson@redhat.com
Add new method to allow customization of ObjectWrap behavior PR-URL: nodejs/node-addon-api#1125 Reviewed-By: Michael Dawson <midawson@redhat.com
Add new method to allow customization of ObjectWrap behavior PR-URL: nodejs/node-addon-api#1125 Reviewed-By: Michael Dawson <midawson@redhat.com
Add new method to allow customization of ObjectWrap behavior PR-URL: nodejs/node-addon-api#1125 Reviewed-By: Michael Dawson <midawson@redhat.com
Addresses #1123 by moving the error-throw into an overridable static method. Default behavior remains exactly the same, but now a developer can override
NonConstructor
in theirObjectWrap
subclass in order to define alternate behavior. As a trivial example of behavior a developer might implement, this code in anObjectWrap
subclass would support the old (ugly) idiom of instantiating a class even when the function is called without thenew
keyword:Which then allows this usage:
I've also included a new test case, which amounts to basically the above, and also tests that exceptions thrown inside the non-constructor are handled appropriately.