Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ deps = {
'packages': [
{
'package': 'flutter/android/embedding_bundle',
'version': 'last_updated:2020-05-20T01:36:16-0700'
'version': 'last_updated:2020-09-11T17:57:41-0700'
}
],
'condition': 'download_android_deps',
Expand Down
2 changes: 2 additions & 0 deletions runtime/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ group("libdart") {

source_set("runtime") {
sources = [
"dart_deferred_load_handler.cc",
"dart_deferred_load_handler.h",
"dart_isolate.cc",
"dart_isolate.h",
"dart_isolate_group_data.cc",
Expand Down
38 changes: 38 additions & 0 deletions runtime/dart_deferred_load_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2020 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_deferred_load_handler.h"

#include "flutter/runtime/runtime_controller.h"

namespace flutter {

void* DartDeferredLoadHandler::runtime_controller_ = nullptr;

Dart_DeferredLoadHandler
DartDeferredLoadHandler::empty_dart_deferred_load_handler =
&DartDeferredLoadHandler::EmptyDartLoadLibrary;

Dart_DeferredLoadHandler DartDeferredLoadHandler::dart_deferred_load_handler =
&DartDeferredLoadHandler::OnDartLoadLibrary;

void DartDeferredLoadHandler::RegisterRuntimeController(
void* runtime_controller) {
runtime_controller_ = runtime_controller;
}

Dart_Handle DartDeferredLoadHandler::OnDartLoadLibrary(
intptr_t loading_unit_id) {
if (runtime_controller_ != nullptr)
return static_cast<RuntimeController*>(runtime_controller_)
->OnDartLoadLibrary(loading_unit_id);
return Dart_Null();
}

Dart_Handle DartDeferredLoadHandler::EmptyDartLoadLibrary(
intptr_t loading_unit_id) {
return Dart_Null();
}

} // namespace flutter
33 changes: 33 additions & 0 deletions runtime/dart_deferred_load_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2020 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.

#ifndef FLUTTER_RUNTIME_DART_DEFERRED_LOAD_HANDLER_H_
#define FLUTTER_RUNTIME_DART_DEFERRED_LOAD_HANDLER_H_

#include <memory>
#include <string>

#include "third_party/dart/runtime/include/dart_api.h"

namespace flutter {

class DartDeferredLoadHandler {
public:
// TODO: Fix deps to not use void*
static void RegisterRuntimeController(void* runtime_controller);

static Dart_DeferredLoadHandler empty_dart_deferred_load_handler;
static Dart_DeferredLoadHandler dart_deferred_load_handler;

private:
static Dart_Handle OnDartLoadLibrary(intptr_t loading_unit_id);
// No-op function that returns Dart_Null() for when the isolate is not
// expected to handle deferred libraries.
static Dart_Handle EmptyDartLoadLibrary(intptr_t loading_unit_id);
static void* runtime_controller_;
};

} // namespace flutter

#endif // FLUTTER_RUNTIME_DART_DEFERRED_LOAD_HANDLER_H_
80 changes: 62 additions & 18 deletions runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "flutter/runtime/dart_isolate.h"
#include "flutter/runtime/dart_deferred_load_handler.h"

#include <cstdlib>
#include <tuple>
Expand Down Expand Up @@ -86,6 +87,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
std::string advisory_script_uri,
std::string advisory_script_entrypoint,
Flags isolate_flags,
Dart_DeferredLoadHandler& deferred_load_handler,
const fml::closure& isolate_create_callback,
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
Expand Down Expand Up @@ -116,6 +118,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
advisory_script_uri, //
advisory_script_entrypoint, //
isolate_flags, //
deferred_load_handler, //
isolate_create_callback, //
isolate_shutdown_callback //
)
Expand Down Expand Up @@ -149,8 +152,8 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
}

if (settings.root_isolate_create_callback) {
// Isolate callbacks always occur in isolate scope and before user code has
// had a chance to run.
// Isolate callbacks always occur in isolate scope and before user code
// has had a chance to run.
tonic::DartState::Scope scope(isolate.get());
settings.root_isolate_create_callback(*isolate.get());
}
Expand Down Expand Up @@ -186,6 +189,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRootIsolate(
std::string advisory_script_uri,
std::string advisory_script_entrypoint,
Flags flags,
Dart_DeferredLoadHandler& deferred_load_handler,
const fml::closure& isolate_create_callback,
const fml::closure& isolate_shutdown_callback) {
TRACE_EVENT0("flutter", "DartIsolate::CreateRootIsolate");
Expand Down Expand Up @@ -222,7 +226,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRootIsolate(
auto isolate_flags = flags.Get();
Dart_Isolate vm_isolate = CreateDartIsolateGroup(
std::move(isolate_group_data), std::move(isolate_data), &isolate_flags,
error.error());
deferred_load_handler, error.error());

if (error) {
FML_LOG(ERROR) << "CreateDartIsolateGroup failed: " << error.str();
Expand Down Expand Up @@ -288,7 +292,8 @@ std::string DartIsolate::GetServiceId() {
return service_id;
}

bool DartIsolate::Initialize(Dart_Isolate dart_isolate) {
bool DartIsolate::Initialize(Dart_Isolate dart_isolate,
Dart_DeferredLoadHandler& deferred_load_handler) {
TRACE_EVENT0("flutter", "DartIsolate::Initialize");
if (phase_ != Phase::Uninitialized) {
return false;
Expand Down Expand Up @@ -320,6 +325,9 @@ bool DartIsolate::Initialize(Dart_Isolate dart_isolate) {
return false;
}

// Dart_SetDeferredLoadHandler(&DartDeferredLoadHandler);
Dart_SetDeferredLoadHandler(deferred_load_handler);

if (!UpdateThreadPoolNames()) {
return false;
}
Expand All @@ -332,6 +340,33 @@ fml::RefPtr<fml::TaskRunner> DartIsolate::GetMessageHandlingTaskRunner() const {
return message_handling_task_runner_;
}

bool DartIsolate::LoadLoadingUnit(intptr_t loading_unit_id,
const uint8_t* snapshot_data,
const uint8_t* snapshot_instructions) {
tonic::DartState::Scope scope(this);
Dart_Handle result = Dart_DeferredLoadComplete(loading_unit_id, snapshot_data,
snapshot_instructions);
if (Dart_IsApiError(result)) {
FML_LOG(ERROR) << "LOADING FAILED " << loading_unit_id;
result =
Dart_DeferredLoadCompleteError(loading_unit_id, Dart_GetError(result),
/*transient*/ true);
return false;
}
return true;
}

void DartIsolate::LoadLoadingUnitFailure(intptr_t loading_unit_id,
const std::string error_message,
bool transient) {
tonic::DartState::Scope scope(this);
Dart_Handle result = Dart_DeferredLoadCompleteError(
loading_unit_id, error_message.c_str(), transient);
if (Dart_IsApiError(result)) {
FML_LOG(ERROR) << "Dart error: " << Dart_GetError(result);
}
}

void DartIsolate::SetMessageHandlingTaskRunner(
fml::RefPtr<fml::TaskRunner> runner) {
if (!IsRootIsolate() || !runner) {
Expand Down Expand Up @@ -649,8 +684,8 @@ bool DartIsolate::RunFromLibrary(std::optional<std::string> library_name,
bool DartIsolate::Shutdown() {
TRACE_EVENT0("flutter", "DartIsolate::Shutdown");
// This call may be re-entrant since Dart_ShutdownIsolate can invoke the
// cleanup callback which deletes the embedder side object of the dart isolate
// (a.k.a. this).
// cleanup callback which deletes the embedder side object of the dart
// isolate (a.k.a. this).
if (phase_ == Phase::Shutdown) {
return false;
}
Expand Down Expand Up @@ -716,8 +751,9 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
DART_VM_SERVICE_ISOLATE_NAME, // script uri
DART_VM_SERVICE_ISOLATE_NAME, // script entrypoint
DartIsolate::Flags{flags}, // flags
nullptr, // isolate create callback
nullptr // isolate shutdown callback
DartDeferredLoadHandler::empty_dart_deferred_load_handler,
nullptr, // isolate create callback
nullptr // isolate shutdown callback
);

std::shared_ptr<DartIsolate> service_isolate = weak_service_isolate.lock();
Expand Down Expand Up @@ -780,8 +816,9 @@ Dart_Isolate DartIsolate::DartIsolateGroupCreateCallback(
// The VM attempts to start the VM service for us on |Dart_Initialize|. In
// such a case, the callback data will be null and the script URI will be
// DART_VM_SERVICE_ISOLATE_NAME. In such cases, we just create the service
// isolate like normal but dont hold a reference to it at all. We also start
// this isolate since we will never again reference it from the engine.
// isolate like normal but dont hold a reference to it at all. We also
// start this isolate since we will never again reference it from the
// engine.
return DartCreateAndStartServiceIsolate(package_root, //
package_config, //
flags, //
Expand Down Expand Up @@ -826,7 +863,8 @@ Dart_Isolate DartIsolate::DartIsolateGroupCreateCallback(
false))); // is_root_isolate

Dart_Isolate vm_isolate = CreateDartIsolateGroup(
std::move(isolate_group_data), std::move(isolate_data), flags, error);
std::move(isolate_group_data), std::move(isolate_data), flags,
DartDeferredLoadHandler::empty_dart_deferred_load_handler, error);

if (*error) {
FML_LOG(ERROR) << "CreateDartIsolateGroup failed: " << error;
Expand Down Expand Up @@ -871,7 +909,9 @@ bool DartIsolate::DartIsolateInitializeCallback(void** child_callback_data,
false))); // is_root_isolate

// root isolate should have been created via CreateRootIsolate
if (!InitializeIsolate(*embedder_isolate, isolate, error)) {
if (!InitializeIsolate(
*embedder_isolate, isolate,
DartDeferredLoadHandler::empty_dart_deferred_load_handler, error)) {
return false;
}

Expand All @@ -887,6 +927,7 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup(
std::unique_ptr<std::shared_ptr<DartIsolateGroupData>> isolate_group_data,
std::unique_ptr<std::shared_ptr<DartIsolate>> isolate_data,
Dart_IsolateFlags* flags,
Dart_DeferredLoadHandler& deferred_load_handler,
char** error) {
TRACE_EVENT0("flutter", "DartIsolate::CreateDartIsolateGroup");

Expand All @@ -902,12 +943,14 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup(
return nullptr;
}

// Ownership of the isolate data objects has been transferred to the Dart VM.
// Ownership of the isolate data objects has been transferred to the Dart
// VM.
std::shared_ptr<DartIsolate> embedder_isolate(*isolate_data);
isolate_group_data.release();
isolate_data.release();

if (!InitializeIsolate(std::move(embedder_isolate), isolate, error)) {
if (!InitializeIsolate(std::move(embedder_isolate), isolate,
deferred_load_handler, error)) {
return nullptr;
}

Expand All @@ -917,9 +960,10 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup(
bool DartIsolate::InitializeIsolate(
std::shared_ptr<DartIsolate> embedder_isolate,
Dart_Isolate isolate,
Dart_DeferredLoadHandler& deferred_load_handler,
char** error) {
TRACE_EVENT0("flutter", "DartIsolate::InitializeIsolate");
if (!embedder_isolate->Initialize(isolate)) {
if (!embedder_isolate->Initialize(isolate, deferred_load_handler)) {
*error = fml::strdup("Embedder could not initialize the Dart isolate.");
FML_DLOG(ERROR) << *error;
return false;
Expand All @@ -932,9 +976,9 @@ bool DartIsolate::InitializeIsolate(
return false;
}

// Root isolates will be setup by the engine and the service isolate (which is
// also a root isolate) by the utility routines in the VM. However, secondary
// isolates will be run by the VM if they are marked as runnable.
// Root isolates will be setup by the engine and the service isolate (which
// is also a root isolate) by the utility routines in the VM. However,
// secondary isolates will be run by the VM if they are marked as runnable.
if (!embedder_isolate->IsRootIsolate()) {
auto child_isolate_preparer =
embedder_isolate->GetIsolateGroupData().GetChildIsolatePreparer();
Expand Down
16 changes: 15 additions & 1 deletion runtime/dart_isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ class DartIsolate : public UIDartState {
std::string advisory_script_uri,
std::string advisory_script_entrypoint,
Flags flags,
Dart_DeferredLoadHandler& deferred_load_handler,
const fml::closure& isolate_create_callback,
const fml::closure& isolate_shutdown_callback,
std::optional<std::string> dart_entrypoint,
Expand Down Expand Up @@ -384,6 +385,14 @@ class DartIsolate : public UIDartState {
///
fml::RefPtr<fml::TaskRunner> GetMessageHandlingTaskRunner() const;

bool LoadLoadingUnit(intptr_t loading_unit_id,
const uint8_t* snapshot_data,
const uint8_t* snapshot_instructions);

void LoadLoadingUnitFailure(intptr_t loading_unit_id,
const std::string error_message,
bool transient);

private:
friend class IsolateConfiguration;
class AutoFireClosure {
Expand Down Expand Up @@ -418,6 +427,7 @@ class DartIsolate : public UIDartState {
std::string advisory_script_uri,
std::string advisory_script_entrypoint,
Flags flags,
Dart_DeferredLoadHandler& deferred_load_handler,
const fml::closure& isolate_create_callback,
const fml::closure& isolate_shutdown_callback);

Expand All @@ -432,7 +442,9 @@ class DartIsolate : public UIDartState {
std::string advisory_script_entrypoint,
bool is_root_isolate);

[[nodiscard]] bool Initialize(Dart_Isolate isolate);
[[nodiscard]] bool Initialize(
Dart_Isolate isolate,
Dart_DeferredLoadHandler& deferred_load_handler);

void SetMessageHandlingTaskRunner(fml::RefPtr<fml::TaskRunner> runner);

Expand Down Expand Up @@ -472,10 +484,12 @@ class DartIsolate : public UIDartState {
std::unique_ptr<std::shared_ptr<DartIsolateGroupData>> isolate_group_data,
std::unique_ptr<std::shared_ptr<DartIsolate>> isolate_data,
Dart_IsolateFlags* flags,
Dart_DeferredLoadHandler& deferred_load_handler,
char** error);

static bool InitializeIsolate(std::shared_ptr<DartIsolate> embedder_isolate,
Dart_Isolate isolate,
Dart_DeferredLoadHandler& deferred_load_handler,
char** error);

// |Dart_IsolateShutdownCallback|
Expand Down
Loading