forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable lambda like native callbacks in tests and add support for cust…
…om entrypoints. (flutter#8299)
- Loading branch information
1 parent
2812c7d
commit 78de8dc
Showing
11 changed files
with
212 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
void main() {} | ||
|
||
@pragma('vm:entry-point') | ||
void customEntrypoint() { | ||
sayHiFromCustomEntrypoint(); | ||
} | ||
|
||
void sayHiFromCustomEntrypoint() native "SayHiFromCustomEntrypoint"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/shell/platform/embedder/tests/embedder_test_resolver.h" | ||
|
||
#include <mutex> | ||
#include <vector> | ||
|
||
#include "flutter/fml/synchronization/thread_annotations.h" | ||
#include "tonic/converter/dart_converter.h" | ||
|
||
namespace shell { | ||
namespace testing { | ||
|
||
EmbedderTestResolver::EmbedderTestResolver() = default; | ||
|
||
EmbedderTestResolver::~EmbedderTestResolver() = default; | ||
|
||
void EmbedderTestResolver::AddNativeCallback(std::string name, | ||
Dart_NativeFunction callback) { | ||
native_callbacks_[name] = callback; | ||
} | ||
|
||
Dart_NativeFunction EmbedderTestResolver::ResolveCallback( | ||
std::string name) const { | ||
auto found = native_callbacks_.find(name); | ||
if (found == native_callbacks_.end()) { | ||
return nullptr; | ||
} | ||
|
||
return found->second; | ||
} | ||
|
||
static std::mutex gIsolateResolversMutex; | ||
static std::map<Dart_Isolate, std::weak_ptr<EmbedderTestResolver>> | ||
gIsolateResolvers FML_GUARDED_BY(gIsolateResolversMutex); | ||
|
||
Dart_NativeFunction EmbedderTestResolver::DartNativeEntryResolverCallback( | ||
Dart_Handle dart_name, | ||
int num_of_arguments, | ||
bool* auto_setup_scope) { | ||
auto name = tonic::StdStringFromDart(dart_name); | ||
|
||
std::lock_guard<std::mutex> lock(gIsolateResolversMutex); | ||
auto found = gIsolateResolvers.find(Dart_CurrentIsolate()); | ||
if (found == gIsolateResolvers.end()) { | ||
return nullptr; | ||
} | ||
|
||
if (auto resolver = found->second.lock()) { | ||
return resolver->ResolveCallback(std::move(name)); | ||
} else { | ||
gIsolateResolvers.erase(found); | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
static const uint8_t* DartNativeEntrySymbolCallback( | ||
Dart_NativeFunction function) { | ||
return reinterpret_cast<const uint8_t*>("¯\\_(ツ)_/¯"); | ||
} | ||
|
||
void EmbedderTestResolver::SetNativeResolverForIsolate() { | ||
auto result = Dart_SetNativeResolver(Dart_RootLibrary(), | ||
DartNativeEntryResolverCallback, | ||
DartNativeEntrySymbolCallback); | ||
|
||
if (Dart_IsError(result)) { | ||
return; | ||
} | ||
|
||
std::lock_guard<std::mutex> lock(gIsolateResolversMutex); | ||
gIsolateResolvers[Dart_CurrentIsolate()] = shared_from_this(); | ||
|
||
std::vector<Dart_Isolate> isolates_with_dead_resolvers; | ||
for (const auto& entry : gIsolateResolvers) { | ||
if (!entry.second.lock()) { | ||
isolates_with_dead_resolvers.push_back(entry.first); | ||
} | ||
} | ||
|
||
for (const auto& dead_isolate : isolates_with_dead_resolvers) { | ||
gIsolateResolvers.erase(dead_isolate); | ||
} | ||
} | ||
|
||
} // namespace testing | ||
} // namespace shell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_TEST_RESOLVER_H_ | ||
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_TEST_RESOLVER_H_ | ||
|
||
#include <map> | ||
#include <memory> | ||
|
||
#include "flutter/fml/macros.h" | ||
#include "third_party/dart/runtime/include/dart_api.h" | ||
|
||
namespace shell { | ||
namespace testing { | ||
|
||
class EmbedderTestResolver | ||
: public std::enable_shared_from_this<EmbedderTestResolver> { | ||
public: | ||
EmbedderTestResolver(); | ||
|
||
~EmbedderTestResolver(); | ||
|
||
void AddNativeCallback(std::string name, Dart_NativeFunction callback); | ||
|
||
private: | ||
// Friend so that the context can set the native resolver. | ||
friend class EmbedderContext; | ||
|
||
std::map<std::string, Dart_NativeFunction> native_callbacks_; | ||
|
||
void SetNativeResolverForIsolate(); | ||
|
||
Dart_NativeFunction ResolveCallback(std::string name) const; | ||
|
||
static Dart_NativeFunction DartNativeEntryResolverCallback( | ||
Dart_Handle dart_name, | ||
int num_of_arguments, | ||
bool* auto_setup_scope); | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestResolver); | ||
}; | ||
|
||
} // namespace testing | ||
} // namespace shell | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_TEST_RESOLVER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters