33#include " env-inl.h"
44#include " node_external_reference.h"
55#include " node_internals.h"
6+ #include " node_threadsafe_cow-inl.h"
67#include " simdutf.h"
78#include " util-inl.h"
89
@@ -53,24 +54,22 @@ BuiltinLoader::BuiltinLoader() : config_(GetConfig()), has_code_cache_(false) {
5354}
5455
5556bool 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 ();
5859}
5960
6061bool BuiltinLoader::Add (const char * id, const UnionBytes& source) {
61- RwLock::ScopedLock source_lock (source_mutex_);
62- Mutex::ScopedLock categories_lock (builtin_categories_mutex_);
6362 builtin_categories_
6463 .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);
6665 return result.second ;
6766}
6867
6968Local<Object> BuiltinLoader::GetSourceObject (Local<Context> context) {
70- RwLock::ScopedReadLock lock (source_mutex_);
7169 Isolate* isolate = context->GetIsolate ();
7270 Local<Object> out = Object::New (isolate);
73- for (auto const & x : source_) {
71+ auto source = source_.read ();
72+ for (auto const & x : *source) {
7473 Local<String> key = OneByteString (isolate, x.first .c_str (), x.first .size ());
7574 out->Set (context, key, x.second .ToStringChecked (isolate)).FromJust ();
7675 }
@@ -82,18 +81,17 @@ Local<String> BuiltinLoader::GetConfigString(Isolate* isolate) {
8281}
8382
8483std::vector<std::string> BuiltinLoader::GetBuiltinIds () const {
85- RwLock::ScopedReadLock lock (source_mutex_);
8684 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) {
8988 ids.emplace_back (x.first );
9089 }
9190 return ids;
9291}
9392
9493const BuiltinLoader::BuiltinCategories&
9594BuiltinLoader::InitializeBuiltinCategories () const {
96- Mutex::ScopedLock lock (builtin_categories_mutex_);
9795 if (LIKELY (builtin_categories_.has_value ())) {
9896 DCHECK (!builtin_categories_.value ().can_be_required .empty ());
9997 return builtin_categories_.value ();
@@ -138,7 +136,8 @@ BuiltinLoader::InitializeBuiltinCategories() const {
138136 " internal/v8_prof_processor" ,
139137 };
140138
141- for (auto const & x : source_) {
139+ auto source = source_.read ();
140+ for (auto const & x : *source) {
142141 const std::string& id = x.first ;
143142 for (auto const & prefix : prefixes) {
144143 if (prefix.length () > id.length ()) {
@@ -151,7 +150,7 @@ BuiltinLoader::InitializeBuiltinCategories() const {
151150 }
152151 }
153152
154- for (auto const & x : source_ ) {
153+ for (auto const & x : *source ) {
155154 const std::string& id = x.first ;
156155 if (0 == builtin_categories.cannot_be_required .count (id)) {
157156 builtin_categories.can_be_required .emplace (id);
@@ -177,7 +176,8 @@ bool BuiltinLoader::CannotBeRequired(const char* id) const {
177176 return GetCannotBeRequired ().count (id) == 1 ;
178177}
179178
180- ScriptCompiler::CachedData* BuiltinLoader::GetCodeCache (const char * id) const {
179+ const ScriptCompiler::CachedData* BuiltinLoader::GetCodeCache (
180+ const char * id) const {
181181 RwLock::ScopedReadLock lock (code_cache_mutex_);
182182 const auto it = code_cache_.find (id);
183183 if (it == code_cache_.end ()) {
@@ -206,12 +206,12 @@ static std::string OnDiskFileName(const char* id) {
206206
207207MaybeLocal<String> BuiltinLoader::LoadBuiltinSource (Isolate* isolate,
208208 const char * id) const {
209+ auto source = source_.read ();
209210#ifdef NODE_BUILTIN_MODULES_PATH
210211 if (strncmp (id, " embedder_main_" , strlen (" embedder_main_" )) == 0 ) {
211212#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 ())) {
215215 fprintf (stderr, " Cannot find native builtin: \" %s\" .\n " , id);
216216 ABORT ();
217217 }
@@ -438,7 +438,7 @@ MaybeLocal<Function> BuiltinLoader::LookupAndCompile(Local<Context> context,
438438 MaybeLocal<Function> maybe =
439439 LookupAndCompileInternal (context, id, ¶meters, &result);
440440 if (optional_realm != nullptr ) {
441- DCHECK_EQ (this , optional_realm->env ()->builtin_loader (). get () );
441+ DCHECK_EQ (this , optional_realm->env ()->builtin_loader ());
442442 RecordResult (id, result, optional_realm);
443443 }
444444 return maybe;
@@ -534,7 +534,7 @@ bool BuiltinLoader::CompileAllBuiltins(Local<Context> context) {
534534 return all_succeeded;
535535}
536536
537- void BuiltinLoader::CopyCodeCache (std::vector<CodeCacheInfo>* out) {
537+ void BuiltinLoader::CopyCodeCache (std::vector<CodeCacheInfo>* out) const {
538538 RwLock::ScopedReadLock lock (code_cache_mutex_);
539539 for (auto const & item : code_cache_) {
540540 out->push_back (
@@ -691,8 +691,19 @@ void BuiltinLoader::HasCachedBuiltins(const FunctionCallbackInfo<Value>& args) {
691691 v8::Boolean::New (args.GetIsolate (), instance->has_code_cache_ ));
692692}
693693
694- std::shared_ptr<BuiltinLoader> BuiltinLoader::Create () {
695- return std::shared_ptr<BuiltinLoader>{new BuiltinLoader ()};
694+ std::unique_ptr<BuiltinLoader> BuiltinLoader::Create () {
695+ return std::unique_ptr<BuiltinLoader>{new BuiltinLoader ()};
696+ }
697+
698+ void BuiltinLoader::CopySourceAndCodeCacheFrom (const BuiltinLoader* other) {
699+ {
700+ std::vector<CodeCacheInfo> cache;
701+ other->CopyCodeCache (&cache);
702+ RefreshCodeCache (cache);
703+ has_code_cache_ = other->has_code_cache_ ;
704+ }
705+
706+ source_ = other->source_ ;
696707}
697708
698709void BuiltinLoader::CreatePerIsolateProperties (IsolateData* isolate_data,
0 commit comments