Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(801)

Unified Diff: test/cctest/test-api.cc

Issue 886473005: Add WeakMap to v8.h (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Moved constructor&destructor to api.cc Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/runtime/runtime-collections.cc ('k') | test/cctest/test-weakmaps.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index dd5358c6c35f67b386807ee5e314c7038c0d3ab3..08981fea85f59c1b27319b9a8bfea8d5577cd308 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -46,6 +46,7 @@
#include "src/isolate.h"
#include "src/objects.h"
#include "src/parser.h"
+#include "src/smart-pointers.h"
#include "src/snapshot.h"
#include "src/unicode-inl.h"
#include "src/utils.h"
@@ -4737,6 +4738,78 @@ TEST(MessageHandler5) {
}
+TEST(WeakKeyMap) {
+ v8::Isolate* isolate = CcTest::isolate();
+ i::SmartPointer<v8::WeakKeyMap> weak_map(v8::WeakKeyMap::New(isolate));
+ CHECK(!weak_map.is_empty());
+
+ LocalContext env;
+ HandleScope scope(isolate);
+
+ Local<Object> value = v8::Object::New(isolate);
+
+ Local<Object> local1 = v8::Object::New(isolate);
+ CHECK(!weak_map->Has(local1));
+ CHECK(weak_map->Get(local1)->IsUndefined());
+ weak_map->Set(local1, value);
+ CHECK(weak_map->Has(local1));
+ CHECK(value->Equals(weak_map->Get(local1)));
+
+ WeakCallCounter counter(1234);
+ WeakCallCounterAndPersistent<Value> o1(&counter);
+ WeakCallCounterAndPersistent<Value> o2(&counter);
+ WeakCallCounterAndPersistent<Value> s1(&counter);
+ {
+ HandleScope scope(isolate);
+ Local<v8::Object> obj1 = v8::Object::New(isolate);
+ Local<v8::Object> obj2 = v8::Object::New(isolate);
+ Local<v8::Symbol> sym1 = v8::Symbol::New(isolate);
+
+ weak_map->Set(obj1, value);
+ weak_map->Set(obj2, value);
+ weak_map->Set(sym1, value);
+
+ o1.handle.Reset(isolate, obj1);
+ o2.handle.Reset(isolate, obj2);
+ s1.handle.Reset(isolate, sym1);
+
+ CHECK(weak_map->Has(local1));
+ CHECK(weak_map->Has(obj1));
+ CHECK(weak_map->Has(obj2));
+ CHECK(weak_map->Has(sym1));
+
+ CHECK(value->Equals(weak_map->Get(local1)));
+ CHECK(value->Equals(weak_map->Get(obj1)));
+ CHECK(value->Equals(weak_map->Get(obj2)));
+ CHECK(value->Equals(weak_map->Get(sym1)));
+ }
+ CcTest::heap()->CollectAllGarbage(TestHeap::Heap::kNoGCFlags);
+ {
+ HandleScope scope(isolate);
+ CHECK(value->Equals(weak_map->Get(local1)));
+ CHECK(value->Equals(weak_map->Get(Local<Value>::New(isolate, o1.handle))));
+ CHECK(value->Equals(weak_map->Get(Local<Value>::New(isolate, o2.handle))));
+ CHECK(value->Equals(weak_map->Get(Local<Value>::New(isolate, s1.handle))));
+ }
+
+ o1.handle.SetWeak(&o1, &WeakPointerCallback);
+ o2.handle.SetWeak(&o2, &WeakPointerCallback);
+ s1.handle.SetWeak(&s1, &WeakPointerCallback);
+
+ CcTest::heap()->CollectAllGarbage(TestHeap::Heap::kNoGCFlags);
+ CHECK_EQ(3, counter.NumberOfWeakCalls());
+
+ CHECK(o1.handle.IsEmpty());
+ CHECK(o2.handle.IsEmpty());
+ CHECK(s1.handle.IsEmpty());
+
+ CHECK(value->Equals(weak_map->Get(local1)));
+ CHECK(weak_map->Delete(local1));
+ CHECK(!weak_map->Has(local1));
+ CHECK(weak_map->Get(local1)->IsUndefined());
+}
+
+
THREADED_TEST(GetSetProperty) {
LocalContext context;
v8::Isolate* isolate = context->GetIsolate();
« no previous file with comments | « src/runtime/runtime-collections.cc ('k') | test/cctest/test-weakmaps.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698