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 | 47d0cf7 | 2018-01-25 19:32:00 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2018 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 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 21 | #pragma once |
darin | c758b28 | 2002-11-20 21:12:14 +0000 | [diff] [blame] | 22 | |
annulen@yandex.ru | 6712c2d | 2017-06-25 17:40:30 +0000 | [diff] [blame] | 23 | #include "ArgList.h" |
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 | 5bed6f6 | 2016-06-16 06:01:47 +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 | |
utatane.tea@gmail.com | 8407763 | 2018-06-23 08:39:34 +0000 | [diff] [blame] | 34 | extern const ASCIILiteral LengthExceededTheMaximumArrayLengthError; |
utatane.tea@gmail.com | 9078287 | 2017-09-30 01:16:52 +0000 | [diff] [blame] | 35 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 36 | class JSArray : public JSNonFinalObject { |
| 37 | friend class LLIntOffsetsExtractor; |
| 38 | friend class Walker; |
| 39 | friend class JIT; |
mrowe@apple.com | f88a463 | 2008-09-07 05:44:58 +0000 | [diff] [blame] | 40 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 41 | public: |
| 42 | typedef JSNonFinalObject Base; |
akling@apple.com | 4b9e000 | 2015-04-13 19:12:48 +0000 | [diff] [blame] | 43 | static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetPropertyNames; |
commit-queue@webkit.org | 6c25c52 | 2011-08-09 20:46:17 +0000 | [diff] [blame] | 44 | |
oliver@apple.com | 26d90af | 2017-04-13 23:13:41 +0000 | [diff] [blame] | 45 | static size_t allocationSize(Checked<size_t> inlineCapacity) |
fpizlo@apple.com | 372fa82 | 2013-08-21 19:43:47 +0000 | [diff] [blame] | 46 | { |
| 47 | ASSERT_UNUSED(inlineCapacity, !inlineCapacity); |
| 48 | return sizeof(JSArray); |
| 49 | } |
| 50 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 51 | protected: |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 52 | explicit JSArray(VM& vm, Structure* structure, Butterfly* butterfly) |
| 53 | : JSNonFinalObject(vm, structure, butterfly) |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 54 | { |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 55 | } |
| 56 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 57 | public: |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 58 | static JSArray* tryCreate(VM&, Structure*, unsigned initialLength = 0); |
utatane.tea@gmail.com | b9355b5 | 2017-09-22 12:19:54 +0000 | [diff] [blame] | 59 | static JSArray* tryCreate(VM&, Structure*, unsigned initialLength, unsigned vectorLengthHint); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 60 | static JSArray* create(VM&, Structure*, unsigned initialLength = 0); |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 61 | static JSArray* createWithButterfly(VM&, GCDeferralContext*, Structure*, Butterfly*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 62 | |
mark.lam@apple.com | 2147640 | 2017-04-27 19:24:07 +0000 | [diff] [blame] | 63 | // tryCreateUninitializedRestricted is used for fast construction of arrays whose size and |
| 64 | // contents are known at time of creation. This is a restricted API for careful use only in |
| 65 | // performance critical code paths. If you don't have a good reason to use it, you probably |
| 66 | // shouldn't use it. Instead, you should go with |
| 67 | // - JSArray::tryCreate() or JSArray::create() instead of tryCreateUninitializedRestricted(), and |
| 68 | // - putDirectIndex() instead of initializeIndex(). |
| 69 | // |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 70 | // Clients of this interface must: |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 71 | // - null-check the result (indicating out of memory, or otherwise unable to allocate vector). |
| 72 | // - call 'initializeIndex' for all properties in sequence, for 0 <= i < initialLength. |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 73 | // - Provide a valid GCDefferalContext* if they might garbage collect when initializing properties, |
| 74 | // otherwise the caller can provide a null GCDefferalContext*. |
mark.lam@apple.com | 2147640 | 2017-04-27 19:24:07 +0000 | [diff] [blame] | 75 | // - Provide a local stack instance of ObjectInitializationScope at the call site. |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 76 | // |
mark.lam@apple.com | 2147640 | 2017-04-27 19:24:07 +0000 | [diff] [blame] | 77 | JS_EXPORT_PRIVATE static JSArray* tryCreateUninitializedRestricted(ObjectInitializationScope&, GCDeferralContext*, Structure*, unsigned initialLength); |
| 78 | static JSArray* tryCreateUninitializedRestricted(ObjectInitializationScope& scope, Structure* structure, unsigned initialLength) |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 79 | { |
mark.lam@apple.com | 2147640 | 2017-04-27 19:24:07 +0000 | [diff] [blame] | 80 | return tryCreateUninitializedRestricted(scope, nullptr, structure, initialLength); |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 81 | } |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 82 | |
mark.lam@apple.com | 5d33893 | 2018-07-11 06:21:22 +0000 | [diff] [blame] | 83 | static void eagerlyInitializeButterfly(ObjectInitializationScope&, JSArray*, unsigned initialLength); |
| 84 | |
barraclough@apple.com | 61ff98c | 2013-08-21 22:32:10 +0000 | [diff] [blame] | 85 | 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] | 86 | |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 87 | JS_EXPORT_PRIVATE static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 88 | |
fpizlo@apple.com | 10ae2d0 | 2013-08-14 02:41:47 +0000 | [diff] [blame] | 89 | DECLARE_EXPORT_INFO; |
darin@apple.com | 7efa84c | 2015-06-24 08:14:14 +0000 | [diff] [blame] | 90 | |
| 91 | // 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] | 92 | unsigned length() const { return getArrayLength(); } |
darin@apple.com | 7efa84c | 2015-06-24 08:14:14 +0000 | [diff] [blame] | 93 | |
| 94 | // 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] | 95 | JS_EXPORT_PRIVATE bool setLength(ExecState*, unsigned, bool throwException = false); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 96 | |
utatane.tea@gmail.com | 9078287 | 2017-09-30 01:16:52 +0000 | [diff] [blame] | 97 | void pushInline(ExecState*, JSValue); |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 98 | JS_EXPORT_PRIVATE void push(ExecState*, JSValue); |
| 99 | JS_EXPORT_PRIVATE JSValue pop(ExecState*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 100 | |
akling@apple.com | 5958530 | 2015-05-22 10:18:47 +0000 | [diff] [blame] | 101 | JSArray* fastSlice(ExecState&, unsigned startIndex, unsigned count); |
rniwa@webkit.org | 2fa4973 | 2015-05-12 21:04:10 +0000 | [diff] [blame] | 102 | |
keith_miller@apple.com | 5bed6f6 | 2016-06-16 06:01:47 +0000 | [diff] [blame] | 103 | bool canFastCopy(VM&, JSArray* otherArray); |
yusukesuzuki@slowstart.org | fbf1df9 | 2018-09-20 05:54:27 +0000 | [diff] [blame] | 104 | bool canDoFastIndexedAccess(VM&); |
keith_miller@apple.com | 5bed6f6 | 2016-06-16 06:01:47 +0000 | [diff] [blame] | 105 | // This function returns NonArray if the indexing types are not compatable for copying. |
| 106 | IndexingType mergeIndexingTypeForCopying(IndexingType other); |
| 107 | bool appendMemcpy(ExecState*, VM&, unsigned startIndex, JSArray* otherArray); |
rniwa@webkit.org | d5e9b42 | 2015-07-06 17:45:29 +0000 | [diff] [blame] | 108 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 109 | enum ShiftCountMode { |
| 110 | // This form of shift hints that we're doing queueing. With this assumption in hand, |
| 111 | // we convert to ArrayStorage, which has queue optimizations. |
| 112 | ShiftCountForShift, |
| 113 | |
| 114 | // This form of shift hints that we're just doing care and feeding on an array that |
| 115 | // is probably typically used for ordinary accesses. With this assumption in hand, |
| 116 | // we try to preserve whatever indexing type it has already. |
| 117 | ShiftCountForSplice |
| 118 | }; |
| 119 | |
| 120 | bool shiftCountForShift(ExecState* exec, unsigned startIndex, unsigned count) |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 121 | { |
mark.lam@apple.com | 23e9624 | 2017-09-09 16:21:45 +0000 | [diff] [blame] | 122 | VM& vm = exec->vm(); |
| 123 | return shiftCountWithArrayStorage(vm, startIndex, count, ensureArrayStorage(vm)); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 124 | } |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 125 | bool shiftCountForSplice(ExecState* exec, unsigned& startIndex, unsigned count) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 126 | { |
| 127 | return shiftCountWithAnyIndexingType(exec, startIndex, count); |
| 128 | } |
| 129 | template<ShiftCountMode shiftCountMode> |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 130 | bool shiftCount(ExecState* exec, unsigned& startIndex, unsigned count) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 131 | { |
| 132 | switch (shiftCountMode) { |
| 133 | case ShiftCountForShift: |
| 134 | return shiftCountForShift(exec, startIndex, count); |
| 135 | case ShiftCountForSplice: |
| 136 | return shiftCountForSplice(exec, startIndex, count); |
| 137 | default: |
| 138 | CRASH(); |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | bool unshiftCountForShift(ExecState* exec, unsigned startIndex, unsigned count) |
| 144 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 145 | return unshiftCountWithArrayStorage(exec, startIndex, count, ensureArrayStorage(exec->vm())); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 146 | } |
| 147 | bool unshiftCountForSplice(ExecState* exec, unsigned startIndex, unsigned count) |
| 148 | { |
| 149 | return unshiftCountWithAnyIndexingType(exec, startIndex, count); |
| 150 | } |
| 151 | template<ShiftCountMode shiftCountMode> |
| 152 | bool unshiftCount(ExecState* exec, unsigned startIndex, unsigned count) |
| 153 | { |
| 154 | switch (shiftCountMode) { |
| 155 | case ShiftCountForShift: |
| 156 | return unshiftCountForShift(exec, startIndex, count); |
| 157 | case ShiftCountForSplice: |
| 158 | return unshiftCountForSplice(exec, startIndex, count); |
| 159 | default: |
| 160 | CRASH(); |
| 161 | return false; |
| 162 | } |
| 163 | } |
| 164 | |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 165 | JS_EXPORT_PRIVATE void fillArgList(ExecState*, MarkedArgumentBuffer&); |
fpizlo@apple.com | bcfd39e | 2015-02-10 23:16:36 +0000 | [diff] [blame] | 166 | 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] | 167 | |
sbarati@apple.com | 732d067 | 2017-04-26 00:52:35 +0000 | [diff] [blame] | 168 | JS_EXPORT_PRIVATE bool isIteratorProtocolFastAndNonObservable(); |
sbarati@apple.com | 5b8aea1 | 2017-01-24 00:15:21 +0000 | [diff] [blame] | 169 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 170 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype, IndexingType indexingType) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 171 | { |
keith_miller@apple.com | 5bed6f6 | 2016-06-16 06:01:47 +0000 | [diff] [blame] | 172 | return Structure::create(vm, globalObject, prototype, TypeInfo(ArrayType, StructureFlags), info(), indexingType); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | protected: |
utatane.tea@gmail.com | 4a748b1 | 2016-09-17 06:32:50 +0000 | [diff] [blame] | 176 | void finishCreation(VM& vm) |
| 177 | { |
| 178 | Base::finishCreation(vm); |
keith_miller@apple.com | 3f89142 | 2018-02-15 02:08:41 +0000 | [diff] [blame] | 179 | ASSERT(jsDynamicCast<JSArray*>(vm, this)); |
utatane.tea@gmail.com | 4a748b1 | 2016-09-17 06:32:50 +0000 | [diff] [blame] | 180 | ASSERT_WITH_MESSAGE(type() == ArrayType || type() == DerivedArrayType, "Instance inheriting JSArray should have either ArrayType or DerivedArrayType"); |
| 181 | } |
| 182 | |
utatane.tea@gmail.com | 78b50c6 | 2016-03-11 17:28:46 +0000 | [diff] [blame] | 183 | static bool put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 184 | |
| 185 | static bool deleteProperty(JSCell*, ExecState*, PropertyName); |
| 186 | JS_EXPORT_PRIVATE static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode); |
| 187 | |
| 188 | private: |
| 189 | bool isLengthWritable() |
| 190 | { |
| 191 | ArrayStorage* storage = arrayStorageOrNull(); |
| 192 | if (!storage) |
| 193 | return true; |
| 194 | SparseArrayValueMap* map = storage->m_sparseMap.get(); |
| 195 | return !map || !map->lengthIsReadOnly(); |
| 196 | } |
| 197 | |
mhahnenberg@apple.com | a3572b4 | 2014-05-20 18:07:48 +0000 | [diff] [blame] | 198 | bool shiftCountWithAnyIndexingType(ExecState*, unsigned& startIndex, unsigned count); |
fpizlo@apple.com | ff27eed | 2014-07-23 04:33:37 +0000 | [diff] [blame] | 199 | JS_EXPORT_PRIVATE bool shiftCountWithArrayStorage(VM&, unsigned startIndex, unsigned count, ArrayStorage*); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 200 | |
| 201 | bool unshiftCountWithAnyIndexingType(ExecState*, unsigned startIndex, unsigned count); |
| 202 | bool unshiftCountWithArrayStorage(ExecState*, unsigned startIndex, unsigned count, ArrayStorage*); |
fpizlo@apple.com | a88b5ac | 2017-01-10 18:44:45 +0000 | [diff] [blame] | 203 | bool unshiftCountSlowCase(const AbstractLocker&, VM&, DeferGC&, bool, unsigned); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 204 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 205 | bool setLengthWithArrayStorage(ExecState*, unsigned newLength, bool throwException, ArrayStorage*); |
| 206 | void setLengthWritable(ExecState*, bool writable); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
keith_miller@apple.com | 66c5765 | 2018-06-18 23:53:27 +0000 | [diff] [blame] | 209 | inline Butterfly* tryCreateArrayButterfly(VM& vm, JSObject* intendedOwner, unsigned initialLength) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 210 | { |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 211 | Butterfly* butterfly = Butterfly::tryCreate( |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 212 | vm, intendedOwner, 0, 0, true, baseIndexingHeaderForArrayStorage(initialLength), |
| 213 | ArrayStorage::sizeFor(BASE_ARRAY_STORAGE_VECTOR_LEN)); |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 214 | if (!butterfly) |
| 215 | return nullptr; |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 216 | ArrayStorage* storage = butterfly->arrayStorage(); |
fpizlo@apple.com | 595eebd | 2016-08-24 19:00:37 +0000 | [diff] [blame] | 217 | storage->m_sparseMap.clear(); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 218 | storage->m_indexBias = 0; |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 219 | storage->m_numValuesInVector = 0; |
| 220 | return butterfly; |
| 221 | } |
| 222 | |
utatane.tea@gmail.com | b9355b5 | 2017-09-22 12:19:54 +0000 | [diff] [blame] | 223 | inline JSArray* JSArray::tryCreate(VM& vm, Structure* structure, unsigned initialLength, unsigned vectorLengthHint) |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 224 | { |
utatane.tea@gmail.com | b9355b5 | 2017-09-22 12:19:54 +0000 | [diff] [blame] | 225 | ASSERT(vectorLengthHint >= initialLength); |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 226 | unsigned outOfLineStorage = structure->outOfLineCapacity(); |
| 227 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 228 | Butterfly* butterfly; |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 229 | IndexingType indexingType = structure->indexingType(); |
| 230 | if (LIKELY(!hasAnyArrayStorage(indexingType))) { |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 231 | ASSERT( |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 232 | hasUndecided(indexingType) |
| 233 | || hasInt32(indexingType) |
| 234 | || hasDouble(indexingType) |
| 235 | || hasContiguous(indexingType)); |
| 236 | |
utatane.tea@gmail.com | b9355b5 | 2017-09-22 12:19:54 +0000 | [diff] [blame] | 237 | if (UNLIKELY(vectorLengthHint > MAX_STORAGE_VECTOR_LENGTH)) |
mark.lam@apple.com | 2147640 | 2017-04-27 19:24:07 +0000 | [diff] [blame] | 238 | return nullptr; |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 239 | |
utatane.tea@gmail.com | b9355b5 | 2017-09-22 12:19:54 +0000 | [diff] [blame] | 240 | unsigned vectorLength = Butterfly::optimalContiguousVectorLength(structure, vectorLengthHint); |
fpizlo@apple.com | 5efcc49 | 2017-11-30 04:39:50 +0000 | [diff] [blame] | 241 | void* temp = vm.jsValueGigacageAuxiliarySpace.allocateNonVirtual( |
fpizlo@apple.com | 47d0cf7 | 2018-01-25 19:32:00 +0000 | [diff] [blame] | 242 | vm, |
fpizlo@apple.com | 5efcc49 | 2017-11-30 04:39:50 +0000 | [diff] [blame] | 243 | Butterfly::totalSize(0, outOfLineStorage, true, vectorLength * sizeof(EncodedJSValue)), |
| 244 | nullptr, AllocationFailureMode::ReturnNull); |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 245 | if (!temp) |
| 246 | return nullptr; |
| 247 | butterfly = Butterfly::fromBase(temp, 0, outOfLineStorage); |
| 248 | butterfly->setVectorLength(vectorLength); |
| 249 | butterfly->setPublicLength(initialLength); |
| 250 | if (hasDouble(indexingType)) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 251 | clearArray(butterfly->contiguousDouble().data(), vectorLength); |
rmorisset@apple.com | 3155c4b | 2018-01-23 20:49:26 +0000 | [diff] [blame] | 252 | else |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 253 | clearArray(butterfly->contiguous().data(), vectorLength); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 254 | } else { |
| 255 | ASSERT( |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 256 | indexingType == ArrayWithSlowPutArrayStorage |
| 257 | || indexingType == ArrayWithArrayStorage); |
mark.lam@apple.com | 2147640 | 2017-04-27 19:24:07 +0000 | [diff] [blame] | 258 | butterfly = tryCreateArrayButterfly(vm, nullptr, initialLength); |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 259 | if (!butterfly) |
| 260 | return nullptr; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 261 | for (unsigned i = 0; i < BASE_ARRAY_STORAGE_VECTOR_LEN; ++i) |
| 262 | butterfly->arrayStorage()->m_vector[i].clear(); |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 263 | } |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame] | 264 | |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 265 | return createWithButterfly(vm, nullptr, structure, butterfly); |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame] | 266 | } |
| 267 | |
utatane.tea@gmail.com | b9355b5 | 2017-09-22 12:19:54 +0000 | [diff] [blame] | 268 | inline JSArray* JSArray::tryCreate(VM& vm, Structure* structure, unsigned initialLength) |
| 269 | { |
| 270 | return tryCreate(vm, structure, initialLength, initialLength); |
| 271 | } |
| 272 | |
msaboff@apple.com | 741818a | 2017-01-23 18:45:17 +0000 | [diff] [blame] | 273 | inline JSArray* JSArray::create(VM& vm, Structure* structure, unsigned initialLength) |
| 274 | { |
| 275 | JSArray* result = JSArray::tryCreate(vm, structure, initialLength); |
| 276 | RELEASE_ASSERT(result); |
| 277 | |
| 278 | return result; |
| 279 | } |
| 280 | |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 281 | inline JSArray* JSArray::createWithButterfly(VM& vm, GCDeferralContext* deferralContext, Structure* structure, Butterfly* butterfly) |
akling@apple.com | 406c2f8 | 2015-06-16 18:38:04 +0000 | [diff] [blame] | 282 | { |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 283 | JSArray* array = new (NotNull, allocateCell<JSArray>(vm.heap, deferralContext)) JSArray(vm, structure, butterfly); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 284 | array->finishCreation(vm); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 285 | return array; |
| 286 | } |
mhahnenberg@apple.com | c274832 | 2012-02-10 22:44:09 +0000 | [diff] [blame] | 287 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 288 | JSArray* asArray(JSValue); |
darin@apple.com | 5a49442 | 2008-10-18 23:08:12 +0000 | [diff] [blame] | 289 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 290 | inline JSArray* asArray(JSCell* cell) |
| 291 | { |
utatane.tea@gmail.com | dd7199d | 2018-03-08 16:06:48 +0000 | [diff] [blame] | 292 | ASSERT(cell->inherits<JSArray>(*cell->vm())); |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 293 | return jsCast<JSArray*>(cell); |
| 294 | } |
darin@apple.com | 5a49442 | 2008-10-18 23:08:12 +0000 | [diff] [blame] | 295 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 296 | inline JSArray* asArray(JSValue value) |
| 297 | { |
| 298 | return asArray(value.asCell()); |
| 299 | } |
darin@apple.com | 8a1a5b5 | 2009-09-04 19:03:33 +0000 | [diff] [blame] | 300 | |
keith_miller@apple.com | 8736ef1 | 2016-04-13 20:49:57 +0000 | [diff] [blame] | 301 | inline bool isJSArray(JSCell* cell) |
| 302 | { |
keith_miller@apple.com | 45da760 | 2017-01-27 01:47:52 +0000 | [diff] [blame] | 303 | ASSERT((cell->classInfo(*cell->vm()) == JSArray::info()) == (cell->type() == ArrayType)); |
keith_miller@apple.com | 5bed6f6 | 2016-06-16 06:01:47 +0000 | [diff] [blame] | 304 | return cell->type() == ArrayType; |
keith_miller@apple.com | 8736ef1 | 2016-04-13 20:49:57 +0000 | [diff] [blame] | 305 | } |
| 306 | |
fpizlo@apple.com | a34ff7d | 2012-11-06 19:10:37 +0000 | [diff] [blame] | 307 | inline bool isJSArray(JSValue v) { return v.isCell() && isJSArray(v.asCell()); } |
ggaren@apple.com | c3343bd | 2009-02-24 03:58:09 +0000 | [diff] [blame] | 308 | |
keith_miller@apple.com | 296d5a7 | 2018-06-19 20:09:15 +0000 | [diff] [blame] | 309 | JS_EXPORT_PRIVATE JSArray* constructArray(ExecState*, Structure*, const ArgList& values); |
| 310 | JS_EXPORT_PRIVATE JSArray* constructArray(ExecState*, Structure*, const JSValue* values, unsigned length); |
| 311 | JS_EXPORT_PRIVATE JSArray* constructArrayNegativeIndexed(ExecState*, Structure*, const JSValue* values, unsigned length); |
msaboff@apple.com | b70e41b | 2013-09-13 18:03:55 +0000 | [diff] [blame] | 312 | |
fpizlo@apple.com | 6c89cd3 | 2012-06-26 19:42:05 +0000 | [diff] [blame] | 313 | } // namespace JSC |