1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/public/isolate_holder.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/sys_info.h"
13 #include "gin/debug_impl.h"
14 #include "gin/function_template.h"
15 #include "gin/per_isolate_data.h"
16 #include "gin/run_microtasks_observer.h"
17 #include "gin/v8_initializer.h"
18 #include "gin/v8_isolate_memory_dump_provider.h"
23 v8::ArrayBuffer::Allocator
* g_array_buffer_allocator
= nullptr;
26 IsolateHolder::IsolateHolder() : IsolateHolder(AccessMode::kSingleThread
) {
29 IsolateHolder::IsolateHolder(AccessMode access_mode
)
30 : access_mode_(access_mode
) {
31 v8::ArrayBuffer::Allocator
* allocator
= g_array_buffer_allocator
;
32 CHECK(allocator
) << "You need to invoke gin::IsolateHolder::Initialize first";
33 v8::Isolate::CreateParams params
;
34 params
.entry_hook
= DebugImpl::GetFunctionEntryHook();
35 params
.code_event_handler
= DebugImpl::GetJitCodeEventHandler();
36 params
.constraints
.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
37 base::SysInfo::AmountOfVirtualMemory());
38 params
.array_buffer_allocator
= allocator
;
39 isolate_
= v8::Isolate::New(params
);
40 isolate_data_
.reset(new PerIsolateData(isolate_
, allocator
));
41 isolate_memory_dump_provider_
.reset(new V8IsolateMemoryDumpProvider(this));
46 isolate_
->GetCodeRange(&code_range
, &size
);
47 Debug::CodeRangeCreatedCallback callback
=
48 DebugImpl::GetCodeRangeCreatedCallback();
49 if (code_range
&& size
&& callback
)
50 callback(code_range
, size
);
55 IsolateHolder::~IsolateHolder() {
56 if (task_observer_
.get())
57 base::MessageLoop::current()->RemoveTaskObserver(task_observer_
.get());
62 isolate_
->GetCodeRange(&code_range
, &size
);
63 Debug::CodeRangeDeletedCallback callback
=
64 DebugImpl::GetCodeRangeDeletedCallback();
65 if (code_range
&& callback
)
69 isolate_memory_dump_provider_
.reset();
70 isolate_data_
.reset();
76 void IsolateHolder::Initialize(ScriptMode mode
,
77 v8::ArrayBuffer::Allocator
* allocator
) {
79 V8Initializer::Initialize(mode
);
80 g_array_buffer_allocator
= allocator
;
83 void IsolateHolder::AddRunMicrotasksObserver() {
84 DCHECK(!task_observer_
.get());
85 task_observer_
.reset(new RunMicrotasksObserver(isolate_
));;
86 base::MessageLoop::current()->AddTaskObserver(task_observer_
.get());
89 void IsolateHolder::RemoveRunMicrotasksObserver() {
90 DCHECK(task_observer_
.get());
91 base::MessageLoop::current()->RemoveTaskObserver(task_observer_
.get());
92 task_observer_
.reset();