ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
| 32 | #include "ast.h" |
| 33 | #include "bootstrapper.h" |
| 34 | #include "codegen.h" |
| 35 | #include "compilation-cache.h" |
| 36 | #include "debug.h" |
| 37 | #include "deoptimizer.h" |
| 38 | #include "heap-profiler.h" |
| 39 | #include "hydrogen.h" |
| 40 | #include "isolate.h" |
| 41 | #include "lithium-allocator.h" |
| 42 | #include "log.h" |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 43 | #include "messages.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 44 | #include "regexp-stack.h" |
| 45 | #include "runtime-profiler.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 46 | #include "scopeinfo.h" |
| 47 | #include "serialize.h" |
| 48 | #include "simulator.h" |
| 49 | #include "spaces.h" |
| 50 | #include "stub-cache.h" |
| 51 | #include "version.h" |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 52 | #include "vm-state-inl.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | namespace v8 { |
| 56 | namespace internal { |
| 57 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 58 | Atomic32 ThreadId::highest_thread_id_ = 0; |
| 59 | |
| 60 | int ThreadId::AllocateThreadId() { |
| 61 | int new_id = NoBarrier_AtomicIncrement(&highest_thread_id_, 1); |
| 62 | return new_id; |
| 63 | } |
| 64 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 65 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 66 | int ThreadId::GetCurrentThreadId() { |
| 67 | int thread_id = Thread::GetThreadLocalInt(Isolate::thread_id_key_); |
| 68 | if (thread_id == 0) { |
| 69 | thread_id = AllocateThreadId(); |
| 70 | Thread::SetThreadLocalInt(Isolate::thread_id_key_, thread_id); |
| 71 | } |
| 72 | return thread_id; |
| 73 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 74 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 75 | |
| 76 | ThreadLocalTop::ThreadLocalTop() { |
| 77 | InitializeInternal(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 78 | // This flag may be set using v8::V8::IgnoreOutOfMemoryException() |
| 79 | // before an isolate is initialized. The initialize methods below do |
| 80 | // not touch it to preserve its value. |
| 81 | ignore_out_of_memory_ = false; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | void ThreadLocalTop::InitializeInternal() { |
| 86 | c_entry_fp_ = 0; |
| 87 | handler_ = 0; |
| 88 | #ifdef USE_SIMULATOR |
| 89 | simulator_ = NULL; |
| 90 | #endif |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 91 | js_entry_sp_ = NULL; |
| 92 | external_callback_ = NULL; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 93 | current_vm_state_ = EXTERNAL; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 94 | try_catch_handler_address_ = NULL; |
| 95 | context_ = NULL; |
| 96 | thread_id_ = ThreadId::Invalid(); |
| 97 | external_caught_exception_ = false; |
| 98 | failed_access_check_callback_ = NULL; |
| 99 | save_context_ = NULL; |
| 100 | catcher_ = NULL; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame^] | 101 | |
| 102 | // These members are re-initialized later after deserialization |
| 103 | // is complete. |
| 104 | pending_exception_ = NULL; |
| 105 | has_pending_message_ = false; |
| 106 | pending_message_obj_ = NULL; |
| 107 | pending_message_script_ = NULL; |
| 108 | scheduled_exception_ = NULL; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | void ThreadLocalTop::Initialize() { |
| 113 | InitializeInternal(); |
| 114 | #ifdef USE_SIMULATOR |
| 115 | #ifdef V8_TARGET_ARCH_ARM |
| 116 | simulator_ = Simulator::current(isolate_); |
| 117 | #elif V8_TARGET_ARCH_MIPS |
| 118 | simulator_ = Simulator::current(isolate_); |
| 119 | #endif |
| 120 | #endif |
| 121 | thread_id_ = ThreadId::Current(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | v8::TryCatch* ThreadLocalTop::TryCatchHandler() { |
| 126 | return TRY_CATCH_FROM_ADDRESS(try_catch_handler_address()); |
| 127 | } |
| 128 | |
| 129 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 130 | // Create a dummy thread that will wait forever on a semaphore. The only |
| 131 | // purpose for this thread is to have some stack area to save essential data |
| 132 | // into for use by a stacks only core dump (aka minidump). |
| 133 | class PreallocatedMemoryThread: public Thread { |
| 134 | public: |
| 135 | char* data() { |
| 136 | if (data_ready_semaphore_ != NULL) { |
| 137 | // Initial access is guarded until the data has been published. |
| 138 | data_ready_semaphore_->Wait(); |
| 139 | delete data_ready_semaphore_; |
| 140 | data_ready_semaphore_ = NULL; |
| 141 | } |
| 142 | return data_; |
| 143 | } |
| 144 | |
| 145 | unsigned length() { |
| 146 | if (data_ready_semaphore_ != NULL) { |
| 147 | // Initial access is guarded until the data has been published. |
| 148 | data_ready_semaphore_->Wait(); |
| 149 | delete data_ready_semaphore_; |
| 150 | data_ready_semaphore_ = NULL; |
| 151 | } |
| 152 | return length_; |
| 153 | } |
| 154 | |
| 155 | // Stop the PreallocatedMemoryThread and release its resources. |
| 156 | void StopThread() { |
| 157 | keep_running_ = false; |
| 158 | wait_for_ever_semaphore_->Signal(); |
| 159 | |
| 160 | // Wait for the thread to terminate. |
| 161 | Join(); |
| 162 | |
| 163 | if (data_ready_semaphore_ != NULL) { |
| 164 | delete data_ready_semaphore_; |
| 165 | data_ready_semaphore_ = NULL; |
| 166 | } |
| 167 | |
| 168 | delete wait_for_ever_semaphore_; |
| 169 | wait_for_ever_semaphore_ = NULL; |
| 170 | } |
| 171 | |
| 172 | protected: |
| 173 | // When the thread starts running it will allocate a fixed number of bytes |
| 174 | // on the stack and publish the location of this memory for others to use. |
| 175 | void Run() { |
| 176 | EmbeddedVector<char, 15 * 1024> local_buffer; |
| 177 | |
| 178 | // Initialize the buffer with a known good value. |
| 179 | OS::StrNCpy(local_buffer, "Trace data was not generated.\n", |
| 180 | local_buffer.length()); |
| 181 | |
| 182 | // Publish the local buffer and signal its availability. |
| 183 | data_ = local_buffer.start(); |
| 184 | length_ = local_buffer.length(); |
| 185 | data_ready_semaphore_->Signal(); |
| 186 | |
| 187 | while (keep_running_) { |
| 188 | // This thread will wait here until the end of time. |
| 189 | wait_for_ever_semaphore_->Wait(); |
| 190 | } |
| 191 | |
| 192 | // Make sure we access the buffer after the wait to remove all possibility |
| 193 | // of it being optimized away. |
| 194 | OS::StrNCpy(local_buffer, "PreallocatedMemoryThread shutting down.\n", |
| 195 | local_buffer.length()); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | private: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 200 | PreallocatedMemoryThread() |
| 201 | : Thread("v8:PreallocMem"), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 202 | keep_running_(true), |
| 203 | wait_for_ever_semaphore_(OS::CreateSemaphore(0)), |
| 204 | data_ready_semaphore_(OS::CreateSemaphore(0)), |
| 205 | data_(NULL), |
| 206 | length_(0) { |
| 207 | } |
| 208 | |
| 209 | // Used to make sure that the thread keeps looping even for spurious wakeups. |
| 210 | bool keep_running_; |
| 211 | |
| 212 | // This semaphore is used by the PreallocatedMemoryThread to wait for ever. |
| 213 | Semaphore* wait_for_ever_semaphore_; |
| 214 | // Semaphore to signal that the data has been initialized. |
| 215 | Semaphore* data_ready_semaphore_; |
| 216 | |
| 217 | // Location and size of the preallocated memory block. |
| 218 | char* data_; |
| 219 | unsigned length_; |
| 220 | |
| 221 | friend class Isolate; |
| 222 | |
| 223 | DISALLOW_COPY_AND_ASSIGN(PreallocatedMemoryThread); |
| 224 | }; |
| 225 | |
| 226 | |
| 227 | void Isolate::PreallocatedMemoryThreadStart() { |
| 228 | if (preallocated_memory_thread_ != NULL) return; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 229 | preallocated_memory_thread_ = new PreallocatedMemoryThread(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 230 | preallocated_memory_thread_->Start(); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | void Isolate::PreallocatedMemoryThreadStop() { |
| 235 | if (preallocated_memory_thread_ == NULL) return; |
| 236 | preallocated_memory_thread_->StopThread(); |
| 237 | // Done with the thread entirely. |
| 238 | delete preallocated_memory_thread_; |
| 239 | preallocated_memory_thread_ = NULL; |
| 240 | } |
| 241 | |
| 242 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 243 | void Isolate::PreallocatedStorageInit(size_t size) { |
| 244 | ASSERT(free_list_.next_ == &free_list_); |
| 245 | ASSERT(free_list_.previous_ == &free_list_); |
| 246 | PreallocatedStorage* free_chunk = |
| 247 | reinterpret_cast<PreallocatedStorage*>(new char[size]); |
| 248 | free_list_.next_ = free_list_.previous_ = free_chunk; |
| 249 | free_chunk->next_ = free_chunk->previous_ = &free_list_; |
| 250 | free_chunk->size_ = size - sizeof(PreallocatedStorage); |
| 251 | preallocated_storage_preallocated_ = true; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | void* Isolate::PreallocatedStorageNew(size_t size) { |
| 256 | if (!preallocated_storage_preallocated_) { |
| 257 | return FreeStoreAllocationPolicy::New(size); |
| 258 | } |
| 259 | ASSERT(free_list_.next_ != &free_list_); |
| 260 | ASSERT(free_list_.previous_ != &free_list_); |
| 261 | |
| 262 | size = (size + kPointerSize - 1) & ~(kPointerSize - 1); |
| 263 | // Search for exact fit. |
| 264 | for (PreallocatedStorage* storage = free_list_.next_; |
| 265 | storage != &free_list_; |
| 266 | storage = storage->next_) { |
| 267 | if (storage->size_ == size) { |
| 268 | storage->Unlink(); |
| 269 | storage->LinkTo(&in_use_list_); |
| 270 | return reinterpret_cast<void*>(storage + 1); |
| 271 | } |
| 272 | } |
| 273 | // Search for first fit. |
| 274 | for (PreallocatedStorage* storage = free_list_.next_; |
| 275 | storage != &free_list_; |
| 276 | storage = storage->next_) { |
| 277 | if (storage->size_ >= size + sizeof(PreallocatedStorage)) { |
| 278 | storage->Unlink(); |
| 279 | storage->LinkTo(&in_use_list_); |
| 280 | PreallocatedStorage* left_over = |
| 281 | reinterpret_cast<PreallocatedStorage*>( |
| 282 | reinterpret_cast<char*>(storage + 1) + size); |
| 283 | left_over->size_ = storage->size_ - size - sizeof(PreallocatedStorage); |
| 284 | ASSERT(size + left_over->size_ + sizeof(PreallocatedStorage) == |
| 285 | storage->size_); |
| 286 | storage->size_ = size; |
| 287 | left_over->LinkTo(&free_list_); |
| 288 | return reinterpret_cast<void*>(storage + 1); |
| 289 | } |
| 290 | } |
| 291 | // Allocation failure. |
| 292 | ASSERT(false); |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | // We don't attempt to coalesce. |
| 298 | void Isolate::PreallocatedStorageDelete(void* p) { |
| 299 | if (p == NULL) { |
| 300 | return; |
| 301 | } |
| 302 | if (!preallocated_storage_preallocated_) { |
| 303 | FreeStoreAllocationPolicy::Delete(p); |
| 304 | return; |
| 305 | } |
| 306 | PreallocatedStorage* storage = reinterpret_cast<PreallocatedStorage*>(p) - 1; |
| 307 | ASSERT(storage->next_->previous_ == storage); |
| 308 | ASSERT(storage->previous_->next_ == storage); |
| 309 | storage->Unlink(); |
| 310 | storage->LinkTo(&free_list_); |
| 311 | } |
| 312 | |
| 313 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 314 | Isolate* Isolate::default_isolate_ = NULL; |
| 315 | Thread::LocalStorageKey Isolate::isolate_key_; |
| 316 | Thread::LocalStorageKey Isolate::thread_id_key_; |
| 317 | Thread::LocalStorageKey Isolate::per_isolate_thread_data_key_; |
| 318 | Mutex* Isolate::process_wide_mutex_ = OS::CreateMutex(); |
| 319 | Isolate::ThreadDataTable* Isolate::thread_data_table_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 320 | |
| 321 | |
| 322 | class IsolateInitializer { |
| 323 | public: |
| 324 | IsolateInitializer() { |
| 325 | Isolate::EnsureDefaultIsolate(); |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | static IsolateInitializer* EnsureDefaultIsolateAllocated() { |
| 330 | // TODO(isolates): Use the system threading API to do this once? |
| 331 | static IsolateInitializer static_initializer; |
| 332 | return &static_initializer; |
| 333 | } |
| 334 | |
| 335 | // This variable only needed to trigger static intialization. |
| 336 | static IsolateInitializer* static_initializer = EnsureDefaultIsolateAllocated(); |
| 337 | |
| 338 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 339 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 340 | |
| 341 | |
| 342 | Isolate::PerIsolateThreadData* Isolate::AllocatePerIsolateThreadData( |
| 343 | ThreadId thread_id) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 344 | ASSERT(!thread_id.Equals(ThreadId::Invalid())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 345 | PerIsolateThreadData* per_thread = new PerIsolateThreadData(this, thread_id); |
| 346 | { |
| 347 | ScopedLock lock(process_wide_mutex_); |
| 348 | ASSERT(thread_data_table_->Lookup(this, thread_id) == NULL); |
| 349 | thread_data_table_->Insert(per_thread); |
| 350 | ASSERT(thread_data_table_->Lookup(this, thread_id) == per_thread); |
| 351 | } |
| 352 | return per_thread; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | Isolate::PerIsolateThreadData* |
| 357 | Isolate::FindOrAllocatePerThreadDataForThisThread() { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 358 | ThreadId thread_id = ThreadId::Current(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 359 | PerIsolateThreadData* per_thread = NULL; |
| 360 | { |
| 361 | ScopedLock lock(process_wide_mutex_); |
| 362 | per_thread = thread_data_table_->Lookup(this, thread_id); |
| 363 | if (per_thread == NULL) { |
| 364 | per_thread = AllocatePerIsolateThreadData(thread_id); |
| 365 | } |
| 366 | } |
| 367 | return per_thread; |
| 368 | } |
| 369 | |
| 370 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 371 | Isolate::PerIsolateThreadData* Isolate::FindPerThreadDataForThisThread() { |
| 372 | ThreadId thread_id = ThreadId::Current(); |
| 373 | PerIsolateThreadData* per_thread = NULL; |
| 374 | { |
| 375 | ScopedLock lock(process_wide_mutex_); |
| 376 | per_thread = thread_data_table_->Lookup(this, thread_id); |
| 377 | } |
| 378 | return per_thread; |
| 379 | } |
| 380 | |
| 381 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 382 | void Isolate::EnsureDefaultIsolate() { |
| 383 | ScopedLock lock(process_wide_mutex_); |
| 384 | if (default_isolate_ == NULL) { |
| 385 | isolate_key_ = Thread::CreateThreadLocalKey(); |
| 386 | thread_id_key_ = Thread::CreateThreadLocalKey(); |
| 387 | per_isolate_thread_data_key_ = Thread::CreateThreadLocalKey(); |
| 388 | thread_data_table_ = new Isolate::ThreadDataTable(); |
| 389 | default_isolate_ = new Isolate(); |
| 390 | } |
| 391 | // Can't use SetIsolateThreadLocals(default_isolate_, NULL) here |
| 392 | // becase a non-null thread data may be already set. |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 393 | if (Thread::GetThreadLocal(isolate_key_) == NULL) { |
| 394 | Thread::SetThreadLocal(isolate_key_, default_isolate_); |
| 395 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 399 | #ifdef ENABLE_DEBUGGER_SUPPORT |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 400 | Debugger* Isolate::GetDefaultIsolateDebugger() { |
| 401 | EnsureDefaultIsolate(); |
| 402 | return default_isolate_->debugger(); |
| 403 | } |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 404 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 405 | |
| 406 | |
| 407 | StackGuard* Isolate::GetDefaultIsolateStackGuard() { |
| 408 | EnsureDefaultIsolate(); |
| 409 | return default_isolate_->stack_guard(); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | void Isolate::EnterDefaultIsolate() { |
| 414 | EnsureDefaultIsolate(); |
| 415 | ASSERT(default_isolate_ != NULL); |
| 416 | |
| 417 | PerIsolateThreadData* data = CurrentPerIsolateThreadData(); |
| 418 | // If not yet in default isolate - enter it. |
| 419 | if (data == NULL || data->isolate() != default_isolate_) { |
| 420 | default_isolate_->Enter(); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | |
| 425 | Isolate* Isolate::GetDefaultIsolateForLocking() { |
| 426 | EnsureDefaultIsolate(); |
| 427 | return default_isolate_; |
| 428 | } |
| 429 | |
| 430 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 431 | Address Isolate::get_address_from_id(Isolate::AddressId id) { |
| 432 | return isolate_addresses_[id]; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | char* Isolate::Iterate(ObjectVisitor* v, char* thread_storage) { |
| 437 | ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(thread_storage); |
| 438 | Iterate(v, thread); |
| 439 | return thread_storage + sizeof(ThreadLocalTop); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | void Isolate::IterateThread(ThreadVisitor* v) { |
| 444 | v->VisitThread(this, thread_local_top()); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | void Isolate::IterateThread(ThreadVisitor* v, char* t) { |
| 449 | ThreadLocalTop* thread = reinterpret_cast<ThreadLocalTop*>(t); |
| 450 | v->VisitThread(this, thread); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | void Isolate::Iterate(ObjectVisitor* v, ThreadLocalTop* thread) { |
| 455 | // Visit the roots from the top for a given thread. |
| 456 | Object* pending; |
| 457 | // The pending exception can sometimes be a failure. We can't show |
| 458 | // that to the GC, which only understands objects. |
| 459 | if (thread->pending_exception_->ToObject(&pending)) { |
| 460 | v->VisitPointer(&pending); |
| 461 | thread->pending_exception_ = pending; // In case GC updated it. |
| 462 | } |
| 463 | v->VisitPointer(&(thread->pending_message_obj_)); |
| 464 | v->VisitPointer(BitCast<Object**>(&(thread->pending_message_script_))); |
| 465 | v->VisitPointer(BitCast<Object**>(&(thread->context_))); |
| 466 | Object* scheduled; |
| 467 | if (thread->scheduled_exception_->ToObject(&scheduled)) { |
| 468 | v->VisitPointer(&scheduled); |
| 469 | thread->scheduled_exception_ = scheduled; |
| 470 | } |
| 471 | |
| 472 | for (v8::TryCatch* block = thread->TryCatchHandler(); |
| 473 | block != NULL; |
| 474 | block = TRY_CATCH_FROM_ADDRESS(block->next_)) { |
| 475 | v->VisitPointer(BitCast<Object**>(&(block->exception_))); |
| 476 | v->VisitPointer(BitCast<Object**>(&(block->message_))); |
| 477 | } |
| 478 | |
| 479 | // Iterate over pointers on native execution stack. |
| 480 | for (StackFrameIterator it(this, thread); !it.done(); it.Advance()) { |
| 481 | it.frame()->Iterate(v); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | |
| 486 | void Isolate::Iterate(ObjectVisitor* v) { |
| 487 | ThreadLocalTop* current_t = thread_local_top(); |
| 488 | Iterate(v, current_t); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | void Isolate::RegisterTryCatchHandler(v8::TryCatch* that) { |
| 493 | // The ARM simulator has a separate JS stack. We therefore register |
| 494 | // the C++ try catch handler with the simulator and get back an |
| 495 | // address that can be used for comparisons with addresses into the |
| 496 | // JS stack. When running without the simulator, the address |
| 497 | // returned will be the address of the C++ try catch handler itself. |
| 498 | Address address = reinterpret_cast<Address>( |
| 499 | SimulatorStack::RegisterCTryCatch(reinterpret_cast<uintptr_t>(that))); |
| 500 | thread_local_top()->set_try_catch_handler_address(address); |
| 501 | } |
| 502 | |
| 503 | |
| 504 | void Isolate::UnregisterTryCatchHandler(v8::TryCatch* that) { |
| 505 | ASSERT(thread_local_top()->TryCatchHandler() == that); |
| 506 | thread_local_top()->set_try_catch_handler_address( |
| 507 | reinterpret_cast<Address>(that->next_)); |
| 508 | thread_local_top()->catcher_ = NULL; |
| 509 | SimulatorStack::UnregisterCTryCatch(); |
| 510 | } |
| 511 | |
| 512 | |
| 513 | Handle<String> Isolate::StackTraceString() { |
| 514 | if (stack_trace_nesting_level_ == 0) { |
| 515 | stack_trace_nesting_level_++; |
| 516 | HeapStringAllocator allocator; |
| 517 | StringStream::ClearMentionedObjectCache(); |
| 518 | StringStream accumulator(&allocator); |
| 519 | incomplete_message_ = &accumulator; |
| 520 | PrintStack(&accumulator); |
| 521 | Handle<String> stack_trace = accumulator.ToString(); |
| 522 | incomplete_message_ = NULL; |
| 523 | stack_trace_nesting_level_ = 0; |
| 524 | return stack_trace; |
| 525 | } else if (stack_trace_nesting_level_ == 1) { |
| 526 | stack_trace_nesting_level_++; |
| 527 | OS::PrintError( |
| 528 | "\n\nAttempt to print stack while printing stack (double fault)\n"); |
| 529 | OS::PrintError( |
| 530 | "If you are lucky you may find a partial stack dump on stdout.\n\n"); |
| 531 | incomplete_message_->OutputToStdOut(); |
| 532 | return factory()->empty_symbol(); |
| 533 | } else { |
| 534 | OS::Abort(); |
| 535 | // Unreachable |
| 536 | return factory()->empty_symbol(); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | |
| 541 | Handle<JSArray> Isolate::CaptureCurrentStackTrace( |
| 542 | int frame_limit, StackTrace::StackTraceOptions options) { |
| 543 | // Ensure no negative values. |
| 544 | int limit = Max(frame_limit, 0); |
| 545 | Handle<JSArray> stack_trace = factory()->NewJSArray(frame_limit); |
| 546 | |
| 547 | Handle<String> column_key = factory()->LookupAsciiSymbol("column"); |
| 548 | Handle<String> line_key = factory()->LookupAsciiSymbol("lineNumber"); |
| 549 | Handle<String> script_key = factory()->LookupAsciiSymbol("scriptName"); |
| 550 | Handle<String> name_or_source_url_key = |
| 551 | factory()->LookupAsciiSymbol("nameOrSourceURL"); |
| 552 | Handle<String> script_name_or_source_url_key = |
| 553 | factory()->LookupAsciiSymbol("scriptNameOrSourceURL"); |
| 554 | Handle<String> function_key = factory()->LookupAsciiSymbol("functionName"); |
| 555 | Handle<String> eval_key = factory()->LookupAsciiSymbol("isEval"); |
| 556 | Handle<String> constructor_key = |
| 557 | factory()->LookupAsciiSymbol("isConstructor"); |
| 558 | |
| 559 | StackTraceFrameIterator it(this); |
| 560 | int frames_seen = 0; |
| 561 | while (!it.done() && (frames_seen < limit)) { |
| 562 | JavaScriptFrame* frame = it.frame(); |
| 563 | // Set initial size to the maximum inlining level + 1 for the outermost |
| 564 | // function. |
| 565 | List<FrameSummary> frames(Compiler::kMaxInliningLevels + 1); |
| 566 | frame->Summarize(&frames); |
| 567 | for (int i = frames.length() - 1; i >= 0 && frames_seen < limit; i--) { |
| 568 | // Create a JSObject to hold the information for the StackFrame. |
| 569 | Handle<JSObject> stackFrame = factory()->NewJSObject(object_function()); |
| 570 | |
| 571 | Handle<JSFunction> fun = frames[i].function(); |
| 572 | Handle<Script> script(Script::cast(fun->shared()->script())); |
| 573 | |
| 574 | if (options & StackTrace::kLineNumber) { |
| 575 | int script_line_offset = script->line_offset()->value(); |
| 576 | int position = frames[i].code()->SourcePosition(frames[i].pc()); |
| 577 | int line_number = GetScriptLineNumber(script, position); |
| 578 | // line_number is already shifted by the script_line_offset. |
| 579 | int relative_line_number = line_number - script_line_offset; |
| 580 | if (options & StackTrace::kColumnOffset && relative_line_number >= 0) { |
| 581 | Handle<FixedArray> line_ends(FixedArray::cast(script->line_ends())); |
| 582 | int start = (relative_line_number == 0) ? 0 : |
| 583 | Smi::cast(line_ends->get(relative_line_number - 1))->value() + 1; |
| 584 | int column_offset = position - start; |
| 585 | if (relative_line_number == 0) { |
| 586 | // For the case where the code is on the same line as the script |
| 587 | // tag. |
| 588 | column_offset += script->column_offset()->value(); |
| 589 | } |
| 590 | SetLocalPropertyNoThrow(stackFrame, column_key, |
| 591 | Handle<Smi>(Smi::FromInt(column_offset + 1))); |
| 592 | } |
| 593 | SetLocalPropertyNoThrow(stackFrame, line_key, |
| 594 | Handle<Smi>(Smi::FromInt(line_number + 1))); |
| 595 | } |
| 596 | |
| 597 | if (options & StackTrace::kScriptName) { |
| 598 | Handle<Object> script_name(script->name(), this); |
| 599 | SetLocalPropertyNoThrow(stackFrame, script_key, script_name); |
| 600 | } |
| 601 | |
| 602 | if (options & StackTrace::kScriptNameOrSourceURL) { |
| 603 | Handle<Object> script_name(script->name(), this); |
| 604 | Handle<JSValue> script_wrapper = GetScriptWrapper(script); |
| 605 | Handle<Object> property = GetProperty(script_wrapper, |
| 606 | name_or_source_url_key); |
| 607 | ASSERT(property->IsJSFunction()); |
| 608 | Handle<JSFunction> method = Handle<JSFunction>::cast(property); |
| 609 | bool caught_exception; |
| 610 | Handle<Object> result = Execution::TryCall(method, script_wrapper, 0, |
| 611 | NULL, &caught_exception); |
| 612 | if (caught_exception) { |
| 613 | result = factory()->undefined_value(); |
| 614 | } |
| 615 | SetLocalPropertyNoThrow(stackFrame, script_name_or_source_url_key, |
| 616 | result); |
| 617 | } |
| 618 | |
| 619 | if (options & StackTrace::kFunctionName) { |
| 620 | Handle<Object> fun_name(fun->shared()->name(), this); |
| 621 | if (fun_name->ToBoolean()->IsFalse()) { |
| 622 | fun_name = Handle<Object>(fun->shared()->inferred_name(), this); |
| 623 | } |
| 624 | SetLocalPropertyNoThrow(stackFrame, function_key, fun_name); |
| 625 | } |
| 626 | |
| 627 | if (options & StackTrace::kIsEval) { |
| 628 | int type = Smi::cast(script->compilation_type())->value(); |
| 629 | Handle<Object> is_eval = (type == Script::COMPILATION_TYPE_EVAL) ? |
| 630 | factory()->true_value() : factory()->false_value(); |
| 631 | SetLocalPropertyNoThrow(stackFrame, eval_key, is_eval); |
| 632 | } |
| 633 | |
| 634 | if (options & StackTrace::kIsConstructor) { |
| 635 | Handle<Object> is_constructor = (frames[i].is_constructor()) ? |
| 636 | factory()->true_value() : factory()->false_value(); |
| 637 | SetLocalPropertyNoThrow(stackFrame, constructor_key, is_constructor); |
| 638 | } |
| 639 | |
| 640 | FixedArray::cast(stack_trace->elements())->set(frames_seen, *stackFrame); |
| 641 | frames_seen++; |
| 642 | } |
| 643 | it.Advance(); |
| 644 | } |
| 645 | |
| 646 | stack_trace->set_length(Smi::FromInt(frames_seen)); |
| 647 | return stack_trace; |
| 648 | } |
| 649 | |
| 650 | |
| 651 | void Isolate::PrintStack() { |
| 652 | if (stack_trace_nesting_level_ == 0) { |
| 653 | stack_trace_nesting_level_++; |
| 654 | |
| 655 | StringAllocator* allocator; |
| 656 | if (preallocated_message_space_ == NULL) { |
| 657 | allocator = new HeapStringAllocator(); |
| 658 | } else { |
| 659 | allocator = preallocated_message_space_; |
| 660 | } |
| 661 | |
| 662 | StringStream::ClearMentionedObjectCache(); |
| 663 | StringStream accumulator(allocator); |
| 664 | incomplete_message_ = &accumulator; |
| 665 | PrintStack(&accumulator); |
| 666 | accumulator.OutputToStdOut(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 667 | InitializeLoggingAndCounters(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 668 | accumulator.Log(); |
| 669 | incomplete_message_ = NULL; |
| 670 | stack_trace_nesting_level_ = 0; |
| 671 | if (preallocated_message_space_ == NULL) { |
| 672 | // Remove the HeapStringAllocator created above. |
| 673 | delete allocator; |
| 674 | } |
| 675 | } else if (stack_trace_nesting_level_ == 1) { |
| 676 | stack_trace_nesting_level_++; |
| 677 | OS::PrintError( |
| 678 | "\n\nAttempt to print stack while printing stack (double fault)\n"); |
| 679 | OS::PrintError( |
| 680 | "If you are lucky you may find a partial stack dump on stdout.\n\n"); |
| 681 | incomplete_message_->OutputToStdOut(); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | |
| 686 | static void PrintFrames(StringStream* accumulator, |
| 687 | StackFrame::PrintMode mode) { |
| 688 | StackFrameIterator it; |
| 689 | for (int i = 0; !it.done(); it.Advance()) { |
| 690 | it.frame()->Print(accumulator, mode, i++); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | |
| 695 | void Isolate::PrintStack(StringStream* accumulator) { |
| 696 | if (!IsInitialized()) { |
| 697 | accumulator->Add( |
| 698 | "\n==== Stack trace is not available ==========================\n\n"); |
| 699 | accumulator->Add( |
| 700 | "\n==== Isolate for the thread is not initialized =============\n\n"); |
| 701 | return; |
| 702 | } |
| 703 | // The MentionedObjectCache is not GC-proof at the moment. |
| 704 | AssertNoAllocation nogc; |
| 705 | ASSERT(StringStream::IsMentionedObjectCacheClear()); |
| 706 | |
| 707 | // Avoid printing anything if there are no frames. |
| 708 | if (c_entry_fp(thread_local_top()) == 0) return; |
| 709 | |
| 710 | accumulator->Add( |
| 711 | "\n==== Stack trace ============================================\n\n"); |
| 712 | PrintFrames(accumulator, StackFrame::OVERVIEW); |
| 713 | |
| 714 | accumulator->Add( |
| 715 | "\n==== Details ================================================\n\n"); |
| 716 | PrintFrames(accumulator, StackFrame::DETAILS); |
| 717 | |
| 718 | accumulator->PrintMentionedObjectCache(); |
| 719 | accumulator->Add("=====================\n\n"); |
| 720 | } |
| 721 | |
| 722 | |
| 723 | void Isolate::SetFailedAccessCheckCallback( |
| 724 | v8::FailedAccessCheckCallback callback) { |
| 725 | thread_local_top()->failed_access_check_callback_ = callback; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | void Isolate::ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type) { |
| 730 | if (!thread_local_top()->failed_access_check_callback_) return; |
| 731 | |
| 732 | ASSERT(receiver->IsAccessCheckNeeded()); |
| 733 | ASSERT(context()); |
| 734 | |
| 735 | // Get the data object from access check info. |
| 736 | JSFunction* constructor = JSFunction::cast(receiver->map()->constructor()); |
| 737 | if (!constructor->shared()->IsApiFunction()) return; |
| 738 | Object* data_obj = |
| 739 | constructor->shared()->get_api_func_data()->access_check_info(); |
| 740 | if (data_obj == heap_.undefined_value()) return; |
| 741 | |
| 742 | HandleScope scope; |
| 743 | Handle<JSObject> receiver_handle(receiver); |
| 744 | Handle<Object> data(AccessCheckInfo::cast(data_obj)->data()); |
| 745 | thread_local_top()->failed_access_check_callback_( |
| 746 | v8::Utils::ToLocal(receiver_handle), |
| 747 | type, |
| 748 | v8::Utils::ToLocal(data)); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | enum MayAccessDecision { |
| 753 | YES, NO, UNKNOWN |
| 754 | }; |
| 755 | |
| 756 | |
| 757 | static MayAccessDecision MayAccessPreCheck(Isolate* isolate, |
| 758 | JSObject* receiver, |
| 759 | v8::AccessType type) { |
| 760 | // During bootstrapping, callback functions are not enabled yet. |
| 761 | if (isolate->bootstrapper()->IsActive()) return YES; |
| 762 | |
| 763 | if (receiver->IsJSGlobalProxy()) { |
| 764 | Object* receiver_context = JSGlobalProxy::cast(receiver)->context(); |
| 765 | if (!receiver_context->IsContext()) return NO; |
| 766 | |
| 767 | // Get the global context of current top context. |
| 768 | // avoid using Isolate::global_context() because it uses Handle. |
| 769 | Context* global_context = isolate->context()->global()->global_context(); |
| 770 | if (receiver_context == global_context) return YES; |
| 771 | |
| 772 | if (Context::cast(receiver_context)->security_token() == |
| 773 | global_context->security_token()) |
| 774 | return YES; |
| 775 | } |
| 776 | |
| 777 | return UNKNOWN; |
| 778 | } |
| 779 | |
| 780 | |
| 781 | bool Isolate::MayNamedAccess(JSObject* receiver, Object* key, |
| 782 | v8::AccessType type) { |
| 783 | ASSERT(receiver->IsAccessCheckNeeded()); |
| 784 | |
| 785 | // The callers of this method are not expecting a GC. |
| 786 | AssertNoAllocation no_gc; |
| 787 | |
| 788 | // Skip checks for hidden properties access. Note, we do not |
| 789 | // require existence of a context in this case. |
| 790 | if (key == heap_.hidden_symbol()) return true; |
| 791 | |
| 792 | // Check for compatibility between the security tokens in the |
| 793 | // current lexical context and the accessed object. |
| 794 | ASSERT(context()); |
| 795 | |
| 796 | MayAccessDecision decision = MayAccessPreCheck(this, receiver, type); |
| 797 | if (decision != UNKNOWN) return decision == YES; |
| 798 | |
| 799 | // Get named access check callback |
| 800 | JSFunction* constructor = JSFunction::cast(receiver->map()->constructor()); |
| 801 | if (!constructor->shared()->IsApiFunction()) return false; |
| 802 | |
| 803 | Object* data_obj = |
| 804 | constructor->shared()->get_api_func_data()->access_check_info(); |
| 805 | if (data_obj == heap_.undefined_value()) return false; |
| 806 | |
| 807 | Object* fun_obj = AccessCheckInfo::cast(data_obj)->named_callback(); |
| 808 | v8::NamedSecurityCallback callback = |
| 809 | v8::ToCData<v8::NamedSecurityCallback>(fun_obj); |
| 810 | |
| 811 | if (!callback) return false; |
| 812 | |
| 813 | HandleScope scope(this); |
| 814 | Handle<JSObject> receiver_handle(receiver, this); |
| 815 | Handle<Object> key_handle(key, this); |
| 816 | Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this); |
| 817 | LOG(this, ApiNamedSecurityCheck(key)); |
| 818 | bool result = false; |
| 819 | { |
| 820 | // Leaving JavaScript. |
| 821 | VMState state(this, EXTERNAL); |
| 822 | result = callback(v8::Utils::ToLocal(receiver_handle), |
| 823 | v8::Utils::ToLocal(key_handle), |
| 824 | type, |
| 825 | v8::Utils::ToLocal(data)); |
| 826 | } |
| 827 | return result; |
| 828 | } |
| 829 | |
| 830 | |
| 831 | bool Isolate::MayIndexedAccess(JSObject* receiver, |
| 832 | uint32_t index, |
| 833 | v8::AccessType type) { |
| 834 | ASSERT(receiver->IsAccessCheckNeeded()); |
| 835 | // Check for compatibility between the security tokens in the |
| 836 | // current lexical context and the accessed object. |
| 837 | ASSERT(context()); |
| 838 | |
| 839 | MayAccessDecision decision = MayAccessPreCheck(this, receiver, type); |
| 840 | if (decision != UNKNOWN) return decision == YES; |
| 841 | |
| 842 | // Get indexed access check callback |
| 843 | JSFunction* constructor = JSFunction::cast(receiver->map()->constructor()); |
| 844 | if (!constructor->shared()->IsApiFunction()) return false; |
| 845 | |
| 846 | Object* data_obj = |
| 847 | constructor->shared()->get_api_func_data()->access_check_info(); |
| 848 | if (data_obj == heap_.undefined_value()) return false; |
| 849 | |
| 850 | Object* fun_obj = AccessCheckInfo::cast(data_obj)->indexed_callback(); |
| 851 | v8::IndexedSecurityCallback callback = |
| 852 | v8::ToCData<v8::IndexedSecurityCallback>(fun_obj); |
| 853 | |
| 854 | if (!callback) return false; |
| 855 | |
| 856 | HandleScope scope(this); |
| 857 | Handle<JSObject> receiver_handle(receiver, this); |
| 858 | Handle<Object> data(AccessCheckInfo::cast(data_obj)->data(), this); |
| 859 | LOG(this, ApiIndexedSecurityCheck(index)); |
| 860 | bool result = false; |
| 861 | { |
| 862 | // Leaving JavaScript. |
| 863 | VMState state(this, EXTERNAL); |
| 864 | result = callback(v8::Utils::ToLocal(receiver_handle), |
| 865 | index, |
| 866 | type, |
| 867 | v8::Utils::ToLocal(data)); |
| 868 | } |
| 869 | return result; |
| 870 | } |
| 871 | |
| 872 | |
| 873 | const char* const Isolate::kStackOverflowMessage = |
| 874 | "Uncaught RangeError: Maximum call stack size exceeded"; |
| 875 | |
| 876 | |
| 877 | Failure* Isolate::StackOverflow() { |
| 878 | HandleScope scope; |
| 879 | Handle<String> key = factory()->stack_overflow_symbol(); |
| 880 | Handle<JSObject> boilerplate = |
| 881 | Handle<JSObject>::cast(GetProperty(js_builtins_object(), key)); |
| 882 | Handle<Object> exception = Copy(boilerplate); |
| 883 | // TODO(1240995): To avoid having to call JavaScript code to compute |
| 884 | // the message for stack overflow exceptions which is very likely to |
| 885 | // double fault with another stack overflow exception, we use a |
| 886 | // precomputed message. |
| 887 | DoThrow(*exception, NULL); |
| 888 | return Failure::Exception(); |
| 889 | } |
| 890 | |
| 891 | |
| 892 | Failure* Isolate::TerminateExecution() { |
| 893 | DoThrow(heap_.termination_exception(), NULL); |
| 894 | return Failure::Exception(); |
| 895 | } |
| 896 | |
| 897 | |
| 898 | Failure* Isolate::Throw(Object* exception, MessageLocation* location) { |
| 899 | DoThrow(exception, location); |
| 900 | return Failure::Exception(); |
| 901 | } |
| 902 | |
| 903 | |
| 904 | Failure* Isolate::ReThrow(MaybeObject* exception, MessageLocation* location) { |
| 905 | bool can_be_caught_externally = false; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 906 | bool catchable_by_javascript = is_catchable_by_javascript(exception); |
| 907 | ShouldReportException(&can_be_caught_externally, catchable_by_javascript); |
| 908 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 909 | thread_local_top()->catcher_ = can_be_caught_externally ? |
| 910 | try_catch_handler() : NULL; |
| 911 | |
| 912 | // Set the exception being re-thrown. |
| 913 | set_pending_exception(exception); |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 914 | if (exception->IsFailure()) return exception->ToFailureUnchecked(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 915 | return Failure::Exception(); |
| 916 | } |
| 917 | |
| 918 | |
| 919 | Failure* Isolate::ThrowIllegalOperation() { |
| 920 | return Throw(heap_.illegal_access_symbol()); |
| 921 | } |
| 922 | |
| 923 | |
| 924 | void Isolate::ScheduleThrow(Object* exception) { |
| 925 | // When scheduling a throw we first throw the exception to get the |
| 926 | // error reporting if it is uncaught before rescheduling it. |
| 927 | Throw(exception); |
| 928 | thread_local_top()->scheduled_exception_ = pending_exception(); |
| 929 | thread_local_top()->external_caught_exception_ = false; |
| 930 | clear_pending_exception(); |
| 931 | } |
| 932 | |
| 933 | |
| 934 | Failure* Isolate::PromoteScheduledException() { |
| 935 | MaybeObject* thrown = scheduled_exception(); |
| 936 | clear_scheduled_exception(); |
| 937 | // Re-throw the exception to avoid getting repeated error reporting. |
| 938 | return ReThrow(thrown); |
| 939 | } |
| 940 | |
| 941 | |
| 942 | void Isolate::PrintCurrentStackTrace(FILE* out) { |
| 943 | StackTraceFrameIterator it(this); |
| 944 | while (!it.done()) { |
| 945 | HandleScope scope; |
| 946 | // Find code position if recorded in relocation info. |
| 947 | JavaScriptFrame* frame = it.frame(); |
| 948 | int pos = frame->LookupCode()->SourcePosition(frame->pc()); |
| 949 | Handle<Object> pos_obj(Smi::FromInt(pos)); |
| 950 | // Fetch function and receiver. |
| 951 | Handle<JSFunction> fun(JSFunction::cast(frame->function())); |
| 952 | Handle<Object> recv(frame->receiver()); |
| 953 | // Advance to the next JavaScript frame and determine if the |
| 954 | // current frame is the top-level frame. |
| 955 | it.Advance(); |
| 956 | Handle<Object> is_top_level = it.done() |
| 957 | ? factory()->true_value() |
| 958 | : factory()->false_value(); |
| 959 | // Generate and print stack trace line. |
| 960 | Handle<String> line = |
| 961 | Execution::GetStackTraceLine(recv, fun, pos_obj, is_top_level); |
| 962 | if (line->length() > 0) { |
| 963 | line->PrintOn(out); |
| 964 | fprintf(out, "\n"); |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | |
| 970 | void Isolate::ComputeLocation(MessageLocation* target) { |
| 971 | *target = MessageLocation(Handle<Script>(heap_.empty_script()), -1, -1); |
| 972 | StackTraceFrameIterator it(this); |
| 973 | if (!it.done()) { |
| 974 | JavaScriptFrame* frame = it.frame(); |
| 975 | JSFunction* fun = JSFunction::cast(frame->function()); |
| 976 | Object* script = fun->shared()->script(); |
| 977 | if (script->IsScript() && |
| 978 | !(Script::cast(script)->source()->IsUndefined())) { |
| 979 | int pos = frame->LookupCode()->SourcePosition(frame->pc()); |
| 980 | // Compute the location from the function and the reloc info. |
| 981 | Handle<Script> casted_script(Script::cast(script)); |
| 982 | *target = MessageLocation(casted_script, pos, pos + 1); |
| 983 | } |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | |
| 988 | bool Isolate::ShouldReportException(bool* can_be_caught_externally, |
| 989 | bool catchable_by_javascript) { |
| 990 | // Find the top-most try-catch handler. |
| 991 | StackHandler* handler = |
| 992 | StackHandler::FromAddress(Isolate::handler(thread_local_top())); |
| 993 | while (handler != NULL && !handler->is_try_catch()) { |
| 994 | handler = handler->next(); |
| 995 | } |
| 996 | |
| 997 | // Get the address of the external handler so we can compare the address to |
| 998 | // determine which one is closer to the top of the stack. |
| 999 | Address external_handler_address = |
| 1000 | thread_local_top()->try_catch_handler_address(); |
| 1001 | |
| 1002 | // The exception has been externally caught if and only if there is |
| 1003 | // an external handler which is on top of the top-most try-catch |
| 1004 | // handler. |
| 1005 | *can_be_caught_externally = external_handler_address != NULL && |
| 1006 | (handler == NULL || handler->address() > external_handler_address || |
| 1007 | !catchable_by_javascript); |
| 1008 | |
| 1009 | if (*can_be_caught_externally) { |
| 1010 | // Only report the exception if the external handler is verbose. |
| 1011 | return try_catch_handler()->is_verbose_; |
| 1012 | } else { |
| 1013 | // Report the exception if it isn't caught by JavaScript code. |
| 1014 | return handler == NULL; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | void Isolate::DoThrow(MaybeObject* exception, MessageLocation* location) { |
| 1020 | ASSERT(!has_pending_exception()); |
| 1021 | |
| 1022 | HandleScope scope; |
| 1023 | Object* exception_object = Smi::FromInt(0); |
| 1024 | bool is_object = exception->ToObject(&exception_object); |
| 1025 | Handle<Object> exception_handle(exception_object); |
| 1026 | |
| 1027 | // Determine reporting and whether the exception is caught externally. |
| 1028 | bool catchable_by_javascript = is_catchable_by_javascript(exception); |
| 1029 | // Only real objects can be caught by JS. |
| 1030 | ASSERT(!catchable_by_javascript || is_object); |
| 1031 | bool can_be_caught_externally = false; |
| 1032 | bool should_report_exception = |
| 1033 | ShouldReportException(&can_be_caught_externally, catchable_by_javascript); |
| 1034 | bool report_exception = catchable_by_javascript && should_report_exception; |
| 1035 | |
| 1036 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1037 | // Notify debugger of exception. |
| 1038 | if (catchable_by_javascript) { |
| 1039 | debugger_->OnException(exception_handle, report_exception); |
| 1040 | } |
| 1041 | #endif |
| 1042 | |
| 1043 | // Generate the message. |
| 1044 | Handle<Object> message_obj; |
| 1045 | MessageLocation potential_computed_location; |
| 1046 | bool try_catch_needs_message = |
| 1047 | can_be_caught_externally && |
| 1048 | try_catch_handler()->capture_message_; |
| 1049 | if (report_exception || try_catch_needs_message) { |
| 1050 | if (location == NULL) { |
| 1051 | // If no location was specified we use a computed one instead |
| 1052 | ComputeLocation(&potential_computed_location); |
| 1053 | location = &potential_computed_location; |
| 1054 | } |
| 1055 | if (!bootstrapper()->IsActive()) { |
| 1056 | // It's not safe to try to make message objects or collect stack |
| 1057 | // traces while the bootstrapper is active since the infrastructure |
| 1058 | // may not have been properly initialized. |
| 1059 | Handle<String> stack_trace; |
| 1060 | if (FLAG_trace_exception) stack_trace = StackTraceString(); |
| 1061 | Handle<JSArray> stack_trace_object; |
| 1062 | if (report_exception && capture_stack_trace_for_uncaught_exceptions_) { |
| 1063 | stack_trace_object = CaptureCurrentStackTrace( |
| 1064 | stack_trace_for_uncaught_exceptions_frame_limit_, |
| 1065 | stack_trace_for_uncaught_exceptions_options_); |
| 1066 | } |
| 1067 | ASSERT(is_object); // Can't use the handle unless there's a real object. |
| 1068 | message_obj = MessageHandler::MakeMessageObject("uncaught_exception", |
| 1069 | location, HandleVector<Object>(&exception_handle, 1), stack_trace, |
| 1070 | stack_trace_object); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | // Save the message for reporting if the the exception remains uncaught. |
| 1075 | thread_local_top()->has_pending_message_ = report_exception; |
| 1076 | if (!message_obj.is_null()) { |
| 1077 | thread_local_top()->pending_message_obj_ = *message_obj; |
| 1078 | if (location != NULL) { |
| 1079 | thread_local_top()->pending_message_script_ = *location->script(); |
| 1080 | thread_local_top()->pending_message_start_pos_ = location->start_pos(); |
| 1081 | thread_local_top()->pending_message_end_pos_ = location->end_pos(); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | // Do not forget to clean catcher_ if currently thrown exception cannot |
| 1086 | // be caught. If necessary, ReThrow will update the catcher. |
| 1087 | thread_local_top()->catcher_ = can_be_caught_externally ? |
| 1088 | try_catch_handler() : NULL; |
| 1089 | |
| 1090 | // NOTE: Notifying the debugger or generating the message |
| 1091 | // may have caused new exceptions. For now, we just ignore |
| 1092 | // that and set the pending exception to the original one. |
| 1093 | if (is_object) { |
| 1094 | set_pending_exception(*exception_handle); |
| 1095 | } else { |
| 1096 | // Failures are not on the heap so they neither need nor work with handles. |
| 1097 | ASSERT(exception_handle->IsFailure()); |
| 1098 | set_pending_exception(exception); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | bool Isolate::IsExternallyCaught() { |
| 1104 | ASSERT(has_pending_exception()); |
| 1105 | |
| 1106 | if ((thread_local_top()->catcher_ == NULL) || |
| 1107 | (try_catch_handler() != thread_local_top()->catcher_)) { |
| 1108 | // When throwing the exception, we found no v8::TryCatch |
| 1109 | // which should care about this exception. |
| 1110 | return false; |
| 1111 | } |
| 1112 | |
| 1113 | if (!is_catchable_by_javascript(pending_exception())) { |
| 1114 | return true; |
| 1115 | } |
| 1116 | |
| 1117 | // Get the address of the external handler so we can compare the address to |
| 1118 | // determine which one is closer to the top of the stack. |
| 1119 | Address external_handler_address = |
| 1120 | thread_local_top()->try_catch_handler_address(); |
| 1121 | ASSERT(external_handler_address != NULL); |
| 1122 | |
| 1123 | // The exception has been externally caught if and only if there is |
| 1124 | // an external handler which is on top of the top-most try-finally |
| 1125 | // handler. |
| 1126 | // There should be no try-catch blocks as they would prohibit us from |
| 1127 | // finding external catcher in the first place (see catcher_ check above). |
| 1128 | // |
| 1129 | // Note, that finally clause would rethrow an exception unless it's |
| 1130 | // aborted by jumps in control flow like return, break, etc. and we'll |
| 1131 | // have another chances to set proper v8::TryCatch. |
| 1132 | StackHandler* handler = |
| 1133 | StackHandler::FromAddress(Isolate::handler(thread_local_top())); |
| 1134 | while (handler != NULL && handler->address() < external_handler_address) { |
| 1135 | ASSERT(!handler->is_try_catch()); |
| 1136 | if (handler->is_try_finally()) return false; |
| 1137 | |
| 1138 | handler = handler->next(); |
| 1139 | } |
| 1140 | |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | |
| 1145 | void Isolate::ReportPendingMessages() { |
| 1146 | ASSERT(has_pending_exception()); |
| 1147 | PropagatePendingExceptionToExternalTryCatch(); |
| 1148 | |
| 1149 | // If the pending exception is OutOfMemoryException set out_of_memory in |
| 1150 | // the global context. Note: We have to mark the global context here |
| 1151 | // since the GenerateThrowOutOfMemory stub cannot make a RuntimeCall to |
| 1152 | // set it. |
| 1153 | HandleScope scope; |
| 1154 | if (thread_local_top_.pending_exception_ == Failure::OutOfMemoryException()) { |
| 1155 | context()->mark_out_of_memory(); |
| 1156 | } else if (thread_local_top_.pending_exception_ == |
| 1157 | heap()->termination_exception()) { |
| 1158 | // Do nothing: if needed, the exception has been already propagated to |
| 1159 | // v8::TryCatch. |
| 1160 | } else { |
| 1161 | if (thread_local_top_.has_pending_message_) { |
| 1162 | thread_local_top_.has_pending_message_ = false; |
| 1163 | if (!thread_local_top_.pending_message_obj_->IsTheHole()) { |
| 1164 | HandleScope scope; |
| 1165 | Handle<Object> message_obj(thread_local_top_.pending_message_obj_); |
| 1166 | if (thread_local_top_.pending_message_script_ != NULL) { |
| 1167 | Handle<Script> script(thread_local_top_.pending_message_script_); |
| 1168 | int start_pos = thread_local_top_.pending_message_start_pos_; |
| 1169 | int end_pos = thread_local_top_.pending_message_end_pos_; |
| 1170 | MessageLocation location(script, start_pos, end_pos); |
| 1171 | MessageHandler::ReportMessage(this, &location, message_obj); |
| 1172 | } else { |
| 1173 | MessageHandler::ReportMessage(this, NULL, message_obj); |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | clear_pending_message(); |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | void Isolate::TraceException(bool flag) { |
| 1183 | FLAG_trace_exception = flag; // TODO(isolates): This is an unfortunate use. |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | bool Isolate::OptionalRescheduleException(bool is_bottom_call) { |
| 1188 | ASSERT(has_pending_exception()); |
| 1189 | PropagatePendingExceptionToExternalTryCatch(); |
| 1190 | |
| 1191 | // Allways reschedule out of memory exceptions. |
| 1192 | if (!is_out_of_memory()) { |
| 1193 | bool is_termination_exception = |
| 1194 | pending_exception() == heap_.termination_exception(); |
| 1195 | |
| 1196 | // Do not reschedule the exception if this is the bottom call. |
| 1197 | bool clear_exception = is_bottom_call; |
| 1198 | |
| 1199 | if (is_termination_exception) { |
| 1200 | if (is_bottom_call) { |
| 1201 | thread_local_top()->external_caught_exception_ = false; |
| 1202 | clear_pending_exception(); |
| 1203 | return false; |
| 1204 | } |
| 1205 | } else if (thread_local_top()->external_caught_exception_) { |
| 1206 | // If the exception is externally caught, clear it if there are no |
| 1207 | // JavaScript frames on the way to the C++ frame that has the |
| 1208 | // external handler. |
| 1209 | ASSERT(thread_local_top()->try_catch_handler_address() != NULL); |
| 1210 | Address external_handler_address = |
| 1211 | thread_local_top()->try_catch_handler_address(); |
| 1212 | JavaScriptFrameIterator it; |
| 1213 | if (it.done() || (it.frame()->sp() > external_handler_address)) { |
| 1214 | clear_exception = true; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | // Clear the exception if needed. |
| 1219 | if (clear_exception) { |
| 1220 | thread_local_top()->external_caught_exception_ = false; |
| 1221 | clear_pending_exception(); |
| 1222 | return false; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | // Reschedule the exception. |
| 1227 | thread_local_top()->scheduled_exception_ = pending_exception(); |
| 1228 | clear_pending_exception(); |
| 1229 | return true; |
| 1230 | } |
| 1231 | |
| 1232 | |
| 1233 | void Isolate::SetCaptureStackTraceForUncaughtExceptions( |
| 1234 | bool capture, |
| 1235 | int frame_limit, |
| 1236 | StackTrace::StackTraceOptions options) { |
| 1237 | capture_stack_trace_for_uncaught_exceptions_ = capture; |
| 1238 | stack_trace_for_uncaught_exceptions_frame_limit_ = frame_limit; |
| 1239 | stack_trace_for_uncaught_exceptions_options_ = options; |
| 1240 | } |
| 1241 | |
| 1242 | |
| 1243 | bool Isolate::is_out_of_memory() { |
| 1244 | if (has_pending_exception()) { |
| 1245 | MaybeObject* e = pending_exception(); |
| 1246 | if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) { |
| 1247 | return true; |
| 1248 | } |
| 1249 | } |
| 1250 | if (has_scheduled_exception()) { |
| 1251 | MaybeObject* e = scheduled_exception(); |
| 1252 | if (e->IsFailure() && Failure::cast(e)->IsOutOfMemoryException()) { |
| 1253 | return true; |
| 1254 | } |
| 1255 | } |
| 1256 | return false; |
| 1257 | } |
| 1258 | |
| 1259 | |
| 1260 | Handle<Context> Isolate::global_context() { |
| 1261 | GlobalObject* global = thread_local_top()->context_->global(); |
| 1262 | return Handle<Context>(global->global_context()); |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | Handle<Context> Isolate::GetCallingGlobalContext() { |
| 1267 | JavaScriptFrameIterator it; |
| 1268 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1269 | if (debug_->InDebugger()) { |
| 1270 | while (!it.done()) { |
| 1271 | JavaScriptFrame* frame = it.frame(); |
| 1272 | Context* context = Context::cast(frame->context()); |
| 1273 | if (context->global_context() == *debug_->debug_context()) { |
| 1274 | it.Advance(); |
| 1275 | } else { |
| 1276 | break; |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 1281 | if (it.done()) return Handle<Context>::null(); |
| 1282 | JavaScriptFrame* frame = it.frame(); |
| 1283 | Context* context = Context::cast(frame->context()); |
| 1284 | return Handle<Context>(context->global_context()); |
| 1285 | } |
| 1286 | |
| 1287 | |
| 1288 | char* Isolate::ArchiveThread(char* to) { |
| 1289 | if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) { |
| 1290 | RuntimeProfiler::IsolateExitedJS(this); |
| 1291 | } |
| 1292 | memcpy(to, reinterpret_cast<char*>(thread_local_top()), |
| 1293 | sizeof(ThreadLocalTop)); |
| 1294 | InitializeThreadLocal(); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame^] | 1295 | clear_pending_exception(); |
| 1296 | clear_pending_message(); |
| 1297 | clear_scheduled_exception(); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1298 | return to + sizeof(ThreadLocalTop); |
| 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | char* Isolate::RestoreThread(char* from) { |
| 1303 | memcpy(reinterpret_cast<char*>(thread_local_top()), from, |
| 1304 | sizeof(ThreadLocalTop)); |
| 1305 | // This might be just paranoia, but it seems to be needed in case a |
| 1306 | // thread_local_top_ is restored on a separate OS thread. |
| 1307 | #ifdef USE_SIMULATOR |
| 1308 | #ifdef V8_TARGET_ARCH_ARM |
| 1309 | thread_local_top()->simulator_ = Simulator::current(this); |
| 1310 | #elif V8_TARGET_ARCH_MIPS |
| 1311 | thread_local_top()->simulator_ = Simulator::current(this); |
| 1312 | #endif |
| 1313 | #endif |
| 1314 | if (RuntimeProfiler::IsEnabled() && current_vm_state() == JS) { |
| 1315 | RuntimeProfiler::IsolateEnteredJS(this); |
| 1316 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1317 | ASSERT(context() == NULL || context()->IsContext()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 1318 | return from + sizeof(ThreadLocalTop); |
| 1319 | } |
| 1320 | |
| 1321 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1322 | Isolate::ThreadDataTable::ThreadDataTable() |
| 1323 | : list_(NULL) { |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | Isolate::PerIsolateThreadData* |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1328 | Isolate::ThreadDataTable::Lookup(Isolate* isolate, |
| 1329 | ThreadId thread_id) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1330 | for (PerIsolateThreadData* data = list_; data != NULL; data = data->next_) { |
| 1331 | if (data->Matches(isolate, thread_id)) return data; |
| 1332 | } |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | |
| 1336 | |
| 1337 | void Isolate::ThreadDataTable::Insert(Isolate::PerIsolateThreadData* data) { |
| 1338 | if (list_ != NULL) list_->prev_ = data; |
| 1339 | data->next_ = list_; |
| 1340 | list_ = data; |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | void Isolate::ThreadDataTable::Remove(PerIsolateThreadData* data) { |
| 1345 | if (list_ == data) list_ = data->next_; |
| 1346 | if (data->next_ != NULL) data->next_->prev_ = data->prev_; |
| 1347 | if (data->prev_ != NULL) data->prev_->next_ = data->next_; |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1348 | delete data; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1352 | void Isolate::ThreadDataTable::Remove(Isolate* isolate, |
| 1353 | ThreadId thread_id) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1354 | PerIsolateThreadData* data = Lookup(isolate, thread_id); |
| 1355 | if (data != NULL) { |
| 1356 | Remove(data); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1361 | void Isolate::ThreadDataTable::RemoveAllThreads(Isolate* isolate) { |
| 1362 | PerIsolateThreadData* data = list_; |
| 1363 | while (data != NULL) { |
| 1364 | PerIsolateThreadData* next = data->next_; |
| 1365 | if (data->isolate() == isolate) Remove(data); |
| 1366 | data = next; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1371 | #ifdef DEBUG |
| 1372 | #define TRACE_ISOLATE(tag) \ |
| 1373 | do { \ |
| 1374 | if (FLAG_trace_isolates) { \ |
| 1375 | PrintF("Isolate %p " #tag "\n", reinterpret_cast<void*>(this)); \ |
| 1376 | } \ |
| 1377 | } while (false) |
| 1378 | #else |
| 1379 | #define TRACE_ISOLATE(tag) |
| 1380 | #endif |
| 1381 | |
| 1382 | |
| 1383 | Isolate::Isolate() |
| 1384 | : state_(UNINITIALIZED), |
| 1385 | entry_stack_(NULL), |
| 1386 | stack_trace_nesting_level_(0), |
| 1387 | incomplete_message_(NULL), |
| 1388 | preallocated_memory_thread_(NULL), |
| 1389 | preallocated_message_space_(NULL), |
| 1390 | bootstrapper_(NULL), |
| 1391 | runtime_profiler_(NULL), |
| 1392 | compilation_cache_(NULL), |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1393 | counters_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1394 | code_range_(NULL), |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1395 | // Must be initialized early to allow v8::SetResourceConstraints calls. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1396 | break_access_(OS::CreateMutex()), |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1397 | debugger_initialized_(false), |
| 1398 | // Must be initialized early to allow v8::Debug calls. |
| 1399 | debugger_access_(OS::CreateMutex()), |
| 1400 | logger_(NULL), |
| 1401 | stats_table_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1402 | stub_cache_(NULL), |
| 1403 | deoptimizer_data_(NULL), |
| 1404 | capture_stack_trace_for_uncaught_exceptions_(false), |
| 1405 | stack_trace_for_uncaught_exceptions_frame_limit_(0), |
| 1406 | stack_trace_for_uncaught_exceptions_options_(StackTrace::kOverview), |
| 1407 | transcendental_cache_(NULL), |
| 1408 | memory_allocator_(NULL), |
| 1409 | keyed_lookup_cache_(NULL), |
| 1410 | context_slot_cache_(NULL), |
| 1411 | descriptor_lookup_cache_(NULL), |
| 1412 | handle_scope_implementer_(NULL), |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1413 | unicode_cache_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1414 | in_use_list_(0), |
| 1415 | free_list_(0), |
| 1416 | preallocated_storage_preallocated_(false), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1417 | inner_pointer_to_code_cache_(NULL), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1418 | write_input_buffer_(NULL), |
| 1419 | global_handles_(NULL), |
| 1420 | context_switcher_(NULL), |
| 1421 | thread_manager_(NULL), |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1422 | fp_stubs_generated_(false), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1423 | string_tracker_(NULL), |
| 1424 | regexp_stack_(NULL), |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1425 | embedder_data_(NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1426 | TRACE_ISOLATE(constructor); |
| 1427 | |
| 1428 | memset(isolate_addresses_, 0, |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1429 | sizeof(isolate_addresses_[0]) * (kIsolateAddressCount + 1)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1430 | |
| 1431 | heap_.isolate_ = this; |
| 1432 | zone_.isolate_ = this; |
| 1433 | stack_guard_.isolate_ = this; |
| 1434 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1435 | // ThreadManager is initialized early to support locking an isolate |
| 1436 | // before it is entered. |
| 1437 | thread_manager_ = new ThreadManager(); |
| 1438 | thread_manager_->isolate_ = this; |
| 1439 | |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1440 | #if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \ |
| 1441 | defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__) |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1442 | simulator_initialized_ = false; |
| 1443 | simulator_i_cache_ = NULL; |
| 1444 | simulator_redirection_ = NULL; |
| 1445 | #endif |
| 1446 | |
| 1447 | #ifdef DEBUG |
| 1448 | // heap_histograms_ initializes itself. |
| 1449 | memset(&js_spill_information_, 0, sizeof(js_spill_information_)); |
| 1450 | memset(code_kind_statistics_, 0, |
| 1451 | sizeof(code_kind_statistics_[0]) * Code::NUMBER_OF_KINDS); |
| 1452 | #endif |
| 1453 | |
| 1454 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1455 | debug_ = NULL; |
| 1456 | debugger_ = NULL; |
| 1457 | #endif |
| 1458 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1459 | handle_scope_data_.Initialize(); |
| 1460 | |
| 1461 | #define ISOLATE_INIT_EXECUTE(type, name, initial_value) \ |
| 1462 | name##_ = (initial_value); |
| 1463 | ISOLATE_INIT_LIST(ISOLATE_INIT_EXECUTE) |
| 1464 | #undef ISOLATE_INIT_EXECUTE |
| 1465 | |
| 1466 | #define ISOLATE_INIT_ARRAY_EXECUTE(type, name, length) \ |
| 1467 | memset(name##_, 0, sizeof(type) * length); |
| 1468 | ISOLATE_INIT_ARRAY_LIST(ISOLATE_INIT_ARRAY_EXECUTE) |
| 1469 | #undef ISOLATE_INIT_ARRAY_EXECUTE |
| 1470 | } |
| 1471 | |
| 1472 | void Isolate::TearDown() { |
| 1473 | TRACE_ISOLATE(tear_down); |
| 1474 | |
| 1475 | // Temporarily set this isolate as current so that various parts of |
| 1476 | // the isolate can access it in their destructors without having a |
| 1477 | // direct pointer. We don't use Enter/Exit here to avoid |
| 1478 | // initializing the thread data. |
| 1479 | PerIsolateThreadData* saved_data = CurrentPerIsolateThreadData(); |
| 1480 | Isolate* saved_isolate = UncheckedCurrent(); |
| 1481 | SetIsolateThreadLocals(this, NULL); |
| 1482 | |
| 1483 | Deinit(); |
| 1484 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 1485 | { ScopedLock lock(process_wide_mutex_); |
| 1486 | thread_data_table_->RemoveAllThreads(this); |
| 1487 | } |
| 1488 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1489 | if (!IsDefaultIsolate()) { |
| 1490 | delete this; |
| 1491 | } |
| 1492 | |
| 1493 | // Restore the previous current isolate. |
| 1494 | SetIsolateThreadLocals(saved_isolate, saved_data); |
| 1495 | } |
| 1496 | |
| 1497 | |
| 1498 | void Isolate::Deinit() { |
| 1499 | if (state_ == INITIALIZED) { |
| 1500 | TRACE_ISOLATE(deinit); |
| 1501 | |
| 1502 | if (FLAG_hydrogen_stats) HStatistics::Instance()->Print(); |
| 1503 | |
| 1504 | // We must stop the logger before we tear down other components. |
| 1505 | logger_->EnsureTickerStopped(); |
| 1506 | |
| 1507 | delete deoptimizer_data_; |
| 1508 | deoptimizer_data_ = NULL; |
| 1509 | if (FLAG_preemption) { |
| 1510 | v8::Locker locker; |
| 1511 | v8::Locker::StopPreemption(); |
| 1512 | } |
| 1513 | builtins_.TearDown(); |
| 1514 | bootstrapper_->TearDown(); |
| 1515 | |
| 1516 | // Remove the external reference to the preallocated stack memory. |
| 1517 | delete preallocated_message_space_; |
| 1518 | preallocated_message_space_ = NULL; |
| 1519 | PreallocatedMemoryThreadStop(); |
| 1520 | |
| 1521 | HeapProfiler::TearDown(); |
| 1522 | CpuProfiler::TearDown(); |
| 1523 | if (runtime_profiler_ != NULL) { |
| 1524 | runtime_profiler_->TearDown(); |
| 1525 | delete runtime_profiler_; |
| 1526 | runtime_profiler_ = NULL; |
| 1527 | } |
| 1528 | heap_.TearDown(); |
| 1529 | logger_->TearDown(); |
| 1530 | |
| 1531 | // The default isolate is re-initializable due to legacy API. |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1532 | state_ = UNINITIALIZED; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | |
| 1537 | void Isolate::SetIsolateThreadLocals(Isolate* isolate, |
| 1538 | PerIsolateThreadData* data) { |
| 1539 | Thread::SetThreadLocal(isolate_key_, isolate); |
| 1540 | Thread::SetThreadLocal(per_isolate_thread_data_key_, data); |
| 1541 | } |
| 1542 | |
| 1543 | |
| 1544 | Isolate::~Isolate() { |
| 1545 | TRACE_ISOLATE(destructor); |
| 1546 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 1547 | // Has to be called while counters_ are still alive. |
| 1548 | zone_.DeleteKeptSegment(); |
| 1549 | |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1550 | delete[] assembler_spare_buffer_; |
| 1551 | assembler_spare_buffer_ = NULL; |
| 1552 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1553 | delete unicode_cache_; |
| 1554 | unicode_cache_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1555 | |
| 1556 | delete regexp_stack_; |
| 1557 | regexp_stack_ = NULL; |
| 1558 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1559 | delete descriptor_lookup_cache_; |
| 1560 | descriptor_lookup_cache_ = NULL; |
| 1561 | delete context_slot_cache_; |
| 1562 | context_slot_cache_ = NULL; |
| 1563 | delete keyed_lookup_cache_; |
| 1564 | keyed_lookup_cache_ = NULL; |
| 1565 | |
| 1566 | delete transcendental_cache_; |
| 1567 | transcendental_cache_ = NULL; |
| 1568 | delete stub_cache_; |
| 1569 | stub_cache_ = NULL; |
| 1570 | delete stats_table_; |
| 1571 | stats_table_ = NULL; |
| 1572 | |
| 1573 | delete logger_; |
| 1574 | logger_ = NULL; |
| 1575 | |
| 1576 | delete counters_; |
| 1577 | counters_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1578 | |
| 1579 | delete handle_scope_implementer_; |
| 1580 | handle_scope_implementer_ = NULL; |
| 1581 | delete break_access_; |
| 1582 | break_access_ = NULL; |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1583 | delete debugger_access_; |
| 1584 | debugger_access_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1585 | |
| 1586 | delete compilation_cache_; |
| 1587 | compilation_cache_ = NULL; |
| 1588 | delete bootstrapper_; |
| 1589 | bootstrapper_ = NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1590 | delete inner_pointer_to_code_cache_; |
| 1591 | inner_pointer_to_code_cache_ = NULL; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1592 | delete write_input_buffer_; |
| 1593 | write_input_buffer_ = NULL; |
| 1594 | |
| 1595 | delete context_switcher_; |
| 1596 | context_switcher_ = NULL; |
| 1597 | delete thread_manager_; |
| 1598 | thread_manager_ = NULL; |
| 1599 | |
| 1600 | delete string_tracker_; |
| 1601 | string_tracker_ = NULL; |
| 1602 | |
| 1603 | delete memory_allocator_; |
| 1604 | memory_allocator_ = NULL; |
| 1605 | delete code_range_; |
| 1606 | code_range_ = NULL; |
| 1607 | delete global_handles_; |
| 1608 | global_handles_ = NULL; |
| 1609 | |
danno@chromium.org | b645116 | 2011-08-17 14:33:23 +0000 | [diff] [blame] | 1610 | delete external_reference_table_; |
| 1611 | external_reference_table_ = NULL; |
| 1612 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1613 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1614 | delete debugger_; |
| 1615 | debugger_ = NULL; |
| 1616 | delete debug_; |
| 1617 | debug_ = NULL; |
| 1618 | #endif |
| 1619 | } |
| 1620 | |
| 1621 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1622 | void Isolate::InitializeThreadLocal() { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1623 | thread_local_top_.isolate_ = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1624 | thread_local_top_.Initialize(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1628 | void Isolate::PropagatePendingExceptionToExternalTryCatch() { |
| 1629 | ASSERT(has_pending_exception()); |
| 1630 | |
| 1631 | bool external_caught = IsExternallyCaught(); |
| 1632 | thread_local_top_.external_caught_exception_ = external_caught; |
| 1633 | |
| 1634 | if (!external_caught) return; |
| 1635 | |
| 1636 | if (thread_local_top_.pending_exception_ == Failure::OutOfMemoryException()) { |
| 1637 | // Do not propagate OOM exception: we should kill VM asap. |
| 1638 | } else if (thread_local_top_.pending_exception_ == |
| 1639 | heap()->termination_exception()) { |
| 1640 | try_catch_handler()->can_continue_ = false; |
| 1641 | try_catch_handler()->exception_ = heap()->null_value(); |
| 1642 | } else { |
| 1643 | // At this point all non-object (failure) exceptions have |
| 1644 | // been dealt with so this shouldn't fail. |
| 1645 | ASSERT(!pending_exception()->IsFailure()); |
| 1646 | try_catch_handler()->can_continue_ = true; |
| 1647 | try_catch_handler()->exception_ = pending_exception(); |
| 1648 | if (!thread_local_top_.pending_message_obj_->IsTheHole()) { |
| 1649 | try_catch_handler()->message_ = thread_local_top_.pending_message_obj_; |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1655 | void Isolate::InitializeLoggingAndCounters() { |
| 1656 | if (logger_ == NULL) { |
| 1657 | logger_ = new Logger; |
| 1658 | } |
| 1659 | if (counters_ == NULL) { |
| 1660 | counters_ = new Counters; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | void Isolate::InitializeDebugger() { |
| 1666 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1667 | ScopedLock lock(debugger_access_); |
| 1668 | if (NoBarrier_Load(&debugger_initialized_)) return; |
| 1669 | InitializeLoggingAndCounters(); |
| 1670 | debug_ = new Debug(this); |
| 1671 | debugger_ = new Debugger(this); |
| 1672 | Release_Store(&debugger_initialized_, true); |
| 1673 | #endif |
| 1674 | } |
| 1675 | |
| 1676 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1677 | bool Isolate::Init(Deserializer* des) { |
| 1678 | ASSERT(state_ != INITIALIZED); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1679 | ASSERT(Isolate::Current() == this); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1680 | TRACE_ISOLATE(init); |
| 1681 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1682 | #ifdef DEBUG |
| 1683 | // The initialization process does not handle memory exhaustion. |
| 1684 | DisallowAllocationFailure disallow_allocation_failure; |
| 1685 | #endif |
| 1686 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1687 | InitializeLoggingAndCounters(); |
| 1688 | |
| 1689 | InitializeDebugger(); |
| 1690 | |
| 1691 | memory_allocator_ = new MemoryAllocator(this); |
| 1692 | code_range_ = new CodeRange(this); |
| 1693 | |
| 1694 | // Safe after setting Heap::isolate_, initializing StackGuard and |
| 1695 | // ensuring that Isolate::Current() == this. |
| 1696 | heap_.SetStackLimits(); |
| 1697 | |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1698 | #define ASSIGN_ELEMENT(CamelName, hacker_name) \ |
| 1699 | isolate_addresses_[Isolate::k##CamelName##Address] = \ |
| 1700 | reinterpret_cast<Address>(hacker_name##_address()); |
| 1701 | FOR_EACH_ISOLATE_ADDRESS_NAME(ASSIGN_ELEMENT) |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1702 | #undef C |
| 1703 | |
| 1704 | string_tracker_ = new StringTracker(); |
| 1705 | string_tracker_->isolate_ = this; |
| 1706 | compilation_cache_ = new CompilationCache(this); |
| 1707 | transcendental_cache_ = new TranscendentalCache(); |
| 1708 | keyed_lookup_cache_ = new KeyedLookupCache(); |
| 1709 | context_slot_cache_ = new ContextSlotCache(); |
| 1710 | descriptor_lookup_cache_ = new DescriptorLookupCache(); |
| 1711 | unicode_cache_ = new UnicodeCache(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1712 | inner_pointer_to_code_cache_ = new InnerPointerToCodeCache(this); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1713 | write_input_buffer_ = new StringInputBuffer(); |
| 1714 | global_handles_ = new GlobalHandles(this); |
| 1715 | bootstrapper_ = new Bootstrapper(); |
| 1716 | handle_scope_implementer_ = new HandleScopeImplementer(this); |
| 1717 | stub_cache_ = new StubCache(this); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1718 | regexp_stack_ = new RegExpStack(); |
| 1719 | regexp_stack_->isolate_ = this; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1720 | |
| 1721 | // Enable logging before setting up the heap |
| 1722 | logger_->Setup(); |
| 1723 | |
| 1724 | CpuProfiler::Setup(); |
| 1725 | HeapProfiler::Setup(); |
| 1726 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1727 | // Initialize other runtime facilities |
| 1728 | #if defined(USE_SIMULATOR) |
lrn@chromium.org | 7516f05 | 2011-03-30 08:52:27 +0000 | [diff] [blame] | 1729 | #if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS) |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 1730 | Simulator::Initialize(this); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1731 | #endif |
| 1732 | #endif |
| 1733 | |
| 1734 | { // NOLINT |
| 1735 | // Ensure that the thread has a valid stack guard. The v8::Locker object |
| 1736 | // will ensure this too, but we don't have to use lockers if we are only |
| 1737 | // using one thread. |
| 1738 | ExecutionAccess lock(this); |
| 1739 | stack_guard_.InitThread(lock); |
| 1740 | } |
| 1741 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1742 | // Setup the object heap. |
| 1743 | const bool create_heap_objects = (des == NULL); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1744 | ASSERT(!heap_.HasBeenSetup()); |
| 1745 | if (!heap_.Setup(create_heap_objects)) { |
| 1746 | V8::SetFatalError(); |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 1750 | InitializeThreadLocal(); |
| 1751 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1752 | bootstrapper_->Initialize(create_heap_objects); |
| 1753 | builtins_.Setup(create_heap_objects); |
| 1754 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1755 | // Only preallocate on the first initialization. |
| 1756 | if (FLAG_preallocate_message_memory && preallocated_message_space_ == NULL) { |
| 1757 | // Start the thread which will set aside some memory. |
| 1758 | PreallocatedMemoryThreadStart(); |
| 1759 | preallocated_message_space_ = |
| 1760 | new NoAllocationStringAllocator( |
| 1761 | preallocated_memory_thread_->data(), |
| 1762 | preallocated_memory_thread_->length()); |
| 1763 | PreallocatedStorageInit(preallocated_memory_thread_->length() / 4); |
| 1764 | } |
| 1765 | |
| 1766 | if (FLAG_preemption) { |
| 1767 | v8::Locker locker; |
| 1768 | v8::Locker::StartPreemption(100); |
| 1769 | } |
| 1770 | |
| 1771 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1772 | debug_->Setup(create_heap_objects); |
| 1773 | #endif |
| 1774 | stub_cache_->Initialize(create_heap_objects); |
| 1775 | |
| 1776 | // If we are deserializing, read the state into the now-empty heap. |
| 1777 | if (des != NULL) { |
| 1778 | des->Deserialize(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1779 | stub_cache_->Initialize(true); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame^] | 1782 | // Finish initialization of ThreadLocal after deserialization is done. |
| 1783 | clear_pending_exception(); |
| 1784 | clear_pending_message(); |
| 1785 | clear_scheduled_exception(); |
| 1786 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1787 | // Deserializing may put strange things in the root array's copy of the |
| 1788 | // stack guard. |
| 1789 | heap_.SetStackLimits(); |
| 1790 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1791 | deoptimizer_data_ = new DeoptimizerData; |
| 1792 | runtime_profiler_ = new RuntimeProfiler(this); |
| 1793 | runtime_profiler_->Setup(); |
| 1794 | |
| 1795 | // If we are deserializing, log non-function code objects and compiled |
| 1796 | // functions found in the snapshot. |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 1797 | if (des != NULL && (FLAG_log_code || FLAG_ll_prof)) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1798 | HandleScope scope; |
| 1799 | LOG(this, LogCodeObjects()); |
| 1800 | LOG(this, LogCompiledFunctions()); |
| 1801 | } |
| 1802 | |
| 1803 | state_ = INITIALIZED; |
| 1804 | return true; |
| 1805 | } |
| 1806 | |
| 1807 | |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 1808 | // Initialized lazily to allow early |
| 1809 | // v8::V8::SetAddHistogramSampleFunction calls. |
| 1810 | StatsTable* Isolate::stats_table() { |
| 1811 | if (stats_table_ == NULL) { |
| 1812 | stats_table_ = new StatsTable; |
| 1813 | } |
| 1814 | return stats_table_; |
| 1815 | } |
| 1816 | |
| 1817 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1818 | void Isolate::Enter() { |
| 1819 | Isolate* current_isolate = NULL; |
| 1820 | PerIsolateThreadData* current_data = CurrentPerIsolateThreadData(); |
| 1821 | if (current_data != NULL) { |
| 1822 | current_isolate = current_data->isolate_; |
| 1823 | ASSERT(current_isolate != NULL); |
| 1824 | if (current_isolate == this) { |
| 1825 | ASSERT(Current() == this); |
| 1826 | ASSERT(entry_stack_ != NULL); |
| 1827 | ASSERT(entry_stack_->previous_thread_data == NULL || |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1828 | entry_stack_->previous_thread_data->thread_id().Equals( |
| 1829 | ThreadId::Current())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1830 | // Same thread re-enters the isolate, no need to re-init anything. |
| 1831 | entry_stack_->entry_count++; |
| 1832 | return; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | // Threads can have default isolate set into TLS as Current but not yet have |
| 1837 | // PerIsolateThreadData for it, as it requires more advanced phase of the |
| 1838 | // initialization. For example, a thread might be the one that system used for |
| 1839 | // static initializers - in this case the default isolate is set in TLS but |
| 1840 | // the thread did not yet Enter the isolate. If PerisolateThreadData is not |
| 1841 | // there, use the isolate set in TLS. |
| 1842 | if (current_isolate == NULL) { |
| 1843 | current_isolate = Isolate::UncheckedCurrent(); |
| 1844 | } |
| 1845 | |
| 1846 | PerIsolateThreadData* data = FindOrAllocatePerThreadDataForThisThread(); |
| 1847 | ASSERT(data != NULL); |
| 1848 | ASSERT(data->isolate_ == this); |
| 1849 | |
| 1850 | EntryStackItem* item = new EntryStackItem(current_data, |
| 1851 | current_isolate, |
| 1852 | entry_stack_); |
| 1853 | entry_stack_ = item; |
| 1854 | |
| 1855 | SetIsolateThreadLocals(this, data); |
| 1856 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1857 | // In case it's the first time some thread enters the isolate. |
| 1858 | set_thread_id(data->thread_id()); |
| 1859 | } |
| 1860 | |
| 1861 | |
| 1862 | void Isolate::Exit() { |
| 1863 | ASSERT(entry_stack_ != NULL); |
| 1864 | ASSERT(entry_stack_->previous_thread_data == NULL || |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 1865 | entry_stack_->previous_thread_data->thread_id().Equals( |
| 1866 | ThreadId::Current())); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1867 | |
| 1868 | if (--entry_stack_->entry_count > 0) return; |
| 1869 | |
| 1870 | ASSERT(CurrentPerIsolateThreadData() != NULL); |
| 1871 | ASSERT(CurrentPerIsolateThreadData()->isolate_ == this); |
| 1872 | |
| 1873 | // Pop the stack. |
| 1874 | EntryStackItem* item = entry_stack_; |
| 1875 | entry_stack_ = item->previous_item; |
| 1876 | |
| 1877 | PerIsolateThreadData* previous_thread_data = item->previous_thread_data; |
| 1878 | Isolate* previous_isolate = item->previous_isolate; |
| 1879 | |
| 1880 | delete item; |
| 1881 | |
| 1882 | // Reinit the current thread for the isolate it was running before this one. |
| 1883 | SetIsolateThreadLocals(previous_isolate, previous_thread_data); |
| 1884 | } |
| 1885 | |
| 1886 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1887 | #ifdef DEBUG |
| 1888 | #define ISOLATE_FIELD_OFFSET(type, name, ignored) \ |
| 1889 | const intptr_t Isolate::name##_debug_offset_ = OFFSET_OF(Isolate, name##_); |
| 1890 | ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET) |
| 1891 | ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET) |
| 1892 | #undef ISOLATE_FIELD_OFFSET |
| 1893 | #endif |
| 1894 | |
| 1895 | } } // namespace v8::internal |