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(JSGlobalObject* globalObject, Structure* structure)
55 : InternalFunction(globalObject, structure)
59 void ArrayConstructor::finishCreation(ExecState* exec, ArrayPrototype* arrayPrototype)
61 Base::finishCreation(exec->globalData(), Identifier(exec, arrayPrototype->classInfo()->className));
62 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly);
63 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
66 bool ArrayConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
68 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), this, propertyName, slot);
71 bool ArrayConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
73 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), this, propertyName, descriptor);
76 // ------------------------------ Functions ---------------------------
78 static inline JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args)
80 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
82 // a single numeric argument denotes the array size (!)
83 if (args.size() == 1 && args.at(0).isNumber()) {
84 uint32_t n = args.at(0).toUInt32(exec);
85 if (n != args.at(0).toNumber(exec))
86 return throwError(exec, createRangeError(exec, "Array size is not a small enough positive integer."));
87 return JSArray::create(exec->globalData(), globalObject->arrayStructure(), n, CreateInitialized);
90 // otherwise the array is constructed with the arguments in it
91 return JSArray::create(exec->globalData(), globalObject->arrayStructure(), args);
94 static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec)
97 return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
100 ConstructType ArrayConstructor::getConstructData(ConstructData& constructData)
102 constructData.native.function = constructWithArrayConstructor;
103 return ConstructTypeHost;
106 static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec)
109 return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
112 CallType ArrayConstructor::getCallData(CallData& callData)
114 // equivalent to 'new Array(....)'
115 callData.native.function = callArrayConstructor;
119 EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState* exec)
121 return JSValue::encode(jsBoolean(exec->argument(0).inherits(&JSArray::s_info)));