Skip to content

Commit e80613c

Browse files
committed
bootstrap: handle snapshot errors gracefully
This patch refactors the SnapshotBuilder::Generate() routines so that when running into errors during the snapshot building process, they can exit gracefully by printing the error and return a non-zero exit code. If the error is likely to be caused by internal scripts, the return code would be 12, if the error is caused by user scripts the return code would be 1. In addition this refactors the generation of embedded snapshots and directly writes to the output file stream instead of producing an intermediate string with string streams.
1 parent 1959510 commit e80613c

File tree

5 files changed

+209
-173
lines changed

5 files changed

+209
-173
lines changed

src/env.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -989,13 +989,17 @@ struct EnvSerializeInfo {
989989
};
990990

991991
struct SnapshotData {
992-
// The result of v8::SnapshotCreator::CreateBlob() during the snapshot
993-
// building process.
994-
v8::StartupData v8_snapshot_blob_data;
992+
enum class DataOwnership { kOwned, kNotOwned };
995993

996994
static const size_t kNodeBaseContextIndex = 0;
997995
static const size_t kNodeMainContextIndex = kNodeBaseContextIndex + 1;
998996

997+
DataOwnership data_ownership;
998+
999+
// The result of v8::SnapshotCreator::CreateBlob() during the snapshot
1000+
// building process.
1001+
v8::StartupData v8_snapshot_blob_data;
1002+
9991003
std::vector<size_t> isolate_data_indices;
10001004
// TODO(joyeecheung): there should be a vector of env_info once we snapshot
10011005
// the worker environments.
@@ -1005,6 +1009,17 @@ struct SnapshotData {
10051009
// read only space. We use native_module::CodeCacheInfo because
10061010
// v8::ScriptCompiler::CachedData is not copyable.
10071011
std::vector<native_module::CodeCacheInfo> code_cache;
1012+
1013+
static std::unique_ptr<SnapshotData> New();
1014+
~SnapshotData();
1015+
1016+
SnapshotData(const SnapshotData&) = delete;
1017+
SnapshotData& operator=(const SnapshotData&) = delete;
1018+
SnapshotData(SnapshotData&&) = delete;
1019+
SnapshotData& operator=(SnapshotData&&) = delete;
1020+
1021+
// Only invoked by New().
1022+
SnapshotData() = default;
10081023
};
10091024

10101025
class Environment : public MemoryRetainer {

src/node_snapshot_builder.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ struct SnapshotData;
1515

1616
class NODE_EXTERN_PRIVATE SnapshotBuilder {
1717
public:
18-
static std::string Generate(const std::vector<std::string> args,
19-
const std::vector<std::string> exec_args);
18+
static int Generate(std::ostream& out,
19+
const std::vector<std::string> args,
20+
const std::vector<std::string> exec_args);
2021

2122
// Generate the snapshot into out.
22-
static void Generate(SnapshotData* out,
23-
const std::vector<std::string> args,
24-
const std::vector<std::string> exec_args);
23+
static int Generate(SnapshotData* out,
24+
const std::vector<std::string> args,
25+
const std::vector<std::string> exec_args);
2526

2627
// If nullptr is returned, the binary is not built with embedded
2728
// snapshot.

0 commit comments

Comments
 (0)