node-api: explicitly set __cdecl for Node-API functions
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 __cdecl
to v8jsi.dll
copy of js_native_api.h
, but we still saw crashes in Windows x86 because __cdecl
was not added to function pointers in js_native_api_types.h
. The issue is being addressed by https://github.com/microsoft/v8-jsi/pull/122.
This example shows how important the calling conventions are for Window x86 platform.
The solution
In this PR we are adding __cdecl
to all functions and function pointers that target Win32 platform.
To do that we add a new macro NAPI_CDECL
. It is expanded to __cdecl
for Win32 platforms and to nothing for other platforms.
Discussion
This PR sets __cdecl
as the calling conventions because it is the default in C/C++ compilers.
It would be ideal to use __stdcall
calling 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 __stdcall
instead of __cdecl
.