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 3480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3491 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | 3491 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
3492 } | 3492 } |
3493 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3493 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
3494 global.Reset(); | 3494 global.Reset(); |
3495 } | 3495 } |
3496 | 3496 |
3497 | 3497 |
3498 template<typename K, typename V> | 3498 template<typename K, typename V> |
3499 class WeakStdMapTraits : public v8::StdMapTraits<K, V> { | 3499 class WeakStdMapTraits : public v8::StdMapTraits<K, V> { |
3500 public: | 3500 public: |
3501 typedef typename v8::DefaultPersistentValueMapTraits<K, V>::Impl Impl; | 3501 typedef typename v8::PersistentValueMap<K, V, WeakStdMapTraits<K, V> > |
3502 static const bool kIsWeak = true; | 3502 MapType; |
| 3503 static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak; |
3503 struct WeakCallbackDataType { | 3504 struct WeakCallbackDataType { |
3504 Impl* impl; | 3505 MapType* map; |
3505 K key; | 3506 K key; |
3506 }; | 3507 }; |
3507 static WeakCallbackDataType* WeakCallbackParameter( | 3508 static WeakCallbackDataType* WeakCallbackParameter( |
3508 Impl* impl, const K& key, Local<V> value) { | 3509 MapType* map, const K& key, Local<V> value) { |
3509 WeakCallbackDataType* data = new WeakCallbackDataType; | 3510 WeakCallbackDataType* data = new WeakCallbackDataType; |
3510 data->impl = impl; | 3511 data->map = map; |
3511 data->key = key; | 3512 data->key = key; |
3512 return data; | 3513 return data; |
3513 } | 3514 } |
3514 static Impl* ImplFromWeakCallbackData( | 3515 static MapType* MapFromWeakCallbackData( |
3515 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | 3516 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { |
3516 return data.GetParameter()->impl; | 3517 return data.GetParameter()->map; |
3517 } | 3518 } |
3518 static K KeyFromWeakCallbackData( | 3519 static K KeyFromWeakCallbackData( |
3519 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | 3520 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { |
3520 return data.GetParameter()->key; | 3521 return data.GetParameter()->key; |
3521 } | 3522 } |
3522 static void DisposeCallbackData(WeakCallbackDataType* data) { | 3523 static void DisposeCallbackData(WeakCallbackDataType* data) { |
3523 delete data; | 3524 delete data; |
3524 } | 3525 } |
3525 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value, | 3526 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value, |
3526 Impl* impl, K key) { } | 3527 K key) { } |
3527 }; | 3528 }; |
3528 | 3529 |
3529 | 3530 |
3530 template<typename Map> | 3531 template<typename Map> |
3531 static void TestPersistentValueMap() { | 3532 static void TestPersistentValueMap() { |
3532 LocalContext env; | 3533 LocalContext env; |
3533 v8::Isolate* isolate = env->GetIsolate(); | 3534 v8::Isolate* isolate = env->GetIsolate(); |
3534 Map map(isolate); | 3535 Map map(isolate); |
3535 v8::internal::GlobalHandles* global_handles = | 3536 v8::internal::GlobalHandles* global_handles = |
3536 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); | 3537 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
3537 int initial_handle_count = global_handles->global_handles_count(); | 3538 int initial_handle_count = global_handles->global_handles_count(); |
3538 CHECK_EQ(0, static_cast<int>(map.Size())); | 3539 CHECK_EQ(0, static_cast<int>(map.Size())); |
3539 { | 3540 { |
3540 HandleScope scope(isolate); | 3541 HandleScope scope(isolate); |
3541 Local<v8::Object> obj = map.Get(7); | 3542 Local<v8::Object> obj = map.Get(7); |
3542 CHECK(obj.IsEmpty()); | 3543 CHECK(obj.IsEmpty()); |
3543 Local<v8::Object> expected = v8::Object::New(isolate); | 3544 Local<v8::Object> expected = v8::Object::New(isolate); |
3544 map.Set(7, expected); | 3545 map.Set(7, expected); |
3545 CHECK_EQ(1, static_cast<int>(map.Size())); | 3546 CHECK_EQ(1, static_cast<int>(map.Size())); |
3546 obj = map.Get(7); | 3547 obj = map.Get(7); |
3547 CHECK_EQ(expected, obj); | 3548 CHECK_EQ(expected, obj); |
| 3549 { |
| 3550 typename Map::PersistentValueReference ref = map.GetReference(7); |
| 3551 CHECK_EQ(expected, ref.NewLocal(isolate)); |
| 3552 } |
3548 v8::UniquePersistent<v8::Object> removed = map.Remove(7); | 3553 v8::UniquePersistent<v8::Object> removed = map.Remove(7); |
3549 CHECK_EQ(0, static_cast<int>(map.Size())); | 3554 CHECK_EQ(0, static_cast<int>(map.Size())); |
3550 CHECK(expected == removed); | 3555 CHECK(expected == removed); |
3551 removed = map.Remove(7); | 3556 removed = map.Remove(7); |
3552 CHECK(removed.IsEmpty()); | 3557 CHECK(removed.IsEmpty()); |
3553 map.Set(8, expected); | 3558 map.Set(8, expected); |
3554 CHECK_EQ(1, static_cast<int>(map.Size())); | 3559 CHECK_EQ(1, static_cast<int>(map.Size())); |
3555 map.Set(8, expected); | 3560 map.Set(8, expected); |
3556 CHECK_EQ(1, static_cast<int>(map.Size())); | 3561 CHECK_EQ(1, static_cast<int>(map.Size())); |
| 3562 { |
| 3563 typename Map::PersistentValueReference ref; |
| 3564 Local<v8::Object> expected2 = v8::Object::New(isolate); |
| 3565 removed = map.Set(8, |
| 3566 v8::UniquePersistent<v8::Object>(isolate, expected2), &ref); |
| 3567 CHECK_EQ(1, static_cast<int>(map.Size())); |
| 3568 CHECK(expected == removed); |
| 3569 CHECK_EQ(expected2, ref.NewLocal(isolate)); |
| 3570 } |
3557 } | 3571 } |
3558 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | 3572 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
3559 if (map.IsWeak()) { | 3573 if (map.IsWeak()) { |
3560 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()-> | 3574 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()-> |
3561 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); | 3575 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
3562 } else { | 3576 } else { |
3563 map.Clear(); | 3577 map.Clear(); |
3564 } | 3578 } |
3565 CHECK_EQ(0, static_cast<int>(map.Size())); | 3579 CHECK_EQ(0, static_cast<int>(map.Size())); |
3566 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3580 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
3567 } | 3581 } |
3568 | 3582 |
3569 | 3583 |
3570 TEST(PersistentValueMap) { | 3584 TEST(PersistentValueMap) { |
3571 // Default case, w/o weak callbacks: | 3585 // Default case, w/o weak callbacks: |
3572 TestPersistentValueMap<v8::StdPersistentValueMap<int, v8::Object> >(); | 3586 TestPersistentValueMap<v8::StdPersistentValueMap<int, v8::Object> >(); |
3573 | 3587 |
3574 // Custom traits with weak callbacks: | 3588 // Custom traits with weak callbacks: |
3575 typedef v8::StdPersistentValueMap<int, v8::Object, | 3589 typedef v8::PersistentValueMap<int, v8::Object, |
3576 WeakStdMapTraits<int, v8::Object> > WeakPersistentValueMap; | 3590 WeakStdMapTraits<int, v8::Object> > WeakPersistentValueMap; |
3577 TestPersistentValueMap<WeakPersistentValueMap>(); | 3591 TestPersistentValueMap<WeakPersistentValueMap>(); |
3578 } | 3592 } |
3579 | 3593 |
3580 | 3594 |
3581 THREADED_TEST(GlobalHandleUpcast) { | 3595 THREADED_TEST(GlobalHandleUpcast) { |
3582 v8::Isolate* isolate = CcTest::isolate(); | 3596 v8::Isolate* isolate = CcTest::isolate(); |
3583 v8::HandleScope scope(isolate); | 3597 v8::HandleScope scope(isolate); |
3584 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); | 3598 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); |
3585 v8::Persistent<String> global_string(isolate, local); | 3599 v8::Persistent<String> global_string(isolate, local); |
(...skipping 18772 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22358 "f.call(friend);"); | 22372 "f.call(friend);"); |
22359 CHECK_EQ(2, named_access_count); | 22373 CHECK_EQ(2, named_access_count); |
22360 | 22374 |
22361 // Test access using Object.setPrototypeOf reflective method. | 22375 // Test access using Object.setPrototypeOf reflective method. |
22362 named_access_count = 0; | 22376 named_access_count = 0; |
22363 CompileRun("Object.setPrototypeOf(friend, {});"); | 22377 CompileRun("Object.setPrototypeOf(friend, {});"); |
22364 CHECK_EQ(1, named_access_count); | 22378 CHECK_EQ(1, named_access_count); |
22365 CompileRun("Object.getPrototypeOf(friend);"); | 22379 CompileRun("Object.getPrototypeOf(friend);"); |
22366 CHECK_EQ(2, named_access_count); | 22380 CHECK_EQ(2, named_access_count); |
22367 } | 22381 } |
OLD | NEW |