⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test-heap.cc.svn-base

📁 Google浏览器V8内核代码
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
  Object* i = Heap::AllocateStringFromAscii(CStrVector("fisk"));  Handle<Object> h = GlobalHandles::Create(i);  GlobalHandles::MakeWeak(h.location(),                          reinterpret_cast<void*>(1234),                          &TestDeleteWeakGlobalHandleCallback);  // Scanvenge does not recognize weak reference.  Heap::PerformScavenge();  CHECK(!WeakPointerCleared);  // Mark-compact treats weak reference properly.  CHECK(Heap::CollectGarbage(0, OLD_POINTER_SPACE));  CHECK(WeakPointerCleared);}static const char* not_so_random_string_table[] = {  "abstract",  "boolean",  "break",  "byte",  "case",  "catch",  "char",  "class",  "const",  "continue",  "debugger",  "default",  "delete",  "do",  "double",  "else",  "enum",  "export",  "extends",  "false",  "final",  "finally",  "float",  "for",  "function",  "goto",  "if",  "implements",  "import",  "in",  "instanceof",  "int",  "interface",  "long",  "native",  "new",  "null",  "package",  "private",  "protected",  "public",  "return",  "short",  "static",  "super",  "switch",  "synchronized",  "this",  "throw",  "throws",  "transient",  "true",  "try",  "typeof",  "var",  "void",  "volatile",  "while",  "with",  0};static void CheckSymbols(const char** strings) {  for (const char* string = *strings; *strings != 0; string = *strings++) {    Object* a = Heap::LookupAsciiSymbol(string);    CHECK(a->IsSymbol());    Object* b = Heap::LookupAsciiSymbol(string);    CHECK_EQ(b, a);    CHECK(String::cast(b)->IsEqualTo(CStrVector(string)));  }}TEST(SymbolTable) {  InitializeVM();  CheckSymbols(not_so_random_string_table);  CheckSymbols(not_so_random_string_table);}TEST(FunctionAllocation) {  InitializeVM();  v8::HandleScope sc;  String* name  = String::cast(Heap::LookupAsciiSymbol("theFunction"));  SharedFunctionInfo* function_share =    SharedFunctionInfo::cast(Heap::AllocateSharedFunctionInfo(name));  JSFunction* function =    JSFunction::cast(Heap::AllocateFunction(*Top::function_map(),                                            function_share,                                            Heap::undefined_value()));  Map* initial_map =      Map::cast(Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize));  function->set_initial_map(initial_map);  String* prop_name = String::cast(Heap::LookupAsciiSymbol("theSlot"));  JSObject* obj = JSObject::cast(Heap::AllocateJSObject(function));  obj->SetProperty(prop_name, Smi::FromInt(23), NONE);  CHECK_EQ(Smi::FromInt(23), obj->GetProperty(prop_name));  // Check that we can add properties to function objects.  function->SetProperty(prop_name, Smi::FromInt(24), NONE);  CHECK_EQ(Smi::FromInt(24), function->GetProperty(prop_name));}TEST(ObjectProperties) {  InitializeVM();  v8::HandleScope sc;  JSFunction* constructor =      JSFunction::cast(          Top::context()->global()->GetProperty(String::cast(                                                    Heap::Object_symbol())));  JSObject* obj = JSObject::cast(Heap::AllocateJSObject(constructor));  String* first = String::cast(Heap::LookupAsciiSymbol("first"));  String* second = String::cast(Heap::LookupAsciiSymbol("second"));  // check for empty  CHECK(!obj->HasLocalProperty(first));  // add first  obj->SetProperty(first, Smi::FromInt(1), NONE);  CHECK(obj->HasLocalProperty(first));  // delete first  CHECK(obj->DeleteProperty(first));  CHECK(!obj->HasLocalProperty(first));  // add first and then second  obj->SetProperty(first, Smi::FromInt(1), NONE);  obj->SetProperty(second, Smi::FromInt(2), NONE);  CHECK(obj->HasLocalProperty(first));  CHECK(obj->HasLocalProperty(second));  // delete first and then second  CHECK(obj->DeleteProperty(first));  CHECK(obj->HasLocalProperty(second));  CHECK(obj->DeleteProperty(second));  CHECK(!obj->HasLocalProperty(first));  CHECK(!obj->HasLocalProperty(second));  // add first and then second  obj->SetProperty(first, Smi::FromInt(1), NONE);  obj->SetProperty(second, Smi::FromInt(2), NONE);  CHECK(obj->HasLocalProperty(first));  CHECK(obj->HasLocalProperty(second));  // delete second and then first  CHECK(obj->DeleteProperty(second));  CHECK(obj->HasLocalProperty(first));  CHECK(obj->DeleteProperty(first));  CHECK(!obj->HasLocalProperty(first));  CHECK(!obj->HasLocalProperty(second));  // check string and symbol match  static const char* string1 = "fisk";  String* s1 =      String::cast(Heap::AllocateStringFromAscii(CStrVector(string1)));  obj->SetProperty(s1, Smi::FromInt(1), NONE);  CHECK(obj->HasLocalProperty(String::cast(Heap::LookupAsciiSymbol(string1))));  // check symbol and string match  static const char* string2 = "fugl";  String* s2 = String::cast(Heap::LookupAsciiSymbol(string2));  obj->SetProperty(s2, Smi::FromInt(1), NONE);  CHECK(obj->HasLocalProperty(            String::cast(Heap::AllocateStringFromAscii(CStrVector(string2)))));}TEST(JSObjectMaps) {  InitializeVM();  v8::HandleScope sc;  String* name  = String::cast(Heap::LookupAsciiSymbol("theFunction"));  SharedFunctionInfo* function_share =    SharedFunctionInfo::cast(Heap::AllocateSharedFunctionInfo(name));  JSFunction* function =    JSFunction::cast(Heap::AllocateFunction(*Top::function_map(),                                            function_share,                                            Heap::undefined_value()));  Map* initial_map =      Map::cast(Heap::AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize));  function->set_initial_map(initial_map);  String* prop_name = String::cast(Heap::LookupAsciiSymbol("theSlot"));  JSObject* obj = JSObject::cast(Heap::AllocateJSObject(function));  // Set a propery  obj->SetProperty(prop_name, Smi::FromInt(23), NONE);  CHECK_EQ(Smi::FromInt(23), obj->GetProperty(prop_name));  // Check the map has changed  CHECK(initial_map != obj->map());}TEST(JSArray) {  InitializeVM();  v8::HandleScope sc;  String* name = String::cast(Heap::LookupAsciiSymbol("Array"));  JSFunction* function =      JSFunction::cast(Top::context()->global()->GetProperty(name));  // Allocate the object.  JSArray* array = JSArray::cast(Heap::AllocateJSObject(function));  array->Initialize(0);  // Set array length to 0.  array->SetElementsLength(Smi::FromInt(0));  CHECK_EQ(Smi::FromInt(0), array->length());  CHECK(array->HasFastElements());  // Must be in fast mode.  // array[length] = name.  array->SetElement(0, name);  CHECK_EQ(Smi::FromInt(1), array->length());  CHECK_EQ(array->GetElement(0), name);  // Set array length with larger than smi value.  Object* length = Heap::NumberFromInt32(Smi::kMaxValue + 1);  array->SetElementsLength(length);  uint32_t int_length = 0;  CHECK(Array::IndexFromObject(length, &int_length));  CHECK_EQ(length, array->length());  CHECK(!array->HasFastElements());  // Must be in slow mode.  // array[length] = name.  array->SetElement(int_length, name);  uint32_t new_int_length = 0;  CHECK(Array::IndexFromObject(array->length(), &new_int_length));  CHECK_EQ(static_cast<double>(int_length), new_int_length - 1);  CHECK_EQ(array->GetElement(int_length), name);  CHECK_EQ(array->GetElement(0), name);}TEST(JSObjectCopy) {  InitializeVM();  v8::HandleScope sc;  String* name = String::cast(Heap::Object_symbol());  JSFunction* constructor =      JSFunction::cast(Top::context()->global()->GetProperty(name));  JSObject* obj = JSObject::cast(Heap::AllocateJSObject(constructor));  String* first = String::cast(Heap::LookupAsciiSymbol("first"));  String* second = String::cast(Heap::LookupAsciiSymbol("second"));  obj->SetProperty(first, Smi::FromInt(1), NONE);  obj->SetProperty(second, Smi::FromInt(2), NONE);  obj->SetElement(0, first);  obj->SetElement(1, second);  // Make the clone.  JSObject* clone = JSObject::cast(obj->Copy());  CHECK(clone != obj);  CHECK_EQ(obj->GetElement(0), clone->GetElement(0));  CHECK_EQ(obj->GetElement(1), clone->GetElement(1));  CHECK_EQ(obj->GetProperty(first), clone->GetProperty(first));  CHECK_EQ(obj->GetProperty(second), clone->GetProperty(second));  // Flip the values.  clone->SetProperty(first, Smi::FromInt(2), NONE);  clone->SetProperty(second, Smi::FromInt(1), NONE);  clone->SetElement(0, second);  clone->SetElement(1, first);  CHECK_EQ(obj->GetElement(1), clone->GetElement(0));  CHECK_EQ(obj->GetElement(0), clone->GetElement(1));  CHECK_EQ(obj->GetProperty(second), clone->GetProperty(first));  CHECK_EQ(obj->GetProperty(first), clone->GetProperty(second));}TEST(StringAllocation) {  InitializeVM();  const unsigned char chars[] = { 0xe5, 0xa4, 0xa7 };  for (int length = 0; length < 100; length++) {    v8::HandleScope scope;    char* non_ascii = NewArray<char>(3 * length + 1);    char* ascii = NewArray<char>(length + 1);    non_ascii[3 * length] = 0;    ascii[length] = 0;    for (int i = 0; i < length; i++) {      ascii[i] = 'a';      non_ascii[3 * i] = chars[0];      non_ascii[3 * i + 1] = chars[1];      non_ascii[3 * i + 2] = chars[2];    }    Handle<String> non_ascii_sym =        Factory::LookupSymbol(Vector<const char>(non_ascii, 3 * length));    CHECK_EQ(length, non_ascii_sym->length());    Handle<String> ascii_sym =        Factory::LookupSymbol(Vector<const char>(ascii, length));    CHECK_EQ(length, ascii_sym->length());    Handle<String> non_ascii_str =        Factory::NewStringFromUtf8(Vector<const char>(non_ascii, 3 * length));    non_ascii_str->Hash();    CHECK_EQ(length, non_ascii_str->length());    Handle<String> ascii_str =        Factory::NewStringFromUtf8(Vector<const char>(ascii, length));    ascii_str->Hash();    CHECK_EQ(length, ascii_str->length());    DeleteArray(non_ascii);    DeleteArray(ascii);  }}static int ObjectsFoundInHeap(Handle<Object> objs[], int size) {  // Count the number of objects found in the heap.  int found_count = 0;  HeapIterator iterator;  while (iterator.has_next()) {    HeapObject* obj = iterator.next();    CHECK(obj != NULL);    for (int i = 0; i < size; i++) {      if (*objs[i] == obj) {        found_count++;      }    }  }  CHECK(!iterator.has_next());  return found_count;}TEST(Iteration) {  InitializeVM();  v8::HandleScope scope;  // Array of objects to scan haep for.  const int objs_count = 6;  Handle<Object> objs[objs_count];  int next_objs_index = 0;  // Allocate a JS array to OLD_POINTER_SPACE and NEW_SPACE  objs[next_objs_index++] = Factory::NewJSArray(10);  objs[next_objs_index++] = Factory::NewJSArray(10, TENURED);  // Allocate a small string to OLD_DATA_SPACE and NEW_SPACE  objs[next_objs_index++] =      Factory::NewStringFromAscii(CStrVector("abcdefghij"));  objs[next_objs_index++] =      Factory::NewStringFromAscii(CStrVector("abcdefghij"), TENURED);  // Allocate a large string (for large object space).  int large_size = Heap::MaxHeapObjectSize() + 1;  char* str = new char[large_size];  for (int i = 0; i < large_size - 1; ++i) str[i] = 'a';  str[large_size - 1] = '\0';  objs[next_objs_index++] =      Factory::NewStringFromAscii(CStrVector(str), TENURED);  delete[] str;  // Add a Map object to look for.  objs[next_objs_index++] = Handle<Map>(HeapObject::cast(*objs[0])->map());  CHECK_EQ(objs_count, next_objs_index);  CHECK_EQ(objs_count, ObjectsFoundInHeap(objs, objs_count));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -