Package convex.core.lang
Class RT
java.lang.Object
convex.core.lang.RT
Static utility class for Runtime functions. Mostly low-level support for Core
language capabilities, which will be wrapped as functions in the initial
execution environment.
"Low-level programming is good for the programmer's soul." — John Carmack
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic APrimitiveGets the absolute value of a numeric value.allEqual(T[] values) Returns true if all elements in an array are equal.static LongGets the count of objects in a collection for destructuring (may be data structure or Java Array)static <R extends ACell>
ADataStructure<R> assoc(ADataStructure<R> coll, ACell key, ACell value) Associates a key position with a given value in an associative data structurestatic booleanConverts any CVM value to a boolean value.static AddressGets a callable Address from a cell value.static AccountKeyCoerce to an AccountKey.static AddresscastAddress(ACell a) Casts the argument to a valid Address.static ABlobConverts an object to a canonical blob representation.static CVMLongExplicitly converts a numerical value to a CVM Byte.static CVMDoublecastDouble(ACell a) Converts a numerical value to a CVM Double.Coerces an argument to a function interface.static AIntegercastInteger(ACell a) Converts a numerical value to a CVM Integer.static KeywordcastKeyword(ACell a) Coerces an named value to a Keyword.static CVMLongConverts a numerical value to a CVM Long.Converts any collection to a setcastVector(ACell o) Converts any countable data structure to a vector.static CVMDoubleGets the ceiling of a number after casting to a Double.static Class<?> commonNumericType(ACell[] args) Get the target common numeric type for a given set of arguments.static LongCompares two objects representing numbers numerically.static ASequence<?> Concatenates two sequences.Prepends an element to a sequential data structure to create a new list.Prepends two elements to a sequential data structure.Prepends three elements to a sequential data structure.static LongGets the count of elements in a countable data structure.static LongGets the count of elements in a collection or Java array.static <T extends ACell>
TConverts a Java value to a CVM type.static CVMDoublestatic doubledoubleFromUnsignedLong(long a) Converts a long value, treated as unsigned, to a double.static AccountKeyImplicit cast to an AccountKey.static AddressEnsures the argument is a valid Address.static ADataStructure<?> Ensures the argument is an associative data structure instance.static ABlobensureBlob(ACell a) Implicitly casts the argument to a Blobstatic CVMCharensureChar(ACell a) Implicitly casts argument to a CVM Characterstatic <E extends ACell>
ACountable<E> Casts to an ACountable instancestatic <E extends ACell>
ADataStructure<E> Casts to an ADataStructure instancestatic CVMDoubleEnsures the argument is a CVM Double value.Ensure the argument is a valid CVM function.static HashensureHash(ACell o) Coerces to Hash type.Casts the argument to a hashmap.static AIntegerEnsures the argument is a CVM Integer value.static CVMLongensureLong(ACell a) Ensures the argument is a CVM Integer within Long range.Converts the argument to a non-null Map.Converts an object to a map entry.static ANumericConverts a CVM value to the standard numeric representation.Ensures argument is a sequence data structure.Ensures the value is a Set.static AStringEnsures the argument is a CVM Stringstatic SymbolEnsures the argument is a Symbol.static ATransactionensureTransaction(ACell maybeTx) Casts to a transaction record, or null if not castableEnsures argument is a Vector data structure.static CVMBoolstatic CVMDoubleComputes the result of a exp operation.static intfindNonNumeric(ACell[] args) Finds the first non-numeric value in an array.static CVMDoubleGets the floor a number after casting to a double.static CVMBoolstatic ACellget(ADataStructure<?> coll, ACell key) Gets an element from a data structure using the given key.static ACellget(ADataStructure<?> coll, ACell key, ACell notFound) Gets an element from a data structure using the given key.static ATypeGet the runtime Type of any CVM valuestatic CVMBoolstatic booleanTests if a value is one of the canonical boolean values 'true' or 'false'static booleanisCountable(ACell val) static booleanstatic booleanTests if a Value is a valid numerical value type.static booleanisValidAmount(long amount) static <T> TConverts a CVM value to equivalent JSON value as expressed in equivalent JVM types.static StringGets a String from a value suitable for use as a JSON map keyConverts a CVM Map to a JSON representationstatic <T> TConverts a CVM value to equivalent JVM valueReturns a Vector of keys of a Map, or null if the object is not a Map WARNING: Potentially O(n) in size of Mapstatic CVMBoolstatic CVMBoolstatic ACellGets the minimum of a set of numeric valuesstatic ACellGets the minimum of two numeric valuesstatic ACellGets the minimum of a set of numeric valuesstatic ACellGets the minimum of two numeric valuesstatic ANumericstatic ANumericstatic AStringGets the name from a CVM value.static <T extends ACell>
TGets the nth element from a sequential collection.static <T extends ACell>
TVariant of nth that also handles Java Arrays.static Class<?> numericType(ACell a) Gets the numeric class of an objectstatic ANumericstatic CVMDoubleComputes the result of a pow operation.static AStringPrints a value to a String as long as the result fits within a given print limit.static AStringPrints a cell to a BlobBuilder, up to a specified limit of bytes WARNING: May return nullstatic booleanprint(BlobBuilder bb, ACell a, long limit) Prints a cell to a BlobBuilder, up to a specified limit of bytesConverts any collection of cells into a Sequence data structure.static ACellGets the signum of a numeric valuestatic longsplitmix64Calc(long seed) Compute a SplitMix64 value for a given seed.static longsplitmix64Update(long seed) Compute a SplitMix64 update.static CVMDoubleGets the exact positive square root of a number after casting to a Double.static AStringConverts a value to a CVM String representation.static AStringConverts arguments to an AString representation.static AddressCasts an arbitrary value to an Addressstatic StringConverts a value to a Java String representationstatic StringConverts a value to a Java String representationstatic voidValidates an object.static voidValidate a Cell.Returns the vector of values of a map, or null if the object is not a mapvec(ACollection<T> coll) Converts any collection to a vector.Converts any data structure to a vector
-
Constructor Details
-
RT
public RT()
-
-
Method Details
-
allEqual
-
eq
-
ge
-
gt
-
le
-
lt
-
min
-
min
-
max
-
max
-
commonNumericType
-
findNonNumeric
Finds the first non-numeric value in an array. Used for error reporting.- Parameters:
args- Argument array- Returns:
- First non-numeric value, or -1 if not found.
-
numericType
-
plus
-
minus
-
multiply
-
divide
-
pow
-
exp
-
floor
-
ceil
-
sqrt
-
splitmix64Update
public static long splitmix64Update(long seed) Compute a SplitMix64 update. Component for splittable PRNG. See: https://xorshift.di.unimi.it/splitmix64.c- Parameters:
seed- Initial SplitMix64 seed- Returns:
- Updated SplitMix64 seed
-
splitmix64Calc
public static long splitmix64Calc(long seed) Compute a SplitMix64 value for a given seed. Component for splittabe PRNG See: https://xorshift.di.unimi.it/splitmix64.c- Parameters:
seed- Initial SplitMix64 seed- Returns:
- Updated SplitMix64 seed
-
doubleFromUnsignedLong
public static double doubleFromUnsignedLong(long a) Converts a long value, treated as unsigned, to a double. Useful for randomness- Parameters:
a- Long value, treated as unsigned- Returns:
- Double value of long
-
abs
Gets the absolute value of a numeric value. Supports double and long.- Parameters:
a- Numeric CVM value- Returns:
- Absolute value, or null if not a numeric value
-
signum
-
compare
Compares two objects representing numbers numerically.- Parameters:
a- First numeric valueb- Second numeric valuenanValue- Value to return in case of a NaN result- Returns:
- -1 if a is smaller, 1 if a is larger, 0 if a equals b, null if either value non-numeric, NaN if either value is NaN
-
ensureNumber
Converts a CVM value to the standard numeric representation. Result will be one of:- Long for Long
- Double for Double
- null for any non-numeric value
- Parameters:
a- Value to convert to numeric representation- Returns:
- The number value, or null if cannot be converted
-
isNumber
Tests if a Value is a valid numerical value type. Note: Returns false for null, but true for NaN- Parameters:
val- Value to test- Returns:
- True if a number, false otherwise
-
castDouble
-
ensureDouble
-
castLong
-
ensureLong
-
castInteger
-
ensureInteger
-
castByte
-
vec
-
castVector
-
castSet
-
vec
Converts any collection to a vector. Always succeeds, but may have O(n) cost Null values are converted to empty vector (considered as empty sequence)- Parameters:
coll- Collection to convert to a Vector- Returns:
- Vector instance
-
sequence
Converts any collection of cells into a Sequence data structure. Potentially O(n) in size of collection. Nulls are converted to an empty vector. Returns null if conversion is not possible.- Type Parameters:
T- Type of cell in collection- Parameters:
o- An object that contains a collection of cells- Returns:
- An ASequence instance, or null if the argument cannot be converted to a sequence
-
ensureSequence
Ensures argument is a sequence data structure. Nulls are converted to an empty vector. Returns null if conversion is not possible.- Type Parameters:
T- Type of sequence elements- Parameters:
o- Value to cast to sequence- Returns:
- An ASequence instance, or null if the argument cannot be converted to a sequence
-
ensureVector
Ensures argument is a Vector data structure. Returns null if not a vector is not possible.- Type Parameters:
T- Type of Vector elements- Parameters:
o- Value to cast to Vector- Returns:
- An AVector instance, or null if the argument is not a Vector
-
nth
Gets the nth element from a sequential collection. Throws an exception if access is out of bounds - caller responsibility to check bounds first- Type Parameters:
T- Type of element in collection- Parameters:
o- Countable Valuei- Index of element to get- Returns:
- Element from collection at the specified position
-
nth
-
count
-
argumentCount
-
count
-
str
Converts arguments to an AString representation. Handles:- CVM Strings (unchanged)
- Blobs (converted to hex)
- Numbers (converted to canonical numeric representation)
- Other Objects (printed in canonical format)
- Parameters:
args- Values to convert to String- Returns:
- AString value, or null if allowable String length exceeded
-
print
Prints a cell to a BlobBuilder, up to a specified limit of bytes- Parameters:
bb- BlobBuilder instancea- Cell to print (may be nil)limit- Limit of printing- Returns:
- True if within limit, false if exceeded (output may still be partially written to BlobBuilder)
-
print
-
print
-
str
-
toString
-
toString
-
name
-
cons
Prepends an element to a sequential data structure to create a new list. May be O(n). The new element will always be in position 0- Type Parameters:
T- Type of elements- Parameters:
x- Element to prependxs- Any sequential object, or null (will be treated as empty sequence)- Returns:
- A new list with the cons'ed element at the start
-
cons
Prepends two elements to a sequential data structure. The new elements will always be in position 0 and 1- Type Parameters:
T- Type of elements- Parameters:
x- Element to prepend at position 0y- Element to prepend at position 1xs- Any sequential object, or null (will be treated as empty sequence)- Returns:
- A new list with the cons'ed elements at the start
-
cons
Prepends three elements to a sequential data structure. The new elements will always be in position 0, 1 and 2- Type Parameters:
T- Type of elements- Parameters:
x- Element to prepend at position 0y- Element to prepend at position 1z- Element to prepend at position 2xs- Any sequential object- Returns:
- A new list with the cons'ed elements at the start
-
castFunction
Coerces an argument to a function interface. Certain values e.g. Keywords can be used / applied in function position.- Type Parameters:
T- Function return type- Parameters:
a- Value to cast to a function- Returns:
- AFn instance, or null if the argument cannot be coerced to a function.
-
ensureFunction
-
castAddress
Casts the argument to a valid Address. Handles:- Strings, which are interpreted as 16-character hex strings
- Addresses, which are returned unchanged
- Blobs, which are converted to addresses if and only if they are of the correct length (8 bytes)
- Numeric Longs, which are converted to the equivalent Address
- Parameters:
a- Value to cast to an Address- Returns:
- Address value or null if not castable to a valid address
-
toAddress
-
ensureAddress
-
ensureAccountKey
Implicit cast to an AccountKey. Accepts blobs of correct length- Parameters:
a- Value to cast- Returns:
- AccountKey instance, or null if coercion fails
-
castAccountKey
Coerce to an AccountKey. Accepts strings and blobs of correct length- Parameters:
a- Value to cast- Returns:
- AccountKey instance, or null if coercion fails
-
castBlob
-
ensureMap
Converts the argument to a non-null Map. Nulls are implicitly converted to the empty map.- Type Parameters:
K- Type of map keysV- Type of map values- Parameters:
a- Value to cast- Returns:
- Map instance, or null if argument cannot be converted to a map
-
get
Gets an element from a data structure using the given key.- Parameters:
coll- Collection to querykey- Key to look up in collection- Returns:
- Value from collection with the specified key, or null if not found.
-
get
Gets an element from a data structure using the given key. Returns the notFound parameter if the data structure does not have the specified key- Parameters:
coll- Collection to querykey- Key to look up in collectionnotFound- Value to return if the lookup failed- Returns:
- Value from collection with the specified key, or notFound argument if not found.
-
bool
Converts any CVM value to a boolean value. An value is considered falsey if null or equal to CVMBool.FALSE, truthy otherwise- Parameters:
a- Object to convert to boolean value- Returns:
- true if object is truthy, false otherwise
-
ensureMapEntry
Converts an object to a map entry. Handles MapEntries and length 2 Vectors.- Type Parameters:
K- Type of map keyV- Type of map value- Parameters:
x- Value to cast- Returns:
- MapEntry instance, or null if conversion fails
-
ensureHash
-
castKeyword
-
ensureSymbol
-
ensureDataStructure
Casts to an ADataStructure instance- Type Parameters:
E- Type of data structure element- Parameters:
a- Value to cast- Returns:
- ADataStructure instance, or null if not a data structure
-
ensureCountable
Casts to an ACountable instance- Type Parameters:
E- Type of countable element- Parameters:
a- Value to cast- Returns:
- ADataStructure instance, or null if not a data structure
-
isCountable
-
isBoolean
Tests if a value is one of the canonical boolean values 'true' or 'false'- Parameters:
value- Value to test- Returns:
- True if the value is a canonical boolean value.
-
concat
Concatenates two sequences. Ignores nulls.- Parameters:
a- First sequence. Will be used to determine the type of the result if not null.b- Second sequence. Will be the result if the first parameter is null.- Returns:
- Concatenated Sequence
-
validate
Validates an object. Might be a Cell or Ref- Parameters:
o- Object to validate- Throws:
InvalidDataException- For any validation failure
-
validateCell
Validate a Cell.- Parameters:
o- Object to validate- Throws:
InvalidDataException- For any validation failure
-
assoc
public static <R extends ACell> ADataStructure<R> assoc(ADataStructure<R> coll, ACell key, ACell value) Associates a key position with a given value in an associative data structure- Parameters:
coll- Any associative data structurekey- Key to update or addvalue- Value to associate with key- Returns:
- Updated data structure, or null if implicit cast of key or value to required type fails
-
keys
-
values
-
ensureAssociative
Ensures the argument is an associative data structure instance. A null argument is considered an empty map.- Parameters:
o- Value to cast- Returns:
- IAssociative instance, or null if conversion is not possible
-
ensureSet
-
ensureHashMap
-
ensureBlob
-
ensureBlobLike
-
ensureString
-
isValidAmount
public static boolean isValidAmount(long amount) -
cvm
-
jvm
Converts a CVM value to equivalent JVM value- Parameters:
o- Value to convert to JVM type- Returns:
- Java value, or unchanged input
-
json
Converts a CVM value to equivalent JSON value as expressed in equivalent JVM types. Note some special one-way conversions that are required because JSON is not sufficiently expressive for all CVM types: - Address becomes a Number (Long type) - Lists and Vectors both become an Array (Java List type) - Characters become a String - Blobs become a hex string representation '0x....'- Parameters:
o- Value to convert to JSON value object- Returns:
- Java Object which represents JSON value
-
jsonMap
-
jsonKey
-
getType
-
isNaN
-
ensureChar
-
callableAddress
-
ensureTransaction
Casts to a transaction record, or null if not castable- Parameters:
maybeTx- Cell which should be a transaction- Returns:
- Transaction value, or null if not a transaction
-