OLD | NEW |
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 3427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3438 { | 3438 { |
3439 v8::UniquePersistent<String> unique = ReturnUnique(isolate, global); | 3439 v8::UniquePersistent<String> unique = ReturnUnique(isolate, global); |
3440 CHECK(unique == global); | 3440 CHECK(unique == global); |
3441 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | 3441 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
3442 } | 3442 } |
3443 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3443 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
3444 global.Reset(); | 3444 global.Reset(); |
3445 } | 3445 } |
3446 | 3446 |
3447 | 3447 |
3448 template<typename K, typename V, bool is_weak> | 3448 template<typename K, typename V> |
3449 class StdPersistentValueMapTraits { | 3449 class StrongSTLMapTraits |
| 3450 : public v8::persistent_value_map_traits::WeakSTLMapTraits<K, V> { |
3450 public: | 3451 public: |
3451 static const bool kIsWeak = is_weak; | 3452 typedef typename |
3452 typedef v8::PersistentContainerValue VInt; | 3453 v8::persistent_value_map_traits::WeakSTLMapTraits<K, V>::Impl Impl; |
3453 typedef std::map<K, VInt> Impl; | 3454 static const bool kIsWeak = true; |
3454 struct WeakCallbackDataType { | 3455 struct WeakCallbackDataType { |
3455 Impl* impl; | 3456 Impl* impl; |
3456 K key; | 3457 K key; |
3457 }; | 3458 }; |
3458 typedef typename Impl::iterator Iterator; | |
3459 static bool Empty(Impl* impl) { return impl->empty(); } | |
3460 static size_t Size(Impl* impl) { return impl->size(); } | |
3461 static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT | |
3462 static Iterator Begin(Impl* impl) { return impl->begin(); } | |
3463 static Iterator End(Impl* impl) { return impl->end(); } | |
3464 static K Key(Iterator it) { return it->first; } | |
3465 static VInt Value(Iterator it) { return it->second; } | |
3466 static VInt Set(Impl* impl, K key, VInt value) { | |
3467 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value)); | |
3468 VInt old_value = v8::kPersistentContainerNotFound; | |
3469 if (!res.second) { | |
3470 old_value = res.first->second; | |
3471 res.first->second = value; | |
3472 } | |
3473 return old_value; | |
3474 } | |
3475 static VInt Get(Impl* impl, K key) { | |
3476 Iterator it = impl->find(key); | |
3477 if (it == impl->end()) return v8::kPersistentContainerNotFound; | |
3478 return it->second; | |
3479 } | |
3480 static VInt Remove(Impl* impl, K key) { | |
3481 Iterator it = impl->find(key); | |
3482 if (it == impl->end()) return v8::kPersistentContainerNotFound; | |
3483 VInt value = it->second; | |
3484 impl->erase(it); | |
3485 return value; | |
3486 } | |
3487 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value, | 3459 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value, |
3488 Impl* impl, K key) {} | 3460 Impl* impl, K key) {} |
3489 static WeakCallbackDataType* WeakCallbackParameter( | 3461 static WeakCallbackDataType* WeakCallbackParameter( |
3490 Impl* impl, const K& key, Local<V> value) { | 3462 Impl* impl, const K& key, Local<V> value) { |
3491 WeakCallbackDataType* data = new WeakCallbackDataType; | 3463 WeakCallbackDataType* data = new WeakCallbackDataType; |
3492 data->impl = impl; | 3464 data->impl = impl; |
3493 data->key = key; | 3465 data->key = key; |
3494 return data; | 3466 return data; |
3495 } | 3467 } |
3496 static Impl* ImplFromWeakCallbackData( | 3468 static Impl* ImplFromWeakCallbackData( |
3497 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | 3469 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { |
3498 return data.GetParameter()->impl; | 3470 return data.GetParameter()->impl; |
3499 } | 3471 } |
3500 static K KeyFromWeakCallbackData( | 3472 static K KeyFromWeakCallbackData( |
3501 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | 3473 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { |
3502 return data.GetParameter()->key; | 3474 return data.GetParameter()->key; |
3503 } | 3475 } |
3504 static void DisposeCallbackData(WeakCallbackDataType* data) { | 3476 static void DisposeCallbackData(WeakCallbackDataType* data) { |
3505 delete data; | 3477 delete data; |
3506 } | 3478 } |
3507 }; | 3479 }; |
3508 | 3480 |
3509 | 3481 |
3510 template<bool is_weak> | 3482 template<typename Traits> |
3511 static void TestPersistentValueMap() { | 3483 static void TestPersistentValueMap() { |
3512 LocalContext env; | 3484 LocalContext env; |
3513 v8::Isolate* isolate = env->GetIsolate(); | 3485 v8::Isolate* isolate = env->GetIsolate(); |
3514 typedef v8::PersistentValueMap<int, v8::Object, | 3486 typedef v8::PersistentValueMap<int, v8::Object, Traits> Map; |
3515 StdPersistentValueMapTraits<int, v8::Object, is_weak> > Map; | |
3516 Map map(isolate); | 3487 Map map(isolate); |
3517 v8::internal::GlobalHandles* global_handles = | 3488 v8::internal::GlobalHandles* global_handles = |
3518 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); | 3489 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
3519 int initial_handle_count = global_handles->global_handles_count(); | 3490 int initial_handle_count = global_handles->global_handles_count(); |
3520 CHECK_EQ(0, static_cast<int>(map.Size())); | 3491 CHECK_EQ(0, static_cast<int>(map.Size())); |
3521 { | 3492 { |
3522 HandleScope scope(isolate); | 3493 HandleScope scope(isolate); |
3523 Local<v8::Object> obj = map.Get(7); | 3494 Local<v8::Object> obj = map.Get(7); |
3524 CHECK(obj.IsEmpty()); | 3495 CHECK(obj.IsEmpty()); |
3525 Local<v8::Object> expected = v8::Object::New(isolate); | 3496 Local<v8::Object> expected = v8::Object::New(isolate); |
3526 map.Set(7, expected); | 3497 map.Set(7, expected); |
3527 CHECK_EQ(1, static_cast<int>(map.Size())); | 3498 CHECK_EQ(1, static_cast<int>(map.Size())); |
3528 obj = map.Get(7); | 3499 obj = map.Get(7); |
3529 CHECK_EQ(expected, obj); | 3500 CHECK_EQ(expected, obj); |
3530 v8::UniquePersistent<v8::Object> removed = map.Remove(7); | 3501 v8::UniquePersistent<v8::Object> removed = map.Remove(7); |
3531 CHECK_EQ(0, static_cast<int>(map.Size())); | 3502 CHECK_EQ(0, static_cast<int>(map.Size())); |
3532 CHECK(expected == removed); | 3503 CHECK(expected == removed); |
3533 removed = map.Remove(7); | 3504 removed = map.Remove(7); |
3534 CHECK(removed.IsEmpty()); | 3505 CHECK(removed.IsEmpty()); |
3535 map.Set(8, expected); | 3506 map.Set(8, expected); |
3536 CHECK_EQ(1, static_cast<int>(map.Size())); | 3507 CHECK_EQ(1, static_cast<int>(map.Size())); |
3537 map.Set(8, expected); | 3508 map.Set(8, expected); |
3538 CHECK_EQ(1, static_cast<int>(map.Size())); | 3509 CHECK_EQ(1, static_cast<int>(map.Size())); |
3539 } | 3510 } |
3540 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | 3511 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
3541 if (is_weak) { | 3512 if (Traits::kIsWeak) { |
3542 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()-> | 3513 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()-> |
3543 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); | 3514 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
3544 } else { | 3515 } else { |
3545 map.Clear(); | 3516 map.Clear(); |
3546 } | 3517 } |
3547 CHECK_EQ(0, static_cast<int>(map.Size())); | 3518 CHECK_EQ(0, static_cast<int>(map.Size())); |
3548 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3519 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
3549 } | 3520 } |
3550 | 3521 |
3551 | 3522 |
3552 TEST(PersistentValueMap) { | 3523 TEST(PersistentValueMap) { |
3553 TestPersistentValueMap<false>(); | 3524 typedef v8::persistent_value_map_traits::WeakSTLMapTraits<int, v8::Object> |
3554 TestPersistentValueMap<true>(); | 3525 WeakMap; |
| 3526 typedef StrongSTLMapTraits<int, v8::Object> StrongMap; |
| 3527 |
| 3528 TestPersistentValueMap<WeakMap>(); |
| 3529 TestPersistentValueMap<StrongMap>(); |
3555 } | 3530 } |
3556 | 3531 |
3557 | 3532 |
3558 THREADED_TEST(GlobalHandleUpcast) { | 3533 THREADED_TEST(GlobalHandleUpcast) { |
3559 v8::Isolate* isolate = CcTest::isolate(); | 3534 v8::Isolate* isolate = CcTest::isolate(); |
3560 v8::HandleScope scope(isolate); | 3535 v8::HandleScope scope(isolate); |
3561 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); | 3536 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); |
3562 v8::Persistent<String> global_string(isolate, local); | 3537 v8::Persistent<String> global_string(isolate, local); |
3563 v8::Persistent<Value>& global_value = | 3538 v8::Persistent<Value>& global_value = |
3564 v8::Persistent<Value>::Cast(global_string); | 3539 v8::Persistent<Value>::Cast(global_string); |
(...skipping 18820 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22385 CompileRun("x1 = x2 = 0;"); | 22360 CompileRun("x1 = x2 = 0;"); |
22386 rr = v8::Promise::Resolver::New(isolate); | 22361 rr = v8::Promise::Resolver::New(isolate); |
22387 rr->GetPromise()->Catch(f1)->Chain(f2); | 22362 rr->GetPromise()->Catch(f1)->Chain(f2); |
22388 rr->Reject(v8::Integer::New(isolate, 3)); | 22363 rr->Reject(v8::Integer::New(isolate, 3)); |
22389 CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); | 22364 CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); |
22390 CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); | 22365 CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
22391 V8::RunMicrotasks(isolate); | 22366 V8::RunMicrotasks(isolate); |
22392 CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); | 22367 CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); |
22393 CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); | 22368 CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); |
22394 } | 22369 } |
OLD | NEW |