com.oracle.truffle.js.runtime.array.ScriptArray Maven / Gradle / Ivy
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.js.runtime.array;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.dsl.Idempotent;
import com.oracle.truffle.api.dsl.InlineSupport;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.frame.FrameInstance;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.js.lang.JavaScriptLanguage;
import com.oracle.truffle.js.runtime.Errors;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.JSException;
import com.oracle.truffle.js.runtime.Strings;
import com.oracle.truffle.js.runtime.array.dyn.AbstractConstantArray;
import com.oracle.truffle.js.runtime.array.dyn.ConstantEmptyArray;
import com.oracle.truffle.js.runtime.array.dyn.ConstantObjectArray;
import com.oracle.truffle.js.runtime.objects.JSDynamicObject;
import com.oracle.truffle.js.runtime.objects.Undefined;
import com.oracle.truffle.js.runtime.util.InlinedProfileBag;
public abstract class ScriptArray {
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
public abstract Object getElement(JSDynamicObject object, long index);
public abstract Object getElementInBounds(JSDynamicObject object, long index);
public abstract ScriptArray setElementImpl(JSDynamicObject object, long index, Object value, boolean strict);
public final ScriptArray setElement(JSDynamicObject object, long index, Object value, boolean strict) {
if (isFrozen()) {
if (strict) {
setElementFrozenStrict(index);
}
return this;
} else if (isLengthNotWritable()) {
if (index >= length(object)) {
if (strict) {
throw Errors.createTypeErrorLengthNotWritable();
}
return this;
}
}
return setElementImpl(object, index, value, strict);
}
@TruffleBoundary
private static void setElementFrozenStrict(long index) {
JSContext context = JavaScriptLanguage.getCurrentLanguage().getJSContext();
if (context.isOptionNashornCompatibilityMode()) {
throw Errors.createTypeErrorFormat("Cannot set property \"%d\" of frozen array", index);
} else {
throw Errors.createTypeErrorCannotRedefineProperty(index);
}
}
public abstract ScriptArray deleteElementImpl(JSDynamicObject object, long index, boolean strict);
public final ScriptArray deleteElement(JSDynamicObject object, long index, boolean strict) {
assert canDeleteElement(object, index, strict);
return deleteElementImpl(object, index, strict);
}
public final boolean canDeleteElement(JSDynamicObject object, long index, boolean strict) {
if (isSealed()) {
if (hasElement(object, index)) {
if (strict) {
throw Errors.createTypeErrorCannotDeletePropertyOfSealedArray(index);
}
return false;
}
}
return true;
}
/**
* @return true if array has an element (not a hole) at this index.
*/
public abstract boolean hasElement(JSDynamicObject object, long index);
public abstract long length(JSDynamicObject object);
public abstract int lengthInt(JSDynamicObject object);
public static class CreateWritableProfileAccess extends InlinedProfileBag {
protected static final int REQUIRED_BITS = 4 * CONDITION_PROFILE_STATE_BITS;
private static final int newArrayLengthZero;
private static final int newArrayLengthBelowLimit;
private static final int indexZero;
private static final int indexLessThanLength;
static {
try (var b = new Builder(REQUIRED_BITS)) {
newArrayLengthZero = b.conditionProfile();
newArrayLengthBelowLimit = b.conditionProfile();
indexZero = b.conditionProfile();
indexLessThanLength = b.conditionProfile();
}
}
private static final CreateWritableProfileAccess UNCACHED = new CreateWritableProfileAccess(null);
@NeverDefault
public static CreateWritableProfileAccess getUncached() {
return UNCACHED;
}
@NeverDefault
public static CreateWritableProfileAccess inline(
@InlineSupport.RequiredField(value = InlineSupport.StateField.class, bits = REQUIRED_BITS) InlineSupport.InlineTarget inlineTarget) {
return new CreateWritableProfileAccess(inlineTarget.getState(0, REQUIRED_BITS));
}
protected CreateWritableProfileAccess(InlineSupport.StateField stateField) {
super(stateField);
}
public boolean newArrayLengthZero(Node node, boolean condition) {
return profile(node, condition, newArrayLengthZero);
}
public final boolean newArrayLengthBelowLimit(Node node, boolean condition) {
return profile(node, condition, newArrayLengthBelowLimit);
}
public final boolean indexZero(Node node, boolean condition) {
return profile(node, condition, indexZero);
}
public final boolean indexLessThanLength(Node node, boolean condition) {
return profile(node, condition, indexLessThanLength);
}
}
public static class SetLengthProfileAccess extends CreateWritableProfileAccess {
private static final int REQUIRED_BITS = 8 * CONDITION_PROFILE_STATE_BITS;
protected static final int TOTAL_REQUIRED_BITS = CreateWritableProfileAccess.REQUIRED_BITS + REQUIRED_BITS;
private static final int lengthZero;
private static final int lengthLess;
private static final int zeroBasedSetUsedLength;
private static final int zeroBasedClearUnusedArea;
private static final int contiguousZeroUsed;
private static final int contiguousNegativeUsed;
private static final int contiguousShrinkUsed;
private static final int clearUnusedArea;
static {
try (var b = new Builder(CreateWritableProfileAccess.REQUIRED_BITS, REQUIRED_BITS)) {
lengthZero = b.conditionProfile();
lengthLess = b.conditionProfile();
zeroBasedSetUsedLength = b.conditionProfile();
zeroBasedClearUnusedArea = b.conditionProfile();
contiguousZeroUsed = b.conditionProfile();
contiguousNegativeUsed = b.conditionProfile();
contiguousShrinkUsed = b.conditionProfile();
clearUnusedArea = b.conditionProfile();
}
}
private static final SetLengthProfileAccess UNCACHED = new SetLengthProfileAccess(null);
@NeverDefault
public static SetLengthProfileAccess getUncached() {
return UNCACHED;
}
@NeverDefault
public static SetLengthProfileAccess inline(
@InlineSupport.RequiredField(value = InlineSupport.StateField.class, bits = TOTAL_REQUIRED_BITS) InlineSupport.InlineTarget inlineTarget) {
return new SetLengthProfileAccess(inlineTarget.getState(0, TOTAL_REQUIRED_BITS));
}
protected SetLengthProfileAccess(InlineSupport.StateField stateField) {
super(stateField);
}
public final boolean lengthZero(Node node, boolean condition) {
return profile(node, condition, lengthZero);
}
public final boolean lengthLess(Node node, boolean condition) {
return profile(node, condition, lengthLess);
}
public final boolean zeroBasedSetUsedLength(Node node, boolean condition) {
return profile(node, condition, zeroBasedSetUsedLength);
}
public final boolean zeroBasedClearUnusedArea(Node node, boolean condition) {
return profile(node, condition, zeroBasedClearUnusedArea);
}
public final boolean contiguousZeroUsed(Node node, boolean condition) {
return profile(node, condition, contiguousZeroUsed);
}
public final boolean contiguousNegativeUsed(Node node, boolean condition) {
return profile(node, condition, contiguousNegativeUsed);
}
public final boolean contiguousShrinkUsed(Node node, boolean condition) {
return profile(node, condition, contiguousShrinkUsed);
}
public final boolean clearUnusedArea(Node node, boolean condition) {
return profile(node, condition, clearUnusedArea);
}
}
public abstract ScriptArray setLengthImpl(JSDynamicObject object, long len, Node node, SetLengthProfileAccess profile);
public final ScriptArray setLength(JSDynamicObject object, long len, boolean strict, Node node, SetLengthProfileAccess profile) {
if (isLengthNotWritable()) {
if (strict) {
throw Errors.createTypeErrorLengthNotWritable();
}
return this;
} else if (isSealed()) {
assert len >= lastElementIndex(object) + 1; // to be checked by caller
}
return setLengthImpl(object, len, node, profile);
}
public final ScriptArray setLength(JSDynamicObject object, long len, boolean strict) {
return setLength(object, len, strict, null, SetLengthProfileAccess.getUncached());
}
/**
* First element index (inclusive).
*/
public abstract long firstElementIndex(JSDynamicObject object);
/**
* Last element index (inclusive).
*/
public abstract long lastElementIndex(JSDynamicObject object);
/**
* Returns the next index. The index is guaranteed either to exist, or be MAX_SAFE_INTEGER.
* Reason for MAX_SAFE_INTEGER is: this array could be the prototype of another one; returning
* the length() of this array would be wrong, if the inheriting array is longer, but has a hole
* at length().
*/
public abstract long nextElementIndex(JSDynamicObject object, long index);
/**
* Returns the previous index. The index is guaranteed either to exist, or be smaller than
* firstElementIndex().
*/
public abstract long previousElementIndex(JSDynamicObject object, long index);
/**
* Range check only, might be a hole depending on array type.
*/
public boolean isInBoundsFast(JSDynamicObject object, long index) {
return firstElementIndex(object) <= index && index <= lastElementIndex(object);
}
public Iterable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy