Home | History | Annotate | Download | only in src
      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/base/platform/platform.h"
     10 #include "src/bootstrapper.h"
     11 #include "src/crankshaft/lithium-allocator.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/isolate.h"
     17 #include "src/libsampler/v8-sampler.h"
     18 #include "src/objects.h"
     19 #include "src/profiler/heap-profiler.h"
     20 #include "src/runtime-profiler.h"
     21 #include "src/snapshot/natives.h"
     22 #include "src/snapshot/snapshot.h"
     23 
     24 
     25 namespace v8 {
     26 namespace internal {
     27 
     28 V8_DECLARE_ONCE(init_once);
     29 
     30 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
     31 V8_DECLARE_ONCE(init_natives_once);
     32 V8_DECLARE_ONCE(init_snapshot_once);
     33 #endif
     34 
     35 v8::Platform* V8::platform_ = NULL;
     36 
     37 
     38 bool V8::Initialize() {
     39   InitializeOncePerProcess();
     40   return true;
     41 }
     42 
     43 
     44 void V8::TearDown() {
     45   Bootstrapper::TearDownExtensions();
     46   ElementsAccessor::TearDown();
     47   LOperand::TearDownCaches();
     48   RegisteredExtension::UnregisterAll();
     49   Isolate::GlobalTearDown();
     50   sampler::Sampler::TearDown();
     51   FlagList::ResetAllFlags();  // Frees memory held by string arguments.
     52 }
     53 
     54 
     55 void V8::InitializeOncePerProcessImpl() {
     56   FlagList::EnforceFlagImplications();
     57 
     58   if (FLAG_predictable && FLAG_random_seed == 0) {
     59     // Avoid random seeds in predictable mode.
     60     FLAG_random_seed = 12347;
     61   }
     62 
     63   if (FLAG_stress_compaction) {
     64     FLAG_force_marking_deque_overflows = true;
     65     FLAG_gc_global = true;
     66     FLAG_max_semi_space_size = 1;
     67   }
     68 
     69   if (FLAG_turbo && strcmp(FLAG_turbo_filter, "~~") == 0) {
     70     const char* filter_flag = "--turbo-filter=*";
     71     FlagList::SetFlagsFromString(filter_flag, StrLength(filter_flag));
     72   }
     73 
     74   base::OS::Initialize(FLAG_random_seed, FLAG_hard_abort, FLAG_gc_fake_mmap);
     75 
     76   Isolate::InitializeOncePerProcess();
     77 
     78   sampler::Sampler::SetUp();
     79   CpuFeatures::Probe(false);
     80   ElementsAccessor::InitializeOncePerProcess();
     81   LOperand::SetUpCaches();
     82   SetUpJSCallerSavedCodeData();
     83   ExternalReference::SetUp();
     84   Bootstrapper::InitializeOncePerProcess();
     85 }
     86 
     87 
     88 void V8::InitializeOncePerProcess() {
     89   base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
     90 }
     91 
     92 
     93 void V8::InitializePlatform(v8::Platform* platform) {
     94   CHECK(!platform_);
     95   CHECK(platform);
     96   platform_ = platform;
     97 }
     98 
     99 
    100 void V8::ShutdownPlatform() {
    101   CHECK(platform_);
    102   platform_ = NULL;
    103 }
    104 
    105 
    106 v8::Platform* V8::GetCurrentPlatform() {
    107   DCHECK(platform_);
    108   return platform_;
    109 }
    110 
    111 
    112 void V8::SetPlatformForTesting(v8::Platform* platform) { platform_ = platform; }
    113 
    114 
    115 void V8::SetNativesBlob(StartupData* natives_blob) {
    116 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
    117   base::CallOnce(&init_natives_once, &SetNativesFromFile, natives_blob);
    118 #else
    119   CHECK(false);
    120 #endif
    121 }
    122 
    123 
    124 void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
    125 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
    126   base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
    127 #else
    128   CHECK(false);
    129 #endif
    130 }
    131 }  // namespace internal
    132 }  // namespace v8
    133