-
-
Couldn't load subscription status.
- Fork 33.6k
node-api: explicitly set __cdecl for Node-API functions #42780
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
Conversation
|
Review requested:
|
|
We discussed this on today's Node API meeting. This makes sense to me. Does anyone else on the team have any issues / concerns? We may need to look at the |
713c6e6 to
7434911
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. From the discussion in the last Node-api team meeting my understaning is that does not change anything since CDECL was already the default, but it does help my making it explicit.
PR-URL: #42780 Reviewed-By: Michael Dawson <[email protected]>
|
Landed in fb74474 |
PR-URL: #42780 Reviewed-By: Michael Dawson <[email protected]>
|
This is blocked by #42459 |
PR-URL: #42780 Reviewed-By: Michael Dawson <[email protected]>
PR-URL: nodejs/node#42780 Reviewed-By: Michael Dawson <[email protected]>
The issue
The Node-API is a quite generic ABI-safe API that can be used as JavaScript engine ABI-safe API outside of Node.JS project.
The issue is that currently it does not specify calling conventions which is critical if Windows DLLs compiled with different default calling conventions. It is not important for x64 or non-Windows platforms because they use one predefined calling convention, but for Windows x86 applications there are multiple calling conventions, and their mismatch causes a runtime crash.
E.g. I had previously added
__cdecltov8jsi.dllcopy ofjs_native_api.h, but we still saw crashes in Windows x86 because__cdeclwas not added to function pointers injs_native_api_types.h. The issue is being addressed by microsoft/v8-jsi#122.This example shows how important the calling conventions are for Window x86 platform.
The solution
In this PR we are adding
__cdeclto all functions and function pointers that target Win32 platform.To do that we add a new macro
NAPI_CDECL. It is expanded to__cdeclfor Win32 platforms and to nothing for other platforms.Discussion
This PR sets
__cdeclas the calling conventions because it is the default in C/C++ compilers.It would be ideal to use
__stdcallcalling conventions to match Windows API because it produces more compact code on calling side, but my concern is that such change may affect existing code. Though, if Node.JS is not shipped for x86, then it may be still safe to use__stdcallinstead of__cdecl.