blob: 5e61761c94ac7bdcefb4f7776c02af6c0c6f2569 [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);
757 if (fun_name->ToBoolean()->IsFalse()) {
758 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();
1045 int limit = static_cast<int>(stack_trace_limit->Number());
1046
1047 Handle<JSArray> stack_trace = CaptureSimpleStackTrace(
1048 exception, factory()->undefined_value(), limit);
1049 JSObject::SetHiddenProperty(exception,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001050 factory()->hidden_stack_trace_string(),
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001051 stack_trace);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001052 return Failure::Exception();
1053}
1054
1055
1056Failure* Isolate::TerminateExecution() {
1057 DoThrow(heap_.termination_exception(), NULL);
1058 return Failure::Exception();
1059}
1060
1061
1062Failure* Isolate::Throw(Object* exception, MessageLocation* location) {
1063 DoThrow(exception, location);
1064 return Failure::Exception();
1065}
1066
1067
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001068Failure* Isolate::ReThrow(MaybeObject* exception) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001069 bool can_be_caught_externally = false;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001070 bool catchable_by_javascript = is_catchable_by_javascript(exception);
1071 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
1072
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001073 thread_local_top()->catcher_ = can_be_caught_externally ?
1074 try_catch_handler() : NULL;
1075
1076 // Set the exception being re-thrown.
1077 set_pending_exception(exception);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001078 if (exception->IsFailure()) return exception->ToFailureUnchecked();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001079 return Failure::Exception();
1080}
1081
1082
1083Failure* Isolate::ThrowIllegalOperation() {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001084 return Throw(heap_.illegal_access_string());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001085}
1086
1087
1088void Isolate::ScheduleThrow(Object* exception) {
1089 // When scheduling a throw we first throw the exception to get the
1090 // error reporting if it is uncaught before rescheduling it.
1091 Throw(exception);
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001092 PropagatePendingExceptionToExternalTryCatch();
1093 if (has_pending_exception()) {
1094 thread_local_top()->scheduled_exception_ = pending_exception();
1095 thread_local_top()->external_caught_exception_ = false;
1096 clear_pending_exception();
1097 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001098}
1099
1100
1101Failure* Isolate::PromoteScheduledException() {
1102 MaybeObject* thrown = scheduled_exception();
1103 clear_scheduled_exception();
1104 // Re-throw the exception to avoid getting repeated error reporting.
1105 return ReThrow(thrown);
1106}
1107
1108
1109void Isolate::PrintCurrentStackTrace(FILE* out) {
1110 StackTraceFrameIterator it(this);
1111 while (!it.done()) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001112 HandleScope scope(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001113 // Find code position if recorded in relocation info.
1114 JavaScriptFrame* frame = it.frame();
1115 int pos = frame->LookupCode()->SourcePosition(frame->pc());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001116 Handle<Object> pos_obj(Smi::FromInt(pos), this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001117 // Fetch function and receiver.
1118 Handle<JSFunction> fun(JSFunction::cast(frame->function()));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001119 Handle<Object> recv(frame->receiver(), this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001120 // Advance to the next JavaScript frame and determine if the
1121 // current frame is the top-level frame.
1122 it.Advance();
1123 Handle<Object> is_top_level = it.done()
1124 ? factory()->true_value()
1125 : factory()->false_value();
1126 // Generate and print stack trace line.
1127 Handle<String> line =
1128 Execution::GetStackTraceLine(recv, fun, pos_obj, is_top_level);
1129 if (line->length() > 0) {
1130 line->PrintOn(out);
1131 fprintf(out, "\n");
1132 }
1133 }
1134}
1135
1136
1137void Isolate::ComputeLocation(MessageLocation* target) {
1138 *target = MessageLocation(Handle<Script>(heap_.empty_script()), -1, -1);
1139 StackTraceFrameIterator it(this);
1140 if (!it.done()) {
1141 JavaScriptFrame* frame = it.frame();
1142 JSFunction* fun = JSFunction::cast(frame->function());
1143 Object* script = fun->shared()->script();
1144 if (script->IsScript() &&
1145 !(Script::cast(script)->source()->IsUndefined())) {
1146 int pos = frame->LookupCode()->SourcePosition(frame->pc());
1147 // Compute the location from the function and the reloc info.
1148 Handle<Script> casted_script(Script::cast(script));
1149 *target = MessageLocation(casted_script, pos, pos + 1);
1150 }
1151 }
1152}
1153
1154
1155bool Isolate::ShouldReportException(bool* can_be_caught_externally,
1156 bool catchable_by_javascript) {
1157 // Find the top-most try-catch handler.
1158 StackHandler* handler =
1159 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001160 while (handler != NULL && !handler->is_catch()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001161 handler = handler->next();
1162 }
1163
1164 // Get the address of the external handler so we can compare the address to
1165 // determine which one is closer to the top of the stack.
1166 Address external_handler_address =
1167 thread_local_top()->try_catch_handler_address();
1168
1169 // The exception has been externally caught if and only if there is
1170 // an external handler which is on top of the top-most try-catch
1171 // handler.
1172 *can_be_caught_externally = external_handler_address != NULL &&
1173 (handler == NULL || handler->address() > external_handler_address ||
1174 !catchable_by_javascript);
1175
1176 if (*can_be_caught_externally) {
1177 // Only report the exception if the external handler is verbose.
1178 return try_catch_handler()->is_verbose_;
1179 } else {
1180 // Report the exception if it isn't caught by JavaScript code.
1181 return handler == NULL;
1182 }
1183}
1184
1185
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001186bool Isolate::IsErrorObject(Handle<Object> obj) {
1187 if (!obj->IsJSObject()) return false;
1188
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001189 String* error_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001190 *(factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("$Error")));
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001191 Object* error_constructor =
1192 js_builtins_object()->GetPropertyNoExceptionThrown(error_key);
1193
1194 for (Object* prototype = *obj; !prototype->IsNull();
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001195 prototype = prototype->GetPrototype(this)) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001196 if (!prototype->IsJSObject()) return false;
1197 if (JSObject::cast(prototype)->map()->constructor() == error_constructor) {
1198 return true;
1199 }
1200 }
1201 return false;
1202}
1203
1204
1205void Isolate::DoThrow(Object* exception, MessageLocation* location) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001206 ASSERT(!has_pending_exception());
1207
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001208 HandleScope scope(this);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001209 Handle<Object> exception_handle(exception, this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001210
1211 // Determine reporting and whether the exception is caught externally.
1212 bool catchable_by_javascript = is_catchable_by_javascript(exception);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001213 bool can_be_caught_externally = false;
1214 bool should_report_exception =
1215 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
1216 bool report_exception = catchable_by_javascript && should_report_exception;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001217 bool try_catch_needs_message =
1218 can_be_caught_externally && try_catch_handler()->capture_message_;
1219 bool bootstrapping = bootstrapper()->IsActive();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001220
1221#ifdef ENABLE_DEBUGGER_SUPPORT
1222 // Notify debugger of exception.
1223 if (catchable_by_javascript) {
1224 debugger_->OnException(exception_handle, report_exception);
1225 }
1226#endif
1227
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001228 // Generate the message if required.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001229 if (report_exception || try_catch_needs_message) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001230 MessageLocation potential_computed_location;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001231 if (location == NULL) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001232 // If no location was specified we use a computed one instead.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001233 ComputeLocation(&potential_computed_location);
1234 location = &potential_computed_location;
1235 }
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001236 // It's not safe to try to make message objects or collect stack traces
1237 // while the bootstrapper is active since the infrastructure may not have
1238 // been properly initialized.
1239 if (!bootstrapping) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001240 Handle<String> stack_trace;
1241 if (FLAG_trace_exception) stack_trace = StackTraceString();
1242 Handle<JSArray> stack_trace_object;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001243 if (capture_stack_trace_for_uncaught_exceptions_) {
1244 if (IsErrorObject(exception_handle)) {
1245 // We fetch the stack trace that corresponds to this error object.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001246 String* key = heap()->hidden_stack_trace_string();
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001247 Object* stack_property =
1248 JSObject::cast(*exception_handle)->GetHiddenProperty(key);
1249 // Property lookup may have failed. In this case it's probably not
1250 // a valid Error object.
1251 if (stack_property->IsJSArray()) {
1252 stack_trace_object = Handle<JSArray>(JSArray::cast(stack_property));
1253 }
1254 }
1255 if (stack_trace_object.is_null()) {
1256 // Not an error object, we capture at throw site.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001257 stack_trace_object = CaptureCurrentStackTrace(
1258 stack_trace_for_uncaught_exceptions_frame_limit_,
1259 stack_trace_for_uncaught_exceptions_options_);
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001260 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001261 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001262
1263 Handle<Object> exception_arg = exception_handle;
1264 // If the exception argument is a custom object, turn it into a string
1265 // before throwing as uncaught exception. Note that the pending
1266 // exception object to be set later must not be turned into a string.
1267 if (exception_arg->IsJSObject() && !IsErrorObject(exception_arg)) {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001268 bool failed = false;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001269 exception_arg = Execution::ToDetailString(exception_arg, &failed);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001270 if (failed) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001271 exception_arg = factory()->InternalizeOneByteString(
1272 STATIC_ASCII_VECTOR("exception"));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001273 }
1274 }
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001275 Handle<Object> message_obj = MessageHandler::MakeMessageObject(
1276 "uncaught_exception",
1277 location,
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001278 HandleVector<Object>(&exception_arg, 1),
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001279 stack_trace,
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001280 stack_trace_object);
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001281 thread_local_top()->pending_message_obj_ = *message_obj;
1282 if (location != NULL) {
1283 thread_local_top()->pending_message_script_ = *location->script();
1284 thread_local_top()->pending_message_start_pos_ = location->start_pos();
1285 thread_local_top()->pending_message_end_pos_ = location->end_pos();
1286 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001287 } else if (location != NULL && !location->script().is_null()) {
1288 // We are bootstrapping and caught an error where the location is set
1289 // and we have a script for the location.
1290 // In this case we could have an extension (or an internal error
1291 // somewhere) and we print out the line number at which the error occured
1292 // to the console for easier debugging.
1293 int line_number = GetScriptLineNumberSafe(location->script(),
1294 location->start_pos());
verwaest@chromium.org37141392012-05-31 13:27:02 +00001295 if (exception->IsString()) {
1296 OS::PrintError(
1297 "Extension or internal compilation error: %s in %s at line %d.\n",
1298 *String::cast(exception)->ToCString(),
1299 *String::cast(location->script()->name())->ToCString(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001300 line_number + 1);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001301 } else {
1302 OS::PrintError(
1303 "Extension or internal compilation error in %s at line %d.\n",
1304 *String::cast(location->script()->name())->ToCString(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001305 line_number + 1);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001306 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001307 }
1308 }
1309
1310 // Save the message for reporting if the the exception remains uncaught.
1311 thread_local_top()->has_pending_message_ = report_exception;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001312
1313 // Do not forget to clean catcher_ if currently thrown exception cannot
1314 // be caught. If necessary, ReThrow will update the catcher.
1315 thread_local_top()->catcher_ = can_be_caught_externally ?
1316 try_catch_handler() : NULL;
1317
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001318 set_pending_exception(*exception_handle);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001319}
1320
1321
1322bool Isolate::IsExternallyCaught() {
1323 ASSERT(has_pending_exception());
1324
1325 if ((thread_local_top()->catcher_ == NULL) ||
1326 (try_catch_handler() != thread_local_top()->catcher_)) {
1327 // When throwing the exception, we found no v8::TryCatch
1328 // which should care about this exception.
1329 return false;
1330 }
1331
1332 if (!is_catchable_by_javascript(pending_exception())) {
1333 return true;
1334 }
1335
1336 // Get the address of the external handler so we can compare the address to
1337 // determine which one is closer to the top of the stack.
1338 Address external_handler_address =
1339 thread_local_top()->try_catch_handler_address();
1340 ASSERT(external_handler_address != NULL);
1341
1342 // The exception has been externally caught if and only if there is
1343 // an external handler which is on top of the top-most try-finally
1344 // handler.
1345 // There should be no try-catch blocks as they would prohibit us from
1346 // finding external catcher in the first place (see catcher_ check above).
1347 //
1348 // Note, that finally clause would rethrow an exception unless it's
1349 // aborted by jumps in control flow like return, break, etc. and we'll
1350 // have another chances to set proper v8::TryCatch.
1351 StackHandler* handler =
1352 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
1353 while (handler != NULL && handler->address() < external_handler_address) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001354 ASSERT(!handler->is_catch());
1355 if (handler->is_finally()) return false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001356
1357 handler = handler->next();
1358 }
1359
1360 return true;
1361}
1362
1363
1364void Isolate::ReportPendingMessages() {
1365 ASSERT(has_pending_exception());
1366 PropagatePendingExceptionToExternalTryCatch();
1367
1368 // If the pending exception is OutOfMemoryException set out_of_memory in
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001369 // the native context. Note: We have to mark the native context here
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001370 // since the GenerateThrowOutOfMemory stub cannot make a RuntimeCall to
1371 // set it.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001372 HandleScope scope(this);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001373 if (thread_local_top_.pending_exception_->IsOutOfMemory()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001374 context()->mark_out_of_memory();
1375 } else if (thread_local_top_.pending_exception_ ==
1376 heap()->termination_exception()) {
1377 // Do nothing: if needed, the exception has been already propagated to
1378 // v8::TryCatch.
1379 } else {
1380 if (thread_local_top_.has_pending_message_) {
1381 thread_local_top_.has_pending_message_ = false;
1382 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001383 HandleScope scope(this);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001384 Handle<Object> message_obj(thread_local_top_.pending_message_obj_,
1385 this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001386 if (thread_local_top_.pending_message_script_ != NULL) {
1387 Handle<Script> script(thread_local_top_.pending_message_script_);
1388 int start_pos = thread_local_top_.pending_message_start_pos_;
1389 int end_pos = thread_local_top_.pending_message_end_pos_;
1390 MessageLocation location(script, start_pos, end_pos);
1391 MessageHandler::ReportMessage(this, &location, message_obj);
1392 } else {
1393 MessageHandler::ReportMessage(this, NULL, message_obj);
1394 }
1395 }
1396 }
1397 }
1398 clear_pending_message();
1399}
1400
1401
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001402MessageLocation Isolate::GetMessageLocation() {
1403 ASSERT(has_pending_exception());
1404
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001405 if (!thread_local_top_.pending_exception_->IsOutOfMemory() &&
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001406 thread_local_top_.pending_exception_ != heap()->termination_exception() &&
1407 thread_local_top_.has_pending_message_ &&
1408 !thread_local_top_.pending_message_obj_->IsTheHole() &&
1409 thread_local_top_.pending_message_script_ != NULL) {
1410 Handle<Script> script(thread_local_top_.pending_message_script_);
1411 int start_pos = thread_local_top_.pending_message_start_pos_;
1412 int end_pos = thread_local_top_.pending_message_end_pos_;
1413 return MessageLocation(script, start_pos, end_pos);
1414 }
1415
1416 return MessageLocation();
1417}
1418
1419
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001420void Isolate::TraceException(bool flag) {
1421 FLAG_trace_exception = flag; // TODO(isolates): This is an unfortunate use.
1422}
1423
1424
1425bool Isolate::OptionalRescheduleException(bool is_bottom_call) {
1426 ASSERT(has_pending_exception());
1427 PropagatePendingExceptionToExternalTryCatch();
1428
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001429 // Always reschedule out of memory exceptions.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001430 if (!is_out_of_memory()) {
1431 bool is_termination_exception =
1432 pending_exception() == heap_.termination_exception();
1433
1434 // Do not reschedule the exception if this is the bottom call.
1435 bool clear_exception = is_bottom_call;
1436
1437 if (is_termination_exception) {
1438 if (is_bottom_call) {
1439 thread_local_top()->external_caught_exception_ = false;
1440 clear_pending_exception();
1441 return false;
1442 }
1443 } else if (thread_local_top()->external_caught_exception_) {
1444 // If the exception is externally caught, clear it if there are no
1445 // JavaScript frames on the way to the C++ frame that has the
1446 // external handler.
1447 ASSERT(thread_local_top()->try_catch_handler_address() != NULL);
1448 Address external_handler_address =
1449 thread_local_top()->try_catch_handler_address();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001450 JavaScriptFrameIterator it(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001451 if (it.done() || (it.frame()->sp() > external_handler_address)) {
1452 clear_exception = true;
1453 }
1454 }
1455
1456 // Clear the exception if needed.
1457 if (clear_exception) {
1458 thread_local_top()->external_caught_exception_ = false;
1459 clear_pending_exception();
1460 return false;
1461 }
1462 }
1463
1464 // Reschedule the exception.
1465 thread_local_top()->scheduled_exception_ = pending_exception();
1466 clear_pending_exception();
1467 return true;
1468}
1469
1470
1471void Isolate::SetCaptureStackTraceForUncaughtExceptions(
1472 bool capture,
1473 int frame_limit,
1474 StackTrace::StackTraceOptions options) {
1475 capture_stack_trace_for_uncaught_exceptions_ = capture;
1476 stack_trace_for_uncaught_exceptions_frame_limit_ = frame_limit;
1477 stack_trace_for_uncaught_exceptions_options_ = options;
1478}
1479
1480
1481bool Isolate::is_out_of_memory() {
1482 if (has_pending_exception()) {
1483 MaybeObject* e = pending_exception();
1484 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1485 return true;
1486 }
1487 }
1488 if (has_scheduled_exception()) {
1489 MaybeObject* e = scheduled_exception();
1490 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1491 return true;
1492 }
1493 }
1494 return false;
1495}
1496
1497
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001498Handle<Context> Isolate::native_context() {
1499 GlobalObject* global = thread_local_top()->context_->global_object();
1500 return Handle<Context>(global->native_context());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001501}
1502
1503
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001504Handle<Context> Isolate::global_context() {
1505 GlobalObject* global = thread_local_top()->context_->global_object();
1506 return Handle<Context>(global->global_context());
1507}
1508
1509
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001510Handle<Context> Isolate::GetCallingNativeContext() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001511 JavaScriptFrameIterator it(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001512#ifdef ENABLE_DEBUGGER_SUPPORT
1513 if (debug_->InDebugger()) {
1514 while (!it.done()) {
1515 JavaScriptFrame* frame = it.frame();
1516 Context* context = Context::cast(frame->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001517 if (context->native_context() == *debug_->debug_context()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001518 it.Advance();
1519 } else {
1520 break;
1521 }
1522 }
1523 }
1524#endif // ENABLE_DEBUGGER_SUPPORT
1525 if (it.done()) return Handle<Context>::null();
1526 JavaScriptFrame* frame = it.frame();
1527 Context* context = Context::cast(frame->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001528 return Handle<Context>(context->native_context());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001529}
1530
1531
1532char* Isolate::ArchiveThread(char* to) {
1533 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1534 RuntimeProfiler::IsolateExitedJS(this);
1535 }
1536 memcpy(to, reinterpret_cast<char*>(thread_local_top()),
1537 sizeof(ThreadLocalTop));
1538 InitializeThreadLocal();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001539 clear_pending_exception();
1540 clear_pending_message();
1541 clear_scheduled_exception();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001542 return to + sizeof(ThreadLocalTop);
1543}
1544
1545
1546char* Isolate::RestoreThread(char* from) {
1547 memcpy(reinterpret_cast<char*>(thread_local_top()), from,
1548 sizeof(ThreadLocalTop));
1549 // This might be just paranoia, but it seems to be needed in case a
1550 // thread_local_top_ is restored on a separate OS thread.
1551#ifdef USE_SIMULATOR
1552#ifdef V8_TARGET_ARCH_ARM
1553 thread_local_top()->simulator_ = Simulator::current(this);
1554#elif V8_TARGET_ARCH_MIPS
1555 thread_local_top()->simulator_ = Simulator::current(this);
1556#endif
1557#endif
1558 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1559 RuntimeProfiler::IsolateEnteredJS(this);
1560 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001561 ASSERT(context() == NULL || context()->IsContext());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001562 return from + sizeof(ThreadLocalTop);
1563}
1564
1565
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001566Isolate::ThreadDataTable::ThreadDataTable()
1567 : list_(NULL) {
1568}
1569
1570
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00001571Isolate::ThreadDataTable::~ThreadDataTable() {
1572 // TODO(svenpanne) The assertion below would fire if an embedder does not
1573 // cleanly dispose all Isolates before disposing v8, so we are conservative
1574 // and leave it out for now.
1575 // ASSERT_EQ(NULL, list_);
1576}
1577
1578
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001579Isolate::PerIsolateThreadData*
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001580 Isolate::ThreadDataTable::Lookup(Isolate* isolate,
1581 ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001582 for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) {
1583 if (data->Matches(isolate, thread_id)) return data;
1584 }
1585 return NULL;
1586}
1587
1588
1589void Isolate::ThreadDataTable::Insert(Isolate::PerIsolateThreadData* data) {
1590 if (list_ != NULL) list_->prev_ = data;
1591 data->next_ = list_;
1592 list_ = data;
1593}
1594
1595
1596void Isolate::ThreadDataTable::Remove(PerIsolateThreadData* data) {
1597 if (list_ == data) list_ = data->next_;
1598 if (data->next_ != NULL) data->next_->prev_ = data->prev_;
1599 if (data->prev_ != NULL) data->prev_->next_ = data->next_;
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001600 delete data;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001601}
1602
1603
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001604void Isolate::ThreadDataTable::Remove(Isolate* isolate,
1605 ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001606 PerIsolateThreadData* data = Lookup(isolate, thread_id);
1607 if (data != NULL) {
1608 Remove(data);
1609 }
1610}
1611
1612
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001613void Isolate::ThreadDataTable::RemoveAllThreads(Isolate* isolate) {
1614 PerIsolateThreadData* data = list_;
1615 while (data != NULL) {
1616 PerIsolateThreadData* next = data->next_;
1617 if (data->isolate() == isolate) Remove(data);
1618 data = next;
1619 }
1620}
1621
1622
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001623#ifdef DEBUG
1624#define TRACE_ISOLATE(tag) \
1625 do { \
1626 if (FLAG_trace_isolates) { \
ulan@chromium.org750145a2013-03-07 15:14:13 +00001627 PrintF("Isolate %p (id %d)" #tag "\n", \
1628 reinterpret_cast<void*>(this), id()); \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001629 } \
1630 } while (false)
1631#else
1632#define TRACE_ISOLATE(tag)
1633#endif
1634
1635
1636Isolate::Isolate()
1637 : state_(UNINITIALIZED),
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001638 embedder_data_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001639 entry_stack_(NULL),
1640 stack_trace_nesting_level_(0),
1641 incomplete_message_(NULL),
1642 preallocated_memory_thread_(NULL),
1643 preallocated_message_space_(NULL),
1644 bootstrapper_(NULL),
1645 runtime_profiler_(NULL),
1646 compilation_cache_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001647 counters_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001648 code_range_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001649 // Must be initialized early to allow v8::SetResourceConstraints calls.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001650 break_access_(OS::CreateMutex()),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001651 debugger_initialized_(false),
1652 // Must be initialized early to allow v8::Debug calls.
1653 debugger_access_(OS::CreateMutex()),
1654 logger_(NULL),
1655 stats_table_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001656 stub_cache_(NULL),
1657 deoptimizer_data_(NULL),
1658 capture_stack_trace_for_uncaught_exceptions_(false),
1659 stack_trace_for_uncaught_exceptions_frame_limit_(0),
1660 stack_trace_for_uncaught_exceptions_options_(StackTrace::kOverview),
1661 transcendental_cache_(NULL),
1662 memory_allocator_(NULL),
1663 keyed_lookup_cache_(NULL),
1664 context_slot_cache_(NULL),
1665 descriptor_lookup_cache_(NULL),
1666 handle_scope_implementer_(NULL),
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001667 unicode_cache_(NULL),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001668 runtime_zone_(this),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001669 in_use_list_(0),
1670 free_list_(0),
1671 preallocated_storage_preallocated_(false),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001672 inner_pointer_to_code_cache_(NULL),
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00001673 write_iterator_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001674 global_handles_(NULL),
1675 context_switcher_(NULL),
1676 thread_manager_(NULL),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001677 fp_stubs_generated_(false),
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001678 has_installed_extensions_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001679 string_tracker_(NULL),
1680 regexp_stack_(NULL),
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001681 date_cache_(NULL),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001682 code_stub_interface_descriptors_(NULL),
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001683 context_exit_happened_(false),
1684 deferred_handles_head_(NULL),
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001685 optimizing_compiler_thread_(this),
1686 marking_thread_(NULL),
1687 sweeper_thread_(NULL) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00001688 id_ = NoBarrier_AtomicIncrement(&isolate_counter_, 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001689 TRACE_ISOLATE(constructor);
1690
1691 memset(isolate_addresses_, 0,
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001692 sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001693
1694 heap_.isolate_ = this;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001695 stack_guard_.isolate_ = this;
1696
lrn@chromium.org1c092762011-05-09 09:42:16 +00001697 // ThreadManager is initialized early to support locking an isolate
1698 // before it is entered.
1699 thread_manager_ = new ThreadManager();
1700 thread_manager_->isolate_ = this;
1701
lrn@chromium.org7516f052011-03-30 08:52:27 +00001702#if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \
1703 defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001704 simulator_initialized_ = false;
1705 simulator_i_cache_ = NULL;
1706 simulator_redirection_ = NULL;
1707#endif
1708
1709#ifdef DEBUG
1710 // heap_histograms_ initializes itself.
1711 memset(&js_spill_information_, 0, sizeof(js_spill_information_));
1712 memset(code_kind_statistics_, 0,
1713 sizeof(code_kind_statistics_[0]) * Code::NUMBER_OF_KINDS);
yangguo@chromium.org003650e2013-01-24 16:31:08 +00001714
1715 allow_handle_deref_ = true;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001716#endif
1717
1718#ifdef ENABLE_DEBUGGER_SUPPORT
1719 debug_ = NULL;
1720 debugger_ = NULL;
1721#endif
1722
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001723 handle_scope_data_.Initialize();
1724
1725#define ISOLATE_INIT_EXECUTE(type, name, initial_value) \
1726 name##_ = (initial_value);
1727 ISOLATE_INIT_LIST(ISOLATE_INIT_EXECUTE)
1728#undef ISOLATE_INIT_EXECUTE
1729
1730#define ISOLATE_INIT_ARRAY_EXECUTE(type, name, length) \
1731 memset(name##_, 0, sizeof(type) * length);
1732 ISOLATE_INIT_ARRAY_LIST(ISOLATE_INIT_ARRAY_EXECUTE)
1733#undef ISOLATE_INIT_ARRAY_EXECUTE
1734}
1735
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001736
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001737void Isolate::TearDown() {
1738 TRACE_ISOLATE(tear_down);
1739
1740 // Temporarily set this isolate as current so that various parts of
1741 // the isolate can access it in their destructors without having a
1742 // direct pointer. We don't use Enter/Exit here to avoid
1743 // initializing the thread data.
1744 PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData();
1745 Isolate* saved_isolate = UncheckedCurrent();
1746 SetIsolateThreadLocals(this, NULL);
1747
1748 Deinit();
1749
danno@chromium.org8c0a43f2012-04-03 08:37:53 +00001750 { ScopedLock lock(process_wide_mutex_);
1751 thread_data_table_->RemoveAllThreads(this);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001752 }
1753
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001754 if (serialize_partial_snapshot_cache_ != NULL) {
1755 delete[] serialize_partial_snapshot_cache_;
1756 serialize_partial_snapshot_cache_ = NULL;
1757 }
1758
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001759 if (!IsDefaultIsolate()) {
1760 delete this;
1761 }
1762
1763 // Restore the previous current isolate.
1764 SetIsolateThreadLocals(saved_isolate, saved_data);
1765}
1766
1767
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00001768void Isolate::GlobalTearDown() {
1769 delete thread_data_table_;
1770}
1771
1772
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001773void Isolate::Deinit() {
1774 if (state_ == INITIALIZED) {
1775 TRACE_ISOLATE(deinit);
1776
ulan@chromium.org750145a2013-03-07 15:14:13 +00001777 if (FLAG_parallel_recompilation) optimizing_compiler_thread_.Stop();
1778
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001779 if (FLAG_sweeper_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001780 for (int i = 0; i < FLAG_sweeper_threads; i++) {
1781 sweeper_thread_[i]->Stop();
1782 delete sweeper_thread_[i];
1783 }
1784 delete[] sweeper_thread_;
1785 }
1786
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001787 if (FLAG_marking_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001788 for (int i = 0; i < FLAG_marking_threads; i++) {
1789 marking_thread_[i]->Stop();
1790 delete marking_thread_[i];
1791 }
1792 delete[] marking_thread_;
1793 }
1794
ulan@chromium.org750145a2013-03-07 15:14:13 +00001795 if (FLAG_hydrogen_stats) GetHStatistics()->Print();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001796
1797 // We must stop the logger before we tear down other components.
1798 logger_->EnsureTickerStopped();
1799
1800 delete deoptimizer_data_;
1801 deoptimizer_data_ = NULL;
1802 if (FLAG_preemption) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001803 v8::Locker locker(reinterpret_cast<v8::Isolate*>(this));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001804 v8::Locker::StopPreemption();
1805 }
1806 builtins_.TearDown();
1807 bootstrapper_->TearDown();
1808
1809 // Remove the external reference to the preallocated stack memory.
1810 delete preallocated_message_space_;
1811 preallocated_message_space_ = NULL;
1812 PreallocatedMemoryThreadStop();
1813
1814 HeapProfiler::TearDown();
1815 CpuProfiler::TearDown();
1816 if (runtime_profiler_ != NULL) {
1817 runtime_profiler_->TearDown();
1818 delete runtime_profiler_;
1819 runtime_profiler_ = NULL;
1820 }
1821 heap_.TearDown();
1822 logger_->TearDown();
1823
1824 // The default isolate is re-initializable due to legacy API.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001825 state_ = UNINITIALIZED;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001826 }
1827}
1828
1829
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001830void Isolate::PushToPartialSnapshotCache(Object* obj) {
1831 int length = serialize_partial_snapshot_cache_length();
1832 int capacity = serialize_partial_snapshot_cache_capacity();
1833
1834 if (length >= capacity) {
1835 int new_capacity = static_cast<int>((capacity + 10) * 1.2);
1836 Object** new_array = new Object*[new_capacity];
1837 for (int i = 0; i < length; i++) {
1838 new_array[i] = serialize_partial_snapshot_cache()[i];
1839 }
1840 if (capacity != 0) delete[] serialize_partial_snapshot_cache();
1841 set_serialize_partial_snapshot_cache(new_array);
1842 set_serialize_partial_snapshot_cache_capacity(new_capacity);
1843 }
1844
1845 serialize_partial_snapshot_cache()[length] = obj;
1846 set_serialize_partial_snapshot_cache_length(length + 1);
1847}
1848
1849
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001850void Isolate::SetIsolateThreadLocals(Isolate* isolate,
1851 PerIsolateThreadData* data) {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +00001852 Thread::SetThreadLocal(isolate_key_, isolate);
1853 Thread::SetThreadLocal(per_isolate_thread_data_key_, data);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001854}
1855
1856
1857Isolate::~Isolate() {
1858 TRACE_ISOLATE(destructor);
1859
danno@chromium.orgb6451162011-08-17 14:33:23 +00001860 // Has to be called while counters_ are still alive.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001861 runtime_zone_.DeleteKeptSegment();
danno@chromium.orgb6451162011-08-17 14:33:23 +00001862
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001863 delete[] assembler_spare_buffer_;
1864 assembler_spare_buffer_ = NULL;
1865
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001866 delete unicode_cache_;
1867 unicode_cache_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001868
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001869 delete date_cache_;
1870 date_cache_ = NULL;
1871
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001872 delete[] code_stub_interface_descriptors_;
1873 code_stub_interface_descriptors_ = NULL;
1874
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001875 delete regexp_stack_;
1876 regexp_stack_ = NULL;
1877
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001878 delete descriptor_lookup_cache_;
1879 descriptor_lookup_cache_ = NULL;
1880 delete context_slot_cache_;
1881 context_slot_cache_ = NULL;
1882 delete keyed_lookup_cache_;
1883 keyed_lookup_cache_ = NULL;
1884
1885 delete transcendental_cache_;
1886 transcendental_cache_ = NULL;
1887 delete stub_cache_;
1888 stub_cache_ = NULL;
1889 delete stats_table_;
1890 stats_table_ = NULL;
1891
1892 delete logger_;
1893 logger_ = NULL;
1894
1895 delete counters_;
1896 counters_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001897
1898 delete handle_scope_implementer_;
1899 handle_scope_implementer_ = NULL;
1900 delete break_access_;
1901 break_access_ = NULL;
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001902 delete debugger_access_;
1903 debugger_access_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001904
1905 delete compilation_cache_;
1906 compilation_cache_ = NULL;
1907 delete bootstrapper_;
1908 bootstrapper_ = NULL;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001909 delete inner_pointer_to_code_cache_;
1910 inner_pointer_to_code_cache_ = NULL;
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00001911 delete write_iterator_;
1912 write_iterator_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001913
1914 delete context_switcher_;
1915 context_switcher_ = NULL;
1916 delete thread_manager_;
1917 thread_manager_ = NULL;
1918
1919 delete string_tracker_;
1920 string_tracker_ = NULL;
1921
1922 delete memory_allocator_;
1923 memory_allocator_ = NULL;
1924 delete code_range_;
1925 code_range_ = NULL;
1926 delete global_handles_;
1927 global_handles_ = NULL;
1928
danno@chromium.orgb6451162011-08-17 14:33:23 +00001929 delete external_reference_table_;
1930 external_reference_table_ = NULL;
1931
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001932#ifdef ENABLE_DEBUGGER_SUPPORT
1933 delete debugger_;
1934 debugger_ = NULL;
1935 delete debug_;
1936 debug_ = NULL;
1937#endif
1938}
1939
1940
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001941void Isolate::InitializeThreadLocal() {
lrn@chromium.org1c092762011-05-09 09:42:16 +00001942 thread_local_top_.isolate_ = this;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001943 thread_local_top_.Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001944}
1945
1946
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001947void Isolate::PropagatePendingExceptionToExternalTryCatch() {
1948 ASSERT(has_pending_exception());
1949
1950 bool external_caught = IsExternallyCaught();
1951 thread_local_top_.external_caught_exception_ = external_caught;
1952
1953 if (!external_caught) return;
1954
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001955 if (thread_local_top_.pending_exception_->IsOutOfMemory()) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001956 // Do not propagate OOM exception: we should kill VM asap.
1957 } else if (thread_local_top_.pending_exception_ ==
1958 heap()->termination_exception()) {
1959 try_catch_handler()->can_continue_ = false;
1960 try_catch_handler()->exception_ = heap()->null_value();
1961 } else {
1962 // At this point all non-object (failure) exceptions have
1963 // been dealt with so this shouldn't fail.
1964 ASSERT(!pending_exception()->IsFailure());
1965 try_catch_handler()->can_continue_ = true;
1966 try_catch_handler()->exception_ = pending_exception();
1967 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
1968 try_catch_handler()->message_ = thread_local_top_.pending_message_obj_;
1969 }
1970 }
1971}
1972
1973
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001974void Isolate::InitializeLoggingAndCounters() {
1975 if (logger_ == NULL) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001976 logger_ = new Logger(this);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001977 }
1978 if (counters_ == NULL) {
1979 counters_ = new Counters;
1980 }
1981}
1982
1983
1984void Isolate::InitializeDebugger() {
1985#ifdef ENABLE_DEBUGGER_SUPPORT
1986 ScopedLock lock(debugger_access_);
1987 if (NoBarrier_Load(&debugger_initialized_)) return;
1988 InitializeLoggingAndCounters();
1989 debug_ = new Debug(this);
1990 debugger_ = new Debugger(this);
1991 Release_Store(&debugger_initialized_, true);
1992#endif
1993}
1994
1995
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001996bool Isolate::Init(Deserializer* des) {
1997 ASSERT(state_ != INITIALIZED);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001998 ASSERT(Isolate::Current() == this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001999 TRACE_ISOLATE(init);
2000
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002001 // The initialization process does not handle memory exhaustion.
2002 DisallowAllocationFailure disallow_allocation_failure;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002003
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002004 InitializeLoggingAndCounters();
2005
2006 InitializeDebugger();
2007
2008 memory_allocator_ = new MemoryAllocator(this);
2009 code_range_ = new CodeRange(this);
2010
2011 // Safe after setting Heap::isolate_, initializing StackGuard and
2012 // ensuring that Isolate::Current() == this.
2013 heap_.SetStackLimits();
2014
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00002015#define ASSIGN_ELEMENT(CamelName, hacker_name) \
2016 isolate_addresses_[Isolate::k##CamelName##Address] = \
2017 reinterpret_cast<Address>(hacker_name##_address());
2018 FOR_EACH_ISOLATE_ADDRESS_NAME(ASSIGN_ELEMENT)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002019#undef C
2020
2021 string_tracker_ = new StringTracker();
2022 string_tracker_->isolate_ = this;
2023 compilation_cache_ = new CompilationCache(this);
2024 transcendental_cache_ = new TranscendentalCache();
2025 keyed_lookup_cache_ = new KeyedLookupCache();
2026 context_slot_cache_ = new ContextSlotCache();
2027 descriptor_lookup_cache_ = new DescriptorLookupCache();
2028 unicode_cache_ = new UnicodeCache();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002029 inner_pointer_to_code_cache_ = new InnerPointerToCodeCache(this);
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00002030 write_iterator_ = new ConsStringIteratorOp();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002031 global_handles_ = new GlobalHandles(this);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002032 bootstrapper_ = new Bootstrapper(this);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002033 handle_scope_implementer_ = new HandleScopeImplementer(this);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002034 stub_cache_ = new StubCache(this, runtime_zone());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002035 regexp_stack_ = new RegExpStack();
2036 regexp_stack_->isolate_ = this;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00002037 date_cache_ = new DateCache();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002038 code_stub_interface_descriptors_ =
2039 new CodeStubInterfaceDescriptor[CodeStub::NUMBER_OF_IDS];
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002040
2041 // Enable logging before setting up the heap
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002042 logger_->SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002043
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002044 CpuProfiler::SetUp();
2045 HeapProfiler::SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002046
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002047 // Initialize other runtime facilities
2048#if defined(USE_SIMULATOR)
lrn@chromium.org7516f052011-03-30 08:52:27 +00002049#if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS)
lrn@chromium.org1c092762011-05-09 09:42:16 +00002050 Simulator::Initialize(this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002051#endif
2052#endif
2053
2054 { // NOLINT
2055 // Ensure that the thread has a valid stack guard. The v8::Locker object
2056 // will ensure this too, but we don't have to use lockers if we are only
2057 // using one thread.
2058 ExecutionAccess lock(this);
2059 stack_guard_.InitThread(lock);
2060 }
2061
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002062 // SetUp the object heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002063 ASSERT(!heap_.HasBeenSetUp());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002064 if (!heap_.SetUp()) {
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00002065 V8::FatalProcessOutOfMemory("heap setup");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002066 return false;
2067 }
2068
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002069 deoptimizer_data_ = new DeoptimizerData;
2070
2071 const bool create_heap_objects = (des == NULL);
2072 if (create_heap_objects && !heap_.CreateHeapObjects()) {
2073 V8::FatalProcessOutOfMemory("heap object creation");
2074 return false;
2075 }
2076
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002077 if (create_heap_objects) {
2078 // Terminate the cache array with the sentinel so we can iterate.
2079 PushToPartialSnapshotCache(heap_.undefined_value());
2080 }
2081
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00002082 InitializeThreadLocal();
2083
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002084 bootstrapper_->Initialize(create_heap_objects);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002085 builtins_.SetUp(create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002086
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002087 // Only preallocate on the first initialization.
2088 if (FLAG_preallocate_message_memory && preallocated_message_space_ == NULL) {
2089 // Start the thread which will set aside some memory.
2090 PreallocatedMemoryThreadStart();
2091 preallocated_message_space_ =
2092 new NoAllocationStringAllocator(
2093 preallocated_memory_thread_->data(),
2094 preallocated_memory_thread_->length());
2095 PreallocatedStorageInit(preallocated_memory_thread_->length() / 4);
2096 }
2097
2098 if (FLAG_preemption) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002099 v8::Locker locker(reinterpret_cast<v8::Isolate*>(this));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002100 v8::Locker::StartPreemption(100);
2101 }
2102
2103#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002104 debug_->SetUp(create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002105#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002106
2107 // If we are deserializing, read the state into the now-empty heap.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002108 if (!create_heap_objects) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002109 des->Deserialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002110 }
ulan@chromium.org812308e2012-02-29 15:58:45 +00002111 stub_cache_->Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002112
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002113 // Finish initialization of ThreadLocal after deserialization is done.
2114 clear_pending_exception();
2115 clear_pending_message();
2116 clear_scheduled_exception();
2117
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002118 // Deserializing may put strange things in the root array's copy of the
2119 // stack guard.
2120 heap_.SetStackLimits();
2121
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002122 // Quiet the heap NaN if needed on target platform.
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00002123 if (!create_heap_objects) Assembler::QuietNaN(heap_.nan_value());
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002124
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002125 runtime_profiler_ = new RuntimeProfiler(this);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002126 runtime_profiler_->SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002127
2128 // If we are deserializing, log non-function code objects and compiled
2129 // functions found in the snapshot.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00002130 if (!create_heap_objects &&
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002131 (FLAG_log_code || FLAG_ll_prof || logger_->is_logging_code_events())) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002132 HandleScope scope(this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002133 LOG(this, LogCodeObjects());
2134 LOG(this, LogCompiledFunctions());
2135 }
2136
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002137 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, state_)),
2138 Internals::kIsolateStateOffset);
2139 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, embedder_data_)),
2140 Internals::kIsolateEmbedderDataOffset);
2141 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, heap_.roots_)),
2142 Internals::kIsolateRootsOffset);
2143
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002144 state_ = INITIALIZED;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002145 time_millis_at_init_ = OS::TimeCurrentMillis();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002146
2147 if (!create_heap_objects) {
2148 // Now that the heap is consistent, it's OK to generate the code for the
2149 // deopt entry table that might have been referred to by optimized code in
2150 // the snapshot.
2151 HandleScope scope(this);
2152 Deoptimizer::EnsureCodeForDeoptimizationEntry(
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002153 this,
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002154 Deoptimizer::LAZY,
2155 kDeoptTableSerializeEntryCount - 1);
2156 }
2157
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002158 if (!Serializer::enabled()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002159 // Ensure that all stubs which need to be generated ahead of time, but
2160 // cannot be serialized into the snapshot have been generated.
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002161 HandleScope scope(this);
ulan@chromium.org750145a2013-03-07 15:14:13 +00002162 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(this);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002163 CodeStub::GenerateFPStubs(this);
2164 StubFailureTrampolineStub::GenerateAheadOfTime(this);
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002165 }
2166
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002167 if (FLAG_parallel_recompilation) optimizing_compiler_thread_.Start();
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002168
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002169 if (FLAG_parallel_marking && FLAG_marking_threads == 0) {
2170 FLAG_marking_threads = SystemThreadManager::
2171 NumberOfParallelSystemThreads(
2172 SystemThreadManager::PARALLEL_MARKING);
2173 }
2174 if (FLAG_marking_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002175 marking_thread_ = new MarkingThread*[FLAG_marking_threads];
2176 for (int i = 0; i < FLAG_marking_threads; i++) {
2177 marking_thread_[i] = new MarkingThread(this);
2178 marking_thread_[i]->Start();
2179 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002180 } else {
2181 FLAG_parallel_marking = false;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002182 }
2183
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002184 if (FLAG_sweeper_threads == 0) {
2185 if (FLAG_concurrent_sweeping) {
2186 FLAG_sweeper_threads = SystemThreadManager::
2187 NumberOfParallelSystemThreads(
2188 SystemThreadManager::CONCURRENT_SWEEPING);
2189 } else if (FLAG_parallel_sweeping) {
2190 FLAG_sweeper_threads = SystemThreadManager::
2191 NumberOfParallelSystemThreads(
2192 SystemThreadManager::PARALLEL_SWEEPING);
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002193 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002194 }
2195 if (FLAG_sweeper_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002196 sweeper_thread_ = new SweeperThread*[FLAG_sweeper_threads];
2197 for (int i = 0; i < FLAG_sweeper_threads; i++) {
2198 sweeper_thread_[i] = new SweeperThread(this);
2199 sweeper_thread_[i]->Start();
2200 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002201 } else {
2202 FLAG_concurrent_sweeping = false;
2203 FLAG_parallel_sweeping = false;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002204 }
ulan@chromium.org750145a2013-03-07 15:14:13 +00002205 if (FLAG_parallel_recompilation &&
2206 SystemThreadManager::NumberOfParallelSystemThreads(
2207 SystemThreadManager::PARALLEL_RECOMPILATION) == 0) {
2208 FLAG_parallel_recompilation = false;
2209 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002210 return true;
2211}
2212
2213
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002214// Initialized lazily to allow early
2215// v8::V8::SetAddHistogramSampleFunction calls.
2216StatsTable* Isolate::stats_table() {
2217 if (stats_table_ == NULL) {
2218 stats_table_ = new StatsTable;
2219 }
2220 return stats_table_;
2221}
2222
2223
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002224void Isolate::Enter() {
2225 Isolate* current_isolate = NULL;
2226 PerIsolateThreadData* current_data = CurrentPerIsolateThreadData();
2227 if (current_data != NULL) {
2228 current_isolate = current_data->isolate_;
2229 ASSERT(current_isolate != NULL);
2230 if (current_isolate == this) {
2231 ASSERT(Current() == this);
2232 ASSERT(entry_stack_ != NULL);
2233 ASSERT(entry_stack_->previous_thread_data == NULL ||
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00002234 entry_stack_->previous_thread_data->thread_id().Equals(
2235 ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002236 // Same thread re-enters the isolate, no need to re-init anything.
2237 entry_stack_->entry_count++;
2238 return;
2239 }
2240 }
2241
2242 // Threads can have default isolate set into TLS as Current but not yet have
2243 // PerIsolateThreadData for it, as it requires more advanced phase of the
2244 // initialization. For example, a thread might be the one that system used for
2245 // static initializers - in this case the default isolate is set in TLS but
2246 // the thread did not yet Enter the isolate. If PerisolateThreadData is not
2247 // there, use the isolate set in TLS.
2248 if (current_isolate == NULL) {
2249 current_isolate = Isolate::UncheckedCurrent();
2250 }
2251
2252 PerIsolateThreadData* data = FindOrAllocatePerThreadDataForThisThread();
2253 ASSERT(data != NULL);
2254 ASSERT(data->isolate_ == this);
2255
2256 EntryStackItem* item = new EntryStackItem(current_data,
2257 current_isolate,
2258 entry_stack_);
2259 entry_stack_ = item;
2260
2261 SetIsolateThreadLocals(this, data);
2262
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002263 // In case it's the first time some thread enters the isolate.
2264 set_thread_id(data->thread_id());
2265}
2266
2267
2268void Isolate::Exit() {
2269 ASSERT(entry_stack_ != NULL);
2270 ASSERT(entry_stack_->previous_thread_data == NULL ||
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00002271 entry_stack_->previous_thread_data->thread_id().Equals(
2272 ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002273
2274 if (--entry_stack_->entry_count > 0) return;
2275
2276 ASSERT(CurrentPerIsolateThreadData() != NULL);
2277 ASSERT(CurrentPerIsolateThreadData()->isolate_ == this);
2278
2279 // Pop the stack.
2280 EntryStackItem* item = entry_stack_;
2281 entry_stack_ = item->previous_item;
2282
2283 PerIsolateThreadData* previous_thread_data = item->previous_thread_data;
2284 Isolate* previous_isolate = item->previous_isolate;
2285
2286 delete item;
2287
2288 // Reinit the current thread for the isolate it was running before this one.
2289 SetIsolateThreadLocals(previous_isolate, previous_thread_data);
2290}
2291
2292
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002293void Isolate::LinkDeferredHandles(DeferredHandles* deferred) {
2294 deferred->next_ = deferred_handles_head_;
2295 if (deferred_handles_head_ != NULL) {
2296 deferred_handles_head_->previous_ = deferred;
2297 }
2298 deferred_handles_head_ = deferred;
2299}
2300
2301
2302void Isolate::UnlinkDeferredHandles(DeferredHandles* deferred) {
2303#ifdef DEBUG
2304 // In debug mode assert that the linked list is well-formed.
2305 DeferredHandles* deferred_iterator = deferred;
2306 while (deferred_iterator->previous_ != NULL) {
2307 deferred_iterator = deferred_iterator->previous_;
2308 }
2309 ASSERT(deferred_handles_head_ == deferred_iterator);
2310#endif
2311 if (deferred_handles_head_ == deferred) {
2312 deferred_handles_head_ = deferred_handles_head_->next_;
2313 }
2314 if (deferred->next_ != NULL) {
2315 deferred->next_->previous_ = deferred->previous_;
2316 }
2317 if (deferred->previous_ != NULL) {
2318 deferred->previous_->next_ = deferred->next_;
2319 }
2320}
2321
2322
ulan@chromium.org750145a2013-03-07 15:14:13 +00002323HStatistics* Isolate::GetHStatistics() {
2324 if (hstatistics() == NULL) set_hstatistics(new HStatistics());
2325 return hstatistics();
2326}
2327
2328
2329HTracer* Isolate::GetHTracer() {
2330 if (htracer() == NULL) set_htracer(new HTracer(id()));
2331 return htracer();
2332}
2333
2334
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002335CodeStubInterfaceDescriptor*
2336 Isolate::code_stub_interface_descriptor(int index) {
2337 return code_stub_interface_descriptors_ + index;
2338}
2339
2340
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002341#ifdef DEBUG
2342#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
2343const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_);
2344ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
2345ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
2346#undef ISOLATE_FIELD_OFFSET
2347#endif
2348
2349} } // namespace v8::internal