@@ -911,7 +911,7 @@ void SnapshotBuilder::InitializeIsolateParams(const SnapshotData* data,
911
911
const_cast <v8::StartupData*>(&(data->v8_snapshot_blob_data ));
912
912
}
913
913
914
- ExitCode SnapshotBuilder::Generate (
914
+ ExitCode BuildSnapshotWithoutCodeCache (
915
915
SnapshotData* out,
916
916
const std::vector<std::string>& args,
917
917
const std::vector<std::string>& exec_args,
@@ -933,8 +933,8 @@ ExitCode SnapshotBuilder::Generate(
933
933
fprintf (stderr, " %s: %s\n " , args[0 ].c_str (), err.c_str ());
934
934
return ExitCode::kBootstrapFailure ;
935
935
}
936
- Isolate* isolate = setup->isolate ();
937
936
937
+ Isolate* isolate = setup->isolate ();
938
938
{
939
939
HandleScope scope (isolate);
940
940
TryCatch bootstrapCatch (isolate);
@@ -968,7 +968,77 @@ ExitCode SnapshotBuilder::Generate(
968
968
}
969
969
}
970
970
971
- return CreateSnapshot (out, setup.get (), static_cast <uint8_t >(snapshot_type));
971
+ return SnapshotBuilder::CreateSnapshot (
972
+ out, setup.get (), static_cast <uint8_t >(snapshot_type));
973
+ }
974
+
975
+ ExitCode BuildCodeCacheFromSnapshot (SnapshotData* out,
976
+ const std::vector<std::string>& args,
977
+ const std::vector<std::string>& exec_args) {
978
+ std::vector<std::string> errors;
979
+ auto data_wrapper = out->AsEmbedderWrapper ();
980
+ auto setup = CommonEnvironmentSetup::CreateFromSnapshot (
981
+ per_process::v8_platform.Platform (),
982
+ &errors,
983
+ data_wrapper.get (),
984
+ args,
985
+ exec_args);
986
+ if (!setup) {
987
+ for (const auto & err : errors)
988
+ fprintf (stderr, " %s: %s\n " , args[0 ].c_str (), err.c_str ());
989
+ return ExitCode::kBootstrapFailure ;
990
+ }
991
+
992
+ Isolate* isolate = setup->isolate ();
993
+ v8::Locker locker (isolate);
994
+ Isolate::Scope isolate_scope (isolate);
995
+ HandleScope handle_scope (isolate);
996
+ TryCatch bootstrapCatch (isolate);
997
+
998
+ auto print_Exception = OnScopeLeave ([&]() {
999
+ if (bootstrapCatch.HasCaught ()) {
1000
+ PrintCaughtException (
1001
+ isolate, isolate->GetCurrentContext (), bootstrapCatch);
1002
+ }
1003
+ });
1004
+
1005
+ Environment* env = setup->env ();
1006
+ // Regenerate all the code cache.
1007
+ if (!env->builtin_loader ()->CompileAllBuiltins (setup->context ())) {
1008
+ return ExitCode::kGenericUserError ;
1009
+ }
1010
+ env->builtin_loader ()->CopyCodeCache (&(out->code_cache ));
1011
+ if (per_process::enabled_debug_list.enabled (DebugCategory::MKSNAPSHOT)) {
1012
+ for (const auto & item : out->code_cache ) {
1013
+ std::string size_str = FormatSize (item.data .length );
1014
+ per_process::Debug (DebugCategory::MKSNAPSHOT,
1015
+ " Generated code cache for %d: %s\n " ,
1016
+ item.id .c_str (),
1017
+ size_str.c_str ());
1018
+ }
1019
+ }
1020
+ return ExitCode::kNoFailure ;
1021
+ }
1022
+
1023
+ ExitCode SnapshotBuilder::Generate (
1024
+ SnapshotData* out,
1025
+ const std::vector<std::string>& args,
1026
+ const std::vector<std::string>& exec_args,
1027
+ std::optional<std::string_view> main_script) {
1028
+ ExitCode code =
1029
+ BuildSnapshotWithoutCodeCache (out, args, exec_args, main_script);
1030
+ if (code != ExitCode::kNoFailure ) {
1031
+ return code;
1032
+ }
1033
+
1034
+ #ifdef NODE_USE_NODE_CODE_CACHE
1035
+ // Deserialize the snapshot to recompile code cache. We need to do this in the
1036
+ // second pass because V8 requires the code cache to be compiled with a
1037
+ // finalized read-only space.
1038
+ return BuildCodeCacheFromSnapshot (out, args, exec_args);
1039
+ #else
1040
+ return ExitCode::kNoFailure ;
1041
+ #endif
972
1042
}
973
1043
974
1044
ExitCode SnapshotBuilder::CreateSnapshot (SnapshotData* out,
@@ -1021,21 +1091,6 @@ ExitCode SnapshotBuilder::CreateSnapshot(SnapshotData* out,
1021
1091
out->isolate_data_info = setup->isolate_data ()->Serialize (creator);
1022
1092
out->env_info = env->Serialize (creator);
1023
1093
1024
- #ifdef NODE_USE_NODE_CODE_CACHE
1025
- // Regenerate all the code cache.
1026
- if (!env->builtin_loader ()->CompileAllBuiltins (main_context)) {
1027
- return ExitCode::kGenericUserError ;
1028
- }
1029
- env->builtin_loader ()->CopyCodeCache (&(out->code_cache ));
1030
- for (const auto & item : out->code_cache ) {
1031
- std::string size_str = FormatSize (item.data .length );
1032
- per_process::Debug (DebugCategory::MKSNAPSHOT,
1033
- " Generated code cache for %d: %s\n " ,
1034
- item.id .c_str (),
1035
- size_str.c_str ());
1036
- }
1037
- #endif
1038
-
1039
1094
ResetContextSettingsBeforeSnapshot (main_context);
1040
1095
}
1041
1096
0 commit comments