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 its first argument and returns the updated accumulator. UI pages normally call these through section .render; direct calls are useful when reading generated output or writing custom route handlers.

Shell and Layout

NativeCall
ui.windowncallr dst, ui.window, html, title
ui.window_endncallr dst, ui.window_end, html
ui.layoutncallr dst, ui.layout, html
ui.layout_endncallr dst, ui.layout_end, html
ui.sidebarncallr dst, ui.sidebar, html
ui.sidebar_endncallr dst, ui.sidebar_end, html
ui.mainncallr dst, ui.main, html
ui.main_endncallr dst, ui.main_end, html
ui.cardncallr dst, ui.card, html, title
ui.card_endncallr dst, ui.card_end, html
ui.sectionncallr dst, ui.section, html, title[, subtitle]
ui.section_endncallr dst, ui.section_end, html
ui.rowncallr dst, ui.row, html
ui.row_endncallr dst, ui.row_end, html
ui.columnncallr dst, ui.column, html
ui.column_endncallr dst, ui.column_end, html
ui.toolbarncallr dst, ui.toolbar, html
ui.toolbar_endncallr dst, ui.toolbar_end, html
ui.action_barncallr dst, ui.action_bar, html
ui.action_bar_endncallr dst, ui.action_bar_end, html
ui.footerncallr dst, ui.footer, html[, text]
ui.footer_endncallr dst, ui.footer_end, html
ui.dividerncallr dst, ui.divider, html
NativeCall
ui.navbarncallr dst, ui.navbar, html
ui.nav_itemncallr dst, ui.nav_item, html, label, href
ui.navbar_endncallr dst, ui.navbar_end, html
ui.menuncallr dst, ui.menu, html, title
ui.menu_itemncallr dst, ui.menu_item, html, label, href
ui.menu_activencallr dst, ui.menu_active, html, label, href
ui.menu_endncallr dst, ui.menu_end, html
ui.breadcrumbncallr dst, ui.breadcrumb, html
ui.breadcrumb_itemncallr dst, ui.breadcrumb_item, html, label, href
ui.breadcrumb_endncallr dst, ui.breadcrumb_end, html
ui.paginationncallr dst, ui.pagination, html
ui.page_itemncallr dst, ui.page_item, html, label, href
ui.page_currentncallr dst, ui.page_current, html, label
ui.pagination_endncallr dst, ui.pagination_end, html

Content

NativeCall
ui.titlencallr dst, ui.title, html, value
ui.textncallr dst, ui.text, html, value
ui.fieldncallr dst, ui.field, html, label, value
ui.badgencallr dst, ui.badge, html, label
ui.alertncallr dst, ui.alert, html, kind, message
ui.statusncallr dst, ui.status, html, label, kind
ui.progressncallr dst, ui.progress, html, value, max
ui.meterncallr dst, ui.meter, html, value, max
ui.statncallr dst, ui.stat, html, label, value
ui.codencallr dst, ui.code, html, value
ui.kbdncallr dst, ui.kbd, html, value
ui.linkncallr dst, ui.link, html, label, href
ui.imagencallr dst, ui.image, html, src, alt
ui.emptyncallr dst, ui.empty, html, message

Lists, Tables, and Groups

NativeCall
ui.listncallr dst, ui.list, html
ui.list_itemncallr dst, ui.list_item, html, text
ui.list_endncallr dst, ui.list_end, html
ui.olncallr dst, ui.ol, html
ui.ol_itemncallr dst, ui.ol_item, html, text
ui.ol_endncallr dst, ui.ol_end, html
ui.tablencallr dst, ui.table, html
ui.captionncallr dst, ui.caption, html, text
ui.trncallr dst, ui.tr, html
ui.tr_endncallr dst, ui.tr_end, html
ui.thncallr dst, ui.th, html, label
ui.tdncallr dst, ui.td, html, value
ui.tfootncallr dst, ui.tfoot, html
ui.tfoot_endncallr dst, ui.tfoot_end, html
ui.table_endncallr dst, ui.table_end, html
ui.tabsncallr dst, ui.tabs, html
ui.tabncallr dst, ui.tab, html, label, id
ui.tabs_bodyncallr dst, ui.tabs_body, html
ui.tab_panelncallr dst, ui.tab_panel, html, id
ui.tab_panel_endncallr dst, ui.tab_panel_end, html
ui.tabs_endncallr dst, ui.tabs_end, html
ui.accordionncallr dst, ui.accordion, html
ui.accordion_itemncallr dst, ui.accordion_item, html, title
ui.accordion_item_endncallr dst, ui.accordion_item_end, html
ui.accordion_endncallr dst, ui.accordion_end, html
ui.treencallr dst, ui.tree, html
ui.tree_itemncallr dst, ui.tree_item, html, label, href
ui.tree_groupncallr dst, ui.tree_group, html, label
ui.tree_group_endncallr dst, ui.tree_group_end, html
ui.tree_endncallr dst, ui.tree_end, html
ui.stepsncallr dst, ui.steps, html
ui.stepncallr dst, ui.step, html, label, active
ui.steps_endncallr dst, ui.steps_end, html

Forms, Actions, and Dialogs

NativeCall
ui.buttonncallr dst, ui.button, html, label, href
ui.formncallr dst, ui.form, html, method, action
ui.form_endncallr dst, ui.form_end, html
ui.fieldsetncallr dst, ui.fieldset, html, legend
ui.fieldset_endncallr dst, ui.fieldset_end, html
ui.inputncallr dst, ui.input, html, label, name
ui.passwordncallr dst, ui.password, html, label, name
ui.numberncallr dst, ui.number, html, label, name
ui.filencallr dst, ui.file, html, label, name
ui.textareancallr dst, ui.textarea, html, label, name
ui.selectncallr dst, ui.select, html, label, name
ui.optionncallr dst, ui.option, html, label, value
ui.select_endncallr dst, ui.select_end, html
ui.checkboxncallr dst, ui.checkbox, html, label, name, value
ui.radioncallr dst, ui.radio, html, label, name, value
ui.hiddenncallr dst, ui.hidden, html, name, value
ui.submitncallr dst, ui.submit, html, label
ui.dialogncallr dst, ui.dialog, html, id, title
ui.dialog_endncallr dst, ui.dialog_end, html
ui.dialog_triggerncallr dst, ui.dialog_trigger, html, label, id
ui.dialog_alertncallr dst, ui.dialog_alert, html, id, title, message
ui.dialog_confirmncallr dst, ui.dialog_confirm, html, id, title, message, action
ui.dialog_promptncallr dst, ui.dialog_prompt, html, id, title, label, name, action

Text-like values are escaped for HTML where appropriate; hrefs, form attributes, names, ids, and similar arguments are attribute-escaped by the individual native.

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.