|
3 | 3 | #include <iostream> |
4 | 4 | #include <sstream> |
5 | 5 | #include <vector> |
| 6 | +#include "aliased_buffer-inl.h" |
6 | 7 | #include "base_object-inl.h" |
7 | 8 | #include "blob_serializer_deserializer-inl.h" |
8 | 9 | #include "debug_utils-inl.h" |
@@ -34,6 +35,7 @@ namespace node { |
34 | 35 | using v8::Context; |
35 | 36 | using v8::Function; |
36 | 37 | using v8::FunctionCallbackInfo; |
| 38 | +using v8::FunctionTemplate; |
37 | 39 | using v8::HandleScope; |
38 | 40 | using v8::Isolate; |
39 | 41 | using v8::Local; |
@@ -1177,8 +1179,6 @@ void SerializeSnapshotableObjects(Realm* realm, |
1177 | 1179 | }); |
1178 | 1180 | } |
1179 | 1181 |
|
1180 | | -namespace mksnapshot { |
1181 | | - |
1182 | 1182 | // NB: This is also used by the regular embedding codepath. |
1183 | 1183 | void GetEmbedderEntryFunction(const FunctionCallbackInfo<Value>& args) { |
1184 | 1184 | Environment* env = Environment::GetCurrent(args); |
@@ -1251,16 +1251,89 @@ void SetDeserializeMainFunction(const FunctionCallbackInfo<Value>& args) { |
1251 | 1251 | env->set_snapshot_deserialize_main(args[0].As<Function>()); |
1252 | 1252 | } |
1253 | 1253 |
|
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(); |
1258 | 1331 | 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, |
1264 | 1337 | target, |
1265 | 1338 | "setDeserializeMainFunction", |
1266 | 1339 | SetDeserializeMainFunction); |
@@ -1274,8 +1347,12 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
1274 | 1347 | registry->Register(SetDeserializeMainFunction); |
1275 | 1348 | } |
1276 | 1349 | } // namespace mksnapshot |
| 1350 | + |
1277 | 1351 | } // namespace node |
1278 | 1352 |
|
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) |
1280 | 1357 | NODE_BINDING_EXTERNAL_REFERENCE(mksnapshot, |
1281 | 1358 | node::mksnapshot::RegisterExternalReferences) |