Upgrade V8 to version 4.9.385.28

https://chromium.googlesource.com/v8/v8/+/4.9.385.28

FPIIM-449

Change-Id: I4b2e74289d4bf3667f2f3dc8aa2e541f63e26eb4
diff --git a/include/v8-util.h b/include/v8-util.h
index 1eaf1ab..73ec658 100644
--- a/include/v8-util.h
+++ b/include/v8-util.h
@@ -5,14 +5,14 @@
 #ifndef V8_UTIL_H_
 #define V8_UTIL_H_
 
-#include "v8.h"
+#include "v8.h"  // NOLINT(build/include)
 #include <map>
 #include <vector>
 
 /**
  * Support for Persistent containers.
  *
- * C++11 embedders can use STL containers with UniquePersistent values,
+ * C++11 embedders can use STL containers with Global values,
  * but pre-C++11 does not support the required move semantic and hence
  * may want these container classes.
  */
@@ -22,7 +22,10 @@
 static const uintptr_t kPersistentContainerNotFound = 0;
 enum PersistentContainerCallbackType {
   kNotWeak,
-  kWeak
+  // These correspond to v8::WeakCallbackType
+  kWeakWithParameter,
+  kWeakWithInternalFields,
+  kWeak = kWeakWithParameter  // For backwards compatibility.  Deprecate.
 };
 
 
@@ -101,13 +104,52 @@
     return K();
   }
   static void DisposeCallbackData(WeakCallbackDataType* data) { }
-  static void Dispose(Isolate* isolate, UniquePersistent<V> value, K key) { }
+  static void Dispose(Isolate* isolate, Global<V> value, K key) {}
+};
+
+
+template <typename K, typename V>
+class DefaultGlobalMapTraits : public StdMapTraits<K, V> {
+ private:
+  template <typename T>
+  struct RemovePointer;
+
+ public:
+  // Weak callback & friends:
+  static const PersistentContainerCallbackType kCallbackType = kNotWeak;
+  typedef GlobalValueMap<K, V, DefaultGlobalMapTraits<K, V> > MapType;
+  typedef void WeakCallbackDataType;
+
+  static WeakCallbackDataType* WeakCallbackParameter(MapType* map, const K& key,
+                                                     Local<V> value) {
+    return nullptr;
+  }
+  static MapType* MapFromWeakCallbackInfo(
+      const WeakCallbackInfo<WeakCallbackDataType>& data) {
+    return nullptr;
+  }
+  static K KeyFromWeakCallbackInfo(
+      const WeakCallbackInfo<WeakCallbackDataType>& data) {
+    return K();
+  }
+  static void DisposeCallbackData(WeakCallbackDataType* data) {}
+  static void OnWeakCallback(
+      const WeakCallbackInfo<WeakCallbackDataType>& data) {}
+  static void Dispose(Isolate* isolate, Global<V> value, K key) {}
+  // This is a second pass callback, so SetSecondPassCallback cannot be called.
+  static void DisposeWeak(const WeakCallbackInfo<WeakCallbackDataType>& data) {}
+
+ private:
+  template <typename T>
+  struct RemovePointer<T*> {
+    typedef T Type;
+  };
 };
 
 
 /**
- * A map wrapper that allows using UniquePersistent as a mapped value.
- * C++11 embedders don't need this class, as they can use UniquePersistent
+ * A map wrapper that allows using Global as a mapped value.
+ * C++11 embedders don't need this class, as they can use Global
  * directly in std containers.
  *
  * The map relies on a backing map, whose type and accessors are described
@@ -115,13 +157,9 @@
  * PersistentContainerValue, with all conversion into and out of V8
  * handles being transparently handled by this class.
  */
