]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ArrayConstructor.cpp
e96bdfc7bd2d9411d1e820ef2eb942f10a7d02ed
[apple/javascriptcore.git] / runtime / ArrayConstructor.cpp
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 "JSArray.h"
29 #include "JSFunction.h"
30 #include "Lookup.h"
31
32 namespace JSC {
33
34 ASSERT_CLASS_FITS_IN_CELL(ArrayConstructor);
35
36 ArrayConstructor::ArrayConstructor(ExecState* exec, PassRefPtr<Structure> structure, ArrayPrototype* arrayPrototype)
37 : InternalFunction(&exec->globalData(), structure, Identifier(exec, arrayPrototype->classInfo()->className))
38 {
39 // ECMA 15.4.3.1 Array.prototype
40 putDirectWithoutTransition(exec->propertyNames().prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly);
41
42 // no. of arguments for constructor
43 putDirectWithoutTransition(exec->propertyNames().length, jsNumber(exec, 1), ReadOnly | DontEnum | DontDelete);
44 }
45
46 static JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args)
47 {
48 // a single numeric argument denotes the array size (!)
49 if (args.size() == 1 && args.at(0).isNumber()) {
50 uint32_t n = args.at(0).toUInt32(exec);
51 if (n != args.at(0).toNumber(exec))
52 return throwError(exec, RangeError, "Array size is not a small enough positive integer.");
53 return new (exec) JSArray(exec->lexicalGlobalObject()->arrayStructure(), n);
54 }
55
56 // otherwise the array is constructed with the arguments in it
57 return new (exec) JSArray(exec->lexicalGlobalObject()->arrayStructure(), args);
58 }
59
60 static JSObject* constructWithArrayConstructor(ExecState* exec, JSObject*, const ArgList& args)
61 {
62 return constructArrayWithSizeQuirk(exec, args);
63 }
64
65 // ECMA 15.4.2
66 ConstructType ArrayConstructor::getConstructData(ConstructData& constructData)
67 {
68 constructData.native.function = constructWithArrayConstructor;
69 return ConstructTypeHost;
70 }
71
72 static JSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec, JSObject*, JSValue, const ArgList& args)
73 {
74 return constructArrayWithSizeQuirk(exec, args);
75 }
76
77 // ECMA 15.6.1
78 CallType ArrayConstructor::getCallData(CallData& callData)
79 {
80 // equivalent to 'new Array(....)'
81 callData.native.function = callArrayConstructor;
82 return CallTypeHost;
83 }
84
85 } // namespace JSC