Commit 34f8d88d authored by Marja Hölttä's avatar Marja Hölttä Committed by Commit Bot

[js weak refs] Add simple cctests for JSWeakRefs

BUG=v8:8179

Change-Id: I8ef7d3b576db9e872ab527895f5ba3be4ba3f6ce
Reviewed-on: https://chromium-review.googlesource.com/c/1379881Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58304}
parent ff746139
...@@ -5,8 +5,10 @@ ...@@ -5,8 +5,10 @@
#include "src/handles-inl.h" #include "src/handles-inl.h"
#include "src/heap/factory-inl.h" #include "src/heap/factory-inl.h"
#include "src/isolate.h" #include "src/isolate.h"
#include "src/objects/js-objects.h"
#include "src/objects/js-weak-refs-inl.h" #include "src/objects/js-weak-refs-inl.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
#include "test/cctest/heap/heap-utils.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
...@@ -29,6 +31,24 @@ Handle<JSWeakFactory> ConstructJSWeakFactory(Isolate* isolate) { ...@@ -29,6 +31,24 @@ Handle<JSWeakFactory> ConstructJSWeakFactory(Isolate* isolate) {
return weak_factory; return weak_factory;
} }
Handle<JSWeakRef> ConstructJSWeakRef(Isolate* isolate,
Handle<JSReceiver> target) {
Factory* factory = isolate->factory();
Handle<String> weak_ref_name = factory->WeakRef_string();
Handle<Object> global =
handle(isolate->native_context()->global_object(), isolate);
Handle<JSFunction> weak_ref_fun = Handle<JSFunction>::cast(
Object::GetProperty(isolate, global, weak_ref_name).ToHandleChecked());
auto weak_ref = Handle<JSWeakRef>::cast(
JSObject::New(weak_ref_fun, weak_ref_fun, Handle<AllocationSite>::null())
.ToHandleChecked());
weak_ref->set_target(*target);
#ifdef VERIFY_HEAP
weak_ref->JSWeakRefVerify(isolate);
#endif // VERIFY_HEAP
return weak_ref;
}
Handle<JSWeakCell> MakeCell(Isolate* isolate, Handle<JSObject> js_object, Handle<JSWeakCell> MakeCell(Isolate* isolate, Handle<JSObject> js_object,
Handle<JSWeakFactory> weak_factory) { Handle<JSWeakFactory> weak_factory) {
Handle<Map> weak_cell_map(isolate->native_context()->js_weak_cell_map(), Handle<Map> weak_cell_map(isolate->native_context()->js_weak_cell_map(),
...@@ -315,5 +335,142 @@ TEST(TestJSWeakCellClearPopped) { ...@@ -315,5 +335,142 @@ TEST(TestJSWeakCellClearPopped) {
ClearWeakCell(weak_cell1, isolate); ClearWeakCell(weak_cell1, isolate);
} }
TEST(TestJSWeakRef) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
HandleScope outer_scope(isolate);
Handle<JSWeakRef> weak_ref;
{
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
// This doesn't add the target into the KeepDuringJob set.
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(isolate, js_object);
CcTest::CollectAllGarbage();
CHECK(!inner_weak_ref->target()->IsUndefined(isolate));
weak_ref = inner_scope.CloseAndEscape(inner_weak_ref);
}
CHECK(!weak_ref->target()->IsUndefined(isolate));
CcTest::CollectAllGarbage();
CHECK(weak_ref->target()->IsUndefined(isolate));
}
TEST(TestJSWeakRefIncrementalMarking) {
FLAG_harmony_weak_refs = true;
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);
Handle<JSWeakRef> weak_ref;
{
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
// This doesn't add the target into the KeepDuringJob set.
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(isolate, js_object);
heap::SimulateIncrementalMarking(heap, true);
CcTest::CollectAllGarbage();
CHECK(!inner_weak_ref->target()->IsUndefined(isolate));
weak_ref = inner_scope.CloseAndEscape(inner_weak_ref);
}
CHECK(!weak_ref->target()->IsUndefined(isolate));
heap::SimulateIncrementalMarking(heap, true);
CcTest::CollectAllGarbage();
CHECK(weak_ref->target()->IsUndefined(isolate));
}
TEST(TestJSWeakRefKeepDuringJob) {
FLAG_harmony_weak_refs = true;
CcTest::InitializeVM();
LocalContext context;
Isolate* isolate = CcTest::i_isolate();
Heap* heap = isolate->heap();
HandleScope outer_scope(isolate);
Handle<JSWeakRef> weak_ref;
{
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(isolate, js_object);
heap->AddKeepDuringJobTarget(js_object);
weak_ref = inner_scope.CloseAndEscape(inner_weak_ref);
}
CHECK(!weak_ref->target()->IsUndefined(isolate));
CcTest::CollectAllGarbage();
CHECK(!weak_ref->target()->IsUndefined(isolate));
// Clears the KeepDuringJob set.
isolate->RunMicrotasks();
CcTest::CollectAllGarbage();
CHECK(weak_ref->target()->IsUndefined(isolate));
}
TEST(TestJSWeakRefKeepDuringJobIncrementalMarking) {
FLAG_harmony_weak_refs = true;
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);
Handle<JSWeakRef> weak_ref;
{
HandleScope inner_scope(isolate);
Handle<JSObject> js_object =
isolate->factory()->NewJSObject(isolate->object_function());
Handle<JSWeakRef> inner_weak_ref = ConstructJSWeakRef(isolate, js_object);
heap->AddKeepDuringJobTarget(js_object);
weak_ref = inner_scope.CloseAndEscape(inner_weak_ref);
}
CHECK(!weak_ref->target()->IsUndefined(isolate));
heap::SimulateIncrementalMarking(heap, true);
CcTest::CollectAllGarbage();
CHECK(!weak_ref->target()->IsUndefined(isolate));
// Clears the KeepDuringJob set.
isolate->RunMicrotasks();
heap::SimulateIncrementalMarking(heap, true);
CcTest::CollectAllGarbage();
CHECK(weak_ref->target()->IsUndefined(isolate));
}
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
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