@@ -640,7 +640,7 @@ template <>
640640EnvSerializeInfo FileReader::Read () {
641641 per_process::Debug (DebugCategory::MKSNAPSHOT, " Read<EnvSerializeInfo>()\n " );
642642 EnvSerializeInfo result;
643- result.bindings = ReadVector<PropInfo>();
643+ result.native_objects = ReadVector<PropInfo>();
644644 result.builtins = ReadVector<std::string>();
645645 result.async_hooks = Read<AsyncHooks::SerializeInfo>();
646646 result.tick_info = Read<TickInfo::SerializeInfo>();
@@ -663,7 +663,7 @@ size_t FileWriter::Write(const EnvSerializeInfo& data) {
663663 }
664664
665665 // Use += here to ensure order of evaluation.
666- size_t written_total = WriteVector<PropInfo>(data.bindings );
666+ size_t written_total = WriteVector<PropInfo>(data.native_objects );
667667 written_total += WriteVector<std::string>(data.builtins );
668668 written_total += Write<AsyncHooks::SerializeInfo>(data.async_hooks );
669669 written_total += Write<TickInfo::SerializeInfo>(data.tick_info );
@@ -1209,17 +1209,6 @@ const char* SnapshotableObject::GetTypeNameChars() const {
12091209 }
12101210}
12111211
1212- bool IsSnapshotableType (FastStringKey key) {
1213- #define V (PropertyName, NativeTypeName ) \
1214- if (key == NativeTypeName::type_name) { \
1215- return true ; \
1216- }
1217- SERIALIZABLE_OBJECT_TYPES (V)
1218- #undef V
1219-
1220- return false ;
1221- }
1222-
12231212void DeserializeNodeInternalFields (Local<Object> holder,
12241213 int index,
12251214 StartupData payload,
@@ -1242,10 +1231,10 @@ void DeserializeNodeInternalFields(Local<Object> holder,
12421231 DCHECK_EQ (index, BaseObject::kEmbedderType );
12431232
12441233 Environment* env_ptr = static_cast <Environment*>(env);
1245- const InternalFieldInfo * info =
1246- reinterpret_cast <const InternalFieldInfo *>(payload.data );
1234+ const InternalFieldInfoBase * info =
1235+ reinterpret_cast <const InternalFieldInfoBase *>(payload.data );
12471236 // TODO(joyeecheung): we can add a constant kNodeEmbedderId to the
1248- // beginning of every InternalFieldInfo to ensure that we don't
1237+ // beginning of every InternalFieldInfoBase to ensure that we don't
12491238 // step on payloads that were not serialized by Node.js.
12501239 switch (info->type ) {
12511240#define V (PropertyName, NativeTypeName ) \
@@ -1255,12 +1244,25 @@ void DeserializeNodeInternalFields(Local<Object> holder,
12551244 (*holder), \
12561245 NativeTypeName::type_name.c_str ()); \
12571246 env_ptr->EnqueueDeserializeRequest ( \
1258- NativeTypeName::Deserialize, holder, index, info->Copy ()); \
1247+ NativeTypeName::Deserialize, \
1248+ holder, \
1249+ index, \
1250+ info->Copy <NativeTypeName::InternalFieldInfo>()); \
12591251 break ; \
12601252 }
12611253 SERIALIZABLE_OBJECT_TYPES (V)
12621254#undef V
1263- default : { UNREACHABLE (); }
1255+ default : {
1256+ // This should only be reachable during development when trying to
1257+ // deserialize a snapshot blob built by a version of Node.js that
1258+ // has more recognizable EmbedderObjectTypes than the deserializing
1259+ // Node.js binary.
1260+ fprintf (stderr,
1261+ " Unknown embedder object type %" PRIu8 " , possibly caused by "
1262+ " mismatched Node.js versions\n " ,
1263+ static_cast <uint8_t >(info->type ));
1264+ ABORT ();
1265+ }
12641266 }
12651267}
12661268
@@ -1293,17 +1295,17 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
12931295 static_cast <int >(index),
12941296 *holder);
12951297
1296- void * binding_ptr =
1298+ void * native_ptr =
12971299 holder->GetAlignedPointerFromInternalField (BaseObject::kSlot );
1298- per_process::Debug (DebugCategory::MKSNAPSHOT, " binding = %p\n " , binding_ptr );
1299- DCHECK (static_cast <BaseObject*>(binding_ptr )->is_snapshotable ());
1300- SnapshotableObject* obj = static_cast <SnapshotableObject*>(binding_ptr );
1300+ per_process::Debug (DebugCategory::MKSNAPSHOT, " native = %p\n " , native_ptr );
1301+ DCHECK (static_cast <BaseObject*>(native_ptr )->is_snapshotable ());
1302+ SnapshotableObject* obj = static_cast <SnapshotableObject*>(native_ptr );
13011303
13021304 per_process::Debug (DebugCategory::MKSNAPSHOT,
13031305 " Object %p is %s, " ,
13041306 *holder,
13051307 obj->GetTypeNameChars ());
1306- InternalFieldInfo * info = obj->Serialize (index);
1308+ InternalFieldInfoBase * info = obj->Serialize (index);
13071309
13081310 per_process::Debug (DebugCategory::MKSNAPSHOT,
13091311 " payload size=%d\n " ,
@@ -1312,31 +1314,35 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
13121314 static_cast <int >(info->length )};
13131315}
13141316
1315- void SerializeBindingData (Environment* env,
1316- SnapshotCreator* creator,
1317- EnvSerializeInfo* info) {
1317+ void SerializeSnapshotableObjects (Environment* env,
1318+ SnapshotCreator* creator,
1319+ EnvSerializeInfo* info) {
13181320 uint32_t i = 0 ;
1319- env->ForEachBindingData ([&](FastStringKey key,
1320- BaseObjectPtr<BaseObject> binding) {
1321+ env->ForEachBaseObject ([&](BaseObject* obj) {
1322+ // If there are any BaseObjects that are not snapshotable left
1323+ // during context serialization, V8 would crash due to unregistered
1324+ // global handles and print detailed information about them.
1325+ if (!obj->is_snapshotable ()) {
1326+ return ;
1327+ }
1328+ SnapshotableObject* ptr = static_cast <SnapshotableObject*>(obj);
1329+
1330+ const char * type_name = ptr->GetTypeNameChars ();
13211331 per_process::Debug (DebugCategory::MKSNAPSHOT,
1322- " Serialize binding %i (%p), object=%p, type=%s\n " ,
1332+ " Serialize snapshotable object %i (%p), "
1333+ " object=%p, type=%s\n " ,
13231334 static_cast <int >(i),
1324- binding. get () ,
1325- *(binding ->object ()),
1326- key. c_str () );
1335+ ptr ,
1336+ *(ptr ->object ()),
1337+ type_name );
13271338
1328- if (IsSnapshotableType (key )) {
1329- SnapshotIndex index = creator->AddData (env->context (), binding ->object ());
1339+ if (ptr-> PrepareForSerialization (env-> context (), creator )) {
1340+ SnapshotIndex index = creator->AddData (env->context (), obj ->object ());
13301341 per_process::Debug (DebugCategory::MKSNAPSHOT,
13311342 " Serialized with index=%d\n " ,
13321343 static_cast <int >(index));
1333- info->bindings .push_back ({key.c_str (), i, index});
1334- SnapshotableObject* ptr = static_cast <SnapshotableObject*>(binding.get ());
1335- ptr->PrepareForSerialization (env->context (), creator);
1336- } else {
1337- UNREACHABLE ();
1344+ info->native_objects .push_back ({type_name, i, index});
13381345 }
1339-
13401346 i++;
13411347 });
13421348}
0 commit comments