darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 1 | /* |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
fpizlo@apple.com | 74485fb | 2015-02-10 03:27:43 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003, 2007, 2008, 2009, 2012, 2015 Apple Inc. All rights reserved. |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 4 | * |
| 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 |
mjs | cdff33b | 2006-01-23 21:41:36 +0000 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
darin@apple.com | 5c0863d | 2008-06-16 04:17:44 +0000 | [diff] [blame] | 21 | #ifndef JSArray_h |
| 22 | #define JSArray_h |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 23 | |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 24 | #include "ArrayConventions.h" |
mark.lam@apple.com | a4fe7ab | 2012-11-09 03:03:44 +0000 | [diff] [blame] | 25 | #include "ButterflyInlines.h" |
keith_miller@apple.com | f4a459e | 2016-05-04 20:15:26 +0000 | [diff] [blame] | 26 | #include "JSCellInlines.h" |
ggaren@apple.com | e553197 | 2012-08-30 23:33:05 +0000 | [diff] [blame] | 27 | #include "JSObject.h" |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 28 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 29 | namespace JSC { |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 30 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 31 | class JSArray; |
| 32 | class LLIntOffsetsExtractor; |
barraclough@apple.com | 617f464 | 2011-12-23 01:41:04 +0000 | [diff] [blame] | 33 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 34 | class JSArray : public JSNonFinalObject { |
| 35 | friend class LLIntOffsetsExtractor; |
| 36 | friend class Walker; |
| 37 | friend class JIT; |
mrowe@apple.com | f88a463 | 2008-09-07 05:44:58 +0000 | [diff] [blame] | 38 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 39 | public: |
| 40 | typedef JSNonFinalObject Base; |
akling@apple.com | 4b9e000 | 2015-04-13 19:12:48 +0000 | [diff] [blame] | 41 | static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetPropertyNames; |
commit-queue@webkit.org | 6c25c52 | 2011-08-09 20:46:17 +0000 | [diff] [blame] | 42 | |
fpizlo@apple.com | 372fa82 | 2013-08-21 19:43:47 +0000 | [diff] [blame] | 43 | static size_t allocationSize(size_t inlineCapacity) |
| 44 | { |
| 45 | ASSERT_UNUSED(inlineCapacity, !inlineCapacity); |
| 46 | return sizeof(JSArray); |
| 47 | } |
| 48 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 49 | protected: |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 50 | explicit JSArray(VM& vm, Structure* structure, Butterfly* butterfly) |
| 51 | : JSNonFinalObject(vm, structure, butterfly) |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 52 | { |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 53 | } |
| 54 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 55 | public: |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 56 | static JSArray* create(VM&, Structure*, unsigned initialLength = 0); |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame] | 57 | static JSArray* createWithButterfly(VM&, Structure*, Butterfly*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 58 | |
| 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.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 63 | static JSArray* tryCreateUninitialized(VM&, Structure*, unsigned initialLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 64 | |
barraclough@apple.com | 61ff98c | 2013-08-21 22:32:10 +0000 | [diff] [blame] | 65 | JS_EXPORT_PRIVATE static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool throwException); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 66 | |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 67 | JS_EXPORT_PRIVATE static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 68 | |
fpizlo@apple.com | 10ae2d0 | 2013-08-14 02:41:47 +0000 | [diff] [blame] | 69 | DECLARE_EXPORT_INFO; |
darin@apple.com | 7efa84c | 2015-06-24 08:14:14 +0000 | [diff] [blame] | 70 | |
| 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.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 72 | unsigned length() const { return getArrayLength(); } |
darin@apple.com | 7efa84c | 2015-06-24 08:14:14 +0000 | [diff] [blame] | 73 | |
| 74 | // OK to use on new arrays, but not if it might be a RegExpMatchArray or RuntimeArray. |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 75 | JS_EXPORT_PRIVATE bool setLength(ExecState*, unsigned, bool throwException = false); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 76 | |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 77 | JS_EXPORT_PRIVATE void push(ExecState*, JSValue); |
| 78 | JS_EXPORT_PRIVATE JSValue pop(ExecState*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 79 | |
akling@apple.com | 5958530 | 2015-05-22 10:18:47 +0000 | [diff] [blame] | 80 | JSArray* fastSlice(ExecState&, unsigned startIndex, unsigned count); |
rniwa@webkit.org | 2fa4973 | 2015-05-12 21:04:10 +0000 | [diff] [blame] | 81 | |
keith_miller@apple.com | f4a459e | 2016-05-04 20:15:26 +0000 | [diff] [blame] | 82 | 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.org | d5e9b42 | 2015-07-06 17:45:29 +0000 | [diff] [blame] | 86 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 87 | 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.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 99 | { |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 100 | return shiftCountWithArrayStorage(exec->vm(), startIndex, count, ensureArrayStorage(exec->vm())); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 101 | } |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 102 | bool shiftCountForSplice(ExecState* exec, unsigned& startIndex, unsigned count) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 103 | { |
| 104 | return shiftCountWithAnyIndexingType(exec, startIndex, count); |
| 105 | } |
| 106 | template<ShiftCountMode shiftCountMode> |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 107 | bool shiftCount(ExecState* exec, unsigned& startIndex, unsigned count) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 108 | { |
| 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.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 122 | return unshiftCountWithArrayStorage(exec, startIndex, count, ensureArrayStorage(exec->vm())); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 123 | } |
| 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.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 142 | JS_EXPORT_PRIVATE void fillArgList(ExecState*, MarkedArgumentBuffer&); |
fpizlo@apple.com | bcfd39e | 2015-02-10 23:16:36 +0000 | [diff] [blame] | 143 | JS_EXPORT_PRIVATE void copyToArguments(ExecState*, VirtualRegister firstElementDest, unsigned offset, unsigned length); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 144 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 145 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype, IndexingType indexingType) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 146 | { |
keith_miller@apple.com | f4a459e | 2016-05-04 20:15:26 +0000 | [diff] [blame] | 147 | return Structure::create(vm, globalObject, prototype, TypeInfo(ArrayType, StructureFlags), info(), indexingType); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | protected: |
utatane.tea@gmail.com | 78b50c6 | 2016-03-11 17:28:46 +0000 | [diff] [blame] | 151 | static bool put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 152 | |
| 153 | static bool deleteProperty(JSCell*, ExecState*, PropertyName); |
| 154 | JS_EXPORT_PRIVATE static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode); |
| 155 | |
| 156 | private: |
| 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.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 166 | bool shiftCountWithAnyIndexingType(ExecState*, unsigned& startIndex, unsigned count); |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 167 | JS_EXPORT_PRIVATE bool shiftCountWithArrayStorage(VM&, unsigned startIndex, unsigned count, ArrayStorage*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 168 | |
| 169 | bool unshiftCountWithAnyIndexingType(ExecState*, unsigned startIndex, unsigned count); |
| 170 | bool unshiftCountWithArrayStorage(ExecState*, unsigned startIndex, unsigned count, ArrayStorage*); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 171 | bool unshiftCountSlowCase(VM&, bool, unsigned); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 172 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 173 | bool setLengthWithArrayStorage(ExecState*, unsigned newLength, bool throwException, ArrayStorage*); |
| 174 | void setLengthWritable(ExecState*, bool writable); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 177 | inline Butterfly* createContiguousArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned length, unsigned& vectorLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 178 | { |
| 179 | IndexingHeader header; |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 180 | vectorLength = std::max(length, BASE_VECTOR_LEN); |
| 181 | header.setVectorLength(vectorLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 182 | header.setPublicLength(length); |
| 183 | Butterfly* result = Butterfly::create( |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 184 | vm, intendedOwner, 0, 0, true, header, vectorLength * sizeof(EncodedJSValue)); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 185 | return result; |
| 186 | } |
| 187 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 188 | inline Butterfly* createArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned initialLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 189 | { |
| 190 | Butterfly* butterfly = Butterfly::create( |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 191 | vm, intendedOwner, 0, 0, true, baseIndexingHeaderForArray(initialLength), |
| 192 | ArrayStorage::sizeFor(BASE_VECTOR_LEN)); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 193 | 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.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 200 | Butterfly* createArrayButterflyInDictionaryIndexingMode( |
| 201 | VM&, JSCell* intendedOwner, unsigned initialLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 202 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 203 | inline JSArray* JSArray::create(VM& vm, Structure* structure, unsigned initialLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 204 | { |
| 205 | Butterfly* butterfly; |
dbatyai.u-szeged@partner.samsung.com | b382e6d | 2014-03-26 15:08:12 +0000 | [diff] [blame] | 206 | if (LIKELY(!hasAnyArrayStorage(structure->indexingType()))) { |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 207 | ASSERT( |
| 208 | hasUndecided(structure->indexingType()) |
| 209 | || hasInt32(structure->indexingType()) |
| 210 | || hasDouble(structure->indexingType()) |
| 211 | || hasContiguous(structure->indexingType())); |
| 212 | unsigned vectorLength; |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 213 | butterfly = createContiguousArrayButterfly(vm, 0, initialLength, vectorLength); |
fpizlo@apple.com | c8f3687 | 2015-05-05 02:40:28 +0000 | [diff] [blame] | 214 | ASSERT(initialLength < MIN_ARRAY_STORAGE_CONSTRUCTION_LENGTH); |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 215 | if (hasDouble(structure->indexingType())) { |
| 216 | for (unsigned i = 0; i < vectorLength; ++i) |
fpizlo@apple.com | beef452 | 2014-04-16 22:44:00 +0000 | [diff] [blame] | 217 | butterfly->contiguousDouble()[i] = PNaN; |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 218 | } |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 219 | } else { |
| 220 | ASSERT( |
| 221 | structure->indexingType() == ArrayWithSlowPutArrayStorage |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 222 | || structure->indexingType() == ArrayWithArrayStorage); |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 223 | butterfly = createArrayButterfly(vm, 0, initialLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 224 | } |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame] | 225 | |
| 226 | return createWithButterfly(vm, structure, butterfly); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 227 | } |
| 228 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 229 | inline JSArray* JSArray::tryCreateUninitialized(VM& vm, Structure* structure, unsigned initialLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 230 | { |
| 231 | unsigned vectorLength = std::max(BASE_VECTOR_LEN, initialLength); |
| 232 | if (vectorLength > MAX_STORAGE_VECTOR_LENGTH) |
| 233 | return 0; |
msaboff@apple.com | 272243a | 2016-03-20 23:08:51 +0000 | [diff] [blame] | 234 | |
| 235 | unsigned outOfLineStorage = structure->outOfLineCapacity(); |
| 236 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 237 | Butterfly* butterfly; |
dbatyai.u-szeged@partner.samsung.com | b382e6d | 2014-03-26 15:08:12 +0000 | [diff] [blame] | 238 | if (LIKELY(!hasAnyArrayStorage(structure->indexingType()))) { |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 239 | ASSERT( |
| 240 | hasUndecided(structure->indexingType()) |
| 241 | || hasInt32(structure->indexingType()) |
| 242 | || hasDouble(structure->indexingType()) |
| 243 | || hasContiguous(structure->indexingType())); |
| 244 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 245 | void* temp; |
msaboff@apple.com | 272243a | 2016-03-20 23:08:51 +0000 | [diff] [blame] | 246 | if (!vm.heap.tryAllocateStorage(0, Butterfly::totalSize(0, outOfLineStorage, true, vectorLength * sizeof(EncodedJSValue)), &temp)) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 247 | return 0; |
msaboff@apple.com | 272243a | 2016-03-20 23:08:51 +0000 | [diff] [blame] | 248 | butterfly = Butterfly::fromBase(temp, 0, outOfLineStorage); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 249 | butterfly->setVectorLength(vectorLength); |
| 250 | butterfly->setPublicLength(initialLength); |
fpizlo@apple.com | 0d5d62c | 2013-01-08 20:21:36 +0000 | [diff] [blame] | 251 | if (hasDouble(structure->indexingType())) { |
| 252 | for (unsigned i = initialLength; i < vectorLength; ++i) |
fpizlo@apple.com | beef452 | 2014-04-16 22:44:00 +0000 | [diff] [blame] | 253 | butterfly->contiguousDouble()[i] = PNaN; |
fpizlo@apple.com | 0d5d62c | 2013-01-08 20:21:36 +0000 | [diff] [blame] | 254 | } |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 255 | } else { |
| 256 | void* temp; |
msaboff@apple.com | 272243a | 2016-03-20 23:08:51 +0000 | [diff] [blame] | 257 | if (!vm.heap.tryAllocateStorage(0, Butterfly::totalSize(0, outOfLineStorage, true, ArrayStorage::sizeFor(vectorLength)), &temp)) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 258 | return 0; |
msaboff@apple.com | 272243a | 2016-03-20 23:08:51 +0000 | [diff] [blame] | 259 | butterfly = Butterfly::fromBase(temp, 0, outOfLineStorage); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 260 | *butterfly->indexingHeader() = indexingHeaderForArray(initialLength, vectorLength); |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 261 | ArrayStorage* storage = butterfly->arrayStorage(); |
| 262 | storage->m_indexBias = 0; |
| 263 | storage->m_sparseMap.clear(); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 264 | storage->m_numValuesInVector = initialLength; |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 265 | } |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame] | 266 | |
| 267 | return createWithButterfly(vm, structure, butterfly); |
| 268 | } |
| 269 | |
| 270 | inline JSArray* JSArray::createWithButterfly(VM& vm, Structure* structure, Butterfly* butterfly) |
| 271 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 272 | JSArray* array = new (NotNull, allocateCell<JSArray>(vm.heap)) JSArray(vm, structure, butterfly); |
| 273 | array->finishCreation(vm); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 274 | return array; |
| 275 | } |
mhahnenberg@apple.com | c274832 | 2012-02-10 22:44:09 +0000 | [diff] [blame] | 276 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 277 | JSArray* asArray(JSValue); |
darin@apple.com | 5a49442 | 2008-10-18 23:08:12 +0000 | [diff] [blame] | 278 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 279 | inline JSArray* asArray(JSCell* cell) |
| 280 | { |
fpizlo@apple.com | 10ae2d0 | 2013-08-14 02:41:47 +0000 | [diff] [blame] | 281 | ASSERT(cell->inherits(JSArray::info())); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 282 | return jsCast<JSArray*>(cell); |
| 283 | } |
darin@apple.com | 5a49442 | 2008-10-18 23:08:12 +0000 | [diff] [blame] | 284 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 285 | inline JSArray* asArray(JSValue value) |
| 286 | { |
| 287 | return asArray(value.asCell()); |
| 288 | } |
darin@apple.com | 8a1a5b5 | 2009-09-04 19:03:33 +0000 | [diff] [blame] | 289 | |
keith_miller@apple.com | 8736ef1 | 2016-04-13 20:49:57 +0000 | [diff] [blame] | 290 | inline bool isJSArray(JSCell* cell) |
| 291 | { |
keith_miller@apple.com | f4a459e | 2016-05-04 20:15:26 +0000 | [diff] [blame] | 292 | ASSERT((cell->classInfo() == JSArray::info()) == (cell->type() == ArrayType)); |
| 293 | return cell->type() == ArrayType; |
keith_miller@apple.com | 8736ef1 | 2016-04-13 20:49:57 +0000 | [diff] [blame] | 294 | } |
| 295 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 296 | inline bool isJSArray(JSValue v) { return v.isCell() && isJSArray(v.asCell()); } |
ggaren@apple.com | c3343bd | 2009-02-24 03:58:09 +0000 | [diff] [blame] | 297 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 298 | inline JSArray* constructArray(ExecState* exec, Structure* arrayStructure, const ArgList& values) |
| 299 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 300 | VM& vm = exec->vm(); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 301 | unsigned length = values.size(); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 302 | JSArray* array = JSArray::tryCreateUninitialized(vm, arrayStructure, length); |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 303 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 304 | // 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.com | 51b41da | 2013-01-23 21:44:29 +0000 | [diff] [blame] | 307 | RELEASE_ASSERT(array); |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 308 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 309 | for (unsigned i = 0; i < length; ++i) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 310 | array->initializeIndex(vm, i, values.at(i)); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 311 | return array; |
| 312 | } |
mhahnenberg@apple.com | 87ff87d | 2012-02-26 22:41:41 +0000 | [diff] [blame] | 313 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 314 | inline JSArray* constructArray(ExecState* exec, Structure* arrayStructure, const JSValue* values, unsigned length) |
| 315 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 316 | VM& vm = exec->vm(); |
| 317 | JSArray* array = JSArray::tryCreateUninitialized(vm, arrayStructure, length); |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 318 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 319 | // 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.com | 51b41da | 2013-01-23 21:44:29 +0000 | [diff] [blame] | 322 | RELEASE_ASSERT(array); |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 323 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 324 | for (unsigned i = 0; i < length; ++i) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 325 | array->initializeIndex(vm, i, values[i]); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 326 | return array; |
| 327 | } |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 328 | |
msaboff@apple.com | b70e41b | 2013-09-13 18:03:55 +0000 | [diff] [blame] | 329 | inline 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.com | f76f1b4 | 2016-03-05 17:01:04 +0000 | [diff] [blame] | 344 | ALWAYS_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.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 351 | } // namespace JSC |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 352 | |
weinig@apple.com | 0e2d66e | 2008-07-06 05:26:58 +0000 | [diff] [blame] | 353 | #endif // JSArray_h |