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

SignatureWhat it does
print(values...)Print the values to standard output, separated by spaces.
len(value) -> IntNumber of items in a list or map, or characters in a string.

Strings

SignatureWhat it does
str_upper(String text) -> StringConvert text to upper case.
str_lower(String text) -> StringConvert text to lower case.
str_trim(String text) -> StringRemove whitespace from both ends.
str_contains(String text, String part) -> BoolWhether text contains part.
str_replace(String text, String from, String to) -> StringReplace every occurrence of from with to.
str_split(String text, String separator) -> ListSplit text into a list of strings.
str_length(String text) -> IntNumber of characters.
str_starts_with(String text, String prefix) -> BoolWhether text starts with prefix.
str_ends_with(String text, String suffix) -> BoolWhether text ends with suffix.
str_index_of(String text, String part) -> IntCharacter index of the first match, or -1.
str_substring(String text, Int start, Int end) -> StringCharacters from start up to, but not including, end.
str_repeat(String text, Int count) -> StringRepeat text count times. The result is limited to 1 MiB.

Math

SignatureWhat it does
math_abs(number) -> FloatAbsolute value.
math_floor(number) -> FloatRound down.
math_ceil(number) -> FloatRound up.
math_sqrt(number) -> FloatSquare root. The number cannot be negative.
math_pow(base, exponent) -> Floatbase raised to exponent.
math_min(a, b) -> FloatThe smaller of two numbers.
math_max(a, b) -> FloatThe larger of two numbers.
math_sin(radians) -> FloatSine.
math_cos(radians) -> FloatCosine.
math_tan(radians) -> FloatTangent.
math_log(number) -> FloatNatural logarithm. The number must be above zero.
math_exp(number) -> Floate raised to number.
math_round(number) -> FloatRound to the nearest whole number.
math_trunc(number) -> FloatDrop the fractional part.

List and map functions

SignatureWhat it does
list_contains(List list, item) -> BoolWhether the list contains item.
list_index_of(List list, item) -> IntIndex of the first match, or -1.
list_reverse(List list) -> ListA new reversed list.
list_slice(List list, Int start, Int end) -> ListA new list from start up to, but not including, end.
list_concat(List first, List second) -> ListA new list joining both lists.
list_insert(List list, Int index, value) -> ListA new list with value inserted. The original list is unchanged.
list_remove(List list, Int index) -> ListA new list without the element at index. The original list is unchanged.
map_keys(Map map) -> ListThe keys, sorted.
map_values(Map map) -> ListThe values, in sorted key order.

JSON, encoding and matching

SignatureWhat it does
json_encode(value) -> StringConvert a value to JSON text.
json_decode(String json) -> valueParse JSON text into maps, lists, and values.
encoding_hex_encode(String text) -> StringEncode text as hexadecimal.
encoding_hex_decode(String hex) -> StringDecode even-length hexadecimal to text.
encoding_base64_encode(String text) -> StringEncode text as Base64.
encoding_base64_decode(String base64) -> StringDecode Base64 to UTF-8 text.
regex_is_match(String text, String pattern) -> BoolWhether text matches the regular expression. Look-around and backreferences are not supported.

Date, time and random numbers

SignatureWhat it does
time_parse_utc(String timestamp) -> IntParse an RFC 3339 timestamp to Unix seconds.
time_format_utc(Int seconds) -> StringFormat Unix seconds as YYYY-MM-DDTHH:MM:SSZ.
time_is_leap_year(Int year) -> BoolWhether year is a leap year.
time_days_in_month(Int year, Int month) -> IntDays in a month, where month is 1 to 12.
clock_unix() -> IntCurrent time in Unix seconds.
clock_unix_millis() -> IntCurrent time in Unix milliseconds.
random_int(Int min, Int max) -> IntRandom integer in an inclusive range. Not cryptographically secure.

System, environment and errors

SignatureWhat it does
env_get(String name) -> StringValue of an environment variable, or an empty string if it is not set.
env_has(String name) -> BoolWhether an environment variable is set.
system_os() -> StringOperating system name.
system_arch() -> StringCPU architecture name.
system_cwd() -> StringCurrent working directory.
system_args() -> ListProcess arguments.
error_message(String error) -> StringThe text of a caught error.
error_contains(String error, String part) -> BoolWhether a caught error contains part.

Files

SignatureWhat it does
file_read(String path) -> StringRead 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) -> BoolWhether a path exists. Respects FUBBY_FS_ROOT.

Collection and database methods

SignatureWhat it does
list.push(value)Add a value to the end of the list.
list.pop() -> valueRemove and return the last value.
collection.length() -> IntNumber of items.
map.set(String key, value)Set a key. You can also write map["key"] = value.
map.get(String key) -> valueValue for a key. A missing key is an error.
map.has(String key) -> BoolWhether the key exists.
map.remove(String key) -> valueRemove a key and return its value. A missing key is an error.
db.execute(String sql, List params) -> MapRun a statement with ?1-style parameters. Returns changes and last_insert_rowid.
db.query(String sql, List params) -> ListRun a query with ?1-style parameters. Returns a list of row maps.

Moon: HTTP and SQLite

SignatureWhat it does
moon.httpHTTP client. Network access is denied unless FUBBY_NET_ALLOWLIST allows the host.
moon.dbDatabase access.
moon.securityNetwork policy and limit information.
moon.netReserved for later. Calling it is an error.
moon.http.get(String url, Map options) -> MapSend 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) -> MapSend a POST request and return status, headers, body, and url. Options can be empty: headers, timeout_ms, max_bytes.
moon.db.sqliteSQLite databases.
moon.db.sqlite.open(String path, Map options) -> DatabaseOpen a database file, or ":memory:". Options can be empty: timeout_ms, max_rows, max_bytes.
moon.security.network_allowed(String host) -> BoolWhether the current allowlist permits this host.
moon.security.limits() -> MapThe enforced HTTP and SQLite limits.

Snow: web server

SignatureWhat 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() -> MapThe current request: method, path, body, params, query, headers, and state.
snow.error() -> StringThe current error message, inside an error handler.
snow.responded() -> BoolWhether 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) -> StringFill {{name}} placeholders with HTML-escaped values.

Bela: background tasks

SignatureWhat it does
bela.spawn(String function, input) -> IntStart 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() -> valueThe 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) -> valueWait for a task and return its result. Each task can be joined once.
bela.cancel(Int task) -> BoolRequest cooperative cancellation. Returns false if the task has already finished.
bela.status(Int task) -> Stringqueued, running, cancelling, completed, failed, cancelled, timed_out, or joined.
bela.channel(Int capacity) -> IntCreate 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) -> valueReceive 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.