Skip to content

Commit 3a3f707

Browse files
authored
Reland "Allow specification of std::functions as native entrypoints from Dart code." (flutter#8329)
This reverts commit 7e77d5c after fixing Windows issues.
1 parent d4275d9 commit 3a3f707

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

shell/platform/embedder/fixtures/simple_main.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,15 @@ void customEntrypoint() {
66
}
77

88
void sayHiFromCustomEntrypoint() native "SayHiFromCustomEntrypoint";
9+
10+
11+
@pragma('vm:entry-point')
12+
void customEntrypoint1() {
13+
sayHiFromCustomEntrypoint1();
14+
sayHiFromCustomEntrypoint2();
15+
sayHiFromCustomEntrypoint3();
16+
}
17+
18+
void sayHiFromCustomEntrypoint1() native "SayHiFromCustomEntrypoint1";
19+
void sayHiFromCustomEntrypoint2() native "SayHiFromCustomEntrypoint2";
20+
void sayHiFromCustomEntrypoint3() native "SayHiFromCustomEntrypoint3";

shell/platform/embedder/tests/embedder_context.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,23 @@
1616
#include "flutter/shell/platform/embedder/embedder.h"
1717
#include "flutter/shell/platform/embedder/tests/embedder_test_resolver.h"
1818

19+
#define CREATE_NATIVE_ENTRY(native_entry) \
20+
([&]() { \
21+
static ::shell::testing::EmbedderContext::NativeEntry closure; \
22+
static Dart_NativeFunction entrypoint = [](Dart_NativeArguments args) { \
23+
closure(args); \
24+
}; \
25+
closure = (native_entry); \
26+
return entrypoint; \
27+
})()
28+
1929
namespace shell {
2030
namespace testing {
2131

2232
class EmbedderContext {
2333
public:
34+
using NativeEntry = std::function<void(Dart_NativeArguments)>;
35+
2436
EmbedderContext(std::string assets_path = "");
2537

2638
~EmbedderContext();

shell/platform/embedder/tests/embedder_unittests.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,45 @@ TEST_F(EmbedderTest, CanInvokeCustomEntrypoint) {
5959
ASSERT_TRUE(engine.is_valid());
6060
}
6161

62+
TEST_F(EmbedderTest, CanInvokeCustomEntrypointMacro) {
63+
auto& context = GetEmbedderContext();
64+
65+
fml::AutoResetWaitableEvent latch1;
66+
fml::AutoResetWaitableEvent latch2;
67+
fml::AutoResetWaitableEvent latch3;
68+
69+
// Can be defined separately.
70+
auto entry1 = [&latch1](Dart_NativeArguments args) {
71+
FML_LOG(ERROR) << "In Callback 1";
72+
latch1.Signal();
73+
};
74+
auto native_entry1 = CREATE_NATIVE_ENTRY(entry1);
75+
context.AddNativeCallback("SayHiFromCustomEntrypoint1", native_entry1);
76+
77+
// Can be wrapped in in the args.
78+
auto entry2 = [&latch2](Dart_NativeArguments args) {
79+
FML_LOG(ERROR) << "In Callback 2";
80+
latch2.Signal();
81+
};
82+
context.AddNativeCallback("SayHiFromCustomEntrypoint2",
83+
CREATE_NATIVE_ENTRY(entry2));
84+
85+
// Everything can be inline.
86+
context.AddNativeCallback(
87+
"SayHiFromCustomEntrypoint3",
88+
CREATE_NATIVE_ENTRY([&latch3](Dart_NativeArguments args) {
89+
FML_LOG(ERROR) << "In Callback 3";
90+
latch3.Signal();
91+
}));
92+
93+
EmbedderConfigBuilder builder(context);
94+
builder.SetDartEntrypoint("customEntrypoint1");
95+
auto engine = builder.LaunchEngine();
96+
latch1.Wait();
97+
latch2.Wait();
98+
latch3.Wait();
99+
ASSERT_TRUE(engine.is_valid());
100+
}
101+
62102
} // namespace testing
63103
} // namespace shell

0 commit comments

Comments
 (0)