/ runtime / JSDataView.cpp
JSDataView.cpp
  1  /*
  2   * Copyright (C) 2013-2016 Apple Inc. All rights reserved.
  3   *
  4   * Redistribution and use in source and binary forms, with or without
  5   * modification, are permitted provided that the following conditions
  6   * are met:
  7   * 1. Redistributions of source code must retain the above copyright
  8   *    notice, this list of conditions and the following disclaimer.
  9   * 2. Redistributions in binary form must reproduce the above copyright
 10   *    notice, this list of conditions and the following disclaimer in the
 11   *    documentation and/or other materials provided with the distribution.
 12   *
 13   * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 14   * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 15   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 16   * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 17   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 18   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 19   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 20   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 21   * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 22   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 23   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 24   */
 25  
 26  #include "config.h"
 27  #include "JSDataView.h"
 28  
 29  #include "JSCInlines.h"
 30  #include "TypeError.h"
 31  
 32  namespace JSC {
 33  
 34  const ClassInfo JSDataView::s_info = {
 35      "DataView", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSDataView)};
 36  
 37  JSDataView::JSDataView(VM& vm, ConstructionContext& context, ArrayBuffer* buffer)
 38      : Base(vm, context)
 39      , m_buffer(buffer)
 40  {
 41  }
 42  
 43  JSDataView* JSDataView::create(
 44      JSGlobalObject* globalObject, Structure* structure, RefPtr<ArrayBuffer>&& buffer,
 45      unsigned byteOffset, unsigned byteLength)
 46  {
 47      VM& vm = globalObject->vm();
 48      auto scope = DECLARE_THROW_SCOPE(vm);
 49  
 50      ASSERT(buffer);
 51      if (buffer->isDetached()) {
 52          throwTypeError(globalObject, scope, "Buffer is already detached"_s);
 53          return nullptr;
 54      }
 55      if (!ArrayBufferView::verifySubRangeLength(*buffer, byteOffset, byteLength, sizeof(uint8_t))) {
 56          throwRangeError(globalObject, scope, "Length out of range of buffer"_s);
 57          return nullptr;
 58      }
 59      if (!ArrayBufferView::verifyByteOffsetAlignment(byteOffset, sizeof(uint8_t))) {
 60          throwRangeError(globalObject, scope, "Byte offset is not aligned"_s);
 61          return nullptr;
 62      }
 63  
 64      ConstructionContext context(
 65          structure, buffer.copyRef(), byteOffset, byteLength, ConstructionContext::DataView);
 66      ASSERT(context);
 67      JSDataView* result =
 68          new (NotNull, allocateCell<JSDataView>(vm.heap)) JSDataView(vm, context, buffer.get());
 69      result->finishCreation(vm);
 70      return result;
 71  }
 72  
 73  JSDataView* JSDataView::createUninitialized(JSGlobalObject*, Structure*, unsigned)
 74  {
 75      UNREACHABLE_FOR_PLATFORM();
 76      return nullptr;
 77  }
 78  
 79  JSDataView* JSDataView::create(JSGlobalObject*, Structure*, unsigned)
 80  {
 81      UNREACHABLE_FOR_PLATFORM();
 82      return nullptr;
 83  }
 84  
 85  bool JSDataView::set(JSGlobalObject*, unsigned, JSObject*, unsigned, unsigned)
 86  {
 87      UNREACHABLE_FOR_PLATFORM();
 88      return false;
 89  }
 90  
 91  bool JSDataView::setIndex(JSGlobalObject*, unsigned, JSValue)
 92  {
 93      UNREACHABLE_FOR_PLATFORM();
 94      return false;
 95  }
 96  
 97  RefPtr<DataView> JSDataView::possiblySharedTypedImpl()
 98  {
 99      return DataView::create(possiblySharedBuffer(), byteOffset(), length());
100  }
101  
102  RefPtr<DataView> JSDataView::unsharedTypedImpl()
103  {
104      return DataView::create(unsharedBuffer(), byteOffset(), length());
105  }
106  
107  Structure* JSDataView::createStructure(
108      VM& vm, JSGlobalObject* globalObject, JSValue prototype)
109  {
110      return Structure::create(
111          vm, globalObject, prototype, TypeInfo(DataViewType, StructureFlags), info(),
112          NonArray);
113  }
114  
115  } // namespace JSC
116