Skip to content

Commit 31058b8

Browse files
legendecasaduh95
authored andcommitted
src: reorganize ContextifyFunction methods
PR-URL: #58434 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 7521077 commit 31058b8

File tree

4 files changed

+67
-46
lines changed

4 files changed

+67
-46
lines changed

src/node_contextify.cc

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,11 @@ void ContextifyContext::CreatePerIsolateProperties(
371371
IsolateData* isolate_data, Local<ObjectTemplate> target) {
372372
Isolate* isolate = isolate_data->isolate();
373373
SetMethod(isolate, target, "makeContext", MakeContext);
374-
SetMethod(isolate, target, "compileFunction", CompileFunction);
375374
}
376375

377376
void ContextifyContext::RegisterExternalReferences(
378377
ExternalReferenceRegistry* registry) {
379378
registry->Register(MakeContext);
380-
registry->Register(CompileFunction);
381379
registry->Register(PropertyQueryCallback);
382380
registry->Register(PropertyGetterCallback);
383381
registry->Register(PropertySetterCallback);
@@ -1161,22 +1159,6 @@ Maybe<void> StoreCodeCacheResult(
11611159
return JustVoid();
11621160
}
11631161

1164-
// TODO(RaisinTen): Reuse in ContextifyContext::CompileFunction().
1165-
MaybeLocal<Function> CompileFunction(Local<Context> context,
1166-
Local<String> filename,
1167-
Local<String> content,
1168-
LocalVector<String>* parameters) {
1169-
ScriptOrigin script_origin(filename, 0, 0, true);
1170-
ScriptCompiler::Source script_source(content, script_origin);
1171-
1172-
return ScriptCompiler::CompileFunction(context,
1173-
&script_source,
1174-
parameters->size(),
1175-
parameters->data(),
1176-
0,
1177-
nullptr);
1178-
}
1179-
11801162
bool ContextifyScript::InstanceOf(Environment* env,
11811163
const Local<Value>& value) {
11821164
return !value.IsEmpty() &&
@@ -1382,7 +1364,19 @@ ContextifyScript::ContextifyScript(Environment* env, Local<Object> object)
13821364

13831365
ContextifyScript::~ContextifyScript() {}
13841366

1385-
void ContextifyContext::CompileFunction(
1367+
void ContextifyFunction::RegisterExternalReferences(
1368+
ExternalReferenceRegistry* registry) {
1369+
registry->Register(CompileFunction);
1370+
}
1371+
1372+
void ContextifyFunction::CreatePerIsolateProperties(
1373+
IsolateData* isolate_data, Local<ObjectTemplate> target) {
1374+
Isolate* isolate = isolate_data->isolate();
1375+
1376+
SetMethod(isolate, target, "compileFunction", CompileFunction);
1377+
}
1378+
1379+
void ContextifyFunction::CompileFunction(
13861380
const FunctionCallbackInfo<Value>& args) {
13871381
Environment* env = Environment::GetCurrent(args);
13881382
Isolate* isolate = env->isolate();
@@ -1532,7 +1526,7 @@ static LocalVector<String> GetCJSParameters(IsolateData* data) {
15321526
return result;
15331527
}
15341528

1535-
MaybeLocal<Object> ContextifyContext::CompileFunctionAndCacheResult(
1529+
MaybeLocal<Object> ContextifyFunction::CompileFunctionAndCacheResult(
15361530
Environment* env,
15371531
Local<Context> parsing_context,
15381532
ScriptCompiler::Source* source,
@@ -1963,6 +1957,7 @@ void CreatePerIsolateProperties(IsolateData* isolate_data,
19631957

19641958
ContextifyContext::CreatePerIsolateProperties(isolate_data, target);
19651959
ContextifyScript::CreatePerIsolateProperties(isolate_data, target);
1960+
ContextifyFunction::CreatePerIsolateProperties(isolate_data, target);
19661961

19671962
SetMethod(isolate, target, "startSigintWatchdog", StartSigintWatchdog);
19681963
SetMethod(isolate, target, "stopSigintWatchdog", StopSigintWatchdog);
@@ -2015,6 +2010,7 @@ static void CreatePerContextProperties(Local<Object> target,
20152010
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
20162011
ContextifyContext::RegisterExternalReferences(registry);
20172012
ContextifyScript::RegisterExternalReferences(registry);
2013+
ContextifyFunction::RegisterExternalReferences(registry);
20182014

20192015
registry->Register(CompileFunctionForCJSLoader);
20202016
registry->Register(StartSigintWatchdog);

src/node_contextify.h

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,6 @@ class ContextifyContext : public BaseObject {
8686
static bool IsStillInitializing(const ContextifyContext* ctx);
8787
static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args);
8888
static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args);
89-
static void CompileFunction(
90-
const v8::FunctionCallbackInfo<v8::Value>& args);
91-
static v8::MaybeLocal<v8::Object> CompileFunctionAndCacheResult(
92-
Environment* env,
93-
v8::Local<v8::Context> parsing_context,
94-
v8::ScriptCompiler::Source* source,
95-
v8::LocalVector<v8::String> params,
96-
v8::LocalVector<v8::Object> context_extensions,
97-
v8::ScriptCompiler::CompileOptions options,
98-
bool produce_cached_data,
99-
v8::Local<v8::Symbol> id_symbol,
100-
const errors::TryCatchScope& try_catch);
10189
static v8::Intercepted PropertyQueryCallback(
10290
v8::Local<v8::Name> property,
10391
const v8::PropertyCallbackInfo<v8::Integer>& args);
@@ -177,6 +165,29 @@ class ContextifyScript : public BaseObject {
177165
v8::Global<v8::UnboundScript> script_;
178166
};
179167

168+
class ContextifyFunction final {
169+
public:
170+
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
171+
static void CreatePerIsolateProperties(IsolateData* isolate_data,
172+
v8::Local<v8::ObjectTemplate> target);
173+
174+
static void CompileFunction(const v8::FunctionCallbackInfo<v8::Value>& args);
175+
static v8::MaybeLocal<v8::Object> CompileFunctionAndCacheResult(
176+
Environment* env,
177+
v8::Local<v8::Context> parsing_context,
178+
v8::ScriptCompiler::Source* source,
179+
v8::LocalVector<v8::String> params,
180+
v8::LocalVector<v8::Object> context_extensions,
181+
v8::ScriptCompiler::CompileOptions options,
182+
bool produce_cached_data,
183+
v8::Local<v8::Symbol> id_symbol,
184+
const errors::TryCatchScope& try_catch);
185+
186+
private:
187+
ContextifyFunction() = delete;
188+
~ContextifyFunction() = delete;
189+
};
190+
180191
v8::Maybe<void> StoreCodeCacheResult(
181192
Environment* env,
182193
v8::Local<v8::Object> target,
@@ -185,12 +196,6 @@ v8::Maybe<void> StoreCodeCacheResult(
185196
bool produce_cached_data,
186197
std::unique_ptr<v8::ScriptCompiler::CachedData> new_cached_data);
187198

188-
v8::MaybeLocal<v8::Function> CompileFunction(
189-
v8::Local<v8::Context> context,
190-
v8::Local<v8::String> filename,
191-
v8::Local<v8::String> content,
192-
v8::LocalVector<v8::String>* parameters);
193-
194199
} // namespace contextify
195200
} // namespace node
196201

src/node_sea.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ using v8::MaybeLocal;
4141
using v8::NewStringType;
4242
using v8::Object;
4343
using v8::ScriptCompiler;
44+
using v8::ScriptOrigin;
4445
using v8::String;
4546
using v8::Value;
4647

@@ -460,16 +461,23 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path,
460461
FIXED_ONE_BYTE_STRING(isolate, "__filename"),
461462
FIXED_ONE_BYTE_STRING(isolate, "__dirname"),
462463
});
463-
464-
// TODO(RaisinTen): Using the V8 code cache prevents us from using `import()`
465-
// in the SEA code. Support it.
466-
// Refs: https://github.com/nodejs/node/pull/48191#discussion_r1213271430
464+
ScriptOrigin script_origin(filename, 0, 0, true);
465+
ScriptCompiler::Source script_source(content, script_origin);
466+
MaybeLocal<Function> maybe_fn =
467+
ScriptCompiler::CompileFunction(context,
468+
&script_source,
469+
parameters.size(),
470+
parameters.data(),
471+
0,
472+
nullptr);
467473
Local<Function> fn;
468-
if (!contextify::CompileFunction(context, filename, content, &parameters)
469-
.ToLocal(&fn)) {
474+
if (!maybe_fn.ToLocal(&fn)) {
470475
return std::nullopt;
471476
}
472477

478+
// TODO(RaisinTen): Using the V8 code cache prevents us from using `import()`
479+
// in the SEA code. Support it.
480+
// Refs: https://github.com/nodejs/node/pull/48191#discussion_r1213271430
473481
std::unique_ptr<ScriptCompiler::CachedData> cache{
474482
ScriptCompiler::CreateCodeCacheForFunction(fn)};
475483
std::string code_cache(cache->data, cache->data + cache->length);

src/node_snapshotable.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ using v8::HandleScope;
4242
using v8::Isolate;
4343
using v8::Local;
4444
using v8::LocalVector;
45+
using v8::MaybeLocal;
4546
using v8::Object;
4647
using v8::ObjectTemplate;
48+
using v8::ScriptCompiler;
49+
using v8::ScriptOrigin;
4750
using v8::SnapshotCreator;
4851
using v8::StartupData;
4952
using v8::String;
@@ -1487,9 +1490,18 @@ void CompileSerializeMain(const FunctionCallbackInfo<Value>& args) {
14871490
FIXED_ONE_BYTE_STRING(isolate, "__filename"),
14881491
FIXED_ONE_BYTE_STRING(isolate, "__dirname"),
14891492
});
1493+
1494+
ScriptOrigin script_origin(filename, 0, 0, true);
1495+
ScriptCompiler::Source script_source(source, script_origin);
1496+
MaybeLocal<Function> maybe_fn =
1497+
ScriptCompiler::CompileFunction(context,
1498+
&script_source,
1499+
parameters.size(),
1500+
parameters.data(),
1501+
0,
1502+
nullptr);
14901503
Local<Function> fn;
1491-
if (contextify::CompileFunction(context, filename, source, &parameters)
1492-
.ToLocal(&fn)) {
1504+
if (maybe_fn.ToLocal(&fn)) {
14931505
args.GetReturnValue().Set(fn);
14941506
}
14951507
}

0 commit comments

Comments
 (0)