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"
22 v8::ArrayBuffer::Allocator
* g_array_buffer_allocator
= nullptr;
25 IsolateHolder::IsolateHolder() {
26 v8::ArrayBuffer::Allocator
* allocator
= g_array_buffer_allocator
;
27 CHECK(allocator
) << "You need to invoke gin::IsolateHolder::Initialize first";
28 v8::Isolate::CreateParams params
;
29 params
.entry_hook
= DebugImpl::GetFunctionEntryHook();
30 params
.code_event_handler
= DebugImpl::GetJitCodeEventHandler();
31 params
.constraints
.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
32 base::SysInfo::AmountOfVirtualMemory());
33 isolate_
= v8::Isolate::New(params
);
34 isolate_data_
.reset(new PerIsolateData(isolate_
, allocator
));
39 isolate_
->GetCodeRange(&code_range
, &size
);
40 Debug::CodeRangeCreatedCallback callback
=
41 DebugImpl::GetCodeRangeCreatedCallback();
42 if (code_range
&& size
&& callback
)
43 callback(code_range
, size
);
48 IsolateHolder::~IsolateHolder() {
49 if (task_observer_
.get())
50 base::MessageLoop::current()->RemoveTaskObserver(task_observer_
.get());
55 isolate_
->GetCodeRange(&code_range
, &size
);
56 Debug::CodeRangeDeletedCallback callback
=
57 DebugImpl::GetCodeRangeDeletedCallback();
58 if (code_range
&& callback
)
62 isolate_data_
.reset();
68 void IsolateHolder::Initialize(ScriptMode mode
,
69 v8::ArrayBuffer::Allocator
* allocator
) {
71 gin::V8Initializer::Initialize(mode
, allocator
);
72 g_array_buffer_allocator
= allocator
;
75 void IsolateHolder::AddRunMicrotasksObserver() {
76 DCHECK(!task_observer_
.get());
77 task_observer_
.reset(new RunMicrotasksObserver(isolate_
));;
78 base::MessageLoop::current()->AddTaskObserver(task_observer_
.get());
81 void IsolateHolder::RemoveRunMicrotasksObserver() {
82 DCHECK(task_observer_
.get());
83 base::MessageLoop::current()->RemoveTaskObserver(task_observer_
.get());
84 task_observer_
.reset();