| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/api.h" | 5 #include "src/api.h" |
| 6 | 6 |
| 7 #include <string.h> // For memcpy, strlen. | 7 #include <string.h> // For memcpy, strlen. |
| 8 #ifdef V8_USE_ADDRESS_SANITIZER | 8 #ifdef V8_USE_ADDRESS_SANITIZER |
| 9 #include <sanitizer/asan_interface.h> | 9 #include <sanitizer/asan_interface.h> |
| 10 #endif // V8_USE_ADDRESS_SANITIZER | 10 #endif // V8_USE_ADDRESS_SANITIZER |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 ->GetCurrentContext(); | 150 ->GetCurrentContext(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 class InternalEscapableScope : public v8::EscapableHandleScope { | 153 class InternalEscapableScope : public v8::EscapableHandleScope { |
| 154 public: | 154 public: |
| 155 explicit inline InternalEscapableScope(i::Isolate* isolate) | 155 explicit inline InternalEscapableScope(i::Isolate* isolate) |
| 156 : v8::EscapableHandleScope(reinterpret_cast<v8::Isolate*>(isolate)) {} | 156 : v8::EscapableHandleScope(reinterpret_cast<v8::Isolate*>(isolate)) {} |
| 157 }; | 157 }; |
| 158 | 158 |
| 159 | 159 |
| 160 #ifdef V8_ENABLE_CHECKS |
| 161 void CheckMicrotasksScopesConsistency(i::Isolate* isolate) { |
| 162 auto handle_scope_implementer = isolate->handle_scope_implementer(); |
| 163 if (handle_scope_implementer->microtasks_policy() == |
| 164 v8::MicrotasksPolicy::kScoped) { |
| 165 DCHECK(handle_scope_implementer->GetMicrotasksScopeDepth() || |
| 166 !handle_scope_implementer->DebugMicrotasksScopeDepthIsZero()); |
| 167 } |
| 168 } |
| 169 #endif |
| 170 |
| 171 |
| 160 class CallDepthScope { | 172 class CallDepthScope { |
| 161 public: | 173 public: |
| 162 explicit CallDepthScope(i::Isolate* isolate, Local<Context> context, | 174 explicit CallDepthScope(i::Isolate* isolate, Local<Context> context, |
| 163 bool do_callback) | 175 bool do_callback) |
| 164 : isolate_(isolate), | 176 : isolate_(isolate), |
| 165 context_(context), | 177 context_(context), |
| 166 escaped_(false), | 178 escaped_(false), |
| 167 do_callback_(do_callback) { | 179 do_callback_(do_callback) { |
| 168 // TODO(dcarney): remove this when blink stops crashing. | 180 // TODO(dcarney): remove this when blink stops crashing. |
| 169 DCHECK(!isolate_->external_caught_exception()); | 181 DCHECK(!isolate_->external_caught_exception()); |
| 170 isolate_->IncrementJsCallsFromApiCounter(); | 182 isolate_->IncrementJsCallsFromApiCounter(); |
| 171 isolate_->handle_scope_implementer()->IncrementCallDepth(); | 183 isolate_->handle_scope_implementer()->IncrementCallDepth(); |
| 172 if (!context_.IsEmpty()) context_->Enter(); | 184 if (!context_.IsEmpty()) context_->Enter(); |
| 173 if (do_callback_) isolate_->FireBeforeCallEnteredCallback(); | 185 if (do_callback_) isolate_->FireBeforeCallEnteredCallback(); |
| 174 } | 186 } |
| 175 ~CallDepthScope() { | 187 ~CallDepthScope() { |
| 176 if (!context_.IsEmpty()) context_->Exit(); | 188 if (!context_.IsEmpty()) context_->Exit(); |
| 177 if (!escaped_) isolate_->handle_scope_implementer()->DecrementCallDepth(); | 189 if (!escaped_) isolate_->handle_scope_implementer()->DecrementCallDepth(); |
| 178 if (do_callback_) isolate_->FireCallCompletedCallback(); | 190 if (do_callback_) isolate_->FireCallCompletedCallback(); |
| 191 #ifdef V8_ENABLE_CHECKS |
| 192 if (do_callback_) CheckMicrotasksScopesConsistency(isolate_); |
| 193 #endif |
| 179 } | 194 } |
| 180 | 195 |
| 181 void Escape() { | 196 void Escape() { |
| 182 DCHECK(!escaped_); | 197 DCHECK(!escaped_); |
| 183 escaped_ = true; | 198 escaped_ = true; |
| 184 auto handle_scope_implementer = isolate_->handle_scope_implementer(); | 199 auto handle_scope_implementer = isolate_->handle_scope_implementer(); |
| 185 handle_scope_implementer->DecrementCallDepth(); | 200 handle_scope_implementer->DecrementCallDepth(); |
| 186 bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero(); | 201 bool call_depth_is_zero = handle_scope_implementer->CallDepthIsZero(); |
| 187 isolate_->OptionalRescheduleException(call_depth_is_zero); | 202 isolate_->OptionalRescheduleException(call_depth_is_zero); |
| 188 } | 203 } |
| (...skipping 7102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7291 Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() { | 7306 Isolate::AllowJavascriptExecutionScope::~AllowJavascriptExecutionScope() { |
| 7292 delete reinterpret_cast<i::AllowJavascriptExecution*>(internal_assert_); | 7307 delete reinterpret_cast<i::AllowJavascriptExecution*>(internal_assert_); |
| 7293 delete reinterpret_cast<i::NoThrowOnJavascriptExecution*>(internal_throws_); | 7308 delete reinterpret_cast<i::NoThrowOnJavascriptExecution*>(internal_throws_); |
| 7294 } | 7309 } |
| 7295 | 7310 |
| 7296 | 7311 |
| 7297 Isolate::SuppressMicrotaskExecutionScope::SuppressMicrotaskExecutionScope( | 7312 Isolate::SuppressMicrotaskExecutionScope::SuppressMicrotaskExecutionScope( |
| 7298 Isolate* isolate) | 7313 Isolate* isolate) |
| 7299 : isolate_(reinterpret_cast<i::Isolate*>(isolate)) { | 7314 : isolate_(reinterpret_cast<i::Isolate*>(isolate)) { |
| 7300 isolate_->handle_scope_implementer()->IncrementCallDepth(); | 7315 isolate_->handle_scope_implementer()->IncrementCallDepth(); |
| 7316 isolate_->handle_scope_implementer()->IncrementMicrotasksSuppressions(); |
| 7301 } | 7317 } |
| 7302 | 7318 |
| 7303 | 7319 |
| 7304 Isolate::SuppressMicrotaskExecutionScope::~SuppressMicrotaskExecutionScope() { | 7320 Isolate::SuppressMicrotaskExecutionScope::~SuppressMicrotaskExecutionScope() { |
| 7321 isolate_->handle_scope_implementer()->DecrementMicrotasksSuppressions(); |
| 7305 isolate_->handle_scope_implementer()->DecrementCallDepth(); | 7322 isolate_->handle_scope_implementer()->DecrementCallDepth(); |
| 7306 } | 7323 } |
| 7307 | 7324 |
| 7308 | 7325 |
| 7309 void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) { | 7326 void Isolate::GetHeapStatistics(HeapStatistics* heap_statistics) { |
| 7310 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); | 7327 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 7311 i::Heap* heap = isolate->heap(); | 7328 i::Heap* heap = isolate->heap(); |
| 7312 heap_statistics->total_heap_size_ = heap->CommittedMemory(); | 7329 heap_statistics->total_heap_size_ = heap->CommittedMemory(); |
| 7313 heap_statistics->total_heap_size_executable_ = | 7330 heap_statistics->total_heap_size_executable_ = |
| 7314 heap->CommittedMemoryExecutable(); | 7331 heap->CommittedMemoryExecutable(); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7436 | 7453 |
| 7437 | 7454 |
| 7438 void Isolate::SetPromiseRejectCallback(PromiseRejectCallback callback) { | 7455 void Isolate::SetPromiseRejectCallback(PromiseRejectCallback callback) { |
| 7439 if (callback == NULL) return; | 7456 if (callback == NULL) return; |
| 7440 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); | 7457 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 7441 isolate->SetPromiseRejectCallback(callback); | 7458 isolate->SetPromiseRejectCallback(callback); |
| 7442 } | 7459 } |
| 7443 | 7460 |
| 7444 | 7461 |
| 7445 void Isolate::RunMicrotasks() { | 7462 void Isolate::RunMicrotasks() { |
| 7463 DCHECK(MicrotasksPolicy::kScoped != GetMicrotasksPolicy()); |
| 7446 reinterpret_cast<i::Isolate*>(this)->RunMicrotasks(); | 7464 reinterpret_cast<i::Isolate*>(this)->RunMicrotasks(); |
| 7447 } | 7465 } |
| 7448 | 7466 |
| 7449 | 7467 |
| 7450 void Isolate::EnqueueMicrotask(Local<Function> microtask) { | 7468 void Isolate::EnqueueMicrotask(Local<Function> microtask) { |
| 7451 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); | 7469 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 7452 isolate->EnqueueMicrotask(Utils::OpenHandle(*microtask)); | 7470 isolate->EnqueueMicrotask(Utils::OpenHandle(*microtask)); |
| 7453 } | 7471 } |
| 7454 | 7472 |
| 7455 | 7473 |
| 7456 void Isolate::EnqueueMicrotask(MicrotaskCallback microtask, void* data) { | 7474 void Isolate::EnqueueMicrotask(MicrotaskCallback microtask, void* data) { |
| 7457 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); | 7475 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 7458 i::HandleScope scope(isolate); | 7476 i::HandleScope scope(isolate); |
| 7459 i::Handle<i::CallHandlerInfo> callback_info = | 7477 i::Handle<i::CallHandlerInfo> callback_info = |
| 7460 i::Handle<i::CallHandlerInfo>::cast( | 7478 i::Handle<i::CallHandlerInfo>::cast( |
| 7461 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE)); | 7479 isolate->factory()->NewStruct(i::CALL_HANDLER_INFO_TYPE)); |
| 7462 SET_FIELD_WRAPPED(callback_info, set_callback, microtask); | 7480 SET_FIELD_WRAPPED(callback_info, set_callback, microtask); |
| 7463 SET_FIELD_WRAPPED(callback_info, set_data, data); | 7481 SET_FIELD_WRAPPED(callback_info, set_data, data); |
| 7464 isolate->EnqueueMicrotask(callback_info); | 7482 isolate->EnqueueMicrotask(callback_info); |
| 7465 } | 7483 } |
| 7466 | 7484 |
| 7467 | 7485 |
| 7468 void Isolate::SetAutorunMicrotasks(bool autorun) { | 7486 void Isolate::SetAutorunMicrotasks(bool autorun) { |
| 7469 reinterpret_cast<i::Isolate*>(this)->set_autorun_microtasks(autorun); | 7487 SetMicrotasksPolicy( |
| 7488 autorun ? MicrotasksPolicy::kAuto : MicrotasksPolicy::kExplicit); |
| 7470 } | 7489 } |
| 7471 | 7490 |
| 7472 | 7491 |
| 7473 bool Isolate::WillAutorunMicrotasks() const { | 7492 bool Isolate::WillAutorunMicrotasks() const { |
| 7474 return reinterpret_cast<const i::Isolate*>(this)->autorun_microtasks(); | 7493 return GetMicrotasksPolicy() == MicrotasksPolicy::kAuto; |
| 7475 } | 7494 } |
| 7476 | 7495 |
| 7477 | 7496 |
| 7497 void Isolate::SetMicrotasksPolicy(MicrotasksPolicy policy) { |
| 7498 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 7499 isolate->handle_scope_implementer()->set_microtasks_policy(policy); |
| 7500 } |
| 7501 |
| 7502 |
| 7503 MicrotasksPolicy Isolate::GetMicrotasksPolicy() const { |
| 7504 i::Isolate* isolate = |
| 7505 reinterpret_cast<i::Isolate*>(const_cast<Isolate*>(this)); |
| 7506 return isolate->handle_scope_implementer()->microtasks_policy(); |
| 7507 } |
| 7508 |
| 7509 |
| 7478 void Isolate::AddMicrotasksCompletedCallback( | 7510 void Isolate::AddMicrotasksCompletedCallback( |
| 7479 MicrotasksCompletedCallback callback) { | 7511 MicrotasksCompletedCallback callback) { |
| 7480 DCHECK(callback); | 7512 DCHECK(callback); |
| 7481 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); | 7513 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 7482 isolate->AddMicrotasksCompletedCallback(callback); | 7514 isolate->AddMicrotasksCompletedCallback(callback); |
| 7483 } | 7515 } |
| 7484 | 7516 |
| 7485 | 7517 |
| 7486 void Isolate::RemoveMicrotasksCompletedCallback( | 7518 void Isolate::RemoveMicrotasksCompletedCallback( |
| 7487 MicrotasksCompletedCallback callback) { | 7519 MicrotasksCompletedCallback callback) { |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7703 | 7735 |
| 7704 void Isolate::VisitWeakHandles(PersistentHandleVisitor* visitor) { | 7736 void Isolate::VisitWeakHandles(PersistentHandleVisitor* visitor) { |
| 7705 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); | 7737 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this); |
| 7706 i::DisallowHeapAllocation no_allocation; | 7738 i::DisallowHeapAllocation no_allocation; |
| 7707 VisitorAdapter visitor_adapter(visitor); | 7739 VisitorAdapter visitor_adapter(visitor); |
| 7708 isolate->global_handles()->IterateWeakRootsInNewSpaceWithClassIds( | 7740 isolate->global_handles()->IterateWeakRootsInNewSpaceWithClassIds( |
| 7709 &visitor_adapter); | 7741 &visitor_adapter); |
| 7710 } | 7742 } |
| 7711 | 7743 |
| 7712 | 7744 |
| 7745 MicrotasksScope::MicrotasksScope(Isolate* isolate, MicrotasksScope::Type type) |
| 7746 : isolate_(reinterpret_cast<i::Isolate*>(isolate)), |
| 7747 run_(type == MicrotasksScope::kRunMicrotasks) { |
| 7748 auto handle_scope_implementer = isolate_->handle_scope_implementer(); |
| 7749 if (run_) handle_scope_implementer->IncrementMicrotasksScopeDepth(); |
| 7750 #ifdef V8_ENABLE_CHECKS |
| 7751 if (!run_) handle_scope_implementer->IncrementDebugMicrotasksScopeDepth(); |
| 7752 #endif |
| 7753 } |
| 7754 |
| 7755 |
| 7756 MicrotasksScope::~MicrotasksScope() { |
| 7757 auto handle_scope_implementer = isolate_->handle_scope_implementer(); |
| 7758 if (run_) { |
| 7759 handle_scope_implementer->DecrementMicrotasksScopeDepth(); |
| 7760 if (MicrotasksPolicy::kScoped == |
| 7761 handle_scope_implementer->microtasks_policy()) { |
| 7762 PerformCheckpoint(reinterpret_cast<Isolate*>(isolate_)); |
| 7763 } |
| 7764 } |
| 7765 #ifdef V8_ENABLE_CHECKS |
| 7766 if (!run_) handle_scope_implementer->DecrementDebugMicrotasksScopeDepth(); |
| 7767 #endif |
| 7768 } |
| 7769 |
| 7770 |
| 7771 void MicrotasksScope::PerformCheckpoint(Isolate* v8Isolate) { |
| 7772 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate); |
| 7773 if (IsExecutionTerminatingCheck(isolate)) return; |
| 7774 auto handle_scope_implementer = isolate->handle_scope_implementer(); |
| 7775 if (!handle_scope_implementer->GetMicrotasksScopeDepth() && |
| 7776 !handle_scope_implementer->HasMicrotasksSuppressions()) { |
| 7777 isolate->RunMicrotasks(); |
| 7778 } |
| 7779 } |
| 7780 |
| 7781 |
| 7782 int MicrotasksScope::GetCurrentDepth(Isolate* v8Isolate) { |
| 7783 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8Isolate); |
| 7784 return isolate->handle_scope_implementer()->GetMicrotasksScopeDepth(); |
| 7785 } |
| 7786 |
| 7787 |
| 7713 String::Utf8Value::Utf8Value(v8::Local<v8::Value> obj) | 7788 String::Utf8Value::Utf8Value(v8::Local<v8::Value> obj) |
| 7714 : str_(NULL), length_(0) { | 7789 : str_(NULL), length_(0) { |
| 7715 if (obj.IsEmpty()) return; | 7790 if (obj.IsEmpty()) return; |
| 7716 i::Isolate* isolate = i::Isolate::Current(); | 7791 i::Isolate* isolate = i::Isolate::Current(); |
| 7717 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate); | 7792 Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate); |
| 7718 ENTER_V8(isolate); | 7793 ENTER_V8(isolate); |
| 7719 i::HandleScope scope(isolate); | 7794 i::HandleScope scope(isolate); |
| 7720 Local<Context> context = v8_isolate->GetCurrentContext(); | 7795 Local<Context> context = v8_isolate->GetCurrentContext(); |
| 7721 TryCatch try_catch(v8_isolate); | 7796 TryCatch try_catch(v8_isolate); |
| 7722 Local<String> str; | 7797 Local<String> str; |
| (...skipping 898 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8621 Address callback_address = | 8696 Address callback_address = |
| 8622 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 8697 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
| 8623 VMState<EXTERNAL> state(isolate); | 8698 VMState<EXTERNAL> state(isolate); |
| 8624 ExternalCallbackScope call_scope(isolate, callback_address); | 8699 ExternalCallbackScope call_scope(isolate, callback_address); |
| 8625 callback(info); | 8700 callback(info); |
| 8626 } | 8701 } |
| 8627 | 8702 |
| 8628 | 8703 |
| 8629 } // namespace internal | 8704 } // namespace internal |
| 8630 } // namespace v8 | 8705 } // namespace v8 |
| OLD | NEW |