Commit 6730ec84 authored by marja@chromium.org's avatar marja@chromium.org

Amend PersistentValueMap:

- Use the surrounding map (instead of Traits::Impl) for weak callback.
- Provide for a fast reference to a mapped value.
- Restructure Traits to accomondate for the first point above.

[Why?] As discussed, I proceeded to replace Impl with the map.
The problem I encountered with that version is that now the
Traits class depends on itself: The weak-related methods require the
map type in their signature. But the map type includes the Traits class
and hence the Traits class method signatures depend on the specific Traits class. That
makes them practically un-derivable: While you can derive a Traits class
from another one, since the compiler now expects methods with a different
signature. To accommodate, I pulled the dispose traits into the weak traits
class. I also removed the Impl*/MapType* parameter from the Dispose call,
since no implementation seems to need it.

R=dcarney@chromium.org
BUG=

Review URL: https://codereview.chromium.org/212893007

Patch from Daniel Vogelheim <vogelheim@chromium.org>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20326 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent f7b437d0
This diff is collapsed.
...@@ -3498,22 +3498,23 @@ THREADED_TEST(UniquePersistent) { ...@@ -3498,22 +3498,23 @@ THREADED_TEST(UniquePersistent) {
template<typename K, typename V> template<typename K, typename V>
class WeakStdMapTraits : public v8::StdMapTraits<K, V> { class WeakStdMapTraits : public v8::StdMapTraits<K, V> {
public: public:
typedef typename v8::DefaultPersistentValueMapTraits<K, V>::Impl Impl; typedef typename v8::PersistentValueMap<K, V, WeakStdMapTraits<K, V> >
static const bool kIsWeak = true; MapType;
static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak;
struct WeakCallbackDataType { struct WeakCallbackDataType {
Impl* impl; MapType* map;
K key; K key;
}; };
static WeakCallbackDataType* WeakCallbackParameter( static WeakCallbackDataType* WeakCallbackParameter(
Impl* impl, const K& key, Local<V> value) { MapType* map, const K& key, Local<V> value) {
WeakCallbackDataType* data = new WeakCallbackDataType; WeakCallbackDataType* data = new WeakCallbackDataType;
data->impl = impl; data->map = map;
data->key = key; data->key = key;
return data; return data;
} }
static Impl* ImplFromWeakCallbackData( static MapType* MapFromWeakCallbackData(
const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { const v8::WeakCallbackData<V, WeakCallbackDataType>& data) {
return data.GetParameter()->impl; return data.GetParameter()->map;
} }
static K KeyFromWeakCallbackData( static K KeyFromWeakCallbackData(
const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { const v8::WeakCallbackData<V, WeakCallbackDataType>& data) {
...@@ -3523,7 +3524,7 @@ class WeakStdMapTraits : public v8::StdMapTraits<K, V> { ...@@ -3523,7 +3524,7 @@ class WeakStdMapTraits : public v8::StdMapTraits<K, V> {
delete data; delete data;
} }
static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value, static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value,
Impl* impl, K key) { } K key) { }
}; };
...@@ -3545,6 +3546,10 @@ static void TestPersistentValueMap() { ...@@ -3545,6 +3546,10 @@ static void TestPersistentValueMap() {
CHECK_EQ(1, static_cast<int>(map.Size())); CHECK_EQ(1, static_cast<int>(map.Size()));
obj = map.Get(7); obj = map.Get(7);
CHECK_EQ(expected, obj); CHECK_EQ(expected, obj);
{
typename Map::PersistentValueReference ref = map.GetReference(7);
CHECK_EQ(expected, ref.NewLocal(isolate));
}
v8::UniquePersistent<v8::Object> removed = map.Remove(7); v8::UniquePersistent<v8::Object> removed = map.Remove(7);
CHECK_EQ(0, static_cast<int>(map.Size())); CHECK_EQ(0, static_cast<int>(map.Size()));
CHECK(expected == removed); CHECK(expected == removed);
...@@ -3554,6 +3559,15 @@ static void TestPersistentValueMap() { ...@@ -3554,6 +3559,15 @@ static void TestPersistentValueMap() {
CHECK_EQ(1, static_cast<int>(map.Size())); CHECK_EQ(1, static_cast<int>(map.Size()));
map.Set(8, expected); map.Set(8, expected);
CHECK_EQ(1, static_cast<int>(map.Size())); CHECK_EQ(1, static_cast<int>(map.Size()));
{
typename Map::PersistentValueReference ref;
Local<v8::Object> expected2 = v8::Object::New(isolate);
removed = map.Set(8,
v8::UniquePersistent<v8::Object>(isolate, expected2), &ref);
CHECK_EQ(1, static_cast<int>(map.Size()));
CHECK(expected == removed);
CHECK_EQ(expected2, ref.NewLocal(isolate));
}
} }
CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count());
if (map.IsWeak()) { if (map.IsWeak()) {
...@@ -3572,7 +3586,7 @@ TEST(PersistentValueMap) { ...@@ -3572,7 +3586,7 @@ TEST(PersistentValueMap) {
TestPersistentValueMap<v8::StdPersistentValueMap<int, v8::Object> >(); TestPersistentValueMap<v8::StdPersistentValueMap<int, v8::Object> >();
// Custom traits with weak callbacks: // Custom traits with weak callbacks:
typedef v8::StdPersistentValueMap<int, v8::Object, typedef v8::PersistentValueMap<int, v8::Object,
WeakStdMapTraits<int, v8::Object> > WeakPersistentValueMap; WeakStdMapTraits<int, v8::Object> > WeakPersistentValueMap;
TestPersistentValueMap<WeakPersistentValueMap>(); TestPersistentValueMap<WeakPersistentValueMap>();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment