@@ -638,7 +638,7 @@ template <>
638
638
EnvSerializeInfo FileReader::Read () {
639
639
per_process::Debug (DebugCategory::MKSNAPSHOT, " Read<EnvSerializeInfo>()\n " );
640
640
EnvSerializeInfo result;
641
- result.bindings = ReadVector<PropInfo>();
641
+ result.native_objects = ReadVector<PropInfo>();
642
642
result.builtins = ReadVector<std::string>();
643
643
result.async_hooks = Read<AsyncHooks::SerializeInfo>();
644
644
result.tick_info = Read<TickInfo::SerializeInfo>();
@@ -661,7 +661,7 @@ size_t FileWriter::Write(const EnvSerializeInfo& data) {
661
661
}
662
662
663
663
// 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 );
665
665
written_total += WriteVector<std::string>(data.builtins );
666
666
written_total += Write<AsyncHooks::SerializeInfo>(data.async_hooks );
667
667
written_total += Write<TickInfo::SerializeInfo>(data.tick_info );
@@ -1179,17 +1179,6 @@ const char* SnapshotableObject::GetTypeNameChars() const {
1179
1179
}
1180
1180
}
1181
1181
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
-
1193
1182
void DeserializeNodeInternalFields (Local<Object> holder,
1194
1183
int index,
1195
1184
StartupData payload,
@@ -1212,10 +1201,10 @@ void DeserializeNodeInternalFields(Local<Object> holder,
1212
1201
DCHECK_EQ (index , BaseObject::kEmbedderType );
1213
1202
1214
1203
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 );
1217
1206
// 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
1219
1208
// step on payloads that were not serialized by Node.js.
1220
1209
switch (info->type ) {
1221
1210
#define V (PropertyName, NativeTypeName ) \
@@ -1225,12 +1214,25 @@ void DeserializeNodeInternalFields(Local<Object> holder,
1225
1214
(*holder), \
1226
1215
NativeTypeName::type_name.c_str ()); \
1227
1216
env_ptr->EnqueueDeserializeRequest ( \
1228
- NativeTypeName::Deserialize, holder, index , info->Copy ()); \
1217
+ NativeTypeName::Deserialize, \
1218
+ holder, \
1219
+ index , \
1220
+ info->Copy <NativeTypeName::InternalFieldInfo>()); \
1229
1221
break ; \
1230
1222
}
1231
1223
SERIALIZABLE_OBJECT_TYPES (V)
1232
1224
#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
+ }
1234
1236
}
1235
1237
}
1236
1238
@@ -1263,17 +1265,17 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
1263
1265
static_cast <int >(index ),
1264
1266
*holder);
1265
1267
1266
- void * binding_ptr =
1268
+ void * native_ptr =
1267
1269
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 );
1271
1273
1272
1274
per_process::Debug (DebugCategory::MKSNAPSHOT,
1273
1275
" Object %p is %s, " ,
1274
1276
*holder,
1275
1277
obj->GetTypeNameChars ());
1276
- InternalFieldInfo * info = obj->Serialize (index );
1278
+ InternalFieldInfoBase * info = obj->Serialize (index );
1277
1279
1278
1280
per_process::Debug (DebugCategory::MKSNAPSHOT,
1279
1281
" payload size=%d\n " ,
@@ -1282,31 +1284,35 @@ StartupData SerializeNodeContextInternalFields(Local<Object> holder,
1282
1284
static_cast <int >(info->length )};
1283
1285
}
1284
1286
1285
- void SerializeBindingData (Environment* env,
1286
- SnapshotCreator* creator,
1287
- EnvSerializeInfo* info) {
1287
+ void SerializeSnapshotableObjects (Environment* env,
1288
+ SnapshotCreator* creator,
1289
+ EnvSerializeInfo* info) {
1288
1290
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 ();
1291
1301
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 " ,
1293
1304
static_cast <int >(i),
1294
- binding. get () ,
1295
- *(binding ->object ()),
1296
- key. c_str () );
1305
+ ptr ,
1306
+ *(ptr ->object ()),
1307
+ type_name );
1297
1308
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 ());
1300
1311
per_process::Debug (DebugCategory::MKSNAPSHOT,
1301
1312
" Serialized with index=%d\n " ,
1302
1313
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 });
1308
1315
}
1309
-
1310
1316
i++;
1311
1317
});
1312
1318
}
0 commit comments