blob: 7d31029a96da23db975c40909390806eefa33d86 [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"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000043#include "messages.h"
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000044#include "platform.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000045#include "regexp-stack.h"
46#include "runtime-profiler.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000047#include "scopeinfo.h"
48#include "serialize.h"
49#include "simulator.h"
50#include "spaces.h"
51#include "stub-cache.h"
52#include "version.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000053#include "vm-state-inl.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054
55
56namespace v8 {
57namespace internal {
58
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000059Atomic32 ThreadId::highest_thread_id_ = 0;
60
61int ThreadId::AllocateThreadId() {
62 int new_id = NoBarrier_AtomicIncrement(&highest_thread_id_, 1);
63 return new_id;
64}
65
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000066
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000067int ThreadId::GetCurrentThreadId() {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000068 int thread_id = Thread::GetThreadLocalInt(Isolate::thread_id_key_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000069 if (thread_id == 0) {
70 thread_id = AllocateThreadId();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000071 Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000072 }
73 return thread_id;
74}
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000075
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000076
77ThreadLocalTop::ThreadLocalTop() {
78 InitializeInternal();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000079 // This flag may be set using v8::V8::IgnoreOutOfMemoryException()
80 // before an isolate is initialized. The initialize methods below do
81 // not touch it to preserve its value.
82 ignore_out_of_memory_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000083}
84
85
86void ThreadLocalTop::InitializeInternal() {
87 c_entry_fp_ = 0;
88 handler_ = 0;
89#ifdef USE_SIMULATOR
90 simulator_ = NULL;
91#endif
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000092 js_entry_sp_ = NULL;
93 external_callback_ = NULL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000094 current_vm_state_ = EXTERNAL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000095 try_catch_handler_address_ = NULL;
96 context_ = NULL;
97 thread_id_ = ThreadId::Invalid();
98 external_caught_exception_ = false;
99 failed_access_check_callback_ = NULL;
100 save_context_ = NULL;
101 catcher_ = NULL;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000102 top_lookup_result_ = NULL;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000103
104 // These members are re-initialized later after deserialization
105 // is complete.
106 pending_exception_ = NULL;
107 has_pending_message_ = false;
108 pending_message_obj_ = NULL;
109 pending_message_script_ = NULL;
110 scheduled_exception_ = NULL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000111}
112
113
114void ThreadLocalTop::Initialize() {
115 InitializeInternal();
116#ifdef USE_SIMULATOR
117#ifdef V8_TARGET_ARCH_ARM
118 simulator_ = Simulator::current(isolate_);
119#elif V8_TARGET_ARCH_MIPS
120 simulator_ = Simulator::current(isolate_);
121#endif
122#endif
123 thread_id_ = ThreadId::Current();
124}
125
126
127v8::TryCatch* ThreadLocalTop::TryCatchHandler() {
128 return TRY_CATCH_FROM_ADDRESS(try_catch_handler_address());
129}
130
131
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000132// Create a dummy thread that will wait forever on a semaphore. The only
133// purpose for this thread is to have some stack area to save essential data
134// into for use by a stacks only core dump (aka minidump).
135class PreallocatedMemoryThread: public Thread {
136 public:
137 char* data() {
138 if (data_ready_semaphore_ != NULL) {
139 // Initial access is guarded until the data has been published.
140 data_ready_semaphore_->Wait();
141 delete data_ready_semaphore_;
142 data_ready_semaphore_ = NULL;
143 }
144 return data_;
145 }
146
147 unsigned length() {
148 if (data_ready_semaphore_ != NULL) {
149 // Initial access is guarded until the data has been published.
150 data_ready_semaphore_->Wait();
151 delete data_ready_semaphore_;
152 data_ready_semaphore_ = NULL;
153 }
154 return length_;
155 }
156
157 // Stop the PreallocatedMemoryThread and release its resources.
158 void StopThread() {
159 keep_running_ = false;
160 wait_for_ever_semaphore_->Signal();
161
162 // Wait for the thread to terminate.
163 Join();
164
165 if (data_ready_semaphore_ != NULL) {
166 delete data_ready_semaphore_;
167 data_ready_semaphore_ = NULL;
168 }
169
170 delete wait_for_ever_semaphore_;
171 wait_for_ever_semaphore_ = NULL;
172 }
173
174 protected:
175 // When the thread starts running it will allocate a fixed number of bytes
176 // on the stack and publish the location of this memory for others to use.
177 void Run() {
178 EmbeddedVector<char, 15 * 1024> local_buffer;
179
180 // Initialize the buffer with a known good value.
181 OS::StrNCpy(local_buffer, "Trace data was not generated.\n",
182 local_buffer.length());
183
184 // Publish the local buffer and signal its availability.
185 data_ = local_buffer.start();
186 length_ = local_buffer.length();
187 data_ready_semaphore_->Signal();
188
189 while (keep_running_) {
190 // This thread will wait here until the end of time.
191 wait_for_ever_semaphore_->Wait();
192 }
193
194 // Make sure we access the buffer after the wait to remove all possibility
195 // of it being optimized away.
196 OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n",
197 local_buffer.length());
198 }
199
200
201 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000202 PreallocatedMemoryThread()
203 : Thread("v8:PreallocMem"),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000204 keep_running_(true),
205 wait_for_ever_semaphore_(OS::CreateSemaphore(0)),
206 data_ready_semaphore_(OS::CreateSemaphore(0)),
207 data_(NULL),
208 length_(0) {
209 }
210
211 // Used to make sure that the thread keeps looping even for spurious wakeups.
212 bool keep_running_;
213
214 // This semaphore is used by the PreallocatedMemoryThread to wait for ever.
215 Semaphore* wait_for_ever_semaphore_;
216 // Semaphore to signal that the data has been initialized.
217 Semaphore* data_ready_semaphore_;
218
219 // Location and size of the preallocated memory block.
220 char* data_;
221 unsigned length_;
222
223 friend class Isolate;
224
225 DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread);
226};
227
228
229void Isolate::PreallocatedMemoryThreadStart() {
230 if (preallocated_memory_thread_ != NULL) return;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000231 preallocated_memory_thread_ = new PreallocatedMemoryThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000232 preallocated_memory_thread_->Start();
233}
234
235
236void Isolate::PreallocatedMemoryThreadStop() {
237 if (preallocated_memory_thread_ == NULL) return;
238 preallocated_memory_thread_->StopThread();
239 // Done with the thread entirely.
240 delete preallocated_memory_thread_;
241 preallocated_memory_thread_ = NULL;
242}
243
244
lrn@chromium.org7516f052011-03-30 08:52:27 +0000245void Isolate::PreallocatedStorageInit(size_t size) {
246 ASSERT(free_list_.next_ == &free_list_);
247 ASSERT(free_list_.previous_ == &free_list_);
248 PreallocatedStorage* free_chunk =
249 reinterpret_cast<PreallocatedStorage*>(new char[size]);
250 free_list_.next_ = free_list_.previous_ = free_chunk;
251 free_chunk->next_ = free_chunk->previous_ = &free_list_;
252 free_chunk->size_ = size - sizeof(PreallocatedStorage);
253 preallocated_storage_preallocated_ = true;
254}
255
256
257void* Isolate::PreallocatedStorageNew(size_t size) {
258 if (!preallocated_storage_preallocated_) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000259 return FreeStoreAllocationPolicy().New(size);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000260 }
261 ASSERT(free_list_.next_ != &free_list_);
262 ASSERT(free_list_.previous_ != &free_list_);
263
264 size = (size + kPointerSize - 1) & ~(kPointerSize - 1);
265 // Search for exact fit.
266 for (PreallocatedStorage* storage = free_list_.next_;
267 storage != &free_list_;
268 storage = storage->next_) {
269 if (storage->size_ == size) {
270 storage->Unlink();
271 storage->LinkTo(&in_use_list_);
272 return reinterpret_cast<void*>(storage + 1);
273 }
274 }
275 // Search for first fit.
276 for (PreallocatedStorage* storage = free_list_.next_;
277 storage != &free_list_;
278 storage = storage->next_) {
279 if (storage->size_ >= size + sizeof(PreallocatedStorage)) {
280 storage->Unlink();
281 storage->LinkTo(&in_use_list_);
282 PreallocatedStorage* left_over =
283 reinterpret_cast<PreallocatedStorage*>(
284 reinterpret_cast<char*>(storage + 1) + size);
285 left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage);
286 ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) ==
287 storage->size_);
288 storage->size_ = size;
289 left_over->LinkTo(&free_list_);
290 return reinterpret_cast<void*>(storage + 1);
291 }
292 }
293 // Allocation failure.
294 ASSERT(false);
295 return NULL;
296}
297
298
299// We don't attempt to coalesce.
300void Isolate::PreallocatedStorageDelete(void* p) {
301 if (p == NULL) {
302 return;
303 }
304 if (!preallocated_storage_preallocated_) {
305 FreeStoreAllocationPolicy::Delete(p);
306 return;
307 }
308 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1;
309 ASSERT(storage->next_->previous_ == storage);
310 ASSERT(storage->previous_->next_ == storage);
311 storage->Unlink();
312 storage->LinkTo(&free_list_);
313}
314
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000315Isolate* Isolate::default_isolate_ = NULL;
316Thread::LocalStorageKey Isolate::isolate_key_;
317Thread::LocalStorageKey Isolate::thread_id_key_;
318Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_;
319Mutex* Isolate::process_wide_mutex_ = OS::CreateMutex();
320Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL;
321
322
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000323Isolate::PerIsolateThreadData* Isolate::AllocatePerIsolateThreadData(
324 ThreadId thread_id) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000325 ASSERT(!thread_id.Equals(ThreadId::Invalid()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000326 PerIsolateThreadData* per_thread = new PerIsolateThreadData(this, thread_id);
327 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000328 ScopedLock lock(process_wide_mutex_);
329 ASSERT(thread_data_table_->Lookup(this, thread_id) == NULL);
330 thread_data_table_->Insert(per_thread);
331 ASSERT(thread_data_table_->Lookup(this, thread_id) == per_thread);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000332 }
333 return per_thread;
334}
335
336
337Isolate::PerIsolateThreadData*
338 Isolate::FindOrAllocatePerThreadDataForThisThread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000339 ThreadId thread_id = ThreadId::Current();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000340 PerIsolateThreadData* per_thread = NULL;
341 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000342 ScopedLock lock(process_wide_mutex_);
343 per_thread = thread_data_table_->Lookup(this, thread_id);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000344 if (per_thread == NULL) {
345 per_thread = AllocatePerIsolateThreadData(thread_id);
346 }
347 }
348 return per_thread;
349}
350
351
lrn@chromium.org1c092762011-05-09 09:42:16 +0000352Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() {
353 ThreadId thread_id = ThreadId::Current();
354 PerIsolateThreadData* per_thread = NULL;
355 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000356 ScopedLock lock(process_wide_mutex_);
357 per_thread = thread_data_table_->Lookup(this, thread_id);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000358 }
359 return per_thread;
360}
361
362
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000363void Isolate::EnsureDefaultIsolate() {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000364 ScopedLock lock(process_wide_mutex_);
365 if (default_isolate_ == NULL) {
366 isolate_key_ = Thread::CreateThreadLocalKey();
367 thread_id_key_ = Thread::CreateThreadLocalKey();
368 per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey();
369 thread_data_table_ = new Isolate::ThreadDataTable();
370 default_isolate_ = new Isolate();
371 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000372 // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000373 // because a non-null thread data may be already set.
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000374 if (Thread::GetThreadLocal(isolate_key_) == NULL) {
375 Thread::SetThreadLocal(isolate_key_, default_isolate_);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000376 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000377}
378
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000379struct StaticInitializer {
380 StaticInitializer() {
381 Isolate::EnsureDefaultIsolate();
382 }
383} static_initializer;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000384
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000385#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386Debugger* Isolate::GetDefaultIsolateDebugger() {
387 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000388 return default_isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000389}
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000390#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000391
392
393StackGuard* Isolate::GetDefaultIsolateStackGuard() {
394 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000395 return default_isolate_->stack_guard();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000396}
397
398
399void Isolate::EnterDefaultIsolate() {
400 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000401 ASSERT(default_isolate_ != NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402
403 PerIsolateThreadData* data = CurrentPerIsolateThreadData();
404 // If not yet in default isolate - enter it.
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000405 if (data == NULL || data->isolate() != default_isolate_) {
406 default_isolate_->Enter();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000407 }
408}
409
410
411Isolate* Isolate::GetDefaultIsolateForLocking() {
412 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000413 return default_isolate_;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000414}
415
416
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000417Address Isolate::get_address_from_id(Isolate::AddressId id) {
418 return isolate_addresses_[id];
419}
420
421
422char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) {
423 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage);
424 Iterate(v, thread);
425 return thread_storage + sizeof(ThreadLocalTop);
426}
427
428
429void Isolate::IterateThread(ThreadVisitor* v) {
430 v->VisitThread(this, thread_local_top());
431}
432
433
434void Isolate::IterateThread(ThreadVisitor* v, char* t) {
435 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(t);
436 v->VisitThread(this, thread);
437}
438
439
440void Isolate::Iterate(ObjectVisitor* v, ThreadLocalTop* thread) {
441 // Visit the roots from the top for a given thread.
442 Object* pending;
443 // The pending exception can sometimes be a failure. We can't show
444 // that to the GC, which only understands objects.
445 if (thread->pending_exception_->ToObject(&pending)) {
446 v->VisitPointer(&pending);
447 thread->pending_exception_ = pending; // In case GC updated it.
448 }
449 v->VisitPointer(&(thread->pending_message_obj_));
450 v->VisitPointer(BitCast<Object**>(&(thread->pending_message_script_)));
451 v->VisitPointer(BitCast<Object**>(&(thread->context_)));
452 Object* scheduled;
453 if (thread->scheduled_exception_->ToObject(&scheduled)) {
454 v->VisitPointer(&scheduled);
455 thread->scheduled_exception_ = scheduled;
456 }
457
458 for (v8::TryCatch* block = thread->TryCatchHandler();
459 block != NULL;
460 block = TRY_CATCH_FROM_ADDRESS(block->next_)) {
461 v->VisitPointer(BitCast<Object**>(&(block->exception_)));
462 v->VisitPointer(BitCast<Object**>(&(block->message_)));
463 }
464
465 // Iterate over pointers on native execution stack.
466 for (StackFrameIterator it(this, thread); !it.done(); it.Advance()) {
467 it.frame()->Iterate(v);
468 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000469
470 // Iterate pointers in live lookup results.
471 thread->top_lookup_result_->Iterate(v);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000472}
473
474
475void Isolate::Iterate(ObjectVisitor* v) {
476 ThreadLocalTop* current_t = thread_local_top();
477 Iterate(v, current_t);
478}
479
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000480void Isolate::IterateDeferredHandles(ObjectVisitor* visitor) {
481 for (DeferredHandles* deferred = deferred_handles_head_;
482 deferred != NULL;
483 deferred = deferred->next_) {
484 deferred->Iterate(visitor);
485 }
486}
487
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000488
489void Isolate::RegisterTryCatchHandler(v8::TryCatch* that) {
490 // The ARM simulator has a separate JS stack. We therefore register
491 // the C++ try catch handler with the simulator and get back an
492 // address that can be used for comparisons with addresses into the
493 // JS stack. When running without the simulator, the address
494 // returned will be the address of the C++ try catch handler itself.
495 Address address = reinterpret_cast<Address>(
496 SimulatorStack::RegisterCTryCatch(reinterpret_cast<uintptr_t>(that)));
497 thread_local_top()->set_try_catch_handler_address(address);
498}
499
500
501void Isolate::UnregisterTryCatchHandler(v8::TryCatch* that) {
502 ASSERT(thread_local_top()->TryCatchHandler() == that);
503 thread_local_top()->set_try_catch_handler_address(
504 reinterpret_cast<Address>(that->next_));
505 thread_local_top()->catcher_ = NULL;
506 SimulatorStack::UnregisterCTryCatch();
507}
508
509
510Handle<String> Isolate::StackTraceString() {
511 if (stack_trace_nesting_level_ == 0) {
512 stack_trace_nesting_level_++;
513 HeapStringAllocator allocator;
514 StringStream::ClearMentionedObjectCache();
515 StringStream accumulator(&allocator);
516 incomplete_message_ = &accumulator;
517 PrintStack(&accumulator);
518 Handle<String> stack_trace = accumulator.ToString();
519 incomplete_message_ = NULL;
520 stack_trace_nesting_level_ = 0;
521 return stack_trace;
522 } else if (stack_trace_nesting_level_ == 1) {
523 stack_trace_nesting_level_++;
524 OS::PrintError(
525 "\n\nAttempt to print stack while printing stack (double fault)\n");
526 OS::PrintError(
527 "If you are lucky you may find a partial stack dump on stdout.\n\n");
528 incomplete_message_->OutputToStdOut();
529 return factory()->empty_symbol();
530 } else {
531 OS::Abort();
532 // Unreachable
533 return factory()->empty_symbol();
534 }
535}
536
537
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +0000538void Isolate::CaptureAndSetCurrentStackTraceFor(Handle<JSObject> error_object) {
539 if (capture_stack_trace_for_uncaught_exceptions_) {
540 // Capture stack trace for a detailed exception message.
541 Handle<String> key = factory()->hidden_stack_trace_symbol();
542 Handle<JSArray> stack_trace = CaptureCurrentStackTrace(
543 stack_trace_for_uncaught_exceptions_frame_limit_,
544 stack_trace_for_uncaught_exceptions_options_);
545 JSObject::SetHiddenProperty(error_object, key, stack_trace);
546 }
547}
548
549
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000550Handle<JSArray> Isolate::CaptureCurrentStackTrace(
551 int frame_limit, StackTrace::StackTraceOptions options) {
552 // Ensure no negative values.
553 int limit = Max(frame_limit, 0);
554 Handle<JSArray> stack_trace = factory()->NewJSArray(frame_limit);
555
556 Handle<String> column_key = factory()->LookupAsciiSymbol("column");
557 Handle<String> line_key = factory()->LookupAsciiSymbol("lineNumber");
558 Handle<String> script_key = factory()->LookupAsciiSymbol("scriptName");
559 Handle<String> name_or_source_url_key =
560 factory()->LookupAsciiSymbol("nameOrSourceURL");
561 Handle<String> script_name_or_source_url_key =
562 factory()->LookupAsciiSymbol("scriptNameOrSourceURL");
563 Handle<String> function_key = factory()->LookupAsciiSymbol("functionName");
564 Handle<String> eval_key = factory()->LookupAsciiSymbol("isEval");
565 Handle<String> constructor_key =
566 factory()->LookupAsciiSymbol("isConstructor");
567
568 StackTraceFrameIterator it(this);
569 int frames_seen = 0;
570 while (!it.done() && (frames_seen < limit)) {
571 JavaScriptFrame* frame = it.frame();
572 // Set initial size to the maximum inlining level + 1 for the outermost
573 // function.
574 List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1);
575 frame->Summarize(&frames);
576 for (int i = frames.length() - 1; i >= 0 && frames_seen < limit; i--) {
577 // Create a JSObject to hold the information for the StackFrame.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000578 Handle<JSObject> stack_frame = factory()->NewJSObject(object_function());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000579
580 Handle<JSFunction> fun = frames[i].function();
581 Handle<Script> script(Script::cast(fun->shared()->script()));
582
583 if (options & StackTrace::kLineNumber) {
584 int script_line_offset = script->line_offset()->value();
585 int position = frames[i].code()->SourcePosition(frames[i].pc());
586 int line_number = GetScriptLineNumber(script, position);
587 // line_number is already shifted by the script_line_offset.
588 int relative_line_number = line_number - script_line_offset;
589 if (options & StackTrace::kColumnOffset && relative_line_number >= 0) {
590 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
591 int start = (relative_line_number == 0) ? 0 :
592 Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1;
593 int column_offset = position - start;
594 if (relative_line_number == 0) {
595 // For the case where the code is on the same line as the script
596 // tag.
597 column_offset += script->column_offset()->value();
598 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000599 CHECK_NOT_EMPTY_HANDLE(
600 this,
601 JSObject::SetLocalPropertyIgnoreAttributes(
602 stack_frame, column_key,
603 Handle<Smi>(Smi::FromInt(column_offset + 1)), NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000604 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000605 CHECK_NOT_EMPTY_HANDLE(
606 this,
607 JSObject::SetLocalPropertyIgnoreAttributes(
608 stack_frame, line_key,
609 Handle<Smi>(Smi::FromInt(line_number + 1)), NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000610 }
611
612 if (options & StackTrace::kScriptName) {
613 Handle<Object> script_name(script->name(), this);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000614 CHECK_NOT_EMPTY_HANDLE(this,
615 JSObject::SetLocalPropertyIgnoreAttributes(
616 stack_frame, script_key, script_name, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000617 }
618
619 if (options & StackTrace::kScriptNameOrSourceURL) {
620 Handle<Object> script_name(script->name(), this);
621 Handle<JSValue> script_wrapper = GetScriptWrapper(script);
622 Handle<Object> property = GetProperty(script_wrapper,
623 name_or_source_url_key);
624 ASSERT(property->IsJSFunction());
625 Handle<JSFunction> method = Handle<JSFunction>::cast(property);
626 bool caught_exception;
627 Handle<Object> result = Execution::TryCall(method, script_wrapper, 0,
628 NULL, &caught_exception);
629 if (caught_exception) {
630 result = factory()->undefined_value();
631 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000632 CHECK_NOT_EMPTY_HANDLE(this,
633 JSObject::SetLocalPropertyIgnoreAttributes(
634 stack_frame, script_name_or_source_url_key,
635 result, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000636 }
637
638 if (options & StackTrace::kFunctionName) {
639 Handle<Object> fun_name(fun->shared()->name(), this);
640 if (fun_name->ToBoolean()->IsFalse()) {
641 fun_name = Handle<Object>(fun->shared()->inferred_name(), this);
642 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000643 CHECK_NOT_EMPTY_HANDLE(this,
644 JSObject::SetLocalPropertyIgnoreAttributes(
645 stack_frame, function_key, fun_name, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000646 }
647
648 if (options & StackTrace::kIsEval) {
649 int type = Smi::cast(script->compilation_type())->value();
650 Handle<Object> is_eval = (type == Script::COMPILATION_TYPE_EVAL) ?
651 factory()->true_value() : factory()->false_value();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000652 CHECK_NOT_EMPTY_HANDLE(this,
653 JSObject::SetLocalPropertyIgnoreAttributes(
654 stack_frame, eval_key, is_eval, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000655 }
656
657 if (options & StackTrace::kIsConstructor) {
658 Handle<Object> is_constructor = (frames[i].is_constructor()) ?
659 factory()->true_value() : factory()->false_value();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000660 CHECK_NOT_EMPTY_HANDLE(this,
661 JSObject::SetLocalPropertyIgnoreAttributes(
662 stack_frame, constructor_key,
663 is_constructor, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000664 }
665
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000666 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stack_frame);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000667 frames_seen++;
668 }
669 it.Advance();
670 }
671
672 stack_trace->set_length(Smi::FromInt(frames_seen));
673 return stack_trace;
674}
675
676
677void Isolate::PrintStack() {
678 if (stack_trace_nesting_level_ == 0) {
679 stack_trace_nesting_level_++;
680
681 StringAllocator* allocator;
682 if (preallocated_message_space_ == NULL) {
683 allocator = new HeapStringAllocator();
684 } else {
685 allocator = preallocated_message_space_;
686 }
687
688 StringStream::ClearMentionedObjectCache();
689 StringStream accumulator(allocator);
690 incomplete_message_ = &accumulator;
691 PrintStack(&accumulator);
692 accumulator.OutputToStdOut();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000693 InitializeLoggingAndCounters();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000694 accumulator.Log();
695 incomplete_message_ = NULL;
696 stack_trace_nesting_level_ = 0;
697 if (preallocated_message_space_ == NULL) {
698 // Remove the HeapStringAllocator created above.
699 delete allocator;
700 }
701 } else if (stack_trace_nesting_level_ == 1) {
702 stack_trace_nesting_level_++;
703 OS::PrintError(
704 "\n\nAttempt to print stack while printing stack (double fault)\n");
705 OS::PrintError(
706 "If you are lucky you may find a partial stack dump on stdout.\n\n");
707 incomplete_message_->OutputToStdOut();
708 }
709}
710
711
712static void PrintFrames(StringStream* accumulator,
713 StackFrame::PrintMode mode) {
714 StackFrameIterator it;
715 for (int i = 0; !it.done(); it.Advance()) {
716 it.frame()->Print(accumulator, mode, i++);
717 }
718}
719
720
721void Isolate::PrintStack(StringStream* accumulator) {
722 if (!IsInitialized()) {
723 accumulator->Add(
724 "\n==== Stack trace is not available ==========================\n\n");
725 accumulator->Add(
726 "\n==== Isolate for the thread is not initialized =============\n\n");
727 return;
728 }
729 // The MentionedObjectCache is not GC-proof at the moment.
730 AssertNoAllocation nogc;
731 ASSERT(StringStream::IsMentionedObjectCacheClear());
732
733 // Avoid printing anything if there are no frames.
734 if (c_entry_fp(thread_local_top()) == 0) return;
735
736 accumulator->Add(
737 "\n==== Stack trace ============================================\n\n");
738 PrintFrames(accumulator, StackFrame::OVERVIEW);
739
740 accumulator->Add(
741 "\n==== Details ================================================\n\n");
742 PrintFrames(accumulator, StackFrame::DETAILS);
743
744 accumulator->PrintMentionedObjectCache();
745 accumulator->Add("=====================\n\n");
746}
747
748
749void Isolate::SetFailedAccessCheckCallback(
750 v8::FailedAccessCheckCallback callback) {
751 thread_local_top()->failed_access_check_callback_ = callback;
752}
753
754
755void Isolate::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) {
756 if (!thread_local_top()->failed_access_check_callback_) return;
757
758 ASSERT(receiver->IsAccessCheckNeeded());
759 ASSERT(context());
760
761 // Get the data object from access check info.
762 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
763 if (!constructor->shared()->IsApiFunction()) return;
764 Object* data_obj =
765 constructor->shared()->get_api_func_data()->access_check_info();
766 if (data_obj == heap_.undefined_value()) return;
767
768 HandleScope scope;
769 Handle<JSObject> receiver_handle(receiver);
770 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data());
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000771 { VMState state(this, EXTERNAL);
772 thread_local_top()->failed_access_check_callback_(
773 v8::Utils::ToLocal(receiver_handle),
774 type,
775 v8::Utils::ToLocal(data));
776 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000777}
778
779
780enum MayAccessDecision {
781 YES, NO, UNKNOWN
782};
783
784
785static MayAccessDecision MayAccessPreCheck(Isolate* isolate,
786 JSObject* receiver,
787 v8::AccessType type) {
788 // During bootstrapping, callback functions are not enabled yet.
789 if (isolate->bootstrapper()->IsActive()) return YES;
790
791 if (receiver->IsJSGlobalProxy()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000792 Object* receiver_context = JSGlobalProxy::cast(receiver)->native_context();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000793 if (!receiver_context->IsContext()) return NO;
794
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000795 // Get the native context of current top context.
796 // avoid using Isolate::native_context() because it uses Handle.
797 Context* native_context =
798 isolate->context()->global_object()->native_context();
799 if (receiver_context == native_context) return YES;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000800
801 if (Context::cast(receiver_context)->security_token() ==
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000802 native_context->security_token())
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000803 return YES;
804 }
805
806 return UNKNOWN;
807}
808
809
810bool Isolate::MayNamedAccess(JSObject* receiver, Object* key,
811 v8::AccessType type) {
812 ASSERT(receiver->IsAccessCheckNeeded());
813
814 // The callers of this method are not expecting a GC.
815 AssertNoAllocation no_gc;
816
817 // Skip checks for hidden properties access. Note, we do not
818 // require existence of a context in this case.
819 if (key == heap_.hidden_symbol()) return true;
820
821 // Check for compatibility between the security tokens in the
822 // current lexical context and the accessed object.
823 ASSERT(context());
824
825 MayAccessDecision decision = MayAccessPreCheck(this, receiver, type);
826 if (decision != UNKNOWN) return decision == YES;
827
828 // Get named access check callback
829 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
830 if (!constructor->shared()->IsApiFunction()) return false;
831
832 Object* data_obj =
833 constructor->shared()->get_api_func_data()->access_check_info();
834 if (data_obj == heap_.undefined_value()) return false;
835
836 Object* fun_obj = AccessCheckInfo::cast(data_obj)->named_callback();
837 v8::NamedSecurityCallback callback =
838 v8::ToCData<v8::NamedSecurityCallback>(fun_obj);
839
840 if (!callback) return false;
841
842 HandleScope scope(this);
843 Handle<JSObject> receiver_handle(receiver, this);
844 Handle<Object> key_handle(key, this);
845 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
846 LOG(this, ApiNamedSecurityCheck(key));
847 bool result = false;
848 {
849 // Leaving JavaScript.
850 VMState state(this, EXTERNAL);
851 result = callback(v8::Utils::ToLocal(receiver_handle),
852 v8::Utils::ToLocal(key_handle),
853 type,
854 v8::Utils::ToLocal(data));
855 }
856 return result;
857}
858
859
860bool Isolate::MayIndexedAccess(JSObject* receiver,
861 uint32_t index,
862 v8::AccessType type) {
863 ASSERT(receiver->IsAccessCheckNeeded());
864 // Check for compatibility between the security tokens in the
865 // current lexical context and the accessed object.
866 ASSERT(context());
867
868 MayAccessDecision decision = MayAccessPreCheck(this, receiver, type);
869 if (decision != UNKNOWN) return decision == YES;
870
871 // Get indexed access check callback
872 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
873 if (!constructor->shared()->IsApiFunction()) return false;
874
875 Object* data_obj =
876 constructor->shared()->get_api_func_data()->access_check_info();
877 if (data_obj == heap_.undefined_value()) return false;
878
879 Object* fun_obj = AccessCheckInfo::cast(data_obj)->indexed_callback();
880 v8::IndexedSecurityCallback callback =
881 v8::ToCData<v8::IndexedSecurityCallback>(fun_obj);
882
883 if (!callback) return false;
884
885 HandleScope scope(this);
886 Handle<JSObject> receiver_handle(receiver, this);
887 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
888 LOG(this, ApiIndexedSecurityCheck(index));
889 bool result = false;
890 {
891 // Leaving JavaScript.
892 VMState state(this, EXTERNAL);
893 result = callback(v8::Utils::ToLocal(receiver_handle),
894 index,
895 type,
896 v8::Utils::ToLocal(data));
897 }
898 return result;
899}
900
901
902const char* const Isolate::kStackOverflowMessage =
903 "Uncaught RangeError: Maximum call stack size exceeded";
904
905
906Failure* Isolate::StackOverflow() {
907 HandleScope scope;
908 Handle<String> key = factory()->stack_overflow_symbol();
909 Handle<JSObject> boilerplate =
910 Handle<JSObject>::cast(GetProperty(js_builtins_object(), key));
911 Handle<Object> exception = Copy(boilerplate);
912 // TODO(1240995): To avoid having to call JavaScript code to compute
913 // the message for stack overflow exceptions which is very likely to
914 // double fault with another stack overflow exception, we use a
915 // precomputed message.
916 DoThrow(*exception, NULL);
917 return Failure::Exception();
918}
919
920
921Failure* Isolate::TerminateExecution() {
922 DoThrow(heap_.termination_exception(), NULL);
923 return Failure::Exception();
924}
925
926
927Failure* Isolate::Throw(Object* exception, MessageLocation* location) {
928 DoThrow(exception, location);
929 return Failure::Exception();
930}
931
932
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000933Failure* Isolate::ReThrow(MaybeObject* exception) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000934 bool can_be_caught_externally = false;
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000935 bool catchable_by_javascript = is_catchable_by_javascript(exception);
936 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
937
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000938 thread_local_top()->catcher_ = can_be_caught_externally ?
939 try_catch_handler() : NULL;
940
941 // Set the exception being re-thrown.
942 set_pending_exception(exception);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000943 if (exception->IsFailure()) return exception->ToFailureUnchecked();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000944 return Failure::Exception();
945}
946
947
948Failure* Isolate::ThrowIllegalOperation() {
949 return Throw(heap_.illegal_access_symbol());
950}
951
952
953void Isolate::ScheduleThrow(Object* exception) {
954 // When scheduling a throw we first throw the exception to get the
955 // error reporting if it is uncaught before rescheduling it.
956 Throw(exception);
yangguo@chromium.orgd2899aa2012-06-21 11:16:20 +0000957 thread_local_top()->scheduled_exception_ = pending_exception();
958 thread_local_top()->external_caught_exception_ = false;
959 clear_pending_exception();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000960}
961
962
963Failure* Isolate::PromoteScheduledException() {
964 MaybeObject* thrown = scheduled_exception();
965 clear_scheduled_exception();
966 // Re-throw the exception to avoid getting repeated error reporting.
967 return ReThrow(thrown);
968}
969
970
971void Isolate::PrintCurrentStackTrace(FILE* out) {
972 StackTraceFrameIterator it(this);
973 while (!it.done()) {
974 HandleScope scope;
975 // Find code position if recorded in relocation info.
976 JavaScriptFrame* frame = it.frame();
977 int pos = frame->LookupCode()->SourcePosition(frame->pc());
978 Handle<Object> pos_obj(Smi::FromInt(pos));
979 // Fetch function and receiver.
980 Handle<JSFunction> fun(JSFunction::cast(frame->function()));
981 Handle<Object> recv(frame->receiver());
982 // Advance to the next JavaScript frame and determine if the
983 // current frame is the top-level frame.
984 it.Advance();
985 Handle<Object> is_top_level = it.done()
986 ? factory()->true_value()
987 : factory()->false_value();
988 // Generate and print stack trace line.
989 Handle<String> line =
990 Execution::GetStackTraceLine(recv, fun, pos_obj, is_top_level);
991 if (line->length() > 0) {
992 line->PrintOn(out);
993 fprintf(out, "\n");
994 }
995 }
996}
997
998
999void Isolate::ComputeLocation(MessageLocation* target) {
1000 *target = MessageLocation(Handle<Script>(heap_.empty_script()), -1, -1);
1001 StackTraceFrameIterator it(this);
1002 if (!it.done()) {
1003 JavaScriptFrame* frame = it.frame();
1004 JSFunction* fun = JSFunction::cast(frame->function());
1005 Object* script = fun->shared()->script();
1006 if (script->IsScript() &&
1007 !(Script::cast(script)->source()->IsUndefined())) {
1008 int pos = frame->LookupCode()->SourcePosition(frame->pc());
1009 // Compute the location from the function and the reloc info.
1010 Handle<Script> casted_script(Script::cast(script));
1011 *target = MessageLocation(casted_script, pos, pos + 1);
1012 }
1013 }
1014}
1015
1016
1017bool Isolate::ShouldReportException(bool* can_be_caught_externally,
1018 bool catchable_by_javascript) {
1019 // Find the top-most try-catch handler.
1020 StackHandler* handler =
1021 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001022 while (handler != NULL && !handler->is_catch()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001023 handler = handler->next();
1024 }
1025
1026 // Get the address of the external handler so we can compare the address to
1027 // determine which one is closer to the top of the stack.
1028 Address external_handler_address =
1029 thread_local_top()->try_catch_handler_address();
1030
1031 // The exception has been externally caught if and only if there is
1032 // an external handler which is on top of the top-most try-catch
1033 // handler.
1034 *can_be_caught_externally = external_handler_address != NULL &&
1035 (handler == NULL || handler->address() > external_handler_address ||
1036 !catchable_by_javascript);
1037
1038 if (*can_be_caught_externally) {
1039 // Only report the exception if the external handler is verbose.
1040 return try_catch_handler()->is_verbose_;
1041 } else {
1042 // Report the exception if it isn't caught by JavaScript code.
1043 return handler == NULL;
1044 }
1045}
1046
1047
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001048bool Isolate::IsErrorObject(Handle<Object> obj) {
1049 if (!obj->IsJSObject()) return false;
1050
1051 String* error_key = *(factory()->LookupAsciiSymbol("$Error"));
1052 Object* error_constructor =
1053 js_builtins_object()->GetPropertyNoExceptionThrown(error_key);
1054
1055 for (Object* prototype = *obj; !prototype->IsNull();
1056 prototype = prototype->GetPrototype()) {
1057 if (!prototype->IsJSObject()) return false;
1058 if (JSObject::cast(prototype)->map()->constructor() == error_constructor) {
1059 return true;
1060 }
1061 }
1062 return false;
1063}
1064
1065
1066void Isolate::DoThrow(Object* exception, MessageLocation* location) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001067 ASSERT(!has_pending_exception());
1068
1069 HandleScope scope;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001070 Handle<Object> exception_handle(exception);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001071
1072 // Determine reporting and whether the exception is caught externally.
1073 bool catchable_by_javascript = is_catchable_by_javascript(exception);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001074 bool can_be_caught_externally = false;
1075 bool should_report_exception =
1076 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
1077 bool report_exception = catchable_by_javascript && should_report_exception;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001078 bool try_catch_needs_message =
1079 can_be_caught_externally && try_catch_handler()->capture_message_;
1080 bool bootstrapping = bootstrapper()->IsActive();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001081
1082#ifdef ENABLE_DEBUGGER_SUPPORT
1083 // Notify debugger of exception.
1084 if (catchable_by_javascript) {
1085 debugger_->OnException(exception_handle, report_exception);
1086 }
1087#endif
1088
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001089 // Generate the message if required.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001090 if (report_exception || try_catch_needs_message) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001091 MessageLocation potential_computed_location;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001092 if (location == NULL) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001093 // If no location was specified we use a computed one instead.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001094 ComputeLocation(&potential_computed_location);
1095 location = &potential_computed_location;
1096 }
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001097 // It's not safe to try to make message objects or collect stack traces
1098 // while the bootstrapper is active since the infrastructure may not have
1099 // been properly initialized.
1100 if (!bootstrapping) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001101 Handle<String> stack_trace;
1102 if (FLAG_trace_exception) stack_trace = StackTraceString();
1103 Handle<JSArray> stack_trace_object;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001104 if (capture_stack_trace_for_uncaught_exceptions_) {
1105 if (IsErrorObject(exception_handle)) {
1106 // We fetch the stack trace that corresponds to this error object.
1107 String* key = heap()->hidden_stack_trace_symbol();
1108 Object* stack_property =
1109 JSObject::cast(*exception_handle)->GetHiddenProperty(key);
1110 // Property lookup may have failed. In this case it's probably not
1111 // a valid Error object.
1112 if (stack_property->IsJSArray()) {
1113 stack_trace_object = Handle<JSArray>(JSArray::cast(stack_property));
1114 }
1115 }
1116 if (stack_trace_object.is_null()) {
1117 // Not an error object, we capture at throw site.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001118 stack_trace_object = CaptureCurrentStackTrace(
1119 stack_trace_for_uncaught_exceptions_frame_limit_,
1120 stack_trace_for_uncaught_exceptions_options_);
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001121 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001122 }
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001123 Handle<Object> message_obj = MessageHandler::MakeMessageObject(
1124 "uncaught_exception",
1125 location,
1126 HandleVector<Object>(&exception_handle, 1),
1127 stack_trace,
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001128 stack_trace_object);
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001129 thread_local_top()->pending_message_obj_ = *message_obj;
1130 if (location != NULL) {
1131 thread_local_top()->pending_message_script_ = *location->script();
1132 thread_local_top()->pending_message_start_pos_ = location->start_pos();
1133 thread_local_top()->pending_message_end_pos_ = location->end_pos();
1134 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001135 } else if (location != NULL && !location->script().is_null()) {
1136 // We are bootstrapping and caught an error where the location is set
1137 // and we have a script for the location.
1138 // In this case we could have an extension (or an internal error
1139 // somewhere) and we print out the line number at which the error occured
1140 // to the console for easier debugging.
1141 int line_number = GetScriptLineNumberSafe(location->script(),
1142 location->start_pos());
verwaest@chromium.org37141392012-05-31 13:27:02 +00001143 if (exception->IsString()) {
1144 OS::PrintError(
1145 "Extension or internal compilation error: %s in %s at line %d.\n",
1146 *String::cast(exception)->ToCString(),
1147 *String::cast(location->script()->name())->ToCString(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001148 line_number + 1);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001149 } else {
1150 OS::PrintError(
1151 "Extension or internal compilation error in %s at line %d.\n",
1152 *String::cast(location->script()->name())->ToCString(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001153 line_number + 1);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001154 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001155 }
1156 }
1157
1158 // Save the message for reporting if the the exception remains uncaught.
1159 thread_local_top()->has_pending_message_ = report_exception;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001160
1161 // Do not forget to clean catcher_ if currently thrown exception cannot
1162 // be caught. If necessary, ReThrow will update the catcher.
1163 thread_local_top()->catcher_ = can_be_caught_externally ?
1164 try_catch_handler() : NULL;
1165
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001166 set_pending_exception(*exception_handle);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001167}
1168
1169
1170bool Isolate::IsExternallyCaught() {
1171 ASSERT(has_pending_exception());
1172
1173 if ((thread_local_top()->catcher_ == NULL) ||
1174 (try_catch_handler() != thread_local_top()->catcher_)) {
1175 // When throwing the exception, we found no v8::TryCatch
1176 // which should care about this exception.
1177 return false;
1178 }
1179
1180 if (!is_catchable_by_javascript(pending_exception())) {
1181 return true;
1182 }
1183
1184 // Get the address of the external handler so we can compare the address to
1185 // determine which one is closer to the top of the stack.
1186 Address external_handler_address =
1187 thread_local_top()->try_catch_handler_address();
1188 ASSERT(external_handler_address != NULL);
1189
1190 // The exception has been externally caught if and only if there is
1191 // an external handler which is on top of the top-most try-finally
1192 // handler.
1193 // There should be no try-catch blocks as they would prohibit us from
1194 // finding external catcher in the first place (see catcher_ check above).
1195 //
1196 // Note, that finally clause would rethrow an exception unless it's
1197 // aborted by jumps in control flow like return, break, etc. and we'll
1198 // have another chances to set proper v8::TryCatch.
1199 StackHandler* handler =
1200 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
1201 while (handler != NULL && handler->address() < external_handler_address) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001202 ASSERT(!handler->is_catch());
1203 if (handler->is_finally()) return false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001204
1205 handler = handler->next();
1206 }
1207
1208 return true;
1209}
1210
1211
1212void Isolate::ReportPendingMessages() {
1213 ASSERT(has_pending_exception());
1214 PropagatePendingExceptionToExternalTryCatch();
1215
1216 // If the pending exception is OutOfMemoryException set out_of_memory in
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001217 // the native context. Note: We have to mark the native context here
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001218 // since the GenerateThrowOutOfMemory stub cannot make a RuntimeCall to
1219 // set it.
1220 HandleScope scope;
1221 if (thread_local_top_.pending_exception_ == Failure::OutOfMemoryException()) {
1222 context()->mark_out_of_memory();
1223 } else if (thread_local_top_.pending_exception_ ==
1224 heap()->termination_exception()) {
1225 // Do nothing: if needed, the exception has been already propagated to
1226 // v8::TryCatch.
1227 } else {
1228 if (thread_local_top_.has_pending_message_) {
1229 thread_local_top_.has_pending_message_ = false;
1230 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
1231 HandleScope scope;
1232 Handle<Object> message_obj(thread_local_top_.pending_message_obj_);
1233 if (thread_local_top_.pending_message_script_ != NULL) {
1234 Handle<Script> script(thread_local_top_.pending_message_script_);
1235 int start_pos = thread_local_top_.pending_message_start_pos_;
1236 int end_pos = thread_local_top_.pending_message_end_pos_;
1237 MessageLocation location(script, start_pos, end_pos);
1238 MessageHandler::ReportMessage(this, &location, message_obj);
1239 } else {
1240 MessageHandler::ReportMessage(this, NULL, message_obj);
1241 }
1242 }
1243 }
1244 }
1245 clear_pending_message();
1246}
1247
1248
1249void Isolate::TraceException(bool flag) {
1250 FLAG_trace_exception = flag; // TODO(isolates): This is an unfortunate use.
1251}
1252
1253
1254bool Isolate::OptionalRescheduleException(bool is_bottom_call) {
1255 ASSERT(has_pending_exception());
1256 PropagatePendingExceptionToExternalTryCatch();
1257
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001258 // Always reschedule out of memory exceptions.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001259 if (!is_out_of_memory()) {
1260 bool is_termination_exception =
1261 pending_exception() == heap_.termination_exception();
1262
1263 // Do not reschedule the exception if this is the bottom call.
1264 bool clear_exception = is_bottom_call;
1265
1266 if (is_termination_exception) {
1267 if (is_bottom_call) {
1268 thread_local_top()->external_caught_exception_ = false;
1269 clear_pending_exception();
1270 return false;
1271 }
1272 } else if (thread_local_top()->external_caught_exception_) {
1273 // If the exception is externally caught, clear it if there are no
1274 // JavaScript frames on the way to the C++ frame that has the
1275 // external handler.
1276 ASSERT(thread_local_top()->try_catch_handler_address() != NULL);
1277 Address external_handler_address =
1278 thread_local_top()->try_catch_handler_address();
1279 JavaScriptFrameIterator it;
1280 if (it.done() || (it.frame()->sp() > external_handler_address)) {
1281 clear_exception = true;
1282 }
1283 }
1284
1285 // Clear the exception if needed.
1286 if (clear_exception) {
1287 thread_local_top()->external_caught_exception_ = false;
1288 clear_pending_exception();
1289 return false;
1290 }
1291 }
1292
1293 // Reschedule the exception.
1294 thread_local_top()->scheduled_exception_ = pending_exception();
1295 clear_pending_exception();
1296 return true;
1297}
1298
1299
1300void Isolate::SetCaptureStackTraceForUncaughtExceptions(
1301 bool capture,
1302 int frame_limit,
1303 StackTrace::StackTraceOptions options) {
1304 capture_stack_trace_for_uncaught_exceptions_ = capture;
1305 stack_trace_for_uncaught_exceptions_frame_limit_ = frame_limit;
1306 stack_trace_for_uncaught_exceptions_options_ = options;
1307}
1308
1309
1310bool Isolate::is_out_of_memory() {
1311 if (has_pending_exception()) {
1312 MaybeObject* e = pending_exception();
1313 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1314 return true;
1315 }
1316 }
1317 if (has_scheduled_exception()) {
1318 MaybeObject* e = scheduled_exception();
1319 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1320 return true;
1321 }
1322 }
1323 return false;
1324}
1325
1326
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001327Handle<Context> Isolate::native_context() {
1328 GlobalObject* global = thread_local_top()->context_->global_object();
1329 return Handle<Context>(global->native_context());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001330}
1331
1332
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001333Handle<Context> Isolate::global_context() {
1334 GlobalObject* global = thread_local_top()->context_->global_object();
1335 return Handle<Context>(global->global_context());
1336}
1337
1338
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001339Handle<Context> Isolate::GetCallingNativeContext() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001340 JavaScriptFrameIterator it;
1341#ifdef ENABLE_DEBUGGER_SUPPORT
1342 if (debug_->InDebugger()) {
1343 while (!it.done()) {
1344 JavaScriptFrame* frame = it.frame();
1345 Context* context = Context::cast(frame->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001346 if (context->native_context() == *debug_->debug_context()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001347 it.Advance();
1348 } else {
1349 break;
1350 }
1351 }
1352 }
1353#endif // ENABLE_DEBUGGER_SUPPORT
1354 if (it.done()) return Handle<Context>::null();
1355 JavaScriptFrame* frame = it.frame();
1356 Context* context = Context::cast(frame->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001357 return Handle<Context>(context->native_context());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001358}
1359
1360
1361char* Isolate::ArchiveThread(char* to) {
1362 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1363 RuntimeProfiler::IsolateExitedJS(this);
1364 }
1365 memcpy(to, reinterpret_cast<char*>(thread_local_top()),
1366 sizeof(ThreadLocalTop));
1367 InitializeThreadLocal();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001368 clear_pending_exception();
1369 clear_pending_message();
1370 clear_scheduled_exception();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001371 return to + sizeof(ThreadLocalTop);
1372}
1373
1374
1375char* Isolate::RestoreThread(char* from) {
1376 memcpy(reinterpret_cast<char*>(thread_local_top()), from,
1377 sizeof(ThreadLocalTop));
1378 // This might be just paranoia, but it seems to be needed in case a
1379 // thread_local_top_ is restored on a separate OS thread.
1380#ifdef USE_SIMULATOR
1381#ifdef V8_TARGET_ARCH_ARM
1382 thread_local_top()->simulator_ = Simulator::current(this);
1383#elif V8_TARGET_ARCH_MIPS
1384 thread_local_top()->simulator_ = Simulator::current(this);
1385#endif
1386#endif
1387 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1388 RuntimeProfiler::IsolateEnteredJS(this);
1389 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001390 ASSERT(context() == NULL || context()->IsContext());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001391 return from + sizeof(ThreadLocalTop);
1392}
1393
1394
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001395Isolate::ThreadDataTable::ThreadDataTable()
1396 : list_(NULL) {
1397}
1398
1399
1400Isolate::PerIsolateThreadData*
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001401 Isolate::ThreadDataTable::Lookup(Isolate* isolate,
1402 ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001403 for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) {
1404 if (data->Matches(isolate, thread_id)) return data;
1405 }
1406 return NULL;
1407}
1408
1409
1410void Isolate::ThreadDataTable::Insert(Isolate::PerIsolateThreadData* data) {
1411 if (list_ != NULL) list_->prev_ = data;
1412 data->next_ = list_;
1413 list_ = data;
1414}
1415
1416
1417void Isolate::ThreadDataTable::Remove(PerIsolateThreadData* data) {
1418 if (list_ == data) list_ = data->next_;
1419 if (data->next_ != NULL) data->next_->prev_ = data->prev_;
1420 if (data->prev_ != NULL) data->prev_->next_ = data->next_;
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001421 delete data;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001422}
1423
1424
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001425void Isolate::ThreadDataTable::Remove(Isolate* isolate,
1426 ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001427 PerIsolateThreadData* data = Lookup(isolate, thread_id);
1428 if (data != NULL) {
1429 Remove(data);
1430 }
1431}
1432
1433
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001434void Isolate::ThreadDataTable::RemoveAllThreads(Isolate* isolate) {
1435 PerIsolateThreadData* data = list_;
1436 while (data != NULL) {
1437 PerIsolateThreadData* next = data->next_;
1438 if (data->isolate() == isolate) Remove(data);
1439 data = next;
1440 }
1441}
1442
1443
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001444#ifdef DEBUG
1445#define TRACE_ISOLATE(tag) \
1446 do { \
1447 if (FLAG_trace_isolates) { \
1448 PrintF("Isolate %p " #tag "\n", reinterpret_cast<void*>(this)); \
1449 } \
1450 } while (false)
1451#else
1452#define TRACE_ISOLATE(tag)
1453#endif
1454
1455
1456Isolate::Isolate()
1457 : state_(UNINITIALIZED),
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001458 embedder_data_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001459 entry_stack_(NULL),
1460 stack_trace_nesting_level_(0),
1461 incomplete_message_(NULL),
1462 preallocated_memory_thread_(NULL),
1463 preallocated_message_space_(NULL),
1464 bootstrapper_(NULL),
1465 runtime_profiler_(NULL),
1466 compilation_cache_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001467 counters_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001468 code_range_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001469 // Must be initialized early to allow v8::SetResourceConstraints calls.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001470 break_access_(OS::CreateMutex()),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001471 debugger_initialized_(false),
1472 // Must be initialized early to allow v8::Debug calls.
1473 debugger_access_(OS::CreateMutex()),
1474 logger_(NULL),
1475 stats_table_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001476 stub_cache_(NULL),
1477 deoptimizer_data_(NULL),
1478 capture_stack_trace_for_uncaught_exceptions_(false),
1479 stack_trace_for_uncaught_exceptions_frame_limit_(0),
1480 stack_trace_for_uncaught_exceptions_options_(StackTrace::kOverview),
1481 transcendental_cache_(NULL),
1482 memory_allocator_(NULL),
1483 keyed_lookup_cache_(NULL),
1484 context_slot_cache_(NULL),
1485 descriptor_lookup_cache_(NULL),
1486 handle_scope_implementer_(NULL),
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001487 unicode_cache_(NULL),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001488 runtime_zone_(this),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001489 in_use_list_(0),
1490 free_list_(0),
1491 preallocated_storage_preallocated_(false),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001492 inner_pointer_to_code_cache_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001493 write_input_buffer_(NULL),
1494 global_handles_(NULL),
1495 context_switcher_(NULL),
1496 thread_manager_(NULL),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001497 fp_stubs_generated_(false),
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001498 has_installed_extensions_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001499 string_tracker_(NULL),
1500 regexp_stack_(NULL),
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001501 date_cache_(NULL),
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001502 context_exit_happened_(false),
1503 deferred_handles_head_(NULL),
1504 optimizing_compiler_thread_(this) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001505 TRACE_ISOLATE(constructor);
1506
1507 memset(isolate_addresses_, 0,
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001508 sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001509
1510 heap_.isolate_ = this;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001511 stack_guard_.isolate_ = this;
1512
lrn@chromium.org1c092762011-05-09 09:42:16 +00001513 // ThreadManager is initialized early to support locking an isolate
1514 // before it is entered.
1515 thread_manager_ = new ThreadManager();
1516 thread_manager_->isolate_ = this;
1517
lrn@chromium.org7516f052011-03-30 08:52:27 +00001518#if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \
1519 defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001520 simulator_initialized_ = false;
1521 simulator_i_cache_ = NULL;
1522 simulator_redirection_ = NULL;
1523#endif
1524
1525#ifdef DEBUG
1526 // heap_histograms_ initializes itself.
1527 memset(&js_spill_information_, 0, sizeof(js_spill_information_));
1528 memset(code_kind_statistics_, 0,
1529 sizeof(code_kind_statistics_[0]) * Code::NUMBER_OF_KINDS);
1530#endif
1531
1532#ifdef ENABLE_DEBUGGER_SUPPORT
1533 debug_ = NULL;
1534 debugger_ = NULL;
1535#endif
1536
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001537 handle_scope_data_.Initialize();
1538
1539#define ISOLATE_INIT_EXECUTE(type, name, initial_value) \
1540 name##_ = (initial_value);
1541 ISOLATE_INIT_LIST(ISOLATE_INIT_EXECUTE)
1542#undef ISOLATE_INIT_EXECUTE
1543
1544#define ISOLATE_INIT_ARRAY_EXECUTE(type, name, length) \
1545 memset(name##_, 0, sizeof(type) * length);
1546 ISOLATE_INIT_ARRAY_LIST(ISOLATE_INIT_ARRAY_EXECUTE)
1547#undef ISOLATE_INIT_ARRAY_EXECUTE
1548}
1549
1550void Isolate::TearDown() {
1551 TRACE_ISOLATE(tear_down);
1552
1553 // Temporarily set this isolate as current so that various parts of
1554 // the isolate can access it in their destructors without having a
1555 // direct pointer. We don't use Enter/Exit here to avoid
1556 // initializing the thread data.
1557 PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData();
1558 Isolate* saved_isolate = UncheckedCurrent();
1559 SetIsolateThreadLocals(this, NULL);
1560
1561 Deinit();
1562
danno@chromium.org8c0a43f2012-04-03 08:37:53 +00001563 { ScopedLock lock(process_wide_mutex_);
1564 thread_data_table_->RemoveAllThreads(this);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001565 }
1566
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001567 if (serialize_partial_snapshot_cache_ != NULL) {
1568 delete[] serialize_partial_snapshot_cache_;
1569 serialize_partial_snapshot_cache_ = NULL;
1570 }
1571
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001572 if (!IsDefaultIsolate()) {
1573 delete this;
1574 }
1575
1576 // Restore the previous current isolate.
1577 SetIsolateThreadLocals(saved_isolate, saved_data);
1578}
1579
1580
1581void Isolate::Deinit() {
1582 if (state_ == INITIALIZED) {
1583 TRACE_ISOLATE(deinit);
1584
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001585 if (FLAG_parallel_recompilation) optimizing_compiler_thread_.Stop();
1586
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001587 if (FLAG_hydrogen_stats) HStatistics::Instance()->Print();
1588
1589 // We must stop the logger before we tear down other components.
1590 logger_->EnsureTickerStopped();
1591
1592 delete deoptimizer_data_;
1593 deoptimizer_data_ = NULL;
1594 if (FLAG_preemption) {
1595 v8::Locker locker;
1596 v8::Locker::StopPreemption();
1597 }
1598 builtins_.TearDown();
1599 bootstrapper_->TearDown();
1600
1601 // Remove the external reference to the preallocated stack memory.
1602 delete preallocated_message_space_;
1603 preallocated_message_space_ = NULL;
1604 PreallocatedMemoryThreadStop();
1605
1606 HeapProfiler::TearDown();
1607 CpuProfiler::TearDown();
1608 if (runtime_profiler_ != NULL) {
1609 runtime_profiler_->TearDown();
1610 delete runtime_profiler_;
1611 runtime_profiler_ = NULL;
1612 }
1613 heap_.TearDown();
1614 logger_->TearDown();
1615
1616 // The default isolate is re-initializable due to legacy API.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001617 state_ = UNINITIALIZED;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001618 }
1619}
1620
1621
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001622void Isolate::PushToPartialSnapshotCache(Object* obj) {
1623 int length = serialize_partial_snapshot_cache_length();
1624 int capacity = serialize_partial_snapshot_cache_capacity();
1625
1626 if (length >= capacity) {
1627 int new_capacity = static_cast<int>((capacity + 10) * 1.2);
1628 Object** new_array = new Object*[new_capacity];
1629 for (int i = 0; i < length; i++) {
1630 new_array[i] = serialize_partial_snapshot_cache()[i];
1631 }
1632 if (capacity != 0) delete[] serialize_partial_snapshot_cache();
1633 set_serialize_partial_snapshot_cache(new_array);
1634 set_serialize_partial_snapshot_cache_capacity(new_capacity);
1635 }
1636
1637 serialize_partial_snapshot_cache()[length] = obj;
1638 set_serialize_partial_snapshot_cache_length(length + 1);
1639}
1640
1641
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001642void Isolate::SetIsolateThreadLocals(Isolate* isolate,
1643 PerIsolateThreadData* data) {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +00001644 Thread::SetThreadLocal(isolate_key_, isolate);
1645 Thread::SetThreadLocal(per_isolate_thread_data_key_, data);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001646}
1647
1648
1649Isolate::~Isolate() {
1650 TRACE_ISOLATE(destructor);
1651
danno@chromium.orgb6451162011-08-17 14:33:23 +00001652 // Has to be called while counters_ are still alive.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001653 runtime_zone_.DeleteKeptSegment();
danno@chromium.orgb6451162011-08-17 14:33:23 +00001654
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001655 delete[] assembler_spare_buffer_;
1656 assembler_spare_buffer_ = NULL;
1657
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001658 delete unicode_cache_;
1659 unicode_cache_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001660
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001661 delete date_cache_;
1662 date_cache_ = NULL;
1663
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001664 delete regexp_stack_;
1665 regexp_stack_ = NULL;
1666
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001667 delete descriptor_lookup_cache_;
1668 descriptor_lookup_cache_ = NULL;
1669 delete context_slot_cache_;
1670 context_slot_cache_ = NULL;
1671 delete keyed_lookup_cache_;
1672 keyed_lookup_cache_ = NULL;
1673
1674 delete transcendental_cache_;
1675 transcendental_cache_ = NULL;
1676 delete stub_cache_;
1677 stub_cache_ = NULL;
1678 delete stats_table_;
1679 stats_table_ = NULL;
1680
1681 delete logger_;
1682 logger_ = NULL;
1683
1684 delete counters_;
1685 counters_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001686
1687 delete handle_scope_implementer_;
1688 handle_scope_implementer_ = NULL;
1689 delete break_access_;
1690 break_access_ = NULL;
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001691 delete debugger_access_;
1692 debugger_access_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001693
1694 delete compilation_cache_;
1695 compilation_cache_ = NULL;
1696 delete bootstrapper_;
1697 bootstrapper_ = NULL;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001698 delete inner_pointer_to_code_cache_;
1699 inner_pointer_to_code_cache_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001700 delete write_input_buffer_;
1701 write_input_buffer_ = NULL;
1702
1703 delete context_switcher_;
1704 context_switcher_ = NULL;
1705 delete thread_manager_;
1706 thread_manager_ = NULL;
1707
1708 delete string_tracker_;
1709 string_tracker_ = NULL;
1710
1711 delete memory_allocator_;
1712 memory_allocator_ = NULL;
1713 delete code_range_;
1714 code_range_ = NULL;
1715 delete global_handles_;
1716 global_handles_ = NULL;
1717
danno@chromium.orgb6451162011-08-17 14:33:23 +00001718 delete external_reference_table_;
1719 external_reference_table_ = NULL;
1720
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001721#ifdef ENABLE_DEBUGGER_SUPPORT
1722 delete debugger_;
1723 debugger_ = NULL;
1724 delete debug_;
1725 debug_ = NULL;
1726#endif
1727}
1728
1729
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001730void Isolate::InitializeThreadLocal() {
lrn@chromium.org1c092762011-05-09 09:42:16 +00001731 thread_local_top_.isolate_ = this;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001732 thread_local_top_.Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001733}
1734
1735
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001736void Isolate::PropagatePendingExceptionToExternalTryCatch() {
1737 ASSERT(has_pending_exception());
1738
1739 bool external_caught = IsExternallyCaught();
1740 thread_local_top_.external_caught_exception_ = external_caught;
1741
1742 if (!external_caught) return;
1743
1744 if (thread_local_top_.pending_exception_ == Failure::OutOfMemoryException()) {
1745 // Do not propagate OOM exception: we should kill VM asap.
1746 } else if (thread_local_top_.pending_exception_ ==
1747 heap()->termination_exception()) {
1748 try_catch_handler()->can_continue_ = false;
1749 try_catch_handler()->exception_ = heap()->null_value();
1750 } else {
1751 // At this point all non-object (failure) exceptions have
1752 // been dealt with so this shouldn't fail.
1753 ASSERT(!pending_exception()->IsFailure());
1754 try_catch_handler()->can_continue_ = true;
1755 try_catch_handler()->exception_ = pending_exception();
1756 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
1757 try_catch_handler()->message_ = thread_local_top_.pending_message_obj_;
1758 }
1759 }
1760}
1761
1762
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001763void Isolate::InitializeLoggingAndCounters() {
1764 if (logger_ == NULL) {
1765 logger_ = new Logger;
1766 }
1767 if (counters_ == NULL) {
1768 counters_ = new Counters;
1769 }
1770}
1771
1772
1773void Isolate::InitializeDebugger() {
1774#ifdef ENABLE_DEBUGGER_SUPPORT
1775 ScopedLock lock(debugger_access_);
1776 if (NoBarrier_Load(&debugger_initialized_)) return;
1777 InitializeLoggingAndCounters();
1778 debug_ = new Debug(this);
1779 debugger_ = new Debugger(this);
1780 Release_Store(&debugger_initialized_, true);
1781#endif
1782}
1783
1784
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001785bool Isolate::Init(Deserializer* des) {
1786 ASSERT(state_ != INITIALIZED);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001787 ASSERT(Isolate::Current() == this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001788 TRACE_ISOLATE(init);
1789
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001790 // The initialization process does not handle memory exhaustion.
1791 DisallowAllocationFailure disallow_allocation_failure;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001792
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001793 InitializeLoggingAndCounters();
1794
1795 InitializeDebugger();
1796
1797 memory_allocator_ = new MemoryAllocator(this);
1798 code_range_ = new CodeRange(this);
1799
1800 // Safe after setting Heap::isolate_, initializing StackGuard and
1801 // ensuring that Isolate::Current() == this.
1802 heap_.SetStackLimits();
1803
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001804#define ASSIGN_ELEMENT(CamelName, hacker_name) \
1805 isolate_addresses_[Isolate::k##CamelName##Address] = \
1806 reinterpret_cast<Address>(hacker_name##_address());
1807 FOR_EACH_ISOLATE_ADDRESS_NAME(ASSIGN_ELEMENT)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001808#undef C
1809
1810 string_tracker_ = new StringTracker();
1811 string_tracker_->isolate_ = this;
1812 compilation_cache_ = new CompilationCache(this);
1813 transcendental_cache_ = new TranscendentalCache();
1814 keyed_lookup_cache_ = new KeyedLookupCache();
1815 context_slot_cache_ = new ContextSlotCache();
1816 descriptor_lookup_cache_ = new DescriptorLookupCache();
1817 unicode_cache_ = new UnicodeCache();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001818 inner_pointer_to_code_cache_ = new InnerPointerToCodeCache(this);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001819 write_input_buffer_ = new StringInputBuffer();
1820 global_handles_ = new GlobalHandles(this);
1821 bootstrapper_ = new Bootstrapper();
1822 handle_scope_implementer_ = new HandleScopeImplementer(this);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001823 stub_cache_ = new StubCache(this, runtime_zone());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001824 regexp_stack_ = new RegExpStack();
1825 regexp_stack_->isolate_ = this;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001826 date_cache_ = new DateCache();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001827
1828 // Enable logging before setting up the heap
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001829 logger_->SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001830
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001831 CpuProfiler::SetUp();
1832 HeapProfiler::SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001833
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001834 // Initialize other runtime facilities
1835#if defined(USE_SIMULATOR)
lrn@chromium.org7516f052011-03-30 08:52:27 +00001836#if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS)
lrn@chromium.org1c092762011-05-09 09:42:16 +00001837 Simulator::Initialize(this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001838#endif
1839#endif
1840
1841 { // NOLINT
1842 // Ensure that the thread has a valid stack guard. The v8::Locker object
1843 // will ensure this too, but we don't have to use lockers if we are only
1844 // using one thread.
1845 ExecutionAccess lock(this);
1846 stack_guard_.InitThread(lock);
1847 }
1848
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001849 // SetUp the object heap.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001850 const bool create_heap_objects = (des == NULL);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001851 ASSERT(!heap_.HasBeenSetUp());
1852 if (!heap_.SetUp(create_heap_objects)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001853 V8::SetFatalError();
1854 return false;
1855 }
1856
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001857 if (create_heap_objects) {
1858 // Terminate the cache array with the sentinel so we can iterate.
1859 PushToPartialSnapshotCache(heap_.undefined_value());
1860 }
1861
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001862 InitializeThreadLocal();
1863
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001864 bootstrapper_->Initialize(create_heap_objects);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001865 builtins_.SetUp(create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001866
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001867 // Only preallocate on the first initialization.
1868 if (FLAG_preallocate_message_memory && preallocated_message_space_ == NULL) {
1869 // Start the thread which will set aside some memory.
1870 PreallocatedMemoryThreadStart();
1871 preallocated_message_space_ =
1872 new NoAllocationStringAllocator(
1873 preallocated_memory_thread_->data(),
1874 preallocated_memory_thread_->length());
1875 PreallocatedStorageInit(preallocated_memory_thread_->length() / 4);
1876 }
1877
1878 if (FLAG_preemption) {
1879 v8::Locker locker;
1880 v8::Locker::StartPreemption(100);
1881 }
1882
1883#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001884 debug_->SetUp(create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001885#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001886
1887 // If we are deserializing, read the state into the now-empty heap.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001888 if (!create_heap_objects) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001889 des->Deserialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001890 }
ulan@chromium.org812308e2012-02-29 15:58:45 +00001891 stub_cache_->Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001892
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001893 // Finish initialization of ThreadLocal after deserialization is done.
1894 clear_pending_exception();
1895 clear_pending_message();
1896 clear_scheduled_exception();
1897
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001898 // Deserializing may put strange things in the root array's copy of the
1899 // stack guard.
1900 heap_.SetStackLimits();
1901
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00001902 // Quiet the heap NaN if needed on target platform.
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00001903 if (!create_heap_objects) Assembler::QuietNaN(heap_.nan_value());
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00001904
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001905 deoptimizer_data_ = new DeoptimizerData;
1906 runtime_profiler_ = new RuntimeProfiler(this);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001907 runtime_profiler_->SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001908
1909 // If we are deserializing, log non-function code objects and compiled
1910 // functions found in the snapshot.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001911 if (create_heap_objects &&
1912 (FLAG_log_code || FLAG_ll_prof || logger_->is_logging_code_events())) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001913 HandleScope scope;
1914 LOG(this, LogCodeObjects());
1915 LOG(this, LogCompiledFunctions());
1916 }
1917
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001918 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, state_)),
1919 Internals::kIsolateStateOffset);
1920 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, embedder_data_)),
1921 Internals::kIsolateEmbedderDataOffset);
1922 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, heap_.roots_)),
1923 Internals::kIsolateRootsOffset);
1924
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001925 state_ = INITIALIZED;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00001926 time_millis_at_init_ = OS::TimeCurrentMillis();
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001927 if (FLAG_parallel_recompilation) optimizing_compiler_thread_.Start();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001928 return true;
1929}
1930
1931
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001932// Initialized lazily to allow early
1933// v8::V8::SetAddHistogramSampleFunction calls.
1934StatsTable* Isolate::stats_table() {
1935 if (stats_table_ == NULL) {
1936 stats_table_ = new StatsTable;
1937 }
1938 return stats_table_;
1939}
1940
1941
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001942void Isolate::Enter() {
1943 Isolate* current_isolate = NULL;
1944 PerIsolateThreadData* current_data = CurrentPerIsolateThreadData();
1945 if (current_data != NULL) {
1946 current_isolate = current_data->isolate_;
1947 ASSERT(current_isolate != NULL);
1948 if (current_isolate == this) {
1949 ASSERT(Current() == this);
1950 ASSERT(entry_stack_ != NULL);
1951 ASSERT(entry_stack_->previous_thread_data == NULL ||
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001952 entry_stack_->previous_thread_data->thread_id().Equals(
1953 ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001954 // Same thread re-enters the isolate, no need to re-init anything.
1955 entry_stack_->entry_count++;
1956 return;
1957 }
1958 }
1959
1960 // Threads can have default isolate set into TLS as Current but not yet have
1961 // PerIsolateThreadData for it, as it requires more advanced phase of the
1962 // initialization. For example, a thread might be the one that system used for
1963 // static initializers - in this case the default isolate is set in TLS but
1964 // the thread did not yet Enter the isolate. If PerisolateThreadData is not
1965 // there, use the isolate set in TLS.
1966 if (current_isolate == NULL) {
1967 current_isolate = Isolate::UncheckedCurrent();
1968 }
1969
1970 PerIsolateThreadData* data = FindOrAllocatePerThreadDataForThisThread();
1971 ASSERT(data != NULL);
1972 ASSERT(data->isolate_ == this);
1973
1974 EntryStackItem* item = new EntryStackItem(current_data,
1975 current_isolate,
1976 entry_stack_);
1977 entry_stack_ = item;
1978
1979 SetIsolateThreadLocals(this, data);
1980
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001981 // In case it's the first time some thread enters the isolate.
1982 set_thread_id(data->thread_id());
1983}
1984
1985
1986void Isolate::Exit() {
1987 ASSERT(entry_stack_ != NULL);
1988 ASSERT(entry_stack_->previous_thread_data == NULL ||
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001989 entry_stack_->previous_thread_data->thread_id().Equals(
1990 ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001991
1992 if (--entry_stack_->entry_count > 0) return;
1993
1994 ASSERT(CurrentPerIsolateThreadData() != NULL);
1995 ASSERT(CurrentPerIsolateThreadData()->isolate_ == this);
1996
1997 // Pop the stack.
1998 EntryStackItem* item = entry_stack_;
1999 entry_stack_ = item->previous_item;
2000
2001 PerIsolateThreadData* previous_thread_data = item->previous_thread_data;
2002 Isolate* previous_isolate = item->previous_isolate;
2003
2004 delete item;
2005
2006 // Reinit the current thread for the isolate it was running before this one.
2007 SetIsolateThreadLocals(previous_isolate, previous_thread_data);
2008}
2009
2010
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002011void Isolate::LinkDeferredHandles(DeferredHandles* deferred) {
2012 deferred->next_ = deferred_handles_head_;
2013 if (deferred_handles_head_ != NULL) {
2014 deferred_handles_head_->previous_ = deferred;
2015 }
2016 deferred_handles_head_ = deferred;
2017}
2018
2019
2020void Isolate::UnlinkDeferredHandles(DeferredHandles* deferred) {
2021#ifdef DEBUG
2022 // In debug mode assert that the linked list is well-formed.
2023 DeferredHandles* deferred_iterator = deferred;
2024 while (deferred_iterator->previous_ != NULL) {
2025 deferred_iterator = deferred_iterator->previous_;
2026 }
2027 ASSERT(deferred_handles_head_ == deferred_iterator);
2028#endif
2029 if (deferred_handles_head_ == deferred) {
2030 deferred_handles_head_ = deferred_handles_head_->next_;
2031 }
2032 if (deferred->next_ != NULL) {
2033 deferred->next_->previous_ = deferred->previous_;
2034 }
2035 if (deferred->previous_ != NULL) {
2036 deferred->previous_->next_ = deferred->next_;
2037 }
2038}
2039
2040
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002041#ifdef DEBUG
2042#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
2043const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_);
2044ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
2045ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
2046#undef ISOLATE_FIELD_OFFSET
2047#endif
2048
2049} } // namespace v8::internal