Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reland "Allow specification of std::functions as native entrypoints from Dart code." #8329

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions shell/platform/embedder/fixtures/simple_main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
12 changes: 12 additions & 0 deletions shell/platform/embedder/tests/embedder_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(Dart_NativeArguments)>;

EmbedderContext(std::string assets_path = "");

~EmbedderContext();
Expand Down
40 changes: 40 additions & 0 deletions shell/platform/embedder/tests/embedder_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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