| 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 "ArrayBufferView.h" |
| 30 | #include "DataView.h" |
| 31 | #include "Error.h" |
| 32 | #include "JSCInlines.h" |
| 33 | #include "TypeError.h" |
| 34 | |
| 35 | namespace JSC { |
| 36 | |
| 37 | const ClassInfo JSDataView::s_info = { |
| 38 | "DataView" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSDataView)}; |
| 39 | |
| 40 | JSDataView::JSDataView(VM& vm, ConstructionContext& context, ArrayBuffer* buffer) |
| 41 | : Base(vm, context) |
| 42 | , m_buffer(buffer) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | JSDataView* JSDataView::create( |
| 47 | ExecState* exec, Structure* structure, RefPtr<ArrayBuffer>&& buffer, |
| 48 | unsigned byteOffset, unsigned byteLength) |
| 49 | { |
| 50 | VM& vm = exec->vm(); |
| 51 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 52 | |
| 53 | ASSERT(buffer); |
| 54 | if (!ArrayBufferView::verifySubRangeLength(*buffer, byteOffset, byteLength, sizeof(uint8_t))) { |
| 55 | throwVMError(exec, scope, createRangeError(exec, "Length out of range of buffer"_s )); |
| 56 | return nullptr; |
| 57 | } |
| 58 | if (!ArrayBufferView::verifyByteOffsetAlignment(byteOffset, sizeof(uint8_t))) { |
| 59 | throwException(exec, scope, createRangeError(exec, "Byte offset is not aligned"_s )); |
| 60 | return nullptr; |
| 61 | } |
| 62 | ConstructionContext context( |
| 63 | structure, buffer.copyRef(), byteOffset, byteLength, ConstructionContext::DataView); |
| 64 | ASSERT(context); |
| 65 | JSDataView* result = |
| 66 | new (NotNull, allocateCell<JSDataView>(vm.heap)) JSDataView(vm, context, buffer.get()); |
| 67 | result->finishCreation(vm); |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | JSDataView* JSDataView::createUninitialized(ExecState*, Structure*, unsigned) |
| 72 | { |
| 73 | UNREACHABLE_FOR_PLATFORM(); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | JSDataView* JSDataView::create(ExecState*, Structure*, unsigned) |
| 78 | { |
| 79 | UNREACHABLE_FOR_PLATFORM(); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | bool JSDataView::set(ExecState*, unsigned, JSObject*, unsigned, unsigned) |
| 84 | { |
| 85 | UNREACHABLE_FOR_PLATFORM(); |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | bool JSDataView::setIndex(ExecState*, unsigned, JSValue) |
| 90 | { |
| 91 | UNREACHABLE_FOR_PLATFORM(); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | RefPtr<DataView> JSDataView::possiblySharedTypedImpl() |
| 96 | { |
| 97 | return DataView::create(possiblySharedBuffer(), byteOffset(), length()); |
| 98 | } |
| 99 | |
| 100 | RefPtr<DataView> JSDataView::unsharedTypedImpl() |
| 101 | { |
| 102 | return DataView::create(unsharedBuffer(), byteOffset(), length()); |
| 103 | } |
| 104 | |
| 105 | bool JSDataView::getOwnPropertySlot( |
| 106 | JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot& slot) |
| 107 | { |
| 108 | VM& vm = exec->vm(); |
| 109 | JSDataView* thisObject = jsCast<JSDataView*>(object); |
| 110 | if (propertyName == vm.propertyNames->byteLength) { |
| 111 | slot.setValue(thisObject, PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly, jsNumber(thisObject->m_length)); |
| 112 | return true; |
| 113 | } |
| 114 | if (propertyName == vm.propertyNames->byteOffset) { |
| 115 | slot.setValue(thisObject, PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly, jsNumber(thisObject->byteOffset())); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | return Base::getOwnPropertySlot(thisObject, exec, propertyName, slot); |
| 120 | } |
| 121 | |
| 122 | bool JSDataView::put( |
| 123 | JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, |
| 124 | PutPropertySlot& slot) |
| 125 | { |
| 126 | VM& vm = exec->vm(); |
| 127 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 128 | JSDataView* thisObject = jsCast<JSDataView*>(cell); |
| 129 | |
| 130 | if (UNLIKELY(isThisValueAltered(slot, thisObject))) |
| 131 | RELEASE_AND_RETURN(scope, ordinarySetSlow(exec, thisObject, propertyName, value, slot.thisValue(), slot.isStrictMode())); |
| 132 | |
| 133 | if (propertyName == vm.propertyNames->byteLength |
| 134 | || propertyName == vm.propertyNames->byteOffset) |
| 135 | return typeError(exec, scope, slot.isStrictMode(), "Attempting to write to read-only typed array property."_s ); |
| 136 | |
| 137 | RELEASE_AND_RETURN(scope, Base::put(thisObject, exec, propertyName, value, slot)); |
| 138 | } |
| 139 | |
| 140 | bool JSDataView::defineOwnProperty( |
| 141 | JSObject* object, ExecState* exec, PropertyName propertyName, |
| 142 | const PropertyDescriptor& descriptor, bool shouldThrow) |
| 143 | { |
| 144 | VM& vm = exec->vm(); |
| 145 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 146 | JSDataView* thisObject = jsCast<JSDataView*>(object); |
| 147 | if (propertyName == vm.propertyNames->byteLength |
| 148 | || propertyName == vm.propertyNames->byteOffset) |
| 149 | return typeError(exec, scope, shouldThrow, "Attempting to define read-only typed array property."_s ); |
| 150 | |
| 151 | RELEASE_AND_RETURN(scope, Base::defineOwnProperty(thisObject, exec, propertyName, descriptor, shouldThrow)); |
| 152 | } |
| 153 | |
| 154 | bool JSDataView::deleteProperty( |
| 155 | JSCell* cell, ExecState* exec, PropertyName propertyName) |
| 156 | { |
| 157 | VM& vm = exec->vm(); |
| 158 | JSDataView* thisObject = jsCast<JSDataView*>(cell); |
| 159 | if (propertyName == vm.propertyNames->byteLength |
| 160 | || propertyName == vm.propertyNames->byteOffset) |
| 161 | return false; |
| 162 | |
| 163 | return Base::deleteProperty(thisObject, exec, propertyName); |
| 164 | } |
| 165 | |
| 166 | void JSDataView::getOwnNonIndexPropertyNames( |
| 167 | JSObject* object, ExecState* exec, PropertyNameArray& array, EnumerationMode mode) |
| 168 | { |
| 169 | VM& vm = exec->vm(); |
| 170 | JSDataView* thisObject = jsCast<JSDataView*>(object); |
| 171 | |
| 172 | if (mode.includeDontEnumProperties()) { |
| 173 | array.add(vm.propertyNames->byteOffset); |
| 174 | array.add(vm.propertyNames->byteLength); |
| 175 | } |
| 176 | |
| 177 | Base::getOwnNonIndexPropertyNames(thisObject, exec, array, mode); |
| 178 | } |
| 179 | |
| 180 | Structure* JSDataView::createStructure( |
| 181 | VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
| 182 | { |
| 183 | return Structure::create( |
| 184 | vm, globalObject, prototype, TypeInfo(DataViewType, StructureFlags), info(), |
| 185 | NonArray); |
| 186 | } |
| 187 | |
| 188 | } // namespace JSC |
| 189 | |
| 190 | |