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