LCOV - code coverage report
Current view: top level - src/builtins - builtins-dataview.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 23 23 100.0 %
Date: 2019-04-18 Functions: 3 4 75.0 %

          Line data    Source code
       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-utils-inl.h"
       6             : #include "src/builtins/builtins.h"
       7             : #include "src/conversions.h"
       8             : #include "src/counters.h"
       9             : #include "src/heap/factory.h"
      10             : #include "src/isolate.h"
      11             : #include "src/objects-inl.h"
      12             : #include "src/objects/js-array-buffer-inl.h"
      13             : 
      14             : namespace v8 {
      15             : namespace internal {
      16             : 
      17             : // -----------------------------------------------------------------------------
      18             : // ES #sec-dataview-objects
      19             : 
      20             : // ES #sec-dataview-constructor
      21       20685 : BUILTIN(DataViewConstructor) {
      22             :   HandleScope scope(isolate);
      23        4137 :   if (args.new_target()->IsUndefined(isolate)) {  // [[Call]]
      24          81 :     THROW_NEW_ERROR_RETURN_FAILURE(
      25             :         isolate, NewTypeError(MessageTemplate::kConstructorNotFunction,
      26             :                               isolate->factory()->NewStringFromAsciiChecked(
      27             :                                   "DataView")));
      28             :   }
      29             :   // [[Construct]]
      30        4110 :   Handle<JSFunction> target = args.target();
      31        4110 :   Handle<JSReceiver> new_target = Handle<JSReceiver>::cast(args.new_target());
      32             :   Handle<Object> buffer = args.atOrUndefined(isolate, 1);
      33             :   Handle<Object> byte_offset = args.atOrUndefined(isolate, 2);
      34             :   Handle<Object> byte_length = args.atOrUndefined(isolate, 3);
      35             : 
      36             :   // 2. If Type(buffer) is not Object, throw a TypeError exception.
      37             :   // 3. If buffer does not have an [[ArrayBufferData]] internal slot, throw a
      38             :   //    TypeError exception.
      39        4110 :   if (!buffer->IsJSArrayBuffer()) {
      40         108 :     THROW_NEW_ERROR_RETURN_FAILURE(
      41             :         isolate, NewTypeError(MessageTemplate::kDataViewNotArrayBuffer));
      42             :   }
      43             :   Handle<JSArrayBuffer> array_buffer = Handle<JSArrayBuffer>::cast(buffer);
      44             : 
      45             :   // 4. Let offset be ? ToIndex(byteOffset).
      46        8130 :   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      47             :       isolate, byte_offset,
      48             :       Object::ToIndex(isolate, byte_offset, MessageTemplate::kInvalidOffset));
      49        4038 :   size_t view_byte_offset = byte_offset->Number();
      50             : 
      51             :   // 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
      52             :   //    We currently violate the specification at this point. TODO: Fix that.
      53             : 
      54             :   // 6. Let bufferByteLength be the value of buffer's
      55             :   // [[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        4038 :   if (view_byte_offset > buffer_byte_length) {
      60          36 :     THROW_NEW_ERROR_RETURN_FAILURE(
      61             :         isolate, NewRangeError(MessageTemplate::kInvalidOffset, byte_offset));
      62             :   }
      63             : 
      64             :   size_t view_byte_length;
      65        4020 :   if (byte_length->IsUndefined(isolate)) {
      66             :     // 8. If byteLength is either not present or undefined, then
      67             :     //       a. Let viewByteLength be bufferByteLength - offset.
      68         909 :     view_byte_length = buffer_byte_length - view_byte_offset;
      69             :   } else {
      70             :     // 9. Else,
      71             :     //       a. Let viewByteLength be ? ToIndex(byteLength).
      72             :     //       b. If offset+viewByteLength > bufferByteLength, throw a
      73             :     //          RangeError exception.
      74        6222 :     ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      75             :         isolate, byte_length,
      76             :         Object::ToIndex(isolate, byte_length,
      77             :                         MessageTemplate::kInvalidDataViewLength));
      78        6222 :     if (view_byte_offset + byte_length->Number() > buffer_byte_length) {
      79          54 :       THROW_NEW_ERROR_RETURN_FAILURE(
      80             :           isolate, NewRangeError(MessageTemplate::kInvalidDataViewLength));
      81             :     }
      82        3084 :     view_byte_length = byte_length->Number();
      83             :   }
      84             : 
      85             :   // 10. Let O be ? OrdinaryCreateFromConstructor(NewTarget,
      86             :   //     "%DataViewPrototype%", «[[DataView]], [[ViewedArrayBuffer]],
      87             :   //     [[ByteLength]], [[ByteOffset]]»).
      88             :   Handle<JSObject> result;
      89        7986 :   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      90             :       isolate, result,
      91             :       JSObject::New(target, new_target, Handle<AllocationSite>::null()));
      92       19965 :   for (int i = 0; i < ArrayBufferView::kEmbedderFieldCount; ++i) {
      93        7986 :     Handle<JSDataView>::cast(result)->SetEmbedderField(i, Smi::kZero);
      94             :   }
      95             : 
      96             :   // 11. Set O's [[ViewedArrayBuffer]] internal slot to buffer.
      97        7986 :   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             :   // 14. Return O.
     106        3993 :   return *result;
     107             : }
     108             : 
     109             : }  // namespace internal
     110      122036 : }  // namespace v8

Generated by: LCOV version 1.10