Skip to content

Commit

Permalink
src: put (de)serialization code into node_snapshotable.h/cc
Browse files Browse the repository at this point in the history
So that it's easier to find the corresponding code.

PR-URL: #37114
Refs: #36943
Reviewed-By: Juan José Arboleda <[email protected]>
  • Loading branch information
joyeecheung authored and danielleadams committed Feb 16, 2021
1 parent 2a5f67b commit 337b4e7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 30 deletions.
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,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',
Expand Down Expand Up @@ -738,6 +739,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',
Expand Down
14 changes: 1 addition & 13 deletions src/node_main_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -22,7 +23,6 @@ using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Locker;
using v8::Object;

std::unique_ptr<ExternalReferenceRegistry> NodeMainInstance::registry_ =
nullptr;
Expand Down Expand Up @@ -167,18 +167,6 @@ int NodeMainInstance::Run(const EnvSerializeInfo* env_info) {
return exit_code;
}

void DeserializeNodeInternalFields(Local<Object> 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<Environment, FreeEnvironment>
NodeMainInstance::CreateMainEnvironment(int* exit_code,
const EnvSerializeInfo* env_info) {
Expand Down
39 changes: 39 additions & 0 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
@@ -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<Object> 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<Object> 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
21 changes: 21 additions & 0 deletions src/node_snapshotable.h
Original file line number Diff line number Diff line change
@@ -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<v8::Object> holder,
int index,
void* env);
void DeserializeNodeInternalFields(v8::Local<v8::Object> holder,
int index,
v8::StartupData payload,
void* env);
} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif // SRC_NODE_SNAPSHOTABLE_H_
18 changes: 1 addition & 17 deletions tools/snapshot/snapshot_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand Down Expand Up @@ -75,22 +75,6 @@ const EnvSerializeInfo* NodeMainInstance::GetEnvSerializeInfo() {
return ss.str();
}

static StartupData SerializeNodeContextInternalFields(Local<Object> 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<std::string> args,
const std::vector<std::string> exec_args) {
Expand Down

0 comments on commit 337b4e7

Please sign in to comment.