mirror of
https://github.com/nodejs/node.git
synced 2026-12-04 02:19:15 -05:00
So far, process initialization has been a bit all over the place in Node.js. `InitializeNodeWithArgs()` is our main public API for this, but inclusion of items in it vs. `InitializeOncePerProcess()` and `PlatformInit()` has been random at best. Likewise, some pieces of initialization have been guarded by `NODE_SHARED_MODE`, but also fairly randomly and without any meaningful connection to shared library usage. This leaves embedders in a position to cherry-pick some of the initialization code into their own code to make their application behave like typical Node.js applications to the degree to which they desire it. Electron takes an alternative route and makes direct use of `InitializeOncePerProcess()` already while it is a private API, with a `TODO` to add it to the public API in Node.js. This commit addresses that `TODO`, and `TODO`s around the `NODE_SHARED_MODE` usage. Specifically: - `InitializeOncePerProcess()` and `TearDownOncePerProcess()` are added to the public API. - The `flags` option of these functions are merged with the `flags` option for `InitializeNodeWithArgs()`, since they essentially share the same semantics. - The return value of the function is made an abstract class, rather than a struct, for easier API/ABI stability. - Initialization code from `main()` is brought into these functions (since that makes sense in general). - Add a `TODO` for turning `InitializeNodeWithArgs()` into a small wrapper around `InitializeOncePerProcess()` and eventually removing it (at least one major release cycle each, presumably). - Remove `NODE_SHARED_MODE` guards and replace them with runtime options. PR-URL: https://github.com/nodejs/node/pull/44121 Backport-PR-URL: https://github.com/nodejs/node/pull/44358 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
96 lines
3.6 KiB
C++
96 lines
3.6 KiB
C++
// Copyright Joyent, Inc. and other Node contributors.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
// copy of this software and associated documentation files (the
|
|
// "Software"), to deal in the Software without restriction, including
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
// following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included
|
|
// in all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
#include "node.h"
|
|
#include <cstdio>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#include <VersionHelpers.h>
|
|
#include <WinError.h>
|
|
|
|
#define SKIP_CHECK_VAR "NODE_SKIP_PLATFORM_CHECK"
|
|
#define SKIP_CHECK_SIZE 1
|
|
#define SKIP_CHECK_VALUE "1"
|
|
|
|
int wmain(int argc, wchar_t* wargv[]) {
|
|
// Windows Server 2012 (not R2) is supported until 10/10/2023, so we allow it
|
|
// to run in the experimental support tier.
|
|
char buf[SKIP_CHECK_SIZE + 1];
|
|
if (!IsWindows8Point1OrGreater() &&
|
|
!(IsWindowsServer() && IsWindows8OrGreater()) &&
|
|
(GetEnvironmentVariableA(SKIP_CHECK_VAR, buf, sizeof(buf)) !=
|
|
SKIP_CHECK_SIZE ||
|
|
strncmp(buf, SKIP_CHECK_VALUE, SKIP_CHECK_SIZE + 1) != 0)) {
|
|
fprintf(stderr, "Node.js is only supported on Windows 8.1, Windows "
|
|
"Server 2012 R2, or higher.\n"
|
|
"Setting the " SKIP_CHECK_VAR " environment variable "
|
|
"to 1 skips this\ncheck, but Node.js might not execute "
|
|
"correctly. Any issues encountered on\nunsupported "
|
|
"platforms will not be fixed.");
|
|
exit(ERROR_EXE_MACHINE_TYPE_MISMATCH);
|
|
}
|
|
|
|
// Convert argv to UTF8
|
|
char** argv = new char*[argc + 1];
|
|
for (int i = 0; i < argc; i++) {
|
|
// Compute the size of the required buffer
|
|
DWORD size = WideCharToMultiByte(CP_UTF8,
|
|
0,
|
|
wargv[i],
|
|
-1,
|
|
nullptr,
|
|
0,
|
|
nullptr,
|
|
nullptr);
|
|
if (size == 0) {
|
|
// This should never happen.
|
|
fprintf(stderr, "Could not convert arguments to utf8.");
|
|
exit(1);
|
|
}
|
|
// Do the actual conversion
|
|
argv[i] = new char[size];
|
|
DWORD result = WideCharToMultiByte(CP_UTF8,
|
|
0,
|
|
wargv[i],
|
|
-1,
|
|
argv[i],
|
|
size,
|
|
nullptr,
|
|
nullptr);
|
|
if (result == 0) {
|
|
// This should never happen.
|
|
fprintf(stderr, "Could not convert arguments to utf8.");
|
|
exit(1);
|
|
}
|
|
}
|
|
argv[argc] = nullptr;
|
|
// Now that conversion is done, we can finally start.
|
|
return node::Start(argc, argv);
|
|
}
|
|
#else
|
|
// UNIX
|
|
|
|
int main(int argc, char* argv[]) {
|
|
return node::Start(argc, argv);
|
|
}
|
|
#endif
|