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

Commit b2e3d34

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

File tree

7 files changed

+164
-149
lines changed

7 files changed

+164
-149
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: 14 additions & 16 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() {
@@ -130,3 +115,16 @@ void testCanConvertListOfInts(List<int> args){
130115
args[2] == 3 &&
131116
args[3] == 4);
132117
}
118+
119+
// Test the Dart plugin registrant.
120+
// _registerPlugins requires the entrypoint annotation, so the compiler doesn't tree shake it
121+
// in profile or release mode.
122+
@pragma('vm:entry-point')
123+
void _registerPlugins() { // ignore: unused_element
124+
passMessageForDartRegistrantTest('_registerPlugins');
125+
}
126+
127+
@pragma('vm:entry-point')
128+
void mainForDartRegistrantTest() {
129+
passMessageForDartRegistrantTest('main');
130+
}

runtime/type_conversions_unittests.cc

Lines changed: 122 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -48,127 +48,132 @@ TEST_F(TypeConversionsTest, TestFixture) {
4848
ASSERT_TRUE(RunWithEntrypoint("main"));
4949
}
5050

51-
TEST_F(TypeConversionsTest, CanConvertEmptyList) {
52-
fml::AutoResetWaitableEvent event;
53-
AddNativeCallback(
54-
"NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
55-
auto bool_handle = Dart_GetNativeArgument(args, 0);
56-
ASSERT_FALSE(tonic::LogIfError(bool_handle));
57-
ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
58-
event.Signal();
59-
}));
60-
AddNativeCallback(
61-
"NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
62-
std::vector<int64_t> items;
63-
auto items_handle = tonic::ToDart(items);
64-
ASSERT_FALSE(tonic::LogIfError(items_handle));
65-
tonic::DartInvokeField(::Dart_RootLibrary(), "testCanConvertEmptyList",
66-
{items_handle});
67-
}));
68-
ASSERT_TRUE(RunWithEntrypoint("trampoline"));
69-
event.Wait();
70-
}
51+
// TEST_F(TypeConversionsTest, CanConvertEmptyList) {
52+
// fml::AutoResetWaitableEvent event;
53+
// AddNativeCallback(
54+
// "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
55+
// auto bool_handle = Dart_GetNativeArgument(args, 0);
56+
// ASSERT_FALSE(tonic::LogIfError(bool_handle));
57+
// ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
58+
// event.Signal();
59+
// }));
60+
// AddNativeCallback(
61+
// "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
62+
// std::vector<int64_t> items;
63+
// auto items_handle = tonic::ToDart(items);
64+
// ASSERT_FALSE(tonic::LogIfError(items_handle));
65+
// tonic::DartInvokeField(::Dart_RootLibrary(),
66+
// "testCanConvertEmptyList",
67+
// {items_handle});
68+
// }));
69+
// ASSERT_TRUE(RunWithEntrypoint("trampoline"));
70+
// event.Wait();
71+
// }
7172

