GeezerVison is showing github.com — src: reorganize ContextifyFunction methods · nodejs/node@63f643e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_contextify.cc +16-20Lines changed: 16 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -365,13 +365,11 @@ void ContextifyContext::CreatePerIsolateProperties( | 365 | 365 | IsolateData* isolate_data, Local<ObjectTemplate> target) { | 366 | 366 | Isolate* isolate = isolate_data->isolate(); | 367 | 367 | SetMethod(isolate, target, "makeContext", MakeContext); | 368 | | - SetMethod(isolate, target, "compileFunction", CompileFunction); | 369 | 368 | } | 370 | 369 |
| 371 | 370 | void ContextifyContext::RegisterExternalReferences( | 372 | 371 | ExternalReferenceRegistry* registry) { | 373 | 372 | registry->Register(MakeContext); | 374 | | - registry->Register(CompileFunction); | 375 | 373 | registry->Register(PropertyQueryCallback); | 376 | 374 | registry->Register(PropertyGetterCallback); | 377 | 375 | registry->Register(PropertySetterCallback); | @@ -1163,22 +1161,6 @@ Maybe<void> StoreCodeCacheResult( | 1163 | 1161 | return JustVoid(); | 1164 | 1162 | } | 1165 | 1163 |
| 1166 | | -// TODO(RaisinTen): Reuse in ContextifyContext::CompileFunction(). | 1167 | | -MaybeLocal<Function> CompileFunction(Local<Context> context, | 1168 | | - Local<String> filename, | 1169 | | - Local<String> content, | 1170 | | - LocalVector<String>* parameters) { | 1171 | | - ScriptOrigin script_origin(filename, 0, 0, true); | 1172 | | - ScriptCompiler::Source script_source(content, script_origin); | 1173 | | - | 1174 | | - return ScriptCompiler::CompileFunction(context, | 1175 | | - &script_source, | 1176 | | - parameters->size(), | 1177 | | - parameters->data(), | 1178 | | - 0, | 1179 | | - nullptr); | 1180 | | -} | 1181 | | - | 1182 | 1164 | bool ContextifyScript::InstanceOf(Environment* env, | 1183 | 1165 | const Local<Value>& value) { | 1184 | 1166 | return !value.IsEmpty() && | @@ -1392,7 +1374,19 @@ ContextifyScript::ContextifyScript(Environment* env, Local<Object> object) { | 1392 | 1374 |
| 1393 | 1375 | ContextifyScript::~ContextifyScript() {} | 1394 | 1376 |
| 1395 | | -void ContextifyContext::CompileFunction( | | 1377 | +void ContextifyFunction::RegisterExternalReferences( | | 1378 | + ExternalReferenceRegistry* registry) { | | 1379 | + registry->Register(CompileFunction); | | 1380 | +} | | 1381 | + | | 1382 | +void ContextifyFunction::CreatePerIsolateProperties( | | 1383 | + IsolateData* isolate_data, Local<ObjectTemplate> target) { | | 1384 | + Isolate* isolate = isolate_data->isolate(); | | 1385 | + | | 1386 | + SetMethod(isolate, target, "compileFunction", CompileFunction); | | 1387 | +} | | 1388 | + | | 1389 | +void ContextifyFunction::CompileFunction( | 1396 | 1390 | const FunctionCallbackInfo<Value>& args) { | 1397 | 1391 | Environment* env = Environment::GetCurrent(args); | 1398 | 1392 | Isolate* isolate = env->isolate(); | @@ -1542,7 +1536,7 @@ static LocalVector<String> GetCJSParameters(IsolateData* data) { | 1542 | 1536 | return result; | 1543 | 1537 | } | 1544 | 1538 |
| 1545 | | -MaybeLocal<Object> ContextifyContext::CompileFunctionAndCacheResult( | | 1539 | +MaybeLocal<Object> ContextifyFunction::CompileFunctionAndCacheResult( | 1546 | 1540 | Environment* env, | 1547 | 1541 | Local<Context> parsing_context, | 1548 | 1542 | ScriptCompiler::Source* source, | @@ -1973,6 +1967,7 @@ void CreatePerIsolateProperties(IsolateData* isolate_data, | 1973 | 1967 |
| 1974 | 1968 | ContextifyContext::CreatePerIsolateProperties(isolate_data, target); | 1975 | 1969 | ContextifyScript::CreatePerIsolateProperties(isolate_data, target); | | 1970 | + ContextifyFunction::CreatePerIsolateProperties(isolate_data, target); | 1976 | 1971 |
| 1977 | 1972 | SetMethod(isolate, target, "startSigintWatchdog", StartSigintWatchdog); | 1978 | 1973 | SetMethod(isolate, target, "stopSigintWatchdog", StopSigintWatchdog); | @@ -2025,6 +2020,7 @@ static void CreatePerContextProperties(Local<Object> target, | 2025 | 2020 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | 2026 | 2021 | ContextifyContext::RegisterExternalReferences(registry); | 2027 | 2022 | ContextifyScript::RegisterExternalReferences(registry); | | 2023 | + ContextifyFunction::RegisterExternalReferences(registry); | 2028 | 2024 |
| 2029 | 2025 | registry->Register(CompileFunctionForCJSLoader); | 2030 | 2026 | registry->Register(StartSigintWatchdog); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_contextify.h +23-18Lines changed: 23 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -144,18 +144,6 @@ class ContextifyContext final : CPPGC_MIXIN(ContextifyContext) { | 144 | 144 | static bool IsStillInitializing(const ContextifyContext* ctx); | 145 | 145 | static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args); | 146 | 146 | static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args); | 147 | | - static void CompileFunction( | 148 | | - const v8::FunctionCallbackInfo<v8::Value>& args); | 149 | | - static v8::MaybeLocal<v8::Object> CompileFunctionAndCacheResult( | 150 | | - Environment* env, | 151 | | - v8::Local<v8::Context> parsing_context, | 152 | | - v8::ScriptCompiler::Source* source, | 153 | | - v8::LocalVector<v8::String> params, | 154 | | - v8::LocalVector<v8::Object> context_extensions, | 155 | | - v8::ScriptCompiler::CompileOptions options, | 156 | | - bool produce_cached_data, | 157 | | - v8::Local<v8::Symbol> id_symbol, | 158 | | - const errors::TryCatchScope& try_catch); | 159 | 147 | static v8::Intercepted PropertyQueryCallback( | 160 | 148 | v8::Local<v8::Name> property, | 161 | 149 | const v8::PropertyCallbackInfo<v8::Integer>& args); | @@ -234,6 +222,29 @@ class ContextifyScript final : CPPGC_MIXIN(ContextifyScript) { | 234 | 222 | v8::TracedReference<v8::UnboundScript> script_; | 235 | 223 | }; | 236 | 224 |
| | 225 | +class ContextifyFunction final { | | 226 | + public: | | 227 | + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); | | 228 | + static void CreatePerIsolateProperties(IsolateData* isolate_data, | | 229 | + v8::Local<v8::ObjectTemplate> target); | | 230 | + | | 231 | + static void CompileFunction(const v8::FunctionCallbackInfo<v8::Value>& args); | | 232 | + static v8::MaybeLocal<v8::Object> CompileFunctionAndCacheResult( | | 233 | + Environment* env, | | 234 | + v8::Local<v8::Context> parsing_context, | | 235 | + v8::ScriptCompiler::Source* source, | | 236 | + v8::LocalVector<v8::String> params, | | 237 | + v8::LocalVector<v8::Object> context_extensions, | | 238 | + v8::ScriptCompiler::CompileOptions options, | | 239 | + bool produce_cached_data, | | 240 | + v8::Local<v8::Symbol> id_symbol, | | 241 | + const errors::TryCatchScope& try_catch); | | 242 | + | | 243 | + private: | | 244 | + ContextifyFunction() = delete; | | 245 | + ~ContextifyFunction() = delete; | | 246 | +}; | | 247 | + | 237 | 248 | v8::Maybe<void> StoreCodeCacheResult( | 238 | 249 | Environment* env, | 239 | 250 | v8::Local<v8::Object> target, | @@ -242,12 +253,6 @@ v8::Maybe<void> StoreCodeCacheResult( | 242 | 253 | bool produce_cached_data, | 243 | 254 | std::unique_ptr<v8::ScriptCompiler::CachedData> new_cached_data); | 244 | 255 |
| 245 | | -v8::MaybeLocal<v8::Function> CompileFunction( | 246 | | - v8::Local<v8::Context> context, | 247 | | - v8::Local<v8::String> filename, | 248 | | - v8::Local<v8::String> content, | 249 | | - v8::LocalVector<v8::String>* parameters); | 250 | | - | 251 | 256 | } // namespace contextify | 252 | 257 | } // namespace node | 253 | 258 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_sea.cc +14-6Lines changed: 14 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,6 +41,7 @@ using v8::MaybeLocal; | 41 | 41 | using v8::NewStringType; | 42 | 42 | using v8::Object; | 43 | 43 | using v8::ScriptCompiler; | | 44 | +using v8::ScriptOrigin; | 44 | 45 | using v8::String; | 45 | 46 | using v8::Value; | 46 | 47 |
| @@ -460,16 +461,23 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path, | 460 | 461 | FIXED_ONE_BYTE_STRING(isolate, "__filename"), | 461 | 462 | FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | 462 | 463 | }); | 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); | 467 | 473 | Local<Function> fn; | 468 | | - if (!contextify::CompileFunction(context, filename, content, ¶meters) | 469 | | - .ToLocal(&fn)) { | | 474 | + if (!maybe_fn.ToLocal(&fn)) { | 470 | 475 | return std::nullopt; | 471 | 476 | } | 472 | 477 |
| | 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 | 473 | 481 | std::unique_ptr<ScriptCompiler::CachedData> cache{ | 474 | 482 | ScriptCompiler::CreateCodeCacheForFunction(fn)}; | 475 | 483 | std::string code_cache(cache->data, cache->data + cache->length); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_snapshotable.cc +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,8 +42,11 @@ using v8::HandleScope; | 42 | 42 | using v8::Isolate; | 43 | 43 | using v8::Local; | 44 | 44 | using v8::LocalVector; | | 45 | +using v8::MaybeLocal; | 45 | 46 | using v8::Object; | 46 | 47 | using v8::ObjectTemplate; | | 48 | +using v8::ScriptCompiler; | | 49 | +using v8::ScriptOrigin; | 47 | 50 | using v8::SnapshotCreator; | 48 | 51 | using v8::StartupData; | 49 | 52 | using v8::String; | @@ -1488,9 +1491,18 @@ void CompileSerializeMain(const FunctionCallbackInfo<Value>& args) { | 1488 | 1491 | FIXED_ONE_BYTE_STRING(isolate, "__filename"), | 1489 | 1492 | FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | 1490 | 1493 | }); | | 1494 | + | | 1495 | + ScriptOrigin script_origin(filename, 0, 0, true); | | 1496 | + ScriptCompiler::Source script_source(source, script_origin); | | 1497 | + MaybeLocal<Function> maybe_fn = | | 1498 | + ScriptCompiler::CompileFunction(context, | | 1499 | + &script_source, | | 1500 | + parameters.size(), | | 1501 | + parameters.data(), | | 1502 | + 0, | | 1503 | + nullptr); | 1491 | 1504 | Local<Function> fn; | 1492 | | - if (contextify::CompileFunction(context, filename, source, ¶meters) | 1493 | | - .ToLocal(&fn)) { | | 1505 | + if (maybe_fn.ToLocal(&fn)) { | 1494 | 1506 | args.GetReturnValue().Set(fn); | 1495 | 1507 | } | 1496 | 1508 | } | |
|