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

Commit 3d896ad

Browse files
author
Emmanuel Garcia
committed
Add flag to settings.h and clean up
1 parent fb0502a commit 3d896ad

File tree

6 files changed

+44
-33
lines changed

6 files changed

+44
-33
lines changed

common/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ struct Settings {
111111
bool enable_dart_profiling = false;
112112
bool disable_dart_asserts = false;
113113

114+
// Whether the _registerPlugin function should be called when defined.
115+
bool enable_dart_plugin_registrant = false;
116+
114117
// Whether embedder only allows secure connections.
115118
bool may_insecurely_connect_to_all_domains = true;
116119
// JSON-formatted domain network policy.

runtime/dart_isolate.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ std::weak_ptr<DartIsolate> DartIsolate::CreateRunningRootIsolate(
182182
settings.root_isolate_create_callback(*isolate.get());
183183
}
184184

185-
if (!isolate->RunFromLibrary(dart_entrypoint_library, //
186-
dart_entrypoint, //
187-
settings.dart_entrypoint_args //
185+
if (!isolate->RunFromLibrary(dart_entrypoint_library, //
186+
dart_entrypoint, //
187+
settings.dart_entrypoint_args, //
188+
settings.enable_dart_plugin_registrant //
188189
)) {
189190
FML_LOG(ERROR) << "Could not run the run main Dart entrypoint.";
190191
return {};
@@ -703,7 +704,8 @@ bool DartIsolate::MarkIsolateRunnable() {
703704

704705
bool DartIsolate::RunFromLibrary(std::optional<std::string> library_name,
705706
std::optional<std::string> entrypoint,
706-
const std::vector<std::string>& args) {
707+
const std::vector<std::string>& args,
708+
bool enable_dart_plugin_registrant) {
707709
TRACE_EVENT0("flutter", "DartIsolate::RunFromLibrary");
708710
if (phase_ != Phase::Ready) {
709711
return false;
@@ -737,14 +739,13 @@ bool DartIsolate::RunFromLibrary(std::optional<std::string> library_name,
737739
// as usual.
738740
//
739741
// This allows embeddings to change the name of the entrypoint function.
740-
auto plugin_registrant_function =
741-
::Dart_GetField(library_handle, tonic::ToDart("_registerPlugins"));
742-
743-
if (Dart_IsError(plugin_registrant_function)) {
744-
plugin_registrant_function = Dart_Null();
745-
FML_DLOG(ERROR) << " plugin_registrant_function IS NULL ";
746-
} else {
747-
FML_DLOG(ERROR) << " plugin_registrant_function IS NOT NULL ";
742+
auto plugin_registrant_function = Dart_Null();
743+
if (enable_dart_plugin_registrant) {
744+
auto defined_plugin_registrant_function =
745+
::Dart_GetField(library_handle, tonic::ToDart("_registerPlugins"));
746+
if (!Dart_IsError(defined_plugin_registrant_function)) {
747+
plugin_registrant_function = defined_plugin_registrant_function;
748+
}
748749
}
749750

750751
if (!InvokeMainEntrypoint(user_entrypoint_function,

runtime/dart_isolate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,15 @@ class DartIsolate : public UIDartState {
376376
/// supplied entrypoint.
377377
/// @param[in] entrypoint The entrypoint in `library_name`
378378
/// @param[in] args A list of string arguments to the entrypoint.
379+
/// @param[in] enable_dart_plugin_registrant Calls the plugin registrant.
379380
///
380381
/// @return If the isolate successfully transitioned to the running phase
381382
/// and the main entrypoint was invoked.
382383
///
383384
[[nodiscard]] bool RunFromLibrary(std::optional<std::string> library_name,
384385
std::optional<std::string> entrypoint,
385-
const std::vector<std::string>& args);
386+
const std::vector<std::string>& args,
387+
bool enable_dart_plugin_registrant);
386388

387389
//----------------------------------------------------------------------------
388390
/// @brief Transition the isolate to the `Phase::Shutdown` phase. The

runtime/dart_isolate_unittests.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,10 @@ TEST_F(DartIsolateTest, DartPluginRegistrantIsCalled) {
613613
messages.push_back(message);
614614
expected_messages.CountDown();
615615
})));
616+
auto settings = CreateSettingsForFixture();
617+
// This is set by default in switches.cc.
618+
settings.enable_dart_plugin_registrant = true;
616619

617-
const auto settings = CreateSettingsForFixture();
618620
auto vm_ref = DartVMRef::Create(settings);
619621
auto thread = CreateNewThread();
620622
TaskRunners task_runners(GetCurrentTestName(), //
@@ -623,8 +625,9 @@ TEST_F(DartIsolateTest, DartPluginRegistrantIsCalled) {
623625
thread, //
624626
thread //
625627
);
626-
auto isolate = RunDartCodeInIsolate(vm_ref, settings, task_runners, "main",
627-
{}, GetFixturesPath());
628+
auto isolate =
629+
RunDartCodeInIsolate(vm_ref, settings, task_runners,
630+
"mainForDartRegistrantTest", {}, GetFixturesPath());
628631
ASSERT_TRUE(isolate);
629632
ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running);
630633
expected_messages.Wait();

runtime/fixtures/runtime_test.dart

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,7 @@ import 'dart:ui';
1010

1111
import 'split_lib_test.dart' deferred as splitlib;
1212

13-
@pragma('vm:entry-point')
14-
void _registerPlugins() { // ignore: unused_element
15-
try {
16-
print('_registerPlugins');
17-
passMessageForDartRegistrantTest('_registerPlugins');
18-
print('_registerPlugins 2');
19-
} catch(_) {}
20-
}
21-
22-
void main() {
23-
try {
24-
print('main');
25-
passMessageForDartRegistrantTest('main');
26-
print('main 2');
27-
} catch(_) {}
28-
}
13+
void main() {}
2914

3015
@pragma('vm:entry-point')
3116
void sayHi() {
@@ -73,7 +58,6 @@ void testCanSaveCompilationTrace() {
7358

7459
void notifyResult(bool success) native 'NotifyNative';
7560
void passMessageForIsolateTest(String message) native 'PassMessageForIsolateTest';
76-
void passMessageForDartRegistrantTest(String message) native 'PassMessageForDartRegistrantTest';
7761

7862
void secondaryIsolateMain(String message) {
7963
print('Secondary isolate got message: ' + message);
@@ -130,3 +114,18 @@ void testCanConvertListOfInts(List<int> args){
130114
args[2] == 3 &&
131115
args[3] == 4);
132116
}
117+
118+
// Test the Dart plugin registrant.
119+
// _registerPlugins requires the entrypoint annotation, so the compiler doesn't tree shake it
120+
// in profile or release mode.
121+
@pragma('vm:entry-point')
122+
void _registerPlugins() { // ignore: unused_element
123+
passMessageForDartRegistrantTest('_registerPlugins');
124+
}
125+
126+
@pragma('vm:entry-point')
127+
void mainForDartRegistrantTest() {
128+
passMessageForDartRegistrantTest('main');
129+
}
130+
131+
void passMessageForDartRegistrantTest(String message) native 'PassMessageForDartRegistrantTest';

shell/common/switches.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) {
415415
&old_gen_heap_size);
416416
settings.old_gen_heap_size = std::stoi(old_gen_heap_size);
417417
}
418+
419+
settings.enable_dart_plugin_registrant = true;
420+
418421
return settings;
419422
}
420423

0 commit comments

Comments
 (0)