From 5590b7b9bc1e9c8ff277f3d0db01009b29bb17dd Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 29 Jan 2021 00:04:22 +0800 Subject: [PATCH] src: put (de)serialization code into node_snapshotable.h/cc So that it's easier to find the corresponding code. --- node.gyp | 2 ++ src/node_main_instance.cc | 14 +---------- src/node_snapshotable.cc | 39 ++++++++++++++++++++++++++++++ src/node_snapshotable.h | 21 ++++++++++++++++ tools/snapshot/snapshot_builder.cc | 18 +------------- 5 files changed, 64 insertions(+), 30 deletions(-) create mode 100644 src/node_snapshotable.cc create mode 100644 src/node_snapshotable.h diff --git a/node.gyp b/node.gyp index a6ddb4e6dc4f56..d2dae2f24abb4c 100644 --- a/node.gyp +++ b/node.gyp @@ -641,6 +641,7 @@ 'src/node_report_module.cc', 'src/node_report_utils.cc', 'src/node_serdes.cc', + 'src/node_snapshotable.cc', 'src/node_sockaddr.cc', 'src/node_stat_watcher.cc', 'src/node_symbols.cc', @@ -743,6 +744,7 @@ 'src/node_report.h', 'src/node_revert.h', 'src/node_root_certs.h', + 'src/node_snapshotable.h', 'src/node_sockaddr.h', 'src/node_sockaddr-inl.h', 'src/node_stat_watcher.h', diff --git a/src/node_main_instance.cc b/src/node_main_instance.cc index 07277ccfa5c167..208d20f4464b46 100644 --- a/src/node_main_instance.cc +++ b/src/node_main_instance.cc @@ -5,6 +5,7 @@ #include "node_external_reference.h" #include "node_internals.h" #include "node_options-inl.h" +#include "node_snapshotable.h" #include "node_v8_platform-inl.h" #include "util-inl.h" #if defined(LEAK_SANITIZER) @@ -22,7 +23,6 @@ using v8::HandleScope; using v8::Isolate; using v8::Local; using v8::Locker; -using v8::Object; std::unique_ptr NodeMainInstance::registry_ = nullptr; @@ -167,18 +167,6 @@ int NodeMainInstance::Run(const EnvSerializeInfo* env_info) { return exit_code; } -void DeserializeNodeInternalFields(Local holder, - int index, - v8::StartupData payload, - void* env) { - if (payload.raw_size == 0) { - holder->SetAlignedPointerInInternalField(index, nullptr); - return; - } - // No embedder object in the builtin snapshot yet. - UNREACHABLE(); -} - DeleteFnPtr NodeMainInstance::CreateMainEnvironment(int* exit_code, const EnvSerializeInfo* env_info) { diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc new file mode 100644 index 00000000000000..21de10868564d3 --- /dev/null +++ b/src/node_snapshotable.cc @@ -0,0 +1,39 @@ + +#include "node_snapshotable.h" +#include "base_object-inl.h" + +namespace node { + +using v8::Local; +using v8::Object; +using v8::StartupData; + +void DeserializeNodeInternalFields(Local holder, + int index, + v8::StartupData payload, + void* env) { + if (payload.raw_size == 0) { + holder->SetAlignedPointerInInternalField(index, nullptr); + return; + } + // No embedder object in the builtin snapshot yet. + UNREACHABLE(); +} + +StartupData SerializeNodeContextInternalFields(Local holder, + int index, + void* env) { + void* ptr = holder->GetAlignedPointerFromInternalField(index); + if (ptr == nullptr || ptr == env) { + return StartupData{nullptr, 0}; + } + if (ptr == env && index == ContextEmbedderIndex::kEnvironment) { + return StartupData{nullptr, 0}; + } + + // No embedder objects in the builtin snapshot yet. + UNREACHABLE(); + return StartupData{nullptr, 0}; +} + +} // namespace node diff --git a/src/node_snapshotable.h b/src/node_snapshotable.h new file mode 100644 index 00000000000000..c45c1381b5554f --- /dev/null +++ b/src/node_snapshotable.h @@ -0,0 +1,21 @@ + +#ifndef SRC_NODE_SNAPSHOTABLE_H_ +#define SRC_NODE_SNAPSHOTABLE_H_ + +#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#include "v8.h" +namespace node { + +v8::StartupData SerializeNodeContextInternalFields(v8::Local holder, + int index, + void* env); +void DeserializeNodeInternalFields(v8::Local holder, + int index, + v8::StartupData payload, + void* env); +} // namespace node + +#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS + +#endif // SRC_NODE_SNAPSHOTABLE_H_ diff --git a/tools/snapshot/snapshot_builder.cc b/tools/snapshot/snapshot_builder.cc index eb755b7d29ed7c..f97c646dfa17c4 100644 --- a/tools/snapshot/snapshot_builder.cc +++ b/tools/snapshot/snapshot_builder.cc @@ -6,6 +6,7 @@ #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 { @@ -14,7 +15,6 @@ using v8::Context; using v8::HandleScope; using v8::Isolate; using v8::Local; -using v8::Object; using v8::SnapshotCreator; using v8::StartupData; @@ -75,22 +75,6 @@ const EnvSerializeInfo* NodeMainInstance::GetEnvSerializeInfo() { return ss.str(); } -static StartupData SerializeNodeContextInternalFields(Local holder, - int index, - void* env) { - void* ptr = holder->GetAlignedPointerFromInternalField(index); - if (ptr == nullptr || ptr == env) { - return StartupData{nullptr, 0}; - } - if (ptr == env && index == ContextEmbedderIndex::kEnvironment) { - return StartupData{nullptr, 0}; - } - - // No embedder objects in the builtin snapshot yet. - UNREACHABLE(); - return StartupData{nullptr, 0}; -} - std::string SnapshotBuilder::Generate( const std::vector args, const std::vector exec_args) {