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
38namespace JSC {
39
40static EncodedJSValue JSC_HOST_CALL arrayBufferFuncIsView(JSGlobalObject*, CallFrame*);
41static EncodedJSValue JSC_HOST_CALL callArrayBuffer(JSGlobalObject*, CallFrame*);
42
43template<>
44const ClassInfo JSArrayBufferConstructor::s_info = {
45 "Function", &Base::s_info, nullptr, nullptr,
46 CREATE_METHOD_TABLE(JSArrayBufferConstructor)
47};
48
49template<>
50const ClassInfo JSSharedArrayBufferConstructor::s_info = {
51 "Function", &Base::s_info, nullptr, nullptr,
52 CREATE_METHOD_TABLE(JSSharedArrayBufferConstructor)
53};
54
55template<ArrayBufferSharingMode sharingMode>
56JSGenericArrayBufferConstructor<sharingMode>::JSGenericArrayBufferConstructor(VM& vm, Structure* structure)
57 : Base(vm, structure, callArrayBuffer, JSGenericArrayBufferConstructor<sharingMode>::constructArrayBuffer)
58{
59}
60
61template<ArrayBufferSharingMode sharingMode>
62void JSGenericArrayBufferConstructor<sharingMode>::finishCreation(VM& vm, JSArrayBufferPrototype* prototype, GetterSetter* speciesSymbol)
63{
64 Base::finishCreation(vm, arrayBufferSharingModeName(sharingMode), 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();
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
76template<ArrayBufferSharingMode sharingMode>
77EncodedJSValue JSC_HOST_CALL JSGenericArrayBufferConstructor<sharingMode>::constructArrayBuffer(JSGlobalObject* globalObject, CallFrame* callFrame)
78{
79 VM& vm = globalObject->vm();
80 auto scope = DECLARE_THROW_SCOPE(vm);
81
82 JSGenericArrayBufferConstructor* constructor = jsCast<JSGenericArrayBufferConstructor*>(callFrame->jsCallee());
83
84 Structure* arrayBufferStructure = InternalFunction::createSubclassStructure(globalObject, callFrame->jsCallee(), callFrame->newTarget(), constructor->globalObject()->arrayBufferStructure(sharingMode));
85 RETURN_IF_EXCEPTION(scope, { });
86
87 unsigned length;
88 if (callFrame->argumentCount()) {
89 length = callFrame->uncheckedArgument(0).toIndex(globalObject, "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(globalObject, 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
110template<ArrayBufferSharingMode sharingMode>
111Structure* JSGenericArrayBufferConstructor<sharingMode>::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
112{
113 return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
114}
115
116template<ArrayBufferSharingMode sharingMode>
117const ClassInfo* JSGenericArrayBufferConstructor<sharingMode>::info()
118{
119 return &JSGenericArrayBufferConstructor<sharingMode>::s_info;
120}
121
122static EncodedJSValue JSC_HOST_CALL callArrayBuffer(JSGlobalObject* globalObject, CallFrame*)
123{
124 VM& vm = globalObject->vm();
125 auto scope = DECLARE_THROW_SCOPE(vm);
126 return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(globalObject, scope, "ArrayBuffer"));
127}
128
129// ------------------------------ Functions --------------------------------
130
131// ECMA 24.1.3.1
132EncodedJSValue JSC_HOST_CALL arrayBufferFuncIsView(JSGlobalObject* globalObject, CallFrame* callFrame)
133{
134 return JSValue::encode(jsBoolean(jsDynamicCast<JSArrayBufferView*>(globalObject->vm(), callFrame->argument(0))));
135}
136
137// Instantiate JSGenericArrayBufferConstructors.
138template class JSGenericArrayBufferConstructor<ArrayBufferSharingMode::Shared>;
139template class JSGenericArrayBufferConstructor<ArrayBufferSharingMode::Default>;
140
141} // namespace JSC
142
143