Skip to content

Commit 9151e7a

Browse files
committed
fixup! src: use NativeModuleLoader to compile per_context.js
1 parent 2a84b8d commit 9151e7a

2 files changed

Lines changed: 32 additions & 26 deletions

File tree

src/node_native_module.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ void NativeModuleLoader::CompileCodeCache(
103103

104104
// TODO(joyeecheung): allow compiling cache for bootstrapper by
105105
// switching on id
106-
MaybeLocal<Value> result = CompileAsModule(env, *id, true);
106+
MaybeLocal<Value> result =
107+
CompileAsModule(env, *id, CompilationResultType::kCodeCache);
107108
if (!result.IsEmpty()) {
108109
args.GetReturnValue().Set(result.ToLocalChecked());
109110
}
@@ -115,7 +116,8 @@ void NativeModuleLoader::CompileFunction(
115116
CHECK(args[0]->IsString());
116117
node::Utf8Value id(env->isolate(), args[0].As<String>());
117118

118-
MaybeLocal<Value> result = CompileAsModule(env, *id, false);
119+
MaybeLocal<Value> result =
120+
CompileAsModule(env, *id, CompilationResultType::kFunction);
119121
if (!result.IsEmpty()) {
120122
args.GetReturnValue().Set(result.ToLocalChecked());
121123
}
@@ -131,7 +133,7 @@ MaybeLocal<Value> NativeModuleLoader::CompileAndCall(
131133
Environment* optional_env) {
132134
Isolate* isolate = context->GetIsolate();
133135
MaybeLocal<Value> compiled = per_process_loader.LookupAndCompile(
134-
context, id, parameters, false, nullptr);
136+
context, id, parameters, CompilationResultType::kFunction, nullptr);
135137
if (compiled.IsEmpty()) {
136138
return compiled;
137139
}
@@ -140,16 +142,15 @@ MaybeLocal<Value> NativeModuleLoader::CompileAndCall(
140142
context, v8::Null(isolate), arguments->size(), arguments->data());
141143
}
142144

143-
MaybeLocal<Value> NativeModuleLoader::CompileAsModule(Environment* env,
144-
const char* id,
145-
bool return_code_cache) {
145+
MaybeLocal<Value> NativeModuleLoader::CompileAsModule(
146+
Environment* env, const char* id, CompilationResultType result) {
146147
std::vector<Local<String>> parameters = {env->exports_string(),
147148
env->require_string(),
148149
env->module_string(),
149150
env->process_string(),
150151
env->internal_binding_string()};
151152
return per_process_loader.LookupAndCompile(
152-
env->context(), id, &parameters, return_code_cache, env);
153+
env->context(), id, &parameters, result, env);
153154
}
154155

155156
// Currently V8 only checks that the length of the source code is the
@@ -214,7 +215,7 @@ MaybeLocal<Value> NativeModuleLoader::LookupAndCompile(
214215
Local<Context> context,
215216
const char* id,
216217
std::vector<Local<String>>* parameters,
217-
bool return_code_cache,
218+
CompilationResultType result_type,
218219
Environment* optional_env) {
219220
Isolate* isolate = context->GetIsolate();
220221
EscapableHandleScope scope(isolate);
@@ -236,7 +237,7 @@ MaybeLocal<Value> NativeModuleLoader::LookupAndCompile(
236237
// built with them.
237238
// 2. If we are generating code cache for tools/general_code_cache.js, we
238239
// are not going to use any cache ourselves.
239-
if (has_code_cache_ && !return_code_cache) {
240+
if (has_code_cache_ && result_type == CompilationResultType::kFunction) {
240241
cached_data = GetCachedData(id);
241242
if (cached_data != nullptr) {
242243
use_cache = true;
@@ -246,7 +247,7 @@ MaybeLocal<Value> NativeModuleLoader::LookupAndCompile(
246247
ScriptCompiler::Source script_source(source, origin, cached_data);
247248

248249
ScriptCompiler::CompileOptions options;
249-
if (return_code_cache) {
250+
if (result_type == CompilationResultType::kCodeCache) {
250251
options = ScriptCompiler::kEagerCompile;
251252
} else if (use_cache) {
252253
options = ScriptCompiler::kConsumeCodeCache;
@@ -269,7 +270,7 @@ MaybeLocal<Value> NativeModuleLoader::LookupAndCompile(
269270
// In the case of early errors, v8 is already capable of
270271
// decorating the stack for us - note that we use CompileFunctionInContext
271272
// so there is no need to worry about wrappers.
272-
return scope.EscapeMaybe(MaybeLocal<Value>());
273+
return MaybeLocal<Value>();
273274
}
274275

275276
Local<Function> fun = maybe_fun.ToLocalChecked();
@@ -289,7 +290,7 @@ MaybeLocal<Value> NativeModuleLoader::LookupAndCompile(
289290
}
290291
}
291292

292-
if (return_code_cache) {
293+
if (result_type == CompilationResultType::kCodeCache) {
293294
std::unique_ptr<ScriptCompiler::CachedData> cached_data(
294295
ScriptCompiler::CreateCodeCacheForFunction(fun));
295296
CHECK_NE(cached_data, nullptr);
@@ -311,7 +312,7 @@ MaybeLocal<Value> NativeModuleLoader::LookupAndCompile(
311312
ret = fun;
312313
}
313314

314-
return scope.EscapeMaybe(MaybeLocal<Value>(ret));
315+
return scope.Escape(ret);
315316
}
316317

317318
void NativeModuleLoader::Initialize(Local<Object> target,

src/node_native_module.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,27 @@ using NativeModuleHashMap = std::map<std::string, std::string>;
2525
// The instances of this class are per-process.
2626
class NativeModuleLoader {
2727
public:
28+
// kCodeCache indicates that the compilation result should be returned
29+
// as a Uint8Array, whereas kFunction indicates that the result should
30+
// be returned as a Function.
31+
// TODO(joyeecheung): it's possible to always produce code cache
32+
// on the main thread and consume them in worker threads, or just
33+
// share the cache among all the threads, although
34+
// we need to decide whether to do that even when workers are not used.
35+
enum class CompilationResultType { kCodeCache, kFunction };
36+
2837
NativeModuleLoader();
2938
static void Initialize(v8::Local<v8::Object> target,
3039
v8::Local<v8::Value> unused,
3140
v8::Local<v8::Context> context);
3241
v8::Local<v8::Object> GetSourceObject(v8::Local<v8::Context> context) const;
3342
v8::Local<v8::String> GetSource(v8::Isolate* isolate, const char* id) const;
34-
// Run a script with JS source bundled inside the binary as if it's a
35-
// function called with a null receiver and arguments specified in C++.
43+
44+
// Run a script with JS source bundled inside the binary as if it's wrapped
45+
// in a function called with a null receiver and arguments specified in C++.
3646
// The returned value is empty if an exception is encountered.
47+
// JS code run with this method can assume that their top-level
48+
// declarations won't affect the global scope.
3749
v8::MaybeLocal<v8::Value> CompileAndCall(
3850
v8::Local<v8::Context> context,
3951
const char* id,
@@ -64,16 +76,9 @@ class NativeModuleLoader {
6476
v8::ScriptCompiler::CachedData* GetCachedData(const char* id) const;
6577

6678
// Compile a script as a NativeModule that can be loaded via
67-
// NativeModule.p.require in JS land. If returns_code_cache is true,
68-
// returns the cache data in a Uint8Array, otherwise returns the compiled
69-
// function.
70-
// TODO(joyeecheung): it's possible to always produce code cache
71-
// on the main thread and consume them in worker threads, or just
72-
// share the cache among all the threads, although
73-
// We need to decide whether to do that even when workers are not used.
74-
static v8::MaybeLocal<v8::Value> CompileAsModule(Environment* env,
75-
const char* id,
76-
bool return_code_cache);
79+
// NativeModule.p.require in JS land.
80+
static v8::MaybeLocal<v8::Value> CompileAsModule(
81+
Environment* env, const char* id, CompilationResultType result_type);
7782

7883
// For bootstrappers optional_env may be a nullptr.
7984
// If an exception is encountered (e.g. source code contains
@@ -82,7 +87,7 @@ class NativeModuleLoader {
8287
v8::Local<v8::Context> context,
8388
const char* id,
8489
std::vector<v8::Local<v8::String>>* parameters,
85-
bool returns_code_cache,
90+
CompilationResultType result_type,
8691
Environment* optional_env);
8792

8893
bool has_code_cache_ = false;

0 commit comments

Comments
 (0)