]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ArrayConstructor.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[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 "ButterflyInlines.h"
29 #include "CopiedSpaceInlines.h"
30 #include "Error.h"
31 #include "ExceptionHelpers.h"
32 #include "JSArray.h"
33 #include "JSFunction.h"
34 #include "Lookup.h"
35 #include "JSCInlines.h"
36
37 namespace JSC {
38
39 static EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState*);
40
41 }
42
43 #include "ArrayConstructor.lut.h"
44
45 namespace JSC {
46
47 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ArrayConstructor);
48
49 const ClassInfo ArrayConstructor::s_info = { "Function", &InternalFunction::s_info, &arrayConstructorTable, CREATE_METHOD_TABLE(ArrayConstructor) };
50
51 /* Source for ArrayConstructor.lut.h
52 @begin arrayConstructorTable
53 isArray arrayConstructorIsArray DontEnum|Function 1
54 of arrayConstructorOf DontEnum|Function 0
55 from arrayConstructorFrom DontEnum|Function 0
56 @end
57 */
58
59 ArrayConstructor::ArrayConstructor(VM& vm, Structure* structure)
60 : InternalFunction(vm, structure)
61 {
62 }
63
64 void ArrayConstructor::finishCreation(VM& vm, ArrayPrototype* arrayPrototype)
65 {
66 Base::finishCreation(vm, arrayPrototype->classInfo()->className);
67 putDirectWithoutTransition(vm, vm.propertyNames->prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly);
68 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
69 }
70
71 bool ArrayConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
72 {
73 return getStaticFunctionSlot<InternalFunction>(exec, arrayConstructorTable, jsCast<ArrayConstructor*>(object), propertyName, slot);
74 }
75
76 // ------------------------------ Functions ---------------------------
77
78 JSObject* constructArrayWithSizeQuirk(ExecState* exec, ArrayAllocationProfile* profile, JSGlobalObject* globalObject, JSValue length)
79 {
80 if (!length.isNumber())
81 return constructArrayNegativeIndexed(exec, profile, globalObject, &length, 1);
82
83 uint32_t n = length.toUInt32(exec);
84 if (n != length.toNumber(exec))
85 return exec->vm().throwException(exec, createRangeError(exec, ASCIILiteral("Array size is not a small enough positive integer.")));
86 return constructEmptyArray(exec, profile, globalObject, n);
87 }
88
89 static inline JSObject* constructArrayWithSizeQuirk(ExecState* exec, const ArgList& args)
90 {
91 JSGlobalObject* globalObject = asInternalFunction(exec->callee())->globalObject();
92
93 // a single numeric argument denotes the array size (!)
94 if (args.size() == 1)
95 return constructArrayWithSizeQuirk(exec, 0, globalObject, args.at(0));
96
97 // otherwise the array is constructed with the arguments in it
98 return constructArray(exec, 0, globalObject, args);
99 }
100
101 static EncodedJSValue JSC_HOST_CALL constructWithArrayConstructor(ExecState* exec)
102 {
103 ArgList args(exec);
104 return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
105 }
106
107 ConstructType ArrayConstructor::getConstructData(JSCell*, ConstructData& constructData)
108 {
109 constructData.native.function = constructWithArrayConstructor;
110 return ConstructTypeHost;
111 }
112
113 static EncodedJSValue JSC_HOST_CALL callArrayConstructor(ExecState* exec)
114 {
115 ArgList args(exec);
116 return JSValue::encode(constructArrayWithSizeQuirk(exec, args));
117 }
118
119 CallType ArrayConstructor::getCallData(JSCell*, CallData& callData)
120 {
121 // equivalent to 'new Array(....)'
122 callData.native.function = callArrayConstructor;
123 return CallTypeHost;
124 }
125
126 EncodedJSValue JSC_HOST_CALL arrayConstructorIsArray(ExecState* exec)
127 {
128 return JSValue::encode(jsBoolean(exec->argument(0).inherits(JSArray::info())));
129 }
130
131 } // namespace JSC