1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/*
* Copyright (C) 2016-2022 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#pragma once
#include "ArrayPrototype.h"
#include "ButterflyInlines.h"
#include "ClonedArguments.h"
#include "DirectArguments.h"
#include "Error.h"
#include "JSArray.h"
#include "JSCellInlines.h"
#include "ScopedArguments.h"
#include "Structure.h"
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
inline Structure* JSArray::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype, IndexingType indexingType)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ArrayType, StructureFlags), info(), indexingType);
}
inline IndexingType JSArray::mergeIndexingTypeForCopying(IndexingType other, bool allowPromotion)
{
IndexingType type = indexingType();
if (!(type & IsArray && other & IsArray))
return NonArray;
if (hasAnyArrayStorage(type) || hasAnyArrayStorage(other))
return NonArray;
if (type == ArrayWithUndecided)
return other;
if (other == ArrayWithUndecided)
return type;
// We can memcpy an Int32 and a Contiguous into a Contiguous array since
// both share the same memory layout for Int32 numbers.
if ((type == ArrayWithInt32 || type == ArrayWithContiguous)
&& (other == ArrayWithInt32 || other == ArrayWithContiguous)) {
if (other == ArrayWithContiguous)
return other;
return type;
}
if (allowPromotion) {
if ((type == ArrayWithInt32 || type == ArrayWithDouble) && (other == ArrayWithInt32 || other == ArrayWithDouble)) {
if (type == other)
return type;
return ArrayWithDouble;
}
}
if (type != other)
return NonArray;
return type;
}
ALWAYS_INLINE bool JSArray::holesMustForwardToPrototype() const
{
Structure* structure = this->structure();
if (LIKELY(type() == ArrayType)) {
ASSERT(!structure->mayInterceptIndexedAccesses());
JSGlobalObject* globalObject = structure->globalObject();
if (LIKELY(structure->hasMonoProto() && structure->storedPrototype() == globalObject->arrayPrototype() && globalObject->arrayPrototypeChainIsSane()))
return false;
}
return structure->holesMustForwardToPrototype(const_cast<JSArray*>(this));
}
inline bool JSArray::canFastCopy(JSArray* otherArray) const
{
if (hasAnyArrayStorage(indexingType()) || hasAnyArrayStorage(otherArray->indexingType()))
return false;
if (holesMustForwardToPrototype() || otherArray->holesMustForwardToPrototype())
return false;
return true;
}
inline bool JSArray::canFastAppend(JSArray* otherArray) const
{
// Append can modify itself, thus, we cannot do fast-append if |this| and otherArray are the same.
if (otherArray == this)
return false;
return canFastCopy(otherArray);
}
inline bool JSArray::canDoFastIndexedAccess() const
{
JSGlobalObject* globalObject = this->globalObject();
if (!globalObject->arrayPrototypeChainIsSane())
return false;
Structure* structure = this->structure();
// This is the fast case. Many arrays will be an original array.
if (globalObject->isOriginalArrayStructure(structure))
return true;
if (structure->mayInterceptIndexedAccesses())
return false;
if (getPrototypeDirect() != globalObject->arrayPrototype())
return false;
return true;
}
ALWAYS_INLINE bool JSArray::definitelyNegativeOneMiss() const
{
JSGlobalObject* globalObject = this->globalObject();
if (!globalObject->arrayPrototypeChainIsSane())
return false;
if (!globalObject->arrayNegativeOneWatchpointSet().isStillValid())
return false;
Structure* structure = this->structure();
// This is the fast case. Many arrays will be an original array.
if (globalObject->isOriginalArrayStructure(structure))
return true;
if (getPrototypeDirect() != globalObject->arrayPrototype())
return false;
if (structure->seenProperties().bits())
return false;
return true;
}
ALWAYS_INLINE uint64_t toLength(JSGlobalObject* globalObject, JSObject* object)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (LIKELY(isJSArray(object)))
return jsCast<JSArray*>(object)->length();
switch (object->type()) {
case DirectArgumentsType:
RELEASE_AND_RETURN(scope, jsCast<DirectArguments*>(object)->length(globalObject));
case ScopedArgumentsType:
RELEASE_AND_RETURN(scope, jsCast<ScopedArguments*>(object)->length(globalObject));
case ClonedArgumentsType:
RELEASE_AND_RETURN(scope, jsCast<ClonedArguments*>(object)->length(globalObject));
default:
break;
}
JSValue lengthValue = object->get(globalObject, vm.propertyNames->length);
RETURN_IF_EXCEPTION(scope, { });
RELEASE_AND_RETURN(scope, lengthValue.toLength(globalObject));
}
ALWAYS_INLINE void JSArray::pushInline(JSGlobalObject* globalObject, JSValue value)
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ensureWritable(vm);
Butterfly* butterfly = this->butterfly();
switch (indexingMode()) {
case ArrayClass: {
createInitialUndecided(vm, 0);
FALLTHROUGH;
}
case ArrayWithUndecided: {
convertUndecidedForValue(vm, value);
scope.release();
push(globalObject, value);
return;
}
case ArrayWithInt32: {
if (!value.isInt32()) {
convertInt32ForValue(vm, value);
scope.release();
push(globalObject, value);
return;
}
unsigned length = butterfly->publicLength();
ASSERT(length <= butterfly->vectorLength());
if (length < butterfly->vectorLength()) {
butterfly->contiguousInt32().at(this, length).setWithoutWriteBarrier(value);
butterfly->setPublicLength(length + 1);
return;
}
if (UNLIKELY(length > MAX_ARRAY_INDEX)) {
methodTable()->putByIndex(this, globalObject, length, value, true);
if (!scope.exception())
throwException(globalObject, scope, createRangeError(globalObject, LengthExceededTheMaximumArrayLengthError));
return;
}
scope.release();
putByIndexBeyondVectorLengthWithoutAttributes<Int32Shape>(globalObject, length, value);
return;
}
case ArrayWithContiguous: {
unsigned length = butterfly->publicLength();
ASSERT(length <= butterfly->vectorLength());
if (length < butterfly->vectorLength()) {
butterfly->contiguous().at(this, length).setWithoutWriteBarrier(value);
butterfly->setPublicLength(length + 1);
vm.writeBarrier(this, value);
return;
}
if (UNLIKELY(length > MAX_ARRAY_INDEX)) {
methodTable()->putByIndex(this, globalObject, length, value, true);
if (!scope.exception())
throwException(globalObject, scope, createRangeError(globalObject, LengthExceededTheMaximumArrayLengthError));
return;
}
scope.release();
putByIndexBeyondVectorLengthWithoutAttributes<ContiguousShape>(globalObject, length, value);
return;
}
case ArrayWithDouble: {
ASSERT(Options::allowDoubleShape());
if (!value.isNumber()) {
convertDoubleToContiguous(vm);
scope.release();
push(globalObject, value);
return;
}
double valueAsDouble = value.asNumber();
if (valueAsDouble != valueAsDouble) {
convertDoubleToContiguous(vm);
scope.release();
push(globalObject, value);
return;
}
unsigned length = butterfly->publicLength();
ASSERT(length <= butterfly->vectorLength());
if (length < butterfly->vectorLength()) {
butterfly->contiguousDouble().at(this, length) = valueAsDouble;
butterfly->setPublicLength(length + 1);
return;
}
if (UNLIKELY(length > MAX_ARRAY_INDEX)) {
methodTable()->putByIndex(this, globalObject, length, value, true);
if (!scope.exception())
throwException(globalObject, scope, createRangeError(globalObject, LengthExceededTheMaximumArrayLengthError));
return;
}
scope.release();
putByIndexBeyondVectorLengthWithoutAttributes<DoubleShape>(globalObject, length, value);
return;
}
case ArrayWithSlowPutArrayStorage: {
unsigned oldLength = length();
bool putResult = false;
bool result = attemptToInterceptPutByIndexOnHole(globalObject, oldLength, value, true, putResult);
RETURN_IF_EXCEPTION(scope, void());
if (result) {
if (oldLength < 0xFFFFFFFFu) {
scope.release();
setLength(globalObject, oldLength + 1, true);
}
return;
}
FALLTHROUGH;
}
case ArrayWithArrayStorage: {
ArrayStorage* storage = butterfly->arrayStorage();
// Fast case - push within vector, always update m_length & m_numValuesInVector.
unsigned length = storage->length();
if (length < storage->vectorLength()) {
storage->m_vector[length].set(vm, this, value);
storage->setLength(length + 1);
++storage->m_numValuesInVector;
return;
}
// Pushing to an array of invalid length (2^31-1) stores the property, but throws a range error.
if (UNLIKELY(storage->length() > MAX_ARRAY_INDEX)) {
methodTable()->putByIndex(this, globalObject, storage->length(), value, true);
// Per ES5.1 15.4.4.7 step 6 & 15.4.5.1 step 3.d.
if (!scope.exception())
throwException(globalObject, scope, createRangeError(globalObject, LengthExceededTheMaximumArrayLengthError));
return;
}
// Handled the same as putIndex.
scope.release();
putByIndexBeyondVectorLengthWithArrayStorage(globalObject, storage->length(), value, true, storage);
return;
}
default:
RELEASE_ASSERT_NOT_REACHED();
}
}
ALWAYS_INLINE JSValue getProperty(JSGlobalObject* globalObject, JSObject* object, uint64_t index)
{
if (JSValue result = object->tryGetIndexQuickly(index))
return result;
// Don't return undefined if the property is not found.
return object->getIfPropertyExists(globalObject, index);
}
ALWAYS_INLINE bool isHole(double value)
{
return std::isnan(value);
}
ALWAYS_INLINE bool isHole(const WriteBarrier<Unknown>& value)
{
return !value;
}
template<typename T>
ALWAYS_INLINE bool containsHole(const T* data, unsigned length)
{
for (unsigned i = 0; i < length; ++i) {
if (isHole(data[i]))
return true;
}
return false;
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|