Commit ccc8e4e4 authored by dcarney's avatar dcarney Committed by Commit bot

prepare to deprecate non phantom weak callbacks

BUG=

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

Cr-Commit-Position: refs/heads/master@{#28046}
parent ae7ce701
...@@ -1130,13 +1130,14 @@ static char* ReadChars(Isolate* isolate, const char* name, int* size_out) { ...@@ -1130,13 +1130,14 @@ static char* ReadChars(Isolate* isolate, const char* name, int* size_out) {
struct DataAndPersistent { struct DataAndPersistent {
uint8_t* data; uint8_t* data;
Persistent<ArrayBuffer> handle; int byte_length;
Global<ArrayBuffer> handle;
}; };
static void ReadBufferWeakCallback( static void ReadBufferWeakCallback(
const v8::WeakCallbackData<ArrayBuffer, DataAndPersistent>& data) { const v8::WeakCallbackInfo<DataAndPersistent>& data) {
size_t byte_length = data.GetValue()->ByteLength(); int byte_length = data.GetParameter()->byte_length;
data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory( data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory(
-static_cast<intptr_t>(byte_length)); -static_cast<intptr_t>(byte_length));
...@@ -1164,10 +1165,12 @@ void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -1164,10 +1165,12 @@ void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
Throw(args.GetIsolate(), "Error reading file"); Throw(args.GetIsolate(), "Error reading file");
return; return;
} }
data->byte_length = length;
Handle<v8::ArrayBuffer> buffer = Handle<v8::ArrayBuffer> buffer =
ArrayBuffer::New(isolate, data->data, length); ArrayBuffer::New(isolate, data->data, length);
data->handle.Reset(isolate, buffer); data->handle.Reset(isolate, buffer);
data->handle.SetWeak(data, ReadBufferWeakCallback); data->handle.SetWeak(data, ReadBufferWeakCallback,
v8::WeakCallbackType::kParameter);
data->handle.MarkIndependent(); data->handle.MarkIndependent();
isolate->AdjustAmountOfExternalAllocatedMemory(length); isolate->AdjustAmountOfExternalAllocatedMemory(length);
......
...@@ -3214,100 +3214,6 @@ TEST(TwoPassPhantomCallbacksNestedGc) { ...@@ -3214,100 +3214,6 @@ TEST(TwoPassPhantomCallbacksNestedGc) {
} }
template <typename K, typename V>
class WeakStdMapTraits : public v8::StdMapTraits<K, V> {
public:
typedef typename v8::PersistentValueMap<K, V, WeakStdMapTraits<K, V>> MapType;
static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak;
struct WeakCallbackDataType {
MapType* map;
K key;
};
static WeakCallbackDataType* WeakCallbackParameter(MapType* map, const K& key,
Local<V> value) {
WeakCallbackDataType* data = new WeakCallbackDataType;
data->map = map;
data->key = key;
return data;
}
static MapType* MapFromWeakCallbackData(
const v8::WeakCallbackData<V, WeakCallbackDataType>& data) {
return data.GetParameter()->map;
}
static K KeyFromWeakCallbackData(
const v8::WeakCallbackData<V, WeakCallbackDataType>& data) {
return data.GetParameter()->key;
}
static void DisposeCallbackData(WeakCallbackDataType* data) { delete data; }
static void Dispose(v8::Isolate* isolate, v8::Global<V> value, K key) {}
};
template <typename Map>
static void TestPersistentValueMap() {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
Map map(isolate);
v8::internal::GlobalHandles* global_handles =
reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles();
int initial_handle_count = global_handles->global_handles_count();
CHECK_EQ(0, static_cast<int>(map.Size()));
{
HandleScope scope(isolate);
Local<v8::Object> obj = map.Get(7);
CHECK(obj.IsEmpty());
Local<v8::Object> expected = v8::Object::New(isolate);
map.Set(7, expected);
CHECK_EQ(1, static_cast<int>(map.Size()));
obj = map.Get(7);
CHECK(expected->Equals(obj));
{
typename Map::PersistentValueReference ref = map.GetReference(7);
CHECK(expected->Equals(ref.NewLocal(isolate)));
}
v8::Global<v8::Object> removed = map.Remove(7);
CHECK_EQ(0, static_cast<int>(map.Size()));
CHECK(expected == removed);
removed = map.Remove(7);
CHECK(removed.IsEmpty());
map.Set(8, expected);
CHECK_EQ(1, static_cast<int>(map.Size()));
map.Set(8, expected);
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::Global<v8::Object>(isolate, expected2), &ref);
CHECK_EQ(1, static_cast<int>(map.Size()));
CHECK(expected == removed);
CHECK(expected2->Equals(ref.NewLocal(isolate)));
}
}
CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count());
if (map.IsWeak()) {
reinterpret_cast<v8::internal::Isolate*>(isolate)
->heap()
->CollectAllGarbage();
} else {
map.Clear();
}
CHECK_EQ(0, static_cast<int>(map.Size()));
CHECK_EQ(initial_handle_count, global_handles->global_handles_count());
}
TEST(PersistentValueMap) {
// Default case, w/o weak callbacks:
TestPersistentValueMap<v8::StdPersistentValueMap<int, v8::Object>>();
// Custom traits with weak callbacks:
typedef v8::PersistentValueMap<int, v8::Object,
WeakStdMapTraits<int, v8::Object>>
WeakPersistentValueMap;
TestPersistentValueMap<WeakPersistentValueMap>();
}
namespace { namespace {
void* IntKeyToVoidPointer(int key) { return reinterpret_cast<void*>(key << 1); } void* IntKeyToVoidPointer(int key) { return reinterpret_cast<void*>(key << 1); }
...@@ -3568,7 +3474,7 @@ struct WeakCallCounterAndPersistent { ...@@ -3568,7 +3474,7 @@ struct WeakCallCounterAndPersistent {
template <typename T> template <typename T>
static void WeakPointerCallback( static void WeakPointerCallback(
const v8::WeakCallbackData<T, WeakCallCounterAndPersistent<T>>& data) { const v8::WeakCallbackInfo<WeakCallCounterAndPersistent<T>>& data) {
CHECK_EQ(1234, data.GetParameter()->counter->id()); CHECK_EQ(1234, data.GetParameter()->counter->id());
data.GetParameter()->counter->increment(); data.GetParameter()->counter->increment();
data.GetParameter()->handle.Reset(); data.GetParameter()->handle.Reset();
...@@ -3600,16 +3506,22 @@ THREADED_TEST(ApiObjectGroups) { ...@@ -3600,16 +3506,22 @@ THREADED_TEST(ApiObjectGroups) {
g1s1.handle.Reset(iso, Object::New(iso)); g1s1.handle.Reset(iso, Object::New(iso));
g1s2.handle.Reset(iso, Object::New(iso)); g1s2.handle.Reset(iso, Object::New(iso));
g1c1.handle.Reset(iso, Object::New(iso)); g1c1.handle.Reset(iso, Object::New(iso));
g1s1.handle.SetWeak(&g1s1, &WeakPointerCallback); g1s1.handle.SetWeak(&g1s1, &WeakPointerCallback,
g1s2.handle.SetWeak(&g1s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g1c1.handle.SetWeak(&g1c1, &WeakPointerCallback); g1s2.handle.SetWeak(&g1s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
g1c1.handle.SetWeak(&g1c1, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
g2s1.handle.Reset(iso, Object::New(iso)); g2s1.handle.Reset(iso, Object::New(iso));
g2s2.handle.Reset(iso, Object::New(iso)); g2s2.handle.Reset(iso, Object::New(iso));
g2c1.handle.Reset(iso, Object::New(iso)); g2c1.handle.Reset(iso, Object::New(iso));
g2s1.handle.SetWeak(&g2s1, &WeakPointerCallback); g2s1.handle.SetWeak(&g2s1, &WeakPointerCallback,
g2s2.handle.SetWeak(&g2s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g2c1.handle.SetWeak(&g2c1, &WeakPointerCallback); g2s2.handle.SetWeak(&g2s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
g2c1.handle.SetWeak(&g2c1, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
} }
WeakCallCounterAndPersistent<Value> root(&counter); WeakCallCounterAndPersistent<Value> root(&counter);
...@@ -3643,7 +3555,8 @@ THREADED_TEST(ApiObjectGroups) { ...@@ -3643,7 +3555,8 @@ THREADED_TEST(ApiObjectGroups) {
CHECK_EQ(0, counter.NumberOfWeakCalls()); CHECK_EQ(0, counter.NumberOfWeakCalls());
// Weaken the root. // Weaken the root.
root.handle.SetWeak(&root, &WeakPointerCallback); root.handle.SetWeak(&root, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
// But make children strong roots---all the objects (except for children) // But make children strong roots---all the objects (except for children)
// should be collectable now. // should be collectable now.
g1c1.handle.ClearWeak(); g1c1.handle.ClearWeak();
...@@ -3667,8 +3580,10 @@ THREADED_TEST(ApiObjectGroups) { ...@@ -3667,8 +3580,10 @@ THREADED_TEST(ApiObjectGroups) {
CHECK_EQ(5, counter.NumberOfWeakCalls()); CHECK_EQ(5, counter.NumberOfWeakCalls());
// And now make children weak again and collect them. // And now make children weak again and collect them.
g1c1.handle.SetWeak(&g1c1, &WeakPointerCallback); g1c1.handle.SetWeak(&g1c1, &WeakPointerCallback,
g2c1.handle.SetWeak(&g2c1, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g2c1.handle.SetWeak(&g2c1, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
heap->CollectAllGarbage(); heap->CollectAllGarbage();
CHECK_EQ(7, counter.NumberOfWeakCalls()); CHECK_EQ(7, counter.NumberOfWeakCalls());
...@@ -3694,16 +3609,22 @@ THREADED_TEST(ApiObjectGroupsForSubtypes) { ...@@ -3694,16 +3609,22 @@ THREADED_TEST(ApiObjectGroupsForSubtypes) {
g1s1.handle.Reset(iso, Object::New(iso)); g1s1.handle.Reset(iso, Object::New(iso));
g1s2.handle.Reset(iso, String::NewFromUtf8(iso, "foo1")); g1s2.handle.Reset(iso, String::NewFromUtf8(iso, "foo1"));
g1c1.handle.Reset(iso, String::NewFromUtf8(iso, "foo2")); g1c1.handle.Reset(iso, String::NewFromUtf8(iso, "foo2"));
g1s1.handle.SetWeak(&g1s1, &WeakPointerCallback); g1s1.handle.SetWeak(&g1s1, &WeakPointerCallback,
g1s2.handle.SetWeak(&g1s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g1c1.handle.SetWeak(&g1c1, &WeakPointerCallback); g1s2.handle.SetWeak(&g1s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
g1c1.handle.SetWeak(&g1c1, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
g2s1.handle.Reset(iso, Object::New(iso)); g2s1.handle.Reset(iso, Object::New(iso));
g2s2.handle.Reset(iso, String::NewFromUtf8(iso, "foo3")); g2s2.handle.Reset(iso, String::NewFromUtf8(iso, "foo3"));
g2c1.handle.Reset(iso, String::NewFromUtf8(iso, "foo4")); g2c1.handle.Reset(iso, String::NewFromUtf8(iso, "foo4"));
g2s1.handle.SetWeak(&g2s1, &WeakPointerCallback); g2s1.handle.SetWeak(&g2s1, &WeakPointerCallback,
g2s2.handle.SetWeak(&g2s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g2c1.handle.SetWeak(&g2c1, &WeakPointerCallback); g2s2.handle.SetWeak(&g2s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
g2c1.handle.SetWeak(&g2c1, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
} }
WeakCallCounterAndPersistent<Value> root(&counter); WeakCallCounterAndPersistent<Value> root(&counter);
...@@ -3737,7 +3658,8 @@ THREADED_TEST(ApiObjectGroupsForSubtypes) { ...@@ -3737,7 +3658,8 @@ THREADED_TEST(ApiObjectGroupsForSubtypes) {
CHECK_EQ(0, counter.NumberOfWeakCalls()); CHECK_EQ(0, counter.NumberOfWeakCalls());
// Weaken the root. // Weaken the root.
root.handle.SetWeak(&root, &WeakPointerCallback); root.handle.SetWeak(&root, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
// But make children strong roots---all the objects (except for children) // But make children strong roots---all the objects (except for children)
// should be collectable now. // should be collectable now.
g1c1.handle.ClearWeak(); g1c1.handle.ClearWeak();
...@@ -3761,8 +3683,10 @@ THREADED_TEST(ApiObjectGroupsForSubtypes) { ...@@ -3761,8 +3683,10 @@ THREADED_TEST(ApiObjectGroupsForSubtypes) {
CHECK_EQ(5, counter.NumberOfWeakCalls()); CHECK_EQ(5, counter.NumberOfWeakCalls());
// And now make children weak again and collect them. // And now make children weak again and collect them.
g1c1.handle.SetWeak(&g1c1, &WeakPointerCallback); g1c1.handle.SetWeak(&g1c1, &WeakPointerCallback,
g2c1.handle.SetWeak(&g2c1, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g2c1.handle.SetWeak(&g2c1, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
heap->CollectAllGarbage(); heap->CollectAllGarbage();
CHECK_EQ(7, counter.NumberOfWeakCalls()); CHECK_EQ(7, counter.NumberOfWeakCalls());
...@@ -3789,29 +3713,37 @@ THREADED_TEST(ApiObjectGroupsCycle) { ...@@ -3789,29 +3713,37 @@ THREADED_TEST(ApiObjectGroupsCycle) {
HandleScope scope(iso); HandleScope scope(iso);
g1s1.handle.Reset(iso, Object::New(iso)); g1s1.handle.Reset(iso, Object::New(iso));
g1s2.handle.Reset(iso, Object::New(iso)); g1s2.handle.Reset(iso, Object::New(iso));
g1s1.handle.SetWeak(&g1s1, &WeakPointerCallback); g1s1.handle.SetWeak(&g1s1, &WeakPointerCallback,
g1s2.handle.SetWeak(&g1s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g1s2.handle.SetWeak(&g1s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
CHECK(g1s1.handle.IsWeak()); CHECK(g1s1.handle.IsWeak());
CHECK(g1s2.handle.IsWeak()); CHECK(g1s2.handle.IsWeak());
g2s1.handle.Reset(iso, Object::New(iso)); g2s1.handle.Reset(iso, Object::New(iso));
g2s2.handle.Reset(iso, Object::New(iso)); g2s2.handle.Reset(iso, Object::New(iso));
g2s1.handle.SetWeak(&g2s1, &WeakPointerCallback); g2s1.handle.SetWeak(&g2s1, &WeakPointerCallback,
g2s2.handle.SetWeak(&g2s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g2s2.handle.SetWeak(&g2s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
CHECK(g2s1.handle.IsWeak()); CHECK(g2s1.handle.IsWeak());
CHECK(g2s2.handle.IsWeak()); CHECK(g2s2.handle.IsWeak());
g3s1.handle.Reset(iso, Object::New(iso)); g3s1.handle.Reset(iso, Object::New(iso));
g3s2.handle.Reset(iso, Object::New(iso)); g3s2.handle.Reset(iso, Object::New(iso));
g3s1.handle.SetWeak(&g3s1, &WeakPointerCallback); g3s1.handle.SetWeak(&g3s1, &WeakPointerCallback,
g3s2.handle.SetWeak(&g3s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g3s2.handle.SetWeak(&g3s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
CHECK(g3s1.handle.IsWeak()); CHECK(g3s1.handle.IsWeak());
CHECK(g3s2.handle.IsWeak()); CHECK(g3s2.handle.IsWeak());
g4s1.handle.Reset(iso, Object::New(iso)); g4s1.handle.Reset(iso, Object::New(iso));
g4s2.handle.Reset(iso, Object::New(iso)); g4s2.handle.Reset(iso, Object::New(iso));
g4s1.handle.SetWeak(&g4s1, &WeakPointerCallback); g4s1.handle.SetWeak(&g4s1, &WeakPointerCallback,
g4s2.handle.SetWeak(&g4s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g4s2.handle.SetWeak(&g4s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
CHECK(g4s1.handle.IsWeak()); CHECK(g4s1.handle.IsWeak());
CHECK(g4s2.handle.IsWeak()); CHECK(g4s2.handle.IsWeak());
} }
...@@ -3849,7 +3781,8 @@ THREADED_TEST(ApiObjectGroupsCycle) { ...@@ -3849,7 +3781,8 @@ THREADED_TEST(ApiObjectGroupsCycle) {
CHECK_EQ(0, counter.NumberOfWeakCalls()); CHECK_EQ(0, counter.NumberOfWeakCalls());
// Weaken the root. // Weaken the root.
root.handle.SetWeak(&root, &WeakPointerCallback); root.handle.SetWeak(&root, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
// Groups are deleted, rebuild groups. // Groups are deleted, rebuild groups.
{ {
...@@ -3878,45 +3811,6 @@ THREADED_TEST(ApiObjectGroupsCycle) { ...@@ -3878,45 +3811,6 @@ THREADED_TEST(ApiObjectGroupsCycle) {
} }
THREADED_TEST(WeakRootsSurviveTwoRoundsOfGC) {
LocalContext env;
v8::Isolate* iso = env->GetIsolate();
HandleScope scope(iso);
WeakCallCounter counter(1234);
WeakCallCounterAndPersistent<Value> weak_obj(&counter);
// Create a weak object that references a internalized string.
{
HandleScope scope(iso);
weak_obj.handle.Reset(iso, Object::New(iso));
weak_obj.handle.SetWeak(&weak_obj, &WeakPointerCallback);
CHECK(weak_obj.handle.IsWeak());
Local<Object>::New(iso, weak_obj.handle.As<Object>())
->Set(v8_str("x"), String::NewFromUtf8(iso, "magic cookie",
String::kInternalizedString));
}
// Do a single full GC
i::Isolate* i_iso = reinterpret_cast<v8::internal::Isolate*>(iso);
i::Heap* heap = i_iso->heap();
heap->CollectAllGarbage();
// We should have received the weak callback.
CHECK_EQ(1, counter.NumberOfWeakCalls());
// Check that the string is still alive.
{
HandleScope scope(iso);
i::MaybeHandle<i::String> magic_string =
i::StringTable::LookupStringIfExists(
i_iso,
v8::Utils::OpenHandle(*String::NewFromUtf8(iso, "magic cookie")));
magic_string.Check();
}
}
// TODO(mstarzinger): This should be a THREADED_TEST but causes failures // TODO(mstarzinger): This should be a THREADED_TEST but causes failures
// on the buildbots, so was made non-threaded for the time being. // on the buildbots, so was made non-threaded for the time being.
TEST(ApiObjectGroupsCycleForScavenger) { TEST(ApiObjectGroupsCycleForScavenger) {
...@@ -3939,18 +3833,24 @@ TEST(ApiObjectGroupsCycleForScavenger) { ...@@ -3939,18 +3833,24 @@ TEST(ApiObjectGroupsCycleForScavenger) {
HandleScope scope(iso); HandleScope scope(iso);
g1s1.handle.Reset(iso, Object::New(iso)); g1s1.handle.Reset(iso, Object::New(iso));
g1s2.handle.Reset(iso, Object::New(iso)); g1s2.handle.Reset(iso, Object::New(iso));
g1s1.handle.SetWeak(&g1s1, &WeakPointerCallback); g1s1.handle.SetWeak(&g1s1, &WeakPointerCallback,
g1s2.handle.SetWeak(&g1s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g1s2.handle.SetWeak(&g1s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
g2s1.handle.Reset(iso, Object::New(iso)); g2s1.handle.Reset(iso, Object::New(iso));
g2s2.handle.Reset(iso, Object::New(iso)); g2s2.handle.Reset(iso, Object::New(iso));
g2s1.handle.SetWeak(&g2s1, &WeakPointerCallback); g2s1.handle.SetWeak(&g2s1, &WeakPointerCallback,
g2s2.handle.SetWeak(&g2s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g2s2.handle.SetWeak(&g2s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
g3s1.handle.Reset(iso, Object::New(iso)); g3s1.handle.Reset(iso, Object::New(iso));
g3s2.handle.Reset(iso, Object::New(iso)); g3s2.handle.Reset(iso, Object::New(iso));
g3s1.handle.SetWeak(&g3s1, &WeakPointerCallback); g3s1.handle.SetWeak(&g3s1, &WeakPointerCallback,
g3s2.handle.SetWeak(&g3s2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
g3s2.handle.SetWeak(&g3s2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
} }
// Make a root. // Make a root.
...@@ -3991,7 +3891,8 @@ TEST(ApiObjectGroupsCycleForScavenger) { ...@@ -3991,7 +3891,8 @@ TEST(ApiObjectGroupsCycleForScavenger) {
CHECK_EQ(0, counter.NumberOfWeakCalls()); CHECK_EQ(0, counter.NumberOfWeakCalls());
// Weaken the root. // Weaken the root.
root.handle.SetWeak(&root, &WeakPointerCallback); root.handle.SetWeak(&root, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
root.handle.MarkPartiallyDependent(); root.handle.MarkPartiallyDependent();
// Groups are deleted, rebuild groups. // Groups are deleted, rebuild groups.
...@@ -4286,9 +4187,12 @@ TEST(NativeWeakMap) { ...@@ -4286,9 +4187,12 @@ TEST(NativeWeakMap) {
CHECK(value->Equals(weak_map->Get(Local<Value>::New(isolate, s1.handle)))); CHECK(value->Equals(weak_map->Get(Local<Value>::New(isolate, s1.handle))));
} }
o1.handle.SetWeak(&o1, &WeakPointerCallback); o1.handle.SetWeak(&o1, &WeakPointerCallback,
o2.handle.SetWeak(&o2, &WeakPointerCallback); v8::WeakCallbackType::kParameter);
s1.handle.SetWeak(&s1, &WeakPointerCallback); o2.handle.SetWeak(&o2, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
s1.handle.SetWeak(&s1, &WeakPointerCallback,
v8::WeakCallbackType::kParameter);
CcTest::heap()->CollectAllGarbage(); CcTest::heap()->CollectAllGarbage();
CHECK_EQ(3, counter.NumberOfWeakCalls()); CHECK_EQ(3, counter.NumberOfWeakCalls());
...@@ -6681,11 +6585,10 @@ THREADED_TEST(InternalFieldCallback) { ...@@ -6681,11 +6585,10 @@ THREADED_TEST(InternalFieldCallback) {
static void ResetUseValueAndSetFlag( static void ResetUseValueAndSetFlag(
const v8::WeakCallbackData<v8::Object, FlagAndPersistent>& data) { const v8::WeakCallbackInfo<FlagAndPersistent>& data) {
// Blink will reset the handle, and then use the other handle, so they // Blink will reset the handle, and then use the other handle, so they
// can't use the same backing slot. // can't use the same backing slot.
data.GetParameter()->handle.Reset(); data.GetParameter()->handle.Reset();
data.GetValue()->IsBoolean(); // Make sure the handle still works.
data.GetParameter()->flag = true; data.GetParameter()->flag = true;
} }
...@@ -6714,8 +6617,10 @@ static void ResetWeakHandle(bool global_gc) { ...@@ -6714,8 +6617,10 @@ static void ResetWeakHandle(bool global_gc) {
object_a.flag = false; object_a.flag = false;
object_b.flag = false; object_b.flag = false;
object_a.handle.SetWeak(&object_a, &ResetUseValueAndSetFlag); object_a.handle.SetWeak(&object_a, &ResetUseValueAndSetFlag,
object_b.handle.SetWeak(&object_b, &ResetUseValueAndSetFlag); v8::WeakCallbackType::kParameter);
object_b.handle.SetWeak(&object_b, &ResetUseValueAndSetFlag,
v8::WeakCallbackType::kParameter);
if (!global_gc) { if (!global_gc) {
object_a.handle.MarkIndependent(); object_a.handle.MarkIndependent();
object_b.handle.MarkIndependent(); object_b.handle.MarkIndependent();
...@@ -6744,21 +6649,31 @@ static void InvokeScavenge() { CcTest::heap()->CollectGarbage(i::NEW_SPACE); } ...@@ -6744,21 +6649,31 @@ static void InvokeScavenge() { CcTest::heap()->CollectGarbage(i::NEW_SPACE); }
static void InvokeMarkSweep() { CcTest::heap()->CollectAllGarbage(); } static void InvokeMarkSweep() { CcTest::heap()->CollectAllGarbage(); }
static void ForceScavenge( static void ForceScavenge2(
const v8::WeakCallbackData<v8::Object, FlagAndPersistent>& data) { const v8::WeakCallbackInfo<FlagAndPersistent>& data) {
data.GetParameter()->handle.Reset();
data.GetParameter()->flag = true; data.GetParameter()->flag = true;
InvokeScavenge(); InvokeScavenge();
} }
static void ForceScavenge1(
static void ForceMarkSweep( const v8::WeakCallbackInfo<FlagAndPersistent>& data) {
const v8::WeakCallbackData<v8::Object, FlagAndPersistent>& data) {
data.GetParameter()->handle.Reset(); data.GetParameter()->handle.Reset();
data.SetSecondPassCallback(ForceScavenge2);
}
static void ForceMarkSweep2(
const v8::WeakCallbackInfo<FlagAndPersistent>& data) {
data.GetParameter()->flag = true; data.GetParameter()->flag = true;
InvokeMarkSweep(); InvokeMarkSweep();
} }
static void ForceMarkSweep1(
const v8::WeakCallbackInfo<FlagAndPersistent>& data) {
data.GetParameter()->handle.Reset();
data.SetSecondPassCallback(ForceMarkSweep2);
}
THREADED_TEST(GCFromWeakCallbacks) { THREADED_TEST(GCFromWeakCallbacks) {
v8::Isolate* isolate = CcTest::isolate(); v8::Isolate* isolate = CcTest::isolate();
...@@ -6767,10 +6682,9 @@ THREADED_TEST(GCFromWeakCallbacks) { ...@@ -6767,10 +6682,9 @@ THREADED_TEST(GCFromWeakCallbacks) {
Context::Scope context_scope(context); Context::Scope context_scope(context);
static const int kNumberOfGCTypes = 2; static const int kNumberOfGCTypes = 2;
typedef v8::WeakCallbackData<v8::Object, FlagAndPersistent>::Callback typedef v8::WeakCallbackInfo<FlagAndPersistent>::Callback Callback;
Callback; Callback gc_forcing_callback[kNumberOfGCTypes] = {&ForceScavenge1,
Callback gc_forcing_callback[kNumberOfGCTypes] = &ForceMarkSweep1};
{&ForceScavenge, &ForceMarkSweep};
typedef void (*GCInvoker)(); typedef void (*GCInvoker)();
GCInvoker invoke_gc[kNumberOfGCTypes] = {&InvokeScavenge, &InvokeMarkSweep}; GCInvoker invoke_gc[kNumberOfGCTypes] = {&InvokeScavenge, &InvokeMarkSweep};
...@@ -6783,7 +6697,8 @@ THREADED_TEST(GCFromWeakCallbacks) { ...@@ -6783,7 +6697,8 @@ THREADED_TEST(GCFromWeakCallbacks) {
object.handle.Reset(isolate, v8::Object::New(isolate)); object.handle.Reset(isolate, v8::Object::New(isolate));
} }
object.flag = false; object.flag = false;
object.handle.SetWeak(&object, gc_forcing_callback[inner_gc]); object.handle.SetWeak(&object, gc_forcing_callback[inner_gc],
v8::WeakCallbackType::kParameter);
object.handle.MarkIndependent(); object.handle.MarkIndependent();
invoke_gc[outer_gc](); invoke_gc[outer_gc]();
CHECK(object.flag); CHECK(object.flag);
...@@ -6792,45 +6707,6 @@ THREADED_TEST(GCFromWeakCallbacks) { ...@@ -6792,45 +6707,6 @@ THREADED_TEST(GCFromWeakCallbacks) {
} }
static void RevivingCallback(
const v8::WeakCallbackData<v8::Object, FlagAndPersistent>& data) {
data.GetParameter()->handle.ClearWeak();
data.GetParameter()->flag = true;
}
THREADED_TEST(IndependentHandleRevival) {
v8::Isolate* isolate = CcTest::isolate();
v8::HandleScope scope(isolate);
v8::Handle<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
FlagAndPersistent object;
{
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> o = v8::Object::New(isolate);
object.handle.Reset(isolate, o);
o->Set(v8_str("x"), v8::Integer::New(isolate, 1));
v8::Local<String> y_str = v8_str("y");
o->Set(y_str, y_str);
}
object.flag = false;
object.handle.SetWeak(&object, &RevivingCallback);
object.handle.MarkIndependent();
CcTest::heap()->CollectGarbage(i::NEW_SPACE);
CHECK(object.flag);
CcTest::heap()->CollectAllGarbage();
{
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> o =
v8::Local<v8::Object>::New(isolate, object.handle);
v8::Local<String> y_str = v8_str("y");
CHECK(v8::Integer::New(isolate, 1)->Equals(o->Get(v8_str("x"))));
CHECK(o->Get(y_str)->Equals(y_str));
}
}
v8::Handle<Function> args_fun; v8::Handle<Function> args_fun;
...@@ -11799,9 +11675,7 @@ TEST(CopyablePersistent) { ...@@ -11799,9 +11675,7 @@ TEST(CopyablePersistent) {
static void WeakApiCallback( static void WeakApiCallback(
const v8::WeakCallbackData<v8::Object, Persistent<v8::Object> >& data) { const v8::WeakCallbackInfo<Persistent<v8::Object>>& data) {
Local<Value> value = data.GetValue()->Get(v8_str("key"));
CHECK_EQ(231, static_cast<int32_t>(Local<v8::Integer>::Cast(value)->Value()));
data.GetParameter()->Reset(); data.GetParameter()->Reset();
delete data.GetParameter(); delete data.GetParameter();
} }
...@@ -11819,8 +11693,8 @@ TEST(WeakCallbackApi) { ...@@ -11819,8 +11693,8 @@ TEST(WeakCallbackApi) {
obj->Set(v8_str("key"), v8::Integer::New(isolate, 231)); obj->Set(v8_str("key"), v8::Integer::New(isolate, 231));
v8::Persistent<v8::Object>* handle = v8::Persistent<v8::Object>* handle =
new v8::Persistent<v8::Object>(isolate, obj); new v8::Persistent<v8::Object>(isolate, obj);
handle->SetWeak<v8::Object, v8::Persistent<v8::Object> >(handle, handle->SetWeak<v8::Persistent<v8::Object>>(
WeakApiCallback); handle, WeakApiCallback, v8::WeakCallbackType::kParameter);
} }
reinterpret_cast<i::Isolate*>(isolate)->heap()->CollectAllGarbage( reinterpret_cast<i::Isolate*>(isolate)->heap()->CollectAllGarbage(
i::Heap::kAbortIncrementalMarkingMask); i::Heap::kAbortIncrementalMarkingMask);
...@@ -11832,11 +11706,18 @@ TEST(WeakCallbackApi) { ...@@ -11832,11 +11706,18 @@ TEST(WeakCallbackApi) {
v8::Persistent<v8::Object> some_object; v8::Persistent<v8::Object> some_object;
v8::Persistent<v8::Object> bad_handle; v8::Persistent<v8::Object> bad_handle;
void NewPersistentHandleCallback(
const v8::WeakCallbackData<v8::Object, v8::Persistent<v8::Object> >& data) { void NewPersistentHandleCallback2(
const v8::WeakCallbackInfo<v8::Persistent<v8::Object>>& data) {
v8::HandleScope scope(data.GetIsolate()); v8::HandleScope scope(data.GetIsolate());
bad_handle.Reset(data.GetIsolate(), some_object); bad_handle.Reset(data.GetIsolate(), some_object);
}
void NewPersistentHandleCallback1(
const v8::WeakCallbackInfo<v8::Persistent<v8::Object>>& data) {
data.GetParameter()->Reset(); data.GetParameter()->Reset();
data.SetSecondPassCallback(NewPersistentHandleCallback2);
} }
...@@ -11855,7 +11736,8 @@ THREADED_TEST(NewPersistentHandleFromWeakCallback) { ...@@ -11855,7 +11736,8 @@ THREADED_TEST(NewPersistentHandleFromWeakCallback) {
// global handle nodes are processed by PostGarbageCollectionProcessing // global handle nodes are processed by PostGarbageCollectionProcessing
// in reverse allocation order, so if second allocated handle is deleted, // in reverse allocation order, so if second allocated handle is deleted,
// weak callback of the first handle would be able to 'reallocate' it. // weak callback of the first handle would be able to 'reallocate' it.
handle1.SetWeak(&handle1, NewPersistentHandleCallback); handle1.SetWeak(&handle1, NewPersistentHandleCallback1,
v8::WeakCallbackType::kParameter);
handle2.Reset(); handle2.Reset();
CcTest::heap()->CollectAllGarbage(); CcTest::heap()->CollectAllGarbage();
} }
...@@ -11863,11 +11745,18 @@ THREADED_TEST(NewPersistentHandleFromWeakCallback) { ...@@ -11863,11 +11745,18 @@ THREADED_TEST(NewPersistentHandleFromWeakCallback) {
v8::Persistent<v8::Object> to_be_disposed; v8::Persistent<v8::Object> to_be_disposed;
void DisposeAndForceGcCallback(
const v8::WeakCallbackData<v8::Object, v8::Persistent<v8::Object> >& data) { void DisposeAndForceGcCallback2(
const v8::WeakCallbackInfo<v8::Persistent<v8::Object>>& data) {
to_be_disposed.Reset(); to_be_disposed.Reset();
CcTest::heap()->CollectAllGarbage(); CcTest::heap()->CollectAllGarbage();
}
void DisposeAndForceGcCallback1(
const v8::WeakCallbackInfo<v8::Persistent<v8::Object>>& data) {
data.GetParameter()->Reset(); data.GetParameter()->Reset();
data.SetSecondPassCallback(DisposeAndForceGcCallback2);
} }
...@@ -11881,22 +11770,28 @@ THREADED_TEST(DoNotUseDeletedNodesInSecondLevelGc) { ...@@ -11881,22 +11770,28 @@ THREADED_TEST(DoNotUseDeletedNodesInSecondLevelGc) {
handle1.Reset(isolate, v8::Object::New(isolate)); handle1.Reset(isolate, v8::Object::New(isolate));
handle2.Reset(isolate, v8::Object::New(isolate)); handle2.Reset(isolate, v8::Object::New(isolate));
} }
handle1.SetWeak(&handle1, DisposeAndForceGcCallback); handle1.SetWeak(&handle1, DisposeAndForceGcCallback1,
v8::WeakCallbackType::kParameter);
to_be_disposed.Reset(isolate, handle2); to_be_disposed.Reset(isolate, handle2);
CcTest::heap()->CollectAllGarbage(); CcTest::heap()->CollectAllGarbage();
} }
void DisposingCallback( void DisposingCallback(
const v8::WeakCallbackData<v8::Object, v8::Persistent<v8::Object> >& data) { const v8::WeakCallbackInfo<v8::Persistent<v8::Object>>& data) {
data.GetParameter()->Reset(); data.GetParameter()->Reset();
} }
void HandleCreatingCallback( void HandleCreatingCallback2(
const v8::WeakCallbackData<v8::Object, v8::Persistent<v8::Object> >& data) { const v8::WeakCallbackInfo<v8::Persistent<v8::Object>>& data) {
v8::HandleScope scope(data.GetIsolate()); v8::HandleScope scope(data.GetIsolate());
v8::Persistent<v8::Object>(data.GetIsolate(), v8::Global<v8::Object>(data.GetIsolate(), v8::Object::New(data.GetIsolate()));
v8::Object::New(data.GetIsolate())); }
void HandleCreatingCallback1(
const v8::WeakCallbackInfo<v8::Persistent<v8::Object>>& data) {
data.GetParameter()->Reset(); data.GetParameter()->Reset();
data.SetSecondPassCallback(HandleCreatingCallback2);
} }
...@@ -11911,8 +11806,10 @@ THREADED_TEST(NoGlobalHandlesOrphaningDueToWeakCallback) { ...@@ -11911,8 +11806,10 @@ THREADED_TEST(NoGlobalHandlesOrphaningDueToWeakCallback) {
handle2.Reset(isolate, v8::Object::New(isolate)); handle2.Reset(isolate, v8::Object::New(isolate));
handle1.Reset(isolate, v8::Object::New(isolate)); handle1.Reset(isolate, v8::Object::New(isolate));
} }
handle2.SetWeak(&handle2, DisposingCallback); handle2.SetWeak(&handle2, DisposingCallback,
handle3.SetWeak(&handle3, HandleCreatingCallback); v8::WeakCallbackType::kParameter);
handle3.SetWeak(&handle3, HandleCreatingCallback1,
v8::WeakCallbackType::kParameter);
CcTest::heap()->CollectAllGarbage(); CcTest::heap()->CollectAllGarbage();
} }
......
...@@ -2031,9 +2031,8 @@ bool HasWeakGlobalHandle() { ...@@ -2031,9 +2031,8 @@ bool HasWeakGlobalHandle() {
static void PersistentHandleCallback( static void PersistentHandleCallback(
const v8::WeakCallbackData<v8::Object, v8::Persistent<v8::Object> >& data) { const v8::WeakCallbackInfo<v8::Persistent<v8::Object> >& data) {
data.GetParameter()->Reset(); data.GetParameter()->Reset();
delete data.GetParameter();
} }
...@@ -2045,7 +2044,8 @@ TEST(WeakGlobalHandle) { ...@@ -2045,7 +2044,8 @@ TEST(WeakGlobalHandle) {
v8::Persistent<v8::Object> handle(env->GetIsolate(), v8::Persistent<v8::Object> handle(env->GetIsolate(),
v8::Object::New(env->GetIsolate())); v8::Object::New(env->GetIsolate()));
handle.SetWeak(&handle, PersistentHandleCallback); handle.SetWeak(&handle, PersistentHandleCallback,
v8::WeakCallbackType::kParameter);
CHECK(HasWeakGlobalHandle()); CHECK(HasWeakGlobalHandle());
} }
......
...@@ -4470,13 +4470,11 @@ TEST(NextCodeLinkIsWeak2) { ...@@ -4470,13 +4470,11 @@ TEST(NextCodeLinkIsWeak2) {
static bool weak_ic_cleared = false; static bool weak_ic_cleared = false;
static void ClearWeakIC(const v8::WeakCallbackData<v8::Object, void>& data) { static void ClearWeakIC(
const v8::WeakCallbackInfo<v8::Persistent<v8::Object>>& data) {
printf("clear weak is called\n"); printf("clear weak is called\n");
weak_ic_cleared = true; weak_ic_cleared = true;
v8::Persistent<v8::Value>* p = data.GetParameter()->Reset();
reinterpret_cast<v8::Persistent<v8::Value>*>(data.GetParameter());
CHECK(p->IsNearDeath());
p->Reset();
} }
...@@ -4507,7 +4505,7 @@ TEST(WeakFunctionInConstructor) { ...@@ -4507,7 +4505,7 @@ TEST(WeakFunctionInConstructor) {
garbage.Reset(isolate, CompileRun(source)->ToObject(isolate)); garbage.Reset(isolate, CompileRun(source)->ToObject(isolate));
} }
weak_ic_cleared = false; weak_ic_cleared = false;
garbage.SetWeak(static_cast<void*>(&garbage), &ClearWeakIC); garbage.SetWeak(&garbage, &ClearWeakIC, v8::WeakCallbackType::kParameter);
Heap* heap = CcTest::i_isolate()->heap(); Heap* heap = CcTest::i_isolate()->heap();
heap->CollectAllGarbage(); heap->CollectAllGarbage();
CHECK(weak_ic_cleared); CHECK(weak_ic_cleared);
...@@ -4546,7 +4544,7 @@ void CheckWeakness(const char* source) { ...@@ -4546,7 +4544,7 @@ void CheckWeakness(const char* source) {
garbage.Reset(isolate, CompileRun(source)->ToObject(isolate)); garbage.Reset(isolate, CompileRun(source)->ToObject(isolate));
} }
weak_ic_cleared = false; weak_ic_cleared = false;
garbage.SetWeak(static_cast<void*>(&garbage), &ClearWeakIC); garbage.SetWeak(&garbage, &ClearWeakIC, v8::WeakCallbackType::kParameter);
Heap* heap = CcTest::i_isolate()->heap(); Heap* heap = CcTest::i_isolate()->heap();
heap->CollectAllGarbage(); heap->CollectAllGarbage();
CHECK(weak_ic_cleared); CHECK(weak_ic_cleared);
......
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