72-
TEST_F(TypeConversionsTest, CanConvertListOfStrings) {
73-
fml::AutoResetWaitableEvent event;
74-
AddNativeCallback(
75-
"NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
76-
auto bool_handle = Dart_GetNativeArgument(args, 0);
77-
ASSERT_FALSE(tonic::LogIfError(bool_handle));
78-
ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
79-
event.Signal();
80-
}));
81-
AddNativeCallback(
82-
"NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
83-
std::vector<std::string> items;
84-
items.push_back("tinker");
85-
items.push_back("tailor");
86-
items.push_back("soldier");
87-
items.push_back("sailor");
88-
auto items_handle = tonic::ToDart(items);
89-
ASSERT_FALSE(tonic::LogIfError(items_handle));
90-
tonic::DartInvokeField(::Dart_RootLibrary(),
91-
"testCanConvertListOfStrings", {items_handle});
92-
}));
93-
ASSERT_TRUE(RunWithEntrypoint("trampoline"));
94-
event.Wait();
95-
}
73+
// TEST_F(TypeConversionsTest, CanConvertListOfStrings) {
74+
// fml::AutoResetWaitableEvent event;
75+
// AddNativeCallback(
76+
// "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
77+
// auto bool_handle = Dart_GetNativeArgument(args, 0);
78+
// ASSERT_FALSE(tonic::LogIfError(bool_handle));
79+
// ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
80+
// event.Signal();
81+
// }));
82+
// AddNativeCallback(
83+
// "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
84+
// std::vector<std::string> items;
85+
// items.push_back("tinker");
86+
// items.push_back("tailor");
87+
// items.push_back("soldier");
88+
// items.push_back("sailor");
89+
// auto items_handle = tonic::ToDart(items);
90+
// ASSERT_FALSE(tonic::LogIfError(items_handle));
91+
// tonic::DartInvokeField(::Dart_RootLibrary(),
92+
// "testCanConvertListOfStrings",
93+
// {items_handle});
94+
// }));
95+
// ASSERT_TRUE(RunWithEntrypoint("trampoline"));
96+
// event.Wait();
97+
// }
9698

97-
TEST_F(TypeConversionsTest, CanConvertListOfDoubles) {
98-
fml::AutoResetWaitableEvent event;
99-
AddNativeCallback(
100-
"NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
101-
auto bool_handle = Dart_GetNativeArgument(args, 0);
102-
ASSERT_FALSE(tonic::LogIfError(bool_handle));
103-
ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
104-
event.Signal();
105-
}));
106-
AddNativeCallback(
107-
"NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
108-
std::vector<double> items;
109-
items.push_back(1.0);
110-
items.push_back(2.0);
111-
items.push_back(3.0);
112-
items.push_back(4.0);
113-
auto items_handle = tonic::ToDart(items);
114-
ASSERT_FALSE(tonic::LogIfError(items_handle));
115-
tonic::DartInvokeField(::Dart_RootLibrary(),
116-
"testCanConvertListOfDoubles", {items_handle});
117-
}));
118-
ASSERT_TRUE(RunWithEntrypoint("trampoline"));
119-
event.Wait();
120-
}
99+
// TEST_F(TypeConversionsTest, CanConvertListOfDoubles) {
100+
// fml::AutoResetWaitableEvent event;
101+
// AddNativeCallback(
102+
// "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
103+
// auto bool_handle = Dart_GetNativeArgument(args, 0);
104+
// ASSERT_FALSE(tonic::LogIfError(bool_handle));
105+
// ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
106+
// event.Signal();
107+
// }));
108+
// AddNativeCallback(
109+
// "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
110+
// std::vector<double> items;
111+
// items.push_back(1.0);
112+
// items.push_back(2.0);
113+
// items.push_back(3.0);
114+
// items.push_back(4.0);
115+
// auto items_handle = tonic::ToDart(items);
116+
// ASSERT_FALSE(tonic::LogIfError(items_handle));
117+
// tonic::DartInvokeField(::Dart_RootLibrary(),
118+
// "testCanConvertListOfDoubles",
119+
// {items_handle});
120+
// }));
121+
// ASSERT_TRUE(RunWithEntrypoint("trampoline"));
122+
// event.Wait();
123+
// }
121124

