@@ -112,6 +112,7 @@ NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) {
112
112
void NativeModuleLoader::GetCodeCache (const FunctionCallbackInfo<Value>& args) {
113
113
Environment* env = Environment::GetCurrent (args);
114
114
Isolate* isolate = env->isolate ();
115
+ CHECK (env->is_main_thread ());
115
116
116
117
CHECK (args[0 ]->IsString ());
117
118
node::Utf8Value id_v (isolate, args[0 ].As <String>());
@@ -133,15 +134,13 @@ MaybeLocal<Uint8Array> NativeModuleLoader::GetCodeCache(Isolate* isolate,
133
134
134
135
ScriptCompiler::CachedData* cached_data = nullptr ;
135
136
const auto it = code_cache_.find (id);
136
- if (it != code_cache_.end ()) {
137
- cached_data = it->second .get ();
138
- }
139
-
140
- // The module has not been compiled before.
141
- if (cached_data == nullptr ) {
137
+ if (it == code_cache_.end ()) {
138
+ // The module has not been compiled before.
142
139
return MaybeLocal<Uint8Array>();
143
140
}
144
141
142
+ cached_data = it->second .get ();
143
+
145
144
MallocedBuffer<uint8_t > copied (cached_data->length );
146
145
memcpy (copied.data , cached_data->data , cached_data->length );
147
146
Local<ArrayBuffer> buf =
@@ -257,7 +256,7 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
257
256
Local<Function> fun = maybe_fun.ToLocalChecked ();
258
257
// XXX(joyeecheung): this bookkeeping is not exactly accurate because
259
258
// it only starts after the Environment is created, so the per_context.js
260
- // will never be any of these two sets, but the two sets are only for
259
+ // will never be in any of these two sets, but the two sets are only for
261
260
// testing anyway.
262
261
if (use_cache) {
263
262
if (optional_env != nullptr ) {
@@ -276,12 +275,12 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
276
275
}
277
276
278
277
// Generate new cache for next compilation
279
- ScriptCompiler::CachedData* new_cached_data =
280
- ScriptCompiler::CreateCodeCacheForFunction (fun);
278
+ std::unique_ptr< ScriptCompiler::CachedData> new_cached_data (
279
+ ScriptCompiler::CreateCodeCacheForFunction (fun)) ;
281
280
CHECK_NE (new_cached_data, nullptr );
282
281
283
282
// The old entry should've been erased by now so we can just emplace
284
- code_cache_.emplace (id, new_cached_data);
283
+ code_cache_.emplace (id, std::move ( new_cached_data) );
285
284
286
285
return scope.Escape (fun);
287
286
}
0 commit comments