@@ -85,8 +85,10 @@ void DartIsolateContext::InitializeJSRuntime() {
8585}
8686
8787void DartIsolateContext::FinalizeJSRuntime () {
88- if (running_dart_isolates > 0 )
88+ if (running_dart_isolates > 0 ||
89+ runtime_ == nullptr ) {
8990 return ;
91+ }
9092
9193 // Prebuilt strings stored in JSRuntime. Only needs to dispose when runtime disposed.
9294 names_installer::Dispose ();
@@ -107,10 +109,10 @@ DartIsolateContext::DartIsolateContext(const uint64_t* dart_methods, int32_t dar
107109 dart_method_ptr_(std::make_unique<DartMethodPointer>(this , dart_methods, dart_methods_length)) {
108110 is_valid_ = true ;
109111 running_dart_isolates++;
110- InitializeJSRuntime ();
111112}
112113
113114JSRuntime* DartIsolateContext::runtime () {
115+ assert_m (runtime_ != nullptr , " nullptr is unsafe" );
114116 return runtime_;
115117}
116118
@@ -187,10 +189,19 @@ void* DartIsolateContext::AddNewPage(double thread_identity,
187189 return nullptr ;
188190}
189191
192+ std::unique_ptr<WebFPage> DartIsolateContext::InitializeNewPageSync (DartIsolateContext* dart_isolate_context,
193+ size_t sync_buffer_size,
194+ double page_context_id) {
195+ dart_isolate_context->profiler ()->StartTrackInitialize ();
196+ DartIsolateContext::InitializeJSRuntime ();
197+ auto page = std::make_unique<WebFPage>(dart_isolate_context, false , sync_buffer_size, page_context_id, nullptr );
198+ dart_isolate_context->profiler ()->FinishTrackInitialize ();
199+
200+ return page;
201+ }
202+
190203void * DartIsolateContext::AddNewPageSync (double thread_identity) {
191- profiler ()->StartTrackSteps (" WebFPage::Initialize" );
192- auto page = std::make_unique<WebFPage>(this , false , 0 , thread_identity, nullptr );
193- profiler ()->FinishTrackSteps ();
204+ auto page = InitializeNewPageSync (this , 0 , thread_identity);
194205
195206 void * p = page.get ();
196207 pages_in_ui_thread_.emplace (std::move (page));
@@ -252,6 +263,10 @@ void DartIsolateContext::RemovePageSync(double thread_identity, WebFPage* page)
252263 break ;
253264 }
254265 }
266+
267+ if (pages_in_ui_thread_.empty ()) {
268+ FinalizeJSRuntime ();
269+ }
255270}
256271
257272} // namespace webf
0 commit comments