blob: 5f7b3f2d3b6134b0c8518d76eca776ad1d8b98fd [file] [log] [blame]
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001// Copyright 2012 the V8 project authors. All rights reserved.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +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 <stdlib.h>
29
30#include "v8.h"
31
32#include "ast.h"
33#include "bootstrapper.h"
34#include "codegen.h"
35#include "compilation-cache.h"
36#include "debug.h"
37#include "deoptimizer.h"
38#include "heap-profiler.h"
39#include "hydrogen.h"
40#include "isolate.h"
41#include "lithium-allocator.h"
42#include "log.h"
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +000043#include "marking-thread.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000044#include "messages.h"
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000045#include "platform.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000046#include "regexp-stack.h"
47#include "runtime-profiler.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000048#include "scopeinfo.h"
49#include "serialize.h"
50#include "simulator.h"
51#include "spaces.h"
52#include "stub-cache.h"
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +000053#include "sweeper-thread.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054#include "version.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000055#include "vm-state-inl.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056
57
58namespace v8 {
59namespace internal {
60
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000061Atomic32 ThreadId::highest_thread_id_ = 0;
62
63int ThreadId::AllocateThreadId() {
64 int new_id = NoBarrier_AtomicIncrement(&highest_thread_id_, 1);
65 return new_id;
66}
67
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000068
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000069int ThreadId::GetCurrentThreadId() {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000070 int thread_id = Thread::GetThreadLocalInt(Isolate::thread_id_key_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000071 if (thread_id == 0) {
72 thread_id = AllocateThreadId();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000073 Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000074 }
75 return thread_id;
76}
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000077
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000078
79ThreadLocalTop::ThreadLocalTop() {
80 InitializeInternal();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000081 // This flag may be set using v8::V8::IgnoreOutOfMemoryException()
82 // before an isolate is initialized. The initialize methods below do
83 // not touch it to preserve its value.
84 ignore_out_of_memory_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000085}
86
87
88void ThreadLocalTop::InitializeInternal() {
89 c_entry_fp_ = 0;
90 handler_ = 0;
91#ifdef USE_SIMULATOR
92 simulator_ = NULL;
93#endif
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000094 js_entry_sp_ = NULL;
95 external_callback_ = NULL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000096 current_vm_state_ = EXTERNAL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000097 try_catch_handler_address_ = NULL;
98 context_ = NULL;
99 thread_id_ = ThreadId::Invalid();
100 external_caught_exception_ = false;
101 failed_access_check_callback_ = NULL;
102 save_context_ = NULL;
103 catcher_ = NULL;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000104 top_lookup_result_ = NULL;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000105
106 // These members are re-initialized later after deserialization
107 // is complete.
108 pending_exception_ = NULL;
109 has_pending_message_ = false;
110 pending_message_obj_ = NULL;
111 pending_message_script_ = NULL;
112 scheduled_exception_ = NULL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000113}
114
115
116void ThreadLocalTop::Initialize() {
117 InitializeInternal();
118#ifdef USE_SIMULATOR
119#ifdef V8_TARGET_ARCH_ARM
120 simulator_ = Simulator::current(isolate_);
121#elif V8_TARGET_ARCH_MIPS
122 simulator_ = Simulator::current(isolate_);
123#endif
124#endif
125 thread_id_ = ThreadId::Current();
126}
127
128
129v8::TryCatch* ThreadLocalTop::TryCatchHandler() {
130 return TRY_CATCH_FROM_ADDRESS(try_catch_handler_address());
131}
132
133
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000134int SystemThreadManager::NumberOfParallelSystemThreads(
135 ParallelSystemComponent type) {
136 int number_of_threads = Min(OS::NumberOfCores(), kMaxThreads);
137 ASSERT(number_of_threads > 0);
138 if (number_of_threads == 1) {
139 return 0;
140 }
141 if (type == PARALLEL_SWEEPING) {
142 return number_of_threads;
143 } else if (type == CONCURRENT_SWEEPING) {
144 return number_of_threads - 1;
145 } else if (type == PARALLEL_MARKING) {
146 return number_of_threads;
147 }
148 return 1;
149}
150
151
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000152// Create a dummy thread that will wait forever on a semaphore. The only
153// purpose for this thread is to have some stack area to save essential data
154// into for use by a stacks only core dump (aka minidump).
155class PreallocatedMemoryThread: public Thread {
156 public:
157 char* data() {
158 if (data_ready_semaphore_ != NULL) {
159 // Initial access is guarded until the data has been published.
160 data_ready_semaphore_->Wait();
161 delete data_ready_semaphore_;
162 data_ready_semaphore_ = NULL;
163 }
164 return data_;
165 }
166
167 unsigned length() {
168 if (data_ready_semaphore_ != NULL) {
169 // Initial access is guarded until the data has been published.
170 data_ready_semaphore_->Wait();
171 delete data_ready_semaphore_;
172 data_ready_semaphore_ = NULL;
173 }
174 return length_;
175 }
176
177 // Stop the PreallocatedMemoryThread and release its resources.
178 void StopThread() {
179 keep_running_ = false;
180 wait_for_ever_semaphore_->Signal();
181
182 // Wait for the thread to terminate.
183 Join();
184
185 if (data_ready_semaphore_ != NULL) {
186 delete data_ready_semaphore_;
187 data_ready_semaphore_ = NULL;
188 }
189
190 delete wait_for_ever_semaphore_;
191 wait_for_ever_semaphore_ = NULL;
192 }
193
194 protected:
195 // When the thread starts running it will allocate a fixed number of bytes
196 // on the stack and publish the location of this memory for others to use.
197 void Run() {
198 EmbeddedVector<char, 15 * 1024> local_buffer;
199
200 // Initialize the buffer with a known good value.
201 OS::StrNCpy(local_buffer, "Trace data was not generated.\n",
202 local_buffer.length());
203
204 // Publish the local buffer and signal its availability.
205 data_ = local_buffer.start();
206 length_ = local_buffer.length();
207 data_ready_semaphore_->Signal();
208
209 while (keep_running_) {
210 // This thread will wait here until the end of time.
211 wait_for_ever_semaphore_->Wait();
212 }
213
214 // Make sure we access the buffer after the wait to remove all possibility
215 // of it being optimized away.
216 OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n",
217 local_buffer.length());
218 }
219
220
221 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000222 PreallocatedMemoryThread()
223 : Thread("v8:PreallocMem"),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000224 keep_running_(true),
225 wait_for_ever_semaphore_(OS::CreateSemaphore(0)),
226 data_ready_semaphore_(OS::CreateSemaphore(0)),
227 data_(NULL),
228 length_(0) {
229 }
230
231 // Used to make sure that the thread keeps looping even for spurious wakeups.
232 bool keep_running_;
233
234 // This semaphore is used by the PreallocatedMemoryThread to wait for ever.
235 Semaphore* wait_for_ever_semaphore_;
236 // Semaphore to signal that the data has been initialized.
237 Semaphore* data_ready_semaphore_;
238
239 // Location and size of the preallocated memory block.
240 char* data_;
241 unsigned length_;
242
243 friend class Isolate;
244
245 DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread);
246};
247
248
249void Isolate::PreallocatedMemoryThreadStart() {
250 if (preallocated_memory_thread_ != NULL) return;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000251 preallocated_memory_thread_ = new PreallocatedMemoryThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000252 preallocated_memory_thread_->Start();
253}
254
255
256void Isolate::PreallocatedMemoryThreadStop() {
257 if (preallocated_memory_thread_ == NULL) return;
258 preallocated_memory_thread_->StopThread();
259 // Done with the thread entirely.
260 delete preallocated_memory_thread_;
261 preallocated_memory_thread_ = NULL;
262}
263
264
lrn@chromium.org7516f052011-03-30 08:52:27 +0000265void Isolate::PreallocatedStorageInit(size_t size) {
266 ASSERT(free_list_.next_ == &free_list_);
267 ASSERT(free_list_.previous_ == &free_list_);
268 PreallocatedStorage* free_chunk =
269 reinterpret_cast<PreallocatedStorage*>(new char[size]);
270 free_list_.next_ = free_list_.previous_ = free_chunk;
271 free_chunk->next_ = free_chunk->previous_ = &free_list_;
272 free_chunk->size_ = size - sizeof(PreallocatedStorage);
273 preallocated_storage_preallocated_ = true;
274}
275
276
277void* Isolate::PreallocatedStorageNew(size_t size) {
278 if (!preallocated_storage_preallocated_) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000279 return FreeStoreAllocationPolicy().New(size);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000280 }
281 ASSERT(free_list_.next_ != &free_list_);
282 ASSERT(free_list_.previous_ != &free_list_);
283
284 size = (size + kPointerSize - 1) & ~(kPointerSize - 1);
285 // Search for exact fit.
286 for (PreallocatedStorage* storage = free_list_.next_;
287 storage != &free_list_;
288 storage = storage->next_) {
289 if (storage->size_ == size) {
290 storage->Unlink();
291 storage->LinkTo(&in_use_list_);
292 return reinterpret_cast<void*>(storage + 1);
293 }
294 }
295 // Search for first fit.
296 for (PreallocatedStorage* storage = free_list_.next_;
297 storage != &free_list_;
298 storage = storage->next_) {
299 if (storage->size_ >= size + sizeof(PreallocatedStorage)) {
300 storage->Unlink();
301 storage->LinkTo(&in_use_list_);
302 PreallocatedStorage* left_over =
303 reinterpret_cast<PreallocatedStorage*>(
304 reinterpret_cast<char*>(storage + 1) + size);
305 left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage);
306 ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) ==
307 storage->size_);
308 storage->size_ = size;
309 left_over->LinkTo(&free_list_);
310 return reinterpret_cast<void*>(storage + 1);
311 }
312 }
313 // Allocation failure.
314 ASSERT(false);
315 return NULL;
316}
317
318
319// We don't attempt to coalesce.
320void Isolate::PreallocatedStorageDelete(void* p) {
321 if (p == NULL) {
322 return;
323 }
324 if (!preallocated_storage_preallocated_) {
325 FreeStoreAllocationPolicy::Delete(p);
326 return;
327 }
328 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1;
329 ASSERT(storage->next_->previous_ == storage);
330 ASSERT(storage->previous_->next_ == storage);
331 storage->Unlink();
332 storage->LinkTo(&free_list_);
333}
334
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000335Isolate* Isolate::default_isolate_ = NULL;
336Thread::LocalStorageKey Isolate::isolate_key_;
337Thread::LocalStorageKey Isolate::thread_id_key_;
338Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_;
339Mutex* Isolate::process_wide_mutex_ = OS::CreateMutex();
340Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL;
ulan@chromium.org750145a2013-03-07 15:14:13 +0000341Atomic32 Isolate::isolate_counter_ = 0;
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000342
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000343Isolate::PerIsolateThreadData* Isolate::AllocatePerIsolateThreadData(
344 ThreadId thread_id) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000345 ASSERT(!thread_id.Equals(ThreadId::Invalid()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000346 PerIsolateThreadData* per_thread = new PerIsolateThreadData(this, thread_id);
347 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000348 ScopedLock lock(process_wide_mutex_);
349 ASSERT(thread_data_table_->Lookup(this, thread_id) == NULL);
350 thread_data_table_->Insert(per_thread);
351 ASSERT(thread_data_table_->Lookup(this, thread_id) == per_thread);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000352 }
353 return per_thread;
354}
355
356
357Isolate::PerIsolateThreadData*
358 Isolate::FindOrAllocatePerThreadDataForThisThread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000359 ThreadId thread_id = ThreadId::Current();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000360 PerIsolateThreadData* per_thread = NULL;
361 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000362 ScopedLock lock(process_wide_mutex_);
363 per_thread = thread_data_table_->Lookup(this, thread_id);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000364 if (per_thread == NULL) {
365 per_thread = AllocatePerIsolateThreadData(thread_id);
366 }
367 }
368 return per_thread;
369}
370
371
lrn@chromium.org1c092762011-05-09 09:42:16 +0000372Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() {
373 ThreadId thread_id = ThreadId::Current();
374 PerIsolateThreadData* per_thread = NULL;
375 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000376 ScopedLock lock(process_wide_mutex_);
377 per_thread = thread_data_table_->Lookup(this, thread_id);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000378 }
379 return per_thread;
380}
381
382
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000383void Isolate::EnsureDefaultIsolate() {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000384 ScopedLock lock(process_wide_mutex_);
385 if (default_isolate_ == NULL) {
386 isolate_key_ = Thread::CreateThreadLocalKey();
387 thread_id_key_ = Thread::CreateThreadLocalKey();
388 per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey();
389 thread_data_table_ = new Isolate::ThreadDataTable();
390 default_isolate_ = new Isolate();
391 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000392 // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000393 // because a non-null thread data may be already set.
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000394 if (Thread::GetThreadLocal(isolate_key_) == NULL) {
395 Thread::SetThreadLocal(isolate_key_, default_isolate_);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000396 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000397}
398
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000399struct StaticInitializer {
400 StaticInitializer() {
401 Isolate::EnsureDefaultIsolate();
402 }
403} static_initializer;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000405#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406Debugger* Isolate::GetDefaultIsolateDebugger() {
407 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000408 return default_isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000409}
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000410#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000411
412
413StackGuard* Isolate::GetDefaultIsolateStackGuard() {
414 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000415 return default_isolate_->stack_guard();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000416}
417
418
419void Isolate::EnterDefaultIsolate() {
420 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000421 ASSERT(default_isolate_ != NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422
423 PerIsolateThreadData* data = CurrentPerIsolateThreadData();
424 // If not yet in default isolate - enter it.
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000425 if (data == NULL || data->isolate() != default_isolate_) {
426 default_isolate_->Enter();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427 }
428}
429
430
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000431v8::Isolate* Isolate::GetDefaultIsolateForLocking() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000432 EnsureDefaultIsolate();
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000433 return reinterpret_cast<v8::Isolate*>(default_isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434}
435
436
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000437Address Isolate::get_address_from_id(Isolate::AddressId id) {
438 return isolate_addresses_[id];
439}
440
441
442char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) {
443 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage);
444 Iterate(v, thread);
445 return thread_storage + sizeof(ThreadLocalTop);
446}
447
448
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000449void Isolate::IterateThread(ThreadVisitor* v, char* t) {
450 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(t);
451 v->VisitThread(this, thread);
452}
453
454
455void Isolate::Iterate(ObjectVisitor* v, ThreadLocalTop* thread) {
456 // Visit the roots from the top for a given thread.
457 Object* pending;
458 // The pending exception can sometimes be a failure. We can't show
459 // that to the GC, which only understands objects.
460 if (thread->pending_exception_->ToObject(&pending)) {
461 v->VisitPointer(&pending);
462 thread->pending_exception_ = pending; // In case GC updated it.
463 }
464 v->VisitPointer(&(thread->pending_message_obj_));
465 v->VisitPointer(BitCast<Object**>(&(thread->pending_message_script_)));
466 v->VisitPointer(BitCast<Object**>(&(thread->context_)));
467 Object* scheduled;
468 if (thread->scheduled_exception_->ToObject(&scheduled)) {
469 v->VisitPointer(&scheduled);
470 thread->scheduled_exception_ = scheduled;
471 }
472
473 for (v8::TryCatch* block = thread->TryCatchHandler();
474 block != NULL;
475 block = TRY_CATCH_FROM_ADDRESS(block->next_)) {
476 v->VisitPointer(BitCast<Object**>(&(block->exception_)));
477 v->VisitPointer(BitCast<Object**>(&(block->message_)));
478 }
479
480 // Iterate over pointers on native execution stack.
481 for (StackFrameIterator it(this, thread); !it.done(); it.Advance()) {
482 it.frame()->Iterate(v);
483 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000484
485 // Iterate pointers in live lookup results.
486 thread->top_lookup_result_->Iterate(v);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000487}
488
489
490void Isolate::Iterate(ObjectVisitor* v) {
491 ThreadLocalTop* current_t = thread_local_top();
492 Iterate(v, current_t);
493}
494
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000495void Isolate::IterateDeferredHandles(ObjectVisitor* visitor) {
496 for (DeferredHandles* deferred = deferred_handles_head_;
497 deferred != NULL;
498 deferred = deferred->next_) {
499 deferred->Iterate(visitor);
500 }
501}
502
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000503
504void Isolate::RegisterTryCatchHandler(v8::TryCatch* that) {
505 // The ARM simulator has a separate JS stack. We therefore register
506 // the C++ try catch handler with the simulator and get back an
507 // address that can be used for comparisons with addresses into the
508 // JS stack. When running without the simulator, the address
509 // returned will be the address of the C++ try catch handler itself.
510 Address address = reinterpret_cast<Address>(
511 SimulatorStack::RegisterCTryCatch(reinterpret_cast<uintptr_t>(that)));
512 thread_local_top()->set_try_catch_handler_address(address);
513}
514
515
516void Isolate::UnregisterTryCatchHandler(v8::TryCatch* that) {
517 ASSERT(thread_local_top()->TryCatchHandler() == that);
518 thread_local_top()->set_try_catch_handler_address(
519 reinterpret_cast<Address>(that->next_));
520 thread_local_top()->catcher_ = NULL;
521 SimulatorStack::UnregisterCTryCatch();
522}
523
524
525Handle<String> Isolate::StackTraceString() {
526 if (stack_trace_nesting_level_ == 0) {
527 stack_trace_nesting_level_++;
528 HeapStringAllocator allocator;
529 StringStream::ClearMentionedObjectCache();
530 StringStream accumulator(&allocator);
531 incomplete_message_ = &accumulator;
532 PrintStack(&accumulator);
533 Handle<String> stack_trace = accumulator.ToString();
534 incomplete_message_ = NULL;
535 stack_trace_nesting_level_ = 0;
536 return stack_trace;
537 } else if (stack_trace_nesting_level_ == 1) {
538 stack_trace_nesting_level_++;
539 OS::PrintError(
540 "\n\nAttempt to print stack while printing stack (double fault)\n");
541 OS::PrintError(
542 "If you are lucky you may find a partial stack dump on stdout.\n\n");
543 incomplete_message_->OutputToStdOut();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000544 return factory()->empty_string();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000545 } else {
546 OS::Abort();
547 // Unreachable
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000548 return factory()->empty_string();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000549 }
550}
551
552
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000553void Isolate::PushStackTraceAndDie(unsigned int magic,
554 Object* object,
555 Map* map,
556 unsigned int magic2) {
557 const int kMaxStackTraceSize = 8192;
558 Handle<String> trace = StackTraceString();
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000559 uint8_t buffer[kMaxStackTraceSize];
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000560 int length = Min(kMaxStackTraceSize - 1, trace->length());
561 String::WriteToFlat(*trace, buffer, 0, length);
562 buffer[length] = '\0';
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000563 // TODO(dcarney): convert buffer to utf8?
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000564 OS::PrintError("Stacktrace (%x-%x) %p %p: %s\n",
565 magic, magic2,
566 static_cast<void*>(object), static_cast<void*>(map),
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000567 reinterpret_cast<char*>(buffer));
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000568 OS::Abort();
569}
570
571
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000572// Determines whether the given stack frame should be displayed in
573// a stack trace. The caller is the error constructor that asked
574// for the stack trace to be collected. The first time a construct
575// call to this function is encountered it is skipped. The seen_caller
576// in/out parameter is used to remember if the caller has been seen
577// yet.
578static bool IsVisibleInStackTrace(StackFrame* raw_frame,
579 Object* caller,
580 bool* seen_caller) {
581 // Only display JS frames.
582 if (!raw_frame->is_java_script()) return false;
583 JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame);
584 Object* raw_fun = frame->function();
585 // Not sure when this can happen but skip it just in case.
586 if (!raw_fun->IsJSFunction()) return false;
587 if ((raw_fun == caller) && !(*seen_caller)) {
588 *seen_caller = true;
589 return false;
590 }
591 // Skip all frames until we've seen the caller.
592 if (!(*seen_caller)) return false;
593 // Also, skip non-visible built-in functions and any call with the builtins
594 // object as receiver, so as to not reveal either the builtins object or
595 // an internal function.
596 // The --builtins-in-stack-traces command line flag allows including
597 // internal call sites in the stack trace for debugging purposes.
598 if (!FLAG_builtins_in_stack_traces) {
599 JSFunction* fun = JSFunction::cast(raw_fun);
600 if (frame->receiver()->IsJSBuiltinsObject() ||
601 (fun->IsBuiltin() && !fun->shared()->native())) {
602 return false;
603 }
604 }
605 return true;
606}
607
608
609Handle<JSArray> Isolate::CaptureSimpleStackTrace(Handle<JSObject> error_object,
610 Handle<Object> caller,
611 int limit) {
612 limit = Max(limit, 0); // Ensure that limit is not negative.
613 int initial_size = Min(limit, 10);
614 Handle<FixedArray> elements =
615 factory()->NewFixedArrayWithHoles(initial_size * 4);
616
617 // If the caller parameter is a function we skip frames until we're
618 // under it before starting to collect.
619 bool seen_caller = !caller->IsJSFunction();
620 int cursor = 0;
621 int frames_seen = 0;
622 for (StackFrameIterator iter(this);
623 !iter.done() && frames_seen < limit;
624 iter.Advance()) {
625 StackFrame* raw_frame = iter.frame();
626 if (IsVisibleInStackTrace(raw_frame, *caller, &seen_caller)) {
627 frames_seen++;
628 JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame);
629 // Set initial size to the maximum inlining level + 1 for the outermost
630 // function.
631 List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1);
632 frame->Summarize(&frames);
633 for (int i = frames.length() - 1; i >= 0; i--) {
634 if (cursor + 4 > elements->length()) {
635 int new_capacity = JSObject::NewElementsCapacity(elements->length());
636 Handle<FixedArray> new_elements =
637 factory()->NewFixedArrayWithHoles(new_capacity);
638 for (int i = 0; i < cursor; i++) {
639 new_elements->set(i, elements->get(i));
640 }
641 elements = new_elements;
642 }
643 ASSERT(cursor + 4 <= elements->length());
644
645 Handle<Object> recv = frames[i].receiver();
646 Handle<JSFunction> fun = frames[i].function();
647 Handle<Code> code = frames[i].code();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000648 Handle<Smi> offset(Smi::FromInt(frames[i].offset()), this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000649 elements->set(cursor++, *recv);
650 elements->set(cursor++, *fun);
651 elements->set(cursor++, *code);
652 elements->set(cursor++, *offset);
653 }
654 }
655 }
656 Handle<JSArray> result = factory()->NewJSArrayWithElements(elements);
657 result->set_length(Smi::FromInt(cursor));
658 return result;
659}
660
661
662void Isolate::CaptureAndSetDetailedStackTrace(Handle<JSObject> error_object) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +0000663 if (capture_stack_trace_for_uncaught_exceptions_) {
664 // Capture stack trace for a detailed exception message.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000665 Handle<String> key = factory()->hidden_stack_trace_string();
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +0000666 Handle<JSArray> stack_trace = CaptureCurrentStackTrace(
667 stack_trace_for_uncaught_exceptions_frame_limit_,
668 stack_trace_for_uncaught_exceptions_options_);
669 JSObject::SetHiddenProperty(error_object, key, stack_trace);
670 }
671}
672
673
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000674Handle<JSArray> Isolate::CaptureCurrentStackTrace(
675 int frame_limit, StackTrace::StackTraceOptions options) {
676 // Ensure no negative values.
677 int limit = Max(frame_limit, 0);
678 Handle<JSArray> stack_trace = factory()->NewJSArray(frame_limit);
679
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000680 Handle<String> column_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000681 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("column"));
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000682 Handle<String> line_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000683 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("lineNumber"));
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000684 Handle<String> script_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000685 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("scriptName"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000686 Handle<String> script_name_or_source_url_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000687 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000688 STATIC_ASCII_VECTOR("scriptNameOrSourceURL"));
689 Handle<String> function_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000690 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("functionName"));
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000691 Handle<String> eval_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000692 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("isEval"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000693 Handle<String> constructor_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000694 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("isConstructor"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000695
696 StackTraceFrameIterator it(this);
697 int frames_seen = 0;
698 while (!it.done() && (frames_seen < limit)) {
699 JavaScriptFrame* frame = it.frame();
700 // Set initial size to the maximum inlining level + 1 for the outermost
701 // function.
702 List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1);
703 frame->Summarize(&frames);
704 for (int i = frames.length() - 1; i >= 0 && frames_seen < limit; i--) {
705 // Create a JSObject to hold the information for the StackFrame.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000706 Handle<JSObject> stack_frame = factory()->NewJSObject(object_function());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000707
708 Handle<JSFunction> fun = frames[i].function();
709 Handle<Script> script(Script::cast(fun->shared()->script()));
710
711 if (options & StackTrace::kLineNumber) {
712 int script_line_offset = script->line_offset()->value();
713 int position = frames[i].code()->SourcePosition(frames[i].pc());
714 int line_number = GetScriptLineNumber(script, position);
715 // line_number is already shifted by the script_line_offset.
716 int relative_line_number = line_number - script_line_offset;
717 if (options & StackTrace::kColumnOffset && relative_line_number >= 0) {
718 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
719 int start = (relative_line_number == 0) ? 0 :
720 Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1;
721 int column_offset = position - start;
722 if (relative_line_number == 0) {
723 // For the case where the code is on the same line as the script
724 // tag.
725 column_offset += script->column_offset()->value();
726 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000727 CHECK_NOT_EMPTY_HANDLE(
728 this,
729 JSObject::SetLocalPropertyIgnoreAttributes(
730 stack_frame, column_key,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000731 Handle<Smi>(Smi::FromInt(column_offset + 1), this), NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000732 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000733 CHECK_NOT_EMPTY_HANDLE(
734 this,
735 JSObject::SetLocalPropertyIgnoreAttributes(
736 stack_frame, line_key,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000737 Handle<Smi>(Smi::FromInt(line_number + 1), this), NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000738 }
739
740 if (options & StackTrace::kScriptName) {
741 Handle<Object> script_name(script->name(), this);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000742 CHECK_NOT_EMPTY_HANDLE(this,
743 JSObject::SetLocalPropertyIgnoreAttributes(
744 stack_frame, script_key, script_name, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000745 }
746
747 if (options & StackTrace::kScriptNameOrSourceURL) {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000748 Handle<Object> result = GetScriptNameOrSourceURL(script);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000749 CHECK_NOT_EMPTY_HANDLE(this,
750 JSObject::SetLocalPropertyIgnoreAttributes(
751 stack_frame, script_name_or_source_url_key,
752 result, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000753 }
754
755 if (options & StackTrace::kFunctionName) {
756 Handle<Object> fun_name(fun->shared()->name(), this);
svenpanne@chromium.org9faefa42013-03-08 13:13:16 +0000757 if (!fun_name->BooleanValue()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000758 fun_name = Handle<Object>(fun->shared()->inferred_name(), this);
759 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000760 CHECK_NOT_EMPTY_HANDLE(this,
761 JSObject::SetLocalPropertyIgnoreAttributes(
762 stack_frame, function_key, fun_name, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000763 }
764
765 if (options & StackTrace::kIsEval) {
766 int type = Smi::cast(script->compilation_type())->value();
767 Handle<Object> is_eval = (type == Script::COMPILATION_TYPE_EVAL) ?
768 factory()->true_value() : factory()->false_value();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000769 CHECK_NOT_EMPTY_HANDLE(this,
770 JSObject::SetLocalPropertyIgnoreAttributes(
771 stack_frame, eval_key, is_eval, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000772 }
773
774 if (options & StackTrace::kIsConstructor) {
775 Handle<Object> is_constructor = (frames[i].is_constructor()) ?
776 factory()->true_value() : factory()->false_value();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000777 CHECK_NOT_EMPTY_HANDLE(this,
778 JSObject::SetLocalPropertyIgnoreAttributes(
779 stack_frame, constructor_key,
780 is_constructor, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000781 }
782
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000783 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stack_frame);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000784 frames_seen++;
785 }
786 it.Advance();
787 }
788
789 stack_trace->set_length(Smi::FromInt(frames_seen));
790 return stack_trace;
791}
792
793
794void Isolate::PrintStack() {
795 if (stack_trace_nesting_level_ == 0) {
796 stack_trace_nesting_level_++;
797
798 StringAllocator* allocator;
799 if (preallocated_message_space_ == NULL) {
800 allocator = new HeapStringAllocator();
801 } else {
802 allocator = preallocated_message_space_;
803 }
804
805 StringStream::ClearMentionedObjectCache();
806 StringStream accumulator(allocator);
807 incomplete_message_ = &accumulator;
808 PrintStack(&accumulator);
809 accumulator.OutputToStdOut();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000810 InitializeLoggingAndCounters();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000811 accumulator.Log();
812 incomplete_message_ = NULL;
813 stack_trace_nesting_level_ = 0;
814 if (preallocated_message_space_ == NULL) {
815 // Remove the HeapStringAllocator created above.
816 delete allocator;
817 }
818 } else if (stack_trace_nesting_level_ == 1) {
819 stack_trace_nesting_level_++;
820 OS::PrintError(
821 "\n\nAttempt to print stack while printing stack (double fault)\n");
822 OS::PrintError(
823 "If you are lucky you may find a partial stack dump on stdout.\n\n");
824 incomplete_message_->OutputToStdOut();
825 }
826}
827
828
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000829static void PrintFrames(Isolate* isolate,
830 StringStream* accumulator,
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000831 StackFrame::PrintMode mode) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000832 StackFrameIterator it(isolate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000833 for (int i = 0; !it.done(); it.Advance()) {
834 it.frame()->Print(accumulator, mode, i++);
835 }
836}
837
838
839void Isolate::PrintStack(StringStream* accumulator) {
840 if (!IsInitialized()) {
841 accumulator->Add(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000842 "\n==== JS stack trace is not available =======================\n\n");
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000843 accumulator->Add(
844 "\n==== Isolate for the thread is not initialized =============\n\n");
845 return;
846 }
847 // The MentionedObjectCache is not GC-proof at the moment.
848 AssertNoAllocation nogc;
849 ASSERT(StringStream::IsMentionedObjectCacheClear());
850
851 // Avoid printing anything if there are no frames.
852 if (c_entry_fp(thread_local_top()) == 0) return;
853
854 accumulator->Add(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000855 "\n==== JS stack trace =========================================\n\n");
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000856 PrintFrames(this, accumulator, StackFrame::OVERVIEW);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000857
858 accumulator->Add(
859 "\n==== Details ================================================\n\n");
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000860 PrintFrames(this, accumulator, StackFrame::DETAILS);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000861
862 accumulator->PrintMentionedObjectCache();
863 accumulator->Add("=====================\n\n");
864}
865
866
867void Isolate::SetFailedAccessCheckCallback(
868 v8::FailedAccessCheckCallback callback) {
869 thread_local_top()->failed_access_check_callback_ = callback;
870}
871
872
873void Isolate::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) {
874 if (!thread_local_top()->failed_access_check_callback_) return;
875
876 ASSERT(receiver->IsAccessCheckNeeded());
877 ASSERT(context());
878
879 // Get the data object from access check info.
880 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
881 if (!constructor->shared()->IsApiFunction()) return;
882 Object* data_obj =
883 constructor->shared()->get_api_func_data()->access_check_info();
884 if (data_obj == heap_.undefined_value()) return;
885
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000886 HandleScope scope(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000887 Handle<JSObject> receiver_handle(receiver);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000888 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000889 { VMState state(this, EXTERNAL);
890 thread_local_top()->failed_access_check_callback_(
891 v8::Utils::ToLocal(receiver_handle),
892 type,
893 v8::Utils::ToLocal(data));
894 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000895}
896
897
898enum MayAccessDecision {
899 YES, NO, UNKNOWN
900};
901
902
903static MayAccessDecision MayAccessPreCheck(Isolate* isolate,
904 JSObject* receiver,
905 v8::AccessType type) {
906 // During bootstrapping, callback functions are not enabled yet.
907 if (isolate->bootstrapper()->IsActive()) return YES;
908
909 if (receiver->IsJSGlobalProxy()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000910 Object* receiver_context = JSGlobalProxy::cast(receiver)->native_context();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000911 if (!receiver_context->IsContext()) return NO;
912
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000913 // Get the native context of current top context.
914 // avoid using Isolate::native_context() because it uses Handle.
915 Context* native_context =
916 isolate->context()->global_object()->native_context();
917 if (receiver_context == native_context) return YES;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000918
919 if (Context::cast(receiver_context)->security_token() ==
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000920 native_context->security_token())
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000921 return YES;
922 }
923
924 return UNKNOWN;
925}
926
927
928bool Isolate::MayNamedAccess(JSObject* receiver, Object* key,
929 v8::AccessType type) {
930 ASSERT(receiver->IsAccessCheckNeeded());
931
932 // The callers of this method are not expecting a GC.
933 AssertNoAllocation no_gc;
934
935 // Skip checks for hidden properties access. Note, we do not
936 // require existence of a context in this case.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000937 if (key == heap_.hidden_string()) return true;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000938
939 // Check for compatibility between the security tokens in the
940 // current lexical context and the accessed object.
941 ASSERT(context());
942
943 MayAccessDecision decision = MayAccessPreCheck(this, receiver, type);
944 if (decision != UNKNOWN) return decision == YES;
945
946 // Get named access check callback
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000947 // TODO(dcarney): revert
948 Map* map = receiver->map();
949 CHECK(map->IsMap());
950 CHECK(map->constructor()->IsJSFunction());
951 JSFunction* constructor = JSFunction::cast(map->constructor());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000952 if (!constructor->shared()->IsApiFunction()) return false;
953
954 Object* data_obj =
955 constructor->shared()->get_api_func_data()->access_check_info();
956 if (data_obj == heap_.undefined_value()) return false;
957
958 Object* fun_obj = AccessCheckInfo::cast(data_obj)->named_callback();
959 v8::NamedSecurityCallback callback =
960 v8::ToCData<v8::NamedSecurityCallback>(fun_obj);
961
962 if (!callback) return false;
963
964 HandleScope scope(this);
965 Handle<JSObject> receiver_handle(receiver, this);
966 Handle<Object> key_handle(key, this);
967 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
968 LOG(this, ApiNamedSecurityCheck(key));
969 bool result = false;
970 {
971 // Leaving JavaScript.
972 VMState state(this, EXTERNAL);
973 result = callback(v8::Utils::ToLocal(receiver_handle),
974 v8::Utils::ToLocal(key_handle),
975 type,
976 v8::Utils::ToLocal(data));
977 }
978 return result;
979}
980
981
982bool Isolate::MayIndexedAccess(JSObject* receiver,
983 uint32_t index,
984 v8::AccessType type) {
985 ASSERT(receiver->IsAccessCheckNeeded());
986 // Check for compatibility between the security tokens in the
987 // current lexical context and the accessed object.
988 ASSERT(context());
989
990 MayAccessDecision decision = MayAccessPreCheck(this, receiver, type);
991 if (decision != UNKNOWN) return decision == YES;
992
993 // Get indexed access check callback
994 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
995 if (!constructor->shared()->IsApiFunction()) return false;
996
997 Object* data_obj =
998 constructor->shared()->get_api_func_data()->access_check_info();
999 if (data_obj == heap_.undefined_value()) return false;
1000
1001 Object* fun_obj = AccessCheckInfo::cast(data_obj)->indexed_callback();
1002 v8::IndexedSecurityCallback callback =
1003 v8::ToCData<v8::IndexedSecurityCallback>(fun_obj);
1004
1005 if (!callback) return false;
1006
1007 HandleScope scope(this);
1008 Handle<JSObject> receiver_handle(receiver, this);
1009 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
1010 LOG(this, ApiIndexedSecurityCheck(index));
1011 bool result = false;
1012 {
1013 // Leaving JavaScript.
1014 VMState state(this, EXTERNAL);
1015 result = callback(v8::Utils::ToLocal(receiver_handle),
1016 index,
1017 type,
1018 v8::Utils::ToLocal(data));
1019 }
1020 return result;
1021}
1022
1023
1024const char* const Isolate::kStackOverflowMessage =
1025 "Uncaught RangeError: Maximum call stack size exceeded";
1026
1027
1028Failure* Isolate::StackOverflow() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001029 HandleScope scope(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001030 // At this point we cannot create an Error object using its javascript
1031 // constructor. Instead, we copy the pre-constructed boilerplate and
1032 // attach the stack trace as a hidden property.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001033 Handle<String> key = factory()->stack_overflow_string();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001034 Handle<JSObject> boilerplate =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001035 Handle<JSObject>::cast(GetProperty(this, js_builtins_object(), key));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001036 Handle<JSObject> exception = Copy(boilerplate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001037 DoThrow(*exception, NULL);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001038
1039 // Get stack trace limit.
1040 Handle<Object> error = GetProperty(js_builtins_object(), "$Error");
1041 if (!error->IsJSObject()) return Failure::Exception();
1042 Handle<Object> stack_trace_limit =
1043 GetProperty(Handle<JSObject>::cast(error), "stackTraceLimit");
1044 if (!stack_trace_limit->IsNumber()) return Failure::Exception();
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001045 double dlimit = stack_trace_limit->Number();
1046 int limit = isnan(dlimit) ? 0 : static_cast<int>(dlimit);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001047
1048 Handle<JSArray> stack_trace = CaptureSimpleStackTrace(
1049 exception, factory()->undefined_value(), limit);
1050 JSObject::SetHiddenProperty(exception,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001051 factory()->hidden_stack_trace_string(),
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001052 stack_trace);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001053 return Failure::Exception();
1054}
1055
1056
1057Failure* Isolate::TerminateExecution() {
1058 DoThrow(heap_.termination_exception(), NULL);
1059 return Failure::Exception();
1060}
1061
1062
1063Failure* Isolate::Throw(Object* exception, MessageLocation* location) {
1064 DoThrow(exception, location);
1065 return Failure::Exception();
1066}
1067
1068
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001069Failure* Isolate::ReThrow(MaybeObject* exception) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001070 bool can_be_caught_externally = false;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001071 bool catchable_by_javascript = is_catchable_by_javascript(exception);
1072 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
1073
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001074 thread_local_top()->catcher_ = can_be_caught_externally ?
1075 try_catch_handler() : NULL;
1076
1077 // Set the exception being re-thrown.
1078 set_pending_exception(exception);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001079 if (exception->IsFailure()) return exception->ToFailureUnchecked();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001080 return Failure::Exception();
1081}
1082
1083
1084Failure* Isolate::ThrowIllegalOperation() {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001085 return Throw(heap_.illegal_access_string());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001086}
1087
1088
1089void Isolate::ScheduleThrow(Object* exception) {
1090 // When scheduling a throw we first throw the exception to get the
1091 // error reporting if it is uncaught before rescheduling it.
1092 Throw(exception);
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001093 PropagatePendingExceptionToExternalTryCatch();
1094 if (has_pending_exception()) {
1095 thread_local_top()->scheduled_exception_ = pending_exception();
1096 thread_local_top()->external_caught_exception_ = false;
1097 clear_pending_exception();
1098 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001099}
1100
1101
1102Failure* Isolate::PromoteScheduledException() {
1103 MaybeObject* thrown = scheduled_exception();
1104 clear_scheduled_exception();
1105 // Re-throw the exception to avoid getting repeated error reporting.
1106 return ReThrow(thrown);
1107}
1108
1109
1110void Isolate::PrintCurrentStackTrace(FILE* out) {
1111 StackTraceFrameIterator it(this);
1112 while (!it.done()) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001113 HandleScope scope(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001114 // Find code position if recorded in relocation info.
1115 JavaScriptFrame* frame = it.frame();
1116 int pos = frame->LookupCode()->SourcePosition(frame->pc());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001117 Handle<Object> pos_obj(Smi::FromInt(pos), this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001118 // Fetch function and receiver.
1119 Handle<JSFunction> fun(JSFunction::cast(frame->function()));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001120 Handle<Object> recv(frame->receiver(), this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001121 // Advance to the next JavaScript frame and determine if the
1122 // current frame is the top-level frame.
1123 it.Advance();
1124 Handle<Object> is_top_level = it.done()
1125 ? factory()->true_value()
1126 : factory()->false_value();
1127 // Generate and print stack trace line.
1128 Handle<String> line =
1129 Execution::GetStackTraceLine(recv, fun, pos_obj, is_top_level);
1130 if (line->length() > 0) {
1131 line->PrintOn(out);
1132 fprintf(out, "\n");
1133 }
1134 }
1135}
1136
1137
1138void Isolate::ComputeLocation(MessageLocation* target) {
1139 *target = MessageLocation(Handle<Script>(heap_.empty_script()), -1, -1);
1140 StackTraceFrameIterator it(this);
1141 if (!it.done()) {
1142 JavaScriptFrame* frame = it.frame();
1143 JSFunction* fun = JSFunction::cast(frame->function());
1144 Object* script = fun->shared()->script();
1145 if (script->IsScript() &&
1146 !(Script::cast(script)->source()->IsUndefined())) {
1147 int pos = frame->LookupCode()->SourcePosition(frame->pc());
1148 // Compute the location from the function and the reloc info.
1149 Handle<Script> casted_script(Script::cast(script));
1150 *target = MessageLocation(casted_script, pos, pos + 1);
1151 }
1152 }
1153}
1154
1155
1156bool Isolate::ShouldReportException(bool* can_be_caught_externally,
1157 bool catchable_by_javascript) {
1158 // Find the top-most try-catch handler.
1159 StackHandler* handler =
1160 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001161 while (handler != NULL && !handler->is_catch()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001162 handler = handler->next();
1163 }
1164
1165 // Get the address of the external handler so we can compare the address to
1166 // determine which one is closer to the top of the stack.
1167 Address external_handler_address =
1168 thread_local_top()->try_catch_handler_address();
1169
1170 // The exception has been externally caught if and only if there is
1171 // an external handler which is on top of the top-most try-catch
1172 // handler.
1173 *can_be_caught_externally = external_handler_address != NULL &&
1174 (handler == NULL || handler->address() > external_handler_address ||
1175 !catchable_by_javascript);
1176
1177 if (*can_be_caught_externally) {
1178 // Only report the exception if the external handler is verbose.
1179 return try_catch_handler()->is_verbose_;
1180 } else {
1181 // Report the exception if it isn't caught by JavaScript code.
1182 return handler == NULL;
1183 }
1184}
1185
1186
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001187bool Isolate::IsErrorObject(Handle<Object> obj) {
1188 if (!obj->IsJSObject()) return false;
1189
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001190 String* error_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001191 *(factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("$Error")));
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001192 Object* error_constructor =
1193 js_builtins_object()->GetPropertyNoExceptionThrown(error_key);
1194
1195 for (Object* prototype = *obj; !prototype->IsNull();
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001196 prototype = prototype->GetPrototype(this)) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001197 if (!prototype->IsJSObject()) return false;
1198 if (JSObject::cast(prototype)->map()->constructor() == error_constructor) {
1199 return true;
1200 }
1201 }
1202 return false;
1203}
1204
1205
1206void Isolate::DoThrow(Object* exception, MessageLocation* location) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001207 ASSERT(!has_pending_exception());
1208
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001209 HandleScope scope(this);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001210 Handle<Object> exception_handle(exception, this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001211
1212 // Determine reporting and whether the exception is caught externally.
1213 bool catchable_by_javascript = is_catchable_by_javascript(exception);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001214 bool can_be_caught_externally = false;
1215 bool should_report_exception =
1216 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
1217 bool report_exception = catchable_by_javascript && should_report_exception;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001218 bool try_catch_needs_message =
1219 can_be_caught_externally && try_catch_handler()->capture_message_;
1220 bool bootstrapping = bootstrapper()->IsActive();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001221
1222#ifdef ENABLE_DEBUGGER_SUPPORT
1223 // Notify debugger of exception.
1224 if (catchable_by_javascript) {
1225 debugger_->OnException(exception_handle, report_exception);
1226 }
1227#endif
1228
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001229 // Generate the message if required.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001230 if (report_exception || try_catch_needs_message) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001231 MessageLocation potential_computed_location;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001232 if (location == NULL) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001233 // If no location was specified we use a computed one instead.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001234 ComputeLocation(&potential_computed_location);
1235 location = &potential_computed_location;
1236 }
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001237 // It's not safe to try to make message objects or collect stack traces
1238 // while the bootstrapper is active since the infrastructure may not have
1239 // been properly initialized.
1240 if (!bootstrapping) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001241 Handle<String> stack_trace;
1242 if (FLAG_trace_exception) stack_trace = StackTraceString();
1243 Handle<JSArray> stack_trace_object;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001244 if (capture_stack_trace_for_uncaught_exceptions_) {
1245 if (IsErrorObject(exception_handle)) {
1246 // We fetch the stack trace that corresponds to this error object.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001247 String* key = heap()->hidden_stack_trace_string();
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001248 Object* stack_property =
1249 JSObject::cast(*exception_handle)->GetHiddenProperty(key);
1250 // Property lookup may have failed. In this case it's probably not
1251 // a valid Error object.
1252 if (stack_property->IsJSArray()) {
1253 stack_trace_object = Handle<JSArray>(JSArray::cast(stack_property));
1254 }
1255 }
1256 if (stack_trace_object.is_null()) {
1257 // Not an error object, we capture at throw site.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001258 stack_trace_object = CaptureCurrentStackTrace(
1259 stack_trace_for_uncaught_exceptions_frame_limit_,
1260 stack_trace_for_uncaught_exceptions_options_);
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001261 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001262 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001263
1264 Handle<Object> exception_arg = exception_handle;
1265 // If the exception argument is a custom object, turn it into a string
1266 // before throwing as uncaught exception. Note that the pending
1267 // exception object to be set later must not be turned into a string.
1268 if (exception_arg->IsJSObject() && !IsErrorObject(exception_arg)) {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001269 bool failed = false;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001270 exception_arg = Execution::ToDetailString(exception_arg, &failed);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001271 if (failed) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001272 exception_arg = factory()->InternalizeOneByteString(
1273 STATIC_ASCII_VECTOR("exception"));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001274 }
1275 }
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001276 Handle<Object> message_obj = MessageHandler::MakeMessageObject(
1277 "uncaught_exception",
1278 location,
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001279 HandleVector<Object>(&exception_arg, 1),
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001280 stack_trace,
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001281 stack_trace_object);
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001282 thread_local_top()->pending_message_obj_ = *message_obj;
1283 if (location != NULL) {
1284 thread_local_top()->pending_message_script_ = *location->script();
1285 thread_local_top()->pending_message_start_pos_ = location->start_pos();
1286 thread_local_top()->pending_message_end_pos_ = location->end_pos();
1287 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001288 } else if (location != NULL && !location->script().is_null()) {
1289 // We are bootstrapping and caught an error where the location is set
1290 // and we have a script for the location.
1291 // In this case we could have an extension (or an internal error
1292 // somewhere) and we print out the line number at which the error occured
1293 // to the console for easier debugging.
1294 int line_number = GetScriptLineNumberSafe(location->script(),
1295 location->start_pos());
verwaest@chromium.org37141392012-05-31 13:27:02 +00001296 if (exception->IsString()) {
1297 OS::PrintError(
1298 "Extension or internal compilation error: %s in %s at line %d.\n",
1299 *String::cast(exception)->ToCString(),
1300 *String::cast(location->script()->name())->ToCString(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001301 line_number + 1);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001302 } else {
1303 OS::PrintError(
1304 "Extension or internal compilation error in %s at line %d.\n",
1305 *String::cast(location->script()->name())->ToCString(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001306 line_number + 1);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001307 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001308 }
1309 }
1310
1311 // Save the message for reporting if the the exception remains uncaught.
1312 thread_local_top()->has_pending_message_ = report_exception;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001313
1314 // Do not forget to clean catcher_ if currently thrown exception cannot
1315 // be caught. If necessary, ReThrow will update the catcher.
1316 thread_local_top()->catcher_ = can_be_caught_externally ?
1317 try_catch_handler() : NULL;
1318
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001319 set_pending_exception(*exception_handle);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001320}
1321
1322
1323bool Isolate::IsExternallyCaught() {
1324 ASSERT(has_pending_exception());
1325
1326 if ((thread_local_top()->catcher_ == NULL) ||
1327 (try_catch_handler() != thread_local_top()->catcher_)) {
1328 // When throwing the exception, we found no v8::TryCatch
1329 // which should care about this exception.
1330 return false;
1331 }
1332
1333 if (!is_catchable_by_javascript(pending_exception())) {
1334 return true;
1335 }
1336
1337 // Get the address of the external handler so we can compare the address to
1338 // determine which one is closer to the top of the stack.
1339 Address external_handler_address =
1340 thread_local_top()->try_catch_handler_address();
1341 ASSERT(external_handler_address != NULL);
1342
1343 // The exception has been externally caught if and only if there is
1344 // an external handler which is on top of the top-most try-finally
1345 // handler.
1346 // There should be no try-catch blocks as they would prohibit us from
1347 // finding external catcher in the first place (see catcher_ check above).
1348 //
1349 // Note, that finally clause would rethrow an exception unless it's
1350 // aborted by jumps in control flow like return, break, etc. and we'll
1351 // have another chances to set proper v8::TryCatch.
1352 StackHandler* handler =
1353 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
1354 while (handler != NULL && handler->address() < external_handler_address) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001355 ASSERT(!handler->is_catch());
1356 if (handler->is_finally()) return false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001357
1358 handler = handler->next();
1359 }
1360
1361 return true;
1362}
1363
1364
1365void Isolate::ReportPendingMessages() {
1366 ASSERT(has_pending_exception());
1367 PropagatePendingExceptionToExternalTryCatch();
1368
1369 // If the pending exception is OutOfMemoryException set out_of_memory in
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001370 // the native context. Note: We have to mark the native context here
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001371 // since the GenerateThrowOutOfMemory stub cannot make a RuntimeCall to
1372 // set it.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001373 HandleScope scope(this);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001374 if (thread_local_top_.pending_exception_->IsOutOfMemory()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001375 context()->mark_out_of_memory();
1376 } else if (thread_local_top_.pending_exception_ ==
1377 heap()->termination_exception()) {
1378 // Do nothing: if needed, the exception has been already propagated to
1379 // v8::TryCatch.
1380 } else {
1381 if (thread_local_top_.has_pending_message_) {
1382 thread_local_top_.has_pending_message_ = false;
1383 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001384 HandleScope scope(this);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001385 Handle<Object> message_obj(thread_local_top_.pending_message_obj_,
1386 this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001387 if (thread_local_top_.pending_message_script_ != NULL) {
1388 Handle<Script> script(thread_local_top_.pending_message_script_);
1389 int start_pos = thread_local_top_.pending_message_start_pos_;
1390 int end_pos = thread_local_top_.pending_message_end_pos_;
1391 MessageLocation location(script, start_pos, end_pos);
1392 MessageHandler::ReportMessage(this, &location, message_obj);
1393 } else {
1394 MessageHandler::ReportMessage(this, NULL, message_obj);
1395 }
1396 }
1397 }
1398 }
1399 clear_pending_message();
1400}
1401
1402
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001403MessageLocation Isolate::GetMessageLocation() {
1404 ASSERT(has_pending_exception());
1405
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001406 if (!thread_local_top_.pending_exception_->IsOutOfMemory() &&
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001407 thread_local_top_.pending_exception_ != heap()->termination_exception() &&
1408 thread_local_top_.has_pending_message_ &&
1409 !thread_local_top_.pending_message_obj_->IsTheHole() &&
1410 thread_local_top_.pending_message_script_ != NULL) {
1411 Handle<Script> script(thread_local_top_.pending_message_script_);
1412 int start_pos = thread_local_top_.pending_message_start_pos_;
1413 int end_pos = thread_local_top_.pending_message_end_pos_;
1414 return MessageLocation(script, start_pos, end_pos);
1415 }
1416
1417 return MessageLocation();
1418}
1419
1420
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001421void Isolate::TraceException(bool flag) {
1422 FLAG_trace_exception = flag; // TODO(isolates): This is an unfortunate use.
1423}
1424
1425
1426bool Isolate::OptionalRescheduleException(bool is_bottom_call) {
1427 ASSERT(has_pending_exception());
1428 PropagatePendingExceptionToExternalTryCatch();
1429
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001430 // Always reschedule out of memory exceptions.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001431 if (!is_out_of_memory()) {
1432 bool is_termination_exception =
1433 pending_exception() == heap_.termination_exception();
1434
1435 // Do not reschedule the exception if this is the bottom call.
1436 bool clear_exception = is_bottom_call;
1437
1438 if (is_termination_exception) {
1439 if (is_bottom_call) {
1440 thread_local_top()->external_caught_exception_ = false;
1441 clear_pending_exception();
1442 return false;
1443 }
1444 } else if (thread_local_top()->external_caught_exception_) {
1445 // If the exception is externally caught, clear it if there are no
1446 // JavaScript frames on the way to the C++ frame that has the
1447 // external handler.
1448 ASSERT(thread_local_top()->try_catch_handler_address() != NULL);
1449 Address external_handler_address =
1450 thread_local_top()->try_catch_handler_address();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001451 JavaScriptFrameIterator it(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001452 if (it.done() || (it.frame()->sp() > external_handler_address)) {
1453 clear_exception = true;
1454 }
1455 }
1456
1457 // Clear the exception if needed.
1458 if (clear_exception) {
1459 thread_local_top()->external_caught_exception_ = false;
1460 clear_pending_exception();
1461 return false;
1462 }
1463 }
1464
1465 // Reschedule the exception.
1466 thread_local_top()->scheduled_exception_ = pending_exception();
1467 clear_pending_exception();
1468 return true;
1469}
1470
1471
1472void Isolate::SetCaptureStackTraceForUncaughtExceptions(
1473 bool capture,
1474 int frame_limit,
1475 StackTrace::StackTraceOptions options) {
1476 capture_stack_trace_for_uncaught_exceptions_ = capture;
1477 stack_trace_for_uncaught_exceptions_frame_limit_ = frame_limit;
1478 stack_trace_for_uncaught_exceptions_options_ = options;
1479}
1480
1481
1482bool Isolate::is_out_of_memory() {
1483 if (has_pending_exception()) {
1484 MaybeObject* e = pending_exception();
1485 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1486 return true;
1487 }
1488 }
1489 if (has_scheduled_exception()) {
1490 MaybeObject* e = scheduled_exception();
1491 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1492 return true;
1493 }
1494 }
1495 return false;
1496}
1497
1498
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001499Handle<Context> Isolate::native_context() {
1500 GlobalObject* global = thread_local_top()->context_->global_object();
1501 return Handle<Context>(global->native_context());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001502}
1503
1504
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001505Handle<Context> Isolate::global_context() {
1506 GlobalObject* global = thread_local_top()->context_->global_object();
1507 return Handle<Context>(global->global_context());
1508}
1509
1510
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001511Handle<Context> Isolate::GetCallingNativeContext() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001512 JavaScriptFrameIterator it(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001513#ifdef ENABLE_DEBUGGER_SUPPORT
1514 if (debug_->InDebugger()) {
1515 while (!it.done()) {
1516 JavaScriptFrame* frame = it.frame();
1517 Context* context = Context::cast(frame->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001518 if (context->native_context() == *debug_->debug_context()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001519 it.Advance();
1520 } else {
1521 break;
1522 }
1523 }
1524 }
1525#endif // ENABLE_DEBUGGER_SUPPORT
1526 if (it.done()) return Handle<Context>::null();
1527 JavaScriptFrame* frame = it.frame();
1528 Context* context = Context::cast(frame->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001529 return Handle<Context>(context->native_context());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001530}
1531
1532
1533char* Isolate::ArchiveThread(char* to) {
1534 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1535 RuntimeProfiler::IsolateExitedJS(this);
1536 }
1537 memcpy(to, reinterpret_cast<char*>(thread_local_top()),
1538 sizeof(ThreadLocalTop));
1539 InitializeThreadLocal();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001540 clear_pending_exception();
1541 clear_pending_message();
1542 clear_scheduled_exception();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001543 return to + sizeof(ThreadLocalTop);
1544}
1545
1546
1547char* Isolate::RestoreThread(char* from) {
1548 memcpy(reinterpret_cast<char*>(thread_local_top()), from,
1549 sizeof(ThreadLocalTop));
1550 // This might be just paranoia, but it seems to be needed in case a
1551 // thread_local_top_ is restored on a separate OS thread.
1552#ifdef USE_SIMULATOR
1553#ifdef V8_TARGET_ARCH_ARM
1554 thread_local_top()->simulator_ = Simulator::current(this);
1555#elif V8_TARGET_ARCH_MIPS
1556 thread_local_top()->simulator_ = Simulator::current(this);
1557#endif
1558#endif
1559 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1560 RuntimeProfiler::IsolateEnteredJS(this);
1561 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001562 ASSERT(context() == NULL || context()->IsContext());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001563 return from + sizeof(ThreadLocalTop);
1564}
1565
1566
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001567Isolate::ThreadDataTable::ThreadDataTable()
1568 : list_(NULL) {
1569}
1570
1571
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00001572Isolate::ThreadDataTable::~ThreadDataTable() {
1573 // TODO(svenpanne) The assertion below would fire if an embedder does not
1574 // cleanly dispose all Isolates before disposing v8, so we are conservative
1575 // and leave it out for now.
1576 // ASSERT_EQ(NULL, list_);
1577}
1578
1579
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001580Isolate::PerIsolateThreadData*
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001581 Isolate::ThreadDataTable::Lookup(Isolate* isolate,
1582 ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001583 for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) {
1584 if (data->Matches(isolate, thread_id)) return data;
1585 }
1586 return NULL;
1587}
1588
1589
1590void Isolate::ThreadDataTable::Insert(Isolate::PerIsolateThreadData* data) {
1591 if (list_ != NULL) list_->prev_ = data;
1592 data->next_ = list_;
1593 list_ = data;
1594}
1595
1596
1597void Isolate::ThreadDataTable::Remove(PerIsolateThreadData* data) {
1598 if (list_ == data) list_ = data->next_;
1599 if (data->next_ != NULL) data->next_->prev_ = data->prev_;
1600 if (data->prev_ != NULL) data->prev_->next_ = data->next_;
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001601 delete data;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001602}
1603
1604
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001605void Isolate::ThreadDataTable::Remove(Isolate* isolate,
1606 ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001607 PerIsolateThreadData* data = Lookup(isolate, thread_id);
1608 if (data != NULL) {
1609 Remove(data);
1610 }
1611}
1612
1613
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001614void Isolate::ThreadDataTable::RemoveAllThreads(Isolate* isolate) {
1615 PerIsolateThreadData* data = list_;
1616 while (data != NULL) {
1617 PerIsolateThreadData* next = data->next_;
1618 if (data->isolate() == isolate) Remove(data);
1619 data = next;
1620 }
1621}
1622
1623
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001624#ifdef DEBUG
1625#define TRACE_ISOLATE(tag) \
1626 do { \
1627 if (FLAG_trace_isolates) { \
ulan@chromium.org750145a2013-03-07 15:14:13 +00001628 PrintF("Isolate %p (id %d)" #tag "\n", \
1629 reinterpret_cast<void*>(this), id()); \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001630 } \
1631 } while (false)
1632#else
1633#define TRACE_ISOLATE(tag)
1634#endif
1635
1636
1637Isolate::Isolate()
1638 : state_(UNINITIALIZED),
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001639 embedder_data_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001640 entry_stack_(NULL),
1641 stack_trace_nesting_level_(0),
1642 incomplete_message_(NULL),
1643 preallocated_memory_thread_(NULL),
1644 preallocated_message_space_(NULL),
1645 bootstrapper_(NULL),
1646 runtime_profiler_(NULL),
1647 compilation_cache_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001648 counters_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001649 code_range_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001650 // Must be initialized early to allow v8::SetResourceConstraints calls.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001651 break_access_(OS::CreateMutex()),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001652 debugger_initialized_(false),
1653 // Must be initialized early to allow v8::Debug calls.
1654 debugger_access_(OS::CreateMutex()),
1655 logger_(NULL),
1656 stats_table_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001657 stub_cache_(NULL),
1658 deoptimizer_data_(NULL),
1659 capture_stack_trace_for_uncaught_exceptions_(false),
1660 stack_trace_for_uncaught_exceptions_frame_limit_(0),
1661 stack_trace_for_uncaught_exceptions_options_(StackTrace::kOverview),
1662 transcendental_cache_(NULL),
1663 memory_allocator_(NULL),
1664 keyed_lookup_cache_(NULL),
1665 context_slot_cache_(NULL),
1666 descriptor_lookup_cache_(NULL),
1667 handle_scope_implementer_(NULL),
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001668 unicode_cache_(NULL),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001669 runtime_zone_(this),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001670 in_use_list_(0),
1671 free_list_(0),
1672 preallocated_storage_preallocated_(false),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001673 inner_pointer_to_code_cache_(NULL),
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00001674 write_iterator_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001675 global_handles_(NULL),
1676 context_switcher_(NULL),
1677 thread_manager_(NULL),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001678 fp_stubs_generated_(false),
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001679 has_installed_extensions_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001680 string_tracker_(NULL),
1681 regexp_stack_(NULL),
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001682 date_cache_(NULL),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001683 code_stub_interface_descriptors_(NULL),
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001684 context_exit_happened_(false),
1685 deferred_handles_head_(NULL),
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001686 optimizing_compiler_thread_(this),
1687 marking_thread_(NULL),
1688 sweeper_thread_(NULL) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00001689 id_ = NoBarrier_AtomicIncrement(&isolate_counter_, 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001690 TRACE_ISOLATE(constructor);
1691
1692 memset(isolate_addresses_, 0,
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001693 sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001694
1695 heap_.isolate_ = this;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001696 stack_guard_.isolate_ = this;
1697
lrn@chromium.org1c092762011-05-09 09:42:16 +00001698 // ThreadManager is initialized early to support locking an isolate
1699 // before it is entered.
1700 thread_manager_ = new ThreadManager();
1701 thread_manager_->isolate_ = this;
1702
lrn@chromium.org7516f052011-03-30 08:52:27 +00001703#if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \
1704 defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001705 simulator_initialized_ = false;
1706 simulator_i_cache_ = NULL;
1707 simulator_redirection_ = NULL;
1708#endif
1709
1710#ifdef DEBUG
1711 // heap_histograms_ initializes itself.
1712 memset(&js_spill_information_, 0, sizeof(js_spill_information_));
1713 memset(code_kind_statistics_, 0,
1714 sizeof(code_kind_statistics_[0]) * Code::NUMBER_OF_KINDS);
yangguo@chromium.org003650e2013-01-24 16:31:08 +00001715
1716 allow_handle_deref_ = true;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001717#endif
1718
1719#ifdef ENABLE_DEBUGGER_SUPPORT
1720 debug_ = NULL;
1721 debugger_ = NULL;
1722#endif
1723
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001724 handle_scope_data_.Initialize();
1725
1726#define ISOLATE_INIT_EXECUTE(type, name, initial_value) \
1727 name##_ = (initial_value);
1728 ISOLATE_INIT_LIST(ISOLATE_INIT_EXECUTE)
1729#undef ISOLATE_INIT_EXECUTE
1730
1731#define ISOLATE_INIT_ARRAY_EXECUTE(type, name, length) \
1732 memset(name##_, 0, sizeof(type) * length);
1733 ISOLATE_INIT_ARRAY_LIST(ISOLATE_INIT_ARRAY_EXECUTE)
1734#undef ISOLATE_INIT_ARRAY_EXECUTE
1735}
1736
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001737
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001738void Isolate::TearDown() {
1739 TRACE_ISOLATE(tear_down);
1740
1741 // Temporarily set this isolate as current so that various parts of
1742 // the isolate can access it in their destructors without having a
1743 // direct pointer. We don't use Enter/Exit here to avoid
1744 // initializing the thread data.
1745 PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData();
1746 Isolate* saved_isolate = UncheckedCurrent();
1747 SetIsolateThreadLocals(this, NULL);
1748
1749 Deinit();
1750
danno@chromium.org8c0a43f2012-04-03 08:37:53 +00001751 { ScopedLock lock(process_wide_mutex_);
1752 thread_data_table_->RemoveAllThreads(this);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001753 }
1754
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001755 if (serialize_partial_snapshot_cache_ != NULL) {
1756 delete[] serialize_partial_snapshot_cache_;
1757 serialize_partial_snapshot_cache_ = NULL;
1758 }
1759
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001760 if (!IsDefaultIsolate()) {
1761 delete this;
1762 }
1763
1764 // Restore the previous current isolate.
1765 SetIsolateThreadLocals(saved_isolate, saved_data);
1766}
1767
1768
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00001769void Isolate::GlobalTearDown() {
1770 delete thread_data_table_;
1771}
1772
1773
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001774void Isolate::Deinit() {
1775 if (state_ == INITIALIZED) {
1776 TRACE_ISOLATE(deinit);
1777
ulan@chromium.org750145a2013-03-07 15:14:13 +00001778 if (FLAG_parallel_recompilation) optimizing_compiler_thread_.Stop();
1779
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001780 if (FLAG_sweeper_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001781 for (int i = 0; i < FLAG_sweeper_threads; i++) {
1782 sweeper_thread_[i]->Stop();
1783 delete sweeper_thread_[i];
1784 }
1785 delete[] sweeper_thread_;
1786 }
1787
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001788 if (FLAG_marking_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001789 for (int i = 0; i < FLAG_marking_threads; i++) {
1790 marking_thread_[i]->Stop();
1791 delete marking_thread_[i];
1792 }
1793 delete[] marking_thread_;
1794 }
1795
ulan@chromium.org750145a2013-03-07 15:14:13 +00001796 if (FLAG_hydrogen_stats) GetHStatistics()->Print();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001797
1798 // We must stop the logger before we tear down other components.
1799 logger_->EnsureTickerStopped();
1800
1801 delete deoptimizer_data_;
1802 deoptimizer_data_ = NULL;
1803 if (FLAG_preemption) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001804 v8::Locker locker(reinterpret_cast<v8::Isolate*>(this));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001805 v8::Locker::StopPreemption();
1806 }
1807 builtins_.TearDown();
1808 bootstrapper_->TearDown();
1809
1810 // Remove the external reference to the preallocated stack memory.
1811 delete preallocated_message_space_;
1812 preallocated_message_space_ = NULL;
1813 PreallocatedMemoryThreadStop();
1814
1815 HeapProfiler::TearDown();
1816 CpuProfiler::TearDown();
1817 if (runtime_profiler_ != NULL) {
1818 runtime_profiler_->TearDown();
1819 delete runtime_profiler_;
1820 runtime_profiler_ = NULL;
1821 }
1822 heap_.TearDown();
1823 logger_->TearDown();
1824
1825 // The default isolate is re-initializable due to legacy API.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001826 state_ = UNINITIALIZED;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001827 }
1828}
1829
1830
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001831void Isolate::PushToPartialSnapshotCache(Object* obj) {
1832 int length = serialize_partial_snapshot_cache_length();
1833 int capacity = serialize_partial_snapshot_cache_capacity();
1834
1835 if (length >= capacity) {
1836 int new_capacity = static_cast<int>((capacity + 10) * 1.2);
1837 Object** new_array = new Object*[new_capacity];
1838 for (int i = 0; i < length; i++) {
1839 new_array[i] = serialize_partial_snapshot_cache()[i];
1840 }
1841 if (capacity != 0) delete[] serialize_partial_snapshot_cache();
1842 set_serialize_partial_snapshot_cache(new_array);
1843 set_serialize_partial_snapshot_cache_capacity(new_capacity);
1844 }
1845
1846 serialize_partial_snapshot_cache()[length] = obj;
1847 set_serialize_partial_snapshot_cache_length(length + 1);
1848}
1849
1850
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001851void Isolate::SetIsolateThreadLocals(Isolate* isolate,
1852 PerIsolateThreadData* data) {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +00001853 Thread::SetThreadLocal(isolate_key_, isolate);
1854 Thread::SetThreadLocal(per_isolate_thread_data_key_, data);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001855}
1856
1857
1858Isolate::~Isolate() {
1859 TRACE_ISOLATE(destructor);
1860
danno@chromium.orgb6451162011-08-17 14:33:23 +00001861 // Has to be called while counters_ are still alive.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001862 runtime_zone_.DeleteKeptSegment();
danno@chromium.orgb6451162011-08-17 14:33:23 +00001863
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001864 delete[] assembler_spare_buffer_;
1865 assembler_spare_buffer_ = NULL;
1866
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001867 delete unicode_cache_;
1868 unicode_cache_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001869
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001870 delete date_cache_;
1871 date_cache_ = NULL;
1872
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001873 delete[] code_stub_interface_descriptors_;
1874 code_stub_interface_descriptors_ = NULL;
1875
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001876 delete regexp_stack_;
1877 regexp_stack_ = NULL;
1878
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001879 delete descriptor_lookup_cache_;
1880 descriptor_lookup_cache_ = NULL;
1881 delete context_slot_cache_;
1882 context_slot_cache_ = NULL;
1883 delete keyed_lookup_cache_;
1884 keyed_lookup_cache_ = NULL;
1885
1886 delete transcendental_cache_;
1887 transcendental_cache_ = NULL;
1888 delete stub_cache_;
1889 stub_cache_ = NULL;
1890 delete stats_table_;
1891 stats_table_ = NULL;
1892
1893 delete logger_;
1894 logger_ = NULL;
1895
1896 delete counters_;
1897 counters_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001898
1899 delete handle_scope_implementer_;
1900 handle_scope_implementer_ = NULL;
1901 delete break_access_;
1902 break_access_ = NULL;
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001903 delete debugger_access_;
1904 debugger_access_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001905
1906 delete compilation_cache_;
1907 compilation_cache_ = NULL;
1908 delete bootstrapper_;
1909 bootstrapper_ = NULL;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001910 delete inner_pointer_to_code_cache_;
1911 inner_pointer_to_code_cache_ = NULL;
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00001912 delete write_iterator_;
1913 write_iterator_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001914
1915 delete context_switcher_;
1916 context_switcher_ = NULL;
1917 delete thread_manager_;
1918 thread_manager_ = NULL;
1919
1920 delete string_tracker_;
1921 string_tracker_ = NULL;
1922
1923 delete memory_allocator_;
1924 memory_allocator_ = NULL;
1925 delete code_range_;
1926 code_range_ = NULL;
1927 delete global_handles_;
1928 global_handles_ = NULL;
1929
danno@chromium.orgb6451162011-08-17 14:33:23 +00001930 delete external_reference_table_;
1931 external_reference_table_ = NULL;
1932
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001933#ifdef ENABLE_DEBUGGER_SUPPORT
1934 delete debugger_;
1935 debugger_ = NULL;
1936 delete debug_;
1937 debug_ = NULL;
1938#endif
1939}
1940
1941
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001942void Isolate::InitializeThreadLocal() {
lrn@chromium.org1c092762011-05-09 09:42:16 +00001943 thread_local_top_.isolate_ = this;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001944 thread_local_top_.Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001945}
1946
1947
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001948void Isolate::PropagatePendingExceptionToExternalTryCatch() {
1949 ASSERT(has_pending_exception());
1950
1951 bool external_caught = IsExternallyCaught();
1952 thread_local_top_.external_caught_exception_ = external_caught;
1953
1954 if (!external_caught) return;
1955
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001956 if (thread_local_top_.pending_exception_->IsOutOfMemory()) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001957 // Do not propagate OOM exception: we should kill VM asap.
1958 } else if (thread_local_top_.pending_exception_ ==
1959 heap()->termination_exception()) {
1960 try_catch_handler()->can_continue_ = false;
1961 try_catch_handler()->exception_ = heap()->null_value();
1962 } else {
1963 // At this point all non-object (failure) exceptions have
1964 // been dealt with so this shouldn't fail.
1965 ASSERT(!pending_exception()->IsFailure());
1966 try_catch_handler()->can_continue_ = true;
1967 try_catch_handler()->exception_ = pending_exception();
1968 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
1969 try_catch_handler()->message_ = thread_local_top_.pending_message_obj_;
1970 }
1971 }
1972}
1973
1974
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001975void Isolate::InitializeLoggingAndCounters() {
1976 if (logger_ == NULL) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001977 logger_ = new Logger(this);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001978 }
1979 if (counters_ == NULL) {
1980 counters_ = new Counters;
1981 }
1982}
1983
1984
1985void Isolate::InitializeDebugger() {
1986#ifdef ENABLE_DEBUGGER_SUPPORT
1987 ScopedLock lock(debugger_access_);
1988 if (NoBarrier_Load(&debugger_initialized_)) return;
1989 InitializeLoggingAndCounters();
1990 debug_ = new Debug(this);
1991 debugger_ = new Debugger(this);
1992 Release_Store(&debugger_initialized_, true);
1993#endif
1994}
1995
1996
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001997bool Isolate::Init(Deserializer* des) {
1998 ASSERT(state_ != INITIALIZED);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001999 ASSERT(Isolate::Current() == this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002000 TRACE_ISOLATE(init);
2001
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002002 // The initialization process does not handle memory exhaustion.
2003 DisallowAllocationFailure disallow_allocation_failure;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002004
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002005 InitializeLoggingAndCounters();
2006
2007 InitializeDebugger();
2008
2009 memory_allocator_ = new MemoryAllocator(this);
2010 code_range_ = new CodeRange(this);
2011
2012 // Safe after setting Heap::isolate_, initializing StackGuard and
2013 // ensuring that Isolate::Current() == this.
2014 heap_.SetStackLimits();
2015
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00002016#define ASSIGN_ELEMENT(CamelName, hacker_name) \
2017 isolate_addresses_[Isolate::k##CamelName##Address] = \
2018 reinterpret_cast<Address>(hacker_name##_address());
2019 FOR_EACH_ISOLATE_ADDRESS_NAME(ASSIGN_ELEMENT)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002020#undef C
2021
2022 string_tracker_ = new StringTracker();
2023 string_tracker_->isolate_ = this;
2024 compilation_cache_ = new CompilationCache(this);
2025 transcendental_cache_ = new TranscendentalCache();
2026 keyed_lookup_cache_ = new KeyedLookupCache();
2027 context_slot_cache_ = new ContextSlotCache();
2028 descriptor_lookup_cache_ = new DescriptorLookupCache();
2029 unicode_cache_ = new UnicodeCache();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002030 inner_pointer_to_code_cache_ = new InnerPointerToCodeCache(this);
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00002031 write_iterator_ = new ConsStringIteratorOp();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002032 global_handles_ = new GlobalHandles(this);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002033 bootstrapper_ = new Bootstrapper(this);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002034 handle_scope_implementer_ = new HandleScopeImplementer(this);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002035 stub_cache_ = new StubCache(this, runtime_zone());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002036 regexp_stack_ = new RegExpStack();
2037 regexp_stack_->isolate_ = this;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00002038 date_cache_ = new DateCache();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002039 code_stub_interface_descriptors_ =
2040 new CodeStubInterfaceDescriptor[CodeStub::NUMBER_OF_IDS];
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002041
2042 // Enable logging before setting up the heap
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002043 logger_->SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002044
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002045 CpuProfiler::SetUp();
2046 HeapProfiler::SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002047
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002048 // Initialize other runtime facilities
2049#if defined(USE_SIMULATOR)
lrn@chromium.org7516f052011-03-30 08:52:27 +00002050#if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS)
lrn@chromium.org1c092762011-05-09 09:42:16 +00002051 Simulator::Initialize(this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002052#endif
2053#endif
2054
2055 { // NOLINT
2056 // Ensure that the thread has a valid stack guard. The v8::Locker object
2057 // will ensure this too, but we don't have to use lockers if we are only
2058 // using one thread.
2059 ExecutionAccess lock(this);
2060 stack_guard_.InitThread(lock);
2061 }
2062
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002063 // SetUp the object heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002064 ASSERT(!heap_.HasBeenSetUp());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002065 if (!heap_.SetUp()) {
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00002066 V8::FatalProcessOutOfMemory("heap setup");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002067 return false;
2068 }
2069
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002070 deoptimizer_data_ = new DeoptimizerData;
2071
2072 const bool create_heap_objects = (des == NULL);
2073 if (create_heap_objects && !heap_.CreateHeapObjects()) {
2074 V8::FatalProcessOutOfMemory("heap object creation");
2075 return false;
2076 }
2077
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002078 if (create_heap_objects) {
2079 // Terminate the cache array with the sentinel so we can iterate.
2080 PushToPartialSnapshotCache(heap_.undefined_value());
2081 }
2082
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00002083 InitializeThreadLocal();
2084
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002085 bootstrapper_->Initialize(create_heap_objects);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002086 builtins_.SetUp(create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002087
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002088 // Only preallocate on the first initialization.
2089 if (FLAG_preallocate_message_memory && preallocated_message_space_ == NULL) {
2090 // Start the thread which will set aside some memory.
2091 PreallocatedMemoryThreadStart();
2092 preallocated_message_space_ =
2093 new NoAllocationStringAllocator(
2094 preallocated_memory_thread_->data(),
2095 preallocated_memory_thread_->length());
2096 PreallocatedStorageInit(preallocated_memory_thread_->length() / 4);
2097 }
2098
2099 if (FLAG_preemption) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002100 v8::Locker locker(reinterpret_cast<v8::Isolate*>(this));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002101 v8::Locker::StartPreemption(100);
2102 }
2103
2104#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002105 debug_->SetUp(create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002106#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002107
2108 // If we are deserializing, read the state into the now-empty heap.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002109 if (!create_heap_objects) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002110 des->Deserialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002111 }
ulan@chromium.org812308e2012-02-29 15:58:45 +00002112 stub_cache_->Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002113
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002114 // Finish initialization of ThreadLocal after deserialization is done.
2115 clear_pending_exception();
2116 clear_pending_message();
2117 clear_scheduled_exception();
2118
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002119 // Deserializing may put strange things in the root array's copy of the
2120 // stack guard.
2121 heap_.SetStackLimits();
2122
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002123 // Quiet the heap NaN if needed on target platform.
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00002124 if (!create_heap_objects) Assembler::QuietNaN(heap_.nan_value());
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002125
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002126 runtime_profiler_ = new RuntimeProfiler(this);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002127 runtime_profiler_->SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002128
2129 // If we are deserializing, log non-function code objects and compiled
2130 // functions found in the snapshot.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00002131 if (!create_heap_objects &&
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002132 (FLAG_log_code || FLAG_ll_prof || logger_->is_logging_code_events())) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002133 HandleScope scope(this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002134 LOG(this, LogCodeObjects());
2135 LOG(this, LogCompiledFunctions());
2136 }
2137
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002138 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, state_)),
2139 Internals::kIsolateStateOffset);
2140 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, embedder_data_)),
2141 Internals::kIsolateEmbedderDataOffset);
2142 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, heap_.roots_)),
2143 Internals::kIsolateRootsOffset);
2144
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002145 state_ = INITIALIZED;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002146 time_millis_at_init_ = OS::TimeCurrentMillis();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002147
2148 if (!create_heap_objects) {
2149 // Now that the heap is consistent, it's OK to generate the code for the
2150 // deopt entry table that might have been referred to by optimized code in
2151 // the snapshot.
2152 HandleScope scope(this);
2153 Deoptimizer::EnsureCodeForDeoptimizationEntry(
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002154 this,
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002155 Deoptimizer::LAZY,
2156 kDeoptTableSerializeEntryCount - 1);
2157 }
2158
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002159 if (!Serializer::enabled()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002160 // Ensure that all stubs which need to be generated ahead of time, but
2161 // cannot be serialized into the snapshot have been generated.
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002162 HandleScope scope(this);
ulan@chromium.org750145a2013-03-07 15:14:13 +00002163 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(this);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002164 CodeStub::GenerateFPStubs(this);
2165 StubFailureTrampolineStub::GenerateAheadOfTime(this);
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002166 }
2167
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002168 if (FLAG_parallel_recompilation) optimizing_compiler_thread_.Start();
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002169
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002170 if (FLAG_parallel_marking && FLAG_marking_threads == 0) {
2171 FLAG_marking_threads = SystemThreadManager::
2172 NumberOfParallelSystemThreads(
2173 SystemThreadManager::PARALLEL_MARKING);
2174 }
2175 if (FLAG_marking_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002176 marking_thread_ = new MarkingThread*[FLAG_marking_threads];
2177 for (int i = 0; i < FLAG_marking_threads; i++) {
2178 marking_thread_[i] = new MarkingThread(this);
2179 marking_thread_[i]->Start();
2180 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002181 } else {
2182 FLAG_parallel_marking = false;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002183 }
2184
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002185 if (FLAG_sweeper_threads == 0) {
2186 if (FLAG_concurrent_sweeping) {
2187 FLAG_sweeper_threads = SystemThreadManager::
2188 NumberOfParallelSystemThreads(
2189 SystemThreadManager::CONCURRENT_SWEEPING);
2190 } else if (FLAG_parallel_sweeping) {
2191 FLAG_sweeper_threads = SystemThreadManager::
2192 NumberOfParallelSystemThreads(
2193 SystemThreadManager::PARALLEL_SWEEPING);
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002194 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002195 }
2196 if (FLAG_sweeper_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002197 sweeper_thread_ = new SweeperThread*[FLAG_sweeper_threads];
2198 for (int i = 0; i < FLAG_sweeper_threads; i++) {
2199 sweeper_thread_[i] = new SweeperThread(this);
2200 sweeper_thread_[i]->Start();
2201 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002202 } else {
2203 FLAG_concurrent_sweeping = false;
2204 FLAG_parallel_sweeping = false;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002205 }
ulan@chromium.org750145a2013-03-07 15:14:13 +00002206 if (FLAG_parallel_recompilation &&
2207 SystemThreadManager::NumberOfParallelSystemThreads(
2208 SystemThreadManager::PARALLEL_RECOMPILATION) == 0) {
2209 FLAG_parallel_recompilation = false;
2210 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002211 return true;
2212}
2213
2214
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002215// Initialized lazily to allow early
2216// v8::V8::SetAddHistogramSampleFunction calls.
2217StatsTable* Isolate::stats_table() {
2218 if (stats_table_ == NULL) {
2219 stats_table_ = new StatsTable;
2220 }
2221 return stats_table_;
2222}
2223
2224
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002225void Isolate::Enter() {
2226 Isolate* current_isolate = NULL;
2227 PerIsolateThreadData* current_data = CurrentPerIsolateThreadData();
2228 if (current_data != NULL) {
2229 current_isolate = current_data->isolate_;
2230 ASSERT(current_isolate != NULL);
2231 if (current_isolate == this) {
2232 ASSERT(Current() == this);
2233 ASSERT(entry_stack_ != NULL);
2234 ASSERT(entry_stack_->previous_thread_data == NULL ||
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00002235 entry_stack_->previous_thread_data->thread_id().Equals(
2236 ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002237 // Same thread re-enters the isolate, no need to re-init anything.
2238 entry_stack_->entry_count++;
2239 return;
2240 }
2241 }
2242
2243 // Threads can have default isolate set into TLS as Current but not yet have
2244 // PerIsolateThreadData for it, as it requires more advanced phase of the
2245 // initialization. For example, a thread might be the one that system used for
2246 // static initializers - in this case the default isolate is set in TLS but
2247 // the thread did not yet Enter the isolate. If PerisolateThreadData is not
2248 // there, use the isolate set in TLS.
2249 if (current_isolate == NULL) {
2250 current_isolate = Isolate::UncheckedCurrent();
2251 }
2252
2253 PerIsolateThreadData* data = FindOrAllocatePerThreadDataForThisThread();
2254 ASSERT(data != NULL);
2255 ASSERT(data->isolate_ == this);
2256
2257 EntryStackItem* item = new EntryStackItem(current_data,
2258 current_isolate,
2259 entry_stack_);
2260 entry_stack_ = item;
2261
2262 SetIsolateThreadLocals(this, data);
2263
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002264 // In case it's the first time some thread enters the isolate.
2265 set_thread_id(data->thread_id());
2266}
2267
2268
2269void Isolate::Exit() {
2270 ASSERT(entry_stack_ != NULL);
2271 ASSERT(entry_stack_->previous_thread_data == NULL ||
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00002272 entry_stack_->previous_thread_data->thread_id().Equals(
2273 ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002274
2275 if (--entry_stack_->entry_count > 0) return;
2276
2277 ASSERT(CurrentPerIsolateThreadData() != NULL);
2278 ASSERT(CurrentPerIsolateThreadData()->isolate_ == this);
2279
2280 // Pop the stack.
2281 EntryStackItem* item = entry_stack_;
2282 entry_stack_ = item->previous_item;
2283
2284 PerIsolateThreadData* previous_thread_data = item->previous_thread_data;
2285 Isolate* previous_isolate = item->previous_isolate;
2286
2287 delete item;
2288
2289 // Reinit the current thread for the isolate it was running before this one.
2290 SetIsolateThreadLocals(previous_isolate, previous_thread_data);
2291}
2292
2293
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002294void Isolate::LinkDeferredHandles(DeferredHandles* deferred) {
2295 deferred->next_ = deferred_handles_head_;
2296 if (deferred_handles_head_ != NULL) {
2297 deferred_handles_head_->previous_ = deferred;
2298 }
2299 deferred_handles_head_ = deferred;
2300}
2301
2302
2303void Isolate::UnlinkDeferredHandles(DeferredHandles* deferred) {
2304#ifdef DEBUG
2305 // In debug mode assert that the linked list is well-formed.
2306 DeferredHandles* deferred_iterator = deferred;
2307 while (deferred_iterator->previous_ != NULL) {
2308 deferred_iterator = deferred_iterator->previous_;
2309 }
2310 ASSERT(deferred_handles_head_ == deferred_iterator);
2311#endif
2312 if (deferred_handles_head_ == deferred) {
2313 deferred_handles_head_ = deferred_handles_head_->next_;
2314 }
2315 if (deferred->next_ != NULL) {
2316 deferred->next_->previous_ = deferred->previous_;
2317 }
2318 if (deferred->previous_ != NULL) {
2319 deferred->previous_->next_ = deferred->next_;
2320 }
2321}
2322
2323
ulan@chromium.org750145a2013-03-07 15:14:13 +00002324HStatistics* Isolate::GetHStatistics() {
2325 if (hstatistics() == NULL) set_hstatistics(new HStatistics());
2326 return hstatistics();
2327}
2328
2329
2330HTracer* Isolate::GetHTracer() {
2331 if (htracer() == NULL) set_htracer(new HTracer(id()));
2332 return htracer();
2333}
2334
2335
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002336CodeStubInterfaceDescriptor*
2337 Isolate::code_stub_interface_descriptor(int index) {
2338 return code_stub_interface_descriptors_ + index;
2339}
2340
2341
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002342#ifdef DEBUG
2343#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
2344const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_);
2345ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
2346ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
2347#undef ISOLATE_FIELD_OFFSET
2348#endif
2349
2350} } // namespace v8::internal