Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: src/api.cc

Issue 14195034: First cut at API for native Typed Arrays. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Self-review Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 2737 matching lines...) Expand 10 before | Expand all | Expand 10 after
2748 2748
2749 void v8::ArrayBuffer::CheckCast(Value* that) { 2749 void v8::ArrayBuffer::CheckCast(Value* that) {
2750 if (IsDeadCheck(i::Isolate::Current(), "v8::ArrayBuffer::Cast()")) return; 2750 if (IsDeadCheck(i::Isolate::Current(), "v8::ArrayBuffer::Cast()")) return;
2751 i::Handle<i::Object> obj = Utils::OpenHandle(that); 2751 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2752 ApiCheck(obj->IsJSArrayBuffer(), 2752 ApiCheck(obj->IsJSArrayBuffer(),
2753 "v8::ArrayBuffer::Cast()", 2753 "v8::ArrayBuffer::Cast()",
2754 "Could not convert to ArrayBuffer"); 2754 "Could not convert to ArrayBuffer");
2755 } 2755 }
2756 2756
2757 2757
2758 void v8::TypedArray::CheckCast(Value* that) {
2759 if (IsDeadCheck(i::Isolate::Current(), "v8::TypedArray::Cast()")) return;
2760 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2761 ApiCheck(obj->IsJSTypedArray(),
2762 "v8::TypedArray::Cast()",
2763 "Could not convert to TypedArray");
2764 }
2765
2766
2767 #define CHECK_TYPED_ARRAY_CAST(ApiClass, typeConst) \
2768 void v8::ApiClass::CheckCast(Value* that) { \
2769 if (IsDeadCheck(i::Isolate::Current(), "v8::" #ApiClass "::Cast()")) \
2770 return; \
2771 i::Handle<i::Object> obj = Utils::OpenHandle(that); \
2772 ApiCheck(obj->IsJSTypedArray() \
2773 && i::JSTypedArray::cast(*obj)->type() == typeConst, \
rossberg 2013/04/29 10:50:27 Nit: indentation (also, we usually put the && on t
Dmitry Lomov (no reviews) 2013/04/29 11:08:53 Done.
2774 "v8::" #ApiClass "::Cast()", \
2775 "Could not convert to " #ApiClass); \
2776 }
2777
2778
2779 CHECK_TYPED_ARRAY_CAST(Uint8Array, kExternalUnsignedByteArray)
2780 CHECK_TYPED_ARRAY_CAST(Int8Array, kExternalByteArray)
2781 CHECK_TYPED_ARRAY_CAST(Uint16Array, kExternalUnsignedShortArray)
2782 CHECK_TYPED_ARRAY_CAST(Int16Array, kExternalShortArray)
2783 CHECK_TYPED_ARRAY_CAST(Uint32Array, kExternalUnsignedIntArray)
2784 CHECK_TYPED_ARRAY_CAST(Int32Array, kExternalIntArray)
2785 CHECK_TYPED_ARRAY_CAST(Float32Array, kExternalFloatArray)
2786 CHECK_TYPED_ARRAY_CAST(Float64Array, kExternalDoubleArray)
2787
2788 #undef CHECK_TYPED_ARRAY_CAST
2789
2790
2758 void v8::Date::CheckCast(v8::Value* that) { 2791 void v8::Date::CheckCast(v8::Value* that) {
2759 i::Isolate* isolate = i::Isolate::Current(); 2792 i::Isolate* isolate = i::Isolate::Current();
2760 if (IsDeadCheck(isolate, "v8::Date::Cast()")) return; 2793 if (IsDeadCheck(isolate, "v8::Date::Cast()")) return;
2761 i::Handle<i::Object> obj = Utils::OpenHandle(that); 2794 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2762 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Date_string()), 2795 ApiCheck(obj->HasSpecificClassOf(isolate->heap()->Date_string()),
2763 "v8::Date::Cast()", 2796 "v8::Date::Cast()",
2764 "Could not convert to date"); 2797 "Could not convert to date");
2765 } 2798 }
2766 2799
2767 2800
(...skipping 3031 matching lines...) Expand 10 before | Expand all | Expand 10 after
5799 EnsureInitializedForIsolate(isolate, "v8::ArrayBuffer::New(void*, size_t)"); 5832 EnsureInitializedForIsolate(isolate, "v8::ArrayBuffer::New(void*, size_t)");
5800 LOG_API(isolate, "v8::ArrayBuffer::New(void*, size_t)"); 5833 LOG_API(isolate, "v8::ArrayBuffer::New(void*, size_t)");
5801 ENTER_V8(isolate); 5834 ENTER_V8(isolate);
5802 i::Handle<i::JSArrayBuffer> obj = 5835 i::Handle<i::JSArrayBuffer> obj =
5803 isolate->factory()->NewJSArrayBuffer(); 5836 isolate->factory()->NewJSArrayBuffer();
5804 i::Runtime::SetupArrayBuffer(isolate, obj, data, byte_length); 5837 i::Runtime::SetupArrayBuffer(isolate, obj, data, byte_length);
5805 return Utils::ToLocal(obj); 5838 return Utils::ToLocal(obj);
5806 } 5839 }
5807 5840
5808 5841
5842 Local<ArrayBuffer> v8::TypedArray::Buffer() {
5843 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
5844 if (IsDeadCheck(isolate, "v8::TypedArray::Buffer()"))
5845 return Local<ArrayBuffer>();
5846 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
5847 ASSERT(obj->buffer()->IsJSArrayBuffer());
5848 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(obj->buffer()));
5849 return Utils::ToLocal(buffer);
5850 }
5851
5852
5853 size_t v8::TypedArray::ByteOffset() {
5854 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
5855 if (IsDeadCheck(isolate, "v8::TypedArray::ByteOffset()")) return 0;
5856 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
5857 return static_cast<size_t>(obj->byte_offset()->Number());
5858 }
5859
5860
5861 size_t v8::TypedArray::ByteLength() {
5862 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
5863 if (IsDeadCheck(isolate, "v8::TypedArray::ByteLength()")) return 0;
5864 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
5865 return static_cast<size_t>(obj->byte_length()->Number());
5866 }
5867
5868
5869 size_t v8::TypedArray::Length() {
5870 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
5871 if (IsDeadCheck(isolate, "v8::TypedArray::Length()")) return 0;
5872 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
5873 return static_cast<size_t>(obj->length()->Number());
5874 }
5875
5876
5877 void* v8::TypedArray::BaseAddress() {
5878 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
5879 if (IsDeadCheck(isolate, "v8::TypedArray::BaseAddress()")) return 0;
rossberg 2013/04/29 10:50:27 Nit: NULL not 0
Dmitry Lomov (no reviews) 2013/04/29 11:08:53 Done.
5880 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
5881 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(obj->buffer()));
5882 void* buffer_data = buffer->backing_store();
5883 size_t byte_offset = static_cast<size_t>(obj->byte_offset()->Number());
5884 return static_cast<uint8_t*>(buffer_data) + byte_offset;
5885 }
5886
5887
5888 template<typename ElementType,
5889 ExternalArrayType array_type,
5890 i::ElementsKind elements_kind>
5891 i::Handle<i::JSTypedArray> NewTypedArray(
5892 i::Isolate* isolate,
5893 Handle<ArrayBuffer> array_buffer, size_t byte_offset, size_t length) {
5894 i::Handle<i::JSTypedArray> obj =
5895 isolate->factory()->NewJSTypedArray(array_type);
5896 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer);
5897
5898 ASSERT(byte_offset % sizeof(ElementType) == 0);
5899 ASSERT(byte_offset + length * sizeof(ElementType) <=
5900 static_cast<size_t>(buffer->byte_length()->Number()));
5901
5902 obj->set_buffer(*buffer);
5903
5904 i::Handle<i::Object> byte_offset_object = isolate->factory()->NewNumber(
5905 static_cast<double>(byte_offset));
5906 obj->set_byte_offset(*byte_offset_object);
5907
5908 i::Handle<i::Object> byte_length_object = isolate->factory()->NewNumber(
5909 static_cast<double>(length*sizeof(ElementType)));
rossberg 2013/04/29 10:50:27 Nit: space around *
Dmitry Lomov (no reviews) 2013/04/29 11:08:53 Done.
5910 obj->set_byte_length(*byte_length_object);
5911
5912 i::Handle<i::Object> length_object = isolate->factory()->NewNumber(
5913 static_cast<double>(length));
5914 obj->set_length(*length_object);
5915
5916 i::Handle<i::ExternalArray> elements =
5917 isolate->factory()->NewExternalArray(
5918 static_cast<int>(length), array_type,
5919 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset);
5920 i::Handle<i::Map> map =
5921 isolate->factory()->GetElementsTransitionMap(
5922 obj, elements_kind);
5923 obj->set_map(*map);
5924 obj->set_elements(*elements);
5925 return obj;
5926 }
5927
5928
5929 #define TYPED_ARRAY_NEW(TypedArray, elementType, arrayType, elementsKind) \
rossberg 2013/04/29 10:50:27 Nit: ElementType, array_type, elements_kind
Dmitry Lomov (no reviews) 2013/04/29 11:08:53 Done.
5930 Local<TypedArray> TypedArray::New(Handle<ArrayBuffer> array_buffer, \
5931 size_t byte_offset, size_t length) { \
5932 i::Isolate* isolate = i::Isolate::Current(); \
5933 EnsureInitializedForIsolate(isolate, \
5934 "v8::" #TypedArray "::New(Handle<ArrayBuffer>, size_t, size_t)"); \
5935 LOG_API(isolate, \
5936 "v8::" #TypedArray "::New(Handle<ArrayBuffer>, size_t, size_t)"); \
5937 ENTER_V8(isolate); \
5938 i::Handle<i::JSTypedArray> obj = \
5939 NewTypedArray<elementType, arrayType, elementsKind>( \
5940 isolate, array_buffer, byte_offset, length); \
5941 return Utils::ToLocal##TypedArray(obj); \
5942 }
5943
5944
5945 TYPED_ARRAY_NEW(Uint8Array, uint8_t, kExternalUnsignedByteArray,
5946 i::EXTERNAL_UNSIGNED_BYTE_ELEMENTS)
5947 TYPED_ARRAY_NEW(Int8Array, int8_t, kExternalByteArray,
5948 i::EXTERNAL_BYTE_ELEMENTS)
5949 TYPED_ARRAY_NEW(Uint16Array, uint16_t, kExternalUnsignedShortArray,
5950 i::EXTERNAL_UNSIGNED_SHORT_ELEMENTS)
5951 TYPED_ARRAY_NEW(Int16Array, int16_t, kExternalShortArray,
5952 i::EXTERNAL_SHORT_ELEMENTS)
5953 TYPED_ARRAY_NEW(Uint32Array, uint32_t, kExternalUnsignedIntArray,
5954 i::EXTERNAL_UNSIGNED_INT_ELEMENTS)
5955 TYPED_ARRAY_NEW(Int32Array, int32_t, kExternalIntArray,
5956 i::EXTERNAL_INT_ELEMENTS)
5957 TYPED_ARRAY_NEW(Float32Array, float, kExternalFloatArray,
5958 i::EXTERNAL_FLOAT_ELEMENTS)
5959 TYPED_ARRAY_NEW(Float64Array, double, kExternalDoubleArray,
5960 i::EXTERNAL_DOUBLE_ELEMENTS)
5961
5962 #undef TYPED_ARRAY_NEW
5963
5964
5809 Local<Symbol> v8::Symbol::New(Isolate* isolate) { 5965 Local<Symbol> v8::Symbol::New(Isolate* isolate) {
5810 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 5966 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
5811 EnsureInitializedForIsolate(i_isolate, "v8::Symbol::New()"); 5967 EnsureInitializedForIsolate(i_isolate, "v8::Symbol::New()");
5812 LOG_API(i_isolate, "Symbol::New()"); 5968 LOG_API(i_isolate, "Symbol::New()");
5813 ENTER_V8(i_isolate); 5969 ENTER_V8(i_isolate);
5814 i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol(); 5970 i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol();
5815 return Utils::ToLocal(result); 5971 return Utils::ToLocal(result);
5816 } 5972 }
5817 5973
5818 5974
(...skipping 1660 matching lines...) Expand 10 before | Expand all | Expand 10 after
7479 7635
7480 v->VisitPointers(blocks_.first(), first_block_limit_); 7636 v->VisitPointers(blocks_.first(), first_block_limit_);
7481 7637
7482 for (int i = 1; i < blocks_.length(); i++) { 7638 for (int i = 1; i < blocks_.length(); i++) {
7483 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]); 7639 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]);
7484 } 7640 }
7485 } 7641 }
7486 7642
7487 7643
7488 } } // namespace v8::internal 7644 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/api.h ('k') | src/bootstrapper.cc » ('j') | test/cctest/test-api.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698