Chromium Code Reviews| 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) \ | |
| 15 if (name->is_shared()) { \ | |
| 16 THROW_NEW_ERROR_RETURN_FAILURE( \ | |
| 17 isolate, \ | |
| 18 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \ | |
| 19 isolate->factory()->NewStringFromAsciiChecked(method), \ | |
| 20 name)); \ | |
| 21 } | |
| 22 | |
| 14 // ----------------------------------------------------------------------------- | 23 // ----------------------------------------------------------------------------- |
| 15 // ES6 section 21.1 ArrayBuffer Objects | 24 // ES6 section 21.1 ArrayBuffer Objects |
| 16 | 25 |
| 17 // ES6 section 24.1.2.1 ArrayBuffer ( length ) for the [[Call]] case. | 26 // ES6 section 24.1.2.1 ArrayBuffer ( length ) for the [[Call]] case. |
| 18 BUILTIN(ArrayBufferConstructor) { | 27 BUILTIN(ArrayBufferConstructor) { |
| 19 HandleScope scope(isolate); | 28 HandleScope scope(isolate); |
| 20 Handle<JSFunction> target = args.target(); | 29 Handle<JSFunction> target = args.target(); |
| 21 DCHECK(*target == target->native_context()->array_buffer_fun() || | 30 DCHECK(*target == target->native_context()->array_buffer_fun() || |
| 22 *target == target->native_context()->shared_array_buffer_fun()); | 31 *target == target->native_context()->shared_array_buffer_fun()); |
| 23 THROW_NEW_ERROR_RETURN_FAILURE( | 32 THROW_NEW_ERROR_RETURN_FAILURE( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 isolate, byte_length, true, | 65 isolate, byte_length, true, |
| 57 shared_flag)) { | 66 shared_flag)) { |
| 58 THROW_NEW_ERROR_RETURN_FAILURE( | 67 THROW_NEW_ERROR_RETURN_FAILURE( |
| 59 isolate, NewRangeError(MessageTemplate::kArrayBufferAllocationFailed)); | 68 isolate, NewRangeError(MessageTemplate::kArrayBufferAllocationFailed)); |
| 60 } | 69 } |
| 61 return *result; | 70 return *result; |
| 62 } | 71 } |
| 63 | 72 |
| 64 // ES6 section 24.1.4.1 get ArrayBuffer.prototype.byteLength | 73 // ES6 section 24.1.4.1 get ArrayBuffer.prototype.byteLength |
| 65 BUILTIN(ArrayBufferPrototypeGetByteLength) { | 74 BUILTIN(ArrayBufferPrototypeGetByteLength) { |
| 75 const char* const kMethodName = "get ArrayBuffer.prototype.byteLength"; | |
| 66 HandleScope scope(isolate); | 76 HandleScope scope(isolate); |
| 67 CHECK_RECEIVER(JSArrayBuffer, array_buffer, | 77 CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName); |
| 68 "get ArrayBuffer.prototype.byteLength"); | 78 CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName); |
| 69 | |
| 70 if (array_buffer->is_shared()) { | |
| 71 THROW_NEW_ERROR_RETURN_FAILURE( | |
| 72 isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, | |
| 73 isolate->factory()->NewStringFromAsciiChecked( | |
| 74 "get ArrayBuffer.prototype.byteLength"), | |
| 75 args.receiver())); | |
| 76 } | |
| 77 // 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 |
| 78 // here if the JSArrayBuffer is detached. | 80 // here if the JSArrayBuffer is detached. |
| 79 return array_buffer->byte_length(); | 81 return array_buffer->byte_length(); |
| 80 } | 82 } |
| 81 | 83 |
| 82 // ES6 section 24.1.3.1 ArrayBuffer.isView ( arg ) | 84 // ES6 section 24.1.3.1 ArrayBuffer.isView ( arg ) |
| 83 BUILTIN(ArrayBufferIsView) { | 85 BUILTIN(ArrayBufferIsView) { |
| 84 SealHandleScope shs(isolate); | 86 SealHandleScope shs(isolate); |
| 85 DCHECK_EQ(2, args.length()); | 87 DCHECK_EQ(2, args.length()); |
| 86 Object* arg = args[1]; | 88 Object* arg = args[1]; |
| 87 return isolate->heap()->ToBoolean(arg->IsJSArrayBufferView()); | 89 return isolate->heap()->ToBoolean(arg->IsJSArrayBufferView()); |
| 88 } | 90 } |
| 89 | 91 |
| 92 // ES6 section 24.1.4.3 ArrayBuffer.prototype.slice ( start, end ) | |
|
Dan Ehrenberg
2017/02/17 09:18:19
Optional nit: As long as you're updating the cross
binji
2017/02/17 18:59:27
Done.
| |
| 93 BUILTIN(ArrayBufferPrototypeSlice) { | |
| 94 const char* const kMethodName = "ArrayBuffer.prototype.slice"; | |
| 95 HandleScope scope(isolate); | |
| 96 Handle<Object> start = args.at(1); | |
| 97 Handle<Object> end = args.atOrUndefined(isolate, 2); | |
| 98 | |
| 99 // 2. If Type(O) is not Object, throw a TypeError exception. | |
| 100 // 3. If O does not have an [[ArrayBufferData]] internal slot, throw a | |
| 101 // TypeError exception. | |
| 102 CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName); | |
| 103 // 4. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. | |
| 104 CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName); | |
| 105 | |
| 106 // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. | |
| 107 if (array_buffer->was_neutered()) { | |
| 108 THROW_NEW_ERROR_RETURN_FAILURE( | |
| 109 isolate, NewTypeError(MessageTemplate::kDetachedOperation, | |
| 110 isolate->factory()->NewStringFromAsciiChecked( | |
| 111 kMethodName))); | |
| 112 } | |
| 113 | |
| 114 // 6. Let len be O.[[ArrayBufferByteLength]]. | |
| 115 double const len = array_buffer->byte_length()->Number(); | |
| 116 | |
| 117 // 7. Let relativeStart be ? ToInteger(start). | |
| 118 Handle<Object> relative_start; | |
| 119 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_start, | |
| 120 Object::ToInteger(isolate, start)); | |
| 121 | |
| 122 // 8. If relativeStart < 0, let first be max((len + relativeStart), 0); else | |
| 123 // let first be min(relativeStart, len). | |
| 124 double const first = (relative_start->Number() < 0) | |
| 125 ? Max(len + relative_start->Number(), 0.0) | |
| 126 : Min(relative_start->Number(), len); | |
| 127 Handle<Object> first_obj = isolate->factory()->NewNumber(first); | |
| 128 | |
| 129 // 9. If end is undefined, let relativeEnd be len; else let relativeEnd be ? | |
| 130 // ToInteger(end). | |
| 131 double relative_end; | |
| 132 if (end->IsUndefined(isolate)) { | |
| 133 relative_end = len; | |
| 134 } else { | |
| 135 Handle<Object> relative_end_obj; | |
| 136 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_end_obj, | |
| 137 Object::ToInteger(isolate, end)); | |
| 138 relative_end = relative_end_obj->Number(); | |
| 139 } | |
| 140 | |
| 141 // 10. If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let | |
| 142 // final be min(relativeEnd, len). | |
| 143 double const final_ = (relative_end < 0) ? Max(len + relative_end, 0.0) | |
| 144 : Min(relative_end, len); | |
| 145 | |
| 146 // 11. Let newLen be max(final-first, 0). | |
| 147 double const new_len = Max(final_ - first, 0.0); | |
| 148 Handle<Object> new_len_obj = isolate->factory()->NewNumber(new_len); | |
| 149 | |
| 150 // 12. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%). | |
| 151 Handle<JSFunction> arraybuffer_fun = isolate->array_buffer_fun(); | |
| 152 Handle<Object> ctor; | |
| 153 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 154 isolate, ctor, | |
| 155 Object::SpeciesConstructor( | |
| 156 isolate, Handle<JSReceiver>::cast(args.receiver()), arraybuffer_fun)); | |
| 157 | |
| 158 // 13. Let new be ? Construct(ctor, newLen). | |
| 159 Handle<JSReceiver> new_; | |
| 160 { | |
| 161 const int argc = 1; | |
| 162 | |
| 163 ScopedVector<Handle<Object>> argv(argc); | |
| 164 argv[0] = new_len_obj; | |
| 165 | |
| 166 Handle<Object> new_obj; | |
| 167 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 168 isolate, new_obj, | |
| 169 Execution::New(Handle<JSFunction>::cast(ctor), argc, argv.start())); | |
| 170 | |
| 171 new_ = Handle<JSReceiver>::cast(new_obj); | |
| 172 } | |
| 173 | |
| 174 // 14. If new does not have an [[ArrayBufferData]] internal slot, throw a | |
| 175 // TypeError exception. | |
| 176 if (!new_->IsJSArrayBuffer()) { | |
| 177 THROW_NEW_ERROR_RETURN_FAILURE( | |
| 178 isolate, | |
| 179 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, | |
| 180 isolate->factory()->NewStringFromAsciiChecked(kMethodName), | |
| 181 new_)); | |
| 182 } | |
| 183 | |
| 184 // 15. If IsSharedArrayBuffer(new) is true, throw a TypeError exception. | |
| 185 Handle<JSArrayBuffer> new_array_buffer = Handle<JSArrayBuffer>::cast(new_); | |
| 186 CHECK_IS_NOT_SHARED_ARRAY_BUFFER(new_array_buffer, kMethodName); | |
| 187 | |
| 188 // 16. If IsDetachedBuffer(new) is true, throw a TypeError exception. | |
| 189 if (new_array_buffer->was_neutered()) { | |
| 190 THROW_NEW_ERROR_RETURN_FAILURE( | |
| 191 isolate, NewTypeError(MessageTemplate::kDetachedOperation, | |
| 192 isolate->factory()->NewStringFromAsciiChecked( | |
| 193 kMethodName))); | |
| 194 } | |
| 195 | |
| 196 // 17. If SameValue(new, O) is true, throw a TypeError exception. | |
| 197 if (new_->SameValue(*args.receiver())) { | |
| 198 THROW_NEW_ERROR_RETURN_FAILURE( | |
| 199 isolate, NewTypeError(MessageTemplate::kArrayBufferSpeciesThis)); | |
| 200 } | |
| 201 | |
| 202 // 18. If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception. | |
| 203 if (new_array_buffer->byte_length()->Number() < new_len) { | |
| 204 THROW_NEW_ERROR_RETURN_FAILURE( | |
| 205 isolate, NewTypeError(MessageTemplate::kArrayBufferTooShort)); | |
| 206 } | |
| 207 | |
| 208 // 19. NOTE: Side-effects of the above steps may have detached O. | |
| 209 // 20. If IsDetachedBuffer(O) is true, throw a TypeError exception. | |
| 210 if (array_buffer->was_neutered()) { | |
| 211 THROW_NEW_ERROR_RETURN_FAILURE( | |
| 212 isolate, NewTypeError(MessageTemplate::kDetachedOperation, | |
| 213 isolate->factory()->NewStringFromAsciiChecked( | |
| 214 kMethodName))); | |
| 215 } | |
| 216 | |
| 217 // 21. Let fromBuf be O.[[ArrayBufferData]]. | |
| 218 // 22. Let toBuf be new.[[ArrayBufferData]]. | |
| 219 // 23. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen). | |
| 220 size_t first_size = 0, new_len_size = 0; | |
| 221 CHECK(TryNumberToSize(*first_obj, &first_size)); | |
| 222 CHECK(TryNumberToSize(*new_len_obj, &new_len_size)); | |
| 223 CHECK(NumberToSize(new_array_buffer->byte_length()) >= new_len_size); | |
|
Dan Ehrenberg
2017/02/17 09:18:19
Should this last one be a DCHECK? Anyway, this (an
binji
2017/02/17 18:59:27
Seems reasonable, done.
| |
| 224 | |
| 225 if (new_len_size != 0) { | |
| 226 size_t from_byte_length = NumberToSize(array_buffer->byte_length()); | |
| 227 CHECK(first_size <= from_byte_length); | |
| 228 CHECK(from_byte_length - first_size >= new_len_size); | |
|
Dan Ehrenberg
2017/02/17 09:18:19
DCHECKs?
binji
2017/02/17 18:59:27
Done.
| |
| 229 uint8_t* from_data = | |
| 230 reinterpret_cast<uint8_t*>(array_buffer->backing_store()); | |
| 231 uint8_t* to_data = | |
| 232 reinterpret_cast<uint8_t*>(new_array_buffer->backing_store()); | |
| 233 CopyBytes(to_data, from_data + first_size, new_len_size); | |
| 234 } | |
| 235 | |
| 236 return *new_; | |
| 237 } | |
| 238 | |
| 90 } // namespace internal | 239 } // namespace internal |
| 91 } // namespace v8 | 240 } // namespace v8 |
| OLD | NEW |