builtins-dataview.cc 4.4 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/builtins/builtins-utils-inl.h"
6
#include "src/builtins/builtins.h"
7
#include "src/execution/isolate.h"
8
#include "src/heap/factory.h"
9
#include "src/logging/counters.h"
10
#include "src/numbers/conversions.h"
11
#include "src/objects/js-array-buffer-inl.h"
12
#include "src/objects/objects-inl.h"
13 14 15 16 17

namespace v8 {
namespace internal {

// -----------------------------------------------------------------------------
18
// ES #sec-dataview-objects
19

20
// ES #sec-dataview-constructor
21 22
BUILTIN(DataViewConstructor) {
  HandleScope scope(isolate);
23
  if (args.new_target()->IsUndefined(isolate)) {  // [[Call]]
24
    THROW_NEW_ERROR_RETURN_FAILURE(
25 26 27
        isolate, NewTypeError(MessageTemplate::kConstructorNotFunction,
                              isolate->factory()->NewStringFromAsciiChecked(
                                  "DataView")));
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  }
  // [[Construct]]
  Handle<JSFunction> target = args.target();
  Handle<JSReceiver> new_target = Handle<JSReceiver>::cast(args.new_target());
  Handle<Object> buffer = args.atOrUndefined(isolate, 1);
  Handle<Object> byte_offset = args.atOrUndefined(isolate, 2);
  Handle<Object> byte_length = args.atOrUndefined(isolate, 3);

  // 2. If Type(buffer) is not Object, throw a TypeError exception.
  // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a
  //    TypeError exception.
  if (!buffer->IsJSArrayBuffer()) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer));
  }
  Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer);
44

45 46
  // 4. Let offset be ? ToIndex(byteOffset).
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
47
      isolate, byte_offset,
48
      Object::ToIndex(isolate, byte_offset, MessageTemplate::kInvalidOffset));
49
  size_t view_byte_offset = byte_offset->Number();
50

51 52
  // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  //    We currently violate the specification at this point. TODO: Fix that.
53

54 55
  // 6. Let bufferByteLength be the value of buffer's
  // [[ArrayBufferByteLength]] internal slot.
56
  size_t const buffer_byte_length = array_buffer->byte_length();
57

58
  // 7. If offset > bufferByteLength, throw a RangeError exception.
59
  if (view_byte_offset > buffer_byte_length) {
60
    THROW_NEW_ERROR_RETURN_FAILURE(
61
        isolate, NewRangeError(MessageTemplate::kInvalidOffset, byte_offset));
62
  }
63

64
  size_t view_byte_length;
65 66 67
  if (byte_length->IsUndefined(isolate)) {
    // 8. If byteLength is either not present or undefined, then
    //       a. Let viewByteLength be bufferByteLength - offset.
68
    view_byte_length = buffer_byte_length - view_byte_offset;
69 70 71 72 73 74
  } else {
    // 9. Else,
    //       a. Let viewByteLength be ? ToIndex(byteLength).
    //       b. If offset+viewByteLength > bufferByteLength, throw a
    //          RangeError exception.
    ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
75
        isolate, byte_length,
76 77
        Object::ToIndex(isolate, byte_length,
                        MessageTemplate::kInvalidDataViewLength));
78
    if (view_byte_offset + byte_length->Number() > buffer_byte_length) {
79 80
      THROW_NEW_ERROR_RETURN_FAILURE(
          isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength));
81
    }
82
    view_byte_length = byte_length->Number();
83
  }
84

85 86 87 88
  // 10. Let O be ? OrdinaryCreateFromConstructor(NewTarget,
  //     "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]],
  //     [[ByteLength]], [[ByteOffset]]»).
  Handle<JSObject> result;
89 90 91
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      isolate, result,
      JSObject::New(target, new_target, Handle<AllocationSite>::null()));
92 93 94
  for (int i = 0; i < ArrayBufferView::kEmbedderFieldCount; ++i) {
    Handle<JSDataView>::cast(result)->SetEmbedderField(i, Smi::kZero);
  }
95

96 97
  // 11. Set O's [[ViewedArrayBuffer]] internal slot to buffer.
  Handle<JSDataView>::cast(result)->set_buffer(*array_buffer);
98

99
  // 12. Set O's [[ByteLength]] internal slot to viewByteLength.
100
  Handle<JSDataView>::cast(result)->set_byte_length(view_byte_length);
101

102
  // 13. Set O's [[ByteOffset]] internal slot to offset.
103
  Handle<JSDataView>::cast(result)->set_byte_offset(view_byte_offset);
104 105
  Handle<JSDataView>::cast(result)->set_data_pointer(
      static_cast<uint8_t*>(array_buffer->backing_store()) + view_byte_offset);
106

107 108
  // 14. Return O.
  return *result;
109 110 111 112
}

}  // namespace internal
}  // namespace v8