2012-05-23 14:24:29 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "factory.h"
|
|
|
|
#include "string-stream.h"
|
|
|
|
|
2011-07-12 08:03:19 +00:00
|
|
|
#include "allocation-inl.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
static const int kMentionedObjectCacheMaxSize = 256;
|
|
|
|
|
|
|
|
char* HeapStringAllocator::allocate(unsigned bytes) {
|
|
|
|
space_ = NewArray<char>(bytes);
|
|
|
|
return space_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-28 12:50:18 +00:00
|
|
|
NoAllocationStringAllocator::NoAllocationStringAllocator(char* memory,
|
|
|
|
unsigned size) {
|
|
|
|
size_ = size;
|
|
|
|
space_ = memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
bool StringStream::Put(char c) {
|
2009-04-16 21:01:05 +00:00
|
|
|
if (full()) return false;
|
|
|
|
ASSERT(length_ < capacity_);
|
|
|
|
// Since the trailing '\0' is not accounted for in length_ fullness is
|
|
|
|
// indicated by a difference of 1 between length_ and capacity_. Thus when
|
|
|
|
// reaching a difference of 2 we need to grow the buffer.
|
|
|
|
if (length_ == capacity_ - 2) {
|
2008-07-03 15:10:15 +00:00
|
|
|
unsigned new_capacity = capacity_;
|
|
|
|
char* new_buffer = allocator_->grow(&new_capacity);
|
|
|
|
if (new_capacity > capacity_) {
|
|
|
|
capacity_ = new_capacity;
|
|
|
|
buffer_ = new_buffer;
|
|
|
|
} else {
|
2009-04-16 21:01:05 +00:00
|
|
|
// Reached the end of the available buffer.
|
|
|
|
ASSERT(capacity_ >= 5);
|
|
|
|
length_ = capacity_ - 1; // Indicate fullness of the stream.
|
|
|
|
buffer_[length_ - 4] = '.';
|
|
|
|
buffer_[length_ - 3] = '.';
|
|
|
|
buffer_[length_ - 2] = '.';
|
|
|
|
buffer_[length_ - 1] = '\n';
|
|
|
|
buffer_[length_] = '\0';
|
2008-07-03 15:10:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer_[length_] = c;
|
|
|
|
buffer_[length_ + 1] = '\0';
|
|
|
|
length_++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// A control character is one that configures a format element. For
|
|
|
|
// instance, in %.5s, .5 are control characters.
|
|
|
|
static bool IsControlChar(char c) {
|
|
|
|
switch (c) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8': case '9': case '.': case '-':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
void StringStream::Add(Vector<const char> format, Vector<FmtElm> elms) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// If we already ran out of space then return immediately.
|
2009-04-16 21:01:05 +00:00
|
|
|
if (full()) return;
|
2008-07-03 15:10:15 +00:00
|
|
|
int offset = 0;
|
|
|
|
int elm = 0;
|
2008-11-25 11:07:48 +00:00
|
|
|
while (offset < format.length()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (format[offset] != '%' || elm == elms.length()) {
|
|
|
|
Put(format[offset]);
|
|
|
|
offset++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Read this formatting directive into a temporary buffer
|
2008-09-11 14:34:48 +00:00
|
|
|
EmbeddedVector<char, 24> temp;
|
2008-07-03 15:10:15 +00:00
|
|
|
int format_length = 0;
|
|
|
|
// Skip over the whole control character sequence until the
|
|
|
|
// format element type
|
|
|
|
temp[format_length++] = format[offset++];
|
2008-11-25 11:07:48 +00:00
|
|
|
while (offset < format.length() && IsControlChar(format[offset]))
|
2008-07-03 15:10:15 +00:00
|
|
|
temp[format_length++] = format[offset++];
|
2008-11-25 11:07:48 +00:00
|
|
|
if (offset >= format.length())
|
|
|
|
return;
|
2008-07-03 15:10:15 +00:00
|
|
|
char type = format[offset];
|
|
|
|
temp[format_length++] = type;
|
|
|
|
temp[format_length] = '\0';
|
|
|
|
offset++;
|
|
|
|
FmtElm current = elms[elm++];
|
|
|
|
switch (type) {
|
|
|
|
case 's': {
|
|
|
|
ASSERT_EQ(FmtElm::C_STR, current.type_);
|
|
|
|
const char* value = current.data_.u_c_str_;
|
|
|
|
Add(value);
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
case 'w': {
|
|
|
|
ASSERT_EQ(FmtElm::LC_STR, current.type_);
|
|
|
|
Vector<const uc16> value = *current.data_.u_lc_str_;
|
|
|
|
for (int i = 0; i < value.length(); i++)
|
|
|
|
Put(static_cast<char>(value[i]));
|
|
|
|
break;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
case 'o': {
|
|
|
|
ASSERT_EQ(FmtElm::OBJ, current.type_);
|
|
|
|
Object* obj = current.data_.u_obj_;
|
|
|
|
PrintObject(obj);
|
|
|
|
break;
|
|
|
|
}
|
2008-11-25 11:07:48 +00:00
|
|
|
case 'k': {
|
|
|
|
ASSERT_EQ(FmtElm::INT, current.type_);
|
|
|
|
int value = current.data_.u_int_;
|
|
|
|
if (0x20 <= value && value <= 0x7F) {
|
|
|
|
Put(value);
|
|
|
|
} else if (value <= 0xff) {
|
|
|
|
Add("\\x%02x", value);
|
|
|
|
} else {
|
|
|
|
Add("\\u%04x", value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2009-08-03 10:53:45 +00:00
|
|
|
case 'i': case 'd': case 'u': case 'x': case 'c': case 'X': {
|
2008-07-03 15:10:15 +00:00
|
|
|
int value = current.data_.u_int_;
|
2008-09-11 14:34:48 +00:00
|
|
|
EmbeddedVector<char, 24> formatted;
|
2008-11-25 11:07:48 +00:00
|
|
|
int length = OS::SNPrintF(formatted, temp.start(), value);
|
|
|
|
Add(Vector<const char>(formatted.start(), length));
|
2008-07-03 15:10:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-11-11 14:16:24 +00:00
|
|
|
case 'f': case 'g': case 'G': case 'e': case 'E': {
|
|
|
|
double value = current.data_.u_double_;
|
|
|
|
EmbeddedVector<char, 28> formatted;
|
|
|
|
OS::SNPrintF(formatted, temp.start(), value);
|
|
|
|
Add(formatted.start());
|
|
|
|
break;
|
|
|
|
}
|
2009-08-03 10:53:45 +00:00
|
|
|
case 'p': {
|
|
|
|
void* value = current.data_.u_pointer_;
|
|
|
|
EmbeddedVector<char, 20> formatted;
|
|
|
|
OS::SNPrintF(formatted, temp.start(), value);
|
|
|
|
Add(formatted.start());
|
|
|
|
break;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-25 11:07:48 +00:00
|
|
|
// Verify that the buffer is 0-terminated
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(buffer_[length_] == '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::PrintObject(Object* o) {
|
|
|
|
o->ShortPrint(this);
|
|
|
|
if (o->IsString()) {
|
2009-11-24 14:10:06 +00:00
|
|
|
if (String::cast(o)->length() <= String::kMaxShortPrintLength) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (o->IsNumber() || o->IsOddball()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (o->IsHeapObject()) {
|
2013-09-03 11:54:08 +00:00
|
|
|
HeapObject* ho = HeapObject::cast(o);
|
|
|
|
DebugObjectCache* debug_object_cache = ho->GetIsolate()->
|
2011-03-18 20:35:07 +00:00
|
|
|
string_stream_debug_object_cache();
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < debug_object_cache->length(); i++) {
|
|
|
|
if ((*debug_object_cache)[i] == o) {
|
|
|
|
Add("#%d#", i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (debug_object_cache->length() < kMentionedObjectCacheMaxSize) {
|
|
|
|
Add("#%d#", debug_object_cache->length());
|
2013-09-02 11:39:23 +00:00
|
|
|
debug_object_cache->Add(HeapObject::cast(o));
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
Add("@%p", o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::Add(const char* format) {
|
2008-11-25 11:07:48 +00:00
|
|
|
Add(CStrVector(format));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::Add(Vector<const char> format) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Add(format, Vector<FmtElm>::empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::Add(const char* format, FmtElm arg0) {
|
|
|
|
const char argc = 1;
|
|
|
|
FmtElm argv[argc] = { arg0 };
|
2008-11-25 11:07:48 +00:00
|
|
|
Add(CStrVector(format), Vector<FmtElm>(argv, argc));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1) {
|
|
|
|
const char argc = 2;
|
|
|
|
FmtElm argv[argc] = { arg0, arg1 };
|
2008-11-25 11:07:48 +00:00
|
|
|
Add(CStrVector(format), Vector<FmtElm>(argv, argc));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1,
|
|
|
|
FmtElm arg2) {
|
|
|
|
const char argc = 3;
|
|
|
|
FmtElm argv[argc] = { arg0, arg1, arg2 };
|
2008-11-25 11:07:48 +00:00
|
|
|
Add(CStrVector(format), Vector<FmtElm>(argv, argc));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1,
|
|
|
|
FmtElm arg2, FmtElm arg3) {
|
|
|
|
const char argc = 4;
|
|
|
|
FmtElm argv[argc] = { arg0, arg1, arg2, arg3 };
|
|
|
|
Add(CStrVector(format), Vector<FmtElm>(argv, argc));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-24 12:29:37 +00:00
|
|
|
void StringStream::Add(const char* format, FmtElm arg0, FmtElm arg1,
|
|
|
|
FmtElm arg2, FmtElm arg3, FmtElm arg4) {
|
|
|
|
const char argc = 5;
|
|
|
|
FmtElm argv[argc] = { arg0, arg1, arg2, arg3, arg4 };
|
|
|
|
Add(CStrVector(format), Vector<FmtElm>(argv, argc));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-09 22:39:47 +00:00
|
|
|
SmartArrayPointer<const char> StringStream::ToCString() const {
|
2008-07-03 15:10:15 +00:00
|
|
|
char* str = NewArray<char>(length_ + 1);
|
2013-04-16 12:30:51 +00:00
|
|
|
OS::MemCopy(str, buffer_, length_);
|
2008-07-03 15:10:15 +00:00
|
|
|
str[length_] = '\0';
|
2011-09-09 22:39:47 +00:00
|
|
|
return SmartArrayPointer<const char>(str);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::Log() {
|
2011-03-18 20:35:07 +00:00
|
|
|
LOG(ISOLATE, StringEvent("StackDump", buffer_));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-20 10:38:19 +00:00
|
|
|
void StringStream::OutputToFile(FILE* out) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Dump the output to stdout, but make sure to break it up into
|
|
|
|
// manageable chunks to avoid losing parts of the output in the OS
|
|
|
|
// printing code. This is a problem on Windows in particular; see
|
|
|
|
// the VPrint() function implementations in platform-win32.cc.
|
|
|
|
unsigned position = 0;
|
|
|
|
for (unsigned next; (next = position + 2048) < length_; position = next) {
|
|
|
|
char save = buffer_[next];
|
|
|
|
buffer_[next] = '\0';
|
2010-12-20 10:38:19 +00:00
|
|
|
internal::PrintF(out, "%s", &buffer_[position]);
|
2008-07-03 15:10:15 +00:00
|
|
|
buffer_[next] = save;
|
|
|
|
}
|
2010-12-20 10:38:19 +00:00
|
|
|
internal::PrintF(out, "%s", &buffer_[position]);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-03 11:54:08 +00:00
|
|
|
Handle<String> StringStream::ToString(Isolate* isolate) {
|
|
|
|
return isolate->factory()->NewStringFromUtf8(
|
|
|
|
Vector<const char>(buffer_, length_));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-03 11:54:08 +00:00
|
|
|
void StringStream::ClearMentionedObjectCache(Isolate* isolate) {
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate->set_string_stream_current_security_token(NULL);
|
|
|
|
if (isolate->string_stream_debug_object_cache() == NULL) {
|
|
|
|
isolate->set_string_stream_debug_object_cache(
|
2013-09-02 11:39:23 +00:00
|
|
|
new List<HeapObject*, PreallocatedStorageAllocationPolicy>(0));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate->string_stream_debug_object_cache()->Clear();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2013-09-03 11:54:08 +00:00
|
|
|
bool StringStream::IsMentionedObjectCacheClear(Isolate* isolate) {
|
|
|
|
return isolate->string_stream_debug_object_cache()->length() == 0;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
bool StringStream::Put(String* str) {
|
|
|
|
return Put(str, 0, str->length());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool StringStream::Put(String* str, int start, int end) {
|
2012-12-31 11:13:50 +00:00
|
|
|
ConsStringIteratorOp op;
|
|
|
|
StringCharacterStream stream(str, &op, start);
|
|
|
|
for (int i = start; i < end && stream.HasMore(); i++) {
|
|
|
|
uint16_t c = stream.GetNext();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (c >= 127 || c < 32) {
|
|
|
|
c = '?';
|
|
|
|
}
|
2012-12-31 11:13:50 +00:00
|
|
|
if (!Put(static_cast<char>(c))) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return false; // Output was truncated.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::PrintName(Object* name) {
|
|
|
|
if (name->IsString()) {
|
|
|
|
String* str = String::cast(name);
|
|
|
|
if (str->length() > 0) {
|
|
|
|
Put(str);
|
|
|
|
} else {
|
|
|
|
Add("/* anonymous */");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Add("%o", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::PrintUsingMap(JSObject* js_object) {
|
|
|
|
Map* map = js_object->map();
|
2013-09-11 07:14:41 +00:00
|
|
|
if (!js_object->GetHeap()->Contains(map) ||
|
2008-07-03 15:10:15 +00:00
|
|
|
!map->IsHeapObject() ||
|
|
|
|
!map->IsMap()) {
|
|
|
|
Add("<Invalid map>\n");
|
|
|
|
return;
|
|
|
|
}
|
Sharing of descriptor arrays.
This CL adds multiple things:
Transition arrays do not directly point at their descriptor array anymore, but rather do so via an indirect pointer (a JSGlobalPropertyCell).
An ownership bit is added to maps indicating whether it owns its own descriptor array or not.
Maps owning a descriptor array can pass on ownership if a transition from that map is generated; but only if the descriptor array stays exactly the same; or if a descriptor is added.
Maps that don't have ownership get ownership back if their direct child to which ownership was passed is cleared in ClearNonLiveTransitions.
To detect which descriptors in an array are valid, each map knows its own NumberOfOwnDescriptors. Since the descriptors are sorted in order of addition, if we search and find a descriptor with index bigger than this number, it is not valid for the given map.
We currently still build up an enumeration cache (although this may disappear). The enumeration cache is always built for the entire descriptor array, even if not all descriptors are owned by the map. Once a descriptor array has an enumeration cache for a given map; this invariant will always be true, even if the descriptor array was extended. The extended array will inherit the enumeration cache from the smaller descriptor array. If a map with more descriptors needs an enumeration cache, it's EnumLength will still be set to invalid, so it will have to recompute the enumeration cache. This new cache will also be valid for smaller maps since they have their own enumlength; and use this to loop over the cache. If the EnumLength is still invalid, but there is already a cache present that is big enough; we just initialize the EnumLength field for the map.
When we apply ClearNonLiveTransitions and descriptor ownership is passed back to a parent map, the descriptor array is trimmed in-place and resorted. At the same time, the enumeration cache is trimmed in-place.
Only transition arrays contain descriptor arrays. If we transition to a map and pass ownership of the descriptor array along, the child map will not store the descriptor array it owns. Rather its parent will keep the pointer. So for every leaf-map, we find the descriptor array by following the back pointer, reading out the transition array, and fetching the descriptor array from the JSGlobalPropertyCell. If a map has a transition array, we fetch it from there. If a map has undefined as its back-pointer and has no transition array; it is considered to have an empty descriptor array.
When we modify properties, we cannot share the descriptor array. To accommodate this, the child map will get its own transition array; even if there are not necessarily any transitions leaving from the child map. This is necessary since it's the only way to store its own descriptor array.
Review URL: https://chromiumcodereview.appspot.com/10909007
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12492 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-09-12 16:43:57 +00:00
|
|
|
int real_size = map->NumberOfOwnDescriptors();
|
2009-07-10 19:25:18 +00:00
|
|
|
DescriptorArray* descs = map->instance_descriptors();
|
2013-05-07 13:09:23 +00:00
|
|
|
for (int i = 0; i < real_size; i++) {
|
Sharing of descriptor arrays.
This CL adds multiple things:
Transition arrays do not directly point at their descriptor array anymore, but rather do so via an indirect pointer (a JSGlobalPropertyCell).
An ownership bit is added to maps indicating whether it owns its own descriptor array or not.
Maps owning a descriptor array can pass on ownership if a transition from that map is generated; but only if the descriptor array stays exactly the same; or if a descriptor is added.
Maps that don't have ownership get ownership back if their direct child to which ownership was passed is cleared in ClearNonLiveTransitions.
To detect which descriptors in an array are valid, each map knows its own NumberOfOwnDescriptors. Since the descriptors are sorted in order of addition, if we search and find a descriptor with index bigger than this number, it is not valid for the given map.
We currently still build up an enumeration cache (although this may disappear). The enumeration cache is always built for the entire descriptor array, even if not all descriptors are owned by the map. Once a descriptor array has an enumeration cache for a given map; this invariant will always be true, even if the descriptor array was extended. The extended array will inherit the enumeration cache from the smaller descriptor array. If a map with more descriptors needs an enumeration cache, it's EnumLength will still be set to invalid, so it will have to recompute the enumeration cache. This new cache will also be valid for smaller maps since they have their own enumlength; and use this to loop over the cache. If the EnumLength is still invalid, but there is already a cache present that is big enough; we just initialize the EnumLength field for the map.
When we apply ClearNonLiveTransitions and descriptor ownership is passed back to a parent map, the descriptor array is trimmed in-place and resorted. At the same time, the enumeration cache is trimmed in-place.
Only transition arrays contain descriptor arrays. If we transition to a map and pass ownership of the descriptor array along, the child map will not store the descriptor array it owns. Rather its parent will keep the pointer. So for every leaf-map, we find the descriptor array by following the back pointer, reading out the transition array, and fetching the descriptor array from the JSGlobalPropertyCell. If a map has a transition array, we fetch it from there. If a map has undefined as its back-pointer and has no transition array; it is considered to have an empty descriptor array.
When we modify properties, we cannot share the descriptor array. To accommodate this, the child map will get its own transition array; even if there are not necessarily any transitions leaving from the child map. This is necessary since it's the only way to store its own descriptor array.
Review URL: https://chromiumcodereview.appspot.com/10909007
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@12492 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-09-12 16:43:57 +00:00
|
|
|
PropertyDetails details = descs->GetDetails(i);
|
|
|
|
if (details.type() == FIELD) {
|
2011-11-09 12:47:15 +00:00
|
|
|
Object* key = descs->GetKey(i);
|
|
|
|
if (key->IsString() || key->IsNumber()) {
|
|
|
|
int len = 3;
|
|
|
|
if (key->IsString()) {
|
|
|
|
len = String::cast(key)->length();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-11-09 12:47:15 +00:00
|
|
|
for (; len < 18; len++)
|
|
|
|
Put(' ');
|
|
|
|
if (key->IsString()) {
|
|
|
|
Put(String::cast(key));
|
|
|
|
} else {
|
|
|
|
key->ShortPrint();
|
|
|
|
}
|
|
|
|
Add(": ");
|
2013-05-08 15:02:08 +00:00
|
|
|
Object* value = js_object->RawFastPropertyAt(descs->GetFieldIndex(i));
|
2011-11-09 12:47:15 +00:00
|
|
|
Add("%o\n", value);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::PrintFixedArray(FixedArray* array, unsigned int limit) {
|
2013-09-11 07:14:41 +00:00
|
|
|
Heap* heap = array->GetHeap();
|
2008-07-03 15:10:15 +00:00
|
|
|
for (unsigned int i = 0; i < 10 && i < limit; i++) {
|
|
|
|
Object* element = array->get(i);
|
2011-03-18 20:35:07 +00:00
|
|
|
if (element != heap->the_hole_value()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int len = 1; len < 18; len++)
|
|
|
|
Put(' ');
|
|
|
|
Add("%d: %o\n", i, array->get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (limit >= 10) {
|
|
|
|
Add(" ...\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::PrintByteArray(ByteArray* byte_array) {
|
|
|
|
unsigned int limit = byte_array->length();
|
|
|
|
for (unsigned int i = 0; i < 10 && i < limit; i++) {
|
|
|
|
byte b = byte_array->get(i);
|
|
|
|
Add(" %d: %3d 0x%02x", i, b, b);
|
|
|
|
if (b >= ' ' && b <= '~') {
|
|
|
|
Add(" '%c'", b);
|
|
|
|
} else if (b == '\n') {
|
|
|
|
Add(" '\n'");
|
|
|
|
} else if (b == '\r') {
|
|
|
|
Add(" '\r'");
|
|
|
|
} else if (b >= 1 && b <= 26) {
|
|
|
|
Add(" ^%c", b + 'A' - 1);
|
|
|
|
}
|
|
|
|
Add("\n");
|
|
|
|
}
|
|
|
|
if (limit >= 10) {
|
|
|
|
Add(" ...\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-03 11:54:08 +00:00
|
|
|
void StringStream::PrintMentionedObjectCache(Isolate* isolate) {
|
2011-03-18 20:35:07 +00:00
|
|
|
DebugObjectCache* debug_object_cache =
|
2013-09-03 11:54:08 +00:00
|
|
|
isolate->string_stream_debug_object_cache();
|
2008-07-03 15:10:15 +00:00
|
|
|
Add("==== Key ============================================\n\n");
|
|
|
|
for (int i = 0; i < debug_object_cache->length(); i++) {
|
|
|
|
HeapObject* printee = (*debug_object_cache)[i];
|
|
|
|
Add(" #%d# %p: ", i, printee);
|
|
|
|
printee->ShortPrint(this);
|
|
|
|
Add("\n");
|
|
|
|
if (printee->IsJSObject()) {
|
|
|
|
if (printee->IsJSValue()) {
|
|
|
|
Add(" value(): %o\n", JSValue::cast(printee)->value());
|
|
|
|
}
|
|
|
|
PrintUsingMap(JSObject::cast(printee));
|
|
|
|
if (printee->IsJSArray()) {
|
|
|
|
JSArray* array = JSArray::cast(printee);
|
2012-05-23 14:24:29 +00:00
|
|
|
if (array->HasFastObjectElements()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
unsigned int limit = FixedArray::cast(array->elements())->length();
|
|
|
|
unsigned int length =
|
|
|
|
static_cast<uint32_t>(JSArray::cast(array)->length()->Number());
|
|
|
|
if (length < limit) limit = length;
|
|
|
|
PrintFixedArray(FixedArray::cast(array->elements()), limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (printee->IsByteArray()) {
|
|
|
|
PrintByteArray(ByteArray::cast(printee));
|
|
|
|
} else if (printee->IsFixedArray()) {
|
|
|
|
unsigned int limit = FixedArray::cast(printee)->length();
|
|
|
|
PrintFixedArray(FixedArray::cast(printee), limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::PrintSecurityTokenIfChanged(Object* f) {
|
2013-09-03 11:54:08 +00:00
|
|
|
if (!f->IsHeapObject()) return;
|
|
|
|
HeapObject* obj = HeapObject::cast(f);
|
|
|
|
Isolate* isolate = obj->GetIsolate();
|
2011-03-18 20:35:07 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2013-09-03 11:54:08 +00:00
|
|
|
if (!heap->Contains(obj)) return;
|
|
|
|
Map* map = obj->map();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!map->IsHeapObject() ||
|
2011-03-18 20:35:07 +00:00
|
|
|
!heap->Contains(map) ||
|
2008-07-03 15:10:15 +00:00
|
|
|
!map->IsMap() ||
|
|
|
|
!f->IsJSFunction()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSFunction* fun = JSFunction::cast(f);
|
2013-06-26 14:04:25 +00:00
|
|
|
Object* perhaps_context = fun->context();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (perhaps_context->IsHeapObject() &&
|
2011-03-18 20:35:07 +00:00
|
|
|
heap->Contains(HeapObject::cast(perhaps_context)) &&
|
2008-07-03 15:10:15 +00:00
|
|
|
perhaps_context->IsContext()) {
|
|
|
|
Context* context = fun->context();
|
2011-03-18 20:35:07 +00:00
|
|
|
if (!heap->Contains(context)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Add("(Function context is outside heap)\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-08-17 09:03:08 +00:00
|
|
|
Object* token = context->native_context()->security_token();
|
2011-03-18 20:35:07 +00:00
|
|
|
if (token != isolate->string_stream_current_security_token()) {
|
Split window support from V8.
Here is a description of the background and design of split window in Chrome and V8:
https://docs.google.com/a/google.com/Doc?id=chhjkpg_47fwddxbfr
This change list splits the window object into two parts: 1) an inner window object used as the global object of contexts; 2) an outer window object exposed to JavaScript and accessible by the name 'window'. Firefox did it awhile ago, here are some discussions: https://wiki.mozilla.org/Gecko:SplitWindow. One additional benefit of splitting window in Chrome is that accessing global variables don't need security checks anymore, it can improve applications that use many global variables.
V8 support of split window:
There are a small number of changes on V8 api to support split window:
Security context is removed from V8, so does related API functions;
A global object can be detached from its context and reused by a new context;
Access checks on an object template can be turned on/off by default;
An object can turn on its access checks later;
V8 has a new object type, ApiGlobalObject, which is the outer window object type. The existing JSGlobalObject becomes the inner window object type. Security checks are moved from JSGlobalObject to ApiGlobalObject. ApiGlobalObject is the one exposed to JavaScript, it is accessible through Context::Global(). ApiGlobalObject's prototype is set to JSGlobalObject so that property lookups are forwarded to JSGlobalObject. ApiGlobalObject forwards all other property access requests to JSGlobalObject, such as SetProperty, DeleteProperty, etc.
Security token is moved to a global context, and ApiGlobalObject has a reference to its global context. JSGlobalObject has a reference to its global context as well. When accessing properties on a global object in JavaScript, the domain security check is performed by comparing the security token of the lexical context (Top::global_context()) to the token of global object's context. The check is only needed when the receiver is a window object, such as 'window.document'. Accessing global variables, such as 'var foo = 3; foo' does not need checks because the receiver is the inner window object.
When an outer window is detached from its global context (when a frame navigates away from a page), it is completely detached from the inner window. A new context is created for the new page, and the outer global object is reused. At this point, the access check on the DOMWindow wrapper of the old context is turned on. The code in old context is still able to access DOMWindow properties, but it has to go through domain security checks.
It is debatable on how to implement the outer window object. Currently each property access function has to check if the receiver is ApiGlobalObject type. This approach might be error-prone that one may forget to check the receiver when adding new functions. It is unlikely a performance issue because accessing global variables are more common than 'window.foo' style coding.
I am still working on the ARM port, and I'd like to hear comments and suggestions on the best way to support it in V8.
Review URL: http://codereview.chromium.org/7366
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@540 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2008-10-21 19:07:58 +00:00
|
|
|
Add("Security context: %o\n", token);
|
2011-03-18 20:35:07 +00:00
|
|
|
isolate->set_string_stream_current_security_token(token);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Add("(Function context is corrupt)\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::PrintFunction(Object* f, Object* receiver, Code** code) {
|
2013-09-11 07:14:41 +00:00
|
|
|
if (!f->IsHeapObject()) {
|
|
|
|
Add("/* warning: 'function' was not a heap object */ ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Heap* heap = HeapObject::cast(f)->GetHeap();
|
|
|
|
if (!heap->Contains(HeapObject::cast(f))) {
|
|
|
|
Add("/* warning: 'function' was not on the heap */ ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!heap->Contains(HeapObject::cast(f)->map())) {
|
|
|
|
Add("/* warning: function's map was not on the heap */ ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!HeapObject::cast(f)->map()->IsMap()) {
|
|
|
|
Add("/* warning: function's map was not a valid map */ ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (f->IsJSFunction()) {
|
|
|
|
JSFunction* fun = JSFunction::cast(f);
|
|
|
|
// Common case: on-stack function present and resolved.
|
|
|
|
PrintPrototype(fun, receiver);
|
|
|
|
*code = fun->code();
|
|
|
|
} else if (f->IsInternalizedString()) {
|
|
|
|
// Unresolved and megamorphic calls: Instead of the function
|
|
|
|
// we have the function name on the stack.
|
|
|
|
PrintName(f);
|
|
|
|
Add("/* unresolved */ ");
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2013-09-11 07:14:41 +00:00
|
|
|
// Unless this is the frame of a built-in function, we should always have
|
|
|
|
// the callee function or name on the stack. If we don't, we have a
|
|
|
|
// problem or a change of the stack frame layout.
|
|
|
|
Add("%o", f);
|
|
|
|
Add("/* warning: no JSFunction object or function name found */ ");
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StringStream::PrintPrototype(JSFunction* fun, Object* receiver) {
|
|
|
|
Object* name = fun->shared()->name();
|
|
|
|
bool print_name = false;
|
2013-02-27 13:22:29 +00:00
|
|
|
Isolate* isolate = fun->GetIsolate();
|
|
|
|
for (Object* p = receiver;
|
|
|
|
p != isolate->heap()->null_value();
|
|
|
|
p = p->GetPrototype(isolate)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (p->IsJSObject()) {
|
|
|
|
Object* key = JSObject::cast(p)->SlowReverseLookup(fun);
|
2013-02-27 13:22:29 +00:00
|
|
|
if (key != isolate->heap()->undefined_value()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!name->IsString() ||
|
|
|
|
!key->IsString() ||
|
|
|
|
!String::cast(name)->Equals(String::cast(key))) {
|
|
|
|
print_name = true;
|
|
|
|
}
|
|
|
|
if (name->IsString() && String::cast(name)->length() == 0) {
|
|
|
|
print_name = false;
|
|
|
|
}
|
|
|
|
name = key;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print_name = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PrintName(name);
|
|
|
|
// Also known as - if the name in the function doesn't match the name under
|
|
|
|
// which it was looked up.
|
|
|
|
if (print_name) {
|
|
|
|
Add("(aka ");
|
|
|
|
PrintName(fun->shared()->name());
|
|
|
|
Put(')');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char* HeapStringAllocator::grow(unsigned* bytes) {
|
|
|
|
unsigned new_bytes = *bytes * 2;
|
|
|
|
// Check for overflow.
|
|
|
|
if (new_bytes <= *bytes) {
|
|
|
|
return space_;
|
|
|
|
}
|
|
|
|
char* new_space = NewArray<char>(new_bytes);
|
|
|
|
if (new_space == NULL) {
|
|
|
|
return space_;
|
|
|
|
}
|
2013-04-16 12:30:51 +00:00
|
|
|
OS::MemCopy(new_space, space_, *bytes);
|
2008-07-03 15:10:15 +00:00
|
|
|
*bytes = new_bytes;
|
|
|
|
DeleteArray(space_);
|
|
|
|
space_ = new_space;
|
|
|
|
return new_space;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-16 21:01:05 +00:00
|
|
|
// Only grow once to the maximum allowable size.
|
2008-07-03 15:10:15 +00:00
|
|
|
char* NoAllocationStringAllocator::grow(unsigned* bytes) {
|
2009-04-16 21:01:05 +00:00
|
|
|
ASSERT(size_ >= *bytes);
|
|
|
|
*bytes = size_;
|
2008-07-03 15:10:15 +00:00
|
|
|
return space_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|