Flutter Engine
 
Loading...
Searching...
No Matches
dart_runner Namespace Reference

Namespaces

namespace  testing
 

Classes

class  DartComponentController
 Starts a Dart component written in CFv2. More...
 
class  DartRunner
 
class  DartTestComponentController
 

Functions

void InitBuiltinLibrariesForIsolate (const std::string &script_uri, fdio_ns_t *namespc, int stdoutfd, int stderrfd, zx::channel directory_request, bool service_isolate)
 
bool IsTestProgram (const fuchsia::data::Dictionary &program_metadata)
 
Dart_Isolate CreateServiceIsolate (const char *uri, Dart_IsolateFlags *flags_unused, char **error)
 

Variables

constexpr char kArgsKey [] = "args"
 
uint8_t const *const vm_isolate_snapshot_buffer
 
uint8_t const *const isolate_snapshot_buffer
 

Function Documentation

◆ CreateServiceIsolate()

Dart_Isolate dart_runner::CreateServiceIsolate ( const char *  uri,
Dart_IsolateFlags *  flags_unused,
char **  error 
)

Definition at line 75 of file service_isolate.cc.

78 {
79 Dart_SetEmbedderInformationCallback(EmbedderInformationCallback);
80
81 const uint8_t *vmservice_data = nullptr, *vmservice_instructions = nullptr;
82
83#if defined(AOT_RUNTIME)
84 // The VM service was compiled as a separate app.
85 const char* snapshot_path = "/pkg/data/vmservice_snapshot.so";
86 if (elf_snapshot.Load(nullptr, snapshot_path)) {
87 vmservice_data = elf_snapshot.IsolateData();
88 vmservice_instructions = elf_snapshot.IsolateInstrs();
89 if (vmservice_data == nullptr || vmservice_instructions == nullptr) {
90 return nullptr;
91 }
92 } else {
93 // The VM service was compiled as a separate app.
94 const char* snapshot_data_path =
95 "/pkg/data/vmservice_isolate_snapshot_data.bin";
96 const char* snapshot_instructions_path =
97 "/pkg/data/vmservice_isolate_snapshot_instructions.bin";
98#else
99 // The VM service is embedded in the core snapshot.
100 const char* snapshot_data_path = "/pkg/data/isolate_core_snapshot_data.bin";
101 const char* snapshot_instructions_path =
102 "/pkg/data/isolate_core_snapshot_instructions.bin";
103#endif
104
106 nullptr, snapshot_data_path, mapped_isolate_snapshot_data)) {
107 *error = strdup("Failed to load snapshot for service isolate");
108 FML_LOG(ERROR) << *error;
109 return nullptr;
110 }
112 nullptr, snapshot_instructions_path,
113 mapped_isolate_snapshot_instructions, true /* executable */)) {
114 *error = strdup("Failed to load snapshot for service isolate");
115 FML_LOG(ERROR) << *error;
116 return nullptr;
117 }
118
119 vmservice_data = mapped_isolate_snapshot_data.address();
120 vmservice_instructions = mapped_isolate_snapshot_instructions.address();
121#if defined(AOT_RUNTIME)
122 }
123#endif
124
125 Dart_IsolateFlags flags;
126 Dart_IsolateFlagsInitialize(&flags);
127 flags.null_safety = true;
128
129 auto state = new std::shared_ptr<tonic::DartState>(new tonic::DartState());
130 Dart_Isolate isolate = Dart_CreateIsolateGroup(
131 uri, DART_VM_SERVICE_ISOLATE_NAME, vmservice_data, vmservice_instructions,
132 &flags, state, state, error);
133 if (!isolate) {
134 FML_LOG(ERROR) << "Dart_CreateIsolateGroup failed: " << *error;
135 return nullptr;
136 }
137
138 state->get()->SetIsolate(isolate);
139
140 // Setup native entries.
141 service_natives = new tonic::DartLibraryNatives();
142 service_natives->Register({
143 {"VMServiceIO_NotifyServerState", NotifyServerState, 1, true},
144 {"VMServiceIO_Shutdown", Shutdown, 0, true},
145 });
146
147 Dart_EnterScope();
148
149 Dart_Handle library =
150 Dart_LookupLibrary(Dart_NewStringFromCString("dart:vmservice_io"));
151 SHUTDOWN_ON_ERROR(library);
152 Dart_Handle result = Dart_SetRootLibrary(library);
153 SHUTDOWN_ON_ERROR(result);
154 result = Dart_SetNativeResolver(library, GetNativeFunction, GetSymbol);
155 SHUTDOWN_ON_ERROR(result);
156
157 // _ip = '127.0.0.1'
158 result = Dart_SetField(library, Dart_NewStringFromCString("_ip"),
159 Dart_NewStringFromCString("127.0.0.1"));
160 SHUTDOWN_ON_ERROR(result);
161
162 // _port = 0
163 result = Dart_SetField(library, Dart_NewStringFromCString("_port"),
164 Dart_NewInteger(0));
165 SHUTDOWN_ON_ERROR(result);
166
167 // _autoStart = true
168 result = Dart_SetField(library, Dart_NewStringFromCString("_autoStart"),
169 Dart_NewBoolean(true));
170 SHUTDOWN_ON_ERROR(result);
171
172 // _originCheckDisabled = false
173 result =
174 Dart_SetField(library, Dart_NewStringFromCString("_originCheckDisabled"),
175 Dart_NewBoolean(false));
176 SHUTDOWN_ON_ERROR(result);
177
178 // _authCodesDisabled = false
179 result =
180 Dart_SetField(library, Dart_NewStringFromCString("_authCodesDisabled"),
181 Dart_NewBoolean(false));
182 SHUTDOWN_ON_ERROR(result);
183
184 InitBuiltinLibrariesForIsolate(std::string(uri), nullptr, fileno(stdout),
185 fileno(stderr), zx::channel(), true);
186
187 // Make runnable.
188 Dart_ExitScope();
189 Dart_ExitIsolate();
190 *error = Dart_IsolateMakeRunnable(isolate);
191 if (*error != nullptr) {
192 FML_LOG(ERROR) << *error;
193 Dart_EnterIsolate(isolate);
194 Dart_ShutdownIsolate();
195 return nullptr;
196 }
197 return isolate;
198} // namespace dart_runner
static bool LoadFromNamespace(fdio_ns_t *namespc, const std::string &path, MappedResource &resource, bool executable=false)
const uint8_t uint32_t uint32_t GError ** error
#define FML_LOG(severity)
Definition logging.h:101
void InitBuiltinLibrariesForIsolate(const std::string &script_uri, fdio_ns_t *namespc, int stdoutfd, int stderrfd, zx::channel directory_request, bool service_isolate)
char * strdup(const char *str1)
#define SHUTDOWN_ON_ERROR(handle)

