Skip to content

Commit

Permalink
src: use std::vector<size_t> instead of IndexArray
Browse files Browse the repository at this point in the history
PR-URL: #27321
Refs: #17058
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
joyeecheung authored and targos committed Apr 27, 2019
1 parent 228127f commit ad42cd6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 63 deletions.
7 changes: 3 additions & 4 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ std::vector<size_t> IsolateData::Serialize(SnapshotCreator* creator) {
return indexes;
}

void IsolateData::DeserializeProperties(
const NodeMainInstance::IndexArray* indexes) {
void IsolateData::DeserializeProperties(const std::vector<size_t>* indexes) {
size_t i = 0;
HandleScope handle_scope(isolate_);

Expand All @@ -86,7 +85,7 @@ void IsolateData::DeserializeProperties(
#define V(TypeName, PropertyName) \
do { \
MaybeLocal<TypeName> field = \
isolate_->GetDataFromSnapshotOnce<TypeName>(indexes->Get(i++)); \
isolate_->GetDataFromSnapshotOnce<TypeName>((*indexes)[i++]); \
if (field.IsEmpty()) { \
fprintf(stderr, "Failed to deserialize " #PropertyName "\n"); \
} \
Expand Down Expand Up @@ -155,7 +154,7 @@ IsolateData::IsolateData(Isolate* isolate,
uv_loop_t* event_loop,
MultiIsolatePlatform* platform,
ArrayBufferAllocator* node_allocator,
const NodeMainInstance::IndexArray* indexes)
const std::vector<size_t>* indexes)
: isolate_(isolate),
event_loop_(event_loop),
allocator_(isolate->GetArrayBufferAllocator()),
Expand Down
4 changes: 2 additions & 2 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ class IsolateData : public MemoryRetainer {
uv_loop_t* event_loop,
MultiIsolatePlatform* platform = nullptr,
ArrayBufferAllocator* node_allocator = nullptr,
const NodeMainInstance::IndexArray* indexes = nullptr);
const std::vector<size_t>* indexes = nullptr);
SET_MEMORY_INFO_NAME(IsolateData);
SET_SELF_SIZE(IsolateData);
void MemoryInfo(MemoryTracker* tracker) const override;
Expand Down Expand Up @@ -454,7 +454,7 @@ class IsolateData : public MemoryRetainer {
IsolateData& operator=(const IsolateData&) = delete;

private:
void DeserializeProperties(const NodeMainInstance::IndexArray* indexes);
void DeserializeProperties(const std::vector<size_t>* indexes);
void CreateProperties();

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
Expand Down
2 changes: 1 addition & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ int Start(int argc, char** argv) {
std::vector<intptr_t> external_references = {
reinterpret_cast<intptr_t>(nullptr)};
v8::StartupData* blob = NodeMainInstance::GetEmbeddedSnapshotBlob();
const NodeMainInstance::IndexArray* indexes =
const std::vector<size_t>* indexes =
NodeMainInstance::GetIsolateDataIndexes();
if (blob != nullptr) {
params.external_references = external_references.data();
Expand Down
13 changes: 7 additions & 6 deletions src/node_main_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ NodeMainInstance* NodeMainInstance::Create(
return new NodeMainInstance(isolate, event_loop, platform, args, exec_args);
}

NodeMainInstance::NodeMainInstance(Isolate::CreateParams* params,
uv_loop_t* event_loop,
MultiIsolatePlatform* platform,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const IndexArray* per_isolate_data_indexes)
NodeMainInstance::NodeMainInstance(
Isolate::CreateParams* params,
uv_loop_t* event_loop,
MultiIsolatePlatform* platform,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<size_t>* per_isolate_data_indexes)
: args_(args),
exec_args_(exec_args),
array_buffer_allocator_(ArrayBufferAllocator::Create()),
Expand Down
27 changes: 8 additions & 19 deletions src/node_main_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ namespace node {
// We may be able to create an abstract class to reuse some of the routines.
class NodeMainInstance {
public:
// An array of indexes that can be used to deserialize data from a V8
// snapshot.
struct IndexArray {
const size_t* data;
size_t length;

size_t Get(size_t index) const {
DCHECK_LT(index, length);
return data[index];
}
};

// To create a main instance that does not own the isoalte,
// The caller needs to do:
//
Expand All @@ -53,12 +41,13 @@ class NodeMainInstance {
void Dispose();

// Create a main instance that owns the isolate
NodeMainInstance(v8::Isolate::CreateParams* params,
uv_loop_t* event_loop,
MultiIsolatePlatform* platform,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const IndexArray* per_isolate_data_indexes = nullptr);
NodeMainInstance(
v8::Isolate::CreateParams* params,
uv_loop_t* event_loop,
MultiIsolatePlatform* platform,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<size_t>* per_isolate_data_indexes = nullptr);
~NodeMainInstance();

// Start running the Node.js instances, return the exit code when finished.
Expand All @@ -72,7 +61,7 @@ class NodeMainInstance {

// If nullptr is returned, the binary is not built with embedded
// snapshot.
static const IndexArray* GetIsolateDataIndexes();
static const std::vector<size_t>* GetIsolateDataIndexes();
static v8::StartupData* GetEmbeddedSnapshotBlob();

static const size_t kNodeContextIndex = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/node_snapshot_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ v8::StartupData* NodeMainInstance::GetEmbeddedSnapshotBlob() {
return nullptr;
}

const NodeMainInstance::IndexArray* NodeMainInstance::GetIsolateDataIndexes() {
const std::vector<size_t>* NodeMainInstance::GetIsolateDataIndexes() {
return nullptr;
}

Expand Down
45 changes: 15 additions & 30 deletions tools/snapshot/snapshot_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ using v8::Locker;
using v8::SnapshotCreator;
using v8::StartupData;

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(v8::StartupData* blob,
const std::vector<size_t>& isolate_data_indexes) {
std::stringstream ss;
size_t isolate_data_indexes_size = isolate_data_indexes.size();

ss << R"(#include <cstddef>
#include "node_main_instance.h"
Expand All @@ -29,47 +35,26 @@ std::string FormatBlob(v8::StartupData* blob,
namespace node {
static const uint8_t blob_data[] = {
static const char blob_data[] = {
)";

for (int i = 0; i < blob->raw_size; i++) {
uint8_t ch = blob->data[i];
ss << std::to_string(ch) << ((i == blob->raw_size - 1) ? '\n' : ',');
}

WriteVector(&ss, blob->data, blob->raw_size);
ss << R"(};
static const int blob_size = )"
<< blob->raw_size << R"(;
static v8::StartupData blob = {
reinterpret_cast<const char*>(blob_data),
blob_size
};
static v8::StartupData blob = { blob_data, blob_size };
)";

ss << R"(v8::StartupData*
NodeMainInstance::GetEmbeddedSnapshotBlob() {
ss << R"(v8::StartupData* NodeMainInstance::GetEmbeddedSnapshotBlob() {
return &blob;
}
static const size_t isolate_data_indexes_raw[] = {
static const std::vector<size_t> isolate_data_indexes {
)";
for (size_t i = 0; i < isolate_data_indexes_size; i++) {
ss << std::to_string(isolate_data_indexes[i])
<< ((i == isolate_data_indexes_size - 1) ? '\n' : ',');
}
ss << "};\n\n";

ss << "static const size_t isolate_data_indexes_size = "
<< isolate_data_indexes_size << R"(;
NodeMainInstance::IndexArray isolate_data_indexes {
isolate_data_indexes_raw,
isolate_data_indexes_size
};
WriteVector(&ss, isolate_data_indexes.data(), isolate_data_indexes.size());
ss << R"(};
const NodeMainInstance::IndexArray*
NodeMainInstance::GetIsolateDataIndexes() {
const std::vector<size_t>* NodeMainInstance::GetIsolateDataIndexes() {
return &isolate_data_indexes;
}
} // namespace node
Expand Down

0 comments on commit ad42cd6

Please sign in to comment.