Hook's core modules
April 1, 2024 ยท View on GitHub
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.
Importing modules
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)); // 5
Also, 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)); // 5
List of modules
Below 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.
math
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 |
abs
Returns the absolute value of a number.
fn abs(num: number) -> number;
Example:
println(math.abs(-10)); // 10
println(math.abs(10)); // 10
sin
Returns the sine of a number.
fn sin(num: number) -> number;
Example:
println(math.sin(0)); // 0
println(math.sin(1)); // 0.841471
cos
Returns the cosine of a number.
fn cos(num: number) -> number;
Example:
println(math.cos(0)); // 1
println(math.cos(1)); // 0.540302
tan
Returns the tangent of a number.
fn tan(num: number) -> number;
Example:
println(math.tan(0)); // 0
println(math.tan(1)); // 1.55741
asin
Returns the arc sine of a number.
fn asin(num: number) -> number;
Example:
println(math.asin(0)); // 0
println(math.asin(1)); // 1.5708
acos
Returns the arc cosine of a number.
fn acos(num: number) -> number;
Example:
println(math.acos(0)); // 1.5708
println(math.acos(1)); // 0
atan
Returns the arc tangent of a number.
fn atan(num: number) -> number;
Example:
println(math.atan(0)); // 0
println(math.atan(1)); // 0.785398
floor
Returns 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)); // 1
ceil
Returns 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)); // 2
round
Returns 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)); // 1
pow
Returns 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)); // 8
sqrt
Returns 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.16228
cbrt
Returns 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.03659
log
Returns 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.30259
log2
Returns 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.32193
log10
Returns 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)); // 1
exp
Returns 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.5
os
The 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 |
CLOCKS_PER_SEC
The number of clock ticks per second.
let CLOCKS_PER_SEC: number;
Example:
println(os.CLOCKS_PER_SEC); // 1e+06
clock
Returns the time in seconds since the start of the program.
fn clock() -> number;
Example:
println(os.clock()); // 0.007073
time
Returns the current time in seconds since the Unix epoch.
fn time() -> number;
Example:
println(os.time()); // 1.67683e+09
system
Executes 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.
getenv
Returns the value of an environment variable.
fn getenv(str: string) -> string;
Example:
println(os.getenv("HOME")); // /home/username
getcwd
Returns the current working directory.
fn getcwd() -> string;
Example:
println(os.getcwd()); // /home/username
name
Returns the name of the operating system. Returns linux, macos, windows, or unknown.
fn name() -> string;
Example:
println(os.name()); // linux
io
The 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 |
stdin, stdout, stderr
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!
SEEK_SET, SEEK_CUR, SEEK_END
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.
open
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.
close
Closes a file.
fn close(stream: userdata);
Example:
let stream = io.open("file.txt", "r");
io.close(stream);
popen
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.
pclose
Closes a process.
let stream = io.popen("ls", "r");
io.pclose(stream);
eof
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));
}
flush
Flushes the output buffer of the given stream.
fn flush(stream: userdata);
Example:
io.writeln(io.stdout, "Hello, world!");
io.flush(io.stdout);
sync
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);
tell
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)); // 6
rewind
Rewinds 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)); // 0
seek
Sets 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!");
read
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!
write
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!");
readln
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!
writeln
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.
numbers
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 |
PI, TAU
The mathematical constant ฯ and ฯ (2ฯ).
let PI: number;
let TAU: number;
Example:
println(numbers.PI); // 3.14159
println(numbers.TAU); // 6.28319
LARGEST, SMALLEST
The largest and smallest representable numbers.
let LARGEST: number;
let SMALLEST: number;
Example:
println(numbers.LARGEST); // 1.79769e+308
println(numbers.SMALLEST); // 2.22507e-308
MAX_INTEGER, MIN_INTEGER
The 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+15
srand
Sets the seed for the random number generator.
fn srand(seed: number);
Example:
numbers.srand(123);
println(numbers.rand());
rand
Returns a random number between 0 and 1.
fn rand() -> number;
Example:
println(numbers.rand());
println(numbers.rand());
strings
The strings module provides functions for working with strings.
| new_string | repeat | hash | lower | upper |
| trim | starts_with | ends_with | reverse |
new_string
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!
repeat
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); // foofoofoo
hash
Returns the hash of the given string.
fn hash(str: string) -> number;
Example:
println(strings.hash("Hello, world!")); // 3.9857e+09
lower
Returns 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!
upper
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!
trim
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!
starts_with
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")); // false
ends_with
Returns 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")); // false
reverse
Returns 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!
arrays
The arrays module provides functions for working with arrays.
| new_array | fill | index_of | min | max |
| sum | avg | reverse | sort |
new_array
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]
fill
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]
index_of
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)); // -1
min
Returns 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)); // 1
max
Returns 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)); // 3
sum
Returns 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)); // 6
avg
Returns 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)); // 2
reverse
Returns 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]
sort
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]
utf8
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 |
len
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("ใใใซใกใฏไธ็")); // 7
sub
Returns 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)); // world
hashing
The hashing module provides functions for computing cryptographic hashes.
| crc32 | crc64 | sha224 | sha256 | sha384 |
| sha512 | sha1 | sha3 | md5 | ripemd160 |
crc32
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+09
crc64
Computes 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+19
sha224
Computes 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...
sha256
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...
sha384
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...
sha512
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...
sha1
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!"))); // 943a702d06f34599aee1f8da8ef9f7296031d699
sha3
Computes 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...
md5
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!"))); // 6cd3556deb0da54bca060b4c39479839
ripemd160
Computes 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!"))); // 58262d1fbdbe4530d8865d3518c6d6e41002610f
encoding
The 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 |
base32_encode
Encodes the given string using the Base32 encoding.
fn base32_encode(str: string) -> string;
Example:
println(encoding.base32_encode("Hello, world!")); // JBSWY3DPFQQHO33SNRSCC===
base32_decode
Decodes the given string using the Base32 encoding.
fn base32_decode(str: string) -> string;
Example:
println(encoding.base32_decode("JBSWY3DPFQQHO33SNRSCC===")); // Hello, world!
base58_encode
Encodes the given string using the Base58 encoding.
fn base58_encode(str: string) -> string;
Example:
println(encoding.base58_encode("Hello, world!")); // 72k1xXWG59wUsYv7h2
base58_decode
Decodes the given string using the Base58 encoding.
fn base58_decode(str: string) -> string;
Example:
println(encoding.base58_decode("72k1xXWG59wUsYv7h2")); // Hello, world!
base64_encode
Encodes the given string using the Base64 encoding.
fn base64_encode(str: string) -> string;
Example:
println(encoding.base64_encode("Hello, world!")); // SGVsbG8sIHdvcmxkIQ==
base64_decode
Decodes the given string using the Base64 encoding.
fn base64_decode(str: string) -> string;
Example:
println(encoding.base64_decode("SGVsbG8sIHdvcmxkIQ==")); // Hello, world!
ascii85_encode
Encodes the given string using the Ascii85 encoding.
fn ascii85_encode(str: string) -> string;
Example:
println(encoding.ascii85_encode("Hello, world!")); // 87cURD_*#TDfTZ)+T
ascii85_decode
Decodes the given string using the Ascii85 encoding.
fn ascii85_decode(str: string) -> string;
Example:
println(encoding.ascii85_decode("87cURD_*#TDfTZ)+T")); // Hello, world!
socket
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 |
AF_INET, AF_INET6
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);
SOCK_STREAM, SOCK_DGRAM
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);
IPPROTO_TCP, IPPROTO_UDP
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);
SOL_SOCKET, SO_REUSEADDR
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);
new
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);
close
Closes the given socket.
fn close(sock: userdata);
Example:
socket.close(sock);
connect
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);
accept
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);
bind
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);
listen
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);
send
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");
recv
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);
set_option
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);
get_option
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); // 1
set_block(sock: userdata) set_nonblock(sock: userdata)
set_block
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);
set_nonblock
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);
json
The json module provides functions for encoding and decoding JSON.
| encode |
| decode |
encode
Encodes the given value to JSON.
fn encode(value: any) -> string;
Example:
let json = json.encode({ hello: "world" });
println(json); // {"hello":"world"}
decode
Decodes the given JSON string to a value.
fn decode(json: string) -> any;
Example:
let value = json.decode('{"hello":"world"}');
println(value.hello); // world
lists
The lists module provides functions for working with lists.
| new_linked_list | len | is_empty | push_front | push_back |
| pop_front | pop_back | front | back |
new_linked_list
Creates a new linked list.
fn new_linked_list() -> userdata;
Example:
let list = lists.new_linked_list();
len
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)); // 3
is_empty
Returns 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)); // true
push_front
Pushes 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)); // 2
pop_front
Pops 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)); // 2
pop_back
Pops 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)); // 1
front
Returns 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)); // 1
back
Returns 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)); // 2
ini
The ini module provides functions for working with .ini files.
| load |
| get |
load
Loads the given .ini file and returns a configuration object.
fn load(filename: string) -> userdata;
Example:
let config = ini.load("config.ini");
get
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")); // value
selectors
The 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 |
POLLIN
The POLLIN constant is used to specify that data is available for reading.
let POLLIN: number;
POLLOUT
The POLLOUT constant is used to specify that data can be written without blocking.
let POLLOUT: number;
POLLERR
The POLLERR constant is used to specify that an error has occurred.
let POLLERR: number;
POLLHUP
The POLLHUP constant is used to specify that the connection has been closed.
let POLLHUP: number;
POLLNVAL
The POLLNVAL constant is used to specify that the file descriptor is invalid.
let POLLNVAL: number;
POLLPRI
The POLLPRI constant is used to specify that urgent data is available.
let POLLPRI: number;
new_poll_selector
Creates a selector of type poll.
fn new_poll_selector() -> userdata;
Example:
let selector = selectors.new_poll_selector();
register
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);
unregister
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);
modify
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);
poll
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);