References dart_utils::MappedResource::address(), error, FML_LOG, InitBuiltinLibrariesForIsolate(), dart_utils::ElfSnapshot::IsolateData(), dart_utils::ElfSnapshot::IsolateInstrs(), dart_utils::ElfSnapshot::Load(), dart_utils::MappedResource::LoadFromNamespace(), tonic::DartLibraryNatives::Register(), and SHUTDOWN_ON_ERROR.

◆ InitBuiltinLibrariesForIsolate()

void dart_runner::InitBuiltinLibrariesForIsolate ( const std::string &  script_uri,
fdio_ns_t *  namespc,
int  stdoutfd,
int  stderrfd,
zx::channel  directory_request,
bool  service_isolate 
)

Definition at line 99 of file builtin_libraries.cc.

104 {
105 // dart:fuchsia --------------------------------------------------------------
106 // dart runner doesn't care about scenic view ref.
107 if (!service_isolate) {
108 fuchsia::dart::Initialize(std::move(directory_request), std::nullopt);
109 }
110
111 // dart:fuchsia.builtin ------------------------------------------------------
112
113 Dart_Handle builtin_lib = Dart_LookupLibrary(ToDart("dart:fuchsia.builtin"));
118
119 // dart:io -------------------------------------------------------------------
120
121 Dart_Handle io_lib = Dart_LookupLibrary(ToDart("dart:io"));
123 result = Dart_SetNativeResolver(io_lib, dart::bin::IONativeLookup,
124 dart::bin::IONativeSymbol);
126
127 // dart:zircon ---------------------------------------------------------------
128
129 Dart_Handle zircon_lib = Dart_LookupLibrary(ToDart("dart:zircon"));
131 // NativeResolver already set by fuchsia::dart::Initialize().
132
133 // Core libraries ------------------------------------------------------------
134
135 Dart_Handle async_lib = Dart_LookupLibrary(ToDart("dart:async"));
137
138 Dart_Handle core_lib = Dart_LookupLibrary(ToDart("dart:core"));
140
141 Dart_Handle internal_lib = Dart_LookupLibrary(ToDart("dart:_internal"));
143
144 Dart_Handle isolate_lib = Dart_LookupLibrary(ToDart("dart:isolate"));
146
147#if !defined(AOT_RUNTIME)
148 // AOT: These steps already happened at compile time in gen_snapshot.
149
150 // We need to ensure that all the scripts loaded so far are finalized
151 // as we are about to invoke some Dart code below to set up closures.
152 result = Dart_FinalizeLoading(false);
154#endif
155
156 // Setup the internal library's 'internalPrint' function.
158 Dart_Invoke(builtin_lib, ToDart("_getPrintClosure"), 0, nullptr);
160
161 result = Dart_SetField(internal_lib, ToDart("_printClosure"), print);
163
164 // Set up the 'scheduleImmediate' closure.
166 if (service_isolate) {
167 // Running on dart::ThreadPool.
169 isolate_lib, ToDart("_getIsolateScheduleImmediateClosure"), 0, nullptr);
170 } else {
171 // Running on async::Loop.
173 builtin_lib, ToDart("_getScheduleMicrotaskClosure"), 0, nullptr);
174 }
176
179 result = Dart_Invoke(async_lib, ToDart("_setScheduleImmediateClosure"), 1,
182
183 // Set up the namespace in dart:io.
185 Dart_GetNonNullableType(io_lib, ToDart("_Namespace"), 0, nullptr);
187
189 namespace_args[0] = ToDart(reinterpret_cast<intptr_t>(namespc));
190 result =
191 Dart_Invoke(namespace_type, ToDart("_setupNamespace"), 1, namespace_args);
193
194 // Set up the namespace in dart:zircon.
196 Dart_GetNonNullableType(zircon_lib, ToDart("_Namespace"), 0, nullptr);
198
199 result = Dart_SetField(namespace_type, ToDart("_namespace"),
200 ToDart(reinterpret_cast<intptr_t>(namespc)));
202
203 // Set up stdout and stderr.
208 result = Dart_Invoke(io_lib, ToDart("_setStdioFDs"), 3, stdio_args);
210
211 // Disable some dart:io operations.
213 Dart_GetNonNullableType(io_lib, ToDart("_EmbedderConfig"), 0, nullptr);
215
216 result =
219
220 // Set the script location.
221 result = Dart_SetField(builtin_lib, ToDart("_rawScript"), ToDart(script_uri));
223
224 // Setup the uriBase with the base uri of the fidl app.
226 Dart_Invoke(io_lib, ToDart("_getUriBaseClosure"), 0, nullptr);
228
229 result = Dart_SetField(core_lib, ToDart("_uriBaseClosure"), uri_base);
231
232 Dart_Handle setup_hooks = ToDart("_setupHooks");
233 result = Dart_Invoke(builtin_lib, setup_hooks, 0, nullptr);
235 result = Dart_Invoke(io_lib, setup_hooks, 0, nullptr);
237 result = Dart_Invoke(isolate_lib, setup_hooks, 0, nullptr);
239}
Dart_NativeFunction function
#define FML_CHECK(condition)
Definition logging.h:104
void Initialize(zx::channel directory_request, std::optional< zx::eventpair > view_ref)
Initializes Dart bindings for the Fuchsia application model.
Definition fuchsia.cc:102
Dart_Handle ToDart(const T &object)
bool CheckAndHandleError(Dart_Handle handle)
Definition dart_error.cc:33

