| 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 "JSArrayBufferConstructor.h" |
| 28 | |
| 29 | #include "BuiltinNames.h" |
| 30 | #include "Error.h" |
| 31 | #include "ExceptionHelpers.h" |
| 32 | #include "GetterSetter.h" |
| 33 | #include "JSArrayBuffer.h" |
| 34 | #include "JSArrayBufferPrototype.h" |
| 35 | #include "JSGlobalObject.h" |
| 36 | #include "JSCInlines.h" |
| 37 | |
| 38 | namespace JSC { |
| 39 | |
| 40 | static EncodedJSValue JSC_HOST_CALL arrayBufferFuncIsView(ExecState*); |
| 41 | static EncodedJSValue JSC_HOST_CALL callArrayBuffer(ExecState*); |
| 42 | |
| 43 | template<> |
| 44 | const ClassInfo JSArrayBufferConstructor::s_info = { |
| 45 | "Function" , &Base::s_info, nullptr, nullptr, |
| 46 | CREATE_METHOD_TABLE(JSArrayBufferConstructor) |
| 47 | }; |
| 48 | |
| 49 | template<> |
| 50 | const ClassInfo JSSharedArrayBufferConstructor::s_info = { |
| 51 | "Function" , &Base::s_info, nullptr, nullptr, |
| 52 | CREATE_METHOD_TABLE(JSSharedArrayBufferConstructor) |
| 53 | }; |
| 54 | |
| 55 | template<ArrayBufferSharingMode sharingMode> |
| 56 | JSGenericArrayBufferConstructor<sharingMode>::JSGenericArrayBufferConstructor(VM& vm, Structure* structure) |
| 57 | : Base(vm, structure, callArrayBuffer, JSGenericArrayBufferConstructor<sharingMode>::constructArrayBuffer) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | template<ArrayBufferSharingMode sharingMode> |
| 62 | void JSGenericArrayBufferConstructor<sharingMode>::finishCreation(VM& vm, JSArrayBufferPrototype* prototype, GetterSetter* speciesSymbol) |
| 63 | { |
| 64 | Base::finishCreation(vm, arrayBufferSharingModeName(sharingMode), NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition); |
| 65 | putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); |
| 66 | putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly); |
| 67 | putDirectNonIndexAccessorWithoutTransition(vm, vm.propertyNames->speciesSymbol, speciesSymbol, PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum); |
| 68 | |
| 69 | if (sharingMode == ArrayBufferSharingMode::Default) { |
| 70 | JSGlobalObject* globalObject = this->globalObject(vm); |
| 71 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->isView, arrayBufferFuncIsView, static_cast<unsigned>(PropertyAttribute::DontEnum), 1); |
| 72 | JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().isViewPrivateName(), arrayBufferFuncIsView, static_cast<unsigned>(PropertyAttribute::DontEnum), 1); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | template<ArrayBufferSharingMode sharingMode> |
| 77 | EncodedJSValue JSC_HOST_CALL JSGenericArrayBufferConstructor<sharingMode>::constructArrayBuffer(ExecState* exec) |
| 78 | { |
| 79 | VM& vm = exec->vm(); |
| 80 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 81 | |
| 82 | JSGenericArrayBufferConstructor* constructor = jsCast<JSGenericArrayBufferConstructor*>(exec->jsCallee()); |
| 83 | |
| 84 | Structure* arrayBufferStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), constructor->globalObject(vm)->arrayBufferStructure(sharingMode)); |
| 85 | RETURN_IF_EXCEPTION(scope, { }); |
| 86 | |
| 87 | unsigned length; |
| 88 | if (exec->argumentCount()) { |
| 89 | length = exec->uncheckedArgument(0).toIndex(exec, "length" ); |
| 90 | RETURN_IF_EXCEPTION(scope, encodedJSValue()); |
| 91 | } else { |
| 92 | // Although the documentation doesn't say so, it is in fact correct to say |
| 93 | // "new ArrayBuffer()". The result is the same as allocating an array buffer |
| 94 | // with a zero length. |
| 95 | length = 0; |
| 96 | } |
| 97 | |
| 98 | auto buffer = ArrayBuffer::tryCreate(length, 1); |
| 99 | if (!buffer) |
| 100 | return JSValue::encode(throwOutOfMemoryError(exec, scope)); |
| 101 | |
| 102 | if (sharingMode == ArrayBufferSharingMode::Shared) |
| 103 | buffer->makeShared(); |
| 104 | ASSERT(sharingMode == buffer->sharingMode()); |
| 105 | |
| 106 | JSArrayBuffer* result = JSArrayBuffer::create(vm, arrayBufferStructure, WTFMove(buffer)); |
| 107 | return JSValue::encode(result); |
| 108 | } |
| 109 | |
| 110 | template<ArrayBufferSharingMode sharingMode> |
| 111 | Structure* JSGenericArrayBufferConstructor<sharingMode>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
| 112 | { |
| 113 | return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info()); |
| 114 | } |
| 115 | |
| 116 | template<ArrayBufferSharingMode sharingMode> |
| 117 | const ClassInfo* JSGenericArrayBufferConstructor<sharingMode>::info() |
| 118 | { |
| 119 | return &JSGenericArrayBufferConstructor<sharingMode>::s_info; |
| 120 | } |
| 121 | |
| 122 | static EncodedJSValue JSC_HOST_CALL callArrayBuffer(ExecState* exec) |
| 123 | { |
| 124 | VM& vm = exec->vm(); |
| 125 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 126 | return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "ArrayBuffer" )); |
| 127 | } |
| 128 | |
| 129 | // ------------------------------ Functions -------------------------------- |
| 130 | |
| 131 | // ECMA 24.1.3.1 |
| 132 | EncodedJSValue JSC_HOST_CALL arrayBufferFuncIsView(ExecState* exec) |
| 133 | { |
| 134 | return JSValue::encode(jsBoolean(jsDynamicCast<JSArrayBufferView*>(exec->vm(), exec->argument(0)))); |
| 135 | } |
| 136 | |
| 137 | // Instantiate JSGenericArrayBufferConstructors. |
| 138 | template class JSGenericArrayBufferConstructor<ArrayBufferSharingMode::Shared>; |
| 139 | template class JSGenericArrayBufferConstructor<ArrayBufferSharingMode::Default>; |
| 140 | |
| 141 | } // namespace JSC |
| 142 | |
| 143 | |