@@ -1155,8 +1155,11 @@ ExitCode SnapshotBuilder::CreateSnapshot(SnapshotData* out,
11551155 CHECK_EQ (index, SnapshotData::kNodeVMContextIndex );
11561156 index = creator->AddContext (base_context);
11571157 CHECK_EQ (index, SnapshotData::kNodeBaseContextIndex );
1158- index = creator->AddContext (main_context,
1159- {SerializeNodeContextInternalFields, env});
1158+ index = creator->AddContext (
1159+ main_context,
1160+ v8::SerializeInternalFieldsCallback (SerializeNodeContextInternalFields,
1161+ env),
1162+ v8::SerializeContextDataCallback (SerializeNodeContextData, env));
11601163 CHECK_EQ (index, SnapshotData::kNodeMainContextIndex );
11611164 }
11621165
@@ -1255,6 +1258,17 @@ std::string SnapshotableObject::GetTypeName() const {
12551258 }
12561259}
12571260
1261+ void DeserializeNodeContextData (Local<Context> holder,
1262+ int index,
1263+ StartupData payload,
1264+ void * callback_data) {
1265+ DCHECK (index == ContextEmbedderIndex::kEnvironment ||
1266+ index == ContextEmbedderIndex::kRealm ||
1267+ index == ContextEmbedderIndex::kContextTag );
1268+ // This is a no-op for now. We will reset all the pointers in
1269+ // Environment::AssignToContext() via the realm constructor.
1270+ }
1271+
12581272void DeserializeNodeInternalFields (Local<Object> holder,
12591273 int index,
12601274 StartupData payload,
@@ -1320,6 +1334,44 @@ void DeserializeNodeInternalFields(Local<Object> holder,
13201334 }
13211335}
13221336
1337+ StartupData SerializeNodeContextData (Local<Context> holder,
1338+ int index,
1339+ void * callback_data) {
1340+ DCHECK (index == ContextEmbedderIndex::kEnvironment ||
1341+ index == ContextEmbedderIndex::kContextifyContext ||
1342+ index == ContextEmbedderIndex::kRealm ||
1343+ index == ContextEmbedderIndex::kContextTag );
1344+ void * data = holder->GetAlignedPointerFromEmbedderData (index);
1345+ per_process::Debug (DebugCategory::MKSNAPSHOT,
1346+ " Serialize context data, index=%d, holder=%p, ptr=%p\n " ,
1347+ static_cast <int >(index),
1348+ *holder,
1349+ data);
1350+ // Serialization of contextify context is not yet supported.
1351+ if (index == ContextEmbedderIndex::kContextifyContext ) {
1352+ DCHECK_NULL (data);
1353+ return {nullptr , 0 };
1354+ }
1355+
1356+ // We need to use use new[] because V8 calls delete[] on the returned data.
1357+ int size = sizeof (ContextEmbedderIndex);
1358+ char * result = new char [size];
1359+ ContextEmbedderIndex* index_data =
1360+ reinterpret_cast <ContextEmbedderIndex*>(result);
1361+ *index_data = static_cast <ContextEmbedderIndex>(index);
1362+
1363+ // For now we just reset all of them in Environment::AssignToContext()
1364+ switch (index) {
1365+ case ContextEmbedderIndex::kEnvironment :
1366+ case ContextEmbedderIndex::kContextifyContext :
1367+ case ContextEmbedderIndex::kRealm :
1368+ case ContextEmbedderIndex::kContextTag :
1369+ return StartupData{result, size};
1370+ default :
1371+ UNREACHABLE ();
1372+ }
1373+ }
1374+
13231375StartupData SerializeNodeContextInternalFields (Local<Object> holder,
13241376 int index,
13251377 void * callback_data) {
0 commit comments