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/conversions.h" | 9 #include "src/conversions.h" |
10 #include "src/isolate-inl.h" | 10 #include "src/isolate-inl.h" |
(...skipping 1748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1759 | 1759 |
1760 TYPED_ARRAYS(TYPED_ARRAY_FUN) | 1760 TYPED_ARRAYS(TYPED_ARRAY_FUN) |
1761 #undef TYPED_ARRAY_FUN | 1761 #undef TYPED_ARRAY_FUN |
1762 | 1762 |
1763 default: | 1763 default: |
1764 UNREACHABLE(); | 1764 UNREACHABLE(); |
1765 return NULL; | 1765 return NULL; |
1766 } | 1766 } |
1767 } | 1767 } |
1768 | 1768 |
| 1769 |
| 1770 void SetupArrayBufferView(i::Isolate* isolate, |
| 1771 i::Handle<i::JSArrayBufferView> obj, |
| 1772 i::Handle<i::JSArrayBuffer> buffer, |
| 1773 size_t byte_offset, size_t byte_length) { |
| 1774 DCHECK(byte_offset + byte_length <= |
| 1775 static_cast<size_t>(buffer->byte_length()->Number())); |
| 1776 |
| 1777 obj->set_buffer(*buffer); |
| 1778 |
| 1779 obj->set_weak_next(buffer->weak_first_view()); |
| 1780 buffer->set_weak_first_view(*obj); |
| 1781 |
| 1782 i::Handle<i::Object> byte_offset_object = |
| 1783 isolate->factory()->NewNumberFromSize(byte_offset); |
| 1784 obj->set_byte_offset(*byte_offset_object); |
| 1785 |
| 1786 i::Handle<i::Object> byte_length_object = |
| 1787 isolate->factory()->NewNumberFromSize(byte_length); |
| 1788 obj->set_byte_length(*byte_length_object); |
| 1789 } |
| 1790 |
| 1791 |
1769 } // namespace | 1792 } // namespace |
1770 | 1793 |
1771 | 1794 |
1772 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) { | 1795 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type) { |
1773 Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate())); | 1796 Handle<JSFunction> typed_array_fun_handle(GetTypedArrayFun(type, isolate())); |
1774 | 1797 |
1775 CALL_HEAP_FUNCTION( | 1798 CALL_HEAP_FUNCTION( |
1776 isolate(), | 1799 isolate(), |
1777 isolate()->heap()->AllocateJSObject(*typed_array_fun_handle), | 1800 isolate()->heap()->AllocateJSObject(*typed_array_fun_handle), |
1778 JSTypedArray); | 1801 JSTypedArray); |
1779 } | 1802 } |
1780 | 1803 |
1781 | 1804 |
1782 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type, | 1805 Handle<JSTypedArray> Factory::NewJSTypedArray(ExternalArrayType type, |
1783 Handle<JSArrayBuffer> buffer, | 1806 Handle<JSArrayBuffer> buffer, |
| 1807 size_t byte_offset, |
1784 size_t length) { | 1808 size_t length) { |
1785 DCHECK(length <= static_cast<size_t>(kMaxInt)); | 1809 Handle<JSTypedArray> obj = NewJSTypedArray(type); |
1786 Handle<JSTypedArray> array = NewJSTypedArray(type); | 1810 |
1787 array->set_buffer(*buffer); | 1811 size_t element_size = GetExternalArrayElementSize(type); |
1788 array->set_weak_next(buffer->weak_first_view()); | 1812 ElementsKind elements_kind = GetExternalArrayElementsKind(type); |
1789 buffer->set_weak_first_view(*array); | 1813 |
1790 array->set_byte_offset(Smi::FromInt(0)); | 1814 CHECK(byte_offset % element_size == 0); |
1791 Handle<Object> byte_length_handle = | 1815 |
1792 NewNumberFromSize(length * GetExternalArrayElementSize(type)); | 1816 CHECK(length <= (std::numeric_limits<size_t>::max() / element_size)); |
1793 array->set_byte_length(*byte_length_handle); | 1817 CHECK(length <= static_cast<size_t>(Smi::kMaxValue)); |
1794 Handle<Object> length_handle = NewNumberFromSize(length); | 1818 size_t byte_length = length * element_size; |
1795 array->set_length(*length_handle); | 1819 SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length); |
1796 Handle<ExternalArray> elements = | 1820 |
1797 NewExternalArray(static_cast<int>(length), type, buffer->backing_store()); | 1821 Handle<Object> length_object = NewNumberFromSize(length); |
1798 JSObject::SetMapAndElements(array, | 1822 obj->set_length(*length_object); |
1799 JSObject::GetElementsTransitionMap( | 1823 |
1800 array, GetExternalArrayElementsKind(type)), | 1824 Handle<ExternalArray> elements = NewExternalArray( |
1801 elements); | 1825 static_cast<int>(length), type, |
1802 return array; | 1826 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
| 1827 Handle<Map> map = JSObject::GetElementsTransitionMap(obj, elements_kind); |
| 1828 JSObject::SetMapAndElements(obj, map, elements); |
| 1829 return obj; |
1803 } | 1830 } |
1804 | 1831 |
1805 | 1832 |
| 1833 Handle<JSDataView> Factory::NewJSDataView(Handle<JSArrayBuffer> buffer, |
| 1834 size_t byte_offset, |
| 1835 size_t byte_length) { |
| 1836 Handle<JSDataView> obj = NewJSDataView(); |
| 1837 SetupArrayBufferView(isolate(), obj, buffer, byte_offset, byte_length); |
| 1838 return obj; |
| 1839 } |
| 1840 |
| 1841 |
1806 Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler, | 1842 Handle<JSProxy> Factory::NewJSProxy(Handle<Object> handler, |
1807 Handle<Object> prototype) { | 1843 Handle<Object> prototype) { |
1808 // Allocate map. | 1844 // Allocate map. |
1809 // TODO(rossberg): Once we optimize proxies, think about a scheme to share | 1845 // TODO(rossberg): Once we optimize proxies, think about a scheme to share |
1810 // maps. Will probably depend on the identity of the handler object, too. | 1846 // maps. Will probably depend on the identity of the handler object, too. |
1811 Handle<Map> map = NewMap(JS_PROXY_TYPE, JSProxy::kSize); | 1847 Handle<Map> map = NewMap(JS_PROXY_TYPE, JSProxy::kSize); |
1812 map->set_prototype(*prototype); | 1848 map->set_prototype(*prototype); |
1813 | 1849 |
1814 // Allocate the proxy object. | 1850 // Allocate the proxy object. |
1815 Handle<JSProxy> result = New<JSProxy>(map, NEW_SPACE); | 1851 Handle<JSProxy> result = New<JSProxy>(map, NEW_SPACE); |
(...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2457 return Handle<Object>::null(); | 2493 return Handle<Object>::null(); |
2458 } | 2494 } |
2459 | 2495 |
2460 | 2496 |
2461 Handle<Object> Factory::ToBoolean(bool value) { | 2497 Handle<Object> Factory::ToBoolean(bool value) { |
2462 return value ? true_value() : false_value(); | 2498 return value ? true_value() : false_value(); |
2463 } | 2499 } |
2464 | 2500 |
2465 | 2501 |
2466 } } // namespace v8::internal | 2502 } } // namespace v8::internal |
OLD | NEW |