Libraries anvil lang

namespace lang
Set of elementary functions and classes. Entities at anvil.lang are accessible directly without a need to import or use anvil.lang prefix.

Namespaces

namespace op
Set of functions which behave same as the corresponding operators.


Classes

class array
extends anvil.lang.object
Array is collections of keys mapped to values. Array has capabilities of list as it also retains doubly linked list of its elements.

class binary
extends anvil.lang.sequence
Binary is an efficient implemtation of variable length array of bytes.

class boolean
extends anvil.lang.object
Boolean is a base class for true and false.

class buffer
extends anvil.lang.sequence
Buffer is a mutable variable length sequence of characters.

class false
extends anvil.lang.object
False is constant indicating boolean false value.

class float
extends anvil.lang.number
Float is a double precision 64 bit floating point number.

class inf
extends anvil.lang.object
Inf is a constant for (semi-)infinite value.

class int
extends anvil.lang.number
Int is a 64 bit signed integer.

class iterator
extends anvil.lang.object
Iterator generates ordered series of (keys and) values. These keys and values can be retrived one at a time.

class list
extends anvil.lang.sequence
List is a mutable sequence of values.

class map
extends anvil.lang.object
Map represents mapping of key to value.

class null
extends anvil.lang.object
Null is constant indicating empty value.

class number
extends anvil.lang.object
Abstract base class for integers and floats.

class object
Object is a root of the class hiearchy: every class has a object as a superclass.

class pattern
extends anvil.lang.object
Pattern represents compiled regular expression.

class range
extends anvil.lang.object
Range represents range of values from start to end, where start is inclusive and end is exclusive.

class sequence
extends anvil.lang.object
Sequence represents list of elements, that may be accessed with index. Some of sequences (such as string and tuple) are immutable and mutating operations always return copy of original sequence modified.

class string
extends anvil.lang.sequence
The string class represents character strings. String is a sequence of characters.

class true
extends anvil.lang.object
True is a constant indicating boolean true value.

class tuple
extends anvil.lang.sequence
Tuple is an immutable sequence of values.

class undefined
extends anvil.lang.object
undefined is constant indicating non existent value.


Exceptions

class AccessDenied
extends anvil.lang.throwable

class AccessError
extends anvil.lang.TypeError

class AcquireError
extends anvil.lang.throwable

class AssertFailed
extends anvil.lang.throwable

class AttributeError
extends anvil.lang.TypeError

class BadParameter
extends anvil.lang.TypeError

class BadState
extends anvil.lang.throwable

class CallError
extends anvil.lang.TypeError

class ClassNotFound
extends anvil.lang.throwable

class CorruptedSerialization
extends anvil.lang.throwable

class ImportError
extends anvil.lang.throwable

class IndexError
extends anvil.lang.TypeError

class InstantiationError
extends anvil.lang.TypeError

class InternalError
extends anvil.lang.throwable

class Interrupted
extends anvil.lang.throwable

class JavaError
extends anvil.lang.throwable

class MalformedPattern
extends anvil.lang.throwable

class NoInstance
extends anvil.lang.TypeError

class NoSuchClass
extends anvil.lang.TypeError

class NoSuchEntity
extends anvil.lang.TypeError

class NoSuchFunction
extends anvil.lang.TypeError

class NoSuchMember
extends anvil.lang.TypeError

class NoSuchMethod
extends anvil.lang.TypeError

class NotEnoughParameters
extends anvil.lang.TypeError

class OperationFailed
extends anvil.lang.throwable

class throwable
extends anvil.lang.object
Throwable is a carrier for exceptions, capable of holding of message and stack trace.

class TypeError
extends anvil.lang.throwable


Functions

int addressOf(object data)
Returns memory address of parameters. Returned value for same value is not guaranteed to be same between calls.

boolean areSame(a, b)
Checks if two objects share the same memory address.

binary serialize(object data)
Serializes data.
Returns Data serialized to binary.

boolean canDo(String name)
boolean canDo(String name, String actions)
Checks if given tool permission exists, against current security policy

string chr(int code)
Converts given character code into character (to string which has the length of one).

object clone(object value)
Creates shallow copy from given value.
Parameters value -  Value to be cloned.
Returns Cloned value

tuple coerce(object a, object b)
object coerce(object a)
Returns list containing 'a' and 'b' converted to similar types according to same rules applied to arithmetic operations. If second parameter is omitted tries to convert string to int or float, if conversion fails returns self.

object context()
Returns context of current custom tag.
Returns Current context.

object copy(object value)
Creates deep copy from 'value'.
Parameters value -  Value to be copied.
Returns Copied value

createArray(range dimensions...)
Creates multidimensional rectangular array with given dimensions.
Parameters dimension -  Integer for upper bound or list as (lobound, hibound).
Returns Array created

