v8.cc 4.63 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/init/v8.h"
6

7 8
#include <fstream>

9
#include "src/api/api.h"
10
#include "src/base/atomicops.h"
11
#include "src/base/once.h"
12
#include "src/base/platform/platform.h"
13 14
#include "src/codegen/cpu-features.h"
#include "src/codegen/interface-descriptors.h"
15
#include "src/debug/debug.h"
16
#include "src/deoptimizer/deoptimizer.h"
17 18 19 20
#include "src/execution/frames.h"
#include "src/execution/isolate.h"
#include "src/execution/runtime-profiler.h"
#include "src/execution/simulator.h"
21
#include "src/init/bootstrapper.h"
22
#include "src/libsampler/sampler.h"
23
#include "src/objects/elements.h"
24
#include "src/objects/objects-inl.h"
25
#include "src/profiler/heap-profiler.h"
26 27
#include "src/snapshot/natives.h"
#include "src/snapshot/snapshot.h"
28
#include "src/tracing/tracing-category-observer.h"
29
#include "src/wasm/wasm-engine.h"
30

31 32
namespace v8 {
namespace internal {
33

34
V8_DECLARE_ONCE(init_once);
35

36 37 38 39 40
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
V8_DECLARE_ONCE(init_natives_once);
V8_DECLARE_ONCE(init_snapshot_once);
#endif

41
v8::Platform* V8::platform_ = nullptr;
42

43
bool V8::Initialize() {
44
  InitializeOncePerProcess();
45
  return true;
46 47
}

48
void V8::TearDown() {
49
  wasm::WasmEngine::GlobalTearDown();
50 51 52
#if defined(USE_SIMULATOR)
  Simulator::GlobalTearDown();
#endif
53
  CallDescriptors::TearDown();
54
  ElementsAccessor::TearDown();
55
  RegisteredExtension::UnregisterAll();
56
  FlagList::ResetAllFlags();  // Frees memory held by string arguments.
57 58
}

59
void V8::InitializeOncePerProcessImpl() {
60
  FlagList::EnforceFlagImplications();
61

62 63 64
  if (FLAG_predictable && FLAG_random_seed == 0) {
    // Avoid random seeds in predictable mode.
    FLAG_random_seed = 12347;
65 66
  }

67 68 69
  if (FLAG_stress_compaction) {
    FLAG_force_marking_deque_overflows = true;
    FLAG_gc_global = true;
70
    FLAG_max_semi_space_size = 1;
71
  }
72

73 74 75 76 77 78
  if (FLAG_trace_turbo) {
    // Create an empty file shared by the process (e.g. the wasm engine).
    std::ofstream(Isolate::GetTurboCfgFileName(nullptr).c_str(),
                  std::ios_base::trunc);
  }

79 80 81 82 83 84 85 86 87 88
  // Do not expose wasm in jitless mode.
  //
  // Even in interpreter-only mode, wasm currently still creates executable
  // memory at runtime. Unexpose wasm until this changes.
  // The correctness fuzzers are a special case: many of their test cases are
  // built by fetching a random property from the the global object, and thus
  // the global object layout must not change between configs. That is why we
  // continue exposing wasm on correctness fuzzers even in jitless mode.
  // TODO(jgruber): Remove this once / if wasm can run without executable
  // memory.
89
  if (FLAG_jitless && !FLAG_correctness_fuzzer_suppressions) {
90 91 92
    FLAG_expose_wasm = false;
  }

93 94 95
  base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap);

  if (FLAG_random_seed) SetRandomMmapSeed(FLAG_random_seed);
96

97 98
  Isolate::InitializeOncePerProcess();

99 100 101
#if defined(USE_SIMULATOR)
  Simulator::InitializeOncePerProcess();
#endif
102
  CpuFeatures::Probe(false);
103
  ElementsAccessor::InitializeOncePerProcess();
104
  Bootstrapper::InitializeOncePerProcess();
105
  CallDescriptors::InitializeOncePerProcess();
106
  wasm::WasmEngine::InitializeOncePerProcess();
107 108 109
}

void V8::InitializeOncePerProcess() {
110
  base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
111 112
}

113
void V8::InitializePlatform(v8::Platform* platform) {
114 115
  CHECK(!platform_);
  CHECK(platform);
116
  platform_ = platform;
117
  v8::base::SetPrintStackTrace(platform_->GetStackTracePrinter());
118
  v8::tracing::TracingCategoryObserver::SetUp();
119 120 121
}

void V8::ShutdownPlatform() {
122
  CHECK(platform_);
123
  v8::tracing::TracingCategoryObserver::TearDown();
124
  v8::base::SetPrintStackTrace(nullptr);
125
  platform_ = nullptr;
126 127 128
}

v8::Platform* V8::GetCurrentPlatform() {
129 130 131 132
  v8::Platform* platform = reinterpret_cast<v8::Platform*>(
      base::Relaxed_Load(reinterpret_cast<base::AtomicWord*>(&platform_)));
  DCHECK(platform);
  return platform;
133 134
}

135 136 137 138
void V8::SetPlatformForTesting(v8::Platform* platform) {
  base::Relaxed_Store(reinterpret_cast<base::AtomicWord*>(&platform_),
                      reinterpret_cast<base::AtomicWord>(platform));
}
139

140 141 142 143
void V8::SetNativesBlob(StartupData* natives_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  base::CallOnce(&init_natives_once, &SetNativesFromFile, natives_blob);
#else
144
  UNREACHABLE();
145 146 147 148 149 150 151
#endif
}

void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
#else
152
  UNREACHABLE();
153 154
#endif
}
155
}  // namespace internal
156 157 158 159 160

// static
double Platform::SystemClockTimeMillis() {
  return base::OS::TimeCurrentMillis();
}
161
}  // namespace v8