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

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

Issue 2693753002: Revert of [typedarrays] move %TypedArray%.prototype.copyWithin to C++ (Closed)
Patch Set: Fix merge conflict 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
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/array.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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
191 int64_t CapRelativeIndex(Handle<Object> num, int64_t minimum, int64_t maximum) {
192 int64_t relative;
193 if (V8_LIKELY(num->IsSmi())) {
194 relative = Smi::cast(*num)->value();
195 } else {
196 DCHECK(num->IsHeapNumber());
197 double fp = HeapNumber::cast(*num)->value();
198 if (V8_UNLIKELY(!std::isfinite(fp))) {
199 // +Infinity / -Infinity
200 DCHECK(!std::isnan(fp));
201 return fp < 0 ? minimum : maximum;
202 }
203 relative = static_cast<int64_t>(fp);
204 }
205 return relative < 0 ? std::max<int64_t>(relative + maximum, minimum)
206 : std::min<int64_t>(relative, maximum);
207 }
208
209 } // namespace
210
211 BUILTIN(TypedArrayPrototypeCopyWithin) {
212 HandleScope scope(isolate);
213
214 Handle<JSTypedArray> array;
215 const char* method = "%TypedArray%.prototype.copyWithin";
216 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
217 isolate, array, ValiadateTypedArray(isolate, args.receiver(), method));
218
219 int64_t len = array->length_value();
220 int64_t to = 0;
221 int64_t from = 0;
222 int64_t final = len;
223
224 if (V8_LIKELY(args.length() > 1)) {
225 Handle<Object> num;
226 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
227 isolate, num, Object::ToInteger(isolate, args.at<Object>(1)));
228 to = CapRelativeIndex(num, 0, len);
229
230 if (args.length() > 2) {
231 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
232 isolate, num, Object::ToInteger(isolate, args.at<Object>(2)));
233 from = CapRelativeIndex(num, 0, len);
234
235 Handle<Object> end = args.atOrUndefined(isolate, 3);
236 if (!end->IsUndefined(isolate)) {
237 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, num,
238 Object::ToInteger(isolate, end));
239 final = CapRelativeIndex(num, 0, len);
240 }
241 }
242 }
243
244 int64_t count = std::min<int64_t>(final - from, len - to);
245 if (count <= 0) return *array;
246
247 Handle<FixedTypedArrayBase> elements(
248 FixedTypedArrayBase::cast(array->elements()));
249 size_t element_size = array->element_size();
250 to = to * element_size;
251 from = from * element_size;
252 count = count * element_size;
253
254 uint8_t* data = static_cast<uint8_t*>(elements->DataPtr());
255 std::memmove(data + to, data + from, count);
256
257 return *array;
258 }
259
260 } // namespace internal 170 } // namespace internal
261 } // namespace v8 171 } // namespace v8
OLDNEW
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/array.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698