ArrayConstructor.cpp [plain text]
#include "config.h"
#include "ArrayConstructor.h"
#include "ArrayPrototype.h"
#include "ButterflyInlines.h"
#include "Error.h"
#include "ExceptionHelpers.h"
#include "GetterSetter.h"
#include "JSArray.h"
#include "JSFunction.h"
#include "Lookup.h"
#include "ProxyObject.h"
#include "JSCInlines.h"
#include "ArrayConstructor.lut.h"
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ArrayConstructor);
const ClassInfo ArrayConstructor::s_info = { "Function", &InternalFunction::s_info, &arrayConstructorTable, nullptr, CREATE_METHOD_TABLE(ArrayConstructor) };
static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState*);
static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState*);
ArrayConstructor::ArrayConstructor(VM& vm, Structure* structure)
: InternalFunction(vm, structure, callArrayConstructor, constructWithArrayConstructor)
{
}
void ArrayConstructor::finishCreation(VM& vm, JSGlobalObject* globalObject, ArrayPrototype* arrayPrototype, GetterSetter* speciesSymbol)
{
Base::finishCreation(vm, arrayPrototype->classInfo(vm)->className);
putDirectWithoutTransition(vm, vm.propertyNames->prototype, arrayPrototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
putDirectNonIndexAccessor(vm, vm.propertyNames->speciesSymbol, speciesSymbol, PropertyAttribute::Accessor | PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum);
JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->isArray, arrayConstructorIsArrayCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
}
JSArray* constructArrayWithSizeQuirk(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, JSValue length, JSValue newTarget)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (!length.isNumber()) {
scope.release();
return constructArrayNegativeIndexed(exec, profile, globalObject, &length, 1, newTarget);
}
uint32_t n = length.toUInt32(exec);
if (n != length.toNumber(exec)) {
throwException(exec, scope, createRangeError(exec, "Array size is not a small enough positive integer."_s));
return nullptr;
}
scope.release();
return constructEmptyArray(exec, profile, globalObject, n, newTarget);
}
static inline JSArray* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args, JSValue newTarget)
{
VM& vm = exec->vm();
JSGlobalObject* globalObject = jsCast<InternalFunction*>(exec->jsCallee())->globalObject(vm);
if (args.size() == 1)
return constructArrayWithSizeQuirk(exec, nullptr, globalObject, args.at(0), newTarget);
return constructArray(exec, nullptr, globalObject, args, newTarget);
}
static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec)
{
ArgList args(exec);
return JSValue::encode(constructArrayWithSizeQuirk(exec, args, exec->newTarget()));
}
static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec)
{
ArgList args(exec);
return JSValue::encode(constructArrayWithSizeQuirk(exec, args, JSValue()));
}
static ALWAYS_INLINE bool isArraySlowInline(ExecState* exec, ProxyObject* proxy)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
while (true) {
if (proxy->isRevoked()) {
throwTypeError(exec, scope, "Array.isArray cannot be called on a Proxy that has been revoked"_s);
return false;
}
JSObject* argument = proxy->target();
if (argument->type() == ArrayType || argument->type() == DerivedArrayType)
return true;
if (argument->type() != ProxyObjectType)
return false;
proxy = jsCast<ProxyObject*>(argument);
}
ASSERT_NOT_REACHED();
}
bool isArraySlow(ExecState* exec, ProxyObject* argument)
{
return isArraySlowInline(exec, argument);
}
EncodedJSValue JSC_HOST_CALL arrayConstructorPrivateFuncIsArraySlow(ExecState* exec)
{
ASSERT(jsDynamicCast<ProxyObject*>(exec->vm(), exec->argument(0)));
return JSValue::encode(jsBoolean(isArraySlowInline(exec, jsCast<ProxyObject*>(exec->uncheckedArgument(0)))));
}
EncodedJSValue JSC_HOST_CALL arrayConstructorPrivateFuncIsArrayConstructor(ExecState* exec)
{
VM& vm = exec->vm();
return JSValue::encode(jsBoolean(jsDynamicCast<ArrayConstructor*>(vm, exec->uncheckedArgument(0))));
}
}