blob: d0ebbb84bf274055c474b7f9d077ccc6a7d5e947 [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
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]314cde12013-11-23 20:26:5114#include "gin/function_template.h"
[email protected]f04b0e92013-11-22 14:20:5515#include "gin/per_isolate_data.h"
[email protected]b64e5212014-04-04 21:09:1616#include "gin/public/v8_platform.h"
[email protected]f04b0e92013-11-22 14:20:5517
18namespace gin {
19
20namespace {
21
22bool GenerateEntropy(unsigned char* buffer, size_t amount) {
23 base::RandBytes(buffer, amount);
24 return true;
25}
26
[email protected]6b395fe2014-04-08 23:58:1527void EnsureV8Initialized(gin::IsolateHolder::ScriptMode mode,
28 bool gin_managed) {
[email protected]f04b0e92013-11-22 14:20:5529 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]89c7c2882013-12-05 10:55:5137 if (!gin_managed)
38 return;
[email protected]f04b0e92013-11-22 14:20:5539
[email protected]b64e5212014-04-04 21:09:1640 v8::V8::InitializePlatform(V8Platform::Get());
[email protected]f04b0e92013-11-22 14:20:5541 v8::V8::SetArrayBufferAllocator(ArrayBufferAllocator::SharedInstance());
[email protected]6b395fe2014-04-08 23:58:1542 if (mode == gin::IsolateHolder::kStrictMode) {
[email protected]9a024902014-05-19 17:15:4943 static const char v8_flags[] = "--use_strict";
[email protected]6b395fe2014-04-08 23:58:1544 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
45 }
[email protected]f04b0e92013-11-22 14:20:5546 v8::V8::SetEntropySource(&GenerateEntropy);
47 v8::V8::Initialize();
48}
49
50} // namespace
51
[email protected]6b395fe2014-04-08 23:58:1552IsolateHolder::IsolateHolder(ScriptMode mode)
[email protected]f04b0e92013-11-22 14:20:5553 : isolate_owner_(true) {
[email protected]6b395fe2014-04-08 23:58:1554 EnsureV8Initialized(mode, true);
[email protected]f04b0e92013-11-22 14:20:5555 isolate_ = v8::Isolate::New();
56 v8::ResourceConstraints constraints;
[email protected]91cd4fe2013-11-28 09:31:5857 constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
[email protected]96daf052014-04-11 14:37:1458 base::SysInfo::AmountOfVirtualMemory(),
[email protected]91cd4fe2013-11-28 09:31:5859 base::SysInfo::NumberOfProcessors());
[email protected]f04b0e92013-11-22 14:20:5560 v8::SetResourceConstraints(isolate_, &constraints);
[email protected]73dcce92014-02-20 08:24:0461 Init(ArrayBufferAllocator::SharedInstance());
[email protected]f04b0e92013-11-22 14:20:5562}
63
[email protected]73dcce92014-02-20 08:24:0464IsolateHolder::IsolateHolder(v8::Isolate* isolate,
65 v8::ArrayBuffer::Allocator* allocator)
66 : isolate_owner_(false), isolate_(isolate) {
[email protected]6b395fe2014-04-08 23:58:1567 EnsureV8Initialized(kNonStrictMode, false);
[email protected]73dcce92014-02-20 08:24:0468 Init(allocator);
[email protected]f04b0e92013-11-22 14:20:5569}
70
71IsolateHolder::~IsolateHolder() {
72 isolate_data_.reset();
73 if (isolate_owner_)
74 isolate_->Dispose();
75}
76
[email protected]73dcce92014-02-20 08:24:0477void IsolateHolder::Init(v8::ArrayBuffer::Allocator* allocator) {
[email protected]ff1554e2013-12-13 15:02:0678 v8::Isolate::Scope isolate_scope(isolate_);
79 v8::HandleScope handle_scope(isolate_);
[email protected]73dcce92014-02-20 08:24:0480 isolate_data_.reset(new PerIsolateData(isolate_, allocator));
[email protected]ff1554e2013-12-13 15:02:0681}
82
[email protected]f04b0e92013-11-22 14:20:5583} // namespace gin