blob: 632ecdc50a32b569ba3224c9428a165ef10deccb [file] [log] [blame]
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001// Copyright 2012 the V8 project authors. All rights reserved.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <stdlib.h>
29
30#include "v8.h"
31
32#include "ast.h"
33#include "bootstrapper.h"
34#include "codegen.h"
35#include "compilation-cache.h"
36#include "debug.h"
37#include "deoptimizer.h"
38#include "heap-profiler.h"
39#include "hydrogen.h"
40#include "isolate.h"
41#include "lithium-allocator.h"
42#include "log.h"
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +000043#include "marking-thread.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000044#include "messages.h"
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000045#include "platform.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000046#include "regexp-stack.h"
47#include "runtime-profiler.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000048#include "scopeinfo.h"
49#include "serialize.h"
50#include "simulator.h"
51#include "spaces.h"
52#include "stub-cache.h"
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +000053#include "sweeper-thread.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000054#include "version.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000055#include "vm-state-inl.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000056
57
58namespace v8 {
59namespace internal {
60
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000061Atomic32 ThreadId::highest_thread_id_ = 0;
62
63int ThreadId::AllocateThreadId() {
64 int new_id = NoBarrier_AtomicIncrement(&highest_thread_id_, 1);
65 return new_id;
66}
67
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000068
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000069int ThreadId::GetCurrentThreadId() {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000070 int thread_id = Thread::GetThreadLocalInt(Isolate::thread_id_key_);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000071 if (thread_id == 0) {
72 thread_id = AllocateThreadId();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +000073 Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id);
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +000074 }
75 return thread_id;
76}
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000077
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000078
79ThreadLocalTop::ThreadLocalTop() {
80 InitializeInternal();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000081 // This flag may be set using v8::V8::IgnoreOutOfMemoryException()
82 // before an isolate is initialized. The initialize methods below do
83 // not touch it to preserve its value.
84 ignore_out_of_memory_ = false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000085}
86
87
88void ThreadLocalTop::InitializeInternal() {
89 c_entry_fp_ = 0;
90 handler_ = 0;
91#ifdef USE_SIMULATOR
92 simulator_ = NULL;
93#endif
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000094 js_entry_sp_ = NULL;
95 external_callback_ = NULL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000096 current_vm_state_ = EXTERNAL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000097 try_catch_handler_address_ = NULL;
98 context_ = NULL;
99 thread_id_ = ThreadId::Invalid();
100 external_caught_exception_ = false;
101 failed_access_check_callback_ = NULL;
102 save_context_ = NULL;
103 catcher_ = NULL;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000104 top_lookup_result_ = NULL;
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000105
106 // These members are re-initialized later after deserialization
107 // is complete.
108 pending_exception_ = NULL;
109 has_pending_message_ = false;
110 pending_message_obj_ = NULL;
111 pending_message_script_ = NULL;
112 scheduled_exception_ = NULL;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000113}
114
115
116void ThreadLocalTop::Initialize() {
117 InitializeInternal();
118#ifdef USE_SIMULATOR
119#ifdef V8_TARGET_ARCH_ARM
120 simulator_ = Simulator::current(isolate_);
121#elif V8_TARGET_ARCH_MIPS
122 simulator_ = Simulator::current(isolate_);
123#endif
124#endif
125 thread_id_ = ThreadId::Current();
126}
127
128
129v8::TryCatch* ThreadLocalTop::TryCatchHandler() {
130 return TRY_CATCH_FROM_ADDRESS(try_catch_handler_address());
131}
132
133
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000134int SystemThreadManager::NumberOfParallelSystemThreads(
135 ParallelSystemComponent type) {
136 int number_of_threads = Min(OS::NumberOfCores(), kMaxThreads);
137 ASSERT(number_of_threads > 0);
138 if (number_of_threads == 1) {
139 return 0;
140 }
141 if (type == PARALLEL_SWEEPING) {
142 return number_of_threads;
143 } else if (type == CONCURRENT_SWEEPING) {
144 return number_of_threads - 1;
145 } else if (type == PARALLEL_MARKING) {
146 return number_of_threads;
147 }
148 return 1;
149}
150
151
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000152// Create a dummy thread that will wait forever on a semaphore. The only
153// purpose for this thread is to have some stack area to save essential data
154// into for use by a stacks only core dump (aka minidump).
155class PreallocatedMemoryThread: public Thread {
156 public:
157 char* data() {
158 if (data_ready_semaphore_ != NULL) {
159 // Initial access is guarded until the data has been published.
160 data_ready_semaphore_->Wait();
161 delete data_ready_semaphore_;
162 data_ready_semaphore_ = NULL;
163 }
164 return data_;
165 }
166
167 unsigned length() {
168 if (data_ready_semaphore_ != NULL) {
169 // Initial access is guarded until the data has been published.
170 data_ready_semaphore_->Wait();
171 delete data_ready_semaphore_;
172 data_ready_semaphore_ = NULL;
173 }
174 return length_;
175 }
176
177 // Stop the PreallocatedMemoryThread and release its resources.
178 void StopThread() {
179 keep_running_ = false;
180 wait_for_ever_semaphore_->Signal();
181
182 // Wait for the thread to terminate.
183 Join();
184
185 if (data_ready_semaphore_ != NULL) {
186 delete data_ready_semaphore_;
187 data_ready_semaphore_ = NULL;
188 }
189
190 delete wait_for_ever_semaphore_;
191 wait_for_ever_semaphore_ = NULL;
192 }
193
194 protected:
195 // When the thread starts running it will allocate a fixed number of bytes
196 // on the stack and publish the location of this memory for others to use.
197 void Run() {
198 EmbeddedVector<char, 15 * 1024> local_buffer;
199
200 // Initialize the buffer with a known good value.
201 OS::StrNCpy(local_buffer, "Trace data was not generated.\n",
202 local_buffer.length());
203
204 // Publish the local buffer and signal its availability.
205 data_ = local_buffer.start();
206 length_ = local_buffer.length();
207 data_ready_semaphore_->Signal();
208
209 while (keep_running_) {
210 // This thread will wait here until the end of time.
211 wait_for_ever_semaphore_->Wait();
212 }
213
214 // Make sure we access the buffer after the wait to remove all possibility
215 // of it being optimized away.
216 OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n",
217 local_buffer.length());
218 }
219
220
221 private:
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000222 PreallocatedMemoryThread()
223 : Thread("v8:PreallocMem"),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000224 keep_running_(true),
225 wait_for_ever_semaphore_(OS::CreateSemaphore(0)),
226 data_ready_semaphore_(OS::CreateSemaphore(0)),
227 data_(NULL),
228 length_(0) {
229 }
230
231 // Used to make sure that the thread keeps looping even for spurious wakeups.
232 bool keep_running_;
233
234 // This semaphore is used by the PreallocatedMemoryThread to wait for ever.
235 Semaphore* wait_for_ever_semaphore_;
236 // Semaphore to signal that the data has been initialized.
237 Semaphore* data_ready_semaphore_;
238
239 // Location and size of the preallocated memory block.
240 char* data_;
241 unsigned length_;
242
243 friend class Isolate;
244
245 DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread);
246};
247
248
249void Isolate::PreallocatedMemoryThreadStart() {
250 if (preallocated_memory_thread_ != NULL) return;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000251 preallocated_memory_thread_ = new PreallocatedMemoryThread();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000252 preallocated_memory_thread_->Start();
253}
254
255
256void Isolate::PreallocatedMemoryThreadStop() {
257 if (preallocated_memory_thread_ == NULL) return;
258 preallocated_memory_thread_->StopThread();
259 // Done with the thread entirely.
260 delete preallocated_memory_thread_;
261 preallocated_memory_thread_ = NULL;
262}
263
264
lrn@chromium.org7516f052011-03-30 08:52:27 +0000265void Isolate::PreallocatedStorageInit(size_t size) {
266 ASSERT(free_list_.next_ == &free_list_);
267 ASSERT(free_list_.previous_ == &free_list_);
268 PreallocatedStorage* free_chunk =
269 reinterpret_cast<PreallocatedStorage*>(new char[size]);
270 free_list_.next_ = free_list_.previous_ = free_chunk;
271 free_chunk->next_ = free_chunk->previous_ = &free_list_;
272 free_chunk->size_ = size - sizeof(PreallocatedStorage);
273 preallocated_storage_preallocated_ = true;
274}
275
276
277void* Isolate::PreallocatedStorageNew(size_t size) {
278 if (!preallocated_storage_preallocated_) {
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000279 return FreeStoreAllocationPolicy().New(size);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000280 }
281 ASSERT(free_list_.next_ != &free_list_);
282 ASSERT(free_list_.previous_ != &free_list_);
283
284 size = (size + kPointerSize - 1) & ~(kPointerSize - 1);
285 // Search for exact fit.
286 for (PreallocatedStorage* storage = free_list_.next_;
287 storage != &free_list_;
288 storage = storage->next_) {
289 if (storage->size_ == size) {
290 storage->Unlink();
291 storage->LinkTo(&in_use_list_);
292 return reinterpret_cast<void*>(storage + 1);
293 }
294 }
295 // Search for first fit.
296 for (PreallocatedStorage* storage = free_list_.next_;
297 storage != &free_list_;
298 storage = storage->next_) {
299 if (storage->size_ >= size + sizeof(PreallocatedStorage)) {
300 storage->Unlink();
301 storage->LinkTo(&in_use_list_);
302 PreallocatedStorage* left_over =
303 reinterpret_cast<PreallocatedStorage*>(
304 reinterpret_cast<char*>(storage + 1) + size);
305 left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage);
306 ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) ==
307 storage->size_);
308 storage->size_ = size;
309 left_over->LinkTo(&free_list_);
310 return reinterpret_cast<void*>(storage + 1);
311 }
312 }
313 // Allocation failure.
314 ASSERT(false);
315 return NULL;
316}
317
318
319// We don't attempt to coalesce.
320void Isolate::PreallocatedStorageDelete(void* p) {
321 if (p == NULL) {
322 return;
323 }
324 if (!preallocated_storage_preallocated_) {
325 FreeStoreAllocationPolicy::Delete(p);
326 return;
327 }
328 PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1;
329 ASSERT(storage->next_->previous_ == storage);
330 ASSERT(storage->previous_->next_ == storage);
331 storage->Unlink();
332 storage->LinkTo(&free_list_);
333}
334
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000335Isolate* Isolate::default_isolate_ = NULL;
336Thread::LocalStorageKey Isolate::isolate_key_;
337Thread::LocalStorageKey Isolate::thread_id_key_;
338Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_;
339Mutex* Isolate::process_wide_mutex_ = OS::CreateMutex();
340Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL;
ulan@chromium.org750145a2013-03-07 15:14:13 +0000341Atomic32 Isolate::isolate_counter_ = 0;
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000342
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000343Isolate::PerIsolateThreadData* Isolate::AllocatePerIsolateThreadData(
344 ThreadId thread_id) {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000345 ASSERT(!thread_id.Equals(ThreadId::Invalid()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000346 PerIsolateThreadData* per_thread = new PerIsolateThreadData(this, thread_id);
347 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000348 ScopedLock lock(process_wide_mutex_);
349 ASSERT(thread_data_table_->Lookup(this, thread_id) == NULL);
350 thread_data_table_->Insert(per_thread);
351 ASSERT(thread_data_table_->Lookup(this, thread_id) == per_thread);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000352 }
353 return per_thread;
354}
355
356
357Isolate::PerIsolateThreadData*
358 Isolate::FindOrAllocatePerThreadDataForThisThread() {
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +0000359 ThreadId thread_id = ThreadId::Current();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000360 PerIsolateThreadData* per_thread = NULL;
361 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000362 ScopedLock lock(process_wide_mutex_);
363 per_thread = thread_data_table_->Lookup(this, thread_id);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000364 if (per_thread == NULL) {
365 per_thread = AllocatePerIsolateThreadData(thread_id);
366 }
367 }
368 return per_thread;
369}
370
371
lrn@chromium.org1c092762011-05-09 09:42:16 +0000372Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() {
373 ThreadId thread_id = ThreadId::Current();
374 PerIsolateThreadData* per_thread = NULL;
375 {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000376 ScopedLock lock(process_wide_mutex_);
377 per_thread = thread_data_table_->Lookup(this, thread_id);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000378 }
379 return per_thread;
380}
381
382
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000383void Isolate::EnsureDefaultIsolate() {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000384 ScopedLock lock(process_wide_mutex_);
385 if (default_isolate_ == NULL) {
386 isolate_key_ = Thread::CreateThreadLocalKey();
387 thread_id_key_ = Thread::CreateThreadLocalKey();
388 per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey();
389 thread_data_table_ = new Isolate::ThreadDataTable();
390 default_isolate_ = new Isolate();
391 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000392 // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000393 // because a non-null thread data may be already set.
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000394 if (Thread::GetThreadLocal(isolate_key_) == NULL) {
395 Thread::SetThreadLocal(isolate_key_, default_isolate_);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000396 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000397}
398
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000399struct StaticInitializer {
400 StaticInitializer() {
401 Isolate::EnsureDefaultIsolate();
402 }
403} static_initializer;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000404
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000405#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000406Debugger* Isolate::GetDefaultIsolateDebugger() {
407 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000408 return default_isolate_->debugger();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000409}
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000410#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000411
412
413StackGuard* Isolate::GetDefaultIsolateStackGuard() {
414 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000415 return default_isolate_->stack_guard();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000416}
417
418
419void Isolate::EnterDefaultIsolate() {
420 EnsureDefaultIsolate();
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000421 ASSERT(default_isolate_ != NULL);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000422
423 PerIsolateThreadData* data = CurrentPerIsolateThreadData();
424 // If not yet in default isolate - enter it.
danno@chromium.org8c0a43f2012-04-03 08:37:53 +0000425 if (data == NULL || data->isolate() != default_isolate_) {
426 default_isolate_->Enter();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000427 }
428}
429
430
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000431v8::Isolate* Isolate::GetDefaultIsolateForLocking() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000432 EnsureDefaultIsolate();
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000433 return reinterpret_cast<v8::Isolate*>(default_isolate_);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434}
435
436
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000437Address Isolate::get_address_from_id(Isolate::AddressId id) {
438 return isolate_addresses_[id];
439}
440
441
442char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) {
443 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage);
444 Iterate(v, thread);
445 return thread_storage + sizeof(ThreadLocalTop);
446}
447
448
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000449void Isolate::IterateThread(ThreadVisitor* v, char* t) {
450 ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(t);
451 v->VisitThread(this, thread);
452}
453
454
455void Isolate::Iterate(ObjectVisitor* v, ThreadLocalTop* thread) {
456 // Visit the roots from the top for a given thread.
457 Object* pending;
458 // The pending exception can sometimes be a failure. We can't show
459 // that to the GC, which only understands objects.
460 if (thread->pending_exception_->ToObject(&pending)) {
461 v->VisitPointer(&pending);
462 thread->pending_exception_ = pending; // In case GC updated it.
463 }
464 v->VisitPointer(&(thread->pending_message_obj_));
465 v->VisitPointer(BitCast<Object**>(&(thread->pending_message_script_)));
466 v->VisitPointer(BitCast<Object**>(&(thread->context_)));
467 Object* scheduled;
468 if (thread->scheduled_exception_->ToObject(&scheduled)) {
469 v->VisitPointer(&scheduled);
470 thread->scheduled_exception_ = scheduled;
471 }
472
473 for (v8::TryCatch* block = thread->TryCatchHandler();
474 block != NULL;
475 block = TRY_CATCH_FROM_ADDRESS(block->next_)) {
476 v->VisitPointer(BitCast<Object**>(&(block->exception_)));
477 v->VisitPointer(BitCast<Object**>(&(block->message_)));
478 }
479
480 // Iterate over pointers on native execution stack.
481 for (StackFrameIterator it(this, thread); !it.done(); it.Advance()) {
482 it.frame()->Iterate(v);
483 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000484
485 // Iterate pointers in live lookup results.
486 thread->top_lookup_result_->Iterate(v);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000487}
488
489
490void Isolate::Iterate(ObjectVisitor* v) {
491 ThreadLocalTop* current_t = thread_local_top();
492 Iterate(v, current_t);
493}
494
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000495void Isolate::IterateDeferredHandles(ObjectVisitor* visitor) {
496 for (DeferredHandles* deferred = deferred_handles_head_;
497 deferred != NULL;
498 deferred = deferred->next_) {
499 deferred->Iterate(visitor);
500 }
501}
502
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000503
504void Isolate::RegisterTryCatchHandler(v8::TryCatch* that) {
505 // The ARM simulator has a separate JS stack. We therefore register
506 // the C++ try catch handler with the simulator and get back an
507 // address that can be used for comparisons with addresses into the
508 // JS stack. When running without the simulator, the address
509 // returned will be the address of the C++ try catch handler itself.
510 Address address = reinterpret_cast<Address>(
511 SimulatorStack::RegisterCTryCatch(reinterpret_cast<uintptr_t>(that)));
512 thread_local_top()->set_try_catch_handler_address(address);
513}
514
515
516void Isolate::UnregisterTryCatchHandler(v8::TryCatch* that) {
517 ASSERT(thread_local_top()->TryCatchHandler() == that);
518 thread_local_top()->set_try_catch_handler_address(
519 reinterpret_cast<Address>(that->next_));
520 thread_local_top()->catcher_ = NULL;
521 SimulatorStack::UnregisterCTryCatch();
522}
523
524
525Handle<String> Isolate::StackTraceString() {
526 if (stack_trace_nesting_level_ == 0) {
527 stack_trace_nesting_level_++;
528 HeapStringAllocator allocator;
529 StringStream::ClearMentionedObjectCache();
530 StringStream accumulator(&allocator);
531 incomplete_message_ = &accumulator;
532 PrintStack(&accumulator);
533 Handle<String> stack_trace = accumulator.ToString();
534 incomplete_message_ = NULL;
535 stack_trace_nesting_level_ = 0;
536 return stack_trace;
537 } else if (stack_trace_nesting_level_ == 1) {
538 stack_trace_nesting_level_++;
539 OS::PrintError(
540 "\n\nAttempt to print stack while printing stack (double fault)\n");
541 OS::PrintError(
542 "If you are lucky you may find a partial stack dump on stdout.\n\n");
543 incomplete_message_->OutputToStdOut();
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000544 return factory()->empty_string();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000545 } else {
546 OS::Abort();
547 // Unreachable
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000548 return factory()->empty_string();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000549 }
550}
551
552
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000553void Isolate::PushStackTraceAndDie(unsigned int magic,
554 Object* object,
555 Map* map,
556 unsigned int magic2) {
557 const int kMaxStackTraceSize = 8192;
558 Handle<String> trace = StackTraceString();
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000559 uint8_t buffer[kMaxStackTraceSize];
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000560 int length = Min(kMaxStackTraceSize - 1, trace->length());
561 String::WriteToFlat(*trace, buffer, 0, length);
562 buffer[length] = '\0';
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000563 // TODO(dcarney): convert buffer to utf8?
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000564 OS::PrintError("Stacktrace (%x-%x) %p %p: %s\n",
565 magic, magic2,
566 static_cast<void*>(object), static_cast<void*>(map),
jkummerow@chromium.org59297c72013-01-09 16:32:23 +0000567 reinterpret_cast<char*>(buffer));
jkummerow@chromium.org67255be2012-09-05 16:44:50 +0000568 OS::Abort();
569}
570
571
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000572// Determines whether the given stack frame should be displayed in
573// a stack trace. The caller is the error constructor that asked
574// for the stack trace to be collected. The first time a construct
575// call to this function is encountered it is skipped. The seen_caller
576// in/out parameter is used to remember if the caller has been seen
577// yet.
578static bool IsVisibleInStackTrace(StackFrame* raw_frame,
579 Object* caller,
580 bool* seen_caller) {
581 // Only display JS frames.
582 if (!raw_frame->is_java_script()) return false;
583 JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame);
584 Object* raw_fun = frame->function();
585 // Not sure when this can happen but skip it just in case.
586 if (!raw_fun->IsJSFunction()) return false;
587 if ((raw_fun == caller) && !(*seen_caller)) {
588 *seen_caller = true;
589 return false;
590 }
591 // Skip all frames until we've seen the caller.
592 if (!(*seen_caller)) return false;
593 // Also, skip non-visible built-in functions and any call with the builtins
594 // object as receiver, so as to not reveal either the builtins object or
595 // an internal function.
596 // The --builtins-in-stack-traces command line flag allows including
597 // internal call sites in the stack trace for debugging purposes.
598 if (!FLAG_builtins_in_stack_traces) {
599 JSFunction* fun = JSFunction::cast(raw_fun);
600 if (frame->receiver()->IsJSBuiltinsObject() ||
601 (fun->IsBuiltin() && !fun->shared()->native())) {
602 return false;
603 }
604 }
605 return true;
606}
607
608
609Handle<JSArray> Isolate::CaptureSimpleStackTrace(Handle<JSObject> error_object,
610 Handle<Object> caller,
611 int limit) {
612 limit = Max(limit, 0); // Ensure that limit is not negative.
613 int initial_size = Min(limit, 10);
614 Handle<FixedArray> elements =
615 factory()->NewFixedArrayWithHoles(initial_size * 4);
616
617 // If the caller parameter is a function we skip frames until we're
618 // under it before starting to collect.
619 bool seen_caller = !caller->IsJSFunction();
620 int cursor = 0;
621 int frames_seen = 0;
622 for (StackFrameIterator iter(this);
623 !iter.done() && frames_seen < limit;
624 iter.Advance()) {
625 StackFrame* raw_frame = iter.frame();
626 if (IsVisibleInStackTrace(raw_frame, *caller, &seen_caller)) {
627 frames_seen++;
628 JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame);
629 // Set initial size to the maximum inlining level + 1 for the outermost
630 // function.
631 List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1);
632 frame->Summarize(&frames);
633 for (int i = frames.length() - 1; i >= 0; i--) {
634 if (cursor + 4 > elements->length()) {
635 int new_capacity = JSObject::NewElementsCapacity(elements->length());
636 Handle<FixedArray> new_elements =
637 factory()->NewFixedArrayWithHoles(new_capacity);
638 for (int i = 0; i < cursor; i++) {
639 new_elements->set(i, elements->get(i));
640 }
641 elements = new_elements;
642 }
643 ASSERT(cursor + 4 <= elements->length());
644
645 Handle<Object> recv = frames[i].receiver();
646 Handle<JSFunction> fun = frames[i].function();
647 Handle<Code> code = frames[i].code();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000648 Handle<Smi> offset(Smi::FromInt(frames[i].offset()), this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000649 elements->set(cursor++, *recv);
650 elements->set(cursor++, *fun);
651 elements->set(cursor++, *code);
652 elements->set(cursor++, *offset);
653 }
654 }
655 }
656 Handle<JSArray> result = factory()->NewJSArrayWithElements(elements);
657 result->set_length(Smi::FromInt(cursor));
658 return result;
659}
660
661
662void Isolate::CaptureAndSetDetailedStackTrace(Handle<JSObject> error_object) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +0000663 if (capture_stack_trace_for_uncaught_exceptions_) {
664 // Capture stack trace for a detailed exception message.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000665 Handle<String> key = factory()->hidden_stack_trace_string();
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +0000666 Handle<JSArray> stack_trace = CaptureCurrentStackTrace(
667 stack_trace_for_uncaught_exceptions_frame_limit_,
668 stack_trace_for_uncaught_exceptions_options_);
669 JSObject::SetHiddenProperty(error_object, key, stack_trace);
670 }
671}
672
673
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000674Handle<JSArray> Isolate::CaptureCurrentStackTrace(
675 int frame_limit, StackTrace::StackTraceOptions options) {
676 // Ensure no negative values.
677 int limit = Max(frame_limit, 0);
678 Handle<JSArray> stack_trace = factory()->NewJSArray(frame_limit);
679
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000680 Handle<String> column_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000681 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("column"));
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000682 Handle<String> line_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000683 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("lineNumber"));
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000684 Handle<String> script_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000685 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("scriptName"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000686 Handle<String> script_name_or_source_url_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000687 factory()->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000688 STATIC_ASCII_VECTOR("scriptNameOrSourceURL"));
689 Handle<String> function_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000690 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("functionName"));
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000691 Handle<String> eval_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000692 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("isEval"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000693 Handle<String> constructor_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000694 factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("isConstructor"));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000695
696 StackTraceFrameIterator it(this);
697 int frames_seen = 0;
698 while (!it.done() && (frames_seen < limit)) {
699 JavaScriptFrame* frame = it.frame();
700 // Set initial size to the maximum inlining level + 1 for the outermost
701 // function.
702 List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1);
703 frame->Summarize(&frames);
704 for (int i = frames.length() - 1; i >= 0 && frames_seen < limit; i--) {
705 // Create a JSObject to hold the information for the StackFrame.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000706 Handle<JSObject> stack_frame = factory()->NewJSObject(object_function());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000707
708 Handle<JSFunction> fun = frames[i].function();
709 Handle<Script> script(Script::cast(fun->shared()->script()));
710
711 if (options & StackTrace::kLineNumber) {
712 int script_line_offset = script->line_offset()->value();
713 int position = frames[i].code()->SourcePosition(frames[i].pc());
714 int line_number = GetScriptLineNumber(script, position);
715 // line_number is already shifted by the script_line_offset.
716 int relative_line_number = line_number - script_line_offset;
717 if (options & StackTrace::kColumnOffset && relative_line_number >= 0) {
718 Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends()));
719 int start = (relative_line_number == 0) ? 0 :
720 Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1;
721 int column_offset = position - start;
722 if (relative_line_number == 0) {
723 // For the case where the code is on the same line as the script
724 // tag.
725 column_offset += script->column_offset()->value();
726 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000727 CHECK_NOT_EMPTY_HANDLE(
728 this,
729 JSObject::SetLocalPropertyIgnoreAttributes(
730 stack_frame, column_key,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000731 Handle<Smi>(Smi::FromInt(column_offset + 1), this), NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000732 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000733 CHECK_NOT_EMPTY_HANDLE(
734 this,
735 JSObject::SetLocalPropertyIgnoreAttributes(
736 stack_frame, line_key,
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000737 Handle<Smi>(Smi::FromInt(line_number + 1), this), NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000738 }
739
740 if (options & StackTrace::kScriptName) {
741 Handle<Object> script_name(script->name(), this);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000742 CHECK_NOT_EMPTY_HANDLE(this,
743 JSObject::SetLocalPropertyIgnoreAttributes(
744 stack_frame, script_key, script_name, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000745 }
746
747 if (options & StackTrace::kScriptNameOrSourceURL) {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000748 Handle<Object> result = GetScriptNameOrSourceURL(script);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000749 CHECK_NOT_EMPTY_HANDLE(this,
750 JSObject::SetLocalPropertyIgnoreAttributes(
751 stack_frame, script_name_or_source_url_key,
752 result, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000753 }
754
755 if (options & StackTrace::kFunctionName) {
756 Handle<Object> fun_name(fun->shared()->name(), this);
svenpanne@chromium.org9faefa42013-03-08 13:13:16 +0000757 if (!fun_name->BooleanValue()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000758 fun_name = Handle<Object>(fun->shared()->inferred_name(), this);
759 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000760 CHECK_NOT_EMPTY_HANDLE(this,
761 JSObject::SetLocalPropertyIgnoreAttributes(
762 stack_frame, function_key, fun_name, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000763 }
764
765 if (options & StackTrace::kIsEval) {
766 int type = Smi::cast(script->compilation_type())->value();
767 Handle<Object> is_eval = (type == Script::COMPILATION_TYPE_EVAL) ?
768 factory()->true_value() : factory()->false_value();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000769 CHECK_NOT_EMPTY_HANDLE(this,
770 JSObject::SetLocalPropertyIgnoreAttributes(
771 stack_frame, eval_key, is_eval, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000772 }
773
774 if (options & StackTrace::kIsConstructor) {
775 Handle<Object> is_constructor = (frames[i].is_constructor()) ?
776 factory()->true_value() : factory()->false_value();
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000777 CHECK_NOT_EMPTY_HANDLE(this,
778 JSObject::SetLocalPropertyIgnoreAttributes(
779 stack_frame, constructor_key,
780 is_constructor, NONE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000781 }
782
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000783 FixedArray::cast(stack_trace->elements())->set(frames_seen, *stack_frame);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000784 frames_seen++;
785 }
786 it.Advance();
787 }
788
789 stack_trace->set_length(Smi::FromInt(frames_seen));
790 return stack_trace;
791}
792
793
794void Isolate::PrintStack() {
795 if (stack_trace_nesting_level_ == 0) {
796 stack_trace_nesting_level_++;
797
798 StringAllocator* allocator;
799 if (preallocated_message_space_ == NULL) {
800 allocator = new HeapStringAllocator();
801 } else {
802 allocator = preallocated_message_space_;
803 }
804
805 StringStream::ClearMentionedObjectCache();
806 StringStream accumulator(allocator);
807 incomplete_message_ = &accumulator;
808 PrintStack(&accumulator);
809 accumulator.OutputToStdOut();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000810 InitializeLoggingAndCounters();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000811 accumulator.Log();
812 incomplete_message_ = NULL;
813 stack_trace_nesting_level_ = 0;
814 if (preallocated_message_space_ == NULL) {
815 // Remove the HeapStringAllocator created above.
816 delete allocator;
817 }
818 } else if (stack_trace_nesting_level_ == 1) {
819 stack_trace_nesting_level_++;
820 OS::PrintError(
821 "\n\nAttempt to print stack while printing stack (double fault)\n");
822 OS::PrintError(
823 "If you are lucky you may find a partial stack dump on stdout.\n\n");
824 incomplete_message_->OutputToStdOut();
825 }
826}
827
828
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000829static void PrintFrames(Isolate* isolate,
830 StringStream* accumulator,
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000831 StackFrame::PrintMode mode) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000832 StackFrameIterator it(isolate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000833 for (int i = 0; !it.done(); it.Advance()) {
834 it.frame()->Print(accumulator, mode, i++);
835 }
836}
837
838
839void Isolate::PrintStack(StringStream* accumulator) {
840 if (!IsInitialized()) {
841 accumulator->Add(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000842 "\n==== JS stack trace is not available =======================\n\n");
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000843 accumulator->Add(
844 "\n==== Isolate for the thread is not initialized =============\n\n");
845 return;
846 }
847 // The MentionedObjectCache is not GC-proof at the moment.
848 AssertNoAllocation nogc;
849 ASSERT(StringStream::IsMentionedObjectCacheClear());
850
851 // Avoid printing anything if there are no frames.
852 if (c_entry_fp(thread_local_top()) == 0) return;
853
854 accumulator->Add(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000855 "\n==== JS stack trace =========================================\n\n");
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000856 PrintFrames(this, accumulator, StackFrame::OVERVIEW);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000857
858 accumulator->Add(
859 "\n==== Details ================================================\n\n");
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000860 PrintFrames(this, accumulator, StackFrame::DETAILS);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000861
862 accumulator->PrintMentionedObjectCache();
863 accumulator->Add("=====================\n\n");
864}
865
866
867void Isolate::SetFailedAccessCheckCallback(
868 v8::FailedAccessCheckCallback callback) {
869 thread_local_top()->failed_access_check_callback_ = callback;
870}
871
872
873void Isolate::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) {
874 if (!thread_local_top()->failed_access_check_callback_) return;
875
876 ASSERT(receiver->IsAccessCheckNeeded());
877 ASSERT(context());
878
879 // Get the data object from access check info.
880 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
881 if (!constructor->shared()->IsApiFunction()) return;
882 Object* data_obj =
883 constructor->shared()->get_api_func_data()->access_check_info();
884 if (data_obj == heap_.undefined_value()) return;
885
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000886 HandleScope scope(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000887 Handle<JSObject> receiver_handle(receiver);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000888 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000889 { VMState state(this, EXTERNAL);
890 thread_local_top()->failed_access_check_callback_(
891 v8::Utils::ToLocal(receiver_handle),
892 type,
893 v8::Utils::ToLocal(data));
894 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000895}
896
897
898enum MayAccessDecision {
899 YES, NO, UNKNOWN
900};
901
902
903static MayAccessDecision MayAccessPreCheck(Isolate* isolate,
904 JSObject* receiver,
905 v8::AccessType type) {
906 // During bootstrapping, callback functions are not enabled yet.
907 if (isolate->bootstrapper()->IsActive()) return YES;
908
909 if (receiver->IsJSGlobalProxy()) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000910 Object* receiver_context = JSGlobalProxy::cast(receiver)->native_context();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000911 if (!receiver_context->IsContext()) return NO;
912
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000913 // Get the native context of current top context.
914 // avoid using Isolate::native_context() because it uses Handle.
915 Context* native_context =
916 isolate->context()->global_object()->native_context();
917 if (receiver_context == native_context) return YES;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000918
919 if (Context::cast(receiver_context)->security_token() ==
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000920 native_context->security_token())
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000921 return YES;
922 }
923
924 return UNKNOWN;
925}
926
927
928bool Isolate::MayNamedAccess(JSObject* receiver, Object* key,
929 v8::AccessType type) {
930 ASSERT(receiver->IsAccessCheckNeeded());
931
932 // The callers of this method are not expecting a GC.
933 AssertNoAllocation no_gc;
934
935 // Skip checks for hidden properties access. Note, we do not
936 // require existence of a context in this case.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000937 if (key == heap_.hidden_string()) return true;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000938
939 // Check for compatibility between the security tokens in the
940 // current lexical context and the accessed object.
941 ASSERT(context());
942
943 MayAccessDecision decision = MayAccessPreCheck(this, receiver, type);
944 if (decision != UNKNOWN) return decision == YES;
945
946 // Get named access check callback
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000947 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000948 if (!constructor->shared()->IsApiFunction()) return false;
949
950 Object* data_obj =
951 constructor->shared()->get_api_func_data()->access_check_info();
952 if (data_obj == heap_.undefined_value()) return false;
953
954 Object* fun_obj = AccessCheckInfo::cast(data_obj)->named_callback();
955 v8::NamedSecurityCallback callback =
956 v8::ToCData<v8::NamedSecurityCallback>(fun_obj);
957
958 if (!callback) return false;
959
960 HandleScope scope(this);
961 Handle<JSObject> receiver_handle(receiver, this);
962 Handle<Object> key_handle(key, this);
963 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
964 LOG(this, ApiNamedSecurityCheck(key));
965 bool result = false;
966 {
967 // Leaving JavaScript.
968 VMState state(this, EXTERNAL);
969 result = callback(v8::Utils::ToLocal(receiver_handle),
970 v8::Utils::ToLocal(key_handle),
971 type,
972 v8::Utils::ToLocal(data));
973 }
974 return result;
975}
976
977
978bool Isolate::MayIndexedAccess(JSObject* receiver,
979 uint32_t index,
980 v8::AccessType type) {
981 ASSERT(receiver->IsAccessCheckNeeded());
982 // Check for compatibility between the security tokens in the
983 // current lexical context and the accessed object.
984 ASSERT(context());
985
986 MayAccessDecision decision = MayAccessPreCheck(this, receiver, type);
987 if (decision != UNKNOWN) return decision == YES;
988
989 // Get indexed access check callback
990 JSFunction* constructor = JSFunction::cast(receiver->map()->constructor());
991 if (!constructor->shared()->IsApiFunction()) return false;
992
993 Object* data_obj =
994 constructor->shared()->get_api_func_data()->access_check_info();
995 if (data_obj == heap_.undefined_value()) return false;
996
997 Object* fun_obj = AccessCheckInfo::cast(data_obj)->indexed_callback();
998 v8::IndexedSecurityCallback callback =
999 v8::ToCData<v8::IndexedSecurityCallback>(fun_obj);
1000
1001 if (!callback) return false;
1002
1003 HandleScope scope(this);
1004 Handle<JSObject> receiver_handle(receiver, this);
1005 Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this);
1006 LOG(this, ApiIndexedSecurityCheck(index));
1007 bool result = false;
1008 {
1009 // Leaving JavaScript.
1010 VMState state(this, EXTERNAL);
1011 result = callback(v8::Utils::ToLocal(receiver_handle),
1012 index,
1013 type,
1014 v8::Utils::ToLocal(data));
1015 }
1016 return result;
1017}
1018
1019
1020const char* const Isolate::kStackOverflowMessage =
1021 "Uncaught RangeError: Maximum call stack size exceeded";
1022
1023
1024Failure* Isolate::StackOverflow() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001025 HandleScope scope(this);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001026 // At this point we cannot create an Error object using its javascript
1027 // constructor. Instead, we copy the pre-constructed boilerplate and
1028 // attach the stack trace as a hidden property.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001029 Handle<String> key = factory()->stack_overflow_string();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001030 Handle<JSObject> boilerplate =
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001031 Handle<JSObject>::cast(GetProperty(this, js_builtins_object(), key));
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001032 Handle<JSObject> exception = Copy(boilerplate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001033 DoThrow(*exception, NULL);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001034
1035 // Get stack trace limit.
1036 Handle<Object> error = GetProperty(js_builtins_object(), "$Error");
1037 if (!error->IsJSObject()) return Failure::Exception();
1038 Handle<Object> stack_trace_limit =
1039 GetProperty(Handle<JSObject>::cast(error), "stackTraceLimit");
1040 if (!stack_trace_limit->IsNumber()) return Failure::Exception();
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001041 double dlimit = stack_trace_limit->Number();
1042 int limit = isnan(dlimit) ? 0 : static_cast<int>(dlimit);
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001043
1044 Handle<JSArray> stack_trace = CaptureSimpleStackTrace(
1045 exception, factory()->undefined_value(), limit);
1046 JSObject::SetHiddenProperty(exception,
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001047 factory()->hidden_stack_trace_string(),
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001048 stack_trace);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001049 return Failure::Exception();
1050}
1051
1052
1053Failure* Isolate::TerminateExecution() {
1054 DoThrow(heap_.termination_exception(), NULL);
1055 return Failure::Exception();
1056}
1057
1058
1059Failure* Isolate::Throw(Object* exception, MessageLocation* location) {
1060 DoThrow(exception, location);
1061 return Failure::Exception();
1062}
1063
1064
mmassi@chromium.org7028c052012-06-13 11:51:58 +00001065Failure* Isolate::ReThrow(MaybeObject* exception) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001066 bool can_be_caught_externally = false;
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001067 bool catchable_by_javascript = is_catchable_by_javascript(exception);
1068 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
1069
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001070 thread_local_top()->catcher_ = can_be_caught_externally ?
1071 try_catch_handler() : NULL;
1072
1073 // Set the exception being re-thrown.
1074 set_pending_exception(exception);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001075 if (exception->IsFailure()) return exception->ToFailureUnchecked();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001076 return Failure::Exception();
1077}
1078
1079
1080Failure* Isolate::ThrowIllegalOperation() {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001081 return Throw(heap_.illegal_access_string());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001082}
1083
1084
1085void Isolate::ScheduleThrow(Object* exception) {
1086 // When scheduling a throw we first throw the exception to get the
1087 // error reporting if it is uncaught before rescheduling it.
1088 Throw(exception);
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001089 PropagatePendingExceptionToExternalTryCatch();
1090 if (has_pending_exception()) {
1091 thread_local_top()->scheduled_exception_ = pending_exception();
1092 thread_local_top()->external_caught_exception_ = false;
1093 clear_pending_exception();
1094 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001095}
1096
1097
1098Failure* Isolate::PromoteScheduledException() {
1099 MaybeObject* thrown = scheduled_exception();
1100 clear_scheduled_exception();
1101 // Re-throw the exception to avoid getting repeated error reporting.
1102 return ReThrow(thrown);
1103}
1104
1105
1106void Isolate::PrintCurrentStackTrace(FILE* out) {
1107 StackTraceFrameIterator it(this);
1108 while (!it.done()) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001109 HandleScope scope(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001110 // Find code position if recorded in relocation info.
1111 JavaScriptFrame* frame = it.frame();
1112 int pos = frame->LookupCode()->SourcePosition(frame->pc());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001113 Handle<Object> pos_obj(Smi::FromInt(pos), this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001114 // Fetch function and receiver.
1115 Handle<JSFunction> fun(JSFunction::cast(frame->function()));
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001116 Handle<Object> recv(frame->receiver(), this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001117 // Advance to the next JavaScript frame and determine if the
1118 // current frame is the top-level frame.
1119 it.Advance();
1120 Handle<Object> is_top_level = it.done()
1121 ? factory()->true_value()
1122 : factory()->false_value();
1123 // Generate and print stack trace line.
1124 Handle<String> line =
1125 Execution::GetStackTraceLine(recv, fun, pos_obj, is_top_level);
1126 if (line->length() > 0) {
1127 line->PrintOn(out);
1128 fprintf(out, "\n");
1129 }
1130 }
1131}
1132
1133
1134void Isolate::ComputeLocation(MessageLocation* target) {
1135 *target = MessageLocation(Handle<Script>(heap_.empty_script()), -1, -1);
1136 StackTraceFrameIterator it(this);
1137 if (!it.done()) {
1138 JavaScriptFrame* frame = it.frame();
1139 JSFunction* fun = JSFunction::cast(frame->function());
1140 Object* script = fun->shared()->script();
1141 if (script->IsScript() &&
1142 !(Script::cast(script)->source()->IsUndefined())) {
1143 int pos = frame->LookupCode()->SourcePosition(frame->pc());
1144 // Compute the location from the function and the reloc info.
1145 Handle<Script> casted_script(Script::cast(script));
1146 *target = MessageLocation(casted_script, pos, pos + 1);
1147 }
1148 }
1149}
1150
1151
1152bool Isolate::ShouldReportException(bool* can_be_caught_externally,
1153 bool catchable_by_javascript) {
1154 // Find the top-most try-catch handler.
1155 StackHandler* handler =
1156 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001157 while (handler != NULL && !handler->is_catch()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001158 handler = handler->next();
1159 }
1160
1161 // Get the address of the external handler so we can compare the address to
1162 // determine which one is closer to the top of the stack.
1163 Address external_handler_address =
1164 thread_local_top()->try_catch_handler_address();
1165
1166 // The exception has been externally caught if and only if there is
1167 // an external handler which is on top of the top-most try-catch
1168 // handler.
1169 *can_be_caught_externally = external_handler_address != NULL &&
1170 (handler == NULL || handler->address() > external_handler_address ||
1171 !catchable_by_javascript);
1172
1173 if (*can_be_caught_externally) {
1174 // Only report the exception if the external handler is verbose.
1175 return try_catch_handler()->is_verbose_;
1176 } else {
1177 // Report the exception if it isn't caught by JavaScript code.
1178 return handler == NULL;
1179 }
1180}
1181
1182
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001183bool Isolate::IsErrorObject(Handle<Object> obj) {
1184 if (!obj->IsJSObject()) return false;
1185
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001186 String* error_key =
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001187 *(factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("$Error")));
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001188 Object* error_constructor =
1189 js_builtins_object()->GetPropertyNoExceptionThrown(error_key);
1190
1191 for (Object* prototype = *obj; !prototype->IsNull();
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001192 prototype = prototype->GetPrototype(this)) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001193 if (!prototype->IsJSObject()) return false;
1194 if (JSObject::cast(prototype)->map()->constructor() == error_constructor) {
1195 return true;
1196 }
1197 }
1198 return false;
1199}
1200
1201
1202void Isolate::DoThrow(Object* exception, MessageLocation* location) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001203 ASSERT(!has_pending_exception());
1204
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001205 HandleScope scope(this);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001206 Handle<Object> exception_handle(exception, this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001207
1208 // Determine reporting and whether the exception is caught externally.
1209 bool catchable_by_javascript = is_catchable_by_javascript(exception);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001210 bool can_be_caught_externally = false;
1211 bool should_report_exception =
1212 ShouldReportException(&can_be_caught_externally, catchable_by_javascript);
1213 bool report_exception = catchable_by_javascript && should_report_exception;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001214 bool try_catch_needs_message =
1215 can_be_caught_externally && try_catch_handler()->capture_message_;
1216 bool bootstrapping = bootstrapper()->IsActive();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001217
1218#ifdef ENABLE_DEBUGGER_SUPPORT
1219 // Notify debugger of exception.
1220 if (catchable_by_javascript) {
1221 debugger_->OnException(exception_handle, report_exception);
1222 }
1223#endif
1224
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001225 // Generate the message if required.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001226 if (report_exception || try_catch_needs_message) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001227 MessageLocation potential_computed_location;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001228 if (location == NULL) {
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001229 // If no location was specified we use a computed one instead.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001230 ComputeLocation(&potential_computed_location);
1231 location = &potential_computed_location;
1232 }
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001233 // It's not safe to try to make message objects or collect stack traces
1234 // while the bootstrapper is active since the infrastructure may not have
1235 // been properly initialized.
1236 if (!bootstrapping) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001237 Handle<String> stack_trace;
1238 if (FLAG_trace_exception) stack_trace = StackTraceString();
1239 Handle<JSArray> stack_trace_object;
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001240 if (capture_stack_trace_for_uncaught_exceptions_) {
1241 if (IsErrorObject(exception_handle)) {
1242 // We fetch the stack trace that corresponds to this error object.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001243 String* key = heap()->hidden_stack_trace_string();
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001244 Object* stack_property =
1245 JSObject::cast(*exception_handle)->GetHiddenProperty(key);
1246 // Property lookup may have failed. In this case it's probably not
1247 // a valid Error object.
1248 if (stack_property->IsJSArray()) {
1249 stack_trace_object = Handle<JSArray>(JSArray::cast(stack_property));
1250 }
1251 }
1252 if (stack_trace_object.is_null()) {
1253 // Not an error object, we capture at throw site.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001254 stack_trace_object = CaptureCurrentStackTrace(
1255 stack_trace_for_uncaught_exceptions_frame_limit_,
1256 stack_trace_for_uncaught_exceptions_options_);
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001257 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001258 }
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001259
1260 Handle<Object> exception_arg = exception_handle;
1261 // If the exception argument is a custom object, turn it into a string
1262 // before throwing as uncaught exception. Note that the pending
1263 // exception object to be set later must not be turned into a string.
1264 if (exception_arg->IsJSObject() && !IsErrorObject(exception_arg)) {
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001265 bool failed = false;
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001266 exception_arg = Execution::ToDetailString(exception_arg, &failed);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001267 if (failed) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001268 exception_arg = factory()->InternalizeOneByteString(
1269 STATIC_ASCII_VECTOR("exception"));
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00001270 }
1271 }
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001272 Handle<Object> message_obj = MessageHandler::MakeMessageObject(
1273 "uncaught_exception",
1274 location,
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +00001275 HandleVector<Object>(&exception_arg, 1),
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001276 stack_trace,
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001277 stack_trace_object);
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001278 thread_local_top()->pending_message_obj_ = *message_obj;
1279 if (location != NULL) {
1280 thread_local_top()->pending_message_script_ = *location->script();
1281 thread_local_top()->pending_message_start_pos_ = location->start_pos();
1282 thread_local_top()->pending_message_end_pos_ = location->end_pos();
1283 }
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001284 } else if (location != NULL && !location->script().is_null()) {
1285 // We are bootstrapping and caught an error where the location is set
1286 // and we have a script for the location.
1287 // In this case we could have an extension (or an internal error
1288 // somewhere) and we print out the line number at which the error occured
1289 // to the console for easier debugging.
1290 int line_number = GetScriptLineNumberSafe(location->script(),
1291 location->start_pos());
verwaest@chromium.org37141392012-05-31 13:27:02 +00001292 if (exception->IsString()) {
1293 OS::PrintError(
1294 "Extension or internal compilation error: %s in %s at line %d.\n",
1295 *String::cast(exception)->ToCString(),
1296 *String::cast(location->script()->name())->ToCString(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001297 line_number + 1);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001298 } else {
1299 OS::PrintError(
1300 "Extension or internal compilation error in %s at line %d.\n",
1301 *String::cast(location->script()->name())->ToCString(),
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001302 line_number + 1);
verwaest@chromium.org37141392012-05-31 13:27:02 +00001303 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001304 }
1305 }
1306
1307 // Save the message for reporting if the the exception remains uncaught.
1308 thread_local_top()->has_pending_message_ = report_exception;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001309
1310 // Do not forget to clean catcher_ if currently thrown exception cannot
1311 // be caught. If necessary, ReThrow will update the catcher.
1312 thread_local_top()->catcher_ = can_be_caught_externally ?
1313 try_catch_handler() : NULL;
1314
jkummerow@chromium.orgab7dad42012-02-07 12:07:34 +00001315 set_pending_exception(*exception_handle);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001316}
1317
1318
1319bool Isolate::IsExternallyCaught() {
1320 ASSERT(has_pending_exception());
1321
1322 if ((thread_local_top()->catcher_ == NULL) ||
1323 (try_catch_handler() != thread_local_top()->catcher_)) {
1324 // When throwing the exception, we found no v8::TryCatch
1325 // which should care about this exception.
1326 return false;
1327 }
1328
1329 if (!is_catchable_by_javascript(pending_exception())) {
1330 return true;
1331 }
1332
1333 // Get the address of the external handler so we can compare the address to
1334 // determine which one is closer to the top of the stack.
1335 Address external_handler_address =
1336 thread_local_top()->try_catch_handler_address();
1337 ASSERT(external_handler_address != NULL);
1338
1339 // The exception has been externally caught if and only if there is
1340 // an external handler which is on top of the top-most try-finally
1341 // handler.
1342 // There should be no try-catch blocks as they would prohibit us from
1343 // finding external catcher in the first place (see catcher_ check above).
1344 //
1345 // Note, that finally clause would rethrow an exception unless it's
1346 // aborted by jumps in control flow like return, break, etc. and we'll
1347 // have another chances to set proper v8::TryCatch.
1348 StackHandler* handler =
1349 StackHandler::FromAddress(Isolate::handler(thread_local_top()));
1350 while (handler != NULL && handler->address() < external_handler_address) {
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001351 ASSERT(!handler->is_catch());
1352 if (handler->is_finally()) return false;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001353
1354 handler = handler->next();
1355 }
1356
1357 return true;
1358}
1359
1360
1361void Isolate::ReportPendingMessages() {
1362 ASSERT(has_pending_exception());
1363 PropagatePendingExceptionToExternalTryCatch();
1364
1365 // If the pending exception is OutOfMemoryException set out_of_memory in
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001366 // the native context. Note: We have to mark the native context here
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001367 // since the GenerateThrowOutOfMemory stub cannot make a RuntimeCall to
1368 // set it.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001369 HandleScope scope(this);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001370 if (thread_local_top_.pending_exception_->IsOutOfMemory()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001371 context()->mark_out_of_memory();
1372 } else if (thread_local_top_.pending_exception_ ==
1373 heap()->termination_exception()) {
1374 // Do nothing: if needed, the exception has been already propagated to
1375 // v8::TryCatch.
1376 } else {
1377 if (thread_local_top_.has_pending_message_) {
1378 thread_local_top_.has_pending_message_ = false;
1379 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001380 HandleScope scope(this);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00001381 Handle<Object> message_obj(thread_local_top_.pending_message_obj_,
1382 this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001383 if (thread_local_top_.pending_message_script_ != NULL) {
1384 Handle<Script> script(thread_local_top_.pending_message_script_);
1385 int start_pos = thread_local_top_.pending_message_start_pos_;
1386 int end_pos = thread_local_top_.pending_message_end_pos_;
1387 MessageLocation location(script, start_pos, end_pos);
1388 MessageHandler::ReportMessage(this, &location, message_obj);
1389 } else {
1390 MessageHandler::ReportMessage(this, NULL, message_obj);
1391 }
1392 }
1393 }
1394 }
1395 clear_pending_message();
1396}
1397
1398
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001399MessageLocation Isolate::GetMessageLocation() {
1400 ASSERT(has_pending_exception());
1401
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001402 if (!thread_local_top_.pending_exception_->IsOutOfMemory() &&
mmassi@chromium.org49a44672012-12-04 13:52:03 +00001403 thread_local_top_.pending_exception_ != heap()->termination_exception() &&
1404 thread_local_top_.has_pending_message_ &&
1405 !thread_local_top_.pending_message_obj_->IsTheHole() &&
1406 thread_local_top_.pending_message_script_ != NULL) {
1407 Handle<Script> script(thread_local_top_.pending_message_script_);
1408 int start_pos = thread_local_top_.pending_message_start_pos_;
1409 int end_pos = thread_local_top_.pending_message_end_pos_;
1410 return MessageLocation(script, start_pos, end_pos);
1411 }
1412
1413 return MessageLocation();
1414}
1415
1416
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001417void Isolate::TraceException(bool flag) {
1418 FLAG_trace_exception = flag; // TODO(isolates): This is an unfortunate use.
1419}
1420
1421
1422bool Isolate::OptionalRescheduleException(bool is_bottom_call) {
1423 ASSERT(has_pending_exception());
1424 PropagatePendingExceptionToExternalTryCatch();
1425
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001426 // Always reschedule out of memory exceptions.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001427 if (!is_out_of_memory()) {
1428 bool is_termination_exception =
1429 pending_exception() == heap_.termination_exception();
1430
1431 // Do not reschedule the exception if this is the bottom call.
1432 bool clear_exception = is_bottom_call;
1433
1434 if (is_termination_exception) {
1435 if (is_bottom_call) {
1436 thread_local_top()->external_caught_exception_ = false;
1437 clear_pending_exception();
1438 return false;
1439 }
1440 } else if (thread_local_top()->external_caught_exception_) {
1441 // If the exception is externally caught, clear it if there are no
1442 // JavaScript frames on the way to the C++ frame that has the
1443 // external handler.
1444 ASSERT(thread_local_top()->try_catch_handler_address() != NULL);
1445 Address external_handler_address =
1446 thread_local_top()->try_catch_handler_address();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001447 JavaScriptFrameIterator it(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001448 if (it.done() || (it.frame()->sp() > external_handler_address)) {
1449 clear_exception = true;
1450 }
1451 }
1452
1453 // Clear the exception if needed.
1454 if (clear_exception) {
1455 thread_local_top()->external_caught_exception_ = false;
1456 clear_pending_exception();
1457 return false;
1458 }
1459 }
1460
1461 // Reschedule the exception.
1462 thread_local_top()->scheduled_exception_ = pending_exception();
1463 clear_pending_exception();
1464 return true;
1465}
1466
1467
1468void Isolate::SetCaptureStackTraceForUncaughtExceptions(
1469 bool capture,
1470 int frame_limit,
1471 StackTrace::StackTraceOptions options) {
1472 capture_stack_trace_for_uncaught_exceptions_ = capture;
1473 stack_trace_for_uncaught_exceptions_frame_limit_ = frame_limit;
1474 stack_trace_for_uncaught_exceptions_options_ = options;
1475}
1476
1477
1478bool Isolate::is_out_of_memory() {
1479 if (has_pending_exception()) {
1480 MaybeObject* e = pending_exception();
1481 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1482 return true;
1483 }
1484 }
1485 if (has_scheduled_exception()) {
1486 MaybeObject* e = scheduled_exception();
1487 if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) {
1488 return true;
1489 }
1490 }
1491 return false;
1492}
1493
1494
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001495Handle<Context> Isolate::native_context() {
1496 GlobalObject* global = thread_local_top()->context_->global_object();
1497 return Handle<Context>(global->native_context());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001498}
1499
1500
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001501Handle<Context> Isolate::global_context() {
1502 GlobalObject* global = thread_local_top()->context_->global_object();
1503 return Handle<Context>(global->global_context());
1504}
1505
1506
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001507Handle<Context> Isolate::GetCallingNativeContext() {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001508 JavaScriptFrameIterator it(this);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001509#ifdef ENABLE_DEBUGGER_SUPPORT
1510 if (debug_->InDebugger()) {
1511 while (!it.done()) {
1512 JavaScriptFrame* frame = it.frame();
1513 Context* context = Context::cast(frame->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001514 if (context->native_context() == *debug_->debug_context()) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001515 it.Advance();
1516 } else {
1517 break;
1518 }
1519 }
1520 }
1521#endif // ENABLE_DEBUGGER_SUPPORT
1522 if (it.done()) return Handle<Context>::null();
1523 JavaScriptFrame* frame = it.frame();
1524 Context* context = Context::cast(frame->context());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001525 return Handle<Context>(context->native_context());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001526}
1527
1528
1529char* Isolate::ArchiveThread(char* to) {
1530 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1531 RuntimeProfiler::IsolateExitedJS(this);
1532 }
1533 memcpy(to, reinterpret_cast<char*>(thread_local_top()),
1534 sizeof(ThreadLocalTop));
1535 InitializeThreadLocal();
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00001536 clear_pending_exception();
1537 clear_pending_message();
1538 clear_scheduled_exception();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001539 return to + sizeof(ThreadLocalTop);
1540}
1541
1542
1543char* Isolate::RestoreThread(char* from) {
1544 memcpy(reinterpret_cast<char*>(thread_local_top()), from,
1545 sizeof(ThreadLocalTop));
1546 // This might be just paranoia, but it seems to be needed in case a
1547 // thread_local_top_ is restored on a separate OS thread.
1548#ifdef USE_SIMULATOR
1549#ifdef V8_TARGET_ARCH_ARM
1550 thread_local_top()->simulator_ = Simulator::current(this);
1551#elif V8_TARGET_ARCH_MIPS
1552 thread_local_top()->simulator_ = Simulator::current(this);
1553#endif
1554#endif
1555 if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) {
1556 RuntimeProfiler::IsolateEnteredJS(this);
1557 }
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001558 ASSERT(context() == NULL || context()->IsContext());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001559 return from + sizeof(ThreadLocalTop);
1560}
1561
1562
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001563Isolate::ThreadDataTable::ThreadDataTable()
1564 : list_(NULL) {
1565}
1566
1567
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00001568Isolate::ThreadDataTable::~ThreadDataTable() {
1569 // TODO(svenpanne) The assertion below would fire if an embedder does not
1570 // cleanly dispose all Isolates before disposing v8, so we are conservative
1571 // and leave it out for now.
1572 // ASSERT_EQ(NULL, list_);
1573}
1574
1575
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001576Isolate::PerIsolateThreadData*
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001577 Isolate::ThreadDataTable::Lookup(Isolate* isolate,
1578 ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001579 for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) {
1580 if (data->Matches(isolate, thread_id)) return data;
1581 }
1582 return NULL;
1583}
1584
1585
1586void Isolate::ThreadDataTable::Insert(Isolate::PerIsolateThreadData* data) {
1587 if (list_ != NULL) list_->prev_ = data;
1588 data->next_ = list_;
1589 list_ = data;
1590}
1591
1592
1593void Isolate::ThreadDataTable::Remove(PerIsolateThreadData* data) {
1594 if (list_ == data) list_ = data->next_;
1595 if (data->next_ != NULL) data->next_->prev_ = data->prev_;
1596 if (data->prev_ != NULL) data->prev_->next_ = data->next_;
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001597 delete data;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001598}
1599
1600
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001601void Isolate::ThreadDataTable::Remove(Isolate* isolate,
1602 ThreadId thread_id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001603 PerIsolateThreadData* data = Lookup(isolate, thread_id);
1604 if (data != NULL) {
1605 Remove(data);
1606 }
1607}
1608
1609
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001610void Isolate::ThreadDataTable::RemoveAllThreads(Isolate* isolate) {
1611 PerIsolateThreadData* data = list_;
1612 while (data != NULL) {
1613 PerIsolateThreadData* next = data->next_;
1614 if (data->isolate() == isolate) Remove(data);
1615 data = next;
1616 }
1617}
1618
1619
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001620#ifdef DEBUG
1621#define TRACE_ISOLATE(tag) \
1622 do { \
1623 if (FLAG_trace_isolates) { \
ulan@chromium.org750145a2013-03-07 15:14:13 +00001624 PrintF("Isolate %p (id %d)" #tag "\n", \
1625 reinterpret_cast<void*>(this), id()); \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001626 } \
1627 } while (false)
1628#else
1629#define TRACE_ISOLATE(tag)
1630#endif
1631
1632
1633Isolate::Isolate()
1634 : state_(UNINITIALIZED),
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001635 embedder_data_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001636 entry_stack_(NULL),
1637 stack_trace_nesting_level_(0),
1638 incomplete_message_(NULL),
1639 preallocated_memory_thread_(NULL),
1640 preallocated_message_space_(NULL),
1641 bootstrapper_(NULL),
1642 runtime_profiler_(NULL),
1643 compilation_cache_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001644 counters_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001645 code_range_(NULL),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001646 // Must be initialized early to allow v8::SetResourceConstraints calls.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001647 break_access_(OS::CreateMutex()),
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001648 debugger_initialized_(false),
1649 // Must be initialized early to allow v8::Debug calls.
1650 debugger_access_(OS::CreateMutex()),
1651 logger_(NULL),
1652 stats_table_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001653 stub_cache_(NULL),
1654 deoptimizer_data_(NULL),
1655 capture_stack_trace_for_uncaught_exceptions_(false),
1656 stack_trace_for_uncaught_exceptions_frame_limit_(0),
1657 stack_trace_for_uncaught_exceptions_options_(StackTrace::kOverview),
1658 transcendental_cache_(NULL),
1659 memory_allocator_(NULL),
1660 keyed_lookup_cache_(NULL),
1661 context_slot_cache_(NULL),
1662 descriptor_lookup_cache_(NULL),
1663 handle_scope_implementer_(NULL),
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001664 unicode_cache_(NULL),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001665 runtime_zone_(this),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001666 in_use_list_(0),
1667 free_list_(0),
1668 preallocated_storage_preallocated_(false),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001669 inner_pointer_to_code_cache_(NULL),
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00001670 write_iterator_(NULL),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001671 global_handles_(NULL),
1672 context_switcher_(NULL),
1673 thread_manager_(NULL),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001674 fp_stubs_generated_(false),
ricow@chromium.org27bf2882011-11-17 08:34:43 +00001675 has_installed_extensions_(false),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001676 string_tracker_(NULL),
1677 regexp_stack_(NULL),
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001678 date_cache_(NULL),
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001679 code_stub_interface_descriptors_(NULL),
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001680 context_exit_happened_(false),
1681 deferred_handles_head_(NULL),
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001682 optimizing_compiler_thread_(this),
1683 marking_thread_(NULL),
1684 sweeper_thread_(NULL) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00001685 id_ = NoBarrier_AtomicIncrement(&isolate_counter_, 1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001686 TRACE_ISOLATE(constructor);
1687
1688 memset(isolate_addresses_, 0,
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001689 sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001690
1691 heap_.isolate_ = this;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001692 stack_guard_.isolate_ = this;
1693
lrn@chromium.org1c092762011-05-09 09:42:16 +00001694 // ThreadManager is initialized early to support locking an isolate
1695 // before it is entered.
1696 thread_manager_ = new ThreadManager();
1697 thread_manager_->isolate_ = this;
1698
lrn@chromium.org7516f052011-03-30 08:52:27 +00001699#if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \
1700 defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001701 simulator_initialized_ = false;
1702 simulator_i_cache_ = NULL;
1703 simulator_redirection_ = NULL;
1704#endif
1705
1706#ifdef DEBUG
1707 // heap_histograms_ initializes itself.
1708 memset(&js_spill_information_, 0, sizeof(js_spill_information_));
1709 memset(code_kind_statistics_, 0,
1710 sizeof(code_kind_statistics_[0]) * Code::NUMBER_OF_KINDS);
yangguo@chromium.org003650e2013-01-24 16:31:08 +00001711
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001712 allow_compiler_thread_handle_deref_ = true;
1713 allow_execution_thread_handle_deref_ = true;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001714#endif
1715
1716#ifdef ENABLE_DEBUGGER_SUPPORT
1717 debug_ = NULL;
1718 debugger_ = NULL;
1719#endif
1720
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001721 handle_scope_data_.Initialize();
1722
1723#define ISOLATE_INIT_EXECUTE(type, name, initial_value) \
1724 name##_ = (initial_value);
1725 ISOLATE_INIT_LIST(ISOLATE_INIT_EXECUTE)
1726#undef ISOLATE_INIT_EXECUTE
1727
1728#define ISOLATE_INIT_ARRAY_EXECUTE(type, name, length) \
1729 memset(name##_, 0, sizeof(type) * length);
1730 ISOLATE_INIT_ARRAY_LIST(ISOLATE_INIT_ARRAY_EXECUTE)
1731#undef ISOLATE_INIT_ARRAY_EXECUTE
1732}
1733
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001734
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001735void Isolate::TearDown() {
1736 TRACE_ISOLATE(tear_down);
1737
1738 // Temporarily set this isolate as current so that various parts of
1739 // the isolate can access it in their destructors without having a
1740 // direct pointer. We don't use Enter/Exit here to avoid
1741 // initializing the thread data.
1742 PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData();
1743 Isolate* saved_isolate = UncheckedCurrent();
1744 SetIsolateThreadLocals(this, NULL);
1745
1746 Deinit();
1747
danno@chromium.org8c0a43f2012-04-03 08:37:53 +00001748 { ScopedLock lock(process_wide_mutex_);
1749 thread_data_table_->RemoveAllThreads(this);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001750 }
1751
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001752 if (serialize_partial_snapshot_cache_ != NULL) {
1753 delete[] serialize_partial_snapshot_cache_;
1754 serialize_partial_snapshot_cache_ = NULL;
1755 }
1756
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001757 if (!IsDefaultIsolate()) {
1758 delete this;
1759 }
1760
1761 // Restore the previous current isolate.
1762 SetIsolateThreadLocals(saved_isolate, saved_data);
1763}
1764
1765
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00001766void Isolate::GlobalTearDown() {
1767 delete thread_data_table_;
1768}
1769
1770
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001771void Isolate::Deinit() {
1772 if (state_ == INITIALIZED) {
1773 TRACE_ISOLATE(deinit);
1774
ulan@chromium.org750145a2013-03-07 15:14:13 +00001775 if (FLAG_parallel_recompilation) optimizing_compiler_thread_.Stop();
1776
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001777 if (FLAG_sweeper_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001778 for (int i = 0; i < FLAG_sweeper_threads; i++) {
1779 sweeper_thread_[i]->Stop();
1780 delete sweeper_thread_[i];
1781 }
1782 delete[] sweeper_thread_;
1783 }
1784
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001785 if (FLAG_marking_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00001786 for (int i = 0; i < FLAG_marking_threads; i++) {
1787 marking_thread_[i]->Stop();
1788 delete marking_thread_[i];
1789 }
1790 delete[] marking_thread_;
1791 }
1792
ulan@chromium.org750145a2013-03-07 15:14:13 +00001793 if (FLAG_hydrogen_stats) GetHStatistics()->Print();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001794
1795 // We must stop the logger before we tear down other components.
1796 logger_->EnsureTickerStopped();
1797
1798 delete deoptimizer_data_;
1799 deoptimizer_data_ = NULL;
1800 if (FLAG_preemption) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001801 v8::Locker locker(reinterpret_cast<v8::Isolate*>(this));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001802 v8::Locker::StopPreemption();
1803 }
1804 builtins_.TearDown();
1805 bootstrapper_->TearDown();
1806
1807 // Remove the external reference to the preallocated stack memory.
1808 delete preallocated_message_space_;
1809 preallocated_message_space_ = NULL;
1810 PreallocatedMemoryThreadStop();
1811
1812 HeapProfiler::TearDown();
1813 CpuProfiler::TearDown();
1814 if (runtime_profiler_ != NULL) {
1815 runtime_profiler_->TearDown();
1816 delete runtime_profiler_;
1817 runtime_profiler_ = NULL;
1818 }
1819 heap_.TearDown();
1820 logger_->TearDown();
1821
1822 // The default isolate is re-initializable due to legacy API.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001823 state_ = UNINITIALIZED;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001824 }
1825}
1826
1827
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001828void Isolate::PushToPartialSnapshotCache(Object* obj) {
1829 int length = serialize_partial_snapshot_cache_length();
1830 int capacity = serialize_partial_snapshot_cache_capacity();
1831
1832 if (length >= capacity) {
1833 int new_capacity = static_cast<int>((capacity + 10) * 1.2);
1834 Object** new_array = new Object*[new_capacity];
1835 for (int i = 0; i < length; i++) {
1836 new_array[i] = serialize_partial_snapshot_cache()[i];
1837 }
1838 if (capacity != 0) delete[] serialize_partial_snapshot_cache();
1839 set_serialize_partial_snapshot_cache(new_array);
1840 set_serialize_partial_snapshot_cache_capacity(new_capacity);
1841 }
1842
1843 serialize_partial_snapshot_cache()[length] = obj;
1844 set_serialize_partial_snapshot_cache_length(length + 1);
1845}
1846
1847
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001848void Isolate::SetIsolateThreadLocals(Isolate* isolate,
1849 PerIsolateThreadData* data) {
danno@chromium.org8c0a43f2012-04-03 08:37:53 +00001850 Thread::SetThreadLocal(isolate_key_, isolate);
1851 Thread::SetThreadLocal(per_isolate_thread_data_key_, data);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001852}
1853
1854
1855Isolate::~Isolate() {
1856 TRACE_ISOLATE(destructor);
1857
danno@chromium.orgb6451162011-08-17 14:33:23 +00001858 // Has to be called while counters_ are still alive.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001859 runtime_zone_.DeleteKeptSegment();
danno@chromium.orgb6451162011-08-17 14:33:23 +00001860
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001861 delete[] assembler_spare_buffer_;
1862 assembler_spare_buffer_ = NULL;
1863
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00001864 delete unicode_cache_;
1865 unicode_cache_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001866
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00001867 delete date_cache_;
1868 date_cache_ = NULL;
1869
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001870 delete[] code_stub_interface_descriptors_;
1871 code_stub_interface_descriptors_ = NULL;
1872
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001873 delete regexp_stack_;
1874 regexp_stack_ = NULL;
1875
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001876 delete descriptor_lookup_cache_;
1877 descriptor_lookup_cache_ = NULL;
1878 delete context_slot_cache_;
1879 context_slot_cache_ = NULL;
1880 delete keyed_lookup_cache_;
1881 keyed_lookup_cache_ = NULL;
1882
1883 delete transcendental_cache_;
1884 transcendental_cache_ = NULL;
1885 delete stub_cache_;
1886 stub_cache_ = NULL;
1887 delete stats_table_;
1888 stats_table_ = NULL;
1889
1890 delete logger_;
1891 logger_ = NULL;
1892
1893 delete counters_;
1894 counters_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001895
1896 delete handle_scope_implementer_;
1897 handle_scope_implementer_ = NULL;
1898 delete break_access_;
1899 break_access_ = NULL;
rossberg@chromium.org28a37082011-08-22 11:03:23 +00001900 delete debugger_access_;
1901 debugger_access_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001902
1903 delete compilation_cache_;
1904 compilation_cache_ = NULL;
1905 delete bootstrapper_;
1906 bootstrapper_ = NULL;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001907 delete inner_pointer_to_code_cache_;
1908 inner_pointer_to_code_cache_ = NULL;
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00001909 delete write_iterator_;
1910 write_iterator_ = NULL;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001911
1912 delete context_switcher_;
1913 context_switcher_ = NULL;
1914 delete thread_manager_;
1915 thread_manager_ = NULL;
1916
1917 delete string_tracker_;
1918 string_tracker_ = NULL;
1919
1920 delete memory_allocator_;
1921 memory_allocator_ = NULL;
1922 delete code_range_;
1923 code_range_ = NULL;
1924 delete global_handles_;
1925 global_handles_ = NULL;
1926
danno@chromium.orgb6451162011-08-17 14:33:23 +00001927 delete external_reference_table_;
1928 external_reference_table_ = NULL;
1929
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001930#ifdef ENABLE_DEBUGGER_SUPPORT
1931 delete debugger_;
1932 debugger_ = NULL;
1933 delete debug_;
1934 debug_ = NULL;
1935#endif
1936}
1937
1938
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001939void Isolate::InitializeThreadLocal() {
lrn@chromium.org1c092762011-05-09 09:42:16 +00001940 thread_local_top_.isolate_ = this;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001941 thread_local_top_.Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001942}
1943
1944
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001945void Isolate::PropagatePendingExceptionToExternalTryCatch() {
1946 ASSERT(has_pending_exception());
1947
1948 bool external_caught = IsExternallyCaught();
1949 thread_local_top_.external_caught_exception_ = external_caught;
1950
1951 if (!external_caught) return;
1952
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00001953 if (thread_local_top_.pending_exception_->IsOutOfMemory()) {
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001954 // Do not propagate OOM exception: we should kill VM asap.
1955 } else if (thread_local_top_.pending_exception_ ==
1956 heap()->termination_exception()) {
1957 try_catch_handler()->can_continue_ = false;
1958 try_catch_handler()->exception_ = heap()->null_value();
1959 } else {
1960 // At this point all non-object (failure) exceptions have
1961 // been dealt with so this shouldn't fail.
1962 ASSERT(!pending_exception()->IsFailure());
1963 try_catch_handler()->can_continue_ = true;
1964 try_catch_handler()->exception_ = pending_exception();
1965 if (!thread_local_top_.pending_message_obj_->IsTheHole()) {
1966 try_catch_handler()->message_ = thread_local_top_.pending_message_obj_;
1967 }
1968 }
1969}
1970
1971
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001972void Isolate::InitializeLoggingAndCounters() {
1973 if (logger_ == NULL) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00001974 logger_ = new Logger(this);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001975 }
1976 if (counters_ == NULL) {
1977 counters_ = new Counters;
1978 }
1979}
1980
1981
1982void Isolate::InitializeDebugger() {
1983#ifdef ENABLE_DEBUGGER_SUPPORT
1984 ScopedLock lock(debugger_access_);
1985 if (NoBarrier_Load(&debugger_initialized_)) return;
1986 InitializeLoggingAndCounters();
1987 debug_ = new Debug(this);
1988 debugger_ = new Debugger(this);
1989 Release_Store(&debugger_initialized_, true);
1990#endif
1991}
1992
1993
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001994bool Isolate::Init(Deserializer* des) {
1995 ASSERT(state_ != INITIALIZED);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001996 ASSERT(Isolate::Current() == this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001997 TRACE_ISOLATE(init);
1998
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001999 // The initialization process does not handle memory exhaustion.
2000 DisallowAllocationFailure disallow_allocation_failure;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002001
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002002 InitializeLoggingAndCounters();
2003
2004 InitializeDebugger();
2005
2006 memory_allocator_ = new MemoryAllocator(this);
2007 code_range_ = new CodeRange(this);
2008
2009 // Safe after setting Heap::isolate_, initializing StackGuard and
2010 // ensuring that Isolate::Current() == this.
2011 heap_.SetStackLimits();
2012
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00002013#define ASSIGN_ELEMENT(CamelName, hacker_name) \
2014 isolate_addresses_[Isolate::k##CamelName##Address] = \
2015 reinterpret_cast<Address>(hacker_name##_address());
2016 FOR_EACH_ISOLATE_ADDRESS_NAME(ASSIGN_ELEMENT)
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002017#undef C
2018
2019 string_tracker_ = new StringTracker();
2020 string_tracker_->isolate_ = this;
2021 compilation_cache_ = new CompilationCache(this);
2022 transcendental_cache_ = new TranscendentalCache();
2023 keyed_lookup_cache_ = new KeyedLookupCache();
2024 context_slot_cache_ = new ContextSlotCache();
2025 descriptor_lookup_cache_ = new DescriptorLookupCache();
2026 unicode_cache_ = new UnicodeCache();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002027 inner_pointer_to_code_cache_ = new InnerPointerToCodeCache(this);
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00002028 write_iterator_ = new ConsStringIteratorOp();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002029 global_handles_ = new GlobalHandles(this);
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002030 bootstrapper_ = new Bootstrapper(this);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002031 handle_scope_implementer_ = new HandleScopeImplementer(this);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002032 stub_cache_ = new StubCache(this, runtime_zone());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002033 regexp_stack_ = new RegExpStack();
2034 regexp_stack_->isolate_ = this;
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00002035 date_cache_ = new DateCache();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002036 code_stub_interface_descriptors_ =
2037 new CodeStubInterfaceDescriptor[CodeStub::NUMBER_OF_IDS];
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002038
2039 // Enable logging before setting up the heap
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002040 logger_->SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002041
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002042 CpuProfiler::SetUp();
2043 HeapProfiler::SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002044
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002045 // Initialize other runtime facilities
2046#if defined(USE_SIMULATOR)
lrn@chromium.org7516f052011-03-30 08:52:27 +00002047#if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS)
lrn@chromium.org1c092762011-05-09 09:42:16 +00002048 Simulator::Initialize(this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002049#endif
2050#endif
2051
2052 { // NOLINT
2053 // Ensure that the thread has a valid stack guard. The v8::Locker object
2054 // will ensure this too, but we don't have to use lockers if we are only
2055 // using one thread.
2056 ExecutionAccess lock(this);
2057 stack_guard_.InitThread(lock);
2058 }
2059
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002060 // SetUp the object heap.
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002061 ASSERT(!heap_.HasBeenSetUp());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002062 if (!heap_.SetUp()) {
yangguo@chromium.org9768bf12013-01-11 14:51:07 +00002063 V8::FatalProcessOutOfMemory("heap setup");
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002064 return false;
2065 }
2066
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00002067 deoptimizer_data_ = new DeoptimizerData(memory_allocator_);
ulan@chromium.org09d7ab52013-02-25 15:50:35 +00002068
2069 const bool create_heap_objects = (des == NULL);
2070 if (create_heap_objects && !heap_.CreateHeapObjects()) {
2071 V8::FatalProcessOutOfMemory("heap object creation");
2072 return false;
2073 }
2074
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002075 if (create_heap_objects) {
2076 // Terminate the cache array with the sentinel so we can iterate.
2077 PushToPartialSnapshotCache(heap_.undefined_value());
2078 }
2079
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00002080 InitializeThreadLocal();
2081
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002082 bootstrapper_->Initialize(create_heap_objects);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002083 builtins_.SetUp(create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002084
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002085 // Only preallocate on the first initialization.
2086 if (FLAG_preallocate_message_memory && preallocated_message_space_ == NULL) {
2087 // Start the thread which will set aside some memory.
2088 PreallocatedMemoryThreadStart();
2089 preallocated_message_space_ =
2090 new NoAllocationStringAllocator(
2091 preallocated_memory_thread_->data(),
2092 preallocated_memory_thread_->length());
2093 PreallocatedStorageInit(preallocated_memory_thread_->length() / 4);
2094 }
2095
2096 if (FLAG_preemption) {
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00002097 v8::Locker locker(reinterpret_cast<v8::Isolate*>(this));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002098 v8::Locker::StartPreemption(100);
2099 }
2100
2101#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002102 debug_->SetUp(create_heap_objects);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002103#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002104
2105 // If we are deserializing, read the state into the now-empty heap.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00002106 if (!create_heap_objects) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002107 des->Deserialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002108 }
ulan@chromium.org812308e2012-02-29 15:58:45 +00002109 stub_cache_->Initialize();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002110
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +00002111 // Finish initialization of ThreadLocal after deserialization is done.
2112 clear_pending_exception();
2113 clear_pending_message();
2114 clear_scheduled_exception();
2115
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002116 // Deserializing may put strange things in the root array's copy of the
2117 // stack guard.
2118 heap_.SetStackLimits();
2119
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002120 // Quiet the heap NaN if needed on target platform.
jkummerow@chromium.org28583c92012-07-16 11:31:55 +00002121 if (!create_heap_objects) Assembler::QuietNaN(heap_.nan_value());
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00002122
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002123 runtime_profiler_ = new RuntimeProfiler(this);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002124 runtime_profiler_->SetUp();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002125
2126 // If we are deserializing, log non-function code objects and compiled
2127 // functions found in the snapshot.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00002128 if (!create_heap_objects &&
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00002129 (FLAG_log_code || FLAG_ll_prof || logger_->is_logging_code_events())) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +00002130 HandleScope scope(this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002131 LOG(this, LogCodeObjects());
2132 LOG(this, LogCompiledFunctions());
2133 }
2134
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00002135 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, state_)),
2136 Internals::kIsolateStateOffset);
2137 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, embedder_data_)),
2138 Internals::kIsolateEmbedderDataOffset);
2139 CHECK_EQ(static_cast<int>(OFFSET_OF(Isolate, heap_.roots_)),
2140 Internals::kIsolateRootsOffset);
2141
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002142 state_ = INITIALIZED;
rossberg@chromium.org994edf62012-02-06 10:12:55 +00002143 time_millis_at_init_ = OS::TimeCurrentMillis();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002144
2145 if (!create_heap_objects) {
2146 // Now that the heap is consistent, it's OK to generate the code for the
2147 // deopt entry table that might have been referred to by optimized code in
2148 // the snapshot.
2149 HandleScope scope(this);
2150 Deoptimizer::EnsureCodeForDeoptimizationEntry(
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002151 this,
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002152 Deoptimizer::LAZY,
2153 kDeoptTableSerializeEntryCount - 1);
2154 }
2155
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002156 if (!Serializer::enabled()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002157 // Ensure that all stubs which need to be generated ahead of time, but
2158 // cannot be serialized into the snapshot have been generated.
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002159 HandleScope scope(this);
ulan@chromium.org750145a2013-03-07 15:14:13 +00002160 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(this);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002161 CodeStub::GenerateFPStubs(this);
2162 StubFailureTrampolineStub::GenerateAheadOfTime(this);
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00002163 }
2164
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002165 if (FLAG_parallel_recompilation) optimizing_compiler_thread_.Start();
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002166
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002167 if (FLAG_parallel_marking && FLAG_marking_threads == 0) {
2168 FLAG_marking_threads = SystemThreadManager::
2169 NumberOfParallelSystemThreads(
2170 SystemThreadManager::PARALLEL_MARKING);
2171 }
2172 if (FLAG_marking_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002173 marking_thread_ = new MarkingThread*[FLAG_marking_threads];
2174 for (int i = 0; i < FLAG_marking_threads; i++) {
2175 marking_thread_[i] = new MarkingThread(this);
2176 marking_thread_[i]->Start();
2177 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002178 } else {
2179 FLAG_parallel_marking = false;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002180 }
2181
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002182 if (FLAG_sweeper_threads == 0) {
2183 if (FLAG_concurrent_sweeping) {
2184 FLAG_sweeper_threads = SystemThreadManager::
2185 NumberOfParallelSystemThreads(
2186 SystemThreadManager::CONCURRENT_SWEEPING);
2187 } else if (FLAG_parallel_sweeping) {
2188 FLAG_sweeper_threads = SystemThreadManager::
2189 NumberOfParallelSystemThreads(
2190 SystemThreadManager::PARALLEL_SWEEPING);
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002191 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002192 }
2193 if (FLAG_sweeper_threads > 0) {
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002194 sweeper_thread_ = new SweeperThread*[FLAG_sweeper_threads];
2195 for (int i = 0; i < FLAG_sweeper_threads; i++) {
2196 sweeper_thread_[i] = new SweeperThread(this);
2197 sweeper_thread_[i]->Start();
2198 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002199 } else {
2200 FLAG_concurrent_sweeping = false;
2201 FLAG_parallel_sweeping = false;
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +00002202 }
ulan@chromium.org750145a2013-03-07 15:14:13 +00002203 if (FLAG_parallel_recompilation &&
2204 SystemThreadManager::NumberOfParallelSystemThreads(
2205 SystemThreadManager::PARALLEL_RECOMPILATION) == 0) {
2206 FLAG_parallel_recompilation = false;
2207 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002208 return true;
2209}
2210
2211
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002212// Initialized lazily to allow early
2213// v8::V8::SetAddHistogramSampleFunction calls.
2214StatsTable* Isolate::stats_table() {
2215 if (stats_table_ == NULL) {
2216 stats_table_ = new StatsTable;
2217 }
2218 return stats_table_;
2219}
2220
2221
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002222void Isolate::Enter() {
2223 Isolate* current_isolate = NULL;
2224 PerIsolateThreadData* current_data = CurrentPerIsolateThreadData();
2225 if (current_data != NULL) {
2226 current_isolate = current_data->isolate_;
2227 ASSERT(current_isolate != NULL);
2228 if (current_isolate == this) {
2229 ASSERT(Current() == this);
2230 ASSERT(entry_stack_ != NULL);
2231 ASSERT(entry_stack_->previous_thread_data == NULL ||
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00002232 entry_stack_->previous_thread_data->thread_id().Equals(
2233 ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002234 // Same thread re-enters the isolate, no need to re-init anything.
2235 entry_stack_->entry_count++;
2236 return;
2237 }
2238 }
2239
2240 // Threads can have default isolate set into TLS as Current but not yet have
2241 // PerIsolateThreadData for it, as it requires more advanced phase of the
2242 // initialization. For example, a thread might be the one that system used for
2243 // static initializers - in this case the default isolate is set in TLS but
2244 // the thread did not yet Enter the isolate. If PerisolateThreadData is not
2245 // there, use the isolate set in TLS.
2246 if (current_isolate == NULL) {
2247 current_isolate = Isolate::UncheckedCurrent();
2248 }
2249
2250 PerIsolateThreadData* data = FindOrAllocatePerThreadDataForThisThread();
2251 ASSERT(data != NULL);
2252 ASSERT(data->isolate_ == this);
2253
2254 EntryStackItem* item = new EntryStackItem(current_data,
2255 current_isolate,
2256 entry_stack_);
2257 entry_stack_ = item;
2258
2259 SetIsolateThreadLocals(this, data);
2260
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002261 // In case it's the first time some thread enters the isolate.
2262 set_thread_id(data->thread_id());
2263}
2264
2265
2266void Isolate::Exit() {
2267 ASSERT(entry_stack_ != NULL);
2268 ASSERT(entry_stack_->previous_thread_data == NULL ||
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00002269 entry_stack_->previous_thread_data->thread_id().Equals(
2270 ThreadId::Current()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002271
2272 if (--entry_stack_->entry_count > 0) return;
2273
2274 ASSERT(CurrentPerIsolateThreadData() != NULL);
2275 ASSERT(CurrentPerIsolateThreadData()->isolate_ == this);
2276
2277 // Pop the stack.
2278 EntryStackItem* item = entry_stack_;
2279 entry_stack_ = item->previous_item;
2280
2281 PerIsolateThreadData* previous_thread_data = item->previous_thread_data;
2282 Isolate* previous_isolate = item->previous_isolate;
2283
2284 delete item;
2285
2286 // Reinit the current thread for the isolate it was running before this one.
2287 SetIsolateThreadLocals(previous_isolate, previous_thread_data);
2288}
2289
2290
yangguo@chromium.org304cc332012-07-24 07:59:48 +00002291void Isolate::LinkDeferredHandles(DeferredHandles* deferred) {
2292 deferred->next_ = deferred_handles_head_;
2293 if (deferred_handles_head_ != NULL) {
2294 deferred_handles_head_->previous_ = deferred;
2295 }
2296 deferred_handles_head_ = deferred;
2297}
2298
2299
2300void Isolate::UnlinkDeferredHandles(DeferredHandles* deferred) {
2301#ifdef DEBUG
2302 // In debug mode assert that the linked list is well-formed.
2303 DeferredHandles* deferred_iterator = deferred;
2304 while (deferred_iterator->previous_ != NULL) {
2305 deferred_iterator = deferred_iterator->previous_;
2306 }
2307 ASSERT(deferred_handles_head_ == deferred_iterator);
2308#endif
2309 if (deferred_handles_head_ == deferred) {
2310 deferred_handles_head_ = deferred_handles_head_->next_;
2311 }
2312 if (deferred->next_ != NULL) {
2313 deferred->next_->previous_ = deferred->previous_;
2314 }
2315 if (deferred->previous_ != NULL) {
2316 deferred->previous_->next_ = deferred->next_;
2317 }
2318}
2319
2320
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002321#ifdef DEBUG
2322bool Isolate::AllowHandleDereference() {
2323 if (allow_execution_thread_handle_deref_ &&
2324 allow_compiler_thread_handle_deref_) {
2325 // Short-cut to avoid polling thread id.
2326 return true;
2327 }
2328 if (FLAG_parallel_recompilation &&
2329 optimizing_compiler_thread()->IsOptimizerThread()) {
2330 return allow_compiler_thread_handle_deref_;
2331 } else {
2332 return allow_execution_thread_handle_deref_;
2333 }
2334}
2335
2336
2337void Isolate::SetAllowHandleDereference(bool allow) {
2338 if (FLAG_parallel_recompilation &&
2339 optimizing_compiler_thread()->IsOptimizerThread()) {
2340 allow_compiler_thread_handle_deref_ = allow;
2341 } else {
2342 allow_execution_thread_handle_deref_ = allow;
2343 }
2344}
2345#endif
2346
2347
ulan@chromium.org750145a2013-03-07 15:14:13 +00002348HStatistics* Isolate::GetHStatistics() {
2349 if (hstatistics() == NULL) set_hstatistics(new HStatistics());
2350 return hstatistics();
2351}
2352
2353
2354HTracer* Isolate::GetHTracer() {
2355 if (htracer() == NULL) set_htracer(new HTracer(id()));
2356 return htracer();
2357}
2358
2359
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002360CodeStubInterfaceDescriptor*
2361 Isolate::code_stub_interface_descriptor(int index) {
2362 return code_stub_interface_descriptors_ + index;
2363}
2364
2365
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002366#ifdef DEBUG
2367#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
2368const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_);
2369ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
2370ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
2371#undef ISOLATE_FIELD_OFFSET
2372#endif
2373
2374} } // namespace v8::internal