Skip to content

Native Functions

Native functions are Rust functions exposed to Flint bytecode through ncall and ncallr.

txt
ncall debug.print, r0
ncallr r1, json.object
ncallr r2, json.set, r2, r3, r4

Arguments must be registers. Load literals into registers before calling a native.

Namespaces

NamespacePurpose
debug.*Print values during development.
string.*Text operations and string conversion.
json.*Create, inspect, update, parse, and serialize JSON.
math.*Numeric helpers and random numbers.
time.*Current Unix time.
env.*Environment variables.
crypto.*UUID generation.
ui.*Build Flint's default styled HTML for UI pages.
http.*Request and response helpers inside HTTP handlers.

Calling Rules

Use ncall for side effects:

txt
ncall http.text, r0

Use ncallr for return values:

txt
ncallr r0, json.object

If a native receives the wrong type, the runtime error names the native and the invalid argument position. Standard natives also reject the wrong number of arguments. If ncallr is used with a native that returns no value, the VM raises a runtime error.

debug.*

NativeCallResult
debug.printncall debug.print, r0, r1, ...Prints arguments to stdout, space-separated.

debug.print returns no value.

string.*

Tests

NativeCallReturns
string.equalsncallr dst, string.equals, a, b1 when strings are equal, else 0.
string.containsncallr dst, string.contains, s, sub1 when s contains sub, else 0.
string.starts_withncallr dst, string.starts_with, s, prefix1 when s starts with prefix, else 0.
string.ends_withncallr dst, string.ends_with, s, suffix1 when s ends with suffix, else 0.
string.escape_htmlncallr dst, string.escape_html, sText escaped for safe HTML insertion.

Transform

NativeCallReturns
string.concatncallr dst, string.concat, a, ba followed by b.
string.trimncallr dst, string.trim, ss without leading and trailing whitespace.
string.to_upperncallr dst, string.to_upper, sUppercase text.
string.to_lowerncallr dst, string.to_lower, sLowercase text.
string.replacencallr dst, string.replace, s, from, toText with all matches replaced.
string.slicencallr dst, string.slice, s, start, endCharacter slice between clamped indices.

string.slice indexes by Unicode scalar values (chars), clamps both indices to the string length, and returns the range between the smaller and larger index.

Convert and Split

NativeCallReturns
string.lenncallr dst, string.len, sCharacter count as int.
string.splitncallr dst, string.split, s, sepJSON array of string parts.
string.to_intncallr dst, string.to_int, sParsed integer after trimming whitespace.
string.from_intncallr dst, string.from_int, nInteger as string.
string.fromncallr dst, string.from, valueAny VM value as string.

string.to_int fails if the string cannot be parsed as an i64.

string.from uses the runtime display form: strings are emitted as raw text, JSON is compact JSON, and numbers use their normal textual form.

string.escape_html replaces HTML-sensitive characters like <, >, &, quotes, and apostrophes with entities. Page output expressions use it automatically after string.from.

json.*

JSON is a VM value. There is no JSON literal syntax in .fl source.

Copy-on-write

json.set, json.push, json.delete, and json.merge return new JSON documents. Store the returned value if you want to keep the change.

Create and Parse

NativeCallReturns
json.objectncallr dst, json.objectEmpty object {}.
json.arrayncallr dst, json.arrayEmpty array [].
json.nullncallr dst, json.nullJSON null.
json.boolncallr dst, json.bool, nJSON true when n != 0, else false.
json.parsencallr dst, json.parse, sParsed JSON from a string.

json.parse fails on invalid JSON.

Read

NativeCallReturns
json.getncallr dst, json.get, j, keyObject field, non-negative array item, or JSON null.
json.hasncallr dst, json.has, j, key1 if object field exists, else 0.
json.lenncallr dst, json.len, jLength of array, object, or JSON string.
json.typencallr dst, json.type, jJSON type name.
json.keysncallr dst, json.keys, jJSON array of object keys.

json.get accepts string keys for objects and non-negative integer keys for arrays. Missing values return JSON null.

json.len fails unless the value is an array, object, or string.

json.keys fails unless the value is an object.

json.type returns one of:

txt
null bool number string array object

Update

NativeCallReturns
json.setncallr dst, json.set, j, key, valueCopy of j with key set.
json.pushncallr dst, json.push, j, valueCopy of an array with value appended.
json.deletencallr dst, json.delete, j, keyCopy of object j without key.
json.mergencallr dst, json.merge, base, patchCopy of base with fields from object patch.

json.set accepts a string key for objects or a non-negative integer key for arrays. If the existing JSON value is not the needed container type, it is normalized to an empty object or array first.

When setting an array index beyond the current length, the array is expanded with JSON null values. Implicit expansion is capped at index 1000000 to avoid accidental huge allocations.

json.push normalizes non-array input to an empty array before appending.

json.delete only removes keys from objects. Non-object input is returned unchanged.

json.merge only merges when both values are objects. Otherwise, base is returned unchanged.

Values inserted by json.set and json.push are converted as:

VM valueJSON value
intJSON number.
floatJSON number, or JSON null if not finite.
strJSON string.
jsonOriginal JSON value.

Convert

