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