2012-02-08 09:55:25 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
2012-04-05 14:10:39 +00:00
|
|
|
#include "assembler.h"
|
2011-03-18 20:35:07 +00:00
|
|
|
#include "isolate.h"
|
2011-08-03 11:12:46 +00:00
|
|
|
#include "elements.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "bootstrapper.h"
|
|
|
|
#include "debug.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "deoptimizer.h"
|
2012-04-05 14:10:39 +00:00
|
|
|
#include "frames.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "heap-profiler.h"
|
|
|
|
#include "hydrogen.h"
|
2013-12-20 07:52:58 +00:00
|
|
|
#ifdef V8_USE_DEFAULT_PLATFORM
|
|
|
|
#include "libplatform/default-platform.h"
|
|
|
|
#endif
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "lithium-allocator.h"
|
2012-11-08 13:44:59 +00:00
|
|
|
#include "objects.h"
|
2012-03-12 13:56:56 +00:00
|
|
|
#include "once.h"
|
|
|
|
#include "platform.h"
|
2013-04-15 13:57:41 +00:00
|
|
|
#include "sampler.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "runtime-profiler.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "serialize.h"
|
2011-09-19 18:36:47 +00:00
|
|
|
#include "store-buffer.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-03-12 13:56:56 +00:00
|
|
|
V8_DECLARE_ONCE(init_once);
|
2011-03-31 16:17:37 +00:00
|
|
|
|
2012-01-04 08:48:35 +00:00
|
|
|
List<CallCompletedCallback>* V8::call_completed_callbacks_ = NULL;
|
2013-06-11 10:41:14 +00:00
|
|
|
v8::ArrayBuffer::Allocator* V8::array_buffer_allocator_ = NULL;
|
2013-11-21 14:07:06 +00:00
|
|
|
v8::Platform* V8::platform_ = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2010-11-16 08:01:45 +00:00
|
|
|
|
2010-03-17 14:53:16 +00:00
|
|
|
bool V8::Initialize(Deserializer* des) {
|
2011-03-31 16:17:37 +00:00
|
|
|
InitializeOncePerProcess();
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// The current thread may not yet had entered an isolate to run.
|
|
|
|
// Note the Isolate::Current() may be non-null because for various
|
2013-09-06 11:24:26 +00:00
|
|
|
// initialization purposes an initializing thread may be assigned an isolate
|
|
|
|
// but not actually enter it.
|
2011-03-18 20:35:07 +00:00
|
|
|
if (i::Isolate::CurrentPerIsolateThreadData() == NULL) {
|
|
|
|
i::Isolate::EnterDefaultIsolate();
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(i::Isolate::CurrentPerIsolateThreadData() != NULL);
|
2011-04-11 23:46:22 +00:00
|
|
|
ASSERT(i::Isolate::CurrentPerIsolateThreadData()->thread_id().Equals(
|
|
|
|
i::ThreadId::Current()));
|
2011-03-18 20:35:07 +00:00
|
|
|
ASSERT(i::Isolate::CurrentPerIsolateThreadData()->isolate() ==
|
|
|
|
i::Isolate::Current());
|
|
|
|
|
|
|
|
Isolate* isolate = Isolate::Current();
|
2013-09-03 09:35:26 +00:00
|
|
|
if (isolate->IsDead()) return false;
|
2011-03-18 20:35:07 +00:00
|
|
|
if (isolate->IsInitialized()) return true;
|
2009-05-25 19:39:52 +00:00
|
|
|
|
2013-12-20 07:52:58 +00:00
|
|
|
#ifdef V8_USE_DEFAULT_PLATFORM
|
|
|
|
DefaultPlatform* platform = static_cast<DefaultPlatform*>(platform_);
|
|
|
|
platform->SetThreadPoolSize(isolate->max_available_threads());
|
2014-02-20 19:32:27 +00:00
|
|
|
// We currently only start the threads early, if we know that we'll use them.
|
|
|
|
if (FLAG_job_based_sweeping) platform->EnsureInitialized();
|
2013-12-20 07:52:58 +00:00
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
return isolate->Init(des);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-02 09:01:40 +00:00
|
|
|
void V8::TearDown() {
|
|
|
|
Isolate* isolate = Isolate::Current();
|
|
|
|
ASSERT(isolate->IsDefaultIsolate());
|
|
|
|
if (!isolate->IsInitialized()) return;
|
2012-03-27 07:37:06 +00:00
|
|
|
|
2013-10-02 09:01:40 +00:00
|
|
|
// The isolate has to be torn down before clearing the LOperand
|
2012-07-19 18:58:23 +00:00
|
|
|
// caches so that the optimizing compiler thread (if running)
|
|
|
|
// doesn't see an inconsistent view of the lithium instructions.
|
2013-10-02 09:01:40 +00:00
|
|
|
isolate->TearDown();
|
|
|
|
delete isolate;
|
2012-07-19 18:58:23 +00:00
|
|
|
|
2014-01-17 10:52:00 +00:00
|
|
|
Bootstrapper::TearDownExtensions();
|
2012-03-27 10:51:13 +00:00
|
|
|
ElementsAccessor::TearDown();
|
2012-03-28 13:12:00 +00:00
|
|
|
LOperand::TearDownCaches();
|
2012-11-26 13:12:35 +00:00
|
|
|
ExternalReference::TearDownMathExpData();
|
2012-03-29 09:45:46 +00:00
|
|
|
RegisteredExtension::UnregisterAll();
|
2013-02-12 11:57:51 +00:00
|
|
|
Isolate::GlobalTearDown();
|
2012-03-27 10:51:13 +00:00
|
|
|
|
2012-01-04 08:48:35 +00:00
|
|
|
delete call_completed_callbacks_;
|
|
|
|
call_completed_callbacks_ = NULL;
|
2012-04-13 14:08:31 +00:00
|
|
|
|
2013-04-15 13:57:41 +00:00
|
|
|
Sampler::TearDown();
|
2013-11-21 14:07:06 +00:00
|
|
|
|
|
|
|
#ifdef V8_USE_DEFAULT_PLATFORM
|
|
|
|
DefaultPlatform* platform = static_cast<DefaultPlatform*>(platform_);
|
|
|
|
platform_ = NULL;
|
|
|
|
delete platform;
|
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2009-05-25 19:39:52 +00:00
|
|
|
|
2012-02-27 15:15:53 +00:00
|
|
|
void V8::SetReturnAddressLocationResolver(
|
|
|
|
ReturnAddressLocationResolver resolver) {
|
|
|
|
StackFrame::SetReturnAddressLocationResolver(resolver);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-04 08:48:35 +00:00
|
|
|
void V8::AddCallCompletedCallback(CallCompletedCallback callback) {
|
|
|
|
if (call_completed_callbacks_ == NULL) { // Lazy init.
|
|
|
|
call_completed_callbacks_ = new List<CallCompletedCallback>();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < call_completed_callbacks_->length(); i++) {
|
|
|
|
if (callback == call_completed_callbacks_->at(i)) return;
|
|
|
|
}
|
|
|
|
call_completed_callbacks_->Add(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void V8::RemoveCallCompletedCallback(CallCompletedCallback callback) {
|
|
|
|
if (call_completed_callbacks_ == NULL) return;
|
|
|
|
for (int i = 0; i < call_completed_callbacks_->length(); i++) {
|
|
|
|
if (callback == call_completed_callbacks_->at(i)) {
|
|
|
|
call_completed_callbacks_->Remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void V8::FireCallCompletedCallback(Isolate* isolate) {
|
2012-11-08 13:44:59 +00:00
|
|
|
bool has_call_completed_callbacks = call_completed_callbacks_ != NULL;
|
2014-02-12 22:04:19 +00:00
|
|
|
bool run_microtasks = isolate->autorun_microtasks() &&
|
|
|
|
isolate->microtask_pending();
|
|
|
|
if (!has_call_completed_callbacks && !run_microtasks) return;
|
2013-11-27 17:21:40 +00:00
|
|
|
|
2012-01-04 08:48:35 +00:00
|
|
|
HandleScopeImplementer* handle_scope_implementer =
|
|
|
|
isolate->handle_scope_implementer();
|
|
|
|
if (!handle_scope_implementer->CallDepthIsZero()) return;
|
|
|
|
// Fire callbacks. Increase call depth to prevent recursive callbacks.
|
|
|
|
handle_scope_implementer->IncrementCallDepth();
|
2014-02-12 22:04:19 +00:00
|
|
|
if (run_microtasks) Execution::RunMicrotasks(isolate);
|
2012-11-08 13:44:59 +00:00
|
|
|
if (has_call_completed_callbacks) {
|
|
|
|
for (int i = 0; i < call_completed_callbacks_->length(); i++) {
|
|
|
|
call_completed_callbacks_->at(i)();
|
|
|
|
}
|
2012-01-04 08:48:35 +00:00
|
|
|
}
|
|
|
|
handle_scope_implementer->DecrementCallDepth();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-12 22:04:19 +00:00
|
|
|
void V8::RunMicrotasks(Isolate* isolate) {
|
|
|
|
if (!isolate->microtask_pending())
|
|
|
|
return;
|
|
|
|
|
|
|
|
HandleScopeImplementer* handle_scope_implementer =
|
|
|
|
isolate->handle_scope_implementer();
|
|
|
|
ASSERT(handle_scope_implementer->CallDepthIsZero());
|
|
|
|
|
|
|
|
// Increase call depth to prevent recursive callbacks.
|
|
|
|
handle_scope_implementer->IncrementCallDepth();
|
|
|
|
Execution::RunMicrotasks(isolate);
|
|
|
|
handle_scope_implementer->DecrementCallDepth();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-12 13:56:56 +00:00
|
|
|
void V8::InitializeOncePerProcessImpl() {
|
2013-01-21 12:04:40 +00:00
|
|
|
FlagList::EnforceFlagImplications();
|
2013-12-18 17:31:12 +00:00
|
|
|
|
2014-03-04 12:42:43 +00:00
|
|
|
if (FLAG_predictable && FLAG_random_seed == 0) {
|
|
|
|
// Avoid random seeds in predictable mode.
|
|
|
|
FLAG_random_seed = 12347;
|
2013-12-18 17:31:12 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 15:52:15 +00:00
|
|
|
if (FLAG_stress_compaction) {
|
|
|
|
FLAG_force_marking_deque_overflows = true;
|
|
|
|
FLAG_gc_global = true;
|
|
|
|
FLAG_max_new_space_size = (1 << (kPageSizeBits - 10)) * 2;
|
|
|
|
}
|
2013-08-05 09:02:47 +00:00
|
|
|
|
2013-11-21 14:07:06 +00:00
|
|
|
#ifdef V8_USE_DEFAULT_PLATFORM
|
|
|
|
platform_ = new DefaultPlatform;
|
|
|
|
#endif
|
2013-04-15 13:57:41 +00:00
|
|
|
Sampler::SetUp();
|
2013-01-21 12:04:40 +00:00
|
|
|
CPU::SetUp();
|
|
|
|
OS::PostSetUp();
|
|
|
|
ElementsAccessor::InitializeOncePerProcess();
|
2012-03-12 13:56:56 +00:00
|
|
|
LOperand::SetUpCaches();
|
2012-04-05 14:10:39 +00:00
|
|
|
SetUpJSCallerSavedCodeData();
|
|
|
|
ExternalReference::SetUp();
|
2013-05-21 12:03:49 +00:00
|
|
|
Bootstrapper::InitializeOncePerProcess();
|
2012-03-12 13:56:56 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2012-03-12 13:56:56 +00:00
|
|
|
void V8::InitializeOncePerProcess() {
|
|
|
|
CallOnce(&init_once, &InitializeOncePerProcessImpl);
|
2011-03-31 16:17:37 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 14:07:06 +00:00
|
|
|
|
|
|
|
void V8::InitializePlatform(v8::Platform* platform) {
|
|
|
|
ASSERT(!platform_);
|
|
|
|
ASSERT(platform);
|
|
|
|
platform_ = platform;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void V8::ShutdownPlatform() {
|
|
|
|
ASSERT(platform_);
|
|
|
|
platform_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v8::Platform* V8::GetCurrentPlatform() {
|
|
|
|
ASSERT(platform_);
|
|
|
|
return platform_;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|