| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gin/public/isolate_holder.h" | 5 #include "gin/public/isolate_holder.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 13 #include "gin/array_buffer.h" | 13 #include "gin/array_buffer.h" |
| 14 #include "gin/function_template.h" | 14 #include "gin/function_template.h" |
| 15 #include "gin/per_isolate_data.h" | 15 #include "gin/per_isolate_data.h" |
| 16 #include "gin/public/v8_platform.h" | 16 #include "gin/public/v8_platform.h" |
| 17 | 17 |
| 18 namespace gin { | 18 namespace gin { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 |
| 21 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; | 22 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
| 22 | 23 |
| 23 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 24 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 24 base::RandBytes(buffer, amount); | 25 base::RandBytes(buffer, amount); |
| 25 return true; | 26 return true; |
| 26 } | 27 } |
| 27 | 28 |
| 28 void EnsureV8Initialized(bool gin_managed) { | |
| 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; | |
| 37 } | |
| 38 | |
| 39 } // namespace | 29 } // namespace |
| 40 | 30 |
| 41 IsolateHolder::IsolateHolder() | 31 IsolateHolder::IsolateHolder() { |
| 42 : isolate_owner_(true) { | 32 CHECK(g_array_buffer_allocator) |
| 43 EnsureV8Initialized(true); | 33 << "You need to invoke gin::IsolateHolder::Initialize first"; |
| 44 isolate_ = v8::Isolate::New(); | 34 isolate_ = v8::Isolate::New(); |
| 45 v8::ResourceConstraints constraints; | 35 v8::ResourceConstraints constraints; |
| 46 constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), | 36 constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
| 47 base::SysInfo::AmountOfVirtualMemory(), | 37 base::SysInfo::AmountOfVirtualMemory(), |
| 48 base::SysInfo::NumberOfProcessors()); | 38 base::SysInfo::NumberOfProcessors()); |
| 49 v8::SetResourceConstraints(isolate_, &constraints); | 39 v8::SetResourceConstraints(isolate_, &constraints); |
| 50 Init(g_array_buffer_allocator); | 40 isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator)); |
| 51 } | |
| 52 | |
| 53 IsolateHolder::IsolateHolder(v8::Isolate* isolate, | |
| 54 v8::ArrayBuffer::Allocator* allocator) | |
| 55 : isolate_owner_(false), isolate_(isolate) { | |
| 56 EnsureV8Initialized(false); | |
| 57 Init(allocator); | |
| 58 } | 41 } |
| 59 | 42 |
| 60 IsolateHolder::~IsolateHolder() { | 43 IsolateHolder::~IsolateHolder() { |
| 61 isolate_data_.reset(); | 44 isolate_data_.reset(); |
| 62 if (isolate_owner_) | 45 isolate_->Dispose(); |
| 63 isolate_->Dispose(); | |
| 64 } | 46 } |
| 65 | 47 |
| 66 // static | 48 // static |
| 67 void IsolateHolder::Initialize(ScriptMode mode, | 49 void IsolateHolder::Initialize(ScriptMode mode, |
| 68 v8::ArrayBuffer::Allocator* allocator) { | 50 v8::ArrayBuffer::Allocator* allocator) { |
| 51 CHECK(allocator); |
| 69 static bool v8_is_initialized = false; | 52 static bool v8_is_initialized = false; |
| 70 if (v8_is_initialized) | 53 if (v8_is_initialized) |
| 71 return; | 54 return; |
| 72 v8::V8::InitializePlatform(V8Platform::Get()); | 55 v8::V8::InitializePlatform(V8Platform::Get()); |
| 73 v8::V8::SetArrayBufferAllocator(allocator); | 56 v8::V8::SetArrayBufferAllocator(allocator); |
| 74 g_array_buffer_allocator = allocator; | 57 g_array_buffer_allocator = allocator; |
| 75 if (mode == gin::IsolateHolder::kStrictMode) { | 58 if (mode == gin::IsolateHolder::kStrictMode) { |
| 76 static const char v8_flags[] = "--use_strict"; | 59 static const char v8_flags[] = "--use_strict"; |
| 77 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 60 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| 78 } | 61 } |
| 79 v8::V8::SetEntropySource(&GenerateEntropy); | 62 v8::V8::SetEntropySource(&GenerateEntropy); |
| 80 v8::V8::Initialize(); | 63 v8::V8::Initialize(); |
| 81 v8_is_initialized = true; | 64 v8_is_initialized = true; |
| 82 } | 65 } |
| 83 | 66 |
| 84 void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) { | |
| 85 v8::Isolate::Scope isolate_scope(isolate_); | |
| 86 v8::HandleScope handle_scope(isolate_); | |
| 87 isolate_data_.reset(new PerIsolateData(isolate_, allocator)); | |
| 88 } | |
| 89 | |
| 90 } // namespace gin | 67 } // namespace gin |
| OLD | NEW |