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
3 // found in the LICENSE file.
4 
5 #include "src/builtins/builtins.h"
6 #include "src/builtins/builtins-utils.h"
7 
8 namespace v8 {
9 namespace internal {
10 
11 // -----------------------------------------------------------------------------
12 // ES6 section 21.1 ArrayBuffer Objects
13 
14 // ES6 section 24.1.2.1 ArrayBuffer ( length ) for the [[Call]] case.
BUILTIN(ArrayBufferConstructor)15 BUILTIN(ArrayBufferConstructor) {
16   HandleScope scope(isolate);
17   Handle<JSFunction> target = args.target();
18   DCHECK(*target == target->native_context()->array_buffer_fun() ||
19          *target == target->native_context()->shared_array_buffer_fun());
20   THROW_NEW_ERROR_RETURN_FAILURE(
21       isolate, NewTypeError(MessageTemplate::kConstructorNotFunction,
22                             handle(target->shared()->name(), isolate)));
23 }
24 
25 // ES6 section 24.1.2.1 ArrayBuffer ( length ) for the [[Construct]] case.
BUILTIN(ArrayBufferConstructor_ConstructStub)26 BUILTIN(ArrayBufferConstructor_ConstructStub) {
27   HandleScope scope(isolate);
28   Handle<JSFunction> target = args.target();
29   Handle<JSReceiver> new_target = Handle<JSReceiver>::cast(args.new_target());
30   Handle<Object> length = args.atOrUndefined(isolate, 1);
31   DCHECK(*target == target->native_context()->array_buffer_fun() ||
32          *target == target->native_context()->shared_array_buffer_fun());
33   Handle<Object> number_length;
34   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_length,
35                                      Object::ToInteger(isolate, length));
36   if (number_length->Number() < 0.0) {
37     THROW_NEW_ERROR_RETURN_FAILURE(
38         isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength));
39   }
40   Handle<JSObject> result;
41   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
42                                      JSObject::New(target, new_target));
43   size_t byte_length;
44   if (!TryNumberToSize(*number_length, &byte_length)) {
45     THROW_NEW_ERROR_RETURN_FAILURE(
46         isolate, NewRangeError(MessageTemplate::kInvalidArrayBufferLength));
47   }
48   SharedFlag shared_flag =
49       (*target == target->native_context()->array_buffer_fun())
50           ? SharedFlag::kNotShared
51           : SharedFlag::kShared;
52   if (!JSArrayBuffer::SetupAllocatingData(Handle<JSArrayBuffer>::cast(result),
53                                           isolate, byte_length, true,
54                                           shared_flag)) {
55     THROW_NEW_ERROR_RETURN_FAILURE(
56         isolate, NewRangeError(MessageTemplate::kArrayBufferAllocationFailed));
57   }
58   return *result;
59 }
60 
61 // ES6 section 24.1.4.1 get ArrayBuffer.prototype.byteLength
BUILTIN(ArrayBufferPrototypeGetByteLength)62 BUILTIN(ArrayBufferPrototypeGetByteLength) {
63   HandleScope scope(isolate);
64   CHECK_RECEIVER(JSArrayBuffer, array_buffer,
65                  "get ArrayBuffer.prototype.byteLength");
66 
67   if (array_buffer->is_shared()) {
68     THROW_NEW_ERROR_RETURN_FAILURE(
69         isolate, NewTypeError(MessageTemplate::kIncompatibleMethodReceiver,
70                               isolate->factory()->NewStringFromAsciiChecked(
71                                   "get ArrayBuffer.prototype.byteLength"),
72                               args.receiver()));
73   }
74   // TODO(franzih): According to the ES6 spec, we should throw a TypeError
75   // here if the JSArrayBuffer is detached.
76   return array_buffer->byte_length();
77 }
78 
79 // ES6 section 24.1.3.1 ArrayBuffer.isView ( arg )
BUILTIN(ArrayBufferIsView)80 BUILTIN(ArrayBufferIsView) {
81   SealHandleScope shs(isolate);
82   DCHECK_EQ(2, args.length());
83   Object* arg = args[1];
84   return isolate->heap()->ToBoolean(arg->IsJSArrayBufferView());
85 }
86 
87 }  // namespace internal
88 }  // namespace v8
89