Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: test/cctest/test-api.cc

Issue 189463019: Implement PersistentValueMap, a map that stores UniquePersistent values. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fix test case. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/global-handles.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 K Key(Iterator it) { return it->first; }
3463 static VInt Value(Iterator it) { return it->second; }
3464 static VInt Set(Impl* impl, K key, VInt value) {
3465 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
3466 VInt old_value = v8::kPersistentContainerNotFound;
3467 if (!res.second) {
3468 old_value = res.first->second;
3469 res.first->second = value;
3470 }
3471 return old_value;
3472 }
3473 static VInt Get(Impl* impl, K key) {
3474 Iterator it = impl->find(key);
3475 if (it == impl->end()) return v8::kPersistentContainerNotFound;
3476 return it->second;
3477 }
3478 static VInt Remove(Impl* impl, K key) {
3479 Iterator it = impl->find(key);
3480 if (it == impl->end()) return v8::kPersistentContainerNotFound;
3481 impl->erase(it);
3482 return it->second;
3483 }
3484 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<V> value,
3485 Impl* impl, K key) {}
3486 static WeakCallbackDataType* WeakCallbackParameter(
3487 Impl* impl, const K& key, Local<V> value) {
3488 WeakCallbackDataType* data = new WeakCallbackDataType;
3489 data->impl = impl;
3490 data->key = key;
3491 return data;
3492 }
3493 static Impl* ImplFromWeakCallbackData(
3494 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) {
3495 return data.GetParameter()->impl;
3496 }
3497 static K KeyFromWeakCallbackData(
3498 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) {
3499 return data.GetParameter()->key;
3500 }
3501 static void DisposeCallbackData(WeakCallbackDataType* data) {
3502 delete data;
3503 }
3504 };
3505
3506
3507 template<bool is_weak>
3508 static void TestPersistentValueMap() {
3509 LocalContext env;
3510 v8::Isolate* isolate = env->GetIsolate();
3511 typedef v8::PersistentValueMap<int, v8::Object,
3512 StdPersistentValueMapTraits<int, v8::Object, is_weak> > Map;
3513 Map map(isolate);
3514 v8::internal::GlobalHandles* global_handles =
3515 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles();
3516 int initial_handle_count = global_handles->global_handles_count();
3517 CHECK_EQ(0, map.Size());
3518 {
3519 HandleScope scope(isolate);
3520 Local<v8::Object> obj = map.Get(7);
3521 CHECK(obj.IsEmpty());
3522 Local<v8::Object> expected = v8::Object::New(isolate);
3523 map.Set(7, expected);
3524 CHECK_EQ(1, map.Size());
3525 obj = map.Get(7);
3526 CHECK_EQ(expected, obj);
3527 v8::UniquePersistent<v8::Object> removed = map.Remove(7);
3528 CHECK_EQ(0, map.Size());
3529 CHECK(expected == removed);
3530 removed = map.Remove(7);
3531 CHECK(removed.IsEmpty());
3532 map.Set(8, expected);
3533 CHECK_EQ(1, map.Size());
3534 map.Set(8, expected);
3535 CHECK_EQ(1, map.Size());
3536 }
3537 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count());
3538 if (is_weak) {
3539 reinterpret_cast<v8::internal::Isolate*>(isolate)->heap()->
3540 CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask);
3541 } else {
3542 map.Clear();
3543 }
3544 CHECK_EQ(0, map.Size());
3545 CHECK_EQ(initial_handle_count, global_handles->global_handles_count());
3546 }
3547
3548
3549 TEST(PersistentValueMap) {
3550 TestPersistentValueMap<false>();
3551 TestPersistentValueMap<true>();
3552 }
3553
3554
3446 THREADED_TEST(GlobalHandleUpcast) { 3555 THREADED_TEST(GlobalHandleUpcast) {
3447 v8::Isolate* isolate = CcTest::isolate(); 3556 v8::Isolate* isolate = CcTest::isolate();
3448 v8::HandleScope scope(isolate); 3557 v8::HandleScope scope(isolate);
3449 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); 3558 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str"));
3450 v8::Persistent<String> global_string(isolate, local); 3559 v8::Persistent<String> global_string(isolate, local);
3451 v8::Persistent<Value>& global_value = 3560 v8::Persistent<Value>& global_value =
3452 v8::Persistent<Value>::Cast(global_string); 3561 v8::Persistent<Value>::Cast(global_string);
3453 CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString()); 3562 CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString());
3454 CHECK(global_string == v8::Persistent<String>::Cast(global_value)); 3563 CHECK(global_string == v8::Persistent<String>::Cast(global_value));
3455 global_string.Reset(); 3564 global_string.Reset();
(...skipping 18648 matching lines...) Expand 10 before | Expand all | Expand 10 after
22104 Local<Object> ApiCallOptimizationChecker::holder; 22213 Local<Object> ApiCallOptimizationChecker::holder;
22105 Local<Object> ApiCallOptimizationChecker::callee; 22214 Local<Object> ApiCallOptimizationChecker::callee;
22106 int ApiCallOptimizationChecker::count = 0; 22215 int ApiCallOptimizationChecker::count = 0;
22107 22216
22108 22217
22109 TEST(TestFunctionCallOptimization) { 22218 TEST(TestFunctionCallOptimization) {
22110 i::FLAG_allow_natives_syntax = true; 22219 i::FLAG_allow_natives_syntax = true;
22111 ApiCallOptimizationChecker checker; 22220 ApiCallOptimizationChecker checker;
22112 checker.RunAll(); 22221 checker.RunAll();
22113 } 22222 }
OLDNEW
« no previous file with comments | « src/global-handles.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698