OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/factory.h" | 5 #include "src/factory.h" |
6 | 6 |
7 #include "src/allocation-site-scopes.h" | 7 #include "src/allocation-site-scopes.h" |
8 #include "src/base/bits.h" | 8 #include "src/base/bits.h" |
9 #include "src/bootstrapper.h" | 9 #include "src/bootstrapper.h" |
10 #include "src/conversions.h" | 10 #include "src/conversions.h" |
(...skipping 1807 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1818 #undef TYPED_ARRAY_CASE | 1818 #undef TYPED_ARRAY_CASE |
1819 } | 1819 } |
1820 | 1820 |
1821 | 1821 |
1822 size_t GetExternalArrayElementSize(ExternalArrayType type) { | 1822 size_t GetExternalArrayElementSize(ExternalArrayType type) { |
1823 switch (type) { | 1823 switch (type) { |
1824 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ | 1824 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ |
1825 case kExternal##Type##Array: \ | 1825 case kExternal##Type##Array: \ |
1826 return size; | 1826 return size; |
1827 TYPED_ARRAYS(TYPED_ARRAY_CASE) | 1827 TYPED_ARRAYS(TYPED_ARRAY_CASE) |
1828 default: | |
1829 UNREACHABLE(); | |
1830 return 0; | |
1831 } | 1828 } |
| 1829 UNREACHABLE(); |
| 1830 return 0; |
1832 #undef TYPED_ARRAY_CASE | 1831 #undef TYPED_ARRAY_CASE |
1833 } | 1832 } |
1834 | 1833 |
1835 | |
1836 size_t GetFixedTypedArraysElementSize(ElementsKind kind) { | |
1837 switch (kind) { | |
1838 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ | |
1839 case TYPE##_ELEMENTS: \ | |
1840 return size; | |
1841 TYPED_ARRAYS(TYPED_ARRAY_CASE) | |
1842 default: | |
1843 UNREACHABLE(); | |
1844 return 0; | |
1845 } | |
1846 #undef TYPED_ARRAY_CASE | |
1847 } | |
1848 | |
1849 | |
1850 ExternalArrayType GetArrayTypeFromElementsKind(ElementsKind kind) { | |
1851 switch (kind) { | |
1852 #define TYPED_ARRAY_CASE(Type, type, TYPE, ctype, size) \ | |
1853 case TYPE##_ELEMENTS: \ | |
1854 return kExternal##Type##Array; | |
1855 TYPED_ARRAYS(TYPED_ARRAY_CASE) | |
1856 default: | |
1857 UNREACHABLE(); | |
1858 return kExternalInt8Array; | |
1859 } | |
1860 #undef TYPED_ARRAY_CASE | |
1861 } | |
1862 | |
1863 | 1834 |
1864 JSFunction* GetTypedArrayFun(ExternalArrayType type, Isolate* isolate) { | 1835 JSFunction* GetTypedArrayFun(ExternalArrayType type, Isolate* isolate) { |
1865 Context* native_context = isolate->context()->native_context(); | 1836 Context* native_context = isolate->context()->native_context(); |
1866 switch (type) { | 1837 switch (type) { |
1867 #define TYPED_ARRAY_FUN(Type, type, TYPE, ctype, size) \ | 1838 #define TYPED_ARRAY_FUN(Type, type, TYPE, ctype, size) \ |
1868 case kExternal##Type##Array: \ | 1839 case kExternal##Type##Array: \ |
1869 return native_context->type##_array_fun(); | 1840 return native_context->type##_array_fun(); |
1870 | 1841 |
1871 TYPED_ARRAYS(TYPED_ARRAY_FUN) | 1842 TYPED_ARRAYS(TYPED_ARRAY_FUN) |
1872 #undef TYPED_ARRAY_FUN | 1843 #undef TYPED_ARRAY_FUN |
1873 | 1844 |
1874 default: | 1845 default: |
1875 UNREACHABLE(); | 1846 UNREACHABLE(); |
1876 return NULL; | 1847 return NULL; |
1877 } | 1848 } |
1878 } | 1849 } |
1879 | 1850 |
1880 | 1851 |
1881 JSFunction* GetTypedArrayFun(ElementsKind elements_kind, Isolate* isolate) { | |
1882 Context* native_context = isolate->context()->native_context(); | |
1883 switch (elements_kind) { | |
1884 #define TYPED_ARRAY_FUN(Type, type, TYPE, ctype, size) \ | |
1885 case TYPE##_ELEMENTS: \ | |
1886 return native_context->type##_array_fun(); | |
1887 | |
1888 TYPED_ARRAYS(TYPED_ARRAY_FUN) | |
1889 #undef TYPED_ARRAY_FUN | |
1890 | |
1891 default: | |
1892 UNREACHABLE(); | |
1893 return NULL; | |
1894 } | |
1895 } | |
1896 | |
1897 | |
1898 void SetupArrayBufferView(i::Isolate* isolate, | 1852 void SetupArrayBufferView(i::Isolate* isolate, |
1899 i::Handle<i::JSArrayBufferView> obj, | 1853 i::Handle<i::JSArrayBufferView> obj, |
1900 i::Handle<i::JSArrayBuffer> buffer, | 1854 i::Handle<i::JSArrayBuffer> buffer, |
1901 size_t byte_offset, size_t byte_length) { | 1855 size_t byte_offset, size_t byte_length) { |
1902 DCHECK(byte_offset + byte_length <= | 1856 DCHECK(byte_offset + byte_length <= |
1903 static_cast<size_t>(buffer->byte_length()->Number())); | 1857 static_cast<size_t>(buffer->byte_length()->Number())); |
1904 | 1858 |
1905 obj->set_buffer(*buffer); | 1859 obj->set_buffer(*buffer); |
1906 | 1860 |
| 1861 Heap* heap = isolate->heap(); |
| 1862 if (heap->InNewSpace(*obj)) { |
| 1863 obj->set_weak_next(heap->new_array_buffer_views_list()); |
| 1864 heap->set_new_array_buffer_views_list(*obj); |
| 1865 } else { |
| 1866 obj->set_weak_next(buffer->weak_first_view()); |
| 1867 buffer->set_weak_first_view(*obj); |
| 1868 } |
| 1869 |
1907 i::Handle<i::Object> byte_offset_object = | 1870 i::Handle<i::Object> byte_offset_object = |
1908 isolate->factory()->NewNumberFromSize(byte_offset); | 1871 isolate->factory()->NewNumberFromSize(byte_offset); |
1909 obj->set_byte_offset(*byte_offset_object); | 1872 obj->set_byte_offset(*byte_offset_object); |
1910 | 1873 |
1911 i::Handle<i::Object> byte_length_object = | 1874 i::Handle<i::Object> byte_length_object = |
1912 isolate->factory()->NewNumberFromSize(byte_length); | 1875 isolate->factory()->NewNumberFromSize(byte_length); |
1913 obj->set_byte_length(*byte_length_object); | 1876 obj->set_byte_length(*byte_length_object); |
1914 } | 1877 } |
1915 | 1878 |
1916 | 1879 |
1917 } // namespace | 1880 } // namespace |
1918 | 1881 |
1919 | 1882 |
1920 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) { | 1883 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) { |
1921 Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate())); | 1884 Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate())); |
1922 | 1885 |
1923 CALL_HEAP_FUNCTION( | 1886 CALL_HEAP_FUNCTION( |
1924 isolate(), | 1887 isolate(), |
1925 isolate()->heap()->AllocateJSObject(*typed_array_fun_handle), | 1888 isolate()->heap()->AllocateJSObject(*typed_array_fun_handle), |
1926 JSTypedArray); | 1889 JSTypedArray); |
1927 } | 1890 } |
1928 | 1891 |
1929 | |
1930 Handle<JSTypedArray> Factory::NewJSTypedArray(ElementsKind elements_kind) { | |
1931 Handle<JSFunction> typed_array_fun_handle( | |
1932 GetTypedArrayFun(elements_kind, isolate())); | |
1933 | |
1934 CALL_HEAP_FUNCTION( | |
1935 isolate(), isolate()->heap()->AllocateJSObject(*typed_array_fun_handle), | |
1936 JSTypedArray); | |
1937 } | |
1938 | |
1939 | 1892 |
1940 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type, | 1893 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type, |
1941 Handle<JSArrayBuffer> buffer, | 1894 Handle<JSArrayBuffer> buffer, |
1942 size_t byte_offset, | 1895 size_t byte_offset, |
1943 size_t length) { | 1896 size_t length) { |
1944 Handle<JSTypedArray> obj = NewJSTypedArray(type); | 1897 Handle<JSTypedArray> obj = NewJSTypedArray(type); |
1945 | 1898 |
1946 size_t element_size = GetExternalArrayElementSize(type); | 1899 size_t element_size = GetExternalArrayElementSize(type); |
1947 ElementsKind elements_kind = GetExternalArrayElementsKind(type); | 1900 ElementsKind elements_kind = GetExternalArrayElementsKind(type); |
1948 | 1901 |
1949 CHECK(byte_offset % element_size == 0); | 1902 CHECK(byte_offset % element_size == 0); |
1950 | 1903 |
1951 CHECK(length <= (std::numeric_limits<size_t>::max() / element_size)); | 1904 CHECK(length <= (std::numeric_limits<size_t>::max() / element_size)); |
1952 CHECK(length <= static_cast<size_t>(Smi::kMaxValue)); | 1905 CHECK(length <= static_cast<size_t>(Smi::kMaxValue)); |
1953 size_t byte_length = length * element_size; | 1906 size_t byte_length = length * element_size; |
1954 SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length); | 1907 SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length); |
1955 | 1908 |
1956 Handle<Object> length_object = NewNumberFromSize(length); | 1909 Handle<Object> length_object = NewNumberFromSize(length); |
1957 obj->set_length(*length_object); | 1910 obj->set_length(*length_object); |
1958 | 1911 |
1959 Handle<ExternalArray> elements = NewExternalArray( | 1912 Handle<ExternalArray> elements = NewExternalArray( |
1960 static_cast<int>(length), type, | 1913 static_cast<int>(length), type, |
1961 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); | 1914 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
1962 Handle<Map> map = JSObject::GetElementsTransitionMap(obj, elements_kind); | 1915 Handle<Map> map = JSObject::GetElementsTransitionMap(obj, elements_kind); |
1963 JSObject::SetMapAndElements(obj, map, elements); | 1916 JSObject::SetMapAndElements(obj, map, elements); |
1964 return obj; | 1917 return obj; |
1965 } | 1918 } |
1966 | 1919 |
1967 | |
1968 Handle<JSTypedArray> Factory::NewJSTypedArray(ElementsKind elements_kind, | |
1969 size_t number_of_elements) { | |
1970 Handle<JSTypedArray> obj = NewJSTypedArray(elements_kind); | |
1971 | |
1972 size_t element_size = GetFixedTypedArraysElementSize(elements_kind); | |
1973 ExternalArrayType array_type = GetArrayTypeFromElementsKind(elements_kind); | |
1974 | |
1975 CHECK(number_of_elements <= | |
1976 (std::numeric_limits<size_t>::max() / element_size)); | |
1977 CHECK(number_of_elements <= static_cast<size_t>(Smi::kMaxValue)); | |
1978 size_t byte_length = number_of_elements * element_size; | |
1979 | |
1980 obj->set_byte_offset(Smi::FromInt(0)); | |
1981 i::Handle<i::Object> byte_length_object = | |
1982 isolate()->factory()->NewNumberFromSize(byte_length); | |
1983 obj->set_byte_length(*byte_length_object); | |
1984 Handle<Object> length_object = NewNumberFromSize(number_of_elements); | |
1985 obj->set_length(*length_object); | |
1986 | |
1987 obj->set_buffer(Smi::FromInt(0)); | |
1988 Handle<FixedTypedArrayBase> elements = | |
1989 isolate()->factory()->NewFixedTypedArray( | |
1990 static_cast<int>(number_of_elements), array_type); | |
1991 obj->set_elements(*elements); | |
1992 return obj; | |
1993 } | |
1994 | |
1995 | 1920 |
1996 Handle<JSDataView> Factory::NewJSDataView(Handle<JSArrayBuffer> buffer, | 1921 Handle<JSDataView> Factory::NewJSDataView(Handle<JSArrayBuffer> buffer, |
1997 size_t byte_offset, | 1922 size_t byte_offset, |
1998 size_t byte_length) { | 1923 size_t byte_length) { |
1999 Handle<JSDataView> obj = NewJSDataView(); | 1924 Handle<JSDataView> obj = NewJSDataView(); |
2000 SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length); | 1925 SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length); |
2001 return obj; | 1926 return obj; |
2002 } | 1927 } |
2003 | 1928 |
2004 | 1929 |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2472 return Handle<Object>::null(); | 2397 return Handle<Object>::null(); |
2473 } | 2398 } |
2474 | 2399 |
2475 | 2400 |
2476 Handle<Object> Factory::ToBoolean(bool value) { | 2401 Handle<Object> Factory::ToBoolean(bool value) { |
2477 return value ? true_value() : false_value(); | 2402 return value ? true_value() : false_value(); |
2478 } | 2403 } |
2479 | 2404 |
2480 | 2405 |
2481 } } // namespace v8::internal | 2406 } } // namespace v8::internal |
OLD | NEW |