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