3
3
#include " env-inl.h"
4
4
#include " node_external_reference.h"
5
5
#include " node_internals.h"
6
+ #include " node_threadsafe_cow-inl.h"
6
7
#include " simdutf.h"
7
8
#include " util-inl.h"
8
9
@@ -53,24 +54,22 @@ BuiltinLoader::BuiltinLoader() : config_(GetConfig()), has_code_cache_(false) {
53
54
}
54
55
55
56
bool BuiltinLoader::Exists (const char * id) {
56
- RwLock::ScopedReadLock lock (source_mutex_ );
57
- return source_. find (id) != source_. end ();
57
+ auto source = source_. read ( );
58
+ return source-> find (id) != source-> end ();
58
59
}
59
60
60
61
bool BuiltinLoader::Add (const char * id, const UnionBytes& source) {
61
- RwLock::ScopedLock source_lock (source_mutex_);
62
- Mutex::ScopedLock categories_lock (builtin_categories_mutex_);
63
62
builtin_categories_
64
63
.reset (); // The category cache is reset by adding new sources
65
- auto result = source_.emplace (id, source);
64
+ auto result = source_.write ()-> emplace (id, source);
66
65
return result.second ;
67
66
}
68
67
69
68
Local<Object> BuiltinLoader::GetSourceObject (Local<Context> context) {
70
- RwLock::ScopedReadLock lock (source_mutex_);
71
69
Isolate* isolate = context->GetIsolate ();
72
70
Local<Object> out = Object::New (isolate);
73
- for (auto const & x : source_) {
71
+ auto source = source_.read ();
72
+ for (auto const & x : *source) {
74
73
Local<String> key = OneByteString (isolate, x.first .c_str (), x.first .size ());
75
74
out->Set (context, key, x.second .ToStringChecked (isolate)).FromJust ();
76
75
}
@@ -82,18 +81,17 @@ Local<String> BuiltinLoader::GetConfigString(Isolate* isolate) {
82
81
}
83
82
84
83
std::vector<std::string> BuiltinLoader::GetBuiltinIds () const {
85
- RwLock::ScopedReadLock lock (source_mutex_);
86
84
std::vector<std::string> ids;
87
- ids.reserve (source_.size ());
88
- for (auto const & x : source_) {
85
+ auto source = source_.read ();
86
+ ids.reserve (source->size ());
87
+ for (auto const & x : *source) {
89
88
ids.emplace_back (x.first );
90
89
}
91
90
return ids;
92
91
}
93
92
94
93
const BuiltinLoader::BuiltinCategories&
95
94
BuiltinLoader::InitializeBuiltinCategories () const {
96
- Mutex::ScopedLock lock (builtin_categories_mutex_);
97
95
if (LIKELY (builtin_categories_.has_value ())) {
98
96
DCHECK (!builtin_categories_.value ().can_be_required .empty ());
99
97
return builtin_categories_.value ();
@@ -138,7 +136,8 @@ BuiltinLoader::InitializeBuiltinCategories() const {
138
136
" internal/v8_prof_processor" ,
139
137
};
140
138
141
- for (auto const & x : source_) {
139
+ auto source = source_.read ();
140
+ for (auto const & x : *source) {
142
141
const std::string& id = x.first ;
143
142
for (auto const & prefix : prefixes) {
144
143
if (prefix.length () > id.length ()) {
@@ -151,7 +150,7 @@ BuiltinLoader::InitializeBuiltinCategories() const {
151
150
}
152
151
}
153
152
154
- for (auto const & x : source_ ) {
153
+ for (auto const & x : *source ) {
155
154
const std::string& id = x.first ;
156
155
if (0 == builtin_categories.cannot_be_required .count (id)) {
157
156
builtin_categories.can_be_required .emplace (id);
@@ -177,7 +176,8 @@ bool BuiltinLoader::CannotBeRequired(const char* id) const {
177
176
return GetCannotBeRequired ().count (id) == 1 ;
178
177
}
179
178
180
- ScriptCompiler::CachedData* BuiltinLoader::GetCodeCache (const char * id) const {
179
+ const ScriptCompiler::CachedData* BuiltinLoader::GetCodeCache (
180
+ const char * id) const {
181
181
RwLock::ScopedReadLock lock (code_cache_mutex_);
182
182
const auto it = code_cache_.find (id);
183
183
if (it == code_cache_.end ()) {
@@ -206,12 +206,12 @@ static std::string OnDiskFileName(const char* id) {
206
206
207
207
MaybeLocal<String> BuiltinLoader::LoadBuiltinSource (Isolate* isolate,
208
208
const char * id) const {
209
+ auto source = source_.read ();
209
210
#ifdef NODE_BUILTIN_MODULES_PATH
210
211
if (strncmp (id, " embedder_main_" , strlen (" embedder_main_" )) == 0 ) {
211
212
#endif // NODE_BUILTIN_MODULES_PATH
212
- RwLock::ScopedReadLock lock (source_mutex_);
213
- const auto source_it = source_.find (id);
214
- if (UNLIKELY (source_it == source_.end ())) {
213
+ const auto source_it = source->find (id);
214
+ if (UNLIKELY (source_it == source->end ())) {
215
215
fprintf (stderr, " Cannot find native builtin: \" %s\" .\n " , id);
216
216
ABORT ();
217
217
}
@@ -439,7 +439,7 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(Local<Context> context,
439
439
MaybeLocal<Function> maybe =
440
440
LookupAndCompileInternal (context, id, ¶meters, &result);
441
441
if (optional_realm != nullptr ) {
442
- DCHECK_EQ (this , optional_realm->env ()->builtin_loader (). get () );
442
+ DCHECK_EQ (this , optional_realm->env ()->builtin_loader ());
443
443
RecordResult (id, result, optional_realm);
444
444
}
445
445
return maybe;
@@ -535,7 +535,7 @@ bool BuiltinLoader::CompileAllBuiltins(Local<Context> context) {
535
535
return all_succeeded;
536
536
}
537
537
538
- void BuiltinLoader::CopyCodeCache (std::vector<CodeCacheInfo>* out) {
538
+ void BuiltinLoader::CopyCodeCache (std::vector<CodeCacheInfo>* out) const {
539
539
RwLock::ScopedReadLock lock (code_cache_mutex_);
540
540
for (auto const & item : code_cache_) {
541
541
out->push_back (
@@ -692,8 +692,19 @@ void BuiltinLoader::HasCachedBuiltins(const FunctionCallbackInfo<Value>& args) {
692
692
v8::Boolean::New (args.GetIsolate (), instance->has_code_cache_ ));
693
693
}
694
694
695
- std::shared_ptr<BuiltinLoader> BuiltinLoader::Create () {
696
- return std::shared_ptr<BuiltinLoader>{new BuiltinLoader ()};
695
+ std::unique_ptr<BuiltinLoader> BuiltinLoader::Create () {
696
+ return std::unique_ptr<BuiltinLoader>{new BuiltinLoader ()};
697
+ }
698
+
699
+ void BuiltinLoader::CopySourceAndCodeCacheFrom (const BuiltinLoader* other) {
700
+ {
701
+ std::vector<CodeCacheInfo> cache;
702
+ other->CopyCodeCache (&cache);
703
+ RefreshCodeCache (cache);
704
+ has_code_cache_ = other->has_code_cache_ ;
705
+ }
706
+
707
+ source_ = other->source_ ;
697
708
}
698
709
699
710
void BuiltinLoader::CreatePerIsolateProperties (IsolateData* isolate_data,
0 commit comments