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

Side by Side Diff: src/api.cc

Issue 15943002: v8 typed arrays: add DataView type (Closed)
Patch Set: 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
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 2569 matching lines...) Expand 10 before | Expand all | Expand 10 after
2580 } 2580 }
2581 2581
2582 2582
2583 bool Value::IsArrayBuffer() const { 2583 bool Value::IsArrayBuffer() const {
2584 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsArrayBuffer()")) 2584 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsArrayBuffer()"))
2585 return false; 2585 return false;
2586 return Utils::OpenHandle(this)->IsJSArrayBuffer(); 2586 return Utils::OpenHandle(this)->IsJSArrayBuffer();
2587 } 2587 }
2588 2588
2589 2589
2590 bool Value::IsDataView() const {
2591 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsDataView()"))
2592 return false;
2593 return Utils::OpenHandle(this)->IsJSDataView();
2594 }
2595
2596
2590 bool Value::IsTypedArray() const { 2597 bool Value::IsTypedArray() const {
2591 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsArrayBuffer()")) 2598 if (IsDeadCheck(i::Isolate::Current(), "v8::Value::IsArrayBuffer()"))
2592 return false; 2599 return false;
2593 return Utils::OpenHandle(this)->IsJSTypedArray(); 2600 return Utils::OpenHandle(this)->IsJSTypedArray();
2594 } 2601 }
2595 2602
2596 2603
2597 #define TYPED_ARRAY_LIST(F) \ 2604 #define TYPED_ARRAY_LIST(F) \
2598 F(Uint8Array, kExternalUnsignedByteArray) \ 2605 F(Uint8Array, kExternalUnsignedByteArray) \
2599 F(Int8Array, kExternalByteArray) \ 2606 F(Int8Array, kExternalByteArray) \
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
2959 2966
2960 void v8::ArrayBuffer::CheckCast(Value* that) { 2967 void v8::ArrayBuffer::CheckCast(Value* that) {
2961 if (IsDeadCheck(i::Isolate::Current(), "v8::ArrayBuffer::Cast()")) return; 2968 if (IsDeadCheck(i::Isolate::Current(), "v8::ArrayBuffer::Cast()")) return;
2962 i::Handle<i::Object> obj = Utils::OpenHandle(that); 2969 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2963 ApiCheck(obj->IsJSArrayBuffer(), 2970 ApiCheck(obj->IsJSArrayBuffer(),
2964 "v8::ArrayBuffer::Cast()", 2971 "v8::ArrayBuffer::Cast()",
2965 "Could not convert to ArrayBuffer"); 2972 "Could not convert to ArrayBuffer");
2966 } 2973 }
2967 2974
2968 2975
2976 void v8::DataView::CheckCast(Value* that) {
2977 if (IsDeadCheck(i::Isolate::Current(), "v8::DataView::Cast()")) return;
2978 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2979 ApiCheck(obj->IsJSDataView(),
2980 "v8::DataView::Cast()",
2981 "Could not convert to DataView");
2982 }
2983
2984
2969 void v8::TypedArray::CheckCast(Value* that) { 2985 void v8::TypedArray::CheckCast(Value* that) {
2970 if (IsDeadCheck(i::Isolate::Current(), "v8::TypedArray::Cast()")) return; 2986 if (IsDeadCheck(i::Isolate::Current(), "v8::TypedArray::Cast()")) return;
2971 i::Handle<i::Object> obj = Utils::OpenHandle(that); 2987 i::Handle<i::Object> obj = Utils::OpenHandle(that);
2972 ApiCheck(obj->IsJSTypedArray(), 2988 ApiCheck(obj->IsJSTypedArray(),
2973 "v8::TypedArray::Cast()", 2989 "v8::TypedArray::Cast()",
2974 "Could not convert to TypedArray"); 2990 "Could not convert to TypedArray");
2975 } 2991 }
2976 2992
2977 2993
2978 #define CHECK_TYPED_ARRAY_CAST(ApiClass, typeConst) \ 2994 #define CHECK_TYPED_ARRAY_CAST(ApiClass, typeConst) \
(...skipping 3088 matching lines...) Expand 10 before | Expand all | Expand 10 after
6067 EnsureInitializedForIsolate(isolate, "v8::ArrayBuffer::New(void*, size_t)"); 6083 EnsureInitializedForIsolate(isolate, "v8::ArrayBuffer::New(void*, size_t)");
6068 LOG_API(isolate, "v8::ArrayBuffer::New(void*, size_t)"); 6084 LOG_API(isolate, "v8::ArrayBuffer::New(void*, size_t)");
6069 ENTER_V8(isolate); 6085 ENTER_V8(isolate);
6070 i::Handle<i::JSArrayBuffer> obj = 6086 i::Handle<i::JSArrayBuffer> obj =
6071 isolate->factory()->NewJSArrayBuffer(); 6087 isolate->factory()->NewJSArrayBuffer();
6072 i::Runtime::SetupArrayBuffer(isolate, obj, true, data, byte_length); 6088 i::Runtime::SetupArrayBuffer(isolate, obj, true, data, byte_length);
6073 return Utils::ToLocal(obj); 6089 return Utils::ToLocal(obj);
6074 } 6090 }
6075 6091
6076 6092
6093 Local<DataView> v8::DataView::New(Handle<ArrayBuffer> array_buffer,
6094 size_t byte_offset,
6095 size_t byte_length) {
6096 i::Isolate* isolate = i::Isolate::Current();
6097 i::Handle<i::JSDataView> obj =
6098 isolate->factory()->NewJSDataView();
6099 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer);
6100 ASSERT(byte_offset + byte_length <=
6101 static_cast<size_t>(buffer->byte_length()->Number()));
6102 obj->set_buffer(*buffer);
6103 i::Handle<i::Object> byte_offset_object = isolate->factory()->NewNumber(
6104 static_cast<double>(byte_offset));
6105 obj->set_byte_offset(*byte_offset_object);
6106 i::Handle<i::Object> byte_length_object = isolate->factory()->NewNumber(
6107 static_cast<double>(byte_length));
6108 obj->set_byte_length(*byte_length_object);
6109 i::Handle<i::ExternalArray> elements =
6110 isolate->factory()->NewExternalArray(
6111 static_cast<int>(byte_length),
6112 kExternalUnsignedByteArray,
6113 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset);
6114 i::Handle<i::Map> map =
6115 isolate->factory()->GetElementsTransitionMap(
6116 obj, i::EXTERNAL_UNSIGNED_BYTE_ELEMENTS);
6117 obj->set_map(*map);
6118 obj->set_elements(*elements);
6119 return Utils::ToLocal(obj);
6120 }
6121
6122
6123 Local<ArrayBuffer> v8::DataView::Buffer() {
6124 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
6125 if (IsDeadCheck(isolate, "v8::DataView::Buffer()"))
6126 return Local<ArrayBuffer>();
6127 i::Handle<i::JSDataView> obj = Utils::OpenHandle(this);
6128 ASSERT(obj->buffer()->IsJSArrayBuffer());
6129 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(obj->buffer()));
6130 return Utils::ToLocal(buffer);
6131 }
6132
6133
6134 size_t v8::DataView::ByteOffset() const {
6135 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
6136 if (IsDeadCheck(isolate, "v8::DataView::ByteOffset()")) return 0;
6137 i::Handle<i::JSDataView> obj = Utils::OpenHandle(this);
6138 return static_cast<size_t>(obj->byte_offset()->Number());
6139 }
6140
6141
6142 size_t v8::DataView::ByteLength() const {
6143 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
6144 if (IsDeadCheck(isolate, "v8::DataView::ByteLength()")) return 0;
6145 i::Handle<i::JSDataView> obj = Utils::OpenHandle(this);
6146 return static_cast<size_t>(obj->byte_length()->Number());
6147 }
6148
6149
6150 void* v8::DataView::Data() const {
6151 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
6152 if (IsDeadCheck(isolate, "v8::DataView::BaseAddress()")) return NULL;
6153 i::Handle<i::JSDataView> obj = Utils::OpenHandle(this);
6154 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(obj->buffer()));
6155 void* buffer_data = buffer->backing_store();
6156 size_t byte_offset = static_cast<size_t>(obj->byte_offset()->Number());
6157 return static_cast<uint8_t*>(buffer_data) + byte_offset;
6158 }
6159
6160
6161 #define DATA_VIEW_GETTER(FunctionName, TypeName) \
Sven Panne 2013/05/24 06:58:25 Don't duplicate tons of code here, just make swizz
bnoordhuis 2013/05/24 11:49:03 That means adding a `bool little_endian` to the Ge
6162 TypeName v8::DataView::FunctionName(size_t byte_offset) const { \
6163 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); \
6164 if (IsDeadCheck(isolate, "v8::DataView::" # FunctionName)) return 0; \
6165 i::Handle<i::JSDataView> obj = Utils::OpenHandle(this); \
6166 size_t byte_length = static_cast<size_t>(obj->byte_length()->Number()); \
6167 if (byte_offset + sizeof(TypeName) > byte_length) return 0; \
6168 if (byte_offset + sizeof(TypeName) < byte_offset) return 0; \
6169 i::Handle<i::JSArrayBuffer> buffer( \
6170 i::JSArrayBuffer::cast(obj->buffer())); \
6171 uint8_t* data = static_cast<uint8_t*>(buffer->backing_store()) + \
6172 static_cast<size_t>(obj->byte_offset()->Number()) + \
6173 byte_offset; \
6174 return *reinterpret_cast<TypeName*>(data); \
6175 }
6176
6177 #define DATA_VIEW_GETTER_SWIZZLE(FunctionName, TypeName) \
6178 TypeName v8::DataView::FunctionName(size_t byte_offset, \
6179 bool little_endian) const { \
6180 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); \
6181 if (IsDeadCheck(isolate, "v8::DataView::" # FunctionName)) return 0; \
6182 i::Handle<i::JSDataView> obj = Utils::OpenHandle(this); \
6183 size_t byte_length = static_cast<size_t>(obj->byte_length()->Number()); \
6184 if (byte_offset + sizeof(TypeName) > byte_length) return 0; \
6185 if (byte_offset + sizeof(TypeName) < byte_offset) return 0; \
6186 i::Handle<i::JSArrayBuffer> buffer( \
6187 i::JSArrayBuffer::cast(obj->buffer())); \
6188 uint8_t* data = static_cast<uint8_t*>(buffer->backing_store()) + \
6189 static_cast<size_t>(obj->byte_offset()->Number()) + \
6190 byte_offset; \
6191 TypeName value; \
6192 i::OS::MemCopy(&value, data, sizeof(value)); \
6193 if (little_endian ^ i::IsLittleEndian()) i::Swizzle(&value); \
6194 return value; \
6195 }
6196
6197 #define DATA_VIEW_SETTER(FunctionName, TypeName) \
Sven Panne 2013/05/24 06:58:25 Same here.
6198 void v8::DataView::FunctionName(size_t byte_offset, TypeName value) { \
6199 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); \
6200 if (IsDeadCheck(isolate, "v8::DataView::" # FunctionName)) return; \
6201 i::Handle<i::JSDataView> obj = Utils::OpenHandle(this); \
6202 size_t byte_length = static_cast<size_t>(obj->byte_length()->Number()); \
6203 if (byte_offset + sizeof(TypeName) > byte_length) return; \
6204 if (byte_offset + sizeof(TypeName) < byte_offset) return; \
6205 i::Handle<i::JSArrayBuffer> buffer( \
6206 i::JSArrayBuffer::cast(obj->buffer())); \
6207 uint8_t* data = static_cast<uint8_t*>(buffer->backing_store()) + \
6208 static_cast<size_t>(obj->byte_offset()->Number()) + \
6209 byte_offset; \
6210 i::OS::MemCopy(data, &value, sizeof(value)); \
6211 }
6212
6213
6214 #define DATA_VIEW_SETTER_SWIZZLE(FunctionName, TypeName) \
Sven Panne 2013/05/24 12:19:20 Can't we make this macro a template and simply del
6215 void v8::DataView::FunctionName(size_t byte_offset, \
6216 TypeName value, \
6217 bool little_endian) { \
6218 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); \
6219 if (IsDeadCheck(isolate, "v8::DataView::" # FunctionName)) return; \
6220 i::Handle<i::JSDataView> obj = Utils::OpenHandle(this); \
6221 size_t byte_length = static_cast<size_t>(obj->byte_length()->Number()); \
6222 if (byte_offset + sizeof(TypeName) > byte_length) return; \
6223 if (byte_offset + sizeof(TypeName) < byte_offset) return; \
6224 i::Handle<i::JSArrayBuffer> buffer( \
6225 i::JSArrayBuffer::cast(obj->buffer())); \
6226 uint8_t* data = static_cast<uint8_t*>(buffer->backing_store()) + \
6227 static_cast<size_t>(obj->byte_offset()->Number()) + \
6228 byte_offset; \
6229 if (little_endian ^ i::IsLittleEndian()) i::Swizzle(&value); \
6230 i::OS::MemCopy(data, &value, sizeof(value)); \
6231 }
6232
6233 DATA_VIEW_GETTER(GetInt8, int8_t)
Sven Panne 2013/05/24 12:19:20 These and the following lines would simply delegat
6234 DATA_VIEW_GETTER(GetUint8, uint8_t)
6235 DATA_VIEW_GETTER_SWIZZLE(GetInt16, int16_t)
6236 DATA_VIEW_GETTER_SWIZZLE(GetUint16, uint16_t)
6237 DATA_VIEW_GETTER_SWIZZLE(GetInt32, int32_t)
6238 DATA_VIEW_GETTER_SWIZZLE(GetUint32, uint32_t)
6239 DATA_VIEW_GETTER_SWIZZLE(GetFloat32, float)
6240 DATA_VIEW_GETTER_SWIZZLE(GetFloat64, double)
6241
6242 DATA_VIEW_SETTER(SetInt8, int8_t)
6243 DATA_VIEW_SETTER(SetUint8, uint8_t)
6244 DATA_VIEW_SETTER_SWIZZLE(SetInt16, int16_t)
6245 DATA_VIEW_SETTER_SWIZZLE(SetUint16, uint16_t)
6246 DATA_VIEW_SETTER_SWIZZLE(SetInt32, int32_t)
6247 DATA_VIEW_SETTER_SWIZZLE(SetUint32, uint32_t)
6248 DATA_VIEW_SETTER_SWIZZLE(SetFloat32, float)
6249 DATA_VIEW_SETTER_SWIZZLE(SetFloat64, double)
6250
6251 #undef DATA_VIEW_GETTER_SWIZZLE
6252 #undef DATA_VIEW_SETTER_SWIZZLE
6253 #undef DATA_VIEW_GETTER
6254 #undef DATA_VIEW_SETTER
6255
6256
6077 Local<ArrayBuffer> v8::TypedArray::Buffer() { 6257 Local<ArrayBuffer> v8::TypedArray::Buffer() {
6078 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); 6258 i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
6079 if (IsDeadCheck(isolate, "v8::TypedArray::Buffer()")) 6259 if (IsDeadCheck(isolate, "v8::TypedArray::Buffer()"))
6080 return Local<ArrayBuffer>(); 6260 return Local<ArrayBuffer>();
6081 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this); 6261 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this);
6082 ASSERT(obj->buffer()->IsJSArrayBuffer()); 6262 ASSERT(obj->buffer()->IsJSArrayBuffer());
6083 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(obj->buffer())); 6263 i::Handle<i::JSArrayBuffer> buffer(i::JSArrayBuffer::cast(obj->buffer()));
6084 return Utils::ToLocal(buffer); 6264 return Utils::ToLocal(buffer);
6085 } 6265 }
6086 6266
(...skipping 1802 matching lines...) Expand 10 before | Expand all | Expand 10 after
7889 8069
7890 v->VisitPointers(blocks_.first(), first_block_limit_); 8070 v->VisitPointers(blocks_.first(), first_block_limit_);
7891 8071
7892 for (int i = 1; i < blocks_.length(); i++) { 8072 for (int i = 1; i < blocks_.length(); i++) {
7893 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]); 8073 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]);
7894 } 8074 }
7895 } 8075 }
7896 8076
7897 8077
7898 } } // namespace v8::internal 8078 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698