diff --git a/src/node_native_module.cc b/src/node_native_module.cc index 5b036f892ffb1c..d7fa5798d0b92e 100644 --- a/src/node_native_module.cc +++ b/src/node_native_module.cc @@ -112,6 +112,7 @@ NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) { void NativeModuleLoader::GetCodeCache(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); + CHECK(env->is_main_thread()); CHECK(args[0]->IsString()); node::Utf8Value id_v(isolate, args[0].As()); @@ -133,15 +134,13 @@ MaybeLocal NativeModuleLoader::GetCodeCache(Isolate* isolate, ScriptCompiler::CachedData* cached_data = nullptr; const auto it = code_cache_.find(id); - if (it != code_cache_.end()) { - cached_data = it->second.get(); - } - - // The module has not been compiled before. - if (cached_data == nullptr) { + if (it == code_cache_.end()) { + // The module has not been compiled before. return MaybeLocal(); } + cached_data = it->second.get(); + MallocedBuffer copied(cached_data->length); memcpy(copied.data, cached_data->data, cached_data->length); Local buf = @@ -257,7 +256,7 @@ MaybeLocal NativeModuleLoader::LookupAndCompile( Local fun = maybe_fun.ToLocalChecked(); // XXX(joyeecheung): this bookkeeping is not exactly accurate because // it only starts after the Environment is created, so the per_context.js - // will never be any of these two sets, but the two sets are only for + // will never be in any of these two sets, but the two sets are only for // testing anyway. if (use_cache) { if (optional_env != nullptr) { @@ -276,12 +275,12 @@ MaybeLocal NativeModuleLoader::LookupAndCompile( } // Generate new cache for next compilation - ScriptCompiler::CachedData* new_cached_data = - ScriptCompiler::CreateCodeCacheForFunction(fun); + std::unique_ptr new_cached_data( + ScriptCompiler::CreateCodeCacheForFunction(fun)); CHECK_NE(new_cached_data, nullptr); // The old entry should've been erased by now so we can just emplace - code_cache_.emplace(id, new_cached_data); + code_cache_.emplace(id, std::move(new_cached_data)); return scope.Escape(fun); } diff --git a/tools/generate_code_cache.js b/tools/generate_code_cache.js index 098be51087bec4..a434f640c29157 100644 --- a/tools/generate_code_cache.js +++ b/tools/generate_code_cache.js @@ -58,7 +58,7 @@ function getInitalizer(key, cache) { const defName = `${key.replace(/\//g, '_').replace(/-/g, '_')}_raw`; const definition = `static const uint8_t ${defName}[] = {\n` + `${cache.join(',')}\n};`; - const dataDef = 'new v8::ScriptCompiler::CachedData(' + + const dataDef = 'std::make_unique(' + `${defName}, static_cast(arraysize(${defName})), ` + 'policy)'; const initializer =