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 WeakStdMapTraits : public v8::StdMapTraits<K, V> { |
3450 public: | 3450 public: |
3451 static const bool kIsWeak = is_weak; | 3451 typedef typename v8::DefaultPersistentValueMapTraits<K, V>::Impl Impl; |
3452 typedef v8::PersistentContainerValue VInt; | 3452 static const bool kIsWeak = true; |
3453 typedef std::map<K, VInt> Impl; | |
3454 struct WeakCallbackDataType { | 3453 struct WeakCallbackDataType { |
3455 Impl* impl; | 3454 Impl* impl; |
3456 K key; | 3455 K key; |
3457 }; | 3456 }; |
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, | |
3488 Impl* impl, K key) {} | |
3489 static WeakCallbackDataType* WeakCallbackParameter( | 3457 static WeakCallbackDataType* WeakCallbackParameter( |
3490 Impl* impl, const K& key, Local<V> value) { | 3458 Impl* impl, const K& key, Local<V> value) { |
3491 WeakCallbackDataType* data = new WeakCallbackDataType; | 3459 WeakCallbackDataType* data = new WeakCallbackDataType; |
3492 data->impl = impl; | 3460 data->impl = impl; |
3493 data->key = key; | 3461 data->key = key; |
3494 return data; | 3462 return data; |
3495 } | 3463 } |
3496 static Impl* ImplFromWeakCallbackData( | 3464 static Impl* ImplFromWeakCallbackData( |
3497 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | 3465 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { |
3498 return data.GetParameter()->impl; | 3466 return data.GetParameter()->impl; |
3499 } | 3467 } |
3500 static K KeyFromWeakCallbackData( | 3468 static K KeyFromWeakCallbackData( |
3501 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | 3469 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { |
3502 return data.GetParameter()->key; | 3470 return data.GetParameter()->key; |
3503 } | 3471 } |
3504 static void DisposeCallbackData(WeakCallbackDataType* data) { | 3472 static void DisposeCallbackData(WeakCallbackDataType* data) { |
3505 delete data; | 3473 delete data; |
3506 } | 3474 } |
| 3475 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value, |
| 3476 Impl* impl, K key) { } |
3507 }; | 3477 }; |
3508 | 3478 |
3509 | 3479 |
3510 template<bool is_weak> | 3480 template<typename Map> |
3511 static void TestPersistentValueMap() { | 3481 static void TestPersistentValueMap() { |
3512 LocalContext env; | 3482 LocalContext env; |
3513 v8::Isolate* isolate = env->GetIsolate(); | 3483 v8::Isolate* isolate = env->GetIsolate(); |
3514 typedef v8::PersistentValueMap<int, v8::Object, | |
3515 StdPersistentValueMapTraits<int, v8::Object, is_weak> > Map; | |
3516 Map map(isolate); | 3484 Map map(isolate); |
3517 v8::internal::GlobalHandles* global_handles = | 3485 v8::internal::GlobalHandles* global_handles = |
3518 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); | 3486 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
3519 int initial_handle_count = global_handles->global_handles_count(); | 3487 int initial_handle_count = global_handles->global_handles_count(); |
3520 CHECK_EQ(0, static_cast<int>(map.Size())); | 3488 CHECK_EQ(0, static_cast<int>(map.Size())); |
3521 { | 3489 { |
3522 HandleScope scope(isolate); | 3490 HandleScope scope(isolate); |
3523 Local<v8::Object> obj = map.Get(7); | 3491 Local<v8::Object> obj = map.Get(7); |
3524 CHECK(obj.IsEmpty()); | 3492 CHECK(obj.IsEmpty()); |
3525 Local<v8::Object> expected = v8::Object::New(isolate); | 3493 Local<v8::Object> expected = v8::Object::New(isolate); |
3526 map.Set(7, expected); | 3494 map.Set(7, expected); |
3527 CHECK_EQ(1, static_cast<int>(map.Size())); | 3495 CHECK_EQ(1, static_cast<int>(map.Size())); |
3528 obj = map.Get(7); | 3496 obj = map.Get(7); |
3529 CHECK_EQ(expected, obj); | 3497 CHECK_EQ(expected, obj); |
3530 v8::UniquePersistent<v8::Object> removed = map.Remove(7); | 3498 v8::UniquePersistent<v8::Object> removed = map.Remove(7); |
3531 CHECK_EQ(0, static_cast<int>(map.Size())); | 3499 CHECK_EQ(0, static_cast<int>(map.Size())); |
3532 CHECK(expected == removed); | 3500 CHECK(expected == removed); |
3533 removed = map.Remove(7); | 3501 removed = map.Remove(7); |
3534 CHECK(removed.IsEmpty()); | 3502 CHECK(removed.IsEmpty()); |
3535 map.Set(8, expected); | 3503 map.Set(8, expected); |
3536 CHECK_EQ(1, static_cast<int>(map.Size())); | 3504 CHECK_EQ(1, static_cast<int>(map.Size())); |
3537 map.Set(8, expected); | 3505 map.Set(8, expected); |
3538 CHECK_EQ(1, static_cast<int>(map.Size())); | 3506 CHECK_EQ(1, static_cast<int>(map.Size())); |
3539 } | 3507 } |
3540 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | 3508 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
3541 if (is_weak) { | 3509 if (map.IsWeak()) { |
3542 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()-> | 3510 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()-> |
3543 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); | 3511 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
3544 } else { | 3512 } else { |
3545 map.Clear(); | 3513 map.Clear(); |
3546 } | 3514 } |
3547 CHECK_EQ(0, static_cast<int>(map.Size())); | 3515 CHECK_EQ(0, static_cast<int>(map.Size())); |
3548 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3516 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
3549 } | 3517 } |
3550 | 3518 |
3551 | 3519 |
3552 TEST(PersistentValueMap) { | 3520 TEST(PersistentValueMap) { |
3553 TestPersistentValueMap<false>(); | 3521 // Default case, w/o weak callbacks: |
3554 TestPersistentValueMap<true>(); | 3522 TestPersistentValueMap<v8::StdPersistentValueMap<int, v8::Object> >(); |
| 3523 |
| 3524 // Custom traits with weak callbacks: |
| 3525 typedef v8::StdPersistentValueMap<int, v8::Object, |
| 3526 WeakStdMapTraits<int, v8::Object> > WeakPersistentValueMap; |
| 3527 TestPersistentValueMap<WeakPersistentValueMap>(); |
3555 } | 3528 } |
3556 | 3529 |
3557 | 3530 |
3558 THREADED_TEST(GlobalHandleUpcast) { | 3531 THREADED_TEST(GlobalHandleUpcast) { |
3559 v8::Isolate* isolate = CcTest::isolate(); | 3532 v8::Isolate* isolate = CcTest::isolate(); |
3560 v8::HandleScope scope(isolate); | 3533 v8::HandleScope scope(isolate); |
3561 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); | 3534 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); |
3562 v8::Persistent<String> global_string(isolate, local); | 3535 v8::Persistent<String> global_string(isolate, local); |
3563 v8::Persistent<Value>& global_value = | 3536 v8::Persistent<Value>& global_value = |
3564 v8::Persistent<Value>::Cast(global_string); | 3537 v8::Persistent<Value>::Cast(global_string); |
(...skipping 18820 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22385 CompileRun("x1 = x2 = 0;"); | 22358 CompileRun("x1 = x2 = 0;"); |
22386 rr = v8::Promise::Resolver::New(isolate); | 22359 rr = v8::Promise::Resolver::New(isolate); |
22387 rr->GetPromise()->Catch(f1)->Chain(f2); | 22360 rr->GetPromise()->Catch(f1)->Chain(f2); |
22388 rr->Reject(v8::Integer::New(isolate, 3)); | 22361 rr->Reject(v8::Integer::New(isolate, 3)); |
22389 CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); | 22362 CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); |
22390 CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); | 22363 CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
22391 V8::RunMicrotasks(isolate); | 22364 V8::RunMicrotasks(isolate); |
22392 CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); | 22365 CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); |
22393 CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); | 22366 CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); |
22394 } | 22367 } |
OLD | NEW |