v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
v8.cc
Go to the documentation of this file.
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 #ifdef V8_USE_DEFAULT_PLATFORM
41 #endif
42 #include "lithium-allocator.h"
43 #include "objects.h"
44 #include "once.h"
45 #include "platform.h"
46 #include "sampler.h"
47 #include "runtime-profiler.h"
48 #include "serialize.h"
49 #include "store-buffer.h"
50 
51 namespace v8 {
52 namespace internal {
53 
54 V8_DECLARE_ONCE(init_once);
55 
56 List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL;
57 v8::ArrayBuffer::Allocator* V8::array_buffer_allocator_ = NULL;
58 v8::Platform* V8::platform_ = NULL;
59 
60 
62  InitializeOncePerProcess();
63 
64  // The current thread may not yet had entered an isolate to run.
65  // Note the Isolate::Current() may be non-null because for various
66  // initialization purposes an initializing thread may be assigned an isolate
67  // but not actually enter it.
70  }
71 
73  ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals(
76  i::Isolate::Current());
77 
78  Isolate* isolate = Isolate::Current();
79  if (isolate->IsDead()) return false;
80  if (isolate->IsInitialized()) return true;
81 
82 #ifdef V8_USE_DEFAULT_PLATFORM
83  DefaultPlatform* platform = static_cast<DefaultPlatform*>(platform_);
84  platform->SetThreadPoolSize(isolate->max_available_threads());
85  // We currently only start the threads early, if we know that we'll use them.
86  if (FLAG_job_based_sweeping) platform->EnsureInitialized();
87 #endif
88 
89  return isolate->Init(des);
90 }
91 
92 
93 void V8::TearDown() {
94  Isolate* isolate = Isolate::Current();
95  ASSERT(isolate->IsDefaultIsolate());
96  if (!isolate->IsInitialized()) return;
97 
98  // The isolate has to be torn down before clearing the LOperand
99  // caches so that the optimizing compiler thread (if running)
100  // doesn't see an inconsistent view of the lithium instructions.
101  isolate->TearDown();
102  delete isolate;
103 
107  ExternalReference::TearDownMathExpData();
110 
111  delete call_completed_callbacks_;
112  call_completed_callbacks_ = NULL;
113 
115 
116 #ifdef V8_USE_DEFAULT_PLATFORM
117  DefaultPlatform* platform = static_cast<DefaultPlatform*>(platform_);
118  platform_ = NULL;
119  delete platform;
120 #endif
121 }
122 
123 
126  StackFrame::SetReturnAddressLocationResolver(resolver);
127 }
128 
129 
131  if (call_completed_callbacks_ == NULL) { // Lazy init.
132  call_completed_callbacks_ = new List<CallCompletedCallback>();
133  }
134  for (int i = 0; i < call_completed_callbacks_->length(); i++) {
135  if (callback == call_completed_callbacks_->at(i)) return;
136  }
137  call_completed_callbacks_->Add(callback);
138 }
139 
140 
142  if (call_completed_callbacks_ == NULL) return;
143  for (int i = 0; i < call_completed_callbacks_->length(); i++) {
144  if (callback == call_completed_callbacks_->at(i)) {
145  call_completed_callbacks_->Remove(i);
146  }
147  }
148 }
149 
150 
152  bool has_call_completed_callbacks = call_completed_callbacks_ != NULL;
153  bool run_microtasks = isolate->autorun_microtasks() &&
154  isolate->microtask_pending();
155  if (!has_call_completed_callbacks && !run_microtasks) return;
156 
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 (run_microtasks) Execution::RunMicrotasks(isolate);
163  if (has_call_completed_callbacks) {
164  for (int i = 0; i < call_completed_callbacks_->length(); i++) {
165  call_completed_callbacks_->at(i)();
166  }
167  }
168  handle_scope_implementer->DecrementCallDepth();
169 }
170 
171 
172 void V8::RunMicrotasks(Isolate* isolate) {
173  if (!isolate->microtask_pending())
174  return;
175 
176  HandleScopeImplementer* handle_scope_implementer =
177  isolate->handle_scope_implementer();
178  ASSERT(handle_scope_implementer->CallDepthIsZero());
179 
180  // Increase call depth to prevent recursive callbacks.
181  handle_scope_implementer->IncrementCallDepth();
182  Execution::RunMicrotasks(isolate);
183  handle_scope_implementer->DecrementCallDepth();
184 }
185 
186 
187 void V8::InitializeOncePerProcessImpl() {
188  FlagList::EnforceFlagImplications();
189 
190  if (FLAG_predictable && FLAG_random_seed == 0) {
191  // Avoid random seeds in predictable mode.
192  FLAG_random_seed = 12347;
193  }
194 
195  if (FLAG_stress_compaction) {
196  FLAG_force_marking_deque_overflows = true;
197  FLAG_gc_global = true;
198  FLAG_max_new_space_size = (1 << (kPageSizeBits - 10)) * 2;
199  }
200 
201 #ifdef V8_USE_DEFAULT_PLATFORM
202  platform_ = new DefaultPlatform;
203 #endif
204  Sampler::SetUp();
205  CPU::SetUp();
206  OS::PostSetUp();
210  ExternalReference::SetUp();
212 }
213 
214 
215 void V8::InitializeOncePerProcess() {
216  CallOnce(&init_once, &InitializeOncePerProcessImpl);
217 }
218 
219 
221  ASSERT(!platform_);
222  ASSERT(platform);
223  platform_ = platform;
224 }
225 
226 
228  ASSERT(platform_);
229  platform_ = NULL;
230 }
231 
232 
234  ASSERT(platform_);
235  return platform_;
236 }
237 
238 } } // namespace v8::internal
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
static bool Initialize(Deserializer *des)
Definition: v8.cc:61
void(* CallCompletedCallback)()
Definition: v8.h:4043
static void TearDownCaches()
Definition: lithium.cc:146
HandleScopeImplementer * handle_scope_implementer()
Definition: isolate.h:902
V8_DECLARE_ONCE(initialize_gc_once)
static void FireCallCompletedCallback(Isolate *isolate)
Definition: v8.cc:151
void CallOnce(OnceType *once, NoArgFunction init_func)
Definition: once.h:105
static void ShutdownPlatform()
Definition: v8.cc:227
bool Init(Deserializer *des)
Definition: isolate.cc:1873
T & at(int i) const
Definition: list.h:90
#define ASSERT(condition)
Definition: checks.h:329
static PerIsolateThreadData * CurrentPerIsolateThreadData()
Definition: isolate.h:463
static void SetUpCaches()
Definition: lithium.cc:139
bool IsDefaultIsolate() const
Definition: isolate.h:501
static ThreadId Current()
Definition: isolate.h:170
void SetThreadPoolSize(int thread_pool_size)
static void EnterDefaultIsolate()
Definition: isolate.cc:229
uintptr_t(* ReturnAddressLocationResolver)(uintptr_t return_addr_location)
Definition: v8.h:4494
T Remove(int i)
Definition: list-inl.h:125
static void InitializeOncePerProcess()
Definition: elements.cc:1861
static void RemoveCallCompletedCallback(CallCompletedCallback callback)
Definition: v8.cc:141
static void UnregisterAll()
Definition: api.cc:429
static void TearDown()
Definition: sampler.cc:638
static void GlobalTearDown()
Definition: isolate.cc:1618
void SetUpJSCallerSavedCodeData()
Definition: frames.cc:1600
static void InitializeOncePerProcess()
static void SetUp()
Definition: sampler.cc:630
static void AddCallCompletedCallback(CallCompletedCallback callback)
Definition: v8.cc:130
static void PostSetUp()
static void SetReturnAddressLocationResolver(ReturnAddressLocationResolver resolver)
Definition: v8.cc:124
static void TearDown()
Definition: v8.cc:93
void Add(const T &element, AllocationPolicy allocator=AllocationPolicy())
Definition: list-inl.h:39
static void InitializePlatform(v8::Platform *platform)
Definition: v8.cc:220
static void RunMicrotasks(Isolate *isolate)
Definition: v8.cc:172
const int kPageSizeBits
Definition: v8globals.h:95
static void RunMicrotasks(Isolate *isolate)
Definition: execution.cc:357
static v8::Platform * GetCurrentPlatform()
Definition: v8.cc:233
static void TearDownExtensions()