11cb0ef41Sopenharmony_ci#include <cstdio> 21cb0ef41Sopenharmony_ci#include <fstream> 31cb0ef41Sopenharmony_ci#include <iostream> 41cb0ef41Sopenharmony_ci#include <sstream> 51cb0ef41Sopenharmony_ci#include <string> 61cb0ef41Sopenharmony_ci#include <vector> 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ci#include "libplatform/libplatform.h" 91cb0ef41Sopenharmony_ci#include "node_internals.h" 101cb0ef41Sopenharmony_ci#include "node_snapshot_builder.h" 111cb0ef41Sopenharmony_ci#include "util-inl.h" 121cb0ef41Sopenharmony_ci#include "v8.h" 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciint BuildSnapshot(int argc, char* argv[]); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci#ifdef _WIN32 171cb0ef41Sopenharmony_ci#include <windows.h> 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciint wmain(int argc, wchar_t* wargv[]) { 201cb0ef41Sopenharmony_ci // Windows needs conversion from wchar_t to char. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci // Convert argv to UTF8. 231cb0ef41Sopenharmony_ci char** argv = new char*[argc + 1]; 241cb0ef41Sopenharmony_ci for (int i = 0; i < argc; i++) { 251cb0ef41Sopenharmony_ci // Compute the size of the required buffer 261cb0ef41Sopenharmony_ci DWORD size = WideCharToMultiByte( 271cb0ef41Sopenharmony_ci CP_UTF8, 0, wargv[i], -1, nullptr, 0, nullptr, nullptr); 281cb0ef41Sopenharmony_ci if (size == 0) { 291cb0ef41Sopenharmony_ci // This should never happen. 301cb0ef41Sopenharmony_ci fprintf(stderr, "Could not convert arguments to utf8."); 311cb0ef41Sopenharmony_ci exit(1); 321cb0ef41Sopenharmony_ci } 331cb0ef41Sopenharmony_ci // Do the actual conversion 341cb0ef41Sopenharmony_ci argv[i] = new char[size]; 351cb0ef41Sopenharmony_ci DWORD result = WideCharToMultiByte( 361cb0ef41Sopenharmony_ci CP_UTF8, 0, wargv[i], -1, argv[i], size, nullptr, nullptr); 371cb0ef41Sopenharmony_ci if (result == 0) { 381cb0ef41Sopenharmony_ci // This should never happen. 391cb0ef41Sopenharmony_ci fprintf(stderr, "Could not convert arguments to utf8."); 401cb0ef41Sopenharmony_ci exit(1); 411cb0ef41Sopenharmony_ci } 421cb0ef41Sopenharmony_ci } 431cb0ef41Sopenharmony_ci argv[argc] = nullptr; 441cb0ef41Sopenharmony_ci#else // UNIX 451cb0ef41Sopenharmony_ciint main(int argc, char* argv[]) { 461cb0ef41Sopenharmony_ci argv = uv_setup_args(argc, argv); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci // Disable stdio buffering, it interacts poorly with printf() 491cb0ef41Sopenharmony_ci // calls elsewhere in the program (e.g., any logging from V8.) 501cb0ef41Sopenharmony_ci setvbuf(stdout, nullptr, _IONBF, 0); 511cb0ef41Sopenharmony_ci setvbuf(stderr, nullptr, _IONBF, 0); 521cb0ef41Sopenharmony_ci#endif // _WIN32 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci v8::V8::SetFlagsFromString("--random_seed=42"); 551cb0ef41Sopenharmony_ci v8::V8::SetFlagsFromString("--harmony-import-assertions"); 561cb0ef41Sopenharmony_ci return BuildSnapshot(argc, argv); 571cb0ef41Sopenharmony_ci} 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ciint BuildSnapshot(int argc, char* argv[]) { 601cb0ef41Sopenharmony_ci if (argc < 2) { 611cb0ef41Sopenharmony_ci std::cerr << "Usage: " << argv[0] << " <path/to/output.cc>\n"; 621cb0ef41Sopenharmony_ci std::cerr << " " << argv[0] << " --build-snapshot " 631cb0ef41Sopenharmony_ci << "<path/to/script.js> <path/to/output.cc>\n"; 641cb0ef41Sopenharmony_ci return 1; 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci std::unique_ptr<node::InitializationResult> result = 681cb0ef41Sopenharmony_ci node::InitializeOncePerProcess( 691cb0ef41Sopenharmony_ci std::vector<std::string>(argv, argv + argc)); 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci CHECK(!result->early_return()); 721cb0ef41Sopenharmony_ci CHECK_EQ(result->exit_code(), 0); 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ci std::string out_path; 751cb0ef41Sopenharmony_ci if (node::per_process::cli_options->build_snapshot) { 761cb0ef41Sopenharmony_ci out_path = result->args()[2]; 771cb0ef41Sopenharmony_ci } else { 781cb0ef41Sopenharmony_ci out_path = result->args()[1]; 791cb0ef41Sopenharmony_ci } 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci std::ofstream out(out_path, std::ios::out | std::ios::binary); 821cb0ef41Sopenharmony_ci if (!out) { 831cb0ef41Sopenharmony_ci std::cerr << "Cannot open " << out_path << "\n"; 841cb0ef41Sopenharmony_ci return 1; 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci int exit_code = 0; 881cb0ef41Sopenharmony_ci { 891cb0ef41Sopenharmony_ci exit_code = node::SnapshotBuilder::Generate( 901cb0ef41Sopenharmony_ci out, result->args(), result->exec_args()); 911cb0ef41Sopenharmony_ci if (exit_code == 0) { 921cb0ef41Sopenharmony_ci if (!out) { 931cb0ef41Sopenharmony_ci std::cerr << "Failed to write " << out_path << "\n"; 941cb0ef41Sopenharmony_ci exit_code = 1; 951cb0ef41Sopenharmony_ci } 961cb0ef41Sopenharmony_ci } 971cb0ef41Sopenharmony_ci } 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci node::TearDownOncePerProcess(); 1001cb0ef41Sopenharmony_ci return exit_code; 1011cb0ef41Sopenharmony_ci} 102