diff --git a/runtime/dart_isolate_unittests.cc b/runtime/dart_isolate_unittests.cc index 21452214c24b6..ab6c29617ecf3 100644 --- a/runtime/dart_isolate_unittests.cc +++ b/runtime/dart_isolate_unittests.cc @@ -9,6 +9,7 @@ #include "flutter/runtime/dart_vm.h" #include "flutter/testing/testing.h" #include "flutter/testing/thread_test.h" +#include "third_party/tonic/scopes/dart_isolate_scope.h" #define CURRENT_TEST_NAME \ std::string { \ @@ -99,6 +100,19 @@ class AutoIsolateShutdown { bool IsValid() const { return isolate_ != nullptr; } + FML_WARN_UNUSED_RESULT + bool RunInIsolateScope(std::function closure) { + if (!isolate_) { + return false; + } + tonic::DartIsolateScope scope(isolate_->isolate()); + tonic::DartApiScope api_scope; + if (closure) { + return closure(); + } + return true; + } + blink::DartIsolate* get() { FML_CHECK(isolate_); return isolate_.get(); @@ -210,4 +224,18 @@ TEST_F(DartIsolateTest, IsolateCannotLoadAndRunUnknownDartEntrypoint) { ASSERT_FALSE(isolate); } +TEST_F(DartIsolateTest, CanRunDartCodeCodeSynchronously) { + auto isolate = RunDartCodeInIsolate(GetCurrentTaskRunner(), "main"); + + ASSERT_TRUE(isolate); + + ASSERT_TRUE(isolate->RunInIsolateScope([]() -> bool { + if (tonic::LogIfError(::Dart_Invoke(Dart_RootLibrary(), + tonic::ToDart("sayHi"), 0, nullptr))) { + return false; + } + return true; + })); +} + } // namespace blink diff --git a/runtime/fixtures/simple_main.dart b/runtime/fixtures/simple_main.dart index f47442176160f..5d6a46eb7f1cf 100644 --- a/runtime/fixtures/simple_main.dart +++ b/runtime/fixtures/simple_main.dart @@ -3,5 +3,12 @@ // found in the LICENSE file. void main() { - print('Hello'); +} + +void sayHi() { + print("Hi"); +} + +void throwExceptionNow() { + throw("Hello"); }