Skip to content
Prev Previous commit
Next Next commit
fixup! build: add --node-snapshot-main configure option
  • Loading branch information
joyeecheung committed Mar 26, 2022
commit da8c19f7f5be99c93196fdecfa207b147f000ac5
58 changes: 48 additions & 10 deletions tools/snapshot/node_mksnapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,73 @@
#include "util-inl.h"
#include "v8.h"

int BuildSnapshot(int argc, char* argv[]);

#ifdef _WIN32
#include <windows.h>

int wmain(int argc, wchar_t* argv[]) {
int wmain(int argc, wchar_t* wargv[]) {
// Windows needs conversion from wchar_t to char.

// 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);
}
Comment thread
joyeecheung marked this conversation as resolved.
// 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);
}
Comment thread
joyeecheung marked this conversation as resolved.
}
argv[argc] = nullptr;
#else // UNIX
int main(int argc, char* argv[]) {
argv = uv_setup_args(argc, argv);

// Disable stdio buffering, it interacts poorly with printf()
// calls elsewhere in the program (e.g., any logging from V8.)
setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);
#endif // _WIN32

v8::V8::SetFlagsFromString("--random_seed=42");
v8::V8::SetFlagsFromString("--harmony-import-assertions");
return BuildSnapshot(argc, argv);
}

int BuildSnapshot(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <path/to/output.cc>\n";
std::cerr << " " << argv[0] << " --build-snapshot "
<< "<path/to/script.js> <path/to/output.cc>\n";
return 1;
}

// Windows needs conversion from wchar_t to char. See node_main.cc
#ifdef _WIN32
int node_argc = 1;
char argv0[] = "node";
char* node_argv[] = {argv0, nullptr};
node::InitializationResult result =
node::InitializeOncePerProcess(node_argc, node_argv);
#else
node::InitializationResult result =
node::InitializeOncePerProcess(argc, argv);
#endif

CHECK(!result.early_return);
CHECK_EQ(result.exit_code, 0);
Expand Down