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

Add module initializer with context corresponding to NODE_MODULE_INITIALIZER #844

Open
murgatroid99 opened this issue Mar 26, 2019 · 9 comments

Comments

@murgatroid99
Copy link

Node 10 added Context-aware addons, using the NODE_MODULE_INITIALIZER. To be able to make such an addon with compatibility with older versions we need a corresponding macro in nan.

@NickNaso
Copy link
Member

NickNaso commented Mar 27, 2019

Hi @murgatroid99,
I think that NAN_MODULE_WORKER_ENALBED is the macro that you need. https://github.com/nodejs/nan/blob/master/nan.h#L158
Can someone say if I'm right or not?

@murgatroid99
Copy link
Author

That does look relevant, but it looks like it just ignores the context parameter, instead of providing some kind of shim or fallback. So, it looks like it doesn't help with actually writing an addon that is context-aware in Node 10+.

@ctavan
Copy link

ctavan commented Oct 25, 2019

@NickNaso are there any plans to resolve this issue or is there anything I could contribute?

This one is currently blocking grpc/grpc-node#778 which in turn is causing other trouble further down stream (vercel/next.js#7894) so I would be interested in getting this resolved.

@murgatroid99
Copy link
Author

According to electron/electron#18397, as of Electron 11 this will be required to load all native addons in Electron. Therefore, changing this should be a priority.

@bnoordhuis
Copy link
Member

That does look relevant, but it looks like it just ignores the context parameter, instead of providing some kind of shim or fallback.

What should that shim/fallback do in your opinion? Add-ons can already obtain the context in their initializer with Nan::GetCurrentContext().

@murgatroid99
Copy link
Author

Look, you have this definition:

#if NODE_MAJOR_VERSION >= 10 || \
    NODE_MAJOR_VERSION == 9 && NODE_MINOR_VERSION >= 3
#define NAN_MODULE_WORKER_ENABLED(module_name, registration)                   \
    extern "C" NODE_MODULE_EXPORT void                                         \
      NAN_CONCAT(node_register_module_v, NODE_MODULE_VERSION)(                 \
        v8::Local<v8::Object> exports, v8::Local<v8::Value> module,            \
        v8::Local<v8::Context> context)                                        \
    {                                                                          \
        registration(exports);                                                 \
    }

That applies to the version range we are interested in, and it discards the context parameter instead of passing it to the registration function. This macro appears to correspond to the NODE_MODULE_INIT macro described in the Context-aware addons documentation, and that documentation states that the context variable is available in the function body passed to that macro. Based on this information, it seems like NAN_MODULE_WORKER_ENABLED does not facilitate the creation of a context-aware addon the way NODE_MODULE_INIT does.

Honestly, I was unaware of Nan::GetCurrentContext() and nobody mentioned it in this issue before, and the documentation doesn't clearly indicate that it can be used in that function as a replacement for the context variable.

@bnoordhuis
Copy link
Member

@murgatroid99 Do you want to update the API or the docs? In both cases a pull request is the best way forward. :-)

@murgatroid99
Copy link
Author

I don't know what the right change here is. I'm just a user of this library; that doesn't mean I'm knowledgeable about the nuances of the Node API or its changes across versions.

In particular, you say that Nan::GetCurrentContext() can access the current context in the module initializer. If that is the case, why does Node's API also pass it in as an argument to the initializer? I think someone who knows the answer to that question is in a better position to determine what change should be made here.

@bnoordhuis
Copy link
Member

why does Node's API also pass it in as an argument to the initializer?

As the one who introduced the original multi-context support in Node: because it seemed like a good idea at the time. :-)

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

No branches or pull requests

4 participants