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 |
| (...skipping 1892 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1903 MaybeObject* maybe_obj = array->GetHeap()->AllocateFixedArray(1); | 1903 MaybeObject* maybe_obj = array->GetHeap()->AllocateFixedArray(1); |
| 1904 if (!maybe_obj->To(&new_backing_store)) return maybe_obj; | 1904 if (!maybe_obj->To(&new_backing_store)) return maybe_obj; |
| 1905 new_backing_store->set(0, length); | 1905 new_backing_store->set(0, length); |
| 1906 { MaybeObject* result = array->SetContent(new_backing_store); | 1906 { MaybeObject* result = array->SetContent(new_backing_store); |
| 1907 if (result->IsFailure()) return result; | 1907 if (result->IsFailure()) return result; |
| 1908 } | 1908 } |
| 1909 return array; | 1909 return array; |
| 1910 } | 1910 } |
| 1911 | 1911 |
| 1912 | 1912 |
| 1913 // TODO(ishell): Temporary wrapper until handlified. | |
| 1914 Handle<Object> ArrayConstructInitializeElements(Handle<JSArray> array, | 1913 Handle<Object> ArrayConstructInitializeElements(Handle<JSArray> array, |
| 1915 Arguments* args) { | 1914 Arguments* args) { |
| 1916 CALL_HEAP_FUNCTION(array->GetIsolate(), | 1915 Isolate* isolate = array->GetIsolate(); |
| 1917 ArrayConstructInitializeElements(*array, args), | 1916 Factory* factory = isolate->factory(); |
|
Yang
2014/03/18 09:52:17
Small nit here. I don't think we need to fetch the
Igor Sheludko
2014/03/18 11:25:10
Done.
| |
| 1918 Object); | |
| 1919 } | |
| 1920 | |
| 1921 | |
| 1922 MUST_USE_RESULT MaybeObject* ArrayConstructInitializeElements( | |
| 1923 JSArray* array, Arguments* args) { | |
| 1924 Heap* heap = array->GetIsolate()->heap(); | |
| 1925 | 1917 |
| 1926 // Optimize the case where there is one argument and the argument is a | 1918 // Optimize the case where there is one argument and the argument is a |
| 1927 // small smi. | 1919 // small smi. |
| 1928 if (args->length() == 1) { | 1920 if (args->length() == 1) { |
| 1929 Object* obj = (*args)[0]; | 1921 Handle<Object> obj = args->at<Object>(0); |
| 1930 if (obj->IsSmi()) { | 1922 if (obj->IsSmi()) { |
| 1931 int len = Smi::cast(obj)->value(); | 1923 int len = Handle<Smi>::cast(obj)->value(); |
| 1932 if (len > 0 && len < JSObject::kInitialMaxFastElementArray) { | 1924 if (len > 0 && len < JSObject::kInitialMaxFastElementArray) { |
| 1933 ElementsKind elements_kind = array->GetElementsKind(); | 1925 ElementsKind elements_kind = array->GetElementsKind(); |
| 1934 MaybeObject* maybe_array = array->Initialize(len, len); | 1926 JSArray::Initialize(array, len, len); |
| 1935 if (maybe_array->IsFailure()) return maybe_array; | |
| 1936 | 1927 |
| 1937 if (!IsFastHoleyElementsKind(elements_kind)) { | 1928 if (!IsFastHoleyElementsKind(elements_kind)) { |
| 1938 elements_kind = GetHoleyElementsKind(elements_kind); | 1929 elements_kind = GetHoleyElementsKind(elements_kind); |
| 1939 maybe_array = array->TransitionElementsKind(elements_kind); | 1930 JSObject::TransitionElementsKind(array, elements_kind); |
| 1940 if (maybe_array->IsFailure()) return maybe_array; | |
| 1941 } | 1931 } |
| 1942 | |
| 1943 return array; | 1932 return array; |
| 1944 } else if (len == 0) { | 1933 } else if (len == 0) { |
| 1945 return array->Initialize(JSArray::kPreallocatedArrayElements); | 1934 JSArray::Initialize(array, JSArray::kPreallocatedArrayElements); |
| 1935 return array; | |
| 1946 } | 1936 } |
| 1947 } | 1937 } |
| 1948 | 1938 |
| 1949 // Take the argument as the length. | 1939 // Take the argument as the length. |
| 1950 MaybeObject* maybe_obj = array->Initialize(0); | 1940 JSArray::Initialize(array, 0); |
| 1951 if (!maybe_obj->To(&obj)) return maybe_obj; | |
| 1952 | 1941 |
| 1953 return array->SetElementsLength((*args)[0]); | 1942 return JSArray::SetElementsLength(array, obj); |
| 1954 } | 1943 } |
| 1955 | 1944 |
| 1956 // Optimize the case where there are no parameters passed. | 1945 // Optimize the case where there are no parameters passed. |
| 1957 if (args->length() == 0) { | 1946 if (args->length() == 0) { |
| 1958 return array->Initialize(JSArray::kPreallocatedArrayElements); | 1947 JSArray::Initialize(array, JSArray::kPreallocatedArrayElements); |
| 1948 return array; | |
| 1959 } | 1949 } |
| 1960 | 1950 |
| 1961 // Set length and elements on the array. | 1951 // Set length and elements on the array. |
| 1962 int number_of_elements = args->length(); | 1952 int number_of_elements = args->length(); |
| 1963 MaybeObject* maybe_object = | 1953 JSObject::EnsureCanContainElements( |
| 1964 array->EnsureCanContainElements(args, 0, number_of_elements, | 1954 array, args, 0, number_of_elements, ALLOW_CONVERTED_DOUBLE_ELEMENTS); |
| 1965 ALLOW_CONVERTED_DOUBLE_ELEMENTS); | |
| 1966 if (maybe_object->IsFailure()) return maybe_object; | |
| 1967 | 1955 |
| 1968 // Allocate an appropriately typed elements array. | 1956 // Allocate an appropriately typed elements array. |
| 1969 MaybeObject* maybe_elms; | |
| 1970 ElementsKind elements_kind = array->GetElementsKind(); | 1957 ElementsKind elements_kind = array->GetElementsKind(); |
| 1958 Handle<FixedArrayBase> elms; | |
| 1971 if (IsFastDoubleElementsKind(elements_kind)) { | 1959 if (IsFastDoubleElementsKind(elements_kind)) { |
| 1972 maybe_elms = heap->AllocateUninitializedFixedDoubleArray( | 1960 elms = Handle<FixedArrayBase>::cast( |
| 1973 number_of_elements); | 1961 factory->NewFixedDoubleArray(number_of_elements)); |
| 1974 } else { | 1962 } else { |
| 1975 maybe_elms = heap->AllocateFixedArrayWithHoles(number_of_elements); | 1963 elms = Handle<FixedArrayBase>::cast( |
| 1964 factory->NewFixedArrayWithHoles(number_of_elements)); | |
| 1976 } | 1965 } |
| 1977 FixedArrayBase* elms; | |
| 1978 if (!maybe_elms->To(&elms)) return maybe_elms; | |
| 1979 | 1966 |
| 1980 // Fill in the content | 1967 // Fill in the content |
| 1981 switch (array->GetElementsKind()) { | 1968 switch (array->GetElementsKind()) { |
| 1982 case FAST_HOLEY_SMI_ELEMENTS: | 1969 case FAST_HOLEY_SMI_ELEMENTS: |
| 1983 case FAST_SMI_ELEMENTS: { | 1970 case FAST_SMI_ELEMENTS: { |
| 1984 FixedArray* smi_elms = FixedArray::cast(elms); | 1971 Handle<FixedArray> smi_elms = Handle<FixedArray>::cast(elms); |
| 1985 for (int index = 0; index < number_of_elements; index++) { | 1972 for (int index = 0; index < number_of_elements; index++) { |
| 1986 smi_elms->set(index, (*args)[index], SKIP_WRITE_BARRIER); | 1973 smi_elms->set(index, (*args)[index], SKIP_WRITE_BARRIER); |
| 1987 } | 1974 } |
| 1988 break; | 1975 break; |
| 1989 } | 1976 } |
| 1990 case FAST_HOLEY_ELEMENTS: | 1977 case FAST_HOLEY_ELEMENTS: |
| 1991 case FAST_ELEMENTS: { | 1978 case FAST_ELEMENTS: { |
| 1992 DisallowHeapAllocation no_gc; | 1979 DisallowHeapAllocation no_gc; |
| 1993 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); | 1980 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); |
| 1994 FixedArray* object_elms = FixedArray::cast(elms); | 1981 Handle<FixedArray> object_elms = Handle<FixedArray>::cast(elms); |
| 1995 for (int index = 0; index < number_of_elements; index++) { | 1982 for (int index = 0; index < number_of_elements; index++) { |
| 1996 object_elms->set(index, (*args)[index], mode); | 1983 object_elms->set(index, (*args)[index], mode); |
| 1997 } | 1984 } |
| 1998 break; | 1985 break; |
| 1999 } | 1986 } |
| 2000 case FAST_HOLEY_DOUBLE_ELEMENTS: | 1987 case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 2001 case FAST_DOUBLE_ELEMENTS: { | 1988 case FAST_DOUBLE_ELEMENTS: { |
| 2002 FixedDoubleArray* double_elms = FixedDoubleArray::cast(elms); | 1989 Handle<FixedDoubleArray> double_elms = |
| 1990 Handle<FixedDoubleArray>::cast(elms); | |
| 2003 for (int index = 0; index < number_of_elements; index++) { | 1991 for (int index = 0; index < number_of_elements; index++) { |
| 2004 double_elms->set(index, (*args)[index]->Number()); | 1992 double_elms->set(index, (*args)[index]->Number()); |
| 2005 } | 1993 } |
| 2006 break; | 1994 break; |
| 2007 } | 1995 } |
| 2008 default: | 1996 default: |
| 2009 UNREACHABLE(); | 1997 UNREACHABLE(); |
| 2010 break; | 1998 break; |
| 2011 } | 1999 } |
| 2012 | 2000 |
| 2013 array->set_elements(elms); | 2001 array->set_elements(*elms); |
| 2014 array->set_length(Smi::FromInt(number_of_elements)); | 2002 array->set_length(Smi::FromInt(number_of_elements)); |
| 2015 return array; | 2003 return array; |
| 2016 } | 2004 } |
| 2017 | 2005 |
| 2018 } } // namespace v8::internal | 2006 } } // namespace v8::internal |
| OLD | NEW |