blob: 494c7b5ff1a50b014e258fef10c5e8980bf13ba7 [file] [log] [blame]
darinc758b282002-11-20 21:12:14 +00001/*
darinc758b282002-11-20 21:12:14 +00002 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
fpizlo@apple.com74485fb2015-02-10 03:27:43 +00003 * Copyright (C) 2003, 2007, 2008, 2009, 2012, 2015 Apple Inc. All rights reserved.
darinc758b282002-11-20 21:12:14 +00004 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
mjscdff33b2006-01-23 21:41:36 +000017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
darinc758b282002-11-20 21:12:14 +000018 *
19 */
20
darin@apple.com5c0863d2008-06-16 04:17:44 +000021#ifndef JSArray_h
22#define JSArray_h
darinc758b282002-11-20 21:12:14 +000023
fpizlo@apple.comd8dd0532012-09-13 04:18:52 +000024#include "ArrayConventions.h"
mark.lam@apple.coma4fe7ab2012-11-09 03:03:44 +000025#include "ButterflyInlines.h"
keith_miller@apple.comf4a459e2016-05-04 20:15:26 +000026#include "JSCellInlines.h"
ggaren@apple.come5531972012-08-30 23:33:05 +000027#include "JSObject.h"
darinc758b282002-11-20 21:12:14 +000028
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000029namespace JSC {
darinc758b282002-11-20 21:12:14 +000030
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000031class JSArray;
32class LLIntOffsetsExtractor;
barraclough@apple.com617f4642011-12-23 01:41:04 +000033
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000034class JSArray : public JSNonFinalObject {
35 friend class LLIntOffsetsExtractor;
36 friend class Walker;
37 friend class JIT;
mrowe@apple.comf88a4632008-09-07 05:44:58 +000038
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000039public:
40 typedef JSNonFinalObject Base;
akling@apple.com4b9e0002015-04-13 19:12:48 +000041 static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetPropertyNames;
commit-queue@webkit.org6c25c522011-08-09 20:46:17 +000042
fpizlo@apple.com372fa822013-08-21 19:43:47 +000043 static size_t allocationSize(size_t inlineCapacity)
44 {
45 ASSERT_UNUSED(inlineCapacity, !inlineCapacity);
46 return sizeof(JSArray);
47 }
48
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000049protected:
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000050 explicit JSArray(VM& vm, Structure* structure, Butterfly* butterfly)
51 : JSNonFinalObject(vm, structure, butterfly)
fpizlo@apple.com0e9910a2012-10-09 23:39:53 +000052 {
fpizlo@apple.com0e9910a2012-10-09 23:39:53 +000053 }
54
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000055public:
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000056 static JSArray* create(VM&, Structure*, unsigned initialLength = 0);
akling@apple.com406c2f82015-06-16 18:38:04 +000057 static JSArray* createWithButterfly(VM&, Structure*, Butterfly*);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000058
59 // tryCreateUninitialized is used for fast construction of arrays whose size and
60 // contents are known at time of creation. Clients of this interface must:
61 // - null-check the result (indicating out of memory, or otherwise unable to allocate vector).
62 // - call 'initializeIndex' for all properties in sequence, for 0 <= i < initialLength.
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +000063 static JSArray* tryCreateUninitialized(VM&, Structure*, unsigned initialLength);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000064
barraclough@apple.com61ff98c2013-08-21 22:32:10 +000065 JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool throwException);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000066
fpizlo@apple.comff27eed2014-07-23 04:33:37 +000067 JS_EXPORT_PRIVATE static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000068
fpizlo@apple.com10ae2d02013-08-14 02:41:47 +000069 DECLARE_EXPORT_INFO;
darin@apple.com7efa84c2015-06-24 08:14:14 +000070
71 // OK if we know this is a JSArray, but not if it could be an object of a derived class; for RuntimeArray this always returns 0.
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000072 unsigned length() const { return getArrayLength(); }
darin@apple.com7efa84c2015-06-24 08:14:14 +000073
74 // OK to use on new arrays, but not if it might be a RegExpMatchArray or RuntimeArray.
fpizlo@apple.comff27eed2014-07-23 04:33:37 +000075 JS_EXPORT_PRIVATE bool setLength(ExecState*, unsigned, bool throwException = false);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000076
fpizlo@apple.comff27eed2014-07-23 04:33:37 +000077 JS_EXPORT_PRIVATE void push(ExecState*, JSValue);
78 JS_EXPORT_PRIVATE JSValue pop(ExecState*);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000079
akling@apple.com59585302015-05-22 10:18:47 +000080 JSArray* fastSlice(ExecState&, unsigned startIndex, unsigned count);
rniwa@webkit.org2fa49732015-05-12 21:04:10 +000081
keith_miller@apple.comf4a459e2016-05-04 20:15:26 +000082 bool canFastCopy(VM&, JSArray* otherArray);
83 // This function returns NonArray if the indexing types are not compatable for memcpying.
84 IndexingType memCopyWithIndexingType(IndexingType other);
85 bool appendMemcpy(ExecState*, VM&, JSArray* otherArray);
rniwa@webkit.orgd5e9b422015-07-06 17:45:29 +000086
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +000087 enum ShiftCountMode {
88 // This form of shift hints that we're doing queueing. With this assumption in hand,
89 // we convert to ArrayStorage, which has queue optimizations.
90 ShiftCountForShift,
91
92 // This form of shift hints that we're just doing care and feeding on an array that
93 // is probably typically used for ordinary accesses. With this assumption in hand,
94 // we try to preserve whatever indexing type it has already.
95 ShiftCountForSplice
96 };
97
98 bool shiftCountForShift(ExecState* exec, unsigned startIndex, unsigned count)
fpizlo@apple.comd8dd0532012-09-13 04:18:52 +000099 {
mhahnenberg@apple.coma3572b42014-05-20 18:07:48 +0000100 return shiftCountWithArrayStorage(exec->vm(), startIndex, count, ensureArrayStorage(exec->vm()));
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000101 }
mhahnenberg@apple.coma3572b42014-05-20 18:07:48 +0000102 bool shiftCountForSplice(ExecState* exec, unsigned& startIndex, unsigned count)
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000103 {
104 return shiftCountWithAnyIndexingType(exec, startIndex, count);
105 }
106 template<ShiftCountMode shiftCountMode>
mhahnenberg@apple.coma3572b42014-05-20 18:07:48 +0000107 bool shiftCount(ExecState* exec, unsigned& startIndex, unsigned count)
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000108 {
109 switch (shiftCountMode) {
110 case ShiftCountForShift:
111 return shiftCountForShift(exec, startIndex, count);
112 case ShiftCountForSplice:
113 return shiftCountForSplice(exec, startIndex, count);
114 default:
115 CRASH();
116 return false;
117 }
118 }
119
120 bool unshiftCountForShift(ExecState* exec, unsigned startIndex, unsigned count)
121 {
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000122 return unshiftCountWithArrayStorage(exec, startIndex, count, ensureArrayStorage(exec->vm()));
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000123 }
124 bool unshiftCountForSplice(ExecState* exec, unsigned startIndex, unsigned count)
125 {
126 return unshiftCountWithAnyIndexingType(exec, startIndex, count);
127 }
128 template<ShiftCountMode shiftCountMode>
129 bool unshiftCount(ExecState* exec, unsigned startIndex, unsigned count)
130 {
131 switch (shiftCountMode) {
132 case ShiftCountForShift:
133 return unshiftCountForShift(exec, startIndex, count);
134 case ShiftCountForSplice:
135 return unshiftCountForSplice(exec, startIndex, count);
136 default:
137 CRASH();
138 return false;
139 }
140 }
141
fpizlo@apple.comff27eed2014-07-23 04:33:37 +0000142 JS_EXPORT_PRIVATE void fillArgList(ExecState*, MarkedArgumentBuffer&);
fpizlo@apple.combcfd39e2015-02-10 23:16:36 +0000143 JS_EXPORT_PRIVATE void copyToArguments(ExecState*, VirtualRegister firstElementDest, unsigned offset, unsigned length);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000144
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000145 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype, IndexingType indexingType)
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000146 {
keith_miller@apple.comf4a459e2016-05-04 20:15:26 +0000147 return Structure::create(vm, globalObject, prototype, TypeInfo(ArrayType, StructureFlags), info(), indexingType);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000148 }
149
150protected:
utatane.tea@gmail.com78b50c62016-03-11 17:28:46 +0000151 static bool put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000152
153 static bool deleteProperty(JSCell*, ExecState*, PropertyName);
154 JS_EXPORT_PRIVATE static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
155
156private:
157 bool isLengthWritable()
158 {
159 ArrayStorage* storage = arrayStorageOrNull();
160 if (!storage)
161 return true;
162 SparseArrayValueMap* map = storage->m_sparseMap.get();
163 return !map || !map->lengthIsReadOnly();
164 }
165
mhahnenberg@apple.coma3572b42014-05-20 18:07:48 +0000166 bool shiftCountWithAnyIndexingType(ExecState*, unsigned& startIndex, unsigned count);
fpizlo@apple.comff27eed2014-07-23 04:33:37 +0000167 JS_EXPORT_PRIVATE bool shiftCountWithArrayStorage(VM&, unsigned startIndex, unsigned count, ArrayStorage*);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000168
169 bool unshiftCountWithAnyIndexingType(ExecState*, unsigned startIndex, unsigned count);
170 bool unshiftCountWithArrayStorage(ExecState*, unsigned startIndex, unsigned count, ArrayStorage*);
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000171 bool unshiftCountSlowCase(VM&, bool, unsigned);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000172
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000173 bool setLengthWithArrayStorage(ExecState*, unsigned newLength, bool throwException, ArrayStorage*);
174 void setLengthWritable(ExecState*, bool writable);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000175};
176
oliver@apple.coma03796a2013-07-25 04:01:20 +0000177inline Butterfly* createContiguousArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned length, unsigned& vectorLength)
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000178{
179 IndexingHeader header;
fpizlo@apple.com75c91a72012-11-08 22:28:25 +0000180 vectorLength = std::max(length, BASE_VECTOR_LEN);
181 header.setVectorLength(vectorLength);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000182 header.setPublicLength(length);
183 Butterfly* result = Butterfly::create(
oliver@apple.coma03796a2013-07-25 04:01:20 +0000184 vm, intendedOwner, 0, 0, true, header, vectorLength * sizeof(EncodedJSValue));
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000185 return result;
186}
187
oliver@apple.coma03796a2013-07-25 04:01:20 +0000188inline Butterfly* createArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned initialLength)
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000189{
190 Butterfly* butterfly = Butterfly::create(
oliver@apple.coma03796a2013-07-25 04:01:20 +0000191 vm, intendedOwner, 0, 0, true, baseIndexingHeaderForArray(initialLength),
192 ArrayStorage::sizeFor(BASE_VECTOR_LEN));
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000193 ArrayStorage* storage = butterfly->arrayStorage();
194 storage->m_indexBias = 0;
195 storage->m_sparseMap.clear();
196 storage->m_numValuesInVector = 0;
197 return butterfly;
198}
199
oliver@apple.coma03796a2013-07-25 04:01:20 +0000200Butterfly* createArrayButterflyInDictionaryIndexingMode(
201 VM&, JSCell* intendedOwner, unsigned initialLength);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000202
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000203inline JSArray* JSArray::create(VM& vm, Structure* structure, unsigned initialLength)
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000204{
205 Butterfly* butterfly;
dbatyai.u-szeged@partner.samsung.comb382e6d2014-03-26 15:08:12 +0000206 if (LIKELY(!hasAnyArrayStorage(structure->indexingType()))) {
fpizlo@apple.com75c91a72012-11-08 22:28:25 +0000207 ASSERT(
208 hasUndecided(structure->indexingType())
209 || hasInt32(structure->indexingType())
210 || hasDouble(structure->indexingType())
211 || hasContiguous(structure->indexingType()));
212 unsigned vectorLength;
oliver@apple.coma03796a2013-07-25 04:01:20 +0000213 butterfly = createContiguousArrayButterfly(vm, 0, initialLength, vectorLength);
fpizlo@apple.comc8f36872015-05-05 02:40:28 +0000214 ASSERT(initialLength < MIN_ARRAY_STORAGE_CONSTRUCTION_LENGTH);
fpizlo@apple.com75c91a72012-11-08 22:28:25 +0000215 if (hasDouble(structure->indexingType())) {
216 for (unsigned i = 0; i < vectorLength; ++i)
fpizlo@apple.combeef4522014-04-16 22:44:00 +0000217 butterfly->contiguousDouble()[i] = PNaN;
fpizlo@apple.com75c91a72012-11-08 22:28:25 +0000218 }
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000219 } else {
220 ASSERT(
221 structure->indexingType() == ArrayWithSlowPutArrayStorage
fpizlo@apple.com75c91a72012-11-08 22:28:25 +0000222 || structure->indexingType() == ArrayWithArrayStorage);
oliver@apple.coma03796a2013-07-25 04:01:20 +0000223 butterfly = createArrayButterfly(vm, 0, initialLength);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000224 }
akling@apple.com406c2f82015-06-16 18:38:04 +0000225
226 return createWithButterfly(vm, structure, butterfly);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000227}
228
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000229inline JSArray* JSArray::tryCreateUninitialized(VM& vm, Structure* structure, unsigned initialLength)
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000230{
231 unsigned vectorLength = std::max(BASE_VECTOR_LEN, initialLength);
232 if (vectorLength > MAX_STORAGE_VECTOR_LENGTH)
233 return 0;
msaboff@apple.com272243a2016-03-20 23:08:51 +0000234
235 unsigned outOfLineStorage = structure->outOfLineCapacity();
236
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000237 Butterfly* butterfly;
dbatyai.u-szeged@partner.samsung.comb382e6d2014-03-26 15:08:12 +0000238 if (LIKELY(!hasAnyArrayStorage(structure->indexingType()))) {
fpizlo@apple.com75c91a72012-11-08 22:28:25 +0000239 ASSERT(
240 hasUndecided(structure->indexingType())
241 || hasInt32(structure->indexingType())
242 || hasDouble(structure->indexingType())
243 || hasContiguous(structure->indexingType()));
244
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000245 void* temp;
msaboff@apple.com272243a2016-03-20 23:08:51 +0000246 if (!vm.heap.tryAllocateStorage(0, Butterfly::totalSize(0, outOfLineStorage, true, vectorLength * sizeof(EncodedJSValue)), &temp))
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000247 return 0;
msaboff@apple.com272243a2016-03-20 23:08:51 +0000248 butterfly = Butterfly::fromBase(temp, 0, outOfLineStorage);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000249 butterfly->setVectorLength(vectorLength);
250 butterfly->setPublicLength(initialLength);
fpizlo@apple.com0d5d62c2013-01-08 20:21:36 +0000251 if (hasDouble(structure->indexingType())) {
252 for (unsigned i = initialLength; i < vectorLength; ++i)
fpizlo@apple.combeef4522014-04-16 22:44:00 +0000253 butterfly->contiguousDouble()[i] = PNaN;
fpizlo@apple.com0d5d62c2013-01-08 20:21:36 +0000254 }
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000255 } else {
256 void* temp;
msaboff@apple.com272243a2016-03-20 23:08:51 +0000257 if (!vm.heap.tryAllocateStorage(0, Butterfly::totalSize(0, outOfLineStorage, true, ArrayStorage::sizeFor(vectorLength)), &temp))
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000258 return 0;
msaboff@apple.com272243a2016-03-20 23:08:51 +0000259 butterfly = Butterfly::fromBase(temp, 0, outOfLineStorage);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000260 *butterfly->indexingHeader() = indexingHeaderForArray(initialLength, vectorLength);
fpizlo@apple.comd8dd0532012-09-13 04:18:52 +0000261 ArrayStorage* storage = butterfly->arrayStorage();
262 storage->m_indexBias = 0;
263 storage->m_sparseMap.clear();
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000264 storage->m_numValuesInVector = initialLength;
fpizlo@apple.comd8dd0532012-09-13 04:18:52 +0000265 }
akling@apple.com406c2f82015-06-16 18:38:04 +0000266
267 return createWithButterfly(vm, structure, butterfly);
268}
269
270inline JSArray* JSArray::createWithButterfly(VM& vm, Structure* structure, Butterfly* butterfly)
271{
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000272 JSArray* array = new (NotNull, allocateCell<JSArray>(vm.heap)) JSArray(vm, structure, butterfly);
273 array->finishCreation(vm);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000274 return array;
275}
mhahnenberg@apple.comc2748322012-02-10 22:44:09 +0000276
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000277JSArray* asArray(JSValue);
darin@apple.com5a494422008-10-18 23:08:12 +0000278
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000279inline JSArray* asArray(JSCell* cell)
280{
fpizlo@apple.com10ae2d02013-08-14 02:41:47 +0000281 ASSERT(cell->inherits(JSArray::info()));
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000282 return jsCast<JSArray*>(cell);
283}
darin@apple.com5a494422008-10-18 23:08:12 +0000284
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000285inline JSArray* asArray(JSValue value)
286{
287 return asArray(value.asCell());
288}
darin@apple.com8a1a5b52009-09-04 19:03:33 +0000289
keith_miller@apple.com8736ef12016-04-13 20:49:57 +0000290inline bool isJSArray(JSCell* cell)
291{
keith_miller@apple.comf4a459e2016-05-04 20:15:26 +0000292 ASSERT((cell->classInfo() == JSArray::info()) == (cell->type() == ArrayType));
293 return cell->type() == ArrayType;
keith_miller@apple.com8736ef12016-04-13 20:49:57 +0000294}
295
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000296inline bool isJSArray(JSValue v) { return v.isCell() && isJSArray(v.asCell()); }
ggaren@apple.comc3343bd2009-02-24 03:58:09 +0000297
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000298inline JSArray* constructArray(ExecState* exec, Structure* arrayStructure, const ArgList& values)
299{
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000300 VM& vm = exec->vm();
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000301 unsigned length = values.size();
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000302 JSArray* array = JSArray::tryCreateUninitialized(vm, arrayStructure, length);
fpizlo@apple.com6c89cd32012-06-26 19:42:05 +0000303
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000304 // FIXME: we should probably throw an out of memory error here, but
305 // when making this change we should check that all clients of this
306 // function will correctly handle an exception being thrown from here.
oliver@apple.com51b41da2013-01-23 21:44:29 +0000307 RELEASE_ASSERT(array);
fpizlo@apple.com6c89cd32012-06-26 19:42:05 +0000308
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000309 for (unsigned i = 0; i < length; ++i)
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000310 array->initializeIndex(vm, i, values.at(i));
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000311 return array;
312}
mhahnenberg@apple.com87ff87d2012-02-26 22:41:41 +0000313
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000314inline JSArray* constructArray(ExecState* exec, Structure* arrayStructure, const JSValue* values, unsigned length)
315{
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000316 VM& vm = exec->vm();
317 JSArray* array = JSArray::tryCreateUninitialized(vm, arrayStructure, length);
fpizlo@apple.com6c89cd32012-06-26 19:42:05 +0000318
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000319 // FIXME: we should probably throw an out of memory error here, but
320 // when making this change we should check that all clients of this
321 // function will correctly handle an exception being thrown from here.
oliver@apple.com51b41da2013-01-23 21:44:29 +0000322 RELEASE_ASSERT(array);
fpizlo@apple.com6c89cd32012-06-26 19:42:05 +0000323
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000324 for (unsigned i = 0; i < length; ++i)
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000325 array->initializeIndex(vm, i, values[i]);
fpizlo@apple.coma34ff7d2012-11-06 19:10:37 +0000326 return array;
327}
fpizlo@apple.com6c89cd32012-06-26 19:42:05 +0000328
msaboff@apple.comb70e41b2013-09-13 18:03:55 +0000329inline JSArray* constructArrayNegativeIndexed(ExecState* exec, Structure* arrayStructure, const JSValue* values, unsigned length)
330{
331 VM& vm = exec->vm();
332 JSArray* array = JSArray::tryCreateUninitialized(vm, arrayStructure, length);
333
334 // FIXME: we should probably throw an out of memory error here, but
335 // when making this change we should check that all clients of this
336 // function will correctly handle an exception being thrown from here.
337 RELEASE_ASSERT(array);
338
339 for (int i = 0; i < static_cast<int>(length); ++i)
340 array->initializeIndex(vm, i, values[-i]);
341 return array;
342}
343
utatane.tea@gmail.comf76f1b42016-03-05 17:01:04 +0000344ALWAYS_INLINE unsigned getLength(ExecState* exec, JSObject* obj)
345{
346 if (isJSArray(obj))
347 return jsCast<JSArray*>(obj)->length();
348 return obj->get(exec, exec->propertyNames().length).toUInt32(exec);
349}
350
fpizlo@apple.com6c89cd32012-06-26 19:42:05 +0000351} // namespace JSC
darinc758b282002-11-20 21:12:14 +0000352
weinig@apple.com0e2d66e2008-07-06 05:26:58 +0000353#endif // JSArray_h