| 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 |
| 22 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 22 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 23 base::RandBytes(buffer, amount); | 23 base::RandBytes(buffer, amount); |
| 24 return true; | 24 return true; |
| 25 } | 25 } |
| 26 | 26 |
| 27 | 27 void EnsureV8Initialized(gin::IsolateHolder::ScriptMode mode, |
| 28 void EnsureV8Initialized(bool gin_managed) { | 28 bool gin_managed) { |
| 29 static bool v8_is_initialized = false; | 29 static bool v8_is_initialized = false; |
| 30 static bool v8_is_gin_managed = false; | 30 static bool v8_is_gin_managed = false; |
| 31 if (v8_is_initialized) { | 31 if (v8_is_initialized) { |
| 32 CHECK_EQ(v8_is_gin_managed, gin_managed); | 32 CHECK_EQ(v8_is_gin_managed, gin_managed); |
| 33 return; | 33 return; |
| 34 } | 34 } |
| 35 v8_is_initialized = true; | 35 v8_is_initialized = true; |
| 36 v8_is_gin_managed = gin_managed; | 36 v8_is_gin_managed = gin_managed; |
| 37 if (!gin_managed) | 37 if (!gin_managed) |
| 38 return; | 38 return; |
| 39 | 39 |
| 40 v8::V8::InitializePlatform(V8Platform::Get()); | 40 v8::V8::InitializePlatform(V8Platform::Get()); |
| 41 v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance()); | 41 v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance()); |
| 42 static const char v8_flags[] = "--use_strict --harmony"; | 42 if (mode == gin::IsolateHolder::kStrictMode) { |
| 43 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 43 // TODO(jochen): drop --harmony. it's really too unstable and broad to use. |
| 44 static const char v8_flags[] = "--use_strict --harmony"; |
| 45 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| 46 } |
| 44 v8::V8::SetEntropySource(&GenerateEntropy); | 47 v8::V8::SetEntropySource(&GenerateEntropy); |
| 45 v8::V8::Initialize(); | 48 v8::V8::Initialize(); |
| 46 } | 49 } |
| 47 | 50 |
| 48 } // namespace | 51 } // namespace |
| 49 | 52 |
| 50 IsolateHolder::IsolateHolder() | 53 IsolateHolder::IsolateHolder(ScriptMode mode) |
| 51 : isolate_owner_(true) { | 54 : isolate_owner_(true) { |
| 52 EnsureV8Initialized(true); | 55 EnsureV8Initialized(mode, true); |
| 53 isolate_ = v8::Isolate::New(); | 56 isolate_ = v8::Isolate::New(); |
| 54 v8::ResourceConstraints constraints; | 57 v8::ResourceConstraints constraints; |
| 55 constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), | 58 constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
| 56 base::SysInfo::NumberOfProcessors()); | 59 base::SysInfo::NumberOfProcessors()); |
| 57 v8::SetResourceConstraints(isolate_, &constraints); | 60 v8::SetResourceConstraints(isolate_, &constraints); |
| 58 Init(ArrayBufferAllocator::SharedInstance()); | 61 Init(ArrayBufferAllocator::SharedInstance()); |
| 59 } | 62 } |
| 60 | 63 |
| 61 IsolateHolder::IsolateHolder(v8::Isolate* isolate, | 64 IsolateHolder::IsolateHolder(v8::Isolate* isolate, |
| 62 v8::ArrayBuffer::Allocator* allocator) | 65 v8::ArrayBuffer::Allocator* allocator) |
| 63 : isolate_owner_(false), isolate_(isolate) { | 66 : isolate_owner_(false), isolate_(isolate) { |
| 64 EnsureV8Initialized(false); | 67 EnsureV8Initialized(kNonStrictMode, false); |
| 65 Init(allocator); | 68 Init(allocator); |
| 66 } | 69 } |
| 67 | 70 |
| 68 IsolateHolder::~IsolateHolder() { | 71 IsolateHolder::~IsolateHolder() { |
| 69 isolate_data_.reset(); | 72 isolate_data_.reset(); |
| 70 if (isolate_owner_) | 73 if (isolate_owner_) |
| 71 isolate_->Dispose(); | 74 isolate_->Dispose(); |
| 72 } | 75 } |
| 73 | 76 |
| 74 void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) { | 77 void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) { |
| 75 v8::Isolate::Scope isolate_scope(isolate_); | 78 v8::Isolate::Scope isolate_scope(isolate_); |
| 76 v8::HandleScope handle_scope(isolate_); | 79 v8::HandleScope handle_scope(isolate_); |
| 77 isolate_data_.reset(new PerIsolateData(isolate_, allocator)); | 80 isolate_data_.reset(new PerIsolateData(isolate_, allocator)); |
| 78 } | 81 } |
| 79 | 82 |
| 80 } // namespace gin | 83 } // namespace gin |
| OLD | NEW |