Skip to content

Commit

Permalink
fixup! src: always compile and store code cache for native modules
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Dec 11, 2018
1 parent 3203b0c commit bdf589b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
19 changes: 9 additions & 10 deletions src/node_native_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) {
void NativeModuleLoader::GetCodeCache(const FunctionCallbackInfo<Value>& 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<String>());
Expand All @@ -133,15 +134,13 @@ MaybeLocal<Uint8Array> 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<Uint8Array>();
}

cached_data = it->second.get();

MallocedBuffer<uint8_t> copied(cached_data->length);
memcpy(copied.data, cached_data->data, cached_data->length);
Local<ArrayBuffer> buf =
Expand Down Expand Up @@ -257,7 +256,7 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
Local<Function> 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) {
Expand All @@ -276,12 +275,12 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
}

// Generate new cache for next compilation
ScriptCompiler::CachedData* new_cached_data =
ScriptCompiler::CreateCodeCacheForFunction(fun);
std::unique_ptr<ScriptCompiler::CachedData> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion tools/generate_code_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::ScriptCompiler::CachedData>(' +
`${defName}, static_cast<int>(arraysize(${defName})), ` +
'policy)';
const initializer =
Expand Down

0 comments on commit bdf589b

Please sign in to comment.