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

Macro NODE_API_MODULE doesn't work with namespace in regfunc #249

Closed
ikokostya opened this issue Apr 26, 2018 · 1 comment
Closed

Macro NODE_API_MODULE doesn't work with namespace in regfunc #249

ikokostya opened this issue Apr 26, 2018 · 1 comment

Comments

@ikokostya
Copy link
Contributor

ikokostya commented Apr 26, 2018

Environment info

  • Node.js version: v10.0.0
  • node-addon-api version: 1.2.0

Steps to reproduce

Wrap functions from hello example to namespace:

diff --git a/1_hello_world/node-addon-api/hello.cc b/1_hello_world/node-addon-api/hello.cc
index 0957f53..f00bb77 100644
--- a/1_hello_world/node-addon-api/hello.cc
+++ b/1_hello_world/node-addon-api/hello.cc
@@ -1,5 +1,7 @@
 #include <napi.h>
 
+namespace node_lib {
+
 Napi::String Method(const Napi::CallbackInfo& info) {
   Napi::Env env = info.Env();
   return Napi::String::New(env, "world");
@@ -11,4 +13,6 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
   return exports;
 }
 
-NODE_API_MODULE(hello, Init)
+}
+
+NODE_API_MODULE(hello, node_lib::Init)

Actual behavior

Compilation error

gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory '/home/kostya/proj/abi-stable-node-addon-examples/1_hello_world/node-addon-api/build'
  CXX(target) Release/obj.target/hello/hello.o
In file included from /home/kostya/proj/abi-stable-node-addon-examples/1_hello_world/node-addon-api/node_modules/node-addon-api/napi.h:1487:0,
                 from ../hello.cc:1:
/home/kostya/proj/abi-stable-node-addon-examples/1_hello_world/node-addon-api/node_modules/node-addon-api/napi-inl.h:159:14: error: ‘__napi_node_lib’ has not been declared
   napi_value __napi_ ## regfunc(napi_env env,             \
              ^
../hello.cc:18:1: note: in expansion of macro ‘NODE_API_MODULE’
 NODE_API_MODULE(hello, node_lib::Init)
 ^
In file included from /home/kostya/proj/abi-stable-node-addon-examples/1_hello_world/node-addon-api/node_modules/node-addon-api/napi.h:4:0,
                 from ../hello.cc:1:
/home/kostya/proj/abi-stable-node-addon-examples/1_hello_world/node-addon-api/node_modules/node-addon-api/napi-inl.h:163:24: error: ‘__napi_node_lib’ has not been declared
   NAPI_MODULE(modname, __napi_ ## regfunc);
                        ^
/home/kostya/.node-gyp/10.0.0/include/node/node_api.h:83:7: note: in definition of macro ‘NAPI_MODULE_X’
       regfunc,                                                        \
       ^
/home/kostya/proj/abi-stable-node-addon-examples/1_hello_world/node-addon-api/node_modules/node-addon-api/napi-inl.h:163:3: note: in expansion of macro ‘NAPI_MODULE’
   NAPI_MODULE(modname, __napi_ ## regfunc);
   ^
../hello.cc:18:1: note: in expansion of macro ‘NODE_API_MODULE’
 NODE_API_MODULE(hello, node_lib::Init)
 ^
hello.target.mk:95: recipe for target 'Release/obj.target/hello/hello.o' failed

Expected behavior

No errors like with nan version.

@ikokostya ikokostya changed the title Macro NODE_API_MODULE doesn't work with namespaces Macro NODE_API_MODULE doesn't work with namespace in regfunc Apr 26, 2018
@gabrielschulhof
Copy link
Contributor

NODE_API_MODULE prepends the string __napi_ to whatever function name you pass into it to create a new function that will be called when the module is loaded by Node.js.

Thus, if you pass in a function called FooInit, the macro will create a function named __napi_FooInit which ultimately results in Node.js calling FooInit.

OTOH, if you pass a namespaced function such as something::FooInit, the macro will unflinchingly prepend the prefix __napi_ to whatever it receives – in this case resulting in a function named __napi_something::FooInit. This is, of course, incorrect, since the namespace __napi_something doesn't exist.

That's why in the above error output the compiler complains that error: ‘__napi_node_lib’ has not been declared – because your namespace is node_lib, not __napi_node_lib.

I'll make a point to document this in node-gyp.md.

gabrielschulhof pushed a commit to gabrielschulhof/node-addon-api that referenced this issue Apr 27, 2018
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this issue Aug 24, 2022
Fixes: nodejs/node-addon-api#249
PR-URL: nodejs/node-addon-api#251
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this issue Aug 26, 2022
Fixes: nodejs/node-addon-api#249
PR-URL: nodejs/node-addon-api#251
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this issue Sep 19, 2022
Fixes: nodejs/node-addon-api#249
PR-URL: nodejs/node-addon-api#251
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
austinli64 added a commit to austinli64/node-addon-api that referenced this issue May 9, 2023
Fixes: nodejs/node-addon-api#249
PR-URL: nodejs/node-addon-api#251
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this issue Aug 11, 2023
Fixes: nodejs/node-addon-api#249
PR-URL: nodejs/node-addon-api#251
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
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

2 participants