v8.cc 3.61 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 6
#include "src/v8.h"

7
#include "src/api.h"
8
#include "src/assembler.h"
9
#include "src/base/atomicops.h"
10
#include "src/base/once.h"
11
#include "src/base/platform/platform.h"
12
#include "src/bootstrapper.h"
13
#include "src/debug/debug.h"
14
#include "src/deoptimizer.h"
15
#include "src/elements.h"
16
#include "src/frames.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/runtime-profiler.h"
22
#include "src/simulator.h"
23 24
#include "src/snapshot/natives.h"
#include "src/snapshot/snapshot.h"
25
#include "src/tracing/tracing-category-observer.h"
26

27 28
namespace v8 {
namespace internal {
29

30
V8_DECLARE_ONCE(init_once);
31

32 33 34 35 36
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
V8_DECLARE_ONCE(init_natives_once);
V8_DECLARE_ONCE(init_snapshot_once);
#endif

37
v8::Platform* V8::platform_ = nullptr;
38

39
bool V8::Initialize() {
40
  InitializeOncePerProcess();
41
  return true;
42 43 44
}


45
void V8::TearDown() {
46 47 48
#if defined(USE_SIMULATOR)
  Simulator::GlobalTearDown();
#endif
49
  Bootstrapper::TearDownExtensions();
50
  ElementsAccessor::TearDown();
51
  RegisteredExtension::UnregisterAll();
52
  Isolate::GlobalTearDown();
53
  sampler::Sampler::TearDown();
54
  FlagList::ResetAllFlags();  // Frees memory held by string arguments.
55 56
}

57

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

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

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

72 73 74
  base::OS::Initialize(FLAG_hard_abort, FLAG_gc_fake_mmap);

  if (FLAG_random_seed) SetRandomMmapSeed(FLAG_random_seed);
75

76 77
  Isolate::InitializeOncePerProcess();

78 79 80
#if defined(USE_SIMULATOR)
  Simulator::InitializeOncePerProcess();
#endif
81
  sampler::Sampler::SetUp();
82
  CpuFeatures::Probe(false);
83
  ElementsAccessor::InitializeOncePerProcess();
84 85
  SetUpJSCallerSavedCodeData();
  ExternalReference::SetUp();
86
  Bootstrapper::InitializeOncePerProcess();
87 88
}

89

90
void V8::InitializeOncePerProcess() {
91
  base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
92 93
}

94 95

void V8::InitializePlatform(v8::Platform* platform) {
96 97
  CHECK(!platform_);
  CHECK(platform);
98
  platform_ = platform;
99
  v8::base::SetPrintStackTrace(platform_->GetStackTracePrinter());
100
  v8::tracing::TracingCategoryObserver::SetUp();
101 102 103 104
}


void V8::ShutdownPlatform() {
105
  CHECK(platform_);
106
  v8::tracing::TracingCategoryObserver::TearDown();
107
  v8::base::SetPrintStackTrace(nullptr);
108
  platform_ = nullptr;
109 110 111 112
}


v8::Platform* V8::GetCurrentPlatform() {
113 114 115 116
  v8::Platform* platform = reinterpret_cast<v8::Platform*>(
      base::Relaxed_Load(reinterpret_cast<base::AtomicWord*>(&platform_)));
  DCHECK(platform);
  return platform;
117 118
}

119 120 121 122
void V8::SetPlatformForTesting(v8::Platform* platform) {
  base::Relaxed_Store(reinterpret_cast<base::AtomicWord*>(&platform_),
                      reinterpret_cast<base::AtomicWord>(platform));
}
123

124 125 126 127
void V8::SetNativesBlob(StartupData* natives_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  base::CallOnce(&init_natives_once, &SetNativesFromFile, natives_blob);
#else
128
  UNREACHABLE();
129 130 131 132 133 134 135 136
#endif
}


void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
  base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
#else
137
  UNREACHABLE();
138 139
#endif
}
140
}  // namespace internal
141 142 143 144 145

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