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 impl->erase(it); | |
3483 return it->second; | |
3484 } | |
3485 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value, | |
3486 Impl* impl, K key) {} | |
3487 static WeakCallbackDataType* WeakCallbackParameter( | |
3488 Impl* impl, const K& key, Local<V> value) { | |
3489 WeakCallbackDataType* data = new WeakCallbackDataType; | |
3490 data->impl = impl; | |
3491 data->key = key; | |
3492 return data; | |
3493 } | |
3494 static Impl* ImplFromWeakCallbackData( | |
3495 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | |
3496 return data.GetParameter()->impl; | |
3497 } | |
3498 static K KeyFromWeakCallbackData( | |
3499 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { | |
3500 return data.GetParameter()->key; | |
3501 } | |
3502 static void DisposeCallbackData(WeakCallbackDataType* data) { | |
3503 delete data; | |
3504 } | |
3505 }; | |
3506 | |
3507 | |
3508 template<bool is_weak> | |
3509 static void TestPersistentValueMap() { | |
3510 LocalContext env; | |
3511 v8::Isolate* isolate = env->GetIsolate(); | |
3512 typedef v8::PersistentValueMap<int, v8::Object, | |
3513 StdPersistentValueMapTraits<int, v8::Object, is_weak> > Map; | |
3514 Map map(isolate); | |
3515 v8::internal::GlobalHandles* global_handles = | |
3516 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); | |
3517 int initial_handle_count = global_handles->global_handles_count(); | |
3518 CHECK_EQ(0, static_cast<int>(map.Size())); | |
3519 { | |
3520 HandleScope scope(isolate); | |
3521 Local<v8::Object> obj = map.Get(7); | |
3522 CHECK(obj.IsEmpty()); | |
3523 Local<v8::Object> expected = v8::Object::New(isolate); | |
3524 map.Set(7, expected); | |
3525 CHECK_EQ(1, static_cast<int>(map.Size())); | |
3526 obj = map.Get(7); | |
3527 CHECK_EQ(expected, obj); | |
3528 v8::UniquePersistent<v8::Object> removed = map.Remove(7); | |
3529 CHECK_EQ(0, static_cast<int>(map.Size())); | |
3530 CHECK(expected == removed); | |
3531 removed = map.Remove(7); | |
3532 CHECK(removed.IsEmpty()); | |
3533 map.Set(8, expected); | |
3534 CHECK_EQ(1, static_cast<int>(map.Size())); | |
3535 map.Set(8, expected); | |
3536 CHECK_EQ(1, static_cast<int>(map.Size())); | |
3537 } | |
3538 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | |
3539 if (is_weak) { | |
3540 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()-> | |
3541 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); | |
3542 } else { | |
3543 map.Clear(); | |
3544 } | |
3545 CHECK_EQ(0, static_cast<int>(map.Size())); | |
3546 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | |
3547 } | |
3548 | |
3549 | |
3550 TEST(PersistentValueMap) { | |
3551 TestPersistentValueMap<false>(); | |
3552 TestPersistentValueMap<true>(); | |
3553 } | |
3554 | |
3555 | |
3556 THREADED_TEST(GlobalHandleUpcast) { | 3447 THREADED_TEST(GlobalHandleUpcast) { |
3557 v8::Isolate* isolate = CcTest::isolate(); | 3448 v8::Isolate* isolate = CcTest::isolate(); |
3558 v8::HandleScope scope(isolate); | 3449 v8::HandleScope scope(isolate); |
3559 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); | 3450 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); |
3560 v8::Persistent<String> global_string(isolate, local); | 3451 v8::Persistent<String> global_string(isolate, local); |
3561 v8::Persistent<Value>& global_value = | 3452 v8::Persistent<Value>& global_value = |
3562 v8::Persistent<Value>::Cast(global_string); | 3453 v8::Persistent<Value>::Cast(global_string); |
3563 CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString()); | 3454 CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString()); |
3564 CHECK(global_string == v8::Persistent<String>::Cast(global_value)); | 3455 CHECK(global_string == v8::Persistent<String>::Cast(global_value)); |
3565 global_string.Reset(); | 3456 global_string.Reset(); |
(...skipping 18740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22306 CompileRun("x1 = x2 = 0;"); | 22197 CompileRun("x1 = x2 = 0;"); |
22307 r = v8::Promise::New(isolate); | 22198 r = v8::Promise::New(isolate); |
22308 r->Catch(f1)->Chain(f2); | 22199 r->Catch(f1)->Chain(f2); |
22309 r->Reject(v8::Integer::New(isolate, 3)); | 22200 r->Reject(v8::Integer::New(isolate, 3)); |
22310 CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); | 22201 CHECK_EQ(0, global->Get(v8_str("x1"))->Int32Value()); |
22311 CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); | 22202 CHECK_EQ(0, global->Get(v8_str("x2"))->Int32Value()); |
22312 V8::RunMicrotasks(isolate); | 22203 V8::RunMicrotasks(isolate); |
22313 CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); | 22204 CHECK_EQ(3, global->Get(v8_str("x1"))->Int32Value()); |
22314 CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); | 22205 CHECK_EQ(4, global->Get(v8_str("x2"))->Int32Value()); |
22315 } | 22206 } |
OLD | NEW |