2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007-2008, 2011, 2016 Apple Inc. All rights reserved.
4 * Copyright (C) 2003 Peter Kelly (pmk@post.com)
5 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
25 #include "ArrayConstructor.h"
27 #include "ArrayPrototype.h"
28 #include "ButterflyInlines.h"
30 #include "ExceptionHelpers.h"
31 #include "GetterSetter.h"
33 #include "JSFunction.h"
35 #include "ProxyObject.h"
36 #include "JSCInlines.h"
38 #include "ArrayConstructor.lut.h"
42 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ArrayConstructor);
44 const ClassInfo ArrayConstructor::s_info = { "Function", &InternalFunction::s_info, &arrayConstructorTable, CREATE_METHOD_TABLE(ArrayConstructor) };
46 /* Source for ArrayConstructor.lut.h
47 @begin arrayConstructorTable
48 of JSBuiltin DontEnum|Function 0
49 from JSBuiltin DontEnum|Function 0
53 ArrayConstructor::ArrayConstructor(VM& vm, Structure* structure)
54 : InternalFunction(vm, structure)
58 void ArrayConstructor::finishCreation(VM& vm, JSGlobalObject* globalObject, ArrayPrototype* arrayPrototype, GetterSetter* speciesSymbol)
60 Base::finishCreation(vm, arrayPrototype->classInfo(vm)->className);
61 putDirectWithoutTransition(vm, vm.propertyNames->prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly);
62 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
63 putDirectNonIndexAccessor(vm, vm.propertyNames->speciesSymbol, speciesSymbol, Accessor | ReadOnly | DontEnum);
64 JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->isArray, arrayConstructorIsArrayCodeGenerator, DontEnum);
67 // ------------------------------ Functions ---------------------------
69 JSValue constructArrayWithSizeQuirk(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, JSValue length, JSValue newTarget)
72 auto scope = DECLARE_THROW_SCOPE(vm);
73 if (!length.isNumber()) {
75 return constructArrayNegativeIndexed(exec, profile, globalObject, &length, 1, newTarget);
78 uint32_t n = length.toUInt32(exec);
79 if (n != length.toNumber(exec))
80 return throwException(exec, scope, createRangeError(exec, ASCIILiteral("Array size is not a small enough positive integer.")));
82 return constructEmptyArray(exec, profile, globalObject, n, newTarget);
85 static inline JSValue constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args, JSValue newTarget)
87 JSGlobalObject* globalObject = asInternalFunction(exec->jsCallee())->globalObject();
89 // a single numeric argument denotes the array size (!)
91 return constructArrayWithSizeQuirk(exec, nullptr, globalObject, args.at(0), newTarget);
93 // otherwise the array is constructed with the arguments in it
94 return constructArray(exec, nullptr, globalObject, args, newTarget);
97 static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec)
100 return JSValue::encode(constructArrayWithSizeQuirk(exec, args, exec->newTarget()));
103 ConstructType ArrayConstructor::getConstructData(JSCell*, ConstructData& constructData)
105 constructData.native.function = constructWithArrayConstructor;
106 return ConstructType::Host;
109 static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec)
112 return JSValue::encode(constructArrayWithSizeQuirk(exec, args, JSValue()));
115 CallType ArrayConstructor::getCallData(JSCell*, CallData& callData)
117 // equivalent to 'new Array(....)'
118 callData.native.function = callArrayConstructor;
119 return CallType::Host;
122 static ALWAYS_INLINE bool isArraySlowInline(ExecState* exec, ProxyObject* proxy)
125 auto scope = DECLARE_THROW_SCOPE(vm);
128 if (proxy->isRevoked()) {
129 throwTypeError(exec, scope, ASCIILiteral("Array.isArray cannot be called on a Proxy that has been revoked"));
132 JSObject* argument = proxy->target();
134 if (argument->type() == ArrayType || argument->type() == DerivedArrayType)
137 if (argument->type() != ProxyObjectType)
140 proxy = jsCast<ProxyObject*>(argument);
143 ASSERT_NOT_REACHED();
146 bool isArraySlow(ExecState* exec, ProxyObject* argument)
148 return isArraySlowInline(exec, argument);
152 // https://tc39.github.io/ecma262/#sec-isarray
153 EncodedJSValue JSC_HOST_CALL arrayConstructorPrivateFuncIsArraySlow(ExecState* exec)
155 ASSERT(jsDynamicCast<ProxyObject*>(exec->vm(), exec->argument(0)));
156 return JSValue::encode(jsBoolean(isArraySlowInline(exec, jsCast<ProxyObject*>(exec->uncheckedArgument(0)))));
159 EncodedJSValue JSC_HOST_CALL arrayConstructorPrivateFuncIsArrayConstructor(ExecState* exec)
162 return JSValue::encode(jsBoolean(jsDynamicCast<ArrayConstructor*>(vm, exec->uncheckedArgument(0))));