OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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/api.h" | 5 #include "src/api.h" |
6 | 6 |
7 #include <string.h> // For memcpy, strlen. | 7 #include <string.h> // For memcpy, strlen. |
8 #ifdef V8_USE_ADDRESS_SANITIZER | 8 #ifdef V8_USE_ADDRESS_SANITIZER |
9 #include <sanitizer/asan_interface.h> | 9 #include <sanitizer/asan_interface.h> |
10 #endif // V8_USE_ADDRESS_SANITIZER | 10 #endif // V8_USE_ADDRESS_SANITIZER |
(...skipping 6082 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6093 return static_cast<size_t>(obj->byte_length()->Number()); | 6093 return static_cast<size_t>(obj->byte_length()->Number()); |
6094 } | 6094 } |
6095 | 6095 |
6096 | 6096 |
6097 size_t v8::TypedArray::Length() { | 6097 size_t v8::TypedArray::Length() { |
6098 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this); | 6098 i::Handle<i::JSTypedArray> obj = Utils::OpenHandle(this); |
6099 return static_cast<size_t>(obj->length()->Number()); | 6099 return static_cast<size_t>(obj->length()->Number()); |
6100 } | 6100 } |
6101 | 6101 |
6102 | 6102 |
6103 static inline void SetupArrayBufferView( | |
6104 i::Isolate* isolate, | |
6105 i::Handle<i::JSArrayBufferView> obj, | |
6106 i::Handle<i::JSArrayBuffer> buffer, | |
6107 size_t byte_offset, | |
6108 size_t byte_length) { | |
6109 DCHECK(byte_offset + byte_length <= | |
6110 static_cast<size_t>(buffer->byte_length()->Number())); | |
6111 | |
6112 obj->set_buffer(*buffer); | |
6113 | |
6114 obj->set_weak_next(buffer->weak_first_view()); | |
6115 buffer->set_weak_first_view(*obj); | |
6116 | |
6117 i::Handle<i::Object> byte_offset_object = | |
6118 isolate->factory()->NewNumberFromSize(byte_offset); | |
6119 obj->set_byte_offset(*byte_offset_object); | |
6120 | |
6121 i::Handle<i::Object> byte_length_object = | |
6122 isolate->factory()->NewNumberFromSize(byte_length); | |
6123 obj->set_byte_length(*byte_length_object); | |
6124 } | |
6125 | |
6126 template<typename ElementType, | |
6127 ExternalArrayType array_type, | |
6128 i::ElementsKind elements_kind> | |
6129 i::Handle<i::JSTypedArray> NewTypedArray( | |
6130 i::Isolate* isolate, | |
6131 Handle<ArrayBuffer> array_buffer, size_t byte_offset, size_t length) { | |
6132 i::Handle<i::JSTypedArray> obj = | |
6133 isolate->factory()->NewJSTypedArray(array_type); | |
6134 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); | |
6135 | |
6136 DCHECK(byte_offset % sizeof(ElementType) == 0); | |
6137 | |
6138 CHECK(length <= (std::numeric_limits<size_t>::max() / sizeof(ElementType))); | |
6139 CHECK(length <= static_cast<size_t>(i::Smi::kMaxValue)); | |
6140 size_t byte_length = length * sizeof(ElementType); | |
6141 SetupArrayBufferView( | |
6142 isolate, obj, buffer, byte_offset, byte_length); | |
6143 | |
6144 i::Handle<i::Object> length_object = | |
6145 isolate->factory()->NewNumberFromSize(length); | |
6146 obj->set_length(*length_object); | |
6147 | |
6148 i::Handle<i::ExternalArray> elements = | |
6149 isolate->factory()->NewExternalArray( | |
6150 static_cast<int>(length), array_type, | |
6151 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); | |
6152 i::Handle<i::Map> map = | |
6153 i::JSObject::GetElementsTransitionMap(obj, elements_kind); | |
6154 i::JSObject::SetMapAndElements(obj, map, elements); | |
6155 return obj; | |
6156 } | |
6157 | |
6158 | |
6159 #define TYPED_ARRAY_NEW(Type, type, TYPE, ctype, size) \ | 6103 #define TYPED_ARRAY_NEW(Type, type, TYPE, ctype, size) \ |
6160 Local<Type##Array> Type##Array::New(Handle<ArrayBuffer> array_buffer, \ | 6104 Local<Type##Array> Type##Array::New(Handle<ArrayBuffer> array_buffer, \ |
6161 size_t byte_offset, size_t length) { \ | 6105 size_t byte_offset, size_t length) { \ |
6162 i::Isolate* isolate = Utils::OpenHandle(*array_buffer)->GetIsolate(); \ | 6106 i::Isolate* isolate = Utils::OpenHandle(*array_buffer)->GetIsolate(); \ |
6163 LOG_API(isolate, \ | 6107 LOG_API(isolate, \ |
6164 "v8::" #Type "Array::New(Handle<ArrayBuffer>, size_t, size_t)"); \ | 6108 "v8::" #Type "Array::New(Handle<ArrayBuffer>, size_t, size_t)"); \ |
6165 ENTER_V8(isolate); \ | 6109 ENTER_V8(isolate); \ |
6166 if (!Utils::ApiCheck(length <= static_cast<size_t>(i::Smi::kMaxValue), \ | 6110 if (!Utils::ApiCheck(length <= static_cast<size_t>(i::Smi::kMaxValue), \ |
6167 "v8::" #Type "Array::New(Handle<ArrayBuffer>, size_t, size_t)", \ | 6111 "v8::" #Type \ |
6168 "length exceeds max allowed value")) { \ | 6112 "Array::New(Handle<ArrayBuffer>, size_t, size_t)", \ |
6169 return Local<Type##Array>(); \ | 6113 "length exceeds max allowed value")) { \ |
| 6114 return Local<Type##Array>(); \ |
6170 } \ | 6115 } \ |
6171 i::Handle<i::JSTypedArray> obj = \ | 6116 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); \ |
6172 NewTypedArray<ctype, v8::kExternal##Type##Array, \ | 6117 i::Handle<i::JSTypedArray> obj = isolate->factory()->NewJSTypedArray( \ |
6173 i::EXTERNAL_##TYPE##_ELEMENTS>( \ | 6118 v8::kExternal##Type##Array, buffer, byte_offset, length); \ |
6174 isolate, array_buffer, byte_offset, length); \ | |
6175 return Utils::ToLocal##Type##Array(obj); \ | 6119 return Utils::ToLocal##Type##Array(obj); \ |
6176 } | 6120 } |
6177 | 6121 |
6178 | 6122 |
6179 TYPED_ARRAYS(TYPED_ARRAY_NEW) | 6123 TYPED_ARRAYS(TYPED_ARRAY_NEW) |
6180 #undef TYPED_ARRAY_NEW | 6124 #undef TYPED_ARRAY_NEW |
6181 | 6125 |
6182 Local<DataView> DataView::New(Handle<ArrayBuffer> array_buffer, | 6126 Local<DataView> DataView::New(Handle<ArrayBuffer> array_buffer, |
6183 size_t byte_offset, size_t byte_length) { | 6127 size_t byte_offset, size_t byte_length) { |
6184 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); | 6128 i::Handle<i::JSArrayBuffer> buffer = Utils::OpenHandle(*array_buffer); |
6185 i::Isolate* isolate = buffer->GetIsolate(); | 6129 i::Isolate* isolate = buffer->GetIsolate(); |
6186 LOG_API(isolate, "v8::DataView::New(void*, size_t, size_t)"); | 6130 LOG_API(isolate, "v8::DataView::New(void*, size_t, size_t)"); |
6187 ENTER_V8(isolate); | 6131 ENTER_V8(isolate); |
6188 i::Handle<i::JSDataView> obj = isolate->factory()->NewJSDataView(); | 6132 i::Handle<i::JSDataView> obj = |
6189 SetupArrayBufferView( | 6133 isolate->factory()->NewJSDataView(buffer, byte_offset, byte_length); |
6190 isolate, obj, buffer, byte_offset, byte_length); | |
6191 return Utils::ToLocal(obj); | 6134 return Utils::ToLocal(obj); |
6192 } | 6135 } |
6193 | 6136 |
6194 | 6137 |
6195 Local<Symbol> v8::Symbol::New(Isolate* isolate, Local<String> name) { | 6138 Local<Symbol> v8::Symbol::New(Isolate* isolate, Local<String> name) { |
6196 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); | 6139 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
6197 LOG_API(i_isolate, "Symbol::New()"); | 6140 LOG_API(i_isolate, "Symbol::New()"); |
6198 ENTER_V8(i_isolate); | 6141 ENTER_V8(i_isolate); |
6199 i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol(); | 6142 i::Handle<i::Symbol> result = i_isolate->factory()->NewSymbol(); |
6200 if (!name.IsEmpty()) result->set_name(*Utils::OpenHandle(*name)); | 6143 if (!name.IsEmpty()) result->set_name(*Utils::OpenHandle(*name)); |
(...skipping 1517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7718 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); | 7661 Isolate* isolate = reinterpret_cast<Isolate*>(info.GetIsolate()); |
7719 Address callback_address = | 7662 Address callback_address = |
7720 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 7663 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
7721 VMState<EXTERNAL> state(isolate); | 7664 VMState<EXTERNAL> state(isolate); |
7722 ExternalCallbackScope call_scope(isolate, callback_address); | 7665 ExternalCallbackScope call_scope(isolate, callback_address); |
7723 callback(info); | 7666 callback(info); |
7724 } | 7667 } |
7725 | 7668 |
7726 | 7669 |
7727 } } // namespace v8::internal | 7670 } } // namespace v8::internal |
OLD | NEW |