| 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 3343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3354 v8::Object::GetAlignedPointerFromInternalField(value, 0)); | 3354 v8::Object::GetAlignedPointerFromInternalField(value, 0)); |
| 3355 } | 3355 } |
| 3356 static void DisposeWeak( | 3356 static void DisposeWeak( |
| 3357 const v8::WeakCallbackInfo<WeakCallbackDataType>& info) { | 3357 const v8::WeakCallbackInfo<WeakCallbackDataType>& info) { |
| 3358 K key = KeyFromWeakCallbackInfo(info); | 3358 K key = KeyFromWeakCallbackInfo(info); |
| 3359 CHECK_EQ(IntKeyToVoidPointer(key), info.GetInternalField(0)); | 3359 CHECK_EQ(IntKeyToVoidPointer(key), info.GetInternalField(0)); |
| 3360 DisposeCallbackData(info.GetParameter()); | 3360 DisposeCallbackData(info.GetParameter()); |
| 3361 } | 3361 } |
| 3362 }; | 3362 }; |
| 3363 | 3363 |
| 3364 } // namespace | |
| 3365 | 3364 |
| 3366 | 3365 template <typename Map> |
| 3367 TEST(GlobalValueMap) { | 3366 void TestGlobalValueMap() { |
| 3368 typedef v8::GlobalValueMap<int, v8::Object, | |
| 3369 PhantomStdMapTraits<int, v8::Object>> Map; | |
| 3370 LocalContext env; | 3367 LocalContext env; |
| 3371 v8::Isolate* isolate = env->GetIsolate(); | 3368 v8::Isolate* isolate = env->GetIsolate(); |
| 3372 v8::Global<ObjectTemplate> templ; | 3369 v8::Global<ObjectTemplate> templ; |
| 3373 { | 3370 { |
| 3374 HandleScope scope(isolate); | 3371 HandleScope scope(isolate); |
| 3375 auto t = ObjectTemplate::New(isolate); | 3372 auto t = ObjectTemplate::New(isolate); |
| 3376 t->SetInternalFieldCount(1); | 3373 t->SetInternalFieldCount(1); |
| 3377 templ.Reset(isolate, t); | 3374 templ.Reset(isolate, t); |
| 3378 } | 3375 } |
| 3379 Map map(isolate); | 3376 Map map(isolate); |
| 3380 v8::internal::GlobalHandles* global_handles = | 3377 v8::internal::GlobalHandles* global_handles = |
| 3381 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); | 3378 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
| 3382 int initial_handle_count = global_handles->global_handles_count(); | 3379 int initial_handle_count = global_handles->global_handles_count(); |
| 3383 CHECK_EQ(0, static_cast<int>(map.Size())); | 3380 CHECK_EQ(0, static_cast<int>(map.Size())); |
| 3384 { | 3381 { |
| 3385 HandleScope scope(isolate); | 3382 HandleScope scope(isolate); |
| 3386 Local<v8::Object> obj = map.Get(7); | 3383 Local<v8::Object> obj = map.Get(7); |
| 3387 CHECK(obj.IsEmpty()); | 3384 CHECK(obj.IsEmpty()); |
| 3388 Local<v8::Object> expected = v8::Object::New(isolate); | 3385 Local<v8::Object> expected = v8::Object::New(isolate); |
| 3389 map.Set(7, expected); | 3386 map.Set(7, expected); |
| 3390 CHECK_EQ(1, static_cast<int>(map.Size())); | 3387 CHECK_EQ(1, static_cast<int>(map.Size())); |
| 3391 obj = map.Get(7); | 3388 obj = map.Get(7); |
| 3392 CHECK(expected->Equals(obj)); | 3389 CHECK(expected->Equals(obj)); |
| 3393 { | 3390 { |
| 3394 Map::PersistentValueReference ref = map.GetReference(7); | 3391 typename Map::PersistentValueReference ref = map.GetReference(7); |
| 3395 CHECK(expected->Equals(ref.NewLocal(isolate))); | 3392 CHECK(expected->Equals(ref.NewLocal(isolate))); |
| 3396 } | 3393 } |
| 3397 v8::Global<v8::Object> removed = map.Remove(7); | 3394 v8::Global<v8::Object> removed = map.Remove(7); |
| 3398 CHECK_EQ(0, static_cast<int>(map.Size())); | 3395 CHECK_EQ(0, static_cast<int>(map.Size())); |
| 3399 CHECK(expected == removed); | 3396 CHECK(expected == removed); |
| 3400 removed = map.Remove(7); | 3397 removed = map.Remove(7); |
| 3401 CHECK(removed.IsEmpty()); | 3398 CHECK(removed.IsEmpty()); |
| 3402 map.Set(8, expected); | 3399 map.Set(8, expected); |
| 3403 CHECK_EQ(1, static_cast<int>(map.Size())); | 3400 CHECK_EQ(1, static_cast<int>(map.Size())); |
| 3404 map.Set(8, expected); | 3401 map.Set(8, expected); |
| 3405 CHECK_EQ(1, static_cast<int>(map.Size())); | 3402 CHECK_EQ(1, static_cast<int>(map.Size())); |
| 3406 { | 3403 { |
| 3407 Map::PersistentValueReference ref; | 3404 typename Map::PersistentValueReference ref; |
| 3408 Local<v8::Object> expected2 = NewObjectForIntKey(isolate, templ, 8); | 3405 Local<v8::Object> expected2 = NewObjectForIntKey(isolate, templ, 8); |
| 3409 removed = map.Set(8, v8::Global<v8::Object>(isolate, expected2), &ref); | 3406 removed = map.Set(8, v8::Global<v8::Object>(isolate, expected2), &ref); |
| 3410 CHECK_EQ(1, static_cast<int>(map.Size())); | 3407 CHECK_EQ(1, static_cast<int>(map.Size())); |
| 3411 CHECK(expected == removed); | 3408 CHECK(expected == removed); |
| 3412 CHECK(expected2->Equals(ref.NewLocal(isolate))); | 3409 CHECK(expected2->Equals(ref.NewLocal(isolate))); |
| 3413 } | 3410 } |
| 3414 } | 3411 } |
| 3415 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); | 3412 CHECK_EQ(initial_handle_count + 1, global_handles->global_handles_count()); |
| 3416 CcTest::i_isolate()->heap()->CollectAllGarbage( | 3413 if (map.IsWeak()) { |
| 3417 i::Heap::kAbortIncrementalMarkingMask); | 3414 CcTest::i_isolate()->heap()->CollectAllGarbage( |
| 3415 i::Heap::kAbortIncrementalMarkingMask); |
| 3416 } else { |
| 3417 map.Clear(); |
| 3418 } |
| 3418 CHECK_EQ(0, static_cast<int>(map.Size())); | 3419 CHECK_EQ(0, static_cast<int>(map.Size())); |
| 3419 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3420 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
| 3420 { | 3421 { |
| 3421 HandleScope scope(isolate); | 3422 HandleScope scope(isolate); |
| 3422 Local<v8::Object> value = NewObjectForIntKey(isolate, templ, 9); | 3423 Local<v8::Object> value = NewObjectForIntKey(isolate, templ, 9); |
| 3423 map.Set(9, value); | 3424 map.Set(9, value); |
| 3424 map.Clear(); | 3425 map.Clear(); |
| 3425 } | 3426 } |
| 3426 CHECK_EQ(0, static_cast<int>(map.Size())); | 3427 CHECK_EQ(0, static_cast<int>(map.Size())); |
| 3427 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); | 3428 CHECK_EQ(initial_handle_count, global_handles->global_handles_count()); |
| 3428 } | 3429 } |
| 3429 | 3430 |
| 3431 } // namespace |
| 3432 |
| 3433 |
| 3434 TEST(GlobalValueMap) { |
| 3435 // Default case, w/o weak callbacks: |
| 3436 TestGlobalValueMap<v8::StdGlobalValueMap<int, v8::Object>>(); |
| 3437 |
| 3438 // Custom traits with weak callbacks: |
| 3439 typedef v8::GlobalValueMap<int, v8::Object, |
| 3440 PhantomStdMapTraits<int, v8::Object>> WeakMap; |
| 3441 TestGlobalValueMap<WeakMap>(); |
| 3442 } |
| 3443 |
| 3430 | 3444 |
| 3431 TEST(PersistentValueVector) { | 3445 TEST(PersistentValueVector) { |
| 3432 LocalContext env; | 3446 LocalContext env; |
| 3433 v8::Isolate* isolate = env->GetIsolate(); | 3447 v8::Isolate* isolate = env->GetIsolate(); |
| 3434 v8::internal::GlobalHandles* global_handles = | 3448 v8::internal::GlobalHandles* global_handles = |
| 3435 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); | 3449 reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
| 3436 int handle_count = global_handles->global_handles_count(); | 3450 int handle_count = global_handles->global_handles_count(); |
| 3437 HandleScope scope(isolate); | 3451 HandleScope scope(isolate); |
| 3438 | 3452 |
| 3439 v8::PersistentValueVector<v8::Object> vector(isolate); | 3453 v8::PersistentValueVector<v8::Object> vector(isolate); |
| (...skipping 17579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 21019 | 21033 |
| 21020 { | 21034 { |
| 21021 v8::HandleScope handle_scope(isolate); | 21035 v8::HandleScope handle_scope(isolate); |
| 21022 | 21036 |
| 21023 // Should work | 21037 // Should work |
| 21024 v8::Local<v8::Object> obj = v8::Object::New(isolate); | 21038 v8::Local<v8::Object> obj = v8::Object::New(isolate); |
| 21025 | 21039 |
| 21026 USE(obj); | 21040 USE(obj); |
| 21027 } | 21041 } |
| 21028 } | 21042 } |
| OLD | NEW |