[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "gin/public/isolate_holder.h" |
| 6 | |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include "base/logging.h" |
| 11 | #include "base/rand_util.h" |
| 12 | #include "base/sys_info.h" |
| 13 | #include "gin/array_buffer.h" |
[email protected] | 314cde1 | 2013-11-23 20:26:51 | [diff] [blame] | 14 | #include "gin/function_template.h" |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 15 | #include "gin/per_isolate_data.h" |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 16 | #include "gin/public/v8_platform.h" |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 17 | |
| 18 | namespace gin { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 23 | base::RandBytes(buffer, amount); |
| 24 | return true; |
| 25 | } |
| 26 | |
[email protected] | 6b395fe | 2014-04-08 23:58:15 | [diff] [blame] | 27 | void EnsureV8Initialized(gin::IsolateHolder::ScriptMode mode, |
| 28 | bool gin_managed) { |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 29 | static bool v8_is_initialized = false; |
| 30 | static bool v8_is_gin_managed = false; |
| 31 | if (v8_is_initialized) { |
| 32 | CHECK_EQ(v8_is_gin_managed, gin_managed); |
| 33 | return; |
| 34 | } |
| 35 | v8_is_initialized = true; |
| 36 | v8_is_gin_managed = gin_managed; |
[email protected] | 89c7c288 | 2013-12-05 10:55:51 | [diff] [blame] | 37 | if (!gin_managed) |
| 38 | return; |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 39 | |
[email protected] | b64e521 | 2014-04-04 21:09:16 | [diff] [blame] | 40 | v8::V8::InitializePlatform(V8Platform::Get()); |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 41 | v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance()); |
[email protected] | 6b395fe | 2014-04-08 23:58:15 | [diff] [blame] | 42 | if (mode == gin::IsolateHolder::kStrictMode) { |
[email protected] | 9a02490 | 2014-05-19 17:15:49 | [diff] [blame] | 43 | static const char v8_flags[] = "--use_strict"; |
[email protected] | 6b395fe | 2014-04-08 23:58:15 | [diff] [blame] | 44 | v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| 45 | } |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 46 | v8::V8::SetEntropySource(&GenerateEntropy); |
| 47 | v8::V8::Initialize(); |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
[email protected] | 6b395fe | 2014-04-08 23:58:15 | [diff] [blame] | 52 | IsolateHolder::IsolateHolder(ScriptMode mode) |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 53 | : isolate_owner_(true) { |
[email protected] | 6b395fe | 2014-04-08 23:58:15 | [diff] [blame] | 54 | EnsureV8Initialized(mode, true); |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 55 | isolate_ = v8::Isolate::New(); |
| 56 | v8::ResourceConstraints constraints; |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 57 | constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
[email protected] | 96daf05 | 2014-04-11 14:37:14 | [diff] [blame] | 58 | base::SysInfo::AmountOfVirtualMemory(), |
[email protected] | 91cd4fe | 2013-11-28 09:31:58 | [diff] [blame] | 59 | base::SysInfo::NumberOfProcessors()); |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 60 | v8::SetResourceConstraints(isolate_, &constraints); |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 61 | Init(ArrayBufferAllocator::SharedInstance()); |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 64 | IsolateHolder::IsolateHolder(v8::Isolate* isolate, |
| 65 | v8::ArrayBuffer::Allocator* allocator) |
| 66 | : isolate_owner_(false), isolate_(isolate) { |
[email protected] | 6b395fe | 2014-04-08 23:58:15 | [diff] [blame] | 67 | EnsureV8Initialized(kNonStrictMode, false); |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 68 | Init(allocator); |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | IsolateHolder::~IsolateHolder() { |
| 72 | isolate_data_.reset(); |
| 73 | if (isolate_owner_) |
| 74 | isolate_->Dispose(); |
| 75 | } |
| 76 | |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 77 | void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) { |
[email protected] | ff1554e | 2013-12-13 15:02:06 | [diff] [blame] | 78 | v8::Isolate::Scope isolate_scope(isolate_); |
| 79 | v8::HandleScope handle_scope(isolate_); |
[email protected] | 73dcce9 | 2014-02-20 08:24:04 | [diff] [blame] | 80 | isolate_data_.reset(new PerIsolateData(isolate_, allocator)); |
[email protected] | ff1554e | 2013-12-13 15:02:06 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | f04b0e9 | 2013-11-22 14:20:55 | [diff] [blame] | 83 | } // namespace gin |