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 3425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3436 { | 3436 { |
3437 v8::UniquePersistent<String> unique = ReturnUnique(isolate, global); | 3437 v8::UniquePersistent<String> unique = ReturnUnique(isolate, global); |
3438 CHECK(unique == global); | 3438 CHECK(unique == global); |
3439 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | 3439 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
3440 } | 3440 } |
3441 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3441 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
3442 global.Reset(); | 3442 global.Reset(); |
3443 } | 3443 } |
3444 | 3444 |
3445 | 3445 |
| 3446 template<typename K, typename V, bool is_weak> |
| 3447 class StdPersistentValueMapTraits { |
| 3448 public: |
| 3449 static const bool kIsWeak = is_weak; |
| 3450 typedef v8::PersistentContainerValue VInt; |
| 3451 typedef std::map<K, VInt> Impl; |
| 3452 struct WeakCallbackDataType { |
| 3453 Impl* impl; |
| 3454 K key; |
| 3455 }; |
| 3456 typedef typename Impl::iterator Iterator; |
| 3457 static bool Empty(Impl* impl) { return impl->empty(); } |
| 3458 static size_t Size(Impl* impl) { return impl->size(); } |
| 3459 static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT |
| 3460 static Iterator Begin(Impl* impl) { return impl->begin(); } |
| 3461 static Iterator End(Impl* impl) { return impl->end(); } |
| 3462 static VInt Value(Iterator it) { return it->second; } |
| 3463 static VInt Set(Impl* impl, K key, VInt value) { |
| 3464 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value)); |
| 3465 VInt old_value = v8::kPersistentContainerNotFound; |
| 3466 if (!res.second) { |
| 3467 old_value = res.first->second; |
| 3468 res.first->second = value; |
| 3469 } |
| 3470 return old_value; |
| 3471 } |
| 3472 static VInt Get(Impl* impl, K key) { |
| 3473 Iterator it = impl->find(key); |
| 3474 if (it == impl->end()) return v8::kPersistentContainerNotFound; |
| 3475 return it->second; |
| 3476 } |
| 3477 static VInt Remove(Impl* impl, K key) { |
| 3478 Iterator it = impl->find(key); |
| 3479 if (it == impl->end()) return v8::kPersistentContainerNotFound; |
| 3480 impl->erase(it); |
| 3481 return it->second; |
| 3482 } |
| 3483 // TODO(dcarney): DisposeCallback is badly named. |
| 3484 static void DisposeCallback(Impl* impl, v8::UniquePersistent<V> value) {} |
| 3485 static WeakCallbackDataType* WeakCallbackParameter( |
| 3486 Impl* impl, const K& key, Local<V> value) { |
| 3487 WeakCallbackDataType* data = new WeakCallbackDataType; |
| 3488 data->impl = impl; |
| 3489 data->key = key; |
| 3490 return data; |
| 3491 } |
| 3492 static Impl* ImplFromWeakCallbackData( |
| 3493 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { |
| 3494 return data.GetParameter()->impl; |
| 3495 } |
| 3496 static K KeyFromWeakCallbackData( |
| 3497 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) { |
| 3498 return data.GetParameter()->key; |
| 3499 } |
| 3500 static void DisposeCallbackData(WeakCallbackDataType* data) { |
| 3501 delete data; |
| 3502 } |
| 3503 }; |
| 3504 |
| 3505 |
| 3506 template<bool is_weak> |
| 3507 static void TestPersistentValueMap() { |
| 3508 LocalContext env; |
| 3509 v8::Isolate* isolate = env->GetIsolate(); |
| 3510 typedef v8::PersistentValueMap<int, v8::Object, |
| 3511 StdPersistentValueMapTraits<int, v8::Object, is_weak> > Map; |
| 3512 Map map(isolate); |
| 3513 v8::internal::GlobalHandles* global_handles = |
| 3514 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
| 3515 int initial_handle_count = global_handles->global_handles_count(); |
| 3516 CHECK_EQ(0, map.Size()); |
| 3517 { |
| 3518 HandleScope scope(isolate); |
| 3519 Local<v8::Object> obj = map.Get(7); |
| 3520 CHECK(obj.IsEmpty()); |
| 3521 Local<v8::Object> expected = v8::Object::New(isolate); |
| 3522 map.Set(7, expected); |
| 3523 CHECK_EQ(1, map.Size()); |
| 3524 obj = map.Get(7); |
| 3525 CHECK_EQ(expected, obj); |
| 3526 v8::UniquePersistent<v8::Object> removed = map.Remove(7); |
| 3527 CHECK_EQ(0, map.Size()); |
| 3528 CHECK(expected == removed); |
| 3529 removed = map.Remove(7); |
| 3530 CHECK(removed.IsEmpty()); |
| 3531 map.Set(8, expected); |
| 3532 CHECK_EQ(1, map.Size()); |
| 3533 map.Set(8, expected); |
| 3534 CHECK_EQ(1, map.Size()); |
| 3535 } |
| 3536 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
| 3537 if (is_weak) { |
| 3538 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()-> |
| 3539 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3540 } else { |
| 3541 map.Clear(); |
| 3542 } |
| 3543 CHECK_EQ(0, map.Size()); |
| 3544 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
| 3545 } |
| 3546 |
| 3547 |
| 3548 TEST(PersistentValueMap) { |
| 3549 TestPersistentValueMap<false>(); |
| 3550 TestPersistentValueMap<true>(); |
| 3551 } |
| 3552 |
| 3553 |
3446 THREADED_TEST(GlobalHandleUpcast) { | 3554 THREADED_TEST(GlobalHandleUpcast) { |
3447 v8::Isolate* isolate = CcTest::isolate(); | 3555 v8::Isolate* isolate = CcTest::isolate(); |
3448 v8::HandleScope scope(isolate); | 3556 v8::HandleScope scope(isolate); |
3449 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); | 3557 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); |
3450 v8::Persistent<String> global_string(isolate, local); | 3558 v8::Persistent<String> global_string(isolate, local); |
3451 v8::Persistent<Value>& global_value = | 3559 v8::Persistent<Value>& global_value = |
3452 v8::Persistent<Value>::Cast(global_string); | 3560 v8::Persistent<Value>::Cast(global_string); |
3453 CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString()); | 3561 CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString()); |
3454 CHECK(global_string == v8::Persistent<String>::Cast(global_value)); | 3562 CHECK(global_string == v8::Persistent<String>::Cast(global_value)); |
3455 global_string.Reset(); | 3563 global_string.Reset(); |
(...skipping 18653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22109 Local<Object> ApiCallOptimizationChecker::holder; | 22217 Local<Object> ApiCallOptimizationChecker::holder; |
22110 Local<Object> ApiCallOptimizationChecker::callee; | 22218 Local<Object> ApiCallOptimizationChecker::callee; |
22111 int ApiCallOptimizationChecker::count = 0; | 22219 int ApiCallOptimizationChecker::count = 0; |
22112 | 22220 |
22113 | 22221 |
22114 TEST(TestFunctionCallOptimization) { | 22222 TEST(TestFunctionCallOptimization) { |
22115 i::FLAG_allow_natives_syntax = true; | 22223 i::FLAG_allow_natives_syntax = true; |
22116 ApiCallOptimizationChecker checker; | 22224 ApiCallOptimizationChecker checker; |
22117 checker.RunAll(); | 22225 checker.RunAll(); |
22118 } | 22226 } |
OLD | NEW |