Interface TSObject
- All Superinterfaces:
org.teavm.jso.JSObject
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 TypeMethodDescriptiondefault voidAdds to thisSet.static TSObjectarray()Returns a new empty array.static TSObjectarray(Collection<?> values) An array from any collection.default booleanReturns this value's truthiness.default byteasByte()Returns this value as a byte, truncating the low 8 bits like a Java cast.default charasChar()Returns this value as a char.default doubleasDouble()Returns this value as a double.Returns the elements of this array read as doubles.default floatasFloat()Returns this value as a float, narrowed the way a Java cast narrows.default intasInt()Returns this value as an int, saturating at the bounds like a Java cast.Returns the elements of this array read as ints.asList()Returns the elements of this array,Setor other iterable.default longasLong()Returns this value as a long.Returns the elements of this array read as longs.asMap()Returns this object's own properties, or aMap's entries.asSet()Returns the elements of this array orSet, in encounter order, without duplicates.default shortasShort()Returns this value as a short, truncating the low 16 bits like a Java cast.default StringasString()Returns this value coerced to a string, the wayString(value)would.Returns the elements of this array read as strings.at(int index) Reads one element of an array.default TSObjectCalls one of this object's methods.static TSObjectConverts any Java value, deciding by its runtime type.static TSObjectParses JSON.Reads one property.default booleanisArray()Returns whether this value is a JavaScript array.default booleanisBigInt()Returns whether this value is aBigInt, which is how a Java long crosses over.default booleanReturns whether this value is a boolean.default booleanReturns whether this value is callable.default booleanisMap()Returns whether this value is a JavaScriptMap, not a plain object.default booleanisNull()Returns whether this value isnullorundefined.default booleanisNumber()Returns whether this value is aNumber, which is every Java numeric but long.default booleanisSet()Returns whether this value is a JavaScriptSet.default booleanisString()Returns whether this value is a string.default booleanReturns whether this value isundefinedspecifically.keys()Returns this object's own enumerable property names, or aMap's keys.default intlength()Returns how many elements this holds.static TSObjectA real JavaScriptMap, for an API that wants one.static TSObjectReturns JavaScriptnull.static TSObjectobject()Returns a new empty object.static TSObjectA plain object from a map, which is the shape JSON and a structured clone both use.static TSObjectof(boolean value) static TSObjectof(byte value) static TSObjectof(char value) static TSObjectof(double value) static TSObjectof(float value) static TSObjectof(int value) static TSObjectof(long value) ABigInt, because aNumbercannot hold every long.static TSObjectof(short value) static TSObjectstatic TSObjectof(org.teavm.jso.JSObject value) Wraps any JavaScript value.static TSObjectofNumber(long value) A long as aNumber, for a JavaScript API that will not take aBigInt.default voidAppends to this array.default voidWrites into thisMap, which takes any key rather than only a string.voidWrites one property.static TSObjectset(Collection<?> values) A real JavaScriptSet, for an API that wants one.stream()Returns the elements of this array orSetas a stream.default StringtoJson()Returns this value serialised as JSON.default StringtypeOf()Returns the result of JavaScript'stypeofon this value.static TSObjectReturns JavaScriptundefined, which an absent property reads as.Methods inherited from interface org.teavm.jso.JSObject
cast
-
Method Details
-
get
Reads one property.- Parameters:
name- the property name- Returns:
- the value, which is never
nullfor a property that is present and holdsnull; useisNull()to tell those apart
-
set
Writes one property.- Parameters:
name- the property namevalue- the value
-
at
Reads one element of an array.- Parameters:
index- the index- Returns:
- the element
-
typeOf
Returns the result of JavaScript'stypeofon this value.- Returns:
- the result of JavaScript's
typeofon this value
-
isNull
default boolean isNull()Returns whether this value isnullorundefined.- Returns:
- whether this value is
nullorundefined
-
isUndefined
default boolean isUndefined()Returns whether this value isundefinedspecifically.- Returns:
- whether this value is
undefinedspecifically
-
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 aNumber, 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 aBigInt, 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 JavaScriptMap, 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 JavaScriptSet.- Returns:
- whether this value is a JavaScript
Set
-
asString
Returns this value coerced to a string, the wayString(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
BigIntexactly, and converts aNumberby 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
charcrosses 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
sizefor aMaporSetandlengthfor everything else, because JavaScript spells the same question two ways.- Returns:
- how many elements this holds
-
asList
Returns the elements of this array,Setor other iterable.The list is a Java copy. The elements are not: each is the JavaScript value itself.
- Returns:
- the elements of this array,
Setor other iterable
-
asSet
Returns the elements of this array orSet, in encounter order, without duplicates.- Returns:
- the elements of this array or
Set, in encounter order, without duplicates
-
stream
Returns the elements of this array orSetas a stream.- Returns:
- the elements of this array or
Setas a stream
-
asStringList
Returns the elements of this array read as strings.- Returns:
- the elements of this array read as strings
-
asIntList
Returns the elements of this array read as ints.- Returns:
- the elements of this array read as ints
-
asLongList
Returns the elements of this array read as longs.- Returns:
- the elements of this array read as longs
-
asDoubleList
Returns the elements of this array read as doubles.- Returns:
- the elements of this array read as doubles
-
keys
Returns this object's own enumerable property names, or aMap's keys.A
Mapkey that is not a string reads as its string form, because a JavaMap<String, ?>is what the result has to fit.- Returns:
- this object's own enumerable property names, or a
Map's keys
-
asMap
Returns this object's own properties, or aMap'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
Appends to this array.- Parameters:
value- the element
-
add
Adds to thisSet.- Parameters:
value- the element
-
put
Writes into thisMap, which takes any key rather than only a string.- Parameters:
key- the keyvalue- the value
-
toJson
Returns this value serialised as JSON.A
BigIntis written as a string, becauseJSON.stringifyrefuses one outright and writing it as a number would lose precision above 2^53 — the range alongexists for.asLong()reads a string of digits back exactly, so the round trip is lossless.- Returns:
- this value serialised as JSON
-
call
Calls one of this object's methods.- Parameters:
method- the method nameargs- the arguments- Returns:
- what the method returned
-
of
Wraps any JavaScript value.- Parameters:
value- the value- Returns:
- the same value, typed as a
TSObject
-
of
- Parameters:
value- the value- Returns:
- a JavaScript string
-
of
- Parameters:
value- the value- Returns:
- a JavaScript string of one character, which is what a caller means by a char
-
of
- Parameters:
value- the value- Returns:
- a JavaScript number
-
of
- Parameters:
value- the value- Returns:
- a JavaScript number
-
of
- Parameters:
value- the value- Returns:
- a JavaScript number
-
of
- Parameters:
value- the value- Returns:
- a JavaScript number
-
of
- Parameters:
value- the value- Returns:
- a JavaScript number
-
of
ABigInt, because aNumbercannot hold every long.- Parameters:
value- the value- Returns:
- a JavaScript BigInt
-
ofNumber
A long as aNumber, for a JavaScript API that will not take aBigInt.Refuses a value a
Numbercannot hold exactly rather than silently rounding it.- Parameters:
value- the value, within +/-2^53- Returns:
- a JavaScript number
-
of
- Parameters:
value- the value- Returns:
- a JavaScript boolean
-
nullValue
Returns JavaScriptnull.- Returns:
- JavaScript
null
-
undefined
Returns JavaScriptundefined, which an absent property reads as.- Returns:
- JavaScript
undefined, which an absent property reads as
-
object
Returns a new empty object.- Returns:
- a new empty object
-
object
A plain object from a map, which is the shape JSON and a structured clone both use.- Parameters:
values- the entries, converted byfrom(Object)- Returns:
- the object
-
array
Returns a new empty array.- Returns:
- a new empty array
-
array
An array from any collection.- Parameters:
values- the elements, converted byfrom(Object)- Returns:
- the array
-
set
A real JavaScriptSet, for an API that wants one.array(Collection)is what a JSON-shaped value wants.- Parameters:
values- the elements, converted byfrom(Object)- Returns:
- the set
-
map
A real JavaScriptMap, for an API that wants one.object(Map)is what a JSON-shaped value wants.- Parameters:
values- the entries, converted byfrom(Object)- Returns:
- the map
-
from
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 aSet, and a map becomes a plain object rather than aMap, because those are the shapes JSON and the structured clone algorithm use — reach forset(Collection)ormap(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
Parses JSON.- Parameters:
json- the JSON text- Returns:
- the parsed value
-