Hook's language comes with a set of core modules that serve as the building blocks for scripts and other modules. These modules form the foundation of Hook's language and provide essential functionalities that enable you to write powerful and efficient code.
Before jumping into the documentation, it's important to remember how to import modules in Hook.
To use a core module in your script, you must first import it. You can import a module by using the import keyword followed by the module name. For example, to import the math module, you would write:
import math;Once you have imported the module, you can use its functions, data types, and constants in your code by prefixing them with the module name, followed by a .. For example, to use the abs function, you would write:
println(math.abs(-5)); // 5Also, it is possible to import only specific elements from a module. For example, to import only the abs function, you would write:
import { abs } from math;
println(abs(-5)); // 5Below is a comprehensive list of all the modules available. Click on any module name to quickly access its corresponding documentation.
| math | os | io | numbers | strings | arrays | utf8 |
| hashing | encoding | socket | json | lists | ini | selectors |
Below you will find all modules with their public functions and constants.
The math module provides a wide range of functions for mathematical calculations, including basic arithmetic operations and more advanced functions such as trigonometric and logarithmic operations.
| abs | sin | cos | tan | asin | acos |
| atan | floor | ceil | round | pow | sqrt |
| cbrt | log | log2 | log10 | exp |
Returns the absolute value of a number.
fn abs(num: number) -> number;Example:
println(math.abs(-10)); // 10
println(math.abs(10)); // 10Returns the sine of a number.
fn sin(num: number) -> number;Example:
println(math.sin(0)); // 0
println(math.sin(1)); // 0.841471Returns the cosine of a number.
fn cos(num: number) -> number;Example:
println(math.cos(0)); // 1
println(math.cos(1)); // 0.540302Returns the tangent of a number.
fn tan(num: number) -> number;Example:
println(math.tan(0)); // 0
println(math.tan(1)); // 1.55741Returns the arc sine of a number.
fn asin(num: number) -> number;Example:
println(math.asin(0)); // 0
println(math.asin(1)); // 1.5708Returns the arc cosine of a number.
fn acos(num: number) -> number;Example:
println(math.acos(0)); // 1.5708
println(math.acos(1)); // 0Returns the arc tangent of a number.
fn atan(num: number) -> number;Example:
println(math.atan(0)); // 0
println(math.atan(1)); // 0.785398Returns the largest integer less than or equal to a number.
fn floor(num: number) -> number;Example:
println(math.floor(0.5)); // 0
println(math.floor(1.5)); // 1Returns the smallest integer greater than or equal to a number.
fn ceil(num: number) -> number;Example:
println(math.ceil(0.5)); // 1
println(math.ceil(1.5)); // 2Returns the nearest integer to a number.
fn round(num: number) -> number;Example:
println(math.round(0.4)); // 0
println(math.round(0.5)); // 1
println(math.round(0.6)); // 1Returns the value of a number raised to a power.
fn pow(num: number, power: number) -> number;Example:
println(math.pow(2, 2)); // 4
println(math.pow(2, 3)); // 8Returns the square root of a number.
fn sqrt(num: number) -> number;Example:
println(math.sqrt(4)); // 2
println(math.sqrt(9)); // 3
println(math.sqrt(10)); // 3.16228Returns the cube root of a number.
fn cbrt(num: number) -> number;Example:
println(math.cbrt(8)); // 2
println(math.cbrt(27)); // 3
println(math.cbrt(28)); // 3.03659Returns the natural logarithm of a number.
fn log(num: number) -> number;Example:
println(math.log(1)); // 0
println(math.log(2)); // 0.693147
println(math.log(10)); // 2.30259Returns the base 2 logarithm of a number.
fn log2(num: number) -> number;Example:
println(math.log2(1)); // 0
println(math.log2(2)); // 1
println(math.log2(10)); // 3.32193Returns the base 10 logarithm of a number.
fn log10(num: number) -> number;Example:
println(math.log10(1)); // 0
println(math.log10(2)); // 0.30103
println(math.log10(10)); // 1Returns the value of e constant raised to a power.
fn exp(num: number) -> number;Example:
println(math.exp(0)); // 1
println(math.exp(1)); // 2.71828
println(math.exp(10)); // 22026.5The os module provides a variety of functions and constants that allow you to interact with the operating system. For example, you can use this module to access system information like environment variables.
| CLOCKS_PER_SEC | clock | time | system |
| getenv | getcwd | name |
The number of clock ticks per second.
let CLOCKS_PER_SEC: number;Example:
println(os.CLOCKS_PER_SEC); // 1e+06Returns the time in seconds since the start of the program.
fn clock() -> number;Example:
println(os.clock()); // 0.007073Returns the current time in seconds since the Unix epoch.
fn time() -> number;Example:
println(os.time()); // 1.67683e+09Executes a command in the operating system shell and returns the exit code.
fn system(command: string) -> number;Example:
os.system("echo hello"); // In *nix systems, this will print "hello" to the terminal.Returns the value of an environment variable.
fn getenv(str: string) -> string;Example:
println(os.getenv("HOME")); // /home/usernameReturns the current working directory.
fn getcwd() -> string;Example:
println(os.getcwd()); // /home/usernameReturns the name of the operating system. Returns linux, macos, windows, or unknown.
fn name() -> string;Example:
println(os.name()); // linuxThe io module provides various facilities for handling I/O operations. It enables you to read from and write to different streams, such as files, and provides tools for working with binary and text files.
| stdin | stdout | stderr | SEEK_SET | SEEK_CUR |
| SEEK_END | open | close | popen | pclose |
| eof | flush | sync | tell | rewind |
| seek | read | write | readln | writeln |
The stdin, stdout, and stderr constants are used to access the standard input, output, and error streams.
let stdin: userdata;
let stdout: userdata;
let stderr: userdata;Example:
println(io.readln(io.stdin)); // Read a line from stdin and print it.
io.writeln(io.stdout, "Hello, world!"); // Hello, world!
io.writeln(io.stderr, "Error!"); // Error!The SEEK_SET, SEEK_CUR, and SEEK_END constants are used to specify the origin when seeking in a file. See the seek function for more information.
let SEEK_SET: number;
let SEEK_CUR: number;
let SEEK_END: number;Example:
let stream = io.open("file.txt", "r");
io.seek(stream, 0, io.SEEK_SET); // Seek to the beginning of the file.
io.seek(stream, 0, io.SEEK_CUR); // Seek to the current position.
io.seek(stream, 0, io.SEEK_END); // Seek to the end of the file.Opens a file and returns a file handle.
fn open(filename: string, mode: string) -> userdata;The filename argument is the name of the file to open, and the mode argument is a string that specifies the mode in which the file is opened.
The following table lists the possible values of the mode argument:
"r": Open file for reading. The file must exist."w": Truncate file to zero length or create file for writing."a": Append; open or create file for writing at end-of-file."r+": Open file for update (reading and writing)."w+": Truncate file to zero length or create file for update."a+": Append; open or create file for update, writing at end-of-file.
Example:
let stream = io.open("file.txt", "r"); // Open file.txt for reading.Closes a file.
fn close(stream: userdata);Example:
let stream = io.open("file.txt", "r");
io.close(stream);Opens a process by creating a pipe, forking, and invoking the shell.
fn popen(command: string, mode: string) -> userdata;The command argument is the command to execute, and the mode argument is the same as the mode argument of the open function.
Example:
let stream = io.popen("ls", "r"); // Execute the "ls" command for reading.Closes a process.
let stream = io.popen("ls", "r");
io.pclose(stream);Checks whether the end-of-file indicator is set for the given stream.
fn eof(stream: userdata) -> bool;Example:
let stream = io.open("file.txt", "r");
while (!io.eof(stream)) {
println(io.readln(stream));
}Flushes the output buffer of the given stream.
fn flush(stream: userdata);Example:
io.writeln(io.stdout, "Hello, world!");
io.flush(io.stdout);Flushes and synchronizes the output buffer of the given stream.
fn sync(stream: userdata);Example:
io.writeln(io.stdout, "Hello, world!");
io.sync(io.stdout);Returns the current position of the file pointer for the given stream.
fn tell(stream: userdata) -> number;Example:
let stream = io.open("file.txt", "r");
println(io.tell(stream)); // 0
io.readln(stream);
println(io.tell(stream)); // 6Rewinds the file pointer to the beginning of the given stream.
fn rewind(stream: userdata);Example:
let stream = io.open("file.txt", "r");
io.readln(stream);
io.rewind(stream);
println(io.tell(stream)); // 0Sets the position of the file pointer for the given stream.
fn seek(stream: userdata, offset: number, whence: number);The offset argument is the number of bytes to offset from the position specified by whence. The whence argument is an integer that determines the reference position from which to calculate the new position. It can be one of the following constants:
io.SEEK_SET: The beginning of the file.io.SEEK_CUR: The current position of the file pointer.io.SEEK_END: The end of the file.
Example:
let stream = io.open("file.txt", "w");
io.seek(stream, 0, io.SEEK_END); // Seek to the end of the file.
io.writeln(stream, "Hello, world!");Reads data from the given stream and returns it as a string.
fn read(stream: userdata, size: number) -> string;The size argument is the number of bytes to read.
Example:
let stream = io.open("file.txt", "r");
println(io.read(stream, 13)); // Hello, world!Writes the given string to the given stream.
fn write(stream: userdata, data: string);Example:
let stream = io.open("file.txt", "w");
io.write(stream, "Hello, world!");Reads data from the given stream until a newline character is encountered and returns it as a string.
fn readln(stream: userdata) -> string;Example:
let stream = io.open("file.txt", "r");
println(io.readln(stream)); // Hello, world!Writes the given string to the given stream, followed by a newline character.
fn writeln(stream: userdata, data: string);Example:
let stream = io.open("file.txt", "w");
io.writeln(stream, "Hello, world!"); // Appends a newline character at the end.The numbers module provides mathematical constants and limits, such as the maximum and minimum representable integers.
| PI | TAU | LARGEST | SMALLEST |
| MAX_INTEGER | MIN_INTEGER | srand | rand |
The mathematical constant π and τ (2π).
let PI: number;
let TAU: number;Example:
println(numbers.PI); // 3.14159
println(numbers.TAU); // 6.28319The largest and smallest representable numbers.
let LARGEST: number;
let SMALLEST: number;Example:
println(numbers.LARGEST); // 1.79769e+308
println(numbers.SMALLEST); // 2.22507e-308The largest and smallest safely representable integers.
let MAX_INTEGER: number;
let MIN_INTEGER: number;Example:
println(numbers.MAX_INTEGER); // 9.0072e+15
println(numbers.MIN_INTEGER); // -9.0072e+15Sets the seed for the random number generator.
fn srand(seed: number);Example:
numbers.srand(123);
println(numbers.rand());Returns a random number between 0 and 1.
fn rand() -> number;Example:
println(numbers.rand());
println(numbers.rand());The strings module provides functions for working with strings.
| new_string | repeat | hash | lower | upper |
| trim | starts_with | ends_with | reverse |
Creates a new string with the given minimum capacity.
Note: When setting the initial capacity of a string, it will be adjusted to the next power of two. As a result, the actual capacity of the string may be larger than the specified value.
fn new_string(min_capacity: number) -> string;Example:
let str = strings.new_string(10);
str += "Hello, world!";
println(str); // Hello, world!Returns a new string that is a copy of the given string repeated the given number of times.
fn repeat(str: string, count: number) -> string;Example:
let str = strings.repeat("foo", 3);
println(str); // foofoofooReturns the hash of the given string.
fn hash(str: string) -> number;Example:
println(strings.hash("Hello, world!")); // 3.9857e+09Returns a copy of the given string with all uppercase characters converted to lowercase.
fn lower(str: string) -> string;Example:
println(strings.lower("Hello, world!")); // hello, world!Returns a copy of the given string with all lowercase characters converted to uppercase.
fn upper(str: string) -> string;Example:
println(strings.upper("Hello, world!")); // HELLO, WORLD!Returns a copy of the given string with all leading and trailing whitespace removed.
fn trim(str: string) -> string;Example:
println(strings.trim(" Hello, world! ")); // Hello, world!Returns true if the given string starts with the given prefix.
fn starts_with(str1: string, str2: string) -> bool;Example:
println(strings.starts_with("Hello, world!", "Hello")); // true
println(strings.starts_with("Hello, world!", "world")); // falseReturns true if the given string ends with the given suffix.
fn ends_with(str1: string, str2: string) -> bool;Example:
println(strings.ends_with("Hello, world!", "world!")); // true
println(strings.ends_with("Hello, world!", "Hello")); // falseReturns a copy of the given string with the characters in reverse order.
fn reverse(str: string) -> string;Example:
println(strings.reverse("!dlrow ,olleH")); // Hello, world!The arrays module provides functions for working with arrays.
| new_array | fill | index_of | min | max |
| sum | avg | reverse | sort |
Creates a new array with the given minimum capacity.
Note: When setting the initial capacity of an array, it will be adjusted to the next power of two. As a result, the actual capacity of the array may be larger than the specified value.
fn new_array(min_capacity: number) -> array;Example:
let arr = arrays.new_array(10);
arr[] = 1;
arr[] = 2;
arr[] = 3;
println(arr); // [1, 2, 3]Returns a new array filled with the given value repeated the given number of times.
fn fill(elem, count: number) -> array;Example:
let arr = arrays.fill(1, 3);
println(arr); // [1, 1, 1]Returns the index of the first occurrence of the given element in the given array, or -1 if the element is not found.
fn index_of(arr: array, elem) -> number;Example:
let arr = [1, 2, 3];
println(arrays.index_of(arr, 2)); // 1
println(arrays.index_of(arr, 4)); // -1Returns the minimum value in the given array. All elements in the array must be of the same type and comparable; otherwise, a runtime error will be raised.
fn min(arr: array) -> any;Example:
let arr = [1, 2, 3];
println(arrays.min(arr)); // 1Returns the maximum value in the given array. All elements in the array must be of the same type and comparable; otherwise, a runtime error will be raised.
fn max(arr: array) -> any;Example:
let arr = [1, 2, 3];
println(arrays.max(arr)); // 3Returns the sum of all numbers in the given array. If any element in the array is not a number, the result will be 0.
fn sum(arr: array) -> number;Example:
let arr = [1, 2, 3];
println(arrays.sum(arr)); // 6Returns the average of all numbers in the given array. If any element in the array is not a number, the result will be 0.
fn avg(arr: array) -> number;Example:
let arr = [1, 2, 3];
println(arrays.avg(arr)); // 2Returns a copy of the given array with the elements in reverse order.
fn reverse(arr: array) -> array;Example:
let arr = [1, 2, 3];
println(arrays.reverse(arr)); // [3, 2, 1]Returns a copy of the given array with the elements sorted in ascending order.
fn sort(arr: array) -> array;Example:
let arr = [2, 3, 1];
println(arrays.sort(arr)); // [1, 2, 3]The utf8 module provides functions for working with UTF-8 strings. In Hook, strings are represented as arrays of bytes, making the functions in this module useful for working with strings that contain non-ASCII characters.
| len |
| sub |
Returns the number of unicode characters (code points) in the given string.
fn len(str: string) -> number;Example:
println(utf8.len("Hello, world!")); // 13
println(utf8.len("こんにちは世界")); // 7Returns a substring of the given string, starting at the given index and ending at the given index (exclusive).
fn sub(str: string, start: number, end: number) -> string;Example:
println(utf8.sub("Hello, world!", 7, 12)); // worldThe hashing module provides functions for computing cryptographic hashes.
| crc32 | crc64 | sha224 | sha256 | sha384 |
| sha512 | sha1 | sha3 | md5 | ripemd160 |
Computes the CRC-32 hash of the given string and returns the result as a integer number.
fn crc32(str: string) -> number;Example:
println(hashing.crc32("Hello, world!")); // 2.35637e+09Computes the CRC-64 hash of the given string and returns the result as a integer number.
fn crc64(str: string) -> number;Example:
println(hashing.crc64("Hello, world!")); // 1.26263e+19Computes the SHA-224 hash of the given string and returns the result as a binary string.
fn sha224(str: string) -> string;Example:
println(hex(hashing.sha224("Hello, world!"))); // 8552d8b7a7dc5476cb9e25dee69a8091290764b7...Computes the SHA-256 hash of the given string and returns the result as a binary string.
fn sha256(str: string) -> string;Example:
println(hex(hashing.sha256("Hello, world!"))); // 315f5bdb76d078c43b8ac0064e4a0164612b1fce...Computes the SHA-384 hash of the given string and returns the result as a binary string.
fn sha384(str: string) -> string;Example:
println(hex(hashing.sha384("Hello, world!"))); // 55bc556b0d2fe0fce582ba5fe07baafff0356536...Computes the SHA-512 hash of the given string and returns the result as a binary string.
fn sha512(str: string) -> string;Example:
println(hex(hashing.sha512("Hello, world!"))); // c1527cd893c124773d811911970c8fe6e857d6df...Computes the SHA-1 hash of the given string and returns the result as a binary string.
fn sha1(str: string) -> string;Example:
println(hex(hashing.sha1("Hello, world!"))); // 943a702d06f34599aee1f8da8ef9f7296031d699Computes the SHA-3 (256) hash of the given string and returns the result as a binary string.
fn sha3(str: string) -> string;Example:
println(hex(hashing.sha3("Hello, world!"))); // f345a219da005ebe9c1a1eaad97bbf38a10c8473...Computes the MD5 hash of the given string and returns the result as a binary string.
fn md5(str: string) -> string;Example:
println(hex(hashing.md5("Hello, world!"))); // 6cd3556deb0da54bca060b4c39479839Computes the RIPEMD-160 hash of the given string and returns the result as a binary string.
fn ripemd160(str: string) -> string;Example:
println(hex(hashing.ripemd160("Hello, world!"))); // 58262d1fbdbe4530d8865d3518c6d6e41002610fThe encoding module provides functions for encoding and decoding data.
| base32_encode | base32_decode | base58_encode | base58_decode |
| base64_encode | base64_decode | ascii85_encode | ascii85_decode |
Encodes the given string using the Base32 encoding.
fn base32_encode(str: string) -> string;Example:
println(encoding.base32_encode("Hello, world!")); // JBSWY3DPFQQHO33SNRSCC===Decodes the given string using the Base32 encoding.
fn base32_decode(str: string) -> string;Example:
println(encoding.base32_decode("JBSWY3DPFQQHO33SNRSCC===")); // Hello, world!Encodes the given string using the Base58 encoding.
fn base58_encode(str: string) -> string;Example:
println(encoding.base58_encode("Hello, world!")); // 72k1xXWG59wUsYv7h2Decodes the given string using the Base58 encoding.
fn base58_decode(str: string) -> string;Example:
println(encoding.base58_decode("72k1xXWG59wUsYv7h2")); // Hello, world!Encodes the given string using the Base64 encoding.
fn base64_encode(str: string) -> string;Example:
println(encoding.base64_encode("Hello, world!")); // SGVsbG8sIHdvcmxkIQ==Decodes the given string using the Base64 encoding.
fn base64_decode(str: string) -> string;Example:
println(encoding.base64_decode("SGVsbG8sIHdvcmxkIQ==")); // Hello, world!Encodes the given string using the Ascii85 encoding.
fn ascii85_encode(str: string) -> string;Example:
println(encoding.ascii85_encode("Hello, world!")); // 87cURD_*#TDfTZ)+TDecodes the given string using the Ascii85 encoding.
fn ascii85_decode(str: string) -> string;Example:
println(encoding.ascii85_decode("87cURD_*#TDfTZ)+T")); // Hello, world!The socket module provides functions and constants for working with sockets.
| AF_INET | AF_INET6 | SOCK_STREAM | SOCK_DGRAM | IPPROTO_TCP |
| IPPROTO_UDP | SOL_SOCKET | SO_REUSEADDR | new | close |
| connect | accept | bind | listen | send |
| recv | set_option | get_option | set_block | set_nonblock |
The AF_INET and AF_INET6 constants are used to specify the address family of a socket.
AF_INETis used for IPv4 sockets.AF_INET6is used for IPv6 sockets.
let AF_INET: number;
let AF_INET6: number;Example:
let sock_ipv4 = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
let sock_ipv6 = socket.new(socket.AF_INET6, socket.SOCK_STREAM, 0);The SOCK_STREAM and SOCK_DGRAM constants are used to specify the type of a socket.
SOCK_STREAMis used for TCP sockets.SOCK_DGRAMis used for UDP sockets.
let SOCK_STREAM: number;
let SOCK_DGRAM: number;Example:
let sock_tcp = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
let sock_udp = socket.new(socket.AF_INET, socket.SOCK_DGRAM, 0);The IPPROTO_TCP and IPPROTO_UDP constants are used to specify the protocol of a socket.
IPPROTO_TCPis used for TCP sockets.IPPROTO_UDPis used for UDP sockets.
Note: it is possible to pass
0as the protocol argument tosocket.newand the protocol will be automatically chosen based on the type of the socket.
let IPPROTO_TCP: number;
let IPPROTO_UDP: number;Example:
let sock_tcp = socket.new(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP);
let sock_udp = socket.new(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP);The SOL_SOCKET and SO_REUSEADDR constants are used to specify the level and option of a socket option. See set_option for more information.
SOL_SOCKETis used to specify the level of a socket option.SO_REUSEADDRis a socket option that allows a socket to be bound to an address that is already in use.
let SOL_SOCKET: number;
let SO_REUSEADDR: number;Example:
socket.set_option(sock, socket.SOL_SOCKET, socket.SO_REUSEADDR, 1);Creates a new socket with the given domain, type, and protocol.
fn new(domain: number, type: number, protocol: number) -> userdata;domainis the address family of the socket. See AF_INET, AF_INET6.typeis the type of the socket. See SOCK_STREAM, SOCK_DGRAM.protocolis the protocol of the socket. See IPPROTO_TCP, IPPROTO_UDP.
Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);Closes the given socket.
fn close(sock: userdata);Example:
socket.close(sock);Connects the given socket to the given host and port.
fn connect(sock: userdata, host: string, port: number);sockis the socket to connect.hostis a string representing the host to connect to.portis integer number representing the port to connect to.
Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.connect(sock, "localhost", 8080);Accepts a new connection on the given socket.
fn accept(sock: userdata) -> nil|userdata;Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.bind(sock, "localhost", 8080);
socket.listen(sock, 5);
let client = socket.accept(sock);Binds the given socket to the given host and port.
fn bind(sock: userdata, host: string, port: number);sockis the socket to bind.hostis a string representing the host to bind to.portis integer number representing the port to bind to.
Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.bind(sock, "localhost", 8080);Starts listening on the given socket.
fn listen(sock: userdata, backlog: number);sockis the socket to listen on.backlogis the maximum number of pending connections.
Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.bind(sock, "localhost", 8080);
socket.listen(sock, 5);Sends data on the given socket.
fn send(sock: userdata, data: string) -> number;sockis the socket to send data on.datais a string containing the data to send.
Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.connect(sock, "localhost", 8080);
socket.send(sock, "hello");Receives data on the given socket.
fn recv(sock: userdata, size: number) -> string;sockis the socket to receive data on.sizeis the maximum number of bytes to receive.
Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.connect(sock, "localhost", 8080);
let data = socket.recv(sock, 5);Sets a socket option.
fn set_option(sock: userdata, level: number, option: number, value: number);sockis the socket to set the option on.levelis a integer number representing the level of the option.optionis a integer number representing the option.valueis a integer number representing the value of the option.
Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.set_option(sock, socket.SOL_SOCKET, socket.SO_REUSEADDR, 1);Gets a socket option.
fn get_option(sock: userdata, level: number, option: number) -> number;sockis the socket to get the option from.levelis a integer number representing the level of the option.optionis a integer number representing the option.
Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.set_option(sock, socket.SOL_SOCKET, socket.SO_REUSEADDR, 1);
let value = socket.get_option(sock, socket.SOL_SOCKET, socket.SO_REUSEADDR);
println(value); // 1set_block(sock: userdata) set_nonblock(sock: userdata)
Sets the given socket to blocking mode.
fn set_block(sock: userdata);Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.set_block(sock);Sets the given socket to non-blocking mode.
fn set_nonblock(sock: userdata);Example:
let sock = socket.new(socket.AF_INET, socket.SOCK_STREAM, 0);
socket.set_nonblock(sock);The json module provides functions for encoding and decoding JSON.
| encode |
| decode |
Encodes the given value to JSON.
fn encode(value: any) -> string;Example:
let json = json.encode({ hello: "world" });
println(json); // {"hello":"world"}Decodes the given JSON string to a value.
fn decode(json: string) -> any;Example:
let value = json.decode('{"hello":"world"}');
println(value.hello); // worldThe lists module provides functions for working with lists.
| new_linked_list | len | is_empty | push_front | push_back |
| pop_front | pop_back | front | back |
Creates a new linked list.
fn new_linked_list() -> userdata;Example:
let list = lists.new_linked_list();Returns the length of the given list.
fn len(list: userdata) -> number;Example:
var list = lists.new_linked_list();
list = lists.push_back(list, 1);
list = lists.push_back(list, 2);
list = lists.push_back(list, 3);
println(lists.len(list)); // 3Returns true if the given list is empty.
fn is_empty(list: userdata) -> bool;Example:
let list = lists.new_linked_list();
println(lists.is_empty(list)); // truePushes the given value to the front of the given list and returns the new list.
fn push_front(list: userdata, value: any) -> userdata;Example:
var list = lists.new_linked_list();
list = lists.push_front(list, 1);
list = lists.push_front(list, 2);
println(lists.front(list)); // 2
println(lists.back(list)); // 1
#### push_back
Pushes the given value to the back of the given list and returns the new list.
```rust
fn push_back(list: userdata, value: any) -> userdata;
Example:
var list = lists.new_linked_list();
list = lists.push_back(list, 1);
list = lists.push_back(list, 2);
println(lists.front(list)); // 1
println(lists.back(list)); // 2Pops the first value from the given list and returns the new list.
fn pop_front(list: userdata) -> userdata;Example:
var list1 = lists.new_linked_list();
list1 = lists.push_back(list1, 1);
list1 = lists.push_back(list1, 2);
let list2 = lists.pop_front(list1);
println(lists.front(list2)); // 2Pops the last value from the given list and returns the new list.
fn pop_back(list: userdata) -> userdata;Example:
var list1 = lists.new_linked_list();
list1 = lists.push_back(list1, 1);
list1 = lists.push_back(list1, 2);
let list2 = lists.pop_back(list1);
println(lists.back(list2)); // 1Returns the first value from the given list.
fn front(list: userdata) -> any;Example:
var list = lists.new_linked_list();
list = lists.push_back(list, 1);
list = lists.push_back(list, 2);
println(lists.front(list)); // 1Returns the last value from the given list.
fn back(list: userdata) -> any;Example:
var list = lists.new_linked_list();
list = lists.push_back(list, 1);
list = lists.push_back(list, 2);
println(lists.back(list)); // 2The ini module provides functions for working with .ini files.
| load |
| get |
Loads the given .ini file and returns a configuration object.
fn load(filename: string) -> userdata;Example:
let config = ini.load("config.ini");Returns the value for the given configuration, section, and key.
fn get(config: userdata, section: string, key: string) -> string;Example:
let config = ini.load("config.ini");
println(ini.get(config, "section", "key")); // valueThe selectors module provides a wrapper around the select, poll, etc. system calls for monitoring multiple file descriptors, specially useful for network programming.
| POLLIN | POLLOUT | POLLERR |
| POLLHUP | POLLNVAL | POLLPRI |
| new_poll_selector | register | unregister |
| modify | poll |
The POLLIN constant is used to specify that data is available for reading.
let POLLIN: number;The POLLOUT constant is used to specify that data can be written without blocking.
let POLLOUT: number;The POLLERR constant is used to specify that an error has occurred.
let POLLERR: number;The POLLHUP constant is used to specify that the connection has been closed.
let POLLHUP: number;The POLLNVAL constant is used to specify that the file descriptor is invalid.
let POLLNVAL: number;The POLLPRI constant is used to specify that urgent data is available.
let POLLPRI: number;Creates a selector of type poll.
fn new_poll_selector() -> userdata;Example:
let selector = selectors.new_poll_selector();Registers the given file descriptor with the given events.
fn register(selector: userdata, fd: number, events: number);selectoris the selector to register the file descriptor with.fdis the file descriptor to register.eventsis the events to monitor. It can be a combination ofPOLLIN,POLLOUT,POLLERR,POLLHUP,POLLNVAL, andPOLLPRI.
Example:
let selector = selectors.new_poll_selector();
selectors.register(selector, sock, selectors.POLLIN);Unregisters the given file descriptor.
fn unregister(selector: userdata, fd: number);Example:
let selector = selectors.new_poll_selector();
selectors.register(selector, sock, selectors.POLLIN);
selectors.unregister(selector, sock);Modifies the events to monitor for the given file descriptor.
fn modify(selector: userdata, fd: number, events: number);Example:
let selector = selectors.new_poll_selector();
selectors.register(selector, sock, selectors.POLLIN);
selectors.modify(selector, sock, selectors.POLLOUT);Waits for events on the registered file descriptors.
fn poll(selector: userdata, timeout: number) -> array;selectoris the selector to wait for events on.timeoutis the maximum time to wait in milliseconds. Iftimeoutis0, the function will return immediately. Iftimeoutis-1, the function will block indefinitely.
Example:
let selector = selectors.new_poll_selector();
selectors.register(selector, sock, selectors.POLLIN);
let events = selectors.poll(selector, 1000);