]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ArrayConstructor.cpp
a4df41193d25e25f88a47ec602f7cc6e2605a5de
[apple/javascriptcore.git] / runtime / ArrayConstructor.cpp
1 /*
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)
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
34 namespace JSC {
35
36 static EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState*);
37
38 }
39
40 #include "ArrayConstructor.lut.h"
41
42 namespace JSC {
43
44 const ClassInfo ArrayConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::arrayConstructorTable };
45
46 /* Source for ArrayConstructor.lut.h
47 @begin arrayConstructorTable
48 isArray arrayConstructorIsArray DontEnum|Function 1
49 @end
50 */
51
52 ASSERT_CLASS_FITS_IN_CELL(ArrayConstructor);
53
54 ArrayConstructor::ArrayConstructor(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, ArrayPrototype* arrayPrototype)
55 : InternalFunction(&exec->globalData(), globalObject, structure, Identifier(exec, arrayPrototype->classInfo()->className))
56 {
57 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly);
58 putDirectWithoutTransition(exec->globalData(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
59 }
60
61 bool ArrayConstructor::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
62 {
63 return getStaticFunctionSlot<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), this, propertyName, slot);
64 }
65
66 bool ArrayConstructor::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
67 {
68 return getStaticFunctionDescriptor<InternalFunction>(exec, ExecState::arrayConstructorTable(exec), this, propertyName, descriptor);
69 }
70
71 // ------------------------------ Functions ---------------------------
72
73 static inline JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args)
74 {
75 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
76
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);
83 }
84
85 // otherwise the array is constructed with the arguments in it
86 return new (exec) JSArray(exec->globalData(), globalObject->arrayStructure(), args);
87 }
88
89 static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec)
90 {
91 ArgList args(exec);
92 return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
93 }
94
95 ConstructType ArrayConstructor::getConstructData(ConstructData& constructData)
96 {
97 constructData.native.function = constructWithArrayConstructor;
98 return ConstructTypeHost;
99 }
100
101 static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec)
102 {
103 ArgList args(exec);
104 return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
105 }
106
107 CallType ArrayConstructor::getCallData(CallData& callData)
108 {
109 // equivalent to 'new Array(....)'
110 callData.native.function = callArrayConstructor;
111 return CallTypeHost;
112 }
113
114 EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState* exec)
115 {
116 return JSValue::encode(jsBoolean(exec->argument(0).inherits(&JSArray::s_info)));
117 }
118
119 } // namespace JSC