The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / v8 / src / v8.cc @ f230a1cf
History | View | Annotate | Download (8.29 KB)
1 |
// Copyright 2012 the V8 project authors. All rights reserved.
|
---|---|
2 |
// 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 "assembler.h" |
31 |
#include "isolate.h" |
32 |
#include "elements.h" |
33 |
#include "bootstrapper.h" |
34 |
#include "debug.h" |
35 |
#include "deoptimizer.h" |
36 |
#include "frames.h" |
37 |
#include "heap-profiler.h" |
38 |
#include "hydrogen.h" |
39 |
#include "lithium-allocator.h" |
40 |
#include "objects.h" |
41 |
#include "once.h" |
42 |
#include "platform.h" |
43 |
#include "sampler.h" |
44 |
#include "runtime-profiler.h" |
45 |
#include "serialize.h" |
46 |
#include "store-buffer.h" |
47 |
|
48 |
namespace v8 {
|
49 |
namespace internal {
|
50 |
|
51 |
V8_DECLARE_ONCE(init_once); |
52 |
|
53 |
List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL;
|
54 |
v8::ArrayBuffer::Allocator* V8::array_buffer_allocator_ = NULL;
|
55 |
|
56 |
|
57 |
bool V8::Initialize(Deserializer* des) {
|
58 |
InitializeOncePerProcess(); |
59 |
|
60 |
// The current thread may not yet had entered an isolate to run.
|
61 |
// Note the Isolate::Current() may be non-null because for various
|
62 |
// initialization purposes an initializing thread may be assigned an isolate
|
63 |
// but not actually enter it.
|
64 |
if (i::Isolate::CurrentPerIsolateThreadData() == NULL) { |
65 |
i::Isolate::EnterDefaultIsolate(); |
66 |
} |
67 |
|
68 |
ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL);
|
69 |
ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals( |
70 |
i::ThreadId::Current())); |
71 |
ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() == |
72 |
i::Isolate::Current()); |
73 |
|
74 |
Isolate* isolate = Isolate::Current(); |
75 |
if (isolate->IsDead()) return false; |
76 |
if (isolate->IsInitialized()) return true; |
77 |
|
78 |
return isolate->Init(des);
|
79 |
} |
80 |
|
81 |
|
82 |
void V8::TearDown() {
|
83 |
Isolate* isolate = Isolate::Current(); |
84 |
ASSERT(isolate->IsDefaultIsolate()); |
85 |
if (!isolate->IsInitialized()) return; |
86 |
|
87 |
// The isolate has to be torn down before clearing the LOperand
|
88 |
// caches so that the optimizing compiler thread (if running)
|
89 |
// doesn't see an inconsistent view of the lithium instructions.
|
90 |
isolate->TearDown(); |
91 |
delete isolate;
|
92 |
|
93 |
ElementsAccessor::TearDown(); |
94 |
LOperand::TearDownCaches(); |
95 |
ExternalReference::TearDownMathExpData(); |
96 |
RegisteredExtension::UnregisterAll(); |
97 |
Isolate::GlobalTearDown(); |
98 |
|
99 |
delete call_completed_callbacks_;
|
100 |
call_completed_callbacks_ = NULL;
|
101 |
|
102 |
Sampler::TearDown(); |
103 |
} |
104 |
|
105 |
|
106 |
void V8::SetReturnAddressLocationResolver(
|
107 |
ReturnAddressLocationResolver resolver) { |
108 |
StackFrame::SetReturnAddressLocationResolver(resolver); |
109 |
} |
110 |
|
111 |
|
112 |
// Used by JavaScript APIs
|
113 |
uint32_t V8::Random(Context* context) { |
114 |
ASSERT(context->IsNativeContext()); |
115 |
ByteArray* seed = context->random_seed(); |
116 |
uint32_t* state = reinterpret_cast<uint32_t*>(seed->GetDataStartAddress());
|
117 |
|
118 |
// When we get here, the RNG must have been initialized,
|
119 |
// see the Genesis constructor in file bootstrapper.cc.
|
120 |
ASSERT_NE(0, state[0]); |
121 |
ASSERT_NE(0, state[1]); |
122 |
|
123 |
// Mix the bits. Never replaces state[i] with 0 if it is nonzero.
|
124 |
state[0] = 18273 * (state[0] & 0xFFFF) + (state[0] >> 16); |
125 |
state[1] = 36969 * (state[1] & 0xFFFF) + (state[1] >> 16); |
126 |
|
127 |
return (state[0] << 14) + (state[1] & 0x3FFFF); |
128 |
} |
129 |
|
130 |
|
131 |
void V8::AddCallCompletedCallback(CallCompletedCallback callback) {
|
132 |
if (call_completed_callbacks_ == NULL) { // Lazy init. |
133 |
call_completed_callbacks_ = new List<CallCompletedCallback>();
|
134 |
} |
135 |
for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
136 |
if (callback == call_completed_callbacks_->at(i)) return; |
137 |
} |
138 |
call_completed_callbacks_->Add(callback); |
139 |
} |
140 |
|
141 |
|
142 |
void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) {
|
143 |
if (call_completed_callbacks_ == NULL) return; |
144 |
for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
145 |
if (callback == call_completed_callbacks_->at(i)) {
|
146 |
call_completed_callbacks_->Remove(i); |
147 |
} |
148 |
} |
149 |
} |
150 |
|
151 |
|
152 |
void V8::FireCallCompletedCallback(Isolate* isolate) {
|
153 |
bool has_call_completed_callbacks = call_completed_callbacks_ != NULL; |
154 |
bool observer_delivery_pending =
|
155 |
FLAG_harmony_observation && isolate->observer_delivery_pending(); |
156 |
if (!has_call_completed_callbacks && !observer_delivery_pending) return; |
157 |
HandleScopeImplementer* handle_scope_implementer = |
158 |
isolate->handle_scope_implementer(); |
159 |
if (!handle_scope_implementer->CallDepthIsZero()) return; |
160 |
// Fire callbacks. Increase call depth to prevent recursive callbacks.
|
161 |
handle_scope_implementer->IncrementCallDepth(); |
162 |
if (observer_delivery_pending) {
|
163 |
JSObject::DeliverChangeRecords(isolate); |
164 |
} |
165 |
if (has_call_completed_callbacks) {
|
166 |
for (int i = 0; i < call_completed_callbacks_->length(); i++) { |
167 |
call_completed_callbacks_->at(i)(); |
168 |
} |
169 |
} |
170 |
handle_scope_implementer->DecrementCallDepth(); |
171 |
} |
172 |
|
173 |
|
174 |
// Use a union type to avoid type-aliasing optimizations in GCC.
|
175 |
typedef union { |
176 |
double double_value;
|
177 |
uint64_t uint64_t_value; |
178 |
} double_int_union; |
179 |
|
180 |
|
181 |
Object* V8::FillHeapNumberWithRandom(Object* heap_number, |
182 |
Context* context) { |
183 |
double_int_union r; |
184 |
uint64_t random_bits = Random(context); |
185 |
// Convert 32 random bits to 0.(32 random bits) in a double
|
186 |
// by computing:
|
187 |
// ( 1.(20 0s)(32 random bits) x 2^20 ) - (1.0 x 2^20)).
|
188 |
static const double binary_million = 1048576.0; |
189 |
r.double_value = binary_million; |
190 |
r.uint64_t_value |= random_bits; |
191 |
r.double_value -= binary_million; |
192 |
|
193 |
HeapNumber::cast(heap_number)->set_value(r.double_value); |
194 |
return heap_number;
|
195 |
} |
196 |
|
197 |
|
198 |
void V8::InitializeOncePerProcessImpl() {
|
199 |
FlagList::EnforceFlagImplications(); |
200 |
if (FLAG_stress_compaction) {
|
201 |
FLAG_force_marking_deque_overflows = true;
|
202 |
FLAG_gc_global = true;
|
203 |
FLAG_max_new_space_size = (1 << (kPageSizeBits - 10)) * 2; |
204 |
} |
205 |
|
206 |
if (FLAG_concurrent_recompilation &&
|
207 |
(FLAG_trace_hydrogen || FLAG_trace_hydrogen_stubs)) { |
208 |
FLAG_concurrent_recompilation = false;
|
209 |
PrintF("Concurrent recompilation has been disabled for tracing.\n");
|
210 |
} |
211 |
|
212 |
if (FLAG_sweeper_threads <= 0) { |
213 |
if (FLAG_concurrent_sweeping) {
|
214 |
FLAG_sweeper_threads = SystemThreadManager:: |
215 |
NumberOfParallelSystemThreads( |
216 |
SystemThreadManager::CONCURRENT_SWEEPING); |
217 |
} else if (FLAG_parallel_sweeping) { |
218 |
FLAG_sweeper_threads = SystemThreadManager:: |
219 |
NumberOfParallelSystemThreads( |
220 |
SystemThreadManager::PARALLEL_SWEEPING); |
221 |
} |
222 |
if (FLAG_sweeper_threads == 0) { |
223 |
FLAG_concurrent_sweeping = false;
|
224 |
FLAG_parallel_sweeping = false;
|
225 |
} |
226 |
} else if (!FLAG_concurrent_sweeping && !FLAG_parallel_sweeping) { |
227 |
FLAG_sweeper_threads = 0;
|
228 |
} |
229 |
|
230 |
if (FLAG_concurrent_recompilation &&
|
231 |
SystemThreadManager::NumberOfParallelSystemThreads( |
232 |
SystemThreadManager::PARALLEL_RECOMPILATION) == 0) {
|
233 |
FLAG_concurrent_recompilation = false;
|
234 |
} |
235 |
|
236 |
Sampler::SetUp(); |
237 |
CPU::SetUp(); |
238 |
OS::PostSetUp(); |
239 |
ElementsAccessor::InitializeOncePerProcess(); |
240 |
LOperand::SetUpCaches(); |
241 |
SetUpJSCallerSavedCodeData(); |
242 |
ExternalReference::SetUp(); |
243 |
Bootstrapper::InitializeOncePerProcess(); |
244 |
} |
245 |
|
246 |
|
247 |
void V8::InitializeOncePerProcess() {
|
248 |
CallOnce(&init_once, &InitializeOncePerProcessImpl); |
249 |
} |
250 |
|
251 |
} } // namespace v8::internal
|