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

Side by Side Diff: test/cctest/test-api.cc

Issue 1026283004: fix disposal of phantom handles in GlobalValueMap (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 9 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 unified diff | Download patch
« no previous file with comments | « src/api.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3175 matching lines...) Expand 10 before | Expand all | Expand 10 after
3186 TestPersistentValueMap<v8::StdPersistentValueMap<int, v8::Object>>(); 3186 TestPersistentValueMap<v8::StdPersistentValueMap<int, v8::Object>>();
3187 3187
3188 // Custom traits with weak callbacks: 3188 // Custom traits with weak callbacks:
3189 typedef v8::PersistentValueMap<int, v8::Object, 3189 typedef v8::PersistentValueMap<int, v8::Object,
3190 WeakStdMapTraits<int, v8::Object>> 3190 WeakStdMapTraits<int, v8::Object>>
3191 WeakPersistentValueMap; 3191 WeakPersistentValueMap;
3192 TestPersistentValueMap<WeakPersistentValueMap>(); 3192 TestPersistentValueMap<WeakPersistentValueMap>();
3193 } 3193 }
3194 3194
3195 3195
3196 namespace {
3197
3198 void* IntKeyToVoidPointer(int key) { return reinterpret_cast<void*>(key << 1); }
3199
3200
3201 Local<v8::Object> NewObjectForIntKey(
3202 v8::Isolate* isolate, const v8::Global<v8::ObjectTemplate>& templ,
3203 int key) {
3204 auto local = Local<v8::ObjectTemplate>::New(isolate, templ);
3205 auto obj = local->NewInstance();
3206 obj->SetAlignedPointerInInternalField(0, IntKeyToVoidPointer(key));
3207 return obj;
3208 }
3209
3210
3211 template <typename K, typename V>
3212 class PhantomStdMapTraits : public v8::StdMapTraits<K, V> {
3213 public:
3214 typedef typename v8::GlobalValueMap<K, V, PhantomStdMapTraits<K, V>> MapType;
3215 static const v8::PersistentContainerCallbackType kCallbackType =
3216 v8::kWeakWithInternalFields;
3217 struct WeakCallbackDataType {
3218 MapType* map;
3219 K key;
3220 };
3221 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, const K& key,
3222 Local<V> value) {
3223 WeakCallbackDataType* data = new WeakCallbackDataType;
3224 data->map = map;
3225 data->key = key;
3226 return data;
3227 }
3228 static MapType* MapFromWeakCallbackInfo(
3229 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
3230 return data.GetParameter()->map;
3231 }
3232 static K KeyFromWeakCallbackInfo(
3233 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
3234 return data.GetParameter()->key;
3235 }
3236 static void DisposeCallbackData(WeakCallbackDataType* data) { delete data; }
3237 static void Dispose(v8::Isolate* isolate, v8::Global<V> value, K key) {
3238 CHECK_EQ(IntKeyToVoidPointer(key),
3239 v8::Object::GetAlignedPointerFromInternalField(value, 0));
3240 }
3241 static void DisposeWeak(
3242 v8::Isolate* isolate,
3243 const v8::WeakCallbackInfo<WeakCallbackDataType>& info, K key) {
3244 CHECK_EQ(IntKeyToVoidPointer(key), info.GetInternalField(0));
3245 }
3246 };
3247 }
3248
3249
3250 TEST(GlobalValueMap) {
3251 typedef v8::GlobalValueMap<int, v8::Object,
3252 PhantomStdMapTraits<int, v8::Object>> Map;
3253 LocalContext env;
3254 v8::Isolate* isolate = env->GetIsolate();
3255 v8::Global<ObjectTemplate> templ;
3256 {
3257 HandleScope scope(isolate);
3258 auto t = ObjectTemplate::New(isolate);
3259 t->SetInternalFieldCount(1);
3260 templ.Reset(isolate, t);
3261 }
3262 Map map(isolate);
3263 v8::internal::GlobalHandles* global_handles =
3264 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles();
3265 int initial_handle_count = global_handles->global_handles_count();
3266 CHECK_EQ(0, static_cast<int>(map.Size()));
3267 {
3268 HandleScope scope(isolate);
3269 Local<v8::Object> obj = map.Get(7);
3270 CHECK(obj.IsEmpty());
3271 Local<v8::Object> expected = v8::Object::New(isolate);
3272 map.Set(7, expected);
3273 CHECK_EQ(1, static_cast<int>(map.Size()));
3274 obj = map.Get(7);
3275 CHECK(expected->Equals(obj));
3276 {
3277 Map::PersistentValueReference ref = map.GetReference(7);
3278 CHECK(expected->Equals(ref.NewLocal(isolate)));
3279 }
3280 v8::Global<v8::Object> removed = map.Remove(7);
3281 CHECK_EQ(0, static_cast<int>(map.Size()));
3282 CHECK(expected == removed);
3283 removed = map.Remove(7);
3284 CHECK(removed.IsEmpty());
3285 map.Set(8, expected);
3286 CHECK_EQ(1, static_cast<int>(map.Size()));
3287 map.Set(8, expected);
3288 CHECK_EQ(1, static_cast<int>(map.Size()));
3289 {
3290 Map::PersistentValueReference ref;
3291 Local<v8::Object> expected2 = NewObjectForIntKey(isolate, templ, 8);
3292 removed = map.Set(8, v8::Global<v8::Object>(isolate, expected2), &ref);
3293 CHECK_EQ(1, static_cast<int>(map.Size()));
3294 CHECK(expected == removed);
3295 CHECK(expected2->Equals(ref.NewLocal(isolate)));
3296 }
3297 }
3298 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count());
3299 CcTest::i_isolate()->heap()->CollectAllGarbage(
3300 i::Heap::kAbortIncrementalMarkingMask);
3301 CHECK_EQ(0, static_cast<int>(map.Size()));
3302 CHECK_EQ(initial_handle_count, global_handles->global_handles_count());
3303 {
3304 HandleScope scope(isolate);
3305 Local<v8::Object> value = NewObjectForIntKey(isolate, templ, 9);
3306 map.Set(9, value);
3307 map.Clear();
3308 }
3309 CHECK_EQ(0, static_cast<int>(map.Size()));
3310 CHECK_EQ(initial_handle_count, global_handles->global_handles_count());
3311 }
3312
3313
3196 TEST(PersistentValueVector) { 3314 TEST(PersistentValueVector) {
3197 LocalContext env; 3315 LocalContext env;
3198 v8::Isolate* isolate = env->GetIsolate(); 3316 v8::Isolate* isolate = env->GetIsolate();
3199 v8::internal::GlobalHandles* global_handles = 3317 v8::internal::GlobalHandles* global_handles =
3200 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); 3318 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles();
3201 int handle_count = global_handles->global_handles_count(); 3319 int handle_count = global_handles->global_handles_count();
3202 HandleScope scope(isolate); 3320 HandleScope scope(isolate);
3203 3321
3204 v8::PersistentValueVector<v8::Object> vector(isolate); 3322 v8::PersistentValueVector<v8::Object> vector(isolate);
3205 3323
(...skipping 18428 matching lines...) Expand 10 before | Expand all | Expand 10 after
21634 } 21752 }
21635 { 21753 {
21636 v8::TryCatch try_catch; 21754 v8::TryCatch try_catch;
21637 uint16_t* data = reinterpret_cast<uint16_t*>(buffer); 21755 uint16_t* data = reinterpret_cast<uint16_t*>(buffer);
21638 CHECK(v8::String::NewFromTwoByte(isolate, data, v8::String::kNormalString, 21756 CHECK(v8::String::NewFromTwoByte(isolate, data, v8::String::kNormalString,
21639 length).IsEmpty()); 21757 length).IsEmpty());
21640 CHECK(!try_catch.HasCaught()); 21758 CHECK(!try_catch.HasCaught());
21641 } 21759 }
21642 free(buffer); 21760 free(buffer);
21643 } 21761 }
OLDNEW
« no previous file with comments | « src/api.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698