OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 | 2 |
3 // Test constant pool array code. | 3 // Test constant pool array code. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/factory.h" | 7 #include "src/factory.h" |
8 #include "src/objects.h" | 8 #include "src/objects.h" |
9 #include "test/cctest/cctest.h" | 9 #include "test/cctest/cctest.h" |
10 | 10 |
11 using namespace v8::internal; | 11 using namespace v8::internal; |
12 | 12 |
| 13 static ConstantPoolArray::Type kTypes[] = { ConstantPoolArray::INT64, |
| 14 ConstantPoolArray::CODE_PTR, |
| 15 ConstantPoolArray::HEAP_PTR, |
| 16 ConstantPoolArray::INT32 }; |
| 17 static ConstantPoolArray::LayoutSection kSmall = |
| 18 ConstantPoolArray::SMALL_SECTION; |
| 19 static ConstantPoolArray::LayoutSection kExtended = |
| 20 ConstantPoolArray::EXTENDED_SECTION; |
13 | 21 |
14 Code* DummyCode(LocalContext* context) { | 22 Code* DummyCode(LocalContext* context) { |
15 CompileRun("function foo() {};"); | 23 CompileRun("function foo() {};"); |
16 i::Handle<i::JSFunction> fun = v8::Utils::OpenHandle( | 24 i::Handle<i::JSFunction> fun = v8::Utils::OpenHandle( |
17 *v8::Local<v8::Function>::Cast( | 25 *v8::Local<v8::Function>::Cast( |
18 (*context)->Global()->Get(v8_str("foo")))); | 26 (*context)->Global()->Get(v8_str("foo")))); |
19 return fun->code(); | 27 return fun->code(); |
20 } | 28 } |
21 | 29 |
22 | 30 |
23 TEST(ConstantPool) { | 31 TEST(ConstantPoolSmall) { |
24 LocalContext context; | 32 LocalContext context; |
25 Isolate* isolate = CcTest::i_isolate(); | 33 Isolate* isolate = CcTest::i_isolate(); |
26 Heap* heap = isolate->heap(); | 34 Heap* heap = isolate->heap(); |
27 Factory* factory = isolate->factory(); | 35 Factory* factory = isolate->factory(); |
28 v8::HandleScope scope(context->GetIsolate()); | 36 v8::HandleScope scope(context->GetIsolate()); |
29 | 37 |
30 // Check construction. | 38 // Check construction. |
31 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(3, 1, 2, 1); | 39 ConstantPoolArray::NumberOfEntries small(3, 1, 2, 1); |
32 CHECK_EQ(array->count_of_int64_entries(), 3); | 40 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(small); |
33 CHECK_EQ(array->count_of_code_ptr_entries(), 1); | 41 |
34 CHECK_EQ(array->count_of_heap_ptr_entries(), 2); | 42 int expected_counts[] = { 3, 1, 2, 1 }; |
35 CHECK_EQ(array->count_of_int32_entries(), 1); | 43 int expected_first_idx[] = { 0, 3, 4, 6 }; |
36 CHECK_EQ(array->length(), 7); | 44 int expected_last_idx[] = { 2, 3, 5, 6 }; |
37 CHECK_EQ(array->first_int64_index(), 0); | 45 for (int i = 0; i < 4; i++) { |
38 CHECK_EQ(array->first_code_ptr_index(), 3); | 46 CHECK_EQ(expected_counts[i], array->number_of_entries(kTypes[i], kSmall)); |
39 CHECK_EQ(array->first_heap_ptr_index(), 4); | 47 CHECK_EQ(expected_first_idx[i], array->first_index(kTypes[i], kSmall)); |
40 CHECK_EQ(array->first_int32_index(), 6); | 48 CHECK_EQ(expected_last_idx[i], array->last_index(kTypes[i], kSmall)); |
| 49 } |
| 50 CHECK(!array->is_extended_layout()); |
41 | 51 |
42 // Check getters and setters. | 52 // Check getters and setters. |
43 int64_t big_number = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); | 53 int64_t big_number = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); |
44 Handle<Object> object = factory->NewHeapNumber(4.0); | 54 Handle<Object> object = factory->NewHeapNumber(4.0); |
45 Code* code = DummyCode(&context); | 55 Code* code = DummyCode(&context); |
46 array->set(0, big_number); | 56 array->set(0, big_number); |
47 array->set(1, 0.5); | 57 array->set(1, 0.5); |
48 array->set(2, 3e-24); | 58 array->set(2, 3e-24); |
49 array->set(3, code->entry()); | 59 array->set(3, code->entry()); |
50 array->set(4, code); | 60 array->set(4, code); |
51 array->set(5, *object); | 61 array->set(5, *object); |
52 array->set(6, 50); | 62 array->set(6, 50); |
53 CHECK_EQ(array->get_int64_entry(0), big_number); | 63 CHECK_EQ(big_number, array->get_int64_entry(0)); |
54 CHECK_EQ(array->get_int64_entry_as_double(1), 0.5); | 64 CHECK_EQ(0.5, array->get_int64_entry_as_double(1)); |
55 CHECK_EQ(array->get_int64_entry_as_double(2), 3e-24); | 65 CHECK_EQ(3e-24, array->get_int64_entry_as_double(2)); |
56 CHECK_EQ(array->get_code_ptr_entry(3), code->entry()); | 66 CHECK_EQ(code->entry(), array->get_code_ptr_entry(3)); |
57 CHECK_EQ(array->get_heap_ptr_entry(4), code); | 67 CHECK_EQ(code, array->get_heap_ptr_entry(4)); |
58 CHECK_EQ(array->get_heap_ptr_entry(5), *object); | 68 CHECK_EQ(*object, array->get_heap_ptr_entry(5)); |
59 CHECK_EQ(array->get_int32_entry(6), 50); | 69 CHECK_EQ(50, array->get_int32_entry(6)); |
60 | 70 |
61 // Check pointers are updated on GC. | 71 // Check pointers are updated on GC. |
62 Object* old_ptr = array->get_heap_ptr_entry(5); | 72 Object* old_ptr = array->get_heap_ptr_entry(5); |
63 CHECK_EQ(*object, old_ptr); | 73 CHECK_EQ(*object, old_ptr); |
64 heap->CollectGarbage(NEW_SPACE); | 74 heap->CollectGarbage(NEW_SPACE); |
65 Object* new_ptr = array->get_heap_ptr_entry(5); | 75 Object* new_ptr = array->get_heap_ptr_entry(5); |
66 CHECK_NE(*object, old_ptr); | 76 CHECK_NE(*object, old_ptr); |
67 CHECK_EQ(*object, new_ptr); | 77 CHECK_EQ(*object, new_ptr); |
68 } | 78 } |
| 79 |
| 80 |
| 81 TEST(ConstantPoolExtended) { |
| 82 LocalContext context; |
| 83 Isolate* isolate = CcTest::i_isolate(); |
| 84 Heap* heap = isolate->heap(); |
| 85 Factory* factory = isolate->factory(); |
| 86 v8::HandleScope scope(context->GetIsolate()); |
| 87 |
| 88 // Check construction. |
| 89 ConstantPoolArray::NumberOfEntries small(1, 2, 3, 4); |
| 90 ConstantPoolArray::NumberOfEntries extended(5, 6, 7, 8); |
| 91 Handle<ConstantPoolArray> array = |
| 92 factory->NewExtendedConstantPoolArray(small, extended); |
| 93 |
| 94 // Check small section. |
| 95 int small_counts[] = { 1, 2, 3, 4 }; |
| 96 int small_first_idx[] = { 0, 1, 3, 6 }; |
| 97 int small_last_idx[] = { 0, 2, 5, 9 }; |
| 98 for (int i = 0; i < 4; i++) { |
| 99 CHECK_EQ(small_counts[i], array->number_of_entries(kTypes[i], kSmall)); |
| 100 CHECK_EQ(small_first_idx[i], array->first_index(kTypes[i], kSmall)); |
| 101 CHECK_EQ(small_last_idx[i], array->last_index(kTypes[i], kSmall)); |
| 102 } |
| 103 |
| 104 // Check extended layout. |
| 105 CHECK(array->is_extended_layout()); |
| 106 int extended_counts[] = { 5, 6, 7, 8 }; |
| 107 int extended_first_idx[] = { 10, 15, 21, 28 }; |
| 108 int extended_last_idx[] = { 14, 20, 27, 35 }; |
| 109 for (int i = 0; i < 4; i++) { |
| 110 CHECK_EQ(extended_counts[i], |
| 111 array->number_of_entries(kTypes[i], kExtended)); |
| 112 CHECK_EQ(extended_first_idx[i], array->first_index(kTypes[i], kExtended)); |
| 113 CHECK_EQ(extended_last_idx[i], array->last_index(kTypes[i], kExtended)); |
| 114 } |
| 115 |
| 116 // Check small and large section's don't overlap. |
| 117 int64_t small_section_int64 = V8_2PART_UINT64_C(0x56781234, DEF09ABC); |
| 118 Code* small_section_code_ptr = DummyCode(&context); |
| 119 Handle<Object> small_section_heap_ptr = factory->NewHeapNumber(4.0); |
| 120 int32_t small_section_int32 = 0xab12cd45; |
| 121 |
| 122 int64_t extended_section_int64 = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); |
| 123 Code* extended_section_code_ptr = DummyCode(&context); |
| 124 Handle<Object> extended_section_heap_ptr = factory->NewHeapNumber(4.0); |
| 125 int32_t extended_section_int32 = 0xef67ab89; |
| 126 |
| 127 for (int i = array->first_index(ConstantPoolArray::INT64, kSmall); |
| 128 i <= array->last_index(ConstantPoolArray::INT32, kSmall); i++) { |
| 129 if (i <= array->last_index(ConstantPoolArray::INT64, kSmall)) { |
| 130 array->set(i, small_section_int64); |
| 131 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kSmall)) { |
| 132 array->set(i, small_section_code_ptr->entry()); |
| 133 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kSmall)) { |
| 134 array->set(i, *small_section_heap_ptr); |
| 135 } else { |
| 136 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kSmall)); |
| 137 array->set(i, small_section_int32); |
| 138 } |
| 139 } |
| 140 for (int i = array->first_index(ConstantPoolArray::INT64, kExtended); |
| 141 i <= array->last_index(ConstantPoolArray::INT32, kExtended); i++) { |
| 142 if (i <= array->last_index(ConstantPoolArray::INT64, kExtended)) { |
| 143 array->set(i, extended_section_int64); |
| 144 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kExtended)) { |
| 145 array->set(i, extended_section_code_ptr->entry()); |
| 146 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kExtended)) { |
| 147 array->set(i, *extended_section_heap_ptr); |
| 148 } else { |
| 149 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kExtended)); |
| 150 array->set(i, extended_section_int32); |
| 151 } |
| 152 } |
| 153 |
| 154 for (int i = array->first_index(ConstantPoolArray::INT64, kSmall); |
| 155 i <= array->last_index(ConstantPoolArray::INT32, kSmall); i++) { |
| 156 if (i <= array->last_index(ConstantPoolArray::INT64, kSmall)) { |
| 157 CHECK_EQ(small_section_int64, array->get_int64_entry(i)); |
| 158 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kSmall)) { |
| 159 CHECK_EQ(small_section_code_ptr->entry(), array->get_code_ptr_entry(i)); |
| 160 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kSmall)) { |
| 161 CHECK_EQ(*small_section_heap_ptr, array->get_heap_ptr_entry(i)); |
| 162 } else { |
| 163 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kSmall)); |
| 164 CHECK_EQ(small_section_int32, array->get_int32_entry(i)); |
| 165 } |
| 166 } |
| 167 for (int i = array->first_index(ConstantPoolArray::INT64, kExtended); |
| 168 i <= array->last_index(ConstantPoolArray::INT32, kExtended); i++) { |
| 169 if (i <= array->last_index(ConstantPoolArray::INT64, kExtended)) { |
| 170 CHECK_EQ(extended_section_int64, array->get_int64_entry(i)); |
| 171 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kExtended)) { |
| 172 CHECK_EQ(extended_section_code_ptr->entry(), |
| 173 array->get_code_ptr_entry(i)); |
| 174 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kExtended)) { |
| 175 CHECK_EQ(*extended_section_heap_ptr, array->get_heap_ptr_entry(i)); |
| 176 } else { |
| 177 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kExtended)); |
| 178 CHECK_EQ(extended_section_int32, array->get_int32_entry(i)); |
| 179 } |
| 180 } |
| 181 // Check pointers are updated on GC in extended section. |
| 182 int index = array->first_index(ConstantPoolArray::HEAP_PTR, kExtended); |
| 183 Object* old_ptr = array->get_heap_ptr_entry(index); |
| 184 CHECK_EQ(*extended_section_heap_ptr, old_ptr); |
| 185 heap->CollectGarbage(NEW_SPACE); |
| 186 Object* new_ptr = array->get_heap_ptr_entry(index); |
| 187 CHECK_NE(*extended_section_heap_ptr, old_ptr); |
| 188 CHECK_EQ(*extended_section_heap_ptr, new_ptr); |
| 189 } |
| 190 |
| 191 |
| 192 static void CheckIterator(Handle<ConstantPoolArray> array, |
| 193 ConstantPoolArray::Type type, |
| 194 int expected_indexes[], |
| 195 int count) { |
| 196 int i = 0; |
| 197 ConstantPoolArray::Iterator iter(*array, type); |
| 198 while (!iter.is_finished()) { |
| 199 CHECK_EQ(expected_indexes[i++], iter.next_index()); |
| 200 } |
| 201 CHECK_EQ(count, i); |
| 202 } |
| 203 |
| 204 |
| 205 TEST(ConstantPoolIteratorSmall) { |
| 206 LocalContext context; |
| 207 Isolate* isolate = CcTest::i_isolate(); |
| 208 Factory* factory = isolate->factory(); |
| 209 v8::HandleScope scope(context->GetIsolate()); |
| 210 |
| 211 ConstantPoolArray::NumberOfEntries small(1, 5, 2, 0); |
| 212 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(small); |
| 213 |
| 214 int expected_int64_indexs[] = { 0 }; |
| 215 CheckIterator(array, ConstantPoolArray::INT64, expected_int64_indexs, 1); |
| 216 int expected_code_indexs[] = { 1, 2, 3, 4, 5 }; |
| 217 CheckIterator(array, ConstantPoolArray::CODE_PTR, expected_code_indexs, 5); |
| 218 int expected_heap_indexs[] = { 6, 7 }; |
| 219 CheckIterator(array, ConstantPoolArray::HEAP_PTR, expected_heap_indexs, 2); |
| 220 int expected_int32_indexs[1]; |
| 221 CheckIterator(array, ConstantPoolArray::INT32, expected_int32_indexs, 0); |
| 222 } |
| 223 |
| 224 |
| 225 TEST(ConstantPoolIteratorExtended) { |
| 226 LocalContext context; |
| 227 Isolate* isolate = CcTest::i_isolate(); |
| 228 Factory* factory = isolate->factory(); |
| 229 v8::HandleScope scope(context->GetIsolate()); |
| 230 |
| 231 ConstantPoolArray::NumberOfEntries small(1, 0, 0, 4); |
| 232 ConstantPoolArray::NumberOfEntries extended(5, 0, 3, 0); |
| 233 Handle<ConstantPoolArray> array = |
| 234 factory->NewExtendedConstantPoolArray(small, extended); |
| 235 |
| 236 int expected_int64_indexs[] = { 0, 5, 6, 7, 8, 9 }; |
| 237 CheckIterator(array, ConstantPoolArray::INT64, expected_int64_indexs, 6); |
| 238 int expected_code_indexs[1]; |
| 239 CheckIterator(array, ConstantPoolArray::CODE_PTR, expected_code_indexs, 0); |
| 240 int expected_heap_indexs[] = { 10, 11, 12 }; |
| 241 CheckIterator(array, ConstantPoolArray::HEAP_PTR, expected_heap_indexs, 3); |
| 242 int expected_int32_indexs[] = { 1, 2, 3, 4 }; |
| 243 CheckIterator(array, ConstantPoolArray::INT32, expected_int32_indexs, 4); |
| 244 } |
OLD | NEW |