blob: 4491b392412caac052bf9e41c70a28086ada8a17 [file] [log] [blame]
[email protected]f04b0e92013-11-22 14:20:551// 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.
4
5#include "gin/public/isolate_holder.h"
6
avi90e658dd2015-12-21 07:16:197#include <stddef.h>
[email protected]f04b0e92013-11-22 14:20:558#include <stdlib.h>
9#include <string.h>
mostynbc862da82016-04-03 15:54:3310
11#include <memory>
dchenge48600452015-12-28 02:24:5012#include <utility>
[email protected]f04b0e92013-11-22 14:20:5513
14#include "base/logging.h"
Gabriel Charette5bb3c642018-04-25 22:28:0515#include "base/message_loop/message_loop_current.h"
Gabriel Charette5ff87ce2017-05-16 18:03:4516#include "base/single_thread_task_runner.h"
[email protected]f04b0e92013-11-22 14:20:5517#include "base/sys_info.h"
Siddhartha331b5f0d2017-12-12 14:47:0518#include "base/threading/thread_task_runner_handle.h"
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5519#include "build/build_config.h"
jochen4879fd2c2014-09-16 15:04:2920#include "gin/debug_impl.h"
[email protected]314cde12013-11-23 20:26:5121#include "gin/function_template.h"
[email protected]f04b0e92013-11-22 14:20:5522#include "gin/per_isolate_data.h"
oth05c26fde2015-04-05 14:30:5723#include "gin/v8_initializer.h"
ssid83aa5be2015-05-08 12:03:2624#include "gin/v8_isolate_memory_dump_provider.h"
baixo3a3c88a2014-10-28 11:52:2125
[email protected]f04b0e92013-11-22 14:20:5526namespace gin {
27
28namespace {
oth05c26fde2015-04-05 14:30:5729v8::ArrayBuffer::Allocator* g_array_buffer_allocator = nullptr;
Hitoshi Yoshidaa31cce22017-11-21 04:03:3030const intptr_t* g_reference_table = nullptr;
[email protected]f04b0e92013-11-22 14:20:5531} // namespace
32
altimin124814c2017-01-03 14:06:5433IsolateHolder::IsolateHolder(
Ulan Degenbaev51945ab2018-08-23 14:12:5834 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
35 IsolateType isolate_type)
36 : IsolateHolder(std::move(task_runner),
37 AccessMode::kSingleThread,
38 isolate_type) {}
ssidcf207632015-04-24 14:44:1839
altimin124814c2017-01-03 14:06:5440IsolateHolder::IsolateHolder(
41 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
Ulan Degenbaev51945ab2018-08-23 14:12:5842 AccessMode access_mode,
43 IsolateType isolate_type)
Robert Liao1e7eb052017-12-15 00:30:0544 : IsolateHolder(std::move(task_runner),
45 access_mode,
46 kAllowAtomicsWait,
Ulan Degenbaev51945ab2018-08-23 14:12:5847 isolate_type,
Hitoshi Yoshida9aff02e2018-01-19 16:55:0348 IsolateCreationMode::kNormal) {}
binjifeca6e82017-02-01 23:16:1149
50IsolateHolder::IsolateHolder(
51 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
52 AccessMode access_mode,
Robert Liao1e7eb052017-12-15 00:30:0553 AllowAtomicsWaitMode atomics_wait_mode,
Ulan Degenbaev51945ab2018-08-23 14:12:5854 IsolateType isolate_type,
Hitoshi Yoshida9aff02e2018-01-19 16:55:0355 IsolateCreationMode isolate_creation_mode)
Ulan Degenbaev51945ab2018-08-23 14:12:5856 : access_mode_(access_mode), isolate_type_(isolate_type) {
Hitoshi Yoshida9aff02e2018-01-19 16:55:0357 DCHECK(task_runner);
58 DCHECK(task_runner->BelongsToCurrentThread());
59
oth05c26fde2015-04-05 14:30:5760 v8::ArrayBuffer::Allocator* allocator = g_array_buffer_allocator;
61 CHECK(allocator) << "You need to invoke gin::IsolateHolder::Initialize first";
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5562
Andreas Haas9473d2a02018-05-02 16:21:1963 isolate_ = v8::Isolate::Allocate();
64 isolate_data_.reset(
65 new PerIsolateData(isolate_, allocator, access_mode_, task_runner));
Hitoshi Yoshida9aff02e2018-01-19 16:55:0366 if (isolate_creation_mode == IsolateCreationMode::kCreateSnapshot) {
67 // This branch is called when creating a V8 snapshot for Blink.
68 // Note SnapshotCreator calls isolate->Enter() in its construction.
Andreas Haas9473d2a02018-05-02 16:21:1969 snapshot_creator_.reset(
70 new v8::SnapshotCreator(isolate_, g_reference_table));
71 DCHECK_EQ(isolate_, snapshot_creator_->GetIsolate());
Hitoshi Yoshida9aff02e2018-01-19 16:55:0372 } else {
73 v8::Isolate::CreateParams params;
74 params.entry_hook = DebugImpl::GetFunctionEntryHook();
75 params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
76 params.constraints.ConfigureDefaults(
77 base::SysInfo::AmountOfPhysicalMemory(),
78 base::SysInfo::AmountOfVirtualMemory());
79 params.array_buffer_allocator = allocator;
80 params.allow_atomics_wait =
81 atomics_wait_mode == AllowAtomicsWaitMode::kAllowAtomicsWait;
82 params.external_references = g_reference_table;
Alexey Kozyatinskiy8d0f1f72018-05-01 18:04:1583 params.only_terminate_in_safe_scope = true;
Robert Liao1e7eb052017-12-15 00:30:0584
Andreas Haas9473d2a02018-05-02 16:21:1985 v8::Isolate::Initialize(isolate_, params);
Hitoshi Yoshida440591c2017-09-16 06:34:5686 }
87
Hitoshi Yoshida9aff02e2018-01-19 16:55:0388 isolate_memory_dump_provider_.reset(
89 new V8IsolateMemoryDumpProvider(this, task_runner));
90#if defined(OS_WIN)
91 {
92 void* code_range;
93 size_t size;
94 isolate_->GetCodeRange(&code_range, &size);
95 Debug::CodeRangeCreatedCallback callback =
96 DebugImpl::GetCodeRangeCreatedCallback();
97 if (code_range && size && callback)
98 callback(code_range, size);
99 }
100#endif
periada9d8be2017-05-30 16:08:38101}
102
[email protected]f04b0e92013-11-22 14:20:55103IsolateHolder::~IsolateHolder() {
jochen284435c2014-10-02 13:08:23104#if defined(OS_WIN)
105 {
106 void* code_range;
107 size_t size;
108 isolate_->GetCodeRange(&code_range, &size);
109 Debug::CodeRangeDeletedCallback callback =
110 DebugImpl::GetCodeRangeDeletedCallback();
111 if (code_range && callback)
112 callback(code_range);
113 }
114#endif
ssid83aa5be2015-05-08 12:03:26115 isolate_memory_dump_provider_.reset();
[email protected]f04b0e92013-11-22 14:20:55116 isolate_data_.reset();
jochenb6d92a82014-09-12 09:53:43117 isolate_->Dispose();
Hitoshi Yoshidaf2f50de2017-08-22 13:23:55118 isolate_ = nullptr;
[email protected]f04b0e92013-11-22 14:20:55119}
120
jochen2f43f2c92014-09-10 23:47:31121// static
122void IsolateHolder::Initialize(ScriptMode mode,
yhirano93150242015-12-07 12:28:33123 V8ExtrasMode v8_extras_mode,
Hitoshi Yoshidaa31cce22017-11-21 04:03:30124 v8::ArrayBuffer::Allocator* allocator,
125 const intptr_t* reference_table) {
jochenb6d92a82014-09-12 09:53:43126 CHECK(allocator);
yhirano93150242015-12-07 12:28:33127 V8Initializer::Initialize(mode, v8_extras_mode);
jochen90e7f202014-09-11 15:53:38128 g_array_buffer_allocator = allocator;
Hitoshi Yoshidaa31cce22017-11-21 04:03:30129 g_reference_table = reference_table;
jochen2f43f2c92014-09-10 23:47:31130}
131
ulan3cbdcd02015-07-20 11:32:58132void IsolateHolder::EnableIdleTasks(
mostynbc862da82016-04-03 15:54:33133 std::unique_ptr<V8IdleTaskRunner> idle_task_runner) {
ulan3cbdcd02015-07-20 11:32:58134 DCHECK(isolate_data_.get());
dchenge48600452015-12-28 02:24:50135 isolate_data_->EnableIdleTasks(std::move(idle_task_runner));
ulan3cbdcd02015-07-20 11:32:58136}
137
[email protected]f04b0e92013-11-22 14:20:55138} // namespace gin