33#include  < sstream> 
44#include  " debug_utils-inl.h" 
55#include  " env-inl.h" 
6+ #include  " node_errors.h" 
67#include  " node_external_reference.h" 
78#include  " node_internals.h" 
89#include  " node_main_instance.h" 
@@ -15,10 +16,8 @@ using v8::Context;
1516using  v8::HandleScope;
1617using  v8::Isolate;
1718using  v8::Local;
18- using  v8::Object;
1919using  v8::SnapshotCreator;
2020using  v8::StartupData;
21- using  v8::String;
2221using  v8::TryCatch;
2322using  v8::Value;
2423
@@ -112,55 +111,44 @@ std::string SnapshotBuilder::Generate(
112111      creator.SetDefaultContext (Context::New (isolate));
113112      isolate_data_indexes = main_instance->isolate_data ()->Serialize (&creator);
114113
115-       TryCatch bootstrapCatch (isolate);
116-       Local<Context> context = NewContext (isolate);
117-       if  (bootstrapCatch.HasCaught ()) {
118-         Local<Object> obj = bootstrapCatch.Exception ()->ToObject (context)
119-             .ToLocalChecked ();
120-         Local<Value> stack = obj->Get (
121-             context,
122-             FIXED_ONE_BYTE_STRING (isolate, " stack"  )).ToLocalChecked ();
123-         if  (stack->IsUndefined ()) {
124-           Local<String> str = obj->Get (
125-               context,
126-               FIXED_ONE_BYTE_STRING (isolate, " name"  ))
127-             .ToLocalChecked ()->ToString (context).ToLocalChecked ();
128-           str = String::Concat (
129-             isolate,
130-             str,
131-             FIXED_ONE_BYTE_STRING (isolate, " : "  ));
132-           stack = String::Concat (
133-             isolate,
134-             str,
135-             obj->Get (
136-                 context,
137-                 FIXED_ONE_BYTE_STRING (isolate, " message"  ))
138-               .ToLocalChecked ()->ToString (context).ToLocalChecked ());
114+       //  Run the per-context scripts
115+       Local<Context> context;
116+       {
117+         TryCatch bootstrapCatch (isolate);
118+         context = NewContext (isolate);
119+         if  (bootstrapCatch.HasCaught ()) {
120+           PrintCaughtException (isolate, context, bootstrapCatch);
121+           abort ();
139122        }
140-         v8::String::Utf8Value utf8_value (isolate, stack);
141-         if  (*utf8_value != nullptr ) {
142-           std::string out (*utf8_value, utf8_value.length ());
143-           fprintf (stderr, " Had Exception: %s\n "  , out.c_str ());
144-         } else  {
145-           fprintf (stderr, " Unknown JS Exception\n "  );
146-         }
147-         abort ();
148123      }
149124      Context::Scope context_scope (context);
150125
126+       //  Create the environment
151127      env = new  Environment (main_instance->isolate_data (),
152128                            context,
153129                            args,
154130                            exec_args,
155131                            nullptr ,
156132                            node::EnvironmentFlags::kDefaultFlags ,
157133                            {});
158-       env->RunBootstrapping ().ToLocalChecked ();
134+       //  Run scripts in lib/internal/bootstrap/
135+       {
136+         TryCatch bootstrapCatch (isolate);
137+         v8::MaybeLocal<Value> result = env->RunBootstrapping ();
138+         if  (bootstrapCatch.HasCaught ()) {
139+           PrintCaughtException (isolate, context, bootstrapCatch);
140+         }
141+         result.ToLocalChecked ();
142+       }
143+ 
159144      if  (per_process::enabled_debug_list.enabled (DebugCategory::MKSNAPSHOT)) {
160145        env->PrintAllBaseObjects ();
161146        printf (" Environment = %p\n "  , env);
162147      }
148+ 
149+       //  Serialize the native states
163150      env_info = env->Serialize (&creator);
151+       //  Serialize the context
164152      size_t  index = creator.AddContext (
165153          context, {SerializeNodeContextInternalFields, env});
166154      CHECK_EQ (index, NodeMainInstance::kNodeContextIndex );
0 commit comments