Add NonConstructor to allow customization of ObjectWrap behavior#1125
Add NonConstructor to allow customization of ObjectWrap behavior#1125p120ph37 wants to merge 4 commits intonodejs:mainfrom
NonConstructor to allow customization of ObjectWrap behavior#1125Conversation
| } | ||
|
|
||
| static std::unordered_map<napi_env, Napi::FunctionReference*> constructors; | ||
|
|
There was a problem hiding this comment.
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.
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.
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 |
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
NonConstructorin theirObjectWrapsubclass in order to define alternate behavior. As a trivial example of behavior a developer might implement, this code in anObjectWrapsubclass would support the old (ugly) idiom of instantiating a class even when the function is called without thenewkeyword: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.