Author: [email protected]
Date: Mon May 25 12:39:52 2009
New Revision: 2048

Modified:
    branches/bleeding_edge/src/api.cc
    branches/bleeding_edge/src/bootstrapper.cc
    branches/bleeding_edge/src/v8.cc
    branches/bleeding_edge/src/v8.h
    branches/bleeding_edge/test/cctest/test-log.cc

Log:
Changed the flags that indicate the status of running vs dead
This allows us to optimized the EnsureInitialized() function
so it doesn't require a function call when we're running

Review URL: http://codereview.chromium.org/113121

Modified: branches/bleeding_edge/src/api.cc
==============================================================================
--- branches/bleeding_edge/src/api.cc   (original)
+++ branches/bleeding_edge/src/api.cc   Mon May 25 12:39:52 2009
@@ -99,7 +99,6 @@
  // --- E x c e p t i o n   B e h a v i o r ---


-static bool has_shut_down = false;
  static FatalErrorCallback exception_behavior = NULL;


@@ -123,7 +122,7 @@
  // When V8 cannot allocated memory FatalProcessOutOfMemory is called.
  // The default fatal error handler is called and execution is stopped.
  void i::V8::FatalProcessOutOfMemory(const char* location) {
-  has_shut_down = true;
+  i::V8::SetFatalError();
    FatalErrorCallback callback = GetFatalErrorHandler();
    {
      LEAVE_V8;
@@ -142,13 +141,13 @@
  bool Utils::ReportApiFailure(const char* location, const char* message) {
    FatalErrorCallback callback = GetFatalErrorHandler();
    callback(location, message);
-  has_shut_down = true;
+  i::V8::SetFatalError();
    return false;
  }


  bool V8::IsDead() {
-  return has_shut_down;
+  return i::V8::IsDead();
  }


@@ -186,7 +185,8 @@
   * yet been done.
   */
  static inline bool IsDeadCheck(const char* location) {
-  return has_shut_down ? ReportV8Dead(location) : false;
+  return !i::V8::IsRunning()
+      && i::V8::IsDead() ? ReportV8Dead(location) : false;
  }


@@ -205,9 +205,14 @@
  static i::StringInputBuffer write_input_buffer;


-static void EnsureInitialized(const char* location) {
-  if (IsDeadCheck(location)) return;
-  ApiCheck(v8::V8::Initialize(), location, "Error initializing V8");
+static inline bool EnsureInitialized(const char* location) {
+  if (i::V8::IsRunning()) {
+    return true;
+  }
+  if (IsDeadCheck(location)) {
+    return false;
+  }
+  return ApiCheck(v8::V8::Initialize(), location, "Error initializing V8");
  }


@@ -225,29 +230,25 @@


  v8::Handle<v8::Primitive> ImplementationUtilities::Undefined() {
-  if (IsDeadCheck("v8::Undefined()")) return v8::Handle<v8::Primitive>();
-  EnsureInitialized("v8::Undefined()");
+  if (!EnsureInitialized("v8::Undefined()")) return  
v8::Handle<v8::Primitive>();
    return  
v8::Handle<Primitive>(ToApi<Primitive>(i::Factory::undefined_value()));
  }


  v8::Handle<v8::Primitive> ImplementationUtilities::Null() {
-  if (IsDeadCheck("v8::Null()")) return v8::Handle<v8::Primitive>();
-  EnsureInitialized("v8::Null()");
+  if (!EnsureInitialized("v8::Null()")) return v8::Handle<v8::Primitive>();
    return v8::Handle<Primitive>(ToApi<Primitive>(i::Factory::null_value()));
  }


  v8::Handle<v8::Boolean> ImplementationUtilities::True() {
-  if (IsDeadCheck("v8::True()")) return v8::Handle<v8::Boolean>();
-  EnsureInitialized("v8::True()");
+  if (!EnsureInitialized("v8::True()")) return v8::Handle<v8::Boolean>();
    return v8::Handle<v8::Boolean>(ToApi<Boolean>(i::Factory::true_value()));
  }


  v8::Handle<v8::Boolean> ImplementationUtilities::False() {
-  if (IsDeadCheck("v8::False()")) return v8::Handle<v8::Boolean>();
-  EnsureInitialized("v8::False()");
+  if (!EnsureInitialized("v8::False()")) return v8::Handle<v8::Boolean>();
    return  
v8::Handle<v8::Boolean>(ToApi<Boolean>(i::Factory::false_value()));
  }

@@ -373,21 +374,21 @@

  bool V8::IsGlobalNearDeath(void** obj) {
    LOG_API("IsGlobalNearDeath");
-  if (has_shut_down) return false;
+  if (!i::V8::IsRunning()) return false;
    return i::GlobalHandles::IsNearDeath(reinterpret_cast<i::Object**>(obj));
  }


  bool V8::IsGlobalWeak(void** obj) {
    LOG_API("IsGlobalWeak");
-  if (has_shut_down) return false;
+  if (!i::V8::IsRunning()) return false;
    return i::GlobalHandles::IsWeak(reinterpret_cast<i::Object**>(obj));
  }


  void V8::DisposeGlobal(void** obj) {
    LOG_API("DisposeGlobal");
-  if (has_shut_down) return;
+  if (!i::V8::IsRunning()) return;
    i::Object** ptr = reinterpret_cast<i::Object**>(obj);
    if ((*ptr)->IsGlobalContext()) i::Heap::NotifyContextDisposed();
    i::GlobalHandles::Destroy(ptr);
@@ -431,7 +432,7 @@


  void Context::Exit() {
-  if (has_shut_down) return;
+  if (!i::V8::IsRunning()) return;
    if (!ApiCheck(thread_local.LeaveLastContext(),
                  "v8::Context::Exit()",
                  "Cannot exit non-entered context")) {
@@ -2450,7 +2451,7 @@
  // --- E n v i r o n m e n t ---

  bool v8::V8::Initialize() {
-  if (i::V8::HasBeenSetup()) return true;
+  if (i::V8::IsRunning()) return true;
    ENTER_V8;
    HandleScope scope;
    if (i::Snapshot::Initialize()) {
@@ -3331,7 +3332,7 @@


  void Debug::DebugBreak() {
-  if (!i::V8::HasBeenSetup()) return;
+  if (!i::V8::IsRunning()) return;
    i::StackGuard::DebugBreak();
  }

@@ -3373,7 +3374,7 @@

  void Debug::SendCommand(const uint16_t* command, int length,
                          ClientData* client_data) {
-  if (!i::V8::HasBeenSetup()) return;
+  if (!i::V8::IsRunning()) return;
    i::Debugger::ProcessCommand(i::Vector<const uint16_t>(command, length),
                                client_data);
  }
@@ -3389,7 +3390,7 @@

  Handle<Value> Debug::Call(v8::Handle<v8::Function> fun,
                            v8::Handle<v8::Value> data) {
-  if (!i::V8::HasBeenSetup()) return Handle<Value>();
+  if (!i::V8::IsRunning()) return Handle<Value>();
    ON_BAILOUT("v8::Debug::Call()", return Handle<Value>());
    ENTER_V8;
    i::Handle<i::Object> result;

Modified: branches/bleeding_edge/src/bootstrapper.cc
==============================================================================
--- branches/bleeding_edge/src/bootstrapper.cc  (original)
+++ branches/bleeding_edge/src/bootstrapper.cc  Mon May 25 12:39:52 2009
@@ -1530,8 +1530,8 @@
    current_  = this;
    result_ = NULL;

-  // If V8 hasn't been and cannot be initialized, just return.
-  if (!V8::HasBeenSetup() && !V8::Initialize(NULL)) return;
+  // If V8 isn't running and cannot be initialized, just return.
+  if (!V8::IsRunning() && !V8::Initialize(NULL)) return;

    // Before creating the roots we must save the context and restore it
    // on all function exits.

Modified: branches/bleeding_edge/src/v8.cc
==============================================================================
--- branches/bleeding_edge/src/v8.cc    (original)
+++ branches/bleeding_edge/src/v8.cc    Mon May 25 12:39:52 2009
@@ -36,14 +36,20 @@
  namespace v8 {
  namespace internal {

+bool V8::is_running_ = false;
  bool V8::has_been_setup_ = false;
  bool V8::has_been_disposed_ = false;
+bool V8::has_fatal_error_ = false;

  bool V8::Initialize(Deserializer *des) {
    bool create_heap_objects = des == NULL;
-  if (HasBeenDisposed()) return false;
-  if (HasBeenSetup()) return true;
+  if (has_been_disposed_ || has_fatal_error_) return false;
+  if (IsRunning()) return true;
+
+  is_running_ = true;
    has_been_setup_ = true;
+  has_fatal_error_ = false;
+  has_been_disposed_ = false;
  #ifdef DEBUG
    // The initialization process does not handle memory exhaustion.
    DisallowAllocationFailure disallow_allocation_failure;
@@ -59,7 +65,7 @@
    // Setup the object heap
    ASSERT(!Heap::HasBeenSetup());
    if (!Heap::Setup(create_heap_objects)) {
-    has_been_setup_ = false;
+    SetFatalError();
      return false;
    }

@@ -95,9 +101,14 @@
  }


+void V8::SetFatalError() {
+  is_running_ = false;
+  has_fatal_error_ = true;
+}
+
+
  void V8::TearDown() {
-  if (HasBeenDisposed()) return;
-  if (!HasBeenSetup()) return;
+  if (!has_been_setup_ || has_been_disposed_) return;

    OProfileAgent::TearDown();

@@ -114,8 +125,9 @@
    Heap::TearDown();
    Logger::TearDown();

-  has_been_setup_ = false;
+  is_running_ = false;
    has_been_disposed_ = true;
  }
+

  } }  // namespace v8::internal

Modified: branches/bleeding_edge/src/v8.h
==============================================================================
--- branches/bleeding_edge/src/v8.h     (original)
+++ branches/bleeding_edge/src/v8.h     Mon May 25 12:39:52 2009
@@ -86,13 +86,23 @@
    // deserialized data into an empty heap.
    static bool Initialize(Deserializer* des);
    static void TearDown();
-  static bool HasBeenSetup() { return has_been_setup_; }
-  static bool HasBeenDisposed() { return has_been_disposed_; }
+  static bool IsRunning() { return is_running_; }
+  // To be dead you have to have lived
+  static bool IsDead() { return has_fatal_error_ || has_been_disposed_; }
+  static void SetFatalError();

    // Report process out of memory. Implementation found in api.cc.
    static void FatalProcessOutOfMemory(const char* location);
   private:
+  // True if engine is currently running
+  static bool is_running_;
+  // True if V8 has ever been run
    static bool has_been_setup_;
+  // True if error has been signaled for current engine
+  // (reset to false if engine is restarted)
+  static bool has_fatal_error_;
+  // True if engine has been shut down
+  // (reset if engine is restarted)
    static bool has_been_disposed_;
  };


Modified: branches/bleeding_edge/test/cctest/test-log.cc
==============================================================================
--- branches/bleeding_edge/test/cctest/test-log.cc      (original)
+++ branches/bleeding_edge/test/cctest/test-log.cc      Mon May 25 12:39:52 2009
@@ -201,7 +201,7 @@

    // If tests are being run manually, V8 will be already initialized
    // by the test below.
-  const bool need_to_set_up_logger = i::V8::HasBeenSetup();
+  const bool need_to_set_up_logger = i::V8::IsRunning();
    v8::HandleScope scope;
    v8::Handle<v8::Context> env = v8::Context::New();
    if (need_to_set_up_logger) Logger::Setup();
@@ -620,7 +620,7 @@
    // are using V8.
    //
    // P.S. No, V8 can't be re-initialized after disposal, see include/v8.h.
-  CHECK(!i::V8::HasBeenSetup());
+  CHECK(!i::V8::IsRunning());

    i::FLAG_logfile = "*";
    i::FLAG_log = true;

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to