@@ -422,6 +422,7 @@ base::LazyMutex Shell::workers_mutex_;
422422bool Shell::allow_new_workers_ = true ;
423423i::List<Worker*> Shell::workers_;
424424std::vector<ExternalizedContents> Shell::externalized_contents_;
425+ std::map<v8::Isolate*, bool > Shell::isolate_status;
425426
426427Global<Context> Shell::evaluation_context_;
427428ArrayBuffer::Allocator* Shell::array_buffer_allocator;
@@ -1345,6 +1346,18 @@ void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
13451346 const_cast <v8::FunctionCallbackInfo<v8::Value>*>(&args));
13461347}
13471348
1349+ void Shell::WaitUntilDone (const v8::FunctionCallbackInfo<v8::Value>& args) {
1350+ if (isolate_status.count (args.GetIsolate ()) == 0 ) {
1351+ isolate_status.insert (std::make_pair (args.GetIsolate (), true ));
1352+ } else {
1353+ isolate_status[args.GetIsolate ()] = true ;
1354+ }
1355+ }
1356+
1357+ void Shell::NotifyDone (const v8::FunctionCallbackInfo<v8::Value>& args) {
1358+ DCHECK_EQ (isolate_status.count (args.GetIsolate ()), 1 );
1359+ isolate_status[args.GetIsolate ()] = false ;
1360+ }
13481361
13491362void Shell::Version (const v8::FunctionCallbackInfo<v8::Value>& args) {
13501363 args.GetReturnValue ().Set (
@@ -1582,6 +1595,19 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
15821595 .ToLocalChecked (),
15831596 FunctionTemplate::New (isolate, Quit));
15841597 }
1598+ Local<ObjectTemplate> test_template = ObjectTemplate::New (isolate);
1599+ global_template->Set (
1600+ String::NewFromUtf8 (isolate, " testRunner" , NewStringType::kNormal )
1601+ .ToLocalChecked (),
1602+ test_template);
1603+ test_template->Set (
1604+ String::NewFromUtf8 (isolate, " notifyDone" , NewStringType::kNormal )
1605+ .ToLocalChecked (),
1606+ FunctionTemplate::New (isolate, NotifyDone));
1607+ test_template->Set (
1608+ String::NewFromUtf8 (isolate, " waitUntilDone" , NewStringType::kNormal )
1609+ .ToLocalChecked (),
1610+ FunctionTemplate::New (isolate, WaitUntilDone));
15851611 global_template->Set (
15861612 String::NewFromUtf8 (isolate, " version" , NewStringType::kNormal )
15871613 .ToLocalChecked (),
@@ -2266,6 +2292,8 @@ void SourceGroup::ExecuteInThread() {
22662292 create_params.host_import_module_dynamically_callback_ =
22672293 Shell::HostImportModuleDynamically;
22682294 Isolate* isolate = Isolate::New (create_params);
2295+
2296+ v8::platform::EnsureEventLoopInitialized (g_platform, isolate);
22692297 D8Console console (isolate);
22702298 debug::SetConsoleDelegate (isolate, &console);
22712299 for (int i = 0 ; i < Shell::options.stress_runs ; ++i) {
@@ -2290,6 +2318,8 @@ void SourceGroup::ExecuteInThread() {
22902318 done_semaphore_.Signal ();
22912319 }
22922320
2321+ Shell::CompleteMessageLoop (isolate);
2322+
22932323 isolate->Dispose ();
22942324}
22952325
@@ -2646,6 +2676,7 @@ int Shell::RunMain(Isolate* isolate, int argc, char* argv[], bool last_run) {
26462676 options.isolate_sources [i].StartExecuteInThread ();
26472677 }
26482678 {
2679+ v8::platform::EnsureEventLoopInitialized (g_platform, isolate);
26492680 if (options.lcov_file ) {
26502681 debug::Coverage::SelectMode (isolate, debug::Coverage::kPreciseCount );
26512682 }
@@ -2668,6 +2699,7 @@ int Shell::RunMain(Isolate* isolate, int argc, char* argv[], bool last_run) {
26682699 WriteLcovData (isolate, options.lcov_file );
26692700 }
26702701 CollectGarbage (isolate);
2702+ CompleteMessageLoop (isolate);
26712703 for (int i = 1 ; i < options.num_isolates ; ++i) {
26722704 if (last_run) {
26732705 options.isolate_sources [i].JoinThread ();
@@ -2695,24 +2727,28 @@ void Shell::CollectGarbage(Isolate* isolate) {
26952727 }
26962728}
26972729
2730+ void Shell::CompleteMessageLoop (Isolate* isolate) {
2731+ while (v8::platform::PumpMessageLoop (
2732+ g_platform, isolate,
2733+ Shell::isolate_status[isolate]
2734+ ? platform::MessageLoopBehavior::kWaitForWork
2735+ : platform::MessageLoopBehavior::kDoNotWait )) {
2736+ isolate->RunMicrotasks ();
2737+ }
2738+ v8::platform::RunIdleTasks (g_platform, isolate,
2739+ 50.0 / base::Time::kMillisecondsPerSecond );
2740+ }
2741+
26982742void Shell::EmptyMessageQueues (Isolate* isolate) {
26992743 if (i::FLAG_verify_predictable) return ;
2700- while (true ) {
2701- // Pump the message loop until it is empty.
2702- while (v8::platform::PumpMessageLoop (g_platform, isolate)) {
2703- isolate->RunMicrotasks ();
2704- }
2705- // Run the idle tasks.
2706- v8::platform::RunIdleTasks (g_platform, isolate,
2707- 50.0 / base::Time::kMillisecondsPerSecond );
2708- // If there are still outstanding waiters, sleep a little (to wait for
2709- // background tasks) and then try everything again.
2710- if (reinterpret_cast <i::Isolate*>(isolate)->GetWaitCountForTesting () > 0 ) {
2711- base::OS::Sleep (base::TimeDelta::FromMilliseconds (1 ));
2712- } else {
2713- break ;
2714- }
2715- }
2744+ // Pump the message loop until it is empty.
2745+ while (v8::platform::PumpMessageLoop (
2746+ g_platform, isolate, platform::MessageLoopBehavior::kDoNotWait )) {
2747+ isolate->RunMicrotasks ();
2748+ }
2749+ // Run the idle tasks.
2750+ v8::platform::RunIdleTasks (g_platform, isolate,
2751+ 50.0 / base::Time::kMillisecondsPerSecond );
27162752}
27172753
27182754class Serializer : public ValueSerializer ::Delegate {
0 commit comments