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/api.h" 8 #include "src/base/atomicops.h" 9 #include "src/base/once.h" 10 #include "src/base/platform/platform.h" 11 #include "src/bootstrapper.h" 12 #include "src/debug/debug.h" 13 #include "src/deoptimizer.h" 14 #include "src/elements.h" 15 #include "src/frames.h" 16 #include "src/interface-descriptors.h" 17 #include "src/isolate.h" 18 #include "src/libsampler/sampler.h" 19 #include "src/objects-inl.h" 20 #include "src/profiler/heap-profiler.h" 21 #include "src/reloc-info.h" 22 #include "src/runtime-profiler.h" 23 #include "src/simulator.h" 24 #include "src/snapshot/natives.h" 25 #include "src/snapshot/snapshot.h" 26 #include "src/tracing/tracing-category-observer.h" 27 #include "src/wasm/wasm-engine.h" 28 29 namespace v8 { 30 namespace internal { 31 32 V8_DECLARE_ONCE(init_once); 33 34 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 35 V8_DECLARE_ONCE(init_natives_once); 36 V8_DECLARE_ONCE(init_snapshot_once); 37 #endif 38 39 v8::Platform* V8::platform_ = nullptr; 40 Initialize()41bool V8::Initialize() { 42 InitializeOncePerProcess(); 43 return true; 44 } 45 46 TearDown()47void V8::TearDown() { 48 #if defined(USE_SIMULATOR) 49 Simulator::GlobalTearDown(); 50 #endif 51 wasm::WasmEngine::GlobalTearDown(); 52 CallDescriptors::TearDown(); 53 Bootstrapper::TearDownExtensions(); 54 ElementsAccessor::TearDown(); 55 RegisteredExtension::UnregisterAll(); 56 sampler::Sampler::TearDown(); 57 FlagList::ResetAllFlags(); // Frees memory held by string arguments. 58 } 59 60 InitializeOncePerProcessImpl()61void V8::InitializeOncePerProcessImpl() { 62 FlagList::EnforceFlagImplications(); 63 64 if (FLAG_predictable && FLAG_random_seed == 0) { 65 // Avoid random seeds in predictable mode. 66 FLAG_random_seed = 12347; 67 } 68 69 if (FLAG_stress_compaction) { 70 FLAG_force_marking_deque_overflows = true; 71 FLAG_gc_global = true; 72 FLAG_max_semi_space_size = 1; 73 } 74 75 base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap); 76 77 if (FLAG_random_seed) SetRandomMmapSeed(FLAG_random_seed); 78 79 Isolate::InitializeOncePerProcess(); 80 81 #if defined(USE_SIMULATOR) 82 Simulator::InitializeOncePerProcess(); 83 #endif 84 sampler::Sampler::SetUp(); 85 CpuFeatures::Probe(false); 86 ElementsAccessor::InitializeOncePerProcess(); 87 Bootstrapper::InitializeOncePerProcess(); 88 CallDescriptors::InitializeOncePerProcess(); 89 wasm::WasmEngine::InitializeOncePerProcess(); 90 } 91 92 InitializeOncePerProcess()93void V8::InitializeOncePerProcess() { 94 base::CallOnce(&init_once, &InitializeOncePerProcessImpl); 95 } 96 97 InitializePlatform(v8::Platform * platform)98void V8::InitializePlatform(v8::Platform* platform) { 99 CHECK(!platform_); 100 CHECK(platform); 101 platform_ = platform; 102 v8::base::SetPrintStackTrace(platform_->GetStackTracePrinter()); 103 v8::tracing::TracingCategoryObserver::SetUp(); 104 } 105 106 ShutdownPlatform()107void V8::ShutdownPlatform() { 108 CHECK(platform_); 109 v8::tracing::TracingCategoryObserver::TearDown(); 110 v8::base::SetPrintStackTrace(nullptr); 111 platform_ = nullptr; 112 } 113 114 GetCurrentPlatform()115v8::Platform* V8::GetCurrentPlatform() { 116 v8::Platform* platform = reinterpret_cast<v8::Platform*>( 117 base::Relaxed_Load(reinterpret_cast<base::AtomicWord*>(&platform_))); 118 DCHECK(platform); 119 return platform; 120 } 121 SetPlatformForTesting(v8::Platform * platform)122void V8::SetPlatformForTesting(v8::Platform* platform) { 123 base::Relaxed_Store(reinterpret_cast<base::AtomicWord*>(&platform_), 124 reinterpret_cast<base::AtomicWord>(platform)); 125 } 126 SetNativesBlob(StartupData * natives_blob)127void V8::SetNativesBlob(StartupData* natives_blob) { 128 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 129 base::CallOnce(&init_natives_once, &SetNativesFromFile, natives_blob); 130 #else 131 UNREACHABLE(); 132 #endif 133 } 134 135 SetSnapshotBlob(StartupData * snapshot_blob)136void V8::SetSnapshotBlob(StartupData* snapshot_blob) { 137 #ifdef V8_USE_EXTERNAL_STARTUP_DATA 138 base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob); 139 #else 140 UNREACHABLE(); 141 #endif 142 } 143 } // namespace internal 144 145 // static SystemClockTimeMillis()146double Platform::SystemClockTimeMillis() { 147 return base::OS::TimeCurrentMillis(); 148 } 149 } // namespace v8 150