| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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/builtins/builtins-utils.h" | 5 #include "src/builtins/builtins-utils.h" |
| 6 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" |
| 7 #include "src/conversions.h" | 7 #include "src/conversions.h" |
| 8 #include "src/counters.h" | 8 #include "src/counters.h" |
| 9 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 | 13 |
| 14 #define CHECK_IS_NOT_SHARED_ARRAY_BUFFER(name, method) \ | 14 #define CHECK_SHARED(expected, name, method) \ |
| 15 if (name->is_shared()) { \ | 15 if (name->is_shared() != expected) { \ |
| 16 THROW_NEW_ERROR_RETURN_FAILURE( \ | 16 THROW_NEW_ERROR_RETURN_FAILURE( \ |
| 17 isolate, \ | 17 isolate, \ |
| 18 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \ | 18 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \ |
| 19 isolate->factory()->NewStringFromAsciiChecked(method), \ | 19 isolate->factory()->NewStringFromAsciiChecked(method), \ |
| 20 name)); \ | 20 name)); \ |
| 21 } | 21 } |
| 22 | 22 |
| 23 // ----------------------------------------------------------------------------- | 23 // ----------------------------------------------------------------------------- |
| 24 // ES6 section 21.1 ArrayBuffer Objects | 24 // ES6 section 21.1 ArrayBuffer Objects |
| 25 | 25 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 isolate, NewRangeError(MessageTemplate::kArrayBufferAllocationFailed)); | 68 isolate, NewRangeError(MessageTemplate::kArrayBufferAllocationFailed)); |
| 69 } | 69 } |
| 70 return *result; | 70 return *result; |
| 71 } | 71 } |
| 72 | 72 |
| 73 // ES6 section 24.1.4.1 get ArrayBuffer.prototype.byteLength | 73 // ES6 section 24.1.4.1 get ArrayBuffer.prototype.byteLength |
| 74 BUILTIN(ArrayBufferPrototypeGetByteLength) { | 74 BUILTIN(ArrayBufferPrototypeGetByteLength) { |
| 75 const char* const kMethodName = "get ArrayBuffer.prototype.byteLength"; | 75 const char* const kMethodName = "get ArrayBuffer.prototype.byteLength"; |
| 76 HandleScope scope(isolate); | 76 HandleScope scope(isolate); |
| 77 CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName); | 77 CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName); |
| 78 CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName); | 78 CHECK_SHARED(false, array_buffer, kMethodName); |
| 79 // TODO(franzih): According to the ES6 spec, we should throw a TypeError | 79 // TODO(franzih): According to the ES6 spec, we should throw a TypeError |
| 80 // here if the JSArrayBuffer is detached. | 80 // here if the JSArrayBuffer is detached. |
| 81 return array_buffer->byte_length(); | 81 return array_buffer->byte_length(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 // ES7 sharedmem 6.3.4.1 get SharedArrayBuffer.prototype.byteLength |
| 85 BUILTIN(SharedArrayBufferPrototypeGetByteLength) { |
| 86 const char* const kMethodName = "get SharedArrayBuffer.prototype.byteLength"; |
| 87 HandleScope scope(isolate); |
| 88 CHECK_RECEIVER(JSArrayBuffer, array_buffer, |
| 89 "get SharedArrayBuffer.prototype.byteLength"); |
| 90 CHECK_SHARED(true, array_buffer, kMethodName); |
| 91 return array_buffer->byte_length(); |
| 92 } |
| 93 |
| 84 // ES6 section 24.1.3.1 ArrayBuffer.isView ( arg ) | 94 // ES6 section 24.1.3.1 ArrayBuffer.isView ( arg ) |
| 85 BUILTIN(ArrayBufferIsView) { | 95 BUILTIN(ArrayBufferIsView) { |
| 86 SealHandleScope shs(isolate); | 96 SealHandleScope shs(isolate); |
| 87 DCHECK_EQ(2, args.length()); | 97 DCHECK_EQ(2, args.length()); |
| 88 Object* arg = args[1]; | 98 Object* arg = args[1]; |
| 89 return isolate->heap()->ToBoolean(arg->IsJSArrayBufferView()); | 99 return isolate->heap()->ToBoolean(arg->IsJSArrayBufferView()); |
| 90 } | 100 } |
| 91 | 101 |
| 92 // ES #sec-arraybuffer.prototype.slice | 102 static Object* SliceHelper(BuiltinArguments args, Isolate* isolate, |
| 93 // ArrayBuffer.prototype.slice ( start, end ) | 103 const char* kMethodName, bool is_shared) { |
| 94 BUILTIN(ArrayBufferPrototypeSlice) { | |
| 95 const char* const kMethodName = "ArrayBuffer.prototype.slice"; | |
| 96 HandleScope scope(isolate); | 104 HandleScope scope(isolate); |
| 97 Handle<Object> start = args.at(1); | 105 Handle<Object> start = args.at(1); |
| 98 Handle<Object> end = args.atOrUndefined(isolate, 2); | 106 Handle<Object> end = args.atOrUndefined(isolate, 2); |
| 99 | 107 |
| 100 // 2. If Type(O) is not Object, throw a TypeError exception. | 108 // * If Type(O) is not Object, throw a TypeError exception. |
| 101 // 3. If O does not have an [[ArrayBufferData]] internal slot, throw a | 109 // * If O does not have an [[ArrayBufferData]] internal slot, throw a |
| 102 // TypeError exception. | 110 // TypeError exception. |
| 103 CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName); | 111 CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName); |
| 104 // 4. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. | 112 // * [AB] If IsSharedArrayBuffer(O) is true, throw a TypeError exception. |
| 105 CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName); | 113 // * [SAB] If IsSharedArrayBuffer(O) is false, throw a TypeError exception. |
| 114 CHECK_SHARED(is_shared, array_buffer, kMethodName); |
| 106 | 115 |
| 107 // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. | 116 // * [AB] If IsDetachedBuffer(buffer) is true, throw a TypeError exception. |
| 108 if (array_buffer->was_neutered()) { | 117 if (!is_shared && array_buffer->was_neutered()) { |
| 109 THROW_NEW_ERROR_RETURN_FAILURE( | 118 THROW_NEW_ERROR_RETURN_FAILURE( |
| 110 isolate, NewTypeError(MessageTemplate::kDetachedOperation, | 119 isolate, NewTypeError(MessageTemplate::kDetachedOperation, |
| 111 isolate->factory()->NewStringFromAsciiChecked( | 120 isolate->factory()->NewStringFromAsciiChecked( |
| 112 kMethodName))); | 121 kMethodName))); |
| 113 } | 122 } |
| 114 | 123 |
| 115 // 6. Let len be O.[[ArrayBufferByteLength]]. | 124 // * [AB] Let len be O.[[ArrayBufferByteLength]]. |
| 125 // * [SAB] Let len be O.[[ArrayBufferByteLength]]. |
| 116 double const len = array_buffer->byte_length()->Number(); | 126 double const len = array_buffer->byte_length()->Number(); |
| 117 | 127 |
| 118 // 7. Let relativeStart be ? ToInteger(start). | 128 // * Let relativeStart be ? ToInteger(start). |
| 119 Handle<Object> relative_start; | 129 Handle<Object> relative_start; |
| 120 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_start, | 130 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_start, |
| 121 Object::ToInteger(isolate, start)); | 131 Object::ToInteger(isolate, start)); |
| 122 | 132 |
| 123 // 8. If relativeStart < 0, let first be max((len + relativeStart), 0); else | 133 // * If relativeStart < 0, let first be max((len + relativeStart), 0); else |
| 124 // let first be min(relativeStart, len). | 134 // let first be min(relativeStart, len). |
| 125 double const first = (relative_start->Number() < 0) | 135 double const first = (relative_start->Number() < 0) |
| 126 ? Max(len + relative_start->Number(), 0.0) | 136 ? Max(len + relative_start->Number(), 0.0) |
| 127 : Min(relative_start->Number(), len); | 137 : Min(relative_start->Number(), len); |
| 128 Handle<Object> first_obj = isolate->factory()->NewNumber(first); | 138 Handle<Object> first_obj = isolate->factory()->NewNumber(first); |
| 129 | 139 |
| 130 // 9. If end is undefined, let relativeEnd be len; else let relativeEnd be ? | 140 // * If end is undefined, let relativeEnd be len; else let relativeEnd be ? |
| 131 // ToInteger(end). | 141 // ToInteger(end). |
| 132 double relative_end; | 142 double relative_end; |
| 133 if (end->IsUndefined(isolate)) { | 143 if (end->IsUndefined(isolate)) { |
| 134 relative_end = len; | 144 relative_end = len; |
| 135 } else { | 145 } else { |
| 136 Handle<Object> relative_end_obj; | 146 Handle<Object> relative_end_obj; |
| 137 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_end_obj, | 147 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_end_obj, |
| 138 Object::ToInteger(isolate, end)); | 148 Object::ToInteger(isolate, end)); |
| 139 relative_end = relative_end_obj->Number(); | 149 relative_end = relative_end_obj->Number(); |
| 140 } | 150 } |
| 141 | 151 |
| 142 // 10. If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let | 152 // * If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let |
| 143 // final be min(relativeEnd, len). | 153 // final be min(relativeEnd, len). |
| 144 double const final_ = (relative_end < 0) ? Max(len + relative_end, 0.0) | 154 double const final_ = (relative_end < 0) ? Max(len + relative_end, 0.0) |
| 145 : Min(relative_end, len); | 155 : Min(relative_end, len); |
| 146 | 156 |
| 147 // 11. Let newLen be max(final-first, 0). | 157 // * Let newLen be max(final-first, 0). |
| 148 double const new_len = Max(final_ - first, 0.0); | 158 double const new_len = Max(final_ - first, 0.0); |
| 149 Handle<Object> new_len_obj = isolate->factory()->NewNumber(new_len); | 159 Handle<Object> new_len_obj = isolate->factory()->NewNumber(new_len); |
| 150 | 160 |
| 151 // 12. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%). | 161 // * [AB] Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%). |
| 152 Handle<JSFunction> arraybuffer_fun = isolate->array_buffer_fun(); | 162 // * [SAB] Let ctor be ? SpeciesConstructor(O, %SharedArrayBuffer%). |
| 163 Handle<JSFunction> constructor_fun = is_shared |
| 164 ? isolate->shared_array_buffer_fun() |
| 165 : isolate->array_buffer_fun(); |
| 153 Handle<Object> ctor; | 166 Handle<Object> ctor; |
| 154 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 167 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 155 isolate, ctor, | 168 isolate, ctor, |
| 156 Object::SpeciesConstructor( | 169 Object::SpeciesConstructor( |
| 157 isolate, Handle<JSReceiver>::cast(args.receiver()), arraybuffer_fun)); | 170 isolate, Handle<JSReceiver>::cast(args.receiver()), constructor_fun)); |
| 158 | 171 |
| 159 // 13. Let new be ? Construct(ctor, newLen). | 172 // * Let new be ? Construct(ctor, newLen). |
| 160 Handle<JSReceiver> new_; | 173 Handle<JSReceiver> new_; |
| 161 { | 174 { |
| 162 const int argc = 1; | 175 const int argc = 1; |
| 163 | 176 |
| 164 ScopedVector<Handle<Object>> argv(argc); | 177 ScopedVector<Handle<Object>> argv(argc); |
| 165 argv[0] = new_len_obj; | 178 argv[0] = new_len_obj; |
| 166 | 179 |
| 167 Handle<Object> new_obj; | 180 Handle<Object> new_obj; |
| 168 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | 181 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 169 isolate, new_obj, | 182 isolate, new_obj, |
| 170 Execution::New(Handle<JSFunction>::cast(ctor), argc, argv.start())); | 183 Execution::New(Handle<JSFunction>::cast(ctor), argc, argv.start())); |
| 171 | 184 |
| 172 new_ = Handle<JSReceiver>::cast(new_obj); | 185 new_ = Handle<JSReceiver>::cast(new_obj); |
| 173 } | 186 } |
| 174 | 187 |
| 175 // 14. If new does not have an [[ArrayBufferData]] internal slot, throw a | 188 // * If new does not have an [[ArrayBufferData]] internal slot, throw a |
| 176 // TypeError exception. | 189 // TypeError exception. |
| 177 if (!new_->IsJSArrayBuffer()) { | 190 if (!new_->IsJSArrayBuffer()) { |
| 178 THROW_NEW_ERROR_RETURN_FAILURE( | 191 THROW_NEW_ERROR_RETURN_FAILURE( |
| 179 isolate, | 192 isolate, |
| 180 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, | 193 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, |
| 181 isolate->factory()->NewStringFromAsciiChecked(kMethodName), | 194 isolate->factory()->NewStringFromAsciiChecked(kMethodName), |
| 182 new_)); | 195 new_)); |
| 183 } | 196 } |
| 184 | 197 |
| 185 // 15. If IsSharedArrayBuffer(new) is true, throw a TypeError exception. | 198 // * [AB] If IsSharedArrayBuffer(new) is true, throw a TypeError exception. |
| 199 // * [SAB] If IsSharedArrayBuffer(new) is false, throw a TypeError exception. |
| 186 Handle<JSArrayBuffer> new_array_buffer = Handle<JSArrayBuffer>::cast(new_); | 200 Handle<JSArrayBuffer> new_array_buffer = Handle<JSArrayBuffer>::cast(new_); |
| 187 CHECK_IS_NOT_SHARED_ARRAY_BUFFER(new_array_buffer, kMethodName); | 201 CHECK_SHARED(is_shared, new_array_buffer, kMethodName); |
| 188 | 202 |
| 189 // 16. If IsDetachedBuffer(new) is true, throw a TypeError exception. | 203 // * [AB] If IsDetachedBuffer(new) is true, throw a TypeError exception. |
| 190 if (new_array_buffer->was_neutered()) { | 204 if (!is_shared && new_array_buffer->was_neutered()) { |
| 191 THROW_NEW_ERROR_RETURN_FAILURE( | 205 THROW_NEW_ERROR_RETURN_FAILURE( |
| 192 isolate, NewTypeError(MessageTemplate::kDetachedOperation, | 206 isolate, NewTypeError(MessageTemplate::kDetachedOperation, |
| 193 isolate->factory()->NewStringFromAsciiChecked( | 207 isolate->factory()->NewStringFromAsciiChecked( |
| 194 kMethodName))); | 208 kMethodName))); |
| 195 } | 209 } |
| 196 | 210 |
| 197 // 17. If SameValue(new, O) is true, throw a TypeError exception. | 211 // * [AB] If SameValue(new, O) is true, throw a TypeError exception. |
| 198 if (new_->SameValue(*args.receiver())) { | 212 if (!is_shared && new_->SameValue(*args.receiver())) { |
| 199 THROW_NEW_ERROR_RETURN_FAILURE( | 213 THROW_NEW_ERROR_RETURN_FAILURE( |
| 200 isolate, NewTypeError(MessageTemplate::kArrayBufferSpeciesThis)); | 214 isolate, NewTypeError(MessageTemplate::kArrayBufferSpeciesThis)); |
| 201 } | 215 } |
| 202 | 216 |
| 203 // 18. If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception. | 217 // * [SAB] If new.[[ArrayBufferData]] and O.[[ArrayBufferData]] are the same |
| 218 // Shared Data Block values, throw a TypeError exception. |
| 219 if (is_shared && |
| 220 new_array_buffer->backing_store() == array_buffer->backing_store()) { |
| 221 THROW_NEW_ERROR_RETURN_FAILURE( |
| 222 isolate, NewTypeError(MessageTemplate::kSharedArrayBufferSpeciesThis)); |
| 223 } |
| 224 |
| 225 // * If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception. |
| 204 if (new_array_buffer->byte_length()->Number() < new_len) { | 226 if (new_array_buffer->byte_length()->Number() < new_len) { |
| 205 THROW_NEW_ERROR_RETURN_FAILURE( | 227 THROW_NEW_ERROR_RETURN_FAILURE( |
| 206 isolate, NewTypeError(MessageTemplate::kArrayBufferTooShort)); | 228 isolate, |
| 229 NewTypeError(is_shared ? MessageTemplate::kSharedArrayBufferTooShort |
| 230 : MessageTemplate::kArrayBufferTooShort)); |
| 207 } | 231 } |
| 208 | 232 |
| 209 // 19. NOTE: Side-effects of the above steps may have detached O. | 233 // * [AB] NOTE: Side-effects of the above steps may have detached O. |
| 210 // 20. If IsDetachedBuffer(O) is true, throw a TypeError exception. | 234 // * [AB] If IsDetachedBuffer(O) is true, throw a TypeError exception. |
| 211 if (array_buffer->was_neutered()) { | 235 if (!is_shared && array_buffer->was_neutered()) { |
| 212 THROW_NEW_ERROR_RETURN_FAILURE( | 236 THROW_NEW_ERROR_RETURN_FAILURE( |
| 213 isolate, NewTypeError(MessageTemplate::kDetachedOperation, | 237 isolate, NewTypeError(MessageTemplate::kDetachedOperation, |
| 214 isolate->factory()->NewStringFromAsciiChecked( | 238 isolate->factory()->NewStringFromAsciiChecked( |
| 215 kMethodName))); | 239 kMethodName))); |
| 216 } | 240 } |
| 217 | 241 |
| 218 // 21. Let fromBuf be O.[[ArrayBufferData]]. | 242 // * Let fromBuf be O.[[ArrayBufferData]]. |
| 219 // 22. Let toBuf be new.[[ArrayBufferData]]. | 243 // * Let toBuf be new.[[ArrayBufferData]]. |
| 220 // 23. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen). | 244 // * Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen). |
| 221 size_t first_size = 0, new_len_size = 0; | 245 size_t first_size = 0, new_len_size = 0; |
| 222 CHECK(TryNumberToSize(*first_obj, &first_size)); | 246 CHECK(TryNumberToSize(*first_obj, &first_size)); |
| 223 CHECK(TryNumberToSize(*new_len_obj, &new_len_size)); | 247 CHECK(TryNumberToSize(*new_len_obj, &new_len_size)); |
| 224 DCHECK(NumberToSize(new_array_buffer->byte_length()) >= new_len_size); | 248 DCHECK(NumberToSize(new_array_buffer->byte_length()) >= new_len_size); |
| 225 | 249 |
| 226 if (new_len_size != 0) { | 250 if (new_len_size != 0) { |
| 227 size_t from_byte_length = NumberToSize(array_buffer->byte_length()); | 251 size_t from_byte_length = NumberToSize(array_buffer->byte_length()); |
| 228 USE(from_byte_length); | 252 USE(from_byte_length); |
| 229 DCHECK(first_size <= from_byte_length); | 253 DCHECK(first_size <= from_byte_length); |
| 230 DCHECK(from_byte_length - first_size >= new_len_size); | 254 DCHECK(from_byte_length - first_size >= new_len_size); |
| 231 uint8_t* from_data = | 255 uint8_t* from_data = |
| 232 reinterpret_cast<uint8_t*>(array_buffer->backing_store()); | 256 reinterpret_cast<uint8_t*>(array_buffer->backing_store()); |
| 233 uint8_t* to_data = | 257 uint8_t* to_data = |
| 234 reinterpret_cast<uint8_t*>(new_array_buffer->backing_store()); | 258 reinterpret_cast<uint8_t*>(new_array_buffer->backing_store()); |
| 235 CopyBytes(to_data, from_data + first_size, new_len_size); | 259 CopyBytes(to_data, from_data + first_size, new_len_size); |
| 236 } | 260 } |
| 237 | 261 |
| 238 return *new_; | 262 return *new_; |
| 239 } | 263 } |
| 240 | 264 |
| 265 // ES #sec-sharedarraybuffer.prototype.slice |
| 266 BUILTIN(SharedArrayBufferPrototypeSlice) { |
| 267 const char* const kMethodName = "SharedArrayBuffer.prototype.slice"; |
| 268 return SliceHelper(args, isolate, kMethodName, true); |
| 269 } |
| 270 |
| 271 // ES #sec-arraybuffer.prototype.slice |
| 272 // ArrayBuffer.prototype.slice ( start, end ) |
| 273 BUILTIN(ArrayBufferPrototypeSlice) { |
| 274 const char* const kMethodName = "ArrayBuffer.prototype.slice"; |
| 275 return SliceHelper(args, isolate, kMethodName, false); |
| 276 } |
| 277 |
| 241 } // namespace internal | 278 } // namespace internal |
| 242 } // namespace v8 | 279 } // namespace v8 |
| OLD | NEW |