Skip to content

Commit 94335ab

Browse files
committed
src: add an ExternalReferenceRegistry class
1 parent 8ebf40e commit 94335ab

File tree

7 files changed

+74
-4
lines changed

7 files changed

+74
-4
lines changed

node.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@
579579
'src/node_domain.cc',
580580
'src/node_env_var.cc',
581581
'src/node_errors.cc',
582+
'src/node_external_reference.cc',
582583
'src/node_file.cc',
583584
'src/node_http_parser.cc',
584585
'src/node_http2.cc',
@@ -668,6 +669,7 @@
668669
'src/node_contextify.h',
669670
'src/node_dir.h',
670671
'src/node_errors.h',
672+
'src/node_external_reference.h',
671673
'src/node_file.h',
672674
'src/node_file-inl.h',
673675
'src/node_http_common.h',

src/node.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ int Start(int argc, char** argv) {
10381038
if (blob != nullptr) {
10391039
// TODO(joyeecheung): collect external references and set it in
10401040
// params.external_references.
1041+
external_references = NodeMainInstance::CollectExternalReferences();
10411042
external_references.push_back(reinterpret_cast<intptr_t>(nullptr));
10421043
params.external_references = external_references.data();
10431044
params.snapshot_blob = blob;

src/node_external_reference.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "node_external_reference.h"
2+
#include <cinttypes>
3+
#include <vector>
4+
#include "util.h"
5+
6+
namespace node {
7+
8+
const std::vector<intptr_t>& ExternalReferenceRegistry::external_references() {
9+
CHECK(!is_finalized_);
10+
external_references_.push_back(reinterpret_cast<intptr_t>(nullptr));
11+
is_finalized_ = true;
12+
return external_references_;
13+
}
14+
15+
} // namespace node

src/node_external_reference.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef SRC_NODE_EXTERNAL_REFERENCE_H_
2+
#define SRC_NODE_EXTERNAL_REFERENCE_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include <cinttypes>
7+
#include <vector>
8+
9+
namespace node {
10+
11+
// This class manages the external references from the V8 heap
12+
// to the C++ addresses in Node.js.
13+
class ExternalReferenceRegistry {
14+
public:
15+
ExternalReferenceRegistry() {}
16+
17+
template <typename T>
18+
void Register(T* address) {
19+
external_references_.push_back(reinterpret_cast<intptr_t>(address));
20+
}
21+
22+
// This can be called only once.
23+
const std::vector<intptr_t>& external_references();
24+
25+
bool is_empty() { return external_references_.empty(); }
26+
27+
private:
28+
bool is_finalized_ = false;
29+
std::vector<intptr_t> external_references_;
30+
};
31+
32+
} // namespace node
33+
34+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
35+
#endif // SRC_NODE_EXTERNAL_REFERENCE_H_

src/node_main_instance.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <memory>
22

33
#include "node_main_instance.h"
4+
#include "node_errors.h"
5+
#include "node_external_reference.h"
46
#include "node_internals.h"
57
#include "node_options-inl.h"
68
#include "node_v8_platform-inl.h"
@@ -23,6 +25,8 @@ using v8::Locker;
2325
using v8::Object;
2426
using v8::SealHandleScope;
2527

28+
std::unique_ptr<ExternalReferenceRegistry> NodeMainInstance::registry_ =
29+
nullptr;
2630
NodeMainInstance::NodeMainInstance(Isolate* isolate,
2731
uv_loop_t* event_loop,
2832
MultiIsolatePlatform* platform,
@@ -43,6 +47,15 @@ NodeMainInstance::NodeMainInstance(Isolate* isolate,
4347
SetIsolateMiscHandlers(isolate_, misc);
4448
}
4549

50+
const std::vector<intptr_t>& NodeMainInstance::CollectExternalReferences() {
51+
// Cannot be called more than once.
52+
CHECK_NULL(registry_);
53+
registry_.reset(new ExternalReferenceRegistry());
54+
55+
// TODO(joyeecheung): collect more external references here.
56+
return registry_->external_references();
57+
}
58+
4659
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
4760
Isolate* isolate,
4861
uv_loop_t* event_loop,

src/node_main_instance.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace node {
1515

16+
class ExternalReferenceRegistry;
17+
1618
// TODO(joyeecheung): align this with the Worker/WorkerThreadData class.
1719
// We may be able to create an abstract class to reuse some of the routines.
1820
class NodeMainInstance {
@@ -66,6 +68,7 @@ class NodeMainInstance {
6668
// snapshot.
6769
static const std::vector<size_t>* GetIsolateDataIndexes();
6870
static v8::StartupData* GetEmbeddedSnapshotBlob();
71+
static const std::vector<intptr_t>& CollectExternalReferences();
6972

7073
static const size_t kNodeContextIndex = 0;
7174
NodeMainInstance(const NodeMainInstance&) = delete;
@@ -80,6 +83,7 @@ class NodeMainInstance {
8083
const std::vector<std::string>& args,
8184
const std::vector<std::string>& exec_args);
8285

86+
static std::unique_ptr<ExternalReferenceRegistry> registry_;
8387
std::vector<std::string> args_;
8488
std::vector<std::string> exec_args_;
8589
std::unique_ptr<ArrayBufferAllocator> array_buffer_allocator_;

tools/snapshot/snapshot_builder.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "snapshot_builder.h"
22
#include <iostream>
33
#include <sstream>
4+
#include "env-inl.h"
5+
#include "node_external_reference.h"
46
#include "node_internals.h"
57
#include "node_main_instance.h"
68
#include "node_v8_platform-inl.h"
@@ -63,10 +65,8 @@ const std::vector<size_t>* NodeMainInstance::GetIsolateDataIndexes() {
6365
std::string SnapshotBuilder::Generate(
6466
const std::vector<std::string> args,
6567
const std::vector<std::string> exec_args) {
66-
// TODO(joyeecheung): collect external references and set it in
67-
// params.external_references.
68-
std::vector<intptr_t> external_references = {
69-
reinterpret_cast<intptr_t>(nullptr)};
68+
const std::vector<intptr_t>& external_references =
69+
NodeMainInstance::CollectExternalReferences();
7070
Isolate* isolate = Isolate::Allocate();
7171
per_process::v8_platform.Platform()->RegisterIsolate(isolate,
7272
uv_default_loop());

0 commit comments

Comments
 (0)