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

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

Issue 2697013009: Move ArrayBuffer.prototype.slice implementation to C++ (Closed)
Patch Set: merge master Created 3 years, 9 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/arraybuffer.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/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
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 // ES #sec-arraybuffer.prototype.slice
93 // ArrayBuffer.prototype.slice ( start, end )
94 BUILTIN(ArrayBufferPrototypeSlice) {
95 const char* const kMethodName = "ArrayBuffer.prototype.slice";
96 HandleScope scope(isolate);
97 Handle<Object> start = args.at(1);
98 Handle<Object> end = args.atOrUndefined(isolate, 2);
99
100 // 2. If Type(O) is not Object, throw a TypeError exception.
101 // 3. If O does not have an [[ArrayBufferData]] internal slot, throw a
102 // TypeError exception.
103 CHECK_RECEIVER(JSArrayBuffer, array_buffer, kMethodName);
104 // 4. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
105 CHECK_IS_NOT_SHARED_ARRAY_BUFFER(array_buffer, kMethodName);
106
107 // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
108 if (array_buffer->was_neutered()) {
109 THROW_NEW_ERROR_RETURN_FAILURE(
110 isolate, NewTypeError(MessageTemplate::kDetachedOperation,
111 isolate->factory()->NewStringFromAsciiChecked(
112 kMethodName)));
113 }
114
115 // 6. Let len be O.[[ArrayBufferByteLength]].
116 double const len = array_buffer->byte_length()->Number();
117
118 // 7. Let relativeStart be ? ToInteger(start).
119 Handle<Object> relative_start;
120 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_start,
121 Object::ToInteger(isolate, start));
122
123 // 8. If relativeStart < 0, let first be max((len + relativeStart), 0); else
124 // let first be min(relativeStart, len).
125 double const first = (relative_start->Number() < 0)
126 ? Max(len + relative_start->Number(), 0.0)
127 : Min(relative_start->Number(), len);
128 Handle<Object> first_obj = isolate->factory()->NewNumber(first);
129
130 // 9. If end is undefined, let relativeEnd be len; else let relativeEnd be ?
131 // ToInteger(end).
132 double relative_end;
133 if (end->IsUndefined(isolate)) {
134 relative_end = len;
135 } else {
136 Handle<Object> relative_end_obj;
137 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, relative_end_obj,
138 Object::ToInteger(isolate, end));
139 relative_end = relative_end_obj->Number();
140 }
141
142 // 10. If relativeEnd < 0, let final be max((len + relativeEnd), 0); else let
143 // final be min(relativeEnd, len).
144 double const final_ = (relative_end < 0) ? Max(len + relative_end, 0.0)
145 : Min(relative_end, len);
146
147 // 11. Let newLen be max(final-first, 0).
148 double const new_len = Max(final_ - first, 0.0);
149 Handle<Object> new_len_obj = isolate->factory()->NewNumber(new_len);
150
151 // 12. Let ctor be ? SpeciesConstructor(O, %ArrayBuffer%).
152 Handle<JSFunction> arraybuffer_fun = isolate->array_buffer_fun();
153 Handle<Object> ctor;
154 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
155 isolate, ctor,
156 Object::SpeciesConstructor(
157 isolate, Handle<JSReceiver>::cast(args.receiver()), arraybuffer_fun));
158
159 // 13. Let new be ? Construct(ctor, newLen).
160 Handle<JSReceiver> new_;
161 {
162 const int argc = 1;
163
164 ScopedVector<Handle<Object>> argv(argc);
165 argv[0] = new_len_obj;
166
167 Handle<Object> new_obj;
168 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
169 isolate, new_obj,
170 Execution::New(Handle<JSFunction>::cast(ctor), argc, argv.start()));
171
172 new_ = Handle<JSReceiver>::cast(new_obj);
173 }
174
175 // 14. If new does not have an [[ArrayBufferData]] internal slot, throw a
176 // TypeError exception.
177 if (!new_->IsJSArrayBuffer()) {
178 THROW_NEW_ERROR_RETURN_FAILURE(
179 isolate,
180 NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
181 isolate->factory()->NewStringFromAsciiChecked(kMethodName),
182 new_));
183 }
184
185 // 15. If IsSharedArrayBuffer(new) is true, throw a TypeError exception.
186 Handle<JSArrayBuffer> new_array_buffer = Handle<JSArrayBuffer>::cast(new_);
187 CHECK_IS_NOT_SHARED_ARRAY_BUFFER(new_array_buffer, kMethodName);
188
189 // 16. If IsDetachedBuffer(new) is true, throw a TypeError exception.
190 if (new_array_buffer->was_neutered()) {
191 THROW_NEW_ERROR_RETURN_FAILURE(
192 isolate, NewTypeError(MessageTemplate::kDetachedOperation,
193 isolate->factory()->NewStringFromAsciiChecked(
194 kMethodName)));
195 }
196
197 // 17. If SameValue(new, O) is true, throw a TypeError exception.
198 if (new_->SameValue(*args.receiver())) {
199 THROW_NEW_ERROR_RETURN_FAILURE(
200 isolate, NewTypeError(MessageTemplate::kArrayBufferSpeciesThis));
201 }
202
203 // 18. If new.[[ArrayBufferByteLength]] < newLen, throw a TypeError exception.
204 if (new_array_buffer->byte_length()->Number() < new_len) {
205 THROW_NEW_ERROR_RETURN_FAILURE(
206 isolate, NewTypeError(MessageTemplate::kArrayBufferTooShort));
207 }
208
209 // 19. NOTE: Side-effects of the above steps may have detached O.
210 // 20. If IsDetachedBuffer(O) is true, throw a TypeError exception.
211 if (array_buffer->was_neutered()) {
212 THROW_NEW_ERROR_RETURN_FAILURE(
213 isolate, NewTypeError(MessageTemplate::kDetachedOperation,
214 isolate->factory()->NewStringFromAsciiChecked(
215 kMethodName)));
216 }
217
218 // 21. Let fromBuf be O.[[ArrayBufferData]].
219 // 22. Let toBuf be new.[[ArrayBufferData]].
220 // 23. Perform CopyDataBlockBytes(toBuf, 0, fromBuf, first, newLen).
221 size_t first_size = 0, new_len_size = 0;
222 CHECK(TryNumberToSize(*first_obj, &first_size));
223 CHECK(TryNumberToSize(*new_len_obj, &new_len_size));
224 DCHECK(NumberToSize(new_array_buffer->byte_length()) >= new_len_size);
225
226 if (new_len_size != 0) {
227 size_t from_byte_length = NumberToSize(array_buffer->byte_length());
228 USE(from_byte_length);
229 DCHECK(first_size <= from_byte_length);
230 DCHECK(from_byte_length - first_size >= new_len_size);
231 uint8_t* from_data =
232 reinterpret_cast<uint8_t*>(array_buffer->backing_store());
233 uint8_t* to_data =
234 reinterpret_cast<uint8_t*>(new_array_buffer->backing_store());
235 CopyBytes(to_data, from_data + first_size, new_len_size);
236 }
237
238 return *new_;
239 }
240
90 } // namespace internal 241 } // namespace internal
91 } // namespace v8 242 } // namespace v8
OLDNEW
« 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