1/* 2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) 3 * Copyright (C) 2003, 2007, 2008 Apple Inc. All rights reserved. 4 * Copyright (C) 2003 Peter Kelly (pmk@post.com) 5 * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com) 6 * 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. 11 * 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. 16 * 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 20 * USA 21 * 22 */ 23 24#include "config.h" 25#include "ArrayConstructor.h" 26 27#include "ArrayPrototype.h" 28#include "Error.h" 29#include "ExceptionHelpers.h" 30#include "JSArray.h" 31#include "JSFunction.h" 32#include "Lookup.h" 33 34namespace JSC { 35 36ASSERT_CLASS_FITS_IN_CELL(ArrayConstructor); 37 38static EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState*); 39 40ArrayConstructor::ArrayConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, ArrayPrototype* arrayPrototype, Structure* functionStructure) 41 : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, arrayPrototype->classInfo()->className)) 42{ 43 // ECMA 15.4.3.1 Array.prototype 44 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly); 45 46 // no. of arguments for constructor 47 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete); 48 49 // ES5 50 putDirectFunctionWithoutTransition(exec, new (exec) JSFunction(exec, globalObject, functionStructure, 1, exec->propertyNames().isArray, arrayConstructorIsArray), DontEnum); 51} 52 53static inline JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args) 54{ 55 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject(); 56 57 // a single numeric argument denotes the array size (!) 58 if (args.size() == 1 && args.at(0).isNumber()) { 59 uint32_t n = args.at(0).toUInt32(exec); 60 if (n != args.at(0).toNumber(exec)) 61 return throwError(exec, createRangeError(exec, "Array size is not a small enough positive integer.")); 62 return new (exec) JSArray(exec->globalData(), globalObject->arrayStructure(), n, CreateInitialized); 63 } 64 65 // otherwise the array is constructed with the arguments in it 66 return new (exec) JSArray(exec->globalData(), globalObject->arrayStructure(), args); 67} 68 69static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec) 70{ 71 ArgList args(exec); 72 return JSValue::encode(constructArrayWithSizeQuirk(exec, args)); 73} 74 75// ECMA 15.4.2 76ConstructType ArrayConstructor::getConstructData(ConstructData& constructData) 77{ 78 constructData.native.function = constructWithArrayConstructor; 79 return ConstructTypeHost; 80} 81 82static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec) 83{ 84 ArgList args(exec); 85 return JSValue::encode(constructArrayWithSizeQuirk(exec, args)); 86} 87 88// ECMA 15.6.1 89CallType ArrayConstructor::getCallData(CallData& callData) 90{ 91 // equivalent to 'new Array(....)' 92 callData.native.function = callArrayConstructor; 93 return CallTypeHost; 94} 95 96EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState* exec) 97{ 98 return JSValue::encode(jsBoolean(exec->argument(0).inherits(&JSArray::s_info))); 99} 100 101} // namespace JSC 102