Skip to content

Commit bdf589b

Browse files
committed
fixup! src: always compile and store code cache for native modules
1 parent 3203b0c commit bdf589b

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/node_native_module.cc

+9-10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) {
112112
void NativeModuleLoader::GetCodeCache(const FunctionCallbackInfo<Value>& args) {
113113
Environment* env = Environment::GetCurrent(args);
114114
Isolate* isolate = env->isolate();
115+
CHECK(env->is_main_thread());
115116

116117
CHECK(args[0]->IsString());
117118
node::Utf8Value id_v(isolate, args[0].As<String>());
@@ -133,15 +134,13 @@ MaybeLocal<Uint8Array> NativeModuleLoader::GetCodeCache(Isolate* isolate,
133134

134135
ScriptCompiler::CachedData* cached_data = nullptr;
135136
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.
142139
return MaybeLocal<Uint8Array>();
143140
}
144141

142+
cached_data = it->second.get();
143+
145144
MallocedBuffer<uint8_t> copied(cached_data->length);
146145
memcpy(copied.data, cached_data->data, cached_data->length);
147146
Local<ArrayBuffer> buf =
@@ -257,7 +256,7 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
257256
Local<Function> fun = maybe_fun.ToLocalChecked();
258257
// XXX(joyeecheung): this bookkeeping is not exactly accurate because
259258
// 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
261260
// testing anyway.
262261
if (use_cache) {
263262
if (optional_env != nullptr) {
@@ -276,12 +275,12 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
276275
}
277276

278277
// 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));
281280
CHECK_NE(new_cached_data, nullptr);
282281

283282
// 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));
285284

286285
return scope.Escape(fun);
287286
}

tools/generate_code_cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function getInitalizer(key, cache) {
5858
const defName = `${key.replace(/\//g, '_').replace(/-/g, '_')}_raw`;
5959
const definition = `static const uint8_t ${defName}[] = {\n` +
6060
`${cache.join(',')}\n};`;
61-
const dataDef = 'new v8::ScriptCompiler::CachedData(' +
61+
const dataDef = 'std::make_unique<v8::ScriptCompiler::CachedData>(' +
6262
`${defName}, static_cast<int>(arraysize(${defName})), ` +
6363
'policy)';
6464
const initializer =

0 commit comments

Comments
 (0)