blob: c0124e4de0ed63db218935faad1eaea9d70b9cb1 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "debug.h"
32#include "serialize.h"
33#include "stub-cache.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000034#include "oprofile-agent.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
36namespace v8 { namespace internal {
37
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038bool V8::has_been_setup_ = false;
39bool V8::has_been_disposed_ = false;
40
41bool V8::Initialize(Deserializer *des) {
42 bool create_heap_objects = des == NULL;
43 if (HasBeenDisposed()) return false;
44 if (HasBeenSetup()) return true;
45 has_been_setup_ = true;
46#ifdef DEBUG
47 // The initialization process does not handle memory exhaustion.
48 DisallowAllocationFailure disallow_allocation_failure;
49#endif
50
51 // Enable logging before setting up the heap
52 Logger::Setup();
53 if (des) des->GetLog();
54
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000055 // Setup the platform OS support.
56 OS::Setup();
57
58 // Setup the object heap
59 ASSERT(!Heap::HasBeenSetup());
60 if (!Heap::Setup(create_heap_objects)) {
61 has_been_setup_ = false;
62 return false;
63 }
64
65 // Initialize other runtime facilities
66 Bootstrapper::Initialize(create_heap_objects);
67 Builtins::Setup(create_heap_objects);
68 Top::Initialize();
69
70 if (FLAG_preemption) {
71 v8::Locker locker;
72 v8::Locker::StartPreemption(100);
73 }
74
ager@chromium.org65dad4b2009-04-23 08:48:43 +000075#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076 Debug::Setup(create_heap_objects);
ager@chromium.org65dad4b2009-04-23 08:48:43 +000077#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078 StubCache::Initialize(create_heap_objects);
79
80 // If we are deserializing, read the state into the now-empty heap.
81 if (des != NULL) {
82 des->Deserialize();
83 StubCache::Clear();
84 }
85
kasperl@chromium.org061ef742009-02-27 12:16:20 +000086 // Setup the CPU support. Must be done after heap setup and after
87 // any deserialization because we have to have the initial heap
88 // objects in place for creating the code object used for probing.
89 CPU::Setup();
90
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000091 OProfileAgent::Initialize();
92
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093 return true;
94}
95
96
97void V8::TearDown() {
98 if (HasBeenDisposed()) return;
99 if (!HasBeenSetup()) return;
100
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000101 OProfileAgent::TearDown();
102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 if (FLAG_preemption) {
104 v8::Locker locker;
105 v8::Locker::StopPreemption();
106 }
107
108 Builtins::TearDown();
109 Bootstrapper::TearDown();
110
kasper.lundaf4734f2008-07-28 12:50:18 +0000111 Top::TearDown();
112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113 Heap::TearDown();
114 Logger::TearDown();
115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116 has_been_setup_ = false;
117 has_been_disposed_ = true;
118}
119
120} } // namespace v8::internal