Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: src/runtime.cc

Issue 6568007: Fix array concat to follow the specification in the presence of element getters. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge/build-ia32
Patch Set: git utd Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/objects-inl.h ('k') | test/mjsunit/array-concat.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 8010 matching lines...) Expand 10 before | Expand all | Expand 10 after
8021 * of FixedArray, the class can be used by both fast and slow cases. 8021 * of FixedArray, the class can be used by both fast and slow cases.
8022 * The second parameter of the constructor, fast_elements, specifies 8022 * The second parameter of the constructor, fast_elements, specifies
8023 * whether the storage is a FixedArray or Dictionary. 8023 * whether the storage is a FixedArray or Dictionary.
8024 * 8024 *
8025 * An index limit is used to deal with the situation that a result array 8025 * An index limit is used to deal with the situation that a result array
8026 * length overflows 32-bit non-negative integer. 8026 * length overflows 32-bit non-negative integer.
8027 */ 8027 */
8028 class ArrayConcatVisitor { 8028 class ArrayConcatVisitor {
8029 public: 8029 public:
8030 ArrayConcatVisitor(Handle<FixedArray> storage, 8030 ArrayConcatVisitor(Handle<FixedArray> storage,
8031 uint32_t index_limit,
8032 bool fast_elements) : 8031 bool fast_elements) :
8033 storage_(storage), index_limit_(index_limit), 8032 storage_(storage),
8034 index_offset_(0), fast_elements_(fast_elements) { } 8033 index_offset_(0u),
8034 fast_elements_(fast_elements) { }
8035 8035
8036 void visit(uint32_t i, Handle<Object> elm) { 8036 void visit(uint32_t i, Handle<Object> elm) {
8037 if (i >= index_limit_ - index_offset_) return; 8037 if (i >= JSObject::kMaxElementCount - index_offset_) return;
8038 uint32_t index = index_offset_ + i; 8038 uint32_t index = index_offset_ + i;
8039 8039
8040 if (fast_elements_) { 8040 if (fast_elements_) {
8041 ASSERT(index < static_cast<uint32_t>(storage_->length())); 8041 if (index < static_cast<uint32_t>(storage_->length())) {
8042 storage_->set(index, *elm); 8042 storage_->set(index, *elm);
8043 8043 return;
8044 } else { 8044 }
8045 Handle<NumberDictionary> dict = Handle<NumberDictionary>::cast(storage_); 8045 // Our initial estimate of length was foiled, possibly by
8046 Handle<NumberDictionary> result = 8046 // getters on the arrays increasing the length of later arrays
8047 Factory::DictionaryAtNumberPut(dict, index, elm); 8047 // during iteration.
8048 if (!result.is_identical_to(dict)) 8048 // This shouldn't happen in anything but pathological cases.
8049 storage_ = result; 8049 SetDictionaryMode(index);
8050 } 8050 // Fall-through to dictionary mode.
8051 } 8051 }
8052 ASSERT(!fast_elements_);
8053 Handle<NumberDictionary> dict(storage_.cast<NumberDictionary>());
8054 Handle<NumberDictionary> result =
8055 Factory::DictionaryAtNumberPut(dict, index, elm);
8056 if (!result.is_identical_to(dict)) {
8057 storage_ = Handle<FixedArray>::cast(result);
8058 }
8059 }
8052 8060
8053 void increase_index_offset(uint32_t delta) { 8061 void increase_index_offset(uint32_t delta) {
8054 if (index_limit_ - index_offset_ < delta) { 8062 if (JSObject::kMaxElementCount - index_offset_ < delta) {
8055 index_offset_ = index_limit_; 8063 index_offset_ = JSObject::kMaxElementCount;
8056 } else { 8064 } else {
8057 index_offset_ += delta; 8065 index_offset_ += delta;
8058 } 8066 }
8059 } 8067 }
8060 8068
8061 Handle<FixedArray> storage() { return storage_; } 8069 Handle<JSArray> ToArray() {
8070 Handle<JSArray> array = Factory::NewJSArray(0);
8071 Handle<Object> length =
8072 Factory::NewNumber(static_cast<double>(index_offset_));
8073 Handle<Map> map;
8074 if (fast_elements_) {
8075 map = Factory::GetFastElementsMap(Handle<Map>(array->map()));
8076 } else {
8077 map = Factory::GetSlowElementsMap(Handle<Map>(array->map()));
8078 }
8079 array->set_map(*map);
8080 array->set_length(*length);
8081 array->set_elements(*storage_);
8082 return array;
8083 }
8062 8084
8063 private: 8085 private:
8064 Handle<FixedArray> storage_; 8086 // Convert storage to dictionary mode.
8065 // Limit on the accepted indices. Elements with indices larger than the 8087 void SetDictionaryMode(uint32_t index) {
8066 // limit are ignored by the visitor. 8088 ASSERT(fast_elements_);
8067 uint32_t index_limit_; 8089 Handle<FixedArray> current_storage(storage_.ToHandle());
8068 // Index after last seen index. Always less than or equal to index_limit_. 8090 HandleCell<NumberDictionary> slow_storage(
8091 Factory::NewNumberDictionary(current_storage->length()));
8092 uint32_t current_length = static_cast<uint32_t>(current_storage->length());
8093 for (uint32_t i = 0; i < current_length; i++) {
8094 HandleScope loop_scope;
8095 Handle<Object> element(current_storage->get(i));
8096 if (!element->IsTheHole()) {
8097 slow_storage =
8098 Factory::DictionaryAtNumberPut(slow_storage.ToHandle(), i, element);
8099 }
8100 }
8101 storage_ = slow_storage.cast<FixedArray>();
8102 fast_elements_ = false;
8103 }
8104
8105 HandleCell<FixedArray> storage_;
8106 // Index after last seen index. Always less than or equal to
8107 // JSObject::kMaxElementCount.
8069 uint32_t index_offset_; 8108 uint32_t index_offset_;
8070 const bool fast_elements_; 8109 bool fast_elements_;
8071 }; 8110 };
8072 8111
8073 8112
8113 static uint32_t EstimateElementCount(Handle<JSArray> array) {
8114 uint32_t length = static_cast<uint32_t>(array->length()->Number());
8115 int element_count = 0;
8116 switch (array->GetElementsKind()) {
8117 case JSObject::FAST_ELEMENTS: {
8118 // Fast elements can't have lengths that are not representable by
8119 // a 32-bit signed integer.
8120 ASSERT(static_cast<int32_t>(FixedArray::kMaxLength) >= 0);
8121 int fast_length = static_cast<int>(length);
8122 Handle<FixedArray> elements(FixedArray::cast(array->elements()));
8123 for (int i = 0; i < fast_length; i++) {
8124 if (!elements->get(i)->IsTheHole()) element_count++;
8125 }
8126 break;
8127 }
8128 case JSObject::DICTIONARY_ELEMENTS: {
8129 Handle<NumberDictionary> dictionary(
8130 NumberDictionary::cast(array->elements()));
8131 int capacity = dictionary->Capacity();
8132 for (int i = 0; i < capacity; i++) {
8133 Handle<Object> key(dictionary->KeyAt(i));
8134 if (dictionary->IsKey(*key)) {
8135 element_count++;
8136 }
8137 }
8138 break;
8139 }
8140 default:
8141 // External arrays are always dense.
8142 return length;
8143 }
8144 // As an estimate, we assume that the prototype doesn't contain any
8145 // inherited elements.
8146 return element_count;
8147 }
8148
8149
8150
8074 template<class ExternalArrayClass, class ElementType> 8151 template<class ExternalArrayClass, class ElementType>
8075 static uint32_t IterateExternalArrayElements(Handle<JSObject> receiver, 8152 static void IterateExternalArrayElements(Handle<JSObject> receiver,
8076 bool elements_are_ints, 8153 bool elements_are_ints,
8077 bool elements_are_guaranteed_smis, 8154 bool elements_are_guaranteed_smis,
8078 uint32_t range, 8155 ArrayConcatVisitor* visitor) {
8079 ArrayConcatVisitor* visitor) {
8080 Handle<ExternalArrayClass> array( 8156 Handle<ExternalArrayClass> array(
8081 ExternalArrayClass::cast(receiver->elements())); 8157 ExternalArrayClass::cast(receiver->elements()));
8082 uint32_t len = Min(static_cast<uint32_t>(array->length()), range); 8158 uint32_t len = static_cast<uint32_t>(array->length());
8083 8159
8084 if (visitor != NULL) { 8160 ASSERT(visitor != NULL);
8085 if (elements_are_ints) { 8161 if (elements_are_ints) {
8086 if (elements_are_guaranteed_smis) { 8162 if (elements_are_guaranteed_smis) {
8087 for (uint32_t j = 0; j < len; j++) { 8163 for (uint32_t j = 0; j < len; j++) {
8088 Handle<Smi> e(Smi::FromInt(static_cast<int>(array->get(j)))); 8164 HandleScope loop_scope;
8089 visitor->visit(j, e); 8165 Handle<Smi> e(Smi::FromInt(static_cast<int>(array->get(j))));
8090 } 8166 visitor->visit(j, e);
8091 } else {
8092 for (uint32_t j = 0; j < len; j++) {
8093 int64_t val = static_cast<int64_t>(array->get(j));
8094 if (Smi::IsValid(static_cast<intptr_t>(val))) {
8095 Handle<Smi> e(Smi::FromInt(static_cast<int>(val)));
8096 visitor->visit(j, e);
8097 } else {
8098 Handle<Object> e =
8099 Factory::NewNumber(static_cast<ElementType>(val));
8100 visitor->visit(j, e);
8101 }
8102 }
8103 } 8167 }
8104 } else { 8168 } else {
8105 for (uint32_t j = 0; j < len; j++) { 8169 for (uint32_t j = 0; j < len; j++) {
8106 Handle<Object> e = Factory::NewNumber(array->get(j)); 8170 HandleScope loop_scope;
8107 visitor->visit(j, e); 8171 int64_t val = static_cast<int64_t>(array->get(j));
8108 } 8172 if (Smi::IsValid(static_cast<intptr_t>(val))) {
8109 } 8173 Handle<Smi> e(Smi::FromInt(static_cast<int>(val)));
8110 } 8174 visitor->visit(j, e);
8111 8175 } else {
8112 return len; 8176 Handle<Object> e =
8113 } 8177 Factory::NewNumber(static_cast<ElementType>(val));
8114 8178 visitor->visit(j, e);
8115 /** 8179 }
8116 * A helper function that visits elements of a JSObject. Only elements 8180 }
8117 * whose index between 0 and range (exclusive) are visited. 8181 }
8118 * 8182 } else {
8119 * If the third parameter, visitor, is not NULL, the visitor is called 8183 for (uint32_t j = 0; j < len; j++) {
8120 * with parameters, 'visitor_index_offset + element index' and the element. 8184 HandleScope loop_scope;
8121 * 8185 Handle<Object> e = Factory::NewNumber(array->get(j));
8122 * It returns the number of visisted elements. 8186 visitor->visit(j, e);
8123 */ 8187 }
8124 static uint32_t IterateElements(Handle<JSObject> receiver, 8188 }
8125 uint32_t range, 8189 }
8126 ArrayConcatVisitor* visitor) { 8190
8127 uint32_t num_of_elements = 0; 8191
8128 8192 // Used for sorting indices in a List<uint32_t>.
8129 switch (receiver->GetElementsKind()) { 8193 static int compareUInt32(const uint32_t* ap, const uint32_t* bp) {
8194 uint32_t a = *ap;
8195 uint32_t b = *bp;
8196 return (a == b) ? 0 : (a < b) ? -1 : 1;
8197 }
8198
8199
8200 static void CollectElementIndices(Handle<JSObject> object,
8201 uint32_t range,
8202 List<uint32_t>* indices) {
8203 JSObject::ElementsKind kind = object->GetElementsKind();
8204 switch (kind) {
8130 case JSObject::FAST_ELEMENTS: { 8205 case JSObject::FAST_ELEMENTS: {
8131 Handle<FixedArray> elements(FixedArray::cast(receiver->elements())); 8206 Handle<FixedArray> elements(FixedArray::cast(object->elements()));
8132 uint32_t len = elements->length(); 8207 uint32_t length = static_cast<uint32_t>(elements->length());
8133 if (range < len) { 8208 if (range < length) length = range;
8134 len = range; 8209 for (uint32_t i = 0; i < length; i++) {
8135 } 8210 if (!elements->get(i)->IsTheHole()) {
8136 8211 indices->Add(i);
8137 for (uint32_t j = 0; j < len; j++) { 8212 }
8138 Handle<Object> e(elements->get(j)); 8213 }
8139 if (!e->IsTheHole()) {
8140 num_of_elements++;
8141 if (visitor) {
8142 visitor->visit(j, e);
8143 }
8144 }
8145 }
8146 break;
8147 }
8148 case JSObject::PIXEL_ELEMENTS: {
8149 Handle<PixelArray> pixels(PixelArray::cast(receiver->elements()));
8150 uint32_t len = pixels->length();
8151 if (range < len) {
8152 len = range;
8153 }
8154
8155 for (uint32_t j = 0; j < len; j++) {
8156 num_of_elements++;
8157 if (visitor != NULL) {
8158 Handle<Smi> e(Smi::FromInt(pixels->get(j)));
8159 visitor->visit(j, e);
8160 }
8161 }
8162 break;
8163 }
8164 case JSObject::EXTERNAL_BYTE_ELEMENTS: {
8165 num_of_elements =
8166 IterateExternalArrayElements<ExternalByteArray, int8_t>(
8167 receiver, true, true, range, visitor);
8168 break;
8169 }
8170 case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: {
8171 num_of_elements =
8172 IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>(
8173 receiver, true, true, range, visitor);
8174 break;
8175 }
8176 case JSObject::EXTERNAL_SHORT_ELEMENTS: {
8177 num_of_elements =
8178 IterateExternalArrayElements<ExternalShortArray, int16_t>(
8179 receiver, true, true, range, visitor);
8180 break;
8181 }
8182 case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: {
8183 num_of_elements =
8184 IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>(
8185 receiver, true, true, range, visitor);
8186 break;
8187 }
8188 case JSObject::EXTERNAL_INT_ELEMENTS: {
8189 num_of_elements =
8190 IterateExternalArrayElements<ExternalIntArray, int32_t>(
8191 receiver, true, false, range, visitor);
8192 break;
8193 }
8194 case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: {
8195 num_of_elements =
8196 IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>(
8197 receiver, true, false, range, visitor);
8198 break;
8199 }
8200 case JSObject::EXTERNAL_FLOAT_ELEMENTS: {
8201 num_of_elements =
8202 IterateExternalArrayElements<ExternalFloatArray, float>(
8203 receiver, false, false, range, visitor);
8204 break; 8214 break;
8205 } 8215 }
8206 case JSObject::DICTIONARY_ELEMENTS: { 8216 case JSObject::DICTIONARY_ELEMENTS: {
8207 Handle<NumberDictionary> dict(receiver->element_dictionary()); 8217 Handle<NumberDictionary> dict(NumberDictionary::cast(object->elements()));
8208 uint32_t capacity = dict->Capacity(); 8218 uint32_t capacity = dict->Capacity();
8209 for (uint32_t j = 0; j < capacity; j++) { 8219 for (uint32_t j = 0; j < capacity; j++) {
8220 HandleScope loop_scope;
8210 Handle<Object> k(dict->KeyAt(j)); 8221 Handle<Object> k(dict->KeyAt(j));
8211 if (dict->IsKey(*k)) { 8222 if (dict->IsKey(*k)) {
8212 ASSERT(k->IsNumber()); 8223 ASSERT(k->IsNumber());
8213 uint32_t index = static_cast<uint32_t>(k->Number()); 8224 uint32_t index = static_cast<uint32_t>(k->Number());
8214 if (index < range) { 8225 if (index < range) {
8215 num_of_elements++; 8226 indices->Add(index);
8216 if (visitor) {
8217 visitor->visit(index, Handle<Object>(dict->ValueAt(j)));
8218 }
8219 } 8227 }
8220 } 8228 }
8221 } 8229 }
8222 break; 8230 break;
8223 } 8231 }
8232 default: {
8233 int dense_elements_length;
8234 switch (kind) {
8235 case JSObject::PIXEL_ELEMENTS: {
8236 dense_elements_length =
8237 PixelArray::cast(object->elements())->length();
8238 break;
8239 }
8240 case JSObject::EXTERNAL_BYTE_ELEMENTS: {
8241 dense_elements_length =
8242 ExternalByteArray::cast(object->elements())->length();
8243 break;
8244 }
8245 case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: {
8246 dense_elements_length =
8247 ExternalUnsignedByteArray::cast(object->elements())->length();
8248 break;
8249 }
8250 case JSObject::EXTERNAL_SHORT_ELEMENTS: {
8251 dense_elements_length =
8252 ExternalShortArray::cast(object->elements())->length();
8253 break;
8254 }
8255 case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: {
8256 dense_elements_length =
8257 ExternalUnsignedShortArray::cast(object->elements())->length();
8258 break;
8259 }
8260 case JSObject::EXTERNAL_INT_ELEMENTS: {
8261 dense_elements_length =
8262 ExternalIntArray::cast(object->elements())->length();
8263 break;
8264 }
8265 case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: {
8266 dense_elements_length =
8267 ExternalUnsignedIntArray::cast(object->elements())->length();
8268 break;
8269 }
8270 case JSObject::EXTERNAL_FLOAT_ELEMENTS: {
8271 dense_elements_length =
8272 ExternalFloatArray::cast(object->elements())->length();
8273 break;
8274 }
8275 default:
8276 UNREACHABLE();
8277 break;
8278 }
8279 uint32_t length = static_cast<uint32_t>(dense_elements_length);
8280 if (range <= length) {
8281 length = range;
8282 // We will add all indices, so we might as well clear it first
8283 // and avoid duplicates.
8284 indices->Clear();
8285 }
8286 for (uint32_t i = 0; i < length; i++) {
8287 indices->Add(i);
8288 }
8289 if (length == range) return; // All indices accounted for already.
8290 break;
8291 }
8292 }
8293
8294 Handle<Object> prototype(object->GetPrototype());
8295 if (prototype->IsJSObject()) {
8296 // The prototype will usually have no inherited element indices,
8297 // but we have to check.
8298 CollectElementIndices(Handle<JSObject>::cast(prototype), range, indices);
8299 }
8300 }
8301
8302
8303 /**
8304 * A helper function that visits elements of a JSArray in numerical
8305 * order.
8306 *
8307 * The visitor argument called for each existing element in the array
8308 * with the element index and the element's value.
8309 * Afterwards it increments the base-index of the visitor by the array
8310 * length.
8311 */
8312 static void IterateElements(Handle<JSArray> receiver,
8313 ArrayConcatVisitor* visitor) {
8314 uint32_t length = static_cast<uint32_t>(receiver->length()->Number());
8315 switch (receiver->GetElementsKind()) {
8316 case JSObject::FAST_ELEMENTS: {
8317 // Run through the elements FixedArray and use HasElement and GetElement
8318 // to check the prototype for missing elements.
8319 Handle<FixedArray> elements(FixedArray::cast(receiver->elements()));
8320 int fast_length = static_cast<int>(length);
8321 ASSERT(fast_length <= elements->length());
8322 for (int j = 0; j < fast_length; j++) {
8323 HandleScope loop_scope;
8324 Handle<Object> element_value(elements->get(j));
8325 if (!element_value->IsTheHole()) {
8326 visitor->visit(j, element_value);
8327 } else if (receiver->HasElement(j)) {
8328 // Call GetElement on receiver, not its prototype, or getters won't
8329 // have the correct receiver.
8330 element_value = GetElement(receiver, j);
8331 visitor->visit(j, element_value);
8332 }
8333 }
8334 break;
8335 }
8336 case JSObject::DICTIONARY_ELEMENTS: {
8337 Handle<NumberDictionary> dict(receiver->element_dictionary());
8338 List<uint32_t> indices(dict->Capacity() / 2);
8339 // Collect all indices in the object and the prototypes less
8340 // than length. This might introduce duplicates in the indices list.
8341 CollectElementIndices(receiver, length, &indices);
8342 indices.Sort(&compareUInt32);
8343 int j = 0;
8344 int n = indices.length();
8345 while (j < n) {
8346 HandleScope loop_scope;
8347 uint32_t index = indices[j];
8348 Handle<Object> element = GetElement(receiver, index);
8349 visitor->visit(index, element);
8350 // Skip to next different index (i.e., omit duplicates).
8351 do {
8352 j++;
8353 } while (j < n && indices[j] == index);
8354 }
8355 break;
8356 }
8357 case JSObject::PIXEL_ELEMENTS: {
8358 Handle<PixelArray> pixels(PixelArray::cast(receiver->elements()));
8359 for (uint32_t j = 0; j < length; j++) {
8360 Handle<Smi> e(Smi::FromInt(pixels->get(j)));
8361 visitor->visit(j, e);
8362 }
8363 break;
8364 }
8365 case JSObject::EXTERNAL_BYTE_ELEMENTS: {
8366 IterateExternalArrayElements<ExternalByteArray, int8_t>(
8367 receiver, true, true, visitor);
8368 break;
8369 }
8370 case JSObject::EXTERNAL_UNSIGNED_BYTE_ELEMENTS: {
8371 IterateExternalArrayElements<ExternalUnsignedByteArray, uint8_t>(
8372 receiver, true, true, visitor);
8373 break;
8374 }
8375 case JSObject::EXTERNAL_SHORT_ELEMENTS: {
8376 IterateExternalArrayElements<ExternalShortArray, int16_t>(
8377 receiver, true, true, visitor);
8378 break;
8379 }
8380 case JSObject::EXTERNAL_UNSIGNED_SHORT_ELEMENTS: {
8381 IterateExternalArrayElements<ExternalUnsignedShortArray, uint16_t>(
8382 receiver, true, true, visitor);
8383 break;
8384 }
8385 case JSObject::EXTERNAL_INT_ELEMENTS: {
8386 IterateExternalArrayElements<ExternalIntArray, int32_t>(
8387 receiver, true, false, visitor);
8388 break;
8389 }
8390 case JSObject::EXTERNAL_UNSIGNED_INT_ELEMENTS: {
8391 IterateExternalArrayElements<ExternalUnsignedIntArray, uint32_t>(
8392 receiver, true, false, visitor);
8393 break;
8394 }
8395 case JSObject::EXTERNAL_FLOAT_ELEMENTS: {
8396 IterateExternalArrayElements<ExternalFloatArray, float>(
8397 receiver, false, false, visitor);
8398 break;
8399 }
8224 default: 8400 default:
8225 UNREACHABLE(); 8401 UNREACHABLE();
8226 break; 8402 break;
8227 } 8403 }
8228 8404 visitor->increase_index_offset(length);
8229 return num_of_elements;
8230 }
8231
8232
8233 /**
8234 * A helper function that visits elements of an Array object, and elements
8235 * on its prototypes.
8236 *
8237 * Elements on prototypes are visited first, and only elements whose indices
8238 * less than Array length are visited.
8239 *
8240 * If a ArrayConcatVisitor object is given, the visitor is called with
8241 * parameters, element's index + visitor_index_offset and the element.
8242 *
8243 * The returned number of elements is an upper bound on the actual number
8244 * of elements added. If the same element occurs in more than one object
8245 * in the array's prototype chain, it will be counted more than once, but
8246 * will only occur once in the result.
8247 */
8248 static uint32_t IterateArrayAndPrototypeElements(Handle<JSArray> array,
8249 ArrayConcatVisitor* visitor) {
8250 uint32_t range = static_cast<uint32_t>(array->length()->Number());
8251 Handle<Object> obj = array;
8252
8253 static const int kEstimatedPrototypes = 3;
8254 List< Handle<JSObject> > objects(kEstimatedPrototypes);
8255
8256 // Visit prototype first. If an element on the prototype is shadowed by
8257 // the inheritor using the same index, the ArrayConcatVisitor visits
8258 // the prototype element before the shadowing element.
8259 // The visitor can simply overwrite the old value by new value using
8260 // the same index. This follows Array::concat semantics.
8261 while (!obj->IsNull()) {
8262 objects.Add(Handle<JSObject>::cast(obj));
8263 obj = Handle<Object>(obj->GetPrototype());
8264 }
8265
8266 uint32_t nof_elements = 0;
8267 for (int i = objects.length() - 1; i >= 0; i--) {
8268 Handle<JSObject> obj = objects[i];
8269 uint32_t encountered_elements =
8270 IterateElements(Handle<JSObject>::cast(obj), range, visitor);
8271
8272 if (encountered_elements > JSObject::kMaxElementCount - nof_elements) {
8273 nof_elements = JSObject::kMaxElementCount;
8274 } else {
8275 nof_elements += encountered_elements;
8276 }
8277 }
8278
8279 return nof_elements;
8280 }
8281
8282
8283 /**
8284 * A helper function of Runtime_ArrayConcat.
8285 *
8286 * The first argument is an Array of arrays and objects. It is the
8287 * same as the arguments array of Array::concat JS function.
8288 *
8289 * If an argument is an Array object, the function visits array
8290 * elements. If an argument is not an Array object, the function
8291 * visits the object as if it is an one-element array.
8292 *
8293 * If the result array index overflows 32-bit unsigned integer, the rounded
8294 * non-negative number is used as new length. For example, if one
8295 * array length is 2^32 - 1, second array length is 1, the
8296 * concatenated array length is 0.
8297 * TODO(lrn) Change length behavior to ECMAScript 5 specification (length
8298 * is one more than the last array index to get a value assigned).
8299 */
8300 static uint32_t IterateArguments(Handle<JSArray> arguments,
8301 ArrayConcatVisitor* visitor) {
8302 uint32_t visited_elements = 0;
8303 uint32_t num_of_args = static_cast<uint32_t>(arguments->length()->Number());
8304
8305 for (uint32_t i = 0; i < num_of_args; i++) {
8306 Object *element;
8307 MaybeObject* maybe_element = arguments->GetElement(i);
8308 // This if() is not expected to fail, but we have the check in the
8309 // interest of hardening the runtime calls.
8310 if (maybe_element->ToObject(&element)) {
8311 Handle<Object> obj(element);
8312 if (obj->IsJSArray()) {
8313 Handle<JSArray> array = Handle<JSArray>::cast(obj);
8314 uint32_t len = static_cast<uint32_t>(array->length()->Number());
8315 uint32_t nof_elements =
8316 IterateArrayAndPrototypeElements(array, visitor);
8317 // Total elements of array and its prototype chain can be more than
8318 // the array length, but ArrayConcat can only concatenate at most
8319 // the array length number of elements. We use the length as an estimate
8320 // for the actual number of elements added.
8321 uint32_t added_elements = (nof_elements > len) ? len : nof_elements;
8322 if (JSArray::kMaxElementCount - visited_elements < added_elements) {
8323 visited_elements = JSArray::kMaxElementCount;
8324 } else {
8325 visited_elements += added_elements;
8326 }
8327 if (visitor) visitor->increase_index_offset(len);
8328 } else {
8329 if (visitor) {
8330 visitor->visit(0, obj);
8331 visitor->increase_index_offset(1);
8332 }
8333 if (visited_elements < JSArray::kMaxElementCount) {
8334 visited_elements++;
8335 }
8336 }
8337 }
8338 }
8339 return visited_elements;
8340 } 8405 }
8341 8406
8342 8407
8343 /** 8408 /**
8344 * Array::concat implementation. 8409 * Array::concat implementation.
8345 * See ECMAScript 262, 15.4.4.4. 8410 * See ECMAScript 262, 15.4.4.4.
8346 * TODO(lrn): Fix non-compliance for very large concatenations and update to 8411 * TODO(581): Fix non-compliance for very large concatenations and update to
8347 * following the ECMAScript 5 specification. 8412 * following the ECMAScript 5 specification.
8348 */ 8413 */
8349 static MaybeObject* Runtime_ArrayConcat(Arguments args) { 8414 static MaybeObject* Runtime_ArrayConcat(Arguments args) {
8350 ASSERT(args.length() == 1); 8415 ASSERT(args.length() == 1);
8351 HandleScope handle_scope; 8416 HandleScope handle_scope;
8352 8417
8353 CONVERT_CHECKED(JSArray, arg_arrays, args[0]); 8418 CONVERT_ARG_CHECKED(JSArray, arguments, 0);
8354 Handle<JSArray> arguments(arg_arrays); 8419 int argument_count = static_cast<int>(arguments->length()->Number());
8355 8420 RUNTIME_ASSERT(arguments->HasFastElements());
8356 // Pass 1: estimate the number of elements of the result 8421 Handle<FixedArray> elements(FixedArray::cast(arguments->elements()));
8357 // (it could be more than real numbers if prototype has elements). 8422
8358 uint32_t result_length = 0; 8423 // Pass 1: estimate the length and number of elements of the result.
8359 uint32_t num_of_args = static_cast<uint32_t>(arguments->length()->Number()); 8424 // The actual length can be larger if any of the arguments have getters
8360 8425 // that mutate other arguments (but will otherwise be precise).
8361 { AssertNoAllocation nogc; 8426 // The number of elements is precise if there are no inherited elements.
8362 for (uint32_t i = 0; i < num_of_args; i++) { 8427
8363 Object* obj; 8428 uint32_t estimate_result_length = 0;
8364 MaybeObject* maybe_object = arguments->GetElement(i); 8429 uint32_t estimate_nof_elements = 0;
8365 // This if() is not expected to fail, but we have the check in the 8430 {
8366 // interest of hardening the runtime calls. 8431 for (int i = 0; i < argument_count; i++) {
8367 if (maybe_object->ToObject(&obj)) { 8432 HandleScope loop_scope;
8368 uint32_t length_estimate; 8433 Handle<Object> obj(elements->get(i));
8369 if (obj->IsJSArray()) { 8434 uint32_t length_estimate;
8370 length_estimate = 8435 uint32_t element_estimate;
8371 static_cast<uint32_t>(JSArray::cast(obj)->length()->Number()); 8436 if (obj->IsJSArray()) {
8372 } else { 8437 Handle<JSArray> array(Handle<JSArray>::cast(obj));
8373 length_estimate = 1; 8438 length_estimate =
8374 } 8439 static_cast<uint32_t>(array->length()->Number());
8375 if (JSObject::kMaxElementCount - result_length < length_estimate) { 8440 element_estimate =
8376 result_length = JSObject::kMaxElementCount; 8441 EstimateElementCount(array);
8377 break; 8442 } else {
8378 } 8443 length_estimate = 1;
8379 result_length += length_estimate; 8444 element_estimate = 1;
8380 } 8445 }
8381 } 8446 // Avoid overflows by capping at kMaxElementCount.
8382 } 8447 if (JSObject::kMaxElementCount - estimate_result_length <
8383 8448 length_estimate) {
8384 // Allocate an empty array, will set map, length, and content later. 8449 estimate_result_length = JSObject::kMaxElementCount;
8385 Handle<JSArray> result = Factory::NewJSArray(0); 8450 } else {
8386 8451 estimate_result_length += length_estimate;
8387 uint32_t estimate_nof_elements = IterateArguments(arguments, NULL); 8452 }
8453 if (JSObject::kMaxElementCount - estimate_nof_elements <
8454 element_estimate) {
8455 estimate_nof_elements = JSObject::kMaxElementCount;
8456 } else {
8457 estimate_nof_elements += element_estimate;
8458 }
8459 }
8460 }
8461
8388 // If estimated number of elements is more than half of length, a 8462 // If estimated number of elements is more than half of length, a
8389 // fixed array (fast case) is more time and space-efficient than a 8463 // fixed array (fast case) is more time and space-efficient than a
8390 // dictionary. 8464 // dictionary.
8391 bool fast_case = (estimate_nof_elements * 2) >= result_length; 8465 bool fast_case = (estimate_nof_elements * 2) >= estimate_result_length;
8392 8466
8393 Handle<Map> map;
8394 Handle<FixedArray> storage; 8467 Handle<FixedArray> storage;
8395 if (fast_case) { 8468 if (fast_case) {
8396 // The backing storage array must have non-existing elements to 8469 // The backing storage array must have non-existing elements to
8397 // preserve holes across concat operations. 8470 // preserve holes across concat operations.
8398 map = Factory::GetFastElementsMap(Handle<Map>(result->map())); 8471 storage = Factory::NewFixedArrayWithHoles(estimate_result_length);
8399 storage = Factory::NewFixedArrayWithHoles(result_length);
8400 } else { 8472 } else {
8401 map = Factory::GetSlowElementsMap(Handle<Map>(result->map()));
8402 // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate 8473 // TODO(126): move 25% pre-allocation logic into Dictionary::Allocate
8403 uint32_t at_least_space_for = estimate_nof_elements + 8474 uint32_t at_least_space_for = estimate_nof_elements +
8404 (estimate_nof_elements >> 2); 8475 (estimate_nof_elements >> 2);
8405 storage = Handle<FixedArray>::cast( 8476 storage = Handle<FixedArray>::cast(
8406 Factory::NewNumberDictionary(at_least_space_for)); 8477 Factory::NewNumberDictionary(at_least_space_for));
8407 } 8478 }
8408 8479
8409 Handle<Object> len = Factory::NewNumber(static_cast<double>(result_length)); 8480 ArrayConcatVisitor visitor(storage, fast_case);
8410 8481
8411 ArrayConcatVisitor visitor(storage, result_length, fast_case); 8482 for (int i = 0; i < argument_count; i++) {
8483 Handle<Object> obj(elements->get(i));
8484 if (obj->IsJSArray()) {
8485 Handle<JSArray> array = Handle<JSArray>::cast(obj);
8486 IterateElements(array, &visitor);
8487 } else {
8488 visitor.visit(0, obj);
8489 visitor.increase_index_offset(1);
8490 }
8491 }
8412 8492
8413 IterateArguments(arguments, &visitor); 8493 return *visitor.ToArray();
8414
8415 // Please note:
8416 // - the storage might have been changed in the visitor;
8417 // - the map and the storage must be set together to avoid breaking
8418 // the invariant that the map describes the array's elements.
8419 result->set_map(*map);
8420 result->set_length(*len);
8421 result->set_elements(*visitor.storage());
8422
8423 return *result;
8424 } 8494 }
8425 8495
8426 8496
8427 // This will not allocate (flatten the string), but it may run 8497 // This will not allocate (flatten the string), but it may run
8428 // very slowly for very deeply nested ConsStrings. For debugging use only. 8498 // very slowly for very deeply nested ConsStrings. For debugging use only.
8429 static MaybeObject* Runtime_GlobalPrint(Arguments args) { 8499 static MaybeObject* Runtime_GlobalPrint(Arguments args) {
8430 NoHandleAllocation ha; 8500 NoHandleAllocation ha;
8431 ASSERT(args.length() == 1); 8501 ASSERT(args.length() == 1);
8432 8502
8433 CONVERT_CHECKED(String, string, args[0]); 8503 CONVERT_CHECKED(String, string, args[0]);
(...skipping 2787 matching lines...) Expand 10 before | Expand all | Expand 10 after
11221 } else { 11291 } else {
11222 // Handle last resort GC and make sure to allow future allocations 11292 // Handle last resort GC and make sure to allow future allocations
11223 // to grow the heap without causing GCs (if possible). 11293 // to grow the heap without causing GCs (if possible).
11224 Counters::gc_last_resort_from_js.Increment(); 11294 Counters::gc_last_resort_from_js.Increment();
11225 Heap::CollectAllGarbage(false); 11295 Heap::CollectAllGarbage(false);
11226 } 11296 }
11227 } 11297 }
11228 11298
11229 11299
11230 } } // namespace v8::internal 11300 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects-inl.h ('k') | test/mjsunit/array-concat.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698