danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2 | |
| 3 | #include <stdlib.h> |
| 4 | |
| 5 | #include "v8.h" |
| 6 | |
| 7 | #include "execution.h" |
| 8 | #include "factory.h" |
| 9 | #include "macro-assembler.h" |
| 10 | #include "global-handles.h" |
| 11 | #include "cctest.h" |
| 12 | |
| 13 | using namespace v8::internal; |
| 14 | |
| 15 | static v8::Persistent<v8::Context> env; |
| 16 | |
| 17 | static void InitializeVM() { |
| 18 | if (env.IsEmpty()) env = v8::Context::New(); |
| 19 | v8::HandleScope scope; |
| 20 | env->Enter(); |
| 21 | } |
| 22 | |
| 23 | |
| 24 | static void CheckMap(Map* map, int type, int instance_size) { |
| 25 | CHECK(map->IsHeapObject()); |
| 26 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 27 | CHECK(HEAP->Contains(map)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 28 | #endif |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 29 | CHECK_EQ(HEAP->meta_map(), map->map()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 30 | CHECK_EQ(type, map->instance_type()); |
| 31 | CHECK_EQ(instance_size, map->instance_size()); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | TEST(HeapMaps) { |
| 36 | InitializeVM(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 37 | CheckMap(HEAP->meta_map(), MAP_TYPE, Map::kSize); |
| 38 | CheckMap(HEAP->heap_number_map(), HEAP_NUMBER_TYPE, HeapNumber::kSize); |
| 39 | CheckMap(HEAP->fixed_array_map(), FIXED_ARRAY_TYPE, kVariableSizeSentinel); |
| 40 | CheckMap(HEAP->string_map(), STRING_TYPE, kVariableSizeSentinel); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | |
| 44 | static void CheckOddball(Object* obj, const char* string) { |
| 45 | CHECK(obj->IsOddball()); |
| 46 | bool exc; |
| 47 | Object* print_string = *Execution::ToString(Handle<Object>(obj), &exc); |
| 48 | CHECK(String::cast(print_string)->IsEqualTo(CStrVector(string))); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | static void CheckSmi(int value, const char* string) { |
| 53 | bool exc; |
| 54 | Object* print_string = |
| 55 | *Execution::ToString(Handle<Object>(Smi::FromInt(value)), &exc); |
| 56 | CHECK(String::cast(print_string)->IsEqualTo(CStrVector(string))); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static void CheckNumber(double value, const char* string) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 61 | Object* obj = HEAP->NumberFromDouble(value)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 62 | CHECK(obj->IsNumber()); |
| 63 | bool exc; |
| 64 | Object* print_string = *Execution::ToString(Handle<Object>(obj), &exc); |
| 65 | CHECK(String::cast(print_string)->IsEqualTo(CStrVector(string))); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static void CheckFindCodeObject() { |
| 70 | // Test FindCodeObject |
| 71 | #define __ assm. |
| 72 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 73 | Assembler assm(Isolate::Current(), NULL, 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 74 | |
| 75 | __ nop(); // supported on all architectures |
| 76 | |
| 77 | CodeDesc desc; |
| 78 | assm.GetCode(&desc); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 79 | Object* code = HEAP->CreateCode( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 80 | desc, |
| 81 | Code::ComputeFlags(Code::STUB), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 82 | Handle<Object>(HEAP->undefined_value()))->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 83 | CHECK(code->IsCode()); |
| 84 | |
| 85 | HeapObject* obj = HeapObject::cast(code); |
| 86 | Address obj_addr = obj->address(); |
| 87 | |
| 88 | for (int i = 0; i < obj->Size(); i += kPointerSize) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 89 | Object* found = HEAP->FindCodeObject(obj_addr + i); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 90 | CHECK_EQ(code, found); |
| 91 | } |
| 92 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 93 | Object* copy = HEAP->CreateCode( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 94 | desc, |
| 95 | Code::ComputeFlags(Code::STUB), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 96 | Handle<Object>(HEAP->undefined_value()))->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 97 | CHECK(copy->IsCode()); |
| 98 | HeapObject* obj_copy = HeapObject::cast(copy); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 99 | Object* not_right = HEAP->FindCodeObject(obj_copy->address() + |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 100 | obj_copy->Size() / 2); |
| 101 | CHECK(not_right != code); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | TEST(HeapObjects) { |
| 106 | InitializeVM(); |
| 107 | |
| 108 | v8::HandleScope sc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 109 | Object* value = HEAP->NumberFromDouble(1.000123)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 110 | CHECK(value->IsHeapNumber()); |
| 111 | CHECK(value->IsNumber()); |
| 112 | CHECK_EQ(1.000123, value->Number()); |
| 113 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 114 | value = HEAP->NumberFromDouble(1.0)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 115 | CHECK(value->IsSmi()); |
| 116 | CHECK(value->IsNumber()); |
| 117 | CHECK_EQ(1.0, value->Number()); |
| 118 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 119 | value = HEAP->NumberFromInt32(1024)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 120 | CHECK(value->IsSmi()); |
| 121 | CHECK(value->IsNumber()); |
| 122 | CHECK_EQ(1024.0, value->Number()); |
| 123 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 124 | value = HEAP->NumberFromInt32(Smi::kMinValue)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 125 | CHECK(value->IsSmi()); |
| 126 | CHECK(value->IsNumber()); |
| 127 | CHECK_EQ(Smi::kMinValue, Smi::cast(value)->value()); |
| 128 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 129 | value = HEAP->NumberFromInt32(Smi::kMaxValue)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 130 | CHECK(value->IsSmi()); |
| 131 | CHECK(value->IsNumber()); |
| 132 | CHECK_EQ(Smi::kMaxValue, Smi::cast(value)->value()); |
| 133 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 134 | #ifndef V8_TARGET_ARCH_X64 |
| 135 | // TODO(lrn): We need a NumberFromIntptr function in order to test this. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 136 | value = HEAP->NumberFromInt32(Smi::kMinValue - 1)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 137 | CHECK(value->IsHeapNumber()); |
| 138 | CHECK(value->IsNumber()); |
| 139 | CHECK_EQ(static_cast<double>(Smi::kMinValue - 1), value->Number()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 140 | #endif |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 141 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 142 | MaybeObject* maybe_value = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 143 | HEAP->NumberFromUint32(static_cast<uint32_t>(Smi::kMaxValue) + 1); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 144 | value = maybe_value->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 145 | CHECK(value->IsHeapNumber()); |
| 146 | CHECK(value->IsNumber()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 147 | CHECK_EQ(static_cast<double>(static_cast<uint32_t>(Smi::kMaxValue) + 1), |
| 148 | value->Number()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 149 | |
| 150 | // nan oddball checks |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 151 | CHECK(HEAP->nan_value()->IsNumber()); |
| 152 | CHECK(isnan(HEAP->nan_value()->Number())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 153 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 154 | Handle<String> s = FACTORY->NewStringFromAscii(CStrVector("fisk hest ")); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 155 | CHECK(s->IsString()); |
| 156 | CHECK_EQ(10, s->length()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 157 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 158 | String* object_symbol = String::cast(HEAP->Object_symbol()); |
| 159 | CHECK( |
| 160 | Isolate::Current()->context()->global()->HasLocalProperty(object_symbol)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 161 | |
| 162 | // Check ToString for oddballs |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 163 | CheckOddball(HEAP->true_value(), "true"); |
| 164 | CheckOddball(HEAP->false_value(), "false"); |
| 165 | CheckOddball(HEAP->null_value(), "null"); |
| 166 | CheckOddball(HEAP->undefined_value(), "undefined"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 167 | |
| 168 | // Check ToString for Smis |
| 169 | CheckSmi(0, "0"); |
| 170 | CheckSmi(42, "42"); |
| 171 | CheckSmi(-42, "-42"); |
| 172 | |
| 173 | // Check ToString for Numbers |
| 174 | CheckNumber(1.1, "1.1"); |
| 175 | |
| 176 | CheckFindCodeObject(); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | TEST(Tagging) { |
| 181 | InitializeVM(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 182 | int request = 24; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 183 | CHECK_EQ(request, static_cast<int>(OBJECT_POINTER_ALIGN(request))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 184 | CHECK(Smi::FromInt(42)->IsSmi()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 185 | CHECK(Failure::RetryAfterGC(NEW_SPACE)->IsFailure()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 186 | CHECK_EQ(NEW_SPACE, |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 187 | Failure::RetryAfterGC(NEW_SPACE)->allocation_space()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 188 | CHECK_EQ(OLD_POINTER_SPACE, |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 189 | Failure::RetryAfterGC(OLD_POINTER_SPACE)->allocation_space()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 190 | CHECK(Failure::Exception()->IsFailure()); |
| 191 | CHECK(Smi::FromInt(Smi::kMinValue)->IsSmi()); |
| 192 | CHECK(Smi::FromInt(Smi::kMaxValue)->IsSmi()); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | TEST(GarbageCollection) { |
| 197 | InitializeVM(); |
| 198 | |
| 199 | v8::HandleScope sc; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 200 | // Check GC. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 201 | HEAP->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 202 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 203 | Handle<String> name = FACTORY->LookupAsciiSymbol("theFunction"); |
| 204 | Handle<String> prop_name = FACTORY->LookupAsciiSymbol("theSlot"); |
| 205 | Handle<String> prop_namex = FACTORY->LookupAsciiSymbol("theSlotx"); |
| 206 | Handle<String> obj_name = FACTORY->LookupAsciiSymbol("theObject"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 207 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 208 | { |
| 209 | v8::HandleScope inner_scope; |
| 210 | // Allocate a function and keep it in global object's property. |
| 211 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 212 | FACTORY->NewFunction(name, FACTORY->undefined_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 213 | Handle<Map> initial_map = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 214 | FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 215 | function->set_initial_map(*initial_map); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 216 | Isolate::Current()->context()->global()->SetProperty( |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 217 | *name, *function, NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 218 | // Allocate an object. Unrooted after leaving the scope. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 219 | Handle<JSObject> obj = FACTORY->NewJSObject(function); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 220 | obj->SetProperty( |
| 221 | *prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked(); |
| 222 | obj->SetProperty( |
| 223 | *prop_namex, Smi::FromInt(24), NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 224 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 225 | CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name)); |
| 226 | CHECK_EQ(Smi::FromInt(24), obj->GetProperty(*prop_namex)); |
| 227 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 228 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 229 | HEAP->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 230 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 231 | // Function should be alive. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 232 | CHECK(Isolate::Current()->context()->global()->HasLocalProperty(*name)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 233 | // Check function is retained. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 234 | Object* func_value = Isolate::Current()->context()->global()-> |
| 235 | GetProperty(*name)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 236 | CHECK(func_value->IsJSFunction()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 237 | Handle<JSFunction> function(JSFunction::cast(func_value)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 238 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 239 | { |
| 240 | HandleScope inner_scope; |
| 241 | // Allocate another object, make it reachable from global. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 242 | Handle<JSObject> obj = FACTORY->NewJSObject(function); |
| 243 | Isolate::Current()->context()->global()->SetProperty( |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 244 | *obj_name, *obj, NONE, kNonStrictMode)->ToObjectChecked(); |
| 245 | obj->SetProperty( |
| 246 | *prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 247 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 248 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 249 | // After gc, it should survive. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 250 | HEAP->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 251 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 252 | CHECK(Isolate::Current()->context()->global()->HasLocalProperty(*obj_name)); |
| 253 | CHECK(Isolate::Current()->context()->global()-> |
| 254 | GetProperty(*obj_name)->ToObjectChecked()->IsJSObject()); |
| 255 | Object* obj = Isolate::Current()->context()->global()-> |
| 256 | GetProperty(*obj_name)->ToObjectChecked(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 257 | JSObject* js_obj = JSObject::cast(obj); |
| 258 | CHECK_EQ(Smi::FromInt(23), js_obj->GetProperty(*prop_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | static void VerifyStringAllocation(const char* string) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 263 | v8::HandleScope scope; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 264 | Handle<String> s = FACTORY->NewStringFromUtf8(CStrVector(string)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 265 | CHECK_EQ(StrLength(string), s->length()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 266 | for (int index = 0; index < s->length(); index++) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 267 | CHECK_EQ(static_cast<uint16_t>(string[index]), s->Get(index)); |
| 268 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | TEST(String) { |
| 273 | InitializeVM(); |
| 274 | |
| 275 | VerifyStringAllocation("a"); |
| 276 | VerifyStringAllocation("ab"); |
| 277 | VerifyStringAllocation("abc"); |
| 278 | VerifyStringAllocation("abcd"); |
| 279 | VerifyStringAllocation("fiskerdrengen er paa havet"); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | TEST(LocalHandles) { |
| 284 | InitializeVM(); |
| 285 | |
| 286 | v8::HandleScope scope; |
| 287 | const char* name = "Kasper the spunky"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 288 | Handle<String> string = FACTORY->NewStringFromAscii(CStrVector(name)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 289 | CHECK_EQ(StrLength(name), string->length()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
| 293 | TEST(GlobalHandles) { |
| 294 | InitializeVM(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 295 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 296 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 297 | Handle<Object> h1; |
| 298 | Handle<Object> h2; |
| 299 | Handle<Object> h3; |
| 300 | Handle<Object> h4; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 301 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 302 | { |
| 303 | HandleScope scope; |
| 304 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 305 | Handle<Object> i = FACTORY->NewStringFromAscii(CStrVector("fisk")); |
| 306 | Handle<Object> u = FACTORY->NewNumber(1.12344); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 307 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 308 | h1 = global_handles->Create(*i); |
| 309 | h2 = global_handles->Create(*u); |
| 310 | h3 = global_handles->Create(*i); |
| 311 | h4 = global_handles->Create(*u); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 312 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 313 | |
| 314 | // after gc, it should survive |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 315 | HEAP->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 316 | |
| 317 | CHECK((*h1)->IsString()); |
| 318 | CHECK((*h2)->IsHeapNumber()); |
| 319 | CHECK((*h3)->IsString()); |
| 320 | CHECK((*h4)->IsHeapNumber()); |
| 321 | |
| 322 | CHECK_EQ(*h3, *h1); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 323 | global_handles->Destroy(h1.location()); |
| 324 | global_handles->Destroy(h3.location()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 325 | |
| 326 | CHECK_EQ(*h4, *h2); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 327 | global_handles->Destroy(h2.location()); |
| 328 | global_handles->Destroy(h4.location()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
| 332 | static bool WeakPointerCleared = false; |
| 333 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 334 | static void TestWeakGlobalHandleCallback(v8::Persistent<v8::Value> handle, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 335 | void* id) { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 336 | if (1234 == reinterpret_cast<intptr_t>(id)) WeakPointerCleared = true; |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 337 | handle.Dispose(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
| 341 | TEST(WeakGlobalHandlesScavenge) { |
| 342 | InitializeVM(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 343 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 344 | |
| 345 | WeakPointerCleared = false; |
| 346 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 347 | Handle<Object> h1; |
| 348 | Handle<Object> h2; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 349 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 350 | { |
| 351 | HandleScope scope; |
| 352 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 353 | Handle<Object> i = FACTORY->NewStringFromAscii(CStrVector("fisk")); |
| 354 | Handle<Object> u = FACTORY->NewNumber(1.12344); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 355 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 356 | h1 = global_handles->Create(*i); |
| 357 | h2 = global_handles->Create(*u); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 358 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 359 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 360 | global_handles->MakeWeak(h2.location(), |
| 361 | reinterpret_cast<void*>(1234), |
| 362 | &TestWeakGlobalHandleCallback); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 363 | |
| 364 | // Scavenge treats weak pointers as normal roots. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 365 | HEAP->PerformScavenge(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 366 | |
| 367 | CHECK((*h1)->IsString()); |
| 368 | CHECK((*h2)->IsHeapNumber()); |
| 369 | |
| 370 | CHECK(!WeakPointerCleared); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 371 | CHECK(!global_handles->IsNearDeath(h2.location())); |
| 372 | CHECK(!global_handles->IsNearDeath(h1.location())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 373 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 374 | global_handles->Destroy(h1.location()); |
| 375 | global_handles->Destroy(h2.location()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | |
| 379 | TEST(WeakGlobalHandlesMark) { |
| 380 | InitializeVM(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 381 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 382 | |
| 383 | WeakPointerCleared = false; |
| 384 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 385 | Handle<Object> h1; |
| 386 | Handle<Object> h2; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 387 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 388 | { |
| 389 | HandleScope scope; |
| 390 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 391 | Handle<Object> i = FACTORY->NewStringFromAscii(CStrVector("fisk")); |
| 392 | Handle<Object> u = FACTORY->NewNumber(1.12344); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 393 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 394 | h1 = global_handles->Create(*i); |
| 395 | h2 = global_handles->Create(*u); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 396 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 397 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 398 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
| 399 | HEAP->CollectGarbage(NEW_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 400 | // Make sure the object is promoted. |
| 401 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 402 | global_handles->MakeWeak(h2.location(), |
| 403 | reinterpret_cast<void*>(1234), |
| 404 | &TestWeakGlobalHandleCallback); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 405 | CHECK(!GlobalHandles::IsNearDeath(h1.location())); |
| 406 | CHECK(!GlobalHandles::IsNearDeath(h2.location())); |
| 407 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 408 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 409 | |
| 410 | CHECK((*h1)->IsString()); |
| 411 | |
| 412 | CHECK(WeakPointerCleared); |
| 413 | CHECK(!GlobalHandles::IsNearDeath(h1.location())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 414 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 415 | global_handles->Destroy(h1.location()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | TEST(DeleteWeakGlobalHandle) { |
| 419 | InitializeVM(); |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 420 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 421 | |
| 422 | WeakPointerCleared = false; |
| 423 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 424 | Handle<Object> h; |
| 425 | |
| 426 | { |
| 427 | HandleScope scope; |
| 428 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 429 | Handle<Object> i = FACTORY->NewStringFromAscii(CStrVector("fisk")); |
| 430 | h = global_handles->Create(*i); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 431 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 432 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 433 | global_handles->MakeWeak(h.location(), |
| 434 | reinterpret_cast<void*>(1234), |
| 435 | &TestWeakGlobalHandleCallback); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 436 | |
| 437 | // Scanvenge does not recognize weak reference. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 438 | HEAP->PerformScavenge(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 439 | |
| 440 | CHECK(!WeakPointerCleared); |
| 441 | |
| 442 | // Mark-compact treats weak reference properly. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 443 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 444 | |
| 445 | CHECK(WeakPointerCleared); |
| 446 | } |
| 447 | |
| 448 | static const char* not_so_random_string_table[] = { |
| 449 | "abstract", |
| 450 | "boolean", |
| 451 | "break", |
| 452 | "byte", |
| 453 | "case", |
| 454 | "catch", |
| 455 | "char", |
| 456 | "class", |
| 457 | "const", |
| 458 | "continue", |
| 459 | "debugger", |
| 460 | "default", |
| 461 | "delete", |
| 462 | "do", |
| 463 | "double", |
| 464 | "else", |
| 465 | "enum", |
| 466 | "export", |
| 467 | "extends", |
| 468 | "false", |
| 469 | "final", |
| 470 | "finally", |
| 471 | "float", |
| 472 | "for", |
| 473 | "function", |
| 474 | "goto", |
| 475 | "if", |
| 476 | "implements", |
| 477 | "import", |
| 478 | "in", |
| 479 | "instanceof", |
| 480 | "int", |
| 481 | "interface", |
| 482 | "long", |
| 483 | "native", |
| 484 | "new", |
| 485 | "null", |
| 486 | "package", |
| 487 | "private", |
| 488 | "protected", |
| 489 | "public", |
| 490 | "return", |
| 491 | "short", |
| 492 | "static", |
| 493 | "super", |
| 494 | "switch", |
| 495 | "synchronized", |
| 496 | "this", |
| 497 | "throw", |
| 498 | "throws", |
| 499 | "transient", |
| 500 | "true", |
| 501 | "try", |
| 502 | "typeof", |
| 503 | "var", |
| 504 | "void", |
| 505 | "volatile", |
| 506 | "while", |
| 507 | "with", |
| 508 | 0 |
| 509 | }; |
| 510 | |
| 511 | |
| 512 | static void CheckSymbols(const char** strings) { |
| 513 | for (const char* string = *strings; *strings != 0; string = *strings++) { |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 514 | Object* a; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 515 | MaybeObject* maybe_a = HEAP->LookupAsciiSymbol(string); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 516 | // LookupAsciiSymbol may return a failure if a GC is needed. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 517 | if (!maybe_a->ToObject(&a)) continue; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 518 | CHECK(a->IsSymbol()); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 519 | Object* b; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 520 | MaybeObject* maybe_b = HEAP->LookupAsciiSymbol(string); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 521 | if (!maybe_b->ToObject(&b)) continue; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 522 | CHECK_EQ(b, a); |
| 523 | CHECK(String::cast(b)->IsEqualTo(CStrVector(string))); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | |
| 528 | TEST(SymbolTable) { |
| 529 | InitializeVM(); |
| 530 | |
| 531 | CheckSymbols(not_so_random_string_table); |
| 532 | CheckSymbols(not_so_random_string_table); |
| 533 | } |
| 534 | |
| 535 | |
| 536 | TEST(FunctionAllocation) { |
| 537 | InitializeVM(); |
| 538 | |
| 539 | v8::HandleScope sc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 540 | Handle<String> name = FACTORY->LookupAsciiSymbol("theFunction"); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 541 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 542 | FACTORY->NewFunction(name, FACTORY->undefined_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 543 | Handle<Map> initial_map = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 544 | FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 545 | function->set_initial_map(*initial_map); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 546 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 547 | Handle<String> prop_name = FACTORY->LookupAsciiSymbol("theSlot"); |
| 548 | Handle<JSObject> obj = FACTORY->NewJSObject(function); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 549 | obj->SetProperty( |
| 550 | *prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 551 | CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 552 | // Check that we can add properties to function objects. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 553 | function->SetProperty( |
| 554 | *prop_name, Smi::FromInt(24), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 555 | CHECK_EQ(Smi::FromInt(24), function->GetProperty(*prop_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
| 559 | TEST(ObjectProperties) { |
| 560 | InitializeVM(); |
| 561 | |
| 562 | v8::HandleScope sc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 563 | String* object_symbol = String::cast(HEAP->Object_symbol()); |
| 564 | Object* raw_object = Isolate::Current()->context()->global()-> |
| 565 | GetProperty(object_symbol)->ToObjectChecked(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 566 | JSFunction* object_function = JSFunction::cast(raw_object); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 567 | Handle<JSFunction> constructor(object_function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 568 | Handle<JSObject> obj = FACTORY->NewJSObject(constructor); |
| 569 | Handle<String> first = FACTORY->LookupAsciiSymbol("first"); |
| 570 | Handle<String> second = FACTORY->LookupAsciiSymbol("second"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 571 | |
| 572 | // check for empty |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 573 | CHECK(!obj->HasLocalProperty(*first)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 574 | |
| 575 | // add first |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 576 | obj->SetProperty( |
| 577 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 578 | CHECK(obj->HasLocalProperty(*first)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 579 | |
| 580 | // delete first |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 581 | CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION)); |
| 582 | CHECK(!obj->HasLocalProperty(*first)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 583 | |
| 584 | // add first and then second |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 585 | obj->SetProperty( |
| 586 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
| 587 | obj->SetProperty( |
| 588 | *second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 589 | CHECK(obj->HasLocalProperty(*first)); |
| 590 | CHECK(obj->HasLocalProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 591 | |
| 592 | // delete first and then second |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 593 | CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION)); |
| 594 | CHECK(obj->HasLocalProperty(*second)); |
| 595 | CHECK(obj->DeleteProperty(*second, JSObject::NORMAL_DELETION)); |
| 596 | CHECK(!obj->HasLocalProperty(*first)); |
| 597 | CHECK(!obj->HasLocalProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 598 | |
| 599 | // add first and then second |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 600 | obj->SetProperty( |
| 601 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
| 602 | obj->SetProperty( |
| 603 | *second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 604 | CHECK(obj->HasLocalProperty(*first)); |
| 605 | CHECK(obj->HasLocalProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 606 | |
| 607 | // delete second and then first |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 608 | CHECK(obj->DeleteProperty(*second, JSObject::NORMAL_DELETION)); |
| 609 | CHECK(obj->HasLocalProperty(*first)); |
| 610 | CHECK(obj->DeleteProperty(*first, JSObject::NORMAL_DELETION)); |
| 611 | CHECK(!obj->HasLocalProperty(*first)); |
| 612 | CHECK(!obj->HasLocalProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 613 | |
| 614 | // check string and symbol match |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 615 | const char* string1 = "fisk"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 616 | Handle<String> s1 = FACTORY->NewStringFromAscii(CStrVector(string1)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 617 | obj->SetProperty( |
| 618 | *s1, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 619 | Handle<String> s1_symbol = FACTORY->LookupAsciiSymbol(string1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 620 | CHECK(obj->HasLocalProperty(*s1_symbol)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 621 | |
| 622 | // check symbol and string match |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 623 | const char* string2 = "fugl"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 624 | Handle<String> s2_symbol = FACTORY->LookupAsciiSymbol(string2); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 625 | obj->SetProperty( |
| 626 | *s2_symbol, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 627 | Handle<String> s2 = FACTORY->NewStringFromAscii(CStrVector(string2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 628 | CHECK(obj->HasLocalProperty(*s2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | |
| 632 | TEST(JSObjectMaps) { |
| 633 | InitializeVM(); |
| 634 | |
| 635 | v8::HandleScope sc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 636 | Handle<String> name = FACTORY->LookupAsciiSymbol("theFunction"); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 637 | Handle<JSFunction> function = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 638 | FACTORY->NewFunction(name, FACTORY->undefined_value()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 639 | Handle<Map> initial_map = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 640 | FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 641 | function->set_initial_map(*initial_map); |
| 642 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 643 | Handle<String> prop_name = FACTORY->LookupAsciiSymbol("theSlot"); |
| 644 | Handle<JSObject> obj = FACTORY->NewJSObject(function); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 645 | |
| 646 | // Set a propery |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 647 | obj->SetProperty( |
| 648 | *prop_name, Smi::FromInt(23), NONE, kNonStrictMode)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 649 | CHECK_EQ(Smi::FromInt(23), obj->GetProperty(*prop_name)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 650 | |
| 651 | // Check the map has changed |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 652 | CHECK(*initial_map != obj->map()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | |
| 656 | TEST(JSArray) { |
| 657 | InitializeVM(); |
| 658 | |
| 659 | v8::HandleScope sc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 660 | Handle<String> name = FACTORY->LookupAsciiSymbol("Array"); |
| 661 | Object* raw_object = Isolate::Current()->context()->global()-> |
| 662 | GetProperty(*name)->ToObjectChecked(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 663 | Handle<JSFunction> function = Handle<JSFunction>( |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 664 | JSFunction::cast(raw_object)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 665 | |
| 666 | // Allocate the object. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 667 | Handle<JSObject> object = FACTORY->NewJSObject(function); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 668 | Handle<JSArray> array = Handle<JSArray>::cast(object); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 669 | // We just initialized the VM, no heap allocation failure yet. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 670 | array->Initialize(0)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 671 | |
| 672 | // Set array length to 0. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 673 | array->SetElementsLength(Smi::FromInt(0))->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 674 | CHECK_EQ(Smi::FromInt(0), array->length()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 675 | // Must be in fast mode. |
| 676 | CHECK(array->HasFastTypeElements()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 677 | |
| 678 | // array[length] = name. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 679 | array->SetElement(0, *name, NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 680 | CHECK_EQ(Smi::FromInt(1), array->length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 681 | CHECK_EQ(array->GetElement(0), *name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 682 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 683 | // Set array length with larger than smi value. |
| 684 | Handle<Object> length = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 685 | FACTORY->NewNumberFromUint(static_cast<uint32_t>(Smi::kMaxValue) + 1); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 686 | array->SetElementsLength(*length)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 687 | |
| 688 | uint32_t int_length = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 689 | CHECK(length->ToArrayIndex(&int_length)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 690 | CHECK_EQ(*length, array->length()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 691 | CHECK(array->HasDictionaryElements()); // Must be in slow mode. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 692 | |
| 693 | // array[length] = name. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 694 | array->SetElement(int_length, *name, NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 695 | uint32_t new_int_length = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 696 | CHECK(array->length()->ToArrayIndex(&new_int_length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 697 | CHECK_EQ(static_cast<double>(int_length), new_int_length - 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 698 | CHECK_EQ(array->GetElement(int_length), *name); |
| 699 | CHECK_EQ(array->GetElement(0), *name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | |
| 703 | TEST(JSObjectCopy) { |
| 704 | InitializeVM(); |
| 705 | |
| 706 | v8::HandleScope sc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 707 | String* object_symbol = String::cast(HEAP->Object_symbol()); |
| 708 | Object* raw_object = Isolate::Current()->context()->global()-> |
| 709 | GetProperty(object_symbol)->ToObjectChecked(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 710 | JSFunction* object_function = JSFunction::cast(raw_object); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 711 | Handle<JSFunction> constructor(object_function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 712 | Handle<JSObject> obj = FACTORY->NewJSObject(constructor); |
| 713 | Handle<String> first = FACTORY->LookupAsciiSymbol("first"); |
| 714 | Handle<String> second = FACTORY->LookupAsciiSymbol("second"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 715 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 716 | obj->SetProperty( |
| 717 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
| 718 | obj->SetProperty( |
| 719 | *second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 720 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 721 | obj->SetElement(0, *first, NONE, kNonStrictMode)->ToObjectChecked(); |
| 722 | obj->SetElement(1, *second, NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 723 | |
| 724 | // Make the clone. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 725 | Handle<JSObject> clone = Copy(obj); |
| 726 | CHECK(!clone.is_identical_to(obj)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 727 | |
| 728 | CHECK_EQ(obj->GetElement(0), clone->GetElement(0)); |
| 729 | CHECK_EQ(obj->GetElement(1), clone->GetElement(1)); |
| 730 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 731 | CHECK_EQ(obj->GetProperty(*first), clone->GetProperty(*first)); |
| 732 | CHECK_EQ(obj->GetProperty(*second), clone->GetProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 733 | |
| 734 | // Flip the values. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 735 | clone->SetProperty( |
| 736 | *first, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
| 737 | clone->SetProperty( |
| 738 | *second, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 739 | |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 740 | clone->SetElement(0, *second, NONE, kNonStrictMode)->ToObjectChecked(); |
| 741 | clone->SetElement(1, *first, NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 742 | |
| 743 | CHECK_EQ(obj->GetElement(1), clone->GetElement(0)); |
| 744 | CHECK_EQ(obj->GetElement(0), clone->GetElement(1)); |
| 745 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 746 | CHECK_EQ(obj->GetProperty(*second), clone->GetProperty(*first)); |
| 747 | CHECK_EQ(obj->GetProperty(*first), clone->GetProperty(*second)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | |
| 751 | TEST(StringAllocation) { |
| 752 | InitializeVM(); |
| 753 | |
| 754 | |
| 755 | const unsigned char chars[] = { 0xe5, 0xa4, 0xa7 }; |
| 756 | for (int length = 0; length < 100; length++) { |
| 757 | v8::HandleScope scope; |
| 758 | char* non_ascii = NewArray<char>(3 * length + 1); |
| 759 | char* ascii = NewArray<char>(length + 1); |
| 760 | non_ascii[3 * length] = 0; |
| 761 | ascii[length] = 0; |
| 762 | for (int i = 0; i < length; i++) { |
| 763 | ascii[i] = 'a'; |
| 764 | non_ascii[3 * i] = chars[0]; |
| 765 | non_ascii[3 * i + 1] = chars[1]; |
| 766 | non_ascii[3 * i + 2] = chars[2]; |
| 767 | } |
| 768 | Handle<String> non_ascii_sym = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 769 | FACTORY->LookupSymbol(Vector<const char>(non_ascii, 3 * length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 770 | CHECK_EQ(length, non_ascii_sym->length()); |
| 771 | Handle<String> ascii_sym = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 772 | FACTORY->LookupSymbol(Vector<const char>(ascii, length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 773 | CHECK_EQ(length, ascii_sym->length()); |
| 774 | Handle<String> non_ascii_str = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 775 | FACTORY->NewStringFromUtf8(Vector<const char>(non_ascii, 3 * length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 776 | non_ascii_str->Hash(); |
| 777 | CHECK_EQ(length, non_ascii_str->length()); |
| 778 | Handle<String> ascii_str = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 779 | FACTORY->NewStringFromUtf8(Vector<const char>(ascii, length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 780 | ascii_str->Hash(); |
| 781 | CHECK_EQ(length, ascii_str->length()); |
| 782 | DeleteArray(non_ascii); |
| 783 | DeleteArray(ascii); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | |
| 788 | static int ObjectsFoundInHeap(Handle<Object> objs[], int size) { |
| 789 | // Count the number of objects found in the heap. |
| 790 | int found_count = 0; |
| 791 | HeapIterator iterator; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 792 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 793 | for (int i = 0; i < size; i++) { |
| 794 | if (*objs[i] == obj) { |
| 795 | found_count++; |
| 796 | } |
| 797 | } |
| 798 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 799 | return found_count; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | TEST(Iteration) { |
| 804 | InitializeVM(); |
| 805 | v8::HandleScope scope; |
| 806 | |
| 807 | // Array of objects to scan haep for. |
| 808 | const int objs_count = 6; |
| 809 | Handle<Object> objs[objs_count]; |
| 810 | int next_objs_index = 0; |
| 811 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 812 | // Allocate a JS array to OLD_POINTER_SPACE and NEW_SPACE |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 813 | objs[next_objs_index++] = FACTORY->NewJSArray(10); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 814 | objs[next_objs_index++] = FACTORY->NewJSArray(10, FAST_ELEMENTS, TENURED); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 815 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 816 | // Allocate a small string to OLD_DATA_SPACE and NEW_SPACE |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 817 | objs[next_objs_index++] = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 818 | FACTORY->NewStringFromAscii(CStrVector("abcdefghij")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 819 | objs[next_objs_index++] = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 820 | FACTORY->NewStringFromAscii(CStrVector("abcdefghij"), TENURED); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 821 | |
| 822 | // Allocate a large string (for large object space). |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 823 | int large_size = Page::kMaxNonCodeHeapObjectSize + 1; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 824 | char* str = new char[large_size]; |
| 825 | for (int i = 0; i < large_size - 1; ++i) str[i] = 'a'; |
| 826 | str[large_size - 1] = '\0'; |
| 827 | objs[next_objs_index++] = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 828 | FACTORY->NewStringFromAscii(CStrVector(str), TENURED); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 829 | delete[] str; |
| 830 | |
| 831 | // Add a Map object to look for. |
| 832 | objs[next_objs_index++] = Handle<Map>(HeapObject::cast(*objs[0])->map()); |
| 833 | |
| 834 | CHECK_EQ(objs_count, next_objs_index); |
| 835 | CHECK_EQ(objs_count, ObjectsFoundInHeap(objs, objs_count)); |
| 836 | } |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 837 | |
| 838 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 839 | TEST(EmptyHandleEscapeFrom) { |
| 840 | InitializeVM(); |
| 841 | |
| 842 | v8::HandleScope scope; |
| 843 | Handle<JSObject> runaway; |
| 844 | |
| 845 | { |
| 846 | v8::HandleScope nested; |
| 847 | Handle<JSObject> empty; |
| 848 | runaway = empty.EscapeFrom(&nested); |
| 849 | } |
| 850 | |
| 851 | CHECK(runaway.is_null()); |
| 852 | } |
| 853 | |
| 854 | |
| 855 | static int LenFromSize(int size) { |
| 856 | return (size - FixedArray::kHeaderSize) / kPointerSize; |
| 857 | } |
| 858 | |
| 859 | |
| 860 | TEST(Regression39128) { |
| 861 | // Test case for crbug.com/39128. |
| 862 | InitializeVM(); |
| 863 | |
| 864 | // Increase the chance of 'bump-the-pointer' allocation in old space. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 865 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 866 | |
| 867 | v8::HandleScope scope; |
| 868 | |
| 869 | // The plan: create JSObject which references objects in new space. |
| 870 | // Then clone this object (forcing it to go into old space) and check |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 871 | // that region dirty marks are updated correctly. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 872 | |
| 873 | // Step 1: prepare a map for the object. We add 1 inobject property to it. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 874 | Handle<JSFunction> object_ctor( |
| 875 | Isolate::Current()->global_context()->object_function()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 876 | CHECK(object_ctor->has_initial_map()); |
| 877 | Handle<Map> object_map(object_ctor->initial_map()); |
| 878 | // Create a map with single inobject property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 879 | Handle<Map> my_map = FACTORY->CopyMap(object_map, 1); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 880 | int n_properties = my_map->inobject_properties(); |
| 881 | CHECK_GT(n_properties, 0); |
| 882 | |
| 883 | int object_size = my_map->instance_size(); |
| 884 | |
| 885 | // Step 2: allocate a lot of objects so to almost fill new space: we need |
| 886 | // just enough room to allocate JSObject and thus fill the newspace. |
| 887 | |
| 888 | int allocation_amount = Min(FixedArray::kMaxSize, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 889 | HEAP->MaxObjectSizeInNewSpace()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 890 | int allocation_len = LenFromSize(allocation_amount); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 891 | NewSpace* new_space = HEAP->new_space(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 892 | Address* top_addr = new_space->allocation_top_address(); |
| 893 | Address* limit_addr = new_space->allocation_limit_address(); |
| 894 | while ((*limit_addr - *top_addr) > allocation_amount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 895 | CHECK(!HEAP->always_allocate()); |
| 896 | Object* array = HEAP->AllocateFixedArray(allocation_len)->ToObjectChecked(); |
| 897 | CHECK(!array->IsFailure()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 898 | CHECK(new_space->Contains(array)); |
| 899 | } |
| 900 | |
| 901 | // Step 3: now allocate fixed array and JSObject to fill the whole new space. |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 902 | int to_fill = static_cast<int>(*limit_addr - *top_addr - object_size); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 903 | int fixed_array_len = LenFromSize(to_fill); |
| 904 | CHECK(fixed_array_len < FixedArray::kMaxLength); |
| 905 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 906 | CHECK(!HEAP->always_allocate()); |
| 907 | Object* array = HEAP->AllocateFixedArray(fixed_array_len)->ToObjectChecked(); |
| 908 | CHECK(!array->IsFailure()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 909 | CHECK(new_space->Contains(array)); |
| 910 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 911 | Object* object = HEAP->AllocateJSObjectFromMap(*my_map)->ToObjectChecked(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 912 | CHECK(new_space->Contains(object)); |
| 913 | JSObject* jsobject = JSObject::cast(object); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 914 | CHECK_EQ(0, FixedArray::cast(jsobject->elements())->length()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 915 | CHECK_EQ(0, jsobject->properties()->length()); |
| 916 | // Create a reference to object in new space in jsobject. |
| 917 | jsobject->FastPropertyAtPut(-1, array); |
| 918 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 919 | CHECK_EQ(0, static_cast<int>(*limit_addr - *top_addr)); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 920 | |
| 921 | // Step 4: clone jsobject, but force always allocate first to create a clone |
| 922 | // in old pointer space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 923 | Address old_pointer_space_top = HEAP->old_pointer_space()->top(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 924 | AlwaysAllocateScope aa_scope; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 925 | Object* clone_obj = HEAP->CopyJSObject(jsobject)->ToObjectChecked(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 926 | JSObject* clone = JSObject::cast(clone_obj); |
| 927 | if (clone->address() != old_pointer_space_top) { |
| 928 | // Alas, got allocated from free list, we cannot do checks. |
| 929 | return; |
| 930 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 931 | CHECK(HEAP->old_pointer_space()->Contains(clone->address())); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 932 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 933 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 934 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 935 | TEST(TestCodeFlushing) { |
| 936 | i::FLAG_allow_natives_syntax = true; |
ricow@chromium.org | fd0930e | 2010-06-11 10:37:34 +0000 | [diff] [blame] | 937 | // If we do not flush code this test is invalid. |
| 938 | if (!FLAG_flush_code) return; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 939 | InitializeVM(); |
| 940 | v8::HandleScope scope; |
| 941 | const char* source = "function foo() {" |
| 942 | " var x = 42;" |
| 943 | " var y = 42;" |
| 944 | " var z = x + y;" |
| 945 | "};" |
| 946 | "foo()"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 947 | Handle<String> foo_name = FACTORY->LookupAsciiSymbol("foo"); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 948 | |
| 949 | // This compile will add the code to the compilation cache. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 950 | { v8::HandleScope scope; |
| 951 | CompileRun(source); |
| 952 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 953 | |
| 954 | // Check function is compiled. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 955 | Object* func_value = Isolate::Current()->context()->global()-> |
| 956 | GetProperty(*foo_name)->ToObjectChecked(); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 957 | CHECK(func_value->IsJSFunction()); |
| 958 | Handle<JSFunction> function(JSFunction::cast(func_value)); |
| 959 | CHECK(function->shared()->is_compiled()); |
| 960 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 961 | // TODO(1609) Currently incremental marker does not support code flushing. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 962 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 963 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 964 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 965 | CHECK(function->shared()->is_compiled()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 966 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame^] | 967 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 968 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 969 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 970 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 971 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
| 972 | HEAP->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 973 | |
| 974 | // foo should no longer be in the compilation cache |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 975 | CHECK(!function->shared()->is_compiled() || function->IsOptimized()); |
| 976 | CHECK(!function->is_compiled() || function->IsOptimized()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 977 | // Call foo to get it recompiled. |
| 978 | CompileRun("foo()"); |
| 979 | CHECK(function->shared()->is_compiled()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 980 | CHECK(function->is_compiled()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 981 | } |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 982 | |
| 983 | |
| 984 | // Count the number of global contexts in the weak list of global contexts. |
| 985 | static int CountGlobalContexts() { |
| 986 | int count = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 987 | Object* object = HEAP->global_contexts_list(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 988 | while (!object->IsUndefined()) { |
| 989 | count++; |
| 990 | object = Context::cast(object)->get(Context::NEXT_CONTEXT_LINK); |
| 991 | } |
| 992 | return count; |
| 993 | } |
| 994 | |
| 995 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 996 | // Count the number of user functions in the weak list of optimized |
| 997 | // functions attached to a global context. |
| 998 | static int CountOptimizedUserFunctions(v8::Handle<v8::Context> context) { |
| 999 | int count = 0; |
| 1000 | Handle<Context> icontext = v8::Utils::OpenHandle(*context); |
| 1001 | Object* object = icontext->get(Context::OPTIMIZED_FUNCTIONS_LIST); |
| 1002 | while (object->IsJSFunction() && !JSFunction::cast(object)->IsBuiltin()) { |
| 1003 | count++; |
| 1004 | object = JSFunction::cast(object)->next_function_link(); |
| 1005 | } |
| 1006 | return count; |
| 1007 | } |
| 1008 | |
| 1009 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1010 | TEST(TestInternalWeakLists) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1011 | v8::V8::Initialize(); |
| 1012 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1013 | static const int kNumTestContexts = 10; |
| 1014 | |
| 1015 | v8::HandleScope scope; |
| 1016 | v8::Persistent<v8::Context> ctx[kNumTestContexts]; |
| 1017 | |
| 1018 | CHECK_EQ(0, CountGlobalContexts()); |
| 1019 | |
| 1020 | // Create a number of global contests which gets linked together. |
| 1021 | for (int i = 0; i < kNumTestContexts; i++) { |
| 1022 | ctx[i] = v8::Context::New(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1023 | |
| 1024 | bool opt = (FLAG_always_opt && i::V8::UseCrankshaft()); |
| 1025 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1026 | CHECK_EQ(i + 1, CountGlobalContexts()); |
| 1027 | |
| 1028 | ctx[i]->Enter(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Create a handle scope so no function objects get stuch in the outer |
| 1031 | // handle scope |
| 1032 | v8::HandleScope scope; |
| 1033 | const char* source = "function f1() { };" |
| 1034 | "function f2() { };" |
| 1035 | "function f3() { };" |
| 1036 | "function f4() { };" |
| 1037 | "function f5() { };"; |
| 1038 | CompileRun(source); |
| 1039 | CHECK_EQ(0, CountOptimizedUserFunctions(ctx[i])); |
| 1040 | CompileRun("f1()"); |
| 1041 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1042 | CompileRun("f2()"); |
| 1043 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1044 | CompileRun("f3()"); |
| 1045 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1046 | CompileRun("f4()"); |
| 1047 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1048 | CompileRun("f5()"); |
| 1049 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1050 | |
| 1051 | // Remove function f1, and |
| 1052 | CompileRun("f1=null"); |
| 1053 | |
| 1054 | // Scavenge treats these references as strong. |
| 1055 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1056 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1057 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1058 | } |
| 1059 | |
| 1060 | // Mark compact handles the weak references. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1061 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1062 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1063 | |
| 1064 | // Get rid of f3 and f5 in the same way. |
| 1065 | CompileRun("f3=null"); |
| 1066 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1067 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1068 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1069 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1070 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1071 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1072 | CompileRun("f5=null"); |
| 1073 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1074 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1075 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1076 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1077 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1078 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1079 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1080 | ctx[i]->Exit(); |
| 1081 | } |
| 1082 | |
| 1083 | // Force compilation cache cleanup. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1084 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1085 | |
| 1086 | // Dispose the global contexts one by one. |
| 1087 | for (int i = 0; i < kNumTestContexts; i++) { |
| 1088 | ctx[i].Dispose(); |
| 1089 | ctx[i].Clear(); |
| 1090 | |
| 1091 | // Scavenge treats these references as strong. |
| 1092 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1093 | HEAP->PerformScavenge(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1094 | CHECK_EQ(kNumTestContexts - i, CountGlobalContexts()); |
| 1095 | } |
| 1096 | |
| 1097 | // Mark compact handles the weak references. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1098 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1099 | CHECK_EQ(kNumTestContexts - i - 1, CountGlobalContexts()); |
| 1100 | } |
| 1101 | |
| 1102 | CHECK_EQ(0, CountGlobalContexts()); |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | // Count the number of global contexts in the weak list of global contexts |
| 1107 | // causing a GC after the specified number of elements. |
| 1108 | static int CountGlobalContextsWithGC(int n) { |
| 1109 | int count = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1110 | Handle<Object> object(HEAP->global_contexts_list()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1111 | while (!object->IsUndefined()) { |
| 1112 | count++; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1113 | if (count == n) HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1114 | object = |
| 1115 | Handle<Object>(Context::cast(*object)->get(Context::NEXT_CONTEXT_LINK)); |
| 1116 | } |
| 1117 | return count; |
| 1118 | } |
| 1119 | |
| 1120 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1121 | // Count the number of user functions in the weak list of optimized |
| 1122 | // functions attached to a global context causing a GC after the |
| 1123 | // specified number of elements. |
| 1124 | static int CountOptimizedUserFunctionsWithGC(v8::Handle<v8::Context> context, |
| 1125 | int n) { |
| 1126 | int count = 0; |
| 1127 | Handle<Context> icontext = v8::Utils::OpenHandle(*context); |
| 1128 | Handle<Object> object(icontext->get(Context::OPTIMIZED_FUNCTIONS_LIST)); |
| 1129 | while (object->IsJSFunction() && |
| 1130 | !Handle<JSFunction>::cast(object)->IsBuiltin()) { |
| 1131 | count++; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1132 | if (count == n) HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1133 | object = Handle<Object>( |
| 1134 | Object::cast(JSFunction::cast(*object)->next_function_link())); |
| 1135 | } |
| 1136 | return count; |
| 1137 | } |
| 1138 | |
| 1139 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1140 | TEST(TestInternalWeakListsTraverseWithGC) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1141 | v8::V8::Initialize(); |
| 1142 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1143 | static const int kNumTestContexts = 10; |
| 1144 | |
| 1145 | v8::HandleScope scope; |
| 1146 | v8::Persistent<v8::Context> ctx[kNumTestContexts]; |
| 1147 | |
| 1148 | CHECK_EQ(0, CountGlobalContexts()); |
| 1149 | |
| 1150 | // Create an number of contexts and check the length of the weak list both |
| 1151 | // with and without GCs while iterating the list. |
| 1152 | for (int i = 0; i < kNumTestContexts; i++) { |
| 1153 | ctx[i] = v8::Context::New(); |
| 1154 | CHECK_EQ(i + 1, CountGlobalContexts()); |
| 1155 | CHECK_EQ(i + 1, CountGlobalContextsWithGC(i / 2 + 1)); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1156 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1157 | |
| 1158 | bool opt = (FLAG_always_opt && i::V8::UseCrankshaft()); |
| 1159 | |
| 1160 | // Compile a number of functions the length of the weak list of optimized |
| 1161 | // functions both with and without GCs while iterating the list. |
| 1162 | ctx[0]->Enter(); |
| 1163 | const char* source = "function f1() { };" |
| 1164 | "function f2() { };" |
| 1165 | "function f3() { };" |
| 1166 | "function f4() { };" |
| 1167 | "function f5() { };"; |
| 1168 | CompileRun(source); |
| 1169 | CHECK_EQ(0, CountOptimizedUserFunctions(ctx[0])); |
| 1170 | CompileRun("f1()"); |
| 1171 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1172 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1173 | CompileRun("f2()"); |
| 1174 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1175 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1176 | CompileRun("f3()"); |
| 1177 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1178 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1179 | CompileRun("f4()"); |
| 1180 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1181 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 2)); |
| 1182 | CompileRun("f5()"); |
| 1183 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1184 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 4)); |
| 1185 | |
| 1186 | ctx[0]->Exit(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1187 | } |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1188 | |
| 1189 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1190 | TEST(TestSizeOfObjects) { |
| 1191 | v8::V8::Initialize(); |
| 1192 | |
| 1193 | // Get initial heap size after several full GCs, which will stabilize |
| 1194 | // the heap size and return with sweeping finished completely. |
| 1195 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1196 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1197 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1198 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1199 | CHECK(HEAP->old_pointer_space()->IsSweepingComplete()); |
| 1200 | int initial_size = static_cast<int>(HEAP->SizeOfObjects()); |
| 1201 | |
| 1202 | { |
| 1203 | // Allocate objects on several different old-space pages so that |
| 1204 | // lazy sweeping kicks in for subsequent GC runs. |
| 1205 | AlwaysAllocateScope always_allocate; |
| 1206 | int filler_size = static_cast<int>(FixedArray::SizeFor(8192)); |
| 1207 | for (int i = 1; i <= 100; i++) { |
| 1208 | HEAP->AllocateFixedArray(8192, TENURED)->ToObjectChecked(); |
| 1209 | CHECK_EQ(initial_size + i * filler_size, |
| 1210 | static_cast<int>(HEAP->SizeOfObjects())); |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | // The heap size should go back to initial size after a full GC, even |
| 1215 | // though sweeping didn't finish yet. |
| 1216 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1217 | CHECK(!HEAP->old_pointer_space()->IsSweepingComplete()); |
| 1218 | CHECK_EQ(initial_size, static_cast<int>(HEAP->SizeOfObjects())); |
| 1219 | |
| 1220 | // Advancing the sweeper step-wise should not change the heap size. |
| 1221 | while (!HEAP->old_pointer_space()->IsSweepingComplete()) { |
| 1222 | HEAP->old_pointer_space()->AdvanceSweeper(KB); |
| 1223 | CHECK_EQ(initial_size, static_cast<int>(HEAP->SizeOfObjects())); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1228 | TEST(TestSizeOfObjectsVsHeapIteratorPrecision) { |
| 1229 | InitializeVM(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1230 | HEAP->EnsureHeapIsIterable(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1231 | intptr_t size_of_objects_1 = HEAP->SizeOfObjects(); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1232 | HeapIterator iterator; |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1233 | intptr_t size_of_objects_2 = 0; |
| 1234 | for (HeapObject* obj = iterator.next(); |
| 1235 | obj != NULL; |
| 1236 | obj = iterator.next()) { |
| 1237 | size_of_objects_2 += obj->Size(); |
| 1238 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1239 | // Delta must be within 5% of the larger result. |
| 1240 | // TODO(gc): Tighten this up by distinguishing between byte |
| 1241 | // arrays that are real and those that merely mark free space |
| 1242 | // on the heap. |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1243 | if (size_of_objects_1 > size_of_objects_2) { |
| 1244 | intptr_t delta = size_of_objects_1 - size_of_objects_2; |
| 1245 | PrintF("Heap::SizeOfObjects: %" V8_PTR_PREFIX "d, " |
| 1246 | "Iterator: %" V8_PTR_PREFIX "d, " |
| 1247 | "delta: %" V8_PTR_PREFIX "d\n", |
| 1248 | size_of_objects_1, size_of_objects_2, delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1249 | CHECK_GT(size_of_objects_1 / 20, delta); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1250 | } else { |
| 1251 | intptr_t delta = size_of_objects_2 - size_of_objects_1; |
| 1252 | PrintF("Heap::SizeOfObjects: %" V8_PTR_PREFIX "d, " |
| 1253 | "Iterator: %" V8_PTR_PREFIX "d, " |
| 1254 | "delta: %" V8_PTR_PREFIX "d\n", |
| 1255 | size_of_objects_1, size_of_objects_2, delta); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1256 | CHECK_GT(size_of_objects_2 / 20, delta); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1259 | |
| 1260 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1261 | static void FillUpNewSpace(NewSpace* new_space) { |
| 1262 | // Fill up new space to the point that it is completely full. Make sure |
| 1263 | // that the scavenger does not undo the filling. |
| 1264 | v8::HandleScope scope; |
| 1265 | AlwaysAllocateScope always_allocate; |
| 1266 | intptr_t available = new_space->EffectiveCapacity() - new_space->Size(); |
| 1267 | intptr_t number_of_fillers = (available / FixedArray::SizeFor(1000)) - 10; |
| 1268 | for (intptr_t i = 0; i < number_of_fillers; i++) { |
| 1269 | CHECK(HEAP->InNewSpace(*FACTORY->NewFixedArray(1000, NOT_TENURED))); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1274 | TEST(GrowAndShrinkNewSpace) { |
| 1275 | InitializeVM(); |
| 1276 | NewSpace* new_space = HEAP->new_space(); |
| 1277 | |
| 1278 | // Explicitly growing should double the space capacity. |
| 1279 | intptr_t old_capacity, new_capacity; |
| 1280 | old_capacity = new_space->Capacity(); |
| 1281 | new_space->Grow(); |
| 1282 | new_capacity = new_space->Capacity(); |
| 1283 | CHECK(2 * old_capacity == new_capacity); |
| 1284 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1285 | old_capacity = new_space->Capacity(); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1286 | FillUpNewSpace(new_space); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1287 | new_capacity = new_space->Capacity(); |
| 1288 | CHECK(old_capacity == new_capacity); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1289 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1290 | // Explicitly shrinking should not affect space capacity. |
| 1291 | old_capacity = new_space->Capacity(); |
| 1292 | new_space->Shrink(); |
| 1293 | new_capacity = new_space->Capacity(); |
| 1294 | CHECK(old_capacity == new_capacity); |
| 1295 | |
| 1296 | // Let the scavenger empty the new space. |
| 1297 | HEAP->CollectGarbage(NEW_SPACE); |
| 1298 | CHECK_LE(new_space->Size(), old_capacity); |
| 1299 | |
| 1300 | // Explicitly shrinking should halve the space capacity. |
| 1301 | old_capacity = new_space->Capacity(); |
| 1302 | new_space->Shrink(); |
| 1303 | new_capacity = new_space->Capacity(); |
| 1304 | CHECK(old_capacity == 2 * new_capacity); |
| 1305 | |
| 1306 | // Consecutive shrinking should not affect space capacity. |
| 1307 | old_capacity = new_space->Capacity(); |
| 1308 | new_space->Shrink(); |
| 1309 | new_space->Shrink(); |
| 1310 | new_space->Shrink(); |
| 1311 | new_capacity = new_space->Capacity(); |
| 1312 | CHECK(old_capacity == new_capacity); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1313 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 1314 | |
| 1315 | |
| 1316 | TEST(CollectingAllAvailableGarbageShrinksNewSpace) { |
| 1317 | InitializeVM(); |
| 1318 | v8::HandleScope scope; |
| 1319 | NewSpace* new_space = HEAP->new_space(); |
| 1320 | intptr_t old_capacity, new_capacity; |
| 1321 | old_capacity = new_space->Capacity(); |
| 1322 | new_space->Grow(); |
| 1323 | new_capacity = new_space->Capacity(); |
| 1324 | CHECK(2 * old_capacity == new_capacity); |
| 1325 | FillUpNewSpace(new_space); |
| 1326 | HEAP->CollectAllAvailableGarbage(); |
| 1327 | new_capacity = new_space->Capacity(); |
| 1328 | CHECK(old_capacity == new_capacity); |
| 1329 | } |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 1330 | |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1331 | |
| 1332 | static int NumberOfGlobalObjects() { |
| 1333 | int count = 0; |
| 1334 | HeapIterator iterator; |
| 1335 | for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
| 1336 | if (obj->IsGlobalObject()) count++; |
| 1337 | } |
| 1338 | return count; |
| 1339 | } |
| 1340 | |
| 1341 | |
| 1342 | // Test that we don't embed maps from foreign contexts into |
| 1343 | // optimized code. |
| 1344 | TEST(LeakGlobalContextViaMap) { |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1345 | i::FLAG_allow_natives_syntax = true; |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1346 | v8::HandleScope outer_scope; |
| 1347 | v8::Persistent<v8::Context> ctx1 = v8::Context::New(); |
| 1348 | v8::Persistent<v8::Context> ctx2 = v8::Context::New(); |
| 1349 | ctx1->Enter(); |
| 1350 | |
| 1351 | HEAP->CollectAllAvailableGarbage(); |
| 1352 | CHECK_EQ(4, NumberOfGlobalObjects()); |
| 1353 | |
| 1354 | { |
| 1355 | v8::HandleScope inner_scope; |
| 1356 | CompileRun("var v = {x: 42}"); |
| 1357 | v8::Local<v8::Value> v = ctx1->Global()->Get(v8_str("v")); |
| 1358 | ctx2->Enter(); |
| 1359 | ctx2->Global()->Set(v8_str("o"), v); |
| 1360 | v8::Local<v8::Value> res = CompileRun( |
| 1361 | "function f() { return o.x; }" |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1362 | "for (var i = 0; i < 10; ++i) f();" |
| 1363 | "%OptimizeFunctionOnNextCall(f);" |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1364 | "f();"); |
| 1365 | CHECK_EQ(42, res->Int32Value()); |
| 1366 | ctx2->Global()->Set(v8_str("o"), v8::Int32::New(0)); |
| 1367 | ctx2->Exit(); |
| 1368 | ctx1->Exit(); |
| 1369 | ctx1.Dispose(); |
| 1370 | } |
| 1371 | HEAP->CollectAllAvailableGarbage(); |
| 1372 | CHECK_EQ(2, NumberOfGlobalObjects()); |
| 1373 | ctx2.Dispose(); |
| 1374 | HEAP->CollectAllAvailableGarbage(); |
| 1375 | CHECK_EQ(0, NumberOfGlobalObjects()); |
| 1376 | } |
| 1377 | |
| 1378 | |
| 1379 | // Test that we don't embed functions from foreign contexts into |
| 1380 | // optimized code. |
| 1381 | TEST(LeakGlobalContextViaFunction) { |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1382 | i::FLAG_allow_natives_syntax = true; |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1383 | v8::HandleScope outer_scope; |
| 1384 | v8::Persistent<v8::Context> ctx1 = v8::Context::New(); |
| 1385 | v8::Persistent<v8::Context> ctx2 = v8::Context::New(); |
| 1386 | ctx1->Enter(); |
| 1387 | |
| 1388 | HEAP->CollectAllAvailableGarbage(); |
| 1389 | CHECK_EQ(4, NumberOfGlobalObjects()); |
| 1390 | |
| 1391 | { |
| 1392 | v8::HandleScope inner_scope; |
| 1393 | CompileRun("var v = function() { return 42; }"); |
| 1394 | v8::Local<v8::Value> v = ctx1->Global()->Get(v8_str("v")); |
| 1395 | ctx2->Enter(); |
| 1396 | ctx2->Global()->Set(v8_str("o"), v); |
| 1397 | v8::Local<v8::Value> res = CompileRun( |
| 1398 | "function f(x) { return x(); }" |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1399 | "for (var i = 0; i < 10; ++i) f(o);" |
| 1400 | "%OptimizeFunctionOnNextCall(f);" |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 1401 | "f(o);"); |
| 1402 | CHECK_EQ(42, res->Int32Value()); |
| 1403 | ctx2->Global()->Set(v8_str("o"), v8::Int32::New(0)); |
| 1404 | ctx2->Exit(); |
| 1405 | ctx1->Exit(); |
| 1406 | ctx1.Dispose(); |
| 1407 | } |
| 1408 | HEAP->CollectAllAvailableGarbage(); |
| 1409 | CHECK_EQ(2, NumberOfGlobalObjects()); |
| 1410 | ctx2.Dispose(); |
| 1411 | HEAP->CollectAllAvailableGarbage(); |
| 1412 | CHECK_EQ(0, NumberOfGlobalObjects()); |
| 1413 | } |
danno@chromium.org | e78f9fc | 2011-12-21 08:29:34 +0000 | [diff] [blame] | 1414 | |
| 1415 | |
| 1416 | TEST(LeakGlobalContextViaMapKeyed) { |
| 1417 | i::FLAG_allow_natives_syntax = true; |
| 1418 | v8::HandleScope outer_scope; |
| 1419 | v8::Persistent<v8::Context> ctx1 = v8::Context::New(); |
| 1420 | v8::Persistent<v8::Context> ctx2 = v8::Context::New(); |
| 1421 | ctx1->Enter(); |
| 1422 | |
| 1423 | HEAP->CollectAllAvailableGarbage(); |
| 1424 | CHECK_EQ(4, NumberOfGlobalObjects()); |
| 1425 | |
| 1426 | { |
| 1427 | v8::HandleScope inner_scope; |
| 1428 | CompileRun("var v = [42, 43]"); |
| 1429 | v8::Local<v8::Value> v = ctx1->Global()->Get(v8_str("v")); |
| 1430 | ctx2->Enter(); |
| 1431 | ctx2->Global()->Set(v8_str("o"), v); |
| 1432 | v8::Local<v8::Value> res = CompileRun( |
| 1433 | "function f() { return o[0]; }" |
| 1434 | "for (var i = 0; i < 10; ++i) f();" |
| 1435 | "%OptimizeFunctionOnNextCall(f);" |
| 1436 | "f();"); |
| 1437 | CHECK_EQ(42, res->Int32Value()); |
| 1438 | ctx2->Global()->Set(v8_str("o"), v8::Int32::New(0)); |
| 1439 | ctx2->Exit(); |
| 1440 | ctx1->Exit(); |
| 1441 | ctx1.Dispose(); |
| 1442 | } |
| 1443 | HEAP->CollectAllAvailableGarbage(); |
| 1444 | CHECK_EQ(2, NumberOfGlobalObjects()); |
| 1445 | ctx2.Dispose(); |
| 1446 | HEAP->CollectAllAvailableGarbage(); |
| 1447 | CHECK_EQ(0, NumberOfGlobalObjects()); |
| 1448 | } |
| 1449 | |
| 1450 | |
| 1451 | TEST(LeakGlobalContextViaMapProto) { |
| 1452 | i::FLAG_allow_natives_syntax = true; |
| 1453 | v8::HandleScope outer_scope; |
| 1454 | v8::Persistent<v8::Context> ctx1 = v8::Context::New(); |
| 1455 | v8::Persistent<v8::Context> ctx2 = v8::Context::New(); |
| 1456 | ctx1->Enter(); |
| 1457 | |
| 1458 | HEAP->CollectAllAvailableGarbage(); |
| 1459 | CHECK_EQ(4, NumberOfGlobalObjects()); |
| 1460 | |
| 1461 | { |
| 1462 | v8::HandleScope inner_scope; |
| 1463 | CompileRun("var v = { y: 42}"); |
| 1464 | v8::Local<v8::Value> v = ctx1->Global()->Get(v8_str("v")); |
| 1465 | ctx2->Enter(); |
| 1466 | ctx2->Global()->Set(v8_str("o"), v); |
| 1467 | v8::Local<v8::Value> res = CompileRun( |
| 1468 | "function f() {" |
| 1469 | " var p = {x: 42};" |
| 1470 | " p.__proto__ = o;" |
| 1471 | " return p.x;" |
| 1472 | "}" |
| 1473 | "for (var i = 0; i < 10; ++i) f();" |
| 1474 | "%OptimizeFunctionOnNextCall(f);" |
| 1475 | "f();"); |
| 1476 | CHECK_EQ(42, res->Int32Value()); |
| 1477 | ctx2->Global()->Set(v8_str("o"), v8::Int32::New(0)); |
| 1478 | ctx2->Exit(); |
| 1479 | ctx1->Exit(); |
| 1480 | ctx1.Dispose(); |
| 1481 | } |
| 1482 | HEAP->CollectAllAvailableGarbage(); |
| 1483 | CHECK_EQ(2, NumberOfGlobalObjects()); |
| 1484 | ctx2.Dispose(); |
| 1485 | HEAP->CollectAllAvailableGarbage(); |
| 1486 | CHECK_EQ(0, NumberOfGlobalObjects()); |
| 1487 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1488 | |
| 1489 | |
| 1490 | TEST(InstanceOfStubWriteBarrier) { |
| 1491 | i::FLAG_allow_natives_syntax = true; |
| 1492 | #ifdef DEBUG |
| 1493 | i::FLAG_verify_heap = true; |
| 1494 | #endif |
| 1495 | InitializeVM(); |
| 1496 | if (!i::V8::UseCrankshaft()) return; |
| 1497 | v8::HandleScope outer_scope; |
| 1498 | |
| 1499 | { |
| 1500 | v8::HandleScope scope; |
| 1501 | CompileRun( |
| 1502 | "function foo () { }" |
| 1503 | "function mkbar () { return new (new Function(\"\")) (); }" |
| 1504 | "function f (x) { return (x instanceof foo); }" |
| 1505 | "function g () { f(mkbar()); }" |
| 1506 | "f(new foo()); f(new foo());" |
| 1507 | "%OptimizeFunctionOnNextCall(f);" |
| 1508 | "f(new foo()); g();"); |
| 1509 | } |
| 1510 | |
| 1511 | IncrementalMarking* marking = HEAP->incremental_marking(); |
| 1512 | marking->Abort(); |
| 1513 | marking->Start(); |
| 1514 | |
| 1515 | Handle<JSFunction> f = |
| 1516 | v8::Utils::OpenHandle( |
| 1517 | *v8::Handle<v8::Function>::Cast( |
| 1518 | v8::Context::GetCurrent()->Global()->Get(v8_str("f")))); |
| 1519 | |
| 1520 | CHECK(f->IsOptimized()); |
| 1521 | |
| 1522 | while (!Marking::IsBlack(Marking::MarkBitFrom(f->code())) && |
| 1523 | !marking->IsStopped()) { |
| 1524 | marking->Step(MB); |
| 1525 | } |
| 1526 | |
| 1527 | CHECK(marking->IsMarking()); |
| 1528 | |
| 1529 | // Discard any pending GC requests otherwise we will get GC when we enter |
| 1530 | // code below. |
| 1531 | if (ISOLATE->stack_guard()->IsGCRequest()) { |
| 1532 | ISOLATE->stack_guard()->Continue(GC_REQUEST); |
| 1533 | } |
| 1534 | |
| 1535 | { |
| 1536 | v8::HandleScope scope; |
| 1537 | v8::Handle<v8::Object> global = v8::Context::GetCurrent()->Global(); |
| 1538 | v8::Handle<v8::Function> g = |
| 1539 | v8::Handle<v8::Function>::Cast(global->Get(v8_str("g"))); |
| 1540 | g->Call(global, 0, NULL); |
| 1541 | } |
| 1542 | |
| 1543 | HEAP->incremental_marking()->set_should_hurry(true); |
| 1544 | HEAP->CollectGarbage(OLD_POINTER_SPACE); |
| 1545 | } |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1546 | |
| 1547 | |
| 1548 | TEST(PrototypeTransitionClearing) { |
| 1549 | InitializeVM(); |
| 1550 | v8::HandleScope scope; |
| 1551 | |
| 1552 | CompileRun( |
| 1553 | "var base = {};" |
| 1554 | "var live = [];" |
| 1555 | "for (var i = 0; i < 10; i++) {" |
| 1556 | " var object = {};" |
| 1557 | " var prototype = {};" |
| 1558 | " object.__proto__ = prototype;" |
| 1559 | " if (i >= 3) live.push(object, prototype);" |
| 1560 | "}"); |
| 1561 | |
| 1562 | Handle<JSObject> baseObject = |
| 1563 | v8::Utils::OpenHandle( |
| 1564 | *v8::Handle<v8::Object>::Cast( |
| 1565 | v8::Context::GetCurrent()->Global()->Get(v8_str("base")))); |
| 1566 | |
| 1567 | // Verify that only dead prototype transitions are cleared. |
| 1568 | CHECK_EQ(10, baseObject->map()->NumberOfProtoTransitions()); |
| 1569 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1570 | CHECK_EQ(10 - 3, baseObject->map()->NumberOfProtoTransitions()); |
| 1571 | |
| 1572 | // Verify that prototype transitions array was compacted. |
| 1573 | FixedArray* trans = baseObject->map()->prototype_transitions(); |
| 1574 | for (int i = 0; i < 10 - 3; i++) { |
| 1575 | int j = Map::kProtoTransitionHeaderSize + |
| 1576 | i * Map::kProtoTransitionElementsPerEntry; |
| 1577 | CHECK(trans->get(j + Map::kProtoTransitionMapOffset)->IsMap()); |
| 1578 | CHECK(trans->get(j + Map::kProtoTransitionPrototypeOffset)->IsJSObject()); |
| 1579 | } |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1580 | |
| 1581 | // Make sure next prototype is placed on an old-space evacuation candidate. |
| 1582 | Handle<JSObject> prototype; |
| 1583 | PagedSpace* space = HEAP->old_pointer_space(); |
| 1584 | do { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1585 | prototype = FACTORY->NewJSArray(32 * KB, FAST_ELEMENTS, TENURED); |
jkummerow@chromium.org | 05ed9dd | 2012-01-23 14:42:48 +0000 | [diff] [blame] | 1586 | } while (space->FirstPage() == space->LastPage() || |
| 1587 | !space->LastPage()->Contains(prototype->address())); |
| 1588 | |
| 1589 | // Add a prototype on an evacuation candidate and verify that transition |
| 1590 | // clearing correctly records slots in prototype transition array. |
| 1591 | i::FLAG_always_compact = true; |
| 1592 | Handle<Map> map(baseObject->map()); |
| 1593 | CHECK(!space->LastPage()->Contains(map->prototype_transitions()->address())); |
| 1594 | CHECK(space->LastPage()->Contains(prototype->address())); |
| 1595 | baseObject->SetPrototype(*prototype, false)->ToObjectChecked(); |
| 1596 | CHECK(map->GetPrototypeTransition(*prototype)->IsMap()); |
| 1597 | HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 1598 | CHECK(map->GetPrototypeTransition(*prototype)->IsMap()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 1599 | } |