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"
29 #include "ExceptionHelpers.h"
31 #include "JSFunction.h"
36 static EncodedJSValue JSC_HOST_CALL
arrayConstructorIsArray(ExecState
*);
40 #include "ArrayConstructor.lut.h"
44 const ClassInfo
ArrayConstructor::s_info
= { "Function", &InternalFunction::s_info
, 0, ExecState::arrayConstructorTable
};
46 /* Source for ArrayConstructor.lut.h
47 @begin arrayConstructorTable
48 isArray arrayConstructorIsArray DontEnum|Function 1
52 ASSERT_CLASS_FITS_IN_CELL(ArrayConstructor
);
54 ArrayConstructor::ArrayConstructor(ExecState
* exec
, JSGlobalObject
* globalObject
, Structure
* structure
, ArrayPrototype
* arrayPrototype
)
55 : InternalFunction(&exec
->globalData(), globalObject
, structure
, Identifier(exec
, arrayPrototype
->classInfo()->className
))
57 putDirectWithoutTransition(exec
->globalData(), exec
->propertyNames().prototype
, arrayPrototype
, DontEnum
| DontDelete
| ReadOnly
);
58 putDirectWithoutTransition(exec
->globalData(), exec
->propertyNames().length
, jsNumber(1), ReadOnly
| DontEnum
| DontDelete
);
61 bool ArrayConstructor::getOwnPropertySlot(ExecState
* exec
, const Identifier
& propertyName
, PropertySlot
&slot
)
63 return getStaticFunctionSlot
<InternalFunction
>(exec
, ExecState::arrayConstructorTable(exec
), this, propertyName
, slot
);
66 bool ArrayConstructor::getOwnPropertyDescriptor(ExecState
* exec
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
68 return getStaticFunctionDescriptor
<InternalFunction
>(exec
, ExecState::arrayConstructorTable(exec
), this, propertyName
, descriptor
);
71 // ------------------------------ Functions ---------------------------
73 static inline JSObject
* constructArrayWithSizeQuirk(ExecState
* exec
, const ArgList
& args
)
75 JSGlobalObject
* globalObject
= asInternalFunction(exec
->callee())->globalObject();
77 // a single numeric argument denotes the array size (!)
78 if (args
.size() == 1 && args
.at(0).isNumber()) {
79 uint32_t n
= args
.at(0).toUInt32(exec
);
80 if (n
!= args
.at(0).toNumber(exec
))
81 return throwError(exec
, createRangeError(exec
, "Array size is not a small enough positive integer."));
82 return new (exec
) JSArray(exec
->globalData(), globalObject
->arrayStructure(), n
, CreateInitialized
);
85 // otherwise the array is constructed with the arguments in it
86 return new (exec
) JSArray(exec
->globalData(), globalObject
->arrayStructure(), args
);
89 static EncodedJSValue JSC_HOST_CALL
constructWithArrayConstructor(ExecState
* exec
)
92 return JSValue::encode(constructArrayWithSizeQuirk(exec
, args
));
95 ConstructType
ArrayConstructor::getConstructData(ConstructData
& constructData
)
97 constructData
.native
.function
= constructWithArrayConstructor
;
98 return ConstructTypeHost
;
101 static EncodedJSValue JSC_HOST_CALL
callArrayConstructor(ExecState
* exec
)
104 return JSValue::encode(constructArrayWithSizeQuirk(exec
, args
));
107 CallType
ArrayConstructor::getCallData(CallData
& callData
)
109 // equivalent to 'new Array(....)'
110 callData
.native
.function
= callArrayConstructor
;
114 EncodedJSValue JSC_HOST_CALL
arrayConstructorIsArray(ExecState
* exec
)
116 return JSValue::encode(jsBoolean(exec
->argument(0).inherits(&JSArray::s_info
)));