From f7a415768395fff2dbfab28770cac8dfe8487b0b Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Tue, 18 Jun 2024 08:21:20 +0200 Subject: [PATCH] Setup `NativeAssetsApi` during isolate group creation (flutter/engine#53329) This initializes the `NativeAssetsApi` for native assets resolution in the isolate group creation callback. * https://github.com/dart-lang/sdk/issues/55523 ## Implementation considerations The DartIO initialization lives in its own GN target. This doesn't work for the native assets initialization due to it having to look up the `script_uri` in one of the callbacks. Since the callbacks are function pointers, we can't have a lambda that captures the script uri. So instead, the native assets initialization lives in the flutter/runtime target. The import from dart should probably be `runtime/include/bin/native_assets_api.h` to mirror what we're doing with dart IO, rather than directly importing from `runtime/bin/native_assets.h`. ## Testing All native asset testing is in flutter_tools, so those tests will only run once this rolls into flutter/flutter. I have done manual testing locally with a Dart branch that removes the fallback: https://dart-review.googlesource.com/c/sdk/+/370740 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- engine/src/flutter/runtime/BUILD.gn | 1 + engine/src/flutter/runtime/dart_isolate.cc | 24 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/engine/src/flutter/runtime/BUILD.gn b/engine/src/flutter/runtime/BUILD.gn index 1ed968a4d62..80e5c372a40 100644 --- a/engine/src/flutter/runtime/BUILD.gn +++ b/engine/src/flutter/runtime/BUILD.gn @@ -107,6 +107,7 @@ source_set("runtime") { ":test_font", "$dart_src/runtime:dart_api", "$dart_src/runtime/bin:dart_io_api", + "$dart_src/runtime/bin:native_assets_api", "//flutter/assets", "//flutter/common", "//flutter/flow", diff --git a/engine/src/flutter/runtime/dart_isolate.cc b/engine/src/flutter/runtime/dart_isolate.cc index 9d063bedbe3..3dc8403506f 100644 --- a/engine/src/flutter/runtime/dart_isolate.cc +++ b/engine/src/flutter/runtime/dart_isolate.cc @@ -25,6 +25,7 @@ #include "fml/message_loop_task_queues.h" #include "fml/task_source.h" #include "fml/time/time_point.h" +#include "third_party/dart/runtime/include/bin/native_assets_api.h" #include "third_party/dart/runtime/include/dart_api.h" #include "third_party/dart/runtime/include/dart_tools_api.h" #include "third_party/tonic/converter/dart_converter.h" @@ -1165,6 +1166,27 @@ bool DartIsolate::DartIsolateInitializeCallback(void** child_callback_data, return true; } +static void* NativeAssetsDlopenRelative(const char* path, char** error) { + auto* isolate_group_data = + static_cast*>( + Dart_CurrentIsolateGroupData()); + const std::string& script_uri = (*isolate_group_data)->GetAdvisoryScriptURI(); + return dart::bin::NativeAssets::DlopenRelative(path, script_uri.data(), + error); +} + +static void InitDartFFIForIsolateGroup() { + NativeAssetsApi native_assets; + memset(&native_assets, 0, sizeof(native_assets)); + native_assets.dlopen_absolute = &dart::bin::NativeAssets::DlopenAbsolute; + native_assets.dlopen_relative = &NativeAssetsDlopenRelative; + native_assets.dlopen_system = &dart::bin::NativeAssets::DlopenSystem; + native_assets.dlopen_executable = &dart::bin::NativeAssets::DlopenExecutable; + native_assets.dlopen_process = &dart::bin::NativeAssets::DlopenProcess; + native_assets.dlsym = &dart::bin::NativeAssets::Dlsym; + Dart_InitializeNativeAssetsResolver(&native_assets); +}; + Dart_Isolate DartIsolate::CreateDartIsolateGroup( std::unique_ptr> isolate_group_data, std::unique_ptr> isolate_data, @@ -1191,6 +1213,8 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup( isolate_data.release(); // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks) + InitDartFFIForIsolateGroup(); + success = InitializeIsolate(embedder_isolate, isolate, error); } if (!success) {