From 4eaf46aeff9d8db559a77b09fb0b63de65a030ba Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 27 Mar 2019 11:33:14 -0700 Subject: [PATCH] Reland "Allow specification of std::functions as native entrypoints from Dart code." This reverts commit 7e77d5c484a08cc1c189619e80ae71a7f1df62fb after fixing Windows issues. --- .../embedder/fixtures/simple_main.dart | 12 ++++++ .../embedder/tests/embedder_context.h | 12 ++++++ .../embedder/tests/embedder_unittests.cc | 40 +++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/shell/platform/embedder/fixtures/simple_main.dart b/shell/platform/embedder/fixtures/simple_main.dart index b1124b508b7e3..bd3999563e031 100644 --- a/shell/platform/embedder/fixtures/simple_main.dart +++ b/shell/platform/embedder/fixtures/simple_main.dart @@ -6,3 +6,15 @@ void customEntrypoint() { } void sayHiFromCustomEntrypoint() native "SayHiFromCustomEntrypoint"; + + +@pragma('vm:entry-point') +void customEntrypoint1() { + sayHiFromCustomEntrypoint1(); + sayHiFromCustomEntrypoint2(); + sayHiFromCustomEntrypoint3(); +} + +void sayHiFromCustomEntrypoint1() native "SayHiFromCustomEntrypoint1"; +void sayHiFromCustomEntrypoint2() native "SayHiFromCustomEntrypoint2"; +void sayHiFromCustomEntrypoint3() native "SayHiFromCustomEntrypoint3"; diff --git a/shell/platform/embedder/tests/embedder_context.h b/shell/platform/embedder/tests/embedder_context.h index 6405163d9cd6a..cf31140d40029 100644 --- a/shell/platform/embedder/tests/embedder_context.h +++ b/shell/platform/embedder/tests/embedder_context.h @@ -16,11 +16,23 @@ #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/embedder/tests/embedder_test_resolver.h" +#define CREATE_NATIVE_ENTRY(native_entry) \ + ([&]() { \ + static ::shell::testing::EmbedderContext::NativeEntry closure; \ + static Dart_NativeFunction entrypoint = [](Dart_NativeArguments args) { \ + closure(args); \ + }; \ + closure = (native_entry); \ + return entrypoint; \ + })() + namespace shell { namespace testing { class EmbedderContext { public: + using NativeEntry = std::function; + EmbedderContext(std::string assets_path = ""); ~EmbedderContext(); diff --git a/shell/platform/embedder/tests/embedder_unittests.cc b/shell/platform/embedder/tests/embedder_unittests.cc index 01d23ff0d885f..6bad99a39dd0c 100644 --- a/shell/platform/embedder/tests/embedder_unittests.cc +++ b/shell/platform/embedder/tests/embedder_unittests.cc @@ -59,5 +59,45 @@ TEST_F(EmbedderTest, CanInvokeCustomEntrypoint) { ASSERT_TRUE(engine.is_valid()); } +TEST_F(EmbedderTest, CanInvokeCustomEntrypointMacro) { + auto& context = GetEmbedderContext(); + + fml::AutoResetWaitableEvent latch1; + fml::AutoResetWaitableEvent latch2; + fml::AutoResetWaitableEvent latch3; + + // Can be defined separately. + auto entry1 = [&latch1](Dart_NativeArguments args) { + FML_LOG(ERROR) << "In Callback 1"; + latch1.Signal(); + }; + auto native_entry1 = CREATE_NATIVE_ENTRY(entry1); + context.AddNativeCallback("SayHiFromCustomEntrypoint1", native_entry1); + + // Can be wrapped in in the args. + auto entry2 = [&latch2](Dart_NativeArguments args) { + FML_LOG(ERROR) << "In Callback 2"; + latch2.Signal(); + }; + context.AddNativeCallback("SayHiFromCustomEntrypoint2", + CREATE_NATIVE_ENTRY(entry2)); + + // Everything can be inline. + context.AddNativeCallback( + "SayHiFromCustomEntrypoint3", + CREATE_NATIVE_ENTRY([&latch3](Dart_NativeArguments args) { + FML_LOG(ERROR) << "In Callback 3"; + latch3.Signal(); + })); + + EmbedderConfigBuilder builder(context); + builder.SetDartEntrypoint("customEntrypoint1"); + auto engine = builder.LaunchEngine(); + latch1.Wait(); + latch2.Wait(); + latch3.Wait(); + ASSERT_TRUE(engine.is_valid()); +} + } // namespace testing } // namespace shell