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