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

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: Address 2nd round of feedback. 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
« include/v8.h ('K') | « 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 3424 matching lines...) Expand 10 before | Expand all | Expand 10 after
3435 // Test pass from function call 3435 // Test pass from function call
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 template <typename K, typename V, bool is_weak>
3446 class StdPersistentValueMapTraits {
3447 public:
3448 static const bool kIsWeak = is_weak;
3449 typedef v8::PersistentContainerValue VInt;
3450 typedef std::map<K, VInt> Impl;
3451 struct WeakCallbackDataType {
3452 Impl* impl;
3453 K key;
3454 };
3455 typedef typename Impl::iterator Iterator;
3456 static bool Empty(Impl* impl) { return impl->empty(); }
3457 static size_t Size(Impl* impl) { return impl->size(); }
3458 static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT
3459 static Iterator Begin(Impl* impl) { return impl->begin(); }
3460 static Iterator End(Impl* impl) { return impl->end(); }
3461 static VInt Value(Iterator it) { return it->second; }
3462 static VInt Set(Impl* impl, K key, VInt value) {
3463 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
3464 VInt old_value = v8::kPersistentContainerNotFound;
3465 if (!res.second) {
3466 old_value = res.first->second;
3467 res.first->second = value;
3468 }
3469 return old_value;
3470 }
3471 static VInt Get(Impl* impl, K key) {
3472 Iterator it = impl->find(key);
3473 if (it == impl->end()) return v8::kPersistentContainerNotFound;
3474 return it->second;
3475 }
3476 static VInt Remove(Impl* impl, K key) {
3477 Iterator it = impl->find(key);
3478 if (it == impl->end()) return v8::kPersistentContainerNotFound;
3479 impl->erase(it);
3480 return it->second;
3481 }
3482 // TODO(dcarney): DisposeCallback is badly named.
3483 static void DisposeCallback(Impl* impl, v8::UniquePersistent<V> value) {}
3484 static WeakCallbackDataType* WeakCallbackParameter(Impl* impl, const K& key,
3485 Local<V> value) {
3486 WeakCallbackDataType* data = new WeakCallbackDataType;
3487 data->impl = impl;
3488 data->key = key;
3489 return data;
3490 }
3491 static Impl* ImplFromWeakCallbackData(
3492 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) {
3493 return data.GetParameter()->impl;
3494 }
3495 static K KeyFromWeakCallbackData(
3496 const v8::WeakCallbackData<V, WeakCallbackDataType>& data) {
3497 return data.GetParameter()->key;
3498 }
3499 static void DisposeCallbackData(WeakCallbackDataType* data) { delete data; }
3500 };
3501
3502 template <bool is_weak>
3503 static void TestPersistentValueMap() {
3504 LocalContext env;
3505 v8::Isolate* isolate = env->GetIsolate();
3506 typedef v8::PersistentValueMap<
3507 int, v8::Object, StdPersistentValueMapTraits<int, v8::Object, is_weak> >
3508 Map;
3509 Map map(isolate);
3510 v8::internal::GlobalHandles* global_handles =
3511 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles();
3512 int initial_handle_count = global_handles->global_handles_count();
3513 CHECK_EQ(0, map.Size());
3514 {
3515 HandleScope scope(isolate);
3516 Local<v8::Object> obj = map.Get(7);
3517 CHECK(obj.IsEmpty());
3518 Local<v8::Object> expected = v8::Object::New(isolate);
3519 map.Set(7, expected);
3520 CHECK_EQ(1, map.Size());
3521 obj = map.Get(7);
3522 CHECK_EQ(expected, obj);
3523 v8::UniquePersistent<v8::Object> removed = map.Remove(7);
3524 CHECK_EQ(0, map.Size());
3525 CHECK(expected == removed);
3526 removed = map.Remove(7);
3527 CHECK(removed.IsEmpty());
3528 map.Set(8, expected);
3529 CHECK_EQ(1, map.Size());
3530 map.Set(8, expected);
3531 CHECK_EQ(1, map.Size());
3532 }
3533 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count());
3534 if (is_weak) {
3535 reinterpret_cast<v8::internal::Isolate*>(isolate)
3536 ->heap()
3537 ->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask);
3538 } else {
3539 map.Clear();
3540 }
3541 CHECK_EQ(0, map.Size());
3542 CHECK_EQ(initial_handle_count, global_handles->global_handles_count());
3543 }
3544
3545
3546 TEST(PersistentValueMap) {
3547 TestPersistentValueMap<false>();
3548 TestPersistentValueMap<true>();
3549 }
3550
3445 3551
3446 THREADED_TEST(GlobalHandleUpcast) { 3552 THREADED_TEST(GlobalHandleUpcast) {
3447 v8::Isolate* isolate = CcTest::isolate(); 3553 v8::Isolate* isolate = CcTest::isolate();
3448 v8::HandleScope scope(isolate); 3554 v8::HandleScope scope(isolate);
3449 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); 3555 v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str"));
3450 v8::Persistent<String> global_string(isolate, local); 3556 v8::Persistent<String> global_string(isolate, local);
3451 v8::Persistent<Value>& global_value = 3557 v8::Persistent<Value>& global_value =
3452 v8::Persistent<Value>::Cast(global_string); 3558 v8::Persistent<Value>::Cast(global_string);
3453 CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString()); 3559 CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString());
3454 CHECK(global_string == v8::Persistent<String>::Cast(global_value)); 3560 CHECK(global_string == v8::Persistent<String>::Cast(global_value));
(...skipping 18649 matching lines...) Expand 10 before | Expand all | Expand 10 after
22104 Local<Object> ApiCallOptimizationChecker::holder; 22210 Local<Object> ApiCallOptimizationChecker::holder;
22105 Local<Object> ApiCallOptimizationChecker::callee; 22211 Local<Object> ApiCallOptimizationChecker::callee;
22106 int ApiCallOptimizationChecker::count = 0; 22212 int ApiCallOptimizationChecker::count = 0;
22107 22213
22108 22214
22109 TEST(TestFunctionCallOptimization) { 22215 TEST(TestFunctionCallOptimization) {
22110 i::FLAG_allow_natives_syntax = true; 22216 i::FLAG_allow_natives_syntax = true;
22111 ApiCallOptimizationChecker checker; 22217 ApiCallOptimizationChecker checker;
22112 checker.RunAll(); 22218 checker.RunAll();
22113 } 22219 }
OLDNEW
« include/v8.h ('K') | « src/global-handles.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698