33// found in the LICENSE file.
44
55#include " flutter/runtime/dart_isolate.h"
6+ #include " flutter/runtime/dart_deferred_load_handler.h"
67
78#include < cstdlib>
89#include < tuple>
@@ -86,6 +87,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
8687 std::string advisory_script_uri,
8788 std::string advisory_script_entrypoint,
8889 Flags isolate_flags,
90+ Dart_DeferredLoadHandler& deferred_load_handler,
8991 const fml::closure& isolate_create_callback,
9092 const fml::closure& isolate_shutdown_callback,
9193 std::optional<std::string> dart_entrypoint,
@@ -116,6 +118,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
116118 advisory_script_uri, //
117119 advisory_script_entrypoint, //
118120 isolate_flags, //
121+ deferred_load_handler, //
119122 isolate_create_callback, //
120123 isolate_shutdown_callback //
121124 )
@@ -149,8 +152,8 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
149152 }
150153
151154 if (settings.root_isolate_create_callback ) {
152- // Isolate callbacks always occur in isolate scope and before user code has
153- // had a chance to run.
155+ // Isolate callbacks always occur in isolate scope and before user code
156+ // has had a chance to run.
154157 tonic::DartState::Scope scope (isolate.get ());
155158 settings.root_isolate_create_callback (*isolate.get ());
156159 }
@@ -186,6 +189,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRootIsolate(
186189 std::string advisory_script_uri,
187190 std::string advisory_script_entrypoint,
188191 Flags flags,
192+ Dart_DeferredLoadHandler& deferred_load_handler,
189193 const fml::closure& isolate_create_callback,
190194 const fml::closure& isolate_shutdown_callback) {
191195 TRACE_EVENT0 (" flutter" , " DartIsolate::CreateRootIsolate" );
@@ -222,7 +226,7 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRootIsolate(
222226 auto isolate_flags = flags.Get ();
223227 Dart_Isolate vm_isolate = CreateDartIsolateGroup (
224228 std::move (isolate_group_data), std::move (isolate_data), &isolate_flags,
225- error.error ());
229+ deferred_load_handler, error.error ());
226230
227231 if (error) {
228232 FML_LOG (ERROR) << " CreateDartIsolateGroup failed: " << error.str ();
@@ -288,7 +292,15 @@ std::string DartIsolate::GetServiceId() {
288292 return service_id;
289293}
290294
291- bool DartIsolate::Initialize (Dart_Isolate dart_isolate) {
295+ // Dart_Handle DartDeferredLoadHandler(intptr_t loading_unit_id) {
296+ // FML_LOG(ERROR) << "LOAD REQUEST CALLED FOR ID: " << loading_unit_id;
297+ // // Temporary call for prototyping.
298+ // return
299+ // Engine::GetEngineDelegateTemp()->OnDartLoadLibrary(loading_unit_id);
300+ // }
301+
302+ bool DartIsolate::Initialize (Dart_Isolate dart_isolate,
303+ Dart_DeferredLoadHandler& deferred_load_handler) {
292304 TRACE_EVENT0 (" flutter" , " DartIsolate::Initialize" );
293305 if (phase_ != Phase::Uninitialized) {
294306 return false ;
@@ -320,6 +332,9 @@ bool DartIsolate::Initialize(Dart_Isolate dart_isolate) {
320332 return false ;
321333 }
322334
335+ // Dart_SetDeferredLoadHandler(&DartDeferredLoadHandler);
336+ Dart_SetDeferredLoadHandler (deferred_load_handler);
337+
323338 if (!UpdateThreadPoolNames ()) {
324339 return false ;
325340 }
@@ -332,6 +347,22 @@ fml::RefPtr<fml::TaskRunner> DartIsolate::GetMessageHandlingTaskRunner() const {
332347 return message_handling_task_runner_;
333348}
334349
350+ bool DartIsolate::LoadLoadingUnit (intptr_t loading_unit_id,
351+ const uint8_t * snapshot_data,
352+ const uint8_t * snapshot_instructions) {
353+ tonic::DartState::Scope scope (this );
354+ Dart_Handle result = Dart_DeferredLoadComplete (loading_unit_id, snapshot_data,
355+ snapshot_instructions);
356+ if (Dart_IsApiError (result)) {
357+ FML_LOG (ERROR) << " LOADING FAILED " << loading_unit_id;
358+ result =
359+ Dart_DeferredLoadCompleteError (loading_unit_id, Dart_GetError (result),
360+ /* transient*/ true );
361+ return false ;
362+ }
363+ return true ;
364+ }
365+
335366void DartIsolate::SetMessageHandlingTaskRunner (
336367 fml::RefPtr<fml::TaskRunner> runner) {
337368 if (!IsRootIsolate () || !runner) {
@@ -649,8 +680,8 @@ bool DartIsolate::RunFromLibrary(std::optional<std::string> library_name,
649680bool DartIsolate::Shutdown () {
650681 TRACE_EVENT0 (" flutter" , " DartIsolate::Shutdown" );
651682 // This call may be re-entrant since Dart_ShutdownIsolate can invoke the
652- // cleanup callback which deletes the embedder side object of the dart isolate
653- // (a.k.a. this).
683+ // cleanup callback which deletes the embedder side object of the dart
684+ // isolate (a.k.a. this).
654685 if (phase_ == Phase::Shutdown) {
655686 return false ;
656687 }
@@ -678,7 +709,8 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
678709
679710 if (!vm_data) {
680711 *error = fml::strdup (
681- " Could not access VM data to initialize isolates. This may be because "
712+ " Could not access VM data to initialize isolates. This may be "
713+ " because "
682714 " the VM has initialized shutdown on another thread already." );
683715 return nullptr ;
684716 }
@@ -696,8 +728,8 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
696728
697729#if (FLUTTER_RUNTIME_MODE != FLUTTER_RUNTIME_MODE_DEBUG)
698730 // TODO(68663): The service isolate in debug mode is always launched without
699- // sound null safety. Fix after the isolate snapshot data is created with the
700- // right flags.
731+ // sound null safety. Fix after the isolate snapshot data is created with
732+ // the right flags.
701733 flags->null_safety =
702734 vm_data->GetIsolateSnapshot ()->IsNullSafetyEnabled (nullptr );
703735#endif
@@ -716,8 +748,9 @@ Dart_Isolate DartIsolate::DartCreateAndStartServiceIsolate(
716748 DART_VM_SERVICE_ISOLATE_NAME, // script uri
717749 DART_VM_SERVICE_ISOLATE_NAME, // script entrypoint
718750 DartIsolate::Flags{flags}, // flags
719- nullptr , // isolate create callback
720- nullptr // isolate shutdown callback
751+ DartDeferredLoadHandler::empty_dart_deferred_load_handler,
752+ nullptr , // isolate create callback
753+ nullptr // isolate shutdown callback
721754 );
722755
723756 std::shared_ptr<DartIsolate> service_isolate = weak_service_isolate.lock ();
@@ -780,8 +813,9 @@ Dart_Isolate DartIsolate::DartIsolateGroupCreateCallback(
780813 // The VM attempts to start the VM service for us on |Dart_Initialize|. In
781814 // such a case, the callback data will be null and the script URI will be
782815 // DART_VM_SERVICE_ISOLATE_NAME. In such cases, we just create the service
783- // isolate like normal but dont hold a reference to it at all. We also start
784- // this isolate since we will never again reference it from the engine.
816+ // isolate like normal but dont hold a reference to it at all. We also
817+ // start this isolate since we will never again reference it from the
818+ // engine.
785819 return DartCreateAndStartServiceIsolate (package_root, //
786820 package_config, //
787821 flags, //
@@ -826,7 +860,8 @@ Dart_Isolate DartIsolate::DartIsolateGroupCreateCallback(
826860 false ))); // is_root_isolate
827861
828862 Dart_Isolate vm_isolate = CreateDartIsolateGroup (
829- std::move (isolate_group_data), std::move (isolate_data), flags, error);
863+ std::move (isolate_group_data), std::move (isolate_data), flags,
864+ DartDeferredLoadHandler::empty_dart_deferred_load_handler, error);
830865
831866 if (*error) {
832867 FML_LOG (ERROR) << " CreateDartIsolateGroup failed: " << error;
@@ -871,7 +906,9 @@ bool DartIsolate::DartIsolateInitializeCallback(void** child_callback_data,
871906 false ))); // is_root_isolate
872907
873908 // root isolate should have been created via CreateRootIsolate
874- if (!InitializeIsolate (*embedder_isolate, isolate, error)) {
909+ if (!InitializeIsolate (
910+ *embedder_isolate, isolate,
911+ DartDeferredLoadHandler::empty_dart_deferred_load_handler, error)) {
875912 return false ;
876913 }
877914
@@ -887,6 +924,7 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup(
887924 std::unique_ptr<std::shared_ptr<DartIsolateGroupData>> isolate_group_data,
888925 std::unique_ptr<std::shared_ptr<DartIsolate>> isolate_data,
889926 Dart_IsolateFlags* flags,
927+ Dart_DeferredLoadHandler& deferred_load_handler,
890928 char ** error) {
891929 TRACE_EVENT0 (" flutter" , " DartIsolate::CreateDartIsolateGroup" );
892930
@@ -902,12 +940,14 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup(
902940 return nullptr ;
903941 }
904942
905- // Ownership of the isolate data objects has been transferred to the Dart VM.
943+ // Ownership of the isolate data objects has been transferred to the Dart
944+ // VM.
906945 std::shared_ptr<DartIsolate> embedder_isolate (*isolate_data);
907946 isolate_group_data.release ();
908947 isolate_data.release ();
909948
910- if (!InitializeIsolate (std::move (embedder_isolate), isolate, error)) {
949+ if (!InitializeIsolate (std::move (embedder_isolate), isolate,
950+ deferred_load_handler, error)) {
911951 return nullptr ;
912952 }
913953
@@ -917,9 +957,10 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup(
917957bool DartIsolate::InitializeIsolate (
918958 std::shared_ptr<DartIsolate> embedder_isolate,
919959 Dart_Isolate isolate,
960+ Dart_DeferredLoadHandler& deferred_load_handler,
920961 char ** error) {
921962 TRACE_EVENT0 (" flutter" , " DartIsolate::InitializeIsolate" );
922- if (!embedder_isolate->Initialize (isolate)) {
963+ if (!embedder_isolate->Initialize (isolate, deferred_load_handler )) {
923964 *error = fml::strdup (" Embedder could not initialize the Dart isolate." );
924965 FML_DLOG (ERROR) << *error;
925966 return false ;
@@ -932,9 +973,9 @@ bool DartIsolate::InitializeIsolate(
932973 return false ;
933974 }
934975
935- // Root isolates will be setup by the engine and the service isolate (which is
936- // also a root isolate) by the utility routines in the VM. However, secondary
937- // isolates will be run by the VM if they are marked as runnable.
976+ // Root isolates will be setup by the engine and the service isolate (which
977+ // is also a root isolate) by the utility routines in the VM. However,
978+ // secondary isolates will be run by the VM if they are marked as runnable.
938979 if (!embedder_isolate->IsRootIsolate ()) {
939980 auto child_isolate_preparer =
940981 embedder_isolate->GetIsolateGroupData ().GetChildIsolatePreparer ();
0 commit comments