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

Side by Side Diff: src/builtins/builtins-typedarray.cc

Issue 2671233002: [typedarrays] move %TypedArray%.prototype.copyWithin to C++ (Closed)
Patch Set: alphabetize JSTests.json Created 3 years, 10 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 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
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 BUILTIN(TypedArrayPrototypeCopyWithin) {
171 HandleScope scope(isolate);
172 Handle<Object> receiver = args.receiver();
173 Handle<JSTypedArray> obj;
174
175 { // ValiadateTypedArray
176 if (V8_UNLIKELY(!receiver->IsJSTypedArray())) {
177 const MessageTemplate::Template message = MessageTemplate::kNotTypedArray;
178 THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(message));
179 }
180
181 obj = Handle<JSTypedArray>::cast(receiver);
182 if (V8_UNLIKELY(obj->WasNeutered())) {
183 const MessageTemplate::Template message =
184 MessageTemplate::kDetachedOperation;
185 Handle<String> operation = isolate->factory()->NewStringFromAsciiChecked(
186 "%TypedArray%.prototype.copyWithin");
187 THROW_NEW_ERROR_RETURN_FAILURE(isolate, NewTypeError(message, operation));
188 }
189 }
190
191 // Let len be O.[[ArrayLength]]
192 size_t len = obj->length_value();
Benedikt Meurer 2017/02/06 03:54:33 How about using int64_t for these to avoid sprinkl
caitp 2017/02/06 14:21:13 Done
193 size_t to = 0;
194 size_t from = 0;
195 size_t final = len;
196
197 if (V8_LIKELY(args.length() > 1)) {
198 Handle<Object> target_int;
199 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
200 isolate, target_int, Object::ToInteger(isolate, args.at<Object>(1)));
201 int64_t relative = static_cast<int64_t>(target_int->Number());
202
203 to = relative < 0
204 ? static_cast<size_t>(std::max<int64_t>(relative + len, 0))
205 : static_cast<size_t>(std::min<int64_t>(relative, len));
206
207 if (args.length() > 2) {
208 Handle<Object> start;
209 Handle<Object> end;
210 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
211 isolate, start, Object::ToInteger(isolate, args.at<Object>(2)));
212 relative = static_cast<int64_t>(start->Number());
213 from = relative < 0
214 ? static_cast<size_t>(std::max<int64_t>(relative + len, 0))
215 : static_cast<size_t>(std::min<int64_t>(relative, len));
216
217 end = args.atOrUndefined(isolate, 3);
218 if (!end->IsUndefined(isolate)) {
219 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, end,
220 Object::ToInteger(isolate, end));
221 relative = static_cast<int64_t>(end->Number());
222
223 final = relative < 0
224 ? static_cast<size_t>(std::max<int64_t>(relative + len, 0))
225 : static_cast<size_t>(std::min<int64_t>(relative, len));
226 }
227 }
228 }
229
230 int64_t lcount =
231 std::min(static_cast<int64_t>(final) - static_cast<int64_t>(from),
232 static_cast<int64_t>(len) - static_cast<int64_t>(to));
233 if (lcount <= 0) return *obj;
234
235 size_t count = static_cast<size_t>(lcount);
236 Handle<FixedTypedArrayBase> elements(
237 FixedTypedArrayBase::cast(obj->elements()));
238 size_t element_size = obj->element_size();
239
240 uint8_t* data = static_cast<uint8_t*>(elements->DataPtr());
241 std::memmove(data + (to * element_size), data + (from * element_size),
242 (count * element_size));
243
244 return *obj;
245 }
246
170 } // namespace internal 247 } // namespace internal
171 } // namespace v8 248 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698