Class Shape
java.lang.Object
com.oracle.truffle.api.object.Shape
A Shape is an immutable descriptor of the current object "shape" of a DynamicObject, i.e., object
layout, metadata (type, flags),
and a mapping of properties to storage locations. This allows cached
DynamicObjectLibrary to do a simple shape check to determine the contents of an object
and do fast, constant-time property accesses. Shape changes, like adding or removing a property,
yield a new Shape derived from the old one.
Shapes are shared between objects that assume the same shape if they follow the same shape transitions, like adding the same properties in the same order, starting from a common root shape. Shape transitions are automatically, weakly cached.
Dynamic objects start off with an initial shape that has no instance properties (but may have
constant properties that are stored in the shape). Initial shapes are created via
newBuilder().
- Since:
- 0.8 or earlier
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionprotected ShapeaddProperty(Property property) Add a new property in the map, yielding a new or cached Shape object.booleanallPropertiesMatch(Predicate<Property> predicate) Tests if all properties in the shape match the provided predicate.booleancheck(DynamicObject subject) Checks whether the given object's shape is identical to this shape.defineProperty(Object key, Object value, int propertyFlags) Deprecated.protected ShapedefineProperty(Object key, Object value, int propertyFlags, int putFlags) Add or change property in the map, yielding a new or cached Shape object.Get the shape's dynamic object type identifier.Gets the first property.intgetFlags()Returns the language-specific shape flags previously set usingDynamicObjectLibrary.setShapeFlags(DynamicObject, int)orShape.Builder.shapeFlags(int).Get a list of all property keys in insertion order.getKeys()Get all property keys in insertion order.Gets the last property.Class<? extends DynamicObject> Get the shape's layout class.Get an assumption that the shape is a leaf.AnIterableover the shape's properties in insertion order.getProperty(Object key) Get a property entry by key.Gets a stable property assumption for the given property key.intReturns the number of properties in this shape.Get a list of all properties that this Shape stores.getPropertyListInternal(boolean ascending) Returns all (also hidden) property objects in this shape.getRoot()Get the root shape.Get the shape's shared data.Get an assumption that the shape is valid.protected booleanReturnstrueif this shape has instance properties (i.e., stored in the object).booleanhasProperty(Object name) Check whether the shape has a property with the given key.booleanisLeaf()Check whether this shape is a leaf in the transition graph, i.e.booleanisShared()Returnstrueif this shape is marked as shared.booleanisValid()Check whether this shape is valid.makePropertyGetter(Object key) Makes a property getter for this shape and the given property key, if it exists.Make a shared variant of this shape, to allow safe usage of this object between threads.static Shape.BuilderCreates a new initial shape builder.static Shape.DerivedBuildernewBuilder(Shape baseShape) Creates a new derived shape builder that allows changing a root shape's flags and dynamic type and adding constant properties.protected ShaperemoveProperty(Property property) Remove the given property from the shape.protected ShapereplaceProperty(Property oldProperty, Property newProperty) Replace a property in the shape.protected ShapesetDynamicType(Object dynamicType) Returns a copy of the shape, with the dynamic object type identifier set todynamicType.protected ShapesetFlags(int objectFlags) Returns a copy of the shape, with the shape flags set tonewFlags.toString()Returns a string representation of the object.Try to merge two related shapes to a more general shape that has the same properties and can store at least the values of both shapes.
-
Method Details
-
newBuilder
Creates a new initial shape builder. The builder instance is not thread-safe and must not be used from multiple threads at the same time.- Since:
- 20.2.0
-
newBuilder
Creates a new derived shape builder that allows changing a root shape's flags and dynamic type and adding constant properties. The builder instance is not thread-safe and must not be used from multiple threads at the same time.- Parameters:
baseShape- the shape to be modified- Since:
- 20.2.0
- See Also:
-
getProperty
-
addProperty
-
defineProperty
Deprecated.Add or change property in the map, yielding a new or cached Shape object.- Returns:
- the shape after defining the property
- Since:
- 0.8 or earlier
-
defineProperty
-
getProperties
-
getPropertyList
-
getPropertyListInternal
-
getKeyList
-
getKeys
-
getValidAssumption
Get an assumption that the shape is valid.- Since:
- 0.8 or earlier
-
isValid
public boolean isValid()Check whether this shape is valid.- Since:
- 0.8 or earlier
-
getLeafAssumption
Get an assumption that the shape is a leaf.- Since:
- 0.8 or earlier
-
isLeaf
public boolean isLeaf()Check whether this shape is a leaf in the transition graph, i.e. transitionless.- Since:
- 0.8 or earlier
-
hasProperty
Check whether the shape has a property with the given key.- Since:
- 0.8 or earlier
-
removeProperty
-
replaceProperty
-
getLastProperty
Gets the last property.- Returns:
- the last property in the shape or
nullif empty - Since:
- 0.8 or earlier
-
getFirstProperty
Gets the first property.- Returns:
- the first property in the shape or
nullif empty - Since:
- 25.1
-
getFlags
public int getFlags()Returns the language-specific shape flags previously set usingDynamicObjectLibrary.setShapeFlags(DynamicObject, int)orShape.Builder.shapeFlags(int). If no shape flags were explicitly set, the default of 0 is returned. These flags may be used to tag objects that possess characteristics that need to be queried efficiently on fast and slow paths. For example, they can be used to mark objects as frozen.- Since:
- 20.2.0
- See Also:
-
setFlags
Returns a copy of the shape, with the shape flags set tonewFlags.- Parameters:
objectFlags- the new shape flags; an int value in the range from 0 to 65535 (inclusive)- Throws:
IllegalArgumentException- if the flags value is not in the supported range- Since:
- 20.2.0
- See Also:
-
getPropertyCount
public int getPropertyCount()Returns the number of properties in this shape.- Since:
- 0.8 or earlier
-
getDynamicType
-
setDynamicType
Returns a copy of the shape, with the dynamic object type identifier set todynamicType.- Parameters:
dynamicType- the new dynamic object type identifier- Throws:
NullPointerException- if the argument is null.- Since:
- 20.2.0
- See Also:
-
getRoot
-
check
Checks whether the given object's shape is identical to this shape.- Since:
- 0.8 or earlier
-
getLayoutClass
Get the shape's layout class.- Since:
- 21.1
- See Also:
-
tryMerge
-
hasInstanceProperties
protected boolean hasInstanceProperties()Returnstrueif this shape has instance properties (i.e., stored in the object).- Since:
- 20.2.0
-
getPropertyAssumption
Gets a stable property assumption for the given property key. May be invalid. If a valid assumption is returned, it may be used to assume this particular property is still absent or present at the current storage location in objects of this shape. The assumption is invalidated if a shape change is triggered because of a property with the given key was added, removed, or changed, or aresetShape.Only applicable if property assumptions are enabled for this shape, otherwise always returns an invalid assumption.
- Parameters:
key- the property key of interest- Returns:
- an assumption that the property is stable or an invalid assumption
- Since:
- 20.2.0
- See Also:
-
allPropertiesMatch
Tests if all properties in the shape match the provided predicate. May not evaluate the predicate on all properties if a predicate did not match. If the shape does not contain any properties, returnstrueand does not evaluate the predicate.- Returns:
trueif the all properties match the predicate, elsefalse- Since:
- 20.2.0
-
makePropertyGetter
Makes a property getter for this shape and the given property key, if it exists. Otherwise, returnsnull. Note that the returnedPropertyGetteronly accepts objects of this particularShape.- Parameters:
key- the identifier to look up- Returns:
- a
PropertyGetter, ornullif the property was not found in this shape - Since:
- 22.2
-
toString
-
DynamicObjectLibrary.put(DynamicObject, Object, Object)orDynamicObjectLibrary.putWithFlags(DynamicObject, Object, Object, int).