@@ -230,6 +230,11 @@ PerIsolatePlatformData::PerIsolatePlatformData(
230230 uv_unref (reinterpret_cast <uv_handle_t *>(flush_tasks_));
231231}
232232
233+ std::shared_ptr<v8::TaskRunner>
234+ PerIsolatePlatformData::GetForegroundTaskRunner () {
235+ return shared_from_this ();
236+ }
237+
233238void PerIsolatePlatformData::FlushTasks (uv_async_t * handle) {
234239 auto platform_data = static_cast <PerIsolatePlatformData*>(handle->data );
235240 platform_data->FlushForegroundTasksInternal ();
@@ -267,7 +272,7 @@ void PerIsolatePlatformData::PostNonNestableDelayedTask(
267272}
268273
269274PerIsolatePlatformData::~PerIsolatePlatformData () {
270- Shutdown ( );
275+ CHECK (!flush_tasks_ );
271276}
272277
273278void PerIsolatePlatformData::AddShutdownCallback (void (*callback)(void *),
@@ -325,30 +330,44 @@ NodePlatform::NodePlatform(int thread_pool_size,
325330
326331void NodePlatform::RegisterIsolate (Isolate* isolate, uv_loop_t * loop) {
327332 Mutex::ScopedLock lock (per_isolate_mutex_);
328- std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
329- CHECK (!existing);
330- per_isolate_[isolate] =
331- std::make_shared<PerIsolatePlatformData>(isolate, loop);
333+ auto delegate = std::make_shared<PerIsolatePlatformData>(isolate, loop);
334+ IsolatePlatformDelegate* ptr = delegate.get ();
335+ auto insertion = per_isolate_.emplace (
336+ isolate,
337+ std::make_pair (ptr, std::move (delegate)));
338+ CHECK (insertion.second );
339+ }
340+
341+ void NodePlatform::RegisterIsolate (Isolate* isolate,
342+ IsolatePlatformDelegate* delegate) {
343+ Mutex::ScopedLock lock (per_isolate_mutex_);
344+ auto insertion = per_isolate_.emplace (
345+ isolate,
346+ std::make_pair (delegate, std::shared_ptr<PerIsolatePlatformData>{}));
347+ CHECK (insertion.second );
332348}
333349
334350void NodePlatform::UnregisterIsolate (Isolate* isolate) {
335351 Mutex::ScopedLock lock (per_isolate_mutex_);
336- std::shared_ptr<PerIsolatePlatformData> existing = per_isolate_[isolate];
337- CHECK (existing);
338- existing->Shutdown ();
339- per_isolate_.erase (isolate);
352+ auto existing_it = per_isolate_.find (isolate);
353+ CHECK_NE (existing_it, per_isolate_.end ());
354+ auto & existing = existing_it->second ;
355+ if (existing.second ) {
356+ existing.second ->Shutdown ();
357+ }
358+ per_isolate_.erase (existing_it);
340359}
341360
342361void NodePlatform::AddIsolateFinishedCallback (Isolate* isolate,
343362 void (*cb)(void *), void* data) {
344363 Mutex::ScopedLock lock (per_isolate_mutex_);
345364 auto it = per_isolate_.find (isolate);
346365 if (it == per_isolate_.end ()) {
347- CHECK (it->second );
348366 cb (data);
349367 return ;
350368 }
351- it->second ->AddShutdownCallback (cb, data);
369+ CHECK (it->second .second );
370+ it->second .second ->AddShutdownCallback (cb, data);
352371}
353372
354373void NodePlatform::Shutdown () {
@@ -394,7 +413,7 @@ void PerIsolatePlatformData::RunForegroundTask(uv_timer_t* handle) {
394413}
395414
396415void NodePlatform::DrainTasks (Isolate* isolate) {
397- std::shared_ptr<PerIsolatePlatformData> per_isolate = ForIsolate (isolate);
416+ std::shared_ptr<PerIsolatePlatformData> per_isolate = ForNodeIsolate (isolate);
398417
399418 do {
400419 // Worker tasks aren't associated with an Isolate.
@@ -452,23 +471,32 @@ void NodePlatform::CallDelayedOnWorkerThread(std::unique_ptr<Task> task,
452471}
453472
454473
474+ IsolatePlatformDelegate* NodePlatform::ForIsolate (Isolate* isolate) {
475+ Mutex::ScopedLock lock (per_isolate_mutex_);
476+ auto data = per_isolate_[isolate];
477+ CHECK_NOT_NULL (data.first );
478+ return data.first ;
479+ }
480+
455481std::shared_ptr<PerIsolatePlatformData>
456- NodePlatform::ForIsolate (Isolate* isolate) {
482+ NodePlatform::ForNodeIsolate (Isolate* isolate) {
457483 Mutex::ScopedLock lock (per_isolate_mutex_);
458- std::shared_ptr<PerIsolatePlatformData> data = per_isolate_[isolate];
459- CHECK (data);
460- return data;
484+ auto data = per_isolate_[isolate];
485+ CHECK (data. second );
486+ return data. second ;
461487}
462488
463489bool NodePlatform::FlushForegroundTasks (Isolate* isolate) {
464- return ForIsolate (isolate)->FlushForegroundTasksInternal ();
490+ return ForNodeIsolate (isolate)->FlushForegroundTasksInternal ();
465491}
466492
467- bool NodePlatform::IdleTasksEnabled (Isolate* isolate) { return false ; }
493+ bool NodePlatform::IdleTasksEnabled (Isolate* isolate) {
494+ return ForIsolate (isolate)->IdleTasksEnabled ();
495+ }
468496
469497std::shared_ptr<v8::TaskRunner>
470498NodePlatform::GetForegroundTaskRunner (Isolate* isolate) {
471- return ForIsolate (isolate);
499+ return ForIsolate (isolate)-> GetForegroundTaskRunner () ;
472500}
473501
474502double NodePlatform::MonotonicallyIncreasingTime () {
0 commit comments