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