Reviewers: dcarney,

Message:
dcarney, ptal

Description:
Add Persistent<T>::Reset which disposes the handle and redirects it to point to
another object.

BUG=

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

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 b113d74792108c62ff13627b8117598c17b1b297..59b70f2ac0d9d9d2f9de741d4d433d79d12594c5 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -676,6 +676,11 @@ template <class T> class Persistent // NOLINT
   // TODO(dcarney): remove before cutover
   V8_INLINE(uint16_t WrapperClassId(Isolate* isolate) const);

+  /**
+   * Disposes the current contents of the handle and replaces it.
+   */
+  V8_INLINE(void Reset(Isolate* isolate, const Handle<T>& other));
+
 #ifndef V8_USE_UNSAFE_HANDLES

 #ifndef V8_ALLOW_ACCESS_TO_PERSISTENT_IMPLICIT
@@ -5349,6 +5354,20 @@ void Persistent<T>::SetWrapperClassId(uint16_t class_id) {
 }

 template <class T>
+void Persistent<T>::Reset(Isolate* isolate, const Handle<T>& other) {
+  Dispose(isolate);
+#ifdef V8_USE_UNSAFE_HANDLES
+  *this = *New(isolate, other);
+#else
+  if (other.IsEmpty())
+    return;
+  internal::Object** p = reinterpret_cast<internal::Object**>(other.val_);
+  val_ = reinterpret_cast<T*>(
+ V8::GlobalizeReference(reinterpret_cast<internal::Isolate*>(isolate), p));
+#endif
+}
+
+template <class T>
void Persistent<T>::SetWrapperClassId(Isolate* isolate, uint16_t class_id) {
   typedef internal::Internals I;
   if (this->IsEmpty()) return;
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 11d9fcaba8505908ba6c96c600f929ad540b656a..2e63124ba4f82aad67236637808cf402d2585038 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -2486,6 +2486,35 @@ THREADED_TEST(GlobalHandle) {
 }


+THREADED_TEST(ResettingGlobalHandle) {
+  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->Length(), 3);
+ CHECK_EQ(global_handles->NumberOfGlobalHandles(), initial_handle_count + 1);
+  {
+    v8::HandleScope scope(isolate);
+    Local<String> str = v8_str("longer");
+    global.Reset(isolate, str);
+  }
+  CHECK_EQ(global->Length(), 6);
+ CHECK_EQ(global_handles->NumberOfGlobalHandles(), initial_handle_count + 1);
+  global.Dispose(isolate);
+  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