References tonic::CheckAndHandleError(), FML_CHECK, and fuchsia::dart::Initialize().

Referenced by CreateServiceIsolate().

◆ IsTestProgram()

bool dart_runner::IsTestProgram ( const fuchsia::data::Dictionary &  program_metadata)

Parses the |args| field from the "program" field to determine if a test component is being executed.

Definition at line 167 of file dart_runner.cc.

167 {
168 for (const auto& entry : program_metadata.entries()) {
169 if (entry.key.compare(kArgsKey) != 0 || entry.value == nullptr) {
170 continue;
171 }
172 auto args = entry.value->str_vec();
173
174 // fml::CommandLine expects the first argument to be the name of the
175 // program, so we prepend a dummy argument so we can use fml::CommandLine to
176 // parse the arguments for us.
177 std::vector<std::string> command_line_args = {""};
178 command_line_args.insert(command_line_args.end(), args.begin(), args.end());
180 command_line_args.begin(), command_line_args.end());
181
182 std::string is_test_str;
183 return parsed_args.GetOptionValue("is_test", &is_test_str) &&
184 is_test_str == "true";
185 }
186 return false;
187}
bool GetOptionValue(std::string_view name, std::string *value) const
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static constexpr char kArgsKey[]
CommandLine CommandLineFromIterators(InputIterator first, InputIterator last)

References args, fml::CommandLineFromIterators(), fml::CommandLine::GetOptionValue(), and kArgsKey.

Variable Documentation

◆ isolate_snapshot_buffer

uint8_t const* const dart_runner::isolate_snapshot_buffer
extern

◆ kArgsKey

constexpr char dart_runner::kArgsKey[] = "args"
constexpr

Definition at line 163 of file dart_runner.cc.

Referenced by IsTestProgram().

◆ vm_isolate_snapshot_buffer

uint8_t const* const dart_runner::vm_isolate_snapshot_buffer
extern