// Same as run_timer.cc, but uses std::async instead of a dedicated event loop // thread. Used as a demonstration of a custom message scheduler. #include #include #include #include "helpers.h" #include "include/dart_api.h" #include "include/dart_engine.h" // Calls `startTimer` from timer.dart void StartTimer(Dart_Isolate isolate, uint32_t millis) { WithIsolate(isolate, [&]() { std::initializer_list args{Dart_NewInteger(millis)}; CheckError( Dart_Invoke(Dart_RootLibrary(), Dart_NewStringFromCString("startTimer"), 1, const_cast(args.begin())), "calling startTimer"); }); } // Calls `stopTimer` from timer.dart void StopTimer(Dart_Isolate isolate) { WithIsolate(isolate, [&]() { CheckError(Dart_Invoke(Dart_RootLibrary(), Dart_NewStringFromCString("stopTimer"), 0, nullptr), "calling stopTimer"); }); } // Gets `ticks` from timer.dart int64_t GetTicks(Dart_Isolate isolate) { return WithIsolate(isolate, []() { return IntFromHandle( Dart_GetField(Dart_RootLibrary(), Dart_NewStringFromCString("ticks"))); }); } std::mutex shutdown_mutex; void ScheduleDartMessage(Dart_Isolate isolate, void* context) { std::ignore = std::async(DartEngine_HandleMessage, isolate); } int main(int argc, char** argv) { if (argc == 1) { std::cerr << "Must specify snapshot path" << std::endl; std::exit(1); } char* error = nullptr; // Start an event loop on a separate thread and use it as a default // scheduler. DartEngine_MessageScheduler scheduler{ScheduleDartMessage, nullptr}; DartEngine_SetDefaultMessageScheduler(scheduler); // Load snapshot and create an isolate DartEngine_SnapshotData snapshot_data = AutoSnapshotFromFile(argv[1], &error); CheckError(error, "reading snapshot"); Dart_Isolate isolate = DartEngine_CreateIsolate(snapshot_data, &error); CheckError(error, "creating isolate"); // Call Dart function to start a timer. StartTimer(isolate, 1); // Wait a bit. std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Stop the timer. StopTimer(isolate); // Get timer value. std::cout << "Ticks: " << GetTicks(isolate) << std::endl; DartEngine_Shutdown(); }