Class Value

java.lang.Object
org.graalvm.polyglot.Value

public final class Value extends Object
Represents a polyglot value that can be accessed using a set of language agnostic operations. Polyglot values represent values from host or guest language. Polyglot values are bound to a context. If the context is closed then all value operations throw an IllegalStateException.

Polyglot values have one of the following type combinations:

In addition any value may have one or more of the following traits:
  • Array Elements: This value may contain array elements. The array indices always start with 0, also if the language uses a different style.
  • Members: This value may contain members. Members are structural elements of an object. For example, the members of a Java object are all public methods and fields. Members are accessible using getMember(String).
  • Executable: This value can be executed. This indicates that the value represents an element that can be executed. Guest language examples for executable elements are functions, methods, closures or promises.
  • Instantiable: This value can be instantiated. For example, Java classes are instantiable.
  • Buffer Elements: This value may contain buffer elements. The buffer indices always start with 0, also if the language uses a different style.
  • Iterable: This value provides an iterator which can be used to iterate value elements. For example, Guest language arrays are iterable.
  • hasHashEntries() Hash Entries}: This value represents a map.
  • hasMetaParents() Meta Parents}: This value represents Array Elements of Meta Objects.

In addition to the language agnostic types, the language specific type can be accessed using getMetaObject(). The identity of value objects is unspecified and should not be relied upon. For example, multiple calls to getArrayElement(long) with the same index might return the same or different instances of Value. The equality of values is based on the identity of the value instance. All values return a human-readable string for debugging, formatted by the original language.

Polyglot values may be converted to host objects using as(Class). In addition values may be created from Java values using Context.asValue(Object).

Naive and aware dates and times

If a date or time value has a timezone then it is called aware, otherwise naive.

An aware time and date has sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, to locate itself relative to other aware objects. An aware object is used to represent a specific moment in time that is not open to interpretation.

A naive time and date does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it is up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.

Scoped Values

