55#include " flutter/shell/platform/embedder/tests/embedder_test_context.h"
66
77#include " flutter/fml/make_copyable.h"
8- #include " flutter/fml/paths.h"
98#include " flutter/runtime/dart_vm.h"
109#include " flutter/shell/platform/embedder/tests/embedder_assertions.h"
1110#include " third_party/dart/runtime/bin/elf_loader.h"
1413namespace flutter {
1514namespace testing {
1615
17- static constexpr const char * kAppAOTELFFileName = " app_elf_snapshot.so" ;
18-
1916EmbedderTestContext::EmbedderTestContext (std::string assets_path)
2017 : assets_path_(std::move(assets_path)),
2118 native_resolver_ (std::make_shared<TestDartNativeResolver>()) {
22- SetupAOTMappingsIfNecessary ();
19+ auto assets_dir = fml::OpenDirectory (assets_path_.c_str (), false ,
20+ fml::FilePermission::kRead );
21+
22+ if (flutter::DartVM::IsRunningPrecompiledCode ()) {
23+ std::string filename (assets_path_);
24+ filename += " /app_elf_snapshot.so" ;
25+
26+ const uint8_t *vm_snapshot_data = nullptr ,
27+ *vm_snapshot_instructions = nullptr ,
28+ *isolate_snapshot_data = nullptr ,
29+ *isolate_snapshot_instructions = nullptr ;
30+ const char * error = nullptr ;
31+
32+ elf_library_handle_ =
33+ Dart_LoadELF (filename.c_str (), /* file_offset=*/ 0 , &error,
34+ &vm_snapshot_data, &vm_snapshot_instructions,
35+ &isolate_snapshot_data, &isolate_snapshot_instructions);
36+
37+ if (elf_library_handle_ != nullptr ) {
38+ vm_snapshot_data_.reset (new fml::NonOwnedMapping (vm_snapshot_data, 0 ));
39+ vm_snapshot_instructions_.reset (
40+ new fml::NonOwnedMapping (vm_snapshot_instructions, 0 ));
41+ isolate_snapshot_data_.reset (
42+ new fml::NonOwnedMapping (isolate_snapshot_data, 0 ));
43+ isolate_snapshot_instructions_.reset (
44+ new fml::NonOwnedMapping (isolate_snapshot_instructions, 0 ));
45+ } else {
46+ FML_LOG (WARNING ) << " Could not load snapshot: " << error;
47+ }
48+ }
49+
2350 isolate_create_callbacks_.push_back (
2451 [weak_resolver =
2552 std::weak_ptr<TestDartNativeResolver>{native_resolver_}]() {
@@ -30,64 +57,11 @@ EmbedderTestContext::EmbedderTestContext(std::string assets_path)
3057}
3158
3259EmbedderTestContext::~EmbedderTestContext () {
33- vm_snapshot_data_.reset ();
34- vm_snapshot_instructions_.reset ();
35- isolate_snapshot_data_.reset ();
36- isolate_snapshot_instructions_.reset ();
3760 if (elf_library_handle_ != nullptr ) {
3861 Dart_UnloadELF (elf_library_handle_);
3962 }
4063}
4164
42- void EmbedderTestContext::SetupAOTMappingsIfNecessary () {
43- if (!DartVM::IsRunningPrecompiledCode ()) {
44- // Not in AOT mode. Nothing to do.
45- return ;
46- }
47-
48- auto elf_path = fml::paths::JoinPaths ({assets_path_, kAppAOTELFFileName });
49-
50- if (!fml::IsFile (elf_path.c_str ())) {
51- FML_LOG (ERROR ) << " Could not find the applications AOT ELF file in an "
52- " AOT environment. Engine launches attempted with this "
53- " context will fail. File not found: "
54- << elf_path;
55- return ;
56- }
57-
58- const uint8_t * vm_snapshot_data = nullptr ;
59- const uint8_t * vm_snapshot_instructions = nullptr ;
60- const uint8_t * isolate_snapshot_data = nullptr ;
61- const uint8_t * isolate_snapshot_instructions = nullptr ;
62-
63- const char * error = nullptr ;
64-
65- elf_library_handle_ =
66- Dart_LoadELF (elf_path.c_str (), // filename
67- 0u , // file offset
68- &error, // error (must not be freed)
69- &vm_snapshot_data, // VM snapshot data
70- &vm_snapshot_instructions, // VM snapshot instructions
71- &isolate_snapshot_data, // VM isolate data
72- &isolate_snapshot_instructions // VM isolate instructions
73- );
74-
75- if (elf_library_handle_ == nullptr ) {
76- FML_LOG (ERROR ) << " Could not load snapshot in an AOT environment. Engine "
77- " launches attempted with this context will fail. Error: "
78- << error;
79- return ;
80- }
81-
82- vm_snapshot_data_.reset (new fml::NonOwnedMapping (vm_snapshot_data, 0 ));
83- vm_snapshot_instructions_.reset (
84- new fml::NonOwnedMapping (vm_snapshot_instructions, 0 ));
85- isolate_snapshot_data_.reset (
86- new fml::NonOwnedMapping (isolate_snapshot_data, 0 ));
87- isolate_snapshot_instructions_.reset (
88- new fml::NonOwnedMapping (isolate_snapshot_instructions, 0 ));
89- }
90-
9165const std::string& EmbedderTestContext::GetAssetsPath () const {
9266 return assets_path_;
9367}
0 commit comments