Interface TSObject

All Superinterfaces:
org.teavm.jso.JSObject

public interface TSObject extends org.teavm.jso.JSObject
Any JavaScript value, reachable by name.

This is the floor of the generated npm bindings: a member the generator could not type, a declaration written as any, or a package with no type information at all binds as a TSObject, so nothing is ever unreachable from Java. It is also the body type of a Queue message, which arrives as an arbitrary structured-clone value.

Nothing is copied. This is the JavaScript value itself, and the accessors read through to it.

 TSObject config = module.get("config");
 int port = config.get("port").asInt();
 String host = config.get("host").asString();

Numbers

JavaScript has two numeric types and Java has six. Everything except long maps to Number, which holds every int, short, byte, float and double exactly. long maps to BigInt, because a Number loses precision above 2^53 and a Java long goes well past it.

So of(long) produces a BigInt and asLong() reads one, while asInt() and its siblings read a Number. Each reader accepts either kind and converts, so a value that arrived as a Number still reads correctly through asLong().

Out-of-range conversions follow Java's own rules: asLong() and asInt() saturate at the type's bounds, and asShort(), asByte() and asChar() truncate the low bits, which is what a Java cast does.

Since:
1.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    add(TSObject value)
    Adds to this Set.
    static TSObject
    Returns a new empty array.
    static TSObject
    array(Collection<?> values)
    An array from any collection.
    default boolean
    Returns this value's truthiness.
    default byte
    Returns this value as a byte, truncating the low 8 bits like a Java cast.
    default char
    Returns this value as a char.
    default double
    Returns this value as a double.
    default List<Double>
    Returns the elements of this array read as doubles.
    default float
    Returns this value as a float, narrowed the way a Java cast narrows.
    default int
    Returns this value as an int, saturating at the bounds like a Java cast.
    default List<Integer>
    Returns the elements of this array read as ints.
    default List<TSObject>
    Returns the elements of this array, Set or other iterable.
    default long
    Returns this value as a long.
    default List<Long>
    Returns the elements of this array read as longs.
    Returns this object's own properties, or a Map's entries.
    default Set<TSObject>
    Returns the elements of this array or Set, in encounter order, without duplicates.
    default short
    Returns this value as a short, truncating the low 16 bits like a Java cast.
    default String
    Returns this value coerced to a string, the way String(value) would.
    default List<String>
    Returns the elements of this array read as strings.
    at(int index)
    Reads one element of an array.
    default TSObject
    call(String method, TSObject... args)
    Calls one of this object's methods.
    static TSObject
    from(Object value)
    Converts any Java value, deciding by its runtime type.
    static TSObject
    Parses JSON.
    get(String name)
    Reads one property.
    default boolean
    Returns whether this value is a JavaScript array.
    default boolean
    Returns whether this value is a BigInt, which is how a Java long crosses over.
    default boolean
    Returns whether this value is a boolean.
    default boolean
    Returns whether this value is callable.
    default boolean
    Returns whether this value is a JavaScript Map, not a plain object.
    default boolean
    Returns whether this value is null or undefined.
    default boolean
    Returns whether this value is a Number, which is every Java numeric but long.
    default boolean
    Returns whether this value is a JavaScript Set.
    default boolean
    Returns whether this value is a string.
    default boolean
    Returns whether this value is undefined specifically.
    default List<String>
    Returns this object's own enumerable property names, or a Map's keys.
    default int
    Returns how many elements this holds.
    static TSObject
    map(Map<String,?> values)
    A real JavaScript Map, for an API that wants one.
    static TSObject
    Returns JavaScript null.
    static TSObject
    Returns a new empty object.
    static TSObject
    object(Map<String,?> values)
    A plain object from a map, which is the shape JSON and a structured clone both use.
    static TSObject
    of(boolean value)
     
    static TSObject
    of(byte value)
     
    static TSObject
    of(char value)
     
    static TSObject
    of(double value)
     
    static TSObject
    of(float value)
     
    static TSObject
    of(int value)
     
    static TSObject
    of(long value)
    A BigInt, because a Number cannot hold every long.
    static TSObject
    of(short value)
     
    static TSObject
    of(String value)
     
    static TSObject
    of(org.teavm.jso.JSObject value)
    Wraps any JavaScript value.
    static TSObject
    ofNumber(long value)
    A long as a Number, for a JavaScript API that will not take a BigInt.
    default void
    push(TSObject value)
    Appends to this array.
    default void
    put(TSObject key, TSObject value)
    Writes into this Map, which takes any key rather than only a string.
    void
    set(String name, TSObject value)
    Writes one property.
    static TSObject
    set(Collection<?> values)
    A real JavaScript Set, for an API that wants one.
    default Stream<TSObject>
    Returns the elements of this array or Set as a stream.
    default String
    Returns this value serialised as JSON.
    default String
    Returns the result of JavaScript's typeof on this value.
    static TSObject
    Returns JavaScript undefined, which an absent property reads as.

    Methods inherited from interface org.teavm.jso.JSObject

    cast
  • Method Details

    • get

      TSObject get(String name)
      Reads one property.
      Parameters:
      name - the property name
      Returns:
      the value, which is never null for a property that is present and holds null; use isNull() to tell those apart
    • set

      void set(String name, TSObject value)
      Writes one property.
      Parameters:
      name - the property name
      value - the value
    • at

      TSObject at(int index)
      Reads one element of an array.
      Parameters:
      index - the index
      Returns:
      the element
    • typeOf

      default String typeOf()
      Returns the result of JavaScript's typeof on this value.
      Returns:
      the result of JavaScript's typeof on this value
    • isNull

      default boolean isNull()
      Returns whether this value is null or undefined.
      Returns:
      whether this value is null or undefined
    • isUndefined

      default boolean isUndefined()
      Returns whether this value is undefined specifically.
      Returns:
      whether this value is undefined specifically
    • isArray

      default boolean isArray()
      Returns whether this value is a JavaScript array.
      Returns:
      whether this value is a JavaScript array
    • isNumber

      default boolean isNumber()
      Returns whether this value is a Number, which is every Java numeric but long.
      Returns:
      whether this value is a Number, which is every Java numeric but long
    • isBigInt

      default boolean isBigInt()
      Returns whether this value is a BigInt, which is how a Java long crosses over.
      Returns:
      whether this value is a BigInt, which is how a Java long crosses over
    • isString

      default boolean isString()
      Returns whether this value is a string.
      Returns:
      whether this value is a string
    • isBoolean

      default boolean isBoolean()
      Returns whether this value is a boolean.
      Returns:
      whether this value is a boolean
    • isFunction

      default boolean isFunction()
      Returns whether this value is callable.
      Returns:
      whether this value is callable
    • isMap

      default boolean isMap()
      Returns whether this value is a JavaScript Map, not a plain object.
      Returns:
      whether this value is a JavaScript Map, not a plain object
    • isSet

      default boolean isSet()
      Returns whether this value is a JavaScript Set.
      Returns:
      whether this value is a JavaScript Set
    • asString

      default String asString()
      Returns this value coerced to a string, the way String(value) would.
      Returns:
      this value coerced to a string, the way String(value) would
    • asDouble

      default double asDouble()
      Returns this value as a double.
      Returns:
      this value as a double
    • asFloat

      default float asFloat()
      Returns this value as a float, narrowed the way a Java cast narrows.
      Returns:
      this value as a float, narrowed the way a Java cast narrows
    • asInt

      default int asInt()
      Returns this value as an int, saturating at the bounds like a Java cast.
      Returns:
      this value as an int, saturating at the bounds like a Java cast
    • asLong

      default long asLong()
      Returns this value as a long.

      Reads a BigInt exactly, and converts a Number by truncating toward zero. Out of range saturates rather than wrapping.

      Returns:
      this value as a long
    • asShort

      default short asShort()
      Returns this value as a short, truncating the low 16 bits like a Java cast.
      Returns:
      this value as a short, truncating the low 16 bits like a Java cast
    • asByte

      default byte asByte()
      Returns this value as a byte, truncating the low 8 bits like a Java cast.
      Returns:
      this value as a byte, truncating the low 8 bits like a Java cast
    • asChar

      default char asChar()
      Returns this value as a char.

      A string reads as its first character, which is what a caller means by asking a string for a char. Anything else reads as a UTF-16 code unit, which is how a Java char crosses into JavaScript in the first place.

      Returns:
      this value as a char
    • asBoolean

      default boolean asBoolean()
      Returns this value's truthiness.
      Returns:
      this value's truthiness
    • length

      default int length()
      Returns how many elements this holds.

      Reads size for a Map or Set and length for everything else, because JavaScript spells the same question two ways.

      Returns:
      how many elements this holds
    • asList

      default List<TSObject> asList()
      Returns the elements of this array, Set or other iterable.

      The list is a Java copy. The elements are not: each is the JavaScript value itself.

      Returns:
      the elements of this array, Set or other iterable
    • asSet

      default Set<TSObject> asSet()
      Returns the elements of this array or Set, in encounter order, without duplicates.
      Returns:
      the elements of this array or Set, in encounter order, without duplicates
    • stream

      default Stream<TSObject> stream()
      Returns the elements of this array or Set as a stream.
      Returns:
      the elements of this array or Set as a stream
    • asStringList

      default List<String> asStringList()
      Returns the elements of this array read as strings.
      Returns:
      the elements of this array read as strings
    • asIntList

      default List<Integer> asIntList()
      Returns the elements of this array read as ints.
      Returns:
      the elements of this array read as ints
    • asLongList

      default List<Long> asLongList()
      Returns the elements of this array read as longs.
      Returns:
      the elements of this array read as longs
    • asDoubleList

      default List<Double> asDoubleList()
      Returns the elements of this array read as doubles.
      Returns:
      the elements of this array read as doubles
    • keys

      default List<String> keys()
      Returns this object's own enumerable property names, or a Map's keys.

      A Map key that is not a string reads as its string form, because a Java Map<String, ?> is what the result has to fit.

      Returns:
      this object's own enumerable property names, or a Map's keys
    • asMap

      default Map<String,TSObject> asMap()
      Returns this object's own properties, or a Map's entries.

      Insertion order is preserved, which is the order JavaScript enumerates string keys in.

      Returns:
      this object's own properties, or a Map's entries
    • push

      default void push(TSObject value)
      Appends to this array.
      Parameters:
      value - the element
    • add

      default void add(TSObject value)
      Adds to this Set.
      Parameters:
      value - the element
    • put

      default void put(TSObject key, TSObject value)
      Writes into this Map, which takes any key rather than only a string.
      Parameters:
      key - the key
      value - the value
    • toJson

      default String toJson()
      Returns this value serialised as JSON.

      A BigInt is written as a string, because JSON.stringify refuses one outright and writing it as a number would lose precision above 2^53 — the range a long exists for. asLong() reads a string of digits back exactly, so the round trip is lossless.

      Returns:
      this value serialised as JSON
    • call

      default TSObject call(String method, TSObject... args)
      Calls one of this object's methods.
      Parameters:
      method - the method name
      args - the arguments
      Returns:
      what the method returned
    • of

      static TSObject of(org.teavm.jso.JSObject value)
      Wraps any JavaScript value.
      Parameters:
      value - the value
      Returns:
      the same value, typed as a TSObject
    • of

      static TSObject of(String value)
      Parameters:
      value - the value
      Returns:
      a JavaScript string
    • of

      static TSObject of(char value)
      Parameters:
      value - the value
      Returns:
      a JavaScript string of one character, which is what a caller means by a char
    • of

      static TSObject of(double value)
      Parameters:
      value - the value
      Returns:
      a JavaScript number
    • of

      static TSObject of(float value)
      Parameters:
      value - the value
      Returns:
      a JavaScript number
    • of

      static TSObject of(int value)
      Parameters:
      value - the value
      Returns:
      a JavaScript number
    • of

      static TSObject of(short value)
      Parameters:
      value - the value
      Returns:
      a JavaScript number
    • of

      static TSObject of(byte value)
      Parameters:
      value - the value
      Returns:
      a JavaScript number
    • of

      static TSObject of(long value)
      A BigInt, because a Number cannot hold every long.
      Parameters:
      value - the value
      Returns:
      a JavaScript BigInt
    • ofNumber

      static TSObject ofNumber(long value)
      A long as a Number, for a JavaScript API that will not take a BigInt.

      Refuses a value a Number cannot hold exactly rather than silently rounding it.

      Parameters:
      value - the value, within +/-2^53
      Returns:
      a JavaScript number
    • of

      static TSObject of(boolean value)
      Parameters:
      value - the value
      Returns:
      a JavaScript boolean
    • nullValue

      static TSObject nullValue()
      Returns JavaScript null.
      Returns:
      JavaScript null
    • undefined

      static TSObject undefined()
      Returns JavaScript undefined, which an absent property reads as.
      Returns:
      JavaScript undefined, which an absent property reads as
    • object

      static TSObject object()
      Returns a new empty object.
      Returns:
      a new empty object
    • object

      static TSObject object(Map<String,?> values)
      A plain object from a map, which is the shape JSON and a structured clone both use.
      Parameters:
      values - the entries, converted by from(Object)
      Returns:
      the object
    • array

      static TSObject array()
      Returns a new empty array.
      Returns:
      a new empty array
    • array

      static TSObject array(Collection<?> values)
      An array from any collection.
      Parameters:
      values - the elements, converted by from(Object)
      Returns:
      the array
    • set

      static TSObject set(Collection<?> values)
      A real JavaScript Set, for an API that wants one. array(Collection) is what a JSON-shaped value wants.
      Parameters:
      values - the elements, converted by from(Object)
      Returns:
      the set
    • map

      static TSObject map(Map<String,?> values)
      A real JavaScript Map, for an API that wants one. object(Map) is what a JSON-shaped value wants.
      Parameters:
      values - the entries, converted by from(Object)
      Returns:
      the map
    • from

      static TSObject from(Object value)
      Converts any Java value, deciding by its runtime type.

      Nested collections and maps convert all the way down, so a Map<String, List<Integer>> becomes an object of arrays of numbers. A collection becomes an array rather than a Set, and a map becomes a plain object rather than a Map, because those are the shapes JSON and the structured clone algorithm use — reach for set(Collection) or map(Map) to say otherwise.

      Parameters:
      value - the value
      Returns:
      the JavaScript value
      Throws:
      IllegalArgumentException - for a type with no JavaScript counterpart, rather than producing something that looks converted and is not
    • fromJson

      static TSObject fromJson(String json)
      Parses JSON.
      Parameters:
      json - the JSON text
      Returns:
      the parsed value