Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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 74 of file service_isolate.cc.

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

102 {
103 // dart:fuchsia --------------------------------------------------------------
104 // dart runner doesn't care about scenic view ref.
105 if (!service_isolate) {
106 fuchsia::dart::Initialize(std::move(directory_request), std::nullopt);
107 }
108
109 // dart:fuchsia.builtin ------------------------------------------------------
110
111 Dart_Handle builtin_lib = Dart_LookupLibrary(ToDart("dart:fuchsia.builtin"));
116
117 // dart:io -------------------------------------------------------------------
118
119 Dart_Handle io_lib = Dart_LookupLibrary(ToDart("dart:io"));
121 result = Dart_SetNativeResolver(io_lib, dart::bin::IONativeLookup,
122 dart::bin::IONativeSymbol);
124
125 // dart:zircon ---------------------------------------------------------------
126
127 Dart_Handle zircon_lib = Dart_LookupLibrary(ToDart("dart:zircon"));
129 // NativeResolver already set by fuchsia::dart::Initialize().
130
131 // Core libraries ------------------------------------------------------------
132
133 Dart_Handle async_lib = Dart_LookupLibrary(ToDart("dart:async"));
135
136 Dart_Handle core_lib = Dart_LookupLibrary(ToDart("dart:core"));
138
139 Dart_Handle internal_lib = Dart_LookupLibrary(ToDart("dart:_internal"));
141
142 Dart_Handle isolate_lib = Dart_LookupLibrary(ToDart("dart:isolate"));
144
145#if !defined(AOT_RUNTIME)
146 // AOT: These steps already happened at compile time in gen_snapshot.
147
148 // We need to ensure that all the scripts loaded so far are finalized
149 // as we are about to invoke some Dart code below to set up closures.
150 result = Dart_FinalizeLoading(false);
152#endif
153
154 // Setup the internal library's 'internalPrint' function.
156 Dart_Invoke(builtin_lib, ToDart("_getPrintClosure"), 0, nullptr);
158
159 result = Dart_SetField(internal_lib, ToDart("_printClosure"), print);
161
162 // Set up the 'scheduleImmediate' closure.
164 if (service_isolate) {
165 // Running on dart::ThreadPool.
167 isolate_lib, ToDart("_getIsolateScheduleImmediateClosure"), 0, nullptr);
168 } else {
169 // Running on async::Loop.
171 builtin_lib, ToDart("_getScheduleMicrotaskClosure"), 0, nullptr);
172 }
174
177 result = Dart_Invoke(async_lib, ToDart("_setScheduleImmediateClosure"), 1,
180
181 // Set up the namespace in dart:io.
183 Dart_GetNonNullableType(io_lib, ToDart("_Namespace"), 0, nullptr);
185
187 namespace_args[0] = ToDart(reinterpret_cast<intptr_t>(namespc));
188 result =
189 Dart_Invoke(namespace_type, ToDart("_setupNamespace"), 1, namespace_args);
191
192 // Set up the namespace in dart:zircon.
194 Dart_GetNonNullableType(zircon_lib, ToDart("_Namespace"), 0, nullptr);
196
197 result = Dart_SetField(namespace_type, ToDart("_namespace"),
198 ToDart(reinterpret_cast<intptr_t>(namespc)));
200
201 // Set up stdout and stderr.
206 result = Dart_Invoke(io_lib, ToDart("_setStdioFDs"), 3, stdio_args);
208
209 // Disable some dart:io operations.
211 Dart_GetNonNullableType(io_lib, ToDart("_EmbedderConfig"), 0, nullptr);
213
214 result =
217
218 // Set the script location.
219 result = Dart_SetField(builtin_lib, ToDart("_rawScript"), ToDart(script_uri));
221
222 // Setup the uriBase with the base uri of the fidl app.
224 Dart_Invoke(io_lib, ToDart("_getUriBaseClosure"), 0, nullptr);
226
227 result = Dart_SetField(core_lib, ToDart("_uriBaseClosure"), uri_base);
229
230 Dart_Handle setup_hooks = ToDart("_setupHooks");
231 result = Dart_Invoke(builtin_lib, setup_hooks, 0, nullptr);
233 result = Dart_Invoke(io_lib, setup_hooks, 0, nullptr);
235 result = Dart_Invoke(isolate_lib, setup_hooks, 0, nullptr);
237}
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:103
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