createMatrix(range dimensions...)
Creates multidimensional rectangular array with default values.
Parameters defaultValue -  Default value for each leaf element.
dimension -  Integer for upper bound or list as (lobound, hibound).
Returns Array created

string decode(string str)
string decode(string str, string encoding)
Decodes URL encoded string.
Parameters str -  URL encoded string
encoding -  Character encoding to use
Returns Decoded string
Throws BadParameter -  if charset encoding is not supported

object deref(object ref)
Returns the value referred by this given ref.
Parameters ref -  Ref to variable
derefAll -  If true, all references are followed until first non-ref value is encountered.

int each(object applier, object elements)
Iterates through given elements and executes given function for each element.
Parameters callback -  Callable receiving parameters (element, key)
Returns elements

object eject()
Rtturns the value that was 'injected' to generator using anvil.lang.iterator.inject(). This function may only be called inside generator function.

string encode(string str)
string encode(string str, string encoding)
Encodes string.
Parameters str -  String
encoding -  Character encoding to use
Returns Encoded string
Throws BadParameter -  if charset encoding is not supported

string escape(string str)
Converts string to backslash escaped format.
Parameters str -  String to escape
Returns Escaped string

object parse(String code)
Parses, evaluates and returns a result of constant anvil expression.
Throws ImportError -  If parsing of constant failed
BadParameter -  If expression does not containt constant expression

void expect(map valueToType, ...)
Validates any number of arguments by checking if their are of desired type. Arguments are expected to be mappings containing the value candidates on the left side and types on the right side.
Throws BadArgument -  if any of candidate values didn't match the requested type

iterator find(string pattern, string text)
iterator find(pattern pattern, string text)
Finds all regions from given text mathing given regular expression pattern.
Parameters pattern -  Regular expression pattern
text -  Text to inspect
Returns Iterator of strings
Throws MalformedPattern -  If pattern is invalid

format(string format, ...)
Format 'sprintf' style format string with given parameters. Format may contain fields:

% [-]? [+]? [0]? [width] ( [.] [precision] ) [csdifg]
- Adjust left, instead of right.
+Numbers with sign
0Pad numbers with zeros, instead of spaces
widthWidth of field, if '*' width will be taken from arguments
precisionWidth of fraction part (doubles), if '*' width will be taken from arguments
c Character (integer or first character from string).
sString
d,iInteger
f Float, follows width.precision stricly
g Float with signicant fraction shown, if precision is zero (or not given) shows significant digits from it.
Use %% to escape %.
Parameters format -  Format string
Returns Formatted string

list grep(pattern pattern, list)
list grep(string pattern, list)
Returns a new list containing all elements in given sequence that matched given regular expression.

Object implement(handler, interfaces...)

Creates a java.lang.reflect.Proxy that implements all the java interfaces given as parameters.

If handler is a function it receives the name of method as first argument followed by the parameters of original method call.

Otherwise the handler is treated as a class instance and corresponding method is invoked with parameters of original method call.


string join(object elements)
string join(object elements, string clue)
Joins the elements together with given clue.
Parameters clue -  Clue to join with, default is ", ".

string ls(string str)
Converts parameter to lowercase.

Type loadClass(String classname)
Loads the given java class or interface.

void log(messages...)
Logs messages.

void logError(messages...)
Logs error messages.

object match(string pattern, string text)
object match(string pattern, string text array matches)
object match(string pattern, string text, list matches)
object match(pattern pattern, string text)
object match(pattern pattern, string text, array matches)
object match(pattern pattern, string text, list matches)
Checks if any region on given text matches given regular expression.
Parameters pattern -  Regular expression pattern
text -  Text to inspect
Returns Matches or boolean
Throws MalformedPattern -  If pattern is invalid

int ord(string str)
Converts first character of given string to its ordinal value.

parseFloat(string str)
parseFloat(string str, float defaultValue)
Attempts to parse given string to float.
Parameters str -  String to convert
defaultValue -  Value returned if parsing failed, by default null.
Returns Parsed float.

parseInt(string str)
parseInt(string str, int radix)
parseInt(string str, int radix, int defaultValue)
Attempts to parse given string to integer.
Parameters str -  String to convert
radix -  Radix of string, default is 10.
defaultValue -  Value returned if parsing failed, by default null.
Returns Parsed integer.

parseNumber(string str)
parseNumber(string str, int defaultValue)
Attempts to parse given string to integer. Valid formats are 123 (decimal), 0x123 (hex) , 0b101 (binary) , 0123 (octal)
Parameters str -  String to convert
defaultValue -  Value returned if parsing failed, by default null.
Returns Integer parsed.

iterator pipe(object converter, object elements)
Returns iterator that iterates through given elements and pipes the return value of given function as element on call to iterator.next.
Parameters converter -  Callable converter receiving parameters element, index)

