33#include < iostream>
44#include < sstream>
55#include < vector>
6+ #include " aliased_buffer-inl.h"
67#include " base_object-inl.h"
78#include " blob_serializer_deserializer-inl.h"
89#include " debug_utils-inl.h"
@@ -34,6 +35,7 @@ namespace node {
3435using v8::Context;
3536using v8::Function;
3637using v8::FunctionCallbackInfo;
38+ using v8::FunctionTemplate;
3739using v8::HandleScope;
3840using v8::Isolate;
3941using v8::Local;
@@ -1177,8 +1179,6 @@ void SerializeSnapshotableObjects(Realm* realm,
11771179 });
11781180}
11791181
1180- namespace mksnapshot {
1181-
11821182// NB: This is also used by the regular embedding codepath.
11831183void GetEmbedderEntryFunction (const FunctionCallbackInfo<Value>& args) {
11841184 Environment* env = Environment::GetCurrent (args);
@@ -1251,16 +1251,89 @@ void SetDeserializeMainFunction(const FunctionCallbackInfo<Value>& args) {
12511251 env->set_snapshot_deserialize_main (args[0 ].As <Function>());
12521252}
12531253
1254- void Initialize (Local<Object> target,
1255- Local<Value> unused,
1256- Local<Context> context,
1257- void * priv) {
1254+ namespace mksnapshot {
1255+
1256+ BindingData::BindingData (Realm* realm,
1257+ v8::Local<v8::Object> object,
1258+ InternalFieldInfo* info)
1259+ : SnapshotableObject(realm, object, type_int),
1260+ is_building_snapshot_buffer_ (
1261+ realm->isolate (),
1262+ 1,
1263+ MAYBE_FIELD_PTR(info, is_building_snapshot_buffer)) {
1264+ if (info == nullptr ) {
1265+ object
1266+ ->Set (
1267+ realm->context (),
1268+ FIXED_ONE_BYTE_STRING (realm->isolate (), " isBuildingSnapshotBuffer" ),
1269+ is_building_snapshot_buffer_.GetJSArray ())
1270+ .Check ();
1271+ } else {
1272+ is_building_snapshot_buffer_.Deserialize (realm->context ());
1273+ }
1274+ // Reset the status according to the current state of the realm.
1275+ bool is_building_snapshot = realm->isolate_data ()->is_building_snapshot ();
1276+ DCHECK_IMPLIES (is_building_snapshot,
1277+ realm->isolate_data ()->snapshot_data () == nullptr );
1278+ is_building_snapshot_buffer_[0 ] = is_building_snapshot ? 1 : 0 ;
1279+ is_building_snapshot_buffer_.MakeWeak ();
1280+ }
1281+
1282+ bool BindingData::PrepareForSerialization (Local<Context> context,
1283+ v8::SnapshotCreator* creator) {
1284+ DCHECK_NULL (internal_field_info_);
1285+ internal_field_info_ = InternalFieldInfoBase::New<InternalFieldInfo>(type ());
1286+ internal_field_info_->is_building_snapshot_buffer =
1287+ is_building_snapshot_buffer_.Serialize (context, creator);
1288+ // Return true because we need to maintain the reference to the binding from
1289+ // JS land.
1290+ return true ;
1291+ }
1292+
1293+ InternalFieldInfoBase* BindingData::Serialize (int index) {
1294+ DCHECK_EQ (index, BaseObject::kEmbedderType );
1295+ InternalFieldInfo* info = internal_field_info_;
1296+ internal_field_info_ = nullptr ;
1297+ return info;
1298+ }
1299+
1300+ void BindingData::Deserialize (Local<Context> context,
1301+ Local<Object> holder,
1302+ int index,
1303+ InternalFieldInfoBase* info) {
1304+ DCHECK_EQ (index, BaseObject::kEmbedderType );
1305+ v8::HandleScope scope (context->GetIsolate ());
1306+ Realm* realm = Realm::GetCurrent (context);
1307+ // Recreate the buffer in the constructor.
1308+ InternalFieldInfo* casted_info = static_cast <InternalFieldInfo*>(info);
1309+ BindingData* binding =
1310+ realm->AddBindingData <BindingData>(context, holder, casted_info);
1311+ CHECK_NOT_NULL (binding);
1312+ }
1313+
1314+ void BindingData::MemoryInfo (MemoryTracker* tracker) const {
1315+ tracker->TrackField (" is_building_snapshot_buffer" ,
1316+ is_building_snapshot_buffer_);
1317+ }
1318+
1319+ void CreatePerContextProperties (Local<Object> target,
1320+ Local<Value> unused,
1321+ Local<Context> context,
1322+ void * priv) {
1323+ Realm* realm = Realm::GetCurrent (context);
1324+ realm->AddBindingData <BindingData>(context, target);
1325+ }
1326+
1327+ void CreatePerIsolateProperties (IsolateData* isolate_data,
1328+ Local<FunctionTemplate> ctor) {
1329+ Isolate* isolate = isolate_data->isolate ();
1330+ Local<ObjectTemplate> target = ctor->PrototypeTemplate ();
12581331 SetMethod (
1259- context , target, " getEmbedderEntryFunction" , GetEmbedderEntryFunction);
1260- SetMethod (context , target, " compileSerializeMain" , CompileSerializeMain);
1261- SetMethod (context , target, " setSerializeCallback" , SetSerializeCallback);
1262- SetMethod (context , target, " setDeserializeCallback" , SetDeserializeCallback);
1263- SetMethod (context ,
1332+ isolate , target, " getEmbedderEntryFunction" , GetEmbedderEntryFunction);
1333+ SetMethod (isolate , target, " compileSerializeMain" , CompileSerializeMain);
1334+ SetMethod (isolate , target, " setSerializeCallback" , SetSerializeCallback);
1335+ SetMethod (isolate , target, " setDeserializeCallback" , SetDeserializeCallback);
1336+ SetMethod (isolate ,
12641337 target,
12651338 " setDeserializeMainFunction" ,
12661339 SetDeserializeMainFunction);
@@ -1274,8 +1347,12 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
12741347 registry->Register (SetDeserializeMainFunction);
12751348}
12761349} // namespace mksnapshot
1350+
12771351} // namespace node
12781352
1279- NODE_BINDING_CONTEXT_AWARE_INTERNAL (mksnapshot, node::mksnapshot::Initialize)
1353+ NODE_BINDING_CONTEXT_AWARE_INTERNAL (
1354+ mksnapshot, node::mksnapshot::CreatePerContextProperties)
1355+ NODE_BINDING_PER_ISOLATE_INIT(mksnapshot,
1356+ node::mksnapshot::CreatePerIsolateProperties)
12801357NODE_BINDING_EXTERNAL_REFERENCE(mksnapshot,
12811358 node::mksnapshot::RegisterExternalReferences)
0 commit comments