flutter_flutter/runtime/dart_vm_data.cc
Chinmay Garde 0d6ff1669c
Shut down and restart the Dart VM as needed. (#7832)
The shell was already designed to cleanly shut down the VM but it couldnt
earlier as |Dart_Initialize| could never be called after a |Dart_Cleanup|. This
meant that shutting down an engine instance could not shut down the VM to save
memory because newly created engines in the process after that point couldn't
restart the VM. There can only be one VM running in a process at a time.

This patch separate the previous DartVM object into one that references a
running instance of the DartVM and a set of immutable dependencies that
components can reference even as the VM is shutting down.

Unit tests have been added to assert that non-overlapping engine launches use
difference VM instances.
2019-02-15 14:16:17 -08:00

80 lines
2.4 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/runtime/dart_vm_data.h"
namespace blink {
std::shared_ptr<const DartVMData> DartVMData::Create(
Settings settings,
fml::RefPtr<DartSnapshot> vm_snapshot,
fml::RefPtr<DartSnapshot> isolate_snapshot,
fml::RefPtr<DartSnapshot> shared_snapshot) {
if (!vm_snapshot || !vm_snapshot->IsValid()) {
// Caller did not provide a valid VM snapshot. Attempt to infer one
// from the settings.
vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings);
if (!vm_snapshot) {
FML_LOG(ERROR)
<< "VM snapshot invalid and could not be inferred from settings.";
return {};
}
}
if (!isolate_snapshot || !isolate_snapshot->IsValid()) {
// Caller did not provide a valid isolate snapshot. Attempt to infer one
// from the settings.
isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings);
if (!isolate_snapshot) {
FML_LOG(ERROR) << "Isolate snapshot invalid and could not be inferred "
"from settings.";
return {};
}
}
if (!shared_snapshot || !shared_snapshot->IsValid()) {
shared_snapshot = DartSnapshot::Empty();
if (!shared_snapshot) {
FML_LOG(ERROR) << "Shared snapshot invalid.";
return {};
}
}
return std::shared_ptr<const DartVMData>(new DartVMData(
std::move(settings), //
std::move(vm_snapshot), //
std::move(isolate_snapshot), //
std::move(shared_snapshot) //
));
}
DartVMData::DartVMData(Settings settings,
fml::RefPtr<const DartSnapshot> vm_snapshot,
fml::RefPtr<const DartSnapshot> isolate_snapshot,
fml::RefPtr<const DartSnapshot> shared_snapshot)
: settings_(settings),
vm_snapshot_(vm_snapshot),
isolate_snapshot_(isolate_snapshot),
shared_snapshot_(shared_snapshot) {}
DartVMData::~DartVMData() = default;
const Settings& DartVMData::GetSettings() const {
return settings_;
}
const DartSnapshot& DartVMData::GetVMSnapshot() const {
return *vm_snapshot_;
}
fml::RefPtr<const DartSnapshot> DartVMData::GetIsolateSnapshot() const {
return isolate_snapshot_;
}
fml::RefPtr<const DartSnapshot> DartVMData::GetSharedSnapshot() const {
return shared_snapshot_;
}
} // namespace blink