Skip to content

Commit

Permalink
fixup! src: merge NativeModuleEnv into NativeModuleLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Jul 19, 2022
1 parent a770662 commit 4ab8e25
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
3 changes: 1 addition & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1188,8 +1188,7 @@ int Start(int argc, char** argv) {
uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME);

if (snapshot_data != nullptr) {
native_module::NativeModuleLoader::RefreshCodeCache(
snapshot_data->code_cache);
NativeModuleLoader::RefreshCodeCache(snapshot_data->code_cache);
}
NodeMainInstance main_instance(snapshot_data,
uv_default_loop(),
Expand Down
41 changes: 18 additions & 23 deletions src/node_native_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ using v8::Value;

NativeModuleLoader NativeModuleLoader::instance_;

NativeModuleLoader::NativeModuleLoader() : config_(GetConfig()) {
NativeModuleLoader::NativeModuleLoader()
: config_(GetConfig()), has_code_cache_(false) {
LoadJavaScriptSource();
}

Expand All @@ -39,19 +40,19 @@ NativeModuleLoader* NativeModuleLoader::GetInstance() {
}

bool NativeModuleLoader::Exists(const char* id) {
auto& source = NativeModuleLoader::GetInstance()->source_;
auto& source = GetInstance()->source_;
return source.find(id) != source.end();
}

bool NativeModuleLoader::Add(const char* id, const UnionBytes& source) {
auto result = NativeModuleLoader::GetInstance()->source_.emplace(id, source);
auto result = GetInstance()->source_.emplace(id, source);
return result.second;
}

Local<Object> NativeModuleLoader::GetSourceObject(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
Local<Object> out = Object::New(isolate);
auto& source = NativeModuleLoader::GetInstance()->source_;
auto& source = GetInstance()->source_;
for (auto const& x : source) {
Local<String> key = OneByteString(isolate, x.first.c_str(), x.first.size());
out->Set(context, key, x.second.ToStringChecked(isolate)).FromJust();
Expand All @@ -60,7 +61,7 @@ Local<Object> NativeModuleLoader::GetSourceObject(Local<Context> context) {
}

Local<String> NativeModuleLoader::GetConfigString(Isolate* isolate) {
return NativeModuleLoader::GetInstance()->config_.ToStringChecked(isolate);
return GetInstance()->config_.ToStringChecked(isolate);
}

std::vector<std::string> NativeModuleLoader::GetModuleIds() {
Expand Down Expand Up @@ -361,20 +362,17 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
const char* id,
std::vector<Local<String>>* parameters,
Environment* optional_env) {
NativeModuleLoader::Result result;
Result result;
MaybeLocal<Function> maybe =
NativeModuleLoader::GetInstance()->LookupAndCompileInternal(
context, id, parameters, &result);
GetInstance()->LookupAndCompileInternal(context, id, parameters, &result);
if (optional_env != nullptr) {
RecordResult(id, result, optional_env);
}
return maybe;
}

bool NativeModuleLoader::has_code_cache_ = false;

bool NativeModuleLoader::CompileAllModules(Local<Context> context) {
NativeModuleLoader* loader = NativeModuleLoader::GetInstance();
NativeModuleLoader* loader = GetInstance();
std::vector<std::string> ids = loader->GetModuleIds();
bool all_succeeded = true;
for (const auto& id : ids) {
Expand All @@ -383,7 +381,7 @@ bool NativeModuleLoader::CompileAllModules(Local<Context> context) {
continue;
}
v8::TryCatch bootstrapCatch(context->GetIsolate());
native_module::NativeModuleLoader::Result result;
Result result;
USE(loader->CompileAsModule(context, id.c_str(), &result));
if (bootstrapCatch.HasCaught()) {
per_process::Debug(DebugCategory::CODE_CACHE,
Expand All @@ -397,7 +395,7 @@ bool NativeModuleLoader::CompileAllModules(Local<Context> context) {
}

void NativeModuleLoader::CopyCodeCache(std::vector<CodeCacheInfo>* out) {
NativeModuleLoader* loader = NativeModuleLoader::GetInstance();
NativeModuleLoader* loader = GetInstance();
Mutex::ScopedLock lock(loader->code_cache_mutex());
auto in = loader->code_cache();
for (auto const& item : *in) {
Expand All @@ -409,7 +407,7 @@ void NativeModuleLoader::CopyCodeCache(std::vector<CodeCacheInfo>* out) {

void NativeModuleLoader::RefreshCodeCache(
const std::vector<CodeCacheInfo>& in) {
NativeModuleLoader* loader = NativeModuleLoader::GetInstance();
NativeModuleLoader* loader = GetInstance();
Mutex::ScopedLock lock(loader->code_cache_mutex());
auto out = loader->code_cache();
for (auto const& item : in) {
Expand All @@ -426,7 +424,7 @@ void NativeModuleLoader::RefreshCodeCache(
out->emplace(item.id, new_cache.release());
}
}
NativeModuleLoader::has_code_cache_ = true;
loader->has_code_cache_ = true;
}

void NativeModuleLoader::GetModuleCategories(
Expand All @@ -438,9 +436,8 @@ void NativeModuleLoader::GetModuleCategories(

// Copy from the per-process categories
std::set<std::string> cannot_be_required =
NativeModuleLoader::GetInstance()->GetCannotBeRequired();
std::set<std::string> can_be_required =
NativeModuleLoader::GetInstance()->GetCanBeRequired();
GetInstance()->GetCannotBeRequired();
std::set<std::string> can_be_required = GetInstance()->GetCanBeRequired();

if (!env->owns_process_state()) {
can_be_required.erase("trace_events");
Expand Down Expand Up @@ -522,8 +519,7 @@ void NativeModuleLoader::ModuleIdsGetter(
Local<Name> property, const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();

std::vector<std::string> ids =
NativeModuleLoader::GetInstance()->GetModuleIds();
std::vector<std::string> ids = GetInstance()->GetModuleIds();
info.GetReturnValue().Set(
ToV8Value(isolate->GetCurrentContext(), ids).ToLocalChecked());
}
Expand All @@ -550,8 +546,7 @@ void NativeModuleLoader::CompileFunction(
const char* id = *id_v;
NativeModuleLoader::Result result;
MaybeLocal<Function> maybe =
NativeModuleLoader::GetInstance()->CompileAsModule(
env->context(), id, &result);
GetInstance()->CompileAsModule(env->context(), id, &result);
RecordResult(id, result, env);
Local<Function> fn;
if (maybe.ToLocal(&fn)) {
Expand All @@ -562,7 +557,7 @@ void NativeModuleLoader::CompileFunction(
void NativeModuleLoader::HasCachedBuiltins(
const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
v8::Boolean::New(args.GetIsolate(), NativeModuleLoader::has_code_cache_));
v8::Boolean::New(args.GetIsolate(), GetInstance()->has_code_cache_));
}

// TODO(joyeecheung): It is somewhat confusing that Class::Initialize
Expand Down
2 changes: 1 addition & 1 deletion src/node_native_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class NODE_EXTERN_PRIVATE NativeModuleLoader {
// Used to synchronize access to the code cache map
Mutex code_cache_mutex_;

static bool has_code_cache_;
bool has_code_cache_;

friend class ::PerProcessTest;
};
Expand Down

0 comments on commit 4ab8e25

Please sign in to comment.