Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2d5c28e

Browse files
authored
Setup NativeAssetsApi during isolate group creation (#53329)
This initializes the `NativeAssetsApi` for native assets resolution in the isolate group creation callback. * dart-lang/sdk#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
1 parent 97cd8f1 commit 2d5c28e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

runtime/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ source_set("runtime") {
107107
":test_font",
108108
"$dart_src/runtime:dart_api",
109109
"$dart_src/runtime/bin:dart_io_api",
110+
"$dart_src/runtime/bin:native_assets_api",
110111
"//flutter/assets",
111112
"//flutter/common",
112113
"//flutter/flow",

runtime/dart_isolate.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "fml/message_loop_task_queues.h"
2626
#include "fml/task_source.h"
2727
#include "fml/time/time_point.h"
28+
#include "third_party/dart/runtime/include/bin/native_assets_api.h"
2829
#include "third_party/dart/runtime/include/dart_api.h"
2930
#include "third_party/dart/runtime/include/dart_tools_api.h"
3031
#include "third_party/tonic/converter/dart_converter.h"
@@ -1165,6 +1166,27 @@ bool DartIsolate::DartIsolateInitializeCallback(void** child_callback_data,
11651166
return true;
11661167
}
11671168

1169+
static void* NativeAssetsDlopenRelative(const char* path, char** error) {
1170+
auto* isolate_group_data =
1171+
static_cast<std::shared_ptr<DartIsolateGroupData>*>(
1172+
Dart_CurrentIsolateGroupData());
1173+
const std::string& script_uri = (*isolate_group_data)->GetAdvisoryScriptURI();
1174+
return dart::bin::NativeAssets::DlopenRelative(path, script_uri.data(),
1175+
error);
1176+
}
1177+
1178+
static void InitDartFFIForIsolateGroup() {
1179+
NativeAssetsApi native_assets;
1180+
memset(&native_assets, 0, sizeof(native_assets));
1181+
native_assets.dlopen_absolute = &dart::bin::NativeAssets::DlopenAbsolute;
1182+
native_assets.dlopen_relative = &NativeAssetsDlopenRelative;
1183+
native_assets.dlopen_system = &dart::bin::NativeAssets::DlopenSystem;
1184+
native_assets.dlopen_executable = &dart::bin::NativeAssets::DlopenExecutable;
1185+
native_assets.dlopen_process = &dart::bin::NativeAssets::DlopenProcess;
1186+
native_assets.dlsym = &dart::bin::NativeAssets::Dlsym;
1187+
Dart_InitializeNativeAssetsResolver(&native_assets);
1188+
};
1189+
11681190
Dart_Isolate DartIsolate::CreateDartIsolateGroup(
11691191
std::unique_ptr<std::shared_ptr<DartIsolateGroupData>> isolate_group_data,
11701192
std::unique_ptr<std::shared_ptr<DartIsolate>> isolate_data,
@@ -1191,6 +1213,8 @@ Dart_Isolate DartIsolate::CreateDartIsolateGroup(
11911213
isolate_data.release();
11921214
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
11931215

1216+
InitDartFFIForIsolateGroup();
1217+
11941218
success = InitializeIsolate(embedder_isolate, isolate, error);
11951219
}
11961220
if (!success) {

0 commit comments

Comments
 (0)