Library reference
Everything built into Fubby. There's nothing to import, and every entry comes from the same table that's checked against the interpreter.
Output and size
| Signature | What it does |
|---|
print(values...) | Print the values to standard output, separated by spaces. |
len(value) -> Int | Number of items in a list or map, or characters in a string. |
Strings
| Signature | What it does |
|---|
str_upper(String text) -> String | Convert text to upper case. |
str_lower(String text) -> String | Convert text to lower case. |
str_trim(String text) -> String | Remove whitespace from both ends. |
str_contains(String text, String part) -> Bool | Whether text contains part. |
str_replace(String text, String from, String to) -> String | Replace every occurrence of from with to. |
str_split(String text, String separator) -> List | Split text into a list of strings. |
str_length(String text) -> Int | Number of characters. |
str_starts_with(String text, String prefix) -> Bool | Whether text starts with prefix. |
str_ends_with(String text, String suffix) -> Bool | Whether text ends with suffix. |
str_index_of(String text, String part) -> Int | Character index of the first match, or -1. |
str_substring(String text, Int start, Int end) -> String | Characters from start up to, but not including, end. |
str_repeat(String text, Int count) -> String | Repeat text count times. The result is limited to 1 MiB. |
Math
| Signature | What it does |
|---|
math_abs(number) -> Float | Absolute value. |
math_floor(number) -> Float | Round down. |
math_ceil(number) -> Float | Round up. |
math_sqrt(number) -> Float | Square root. The number cannot be negative. |
math_pow(base, exponent) -> Float | base raised to exponent. |
math_min(a, b) -> Float | The smaller of two numbers. |
math_max(a, b) -> Float | The larger of two numbers. |
math_sin(radians) -> Float | Sine. |
math_cos(radians) -> Float | Cosine. |
math_tan(radians) -> Float | Tangent. |
math_log(number) -> Float | Natural logarithm. The number must be above zero. |
math_exp(number) -> Float | e raised to number. |
math_round(number) -> Float | Round to the nearest whole number. |
math_trunc(number) -> Float | Drop the fractional part. |
List and map functions
| Signature | What it does |
|---|
list_contains(List list, item) -> Bool | Whether the list contains item. |
list_index_of(List list, item) -> Int | Index of the first match, or -1. |
list_reverse(List list) -> List | A new reversed list. |
list_slice(List list, Int start, Int end) -> List | A new list from start up to, but not including, end. |
list_concat(List first, List second) -> List | A new list joining both lists. |
list_insert(List list, Int index, value) -> List | A new list with value inserted. The original list is unchanged. |
list_remove(List list, Int index) -> List | A new list without the element at index. The original list is unchanged. |
map_keys(Map map) -> List | The keys, sorted. |
map_values(Map map) -> List | The values, in sorted key order. |
JSON, encoding and matching
| Signature | What it does |
|---|
json_encode(value) -> String | Convert a value to JSON text. |
json_decode(String json) -> value | Parse JSON text into maps, lists, and values. |
encoding_hex_encode(String text) -> String | Encode text as hexadecimal. |
encoding_hex_decode(String hex) -> String | Decode even-length hexadecimal to text. |
encoding_base64_encode(String text) -> String | Encode text as Base64. |
encoding_base64_decode(String base64) -> String | Decode Base64 to UTF-8 text. |
regex_is_match(String text, String pattern) -> Bool | Whether text matches the regular expression. Look-around and backreferences are not supported. |
Date, time and random numbers
| Signature | What it does |
|---|
time_parse_utc(String timestamp) -> Int | Parse an RFC 3339 timestamp to Unix seconds. |
time_format_utc(Int seconds) -> String | Format Unix seconds as YYYY-MM-DDTHH:MM:SSZ. |
time_is_leap_year(Int year) -> Bool | Whether year is a leap year. |
time_days_in_month(Int year, Int month) -> Int | Days in a month, where month is 1 to 12. |
clock_unix() -> Int | Current time in Unix seconds. |
clock_unix_millis() -> Int | Current time in Unix milliseconds. |
random_int(Int min, Int max) -> Int | Random integer in an inclusive range. Not cryptographically secure. |
System, environment and errors
| Signature | What it does |
|---|
env_get(String name) -> String | Value of an environment variable, or an empty string if it is not set. |
env_has(String name) -> Bool | Whether an environment variable is set. |
system_os() -> String | Operating system name. |
system_arch() -> String | CPU architecture name. |
system_cwd() -> String | Current working directory. |
system_args() -> List | Process arguments. |
error_message(String error) -> String | The text of a caught error. |
error_contains(String error, String part) -> Bool | Whether a caught error contains part. |
Files
| Signature | What it does |
|---|
file_read(String path) -> String | Read a text file. Respects FUBBY_FS_ROOT. |
file_write(String path, String contents) | Write a text file, replacing it if it exists. Respects FUBBY_FS_ROOT. |
file_exists(String path) -> Bool | Whether a path exists. Respects FUBBY_FS_ROOT. |
Collection and database methods
| Signature | What it does |
|---|
list.push(value) | Add a value to the end of the list. |
list.pop() -> value | Remove and return the last value. |
collection.length() -> Int | Number of items. |
map.set(String key, value) | Set a key. You can also write map["key"] = value. |
map.get(String key) -> value | Value for a key. A missing key is an error. |
map.has(String key) -> Bool | Whether the key exists. |
map.remove(String key) -> value | Remove a key and return its value. A missing key is an error. |
db.execute(String sql, List params) -> Map | Run a statement with ?1-style parameters. Returns changes and last_insert_rowid. |
db.query(String sql, List params) -> List | Run a query with ?1-style parameters. Returns a list of row maps. |
Moon: HTTP and SQLite
| Signature | What it does |
|---|
moon.http | HTTP client. Network access is denied unless FUBBY_NET_ALLOWLIST allows the host. |
moon.db | Database access. |
moon.security | Network policy and limit information. |
moon.net | Reserved for later. Calling it is an error. |
moon.http.get(String url, Map options) -> Map | Send a GET request and return status, headers, body, and url. Options can be empty: headers, timeout_ms, max_bytes. |
moon.http.post(String url, String body, Map options) -> Map | Send a POST request and return status, headers, body, and url. Options can be empty: headers, timeout_ms, max_bytes. |
moon.db.sqlite | SQLite databases. |
moon.db.sqlite.open(String path, Map options) -> Database | Open a database file, or ":memory:". Options can be empty: timeout_ms, max_rows, max_bytes. |
moon.security.network_allowed(String host) -> Bool | Whether the current allowlist permits this host. |
moon.security.limits() -> Map | The enforced HTTP and SQLite limits. |
Snow: web server
| Signature | What it does |
|---|
snow.get(String path, String handler) | Register a GET route during startup. The handler name is a function with no parameters and no return value. |
snow.post(String path, String handler) | Register a POST route during startup. |
snow.put(String path, String handler) | Register a PUT route during startup. |
snow.patch(String path, String handler) | Register a PATCH route during startup. |
snow.delete(String path, String handler) | Register a DELETE route during startup. |
snow.route(String method, String path, String handler) | Register a route for a named HTTP method during startup. |
snow.static(String prefix, String directory) | Serve a directory under a URL prefix, registered during startup. |
snow.before(String handler) | Run a handler before each request, registered during startup. |
snow.after(String handler) | Run a handler after each request, registered during startup. |
snow.on_error(String handler) | Set the handler for failed requests, registered during startup. |
snow.request() -> Map | The current request: method, path, body, params, query, headers, and state. |
snow.error() -> String | The current error message, inside an error handler. |
snow.responded() -> Bool | Whether a response has already been produced. |
snow.header(String name, String value) | Set a response header. |
snow.status(Int status) | Set the response status, from 200 to 599. |
snow.text(String body, Int status = 200) | Send a plain text response. |
snow.html(String body, Int status = 200) | Send an HTML response. |
snow.json(value, Int status = 200) | Send a JSON response. |
snow.template(String source, Map values) -> String | Fill {{name}} placeholders with HTML-escaped values. |
Bela: background tasks
| Signature | What it does |
|---|
bela.spawn(String function, input) -> Int | Start a task and return its id. The function takes no parameters and returns nothing; the input is passed as JSON. Workers cannot spawn tasks. |
bela.input() -> value | The task's input. Only available inside a task. |
bela.result(value) | Set the task's result. Only available inside a task. |
bela.join(Int task) -> value | Wait for a task and return its result. Each task can be joined once. |
bela.cancel(Int task) -> Bool | Request cooperative cancellation. Returns false if the task has already finished. |
bela.status(Int task) -> String | queued, running, cancelling, completed, failed, cancelled, timed_out, or joined. |
bela.channel(Int capacity) -> Int | Create a bounded channel with capacity 1 to 64, and return its id. |
bela.send(Int channel, value) | Send a value, waiting while the channel is full. |
bela.recv(Int channel) -> value | Receive a value, waiting while the channel is empty. |
bela.close(Int channel) | Close a channel. |
bela.sleep(Int milliseconds) | Pause for 0 to 30000 milliseconds. |