blob: 5f8aea7105a188846fd48d199bb4e0ca2017a0ab [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"
Sebastien Marchand75a7cdf2018-11-13 23:47:0317#include "base/system/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;
Hitoshi Yoshida9aff02e2018-01-19 16:55:0374 params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
75 params.constraints.ConfigureDefaults(
76 base::SysInfo::AmountOfPhysicalMemory(),
77 base::SysInfo::AmountOfVirtualMemory());
78 params.array_buffer_allocator = allocator;
79 params.allow_atomics_wait =
80 atomics_wait_mode == AllowAtomicsWaitMode::kAllowAtomicsWait;
81 params.external_references = g_reference_table;
Alexey Kozyatinskiy8d0f1f72018-05-01 18:04:1582 params.only_terminate_in_safe_scope = true;
Robert Liao1e7eb052017-12-15 00:30:0583
Andreas Haas9473d2a02018-05-02 16:21:1984 v8::Isolate::Initialize(isolate_, params);
Hitoshi Yoshida440591c2017-09-16 06:34:5685 }
86
Hitoshi Yoshida9aff02e2018-01-19 16:55:0387 isolate_memory_dump_provider_.reset(
88 new V8IsolateMemoryDumpProvider(this, task_runner));
periada9d8be2017-05-30 16:08:3889}
90
[email protected]f04b0e92013-11-22 14:20:5591IsolateHolder::~IsolateHolder() {
ssid83aa5be2015-05-08 12:03:2692 isolate_memory_dump_provider_.reset();
[email protected]f04b0e92013-11-22 14:20:5593 isolate_data_.reset();
jochenb6d92a82014-09-12 09:53:4394 isolate_->Dispose();
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5595 isolate_ = nullptr;
[email protected]f04b0e92013-11-22 14:20:5596}
97
jochen2f43f2c92014-09-10 23:47:3198// static
99void IsolateHolder::Initialize(ScriptMode mode,
Hitoshi Yoshidaa31cce22017-11-21 04:03:30100 v8::ArrayBuffer::Allocator* allocator,
101 const intptr_t* reference_table) {
jochenb6d92a82014-09-12 09:53:43102 CHECK(allocator);
peterwmwongd9cbb392019-01-10 16:58:28103 V8Initializer::Initialize(mode);
jochen90e7f202014-09-11 15:53:38104 g_array_buffer_allocator = allocator;
Hitoshi Yoshidaa31cce22017-11-21 04:03:30105 g_reference_table = reference_table;
jochen2f43f2c92014-09-10 23:47:31106}
107
ulan3cbdcd02015-07-20 11:32:58108void IsolateHolder::EnableIdleTasks(
mostynbc862da82016-04-03 15:54:33109 std::unique_ptr<V8IdleTaskRunner> idle_task_runner) {
ulan3cbdcd02015-07-20 11:32:58110 DCHECK(isolate_data_.get());
dchenge48600452015-12-28 02:24:50111 isolate_data_->EnableIdleTasks(std::move(idle_task_runner));
ulan3cbdcd02015-07-20 11:32:58112}
113
[email protected]f04b0e92013-11-22 14:20:55114} // namespace gin