OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 20 matching lines...) Expand all Loading... |
31 #include "debug.h" | 31 #include "debug.h" |
32 #include "execution.h" | 32 #include "execution.h" |
33 #include "factory.h" | 33 #include "factory.h" |
34 #include "macro-assembler.h" | 34 #include "macro-assembler.h" |
35 #include "objects.h" | 35 #include "objects.h" |
36 #include "global-handles.h" | 36 #include "global-handles.h" |
37 #include "cctest.h" | 37 #include "cctest.h" |
38 | 38 |
39 using namespace v8::internal; | 39 using namespace v8::internal; |
40 | 40 |
| 41 |
41 TEST(ObjectHashTable) { | 42 TEST(ObjectHashTable) { |
42 v8::HandleScope scope; | 43 v8::HandleScope scope; |
43 LocalContext context; | 44 LocalContext context; |
44 Handle<ObjectHashTable> table = FACTORY->NewObjectHashTable(23); | 45 Handle<ObjectHashTable> table = FACTORY->NewObjectHashTable(23); |
45 Handle<JSObject> a = FACTORY->NewJSArray(7); | 46 Handle<JSObject> a = FACTORY->NewJSArray(7); |
46 Handle<JSObject> b = FACTORY->NewJSArray(11); | 47 Handle<JSObject> b = FACTORY->NewJSArray(11); |
47 table = PutIntoObjectHashTable(table, a, b); | 48 table = PutIntoObjectHashTable(table, a, b); |
48 CHECK_EQ(table->NumberOfElements(), 1); | 49 CHECK_EQ(table->NumberOfElements(), 1); |
49 CHECK_EQ(table->Lookup(*a), *b); | 50 CHECK_EQ(table->Lookup(*a), *b); |
50 CHECK_EQ(table->Lookup(*b), HEAP->undefined_value()); | 51 CHECK_EQ(table->Lookup(*b), HEAP->undefined_value()); |
51 | 52 |
52 // Keys still have to be valid after objects were moved. | 53 // Keys still have to be valid after objects were moved. |
53 HEAP->CollectGarbage(NEW_SPACE); | 54 HEAP->CollectGarbage(NEW_SPACE); |
54 CHECK_EQ(table->NumberOfElements(), 1); | 55 CHECK_EQ(table->NumberOfElements(), 1); |
55 CHECK_EQ(table->Lookup(*a), *b); | 56 CHECK_EQ(table->Lookup(*a), *b); |
56 CHECK_EQ(table->Lookup(*b), HEAP->undefined_value()); | 57 CHECK_EQ(table->Lookup(*b), HEAP->undefined_value()); |
57 | 58 |
58 // Keys that are overwritten should not change number of elements. | 59 // Keys that are overwritten should not change number of elements. |
59 table = PutIntoObjectHashTable(table, a, FACTORY->NewJSArray(13)); | 60 table = PutIntoObjectHashTable(table, a, FACTORY->NewJSArray(13)); |
60 CHECK_EQ(table->NumberOfElements(), 1); | 61 CHECK_EQ(table->NumberOfElements(), 1); |
61 CHECK_NE(table->Lookup(*a), *b); | 62 CHECK_NE(table->Lookup(*a), *b); |
62 | 63 |
63 // Keys mapped to undefined should be removed permanently. | 64 // Keys mapped to undefined should be removed permanently. |
64 table = PutIntoObjectHashTable(table, a, FACTORY->undefined_value()); | 65 table = PutIntoObjectHashTable(table, a, FACTORY->undefined_value()); |
65 CHECK_EQ(table->NumberOfElements(), 0); | 66 CHECK_EQ(table->NumberOfElements(), 0); |
66 CHECK_EQ(table->NumberOfDeletedElements(), 1); | 67 CHECK_EQ(table->NumberOfDeletedElements(), 1); |
67 CHECK_EQ(table->Lookup(*a), HEAP->undefined_value()); | 68 CHECK_EQ(table->Lookup(*a), HEAP->undefined_value()); |
68 | 69 |
69 // Keys should map back to their respective values. | 70 // Keys should map back to their respective values and also should get |
| 71 // an identity hash code generated. |
70 for (int i = 0; i < 100; i++) { | 72 for (int i = 0; i < 100; i++) { |
71 Handle<JSObject> key = FACTORY->NewJSArray(7); | 73 Handle<JSObject> key = FACTORY->NewJSArray(7); |
72 Handle<JSObject> value = FACTORY->NewJSArray(11); | 74 Handle<JSObject> value = FACTORY->NewJSArray(11); |
73 table = PutIntoObjectHashTable(table, key, value); | 75 table = PutIntoObjectHashTable(table, key, value); |
74 CHECK_EQ(table->NumberOfElements(), i + 1); | 76 CHECK_EQ(table->NumberOfElements(), i + 1); |
75 CHECK_NE(table->FindEntry(*key), ObjectHashTable::kNotFound); | 77 CHECK_NE(table->FindEntry(*key), ObjectHashTable::kNotFound); |
76 CHECK_EQ(table->Lookup(*key), *value); | 78 CHECK_EQ(table->Lookup(*key), *value); |
| 79 CHECK(key->GetIdentityHash(OMIT_CREATION)->ToObjectChecked()->IsSmi()); |
77 } | 80 } |
78 | 81 |
79 // Keys never added to the map should not be found. | 82 // Keys never added to the map which already have an identity hash |
80 for (int i = 0; i < 1000; i++) { | 83 // code should not be found. |
81 Handle<JSObject> o = FACTORY->NewJSArray(100); | 84 for (int i = 0; i < 100; i++) { |
82 CHECK_EQ(table->FindEntry(*o), ObjectHashTable::kNotFound); | 85 Handle<JSObject> key = FACTORY->NewJSArray(7); |
83 CHECK_EQ(table->Lookup(*o), HEAP->undefined_value()); | 86 CHECK(key->GetIdentityHash(ALLOW_CREATION)->ToObjectChecked()->IsSmi()); |
| 87 CHECK_EQ(table->FindEntry(*key), ObjectHashTable::kNotFound); |
| 88 CHECK_EQ(table->Lookup(*key), HEAP->undefined_value()); |
| 89 CHECK(key->GetIdentityHash(OMIT_CREATION)->ToObjectChecked()->IsSmi()); |
| 90 } |
| 91 |
| 92 // Keys that don't have an identity hash should not be found and also |
| 93 // should not get an identity hash code generated. |
| 94 for (int i = 0; i < 100; i++) { |
| 95 Handle<JSObject> key = FACTORY->NewJSArray(7); |
| 96 CHECK_EQ(table->Lookup(*key), HEAP->undefined_value()); |
| 97 CHECK_EQ(key->GetIdentityHash(OMIT_CREATION), HEAP->undefined_value()); |
84 } | 98 } |
85 } | 99 } |
| 100 |
| 101 |
| 102 #ifdef DEBUG |
| 103 TEST(ObjectHashSetCausesGC) { |
| 104 v8::HandleScope scope; |
| 105 LocalContext context; |
| 106 Handle<ObjectHashSet> table = FACTORY->NewObjectHashSet(1); |
| 107 Handle<JSObject> key = FACTORY->NewJSArray(0); |
| 108 |
| 109 // Simulate a full heap so that generating an identity hash code |
| 110 // in subsequent calls will request GC. |
| 111 FLAG_gc_interval = 0; |
| 112 |
| 113 // Calling Contains() should not cause GC ever. |
| 114 CHECK(!table->Contains(*key)); |
| 115 |
| 116 // Calling Remove() should not cause GC ever. |
| 117 CHECK(!table->Remove(*key)->IsFailure()); |
| 118 |
| 119 // Calling Add() should request GC by returning a failure. |
| 120 CHECK(table->Add(*key)->IsRetryAfterGC()); |
| 121 } |
| 122 #endif |
| 123 |
| 124 |
| 125 #ifdef DEBUG |
| 126 TEST(ObjectHashTableCausesGC) { |
| 127 v8::HandleScope scope; |
| 128 LocalContext context; |
| 129 Handle<ObjectHashTable> table = FACTORY->NewObjectHashTable(1); |
| 130 Handle<JSObject> key = FACTORY->NewJSArray(0); |
| 131 |
| 132 // Simulate a full heap so that generating an identity hash code |
| 133 // in subsequent calls will request GC. |
| 134 FLAG_gc_interval = 0; |
| 135 |
| 136 // Calling Lookup() should not cause GC ever. |
| 137 CHECK(table->Lookup(*key)->IsUndefined()); |
| 138 |
| 139 // Calling Put() should request GC by returning a failure. |
| 140 CHECK(table->Put(*key, *key)->IsRetryAfterGC()); |
| 141 } |
| 142 #endif |
OLD | NEW |