NativeCallReturns
json.stringifyncallr dst, json.stringify, jCompact JSON string.
json.to_intncallr dst, json.to_int, jJSON integer as int.
json.to_strncallr dst, json.to_str, jJSON string as str.
json.from_intncallr dst, json.from_int, nInteger converted to JSON number.
json.from_strncallr dst, json.from_str, sString converted to JSON string.

json.to_int fails unless the JSON value is an integer. json.to_str fails unless the JSON value is a string.

math.*

NativeCallReturns
math.absncallr dst, math.abs, nAbsolute value of an int or float; in-range only.
math.minncallr dst, math.min, a, bSmaller numeric value.
math.maxncallr dst, math.max, a, bLarger numeric value.
math.floorncallr dst, math.floor, nRounded down as int; finite/in-range only.
math.ceilncallr dst, math.ceil, nRounded up as int; finite/in-range only.
math.sqrtncallr dst, math.sqrt, nSquare root as float.
math.powncallr dst, math.pow, base, expbase ^ exp as float.
math.randomncallr dst, math.randomRandom float in [0.0, 1.0).
math.rand_intncallr dst, math.rand_int, min, maxRandom int in [min, max].

math.floor and math.ceil fail if the rounded result is not finite or does not fit in a Flint int.

math.min, math.max, and math.pow accept mixed int and float arguments. Mixed min and max results are floats.

math.rand_int fails when min > max.

time.*

NativeCallReturns
time.nowncallr dst, time.nowUnix timestamp in milliseconds as int.

If the system clock is before the Unix epoch, time.now returns 0.

env.*

NativeCallReturns
env.getncallr dst, env.get, nameEnvironment variable value, or "" if missing.

crypto.*

NativeCallReturns
crypto.uuidncallr dst, crypto.uuidRandom UUID v4 string.

ui.*

ui.* natives build Flint's default styled HTML, one fragment at a time. Each takes the current HTML accumulator as html and returns the accumulator with a fragment appended — the same shape as string.concat. See UI Pages for the page-level usage pattern.

NativeCallReturns
ui.windowncallr dst, ui.window, html, titlehtml with the document shell, default stylesheet, browser tab title (<title>), and a styled page frame for title appended.
ui.window_endncallr dst, ui.window_end, htmlhtml with a frame opened by ui.window closed.
ui.cardncallr dst, ui.card, html, titlehtml with a bordered content panel for title appended.
ui.card_endncallr dst, ui.card_end, htmlhtml with a panel opened by ui.card closed.
ui.sectionncallr dst, ui.section, html, titlehtml with an unframed content group for title appended.
ui.section_endncallr dst, ui.section_end, htmlhtml with a group opened by ui.section closed.
ui.rowncallr dst, ui.row, htmlhtml with a horizontal responsive layout opened.
ui.row_endncallr dst, ui.row_end, htmlhtml with a layout opened by ui.row closed.
ui.columnncallr dst, ui.column, htmlhtml with a vertical layout opened.
ui.column_endncallr dst, ui.column_end, htmlhtml with a layout opened by ui.column closed.
ui.titlencallr dst, ui.title, html, valuehtml with a heading for value appended.
ui.textncallr dst, ui.text, html, valuehtml with a paragraph for value appended.
ui.fieldncallr dst, ui.field, html, label, valuehtml with a label/value display row appended.
ui.buttonncallr dst, ui.button, html, label, hrefhtml with a link styled as a button appended.
ui.formncallr dst, ui.form, html, method, actionhtml with an HTML form opened.
ui.form_endncallr dst, ui.form_end, htmlhtml with a form opened by ui.form closed.
ui.inputncallr dst, ui.input, html, label, namehtml with a labeled text input appended.
ui.submitncallr dst, ui.submit, html, labelhtml with a submit button appended.

title, value, label, href, method, action, and name must be str. ui.title, ui.text, ui.field, and the label arguments of ui.button/ui.input/ui.submit are HTML-escaped; ui.button's href, ui.form's method/action, and ui.input's name are attribute-escaped.

http.*

http.* natives are available in HTTP handlers because the HTTP dispatcher registers them per request. See HTTP Runtime for request flow and response defaults.

Request

NativeCallReturns
http.methodncallr dst, http.methodHTTP method as str.
http.pathncallr dst, http.pathRequest path as str.
http.bodyncallr dst, http.bodyRaw request body as str.
http.paramncallr dst, http.param, namePath parameter as str, or "".
http.queryncallr dst, http.query, nameQuery parameter as str, or "".
http.headerncallr dst, http.header, nameHeader value as lossy UTF-8 str, or "".
http.cookiencallr dst, http.cookie, nameCookie value as str, or "".
http.json_bodyncallr dst, http.json_bodyParsed request body as json.
http.formncallr dst, http.form, fieldURL-encoded form field as str, or "".

http.json_body fails if the body is not valid JSON.

Response

NativeCallEffect
http.set_statusncall http.set_status, codeSet HTTP status code.
http.set_headerncall http.set_header, name, valueAppend a response header.
http.set_cookiencall http.set_cookie, name, valueAppend a simple Set-Cookie header.
http.textncall http.text, sSet plain text response body.
http.htmlncall http.html, sSet HTML response body.
http.jsonncall http.json, jSet JSON response body.
http.redirectncall http.redirect, urlSet status 302 and append location.
http.abortncall http.abortStop the handler and send the current response.

http.set_status fails if the integer is not a valid HTTP status code.

Experimental assembly-like language for APIs and web systems.