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/code-stub-assembler.h" | 7 #include "src/code-stub-assembler.h" |
| 8 | 8 |
| 9 namespace v8 { | 9 namespace v8 { |
| 10 namespace internal { | 10 namespace internal { |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 Generate_TypedArrayPrototypeIterationMethod<IterationKind::kEntries>( | 160 Generate_TypedArrayPrototypeIterationMethod<IterationKind::kEntries>( |
| 161 state, "%TypedArray%.prototype.entries()"); | 161 state, "%TypedArray%.prototype.entries()"); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void Builtins::Generate_TypedArrayPrototypeKeys( | 164 void Builtins::Generate_TypedArrayPrototypeKeys( |
| 165 compiler::CodeAssemblerState* state) { | 165 compiler::CodeAssemblerState* state) { |
| 166 Generate_TypedArrayPrototypeIterationMethod<IterationKind::kKeys>( | 166 Generate_TypedArrayPrototypeIterationMethod<IterationKind::kKeys>( |
| 167 state, "%TypedArray%.prototype.keys()"); | 167 state, "%TypedArray%.prototype.keys()"); |
| 168 } | 168 } |
| 169 | 169 |
| 170 namespace { | |
| 171 | |
| 172 MaybeHandle<JSTypedArray> ValiadateTypedArray(Isolate* isolate, | |
| 173 Handle<Object> receiver, | |
| 174 const char* method_name) { | |
| 175 if (V8_UNLIKELY(!receiver->IsJSTypedArray())) { | |
| 176 const MessageTemplate::Template message = MessageTemplate::kNotTypedArray; | |
| 177 THROW_NEW_ERROR(isolate, NewTypeError(message), JSTypedArray); | |
| 178 } | |
| 179 | |
| 180 Handle<JSTypedArray> array = Handle<JSTypedArray>::cast(receiver); | |
| 181 if (V8_UNLIKELY(array->WasNeutered())) { | |
| 182 const MessageTemplate::Template message = MessageTemplate::kNotTypedArray; | |
| 183 Handle<String> operation = | |
| 184 isolate->factory()->NewStringFromAsciiChecked(method_name); | |
| 185 THROW_NEW_ERROR(isolate, NewTypeError(message, operation), JSTypedArray); | |
| 186 } | |
| 187 | |
| 188 return array; | |
| 189 } | |
| 190 } // namespace | |
| 191 | |
| 192 BUILTIN(TypedArrayPrototypeCopyWithin) { | |
| 193 HandleScope scope(isolate); | |
| 194 | |
| 195 Handle<JSTypedArray> array; | |
| 196 const char* method = "%TypedArray%.prototype.copyWithin"; | |
| 197 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 198 isolate, array, ValiadateTypedArray(isolate, args.receiver(), method)); | |
| 199 | |
| 200 int64_t len = array->length_value(); | |
| 201 int64_t to = 0; | |
| 202 int64_t from = 0; | |
| 203 int64_t final = len; | |
| 204 | |
| 205 if (V8_LIKELY(args.length() > 1)) { | |
| 206 Handle<Object> target_int; | |
| 207 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 208 isolate, target_int, Object::ToInteger(isolate, args.at<Object>(1))); | |
| 209 int64_t relative = static_cast<int64_t>(target_int->Number()); | |
|
Benedikt Meurer
2017/02/06 14:35:23
I think this is wrong for infinities (also below).
Camillo Bruni
2017/02/06 14:43:35
nit: only if you feel motivated ;) could you add a
caitp
2017/02/06 14:44:40
That's a good point, didn't realize ToInteger() ke
caitp
2017/02/06 14:57:12
Because of the issue with -Infiity/+Infinity, we'd
caitp
2017/02/06 16:27:53
Tests added, and I've tried to add a new helper fo
| |
| 210 | |
| 211 to = relative < 0 ? std::max<int64_t>(relative + len, 0) | |
| 212 : std::min<int64_t>(relative, len); | |
| 213 | |
| 214 if (args.length() > 2) { | |
| 215 Handle<Object> start; | |
| 216 Handle<Object> end; | |
| 217 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( | |
| 218 isolate, start, Object::ToInteger(isolate, args.at<Object>(2))); | |
| 219 relative = static_cast<int64_t>(start->Number()); | |
|
Camillo Bruni
2017/02/06 14:43:35
ditto.
| |
| 220 from = relative < 0 ? std::max<int64_t>(relative + len, 0) | |
| 221 : std::min<int64_t>(relative, len); | |
| 222 | |
| 223 end = args.atOrUndefined(isolate, 3); | |
| 224 if (!end->IsUndefined(isolate)) { | |
| 225 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, end, | |
| 226 Object::ToInteger(isolate, end)); | |
| 227 relative = static_cast<int64_t>(end->Number()); | |
|
Camillo Bruni
2017/02/06 14:43:35
ditto.
| |
| 228 | |
| 229 final = relative < 0 ? std::max<int64_t>(relative + len, 0) | |
| 230 : std::min<int64_t>(relative, len); | |
| 231 } | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 int64_t count = std::min<int64_t>(final - from, len - to); | |
| 236 if (count <= 0) return *array; | |
| 237 | |
| 238 Handle<FixedTypedArrayBase> elements( | |
| 239 FixedTypedArrayBase::cast(array->elements())); | |
| 240 size_t element_size = array->element_size(); | |
| 241 to = to * element_size; | |
| 242 from = from * element_size; | |
| 243 count = count * element_size; | |
| 244 | |
| 245 uint8_t* data = static_cast<uint8_t*>(elements->DataPtr()); | |
| 246 std::memmove(data + to, data + from, count); | |
| 247 | |
| 248 return *array; | |
| 249 } | |
| 250 | |
| 170 } // namespace internal | 251 } // namespace internal |
| 171 } // namespace v8 | 252 } // namespace v8 |
| OLD | NEW |