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