@@ -638,7 +638,7 @@ template <>
638638EnvSerializeInfo FileReader::Read () {
639639 per_process::Debug (DebugCategory::MKSNAPSHOT, " Read<EnvSerializeInfo>()\n " );
640640 EnvSerializeInfo result;
641- result.bindings = ReadVector<PropInfo>();
641+ result.native_objects = ReadVector<PropInfo>();
642642 result.builtins = ReadVector<std::string>();
643643 result.async_hooks = Read<AsyncHooks::SerializeInfo>();
644644 result.tick_info = Read<TickInfo::SerializeInfo>();
@@ -661,7 +661,7 @@ size_t FileWriter::Write(const EnvSerializeInfo& data) {
661661 }
662662
663663 // Use += here to ensure order of evaluation.
664- size_t written_total = WriteVector<PropInfo>(data.bindings );
664+ size_t written_total = WriteVector<PropInfo>(data.native_objects );
665665 written_total += WriteVector<std::string>(data.builtins );
666666 written_total += Write<AsyncHooks::SerializeInfo>(data.async_hooks );
667667 written_total += Write<TickInfo::SerializeInfo>(data.tick_info );
@@ -1179,17 +1179,6 @@ const char* SnapshotableObject::GetTypeNameChars() const {
11791179 }
11801180}
11811181
1182- bool IsSnapshotableType (FastStringKey key) {
1183- #define V (PropertyName, NativeTypeName ) \
1184- if (key == NativeTypeName::type_name) { \
1185- return true ; \
1186- }
1187- SERIALIZABLE_OBJECT_TYPES (V)
1188- #undef V
1189-
1190- return false ;
1191- }
1192-
11931182void DeserializeNodeInternalFields (Local<Object> holder,
11941183 int index,
11951184 StartupData payload,
@@ -1212,10 +1201,10 @@ void DeserializeNodeInternalFields(Local<Object> holder,
12121201 DCHECK_EQ (index, BaseObject::kEmbedderType );
12131202
12141203 Environment* env_ptr = static_cast <Environment*>(env);
1215- const InternalFieldInfo * info =
1216- reinterpret_cast <const InternalFieldInfo *>(payload.data );
1204+ const InternalFieldInfoBase * info =
1205+ reinterpret_cast <const InternalFieldInfoBase *>(payload.data );
12171206 // TODO(joyeecheung): we can add a constant kNodeEmbedderId to the
1218- // beginning of every InternalFieldInfo to ensure that we don't
1207+ // beginning of every InternalFieldInfoBase to ensure that we don't
12191208 // step on payloads that were not serialized by Node.js.
12201209 switch (info->type ) {
12211210#define V (PropertyName, NativeTypeName ) \
@@ -1225,12 +1214,25 @@ void DeserializeNodeInternalFields(Local<Object> holder,
12251214 (*holder), \
12261215 NativeTypeName::type_name.c_str ()); \
12271216 env_ptr->EnqueueDeserializeRequest ( \
1228- NativeTypeName::Deserialize, holder, index, info->Copy ()); \
1217+ NativeTypeName::Deserialize, \
1218+ holder, \
1219+ index, \
1220+ info->Copy <NativeTypeName::InternalFieldInfo>()); \
12291221 break ; \
12301222 }
12311223 SERIALIZABLE_OBJECT_TYPES (V)
12321224#undef V
1233- default : { UNREACHABLE (); }
1225+ default : {
1226+ // This should only be reachable during development when trying to
1227+ // deserialize a snapshot blob built by a version of Node.js that
1228+ // has more recognizable EmbedderObjectTypes than the deserializing
1229+ // Node.js binary.
1230+ fprintf (stderr,
1231+ " Unknown embedder object type %" PRIu8 " , possibly caused by "
1232+ " mismatched Node.js versions\n " ,
1233+ static_cast <uint8_t >(info->type ));
1234+ ABORT ();
1235+ }
12341236 }
12351237}
12361238
@@ -1263,17 +1265,17 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
12631265 static_cast <int >(index),
12641266 *holder);
12651267
1266- void * binding_ptr =
1268+ void * native_ptr =
12671269 holder->GetAlignedPointerFromInternalField (BaseObject::kSlot );
1268- per_process::Debug (DebugCategory::MKSNAPSHOT, " binding = %p\n " , binding_ptr );
1269- DCHECK (static_cast <BaseObject*>(binding_ptr )->is_snapshotable ());
1270- SnapshotableObject* obj = static_cast <SnapshotableObject*>(binding_ptr );
1270+ per_process::Debug (DebugCategory::MKSNAPSHOT, " native = %p\n " , native_ptr );
1271+ DCHECK (static_cast <BaseObject*>(native_ptr )->is_snapshotable ());
1272+ SnapshotableObject* obj = static_cast <SnapshotableObject*>(native_ptr );
12711273
12721274 per_process::Debug (DebugCategory::MKSNAPSHOT,
12731275 " Object %p is %s, " ,
12741276 *holder,
12751277 obj->GetTypeNameChars ());
1276- InternalFieldInfo * info = obj->Serialize (index);
1278+ InternalFieldInfoBase * info = obj->Serialize (index);
12771279
12781280 per_process::Debug (DebugCategory::MKSNAPSHOT,
12791281 " payload size=%d\n " ,
@@ -1282,31 +1284,35 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
12821284 static_cast <int >(info->length )};
12831285}
12841286
1285- void SerializeBindingData (Environment* env,
1286- SnapshotCreator* creator,
1287- EnvSerializeInfo* info) {
1287+ void SerializeSnapshotableObjects (Environment* env,
1288+ SnapshotCreator* creator,
1289+ EnvSerializeInfo* info) {
12881290 uint32_t i = 0 ;
1289- env->ForEachBindingData ([&](FastStringKey key,
1290- BaseObjectPtr<BaseObject> binding) {
1291+ env->ForEachBaseObject ([&](BaseObject* obj) {
1292+ // If there are any BaseObjects that are not snapshotable left
1293+ // during context serialization, V8 would crash due to unregistered
1294+ // global handles and print detailed information about them.
1295+ if (!obj->is_snapshotable ()) {
1296+ return ;
1297+ }
1298+ SnapshotableObject* ptr = static_cast <SnapshotableObject*>(obj);
1299+
1300+ const char * type_name = ptr->GetTypeNameChars ();
12911301 per_process::Debug (DebugCategory::MKSNAPSHOT,
1292- " Serialize binding %i (%p), object=%p, type=%s\n " ,
1302+ " Serialize snapshotable object %i (%p), "
1303+ " object=%p, type=%s\n " ,
12931304 static_cast <int >(i),
1294- binding. get () ,
1295- *(binding ->object ()),
1296- key. c_str () );
1305+ ptr ,
1306+ *(ptr ->object ()),
1307+ type_name );
12971308
1298- if (IsSnapshotableType (key )) {
1299- SnapshotIndex index = creator->AddData (env->context (), binding ->object ());
1309+ if (ptr-> PrepareForSerialization (env-> context (), creator )) {
1310+ SnapshotIndex index = creator->AddData (env->context (), obj ->object ());
13001311 per_process::Debug (DebugCategory::MKSNAPSHOT,
13011312 " Serialized with index=%d\n " ,
13021313 static_cast <int >(index));
1303- info->bindings .push_back ({key.c_str (), i, index});
1304- SnapshotableObject* ptr = static_cast <SnapshotableObject*>(binding.get ());
1305- ptr->PrepareForSerialization (env->context (), creator);
1306- } else {
1307- UNREACHABLE ();
1314+ info->native_objects .push_back ({type_name, i, index});
13081315 }
1309-
13101316 i++;
13111317 });
13121318}
0 commit comments