2018-03-05 13:18:51 +00:00
|
|
|
// Copyright 2018 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2019-05-21 09:30:15 +00:00
|
|
|
#include "src/codegen/assembler-inl.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/isolate.h"
|
2018-04-09 19:11:22 +00:00
|
|
|
#include "src/heap/factory.h"
|
2019-02-14 21:10:30 +00:00
|
|
|
#include "src/heap/heap-inl.h"
|
2018-11-03 00:13:22 +00:00
|
|
|
#include "src/objects/smi.h"
|
2018-03-05 13:18:51 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2018-05-22 16:03:55 +00:00
|
|
|
#include "test/cctest/heap/heap-tester.h"
|
2018-03-05 13:18:51 +00:00
|
|
|
#include "test/cctest/heap/heap-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace heap {
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> CreateLoadHandlerForTest(
|
|
|
|
Factory* factory, AllocationType allocation = AllocationType::kYoung) {
|
|
|
|
Handle<LoadHandler> result = factory->NewLoadHandler(1, allocation);
|
|
|
|
result->set_smi_handler(Smi::kZero);
|
|
|
|
result->set_validity_cell(Smi::kZero);
|
|
|
|
result->set_data1(MaybeObject::FromSmi(Smi::zero()));
|
|
|
|
return result;
|
2018-03-05 13:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakReferencesBasic) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh = CreateLoadHandlerForTest(factory);
|
|
|
|
CHECK(Heap::InYoungGeneration(*lh));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
MaybeObject code_object = lh->data1();
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK(code_object->IsSmi());
|
|
|
|
CcTest::CollectAllGarbage();
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*lh));
|
|
|
|
CHECK_EQ(code_object, lh->data1());
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope inner_scope(isolate);
|
|
|
|
|
|
|
|
// Create a new Code.
|
2019-01-16 18:29:52 +00:00
|
|
|
Assembler assm(AssemblerOptions{});
|
2018-03-05 13:18:51 +00:00
|
|
|
assm.nop(); // supported on all architectures
|
|
|
|
CodeDesc desc;
|
|
|
|
assm.GetCode(isolate, &desc);
|
2019-04-29 16:15:47 +00:00
|
|
|
Handle<Code> code = Factory::CodeBuilder(isolate, desc, Code::STUB).Build();
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK(code->IsCode());
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*code));
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject code_heap_object;
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->GetHeapObjectIfWeak(&code_heap_object));
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK_EQ(*code, code_heap_object);
|
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->GetHeapObjectIfWeak(&code_heap_object));
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK_EQ(*code, code_heap_object);
|
|
|
|
} // code will go out of scope.
|
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->IsCleared());
|
2018-03-05 13:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakReferencesOldToOld) {
|
|
|
|
// Like WeakReferencesBasic, but the updated weak slot is in the old space,
|
|
|
|
// and referring to an old space object.
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
FLAG_manual_evacuation_candidates_selection = true;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh =
|
|
|
|
CreateLoadHandlerForTest(factory, AllocationType::kOld);
|
|
|
|
CHECK(heap->InOldSpace(*lh));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Create a new FixedArray which the LoadHandler will point to.
|
2019-03-11 19:04:02 +00:00
|
|
|
Handle<FixedArray> fixed_array =
|
|
|
|
factory->NewFixedArray(1, AllocationType::kOld);
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK(heap->InOldSpace(*fixed_array));
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*fixed_array));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
2019-01-15 00:23:43 +00:00
|
|
|
Page* page_before_gc = Page::FromHeapObject(*fixed_array);
|
2018-03-05 13:18:51 +00:00
|
|
|
heap::ForceEvacuationCandidate(page_before_gc);
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
CHECK(heap->InOldSpace(*fixed_array));
|
|
|
|
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->GetHeapObjectIfWeak(&heap_object));
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK_EQ(heap_object, *fixed_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakReferencesOldToNew) {
|
|
|
|
// Like WeakReferencesBasic, but the updated weak slot is in the old space,
|
|
|
|
// and referring to an new space object.
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh =
|
|
|
|
CreateLoadHandlerForTest(factory, AllocationType::kOld);
|
|
|
|
CHECK(heap->InOldSpace(*lh));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Create a new FixedArray which the LoadHandler will point to.
|
2018-03-05 13:18:51 +00:00
|
|
|
Handle<FixedArray> fixed_array = factory->NewFixedArray(1);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*fixed_array));
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*fixed_array));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->GetHeapObjectIfWeak(&heap_object));
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK_EQ(heap_object, *fixed_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakReferencesOldToNewScavenged) {
|
|
|
|
// Like WeakReferencesBasic, but the updated weak slot is in the old space,
|
|
|
|
// and referring to an new space object, which is then scavenged.
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh =
|
|
|
|
CreateLoadHandlerForTest(factory, AllocationType::kOld);
|
|
|
|
CHECK(heap->InOldSpace(*lh));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Create a new FixedArray which the LoadHandler will point to.
|
2018-03-05 13:18:51 +00:00
|
|
|
Handle<FixedArray> fixed_array = factory->NewFixedArray(1);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*fixed_array));
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*fixed_array));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
|
|
|
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->GetHeapObjectIfWeak(&heap_object));
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK_EQ(heap_object, *fixed_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakReferencesOldToCleared) {
|
|
|
|
// Like WeakReferencesBasic, but the updated weak slot is in the old space,
|
|
|
|
// and is cleared.
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
FLAG_manual_evacuation_candidates_selection = true;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh =
|
|
|
|
CreateLoadHandlerForTest(factory, AllocationType::kOld);
|
|
|
|
CHECK(heap->InOldSpace(*lh));
|
|
|
|
lh->set_data1(HeapObjectReference::ClearedValue(isolate));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->IsCleared());
|
2018-03-05 13:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ObjectMovesBeforeClearingWeakField) {
|
|
|
|
if (!FLAG_incremental_marking) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh = CreateLoadHandlerForTest(factory);
|
|
|
|
CHECK(Heap::InYoungGeneration(*lh));
|
|
|
|
LoadHandler lh_location = *lh;
|
2018-03-05 13:18:51 +00:00
|
|
|
{
|
|
|
|
HandleScope inner_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
// Create a new FixedArray which the LoadHandler will point to.
|
2018-03-05 13:18:51 +00:00
|
|
|
Handle<FixedArray> fixed_array = factory->NewFixedArray(1);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*fixed_array));
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*fixed_array));
|
2018-03-05 13:18:51 +00:00
|
|
|
// inner_scope will go out of scope, so when marking the next time,
|
|
|
|
// *fixed_array will stay white.
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Do marking steps; this will store *lh into the list for later processing
|
2018-03-05 13:18:51 +00:00
|
|
|
// (since it points to a white object).
|
|
|
|
SimulateIncrementalMarking(heap, true);
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Scavenger will move *lh.
|
2018-03-05 13:18:51 +00:00
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
2019-06-13 15:45:49 +00:00
|
|
|
LoadHandler new_lh_location = *lh;
|
|
|
|
CHECK_NE(lh_location, new_lh_location);
|
|
|
|
CHECK(lh->data1()->IsWeak());
|
2018-03-05 13:18:51 +00:00
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Now we try to clear *lh.
|
2018-03-05 13:18:51 +00:00
|
|
|
CcTest::CollectAllGarbage();
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->IsCleared());
|
2018-03-05 13:18:51 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 11:09:53 +00:00
|
|
|
TEST(ObjectWithWeakFieldDies) {
|
|
|
|
if (!FLAG_incremental_marking) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh = CreateLoadHandlerForTest(factory);
|
|
|
|
CHECK(Heap::InYoungGeneration(*lh));
|
2018-04-10 11:09:53 +00:00
|
|
|
{
|
|
|
|
HandleScope inner_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
// Create a new FixedArray which the LoadHandler will point to.
|
2018-04-10 11:09:53 +00:00
|
|
|
Handle<FixedArray> fixed_array = factory->NewFixedArray(1);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*fixed_array));
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*fixed_array));
|
2018-04-10 11:09:53 +00:00
|
|
|
// inner_scope will go out of scope, so when marking the next time,
|
|
|
|
// *fixed_array will stay white.
|
|
|
|
}
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Do marking steps; this will store *lh into the list for later processing
|
2018-04-10 11:09:53 +00:00
|
|
|
// (since it points to a white object).
|
|
|
|
SimulateIncrementalMarking(heap, true);
|
|
|
|
} // outer_scope goes out of scope
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// lh will die
|
2018-04-10 11:09:53 +00:00
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
|
|
|
|
|
|
|
// This used to crash when processing the dead weak reference.
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
}
|
|
|
|
|
2018-03-05 13:18:51 +00:00
|
|
|
TEST(ObjectWithWeakReferencePromoted) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh = CreateLoadHandlerForTest(factory);
|
|
|
|
CHECK(Heap::InYoungGeneration(*lh));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Create a new FixedArray which the LoadHandler will point to.
|
2018-03-05 13:18:51 +00:00
|
|
|
Handle<FixedArray> fixed_array = factory->NewFixedArray(1);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*fixed_array));
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*fixed_array));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(heap->InOldSpace(*lh));
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK(heap->InOldSpace(*fixed_array));
|
|
|
|
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->GetHeapObjectIfWeak(&heap_object));
|
2018-03-05 13:18:51 +00:00
|
|
|
CHECK_EQ(heap_object, *fixed_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ObjectWithClearedWeakReferencePromoted) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh = CreateLoadHandlerForTest(factory);
|
|
|
|
CHECK(Heap::InYoungGeneration(*lh));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::ClearedValue(isolate));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*lh));
|
|
|
|
CHECK(lh->data1()->IsCleared());
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(heap->InOldSpace(*lh));
|
|
|
|
CHECK(lh->data1()->IsCleared());
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->IsCleared());
|
2018-03-05 13:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakReferenceWriteBarrier) {
|
|
|
|
if (!FLAG_incremental_marking) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
|
|
|
|
HandleScope outer_scope(isolate);
|
2019-06-13 15:45:49 +00:00
|
|
|
Handle<LoadHandler> lh = CreateLoadHandlerForTest(factory);
|
|
|
|
CHECK(Heap::InYoungGeneration(*lh));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope inner_scope(isolate);
|
|
|
|
|
2019-06-13 15:45:49 +00:00
|
|
|
// Create a new FixedArray which the LoadHandler will point to.
|
2018-03-05 13:18:51 +00:00
|
|
|
Handle<FixedArray> fixed_array1 = factory->NewFixedArray(1);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*fixed_array1));
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*fixed_array1));
|
2018-03-05 13:18:51 +00:00
|
|
|
|
|
|
|
SimulateIncrementalMarking(heap, true);
|
|
|
|
|
|
|
|
Handle<FixedArray> fixed_array2 = factory->NewFixedArray(1);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*fixed_array2));
|
2018-03-05 13:18:51 +00:00
|
|
|
// This write will trigger the write barrier.
|
2019-06-13 15:45:49 +00:00
|
|
|
lh->set_data1(HeapObjectReference::Weak(*fixed_array2));
|
2018-03-05 13:18:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
|
|
|
|
// Check that the write barrier treated the weak reference as strong.
|
2019-06-13 15:45:49 +00:00
|
|
|
CHECK(lh->data1()->IsWeak());
|
2018-03-05 13:18:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 10:26:53 +00:00
|
|
|
TEST(EmptyWeakArray) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
|
|
|
|
Handle<WeakFixedArray> array = factory->empty_weak_fixed_array();
|
|
|
|
CHECK(array->IsWeakFixedArray());
|
|
|
|
CHECK(!array->IsFixedArray());
|
|
|
|
CHECK_EQ(array->length(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakArraysBasic) {
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
|
|
|
|
const int length = 4;
|
|
|
|
Handle<WeakFixedArray> array = factory->NewWeakFixedArray(length);
|
|
|
|
CHECK(array->IsWeakFixedArray());
|
|
|
|
CHECK(!array->IsFixedArray());
|
|
|
|
CHECK_EQ(array->length(), length);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*array));
|
2018-03-12 10:26:53 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i) {
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(i)->GetHeapObjectIfStrong(&heap_object));
|
2018-07-04 09:10:05 +00:00
|
|
|
CHECK_EQ(heap_object, ReadOnlyRoots(heap).undefined_value());
|
2018-03-12 10:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<HeapObject> saved;
|
|
|
|
{
|
|
|
|
HandleScope inner_scope(isolate);
|
|
|
|
Handle<FixedArray> index0 = factory->NewFixedArray(1);
|
|
|
|
index0->set(0, Smi::FromInt(2016));
|
|
|
|
Handle<FixedArray> index1 = factory->NewFixedArray(1);
|
|
|
|
index1->set(0, Smi::FromInt(2017));
|
|
|
|
|
|
|
|
Handle<FixedArray> index2 = factory->NewFixedArray(1);
|
|
|
|
index2->set(0, Smi::FromInt(2018));
|
|
|
|
Handle<FixedArray> index3 = factory->NewFixedArray(1);
|
|
|
|
index3->set(0, Smi::FromInt(2019));
|
|
|
|
|
|
|
|
array->Set(0, HeapObjectReference::Weak(*index0));
|
|
|
|
array->Set(1, HeapObjectReference::Weak(*index1));
|
|
|
|
array->Set(2, HeapObjectReference::Strong(*index2));
|
|
|
|
array->Set(3, HeapObjectReference::Weak(*index3));
|
|
|
|
saved = inner_scope.CloseAndEscape(index1);
|
|
|
|
} // inner_scope goes out of scope.
|
|
|
|
|
|
|
|
// The references are only cleared by the mark-compact (scavenger treats weak
|
|
|
|
// references as strong). Thus we need to GC until the array reaches old
|
|
|
|
// space.
|
|
|
|
|
|
|
|
// TODO(marja): update this when/if we do handle weak references in the new
|
|
|
|
// space.
|
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(0)->GetHeapObjectIfWeak(&heap_object));
|
2018-03-12 10:26:53 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2016);
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(1)->GetHeapObjectIfWeak(&heap_object));
|
2018-03-12 10:26:53 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2017);
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(2)->GetHeapObjectIfStrong(&heap_object));
|
2018-03-12 10:26:53 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2018);
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(3)->GetHeapObjectIfWeak(&heap_object));
|
2018-03-12 10:26:53 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2019);
|
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
CHECK(heap->InOldSpace(*array));
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(0)->IsCleared());
|
|
|
|
CHECK(array->Get(1)->GetHeapObjectIfWeak(&heap_object));
|
2018-03-12 10:26:53 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2017);
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(2)->GetHeapObjectIfStrong(&heap_object));
|
2018-03-12 10:26:53 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2018);
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(3)->IsCleared());
|
2018-03-12 10:26:53 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 07:28:28 +00:00
|
|
|
TEST(WeakArrayListBasic) {
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
|
2018-07-04 09:10:05 +00:00
|
|
|
Handle<WeakArrayList> array(ReadOnlyRoots(heap).empty_weak_array_list(),
|
|
|
|
isolate);
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK(array->IsWeakArrayList());
|
|
|
|
CHECK(!array->IsFixedArray());
|
|
|
|
CHECK(!array->IsWeakFixedArray());
|
|
|
|
CHECK_EQ(array->length(), 0);
|
|
|
|
|
|
|
|
Handle<FixedArray> index2 = factory->NewFixedArray(1);
|
|
|
|
index2->set(0, Smi::FromInt(2017));
|
|
|
|
|
|
|
|
Handle<HeapObject> saved;
|
|
|
|
{
|
|
|
|
HandleScope inner_scope(isolate);
|
|
|
|
Handle<FixedArray> index0 = factory->NewFixedArray(1);
|
|
|
|
index0->set(0, Smi::FromInt(2016));
|
|
|
|
Handle<FixedArray> index4 = factory->NewFixedArray(1);
|
|
|
|
index4->set(0, Smi::FromInt(2018));
|
|
|
|
Handle<FixedArray> index6 = factory->NewFixedArray(1);
|
|
|
|
index6->set(0, Smi::FromInt(2019));
|
|
|
|
|
2018-07-13 09:32:35 +00:00
|
|
|
array = WeakArrayList::AddToEnd(isolate, array,
|
|
|
|
MaybeObjectHandle::Weak(index0));
|
2018-06-04 08:09:12 +00:00
|
|
|
array = WeakArrayList::AddToEnd(
|
2018-07-13 09:32:35 +00:00
|
|
|
isolate, array, MaybeObjectHandle(Smi::FromInt(1), isolate));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(array->length(), 2);
|
|
|
|
|
2018-07-13 09:32:35 +00:00
|
|
|
array = WeakArrayList::AddToEnd(isolate, array,
|
|
|
|
MaybeObjectHandle::Weak(index2));
|
2018-06-04 08:09:12 +00:00
|
|
|
array = WeakArrayList::AddToEnd(
|
2018-07-13 09:32:35 +00:00
|
|
|
isolate, array, MaybeObjectHandle(Smi::FromInt(3), isolate));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(array->length(), 4);
|
|
|
|
|
2018-07-13 09:32:35 +00:00
|
|
|
array = WeakArrayList::AddToEnd(isolate, array,
|
|
|
|
MaybeObjectHandle::Weak(index4));
|
2018-06-04 08:09:12 +00:00
|
|
|
array = WeakArrayList::AddToEnd(
|
2018-07-13 09:32:35 +00:00
|
|
|
isolate, array, MaybeObjectHandle(Smi::FromInt(5), isolate));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(array->length(), 6);
|
|
|
|
|
2018-07-13 09:32:35 +00:00
|
|
|
array = WeakArrayList::AddToEnd(isolate, array,
|
|
|
|
MaybeObjectHandle::Weak(index6));
|
2018-06-04 08:09:12 +00:00
|
|
|
array = WeakArrayList::AddToEnd(
|
2018-07-13 09:32:35 +00:00
|
|
|
isolate, array, MaybeObjectHandle(Smi::FromInt(7), isolate));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(array->length(), 8);
|
|
|
|
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(Heap::InYoungGeneration(*array));
|
2018-04-16 07:28:28 +00:00
|
|
|
|
|
|
|
CHECK_EQ(array->Get(0), HeapObjectReference::Weak(*index0));
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(1).ToSmi().value(), 1);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
|
|
|
CHECK_EQ(array->Get(2), HeapObjectReference::Weak(*index2));
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(3).ToSmi().value(), 3);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
|
|
|
CHECK_EQ(array->Get(4), HeapObjectReference::Weak(*index4));
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(5).ToSmi().value(), 5);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
|
|
|
CHECK_EQ(array->Get(6), HeapObjectReference::Weak(*index6));
|
|
|
|
array = inner_scope.CloseAndEscape(array);
|
|
|
|
} // inner_scope goes out of scope.
|
|
|
|
|
|
|
|
// The references are only cleared by the mark-compact (scavenger treats weak
|
|
|
|
// references as strong). Thus we need to GC until the array reaches old
|
|
|
|
// space.
|
|
|
|
|
|
|
|
// TODO(marja): update this when/if we do handle weak references in the new
|
|
|
|
// space.
|
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject heap_object;
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(array->length(), 8);
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(0)->GetHeapObjectIfWeak(&heap_object));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2016);
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(1).ToSmi().value(), 1);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(2)->GetHeapObjectIfWeak(&heap_object));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2017);
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(3).ToSmi().value(), 3);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(4)->GetHeapObjectIfWeak(&heap_object));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2018);
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(5).ToSmi().value(), 5);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(6)->GetHeapObjectIfWeak(&heap_object));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2019);
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(7).ToSmi().value(), 7);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
CHECK(heap->InOldSpace(*array));
|
|
|
|
CHECK_EQ(array->length(), 8);
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(0)->IsCleared());
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(1).ToSmi().value(), 1);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(2)->GetHeapObjectIfWeak(&heap_object));
|
2018-04-16 07:28:28 +00:00
|
|
|
CHECK_EQ(Smi::cast(FixedArray::cast(heap_object).get(0)).value(), 2017);
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(3).ToSmi().value(), 3);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(4)->IsCleared());
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(5).ToSmi().value(), 5);
|
2018-04-16 07:28:28 +00:00
|
|
|
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(6)->IsCleared());
|
2018-11-03 00:13:22 +00:00
|
|
|
CHECK_EQ(array->Get(7).ToSmi().value(), 7);
|
2018-04-16 07:28:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 11:53:58 +00:00
|
|
|
TEST(WeakArrayListRemove) {
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
|
|
|
|
Handle<WeakArrayList> array(ReadOnlyRoots(heap).empty_weak_array_list(),
|
|
|
|
isolate);
|
|
|
|
|
|
|
|
Handle<FixedArray> elem0 = factory->NewFixedArray(1);
|
|
|
|
Handle<FixedArray> elem1 = factory->NewFixedArray(1);
|
|
|
|
Handle<FixedArray> elem2 = factory->NewFixedArray(1);
|
|
|
|
|
|
|
|
array =
|
|
|
|
WeakArrayList::AddToEnd(isolate, array, MaybeObjectHandle::Weak(elem0));
|
|
|
|
array =
|
|
|
|
WeakArrayList::AddToEnd(isolate, array, MaybeObjectHandle::Weak(elem1));
|
|
|
|
array =
|
|
|
|
WeakArrayList::AddToEnd(isolate, array, MaybeObjectHandle::Weak(elem2));
|
|
|
|
|
|
|
|
CHECK_EQ(array->length(), 3);
|
|
|
|
CHECK_EQ(array->Get(0), HeapObjectReference::Weak(*elem0));
|
|
|
|
CHECK_EQ(array->Get(1), HeapObjectReference::Weak(*elem1));
|
|
|
|
CHECK_EQ(array->Get(2), HeapObjectReference::Weak(*elem2));
|
|
|
|
|
|
|
|
CHECK(array->RemoveOne(MaybeObjectHandle::Weak(elem1)));
|
|
|
|
|
2018-08-02 14:26:12 +00:00
|
|
|
CHECK_EQ(array->length(), 2);
|
2018-07-30 11:53:58 +00:00
|
|
|
CHECK_EQ(array->Get(0), HeapObjectReference::Weak(*elem0));
|
2018-08-02 14:26:12 +00:00
|
|
|
CHECK_EQ(array->Get(1), HeapObjectReference::Weak(*elem2));
|
2018-07-30 11:53:58 +00:00
|
|
|
|
|
|
|
CHECK(!array->RemoveOne(MaybeObjectHandle::Weak(elem1)));
|
|
|
|
|
2018-08-02 14:26:12 +00:00
|
|
|
CHECK_EQ(array->length(), 2);
|
2018-07-30 11:53:58 +00:00
|
|
|
CHECK_EQ(array->Get(0), HeapObjectReference::Weak(*elem0));
|
2018-08-02 14:26:12 +00:00
|
|
|
CHECK_EQ(array->Get(1), HeapObjectReference::Weak(*elem2));
|
2018-07-30 11:53:58 +00:00
|
|
|
|
|
|
|
CHECK(array->RemoveOne(MaybeObjectHandle::Weak(elem0)));
|
|
|
|
|
2018-08-02 14:26:12 +00:00
|
|
|
CHECK_EQ(array->length(), 1);
|
|
|
|
CHECK_EQ(array->Get(0), HeapObjectReference::Weak(*elem2));
|
2018-07-30 11:53:58 +00:00
|
|
|
|
|
|
|
CHECK(array->RemoveOne(MaybeObjectHandle::Weak(elem2)));
|
|
|
|
|
2018-08-02 14:26:12 +00:00
|
|
|
CHECK_EQ(array->length(), 0);
|
2018-07-30 11:53:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 16:03:55 +00:00
|
|
|
TEST(Regress7768) {
|
|
|
|
i::FLAG_allow_natives_syntax = true;
|
|
|
|
i::FLAG_turbo_inlining = false;
|
|
|
|
if (!FLAG_incremental_marking) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
// Create an optimized code which will contain a weak reference to another
|
|
|
|
// function ("f"). The weak reference is the only reference to the function.
|
|
|
|
CompileRun(
|
|
|
|
"function myfunc(f) { f(); } "
|
2019-04-30 11:04:41 +00:00
|
|
|
"%PrepareFunctionForOptimization(myfunc); "
|
2018-05-22 16:03:55 +00:00
|
|
|
"(function wrapper() { "
|
|
|
|
" function f() {}; myfunc(f); myfunc(f); "
|
|
|
|
" %OptimizeFunctionOnNextCall(myfunc); myfunc(f); "
|
|
|
|
" %ClearFunctionFeedback(wrapper);"
|
|
|
|
"})(); "
|
|
|
|
"%ClearFunctionFeedback(myfunc);");
|
|
|
|
|
|
|
|
// Do marking steps; this will store the objects pointed by myfunc for later
|
|
|
|
// processing.
|
|
|
|
SimulateIncrementalMarking(heap, true);
|
|
|
|
|
|
|
|
// Deoptimize the code; now the pointers inside it will be replaced with
|
|
|
|
// undefined, and the weak_objects_in_code is the only place pointing to the
|
|
|
|
// function f.
|
|
|
|
CompileRun("%DeoptimizeFunction(myfunc);");
|
|
|
|
|
|
|
|
// The object pointed to by the weak reference won't be scavenged.
|
|
|
|
CcTest::CollectGarbage(NEW_SPACE);
|
|
|
|
|
|
|
|
// Make sure the memory where it's stored is invalidated, so that we'll crash
|
|
|
|
// if we try to access it.
|
|
|
|
HeapTester::UncommitFromSpace(heap);
|
|
|
|
|
|
|
|
// This used to crash when processing the dead weak reference.
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
}
|
|
|
|
|
2018-07-17 07:05:17 +00:00
|
|
|
TEST(PrototypeUsersBasic) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
|
|
|
|
Handle<WeakArrayList> array(ReadOnlyRoots(heap).empty_weak_array_list(),
|
|
|
|
isolate);
|
|
|
|
|
|
|
|
// Add some objects into the array.
|
|
|
|
int index = -1;
|
|
|
|
{
|
|
|
|
Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, map, &index);
|
|
|
|
CHECK_EQ(array->length(), index + 1);
|
|
|
|
}
|
|
|
|
CHECK_EQ(index, 1);
|
|
|
|
|
|
|
|
int empty_index = index;
|
|
|
|
PrototypeUsers::MarkSlotEmpty(*array, empty_index);
|
|
|
|
|
|
|
|
// Even though we have an empty slot, we still add to the end.
|
|
|
|
int last_index = index;
|
|
|
|
int old_capacity = array->capacity();
|
|
|
|
while (!array->IsFull()) {
|
|
|
|
Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, map, &index);
|
|
|
|
CHECK_EQ(index, last_index + 1);
|
|
|
|
CHECK_EQ(array->length(), index + 1);
|
|
|
|
last_index = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The next addition will fill the empty slot.
|
|
|
|
{
|
|
|
|
Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, map, &index);
|
|
|
|
}
|
|
|
|
CHECK_EQ(index, empty_index);
|
|
|
|
|
|
|
|
// The next addition will make the arrow grow again.
|
|
|
|
{
|
|
|
|
Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, map, &index);
|
|
|
|
CHECK_EQ(array->length(), index + 1);
|
|
|
|
last_index = index;
|
|
|
|
}
|
|
|
|
CHECK_GT(array->capacity(), old_capacity);
|
|
|
|
|
|
|
|
// Make multiple slots empty.
|
|
|
|
int empty_index1 = 1;
|
|
|
|
int empty_index2 = 2;
|
|
|
|
PrototypeUsers::MarkSlotEmpty(*array, empty_index1);
|
|
|
|
PrototypeUsers::MarkSlotEmpty(*array, empty_index2);
|
|
|
|
|
|
|
|
// Fill the array (still adding to the end)
|
|
|
|
old_capacity = array->capacity();
|
|
|
|
while (!array->IsFull()) {
|
|
|
|
Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, map, &index);
|
|
|
|
CHECK_EQ(index, last_index + 1);
|
|
|
|
CHECK_EQ(array->length(), index + 1);
|
|
|
|
last_index = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we use the empty slots in (reverse) order.
|
|
|
|
{
|
|
|
|
Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, map, &index);
|
|
|
|
}
|
|
|
|
CHECK_EQ(index, empty_index2);
|
|
|
|
|
|
|
|
{
|
|
|
|
Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, map, &index);
|
|
|
|
}
|
|
|
|
CHECK_EQ(index, empty_index1);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-12-20 15:47:47 +00:00
|
|
|
HeapObject saved_heap_object;
|
2018-07-17 07:05:17 +00:00
|
|
|
|
2018-12-20 15:47:47 +00:00
|
|
|
static void TestCompactCallback(HeapObject value, int old_index,
|
2018-07-17 07:05:17 +00:00
|
|
|
int new_index) {
|
|
|
|
saved_heap_object = value;
|
|
|
|
CHECK_EQ(old_index, 2);
|
|
|
|
CHECK_EQ(new_index, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(PrototypeUsersCompacted) {
|
|
|
|
ManualGCScope manual_gc_scope;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
LocalContext context;
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Heap* heap = isolate->heap();
|
|
|
|
HandleScope outer_scope(isolate);
|
|
|
|
|
|
|
|
Handle<WeakArrayList> array(ReadOnlyRoots(heap).empty_weak_array_list(),
|
|
|
|
isolate);
|
|
|
|
|
|
|
|
// Add some objects into the array.
|
|
|
|
int index = -1;
|
|
|
|
Handle<Map> map_cleared_by_user =
|
|
|
|
factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, map_cleared_by_user, &index);
|
|
|
|
CHECK_EQ(index, 1);
|
|
|
|
Handle<Map> live_map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, live_map, &index);
|
|
|
|
CHECK_EQ(index, 2);
|
|
|
|
{
|
|
|
|
HandleScope inner_scope(isolate);
|
|
|
|
Handle<Map> soon_dead_map =
|
|
|
|
factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
|
|
|
|
array = PrototypeUsers::Add(isolate, array, soon_dead_map, &index);
|
|
|
|
CHECK_EQ(index, 3);
|
|
|
|
|
|
|
|
array = inner_scope.CloseAndEscape(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
PrototypeUsers::MarkSlotEmpty(*array, 1);
|
|
|
|
CcTest::CollectAllGarbage();
|
Reland [in-place weak refs] Fix MaybeObject function names
E.g., "ToWeakHeapObject" was misleading, since it didn't convert to a weak heap
object, instead returned a weakly pointed heap object. Change the function names
(in this case, to "GetHeapObjectIfWeak") to reflect this.
Also make casts explicit, if a MaybeObject is an Object, we can call cast<Object>().
Previous version: https://chromium-review.googlesource.com/1219025
BUG=v8:7308
TBR=ishell@chromium.org, ulan@chromium.org, ahaas@chromium.org, yangguo@chromium.org, tebbi@chromium.org
Change-Id: I503d4a2a3a68f85e9e02e1c2f9fc1c4187c8e9a1
Reviewed-on: https://chromium-review.googlesource.com/1226800
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55934}
2018-09-17 07:36:25 +00:00
|
|
|
CHECK(array->Get(3)->IsCleared());
|
2018-07-17 07:05:17 +00:00
|
|
|
|
|
|
|
CHECK_EQ(array->length(), 3 + PrototypeUsers::kFirstIndex);
|
2018-12-03 22:43:18 +00:00
|
|
|
WeakArrayList new_array =
|
2018-07-17 07:05:17 +00:00
|
|
|
PrototypeUsers::Compact(array, heap, TestCompactCallback);
|
|
|
|
CHECK_EQ(new_array.length(), 1 + PrototypeUsers::kFirstIndex);
|
|
|
|
CHECK_EQ(saved_heap_object, *live_map);
|
|
|
|
}
|
|
|
|
|
2018-03-05 13:18:51 +00:00
|
|
|
} // namespace heap
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|