void print(...)
Same as builtin statement print. Prints all parameters.

void printbr(...)
Same as builtin statement printbr. Prints all parameters, <br;gt; and a newline.

void println(...)
Same as builtin statement println. Prints all parameters and newline.

string quote(string str)
Converts all applicable characters in string to html-entities.

string quotemeta(string str)
Converts all meta characters in string to html-entities. Meta characters are ampersand, apostrophe, greater-than, less-than non-breaking space, quotation mark and soft hyphen.

object reduce(object reducer, object elements)
object reduce(object reducer, object elements, object initialvalue)
Iterates through the given elements and calls reducer function so that the first parameter will be either the result of previous reducer call or intialValue (if it was given), and all other parameters, up to function's required parameter count are taken from the elements.
Parameters reducer -  Callable reducer taking from count parameters
intialValue -  initial value of reduction
Returns the return value of last call to reducer or initialValue

object ref(object provider)
Generates and returns programmable ref from given class instance.
Parameters impl -  Class having _getRef (and _setRef) methods

string replace(string pattern, string text, object replacement [, int amount] )
string replace(pattern pattern, string text, object replacement [, int amount] )
Substitutes all regions from given text matching given regular expression with given replacement
Parameters pattern -  Regular expression pattern
replacement -  Replacement, either a string or function: function replacer(list matchedGroups).
amount -  Number of occurences to replace, if omitted, all occurences are substituted.
Throws MalformedPattern -  If pattern is invalid

object reveal(object ref)
Reveals and returns the ref implementation, encapsulated with ref().
Parameters ref -  Programmable ref

iterator join(object elements)
Reverses given list of elements and returns new iterator from it.

iterator select(object selector, object elements)
Returns iterator of all elements for which the given function returned true.
Parameters pipe -  Callable selector receiving parameters (element, index)

string serialize(object data)
Serializes data.
Returns Data serialized to string representation.

iterator sort(object sorter, object elements)
Sorts the elements of sequence with given comparator.
Parameters sorter -  Callable comparator receiving parameters (value1, value2)

list split(string str)
list split(pattern pat, string str)
list split(pattern pat, string str, int limit)
Splits a string into a list of strings and returns that list. If patter is omitted splits according to whitespaces.

string th(int number)
Returns english numeral suffix (st, nd, th) for given value.
Parameters value - 
Returns Numeral suffix.

string toAnvil(object value)
Converts value to valid code producing the same value.

array toArray(object value)
Converts value to array. Useful with sequences, arrays, iterators and any other values returning iterator.

string toBin(int number)
string toBin(int number, int length)
string toBin(int number, int length, string pad)
Converts number to binary format.
Parameters number -  Number to convert
length -  Length of resulting string
pad -  Padding character, default is '0'
Returns Number converted to binary

binary toBinary(object value)
Converts sequence to binary. Useful with sequences, arrays, iterators and any other values returning iterator.

string toBuffer(object value)
Converts sequence to buffer. Useful with sequences, arrays, iterators and any other values returning iterator.

string toHex(int number)
string toHex(int number, int length)
string toHex(int number, int length, string pad)
Converts number to hexadecimal format.
Parameters number -  Number to convert
length -  Length of resulting string
pad -  Padding character, default is '0'
Returns Number converted to hexadecimal

toHref(param, ...)
Creates query string.
Parameters param -  Map key=>value for mapped values, array or sequence of values or anything else for mappings with empty value.
Returns Query string

list toList(object value)
Converts value to binary. Useful with sequences, arrays, iterators and any other values returning iterator.

string toLower(string str)
Converts given parameter to lower case.

string toOctal(int number)
string toOctal(int number, int length)
string toOctal(int number, int length, string pad)
Converts number to octal format.
Parameters number -  Number to convert
length -  Length of resulting string
pad -  Padding character, default is '0'
Returns Number converted to octal

string toRadix(int number, int radix)
string toRadix(int number, int radix, int length)
string toRadix(int number, int radix, int length, string pad)
Converts number to given radix format.
Parameters number -  Number to convert
radix -  Radix
length -  Length of resulting string
pad -  Padding character, default is '0'
Returns Number converted to given radix

string toString(object value)
Converts sequence to string. Useful with sequences, arrays, iterators and any other values returning iterator.

tuple toTuple(object value)
Converts sequence to binary. Useful with sequences, arrays, iterators and any other values returning iterator.

string toUpper(string str)
Converts given parameter to upper case.

string trim(string str)
Trims whitespaces from beginning and end of string.

string uc(string str)
Converts parameter to uppercase.

string unquote(string str)
Converts all applicable "entities; to their character counterparts.
Parameters str -  String to unquote
Returns Unquoted string

object unserialize(string repr)
object unserialize(binary repr)
Unserializes data representation.

string wmlquote(string str)
Converts all applicable characters in string to wml supported entities.