In the case of a guest-to-host callback, a value may be passed as a parameter. These values may represent objects that are only valid during the invocation of the callback function, i.e. they are scoped, with the scope being the callback function. If enabled via the corresponding settings in HostAccess, such values are released when the function returns, with all future invocations of value operations throwing an exception. If an embedder wishes to extend the scope of the value beyond the callback's return, the value can be pinned, such that it is not released automatically.
Since:
19.0
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    as(Class<T> targetType)
    Maps a polyglot value to a value with a given Java target type.
    <T> T
    as(TypeLiteral<T> targetType)
    Maps a polyglot value to a given Java target type literal.
    Returns a BigInteger representation of this value if it is number and the value fits.
    boolean
    Returns a boolean representation of this value if it is boolean.
    byte
    Returns a byte representation of this value if it is number and the value fits.
    Returns this value as date if this object represents a date.
    double
    Returns a double representation of this value if it is number and the value fits.
    Returns this value as duration if this object represents a duration.
    float
    Returns a float representation of this value if it is number and the value fits.
    <T> T
    Returns the original Java host language object.
    Returns this value as instant if this object represents an instant.
    int
    Returns an int representation of this value if it is number and the value fits.
    long
    Returns a long representation of this value if it is number and the value fits.
    long
    Returns the value of the pointer as long value.
    <T extends Proxy>
    T
    Returns the unboxed instance of the Proxy.
    short
    Returns a short representation of this value if it is number and the value fits.
    Returns the String value if this value is a string.
    Returns this value as time if this object represents a time.
    Returns this value as timestamp if this object represents a timezone.
    static Value
    Converts a Java host value to a polyglot value.
    boolean
    Returns true if the value can be executed.
    boolean
    Returns true if the value can be instantiated.
    boolean
    canInvokeMember(String identifier)
    Returns true if the given member exists and can be invoked.
    boolean
    Compares the identity of the underlying polyglot objects.
    execute(Object... arguments)
    Executes this value if it can be executed and returns its result.
    void
    executeVoid(Object... arguments)
    Executes this value if it can be executed.
    boolean
    Returns true if this value represents a number and the value fits in BigInteger, else false.
    boolean
    Returns true if this value represents a number and the value fits in byte, else false.
    boolean
    Returns true if this value represents a number and the value fits in double, else false.
    boolean
    Returns true if this value represents a number and the value fits in float, else false.
    boolean
    Returns true if this value represents a number and the value fits in int, else false.
    boolean
    Returns true if this value represents a number and the value fits in long, else false.
    boolean
    Returns true if this value represents a number and the value fits in short, else false.
    getArrayElement(long index)
    Returns the array element of a given index.
    long
    Returns the array size for values with array elements.
    long
    Returns the buffer size in bytes for values with buffer elements.
    Returns the context this value was created with.
    Creates a new hash entries iterator that allows read each map entry.
    Creates a new hash keys iterator that allows read each map key.
    long
    Returns the number of map entries for values with hash entries.
    Returns the value for the specified key or null if the mapping for the specified key does not exist.
    getHashValueOrDefault(Object key, Object defaultValue)
    Returns the value for the specified key or the default value if the mapping for the specified key does not exist or is not readable.
    Creates a new hash values iterator that allows read each map value.
    Creates a new iterator that allows read each element of a sequence.
    Returns the next element in the iteration.
    getMember(String identifier)
    Returns the member with a given identifier or null if the member does not exist.
    Returns a set of all member keys.
    Returns the metaobject that is associated with this value or null if no metaobject is available.
    Returns the meta parents of a meta object as an array object hasArrayElements().
    Returns the qualified name of a metaobject as String.
    Returns the simple name of a metaobject as string.
    Returns the declared source location of the value.
    boolean
    Returns true if this polyglot value has array elements.
    boolean
    Returns true if the receiver may have buffer elements.
    boolean
    Returns true if this polyglot value represents a map.
    boolean
    Returns true if mapping for the specified key exists.
    int
    Returns the identity hash code of the underlying object.
    boolean
    Returns true if this polyglot value provides an iterator.
    boolean
    Returns true if the value represents an iterator which has more elements, else false.
    boolean
    hasMember(String identifier)
    Returns true if such a member exists for a given identifier.
    boolean
    Returns true if this value generally supports containing members.
    boolean
    Returns true if the value represents a metaobject and the metaobject has meta parents.
    invokeMember(String identifier, Object... arguments)
    Invokes the given member of this value.
    boolean
    Returns true if this value represents a boolean value.
    boolean
    Returns true if the receiver object is a modifiable buffer.
    boolean
    Returns true if this object represents a date, else false.
    boolean
    Returns true if this object represents a duration, else false.
    boolean
    Returns true if this object represents an exception, else false.
    boolean
    Returns true if the value originated form the host language Java.
    boolean
    Returns true if this value represents an instant.
    boolean
    Returns true if the value represents an iterator object.
    boolean
    Returns true if the given instance is an instance of this value, else false.
    boolean
    Returns true if the value represents a metaobject.
    boolean
    Returns true if this value is a native pointer.
    boolean
    Returns true if this value is a null like.
    boolean
    Returns true if this value represents a number, else false.
    boolean
    Returns true if this value represents a Proxy.
    boolean
    Returns true if this value represents a string.
    boolean
    Returns true if this object represents a time, else false.
    boolean
    Returns true if this object represents a timezone, else false.
    newInstance(Object... arguments)
    Instantiates this value if it can be instantiated.
    void
    pin()
    Pins a scoped value such that it can be used beyond the scope of a scoped host method call.
    void
    putHashEntry(Object key, Object value)
    Associates the specified value with the specified key.
    void
    putMember(String identifier, Object value)
    Sets the value of a member using an identifier.
    void
    readBuffer(long byteOffset, byte[] destination, int destinationOffset, int length)
    Reads bytes from the receiver object into the specified byte array.
    byte
    readBufferByte(long byteOffset)
    Reads the byte at the given byte offset from the start of the buffer.
    double
    readBufferDouble(ByteOrder order, long byteOffset)
    Reads the double at the given byte offset from the start of the buffer in the given byte order.
    float
    readBufferFloat(ByteOrder order, long byteOffset)
    Reads the float at the given byte offset from the start of the buffer in the given byte order.
    int
    readBufferInt(ByteOrder order, long byteOffset)
    Reads the int at the given byte offset from the start of the buffer in the given byte order.
    long
    readBufferLong(ByteOrder order, long byteOffset)
    Reads the long at the given byte offset from the start of the buffer in the given byte order.
    short
    readBufferShort(ByteOrder order, long byteOffset)
    Reads the short at the given byte offset from the start of the buffer in the given byte order.
    boolean
    removeArrayElement(long index)
    Removes an array element at a given index.
    boolean
    Removes the mapping for a given key.
    boolean
    removeMember(String identifier)
    Removes a single member from the object.
    void
    setArrayElement(long index, Object value)
    Sets the value at a given index.
    Throws this value if this object represents an exception.
    Converts this value to a human readable string.
    void
    writeBufferByte(long byteOffset, byte value)
    Writes the given byte at the given byte offset from the start of the buffer.
    void
    writeBufferDouble(ByteOrder order, long byteOffset, double value)
    Writes the given double in the given byte order at the given byte offset from the start of the buffer.
    void
    writeBufferFloat(ByteOrder order, long byteOffset, float value)
    Writes the given float in the given byte order at the given byte offset from the start of the buffer.
    void
    writeBufferInt(ByteOrder order, long byteOffset, int value)
    Writes the given int in the given byte order at the given byte offset from the start of the buffer.
    void
    writeBufferLong(ByteOrder order, long byteOffset, long value)
    Writes the given long in the given byte order at the given byte offset from the start of the buffer.
    void
    writeBufferShort(ByteOrder order, long byteOffset, short value)
    Writes the given short in the given byte order at the given byte offset from the start of the buffer.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait