node-api: explicitly set __cdecl for Node-API functions#42780
Closed
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.