1// Copyright 2012 the V8 project 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 "src/v8.h" 6 7#include "src/assembler.h" 8#include "src/base/once.h" 9#include "src/isolate.h" 10#include "src/elements.h" 11#include "src/bootstrapper.h" 12#include "src/debug.h" 13#include "src/deoptimizer.h" 14#include "src/frames.h" 15#include "src/heap-profiler.h" 16#include "src/hydrogen.h" 17#ifdef V8_USE_DEFAULT_PLATFORM 18#include "src/libplatform/default-platform.h" 19#endif 20#include "src/lithium-allocator.h" 21#include "src/objects.h" 22#include "src/platform.h" 23#include "src/sampler.h" 24#include "src/runtime-profiler.h" 25#include "src/serialize.h" 26#include "src/store-buffer.h" 27 28namespace v8 { 29namespace internal { 30 31V8_DECLARE_ONCE(init_once); 32 33v8::ArrayBuffer::Allocator* V8::array_buffer_allocator_ = NULL; 34v8::Platform* V8::platform_ = NULL; 35 36 37bool V8::Initialize(Deserializer* des) { 38 InitializeOncePerProcess(); 39 Isolate* isolate = Isolate::UncheckedCurrent(); 40 if (isolate == NULL) return true; 41 if (isolate->IsDead()) return false; 42 if (isolate->IsInitialized()) return true; 43 44#ifdef V8_USE_DEFAULT_PLATFORM 45 DefaultPlatform* platform = static_cast<DefaultPlatform*>(platform_); 46 platform->SetThreadPoolSize(isolate->max_available_threads()); 47 // We currently only start the threads early, if we know that we'll use them. 48 if (FLAG_job_based_sweeping) platform->EnsureInitialized(); 49#endif 50 51 return isolate->Init(des); 52} 53 54 55void V8::TearDown() { 56 Bootstrapper::TearDownExtensions(); 57 ElementsAccessor::TearDown(); 58 LOperand::TearDownCaches(); 59 ExternalReference::TearDownMathExpData(); 60 RegisteredExtension::UnregisterAll(); 61 Isolate::GlobalTearDown(); 62 63 Sampler::TearDown(); 64 65#ifdef V8_USE_DEFAULT_PLATFORM 66 DefaultPlatform* platform = static_cast<DefaultPlatform*>(platform_); 67 platform_ = NULL; 68 delete platform; 69#endif 70} 71 72 73void V8::SetReturnAddressLocationResolver( 74 ReturnAddressLocationResolver resolver) { 75 StackFrame::SetReturnAddressLocationResolver(resolver); 76} 77 78 79void V8::InitializeOncePerProcessImpl() { 80 FlagList::EnforceFlagImplications(); 81 82 if (FLAG_predictable && FLAG_random_seed == 0) { 83 // Avoid random seeds in predictable mode. 84 FLAG_random_seed = 12347; 85 } 86 87 if (FLAG_stress_compaction) { 88 FLAG_force_marking_deque_overflows = true; 89 FLAG_gc_global = true; 90 FLAG_max_semi_space_size = 1; 91 } 92 93#ifdef V8_USE_DEFAULT_PLATFORM 94 platform_ = new DefaultPlatform; 95#endif 96 Sampler::SetUp(); 97 CpuFeatures::Probe(false); 98 init_memcopy_functions(); 99 // The custom exp implementation needs 16KB of lookup data; initialize it 100 // on demand. 101 init_fast_sqrt_function(); 102#ifdef _WIN64 103 init_modulo_function(); 104#endif 105 ElementsAccessor::InitializeOncePerProcess(); 106 LOperand::SetUpCaches(); 107 SetUpJSCallerSavedCodeData(); 108 ExternalReference::SetUp(); 109 Bootstrapper::InitializeOncePerProcess(); 110} 111 112 113void V8::InitializeOncePerProcess() { 114 base::CallOnce(&init_once, &InitializeOncePerProcessImpl); 115} 116 117 118void V8::InitializePlatform(v8::Platform* platform) { 119 ASSERT(!platform_); 120 ASSERT(platform); 121 platform_ = platform; 122} 123 124 125void V8::ShutdownPlatform() { 126 ASSERT(platform_); 127 platform_ = NULL; 128} 129 130 131v8::Platform* V8::GetCurrentPlatform() { 132 ASSERT(platform_); 133 return platform_; 134} 135 136} } // namespace v8::internal 137