Set Platform.executable on startup (flutter/engine#33127)

Previously, using Platform.executable (from dart:io) returned null (if
non-null-by-default was disabled) or threw an exception (if NNBD was
enabled) since we weren't setting it.

We now pass the executable name to Dart during VM startup based on the
first value in the FlutterProjectArgs::command_line_argv array passed to
FlutterEngineRun (or FlutterEngineInitialize) on startup. argv[0] (if
specified) is explicitly documented as being required to be the
executable name in embedder.h. In the case where no argv[0] is
specified, we instead set Platform.executable to "Flutter" in order to
avoid violating the (non-nullable) type annotation on
Platform.executable.

Note that dart::bin::SetExecutableName() does NOT make a copy of the
input string, so that value needs to be available for the entire lifetime
of the VM.

This also adds EmbedderConfigBuilder::SetExecutableName() to support
setting a fake executable name in unittests. By default, we continue to
set the name "embedder_unittest" unless overridden using this method.

See: https://api.flutter.dev/flutter/dart-io/Platform/executable.html
See: https://github.com/dart-lang/sdk/issues/48427

Issue: https://github.com/flutter/flutter/issues/83921
This commit is contained in:
Chris Bracken
2022-05-05 11:14:16 -07:00
committed by GitHub
parent 1df74f0938
commit 1af24ccf62
7 changed files with 52 additions and 2 deletions

View File

@@ -157,6 +157,10 @@ struct Settings {
// Dart code is executed.
std::string advisory_script_entrypoint = "main";
// The executable path associated with this process. This is returned by
// Platform.executable from dart:io. If unknown, defaults to "Flutter".
std::string executable_name = "Flutter";
// Observatory settings
// Whether the Dart VM service should be enabled.

View File

@@ -436,6 +436,8 @@ DartVM::DartVM(std::shared_ptr<const DartVMData> vm_data,
DartUI::InitForGlobal();
dart::bin::SetExecutableName(settings_.executable_name.c_str());
{
TRACE_EVENT0("flutter", "Dart_Initialize");
Dart_InitializeParams params = {};

View File

@@ -233,6 +233,11 @@ std::unique_ptr<fml::Mapping> GetSymbolMapping(std::string symbol_prefix,
Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
Settings settings = {};
// Set executable name.
if (command_line.has_argv0()) {
settings.executable_name = command_line.argv0();
}
// Enable Observatory
settings.enable_observatory =
!command_line.HasOption(FlagForSwitch(Switch::DisableObservatory));

View File

@@ -41,6 +41,14 @@ void terminateExitCodeHandler() {
}
@pragma('vm:entry-point')
void executableNameNotNull() {
notifyStringValue(Platform.executable);
}
void notifyStringValue(String value) native 'NotifyStringValue';
@pragma('vm:entry-point')
void invokePlatformTaskRunner() {
PlatformDispatcher.instance.sendPlatformMessage('OhHi', null, null);

View File

@@ -102,8 +102,8 @@ EmbedderConfigBuilder::EmbedderConfigBuilder(
SkImage::MakeFromBitmap(bitmap));
};
// The first argument is treated as the executable name. Don't make tests have
// to do this manually.
// The first argument is always the executable name. Don't make tests have to
// do this manually.
AddCommandLineArgument("embedder_unittest");
if (preference != InitializationPreference::kNoInitialize) {
@@ -264,6 +264,13 @@ void EmbedderConfigBuilder::SetLocalizationCallbackHooks() {
EmbedderTestContext::GetComputePlatformResolvedLocaleCallbackHook();
}
void EmbedderConfigBuilder::SetExecutableName(std::string executable_name) {
if (executable_name.size() == 0) {
return;
}
command_line_arguments_[0] = std::move(executable_name);
}
void EmbedderConfigBuilder::SetDartEntrypoint(std::string entrypoint) {
if (entrypoint.size() == 0) {
return;

View File

@@ -85,6 +85,8 @@ class EmbedderConfigBuilder {
void SetLocalizationCallbackHooks();
void SetExecutableName(std::string executable_name);
void SetDartEntrypoint(std::string entrypoint);
void AddCommandLineArgument(std::string arg);

View File

@@ -151,6 +151,28 @@ TEST_F(EmbedderTest, CanTerminateCleanly) {
ASSERT_TRUE(engine.is_valid());
}
TEST_F(EmbedderTest, ExecutableNameNotNull) {
auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
// Supply a callback to Dart for the test fixture to pass Platform.executable
// back to us.
fml::AutoResetWaitableEvent latch;
context.AddNativeCallback(
"NotifyStringValue", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
const auto dart_string = tonic::DartConverter<std::string>::FromDart(
Dart_GetNativeArgument(args, 0));
EXPECT_EQ("/path/to/binary", dart_string);
latch.Signal();
}));
EmbedderConfigBuilder builder(context);
builder.SetSoftwareRendererConfig();
builder.SetDartEntrypoint("executableNameNotNull");
builder.SetExecutableName("/path/to/binary");
auto engine = builder.LaunchEngine();
latch.Wait();
}
std::atomic_size_t EmbedderTestTaskRunner::sEmbedderTaskRunnerIdentifiers = {};
TEST_F(EmbedderTest, CanSpecifyCustomPlatformTaskRunner) {