| 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 "JSArray.h" |
| 30 | #include "JSFunction.h" |
| 31 | #include "Lookup.h" |
| 32 | #include "PrototypeFunction.h" |
| 33 | |
| 34 | namespace JSC { |
| 35 | |
| 36 | ASSERT_CLASS_FITS_IN_CELL(ArrayConstructor); |
| 37 | |
| 38 | static JSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState*, JSObject*, JSValue, const ArgList&); |
| 39 | |
| 40 | ArrayConstructor::ArrayConstructor(ExecState* exec, NonNullPassRefPtr<Structure> structure, ArrayPrototype* arrayPrototype, Structure* prototypeFunctionStructure) |
| 41 | : InternalFunction(&exec->globalData(), structure, Identifier(exec, arrayPrototype->classInfo()->className)) |
| 42 | { |
| 43 | // ECMA 15.4.3.1 Array.prototype |
| 44 | putDirectWithoutTransition(propertyName: exec->propertyNames().prototype, value: arrayPrototype, attributes: DontEnum | DontDelete | ReadOnly); |
| 45 | |
| 46 | // no. of arguments for constructor |
| 47 | putDirectWithoutTransition(propertyName: exec->propertyNames().length, value: jsNumber(exec, i: 1), attributes: ReadOnly | DontEnum | DontDelete); |
| 48 | |
| 49 | // ES5 |
| 50 | putDirectFunctionWithoutTransition(exec, function: new (exec) NativeFunctionWrapper(exec, prototypeFunctionStructure, 1, exec->propertyNames().isArray, arrayConstructorIsArray), attr: DontEnum); |
| 51 | } |
| 52 | |
| 53 | static inline JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args) |
| 54 | { |
| 55 | // a single numeric argument denotes the array size (!) |
| 56 | if (args.size() == 1 && args.at(idx: 0).isNumber()) { |
| 57 | uint32_t n = args.at(idx: 0).toUInt32(exec); |
| 58 | if (n != args.at(idx: 0).toNumber(exec)) |
| 59 | return throwError(exec, RangeError, message: "Array size is not a small enough positive integer." ); |
| 60 | return new (exec) JSArray(exec->lexicalGlobalObject()->arrayStructure(), n); |
| 61 | } |
| 62 | |
| 63 | // otherwise the array is constructed with the arguments in it |
| 64 | return new (exec) JSArray(exec->lexicalGlobalObject()->arrayStructure(), args); |
| 65 | } |
| 66 | |
| 67 | static JSObject* constructWithArrayConstructor(ExecState* exec, JSObject*, const ArgList& args) |
| 68 | { |
| 69 | return constructArrayWithSizeQuirk(exec, args); |
| 70 | } |
| 71 | |
| 72 | // ECMA 15.4.2 |
| 73 | ConstructType ArrayConstructor::getConstructData(ConstructData& constructData) |
| 74 | { |
| 75 | constructData.native.function = constructWithArrayConstructor; |
| 76 | return ConstructTypeHost; |
| 77 | } |
| 78 | |
| 79 | static JSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
| 80 | { |
| 81 | return constructArrayWithSizeQuirk(exec, args); |
| 82 | } |
| 83 | |
| 84 | // ECMA 15.6.1 |
| 85 | CallType ArrayConstructor::getCallData(CallData& callData) |
| 86 | { |
| 87 | // equivalent to 'new Array(....)' |
| 88 | callData.native.function = callArrayConstructor; |
| 89 | return CallTypeHost; |
| 90 | } |
| 91 | |
| 92 | JSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState*, JSObject*, JSValue, const ArgList& args) |
| 93 | { |
| 94 | return jsBoolean(b: args.at(idx: 0).inherits(classInfo: &JSArray::info)); |
| 95 | } |
| 96 | |
| 97 | } // namespace JSC |
| 98 | |