Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2021-05-26 19:42:00 +0300
committerMichaƫl Zasso <targos@protonmail.com>2021-06-11 08:24:59 +0300
commit9bf9ddb49044762e75ab013f89830d9f65ed8d3f (patch)
tree593cf4fdf21117c420ad18f2660429cb5cd4e5cb /tools
parent711916a271f63299bba9f6289ac79a23e0d8ce38 (diff)
tools: refactor snapshot builder
This patch: - Moves the snapshot building code to src/ so that we can reuse it later when generating custom snapshots from an entry point accepted by the node binary. - Create a SnapshotData struct that incorporates all the data useful for a snapshot blob, including both the V8 data and the Node.js data. PR-URL: https://github.com/nodejs/node/pull/38902 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/snapshot/README.md2
-rw-r--r--tools/snapshot/node_mksnapshot.cc2
-rw-r--r--tools/snapshot/snapshot_builder.cc172
-rw-r--r--tools/snapshot/snapshot_builder.h15
4 files changed, 2 insertions, 189 deletions
diff --git a/tools/snapshot/README.md b/tools/snapshot/README.md
index 34dc574d56c..fb22c03ed50 100644
--- a/tools/snapshot/README.md
+++ b/tools/snapshot/README.md
@@ -23,7 +23,7 @@ into the Node.js executable, `libnode` is first built with these unresolved
symbols:
- `node::NodeMainInstance::GetEmbeddedSnapshotBlob`
-- `node::NodeMainInstance::GetIsolateDataIndexes`
+- `node::NodeMainInstance::GetIsolateDataIndices`
Then the `node_mksnapshot` executable is built with C++ files in this
directory, as well as `src/node_snapshot_stub.cc` which defines the unresolved
diff --git a/tools/snapshot/node_mksnapshot.cc b/tools/snapshot/node_mksnapshot.cc
index c5bfcd8fc5c..e591f64a2a0 100644
--- a/tools/snapshot/node_mksnapshot.cc
+++ b/tools/snapshot/node_mksnapshot.cc
@@ -7,7 +7,7 @@
#include "libplatform/libplatform.h"
#include "node_internals.h"
-#include "snapshot_builder.h"
+#include "node_snapshotable.h"
#include "util-inl.h"
#include "v8.h"
diff --git a/tools/snapshot/snapshot_builder.cc b/tools/snapshot/snapshot_builder.cc
deleted file mode 100644
index cf76f38ece8..00000000000
--- a/tools/snapshot/snapshot_builder.cc
+++ /dev/null
@@ -1,172 +0,0 @@
-#include "snapshot_builder.h"
-#include <iostream>
-#include <sstream>
-#include "debug_utils-inl.h"
-#include "env-inl.h"
-#include "node_errors.h"
-#include "node_external_reference.h"
-#include "node_internals.h"
-#include "node_main_instance.h"
-#include "node_snapshotable.h"
-#include "node_v8_platform-inl.h"
-
-namespace node {
-
-using v8::Context;
-using v8::HandleScope;
-using v8::Isolate;
-using v8::Local;
-using v8::SnapshotCreator;
-using v8::StartupData;
-using v8::TryCatch;
-using v8::Value;
-
-template <typename T>
-void WriteVector(std::stringstream* ss, const T* vec, size_t size) {
- for (size_t i = 0; i < size; i++) {
- *ss << std::to_string(vec[i]) << (i == size - 1 ? '\n' : ',');
- }
-}
-
-std::string FormatBlob(StartupData* blob,
- const std::vector<size_t>& isolate_data_indexes,
- const EnvSerializeInfo& env_info) {
- std::stringstream ss;
-
- ss << R"(#include <cstddef>
-#include "env.h"
-#include "node_main_instance.h"
-#include "v8.h"
-
-// This file is generated by tools/snapshot. Do not edit.
-
-namespace node {
-
-static const char blob_data[] = {
-)";
- WriteVector(&ss, blob->data, blob->raw_size);
- ss << R"(};
-
-static const int blob_size = )"
- << blob->raw_size << R"(;
-static v8::StartupData blob = { blob_data, blob_size };
-)";
-
- ss << R"(v8::StartupData* NodeMainInstance::GetEmbeddedSnapshotBlob() {
- return &blob;
-}
-
-static const std::vector<size_t> isolate_data_indexes {
-)";
- WriteVector(&ss, isolate_data_indexes.data(), isolate_data_indexes.size());
- ss << R"(};
-
-const std::vector<size_t>* NodeMainInstance::GetIsolateDataIndexes() {
- return &isolate_data_indexes;
-}
-
-static const EnvSerializeInfo env_info )"
- << env_info << R"(;
-
-const EnvSerializeInfo* NodeMainInstance::GetEnvSerializeInfo() {
- return &env_info;
-}
-
-} // namespace node
-)";
-
- return ss.str();
-}
-
-std::string SnapshotBuilder::Generate(
- const std::vector<std::string> args,
- const std::vector<std::string> exec_args) {
- Isolate* isolate = Isolate::Allocate();
- isolate->SetCaptureStackTraceForUncaughtExceptions(
- true,
- 10,
- v8::StackTrace::StackTraceOptions::kDetailed);
- per_process::v8_platform.Platform()->RegisterIsolate(isolate,
- uv_default_loop());
- std::unique_ptr<NodeMainInstance> main_instance;
- std::string result;
-
- {
- std::vector<size_t> isolate_data_indexes;
- EnvSerializeInfo env_info;
-
- const std::vector<intptr_t>& external_references =
- NodeMainInstance::CollectExternalReferences();
- SnapshotCreator creator(isolate, external_references.data());
- Environment* env;
- {
- main_instance =
- NodeMainInstance::Create(isolate,
- uv_default_loop(),
- per_process::v8_platform.Platform(),
- args,
- exec_args);
-
- HandleScope scope(isolate);
- creator.SetDefaultContext(Context::New(isolate));
- isolate_data_indexes = main_instance->isolate_data()->Serialize(&creator);
-
- // Run the per-context scripts
- Local<Context> context;
- {
- TryCatch bootstrapCatch(isolate);
- context = NewContext(isolate);
- if (bootstrapCatch.HasCaught()) {
- PrintCaughtException(isolate, context, bootstrapCatch);
- abort();
- }
- }
- Context::Scope context_scope(context);
-
- // Create the environment
- env = new Environment(main_instance->isolate_data(),
- context,
- args,
- exec_args,
- nullptr,
- node::EnvironmentFlags::kDefaultFlags,
- {});
- // Run scripts in lib/internal/bootstrap/
- {
- TryCatch bootstrapCatch(isolate);
- v8::MaybeLocal<Value> result = env->RunBootstrapping();
- if (bootstrapCatch.HasCaught()) {
- PrintCaughtException(isolate, context, bootstrapCatch);
- }
- result.ToLocalChecked();
- }
-
- if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
- env->PrintAllBaseObjects();
- printf("Environment = %p\n", env);
- }
-
- // Serialize the native states
- env_info = env->Serialize(&creator);
- // Serialize the context
- size_t index = creator.AddContext(
- context, {SerializeNodeContextInternalFields, env});
- CHECK_EQ(index, NodeMainInstance::kNodeContextIndex);
- }
-
- // Must be out of HandleScope
- StartupData blob =
- creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kClear);
- CHECK(blob.CanBeRehashed());
- // Must be done while the snapshot creator isolate is entered i.e. the
- // creator is still alive.
- FreeEnvironment(env);
- main_instance->Dispose();
- result = FormatBlob(&blob, isolate_data_indexes, env_info);
- delete[] blob.data;
- }
-
- per_process::v8_platform.Platform()->UnregisterIsolate(isolate);
- return result;
-}
-} // namespace node
diff --git a/tools/snapshot/snapshot_builder.h b/tools/snapshot/snapshot_builder.h
deleted file mode 100644
index 2e587d078b9..00000000000
--- a/tools/snapshot/snapshot_builder.h
+++ /dev/null
@@ -1,15 +0,0 @@
-#ifndef TOOLS_SNAPSHOT_SNAPSHOT_BUILDER_H_
-#define TOOLS_SNAPSHOT_SNAPSHOT_BUILDER_H_
-
-#include <string>
-#include <vector>
-
-namespace node {
-class SnapshotBuilder {
- public:
- static std::string Generate(const std::vector<std::string> args,
- const std::vector<std::string> exec_args);
-};
-} // namespace node
-
-#endif // TOOLS_SNAPSHOT_SNAPSHOT_BUILDER_H_