2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008, 2011 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"
29 #include "CopiedSpaceInlines.h"
31 #include "ExceptionHelpers.h"
33 #include "JSFunction.h"
35 #include "JSCInlines.h"
39 static EncodedJSValue JSC_HOST_CALL
arrayConstructorIsArray(ExecState
*);
43 #include "ArrayConstructor.lut.h"
47 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ArrayConstructor
);
49 const ClassInfo
ArrayConstructor::s_info
= { "Function", &InternalFunction::s_info
, 0, ExecState::arrayConstructorTable
, CREATE_METHOD_TABLE(ArrayConstructor
) };
51 /* Source for ArrayConstructor.lut.h
52 @begin arrayConstructorTable
53 isArray arrayConstructorIsArray DontEnum|Function 1
57 ArrayConstructor::ArrayConstructor(VM
& vm
, Structure
* structure
)
58 : InternalFunction(vm
, structure
)
62 void ArrayConstructor::finishCreation(VM
& vm
, ArrayPrototype
* arrayPrototype
)
64 Base::finishCreation(vm
, arrayPrototype
->classInfo()->className
);
65 putDirectWithoutTransition(vm
, vm
.propertyNames
->prototype
, arrayPrototype
, DontEnum
| DontDelete
| ReadOnly
);
66 putDirectWithoutTransition(vm
, vm
.propertyNames
->length
, jsNumber(1), ReadOnly
| DontEnum
| DontDelete
);
69 bool ArrayConstructor::getOwnPropertySlot(JSObject
* object
, ExecState
* exec
, PropertyName propertyName
, PropertySlot
&slot
)
71 return getStaticFunctionSlot
<InternalFunction
>(exec
, ExecState::arrayConstructorTable(exec
->vm()), jsCast
<ArrayConstructor
*>(object
), propertyName
, slot
);
74 // ------------------------------ Functions ---------------------------
76 JSObject
* constructArrayWithSizeQuirk(ExecState
* exec
, ArrayAllocationProfile
* profile
, JSGlobalObject
* globalObject
, JSValue length
)
78 if (!length
.isNumber())
79 return constructArrayNegativeIndexed(exec
, profile
, globalObject
, &length
, 1);
81 uint32_t n
= length
.toUInt32(exec
);
82 if (n
!= length
.toNumber(exec
))
83 return exec
->vm().throwException(exec
, createRangeError(exec
, ASCIILiteral("Array size is not a small enough positive integer.")));
84 return constructEmptyArray(exec
, profile
, globalObject
, n
);
87 static inline JSObject
* constructArrayWithSizeQuirk(ExecState
* exec
, const ArgList
& args
)
89 JSGlobalObject
* globalObject
= asInternalFunction(exec
->callee())->globalObject();
91 // a single numeric argument denotes the array size (!)
93 return constructArrayWithSizeQuirk(exec
, 0, globalObject
, args
.at(0));
95 // otherwise the array is constructed with the arguments in it
96 return constructArray(exec
, 0, globalObject
, args
);
99 static EncodedJSValue JSC_HOST_CALL
constructWithArrayConstructor(ExecState
* exec
)
102 return JSValue::encode(constructArrayWithSizeQuirk(exec
, args
));
105 ConstructType
ArrayConstructor::getConstructData(JSCell
*, ConstructData
& constructData
)
107 constructData
.native
.function
= constructWithArrayConstructor
;
108 return ConstructTypeHost
;
111 static EncodedJSValue JSC_HOST_CALL
callArrayConstructor(ExecState
* exec
)
114 return JSValue::encode(constructArrayWithSizeQuirk(exec
, args
));
117 CallType
ArrayConstructor::getCallData(JSCell
*, CallData
& callData
)
119 // equivalent to 'new Array(....)'
120 callData
.native
.function
= callArrayConstructor
;
124 EncodedJSValue JSC_HOST_CALL
arrayConstructorIsArray(ExecState
* exec
)
126 return JSValue::encode(jsBoolean(exec
->argument(0).inherits(JSArray::info())));