| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8d93af3 commit c480559
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,11 +14,11 @@ const { | |||
| 14 | 14 | setSerializeCallback, | |
| 15 | 15 | setDeserializeCallback, | |
| 16 | 16 | setDeserializeMainFunction: _setDeserializeMainFunction, | |
| 17 | + isBuildingSnapshotBuffer | ||
| 17 | 18 | } = internalBinding('mksnapshot'); | |
| 18 | 19 | ||
| 19 | 20 | function isBuildingSnapshot() { | |
| 20 | - // For now this is the only way to build a snapshot. | ||
| 21 | - return require('internal/options').getOptionValue('--build-snapshot'); | ||
| 21 | + return isBuildingSnapshotBuffer[0]; | ||
| 22 | 22 | } | |
| 23 | 23 | ||
| 24 | 24 | function throwIfNotBuildingSnapshot() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -142,8 +142,8 @@ CommonEnvironmentSetup::CommonEnvironmentSetup( | |||
| 142 | 142 | ||
| 143 | 143 | impl_->isolate_data.reset(CreateIsolateData( | |
| 144 | 144 | isolate, loop, platform, impl_->allocator.get(), snapshot_data)); | |
| 145 | - impl_->isolate_data->options()->build_snapshot = | ||
| 146 | - impl_->snapshot_creator.has_value(); | ||
| 145 | + impl_->isolate_data->set_is_building_snapshot( | ||
| 146 | + impl_->snapshot_creator.has_value()); | ||
| 147 | 147 | ||
| 148 | 148 | if (snapshot_data) { | |
| 149 | 149 | impl_->env.reset(make_env(this)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ namespace node { | |||
| 12 | 12 | #define SERIALIZABLE_BINDING_TYPES(V) \ | |
| 13 | 13 | V(encoding_binding_data, encoding_binding::BindingData) \ | |
| 14 | 14 | V(fs_binding_data, fs::BindingData) \ | |
| 15 | + V(mksnapshot_binding_data, mksnapshot::BindingData) \ | ||
| 15 | 16 | V(v8_binding_data, v8_utils::BindingData) \ | |
| 16 | 17 | V(blob_binding_data, BlobBindingData) \ | |
| 17 | 18 | V(process_binding_data, process::BindingData) \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -136,6 +136,9 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { | |||
| 136 | 136 | void MemoryInfo(MemoryTracker* tracker) const override; | |
| 137 | 137 | IsolateDataSerializeInfo Serialize(v8::SnapshotCreator* creator); | |
| 138 | 138 | ||
| 139 | + bool is_building_snapshot() const { return is_building_snapshot_; } | ||
| 140 | + void set_is_building_snapshot(bool value) { is_building_snapshot_ = value; } | ||
| 141 | + | ||
| 139 | 142 | inline uv_loop_t* event_loop() const; | |
| 140 | 143 | inline MultiIsolatePlatform* platform() const; | |
| 141 | 144 | inline const SnapshotData* snapshot_data() const; | |
@@ -219,6 +222,7 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { | |||
| 219 | 222 | const SnapshotData* snapshot_data_; | |
| 220 | 223 | std::shared_ptr<PerIsolateOptions> options_; | |
| 221 | 224 | worker::Worker* worker_context_ = nullptr; | |
| 225 | + bool is_building_snapshot_ = false; | ||
| 222 | 226 | }; | |
| 223 | 227 | ||
| 224 | 228 | struct ContextInfo { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -283,7 +283,7 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) { | |||
| 283 | 283 | auto reset_entry_point = | |
| 284 | 284 | OnScopeLeave([&]() { env->set_embedder_entry_point({}); }); | |
| 285 | 285 | ||
| 286 | - const char* entry = env->isolate_data()->options()->build_snapshot | ||
| 286 | + const char* entry = env->isolate_data()->is_building_snapshot() | ||
| 287 | 287 | ? "internal/main/mksnapshot" | |
| 288 | 288 | : "internal/main/embedding"; | |
| 289 | 289 | ||
@@ -311,7 +311,7 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) { | |||
| 311 | 311 | return StartExecution(env, "internal/main/inspect"); | |
| 312 | 312 | } | |
| 313 | 313 | ||
| 314 | - if (env->isolate_data()->options()->build_snapshot) { | ||
| 314 | + if (env->isolate_data()->is_building_snapshot()) { | ||
| 315 | 315 | return StartExecution(env, "internal/main/mksnapshot"); | |
| 316 | 316 | } | |
| 317 | 317 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,6 +37,7 @@ static_assert(static_cast<int>(NM_F_LINKED) == | |||
| 37 | 37 | V(contextify) \ | |
| 38 | 38 | V(encoding_binding) \ | |
| 39 | 39 | V(fs) \ | |
| 40 | + V(mksnapshot) \ | ||
| 40 | 41 | V(timers) \ | |
| 41 | 42 | V(process_methods) \ | |
| 42 | 43 | V(performance) \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,6 +56,8 @@ NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data, | |||
| 56 | 56 | platform, | |
| 57 | 57 | array_buffer_allocator_.get(), | |
| 58 | 58 | snapshot_data->AsEmbedderWrapper().get())); | |
| 59 | + isolate_data_->set_is_building_snapshot( | ||
| 60 | + per_process::cli_options->per_isolate->build_snapshot); | ||
| 59 | 61 | ||
| 60 | 62 | isolate_data_->max_young_gen_size = | |
| 61 | 63 | isolate_params_->constraints.max_young_generation_size_in_bytes(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 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) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | 5 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| 6 | 6 | ||
| 7 | + #include "aliased_buffer.h" | ||
| 7 | 8 | #include "base_object.h" | |
| 8 | 9 | #include "util.h" | |
| 9 | 10 | ||
@@ -121,6 +122,31 @@ void DeserializeNodeInternalFields(v8::Local<v8::Object> holder, | |||
| 121 | 122 | void SerializeSnapshotableObjects(Realm* realm, | |
| 122 | 123 | v8::SnapshotCreator* creator, | |
| 123 | 124 | RealmSerializeInfo* info); | |
| 125 | + | ||
| 126 | + namespace mksnapshot { | ||
| 127 | + class BindingData : public SnapshotableObject { | ||
| 128 | + public: | ||
| 129 | + struct InternalFieldInfo : public node::InternalFieldInfoBase { | ||
| 130 | + AliasedBufferIndex is_building_snapshot_buffer; | ||
| 131 | + }; | ||
| 132 | + | ||
| 133 | + BindingData(Realm* realm, | ||
| 134 | + v8::Local<v8::Object> obj, | ||
| 135 | + InternalFieldInfo* info = nullptr); | ||
| 136 | + SET_BINDING_ID(mksnapshot_binding_data) | ||
| 137 | + SERIALIZABLE_OBJECT_METHODS() | ||
| 138 | + | ||
| 139 | + void MemoryInfo(MemoryTracker* tracker) const override; | ||
| 140 | + SET_SELF_SIZE(BindingData) | ||
| 141 | + SET_MEMORY_INFO_NAME(BindingData) | ||
| 142 | + | ||
| 143 | + private: | ||
| 144 | + AliasedUint8Array is_building_snapshot_buffer_; | ||
| 145 | + InternalFieldInfo* internal_field_info_ = nullptr; | ||
| 146 | + }; | ||
| 147 | + | ||
| 148 | + } // namespace mksnapshot | ||
| 149 | + | ||
| 124 | 150 | } // namespace node | |
| 125 | 151 | ||
| 126 | 152 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -186,6 +186,7 @@ class WorkerThreadData { | |||
| 186 | 186 | allocator.get(), | |
| 187 | 187 | w->snapshot_data()->AsEmbedderWrapper().get())); | |
| 188 | 188 | CHECK(isolate_data_); | |
| 189 | + CHECK(!isolate_data_->is_building_snapshot()); | ||
| 189 | 190 | if (w_->per_isolate_opts_) | |
| 190 | 191 | isolate_data_->set_options(std::move(w_->per_isolate_opts_)); | |
| 191 | 192 | isolate_data_->set_worker_context(w_); | |
@@ -481,6 +482,7 @@ void Worker::New(const FunctionCallbackInfo<Value>& args) { | |||
| 481 | 482 | THROW_ERR_MISSING_PLATFORM_FOR_WORKER(env); | |
| 482 | 483 | return; | |
| 483 | 484 | } | |
| 485 | + CHECK(!env->isolate_data()->is_building_snapshot()); | ||
| 484 | 486 | ||
| 485 | 487 | std::string url; | |
| 486 | 488 | std::string name; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments