Class JSValue

java.lang.Object
org.graalvm.webimage.api.JSValue
Direct Known Subclasses:
JSBigInt, JSBoolean, JSNumber, JSObject, JSString, JSSymbol, JSUndefined

public abstract class JSValue extends Object
Java representation of a JavaScript value.

The subclasses of this class represent JavaScript's six primitive data types and the object data type. The JavaScript Null data type does not have a special representation -- the JavaScript null value is directly mapped to the Java null value.

  • Method Details

    • checkedCoerce

      public static <R> R checkedCoerce(Object value, Class<R> cls)
      Attempts to coerce a given value to the specified Java type.

      If the value is a JSValue, it will be converted using as(Class). Otherwise, the value is cast directly.

      Type Parameters:
      R - the return type
      Parameters:
      value - the value to coerce, which may be a JSValue or a native Java object
      cls - the target Java class to coerce to
      Returns:
      the coerced value as an instance of cls
      Throws:
      ClassCastException - if the coercion fails or is unsupported
    • isUndefined

      public static boolean isUndefined(Object value)
      Checks whether the given value is the JavaScript undefined value.
      See Also:
    • isUndefined

      public static boolean isUndefined(JSValue value)
      Specialization of isUndefined(Object) that avoids the type check for JSValue.
    • isUndefined

      public boolean isUndefined()
      Checks whether this is the JavaScript undefined value.
    • undefined

      public static JSUndefined undefined()
    • typeof

      public abstract String typeof()
    • stringValue

      public final String stringValue()
      Returns the JS string representation of this value (by calling the JS toString method on it) and "undefined" for the JS undefined value.
      Since:
      25.1
    • asBoolean

      public Boolean asBoolean()
    • asByte

      public Byte asByte()
    • asShort

      public Short asShort()
    • asChar

      public Character asChar()
    • asInt

      public Integer asInt()
    • asFloat

      public Float asFloat()
    • asLong

      public Long asLong()
    • asDouble

      public Double asDouble()
    • asBigInteger

      public BigInteger asBigInteger()
    • asString

      public String asString()
    • as

      public <T> T as(Class<T> cls)
      Coerces this JavaScript value to the requested Java type. See JS.Coerce for the JavaScript to Java coercion rules.
      Parameters:
      cls - The Java type to coerce to.
      Returns:
      The non-null coerced value of the requested type.
      Throws:
      ClassCastException - If this value cannot be coerced to the requested type.
    • toString

      public String toString()
      Overrides:
      toString in class Object