ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 294 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 295 | InitializeVM(); |
| 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) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 342 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 343 | InitializeVM(); |
| 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) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 380 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 381 | InitializeVM(); |
| 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) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 419 | GlobalHandles* global_handles = Isolate::Current()->global_handles(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 420 | InitializeVM(); |
| 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 |
| 615 | static 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 |
| 623 | static 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. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 670 | Object* ok = 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. |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 673 | ok = 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()); |
| 675 | CHECK(array->HasFastElements()); // Must be in fast mode. |
| 676 | |
| 677 | // array[length] = name. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 678 | ok = array->SetElement(0, *name, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 679 | CHECK_EQ(Smi::FromInt(1), array->length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 680 | CHECK_EQ(array->GetElement(0), *name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 681 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 682 | // Set array length with larger than smi value. |
| 683 | Handle<Object> length = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 684 | FACTORY->NewNumberFromUint(static_cast<uint32_t>(Smi::kMaxValue) + 1); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 685 | ok = array->SetElementsLength(*length)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 686 | |
| 687 | uint32_t int_length = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 688 | CHECK(length->ToArrayIndex(&int_length)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 689 | CHECK_EQ(*length, array->length()); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 690 | CHECK(array->HasDictionaryElements()); // Must be in slow mode. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 691 | |
| 692 | // array[length] = name. |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 693 | ok = array->SetElement(int_length, *name, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 694 | uint32_t new_int_length = 0; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 695 | CHECK(array->length()->ToArrayIndex(&new_int_length)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 696 | CHECK_EQ(static_cast<double>(int_length), new_int_length - 1); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 697 | CHECK_EQ(array->GetElement(int_length), *name); |
| 698 | CHECK_EQ(array->GetElement(0), *name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | |
| 702 | TEST(JSObjectCopy) { |
| 703 | InitializeVM(); |
| 704 | |
| 705 | v8::HandleScope sc; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 706 | String* object_symbol = String::cast(HEAP->Object_symbol()); |
| 707 | Object* raw_object = Isolate::Current()->context()->global()-> |
| 708 | GetProperty(object_symbol)->ToObjectChecked(); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 709 | JSFunction* object_function = JSFunction::cast(raw_object); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 710 | Handle<JSFunction> constructor(object_function); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 711 | Handle<JSObject> obj = FACTORY->NewJSObject(constructor); |
| 712 | Handle<String> first = FACTORY->LookupAsciiSymbol("first"); |
| 713 | Handle<String> second = FACTORY->LookupAsciiSymbol("second"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 714 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 715 | obj->SetProperty( |
| 716 | *first, Smi::FromInt(1), NONE, kNonStrictMode)->ToObjectChecked(); |
| 717 | obj->SetProperty( |
| 718 | *second, Smi::FromInt(2), NONE, kNonStrictMode)->ToObjectChecked(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 719 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 720 | Object* ok = obj->SetElement(0, *first, kNonStrictMode)->ToObjectChecked(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 721 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 722 | ok = obj->SetElement(1, *second, 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 | |
karlklose@chromium.org | 8f806e8 | 2011-03-07 14:06:08 +0000 | [diff] [blame] | 740 | ok = clone->SetElement(0, *second, kNonStrictMode)->ToObjectChecked(); |
| 741 | ok = clone->SetElement(1, *first, 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); |
| 814 | objs[next_objs_index++] = FACTORY->NewJSArray(10, 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). |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 823 | int large_size = HEAP->MaxObjectSizeInPagedSpace() + 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 | |
| 839 | TEST(LargeObjectSpaceContains) { |
| 840 | InitializeVM(); |
| 841 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 842 | HEAP->CollectGarbage(NEW_SPACE); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 843 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 844 | Address current_top = HEAP->new_space()->top(); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 845 | Page* page = Page::FromAddress(current_top); |
| 846 | Address current_page = page->address(); |
| 847 | Address next_page = current_page + Page::kPageSize; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 848 | int bytes_to_page = static_cast<int>(next_page - current_top); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 849 | if (bytes_to_page <= FixedArray::kHeaderSize) { |
| 850 | // Alas, need to cross another page to be able to |
| 851 | // put desired value. |
| 852 | next_page += Page::kPageSize; |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 853 | bytes_to_page = static_cast<int>(next_page - current_top); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 854 | } |
| 855 | CHECK(bytes_to_page > FixedArray::kHeaderSize); |
| 856 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 857 | intptr_t* flags_ptr = &Page::FromAddress(next_page)->flags_; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 858 | Address flags_addr = reinterpret_cast<Address>(flags_ptr); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 859 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 860 | int bytes_to_allocate = |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 861 | static_cast<int>(flags_addr - current_top) + kPointerSize; |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 862 | |
| 863 | int n_elements = (bytes_to_allocate - FixedArray::kHeaderSize) / |
| 864 | kPointerSize; |
| 865 | CHECK_EQ(bytes_to_allocate, FixedArray::SizeFor(n_elements)); |
| 866 | FixedArray* array = FixedArray::cast( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 867 | HEAP->AllocateFixedArray(n_elements)->ToObjectChecked()); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 868 | |
| 869 | int index = n_elements - 1; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 870 | CHECK_EQ(flags_ptr, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 871 | HeapObject::RawField(array, FixedArray::OffsetOfElementAt(index))); |
| 872 | array->set(index, Smi::FromInt(0)); |
| 873 | // This chould have turned next page into LargeObjectPage: |
| 874 | // CHECK(Page::FromAddress(next_page)->IsLargeObjectPage()); |
| 875 | |
| 876 | HeapObject* addr = HeapObject::FromAddress(next_page + 2 * kPointerSize); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 877 | CHECK(HEAP->new_space()->Contains(addr)); |
| 878 | CHECK(!HEAP->lo_space()->Contains(addr)); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 879 | } |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 880 | |
| 881 | |
| 882 | TEST(EmptyHandleEscapeFrom) { |
| 883 | InitializeVM(); |
| 884 | |
| 885 | v8::HandleScope scope; |
| 886 | Handle<JSObject> runaway; |
| 887 | |
| 888 | { |
| 889 | v8::HandleScope nested; |
| 890 | Handle<JSObject> empty; |
| 891 | runaway = empty.EscapeFrom(&nested); |
| 892 | } |
| 893 | |
| 894 | CHECK(runaway.is_null()); |
| 895 | } |
| 896 | |
| 897 | |
| 898 | static int LenFromSize(int size) { |
| 899 | return (size - FixedArray::kHeaderSize) / kPointerSize; |
| 900 | } |
| 901 | |
| 902 | |
| 903 | TEST(Regression39128) { |
| 904 | // Test case for crbug.com/39128. |
| 905 | InitializeVM(); |
| 906 | |
| 907 | // Increase the chance of 'bump-the-pointer' allocation in old space. |
| 908 | bool force_compaction = true; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 909 | HEAP->CollectAllGarbage(force_compaction); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 910 | |
| 911 | v8::HandleScope scope; |
| 912 | |
| 913 | // The plan: create JSObject which references objects in new space. |
| 914 | // 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] | 915 | // that region dirty marks are updated correctly. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 916 | |
| 917 | // 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] | 918 | Handle<JSFunction> object_ctor( |
| 919 | Isolate::Current()->global_context()->object_function()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 920 | CHECK(object_ctor->has_initial_map()); |
| 921 | Handle<Map> object_map(object_ctor->initial_map()); |
| 922 | // Create a map with single inobject property. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 923 | Handle<Map> my_map = FACTORY->CopyMap(object_map, 1); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 924 | int n_properties = my_map->inobject_properties(); |
| 925 | CHECK_GT(n_properties, 0); |
| 926 | |
| 927 | int object_size = my_map->instance_size(); |
| 928 | |
| 929 | // Step 2: allocate a lot of objects so to almost fill new space: we need |
| 930 | // just enough room to allocate JSObject and thus fill the newspace. |
| 931 | |
| 932 | int allocation_amount = Min(FixedArray::kMaxSize, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 933 | HEAP->MaxObjectSizeInNewSpace()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 934 | int allocation_len = LenFromSize(allocation_amount); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 935 | NewSpace* new_space = HEAP->new_space(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 936 | Address* top_addr = new_space->allocation_top_address(); |
| 937 | Address* limit_addr = new_space->allocation_limit_address(); |
| 938 | while ((*limit_addr - *top_addr) > allocation_amount) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 939 | CHECK(!HEAP->always_allocate()); |
| 940 | Object* array = HEAP->AllocateFixedArray(allocation_len)->ToObjectChecked(); |
| 941 | CHECK(!array->IsFailure()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 942 | CHECK(new_space->Contains(array)); |
| 943 | } |
| 944 | |
| 945 | // 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] | 946 | 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] | 947 | int fixed_array_len = LenFromSize(to_fill); |
| 948 | CHECK(fixed_array_len < FixedArray::kMaxLength); |
| 949 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 950 | CHECK(!HEAP->always_allocate()); |
| 951 | Object* array = HEAP->AllocateFixedArray(fixed_array_len)->ToObjectChecked(); |
| 952 | CHECK(!array->IsFailure()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 953 | CHECK(new_space->Contains(array)); |
| 954 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 955 | Object* object = HEAP->AllocateJSObjectFromMap(*my_map)->ToObjectChecked(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 956 | CHECK(new_space->Contains(object)); |
| 957 | JSObject* jsobject = JSObject::cast(object); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 958 | CHECK_EQ(0, FixedArray::cast(jsobject->elements())->length()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 959 | CHECK_EQ(0, jsobject->properties()->length()); |
| 960 | // Create a reference to object in new space in jsobject. |
| 961 | jsobject->FastPropertyAtPut(-1, array); |
| 962 | |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 963 | CHECK_EQ(0, static_cast<int>(*limit_addr - *top_addr)); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 964 | |
| 965 | // Step 4: clone jsobject, but force always allocate first to create a clone |
| 966 | // in old pointer space. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 967 | Address old_pointer_space_top = HEAP->old_pointer_space()->top(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 968 | AlwaysAllocateScope aa_scope; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 969 | Object* clone_obj = HEAP->CopyJSObject(jsobject)->ToObjectChecked(); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 970 | JSObject* clone = JSObject::cast(clone_obj); |
| 971 | if (clone->address() != old_pointer_space_top) { |
| 972 | // Alas, got allocated from free list, we cannot do checks. |
| 973 | return; |
| 974 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 975 | CHECK(HEAP->old_pointer_space()->Contains(clone->address())); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 976 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 977 | // Step 5: verify validity of region dirty marks. |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 978 | Address clone_addr = clone->address(); |
| 979 | Page* page = Page::FromAddress(clone_addr); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 980 | // Check that region covering inobject property 1 is marked dirty. |
| 981 | CHECK(page->IsRegionDirty(clone_addr + (object_size - kPointerSize))); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 982 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 983 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 984 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 985 | TEST(TestCodeFlushing) { |
| 986 | i::FLAG_allow_natives_syntax = true; |
ricow@chromium.org | fd0930e | 2010-06-11 10:37:34 +0000 | [diff] [blame] | 987 | // If we do not flush code this test is invalid. |
| 988 | if (!FLAG_flush_code) return; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 989 | InitializeVM(); |
| 990 | v8::HandleScope scope; |
| 991 | const char* source = "function foo() {" |
| 992 | " var x = 42;" |
| 993 | " var y = 42;" |
| 994 | " var z = x + y;" |
| 995 | "};" |
| 996 | "foo()"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 997 | Handle<String> foo_name = FACTORY->LookupAsciiSymbol("foo"); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 998 | |
| 999 | // This compile will add the code to the compilation cache. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1000 | { v8::HandleScope scope; |
| 1001 | CompileRun(source); |
| 1002 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1003 | |
| 1004 | // Check function is compiled. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1005 | Object* func_value = Isolate::Current()->context()->global()-> |
| 1006 | GetProperty(*foo_name)->ToObjectChecked(); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1007 | CHECK(func_value->IsJSFunction()); |
| 1008 | Handle<JSFunction> function(JSFunction::cast(func_value)); |
| 1009 | CHECK(function->shared()->is_compiled()); |
| 1010 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1011 | HEAP->CollectAllGarbage(true); |
| 1012 | HEAP->CollectAllGarbage(true); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1013 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1014 | CHECK(function->shared()->is_compiled()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1015 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1016 | HEAP->CollectAllGarbage(true); |
| 1017 | HEAP->CollectAllGarbage(true); |
| 1018 | HEAP->CollectAllGarbage(true); |
| 1019 | HEAP->CollectAllGarbage(true); |
| 1020 | HEAP->CollectAllGarbage(true); |
| 1021 | HEAP->CollectAllGarbage(true); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1022 | |
| 1023 | // foo should no longer be in the compilation cache |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1024 | CHECK(!function->shared()->is_compiled() || function->IsOptimized()); |
| 1025 | CHECK(!function->is_compiled() || function->IsOptimized()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1026 | // Call foo to get it recompiled. |
| 1027 | CompileRun("foo()"); |
| 1028 | CHECK(function->shared()->is_compiled()); |
ricow@chromium.org | 0b9f850 | 2010-08-18 07:45:01 +0000 | [diff] [blame] | 1029 | CHECK(function->is_compiled()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1030 | } |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1031 | |
| 1032 | |
| 1033 | // Count the number of global contexts in the weak list of global contexts. |
| 1034 | static int CountGlobalContexts() { |
| 1035 | int count = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1036 | Object* object = HEAP->global_contexts_list(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1037 | while (!object->IsUndefined()) { |
| 1038 | count++; |
| 1039 | object = Context::cast(object)->get(Context::NEXT_CONTEXT_LINK); |
| 1040 | } |
| 1041 | return count; |
| 1042 | } |
| 1043 | |
| 1044 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1045 | // Count the number of user functions in the weak list of optimized |
| 1046 | // functions attached to a global context. |
| 1047 | static int CountOptimizedUserFunctions(v8::Handle<v8::Context> context) { |
| 1048 | int count = 0; |
| 1049 | Handle<Context> icontext = v8::Utils::OpenHandle(*context); |
| 1050 | Object* object = icontext->get(Context::OPTIMIZED_FUNCTIONS_LIST); |
| 1051 | while (object->IsJSFunction() && !JSFunction::cast(object)->IsBuiltin()) { |
| 1052 | count++; |
| 1053 | object = JSFunction::cast(object)->next_function_link(); |
| 1054 | } |
| 1055 | return count; |
| 1056 | } |
| 1057 | |
| 1058 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1059 | TEST(TestInternalWeakLists) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1060 | v8::V8::Initialize(); |
| 1061 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1062 | static const int kNumTestContexts = 10; |
| 1063 | |
| 1064 | v8::HandleScope scope; |
| 1065 | v8::Persistent<v8::Context> ctx[kNumTestContexts]; |
| 1066 | |
| 1067 | CHECK_EQ(0, CountGlobalContexts()); |
| 1068 | |
| 1069 | // Create a number of global contests which gets linked together. |
| 1070 | for (int i = 0; i < kNumTestContexts; i++) { |
| 1071 | ctx[i] = v8::Context::New(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1072 | |
| 1073 | bool opt = (FLAG_always_opt && i::V8::UseCrankshaft()); |
| 1074 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1075 | CHECK_EQ(i + 1, CountGlobalContexts()); |
| 1076 | |
| 1077 | ctx[i]->Enter(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1078 | |
| 1079 | // Create a handle scope so no function objects get stuch in the outer |
| 1080 | // handle scope |
| 1081 | v8::HandleScope scope; |
| 1082 | const char* source = "function f1() { };" |
| 1083 | "function f2() { };" |
| 1084 | "function f3() { };" |
| 1085 | "function f4() { };" |
| 1086 | "function f5() { };"; |
| 1087 | CompileRun(source); |
| 1088 | CHECK_EQ(0, CountOptimizedUserFunctions(ctx[i])); |
| 1089 | CompileRun("f1()"); |
| 1090 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1091 | CompileRun("f2()"); |
| 1092 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1093 | CompileRun("f3()"); |
| 1094 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1095 | CompileRun("f4()"); |
| 1096 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1097 | CompileRun("f5()"); |
| 1098 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1099 | |
| 1100 | // Remove function f1, and |
| 1101 | CompileRun("f1=null"); |
| 1102 | |
| 1103 | // Scavenge treats these references as strong. |
| 1104 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1105 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1106 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1107 | } |
| 1108 | |
| 1109 | // Mark compact handles the weak references. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1110 | HEAP->CollectAllGarbage(true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1111 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1112 | |
| 1113 | // Get rid of f3 and f5 in the same way. |
| 1114 | CompileRun("f3=null"); |
| 1115 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1116 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1117 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1118 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1119 | HEAP->CollectAllGarbage(true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1120 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1121 | CompileRun("f5=null"); |
| 1122 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1123 | HEAP->PerformScavenge(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1124 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1125 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1126 | HEAP->CollectAllGarbage(true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1127 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[i])); |
| 1128 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1129 | ctx[i]->Exit(); |
| 1130 | } |
| 1131 | |
| 1132 | // Force compilation cache cleanup. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1133 | HEAP->CollectAllGarbage(true); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1134 | |
| 1135 | // Dispose the global contexts one by one. |
| 1136 | for (int i = 0; i < kNumTestContexts; i++) { |
| 1137 | ctx[i].Dispose(); |
| 1138 | ctx[i].Clear(); |
| 1139 | |
| 1140 | // Scavenge treats these references as strong. |
| 1141 | for (int j = 0; j < 10; j++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1142 | HEAP->PerformScavenge(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1143 | CHECK_EQ(kNumTestContexts - i, CountGlobalContexts()); |
| 1144 | } |
| 1145 | |
| 1146 | // Mark compact handles the weak references. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1147 | HEAP->CollectAllGarbage(true); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1148 | CHECK_EQ(kNumTestContexts - i - 1, CountGlobalContexts()); |
| 1149 | } |
| 1150 | |
| 1151 | CHECK_EQ(0, CountGlobalContexts()); |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | // Count the number of global contexts in the weak list of global contexts |
| 1156 | // causing a GC after the specified number of elements. |
| 1157 | static int CountGlobalContextsWithGC(int n) { |
| 1158 | int count = 0; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1159 | Handle<Object> object(HEAP->global_contexts_list()); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1160 | while (!object->IsUndefined()) { |
| 1161 | count++; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1162 | if (count == n) HEAP->CollectAllGarbage(true); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1163 | object = |
| 1164 | Handle<Object>(Context::cast(*object)->get(Context::NEXT_CONTEXT_LINK)); |
| 1165 | } |
| 1166 | return count; |
| 1167 | } |
| 1168 | |
| 1169 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1170 | // Count the number of user functions in the weak list of optimized |
| 1171 | // functions attached to a global context causing a GC after the |
| 1172 | // specified number of elements. |
| 1173 | static int CountOptimizedUserFunctionsWithGC(v8::Handle<v8::Context> context, |
| 1174 | int n) { |
| 1175 | int count = 0; |
| 1176 | Handle<Context> icontext = v8::Utils::OpenHandle(*context); |
| 1177 | Handle<Object> object(icontext->get(Context::OPTIMIZED_FUNCTIONS_LIST)); |
| 1178 | while (object->IsJSFunction() && |
| 1179 | !Handle<JSFunction>::cast(object)->IsBuiltin()) { |
| 1180 | count++; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1181 | if (count == n) HEAP->CollectAllGarbage(true); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1182 | object = Handle<Object>( |
| 1183 | Object::cast(JSFunction::cast(*object)->next_function_link())); |
| 1184 | } |
| 1185 | return count; |
| 1186 | } |
| 1187 | |
| 1188 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1189 | TEST(TestInternalWeakListsTraverseWithGC) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1190 | v8::V8::Initialize(); |
| 1191 | |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1192 | static const int kNumTestContexts = 10; |
| 1193 | |
| 1194 | v8::HandleScope scope; |
| 1195 | v8::Persistent<v8::Context> ctx[kNumTestContexts]; |
| 1196 | |
| 1197 | CHECK_EQ(0, CountGlobalContexts()); |
| 1198 | |
| 1199 | // Create an number of contexts and check the length of the weak list both |
| 1200 | // with and without GCs while iterating the list. |
| 1201 | for (int i = 0; i < kNumTestContexts; i++) { |
| 1202 | ctx[i] = v8::Context::New(); |
| 1203 | CHECK_EQ(i + 1, CountGlobalContexts()); |
| 1204 | CHECK_EQ(i + 1, CountGlobalContextsWithGC(i / 2 + 1)); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1205 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1206 | |
| 1207 | bool opt = (FLAG_always_opt && i::V8::UseCrankshaft()); |
| 1208 | |
| 1209 | // Compile a number of functions the length of the weak list of optimized |
| 1210 | // functions both with and without GCs while iterating the list. |
| 1211 | ctx[0]->Enter(); |
| 1212 | const char* source = "function f1() { };" |
| 1213 | "function f2() { };" |
| 1214 | "function f3() { };" |
| 1215 | "function f4() { };" |
| 1216 | "function f5() { };"; |
| 1217 | CompileRun(source); |
| 1218 | CHECK_EQ(0, CountOptimizedUserFunctions(ctx[0])); |
| 1219 | CompileRun("f1()"); |
| 1220 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1221 | CHECK_EQ(opt ? 1 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1222 | CompileRun("f2()"); |
| 1223 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1224 | CHECK_EQ(opt ? 2 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1225 | CompileRun("f3()"); |
| 1226 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1227 | CHECK_EQ(opt ? 3 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 1)); |
| 1228 | CompileRun("f4()"); |
| 1229 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1230 | CHECK_EQ(opt ? 4 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 2)); |
| 1231 | CompileRun("f5()"); |
| 1232 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctions(ctx[0])); |
| 1233 | CHECK_EQ(opt ? 5 : 0, CountOptimizedUserFunctionsWithGC(ctx[0], 4)); |
| 1234 | |
| 1235 | ctx[0]->Exit(); |
whesse@chromium.org | 4a5224e | 2010-10-20 12:37:07 +0000 | [diff] [blame] | 1236 | } |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1237 | |
| 1238 | |
| 1239 | TEST(TestSizeOfObjectsVsHeapIteratorPrecision) { |
| 1240 | InitializeVM(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1241 | intptr_t size_of_objects_1 = HEAP->SizeOfObjects(); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1242 | HeapIterator iterator(HeapIterator::kFilterFreeListNodes); |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 1243 | intptr_t size_of_objects_2 = 0; |
| 1244 | for (HeapObject* obj = iterator.next(); |
| 1245 | obj != NULL; |
| 1246 | obj = iterator.next()) { |
| 1247 | size_of_objects_2 += obj->Size(); |
| 1248 | } |
| 1249 | // Delta must be within 1% of the larger result. |
| 1250 | if (size_of_objects_1 > size_of_objects_2) { |
| 1251 | intptr_t delta = size_of_objects_1 - size_of_objects_2; |
| 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); |
| 1256 | CHECK_GT(size_of_objects_1 / 100, delta); |
| 1257 | } else { |
| 1258 | intptr_t delta = size_of_objects_2 - size_of_objects_1; |
| 1259 | PrintF("Heap::SizeOfObjects: %" V8_PTR_PREFIX "d, " |
| 1260 | "Iterator: %" V8_PTR_PREFIX "d, " |
| 1261 | "delta: %" V8_PTR_PREFIX "d\n", |
| 1262 | size_of_objects_1, size_of_objects_2, delta); |
| 1263 | CHECK_GT(size_of_objects_2 / 100, delta); |
| 1264 | } |
| 1265 | } |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1266 | |
| 1267 | |
| 1268 | class HeapIteratorTestHelper { |
| 1269 | public: |
| 1270 | HeapIteratorTestHelper(Object* a, Object* b) |
| 1271 | : a_(a), b_(b), a_found_(false), b_found_(false) {} |
| 1272 | bool a_found() { return a_found_; } |
| 1273 | bool b_found() { return b_found_; } |
| 1274 | void IterateHeap(HeapIterator::HeapObjectsFiltering mode) { |
| 1275 | HeapIterator iterator(mode); |
| 1276 | for (HeapObject* obj = iterator.next(); |
| 1277 | obj != NULL; |
| 1278 | obj = iterator.next()) { |
| 1279 | if (obj == a_) |
| 1280 | a_found_ = true; |
| 1281 | else if (obj == b_) |
| 1282 | b_found_ = true; |
| 1283 | } |
| 1284 | } |
| 1285 | private: |
| 1286 | Object* a_; |
| 1287 | Object* b_; |
| 1288 | bool a_found_; |
| 1289 | bool b_found_; |
| 1290 | }; |
| 1291 | |
| 1292 | TEST(HeapIteratorFilterUnreachable) { |
| 1293 | InitializeVM(); |
| 1294 | v8::HandleScope scope; |
| 1295 | CompileRun("a = {}; b = {};"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1296 | v8::Handle<Object> a(ISOLATE->context()->global()->GetProperty( |
| 1297 | *FACTORY->LookupAsciiSymbol("a"))->ToObjectChecked()); |
| 1298 | v8::Handle<Object> b(ISOLATE->context()->global()->GetProperty( |
| 1299 | *FACTORY->LookupAsciiSymbol("b"))->ToObjectChecked()); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1300 | CHECK_NE(*a, *b); |
| 1301 | { |
| 1302 | HeapIteratorTestHelper helper(*a, *b); |
| 1303 | helper.IterateHeap(HeapIterator::kFilterUnreachable); |
| 1304 | CHECK(helper.a_found()); |
| 1305 | CHECK(helper.b_found()); |
| 1306 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1307 | CHECK(ISOLATE->context()->global()->DeleteProperty( |
| 1308 | *FACTORY->LookupAsciiSymbol("a"), JSObject::FORCE_DELETION)); |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 1309 | // We ensure that GC will not happen, so our raw pointer stays valid. |
| 1310 | AssertNoAllocation no_alloc; |
| 1311 | Object* a_saved = *a; |
| 1312 | a.Clear(); |
| 1313 | // Verify that "a" object still resides in the heap... |
| 1314 | { |
| 1315 | HeapIteratorTestHelper helper(a_saved, *b); |
| 1316 | helper.IterateHeap(HeapIterator::kNoFiltering); |
| 1317 | CHECK(helper.a_found()); |
| 1318 | CHECK(helper.b_found()); |
| 1319 | } |
| 1320 | // ...but is now unreachable. |
| 1321 | { |
| 1322 | HeapIteratorTestHelper helper(a_saved, *b); |
| 1323 | helper.IterateHeap(HeapIterator::kFilterUnreachable); |
| 1324 | CHECK(!helper.a_found()); |
| 1325 | CHECK(helper.b_found()); |
| 1326 | } |
| 1327 | } |