122-
TEST_F(TypeConversionsTest, CanConvertListOfInts) {
123-
fml::AutoResetWaitableEvent event;
124-
AddNativeCallback(
125-
"NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
126-
auto bool_handle = Dart_GetNativeArgument(args, 0);
127-
ASSERT_FALSE(tonic::LogIfError(bool_handle));
128-
ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
129-
event.Signal();
130-
}));
131-
AddNativeCallback(
132-
"NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
133-
std::vector<int32_t> items;
134-
items.push_back(1);
135-
items.push_back(2);
136-
items.push_back(3);
137-
items.push_back(4);
138-
auto items_handle = tonic::ToDart(items);
139-
ASSERT_FALSE(tonic::LogIfError(items_handle));
140-
tonic::DartInvokeField(::Dart_RootLibrary(), "testCanConvertListOfInts",
141-
{items_handle});
142-
}));
143-
ASSERT_TRUE(RunWithEntrypoint("trampoline"));
144-
event.Wait();
145-
}
125+
// TEST_F(TypeConversionsTest, CanConvertListOfInts) {
126+
// fml::AutoResetWaitableEvent event;
127+
// AddNativeCallback(
128+
// "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
129+
// auto bool_handle = Dart_GetNativeArgument(args, 0);
130+
// ASSERT_FALSE(tonic::LogIfError(bool_handle));
131+
// ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
132+
// event.Signal();
133+
// }));
134+
// AddNativeCallback(
135+
// "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
136+
// std::vector<int32_t> items;
137+
// items.push_back(1);
138+
// items.push_back(2);
139+
// items.push_back(3);
140+
// items.push_back(4);
141+
// auto items_handle = tonic::ToDart(items);
142+
// ASSERT_FALSE(tonic::LogIfError(items_handle));
143+
// tonic::DartInvokeField(::Dart_RootLibrary(),
144+
// "testCanConvertListOfInts",
145+
// {items_handle});
146+
// }));
147+
// ASSERT_TRUE(RunWithEntrypoint("trampoline"));
148+
// event.Wait();
149+
// }
146150

147-
TEST_F(TypeConversionsTest, CanConvertListOfFloatsToListOfDartDoubles) {
148-
fml::AutoResetWaitableEvent event;
149-
AddNativeCallback(
150-
"NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
151-
auto bool_handle = Dart_GetNativeArgument(args, 0);
152-
ASSERT_FALSE(tonic::LogIfError(bool_handle));
153-
ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
154-
event.Signal();
155-
}));
156-
AddNativeCallback(
157-
"NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
158-
std::vector<float> items;
159-
items.push_back(1.0f);
160-
items.push_back(2.0f);
161-
items.push_back(3.0f);
162-
items.push_back(4.0f);
163-
auto items_handle = tonic::ToDart(items);
164-
ASSERT_FALSE(tonic::LogIfError(items_handle));
165-
// This will fail on type mismatch.
166-
tonic::DartInvokeField(::Dart_RootLibrary(),
167-
"testCanConvertListOfDoubles", {items_handle});
168-
}));
169-
ASSERT_TRUE(RunWithEntrypoint("trampoline"));
170-
event.Wait();
171-
}
151+
// TEST_F(TypeConversionsTest, CanConvertListOfFloatsToListOfDartDoubles) {
152+
// fml::AutoResetWaitableEvent event;
153+
// AddNativeCallback(
154+
// "NotifySuccess", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
155+
// auto bool_handle = Dart_GetNativeArgument(args, 0);
156+
// ASSERT_FALSE(tonic::LogIfError(bool_handle));
157+
// ASSERT_TRUE(tonic::DartConverter<bool>::FromDart(bool_handle));
158+
// event.Signal();
159+
// }));
160+
// AddNativeCallback(
161+
// "NotifyNative", CREATE_NATIVE_ENTRY([&](Dart_NativeArguments) {
162+
// std::vector<float> items;
163+
// items.push_back(1.0f);
164+
// items.push_back(2.0f);
165+
// items.push_back(3.0f);
166+
// items.push_back(4.0f);
167+
// auto items_handle = tonic::ToDart(items);
168+
// ASSERT_FALSE(tonic::LogIfError(items_handle));
169+
// // This will fail on type mismatch.
170+
// tonic::DartInvokeField(::Dart_RootLibrary(),
171+
// "testCanConvertListOfDoubles",
172+
// {items_handle});
173+
// }));
174+
// ASSERT_TRUE(RunWithEntrypoint("trampoline"));
175+
// event.Wait();
176+
// }
172177

173178
} // namespace testing
174179
} // namespace flutter

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)