Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "arguments.h" | |
| 30 #include "objects.h" | 31 #include "objects.h" |
| 31 #include "elements.h" | 32 #include "elements.h" |
| 32 #include "utils.h" | 33 #include "utils.h" |
| 33 #include "v8conversions.h" | 34 #include "v8conversions.h" |
| 34 | 35 |
| 35 // Each concrete ElementsAccessor can handle exactly one ElementsKind, | 36 // Each concrete ElementsAccessor can handle exactly one ElementsKind, |
| 36 // several abstract ElementsAccessor classes are used to allow sharing | 37 // several abstract ElementsAccessor classes are used to allow sharing |
| 37 // common code. | 38 // common code. |
| 38 // | 39 // |
| 39 // Inheritance hierarchy: | 40 // Inheritance hierarchy: |
| (...skipping 1926 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1966 MaybeObject* maybe_obj = array->GetHeap()->AllocateFixedArray(1); | 1967 MaybeObject* maybe_obj = array->GetHeap()->AllocateFixedArray(1); |
| 1967 if (!maybe_obj->To(&new_backing_store)) return maybe_obj; | 1968 if (!maybe_obj->To(&new_backing_store)) return maybe_obj; |
| 1968 new_backing_store->set(0, length); | 1969 new_backing_store->set(0, length); |
| 1969 { MaybeObject* result = array->SetContent(new_backing_store); | 1970 { MaybeObject* result = array->SetContent(new_backing_store); |
| 1970 if (result->IsFailure()) return result; | 1971 if (result->IsFailure()) return result; |
| 1971 } | 1972 } |
| 1972 return array; | 1973 return array; |
| 1973 } | 1974 } |
| 1974 | 1975 |
| 1975 | 1976 |
| 1977 MUST_USE_RESULT MaybeObject* ArrayConstructInitializeElements( | |
| 1978 JSArray* array, Arguments* args) { | |
| 1979 Heap* heap = array->GetIsolate()->heap(); | |
| 1980 | |
| 1981 // Optimize the case where there is one argument and the argument is a | |
| 1982 // small smi. | |
| 1983 if (args->length() == 1) { | |
| 1984 Object* obj = (*args)[0]; | |
| 1985 if (obj->IsSmi()) { | |
| 1986 int len = Smi::cast(obj)->value(); | |
| 1987 if (len > 0 && len < JSObject::kInitialMaxFastElementArray) { | |
| 1988 FixedArrayBase* fixed_array; | |
| 1989 ElementsKind elements_kind = array->GetElementsKind(); | |
| 1990 { | |
|
Toon Verwaest
2013/02/13 15:14:51
Remove { ... }.
mvstanton
2013/02/19 11:04:08
Done.
| |
| 1991 MaybeObject* maybe_obj; | |
| 1992 if (IsFastDoubleElementsKind(elements_kind)) { | |
| 1993 maybe_obj = heap->AllocateFixedDoubleArrayWithHoles(len); | |
| 1994 } else { | |
| 1995 maybe_obj = heap->AllocateFixedArrayWithHoles(len); | |
| 1996 } | |
| 1997 if (!maybe_obj->To(&fixed_array)) return maybe_obj; | |
|
Toon Verwaest
2013/02/13 15:14:51
It seems like this method should be merged with Al
mvstanton
2013/02/19 11:04:08
I did this, going through helper JSArray::Initiali
| |
| 1998 } | |
| 1999 if (!IsFastHoleyElementsKind(elements_kind)) { | |
| 2000 elements_kind = GetHoleyElementsKind(elements_kind); | |
| 2001 MaybeObject* maybe_array = | |
| 2002 array->TransitionElementsKind(elements_kind); | |
| 2003 if (maybe_array->IsFailure()) return maybe_array; | |
| 2004 } | |
| 2005 // We do not use SetContent to skip the unnecessary elements type check. | |
| 2006 array->set_elements(fixed_array); | |
| 2007 array->set_length(Smi::cast(obj)); | |
| 2008 return array; | |
| 2009 } else if (len == 0) { | |
| 2010 return array->Initialize(JSArray::kPreallocatedArrayElements); | |
| 2011 } | |
| 2012 } | |
| 2013 // Take the argument as the length. | |
| 2014 { MaybeObject* maybe_obj = array->Initialize(0); | |
|
Toon Verwaest
2013/02/13 15:14:51
You don't need the { .. }; and To(& rather than To
mvstanton
2013/02/19 11:04:08
Done.
| |
| 2015 if (!maybe_obj->ToObject(&obj)) return maybe_obj; | |
| 2016 } | |
| 2017 return array->SetElementsLength((*args)[0]); | |
| 2018 } | |
| 2019 | |
| 2020 // Optimize the case where there are no parameters passed. | |
| 2021 if (args->length() == 0) { | |
| 2022 return array->Initialize(JSArray::kPreallocatedArrayElements); | |
| 2023 } | |
|
Toon Verwaest
2013/02/13 15:14:51
nit: I'd move this case above the == 1 case.
mvstanton
2013/02/19 11:04:08
Done.
| |
| 2024 | |
| 2025 // Set length and elements on the array. | |
| 2026 int number_of_elements = args->length(); | |
| 2027 MaybeObject* maybe_object = | |
| 2028 array->EnsureCanContainElements(args, 0, number_of_elements, | |
| 2029 ALLOW_CONVERTED_DOUBLE_ELEMENTS); | |
| 2030 if (maybe_object->IsFailure()) return maybe_object; | |
| 2031 | |
| 2032 | |
| 2033 // Allocate an appropriately typed elements array. | |
| 2034 MaybeObject* maybe_elms; | |
| 2035 ElementsKind elements_kind = array->GetElementsKind(); | |
| 2036 if (IsFastDoubleElementsKind(elements_kind)) { | |
| 2037 maybe_elms = heap->AllocateUninitializedFixedDoubleArray( | |
| 2038 number_of_elements); | |
| 2039 } else { | |
| 2040 maybe_elms = heap->AllocateFixedArrayWithHoles(number_of_elements); | |
| 2041 } | |
| 2042 FixedArrayBase* elms; | |
| 2043 if (!maybe_elms->To(&elms)) return maybe_elms; | |
| 2044 | |
| 2045 // Fill in the content | |
| 2046 switch (array->GetElementsKind()) { | |
| 2047 case FAST_HOLEY_SMI_ELEMENTS: | |
| 2048 case FAST_SMI_ELEMENTS: { | |
| 2049 FixedArray* smi_elms = FixedArray::cast(elms); | |
| 2050 for (int index = 0; index < number_of_elements; index++) { | |
| 2051 smi_elms->set(index, (*args)[index], SKIP_WRITE_BARRIER); | |
| 2052 } | |
| 2053 break; | |
| 2054 } | |
| 2055 case FAST_HOLEY_ELEMENTS: | |
| 2056 case FAST_ELEMENTS: { | |
| 2057 AssertNoAllocation no_gc; | |
| 2058 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); | |
| 2059 FixedArray* object_elms = FixedArray::cast(elms); | |
| 2060 for (int index = 0; index < number_of_elements; index++) { | |
| 2061 object_elms->set(index, (*args)[index], mode); | |
| 2062 } | |
| 2063 break; | |
| 2064 } | |
| 2065 case FAST_HOLEY_DOUBLE_ELEMENTS: | |
| 2066 case FAST_DOUBLE_ELEMENTS: { | |
| 2067 FixedDoubleArray* double_elms = FixedDoubleArray::cast(elms); | |
| 2068 for (int index = 0; index < number_of_elements; index++) { | |
| 2069 double_elms->set(index, (*args)[index]->Number()); | |
| 2070 } | |
| 2071 break; | |
| 2072 } | |
| 2073 default: | |
| 2074 UNREACHABLE(); | |
| 2075 break; | |
| 2076 } | |
| 2077 | |
| 2078 array->set_elements(elms); | |
| 2079 array->set_length(Smi::FromInt(number_of_elements)); | |
| 2080 return array; | |
| 2081 } | |
| 2082 | |
| 1976 } } // namespace v8::internal | 2083 } } // namespace v8::internal |
| OLD | NEW |