blob: 31b48780e4f0d059f0ec218015e156600a6c71e4 [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 Murdochb8a8cc12014-11-26 15:28:44 +000017#include "src/objects.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018#include "src/profiler/heap-profiler.h"
19#include "src/profiler/sampler.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"
22#include "src/snapshot/serialize.h"
23#include "src/snapshot/snapshot.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024
Steve Blocka7e24c12009-10-30 11:49:00 +000025
26namespace v8 {
27namespace internal {
28
Ben Murdoch3ef787d2012-04-12 10:51:47 +010029V8_DECLARE_ONCE(init_once);
Ben Murdoch8b112d22011-06-08 16:22:53 +010030
Emily Bernierd0a1eb72015-03-24 16:35:39 -040031#ifdef V8_USE_EXTERNAL_STARTUP_DATA
32V8_DECLARE_ONCE(init_natives_once);
33V8_DECLARE_ONCE(init_snapshot_once);
34#endif
35
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036v8::Platform* V8::platform_ = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000037
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080038
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039bool V8::Initialize() {
Ben Murdoch8b112d22011-06-08 16:22:53 +010040 InitializeOncePerProcess();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +000042}
43
44
45void V8::TearDown() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046 Bootstrapper::TearDownExtensions();
47 ElementsAccessor::TearDown();
48 LOperand::TearDownCaches();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049 ExternalReference::TearDownMathExpData();
50 RegisteredExtension::UnregisterAll();
51 Isolate::GlobalTearDown();
52 Sampler::TearDown();
53 FlagList::ResetAllFlags(); // Frees memory held by string arguments.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080054}
55
56
Ben Murdoch3ef787d2012-04-12 10:51:47 +010057void V8::InitializeOncePerProcessImpl() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 FlagList::EnforceFlagImplications();
Ben Murdoch8b112d22011-06-08 16:22:53 +010059
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 if (FLAG_predictable && FLAG_random_seed == 0) {
61 // Avoid random seeds in predictable mode.
62 FLAG_random_seed = 12347;
Ben Murdoch8b112d22011-06-08 16:22:53 +010063 }
64
Ben Murdoch3ef787d2012-04-12 10:51:47 +010065 if (FLAG_stress_compaction) {
66 FLAG_force_marking_deque_overflows = true;
67 FLAG_gc_global = true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 FLAG_max_semi_space_size = 1;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010069 }
70
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071 if (FLAG_turbo && strcmp(FLAG_turbo_filter, "~~") == 0) {
72 const char* filter_flag = "--turbo-filter=*";
73 FlagList::SetFlagsFromString(filter_flag, StrLength(filter_flag));
74 }
75
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 base::OS::Initialize(FLAG_random_seed, FLAG_hard_abort, FLAG_gc_fake_mmap);
77
78 Isolate::InitializeOncePerProcess();
79
80 Sampler::SetUp();
81 CpuFeatures::Probe(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 ElementsAccessor::InitializeOncePerProcess();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010083 LOperand::SetUpCaches();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 SetUpJSCallerSavedCodeData();
85 ExternalReference::SetUp();
86 Bootstrapper::InitializeOncePerProcess();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010087}
88
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089
Ben Murdoch3ef787d2012-04-12 10:51:47 +010090void V8::InitializeOncePerProcess() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 base::CallOnce(&init_once, &InitializeOncePerProcessImpl);
92}
93
94
95void V8::InitializePlatform(v8::Platform* platform) {
96 CHECK(!platform_);
97 CHECK(platform);
98 platform_ = platform;
99}
100
101
102void V8::ShutdownPlatform() {
103 CHECK(platform_);
104 platform_ = NULL;
105}
106
107
108v8::Platform* V8::GetCurrentPlatform() {
109 DCHECK(platform_);
110 return platform_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100111}
112
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400113
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114void V8::SetPlatformForTesting(v8::Platform* platform) { platform_ = platform; }
115
116
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400117void V8::SetNativesBlob(StartupData* natives_blob) {
118#ifdef V8_USE_EXTERNAL_STARTUP_DATA
119 base::CallOnce(&init_natives_once, &SetNativesFromFile, natives_blob);
120#else
121 CHECK(false);
122#endif
123}
124
125
126void V8::SetSnapshotBlob(StartupData* snapshot_blob) {
127#ifdef V8_USE_EXTERNAL_STARTUP_DATA
128 base::CallOnce(&init_snapshot_once, &SetSnapshotFromFile, snapshot_blob);
129#else
130 CHECK(false);
131#endif
132}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000133} // namespace internal
134} // namespace v8