-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Is well-known symbol the recommended way to build addon? #21291
Comments
If you go with the well-known symbol approach, you will want to avoid
having global static data, because you are committing yourself to
supporting multiple loading of your addon. This is desirable.
Note that you can perform the following steps to avoid using global static
data:
1. Heap-allocate a structure during module init which stores all the data
you would normally store in global static variables.
2. Create a weak reference to the exports object which, when called,
destroys the structure.
3. Pass the native pointer to each binding you expose to JavaScript.
4. Pass the native pointer to each async callback.
Then, in all your bindings you will be able to call `info.Data()` to
retrieve the pointer if it's a V8 binding.
Similarly, in N-API you can associate data with a binding and it will be
returned in argument 6 of `napi_get_cb_info()`.
I have created a gist[0] to provide a N-API example.
[0] https://gist.github.com/gabrielschulhof/d24c1f66b9f48896318e332d8a756b87
…On Tue, Jun 12, 2018 at 2:06 PM, Joyee Cheung ***@***.***> wrote:
cc @bnoordhuis <https://github.com/bnoordhuis> @gabrielschulhof
<https://github.com/gabrielschulhof>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#21291 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AA7k0el7EykEYQ1xrI58so82s-cEYXkXks5t8AMWgaJpZM4Uk4Pt>
.
|
@joyeecheung I wrote the previous comment wrong. In step 1. I meant to say "... you would normally store in global static variables." |
@joyeecheung AFAIK currently |
@joyeecheung the N-API documentation on master mentions the macro |
This has not been back-ported though, AFAIK. |
@joyeecheung the hello world addon test already tests multiple loading of an addon. |
thanks @gabrielschulhof for the detailed information. The steps show HOW to avoid using global static data, but I don't understand WHY should avoid using global static data. Sometimes I want to have a shared state (like a singleton pattern), I believe this scenario is OK to have. So the good news is, the N-API macro |
@fs-eire I guess it boils down to whether the global static singleton contains any references to JavaScript values. Since the module may be loaded multiple times from different contexts, and perhaps even different isolates, the JavaScript value(s) stored in the singleton may not be valid. Thus, if you want to have a singleton, it should only contain native data.
|
Introduces macros `NODE_MODULE_INITIALIZER` which expands to the name of the special symbol that process.dlopen() will look for to initialize an addon, and `NODE_MODULE_INIT()` which creates the boilerplate for a context-aware module which can be loaded multiple times via the special symbol mechanism. Additionally, provides an example of using the new macro to construct an addon which stores per-addon-instance data in a heap-allocated structure that gets passed to each binding, rather than in a collection of global static variables. Re: nodejs#21291 (comment) PR-URL: nodejs#21318 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
Introduces macros `NODE_MODULE_INITIALIZER` which expands to the name of the special symbol that process.dlopen() will look for to initialize an addon, and `NODE_MODULE_INIT()` which creates the boilerplate for a context-aware module which can be loaded multiple times via the special symbol mechanism. Additionally, provides an example of using the new macro to construct an addon which stores per-addon-instance data in a heap-allocated structure that gets passed to each binding, rather than in a collection of global static variables. Re: #21291 (comment) PR-URL: #21318 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
There's been no further discussion here and it's not clear if there's anything actionable. Closing. Can reopen if necessary |
Introduces macro `NODE_MODULE_INIT()` which creates the boilerplate for a context-aware module which can be loaded multiple times via the special symbol mechanism. Additionally, provides an example of using the new macro to construct an addon which stores per-addon-instance data in a heap-allocated structure that gets passed to each binding, rather than in a collection of global static variables. Re: nodejs/node#21291 (comment)
The change #18934 introduced a new interface enabling addon to initialize itself for multiple context using symbol
node_register_module_v${NODE_MODULE_VERSION}
.However I didn't find much more information about this feature:
no related documents on https://nodejs.org/dist/latest-v10.x/docs/api/addons.html;
no examples ( only one simple test );
no related macro definition in node.h;
no n-api equivalent
I think the feature is still in experimental.. will it become a preferred and recommended way for addon, or it just going to be remove in some future version?
The text was updated successfully, but these errors were encountered: