Skip to content

Built-in Functions

FunctionDescriptionExample
print(...)Print values to consoleprint("Hello", x, y);
input(prompt)Read string from userstr name = input("Name: ");
inputInt(prompt)Read integerint age = inputInt("Age: ");
inputFloat(prompt)Read floatfloat val = inputFloat("Value: ");
FunctionDescriptionExample
toInt(value)Convert to integerint x = toInt("123");
toFloat(value)Convert to floatfloat y = toFloat("3.14");
toString(value)Convert to stringstr s = toString(42);
toBool(value)Convert to booleanbool b = toBool(1);
FunctionDescriptionExample
length(arr)Get array/object lengthint len = length(arr);
push(arr, value)Add elementpush(arr, 10);
pop(arr)Remove and return lastint x = pop(arr);
range(n)Create integer rangefor (i in range(10)) {...}
keys(obj)Get object keys (insertion order)array k = keys({a:1,b:2});
FunctionDescriptionExample
now_iso8601()Current UTC time as ISO 8601str now = now_iso8601();
format_iso8601(secs)Unix seconds → YYYY-MM-DDTHH:MM:SSZstr s = format_iso8601(0);
parse_iso8601(s)ISO 8601 → unix seconds (-1 on fail)int t = parse_iso8601(now);
weekday(secs)Day of week (0=Sun … 6=Sat)int d = weekday(timestamp());
date_add_seconds(t, d)Add d seconds to tint t2 = date_add_seconds(t, 3600);
timestamp()Current Unix epoch (seconds)int now = timestamp();
time_ms()Current Unix epoch (milliseconds)int ms = time_ms();
clock_ms()Monotonic high-precision timerfloat t = clock_ms();
sleep(ms)Sleep for ms millisecondssleep(100);

POSIX/ECMAScript syntax via std::regex.

FunctionDescriptionExample
regex_match(pat, s)1 if s fully matches patint ok = regex_match("[0-9]+", "42");
regex_search(pat, s)1 if substring of s matchesint hit = regex_search("\\d+", "x42y");
regex_capture(pat, s)[whole, g1, g2, ...] or []array g = regex_capture("(\\w+)=(\\w+)", "a=b");
regex_replace(pat, s, r)Replace all matches; $1 etcstr s = regex_replace("\\d", "x", "_");
FunctionDescriptionExample
read_file(path)Read whole filestr s = read_file("data.txt");
write_file(path, data)Truncate + writewrite_file("out.txt", body);
append_file(path, data)Append to existingappend_file("log.txt", line);
file_exists(path)Check existencebool ok = file_exists("x");
file_glob(pattern)Shell-style * ? globarray f = file_glob("./*.tpr");

Built on an in-tree SHA-256 — no OpenSSL dependency, available everywhere the runtime runs.

FunctionDescriptionExample
sha256(s)Lowercase 64-char hex digeststr h = sha256(body);
hmac_sha256(key, msg)Keyed MAC (RFC 2104), 64-char hexstr sig = hmac_sha256(secret, data);
password_hash(pw)PBKDF2-HMAC-SHA256, self-describingstr h = password_hash(pw);
password_verify(pw, stored)Constant-time check vs password_hashbool ok = password_verify(pw, h);
secure_token(n)CSPRNG base62 string, n charsstr t = secure_token(48);
base64_encode(s) / base64_decode(s)Standard base64 (byte-safe)str b = base64_encode(raw);

Guidance:

  • Passwordspassword_hash / password_verify. Never store bare sha256(pw) (unsalted, fast to brute-force).
  • Session / API tokens, saltssecure_token, not randint (which is not cryptographically secure).
  • Signing / authentication (signed cookies, webhook signatures, JWT-style tokens) → hmac_sha256 with a long random secret. Verify by recomputing the MAC and comparing.

hmac_sha256 is what the wings_jwt package uses to issue and verify HS256 session tokens:

import "wings_jwt" as jwt;
str token = jwt.sign_ttl({"sub": "42", "role": "admin"}, SECRET, 3600);
json v = jwt.verify(token, SECRET); // {"ok":1,"claims":{…}} | {"ok":0,"error":…}
FunctionDescriptionExample
csv_parse(s)RFC 4180 → array of rowsarray rows = csv_parse(text);
csv_emit(rows)Array of rows → CSV stringstr s = csv_emit(rows);
FunctionDescriptionExample
env(name)Read env var (empty if unset)str dbg = env("DEBUG");
exit(code)Terminate with statusexit(1);
FunctionDescriptionExample
arena_save()Snapshot current arena tipint wm = arena_save();
arena_restore(wm)Roll back allocs to that tiparena_restore(wm);

Wings auto-uses these per request so long-running servers don’t grow memory. Handlers MUST NOT stash arena pointers on globals.

FunctionDescriptionExample
thread_create(fn, arg)Spawn thread; returns thread idint t = thread_create(worker, x);
thread_join(id)Wait for thread to finishthread_join(t);
thread_detach(id)Detach (auto-cleanup on exit)thread_detach(t);
mutex_create()Create a mutexint m = mutex_create();
mutex_lock(m)Acquire lockmutex_lock(m);
mutex_unlock(m)Release lockmutex_unlock(m);
mutex_destroy(m)Free a mutexmutex_destroy(m);

http_* builtins are documented separately in HTTP Server (Wings) and HTTP Client.