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" |
ggaren@apple.com | e553197 | 2012-08-30 23:33:05 +0000 | [diff] [blame] | 26 | #include "JSObject.h" |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 27 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 28 | namespace JSC { |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 29 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 30 | class JSArray; |
| 31 | class LLIntOffsetsExtractor; |
barraclough@apple.com | 617f464 | 2011-12-23 01:41:04 +0000 | [diff] [blame] | 32 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 33 | class JSArray : public JSNonFinalObject { |
| 34 | friend class LLIntOffsetsExtractor; |
| 35 | friend class Walker; |
| 36 | friend class JIT; |
mrowe@apple.com | f88a463 | 2008-09-07 05:44:58 +0000 | [diff] [blame] | 37 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 38 | public: |
| 39 | typedef JSNonFinalObject Base; |
akling@apple.com | 4b9e000 | 2015-04-13 19:12:48 +0000 | [diff] [blame] | 40 | static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetPropertyNames; |
commit-queue@webkit.org | 6c25c52 | 2011-08-09 20:46:17 +0000 | [diff] [blame] | 41 | |
fpizlo@apple.com | 372fa82 | 2013-08-21 19:43:47 +0000 | [diff] [blame] | 42 | static size_t allocationSize(size_t inlineCapacity) |
| 43 | { |
| 44 | ASSERT_UNUSED(inlineCapacity, !inlineCapacity); |
| 45 | return sizeof(JSArray); |
| 46 | } |
| 47 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 48 | protected: |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 49 | explicit JSArray(VM& vm, Structure* structure, Butterfly* butterfly) |
| 50 | : JSNonFinalObject(vm, structure, butterfly) |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 51 | { |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 52 | } |
| 53 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 54 | public: |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 55 | static JSArray* create(VM&, Structure*, unsigned initialLength = 0); |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame^] | 56 | static JSArray* createWithButterfly(VM&, Structure*, Butterfly*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 57 | |
| 58 | // tryCreateUninitialized is used for fast construction of arrays whose size and |
| 59 | // contents are known at time of creation. Clients of this interface must: |
| 60 | // - null-check the result (indicating out of memory, or otherwise unable to allocate vector). |
| 61 | // - call 'initializeIndex' for all properties in sequence, for 0 <= i < initialLength. |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 62 | static JSArray* tryCreateUninitialized(VM&, Structure*, unsigned initialLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 63 | |
barraclough@apple.com | 61ff98c | 2013-08-21 22:32:10 +0000 | [diff] [blame] | 64 | 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] | 65 | |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 66 | JS_EXPORT_PRIVATE static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 67 | |
fpizlo@apple.com | 10ae2d0 | 2013-08-14 02:41:47 +0000 | [diff] [blame] | 68 | DECLARE_EXPORT_INFO; |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 69 | |
| 70 | unsigned length() const { return getArrayLength(); } |
| 71 | // OK to use on new arrays, but not if it might be a RegExpMatchArray. |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 72 | JS_EXPORT_PRIVATE bool setLength(ExecState*, unsigned, bool throwException = false); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 73 | |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 74 | JS_EXPORT_PRIVATE void push(ExecState*, JSValue); |
| 75 | JS_EXPORT_PRIVATE JSValue pop(ExecState*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 76 | |
akling@apple.com | 5958530 | 2015-05-22 10:18:47 +0000 | [diff] [blame] | 77 | JSArray* fastSlice(ExecState&, unsigned startIndex, unsigned count); |
rniwa@webkit.org | 2fa4973 | 2015-05-12 21:04:10 +0000 | [diff] [blame] | 78 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 79 | enum ShiftCountMode { |
| 80 | // This form of shift hints that we're doing queueing. With this assumption in hand, |
| 81 | // we convert to ArrayStorage, which has queue optimizations. |
| 82 | ShiftCountForShift, |
| 83 | |
| 84 | // This form of shift hints that we're just doing care and feeding on an array that |
| 85 | // is probably typically used for ordinary accesses. With this assumption in hand, |
| 86 | // we try to preserve whatever indexing type it has already. |
| 87 | ShiftCountForSplice |
| 88 | }; |
| 89 | |
| 90 | bool shiftCountForShift(ExecState* exec, unsigned startIndex, unsigned count) |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 91 | { |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 92 | return shiftCountWithArrayStorage(exec->vm(), startIndex, count, ensureArrayStorage(exec->vm())); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 93 | } |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 94 | bool shiftCountForSplice(ExecState* exec, unsigned& startIndex, unsigned count) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 95 | { |
| 96 | return shiftCountWithAnyIndexingType(exec, startIndex, count); |
| 97 | } |
| 98 | template<ShiftCountMode shiftCountMode> |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 99 | bool shiftCount(ExecState* exec, unsigned& startIndex, unsigned count) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 100 | { |
| 101 | switch (shiftCountMode) { |
| 102 | case ShiftCountForShift: |
| 103 | return shiftCountForShift(exec, startIndex, count); |
| 104 | case ShiftCountForSplice: |
| 105 | return shiftCountForSplice(exec, startIndex, count); |
| 106 | default: |
| 107 | CRASH(); |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | bool unshiftCountForShift(ExecState* exec, unsigned startIndex, unsigned count) |
| 113 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 114 | return unshiftCountWithArrayStorage(exec, startIndex, count, ensureArrayStorage(exec->vm())); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 115 | } |
| 116 | bool unshiftCountForSplice(ExecState* exec, unsigned startIndex, unsigned count) |
| 117 | { |
| 118 | return unshiftCountWithAnyIndexingType(exec, startIndex, count); |
| 119 | } |
| 120 | template<ShiftCountMode shiftCountMode> |
| 121 | bool unshiftCount(ExecState* exec, unsigned startIndex, unsigned count) |
| 122 | { |
| 123 | switch (shiftCountMode) { |
| 124 | case ShiftCountForShift: |
| 125 | return unshiftCountForShift(exec, startIndex, count); |
| 126 | case ShiftCountForSplice: |
| 127 | return unshiftCountForSplice(exec, startIndex, count); |
| 128 | default: |
| 129 | CRASH(); |
| 130 | return false; |
| 131 | } |
| 132 | } |
| 133 | |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 134 | JS_EXPORT_PRIVATE void fillArgList(ExecState*, MarkedArgumentBuffer&); |
fpizlo@apple.com | bcfd39e | 2015-02-10 23:16:36 +0000 | [diff] [blame] | 135 | 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] | 136 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 137 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype, IndexingType indexingType) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 138 | { |
fpizlo@apple.com | 10ae2d0 | 2013-08-14 02:41:47 +0000 | [diff] [blame] | 139 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info(), indexingType); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | protected: |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 143 | static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&); |
| 144 | |
| 145 | static bool deleteProperty(JSCell*, ExecState*, PropertyName); |
| 146 | JS_EXPORT_PRIVATE static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode); |
| 147 | |
| 148 | private: |
| 149 | bool isLengthWritable() |
| 150 | { |
| 151 | ArrayStorage* storage = arrayStorageOrNull(); |
| 152 | if (!storage) |
| 153 | return true; |
| 154 | SparseArrayValueMap* map = storage->m_sparseMap.get(); |
| 155 | return !map || !map->lengthIsReadOnly(); |
| 156 | } |
| 157 | |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 158 | bool shiftCountWithAnyIndexingType(ExecState*, unsigned& startIndex, unsigned count); |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 159 | JS_EXPORT_PRIVATE bool shiftCountWithArrayStorage(VM&, unsigned startIndex, unsigned count, ArrayStorage*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 160 | |
| 161 | bool unshiftCountWithAnyIndexingType(ExecState*, unsigned startIndex, unsigned count); |
| 162 | bool unshiftCountWithArrayStorage(ExecState*, unsigned startIndex, unsigned count, ArrayStorage*); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 163 | bool unshiftCountSlowCase(VM&, bool, unsigned); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 164 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 165 | bool setLengthWithArrayStorage(ExecState*, unsigned newLength, bool throwException, ArrayStorage*); |
| 166 | void setLengthWritable(ExecState*, bool writable); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 169 | inline Butterfly* createContiguousArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned length, unsigned& vectorLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 170 | { |
| 171 | IndexingHeader header; |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 172 | vectorLength = std::max(length, BASE_VECTOR_LEN); |
| 173 | header.setVectorLength(vectorLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 174 | header.setPublicLength(length); |
| 175 | Butterfly* result = Butterfly::create( |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 176 | vm, intendedOwner, 0, 0, true, header, vectorLength * sizeof(EncodedJSValue)); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 177 | return result; |
| 178 | } |
| 179 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 180 | inline Butterfly* createArrayButterfly(VM& vm, JSCell* intendedOwner, unsigned initialLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 181 | { |
| 182 | Butterfly* butterfly = Butterfly::create( |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 183 | vm, intendedOwner, 0, 0, true, baseIndexingHeaderForArray(initialLength), |
| 184 | ArrayStorage::sizeFor(BASE_VECTOR_LEN)); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 185 | ArrayStorage* storage = butterfly->arrayStorage(); |
| 186 | storage->m_indexBias = 0; |
| 187 | storage->m_sparseMap.clear(); |
| 188 | storage->m_numValuesInVector = 0; |
| 189 | return butterfly; |
| 190 | } |
| 191 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 192 | Butterfly* createArrayButterflyInDictionaryIndexingMode( |
| 193 | VM&, JSCell* intendedOwner, unsigned initialLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 194 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 195 | inline JSArray* JSArray::create(VM& vm, Structure* structure, unsigned initialLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 196 | { |
| 197 | Butterfly* butterfly; |
dbatyai.u-szeged@partner.samsung.com | b382e6d | 2014-03-26 15:08:12 +0000 | [diff] [blame] | 198 | if (LIKELY(!hasAnyArrayStorage(structure->indexingType()))) { |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 199 | ASSERT( |
| 200 | hasUndecided(structure->indexingType()) |
| 201 | || hasInt32(structure->indexingType()) |
| 202 | || hasDouble(structure->indexingType()) |
| 203 | || hasContiguous(structure->indexingType())); |
| 204 | unsigned vectorLength; |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 205 | butterfly = createContiguousArrayButterfly(vm, 0, initialLength, vectorLength); |
fpizlo@apple.com | c8f3687 | 2015-05-05 02:40:28 +0000 | [diff] [blame] | 206 | ASSERT(initialLength < MIN_ARRAY_STORAGE_CONSTRUCTION_LENGTH); |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 207 | if (hasDouble(structure->indexingType())) { |
| 208 | for (unsigned i = 0; i < vectorLength; ++i) |
fpizlo@apple.com | beef452 | 2014-04-16 22:44:00 +0000 | [diff] [blame] | 209 | butterfly->contiguousDouble()[i] = PNaN; |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 210 | } |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 211 | } else { |
| 212 | ASSERT( |
| 213 | structure->indexingType() == ArrayWithSlowPutArrayStorage |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 214 | || structure->indexingType() == ArrayWithArrayStorage); |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 215 | butterfly = createArrayButterfly(vm, 0, initialLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 216 | } |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame^] | 217 | |
| 218 | return createWithButterfly(vm, structure, butterfly); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 219 | } |
| 220 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 221 | inline JSArray* JSArray::tryCreateUninitialized(VM& vm, Structure* structure, unsigned initialLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 222 | { |
| 223 | unsigned vectorLength = std::max(BASE_VECTOR_LEN, initialLength); |
| 224 | if (vectorLength > MAX_STORAGE_VECTOR_LENGTH) |
| 225 | return 0; |
| 226 | |
| 227 | Butterfly* butterfly; |
dbatyai.u-szeged@partner.samsung.com | b382e6d | 2014-03-26 15:08:12 +0000 | [diff] [blame] | 228 | if (LIKELY(!hasAnyArrayStorage(structure->indexingType()))) { |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 229 | ASSERT( |
| 230 | hasUndecided(structure->indexingType()) |
| 231 | || hasInt32(structure->indexingType()) |
| 232 | || hasDouble(structure->indexingType()) |
| 233 | || hasContiguous(structure->indexingType())); |
| 234 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 235 | void* temp; |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 236 | if (!vm.heap.tryAllocateStorage(0, Butterfly::totalSize(0, 0, true, vectorLength * sizeof(EncodedJSValue)), &temp)) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 237 | return 0; |
| 238 | butterfly = Butterfly::fromBase(temp, 0, 0); |
| 239 | butterfly->setVectorLength(vectorLength); |
| 240 | butterfly->setPublicLength(initialLength); |
fpizlo@apple.com | 0d5d62c | 2013-01-08 20:21:36 +0000 | [diff] [blame] | 241 | if (hasDouble(structure->indexingType())) { |
| 242 | for (unsigned i = initialLength; i < vectorLength; ++i) |
fpizlo@apple.com | beef452 | 2014-04-16 22:44:00 +0000 | [diff] [blame] | 243 | butterfly->contiguousDouble()[i] = PNaN; |
fpizlo@apple.com | 0d5d62c | 2013-01-08 20:21:36 +0000 | [diff] [blame] | 244 | } |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 245 | } else { |
| 246 | void* temp; |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 247 | if (!vm.heap.tryAllocateStorage(0, Butterfly::totalSize(0, 0, true, ArrayStorage::sizeFor(vectorLength)), &temp)) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 248 | return 0; |
| 249 | butterfly = Butterfly::fromBase(temp, 0, 0); |
| 250 | *butterfly->indexingHeader() = indexingHeaderForArray(initialLength, vectorLength); |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 251 | ArrayStorage* storage = butterfly->arrayStorage(); |
| 252 | storage->m_indexBias = 0; |
| 253 | storage->m_sparseMap.clear(); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 254 | storage->m_numValuesInVector = initialLength; |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 255 | } |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame^] | 256 | |
| 257 | return createWithButterfly(vm, structure, butterfly); |
| 258 | } |
| 259 | |
| 260 | inline JSArray* JSArray::createWithButterfly(VM& vm, Structure* structure, Butterfly* butterfly) |
| 261 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 262 | JSArray* array = new (NotNull, allocateCell<JSArray>(vm.heap)) JSArray(vm, structure, butterfly); |
| 263 | array->finishCreation(vm); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 264 | return array; |
| 265 | } |
mhahnenberg@apple.com | c274832 | 2012-02-10 22:44:09 +0000 | [diff] [blame] | 266 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 267 | JSArray* asArray(JSValue); |
darin@apple.com | 5a49442 | 2008-10-18 23:08:12 +0000 | [diff] [blame] | 268 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 269 | inline JSArray* asArray(JSCell* cell) |
| 270 | { |
fpizlo@apple.com | 10ae2d0 | 2013-08-14 02:41:47 +0000 | [diff] [blame] | 271 | ASSERT(cell->inherits(JSArray::info())); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 272 | return jsCast<JSArray*>(cell); |
| 273 | } |
darin@apple.com | 5a49442 | 2008-10-18 23:08:12 +0000 | [diff] [blame] | 274 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 275 | inline JSArray* asArray(JSValue value) |
| 276 | { |
| 277 | return asArray(value.asCell()); |
| 278 | } |
darin@apple.com | 8a1a5b5 | 2009-09-04 19:03:33 +0000 | [diff] [blame] | 279 | |
fpizlo@apple.com | 10ae2d0 | 2013-08-14 02:41:47 +0000 | [diff] [blame] | 280 | inline bool isJSArray(JSCell* cell) { return cell->classInfo() == JSArray::info(); } |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 281 | inline bool isJSArray(JSValue v) { return v.isCell() && isJSArray(v.asCell()); } |
ggaren@apple.com | c3343bd | 2009-02-24 03:58:09 +0000 | [diff] [blame] | 282 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 283 | inline JSArray* constructArray(ExecState* exec, Structure* arrayStructure, const ArgList& values) |
| 284 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 285 | VM& vm = exec->vm(); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 286 | unsigned length = values.size(); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 287 | JSArray* array = JSArray::tryCreateUninitialized(vm, arrayStructure, length); |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 288 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 289 | // FIXME: we should probably throw an out of memory error here, but |
| 290 | // when making this change we should check that all clients of this |
| 291 | // function will correctly handle an exception being thrown from here. |
oliver@apple.com | 51b41da | 2013-01-23 21:44:29 +0000 | [diff] [blame] | 292 | RELEASE_ASSERT(array); |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 293 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 294 | for (unsigned i = 0; i < length; ++i) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 295 | array->initializeIndex(vm, i, values.at(i)); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 296 | return array; |
| 297 | } |
mhahnenberg@apple.com | 87ff87d | 2012-02-26 22:41:41 +0000 | [diff] [blame] | 298 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 299 | inline JSArray* constructArray(ExecState* exec, Structure* arrayStructure, const JSValue* values, unsigned length) |
| 300 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 301 | VM& vm = exec->vm(); |
| 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[i]); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 311 | return array; |
| 312 | } |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 313 | |
msaboff@apple.com | b70e41b | 2013-09-13 18:03:55 +0000 | [diff] [blame] | 314 | inline JSArray* constructArrayNegativeIndexed(ExecState* exec, Structure* arrayStructure, const JSValue* values, unsigned length) |
| 315 | { |
| 316 | VM& vm = exec->vm(); |
| 317 | JSArray* array = JSArray::tryCreateUninitialized(vm, arrayStructure, length); |
| 318 | |
| 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. |
| 322 | RELEASE_ASSERT(array); |
| 323 | |
| 324 | for (int i = 0; i < static_cast<int>(length); ++i) |
| 325 | array->initializeIndex(vm, i, values[-i]); |
| 326 | return array; |
| 327 | } |
| 328 | |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 329 | } // namespace JSC |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 330 | |
weinig@apple.com | 0e2d66e | 2008-07-06 05:26:58 +0000 | [diff] [blame] | 331 | #endif // JSArray_h |