Skip to content

Explicit initialisation of Node-API addons #44853

@cosinusoidally

Description

@cosinusoidally

What is the problem this feature will solve?

I have been trying to build a Node-API module using the Tiny C Compiler (https://bellard.org/tcc/) . I've managed to do it (with a couple of work arounds) but I ran into some issues around the use of __attribute__((constructor)) . Is there a reason this particular API design was chosen? Could an explicitly known init function be called if a module failed to self register?

tcc has a couple of issues around the use of __attribute__((constructor)):

  1. support was not added until late 2019. Not an issue now, but easy to get caught out if you use the most recent "official" release of tcc from late 2017.
  2. including glibc headers (like string.h) will strip out all uses of __attribute__((constructor)) by tcc. I think this is a bug in the glibc headers, see https://lists.nongnu.org/archive/html/tinycc-devel/2022-10/msg00000.html

To work around this I used a newer version of tcc, and also had to expand out the NAPI_MODULE macro and move the _register_... declaration to the top of my source file: https://github.com/cosinusoidally/mishmashvm/blob/dev/tests/nodejs/stub.c (build script is https://github.com/cosinusoidally/mishmashvm/blob/dev/tests/nodejs/mk)

Having an explicit init function could also be useful for compilers for other languages that potentially do not have support for constructors in shared libraries.

What is the feature you are proposing to solve the problem?

The ability to define an explicit init funtion to avoid the use of __attribute__((constructor)) .

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions