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

Unified Diff: src/builtins/builtins-arraybuffer.cc

Issue 2697013009: Move ArrayBuffer.prototype.slice implementation to C++ (Closed)
Patch Set: merge master 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/arraybuffer.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/builtins/builtins-arraybuffer.cc
diff --git a/src/builtins/builtins-arraybuffer.cc b/src/builtins/builtins-arraybuffer.cc
index e82c3850afabbb54b174badbfeaff1c42a7d6da7..07824d402340a3086e51b70897fd82a1d4c0b533 100644
--- a/src/builtins/builtins-arraybuffer.cc
+++ b/src/builtins/builtins-arraybuffer.cc
@@ -11,6 +11,15 @@
namespace v8 {
namespace internal {
+#define CHECK_IS_NOT_SHARED_ARRAY_BUFFER(name, method) \
+ if (name->is_shared()) { \
+ THROW_NEW_ERROR_RETURN_FAILURE( \
+ isolate, \
+ NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, \
+ isolate->factory()->NewStringFromAsciiChecked(method), \
+ name)); \
+ }
+
// -----------------------------------------------------------------------------
// ES6 section 21.1 ArrayBuffer Objects
@@ -63,17 +72,10 @@ BUILTIN(ArrayBufferConstructor_ConstructStub) {
// ES6 section 24.1.4.1 get ArrayBuffer.prototype.byteLength
BUILTIN(ArrayBufferPrototypeGetByteLength) {
+ const char* const kMethodName = "get ArrayBuffer.prototype.byteLength";
HandleScope scope(isolate);
- CHECK_RECEIVER(JSArrayBuffer, array_buffer,
- "get ArrayBuffer.prototype.byteLength");
-
- if (array_buffer->is_shared()) {
- THROW_NEW_ERROR_RETURN_FAILURE(
- isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
- isolate->factory()->NewStringFromAsciiChecked(
- "get ArrayBuffer.prototype.byteLength"),
- args.receiver()));
- }
+ CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
+ CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName);
// TODO(franzih): According to the ES6 spec, we should throw a TypeError
// here if the JSArrayBuffer is detached.
return array_buffer->byte_length();
@@ -87,5 +89,154 @@ BUILTIN(ArrayBufferIsView) {
return isolate->heap()->ToBoolean(arg->IsJSArrayBufferView());
}
+// ES #sec-arraybuffer.prototype.slice
+// ArrayBuffer.prototype.slice ( start, end )
+BUILTIN(ArrayBufferPrototypeSlice) {
+ const char* const kMethodName = "ArrayBuffer.prototype.slice";
+ HandleScope scope(isolate);
+ Handle<Object> start = args.at(1);
+ Handle<Object> end = args.atOrUndefined(isolate, 2);
+
+ // 2. If Type(O) is not Object, throw a TypeError exception.
+ // 3. If O does not have an [[ArrayBufferData]] internal slot, throw a
+ // TypeError exception.
+ CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
+ // 4. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
+ CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName);
+
+ // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
+ if (array_buffer->was_neutered()) {
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError(MessageTemplate::kDetachedOperation,
+ isolate->factory()->NewStringFromAsciiChecked(
+ kMethodName)));
+ }
+
+ // 6. Let len be O.[[ArrayBufferByteLength]].
+ double const len = array_buffer->byte_length()->Number();
+
+ // 7. Let relativeStart be ? ToInteger(start).
+ Handle<Object> relative_start;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_start,
+ Object::ToInteger(isolate, start));
+
+ // 8. If relativeStart < 0, let first be max((len + relativeStart), 0); else
+ // let first be min(relativeStart, len).
+ double const first = (relative_start->Number() < 0)
+ ? Max(len + relative_start->Number(), 0.0)
+ : Min(relative_start->Number(), len);
+ Handle<Object> first_obj = isolate->factory()->NewNumber(first);
+
+ // 9. If end is undefined, let relativeEnd be len; else let relativeEnd be ?
+ // ToInteger(end).
+ double relative_end;
+ if (end->IsUndefined(isolate)) {
+ relative_end = len;
+ } else {
+ Handle<Object> relative_end_obj;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_end_obj,
+ Object::ToInteger(isolate, end));
+ relative_end = relative_end_obj->Number();
+ }
+
+ // 10. If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let
+ // final be min(relativeEnd, len).
+ double const final_ = (relative_end < 0) ? Max(len + relative_end, 0.0)
+ : Min(relative_end, len);
+
+ // 11. Let newLen be max(final-first, 0).
+ double const new_len = Max(final_ - first, 0.0);
+ Handle<Object> new_len_obj = isolate->factory()->NewNumber(new_len);
+
+ // 12. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
+ Handle<JSFunction> arraybuffer_fun = isolate->array_buffer_fun();
+ Handle<Object> ctor;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, ctor,
+ Object::SpeciesConstructor(
+ isolate, Handle<JSReceiver>::cast(args.receiver()), arraybuffer_fun));
+
+ // 13. Let new be ? Construct(ctor, newLen).
+ Handle<JSReceiver> new_;
+ {
+ const int argc = 1;
+
+ ScopedVector<Handle<Object>> argv(argc);
+ argv[0] = new_len_obj;
+
+ Handle<Object> new_obj;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, new_obj,
+ Execution::New(Handle<JSFunction>::cast(ctor), argc, argv.start()));
+
+ new_ = Handle<JSReceiver>::cast(new_obj);
+ }
+
+ // 14. If new does not have an [[ArrayBufferData]] internal slot, throw a
+ // TypeError exception.
+ if (!new_->IsJSArrayBuffer()) {
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate,
+ NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
+ isolate->factory()->NewStringFromAsciiChecked(kMethodName),
+ new_));
+ }
+
+ // 15. If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
+ Handle<JSArrayBuffer> new_array_buffer = Handle<JSArrayBuffer>::cast(new_);
+ CHECK_IS_NOT_SHARED_ARRAY_BUFFER(new_array_buffer, kMethodName);
+
+ // 16. If IsDetachedBuffer(new) is true, throw a TypeError exception.
+ if (new_array_buffer->was_neutered()) {
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError(MessageTemplate::kDetachedOperation,
+ isolate->factory()->NewStringFromAsciiChecked(
+ kMethodName)));
+ }
+
+ // 17. If SameValue(new, O) is true, throw a TypeError exception.
+ if (new_->SameValue(*args.receiver())) {
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError(MessageTemplate::kArrayBufferSpeciesThis));
+ }
+
+ // 18. If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception.
+ if (new_array_buffer->byte_length()->Number() < new_len) {
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError(MessageTemplate::kArrayBufferTooShort));
+ }
+
+ // 19. NOTE: Side-effects of the above steps may have detached O.
+ // 20. If IsDetachedBuffer(O) is true, throw a TypeError exception.
+ if (array_buffer->was_neutered()) {
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError(MessageTemplate::kDetachedOperation,
+ isolate->factory()->NewStringFromAsciiChecked(
+ kMethodName)));
+ }
+
+ // 21. Let fromBuf be O.[[ArrayBufferData]].
+ // 22. Let toBuf be new.[[ArrayBufferData]].
+ // 23. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
+ size_t first_size = 0, new_len_size = 0;
+ CHECK(TryNumberToSize(*first_obj, &first_size));
+ CHECK(TryNumberToSize(*new_len_obj, &new_len_size));
+ DCHECK(NumberToSize(new_array_buffer->byte_length()) >= new_len_size);
+
+ if (new_len_size != 0) {
+ size_t from_byte_length = NumberToSize(array_buffer->byte_length());
+ USE(from_byte_length);
+ DCHECK(first_size <= from_byte_length);
+ DCHECK(from_byte_length - first_size >= new_len_size);
+ uint8_t* from_data =
+ reinterpret_cast<uint8_t*>(array_buffer->backing_store());
+ uint8_t* to_data =
+ reinterpret_cast<uint8_t*>(new_array_buffer->backing_store());
+ CopyBytes(to_data, from_data + first_size, new_len_size);
+ }
+
+ return *new_;
+}
+
} // namespace internal
} // namespace v8
« no previous file with comments | « src/builtins/builtins.h ('k') | src/js/arraybuffer.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698