Repository: node
Issue: "node-gyp rebuild" fails with node addon api if module name contains special chars
Labels: gyp

<!--
Thank you for reporting an issue.

This issue tracker is for bugs and issues found within Node.js core.
If you require more general support please file an issue on our help
repo. https://github.com/nodejs/help


Please fill in as much of the template below as you're able.

Version: output of `node -v`

Platform: output of `uname -a` (UNIX), or output of `"$([Environment]::OSVersion | ForEach-Object VersionString) $(if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" })"` in PowerShell console (Windows)
Subsystem: if known, please specify affected core module name
-->

* **Version**: v12.18.2   (but I've had this problem with older versions as well)

* **Platform**: Linux djfujnb 5.4.0-58-generic #64-Ubuntu SMP Wed Dec 9 08:16:25 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
* **Subsystem**:  node-gyp + node addon api

### What steps will reproduce the bug?
"node-gyp rebuild"  on a C++ node addon that contains special chars within target_name in binding.gyp

<!--
Enter details about your bug, preferably a simple code snippet that can be
run using `node` directly without installing third-party dependencies.
-->
(snip from binding.gyp)
  "target_name": "my-addon",  #this will fail
  "target_name": "my_addon",  #no errors

### How often does it reproduce? Is there a required condition?
100% reproducible with conditions specified

### What is the expected behavior?
successful node addon rebuild
<!--
If possible please provide textual output instead of screenshots.
-->

### What do you see instead?
(snip from build output)
<command-line>: error: expected initializer before ‘-’ token
/home/dj/.cache/node-gyp/12.18.2/include/node/node_api.h:53:15: note: in definition of macro ‘NAPI_C_CTOR’
   53 |   static void fn(void) __attribute__((constructor)); \
      |               ^~

(snip)
<command-line>: error: expected initializer before ‘-’ token
/home/dj/.cache/node-gyp/12.18.2/include/node/node_api.h:54:15: note: in definition of macro ‘NAPI_C_CTOR’
   54 |   static void fn(void)
      |               ^~
/home/dj/.cache/node-gyp/12.18.2/include/node/node_api.h:75:3: note: in expansion of macro ‘NAPI_MODULE_X’
   75 |   NAPI_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
      |   ^~~~~~~~~~~~~


<!--
If possible please provide textual output instead of screenshots.
-->

### Additional information
Problem exists within the definition of macro NAPI_MODULE_X within ~/.nvm/versions/node/v12.18.2/include/node/node_api.h
Root cause is that the module name is being used as a C++ symbol when it doesn't need to be.

<!--
Tell us anything else you think we should know.
-->
To fix, change NAPI_MODULE_X at line 57 of ~/.nvm/versions/node/v12.18.2/include/node/node_api.h
from:
#define NAPI_MODULE_X(modname, regfunc, priv, flags)                  \
  EXTERN_C_START                                                      \
    static napi_module _module =                                      \
    {                                                                 \
      NAPI_MODULE_VERSION,                                            \
      flags,                                                          \
      __FILE__,                                                       \
      regfunc,                                                        \
      #modname,                                                       \
      priv,                                                           \
      {0},                                                            \
    };                                                                \
    NAPI_C_CTOR(_register_ ## modname) {                              \
      napi_module_register(&_module);                                 \
    }                                                                 \
  EXTERN_C_END

to (see "CHANGED HERE"):
#define NAPI_MODULE_X(modname, regfunc, priv, flags)                  \
  EXTERN_C_START                                                      \
    static napi_module _module =                                      \
    {                                                                 \
      NAPI_MODULE_VERSION,                                            \
      flags,                                                          \
      __FILE__,                                                       \
      regfunc,                                                        \
      #modname,                                                       \
      priv,                                                           \
      {0},                                                            \
    };                                                                \
    NAPI_C_CTOR(_register_ ## regfunc /\*modname   CHANGED HERE\*/) {                              \
      napi_module_register(&_module);                                 \
    }                                                                 \
  EXTERN_C_END

This change will use regfunc instead of modname in the token pasting for the C ctor name.