-template<typename K, typename V, typename Traits>
-class PersistentValueMap {
+template <typename K, typename V, typename Traits>
+class PersistentValueMapBase {
  public:
-  explicit PersistentValueMap(Isolate* isolate) : isolate_(isolate) {}
-
-  ~PersistentValueMap() { Clear(); }
-
   Isolate* GetIsolate() { return isolate_; }
 
   /**
@@ -168,26 +206,9 @@
   }
 
   /**
-   * Put value into map. Depending on Traits::kIsWeak, the value will be held
-   * by the map strongly or weakly.
-   * Returns old value as UniquePersistent.
-   */
-  UniquePersistent<V> Set(const K& key, Local<V> value) {
-    UniquePersistent<V> persistent(isolate_, value);
-    return SetUnique(key, &persistent);
-  }
-
-  /**
-   * Put value into map, like Set(const K&, Local<V>).
-   */
-  UniquePersistent<V> Set(const K& key, UniquePersistent<V> value) {
-    return SetUnique(key, &value);
-  }
-
-  /**
    * Return value for key and remove it from the map.
    */
-  UniquePersistent<V> Remove(const K& key) {
+  Global<V> Remove(const K& key) {
     return Release(Traits::Remove(&impl_, key)).Pass();
   }
 
@@ -237,7 +258,9 @@
     }
 
    private:
-    friend class PersistentValueMap;
+    friend class PersistentValueMapBase;
+    friend class PersistentValueMap<K, V, Traits>;
+    friend class GlobalValueMap<K, V, Traits>;
 
     explicit PersistentValueReference(PersistentContainerValue value)
         : value_(value) { }
@@ -263,35 +286,121 @@
     return PersistentValueReference(Traits::Get(&impl_, key));
   }
 
+ protected:
+  explicit PersistentValueMapBase(Isolate* isolate) : isolate_(isolate) {}
+
+  ~PersistentValueMapBase() { Clear(); }
+
+  Isolate* isolate() { return isolate_; }
+  typename Traits::Impl* impl() { return &impl_; }
+
+  static V* FromVal(PersistentContainerValue v) {
+    return reinterpret_cast<V*>(v);
+  }
+
+  static PersistentContainerValue ClearAndLeak(Global<V>* persistent) {
+    V* v = persistent->val_;
+    persistent->val_ = 0;
+    return reinterpret_cast<PersistentContainerValue>(v);
+  }
+
+  static PersistentContainerValue Leak(Global<V>* persistent) {
+    return reinterpret_cast<PersistentContainerValue>(persistent->val_);
+  }
+
   /**
-   * Put a value into the map and update the reference.
-   * Restrictions of GetReference apply here as well.
+   * Return a container value as Global and make sure the weak
+   * callback is properly disposed of. All remove functionality should go
+   * through this.
    */
-  UniquePersistent<V> Set(const K& key, UniquePersistent<V> value,
-                          PersistentValueReference* reference) {
-    *reference = Leak(&value);
-    return SetUnique(key, &value);
+  static Global<V> Release(PersistentContainerValue v) {
+    Global<V> p;
+    p.val_ = FromVal(v);
+    if (Traits::kCallbackType != kNotWeak && p.IsWeak()) {
+      Traits::DisposeCallbackData(
+          p.template ClearWeak<typename Traits::WeakCallbackDataType>());
+    }
+    return p.Pass();
+  }
+
+  void RemoveWeak(const K& key) {
+    Global<V> p;
+    p.val_ = FromVal(Traits::Remove(&impl_, key));
+    p.Reset();
   }
 
  private:
-  PersistentValueMap(PersistentValueMap&);
-  void operator=(PersistentValueMap&);
+  PersistentValueMapBase(PersistentValueMapBase&);
+  void operator=(PersistentValueMapBase&);
+
+  static bool SetReturnValueFromVal(ReturnValue<Value>* returnValue,
+                                    PersistentContainerValue value) {
+    bool hasValue = value != kPersistentContainerNotFound;
+    if (hasValue) {
+      returnValue->SetInternal(
+          *reinterpret_cast<internal::Object**>(FromVal(value)));
+    }
+    return hasValue;
+  }
+
+  Isolate* isolate_;
+  typename Traits::Impl impl_;
+};
+
+
+template <typename K, typename V, typename Traits>
+class PersistentValueMap : public PersistentValueMapBase<K, V, Traits> {
+ public:
+  explicit PersistentValueMap(Isolate* isolate)
+      : PersistentValueMapBase<K, V, Traits>(isolate) {}
+
+  typedef
+      typename PersistentValueMapBase<K, V, Traits>::PersistentValueReference
+          PersistentValueReference;
+
+  /**
+   * Put value into map. Depending on Traits::kIsWeak, the value will be held
+   * by the map strongly or weakly.
+   * Returns old value as Global.
+   */
+  Global<V> Set(const K& key, Local<V> value) {
+    Global<V> persistent(this->isolate(), value);
+    return SetUnique(key, &persistent);
+  }
+
+  /**
+   * Put value into map, like Set(const K&, Local<V>).
+   */
+  Global<V> Set(const K& key, Global<V> value) {
+    return SetUnique(key, &value);
+  }
 
   /**
    * Put the value into the map, and set the 'weak' callback when demanded
    * by the Traits class.
    */
-  UniquePersistent<V> SetUnique(const K& key, UniquePersistent<V>* persistent) {
+  Global<V> SetUnique(const K& key, Global<V>* persistent) {
     if (Traits::kCallbackType != kNotWeak) {
-      Local<V> value(Local<V>::New(isolate_, *persistent));
+      Local<V> value(Local<V>::New(this->isolate(), *persistent));
       persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
         Traits::WeakCallbackParameter(this, key, value), WeakCallback);
     }
     PersistentContainerValue old_value =
-        Traits::Set(&impl_, key, ClearAndLeak(persistent));
-    return Release(old_value).Pass();
+        Traits::Set(this->impl(), key, this->ClearAndLeak(persistent));
+    return this->Release(old_value).Pass();
   }
 
+  /**
+   * Put a value into the map and update the reference.
+   * Restrictions of GetReference apply here as well.
+   */
+  Global<V> Set(const K& key, Global<V> value,
+                PersistentValueReference* reference) {
+    *reference = this->Leak(&value);
+    return SetUnique(key, &value);
+  }
+
+ private:
   static void WeakCallback(
       const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& data) {
     if (Traits::kCallbackType != kNotWeak) {
@@ -303,59 +412,91 @@
       Traits::DisposeCallbackData(data.GetParameter());
     }
   }
+};
 
-  static V* FromVal(PersistentContainerValue v) {
-    return reinterpret_cast<V*>(v);
-  }
 
-  static bool SetReturnValueFromVal(
-      ReturnValue<Value>* returnValue, PersistentContainerValue value) {
-    bool hasValue = value != kPersistentContainerNotFound;
-    if (hasValue) {
-      returnValue->SetInternal(
-          *reinterpret_cast<internal::Object**>(FromVal(value)));
-    }
-    return hasValue;
-  }
+template <typename K, typename V, typename Traits>
+class GlobalValueMap : public PersistentValueMapBase<K, V, Traits> {
+ public:
+  explicit GlobalValueMap(Isolate* isolate)
+      : PersistentValueMapBase<K, V, Traits>(isolate) {}
 
-  static PersistentContainerValue ClearAndLeak(
-      UniquePersistent<V>* persistent) {
-    V* v = persistent->val_;
-    persistent->val_ = 0;
-    return reinterpret_cast<PersistentContainerValue>(v);
-  }
+  typedef
+      typename PersistentValueMapBase<K, V, Traits>::PersistentValueReference
+          PersistentValueReference;
 
-  static PersistentContainerValue Leak(
-      UniquePersistent<V>* persistent) {
-    return reinterpret_cast<PersistentContainerValue>(persistent->val_);
+  /**
+   * Put value into map. Depending on Traits::kIsWeak, the value will be held
+   * by the map strongly or weakly.
+   * Returns old value as Global.
+   */
+  Global<V> Set(const K& key, Local<V> value) {
+    Global<V> persistent(this->isolate(), value);
+    return SetUnique(key, &persistent);
   }
 
   /**
-   * Return a container value as UniquePersistent and make sure the weak
-   * callback is properly disposed of. All remove functionality should go
-   * through this.
+   * Put value into map, like Set(const K&, Local<V>).
    */
-  static UniquePersistent<V> Release(PersistentContainerValue v) {
-    UniquePersistent<V> p;
-    p.val_ = FromVal(v);
-    if (Traits::kCallbackType != kNotWeak && p.IsWeak()) {
-      Traits::DisposeCallbackData(
-          p.template ClearWeak<typename Traits::WeakCallbackDataType>());
-    }
-    return p.Pass();
+  Global<V> Set(const K& key, Global<V> value) {
+    return SetUnique(key, &value);
   }
 
-  Isolate* isolate_;
-  typename Traits::Impl impl_;
+  /**
+   * Put the value into the map, and set the 'weak' callback when demanded
+   * by the Traits class.
+   */
+  Global<V> SetUnique(const K& key, Global<V>* persistent) {
+    if (Traits::kCallbackType != kNotWeak) {
+      WeakCallbackType callback_type =
+          Traits::kCallbackType == kWeakWithInternalFields
+              ? WeakCallbackType::kInternalFields
+              : WeakCallbackType::kParameter;
+      Local<V> value(Local<V>::New(this->isolate(), *persistent));
+      persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
+          Traits::WeakCallbackParameter(this, key, value), OnWeakCallback,
+          callback_type);
+    }
+    PersistentContainerValue old_value =
+        Traits::Set(this->impl(), key, this->ClearAndLeak(persistent));
+    return this->Release(old_value).Pass();
+  }
+
+  /**
+   * Put a value into the map and update the reference.
+   * Restrictions of GetReference apply here as well.
+   */
+  Global<V> Set(const K& key, Global<V> value,
+                PersistentValueReference* reference) {
+    *reference = this->Leak(&value);
+    return SetUnique(key, &value);
+  }
+
+ private:
+  static void OnWeakCallback(
+      const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
+    if (Traits::kCallbackType != kNotWeak) {
+      auto map = Traits::MapFromWeakCallbackInfo(data);
+      K key = Traits::KeyFromWeakCallbackInfo(data);
+      map->RemoveWeak(key);
+      Traits::OnWeakCallback(data);
+      data.SetSecondPassCallback(SecondWeakCallback);
+    }
+  }
+
+  static void SecondWeakCallback(
+      const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
+    Traits::DisposeWeak(data);
+  }
 };
 
 
 /**
- * A map that uses UniquePersistent as value and std::map as the backing
+ * A map that uses Global as value and std::map as the backing
  * implementation. Persistents are held non-weak.
  *
  * C++11 embedders don't need this class, as they can use
- * UniquePersistent directly in std containers.
+ * Global directly in std containers.
  */
 template<typename K, typename V,
     typename Traits = DefaultPersistentValueMapTraits<K, V> >
@@ -366,6 +507,22 @@
 };
 
 
+/**
+ * A map that uses Global as value and std::map as the backing
+ * implementation. Globals are held non-weak.
+ *
+ * C++11 embedders don't need this class, as they can use
+ * Global directly in std containers.
+ */
+template <typename K, typename V,
+          typename Traits = DefaultGlobalMapTraits<K, V> >
+class StdGlobalValueMap : public GlobalValueMap<K, V, Traits> {
+ public:
+  explicit StdGlobalValueMap(Isolate* isolate)
+      : GlobalValueMap<K, V, Traits>(isolate) {}
+};
+
+
 class DefaultPersistentValueVectorTraits {
  public:
   typedef std::vector<PersistentContainerValue> Impl;
@@ -392,8 +549,8 @@
 
 
 /**
- * A vector wrapper that safely stores UniquePersistent values.
- * C++11 embedders don't need this class, as they can use UniquePersistent
+ * A vector wrapper that safely stores Global values.
+ * C++11 embedders don't need this class, as they can use Global
  * directly in std containers.
  *
  * This class relies on a backing vector implementation, whose type and methods
@@ -414,14 +571,14 @@
    * Append a value to the vector.
    */
   void Append(Local<V> value) {
-    UniquePersistent<V> persistent(isolate_, value);
+    Global<V> persistent(isolate_, value);
     Traits::Append(&impl_, ClearAndLeak(&persistent));
   }
 
   /**
    * Append a persistent's value to the vector.
    */
-  void Append(UniquePersistent<V> persistent) {
+  void Append(Global<V> persistent) {
     Traits::Append(&impl_, ClearAndLeak(&persistent));
   }
 
@@ -452,7 +609,7 @@
   void Clear() {
     size_t length = Traits::Size(&impl_);
     for (size_t i = 0; i < length; i++) {
-      UniquePersistent<V> p;
+      Global<V> p;
       p.val_ = FromVal(Traits::Get(&impl_, i));
     }
     Traits::Clear(&impl_);
@@ -467,8 +624,7 @@
   }
 
  private:
-  static PersistentContainerValue ClearAndLeak(
-      UniquePersistent<V>* persistent) {
+  static PersistentContainerValue ClearAndLeak(Global<V>* persistent) {
     V* v = persistent->val_;
     persistent->val_ = 0;
     return reinterpret_cast<PersistentContainerValue>(v);
@@ -484,4 +640,4 @@
 
 }  // namespace v8
 
-#endif  // V8_UTIL_H_
+#endif  // V8_UTIL_H