blob: d660b58c1096d9e08b372d609076b48ca1ed316b [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/assembler.h"
8#include "src/base/once.h"
9#include "src/base/platform/platform.h"
10#include "src/bootstrapper.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/crankshaft/lithium-allocator.h"
12#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "src/deoptimizer.h"
14#include "src/elements.h"
15#include "src/frames.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016#include "src/isolate.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010017#include "src/libsampler/v8-sampler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018#include "src/objects.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019#include "src/profiler/heap-profiler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020#include "src/runtime-profiler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021#include "src/snapshot/natives.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022#include "src/snapshot/snapshot.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023
Steve Blocka7e24c12009-10-30 11:49:00 +000024
25namespace v8 {
26namespace internal {
27
Ben Murdoch3ef787d2012-04-12 10:51:47 +010028V8_DECLARE_ONCE(init_once);
Ben Murdoch8b112d22011-06-08 16:22:53 +010029
Emily Bernierd0a1eb72015-03-24 16:35:39 -040030#ifdef V8_USE_EXTERNAL_STARTUP_DATA
31V8_DECLARE_ONCE(init_natives_once);
32V8_DECLARE_ONCE(init_snapshot_once);
33#endif
34
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035v8::Platform* V8::platform_ = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000036
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080037
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038bool V8::Initialize() {
Ben Murdoch8b112d22011-06-08 16:22:53 +010039 InitializeOncePerProcess();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000040 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000041}
42
43
44void V8::TearDown() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 Bootstrapper::TearDownExtensions();
46 ElementsAccessor::TearDown();
47 LOperand::TearDownCaches();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 RegisteredExtension::UnregisterAll();
49 Isolate::GlobalTearDown();
Ben Murdoch61f157c2016-09-16 13:49:30 +010050 sampler::Sampler::TearDown();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051 FlagList::ResetAllFlags(); // Frees memory held by string arguments.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080052}
53
54
Ben Murdoch3ef787d2012-04-12 10:51:47 +010055void V8::InitializeOncePerProcessImpl() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 FlagList::EnforceFlagImplications();
Ben Murdoch8b112d22011-06-08 16:22:53 +010057
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 if (FLAG_predictable && FLAG_random_seed == 0) {
59 // Avoid random seeds in predictable mode.
60 FLAG_random_seed = 12347;
Ben Murdoch8b112d22011-06-08 16:22:53 +010061 }
62
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063 if (FLAG_stress_compaction) {
64 FLAG_force_marking_deque_overflows = true;
65 FLAG_gc_global = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066 FLAG_max_semi_space_size = 1;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010067 }
68
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 base::OS::Initialize(FLAG_random_seed, FLAG_hard_abort, FLAG_gc_fake_mmap);
75
76 Isolate::InitializeOncePerProcess();
77
Ben Murdoch61f157c2016-09-16 13:49:30 +010078 sampler::Sampler::SetUp();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 CpuFeatures::Probe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080 ElementsAccessor::InitializeOncePerProcess();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010081 LOperand::SetUpCaches();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 SetUpJSCallerSavedCodeData();
83 ExternalReference::SetUp();
84 Bootstrapper::InitializeOncePerProcess();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010085}
86
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087
Ben Murdoch3ef787d2012-04-12 10:51:47 +010088void V8::InitializeOncePerProcess() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
90}
91
92
93void V8::InitializePlatform(v8::Platform* platform) {
94 CHECK(!platform_);
95 CHECK(platform);
96 platform_ = platform;
97}
98
99
100void V8::ShutdownPlatform() {
101 CHECK(platform_);
102 platform_ = NULL;
103}
104
105
106v8::Platform* V8::GetCurrentPlatform() {
107 DCHECK(platform_);
108 return platform_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100109}
110
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400111
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112void V8::SetPlatformForTesting(v8::Platform* platform) { platform_ = platform; }
113
114
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400115void 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
124void 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}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000131} // namespace internal
132} // namespace v8