Reviewers: Sven Panne,

Message:
svenpanne, ptal

Description:
Add Persistent::LeakPersistent.

This will be relevant after Persistent is changed to Dispose itself when
destructed. With Persistent::LeakPersistnet, Blink can take the ownership of the
object pointed by a Persistent and avoid it getting destructed.

BUG=

Please review this at https://codereview.chromium.org/15023010/

SVN Base: git://github.com/v8/v8.git@master

Affected files:
  M include/v8.h
  M test/cctest/test-api.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index 3a86e86e020cbc9448c50f2ccd21476035a66c2e..ba70a33d9739161e09f973afc1be657bce60f7c8 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -695,6 +695,8 @@ template <class T> class Persistent // NOLINT
    */
   V8_INLINE(void Reset(Isolate* isolate, const Handle<T>& other));

+  V8_INLINE(T* LeakPersistent());
+
 #ifndef V8_USE_UNSAFE_HANDLES

 #ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
@@ -5384,6 +5386,7 @@ void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
   SetWrapperClassId(Isolate::GetCurrent(), class_id);
 }

+
 template <class T>
 void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
   Dispose(isolate);
@@ -5400,6 +5403,21 @@ void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
 #endif
 }

+
+template <class T>
+T* Persistent<T>::LeakPersistent() {
+  T* old;
+#ifdef V8_USE_UNSAFE_HANDLES
+  old = this->operator*();
+  *this = Persistent<T>();
+#else
+  old = val_;
+  val_ = NULL;
+#endif
+  return old;
+}
+
+
 template <class T>
void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
   typedef internal::Internals I;
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index c9685f8f4c07fa07914b68b4f15196543f29e815..e25430fe36943230b36096e5bff7ca2cd7f95189 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -2539,6 +2539,30 @@ THREADED_TEST(ResettingGlobalHandleToEmpty) {
 }


+THREADED_TEST(LeakPersistent) {
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  v8::internal::GlobalHandles* global_handles = NULL;
+  int initial_handle_count = 0;
+  v8::Persistent<String> global;
+  {
+    v8::HandleScope scope(isolate);
+    Local<String> str = v8_str("str");
+    global_handles =
+ reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles();
+    initial_handle_count = global_handles->NumberOfGlobalHandles();
+    global = v8::Persistent<String>::New(isolate, str);
+  }
+ CHECK_EQ(global_handles->NumberOfGlobalHandles(), initial_handle_count + 1);
+  String* str = global.LeakPersistent();
+  CHECK(global.IsEmpty());
+ CHECK_EQ(global_handles->NumberOfGlobalHandles(), initial_handle_count + 1);
+  v8::Persistent<String>* new_global =
+      reinterpret_cast<v8::Persistent<String>*>(&str);
+  new_global->Dispose();
+  CHECK_EQ(global_handles->NumberOfGlobalHandles(), initial_handle_count);
+}
+
+
 THREADED_TEST(LocalHandle) {
   v8::HandleScope scope(v8::Isolate::GetCurrent());
   v8::Local<String> local = v8::Local<String>::New(v8_str("str"));


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to